From c147600152b262718f40530979615207c21a3b47 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Mon, 6 May 2024 10:43:51 -0700 Subject: [PATCH] fix: dont set _contentLength if not in headers The opposite of #366. This is not a change in behavior but makes it clear that we don't expect _contentLength to be set when the request is coming directly from the registry when there is no cache. --- lib/registry.js | 5 ++++- test/registry.js | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/registry.js b/lib/registry.js index 3dc2ccc0..f2b5a8a5 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -95,7 +95,10 @@ class RegistryFetcher extends Fetcher { integrity: null, }) const packument = await res.json() - packument._contentLength = +res.headers.get('content-length') + const contentLength = res.headers.get('content-length') + if (contentLength) { + packument._contentLength = Number(contentLength) + } this.packumentCache?.set(this.packumentUrl, packument) return packument } catch (err) { diff --git a/test/registry.js b/test/registry.js index 7037f802..99c12185 100644 --- a/test/registry.js +++ b/test/registry.js @@ -1341,3 +1341,9 @@ t.test('option replaceRegistryHost', rhTest => { rhTest.end() }) + +t.test('packument contentLength from registry', async t => { + const f = new RegistryFetcher('underscore', { registry, cache }) + const p = await f.packument() + t.equal(p._contentLength, undefined, 'content length is undefined from registry requests') +})