@vltpkg/package-info 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -67,6 +67,12 @@ export type PackageInfoClientOptions = RegistryClientOptions & SpecOptions & {
67
67
  * tarball, anything else goes through the global store first.
68
68
  */
69
69
  'store-linker'?: StoreLinker;
70
+ /**
71
+ * Resolve to the registry's Brotli (`.tar.br`) tarball when it
72
+ * advertises one in `dist.alternates`. Defaults to true; set false to
73
+ * always resolve to the gzip `.tgz`.
74
+ */
75
+ 'brotli-tarballs'?: boolean;
70
76
  };
71
77
  export type PackageInfoClientRequestOptions = PickManifestOptions & RegistryClientRequestOptions & {
72
78
  /** dir to resolve `file://` specifiers against. Defaults to projectRoot. */
@@ -84,8 +90,8 @@ export type PackageInfoClientExtractOptions = PackageInfoClientRequestOptions &
84
90
  /**
85
91
  * When true, indicates that integrity + resolved came from a
86
92
  * lockfile (i.e. they were already verified on first install).
87
- * Skips the client-side tarball integrity check.
88
- * Defaults to false — fresh installs always verify integrity.
93
+ * Skips re-hashing a refetched body of an already verified url.
94
+ * Other network bodies are always checked.
89
95
  */
90
96
  fromLockfile?: boolean;
91
97
  /**
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import { pickManifest } from '@vltpkg/pick-manifest';
8
8
  import { registryErrorMessage, tokenRefusalAdvice, } from '@vltpkg/registry-client/registry-error';
9
9
  import { storeRoot } from '@vltpkg/registry-client/store-root';
10
10
  import { Spec } from '@vltpkg/spec';
11
- import { asPackument, integrityHex } from '@vltpkg/types';
11
+ import { asPackument, brotliTarballUrl, integrityHex, tarballFormat, } from '@vltpkg/types';
12
12
  import ssri from 'ssri';
13
13
  import { Monorepo } from '@vltpkg/workspaces';
14
14
  import { XDG } from '@vltpkg/xdg';
@@ -178,6 +178,19 @@ export class PackageInfoClient {
178
178
  });
179
179
  return this.#tarPoolPromise;
180
180
  }
181
+ /**
182
+ * A version's Brotli (`.tar.br`) alternate: its absolute URL, plus its
183
+ * hash if the registry sent one. Returns undefined if the version has no
184
+ * alternate, if it has one this client does not use (see
185
+ * {@link brotliTarballUrl}), or if `--no-brotli-tarballs` is set.
186
+ * `dist.tarball` is already absolute by this point; see
187
+ * `absolutizeTarballs`.
188
+ */
189
+ #brotliTarball(tarball, alternates) {
190
+ if (this.options['brotli-tarballs'] === false)
191
+ return undefined;
192
+ return brotliTarballUrl(tarball, alternates);
193
+ }
181
194
  constructor(options = {}) {
182
195
  this.options = options;
183
196
  this.#projectRoot = options.projectRoot || process.cwd();
@@ -210,10 +223,19 @@ export class PackageInfoClient {
210
223
  const { from = this.#projectRoot, integrity, resolved, fromLockfile = false, installScripts = false, } = options;
211
224
  const f = spec.final;
212
225
  // If the caller already provides both integrity and resolved
213
- // (from lockfile or prior resolution), skip re-resolving.
214
- const alreadyResolved = !!(integrity && resolved);
215
- const r = alreadyResolved ?
216
- { resolved, integrity, spec }
226
+ // (from lockfile or prior resolution), skip re-resolving. A
227
+ // `.tar.br` needs only the URL: its hash is not knowable before the
228
+ // download, so the graph can never hand one over, and `Repr-Digest`
229
+ // is what pins it -- `required`, so a registry that serves the
230
+ // alternate unlabelled is rejected rather than trusted.
231
+ const brotli = !!resolved && tarballFormat(resolved) === 'brotli';
232
+ const r = resolved && (integrity || brotli) ?
233
+ {
234
+ resolved,
235
+ integrity,
236
+ spec,
237
+ ...(brotli && !integrity ? { digestRequired: true } : {}),
238
+ }
217
239
  : await this.resolve(spec, options);
218
240
  switch (f.type) {
219
241
  case 'git': {
@@ -244,11 +266,21 @@ export class PackageInfoClient {
244
266
  }
245
267
  case 'registry': {
246
268
  const pool = await this.getTarPool();
269
+ // brotli bytes carry no signature of their own, so every unpack
270
+ // below has to be told; gzip and raw tar are sniffed.
271
+ const format = tarballFormat(r.resolved);
247
272
  // git tarballs keep the unpack path
248
- const hex = f.type === 'registry' && this.#storeLinker !== 'unpack' ?
249
- integrityHex(r.integrity)
250
- : undefined;
273
+ const storeOn = f.type === 'registry' && this.#storeLinker !== 'unpack';
251
274
  const copy = this.#storeLinker === 'copy' || installScripts;
275
+ // If we already have a hash, that is the store key. Some
276
+ // tarballs arrive without one: a `.tar.br`, or anything a
277
+ // registry serves with no `dist.integrity`. For those, read the
278
+ // hash off the cache entry, which recorded it when the bytes
279
+ // were verified. Installs that do have a hash skip all of this,
280
+ // so a store hit still never builds a registry client.
281
+ const hex = !storeOn ? undefined
282
+ : r.integrity ? integrityHex(r.integrity)
283
+ : integrityHex((await this.getRegistryClient()).cachedIntegrity(r.resolved));
252
284
  if (hex) {
253
285
  if (debug.enabled && !this.#storeHitRateLogged) {
254
286
  this.#storeHitRateLogged = true;
@@ -281,13 +313,16 @@ export class PackageInfoClient {
281
313
  });
282
314
  if (cached) {
283
315
  try {
284
- await pool.unpack(cached.body, target);
316
+ await pool.unpack(cached.body, target, format);
285
317
  logRequest(r.resolved, 'cache');
286
318
  r.integrity ??= cached.integrity;
287
- // a warm install writes nothing to the cache, so queue the
288
- // store miss here or an existing cache never converges,
289
- // and a gzipped body with no store link, to unzip it
290
- if (hex || cached.gzip) {
319
+ // A warm install writes nothing to the cache, so queue the
320
+ // store miss here. Without this an existing cache would
321
+ // never catch up. Also queue a gzipped body that did not
322
+ // come from the store, so it gets un-gzipped. A brotli body
323
+ // is left alone: cache-unzip only handles gzip, and keeping
324
+ // it compressed keeps the cache small.
325
+ if (storeOn || cached.gzip) {
291
326
  rc.queueForStore(cached.key, r.integrity);
292
327
  }
293
328
  return r;
@@ -316,24 +351,21 @@ export class PackageInfoClient {
316
351
  response,
317
352
  });
318
353
  }
354
+ // checkIntegrity() hashes the body unless its url is trusted
355
+ const verified = !trustIntegrity &&
356
+ response.checkIntegrity({ spec, url: resolved });
319
357
  // if it's not trusted already, but valid, start trusting
320
- if (!trustIntegrity &&
321
- response.checkIntegrity({ spec, url: resolved })) {
358
+ if (verified) {
322
359
  this.#trustedIntegrities.set(r.resolved, response.integrity);
323
360
  }
324
361
  const buf = response.buffer();
325
362
  if (r.integrity) {
326
- // Verify network-delivered tarball bytes against dist.integrity.
327
- // Skip cache-served bodies: they were verified on the fetch that
328
- // populated the cache, and cache-unzip rewrites them un-gzipped
329
- // so the gzip-hash can never match. Skip lockfile-sourced
330
- // integrity: it was verified on first install.
331
- if (!fromLockfile && !response.fromCache) {
363
+ // hash only network bytes checkIntegrity() skipped (trusted
364
+ // url). cache bodies were verified when cached.
365
+ if (!verified && !fromLockfile && !response.fromCache) {
332
366
  const hash = createHash('sha512');
333
367
  hash.update(buf);
334
368
  const computed = `sha512-${hash.digest('base64')}`;
335
- /* c8 ignore start - defense-in-depth: registry client's
336
- * checkIntegrity() usually catches mismatches first. */
337
369
  if (computed !== r.integrity) {
338
370
  throw error('Tarball integrity check failed', {
339
371
  code: 'EINTEGRITY',
@@ -343,7 +375,6 @@ export class PackageInfoClient {
343
375
  found: computed,
344
376
  });
345
377
  }
346
- /* c8 ignore stop */
347
378
  }
348
379
  }
349
380
  else if (response.fromCache) {
@@ -379,7 +410,7 @@ export class PackageInfoClient {
379
410
  }
380
411
  }
381
412
  try {
382
- await (await this.getTarPool()).unpack(buf, target);
413
+ await pool.unpack(buf, target, format);
383
414
  }
384
415
  catch (er) {
385
416
  throw this.#resolveError(spec, options, 'tar unpack failed', { cause: er });
@@ -568,19 +599,19 @@ export class PackageInfoClient {
568
599
  `version may have been unpublished, or the registry may ` +
569
600
  `be misconfigured or unreachable.`, { response, url: tarball });
570
601
  }
602
+ const verified = !trustIntegrity &&
603
+ response.checkIntegrity({ spec, url: tarball });
571
604
  // if we don't already trust it, but it's valid, start
572
605
  // trusting it
573
- if (!trustIntegrity &&
574
- response.checkIntegrity({ spec, url: tarball })) {
606
+ if (verified) {
575
607
  this.#trustedIntegrities.set(tarball, response.integrity);
576
608
  }
577
609
  const buf = response.buffer();
578
- // Same as extract(): only hash network-delivered bodies.
579
- if (integrity && !response.fromCache) {
610
+ // Same as extract()
611
+ if (integrity && !verified && !response.fromCache) {
580
612
  const hash = createHash('sha512');
581
613
  hash.update(buf);
582
614
  const computed = `sha512-${hash.digest('base64')}`;
583
- /* c8 ignore start - defense-in-depth (see extract) */
584
615
  if (computed !== integrity) {
585
616
  throw error('Tarball integrity check failed', {
586
617
  code: 'EINTEGRITY',
@@ -590,7 +621,6 @@ export class PackageInfoClient {
590
621
  found: computed,
591
622
  });
592
623
  }
593
- /* c8 ignore stop */
594
624
  }
595
625
  return buf;
596
626
  };
@@ -977,15 +1007,31 @@ export class PackageInfoClient {
977
1007
  case 'registry': {
978
1008
  const mani = await this.manifest(spec, options);
979
1009
  if (mani.dist) {
980
- const { integrity, tarball, signatures } = mani.dist;
1010
+ const { integrity, tarball, signatures, alternates } = mani.dist;
981
1011
  if (tarball) {
982
- const r = {
983
- resolved: tarball,
984
- integrity,
985
- signatures,
986
- spec,
987
- };
988
- if (!integrity &&
1012
+ // A `.tar.br` is a distinct artifact, not a re-encoding of the
1013
+ // `.tgz`: it hashes differently, so `dist.integrity` and the
1014
+ // signatures over it describe the wrong bytes and are dropped.
1015
+ // What pins it instead is its own `Repr-Digest`, which the
1016
+ // registry that advertised it always sends -- hence `required`.
1017
+ const brotli = this.#brotliTarball(tarball, alternates);
1018
+ const r = brotli ?
1019
+ {
1020
+ resolved: brotli.tarball,
1021
+ spec,
1022
+ // Pin the alternate's own hash if the registry sent
1023
+ // one. That also means we can look the package up in
1024
+ // the global store before downloading it. If there is
1025
+ // no hash, the Repr-Digest header is the only check we
1026
+ // get, so require it instead of treating it as
1027
+ // optional.
1028
+ ...(brotli.integrity ?
1029
+ { integrity: brotli.integrity }
1030
+ : { digestRequired: true }),
1031
+ }
1032
+ : { resolved: tarball, integrity, signatures, spec };
1033
+ if (!brotli &&
1034
+ !integrity &&
989
1035
  this.#vltPackuments.has(`${f.registry}${f.name}`)) {
990
1036
  r.digestRequired = true;
991
1037
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/package-info",
3
3
  "description": "Resolve and fetch package metadata and tarballs",
4
- "version": "1.2.0",
4
+ "version": "1.3.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,17 +13,17 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.2.0",
17
- "@vltpkg/git": "1.2.0",
18
- "@vltpkg/output": "1.2.0",
19
- "@vltpkg/package-json": "1.2.0",
20
- "@vltpkg/pick-manifest": "1.2.0",
21
- "@vltpkg/registry-client": "1.2.0",
22
- "@vltpkg/spec": "1.2.0",
23
- "@vltpkg/tar": "1.2.0",
24
- "@vltpkg/types": "1.2.0",
25
- "@vltpkg/workspaces": "1.2.0",
26
- "@vltpkg/xdg": "1.2.0",
16
+ "@vltpkg/error-cause": "1.3.1",
17
+ "@vltpkg/git": "1.3.1",
18
+ "@vltpkg/output": "1.3.1",
19
+ "@vltpkg/package-json": "1.3.1",
20
+ "@vltpkg/pick-manifest": "1.3.1",
21
+ "@vltpkg/registry-client": "1.3.1",
22
+ "@vltpkg/spec": "1.3.1",
23
+ "@vltpkg/tar": "1.3.1",
24
+ "@vltpkg/types": "1.3.1",
25
+ "@vltpkg/workspaces": "1.3.1",
26
+ "@vltpkg/xdg": "1.3.1",
27
27
  "ssri": "^13.0.0",
28
28
  "tar": "^7.5.2"
29
29
  },
@@ -32,8 +32,8 @@
32
32
  "@types/node": "^22.19.2",
33
33
  "@types/pacote": "^11.1.8",
34
34
  "@vltpkg/benchmark": "0.0.0",
35
- "@vltpkg/cache-unzip": "1.2.0",
36
- "@vltpkg/vlt-json": "1.2.0",
35
+ "@vltpkg/cache-unzip": "1.3.1",
36
+ "@vltpkg/vlt-json": "1.3.1",
37
37
  "eslint": "^9.39.1",
38
38
  "pacote": "^21.0.4",
39
39
  "prettier": "^3.7.4",