@vltpkg/registry-client 1.2.0 → 1.3.0

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.js CHANGED
@@ -7,7 +7,6 @@ import { urlOpen } from '@vltpkg/url-open';
7
7
  import { XDG } from '@vltpkg/xdg';
8
8
  import { randomUUID } from 'node:crypto';
9
9
  import { readFileSync } from 'node:fs';
10
- import { STATUS_CODES } from 'node:http';
11
10
  import { availableParallelism } from 'node:os';
12
11
  import { dirname, resolve } from 'node:path';
13
12
  import { setTimeout } from 'node:timers/promises';
@@ -22,7 +21,7 @@ import { handleCacheHitResponse } from "./handle-304-response.js";
22
21
  import { otplease } from "./otplease.js";
23
22
  import { getDispatcher } from "./proxy.js";
24
23
  import { isRedirect, redirect } from "./redirect.js";
25
- import { assertOk, registryErrorMessage, requestError, } from "./registry-error.js";
24
+ import { assertOk, registryErrorMessage, requestError, statusText, } from "./registry-error.js";
26
25
  import { setCacheHeaders } from "./set-cache-headers.js";
27
26
  import { getTokenResponse } from "./token-response.js";
28
27
  import { getWebAuthChallenge } from "./web-auth-challenge.js";
@@ -140,7 +139,7 @@ export class RegistryClient {
140
139
  catch {
141
140
  // Registries and proxies can return plain text or HTML errors.
142
141
  }
143
- const status = STATUS_CODES[statusCode];
142
+ const status = statusText(statusCode);
144
143
  throw error(`Failed to fetch paginated results: ${statusCode}${status ? ` ${status}` : ''}${typeof detail === 'string' && detail ? `: ${detail}` : ''}`, { code: 'EREQUEST', status: statusCode, url: String(url) });
145
144
  }
146
145
  let body;
@@ -286,7 +285,7 @@ export class RegistryClient {
286
285
  }
287
286
  // TODO: fall back to username/password login, and/or couchdb PUT login
288
287
  const { statusCode } = response;
289
- const status = STATUS_CODES[statusCode];
288
+ const status = statusText(statusCode);
290
289
  throw error(`Failed to perform web login: ${statusCode}${status ? ` ${status}` : ''}`, {
291
290
  code: 'EREQUEST',
292
291
  url: webLoginURL,
@@ -405,7 +404,11 @@ export class RegistryClient {
405
404
  // disk underneath it -- benign, tarball urls are immutable.)
406
405
  if (this.cache.peek(key))
407
406
  return undefined;
408
- // same order as the cache's own disk read
407
+ // a pin reads only the integrity path: cache.set hardlinks every
408
+ // artifact under its hash, while the key holds whatever was last
409
+ // fetched from that url. nothing here hashes the body, so a miss
410
+ // -- which sends the caller to request(), which verifies -- is
411
+ // the safe answer.
409
412
  const paths = new Set();
410
413
  try {
411
414
  const i = this.cache.integrityPath(integrity);
@@ -413,7 +416,8 @@ export class RegistryClient {
413
416
  paths.add(i);
414
417
  }
415
418
  catch { }
416
- paths.add(this.cache.path(key));
419
+ if (!integrity)
420
+ paths.add(this.cache.path(key));
417
421
  for (const path of paths) {
418
422
  let buf;
419
423
  try {
@@ -549,9 +553,21 @@ export class RegistryClient {
549
553
  // a server-sent integrity header is not evidence: only the caller's
550
554
  // expectation, or a body read back from the cache, is trusted. the
551
555
  // header is dropped so it can never be stored as the entry's hash.
556
+ //
557
+ // the check has to happen here, before the cache write below, or a
558
+ // body that fails it is still stored under the hash it was supposed
559
+ // to have -- and served from there, unverified, on the next run.
560
+ // `!fromCache` is the whole precondition: only a cached entry can
561
+ // have been rewritten (cache-unzip un-gzips in place), so anything
562
+ // else is the bytes as they came off the wire and hashes as such.
563
+ // this used to also require `isGzip`, which was a proxy for that
564
+ // and silently exempted every artifact that is not gzip -- a
565
+ // `.tar.br` among them.
552
566
  if (!trustIntegrity && !result.fromCache) {
553
567
  result.deleteHeader('integrity');
554
- if (result.isGzip)
568
+ // 200 only: an error body is not the artifact and was never
569
+ // supposed to hash to it, and it is not what gets cached either.
570
+ if (result.statusCode === 200)
555
571
  result.checkIntegrity({ url });
556
572
  }
557
573
  // same for the digest the server labels an unlabelled artifact with.
@@ -11,6 +11,7 @@ export type ErrorResponse = {
11
11
  text?: () => string;
12
12
  [k: number | string | symbol]: any;
13
13
  };
14
+ export declare const statusText: (code: number) => string | undefined;
14
15
  /**
15
16
  * Format a non-2xx registry response as `"403 Forbidden — <detail>"`,
16
17
  * where the detail is whatever the registry actually said.
@@ -1,6 +1,7 @@
1
1
  import { error } from '@vltpkg/error-cause';
2
2
  import { asError, isObject } from '@vltpkg/types';
3
- import { STATUS_CODES } from 'node:http';
3
+ // read on use: a static node:http import loads undici, http2, tls at startup
4
+ export const statusText = (code) => process.getBuiltinModule('node:http').STATUS_CODES[code];
4
5
  /** longest registry-supplied detail spliced into an error message */
5
6
  const MAX_DETAIL_LENGTH = 512;
6
7
  const truncate = (s) => s.length <= MAX_DETAIL_LENGTH ?
@@ -50,7 +51,7 @@ const detailFromJSON = (text) => {
50
51
  * decoded, and to the raw body text when it is not JSON.
51
52
  */
52
53
  export const registryErrorMessage = (response) => {
53
- const statusMessage = STATUS_CODES[response.statusCode];
54
+ const statusMessage = statusText(response.statusCode);
54
55
  const status = `${response.statusCode}${statusMessage ? ` ${statusMessage}` : ''}`;
55
56
  let text;
56
57
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/registry-client",
3
3
  "description": "Fetch package artifacts and metadata from registries",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,15 +13,15 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/cache": "1.2.0",
17
- "@vltpkg/cache-unzip": "1.2.0",
18
- "@vltpkg/error-cause": "1.2.0",
19
- "@vltpkg/keychain": "1.2.0",
20
- "@vltpkg/output": "1.2.0",
21
- "@vltpkg/types": "1.2.0",
22
- "@vltpkg/url-open": "1.2.0",
23
- "@vltpkg/user-agent": "1.2.0",
24
- "@vltpkg/xdg": "1.2.0",
16
+ "@vltpkg/cache": "1.3.0",
17
+ "@vltpkg/cache-unzip": "1.3.0",
18
+ "@vltpkg/error-cause": "1.3.0",
19
+ "@vltpkg/keychain": "1.3.0",
20
+ "@vltpkg/output": "1.3.0",
21
+ "@vltpkg/types": "1.3.0",
22
+ "@vltpkg/url-open": "1.3.0",
23
+ "@vltpkg/user-agent": "1.3.0",
24
+ "@vltpkg/xdg": "1.3.0",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },