@vltpkg/registry-client 1.6.1 → 1.6.2

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +33 -0
  2. package/dist/index.js +101 -24
  3. package/package.json +10 -10
package/dist/index.d.ts CHANGED
@@ -29,6 +29,22 @@ export type CachedBody = {
29
29
  /** the body on disk is gzipped (`body` may be un-gzipped) */
30
30
  gzip: boolean;
31
31
  };
32
+ /**
33
+ * A valid cached tarball found by {@link RegistryClient.cachedTarball}.
34
+ * Only the head was read: the body is still on disk.
35
+ */
36
+ export type CachedTarball = {
37
+ /** the cache file it is in */
38
+ path: string;
39
+ /** the cache key of the request */
40
+ key: string;
41
+ /** the size of the body on disk, in bytes */
42
+ bytes: number;
43
+ /** the hash the entry was stored under, if it has one */
44
+ integrity?: Integrity;
45
+ /** the body on disk is gzipped */
46
+ gzip: boolean;
47
+ };
32
48
  export type CacheableMethod = 'GET' | 'HEAD';
33
49
  export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
34
50
  export declare const cacheKey: (method: string, url: URL | string) => string;
@@ -239,6 +255,23 @@ export declare class RegistryClient {
239
255
  integrity?: Integrity;
240
256
  method?: CacheableMethod;
241
257
  }): CachedBody | undefined;
258
+ /**
259
+ * Find a valid cached non-JSON body on disk without reading it: only
260
+ * the head is read, to check the entry. Bypasses the in-memory cache
261
+ * LRU like {@link cachedBody}.
262
+ *
263
+ * The body's offset is not returned. `cache-unzip` can rewrite the
264
+ * entry, and its head, before the body is read, so whoever reads the
265
+ * body takes the head length from the same bytes (see `@vltpkg/tar`'s
266
+ * `unpackCacheEntry`). A rewrite keeps the same tarball, un-gzipped.
267
+ *
268
+ * Does not log a cache hit. Returns undefined on anything unexpected;
269
+ * callers fall back to {@link request}.
270
+ */
271
+ cachedTarball(url: URL | string, options?: {
272
+ integrity?: Integrity;
273
+ method?: CacheableMethod;
274
+ }): CachedTarball | undefined;
242
275
  /**
243
276
  * Queue the cached tarball at `key` (a {@link CachedBody} key) for the
244
277
  * background child: exploded into the global store when that is on,
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 { closeSync, openSync, readFileSync, readSync } from 'node:fs';
9
+ import { closeSync, fstatSync, openSync, readFileSync, readSync, } from 'node:fs';
10
10
  import { availableParallelism } from 'node:os';
11
11
  import { dirname, resolve } from 'node:path';
12
12
  import { setMaxListeners } from 'node:events';
@@ -467,30 +467,10 @@ export class RegistryClient {
467
467
  cachedBody(url, options = {}) {
468
468
  const { integrity, method = 'GET' } = options;
469
469
  try {
470
- // normalize like request() does, or a url that is not already in
471
- // canonical form (an explicit :443, say) keys a different file
472
- // and the fast path silently never hits.
473
- const key = cacheKey(method, typeof url === 'string' ? new URL(url) : url);
474
- // an in-memory value can be newer than disk, since cache.set
475
- // writes to disk asynchronously. let request() serve those.
476
- // (an in-flight fetch() peeks as undefined, so the probe reads
477
- // disk underneath it -- benign, tarball urls are immutable.)
478
- if (this.cache.peek(key))
470
+ const found = this.#cachedPaths(url, method, integrity);
471
+ if (!found)
479
472
  return undefined;
480
- // a pin reads only the integrity path: cache.set hardlinks every
481
- // artifact under its hash, while the key holds whatever was last
482
- // fetched from that url. nothing here hashes the body, so a miss
483
- // -- which sends the caller to request(), which verifies -- is
484
- // the safe answer.
485
- const paths = new Set();
486
- try {
487
- const i = this.cache.integrityPath(integrity);
488
- if (i)
489
- paths.add(i);
490
- }
491
- catch { }
492
- if (!integrity)
493
- paths.add(this.cache.path(key));
473
+ const { key, paths } = found;
494
474
  for (const path of paths) {
495
475
  let buf;
496
476
  try {
@@ -522,6 +502,103 @@ export class RegistryClient {
522
502
  catch { }
523
503
  return undefined;
524
504
  }
505
+ /**
506
+ * Find a valid cached non-JSON body on disk without reading it: only
507
+ * the head is read, to check the entry. Bypasses the in-memory cache
508
+ * LRU like {@link cachedBody}.
509
+ *
510
+ * The body's offset is not returned. `cache-unzip` can rewrite the
511
+ * entry, and its head, before the body is read, so whoever reads the
512
+ * body takes the head length from the same bytes (see `@vltpkg/tar`'s
513
+ * `unpackCacheEntry`). A rewrite keeps the same tarball, un-gzipped.
514
+ *
515
+ * Does not log a cache hit. Returns undefined on anything unexpected;
516
+ * callers fall back to {@link request}.
517
+ */
518
+ cachedTarball(url, options = {}) {
519
+ const { integrity, method = 'GET' } = options;
520
+ try {
521
+ const found = this.#cachedPaths(url, method, integrity);
522
+ if (!found)
523
+ return undefined;
524
+ const { key, paths } = found;
525
+ for (const path of paths) {
526
+ let fd;
527
+ try {
528
+ fd = openSync(path, 'r');
529
+ }
530
+ catch {
531
+ continue;
532
+ }
533
+ try {
534
+ const size = Buffer.allocUnsafe(4);
535
+ if (readSync(fd, size, 0, 4, 0) < 4)
536
+ continue;
537
+ const headSize = size.readUInt32BE(0);
538
+ // Reject a length we do not believe, so a corrupt file cannot
539
+ // make us allocate a huge buffer.
540
+ if (headSize < 7 || headSize > maxHeadSize)
541
+ continue;
542
+ // two more bytes than the head, to see whether the body is
543
+ // gzipped
544
+ const head = Buffer.allocUnsafe(headSize + 2);
545
+ const read = readSync(fd, head, 0, headSize + 2, 0);
546
+ if (read < headSize)
547
+ continue;
548
+ const entry = CacheEntry.decodeHead(head.subarray(0, read));
549
+ // statusCode must stay first: see cachedBody
550
+ if (entry.statusCode !== 200 ||
551
+ !entry.valid ||
552
+ entry.isJSON) {
553
+ continue;
554
+ }
555
+ return {
556
+ path,
557
+ key,
558
+ bytes: fstatSync(fd).size - headSize,
559
+ integrity: entry.integrity,
560
+ gzip: CacheEntry.isGzipEntry(head.subarray(0, read)),
561
+ };
562
+ }
563
+ finally {
564
+ closeSync(fd);
565
+ }
566
+ }
567
+ /* c8 ignore next */
568
+ }
569
+ catch { }
570
+ return undefined;
571
+ }
572
+ // The cache files that may hold a body for `url`, or undefined when
573
+ // the in-memory cache should answer instead. Shared by cachedBody and
574
+ // cachedTarball.
575
+ #cachedPaths(url, method, integrity) {
576
+ // normalize like request() does, or a url that is not already in
577
+ // canonical form (an explicit :443, say) keys a different file
578
+ // and the fast path silently never hits.
579
+ const key = cacheKey(method, typeof url === 'string' ? new URL(url) : url);
580
+ // an in-memory value can be newer than disk, since cache.set
581
+ // writes to disk asynchronously. let request() serve those.
582
+ // (an in-flight fetch() peeks as undefined, so the probe reads
583
+ // disk underneath it -- benign, tarball urls are immutable.)
584
+ if (this.cache.peek(key))
585
+ return undefined;
586
+ // a pin reads only the integrity path: cache.set hardlinks every
587
+ // artifact under its hash, while the key holds whatever was last
588
+ // fetched from that url. nothing here hashes the body, so a miss
589
+ // -- which sends the caller to request(), which verifies -- is
590
+ // the safe answer.
591
+ const paths = new Set();
592
+ try {
593
+ const i = this.cache.integrityPath(integrity);
594
+ if (i)
595
+ paths.add(i);
596
+ }
597
+ catch { }
598
+ if (!integrity)
599
+ paths.add(this.cache.path(key));
600
+ return { key, paths };
601
+ }
525
602
  /**
526
603
  * Queue the cached tarball at `key` (a {@link CachedBody} key) for the
527
604
  * background child: exploded into the global store when that is on,
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.6.1",
4
+ "version": "1.6.2",
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.6.1",
17
- "@vltpkg/cache-unzip": "1.6.1",
18
- "@vltpkg/error-cause": "1.6.1",
19
- "@vltpkg/keychain": "1.6.1",
20
- "@vltpkg/output": "1.6.1",
21
- "@vltpkg/types": "1.6.1",
22
- "@vltpkg/url-open": "1.6.1",
23
- "@vltpkg/user-agent": "1.6.1",
24
- "@vltpkg/xdg": "1.6.1",
16
+ "@vltpkg/cache": "1.6.2",
17
+ "@vltpkg/cache-unzip": "1.6.2",
18
+ "@vltpkg/error-cause": "1.6.2",
19
+ "@vltpkg/keychain": "1.6.2",
20
+ "@vltpkg/output": "1.6.2",
21
+ "@vltpkg/types": "1.6.2",
22
+ "@vltpkg/url-open": "1.6.2",
23
+ "@vltpkg/user-agent": "1.6.2",
24
+ "@vltpkg/xdg": "1.6.2",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },