Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait for size event from body for contentLength #366

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ?? effectively the same test as isStream? Do we really need them both?

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
Loading