@vltpkg/registry-client 0.0.0-9 → 1.0.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/esm/cache-entry.d.ts +32 -8
- package/dist/esm/cache-entry.d.ts.map +1 -1
- package/dist/esm/cache-entry.js +141 -62
- package/dist/esm/cache-entry.js.map +1 -1
- package/dist/esm/handle-304-response.js +1 -1
- package/dist/esm/handle-304-response.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +48 -25
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/otplease.d.ts.map +1 -1
- package/dist/esm/otplease.js +33 -25
- package/dist/esm/otplease.js.map +1 -1
- package/dist/esm/raw-header.d.ts +3 -3
- package/dist/esm/raw-header.d.ts.map +1 -1
- package/dist/esm/raw-header.js +12 -11
- package/dist/esm/raw-header.js.map +1 -1
- package/dist/esm/redirect.d.ts +1 -1
- package/dist/esm/redirect.d.ts.map +1 -1
- package/dist/esm/redirect.js +5 -1
- package/dist/esm/redirect.js.map +1 -1
- package/dist/esm/revalidate.d.ts +1 -1
- package/dist/esm/revalidate.d.ts.map +1 -1
- package/dist/esm/revalidate.js +11 -5
- package/dist/esm/revalidate.js.map +1 -1
- package/dist/esm/set-raw-header.d.ts +1 -1
- package/dist/esm/set-raw-header.d.ts.map +1 -1
- package/dist/esm/set-raw-header.js +6 -4
- package/dist/esm/set-raw-header.js.map +1 -1
- package/dist/esm/string-encoding.d.ts +9 -0
- package/dist/esm/string-encoding.d.ts.map +1 -0
- package/dist/esm/string-encoding.js +25 -0
- package/dist/esm/string-encoding.js.map +1 -0
- package/dist/esm/token-response.d.ts +1 -1
- package/dist/esm/token-response.d.ts.map +1 -1
- package/dist/esm/token-response.js +5 -2
- package/dist/esm/token-response.js.map +1 -1
- package/dist/esm/web-auth-challenge.d.ts +2 -2
- package/dist/esm/web-auth-challenge.d.ts.map +1 -1
- package/dist/esm/web-auth-challenge.js +13 -6
- package/dist/esm/web-auth-challenge.js.map +1 -1
- package/package.json +23 -22
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ If the response is `content-type: application/octet-stream` and starts
|
|
|
52
52
|
with the gzip header, then we return the raw body as we received it,
|
|
53
53
|
but as a best-effort background job, unzip it and update the cache
|
|
54
54
|
entry to be an unzipped response body. This is done in the
|
|
55
|
-
`@vltpkg/cache-unzip`
|
|
55
|
+
`@vltpkg/cache-unzip` child process.
|
|
56
56
|
|
|
57
57
|
So,
|
|
58
58
|
|
|
@@ -5,6 +5,19 @@ import type { InspectOptions } from 'node:util';
|
|
|
5
5
|
export type JSONObj = Record<string, JSONField>;
|
|
6
6
|
declare const kCustomInspect: unique symbol;
|
|
7
7
|
export type CacheEntryOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* An optional body to use.
|
|
10
|
+
*
|
|
11
|
+
* This is used when decoding a cache entry from a buffer, and the body
|
|
12
|
+
* is already in a ArrayBuffer we can use. When this option is
|
|
13
|
+
* provided the `addBody` method should not be used.
|
|
14
|
+
*/
|
|
15
|
+
body?: Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* An optional content length of the body to use, if undefined the
|
|
18
|
+
* content-length header will be used.
|
|
19
|
+
*/
|
|
20
|
+
contentLength?: number;
|
|
8
21
|
/**
|
|
9
22
|
* The expected integrity value for this response body
|
|
10
23
|
*/
|
|
@@ -37,7 +50,7 @@ export type CacheEntryOptions = {
|
|
|
37
50
|
};
|
|
38
51
|
export declare class CacheEntry {
|
|
39
52
|
#private;
|
|
40
|
-
constructor(statusCode: number, headers:
|
|
53
|
+
constructor(statusCode: number, headers: Uint8Array[], { body, integrity, trustIntegrity, 'stale-while-revalidate-factor': staleWhileRevalidateFactor, contentLength, }?: CacheEntryOptions);
|
|
41
54
|
toJSON(): {
|
|
42
55
|
[k: string]: string | number | boolean | [string, string][] | Date | ccp.CacheControl | undefined;
|
|
43
56
|
};
|
|
@@ -48,9 +61,16 @@ export declare class CacheEntry {
|
|
|
48
61
|
get staleWhileRevalidate(): boolean;
|
|
49
62
|
get contentType(): string;
|
|
50
63
|
get valid(): boolean;
|
|
51
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Add contents to the entry body.
|
|
66
|
+
*/
|
|
67
|
+
addBody(b: Uint8Array): void;
|
|
52
68
|
get statusCode(): number;
|
|
53
|
-
get headers():
|
|
69
|
+
get headers(): Uint8Array[];
|
|
70
|
+
/**
|
|
71
|
+
* Returns the body as a single Uint8Array, concatenating parts if needed.
|
|
72
|
+
*/
|
|
73
|
+
get _body(): Uint8Array;
|
|
54
74
|
/**
|
|
55
75
|
* Check that the sri integrity string that was provided to the ctor
|
|
56
76
|
* matches the body that we actually received. This should only be called
|
|
@@ -74,16 +94,20 @@ export declare class CacheEntry {
|
|
|
74
94
|
/**
|
|
75
95
|
* Give it a key, and it'll return the buffer of that header value
|
|
76
96
|
*/
|
|
77
|
-
getHeader(h: string):
|
|
97
|
+
getHeader(h: string): Uint8Array | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Give it a key, and it'll return the decoded string of that header value
|
|
100
|
+
*/
|
|
101
|
+
getHeaderString(h: string): string | undefined;
|
|
78
102
|
/**
|
|
79
103
|
* Set a header to a specific value
|
|
80
104
|
*/
|
|
81
|
-
setHeader(h: string, value:
|
|
105
|
+
setHeader(h: string, value: Uint8Array | string): void;
|
|
82
106
|
/**
|
|
83
107
|
* Return the body of the entry as a Buffer
|
|
84
108
|
*/
|
|
85
109
|
buffer(): Buffer;
|
|
86
|
-
get body():
|
|
110
|
+
get body(): Uint8Array | Record<string, any>;
|
|
87
111
|
get isJSON(): boolean;
|
|
88
112
|
get isGzip(): boolean;
|
|
89
113
|
/**
|
|
@@ -106,8 +130,8 @@ export declare class CacheEntry {
|
|
|
106
130
|
* and this static method will decode it into a CacheEntry representing
|
|
107
131
|
* the cached response.
|
|
108
132
|
*/
|
|
109
|
-
static decode(buffer:
|
|
110
|
-
static isGzipEntry(buffer:
|
|
133
|
+
static decode(buffer: Uint8Array): CacheEntry;
|
|
134
|
+
static isGzipEntry(buffer: Uint8Array): boolean;
|
|
111
135
|
/**
|
|
112
136
|
* Encode the entry as a single Buffer for writing to the cache
|
|
113
137
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-entry.d.ts","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzD,OAAO,GAAG,MAAM,sBAAsB,CAAA;AAEtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"cache-entry.d.ts","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzD,OAAO,GAAG,MAAM,sBAAsB,CAAA;AAEtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAS/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;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,UAAU,CAAA;IAEjB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB;;;;;;;OAOG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;IAExB;;;;;;;;;;;;;;OAcG;IACH,+BAA+B,CAAC,EAAE,MAAM,CAAA;CACzC,CAAA;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IAI/C;;OAEG;IACH,MAAM,IAAI,MAAM;IAUhB,IAAI,IAAI,IAAI,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAE3C;IAGD,IAAI,MAAM,IAAI,OAAO,CAYpB;IAGD,IAAI,MAAM,IAAI,OAAO,CAcpB;IAED;;;;OAIG;IACH,KAAK;IAqBL;;;OAGG;IACH,IAAI;IAKJ;;OAEG;IACH,IAAI,IAAI,OAAO;IAQf;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU;IAuD7C,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO;IAO/C;;OAEG;IACH,MAAM,IAAI,MAAM;CAkDjB"}
|
package/dist/esm/cache-entry.js
CHANGED
|
@@ -24,6 +24,7 @@ import { createHash } from 'node:crypto';
|
|
|
24
24
|
import { inspect } from 'node:util';
|
|
25
25
|
import { gunzipSync } from 'node:zlib';
|
|
26
26
|
import { getRawHeader, setRawHeader } from "./raw-header.js";
|
|
27
|
+
import { getDecodedValue, getEncondedValue, } from "./string-encoding.js";
|
|
27
28
|
const readSize = (buf, offset) => {
|
|
28
29
|
const a = buf[offset];
|
|
29
30
|
const b = buf[offset + 1];
|
|
@@ -45,27 +46,57 @@ const readSize = (buf, offset) => {
|
|
|
45
46
|
const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
|
|
46
47
|
export class CacheEntry {
|
|
47
48
|
#statusCode;
|
|
49
|
+
/** The raw headers as an array of buffers */
|
|
48
50
|
#headers;
|
|
49
|
-
|
|
51
|
+
/** The body buffer, used if the content length is known. */
|
|
52
|
+
#body;
|
|
53
|
+
/**
|
|
54
|
+
* If the content length is unknown we save the body in multiple parts
|
|
55
|
+
* in order to only concatenate once at the end and save extra memory copies.
|
|
56
|
+
*/
|
|
57
|
+
#bodyParts = [];
|
|
58
|
+
/** Used to track the length of the body while reading chunks */
|
|
50
59
|
#bodyLength = 0;
|
|
60
|
+
/** The total length of the body, if known */
|
|
61
|
+
#contentLength;
|
|
51
62
|
#integrity;
|
|
52
63
|
#integrityActual;
|
|
53
64
|
#json;
|
|
54
65
|
#trustIntegrity;
|
|
55
66
|
#staleWhileRevalidateFactor;
|
|
56
|
-
constructor(statusCode, headers, { integrity, trustIntegrity = false, 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, } = {}) {
|
|
67
|
+
constructor(statusCode, headers, { body, integrity, trustIntegrity = false, 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, contentLength, } = {}) {
|
|
57
68
|
this.#headers = headers;
|
|
58
69
|
this.#statusCode = statusCode;
|
|
59
70
|
this.#trustIntegrity = trustIntegrity;
|
|
60
71
|
this.#staleWhileRevalidateFactor = staleWhileRevalidateFactor;
|
|
61
72
|
if (integrity)
|
|
62
73
|
this.integrity = integrity;
|
|
74
|
+
// if content-legnth is known then we'll only allocate that much memory
|
|
75
|
+
// and we'll avoid copying memory around when adding new chunks.
|
|
76
|
+
if (contentLength != null && typeof contentLength === 'number') {
|
|
77
|
+
this.#contentLength = contentLength;
|
|
78
|
+
}
|
|
79
|
+
// if a body is provided then use that, in this case the `addBody`
|
|
80
|
+
// method should no longer be used.
|
|
81
|
+
if (body) {
|
|
82
|
+
const buffer = new ArrayBuffer(body.byteLength);
|
|
83
|
+
this.#body = new Uint8Array(buffer, 0, body.byteLength);
|
|
84
|
+
this.#body.set(body, 0);
|
|
85
|
+
this.#bodyLength = body.byteLength;
|
|
86
|
+
/* c8 ignore start */
|
|
87
|
+
}
|
|
88
|
+
else if (this.#contentLength) {
|
|
89
|
+
const buffer = new ArrayBuffer(this.#contentLength);
|
|
90
|
+
this.#body = new Uint8Array(buffer, 0, this.#contentLength);
|
|
91
|
+
this.#bodyLength = 0;
|
|
92
|
+
}
|
|
93
|
+
/* c8 ignore stop */
|
|
63
94
|
}
|
|
64
95
|
get #headersAsObject() {
|
|
65
96
|
const ret = [];
|
|
66
97
|
for (let i = 0; i < this.#headers.length - 1; i += 2) {
|
|
67
|
-
const key =
|
|
68
|
-
const val =
|
|
98
|
+
const key = getDecodedValue(this.#headers[i]);
|
|
99
|
+
const val = getDecodedValue(this.#headers[i + 1]);
|
|
69
100
|
ret.push([key, val]);
|
|
70
101
|
}
|
|
71
102
|
return ret;
|
|
@@ -105,7 +136,7 @@ export class CacheEntry {
|
|
|
105
136
|
get date() {
|
|
106
137
|
if (this.#date)
|
|
107
138
|
return this.#date;
|
|
108
|
-
const dh = this.
|
|
139
|
+
const dh = this.getHeaderString('date');
|
|
109
140
|
if (dh)
|
|
110
141
|
this.#date = new Date(dh);
|
|
111
142
|
return this.#date;
|
|
@@ -125,7 +156,7 @@ export class CacheEntry {
|
|
|
125
156
|
get cacheControl() {
|
|
126
157
|
if (this.#cacheControl)
|
|
127
158
|
return this.#cacheControl;
|
|
128
|
-
const cc = this.
|
|
159
|
+
const cc = this.getHeaderString('cache-control');
|
|
129
160
|
this.#cacheControl = cc ? ccp.parse(cc) : {};
|
|
130
161
|
return this.#cacheControl;
|
|
131
162
|
}
|
|
@@ -145,8 +176,7 @@ export class CacheEntry {
|
|
|
145
176
|
get contentType() {
|
|
146
177
|
if (this.#contentType !== undefined)
|
|
147
178
|
return this.#contentType;
|
|
148
|
-
this.#contentType =
|
|
149
|
-
this.getHeader('content-type')?.toString() ?? '';
|
|
179
|
+
this.#contentType = this.getHeaderString('content-type') ?? '';
|
|
150
180
|
return this.#contentType;
|
|
151
181
|
}
|
|
152
182
|
/**
|
|
@@ -175,9 +205,22 @@ export class CacheEntry {
|
|
|
175
205
|
this.date.getTime() + this.maxAge * 1000 > Date.now();
|
|
176
206
|
return this.#valid;
|
|
177
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Add contents to the entry body.
|
|
210
|
+
*/
|
|
178
211
|
addBody(b) {
|
|
179
|
-
|
|
180
|
-
|
|
212
|
+
// when the content length is uknown we store each chunk in an array that
|
|
213
|
+
// later on is concatenate into a single buffer, otherwise we just append
|
|
214
|
+
// the new chunk of bytes to the already allocated buffer keeping track
|
|
215
|
+
// of the current offset in the `this.#bodyLength` property.
|
|
216
|
+
if (!this.#body) {
|
|
217
|
+
this.#bodyParts.push(b);
|
|
218
|
+
this.#bodyLength += b.byteLength;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
this.#body.set(b, this.#bodyLength);
|
|
222
|
+
this.#bodyLength += b.byteLength;
|
|
223
|
+
}
|
|
181
224
|
}
|
|
182
225
|
get statusCode() {
|
|
183
226
|
return this.#statusCode;
|
|
@@ -185,6 +228,23 @@ export class CacheEntry {
|
|
|
185
228
|
get headers() {
|
|
186
229
|
return this.#headers;
|
|
187
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Returns the body as a single Uint8Array, concatenating parts if needed.
|
|
233
|
+
*/
|
|
234
|
+
get _body() {
|
|
235
|
+
// if the body is known we'll just use that
|
|
236
|
+
if (this.#body)
|
|
237
|
+
return this.#body;
|
|
238
|
+
// otherwise we concatenate the body parts into a single buffer
|
|
239
|
+
const buffer = new ArrayBuffer(this.#bodyLength);
|
|
240
|
+
const b = new Uint8Array(buffer, 0, this.#bodyLength);
|
|
241
|
+
let off = 0;
|
|
242
|
+
for (const part of this.#bodyParts) {
|
|
243
|
+
b.set(part, off);
|
|
244
|
+
off += part.byteLength;
|
|
245
|
+
}
|
|
246
|
+
return b;
|
|
247
|
+
}
|
|
188
248
|
/**
|
|
189
249
|
* Check that the sri integrity string that was provided to the ctor
|
|
190
250
|
* matches the body that we actually received. This should only be called
|
|
@@ -216,8 +276,7 @@ export class CacheEntry {
|
|
|
216
276
|
if (this.#integrityActual)
|
|
217
277
|
return this.#integrityActual;
|
|
218
278
|
const hash = createHash('sha512');
|
|
219
|
-
|
|
220
|
-
hash.update(buf);
|
|
279
|
+
hash.update(this._body);
|
|
221
280
|
const i = `sha512-${hash.digest('base64')}`;
|
|
222
281
|
this.integrityActual = i;
|
|
223
282
|
return i;
|
|
@@ -242,6 +301,15 @@ export class CacheEntry {
|
|
|
242
301
|
getHeader(h) {
|
|
243
302
|
return getRawHeader(this.#headers, h);
|
|
244
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* Give it a key, and it'll return the decoded string of that header value
|
|
306
|
+
*/
|
|
307
|
+
getHeaderString(h) {
|
|
308
|
+
const value = getRawHeader(this.#headers, h);
|
|
309
|
+
if (value) {
|
|
310
|
+
return getDecodedValue(value);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
245
313
|
/**
|
|
246
314
|
* Set a header to a specific value
|
|
247
315
|
*/
|
|
@@ -252,14 +320,7 @@ export class CacheEntry {
|
|
|
252
320
|
* Return the body of the entry as a Buffer
|
|
253
321
|
*/
|
|
254
322
|
buffer() {
|
|
255
|
-
|
|
256
|
-
if (!b)
|
|
257
|
-
return Buffer.allocUnsafe(0);
|
|
258
|
-
if (this.#body.length === 1)
|
|
259
|
-
return b;
|
|
260
|
-
const cat = Buffer.concat(this.#body, this.#bodyLength);
|
|
261
|
-
this.#body = [cat];
|
|
262
|
-
return cat;
|
|
323
|
+
return Buffer.from(this._body.buffer, this._body.byteOffset, this._body.byteLength);
|
|
263
324
|
}
|
|
264
325
|
// return the buffer if it's a tarball, or the parsed
|
|
265
326
|
// JSON if it's not.
|
|
@@ -270,7 +331,7 @@ export class CacheEntry {
|
|
|
270
331
|
get isJSON() {
|
|
271
332
|
if (this.#isJSON !== undefined)
|
|
272
333
|
return this.#isJSON;
|
|
273
|
-
const ct = this.
|
|
334
|
+
const ct = this.getHeaderString('content-type');
|
|
274
335
|
// if it says it's json, assume json
|
|
275
336
|
if (ct)
|
|
276
337
|
return (this.#isJSON = /\bjson\b/.test(ct));
|
|
@@ -288,10 +349,10 @@ export class CacheEntry {
|
|
|
288
349
|
get isGzip() {
|
|
289
350
|
if (this.#isGzip !== undefined)
|
|
290
351
|
return this.#isGzip;
|
|
291
|
-
const ce = this.
|
|
352
|
+
const ce = this.getHeaderString('content-encoding');
|
|
292
353
|
if (ce && !/\bgzip\b/.test(ce))
|
|
293
354
|
return (this.#isGzip = false);
|
|
294
|
-
const buf = this.
|
|
355
|
+
const buf = this._body;
|
|
295
356
|
if (buf.length < 2)
|
|
296
357
|
return false;
|
|
297
358
|
this.#isGzip = buf[0] === 0x1f && buf[1] === 0x8b;
|
|
@@ -314,14 +375,16 @@ export class CacheEntry {
|
|
|
314
375
|
// we know that if we know it's gzip, that the body has been
|
|
315
376
|
// flattened to a single buffer, so save the extra call.
|
|
316
377
|
/* c8 ignore start */
|
|
317
|
-
if (this
|
|
378
|
+
if (this._body.length === 0)
|
|
318
379
|
throw error('Invalid buffer, cant unzip');
|
|
319
380
|
/* c8 ignore stop */
|
|
320
|
-
const b = gunzipSync(this
|
|
381
|
+
const b = gunzipSync(this._body);
|
|
321
382
|
this.setHeader('content-encoding', 'identity');
|
|
322
|
-
|
|
323
|
-
this.#
|
|
324
|
-
this
|
|
383
|
+
const u8 = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
384
|
+
this.#body = u8;
|
|
385
|
+
this.#bodyLength = u8.byteLength;
|
|
386
|
+
this.#contentLength = u8.byteLength;
|
|
387
|
+
this.setHeader('content-length', String(this.#contentLength));
|
|
325
388
|
this.#isGzip = false;
|
|
326
389
|
return true;
|
|
327
390
|
}
|
|
@@ -333,7 +396,7 @@ export class CacheEntry {
|
|
|
333
396
|
*/
|
|
334
397
|
text() {
|
|
335
398
|
this.unzip();
|
|
336
|
-
return this.
|
|
399
|
+
return getDecodedValue(this._body);
|
|
337
400
|
}
|
|
338
401
|
/**
|
|
339
402
|
* Parse the entry body as JSON and return the result
|
|
@@ -359,9 +422,9 @@ export class CacheEntry {
|
|
|
359
422
|
if (buffer.length < headSize) {
|
|
360
423
|
return emptyCacheEntry;
|
|
361
424
|
}
|
|
362
|
-
const statusCode = Number(buffer.subarray(4, 7)
|
|
425
|
+
const statusCode = Number(getDecodedValue(buffer.subarray(4, 7)));
|
|
363
426
|
const headersBuffer = buffer.subarray(7, headSize);
|
|
364
|
-
// walk through the headers array, building up the rawHeaders
|
|
427
|
+
// walk through the headers array, building up the rawHeaders
|
|
365
428
|
const headers = [];
|
|
366
429
|
let i = 0;
|
|
367
430
|
let integrity = undefined;
|
|
@@ -369,20 +432,21 @@ export class CacheEntry {
|
|
|
369
432
|
const size = readSize(headersBuffer, i);
|
|
370
433
|
const val = headersBuffer.subarray(i + 4, i + size);
|
|
371
434
|
// if the last one was the key integrity, then this one is the value
|
|
372
|
-
if (headers.length % 2 === 1
|
|
373
|
-
|
|
374
|
-
|
|
435
|
+
if (headers.length % 2 === 1) {
|
|
436
|
+
const k = getDecodedValue(headers[headers.length - 1]).toLowerCase();
|
|
437
|
+
if (k === 'integrity')
|
|
438
|
+
integrity = getDecodedValue(val);
|
|
375
439
|
}
|
|
376
440
|
headers.push(val);
|
|
377
441
|
i += size;
|
|
378
442
|
}
|
|
379
443
|
const body = buffer.subarray(headSize);
|
|
380
444
|
const c = new CacheEntry(statusCode, setRawHeader(headers, 'content-length', String(body.byteLength)), {
|
|
445
|
+
body,
|
|
381
446
|
integrity,
|
|
382
447
|
trustIntegrity: true,
|
|
448
|
+
contentLength: body.byteLength,
|
|
383
449
|
});
|
|
384
|
-
c.#body = [body];
|
|
385
|
-
c.#bodyLength = body.byteLength;
|
|
386
450
|
if (c.isJSON) {
|
|
387
451
|
try {
|
|
388
452
|
c.json();
|
|
@@ -403,37 +467,52 @@ export class CacheEntry {
|
|
|
403
467
|
/**
|
|
404
468
|
* Encode the entry as a single Buffer for writing to the cache
|
|
405
469
|
*/
|
|
406
|
-
// TODO: should this maybe not concat, and just return Buffer[]?
|
|
407
|
-
// Then we can writev it to the cache file and save the memory copy
|
|
408
470
|
encode() {
|
|
409
471
|
if (this.isJSON)
|
|
410
472
|
this.json();
|
|
411
|
-
const
|
|
412
|
-
const
|
|
413
|
-
|
|
473
|
+
const statusStr = String(this.#statusCode);
|
|
474
|
+
const statusBytes = getEncondedValue(statusStr);
|
|
475
|
+
// compute headLength = 4 (length field itself) + statusBytes + Σ(4 + headerLen) for each header item
|
|
476
|
+
let headLength = 4 + statusBytes.byteLength;
|
|
477
|
+
for (const h of this.#headers)
|
|
478
|
+
headLength += 4 + h.byteLength;
|
|
479
|
+
// allocate and fill head length prefix (big-endian)
|
|
480
|
+
const headLenBytes = new Uint8Array(4);
|
|
481
|
+
headLenBytes[0] = (headLength >> 24) & 0xff;
|
|
482
|
+
headLenBytes[1] = (headLength >> 16) & 0xff;
|
|
483
|
+
headLenBytes[2] = (headLength >> 8) & 0xff;
|
|
484
|
+
headLenBytes[3] = headLength & 0xff;
|
|
485
|
+
// header chunks: [len, bytes] for each header item
|
|
486
|
+
const headerChunks = [];
|
|
414
487
|
for (const h of this.#headers) {
|
|
415
|
-
const
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
488
|
+
const l = headLenBytes.byteLength + h.byteLength;
|
|
489
|
+
const lb = new Uint8Array(4);
|
|
490
|
+
lb[0] = (l >> 24) & 0xff;
|
|
491
|
+
lb[1] = (l >> 16) & 0xff;
|
|
492
|
+
lb[2] = (l >> 8) & 0xff;
|
|
493
|
+
lb[3] = l & 0xff;
|
|
494
|
+
headerChunks.push(lb, h);
|
|
495
|
+
}
|
|
496
|
+
// total size
|
|
497
|
+
const total = headLenBytes.byteLength +
|
|
498
|
+
statusBytes.byteLength +
|
|
499
|
+
headerChunks.reduce((n, b) => n + b.byteLength, 0) +
|
|
500
|
+
this._body.byteLength;
|
|
501
|
+
// returns the concatenate buffer with all the pieces
|
|
502
|
+
const outBuffer = new ArrayBuffer(total);
|
|
503
|
+
const out = Buffer.from(outBuffer, 0, total);
|
|
504
|
+
let off = 0;
|
|
505
|
+
out.set(headLenBytes, off);
|
|
506
|
+
off += headLenBytes.byteLength;
|
|
507
|
+
out.set(statusBytes, off);
|
|
508
|
+
off += statusBytes.byteLength;
|
|
509
|
+
for (const chunk of headerChunks) {
|
|
510
|
+
out.set(chunk, off);
|
|
511
|
+
off += chunk.byteLength;
|
|
425
512
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
(headLength >> 24) & 0xff,
|
|
429
|
-
(headLength >> 16) & 0xff,
|
|
430
|
-
(headLength >> 8) & 0xff,
|
|
431
|
-
headLength & 0xff,
|
|
432
|
-
], 0);
|
|
433
|
-
chunks.unshift(hlBuf);
|
|
434
|
-
chunks.push(...this.#body);
|
|
435
|
-
return Buffer.concat(chunks, headLength + this.#bodyLength);
|
|
513
|
+
out.set(this._body, off);
|
|
514
|
+
return out;
|
|
436
515
|
}
|
|
437
516
|
}
|
|
438
|
-
const emptyCacheEntry = new CacheEntry(0, []);
|
|
517
|
+
const emptyCacheEntry = new CacheEntry(0, [], { contentLength: 0 });
|
|
439
518
|
//# sourceMappingURL=cache-entry.js.map
|
|
@@ -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,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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"]}
|
|
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,8EAA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AAC;QACD,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACxB,OAAO,GAAG,CAAA;IACZ,CAAC;CACF;AAED,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,aAAa,EAAE,CAAC,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'\nimport {\n getDecodedValue,\n getEncondedValue,\n} from './string-encoding.ts'\n\nexport type JSONObj = Record<string, JSONField>\n\nconst readSize = (buf: Uint8Array, 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 * An optional body to use.\n *\n * This is used when decoding a cache entry from a buffer, and the body\n * is already in a ArrayBuffer we can use. When this option is\n * provided the `addBody` method should not be used.\n */\n body?: Uint8Array\n\n /**\n * An optional content length of the body to use, if undefined the\n * content-length header will be used.\n */\n contentLength?: number\n\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 /** The raw headers as an array of buffers */\n #headers: Uint8Array[]\n /** The body buffer, used if the content length is known. */\n #body?: Uint8Array\n /**\n * If the content length is unknown we save the body in multiple parts\n * in order to only concatenate once at the end and save extra memory copies.\n */\n #bodyParts: Uint8Array[] = []\n /** Used to track the length of the body while reading chunks */\n #bodyLength = 0\n /** The total length of the body, if known */\n #contentLength?: number\n #integrity?: Integrity\n #integrityActual?: Integrity\n #json?: JSONObj\n #trustIntegrity\n #staleWhileRevalidateFactor\n\n constructor(\n statusCode: number,\n headers: Uint8Array[],\n {\n body,\n integrity,\n trustIntegrity = false,\n 'stale-while-revalidate-factor':\n staleWhileRevalidateFactor = 60,\n contentLength,\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 // if content-legnth is known then we'll only allocate that much memory\n // and we'll avoid copying memory around when adding new chunks.\n if (contentLength != null && typeof contentLength === 'number') {\n this.#contentLength = contentLength\n }\n\n // if a body is provided then use that, in this case the `addBody`\n // method should no longer be used.\n if (body) {\n const buffer = new ArrayBuffer(body.byteLength)\n this.#body = new Uint8Array(buffer, 0, body.byteLength)\n this.#body.set(body, 0)\n this.#bodyLength = body.byteLength\n /* c8 ignore start */\n } else if (this.#contentLength) {\n const buffer = new ArrayBuffer(this.#contentLength)\n this.#body = new Uint8Array(buffer, 0, this.#contentLength)\n this.#bodyLength = 0\n }\n /* c8 ignore stop */\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 = getDecodedValue(this.#headers[i])\n const val = getDecodedValue(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.getHeaderString('date')\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.getHeaderString('cache-control')\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 = this.getHeaderString('content-type') ?? ''\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 /**\n * Add contents to the entry body.\n */\n addBody(b: Uint8Array) {\n // when the content length is uknown we store each chunk in an array that\n // later on is concatenate into a single buffer, otherwise we just append\n // the new chunk of bytes to the already allocated buffer keeping track\n // of the current offset in the `this.#bodyLength` property.\n if (!this.#body) {\n this.#bodyParts.push(b)\n this.#bodyLength += b.byteLength\n } else {\n this.#body.set(b, this.#bodyLength)\n this.#bodyLength += b.byteLength\n }\n }\n\n get statusCode() {\n return this.#statusCode\n }\n get headers(): Uint8Array[] {\n return this.#headers\n }\n\n /**\n * Returns the body as a single Uint8Array, concatenating parts if needed.\n */\n get _body(): Uint8Array {\n // if the body is known we'll just use that\n if (this.#body) return this.#body\n // otherwise we concatenate the body parts into a single buffer\n const buffer = new ArrayBuffer(this.#bodyLength)\n const b = new Uint8Array(buffer, 0, this.#bodyLength)\n let off = 0\n for (const part of this.#bodyParts) {\n b.set(part, off)\n off += part.byteLength\n }\n return b\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 hash.update(this._body)\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): Uint8Array | undefined {\n return getRawHeader(this.#headers, h)\n }\n\n /**\n * Give it a key, and it'll return the decoded string of that header value\n */\n getHeaderString(h: string): string | undefined {\n const value = getRawHeader(this.#headers, h)\n if (value) {\n return getDecodedValue(value)\n }\n }\n\n /**\n * Set a header to a specific value\n */\n setHeader(h: string, value: Uint8Array | 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 return Buffer.from(\n this._body.buffer,\n this._body.byteOffset,\n this._body.byteLength,\n )\n }\n\n // return the buffer if it's a tarball, or the parsed\n // JSON if it's not.\n get body(): Uint8Array | 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.getHeaderString('content-type')\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.getHeaderString('content-encoding')\n if (ce && !/\\bgzip\\b/.test(ce)) return (this.#isGzip = false)\n const buf = this._body\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.length === 0)\n throw error('Invalid buffer, cant unzip')\n /* c8 ignore stop */\n const b = gunzipSync(this._body)\n this.setHeader('content-encoding', 'identity')\n const u8 = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)\n this.#body = u8\n this.#bodyLength = u8.byteLength\n this.#contentLength = u8.byteLength\n this.setHeader('content-length', String(this.#contentLength))\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 getDecodedValue(this._body)\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: Uint8Array): 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(getDecodedValue(buffer.subarray(4, 7)))\n const headersBuffer = buffer.subarray(7, headSize)\n // walk through the headers array, building up the rawHeaders\n const headers: Uint8Array[] = []\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 (headers.length % 2 === 1) {\n const k = getDecodedValue(\n headers[headers.length - 1],\n ).toLowerCase()\n if (k === 'integrity')\n integrity = getDecodedValue(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 body,\n integrity,\n trustIntegrity: true,\n contentLength: body.byteLength,\n },\n )\n\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: Uint8Array): 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 encode(): Buffer {\n if (this.isJSON) this.json()\n const statusStr = String(this.#statusCode)\n const statusBytes = getEncondedValue(statusStr)\n\n // compute headLength = 4 (length field itself) + statusBytes + Σ(4 + headerLen) for each header item\n let headLength = 4 + statusBytes.byteLength\n for (const h of this.#headers) headLength += 4 + h.byteLength\n\n // allocate and fill head length prefix (big-endian)\n const headLenBytes = new Uint8Array(4)\n headLenBytes[0] = (headLength >> 24) & 0xff\n headLenBytes[1] = (headLength >> 16) & 0xff\n headLenBytes[2] = (headLength >> 8) & 0xff\n headLenBytes[3] = headLength & 0xff\n\n // header chunks: [len, bytes] for each header item\n const headerChunks: Uint8Array[] = []\n for (const h of this.#headers) {\n const l = headLenBytes.byteLength + h.byteLength\n const lb = new Uint8Array(4)\n lb[0] = (l >> 24) & 0xff\n lb[1] = (l >> 16) & 0xff\n lb[2] = (l >> 8) & 0xff\n lb[3] = l & 0xff\n headerChunks.push(lb, h)\n }\n\n // total size\n const total =\n headLenBytes.byteLength +\n statusBytes.byteLength +\n headerChunks.reduce((n, b) => n + b.byteLength, 0) +\n this._body.byteLength\n\n // returns the concatenate buffer with all the pieces\n const outBuffer = new ArrayBuffer(total)\n const out = Buffer.from(outBuffer, 0, total)\n let off = 0\n out.set(headLenBytes, off)\n off += headLenBytes.byteLength\n out.set(statusBytes, off)\n off += statusBytes.byteLength\n for (const chunk of headerChunks) {\n out.set(chunk, off)\n off += chunk.byteLength\n }\n out.set(this._body, off)\n return out\n }\n}\n\nconst emptyCacheEntry = new CacheEntry(0, [], { contentLength: 0 })\n"]}
|
|
@@ -2,7 +2,7 @@ export const handle304Response = (resp, entry) => {
|
|
|
2
2
|
if (resp.statusCode !== 304 || !entry)
|
|
3
3
|
return false;
|
|
4
4
|
const d = String(resp.headers.date ?? '') || new Date().toUTCString();
|
|
5
|
-
entry.setHeader('date',
|
|
5
|
+
entry.setHeader('date', d);
|
|
6
6
|
// shouldn't have a body, but just in case.
|
|
7
7
|
resp.body.resume();
|
|
8
8
|
return true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle-304-response.js","sourceRoot":"","sources":["../../src/handle-304-response.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,IAA6B,EAC7B,KAAkB,EACG,EAAE;IACvB,IAAI,IAAI,CAAC,UAAU,KAAK,GAAG,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAEnD,MAAM,CAAC,GACL,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;IAC7D,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"handle-304-response.js","sourceRoot":"","sources":["../../src/handle-304-response.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,IAA6B,EAC7B,KAAkB,EACG,EAAE;IACvB,IAAI,IAAI,CAAC,UAAU,KAAK,GAAG,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAEnD,MAAM,CAAC,GACL,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;IAC7D,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAC1B,2CAA2C;IAC3C,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAA;IAClB,OAAO,IAAI,CAAA;AACb,CAAC,CAAA","sourcesContent":["import type { Dispatcher } from 'undici'\nimport type { CacheEntry } from './cache-entry.ts'\n\nexport const handle304Response = (\n resp: Dispatcher.ResponseData,\n entry?: CacheEntry,\n): entry is CacheEntry => {\n if (resp.statusCode !== 304 || !entry) return false\n\n const d =\n String(resp.headers.date ?? '') || new Date().toUTCString()\n entry.setHeader('date', d)\n // shouldn't have a body, but just in case.\n resp.body.resume()\n return true\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -135,10 +135,10 @@ export declare class RegistryClient {
|
|
|
135
135
|
*/
|
|
136
136
|
login(registry: string): Promise<void>;
|
|
137
137
|
/**
|
|
138
|
-
* Given a {@link WebAuthChallenge}, open the `
|
|
138
|
+
* Given a {@link WebAuthChallenge}, open the `authUrl` in a browser and
|
|
139
139
|
* hang on the `doneUrl` until it returns a {@link TokenResponse} object.
|
|
140
140
|
*/
|
|
141
|
-
webAuthOpener({ doneUrl,
|
|
141
|
+
webAuthOpener({ doneUrl, authUrl }: WebAuthChallenge): Promise<TokenResponse>;
|
|
142
142
|
request(url: URL | string, options?: RegistryClientRequestOptions): Promise<CacheEntry>;
|
|
143
143
|
}
|
|
144
144
|
//# sourceMappingURL=index.d.ts.map
|