Skip to content

Commit

Permalink
fix: wait for size event from body for contentLength
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed May 6, 2024
1 parent 005d8a9 commit 0cef7a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const ssri = require('ssri')
const crypto = require('crypto')
const npa = require('npm-package-arg')
const sigstore = require('sigstore')
const { isStream } = require('minipass')
const { once } = require('node:stream')

// Corgis are cute. 🐕🐶
const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
Expand Down Expand Up @@ -96,7 +98,11 @@ class RegistryFetcher extends Fetcher {
integrity: null,
})
const packument = await res.json()
packument._contentLength = +res.headers.get('content-length')
let contentLength = res.headers.get('content-length') ?? null
if (contentLength === null && isStream(res.body)) {
contentLength = await once(res.body, 'size').then(r => r[0])
}
packument._contentLength = +contentLength
if (this.packumentCache) {
this.packumentCache.set(this.packumentUrl, packument)
}
Expand Down
5 changes: 5 additions & 0 deletions test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,11 @@ t.test('a manifest that lacks integrity', async t => {
t.equal(await f.packument(), await f2.packument(), 'serve cached packument')
})

t.test('packument has contentLength', async t => {
const f = new RegistryFetcher('underscore', { registry, cache })
t.match(await f.packument(), { _contentLength: 40966 }, 'got contentLength from body')
})

t.test('packument that has been cached', async t => {
const packumentUrl = `${registry}asdf`
const packument = {
Expand Down

0 comments on commit 0cef7a9

Please sign in to comment.