@vltpkg/registry-client 1.3.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.
@@ -184,6 +184,11 @@ export declare class CacheEntry {
184
184
  * i.e. a tarball the global store can be built from.
185
185
  */
186
186
  static isTarballEntry(buffer: Uint8Array): boolean;
187
+ /**
188
+ * The hash recorded on such an entry, or undefined if this is not one.
189
+ * {@link isTarballEntry} is built on this, and both only need the head.
190
+ */
191
+ static tarballIntegrity(buffer: Uint8Array): Integrity | undefined;
187
192
  /**
188
193
  * Encode status + headers (no body) as they appear at the start of
189
194
  * an on-disk cache file.
@@ -599,11 +599,21 @@ export class CacheEntry {
599
599
  * i.e. a tarball the global store can be built from.
600
600
  */
601
601
  static isTarballEntry(buffer) {
602
+ return !!CacheEntry.tarballIntegrity(buffer);
603
+ }
604
+ /**
605
+ * The hash recorded on such an entry, or undefined if this is not one.
606
+ * {@link isTarballEntry} is built on this, and both only need the head.
607
+ */
608
+ static tarballIntegrity(buffer) {
602
609
  const parsed = CacheEntry.#parseHead(buffer);
603
- if (parsed?.statusCode !== 200 || !parsed.integrity)
604
- return false;
610
+ if (parsed?.statusCode !== 200 || !parsed.integrity) {
611
+ return undefined;
612
+ }
605
613
  const ct = getRawHeader(parsed.headers, 'content-type');
606
- return !ct || !/\bjson\b/.test(getDecodedValue(ct));
614
+ return !ct || !/\bjson\b/.test(getDecodedValue(ct)) ?
615
+ parsed.integrity
616
+ : undefined;
607
617
  }
608
618
  /**
609
619
  * Encode status + headers (no body) as they appear at the start of
package/dist/index.d.ts CHANGED
@@ -185,6 +185,21 @@ export declare class RegistryClient {
185
185
  * hang on the `doneUrl` until it returns a {@link TokenResponse} object.
186
186
  */
187
187
  webAuthOpener({ doneUrl, authUrl }: WebAuthChallenge): Promise<TokenResponse>;
188
+ /**
189
+ * The hash the cache recorded for `url`. This reads only the entry's
190
+ * head, which is about a kilobyte, rather than the body, which can be a
191
+ * megabyte. A caller that is about to look the package up in the global
192
+ * store needs nothing else, and if the store misses it can still read
193
+ * the body afterwards.
194
+ *
195
+ * Returns undefined if there is no entry, or if the entry is not a
196
+ * tarball. This never reads the body, so if `cache-unzip` rewrote the
197
+ * entry underneath us the worst case is a stale hash. That misses in the
198
+ * store; it cannot return the wrong bytes.
199
+ */
200
+ cachedIntegrity(url: URL | string, options?: {
201
+ method?: CacheableMethod;
202
+ }): Integrity | undefined;
188
203
  /**
189
204
  * Read a valid cached non-JSON body straight off disk, bypassing the
190
205
  * in-memory cache LRU so that a tarball is never held in it.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { logRequest } from '@vltpkg/output';
6
6
  import { urlOpen } from '@vltpkg/url-open';
7
7
  import { XDG } from '@vltpkg/xdg';
8
8
  import { randomUUID } from 'node:crypto';
9
- import { readFileSync } from 'node:fs';
9
+ import { closeSync, openSync, readFileSync, readSync } from 'node:fs';
10
10
  import { availableParallelism } from 'node:os';
11
11
  import { dirname, resolve } from 'node:path';
12
12
  import { setTimeout } from 'node:timers/promises';
@@ -32,6 +32,9 @@ import { oidc } from "./oidc.js";
32
32
  const log = (msg) => console.error(msg);
33
33
  export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
34
34
  export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
35
+ // A head is about a kilobyte. This limit exists only so that a corrupt
36
+ // length cannot make us allocate something enormous.
37
+ const maxHeadSize = 1024 * 1024;
35
38
  export const cacheKey = (method, url) => `${method !== 'GET' ? method + ' ' : ''}${url}`;
36
39
  /**
37
40
  * The `User-Agent` header sent with every request. Re-exported from
@@ -55,12 +58,13 @@ export { userAgent };
55
58
  // CDN throttling beyond what reify can consume.
56
59
  // HTTP/2 (allowH2) is untested and unjustified while transport CPU is ~1/4
57
60
  // of body handling.
61
+ // keepAliveTimeoutThreshold: undici default. Values >= the server's
62
+ // Keep-Alive hint (Node http: timeout=5) reset the socket every response.
58
63
  const agentOptions = {
59
64
  bodyTimeout: 600_000,
60
65
  headersTimeout: 600_000,
61
66
  keepAliveMaxTimeout: 1_200_000,
62
67
  keepAliveTimeout: 600_000,
63
- keepAliveTimeoutThreshold: 30_000,
64
68
  connect: {
65
69
  timeout: 600_000,
66
70
  keepAlive: true,
@@ -377,6 +381,50 @@ export class RegistryClient {
377
381
  response,
378
382
  });
379
383
  }
384
+ /**
385
+ * The hash the cache recorded for `url`. This reads only the entry's
386
+ * head, which is about a kilobyte, rather than the body, which can be a
387
+ * megabyte. A caller that is about to look the package up in the global
388
+ * store needs nothing else, and if the store misses it can still read
389
+ * the body afterwards.
390
+ *
391
+ * Returns undefined if there is no entry, or if the entry is not a
392
+ * tarball. This never reads the body, so if `cache-unzip` rewrote the
393
+ * entry underneath us the worst case is a stale hash. That misses in the
394
+ * store; it cannot return the wrong bytes.
395
+ */
396
+ cachedIntegrity(url, options = {}) {
397
+ const { method = 'GET' } = options;
398
+ try {
399
+ const key = cacheKey(method, typeof url === 'string' ? new URL(url) : url);
400
+ // The in-memory copy can be newer than the file on disk, so let
401
+ // request() serve those. Same rule as cachedBody.
402
+ if (this.cache.peek(key))
403
+ return undefined;
404
+ const fd = openSync(this.cache.path(key), 'r');
405
+ try {
406
+ const size = Buffer.allocUnsafe(4);
407
+ if (readSync(fd, size, 0, 4, 0) < 4)
408
+ return undefined;
409
+ const headSize = size.readUInt32BE(0);
410
+ // Reject a length we do not believe, so a corrupt file cannot
411
+ // make us allocate a huge buffer.
412
+ if (headSize < 7 || headSize > maxHeadSize)
413
+ return undefined;
414
+ const head = Buffer.allocUnsafe(headSize);
415
+ if (readSync(fd, head, 0, headSize, 0) < headSize) {
416
+ return undefined;
417
+ }
418
+ return CacheEntry.tarballIntegrity(head);
419
+ }
420
+ finally {
421
+ closeSync(fd);
422
+ }
423
+ /* c8 ignore next */
424
+ }
425
+ catch { }
426
+ return undefined;
427
+ }
380
428
  /**
381
429
  * Read a valid cached non-JSON body straight off disk, bypassing the
382
430
  * in-memory cache LRU so that a tarball is never held in it.
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.3.0",
4
+ "version": "1.3.1",
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.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",
16
+ "@vltpkg/cache": "1.3.1",
17
+ "@vltpkg/cache-unzip": "1.3.1",
18
+ "@vltpkg/error-cause": "1.3.1",
19
+ "@vltpkg/keychain": "1.3.1",
20
+ "@vltpkg/output": "1.3.1",
21
+ "@vltpkg/types": "1.3.1",
22
+ "@vltpkg/url-open": "1.3.1",
23
+ "@vltpkg/user-agent": "1.3.1",
24
+ "@vltpkg/xdg": "1.3.1",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },