@vltpkg/registry-client 0.0.0-7 → 0.0.0-9

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.
@@ -1,5 +1,6 @@
1
- import type { ErrorCauseObject } from '@vltpkg/error-cause';
1
+ import type { ErrorCauseOptions } from '@vltpkg/error-cause';
2
2
  import type { Integrity, JSONField } from '@vltpkg/types';
3
+ import ccp from 'cache-control-parser';
3
4
  import type { InspectOptions } from 'node:util';
4
5
  export type JSONObj = Record<string, JSONField>;
5
6
  declare const kCustomInspect: unique symbol;
@@ -17,15 +18,35 @@ export type CacheEntryOptions = {
17
18
  * artifact request.
18
19
  */
19
20
  trustIntegrity?: boolean;
21
+ /**
22
+ * If the server does not serve a `stale-while-revalidate` value in the
23
+ * `cache-control` header, then this multiplier is applied to the `max-age`
24
+ * or `s-maxage` values.
25
+ *
26
+ * By default, this is `60`, so for example a response that is cacheable for
27
+ * 5 minutes will allow a stale response while revalidating for up to 5
28
+ * hours.
29
+ *
30
+ * If the server *does* provide a `stale-while-revalidate` value, then that
31
+ * is always used.
32
+ *
33
+ * Set to 0 to prevent any `stale-while-revalidate` behavior unless
34
+ * explicitly allowed by the server's `cache-control` header.
35
+ */
36
+ 'stale-while-revalidate-factor'?: number;
20
37
  };
21
38
  export declare class CacheEntry {
22
39
  #private;
23
- constructor(statusCode: number, headers: Buffer[], { integrity, trustIntegrity }?: CacheEntryOptions);
40
+ constructor(statusCode: number, headers: Buffer[], { integrity, trustIntegrity, 'stale-while-revalidate-factor': staleWhileRevalidateFactor, }?: CacheEntryOptions);
41
+ toJSON(): {
42
+ [k: string]: string | number | boolean | [string, string][] | Date | ccp.CacheControl | undefined;
43
+ };
24
44
  [kCustomInspect](depth: number, options: InspectOptions): string;
25
- /**
26
- * `true` if the entry represents a cached response that is still
27
- * valid to use.
28
- */
45
+ get date(): Date | undefined;
46
+ get maxAge(): number;
47
+ get cacheControl(): ccp.CacheControl;
48
+ get staleWhileRevalidate(): boolean;
49
+ get contentType(): string;
29
50
  get valid(): boolean;
30
51
  addBody(b: Buffer): void;
31
52
  get statusCode(): number;
@@ -43,7 +64,7 @@ export declare class CacheEntry {
43
64
  *
44
65
  * Returns true if anything was actually verified.
45
66
  */
46
- checkIntegrity(context?: ErrorCauseObject): this is CacheEntry & {
67
+ checkIntegrity(context?: ErrorCauseOptions): this is CacheEntry & {
47
68
  integrity: Integrity;
48
69
  };
49
70
  get integrityActual(): Integrity;
@@ -1 +1 @@
1
- {"version":3,"file":"cache-entry.d.ts","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAE3D,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAGzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAK/C,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAyB/C,QAAA,MAAM,cAAc,eAA2C,CAAA;AAE/D,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB;;;;;;;OAOG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,qBAAa,UAAU;;gBAWnB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,EACjB,EAAE,SAAS,EAAE,cAAsB,EAAE,GAAE,iBAAsB;IAkB/D,CAAC,cAAc,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,MAAM;IAuBhE;;;OAGG;IACH,IAAI,KAAK,IAAI,OAAO,CAsBnB;IAED,OAAO,CAAC,CAAC,EAAE,MAAM;IAKjB,IAAI,UAAU,WAEb;IACD,IAAI,OAAO,8BAEV;IAED;;;;;;;;;;;;OAYG;IACH,cAAc,CACZ,OAAO,GAAE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,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAI3C;;OAEG;IACH,MAAM,IAAI,MAAM;IAWhB,IAAI,IAAI,IAAI,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAEvC;IAGD,IAAI,MAAM,IAAI,OAAO,CAYpB;IAGD,IAAI,MAAM,IAAI,OAAO,CAcpB;IAED;;;;OAIG;IACH,KAAK;IAmBL;;;OAGG;IACH,IAAI;IAKJ;;OAEG;IACH,IAAI,IAAI,OAAO;IAQf;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IAsDzC,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAO3C;;OAEG;IAGH,MAAM,IAAI,MAAM;CAmCjB"}
@@ -52,10 +52,12 @@ export class CacheEntry {
52
52
  #integrityActual;
53
53
  #json;
54
54
  #trustIntegrity;
55
- constructor(statusCode, headers, { integrity, trustIntegrity = false } = {}) {
55
+ #staleWhileRevalidateFactor;
56
+ constructor(statusCode, headers, { integrity, trustIntegrity = false, 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, } = {}) {
56
57
  this.#headers = headers;
57
58
  this.#statusCode = statusCode;
58
59
  this.#trustIntegrity = trustIntegrity;
60
+ this.#staleWhileRevalidateFactor = staleWhileRevalidateFactor;
59
61
  if (integrity)
60
62
  this.integrity = integrity;
61
63
  }
@@ -68,49 +70,110 @@ export class CacheEntry {
68
70
  }
69
71
  return ret;
70
72
  }
71
- [kCustomInspect](depth, options) {
72
- const raw = this.text();
73
- const isBinary = raw.includes('\x00');
74
- const show = isBinary ? '[binary data]' : raw.substring(0, 512);
75
- const text = !isBinary && show.length < raw.length ? raw + '…' : show;
76
- const str = inspect({
77
- statusCode: this.statusCode,
73
+ toJSON() {
74
+ const { statusCode, valid, staleWhileRevalidate, cacheControl, date, contentType, integrity, maxAge, isGzip, isJSON, } = this;
75
+ /* c8 ignore start */
76
+ const age = date ?
77
+ Math.floor((Date.now() - date.getTime()) / 1000)
78
+ : undefined;
79
+ const expires = date ? new Date(date.getTime() + this.maxAge * 1000) : undefined;
80
+ /* c8 ignore end */
81
+ return Object.fromEntries(Object.entries({
82
+ statusCode,
78
83
  headers: this.#headersAsObject,
79
- // we know that gzip and tar data will always include at least
80
- // one \x00, and json never will, so this is fine for our purpose,
81
- // to avoid dumping bells and whatnot to the terminal.
82
- text,
83
- }, {
84
+ contentType,
85
+ integrity,
86
+ date,
87
+ expires,
88
+ cacheControl,
89
+ valid,
90
+ staleWhileRevalidate,
91
+ age,
92
+ maxAge,
93
+ isGzip,
94
+ isJSON,
95
+ }).filter(([_, v]) => v !== undefined));
96
+ }
97
+ [kCustomInspect](depth, options) {
98
+ const str = inspect(this.toJSON(), {
84
99
  depth,
85
100
  ...options,
86
101
  });
87
102
  return `@vltpkg/registry-client.CacheEntry ${str}`;
88
103
  }
104
+ #date;
105
+ get date() {
106
+ if (this.#date)
107
+ return this.#date;
108
+ const dh = this.getHeader('date')?.toString();
109
+ if (dh)
110
+ this.#date = new Date(dh);
111
+ return this.#date;
112
+ }
113
+ #maxAge;
114
+ get maxAge() {
115
+ if (this.#maxAge !== undefined)
116
+ return this.#maxAge;
117
+ // see if the max-age has not yet been crossed
118
+ // default to 5m if maxage is not set, as some registries
119
+ // do not set a cache control header at all.
120
+ const cc = this.cacheControl;
121
+ this.#maxAge = cc['max-age'] || cc['s-maxage'] || 300;
122
+ return this.#maxAge;
123
+ }
124
+ #cacheControl;
125
+ get cacheControl() {
126
+ if (this.#cacheControl)
127
+ return this.#cacheControl;
128
+ const cc = this.getHeader('cache-control')?.toString();
129
+ this.#cacheControl = cc ? ccp.parse(cc) : {};
130
+ return this.#cacheControl;
131
+ }
132
+ #staleWhileRevalidate;
133
+ get staleWhileRevalidate() {
134
+ if (this.#staleWhileRevalidate !== undefined)
135
+ return this.#staleWhileRevalidate;
136
+ if (this.valid || !this.date)
137
+ return true;
138
+ const swv = this.cacheControl['stale-while-revalidate'] ??
139
+ this.maxAge * this.#staleWhileRevalidateFactor;
140
+ this.#staleWhileRevalidate =
141
+ this.date.getTime() + swv * 1000 > Date.now();
142
+ return this.#staleWhileRevalidate;
143
+ }
144
+ #contentType;
145
+ get contentType() {
146
+ if (this.#contentType !== undefined)
147
+ return this.#contentType;
148
+ this.#contentType =
149
+ this.getHeader('content-type')?.toString() ?? '';
150
+ return this.#contentType;
151
+ }
89
152
  /**
90
153
  * `true` if the entry represents a cached response that is still
91
154
  * valid to use.
92
155
  */
156
+ #valid;
93
157
  get valid() {
94
- const cc_ = this.getHeader('cache-control')?.toString();
95
- const cc = cc_ ? ccp.parse(cc_) : {};
96
- const ct = this.getHeader('content-type')?.toString() ?? '';
97
- const dh = this.getHeader('date')?.toString();
158
+ if (this.#valid !== undefined)
159
+ return this.#valid;
98
160
  // immutable = never changes
99
- if (cc.immutable)
100
- return true;
161
+ if (this.cacheControl.immutable)
162
+ return (this.#valid = true);
101
163
  // some registries do text/json, some do application/json,
102
164
  // some do application/vnd.npm.install-v1+json
103
165
  // If it's NOT json, it's an immutable tarball
104
- if (ct !== '' && !/\bjson\b/.test(ct))
105
- return true;
166
+ const ct = this.contentType;
167
+ if (ct && !/\bjson\b/.test(ct))
168
+ return (this.#valid = true);
106
169
  // see if the max-age has not yet been crossed
107
170
  // default to 5m if maxage is not set, as some registries
108
171
  // do not set a cache control header at all.
109
- const ma = cc['max-age'] || cc['s-maxage'] || 300;
110
- if (ma && dh) {
111
- return Date.parse(dh) + ma * 1000 > Date.now();
112
- }
113
- return false;
172
+ if (!this.date)
173
+ return (this.#valid = false);
174
+ this.#valid =
175
+ this.date.getTime() + this.maxAge * 1000 > Date.now();
176
+ return this.#valid;
114
177
  }
115
178
  addBody(b) {
116
179
  this.#body.push(b);
@@ -278,7 +341,8 @@ export class CacheEntry {
278
341
  json() {
279
342
  if (this.#json !== undefined)
280
343
  return this.#json;
281
- const obj = JSON.parse(this.text());
344
+ const text = this.text();
345
+ const obj = JSON.parse(text || '{}');
282
346
  this.#json = obj;
283
347
  return obj;
284
348
  }
@@ -1 +1 @@
1
- {"version":3,"file":"cache-entry.js","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,4EAA4E;AAC5E,yDAAyD;AACzD,EAAE;AACF,sBAAsB;AACtB,EAAE;AACF,gBAAgB;AAChB,yBAAyB;AACzB,YAAY;AACZ,SAAS;AACT,EAAE;AACF,0EAA0E;AAC1E,+CAA+C;AAC/C,EAAE;AACF,8EAA8E;AAC9E,4DAA4D;AAC5D,EAAE;AACF,2EAA2E;AAC3E,eAAe;AAGf,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAE3C,OAAO,GAAG,MAAM,sBAAsB,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAI5D,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,MAAc,EAAE,EAAE;IAC/C,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAA;IACrB,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACzB,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACzB,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAEzB,oCAAoC;IACpC,qBAAqB;IACrB,IACE,CAAC,KAAK,SAAS;QACf,CAAC,KAAK,SAAS;QACf,CAAC,KAAK,SAAS;QACf,CAAC,KAAK,SAAS,EACf,CAAC;QACD,MAAM,KAAK,CAAC,6CAA6C,EAAE;YACzD,KAA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AK,CAAC,GAAG,CACP;YACE,CAAC,UAAU,IAAI,EAAE,CAAC,GAAG,IAAI;YACzB,CAAC,UAAU,IAAI,EAAE,CAAC,GAAG,IAAI;YACzB,CAAC,UAAU,IAAI,CAAC,CAAC,GAAG,IAAI;YACxB,UAAU,GAAG,IAAI;SAClB,EACD,CAAC,CACF,CAAA;QACD,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACrB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;QAC1B,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,CAAA;IAC7D,CAAC;CACF;AAED,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA","sourcesContent":["// A response object in the cache.\n//\n// The cache stores Buffer objects, and it's convenient to have headers/body\n// together, so we have a simple data structure for this.\n//\n// The shape of it is:\n//\n// [head length]\n// <status code in ascii>\n// [headers]\n// [body]\n//\n// The [UInt32BE head length] is 4 bytes specifying the full length of the\n// status code plus all header keys and values.\n//\n// The [headers] section is key/value/key2/value2/... where each key and value\n// is a 4-byte Uint32BE length, followed by that many bytes.\n//\n// From there, the body can be of any indeterminate length, and is the rest\n// of the file.\n\nimport type { ErrorCauseObject } from '@vltpkg/error-cause'\nimport { error } from '@vltpkg/error-cause'\nimport type { Integrity, JSONField } from '@vltpkg/types'\nimport ccp from 'cache-control-parser'\nimport { createHash } from 'node:crypto'\nimport type { InspectOptions } from 'node:util'\nimport { inspect } from 'node:util'\nimport { gunzipSync } from 'node:zlib'\nimport { getRawHeader, setRawHeader } from './raw-header.ts'\n\nexport type JSONObj = Record<string, JSONField>\n\nconst readSize = (buf: Buffer, offset: number) => {\n const a = buf[offset]\n const b = buf[offset + 1]\n const c = buf[offset + 2]\n const d = buf[offset + 3]\n\n // not possible, we check the length\n /* c8 ignore start */\n if (\n a === undefined ||\n b === undefined ||\n c === undefined ||\n d === undefined\n ) {\n throw error('Invalid buffer, not long enough to readSize', {\n found: buf.length,\n })\n }\n /* c8 ignore stop */\n\n return (a << 24) | (b << 16) | (c << 8) | d\n}\n\nconst kCustomInspect = Symbol.for('nodejs.util.inspect.custom')\n\nexport type CacheEntryOptions = {\n /**\n * The expected integrity value for this response body\n */\n integrity?: Integrity\n /**\n * Whether to trust the integrity, or calculate the actual value.\n *\n * This indicates that we just accept whatever the integrity is as the actual\n * integrity for saving back to the cache, because it's coming directly from\n * the registry that we fetched a packument from, and is an initial gzipped\n * artifact request.\n */\n trustIntegrity?: boolean\n}\n\nexport class CacheEntry {\n #statusCode: number\n #headers: Buffer[]\n #body: Buffer[] = []\n #bodyLength = 0\n #integrity?: Integrity\n #integrityActual?: Integrity\n #json?: JSONObj\n #trustIntegrity\n\n constructor(\n statusCode: number,\n headers: Buffer[],\n { integrity, trustIntegrity = false }: CacheEntryOptions = {},\n ) {\n this.#headers = headers\n this.#statusCode = statusCode\n this.#trustIntegrity = trustIntegrity\n if (integrity) this.integrity = integrity\n }\n\n get #headersAsObject(): [string, string][] {\n const ret: [string, string][] = []\n for (let i = 0; i < this.#headers.length - 1; i += 2) {\n const key = String(this.#headers[i])\n const val = String(this.#headers[i + 1])\n ret.push([key, val])\n }\n return ret\n }\n\n [kCustomInspect](depth: number, options: InspectOptions): string {\n const raw = this.text()\n const isBinary = raw.includes('\\x00')\n const show = isBinary ? '[binary data]' : raw.substring(0, 512)\n const text =\n !isBinary && show.length < raw.length ? raw + '…' : show\n const str = inspect(\n {\n statusCode: this.statusCode,\n headers: this.#headersAsObject,\n // we know that gzip and tar data will always include at least\n // one \\x00, and json never will, so this is fine for our purpose,\n // to avoid dumping bells and whatnot to the terminal.\n text,\n },\n {\n depth,\n ...options,\n },\n )\n return `@vltpkg/registry-client.CacheEntry ${str}`\n }\n\n /**\n * `true` if the entry represents a cached response that is still\n * valid to use.\n */\n get valid(): boolean {\n const cc_ = this.getHeader('cache-control')?.toString()\n const cc = cc_ ? ccp.parse(cc_) : {}\n const ct = this.getHeader('content-type')?.toString() ?? ''\n const dh = this.getHeader('date')?.toString()\n\n // immutable = never changes\n if (cc.immutable) return true\n\n // some registries do text/json, some do application/json,\n // some do application/vnd.npm.install-v1+json\n // If it's NOT json, it's an immutable tarball\n if (ct !== '' && !/\\bjson\\b/.test(ct)) return true\n\n // see if the max-age has not yet been crossed\n // default to 5m if maxage is not set, as some registries\n // do not set a cache control header at all.\n const ma = cc['max-age'] || cc['s-maxage'] || 300\n if (ma && dh) {\n return Date.parse(dh) + ma * 1000 > Date.now()\n }\n return false\n }\n\n addBody(b: Buffer) {\n this.#body.push(b)\n this.#bodyLength += b.byteLength\n }\n\n get statusCode() {\n return this.#statusCode\n }\n get headers() {\n return this.#headers\n }\n\n /**\n * Check that the sri integrity string that was provided to the ctor\n * matches the body that we actually received. This should only be called\n * AFTER the entire body has been completely downloaded.\n *\n * This method **will throw** if the integrity values do not match.\n *\n * Note that this will *usually* not be true if the value is coming out of\n * the cache, because the cache entries are un-gzipped in place. It should\n * _only_ be called for artifacts that come from an actual http response.\n *\n * Returns true if anything was actually verified.\n */\n checkIntegrity(\n context: ErrorCauseObject = {},\n ): this is CacheEntry & { integrity: Integrity } {\n if (!this.#integrity) return false\n if (this.integrityActual !== this.#integrity) {\n throw error('Integrity check failure', {\n code: 'EINTEGRITY',\n response: this,\n wanted: this.#integrity,\n found: this.integrityActual,\n ...context,\n })\n }\n return true\n }\n\n get integrityActual(): Integrity {\n if (this.#integrityActual) return this.#integrityActual\n const hash = createHash('sha512')\n for (const buf of this.#body) hash.update(buf)\n const i: Integrity = `sha512-${hash.digest('base64')}`\n this.integrityActual = i\n return i\n }\n\n set integrityActual(i: Integrity) {\n this.#integrityActual = i\n this.setHeader('integrity', i)\n }\n\n set integrity(i: Integrity | undefined) {\n if (!this.#integrity && i) {\n this.#integrity = i\n if (this.#trustIntegrity) this.integrityActual = i\n }\n }\n get integrity() {\n return this.#integrity\n }\n\n /**\n * Give it a key, and it'll return the buffer of that header value\n */\n getHeader(h: string) {\n return getRawHeader(this.#headers, h)\n }\n\n /**\n * Set a header to a specific value\n */\n setHeader(h: string, value: Buffer | string) {\n this.#headers = setRawHeader(this.#headers, h, value)\n }\n\n /**\n * Return the body of the entry as a Buffer\n */\n buffer(): Buffer {\n const b = this.#body[0]\n if (!b) return Buffer.allocUnsafe(0)\n if (this.#body.length === 1) return b\n const cat = Buffer.concat(this.#body, this.#bodyLength)\n this.#body = [cat]\n return cat\n }\n\n // return the buffer if it's a tarball, or the parsed\n // JSON if it's not.\n get body(): Buffer | Record<string, any> {\n return this.isJSON ? this.json() : this.buffer()\n }\n\n #isJSON?: boolean\n get isJSON(): boolean {\n if (this.#isJSON !== undefined) return this.#isJSON\n const ct = this.getHeader('content-type')?.toString()\n // if it says it's json, assume json\n if (ct) return (this.#isJSON = /\\bjson\\b/.test(ct))\n const text = this.text()\n // don't cache, because we might just not have it yet.\n if (!text) return false\n // all registry json starts with {, and no tarball ever can.\n this.#isJSON = text.startsWith('{')\n if (this.#isJSON) this.setHeader('content-type', 'text/json')\n return this.#isJSON\n }\n\n #isGzip?: boolean\n get isGzip(): boolean {\n if (this.#isGzip !== undefined) return this.#isGzip\n const ce = this.getHeader('content-encoding')?.toString()\n if (ce && !/\\bgzip\\b/.test(ce)) return (this.#isGzip = false)\n const buf = this.buffer()\n if (buf.length < 2) return false\n this.#isGzip = buf[0] === 0x1f && buf[1] === 0x8b\n if (this.#isGzip) {\n this.setHeader('content-encoding', 'gzip')\n } else {\n this.setHeader('content-encoding', 'identity')\n this.setHeader('content-length', String(this.#bodyLength))\n }\n return this.#isGzip\n }\n\n /**\n * Un-gzip encode the body.\n * Returns true if it was previously gzip (so something was done), otherwise\n * returns false.\n */\n unzip() {\n if (this.isGzip) {\n // we know that if we know it's gzip, that the body has been\n // flattened to a single buffer, so save the extra call.\n /* c8 ignore start */\n if (this.#body[0] == null)\n throw error('Invalid buffer, cant unzip')\n /* c8 ignore stop */\n const b = gunzipSync(this.#body[0])\n this.setHeader('content-encoding', 'identity')\n this.#body = [b]\n this.#bodyLength = b.byteLength\n this.setHeader('content-length', String(this.#bodyLength))\n this.#isGzip = false\n return true\n }\n return false\n }\n\n /**\n * Return the body of the entry as utf8 text\n * Automatically unzips if the content is gzip encoded\n */\n text() {\n this.unzip()\n return this.buffer().toString()\n }\n\n /**\n * Parse the entry body as JSON and return the result\n */\n json(): JSONObj {\n if (this.#json !== undefined) return this.#json\n const obj = JSON.parse(this.text()) as JSONObj\n this.#json = obj\n return obj\n }\n\n /**\n * Pass the contents of a @vltpkg/cache.Cache object as a buffer,\n * and this static method will decode it into a CacheEntry representing\n * the cached response.\n */\n static decode(buffer: Buffer): CacheEntry {\n if (buffer.length < 4) {\n return emptyCacheEntry\n }\n const headSize = readSize(buffer, 0)\n if (buffer.length < headSize) {\n return emptyCacheEntry\n }\n const statusCode = Number(buffer.subarray(4, 7).toString())\n const headersBuffer = buffer.subarray(7, headSize)\n // walk through the headers array, building up the rawHeaders Buffer[]\n const headers: Buffer[] = []\n let i = 0\n let integrity: Integrity | undefined = undefined\n while (i < headersBuffer.length - 4) {\n const size = readSize(headersBuffer, i)\n const val = headersBuffer.subarray(i + 4, i + size)\n // if the last one was the key integrity, then this one is the value\n if (\n headers.length % 2 === 1 &&\n String(headers[headers.length - 1]) === 'integrity'\n ) {\n integrity = String(val) as Integrity\n }\n headers.push(val)\n i += size\n }\n const body = buffer.subarray(headSize)\n\n const c = new CacheEntry(\n statusCode,\n setRawHeader(\n headers,\n 'content-length',\n String(body.byteLength),\n ),\n {\n integrity,\n trustIntegrity: true,\n },\n )\n\n c.#body = [body]\n c.#bodyLength = body.byteLength\n if (c.isJSON) {\n try {\n c.json()\n } catch {\n return emptyCacheEntry\n }\n }\n return c\n }\n\n static isGzipEntry(buffer: Buffer): boolean {\n if (buffer.length < 4) return false\n const headSize = readSize(buffer, 0)\n const gzipBytes = buffer.subarray(headSize, headSize + 2)\n return gzipBytes[0] === 0x1f && gzipBytes[1] === 0x8b\n }\n\n /**\n * Encode the entry as a single Buffer for writing to the cache\n */\n // TODO: should this maybe not concat, and just return Buffer[]?\n // Then we can writev it to the cache file and save the memory copy\n encode(): Buffer {\n if (this.isJSON) this.json()\n const sb = Buffer.from(String(this.#statusCode))\n const chunks: Buffer[] = [sb]\n let headLength = sb.byteLength + 4\n for (const h of this.#headers) {\n const hlBuf = Buffer.allocUnsafe(4)\n const hl = h.byteLength + 4\n headLength += hl\n hlBuf.set(\n [\n (hl >> 24) & 0xff,\n (hl >> 16) & 0xff,\n (hl >> 8) & 0xff,\n hl & 0xff,\n ],\n 0,\n )\n chunks.push(hlBuf, h)\n }\n\n const hlBuf = Buffer.allocUnsafe(4)\n hlBuf.set(\n [\n (headLength >> 24) & 0xff,\n (headLength >> 16) & 0xff,\n (headLength >> 8) & 0xff,\n headLength & 0xff,\n ],\n 0,\n )\n chunks.unshift(hlBuf)\n chunks.push(...this.#body)\n return Buffer.concat(chunks, headLength + this.#bodyLength)\n }\n}\n\nconst emptyCacheEntry = new CacheEntry(0, [])\n"]}
1
+ {"version":3,"file":"cache-entry.js","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,4EAA4E;AAC5E,yDAAyD;AACzD,EAAE;AACF,sBAAsB;AACtB,EAAE;AACF,gBAAgB;AAChB,yBAAyB;AACzB,YAAY;AACZ,SAAS;AACT,EAAE;AACF,0EAA0E;AAC1E,+CAA+C;AAC/C,EAAE;AACF,8EAA8E;AAC9E,4DAA4D;AAC5D,EAAE;AACF,2EAA2E;AAC3E,eAAe;AAGf,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAE3C,OAAO,GAAG,MAAM,sBAAsB,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,UAAU,EAAE,MA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BACjB,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI;gBACjB,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,IAAI;gBAChB,EAAE,GAAG,IAAI;aACV,EACD,CAAC,CACF,CAAA;YACD,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;QACvB,CAAC;QAED,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;QACnC,KAAK,CAAC,GAAG,CACP;YACE,CAAC,UAAU,IAAI,EAAE,CAAC,GAAG,IAAI;YACzB,CAAC,UAAU,IAAI,EAAE,CAAC,GAAG,IAAI;YACzB,CAAC,UAAU,IAAI,CAAC,CAAC,GAAG,IAAI;YACxB,UAAU,GAAG,IAAI;SAClB,EACD,CAAC,CACF,CAAA;QACD,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACrB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;QAC1B,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,CAAA;IAC7D,CAAC;CACF;AAED,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA","sourcesContent":["// A response object in the cache.\n//\n// The cache stores Buffer objects, and it's convenient to have headers/body\n// together, so we have a simple data structure for this.\n//\n// The shape of it is:\n//\n// [head length]\n// <status code in ascii>\n// [headers]\n// [body]\n//\n// The [UInt32BE head length] is 4 bytes specifying the full length of the\n// status code plus all header keys and values.\n//\n// The [headers] section is key/value/key2/value2/... where each key and value\n// is a 4-byte Uint32BE length, followed by that many bytes.\n//\n// From there, the body can be of any indeterminate length, and is the rest\n// of the file.\n\nimport type { ErrorCauseOptions } from '@vltpkg/error-cause'\nimport { error } from '@vltpkg/error-cause'\nimport type { Integrity, JSONField } from '@vltpkg/types'\nimport ccp from 'cache-control-parser'\nimport { createHash } from 'node:crypto'\nimport type { InspectOptions } from 'node:util'\nimport { inspect } from 'node:util'\nimport { gunzipSync } from 'node:zlib'\nimport { getRawHeader, setRawHeader } from './raw-header.ts'\n\nexport type JSONObj = Record<string, JSONField>\n\nconst readSize = (buf: Buffer, offset: number) => {\n const a = buf[offset]\n const b = buf[offset + 1]\n const c = buf[offset + 2]\n const d = buf[offset + 3]\n\n // not possible, we check the length\n /* c8 ignore start */\n if (\n a === undefined ||\n b === undefined ||\n c === undefined ||\n d === undefined\n ) {\n throw error('Invalid buffer, not long enough to readSize', {\n found: buf.length,\n })\n }\n /* c8 ignore stop */\n\n return (a << 24) | (b << 16) | (c << 8) | d\n}\n\nconst kCustomInspect = Symbol.for('nodejs.util.inspect.custom')\n\nexport type CacheEntryOptions = {\n /**\n * The expected integrity value for this response body\n */\n integrity?: Integrity\n /**\n * Whether to trust the integrity, or calculate the actual value.\n *\n * This indicates that we just accept whatever the integrity is as the actual\n * integrity for saving back to the cache, because it's coming directly from\n * the registry that we fetched a packument from, and is an initial gzipped\n * artifact request.\n */\n trustIntegrity?: boolean\n\n /**\n * If the server does not serve a `stale-while-revalidate` value in the\n * `cache-control` header, then this multiplier is applied to the `max-age`\n * or `s-maxage` values.\n *\n * By default, this is `60`, so for example a response that is cacheable for\n * 5 minutes will allow a stale response while revalidating for up to 5\n * hours.\n *\n * If the server *does* provide a `stale-while-revalidate` value, then that\n * is always used.\n *\n * Set to 0 to prevent any `stale-while-revalidate` behavior unless\n * explicitly allowed by the server's `cache-control` header.\n */\n 'stale-while-revalidate-factor'?: number\n}\n\nexport class CacheEntry {\n #statusCode: number\n #headers: Buffer[]\n #body: Buffer[] = []\n #bodyLength = 0\n #integrity?: Integrity\n #integrityActual?: Integrity\n #json?: JSONObj\n #trustIntegrity\n #staleWhileRevalidateFactor\n\n constructor(\n statusCode: number,\n headers: Buffer[],\n {\n integrity,\n trustIntegrity = false,\n 'stale-while-revalidate-factor':\n staleWhileRevalidateFactor = 60,\n }: CacheEntryOptions = {},\n ) {\n this.#headers = headers\n this.#statusCode = statusCode\n this.#trustIntegrity = trustIntegrity\n this.#staleWhileRevalidateFactor = staleWhileRevalidateFactor\n if (integrity) this.integrity = integrity\n }\n\n get #headersAsObject(): [string, string][] {\n const ret: [string, string][] = []\n for (let i = 0; i < this.#headers.length - 1; i += 2) {\n const key = String(this.#headers[i])\n const val = String(this.#headers[i + 1])\n ret.push([key, val])\n }\n return ret\n }\n\n toJSON() {\n const {\n statusCode,\n valid,\n staleWhileRevalidate,\n cacheControl,\n date,\n contentType,\n integrity,\n maxAge,\n isGzip,\n isJSON,\n } = this\n /* c8 ignore start */\n const age =\n date ?\n Math.floor((Date.now() - date.getTime()) / 1000)\n : undefined\n const expires =\n date ? new Date(date.getTime() + this.maxAge * 1000) : undefined\n /* c8 ignore end */\n return Object.fromEntries(\n Object.entries({\n statusCode,\n headers: this.#headersAsObject,\n contentType,\n integrity,\n date,\n expires,\n cacheControl,\n valid,\n staleWhileRevalidate,\n age,\n maxAge,\n isGzip,\n isJSON,\n }).filter(([_, v]) => v !== undefined),\n )\n }\n\n [kCustomInspect](depth: number, options: InspectOptions): string {\n const str = inspect(this.toJSON(), {\n depth,\n ...options,\n })\n return `@vltpkg/registry-client.CacheEntry ${str}`\n }\n\n #date?: Date\n get date(): Date | undefined {\n if (this.#date) return this.#date\n const dh = this.getHeader('date')?.toString()\n if (dh) this.#date = new Date(dh)\n return this.#date\n }\n\n #maxAge?: number\n get maxAge(): number {\n if (this.#maxAge !== undefined) return this.#maxAge\n // see if the max-age has not yet been crossed\n // default to 5m if maxage is not set, as some registries\n // do not set a cache control header at all.\n const cc = this.cacheControl\n this.#maxAge = cc['max-age'] || cc['s-maxage'] || 300\n return this.#maxAge\n }\n\n #cacheControl?: ccp.CacheControl\n get cacheControl(): ccp.CacheControl {\n if (this.#cacheControl) return this.#cacheControl\n const cc = this.getHeader('cache-control')?.toString()\n this.#cacheControl = cc ? ccp.parse(cc) : {}\n return this.#cacheControl\n }\n\n #staleWhileRevalidate?: boolean\n get staleWhileRevalidate(): boolean {\n if (this.#staleWhileRevalidate !== undefined)\n return this.#staleWhileRevalidate\n if (this.valid || !this.date) return true\n const swv =\n this.cacheControl['stale-while-revalidate'] ??\n this.maxAge * this.#staleWhileRevalidateFactor\n\n this.#staleWhileRevalidate =\n this.date.getTime() + swv * 1000 > Date.now()\n return this.#staleWhileRevalidate\n }\n\n #contentType?: string\n get contentType() {\n if (this.#contentType !== undefined) return this.#contentType\n this.#contentType =\n this.getHeader('content-type')?.toString() ?? ''\n return this.#contentType\n }\n\n /**\n * `true` if the entry represents a cached response that is still\n * valid to use.\n */\n #valid?: boolean\n get valid(): boolean {\n if (this.#valid !== undefined) return this.#valid\n\n // immutable = never changes\n if (this.cacheControl.immutable) return (this.#valid = true)\n\n // some registries do text/json, some do application/json,\n // some do application/vnd.npm.install-v1+json\n // If it's NOT json, it's an immutable tarball\n const ct = this.contentType\n if (ct && !/\\bjson\\b/.test(ct)) return (this.#valid = true)\n\n // see if the max-age has not yet been crossed\n // default to 5m if maxage is not set, as some registries\n // do not set a cache control header at all.\n if (!this.date) return (this.#valid = false)\n this.#valid =\n this.date.getTime() + this.maxAge * 1000 > Date.now()\n return this.#valid\n }\n\n addBody(b: Buffer) {\n this.#body.push(b)\n this.#bodyLength += b.byteLength\n }\n\n get statusCode() {\n return this.#statusCode\n }\n get headers() {\n return this.#headers\n }\n\n /**\n * Check that the sri integrity string that was provided to the ctor\n * matches the body that we actually received. This should only be called\n * AFTER the entire body has been completely downloaded.\n *\n * This method **will throw** if the integrity values do not match.\n *\n * Note that this will *usually* not be true if the value is coming out of\n * the cache, because the cache entries are un-gzipped in place. It should\n * _only_ be called for artifacts that come from an actual http response.\n *\n * Returns true if anything was actually verified.\n */\n checkIntegrity(\n context: ErrorCauseOptions = {},\n ): this is CacheEntry & { integrity: Integrity } {\n if (!this.#integrity) return false\n if (this.integrityActual !== this.#integrity) {\n throw error('Integrity check failure', {\n code: 'EINTEGRITY',\n response: this,\n wanted: this.#integrity,\n found: this.integrityActual,\n ...context,\n })\n }\n return true\n }\n\n get integrityActual(): Integrity {\n if (this.#integrityActual) return this.#integrityActual\n const hash = createHash('sha512')\n for (const buf of this.#body) hash.update(buf)\n const i: Integrity = `sha512-${hash.digest('base64')}`\n this.integrityActual = i\n return i\n }\n\n set integrityActual(i: Integrity) {\n this.#integrityActual = i\n this.setHeader('integrity', i)\n }\n\n set integrity(i: Integrity | undefined) {\n if (!this.#integrity && i) {\n this.#integrity = i\n if (this.#trustIntegrity) this.integrityActual = i\n }\n }\n get integrity() {\n return this.#integrity\n }\n\n /**\n * Give it a key, and it'll return the buffer of that header value\n */\n getHeader(h: string) {\n return getRawHeader(this.#headers, h)\n }\n\n /**\n * Set a header to a specific value\n */\n setHeader(h: string, value: Buffer | string) {\n this.#headers = setRawHeader(this.#headers, h, value)\n }\n\n /**\n * Return the body of the entry as a Buffer\n */\n buffer(): Buffer {\n const b = this.#body[0]\n if (!b) return Buffer.allocUnsafe(0)\n if (this.#body.length === 1) return b\n const cat = Buffer.concat(this.#body, this.#bodyLength)\n this.#body = [cat]\n return cat\n }\n\n // return the buffer if it's a tarball, or the parsed\n // JSON if it's not.\n get body(): Buffer | Record<string, any> {\n return this.isJSON ? this.json() : this.buffer()\n }\n\n #isJSON?: boolean\n get isJSON(): boolean {\n if (this.#isJSON !== undefined) return this.#isJSON\n const ct = this.getHeader('content-type')?.toString()\n // if it says it's json, assume json\n if (ct) return (this.#isJSON = /\\bjson\\b/.test(ct))\n const text = this.text()\n // don't cache, because we might just not have it yet.\n if (!text) return false\n // all registry json starts with {, and no tarball ever can.\n this.#isJSON = text.startsWith('{')\n if (this.#isJSON) this.setHeader('content-type', 'text/json')\n return this.#isJSON\n }\n\n #isGzip?: boolean\n get isGzip(): boolean {\n if (this.#isGzip !== undefined) return this.#isGzip\n const ce = this.getHeader('content-encoding')?.toString()\n if (ce && !/\\bgzip\\b/.test(ce)) return (this.#isGzip = false)\n const buf = this.buffer()\n if (buf.length < 2) return false\n this.#isGzip = buf[0] === 0x1f && buf[1] === 0x8b\n if (this.#isGzip) {\n this.setHeader('content-encoding', 'gzip')\n } else {\n this.setHeader('content-encoding', 'identity')\n this.setHeader('content-length', String(this.#bodyLength))\n }\n return this.#isGzip\n }\n\n /**\n * Un-gzip encode the body.\n * Returns true if it was previously gzip (so something was done), otherwise\n * returns false.\n */\n unzip() {\n if (this.isGzip) {\n // we know that if we know it's gzip, that the body has been\n // flattened to a single buffer, so save the extra call.\n /* c8 ignore start */\n if (this.#body[0] == null)\n throw error('Invalid buffer, cant unzip')\n /* c8 ignore stop */\n const b = gunzipSync(this.#body[0])\n this.setHeader('content-encoding', 'identity')\n this.#body = [b]\n this.#bodyLength = b.byteLength\n this.setHeader('content-length', String(this.#bodyLength))\n this.#isGzip = false\n return true\n }\n return false\n }\n\n /**\n * Return the body of the entry as utf8 text\n * Automatically unzips if the content is gzip encoded\n */\n text() {\n this.unzip()\n return this.buffer().toString()\n }\n\n /**\n * Parse the entry body as JSON and return the result\n */\n json(): JSONObj {\n if (this.#json !== undefined) return this.#json\n const text = this.text()\n const obj = JSON.parse(text || '{}') as JSONObj\n this.#json = obj\n return obj\n }\n\n /**\n * Pass the contents of a @vltpkg/cache.Cache object as a buffer,\n * and this static method will decode it into a CacheEntry representing\n * the cached response.\n */\n static decode(buffer: Buffer): CacheEntry {\n if (buffer.length < 4) {\n return emptyCacheEntry\n }\n const headSize = readSize(buffer, 0)\n if (buffer.length < headSize) {\n return emptyCacheEntry\n }\n const statusCode = Number(buffer.subarray(4, 7).toString())\n const headersBuffer = buffer.subarray(7, headSize)\n // walk through the headers array, building up the rawHeaders Buffer[]\n const headers: Buffer[] = []\n let i = 0\n let integrity: Integrity | undefined = undefined\n while (i < headersBuffer.length - 4) {\n const size = readSize(headersBuffer, i)\n const val = headersBuffer.subarray(i + 4, i + size)\n // if the last one was the key integrity, then this one is the value\n if (\n headers.length % 2 === 1 &&\n String(headers[headers.length - 1]) === 'integrity'\n ) {\n integrity = String(val) as Integrity\n }\n headers.push(val)\n i += size\n }\n const body = buffer.subarray(headSize)\n\n const c = new CacheEntry(\n statusCode,\n setRawHeader(\n headers,\n 'content-length',\n String(body.byteLength),\n ),\n {\n integrity,\n trustIntegrity: true,\n },\n )\n\n c.#body = [body]\n c.#bodyLength = body.byteLength\n if (c.isJSON) {\n try {\n c.json()\n } catch {\n return emptyCacheEntry\n }\n }\n return c\n }\n\n static isGzipEntry(buffer: Buffer): boolean {\n if (buffer.length < 4) return false\n const headSize = readSize(buffer, 0)\n const gzipBytes = buffer.subarray(headSize, headSize + 2)\n return gzipBytes[0] === 0x1f && gzipBytes[1] === 0x8b\n }\n\n /**\n * Encode the entry as a single Buffer for writing to the cache\n */\n // TODO: should this maybe not concat, and just return Buffer[]?\n // Then we can writev it to the cache file and save the memory copy\n encode(): Buffer {\n if (this.isJSON) this.json()\n const sb = Buffer.from(String(this.#statusCode))\n const chunks: Buffer[] = [sb]\n let headLength = sb.byteLength + 4\n for (const h of this.#headers) {\n const hlBuf = Buffer.allocUnsafe(4)\n const hl = h.byteLength + 4\n headLength += hl\n hlBuf.set(\n [\n (hl >> 24) & 0xff,\n (hl >> 16) & 0xff,\n (hl >> 8) & 0xff,\n hl & 0xff,\n ],\n 0,\n )\n chunks.push(hlBuf, h)\n }\n\n const hlBuf = Buffer.allocUnsafe(4)\n hlBuf.set(\n [\n (headLength >> 24) & 0xff,\n (headLength >> 16) & 0xff,\n (headLength >> 8) & 0xff,\n headLength & 0xff,\n ],\n 0,\n )\n chunks.unshift(hlBuf)\n chunks.push(...this.#body)\n return Buffer.concat(chunks, headLength + this.#bodyLength)\n }\n}\n\nconst emptyCacheEntry = new CacheEntry(0, [])\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const register: (path: string, method: "HEAD" | "GET", url: string | URL) => void;
2
+ //# sourceMappingURL=cache-revalidate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-revalidate.d.ts","sourceRoot":"","sources":["../../src/cache-revalidate.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,QAAQ,SACb,MAAM,UACJ,MAAM,GAAG,KAAK,OACjB,MAAM,GAAG,GAAG,KAChB,IASF,CAAA"}
@@ -0,0 +1,66 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { __CODE_SPLIT_SCRIPT_NAME } from "./revalidate.js";
3
+ import { pathToFileURL } from 'node:url';
4
+ const isDeno = globalThis.Deno != undefined;
5
+ let didProcessBeforeExitHook = false;
6
+ const registered = new Map();
7
+ export const register = (path, method, url) => {
8
+ const r = registered.get(path) ?? new Set();
9
+ const key = `${method} ${url}`;
10
+ r.add(key);
11
+ registered.set(path, r);
12
+ if (!didProcessBeforeExitHook) {
13
+ didProcessBeforeExitHook = true;
14
+ process.on('beforeExit', handleBeforeExit);
15
+ }
16
+ };
17
+ const handleBeforeExit = () => {
18
+ for (const [path, r] of registered) {
19
+ /* c8 ignore next */
20
+ if (!r.size)
21
+ return;
22
+ const env = { ...process.env };
23
+ const args = [];
24
+ /* c8 ignore start */
25
+ // When compiled the script to be run is passed as an
26
+ // environment variable and then routed by the main entry point
27
+ if (process.env.__VLT_INTERNAL_COMPILED) {
28
+ env.__VLT_INTERNAL_MAIN = pathToFileURL(__CODE_SPLIT_SCRIPT_NAME).toString();
29
+ args.push(path);
30
+ }
31
+ else {
32
+ // If we are running deno from source we need to add the
33
+ // unstable flags we need. The '-A' flag does not need
34
+ // to be passed in as Deno supplies that automatically.
35
+ if (isDeno) {
36
+ args.push('--unstable-node-globals', '--unstable-bare-node-builtins');
37
+ }
38
+ /* c8 ignore stop */
39
+ args.push(__CODE_SPLIT_SCRIPT_NAME, path);
40
+ }
41
+ registered.delete(path);
42
+ // Deno on Windows does not support detached processes
43
+ // https://github.com/denoland/deno/issues/25867
44
+ // TODO: figure out something better to do here?
45
+ /* c8 ignore next */
46
+ const detached = !(isDeno && process.platform === 'win32');
47
+ const proc = spawn(process.execPath, args, {
48
+ detached,
49
+ stdio: ['pipe', 'ignore', 'ignore'],
50
+ env,
51
+ });
52
+ for (const key of r) {
53
+ proc.stdin.write(`${key}\0`);
54
+ }
55
+ proc.stdin.end();
56
+ // Another Deno oddity. Calling unref on a spawned process will kill the
57
+ // process unless it is detached. https://github.com/denoland/deno/issues/21446
58
+ // So in this case Deno on Windows will be slower to exit the main process
59
+ // since it will wait for the child process to exit.
60
+ // TODO: figure out something better to do here?
61
+ if (detached) {
62
+ proc.unref();
63
+ }
64
+ }
65
+ };
66
+ //# sourceMappingURL=cache-revalidate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-revalidate.js","sourceRoot":"","sources":["../../src/cache-revalidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,MAAM,MAAM,GACT,UAAiD,CAAC,IAAI,IAAI,SAAS,CAAA;AAEtE,IAAI,wBAAwB,GAAG,KAAK,CAAA;AACpC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAuB,CAAA;AAEjD,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,IAAY,EACZ,MAAsB,EACtB,GAAiB,EACX,EAAE;IACR,MAAM,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,EAAU,CAAA;IACnD,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,GAAG,EAAE,CAAA;IAC9B,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACV,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACvB,IAAI,CAAC,wBAAwB,EAAE,CAAC;QAC9B,wBAAwB,GAAG,IAAI,CAAA;QAC/B,OAAO,CAAC,EAAE,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAA;IAC5C,CAAC;AACH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AC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAA;QAC9B,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAA;QAChB,wEAAwE;QACxE,+EAA+E;QAC/E,0EAA0E;QAC1E,oDAAoD;QACpD,gDAAgD;QAChD,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,CAAC,KAAK,EAAE,CAAA;QACd,CAAC;IACH,CAAC;AACH,CAAC,CAAA","sourcesContent":["import { spawn } from 'node:child_process'\nimport { __CODE_SPLIT_SCRIPT_NAME } from './revalidate.ts'\nimport { pathToFileURL } from 'node:url'\n\nconst isDeno =\n (globalThis as typeof globalThis & { Deno?: any }).Deno != undefined\n\nlet didProcessBeforeExitHook = false\nconst registered = new Map<string, Set<string>>()\n\nexport const register = (\n path: string,\n method: 'HEAD' | 'GET',\n url: string | URL,\n): void => {\n const r = registered.get(path) ?? new Set<string>()\n const key = `${method} ${url}`\n r.add(key)\n registered.set(path, r)\n if (!didProcessBeforeExitHook) {\n didProcessBeforeExitHook = true\n process.on('beforeExit', handleBeforeExit)\n }\n}\n\nconst handleBeforeExit = () => {\n for (const [path, r] of registered) {\n /* c8 ignore next */\n if (!r.size) return\n const env = { ...process.env }\n const args = []\n /* c8 ignore start */\n // When compiled the script to be run is passed as an\n // environment variable and then routed by the main entry point\n if (process.env.__VLT_INTERNAL_COMPILED) {\n env.__VLT_INTERNAL_MAIN = pathToFileURL(\n __CODE_SPLIT_SCRIPT_NAME,\n ).toString()\n args.push(path)\n } else {\n // If we are running deno from source we need to add the\n // unstable flags we need. The '-A' flag does not need\n // to be passed in as Deno supplies that automatically.\n if (isDeno) {\n args.push(\n '--unstable-node-globals',\n '--unstable-bare-node-builtins',\n )\n }\n /* c8 ignore stop */\n args.push(__CODE_SPLIT_SCRIPT_NAME, path)\n }\n registered.delete(path)\n // Deno on Windows does not support detached processes\n // https://github.com/denoland/deno/issues/25867\n // TODO: figure out something better to do here?\n /* c8 ignore next */\n const detached = !(isDeno && process.platform === 'win32')\n const proc = spawn(process.execPath, args, {\n detached,\n stdio: ['pipe', 'ignore', 'ignore'],\n env,\n })\n for (const key of r) {\n proc.stdin.write(`${key}\\0`)\n }\n proc.stdin.end()\n // Another Deno oddity. Calling unref on a spawned process will kill the\n // process unless it is detached. https://github.com/denoland/deno/issues/21446\n // So in this case Deno on Windows will be slower to exit the main process\n // since it will wait for the child process to exit.\n // TODO: figure out something better to do here?\n if (detached) {\n proc.unref()\n }\n }\n}\n"]}
@@ -8,11 +8,14 @@ import type { JSONObj } from './cache-entry.ts';
8
8
  import { CacheEntry } from './cache-entry.ts';
9
9
  import type { TokenResponse } from './token-response.ts';
10
10
  import type { WebAuthChallenge } from './web-auth-challenge.ts';
11
- export { deleteToken, getKC, isToken, keychains, setToken, type CacheEntry, type JSONObj, type Token, type TokenResponse, type WebAuthChallenge, };
11
+ export { CacheEntry, deleteToken, getKC, isToken, keychains, setToken, type JSONObj, type Token, type TokenResponse, type WebAuthChallenge, };
12
+ export type CacheableMethod = 'GET' | 'HEAD';
13
+ export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
12
14
  export type RegistryClientOptions = {
13
15
  /**
14
16
  * Path on disk where the cache should be stored
15
- * @default `$HOME/.config/vlt/cache`
17
+ *
18
+ * Defaults to the XDG cache folder for `vlt/registry-client`
16
19
  */
17
20
  cache?: string;
18
21
  /**
@@ -28,6 +31,22 @@ export type RegistryClientOptions = {
28
31
  'fetch-retry-maxtimeout'?: number;
29
32
  /** the identity to use for storing auth tokens */
30
33
  identity?: string;
34
+ /**
35
+ * If the server does not serve a `stale-while-revalidate` value in the
36
+ * `cache-control` header, then this multiplier is applied to the `max-age`
37
+ * or `s-maxage` values.
38
+ *
39
+ * By default, this is `60`, so for example a response that is cacheable for
40
+ * 5 minutes will allow a stale response while revalidating for up to 5
41
+ * hours.
42
+ *
43
+ * If the server *does* provide a `stale-while-revalidate` value, then that
44
+ * is always used.
45
+ *
46
+ * Set to 0 to prevent any `stale-while-revalidate` behavior unless
47
+ * explicitly allowed by the server's `cache-control` header.
48
+ */
49
+ 'stale-while-revalidate-factor'?: number;
31
50
  };
32
51
  export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'method' | 'path'> & {
33
52
  /**
@@ -71,7 +90,7 @@ export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'meth
71
90
  * Set to `false` to suppress ANY lookups from cache. This will also
72
91
  * prevent storing the result to the cache.
73
92
  */
74
- cache?: false;
93
+ useCache?: false;
75
94
  /**
76
95
  * Set to pass an `npm-otp` header on the request.
77
96
  *
@@ -80,6 +99,12 @@ export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'meth
80
99
  * @internal
81
100
  */
82
101
  otp?: string;
102
+ /**
103
+ * Set to false to explicitly prevent `stale-while-revalidate` behavior,
104
+ * for use in revalidating while stale.
105
+ * @internal
106
+ */
107
+ staleWhileRevalidate?: false;
83
108
  };
84
109
  export declare const userAgent: string;
85
110
  export declare class RegistryClient {
@@ -87,6 +112,7 @@ export declare class RegistryClient {
87
112
  agent: RetryAgent;
88
113
  cache: Cache;
89
114
  identity: string;
115
+ staleWhileRevalidateFactor: number;
90
116
  constructor(options: RegistryClientOptions);
91
117
  /**
92
118
  * Fetch the entire set of a paginated list of objects
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAIrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAK9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AACxC,OAAO,EAAS,UAAU,EAAE,MAAM,QAAQ,CAAA;AAE1C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EACL,WAAW,EACX,KAAK,EAEL,OAAO,EACP,SAAS,EACT,QAAQ,EACT,MAAM,WAAW,CAAA;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,OAAO,EAAE,QAAQ,EAAE,EAAE,gBAAgB;IA6CrD,OAAO,CACX,GAAG,EAAE,GAAG,GAAG,MAAM,EACjB,OAAO,GAAE,4BAAiC,GACzC,OAAO,CAAC,UAAU,CAAC;CAuJvB"}
package/dist/esm/index.js CHANGED
@@ -1,15 +1,17 @@
1
1
  import { Cache } from '@vltpkg/cache';
2
- import { register } from '@vltpkg/cache-unzip';
3
- import { error } from '@vltpkg/error-cause';
2
+ import { register as cacheUnzipRegister } from '@vltpkg/cache-unzip';
3
+ import { asError, error } from '@vltpkg/error-cause';
4
4
  import { logRequest } from '@vltpkg/output';
5
5
  import { urlOpen } from '@vltpkg/url-open';
6
6
  import { XDG } from '@vltpkg/xdg';
7
+ import { resolve } from 'node:path';
7
8
  import { setTimeout } from 'node:timers/promises';
8
9
  import { loadPackageJson } from 'package-json-from-dist';
9
10
  import { Agent, RetryAgent } from 'undici';
10
11
  import { addHeader } from "./add-header.js";
11
12
  import { deleteToken, getKC, getToken, isToken, keychains, setToken, } from "./auth.js";
12
13
  import { CacheEntry } from "./cache-entry.js";
14
+ import { register } from "./cache-revalidate.js";
13
15
  import { bun, deno, node } from "./env.js";
14
16
  import { handle304Response } from "./handle-304-response.js";
15
17
  import { otplease } from "./otplease.js";
@@ -17,13 +19,16 @@ import { isRedirect, redirect } from "./redirect.js";
17
19
  import { setCacheHeaders } from "./set-cache-headers.js";
18
20
  import { isTokenResponse } from "./token-response.js";
19
21
  import { isWebAuthChallenge } from "./web-auth-challenge.js";
20
- export { deleteToken, getKC, isToken, keychains, setToken, };
22
+ export { CacheEntry, deleteToken, getKC, isToken, keychains, setToken, };
23
+ export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
21
24
  const { version } = loadPackageJson(import.meta.filename, process.env.__VLT_INTERNAL_REGISTRY_CLIENT_PACKAGE_JSON);
25
+ /* c8 ignore start - we do test this, but coverage fails */
22
26
  const nua = globalThis.navigator?.userAgent ??
23
27
  (bun ? `Bun/${bun}`
24
28
  : deno ? `Deno/${deno}`
25
29
  : node ? `Node.js/${node}`
26
30
  : '(unknown platform)');
31
+ /* c8 ignore stop */
27
32
  export const userAgent = `@vltpkg/registry-client/${version} ${nua}`;
28
33
  const agentOptions = {
29
34
  bodyTimeout: 600_000,
@@ -45,14 +50,18 @@ export class RegistryClient {
45
50
  agent;
46
51
  cache;
47
52
  identity;
53
+ staleWhileRevalidateFactor;
48
54
  constructor(options) {
49
- const { cache = xdg.cache('registry-client'), 'fetch-retry-factor': timeoutFactor = 2, 'fetch-retry-mintimeout': minTimeout = 0, 'fetch-retry-maxtimeout': maxTimeout = 30_000, 'fetch-retries': maxRetries = 3, identity = '', } = options;
55
+ const { cache = xdg.cache(), 'fetch-retry-factor': timeoutFactor = 2, 'fetch-retry-mintimeout': minTimeout = 0, 'fetch-retry-maxtimeout': maxTimeout = 30_000, 'fetch-retries': maxRetries = 3, identity = '', 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, } = options;
50
56
  this.identity = identity;
57
+ this.staleWhileRevalidateFactor = staleWhileRevalidateFactor;
58
+ const path = resolve(cache, 'registry-client');
51
59
  this.cache = new Cache({
52
- path: cache,
60
+ path,
53
61
  onDiskWrite(_path, key, data) {
54
- if (CacheEntry.isGzipEntry(data))
55
- register(cache, key);
62
+ if (CacheEntry.isGzipEntry(data)) {
63
+ cacheUnzipRegister(path, key);
64
+ }
56
65
  },
57
66
  });
58
67
  const dispatch = new Agent(agentOptions);
@@ -103,11 +112,11 @@ export class RegistryClient {
103
112
  const s = tok.replace(/^(Bearer|Basic) /i, '');
104
113
  const tokensUrl = new URL('-/npm/v1/tokens', registry);
105
114
  const record = await this.seek(tokensUrl, ({ token }) => s.startsWith(token), {
106
- cache: false,
115
+ useCache: false,
107
116
  }).catch(() => undefined);
108
117
  if (record) {
109
118
  const { key } = record;
110
- await this.request(new URL(`-/npm/v1/tokens/token/${key}`, registry), { cache: false, method: 'DELETE' });
119
+ await this.request(new URL(`-/npm/v1/tokens/token/${key}`, registry), { useCache: false, method: 'DELETE' });
111
120
  }
112
121
  await deleteToken(registry, this.identity);
113
122
  }
@@ -128,7 +137,7 @@ export class RegistryClient {
128
137
  const webLoginURL = new URL('-/v1/login', registry);
129
138
  const response = await this.request(webLoginURL, {
130
139
  method: 'POST',
131
- cache: false,
140
+ useCache: false,
132
141
  headers: {
133
142
  'content-type': 'application/json',
134
143
  'npm-auth-type': 'web',
@@ -162,7 +171,7 @@ export class RegistryClient {
162
171
  return result;
163
172
  }),
164
173
  urlOpen(loginUrl, { signal }).catch((er) => {
165
- if (er.name === 'AbortError')
174
+ if (asError(er).name === 'AbortError')
166
175
  return;
167
176
  ac.abort();
168
177
  throw er;
@@ -174,7 +183,7 @@ export class RegistryClient {
174
183
  async #checkLogin(url, options = {}) {
175
184
  const response = await this.request(url, {
176
185
  ...options,
177
- cache: false,
186
+ useCache: false,
178
187
  });
179
188
  const { signal } = options;
180
189
  if (response.statusCode === 202) {
@@ -197,23 +206,27 @@ export class RegistryClient {
197
206
  async request(url, options = {}) {
198
207
  logRequest(url, 'start');
199
208
  const u = typeof url === 'string' ? new URL(url) : url;
200
- const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), } = options;
201
- const { cache = method === 'GET' || method === 'HEAD' } = options;
209
+ const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, } = options;
210
+ let { trustIntegrity } = options;
211
+ const m = isCacheableMethod(method) ? method : undefined;
212
+ const { useCache = !!m } = options;
202
213
  signal?.throwIfAborted();
203
214
  // first, try to get from the cache before making any request.
204
- const { origin, pathname } = u;
205
- const key = JSON.stringify([origin, method, pathname]);
206
- const buffer = cache ?
215
+ const { origin } = u;
216
+ const key = `${method !== 'GET' ? method + ' ' : ''}${u}`;
217
+ const buffer = useCache ?
207
218
  await this.cache.fetch(key, { context: { integrity } })
208
219
  : undefined;
209
220
  const entry = buffer ? CacheEntry.decode(buffer) : undefined;
210
221
  if (entry?.valid) {
211
222
  return entry;
212
223
  }
213
- // TODO: stale-while-revalidate timeout, say 1 day, where we'll
214
- // use the cached response even if it's invalid, and validate
215
- // in the background without waiting for it.
216
- // either no cache entry, or need to revalidate it.
224
+ if (staleWhileRevalidate && entry?.staleWhileRevalidate && m) {
225
+ // revalidate while returning the stale entry
226
+ register(this.cache.path(), m, url);
227
+ return entry;
228
+ }
229
+ // either no cache entry, or need to revalidate before use.
217
230
  setCacheHeaders(options, entry);
218
231
  redirections.add(String(url));
219
232
  Object.assign(options, {
@@ -232,8 +245,17 @@ export class RegistryClient {
232
245
  // will remove if we don't have a token.
233
246
  options.headers = addHeader(options.headers, 'authorization', await getToken(origin, this.identity));
234
247
  const result = await this.#handleResponse(u, options, await this.agent.request(options), entry);
235
- if (cache)
236
- this.cache.set(key, result.encode());
248
+ if (result.getHeader('integrity')) {
249
+ trustIntegrity = true;
250
+ }
251
+ if (result.isGzip && !trustIntegrity) {
252
+ result.checkIntegrity({ url });
253
+ }
254
+ if (useCache) {
255
+ this.cache.set(key, result.encode(), {
256
+ integrity: result.integrity,
257
+ });
258
+ }
237
259
  return result;
238
260
  }
239
261
  async #handleResponse(url, options, response, entry) {
@@ -258,22 +280,18 @@ export class RegistryClient {
258
280
  const { integrity, trustIntegrity } = options;
259
281
  const result = new CacheEntry(
260
282
  /* c8 ignore next - should always have a status code */
261
- response.statusCode || 200, h, { integrity, trustIntegrity });
283
+ response.statusCode || 200, h, {
284
+ integrity,
285
+ trustIntegrity,
286
+ 'stale-while-revalidate-factor': this.staleWhileRevalidateFactor,
287
+ });
262
288
  if (isRedirect(result)) {
263
289
  response.body.resume();
264
- // remove the try/catch once rebasing onto main with Luke's error-cause stuff
265
- try {
266
- const [nextURL, nextOptions] = redirect(options, result, url);
267
- if (nextOptions && nextURL) {
268
- return await this.request(nextURL, nextOptions);
269
- }
270
- return result;
271
- }
272
- catch (er) {
273
- /* c8 ignore start */
274
- throw er instanceof Error ? er : (new Error(typeof er === 'string' ? er : 'Unknown error'));
275
- /* c8 ignore stop */
290
+ const [nextURL, nextOptions] = redirect(options, result, url);
291
+ if (nextOptions && nextURL) {
292
+ return await this.request(nextURL, nextOptions);
276
293
  }
294
+ return result;
277
295
  }
278
296
  response.body.on('data', (chunk) => result.addBody(chunk));
279
297
  return await new Promise((res, rej) => {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAExD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EACL,WAAW,EACX,KAAK,EACL,QAAQ,EACR,OAAO,EACP,SAAS,EACT,QAAQ,GACT,MAAM,WAAW,CAAA;AAElB,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,eAAe,EAAE,MAA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E,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;IACJ,CAAC;CACF","sourcesContent":["import { Cache } from '@vltpkg/cache'\nimport { register } from '@vltpkg/cache-unzip'\nimport { error } from '@vltpkg/error-cause'\nimport { logRequest } from '@vltpkg/output'\nimport type { Integrity } from '@vltpkg/types'\nimport { urlOpen } from '@vltpkg/url-open'\nimport { XDG } from '@vltpkg/xdg'\nimport { setTimeout } from 'node:timers/promises'\nimport { loadPackageJson } from 'package-json-from-dist'\nimport type { Dispatcher } from 'undici'\nimport { Agent, RetryAgent } from 'undici'\nimport { addHeader } from './add-header.ts'\nimport type { Token } from './auth.ts'\nimport {\n deleteToken,\n getKC,\n getToken,\n isToken,\n keychains,\n setToken,\n} from './auth.ts'\nimport type { JSONObj } from './cache-entry.ts'\nimport { CacheEntry } from './cache-entry.ts'\nimport { bun, deno, node } from './env.ts'\nimport { handle304Response } from './handle-304-response.ts'\nimport { otplease } from './otplease.ts'\nimport { isRedirect, redirect } from './redirect.ts'\nimport { setCacheHeaders } from './set-cache-headers.ts'\nimport type { TokenResponse } from './token-response.ts'\nimport { isTokenResponse } from './token-response.ts'\nimport type { WebAuthChallenge } from './web-auth-challenge.ts'\nimport { isWebAuthChallenge } from './web-auth-challenge.ts'\nexport {\n deleteToken,\n getKC,\n isToken,\n keychains,\n setToken,\n type CacheEntry,\n type JSONObj,\n type Token,\n type TokenResponse,\n type WebAuthChallenge,\n}\n\nexport type RegistryClientOptions = {\n /**\n * Path on disk where the cache should be stored\n * @default `$HOME/.config/vlt/cache`\n */\n cache?: string\n /**\n * Number of retries to perform when encountering network errors or\n * likely-transient errors from git hosts.\n */\n 'fetch-retries'?: number\n /** The exponential backoff factor to use when retrying git hosts */\n 'fetch-retry-factor'?: number\n /** Number of milliseconds before starting first retry */\n 'fetch-retry-mintimeout'?: number\n /** Maximum number of milliseconds between two retries */\n 'fetch-retry-maxtimeout'?: number\n\n /** the identity to use for storing auth tokens */\n identity?: string\n}\n\nexport type RegistryClientRequestOptions = Omit<\n Dispatcher.RequestOptions,\n 'method' | 'path'\n> & {\n /**\n * `path` should not be set when using the RegistryClient.\n * It will be overwritten with the path on the URL being requested.\n * This only here for compliance with the DispatchOptions base type.\n * @deprecated\n */\n path?: string\n\n /**\n * Method is optional, defaults to 'GET'\n */\n method?: Dispatcher.DispatchOptions['method']\n /**\n * Provide an SRI string to verify integrity of the item being fetched.\n *\n * This is only relevant when it must make a request to the registry. Once in\n * the local disk cache, items are assumed to be trustworthy.\n */\n integrity?: Integrity\n\n /**\n * Set to true if the integrity should be trusted implicitly without\n * a recalculation, for example if it comes from a trusted registry that\n * also serves the tarball itself.\n */\n trustIntegrity?: boolean\n\n /**\n * Follow up to 10 redirections by default. Set this to 0 to just return\n * the 3xx response. If the max redirections are expired, and we still get\n * a redirection response, then fail the request. Redirection cycles are\n * always treated as an error.\n */\n maxRedirections?: number\n\n /**\n * the number of redirections that have already been seen. This is used\n * internally, and should always start at 0.\n * @internal\n */\n redirections?: Set<string>\n\n /**\n * Set to `false` to suppress ANY lookups from cache. This will also\n * prevent storing the result to the cache.\n */\n cache?: false\n\n /**\n * Set to pass an `npm-otp` header on the request.\n *\n * This should not be set except by the RegistryClient itself, when\n * we receive a 401 response with an OTP challenge.\n * @internal\n */\n otp?: string\n}\n\nconst { version } = loadPackageJson(\n import.meta.filename,\n process.env.__VLT_INTERNAL_REGISTRY_CLIENT_PACKAGE_JSON,\n) as {\n version: string\n}\nconst nua =\n (globalThis.navigator as Navigator | undefined)?.userAgent ??\n (bun ? `Bun/${bun}`\n : deno ? `Deno/${deno}`\n : node ? `Node.js/${node}`\n : '(unknown platform)')\nexport const userAgent = `@vltpkg/registry-client/${version} ${nua}`\n\nconst agentOptions: Agent.Options = {\n bodyTimeout: 600_000,\n headersTimeout: 600_000,\n keepAliveMaxTimeout: 1_200_000,\n keepAliveTimeout: 600_000,\n keepAliveTimeoutThreshold: 30_000,\n connect: {\n timeout: 600_000,\n keepAlive: true,\n keepAliveInitialDelay: 30_000,\n sessionTimeout: 600,\n },\n connections: 128,\n pipelining: 10,\n}\n\nconst xdg = new XDG('vlt')\n\nexport class RegistryClient {\n agent: RetryAgent\n cache: Cache\n identity: string\n\n constructor(options: RegistryClientOptions) {\n const {\n cache = xdg.cache('registry-client'),\n 'fetch-retry-factor': timeoutFactor = 2,\n 'fetch-retry-mintimeout': minTimeout = 0,\n 'fetch-retry-maxtimeout': maxTimeout = 30_000,\n 'fetch-retries': maxRetries = 3,\n identity = '',\n } = options\n this.identity = identity\n this.cache = new Cache({\n path: cache,\n onDiskWrite(_path, key, data) {\n if (CacheEntry.isGzipEntry(data)) register(cache, key)\n },\n })\n const dispatch = new Agent(agentOptions)\n this.agent = new RetryAgent(dispatch, {\n maxRetries,\n timeoutFactor,\n minTimeout,\n maxTimeout,\n retryAfter: true,\n errorCodes: [\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'EHOSTDOWN',\n 'ENETDOWN',\n 'ENETUNREACH',\n 'ENOTFOUND',\n 'EPIPE',\n 'UND_ERR_SOCKET',\n ],\n })\n }\n\n /**\n * Fetch the entire set of a paginated list of objects\n */\n async scroll<T>(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n seek?: (obj: T) => boolean,\n ): Promise<T[]> {\n const resp = await this.request(url, options)\n const { objects, urls } = resp.json() as {\n objects: T[]\n urls: { next?: string }\n }\n // if we have more, and haven't found our target, fetch more\n return urls.next && !(seek && objects.some(seek)) ?\n objects.concat(await this.scroll<T>(urls.next, options, seek))\n : objects\n }\n\n /**\n * find a given item in a paginated set\n */\n async seek<T>(\n url: URL | string,\n seek: (obj: T) => boolean,\n options: RegistryClientRequestOptions = {},\n ): Promise<T | undefined> {\n return (await this.scroll(url, options, seek)).find(seek)\n }\n\n /**\n * Log out from the registry specified, attempting to destroy the\n * token if the registry supports that endpoint.\n */\n async logout(registry: string) {\n // if we have no token for that registry, nothing to do\n const tok = await getToken(registry, this.identity)\n if (!tok) return\n\n const s = tok.replace(/^(Bearer|Basic) /i, '')\n\n const tokensUrl = new URL('-/npm/v1/tokens', registry)\n const record = await this.seek<{\n key: string\n token: string\n }>(tokensUrl, ({ token }) => s.startsWith(token), {\n cache: false,\n }).catch(() => undefined)\n\n if (record) {\n const { key } = record\n await this.request(\n new URL(`-/npm/v1/tokens/token/${key}`, registry),\n { cache: false, method: 'DELETE' },\n )\n }\n\n await deleteToken(registry, this.identity)\n }\n\n /**\n * Log into the registry specified\n *\n * Does not return the token or expose it, just saves to the auth keychain\n * and returns void if it worked. Otherwise, error is raised.\n */\n async login(registry: string) {\n // - make POST to '/-/v1/login'\n // - include a body of {} and npm-auth-type:web\n // - get a {doneUrl, loginUrl}\n // - open the loginUrl\n // - hang on the doneUrl until done\n //\n // if that fails: fall back to couchdb login\n const webLoginURL = new URL('-/v1/login', registry)\n const response = await this.request(webLoginURL, {\n method: 'POST',\n cache: false,\n headers: {\n 'content-type': 'application/json',\n 'npm-auth-type': 'web',\n },\n body: '{}',\n })\n\n if (response.statusCode === 200) {\n const challenge = response.json()\n if (isWebAuthChallenge(challenge)) {\n const result = await this.webAuthOpener(challenge)\n await setToken(\n registry,\n `Bearer ${result.token}`,\n this.identity,\n )\n return\n }\n }\n /* c8 ignore start */\n // TODO: fall back to username/password login, and/or couchdb PUT login\n throw error('Failed to perform web login', { response })\n }\n /* c8 ignore stop */\n\n /**\n * Given a {@link WebAuthChallenge}, open the `loginUrl` in a browser and\n * hang on the `doneUrl` until it returns a {@link TokenResponse} object.\n */\n async webAuthOpener({ doneUrl, loginUrl }: WebAuthChallenge) {\n const ac = new AbortController()\n const { signal } = ac\n /* c8 ignore start - race condition */\n const [result] = await Promise.all([\n this.#checkLogin(doneUrl, { signal }).then(result => {\n ac.abort()\n return result\n }),\n urlOpen(loginUrl, { signal }).catch((er: unknown) => {\n if ((er as Error).name === 'AbortError') return\n ac.abort()\n throw er\n }),\n ])\n /* c8 ignore stop */\n return result\n }\n\n async #checkLogin(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n ): Promise<TokenResponse> {\n const response = await this.request(url, {\n ...options,\n cache: false,\n })\n const { signal } = options as { signal?: AbortSignal }\n if (response.statusCode === 202) {\n const rt = response.getHeader('retry-after')\n const retryAfter = rt ? Number(rt.toString()) : -1\n if (retryAfter > 0) {\n await setTimeout(retryAfter * 1000, null, { signal })\n }\n return await this.#checkLogin(url, options)\n }\n if (response.statusCode === 200) {\n const body = response.json()\n if (isTokenResponse(body)) return body\n }\n throw error('Invalid response from web login endpoint', {\n response,\n })\n }\n\n async request(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n ): Promise<CacheEntry> {\n logRequest(url, 'start')\n\n const u = typeof url === 'string' ? new URL(url) : url\n const {\n method = 'GET',\n integrity,\n redirections = new Set(),\n signal,\n otp = (process.env.VLT_OTP ?? '').trim(),\n } = options\n\n const { cache = method === 'GET' || method === 'HEAD' } = options\n\n ;(signal as AbortSignal | null)?.throwIfAborted()\n\n // first, try to get from the cache before making any request.\n const { origin, pathname } = u\n const key = JSON.stringify([origin, method, pathname])\n const buffer =\n cache ?\n await this.cache.fetch(key, { context: { integrity } })\n : undefined\n\n const entry = buffer ? CacheEntry.decode(buffer) : undefined\n if (entry?.valid) {\n return entry\n }\n // TODO: stale-while-revalidate timeout, say 1 day, where we'll\n // use the cached response even if it's invalid, and validate\n // in the background without waiting for it.\n\n // either no cache entry, or need to revalidate it.\n setCacheHeaders(options, entry)\n\n redirections.add(String(url))\n\n Object.assign(options, {\n path: u.pathname.replace(/\\/+$/, '') + u.search,\n ...agentOptions,\n })\n\n options.origin = u.origin\n options.headers = addHeader(\n addHeader(\n options.headers,\n 'accept-encoding',\n 'gzip;q=1.0, identity;q=0.5',\n ),\n 'user-agent',\n userAgent,\n )\n if (otp) {\n options.headers = addHeader(options.headers, 'npm-otp', otp)\n }\n if (integrity) {\n options.headers = addHeader(\n options.headers,\n 'accept-integrity',\n integrity,\n )\n }\n options.method = options.method ?? 'GET'\n\n // will remove if we don't have a token.\n options.headers = addHeader(\n options.headers,\n 'authorization',\n await getToken(origin, this.identity),\n )\n\n const result = await this.#handleResponse(\n u,\n options,\n await this.agent.request(options as Dispatcher.RequestOptions),\n entry,\n )\n\n if (cache) this.cache.set(key, result.encode())\n return result\n }\n\n async #handleResponse(\n url: URL,\n options: RegistryClientRequestOptions,\n response: Dispatcher.ResponseData,\n entry?: CacheEntry,\n ): Promise<CacheEntry> {\n if (handle304Response(response, entry)) return entry\n\n if (response.statusCode === 401) {\n const repeatRequest = await otplease(this, options, response)\n if (repeatRequest) return await this.request(url, repeatRequest)\n }\n\n const h: Buffer[] = []\n for (const [key, value] of Object.entries(response.headers)) {\n /* c8 ignore start - theoretical */\n if (Array.isArray(value)) {\n h.push(Buffer.from(key), Buffer.from(value.join(', ')))\n /* c8 ignore stop */\n } else if (typeof value === 'string') {\n h.push(Buffer.from(key), Buffer.from(value))\n }\n }\n\n const { integrity, trustIntegrity } = options\n const result = new CacheEntry(\n /* c8 ignore next - should always have a status code */\n response.statusCode || 200,\n h,\n { integrity, trustIntegrity },\n )\n\n if (isRedirect(result)) {\n response.body.resume()\n // remove the try/catch once rebasing onto main with Luke's error-cause stuff\n try {\n const [nextURL, nextOptions] = redirect(options, result, url)\n if (nextOptions && nextURL) {\n return await this.request(nextURL, nextOptions)\n }\n return result\n } catch (er) {\n /* c8 ignore start */\n throw er instanceof Error ? er : (\n new Error(typeof er === 'string' ? er : 'Unknown error')\n )\n /* c8 ignore stop */\n }\n }\n\n response.body.on('data', (chunk: Buffer) => result.addBody(chunk))\n return await new Promise<CacheEntry>((res, rej) => {\n response.body.on('error', rej)\n response.body.on('end', () => res(result))\n })\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,M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;YAC7D,IAAI,WAAW,IAAI,OAAO,EAAE,CAAC;gBAC3B,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;YACjD,CAAC;YACD,OAAO,MAAM,CAAA;QACf,CAAC;QAED,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;QAClE,OAAO,MAAM,IAAI,OAAO,CAAa,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YAChD,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;YAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;IACJ,CAAC;CACF","sourcesContent":["import { Cache } from '@vltpkg/cache'\nimport { register as cacheUnzipRegister } from '@vltpkg/cache-unzip'\nimport { asError, error } from '@vltpkg/error-cause'\nimport { logRequest } from '@vltpkg/output'\nimport type { Integrity } from '@vltpkg/types'\nimport { urlOpen } from '@vltpkg/url-open'\nimport { XDG } from '@vltpkg/xdg'\nimport { resolve } from 'node:path'\nimport { setTimeout } from 'node:timers/promises'\nimport { loadPackageJson } from 'package-json-from-dist'\nimport type { Dispatcher } from 'undici'\nimport { Agent, RetryAgent } from 'undici'\nimport { addHeader } from './add-header.ts'\nimport type { Token } from './auth.ts'\nimport {\n deleteToken,\n getKC,\n getToken,\n isToken,\n keychains,\n setToken,\n} from './auth.ts'\nimport type { JSONObj } from './cache-entry.ts'\nimport { CacheEntry } from './cache-entry.ts'\nimport { register } from './cache-revalidate.ts'\nimport { bun, deno, node } from './env.ts'\nimport { handle304Response } from './handle-304-response.ts'\nimport { otplease } from './otplease.ts'\nimport { isRedirect, redirect } from './redirect.ts'\nimport { setCacheHeaders } from './set-cache-headers.ts'\nimport type { TokenResponse } from './token-response.ts'\nimport { isTokenResponse } from './token-response.ts'\nimport type { WebAuthChallenge } from './web-auth-challenge.ts'\nimport { isWebAuthChallenge } from './web-auth-challenge.ts'\nexport {\n CacheEntry,\n deleteToken,\n getKC,\n isToken,\n keychains,\n setToken,\n type JSONObj,\n type Token,\n type TokenResponse,\n type WebAuthChallenge,\n}\n\nexport type CacheableMethod = 'GET' | 'HEAD'\nexport const isCacheableMethod = (m: unknown): m is CacheableMethod =>\n m === 'GET' || m === 'HEAD'\n\nexport type RegistryClientOptions = {\n /**\n * Path on disk where the cache should be stored\n *\n * Defaults to the XDG cache folder for `vlt/registry-client`\n */\n cache?: string\n /**\n * Number of retries to perform when encountering network errors or\n * likely-transient errors from git hosts.\n */\n 'fetch-retries'?: number\n /** The exponential backoff factor to use when retrying git hosts */\n 'fetch-retry-factor'?: number\n /** Number of milliseconds before starting first retry */\n 'fetch-retry-mintimeout'?: number\n /** Maximum number of milliseconds between two retries */\n 'fetch-retry-maxtimeout'?: number\n\n /** the identity to use for storing auth tokens */\n identity?: string\n\n /**\n * If the server does not serve a `stale-while-revalidate` value in the\n * `cache-control` header, then this multiplier is applied to the `max-age`\n * or `s-maxage` values.\n *\n * By default, this is `60`, so for example a response that is cacheable for\n * 5 minutes will allow a stale response while revalidating for up to 5\n * hours.\n *\n * If the server *does* provide a `stale-while-revalidate` value, then that\n * is always used.\n *\n * Set to 0 to prevent any `stale-while-revalidate` behavior unless\n * explicitly allowed by the server's `cache-control` header.\n */\n 'stale-while-revalidate-factor'?: number\n}\n\nexport type RegistryClientRequestOptions = Omit<\n Dispatcher.RequestOptions,\n 'method' | 'path'\n> & {\n /**\n * `path` should not be set when using the RegistryClient.\n * It will be overwritten with the path on the URL being requested.\n * This only here for compliance with the DispatchOptions base type.\n * @deprecated\n */\n path?: string\n\n /**\n * Method is optional, defaults to 'GET'\n */\n method?: Dispatcher.DispatchOptions['method']\n /**\n * Provide an SRI string to verify integrity of the item being fetched.\n *\n * This is only relevant when it must make a request to the registry. Once in\n * the local disk cache, items are assumed to be trustworthy.\n */\n integrity?: Integrity\n\n /**\n * Set to true if the integrity should be trusted implicitly without\n * a recalculation, for example if it comes from a trusted registry that\n * also serves the tarball itself.\n */\n trustIntegrity?: boolean\n\n /**\n * Follow up to 10 redirections by default. Set this to 0 to just return\n * the 3xx response. If the max redirections are expired, and we still get\n * a redirection response, then fail the request. Redirection cycles are\n * always treated as an error.\n */\n maxRedirections?: number\n\n /**\n * the number of redirections that have already been seen. This is used\n * internally, and should always start at 0.\n * @internal\n */\n redirections?: Set<string>\n\n /**\n * Set to `false` to suppress ANY lookups from cache. This will also\n * prevent storing the result to the cache.\n */\n useCache?: false\n\n /**\n * Set to pass an `npm-otp` header on the request.\n *\n * This should not be set except by the RegistryClient itself, when\n * we receive a 401 response with an OTP challenge.\n * @internal\n */\n otp?: string\n\n /**\n * Set to false to explicitly prevent `stale-while-revalidate` behavior,\n * for use in revalidating while stale.\n * @internal\n */\n staleWhileRevalidate?: false\n}\n\nconst { version } = loadPackageJson(\n import.meta.filename,\n process.env.__VLT_INTERNAL_REGISTRY_CLIENT_PACKAGE_JSON,\n) as {\n version: string\n}\n/* c8 ignore start - we do test this, but coverage fails */\nconst nua =\n (globalThis.navigator as Navigator | undefined)?.userAgent ??\n (bun ? `Bun/${bun}`\n : deno ? `Deno/${deno}`\n : node ? `Node.js/${node}`\n : '(unknown platform)')\n/* c8 ignore stop */\nexport const userAgent = `@vltpkg/registry-client/${version} ${nua}`\n\nconst agentOptions: Agent.Options = {\n bodyTimeout: 600_000,\n headersTimeout: 600_000,\n keepAliveMaxTimeout: 1_200_000,\n keepAliveTimeout: 600_000,\n keepAliveTimeoutThreshold: 30_000,\n connect: {\n timeout: 600_000,\n keepAlive: true,\n keepAliveInitialDelay: 30_000,\n sessionTimeout: 600,\n },\n connections: 128,\n pipelining: 10,\n}\n\nconst xdg = new XDG('vlt')\n\nexport class RegistryClient {\n agent: RetryAgent\n cache: Cache\n identity: string\n staleWhileRevalidateFactor: number\n\n constructor(options: RegistryClientOptions) {\n const {\n cache = xdg.cache(),\n 'fetch-retry-factor': timeoutFactor = 2,\n 'fetch-retry-mintimeout': minTimeout = 0,\n 'fetch-retry-maxtimeout': maxTimeout = 30_000,\n 'fetch-retries': maxRetries = 3,\n identity = '',\n 'stale-while-revalidate-factor':\n staleWhileRevalidateFactor = 60,\n } = options\n this.identity = identity\n this.staleWhileRevalidateFactor = staleWhileRevalidateFactor\n const path = resolve(cache, 'registry-client')\n this.cache = new Cache({\n path,\n onDiskWrite(_path, key, data) {\n if (CacheEntry.isGzipEntry(data)) {\n cacheUnzipRegister(path, key)\n }\n },\n })\n const dispatch = new Agent(agentOptions)\n this.agent = new RetryAgent(dispatch, {\n maxRetries,\n timeoutFactor,\n minTimeout,\n maxTimeout,\n retryAfter: true,\n errorCodes: [\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'EHOSTDOWN',\n 'ENETDOWN',\n 'ENETUNREACH',\n 'ENOTFOUND',\n 'EPIPE',\n 'UND_ERR_SOCKET',\n ],\n })\n }\n\n /**\n * Fetch the entire set of a paginated list of objects\n */\n async scroll<T>(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n seek?: (obj: T) => boolean,\n ): Promise<T[]> {\n const resp = await this.request(url, options)\n const { objects, urls } = resp.json() as {\n objects: T[]\n urls: { next?: string }\n }\n // if we have more, and haven't found our target, fetch more\n return urls.next && !(seek && objects.some(seek)) ?\n objects.concat(await this.scroll<T>(urls.next, options, seek))\n : objects\n }\n\n /**\n * find a given item in a paginated set\n */\n async seek<T>(\n url: URL | string,\n seek: (obj: T) => boolean,\n options: RegistryClientRequestOptions = {},\n ): Promise<T | undefined> {\n return (await this.scroll(url, options, seek)).find(seek)\n }\n\n /**\n * Log out from the registry specified, attempting to destroy the\n * token if the registry supports that endpoint.\n */\n async logout(registry: string) {\n // if we have no token for that registry, nothing to do\n const tok = await getToken(registry, this.identity)\n if (!tok) return\n\n const s = tok.replace(/^(Bearer|Basic) /i, '')\n\n const tokensUrl = new URL('-/npm/v1/tokens', registry)\n const record = await this.seek<{\n key: string\n token: string\n }>(tokensUrl, ({ token }) => s.startsWith(token), {\n useCache: false,\n }).catch(() => undefined)\n\n if (record) {\n const { key } = record\n await this.request(\n new URL(`-/npm/v1/tokens/token/${key}`, registry),\n { useCache: false, method: 'DELETE' },\n )\n }\n\n await deleteToken(registry, this.identity)\n }\n\n /**\n * Log into the registry specified\n *\n * Does not return the token or expose it, just saves to the auth keychain\n * and returns void if it worked. Otherwise, error is raised.\n */\n async login(registry: string) {\n // - make POST to '/-/v1/login'\n // - include a body of {} and npm-auth-type:web\n // - get a {doneUrl, loginUrl}\n // - open the loginUrl\n // - hang on the doneUrl until done\n //\n // if that fails: fall back to couchdb login\n const webLoginURL = new URL('-/v1/login', registry)\n const response = await this.request(webLoginURL, {\n method: 'POST',\n useCache: false,\n headers: {\n 'content-type': 'application/json',\n 'npm-auth-type': 'web',\n },\n body: '{}',\n })\n\n if (response.statusCode === 200) {\n const challenge = response.json()\n if (isWebAuthChallenge(challenge)) {\n const result = await this.webAuthOpener(challenge)\n await setToken(\n registry,\n `Bearer ${result.token}`,\n this.identity,\n )\n return\n }\n }\n /* c8 ignore start */\n // TODO: fall back to username/password login, and/or couchdb PUT login\n throw error('Failed to perform web login', { response })\n }\n /* c8 ignore stop */\n\n /**\n * Given a {@link WebAuthChallenge}, open the `loginUrl` in a browser and\n * hang on the `doneUrl` until it returns a {@link TokenResponse} object.\n */\n async webAuthOpener({ doneUrl, loginUrl }: WebAuthChallenge) {\n const ac = new AbortController()\n const { signal } = ac\n /* c8 ignore start - race condition */\n const [result] = await Promise.all([\n this.#checkLogin(doneUrl, { signal }).then(result => {\n ac.abort()\n return result\n }),\n urlOpen(loginUrl, { signal }).catch((er: unknown) => {\n if (asError(er).name === 'AbortError') return\n ac.abort()\n throw er\n }),\n ])\n /* c8 ignore stop */\n return result\n }\n\n async #checkLogin(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n ): Promise<TokenResponse> {\n const response = await this.request(url, {\n ...options,\n useCache: false,\n })\n const { signal } = options as { signal?: AbortSignal }\n if (response.statusCode === 202) {\n const rt = response.getHeader('retry-after')\n const retryAfter = rt ? Number(rt.toString()) : -1\n if (retryAfter > 0) {\n await setTimeout(retryAfter * 1000, null, { signal })\n }\n return await this.#checkLogin(url, options)\n }\n if (response.statusCode === 200) {\n const body = response.json()\n if (isTokenResponse(body)) return body\n }\n throw error('Invalid response from web login endpoint', {\n response,\n })\n }\n\n async request(\n url: URL | string,\n options: RegistryClientRequestOptions = {},\n ): Promise<CacheEntry> {\n logRequest(url, 'start')\n\n const u = typeof url === 'string' ? new URL(url) : url\n const {\n method = 'GET',\n integrity,\n redirections = new Set(),\n signal,\n otp = (process.env.VLT_OTP ?? '').trim(),\n staleWhileRevalidate = true,\n } = options\n let { trustIntegrity } = options\n\n const m = isCacheableMethod(method) ? method : undefined\n const { useCache = !!m } = options\n\n ;(signal as AbortSignal | null)?.throwIfAborted()\n\n // first, try to get from the cache before making any request.\n const { origin } = u\n const key = `${method !== 'GET' ? method + ' ' : ''}${u}`\n const buffer =\n useCache ?\n await this.cache.fetch(key, { context: { integrity } })\n : undefined\n\n const entry = buffer ? CacheEntry.decode(buffer) : undefined\n if (entry?.valid) {\n return entry\n }\n\n if (staleWhileRevalidate && entry?.staleWhileRevalidate && m) {\n // revalidate while returning the stale entry\n register(this.cache.path(), m, url)\n return entry\n }\n\n // either no cache entry, or need to revalidate before use.\n setCacheHeaders(options, entry)\n\n redirections.add(String(url))\n\n Object.assign(options, {\n path: u.pathname.replace(/\\/+$/, '') + u.search,\n ...agentOptions,\n })\n\n options.origin = u.origin\n options.headers = addHeader(\n addHeader(\n options.headers,\n 'accept-encoding',\n 'gzip;q=1.0, identity;q=0.5',\n ),\n 'user-agent',\n userAgent,\n )\n if (otp) {\n options.headers = addHeader(options.headers, 'npm-otp', otp)\n }\n if (integrity) {\n options.headers = addHeader(\n options.headers,\n 'accept-integrity',\n integrity,\n )\n }\n options.method = options.method ?? 'GET'\n\n // will remove if we don't have a token.\n options.headers = addHeader(\n options.headers,\n 'authorization',\n await getToken(origin, this.identity),\n )\n\n const result = await this.#handleResponse(\n u,\n options,\n await this.agent.request(options as Dispatcher.RequestOptions),\n entry,\n )\n\n if (result.getHeader('integrity')) {\n trustIntegrity = true\n }\n\n if (result.isGzip && !trustIntegrity) {\n result.checkIntegrity({ url })\n }\n if (useCache) {\n this.cache.set(key, result.encode(), {\n integrity: result.integrity,\n })\n }\n return result\n }\n\n async #handleResponse(\n url: URL,\n options: RegistryClientRequestOptions,\n response: Dispatcher.ResponseData,\n entry?: CacheEntry,\n ): Promise<CacheEntry> {\n if (handle304Response(response, entry)) return entry\n\n if (response.statusCode === 401) {\n const repeatRequest = await otplease(this, options, response)\n if (repeatRequest) return await this.request(url, repeatRequest)\n }\n\n const h: Buffer[] = []\n for (const [key, value] of Object.entries(response.headers)) {\n /* c8 ignore start - theoretical */\n if (Array.isArray(value)) {\n h.push(Buffer.from(key), Buffer.from(value.join(', ')))\n /* c8 ignore stop */\n } else if (typeof value === 'string') {\n h.push(Buffer.from(key), Buffer.from(value))\n }\n }\n\n const { integrity, trustIntegrity } = options\n const result = new CacheEntry(\n /* c8 ignore next - should always have a status code */\n response.statusCode || 200,\n h,\n {\n integrity,\n trustIntegrity,\n 'stale-while-revalidate-factor':\n this.staleWhileRevalidateFactor,\n },\n )\n\n if (isRedirect(result)) {\n response.body.resume()\n const [nextURL, nextOptions] = redirect(options, result, url)\n if (nextOptions && nextURL) {\n return await this.request(nextURL, nextOptions)\n }\n return result\n }\n\n response.body.on('data', (chunk: Buffer) => result.addBody(chunk))\n return await new Promise<CacheEntry>((res, rej) => {\n response.body.on('error', rej)\n response.body.on('end', () => res(result))\n })\n }\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export declare const __CODE_SPLIT_SCRIPT_NAME: string;
2
+ export declare const main: (cache?: string, input?: NodeJS.ReadStream & {
3
+ fd: 0;
4
+ }) => Promise<void>;
5
+ //# sourceMappingURL=revalidate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"revalidate.d.ts","sourceRoot":"","sources":["../../src/revalidate.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,wBAAwB,QAAuB,CAAA;AAM5D,eAAO,MAAM,IAAI,WAAkB,MAAM;;mBAuCxC,CAAA"}
@@ -0,0 +1,49 @@
1
+ // This needs to live in the same workspace as the RegistryClient, because
2
+ // otherwise we have a cyclical dependency cycle of dependencies in a cycle,
3
+ // which is even more cyclical than this description describing it.
4
+ import { pathToFileURL } from 'node:url';
5
+ import { RegistryClient } from "./index.js";
6
+ export const __CODE_SPLIT_SCRIPT_NAME = import.meta.filename;
7
+ const isMain = (path) => path === __CODE_SPLIT_SCRIPT_NAME ||
8
+ path === pathToFileURL(__CODE_SPLIT_SCRIPT_NAME).toString();
9
+ export const main = async (cache, input = process.stdin) => {
10
+ if (!cache)
11
+ process.exit(1);
12
+ const reqs = await new Promise(res => {
13
+ const chunks = [];
14
+ let chunkLen = 0;
15
+ input.on('data', chunk => {
16
+ chunks.push(chunk);
17
+ chunkLen += chunk.length;
18
+ });
19
+ input.on('end', () => {
20
+ const reqs = Buffer.concat(chunks, chunkLen)
21
+ .toString()
22
+ .split('\0')
23
+ .filter(i => !!i && (i.startsWith('GET ') || i.startsWith('HEAD ')))
24
+ .map(i => i.startsWith('GET ') ?
25
+ ['GET', new URL(i.substring('GET '.length))]
26
+ : ['HEAD', new URL(i.substring('HEAD '.length))]);
27
+ res(reqs);
28
+ });
29
+ });
30
+ if (!reqs.length)
31
+ process.exit(1);
32
+ const rc = new RegistryClient({ cache });
33
+ await Promise.all(reqs.map(async ([method, url]) => {
34
+ await rc.request(url, {
35
+ method,
36
+ staleWhileRevalidate: false,
37
+ });
38
+ }));
39
+ };
40
+ const g = globalThis;
41
+ if (isMain(g.__VLT_INTERNAL_MAIN ?? process.argv[1])) {
42
+ process.title = 'vlt-cache-revalidate';
43
+ // When compiled there can be other leading args supplied by Deno
44
+ // so always use the last arg unless there are only two which means
45
+ // no path was supplied.
46
+ const cacheFolder = process.argv.length === 2 ? undefined : process.argv.at(-1);
47
+ void main(cacheFolder, process.stdin);
48
+ }
49
+ //# sourceMappingURL=revalidate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"revalidate.js","sourceRoot":"","sources":["../../src/revalidate.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,mEAAmE;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAE3C,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAA;AAE5D,MAAM,MAAM,GAAG,CAAC,IAAa,EAAE,EAAE,CAC/B,IAAI,KAAK,wBAAwB;IACjC,IAAI,KAAK,aAAa,CAAC,wBAAwB,CAAC,CAAC,QAAQ,EAAE,CAAA;AAE7D,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EAAE,KAAc,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,EAAE;IAClE,IAAI,CAAC,KAAK;QAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC3B,MAAM,IAAI,GAAG,MAAM,IAAI,OAAO,CAA0B,GAAG,CAAC,EAAE;QAC5D,MAAM,MAAM,GAAa,EAAE,CAAA;QAC3B,IAAI,QAAQ,GAAG,CAAC,CAAA;QAChB,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE;YACvB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAClB,QAAQ,IAAI,KAAK,CAAC,MAAM,CAAA;QAC1B,CAAC,CAAC,CAAA;QACF,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YACnB,MAAM,IAAI,GAA4B,MAAM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C,KAAK,GAAG,sBAAsB,CAAA;IACtC,iEAAiE;IACjE,mEAAmE;IACnE,wBAAwB;IACxB,MAAM,WAAW,GACf,OAAO,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;IAE7D,KAAK,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,KAAK,CAAC,CAAA;AACvC,CAAC","sourcesContent":["// This needs to live in the same workspace as the RegistryClient, because\n// otherwise we have a cyclical dependency cycle of dependencies in a cycle,\n// which is even more cyclical than this description describing it.\nimport { pathToFileURL } from 'node:url'\nimport { RegistryClient } from './index.ts'\n\nexport const __CODE_SPLIT_SCRIPT_NAME = import.meta.filename\n\nconst isMain = (path?: string) =>\n path === __CODE_SPLIT_SCRIPT_NAME ||\n path === pathToFileURL(__CODE_SPLIT_SCRIPT_NAME).toString()\n\nexport const main = async (cache?: string, input = process.stdin) => {\n if (!cache) process.exit(1)\n const reqs = await new Promise<['GET' | 'HEAD', URL][]>(res => {\n const chunks: Buffer[] = []\n let chunkLen = 0\n input.on('data', chunk => {\n chunks.push(chunk)\n chunkLen += chunk.length\n })\n input.on('end', () => {\n const reqs: ['GET' | 'HEAD', URL][] = Buffer.concat(\n chunks,\n chunkLen,\n )\n .toString()\n .split('\\0')\n .filter(\n i => !!i && (i.startsWith('GET ') || i.startsWith('HEAD ')),\n )\n .map(i =>\n i.startsWith('GET ') ?\n ['GET', new URL(i.substring('GET '.length))]\n : ['HEAD', new URL(i.substring('HEAD '.length))],\n )\n\n res(reqs)\n })\n })\n\n if (!reqs.length) process.exit(1)\n const rc = new RegistryClient({ cache })\n await Promise.all(\n reqs.map(async ([method, url]) => {\n await rc.request(url, {\n method,\n staleWhileRevalidate: false,\n })\n }),\n )\n}\n\nconst g = globalThis as typeof globalThis & {\n __VLT_INTERNAL_MAIN?: string\n}\n\nif (isMain(g.__VLT_INTERNAL_MAIN ?? process.argv[1])) {\n process.title = 'vlt-cache-revalidate'\n // When compiled there can be other leading args supplied by Deno\n // so always use the last arg unless there are only two which means\n // no path was supplied.\n const cacheFolder =\n process.argv.length === 2 ? undefined : process.argv.at(-1)\n\n void main(cacheFolder, process.stdin)\n}\n"]}
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": "0.0.0-7",
4
+ "version": "0.0.0-9",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -23,14 +23,14 @@
23
23
  "cache-control-parser": "^2.0.5",
24
24
  "package-json-from-dist": "^1.0.0",
25
25
  "undici": "^7.5.0",
26
- "@vltpkg/cache-unzip": "0.0.0-7",
27
- "@vltpkg/cache": "0.0.0-7",
28
- "@vltpkg/error-cause": "0.0.0-7",
29
- "@vltpkg/keychain": "0.0.0-7",
30
- "@vltpkg/types": "0.0.0-7",
31
- "@vltpkg/output": "0.0.0-7",
32
- "@vltpkg/url-open": "0.0.0-7",
33
- "@vltpkg/xdg": "0.0.0-7"
26
+ "@vltpkg/cache": "0.0.0-9",
27
+ "@vltpkg/keychain": "0.0.0-9",
28
+ "@vltpkg/error-cause": "0.0.0-9",
29
+ "@vltpkg/url-open": "0.0.0-9",
30
+ "@vltpkg/output": "0.0.0-9",
31
+ "@vltpkg/types": "0.0.0-9",
32
+ "@vltpkg/cache-unzip": "0.0.0-9",
33
+ "@vltpkg/xdg": "0.0.0-9"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@eslint/js": "^9.20.0",