@vltpkg/registry-client 1.0.0-rc.22 → 1.0.0-rc.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/add-header.d.ts +1 -0
- package/dist/add-header.js +26 -0
- package/dist/auth.d.ts +15 -0
- package/dist/auth.js +53 -0
- package/dist/cache-entry.d.ts +140 -0
- package/dist/cache-entry.js +517 -0
- package/dist/cache-revalidate.d.ts +1 -0
- package/dist/cache-revalidate.js +56 -0
- package/dist/delete-header.d.ts +1 -0
- package/dist/delete-header.js +31 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +12 -0
- package/dist/get-header.d.ts +1 -0
- package/dist/get-header.js +36 -0
- package/dist/handle-304-response.d.ts +3 -0
- package/dist/handle-304-response.js +8 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.js +326 -0
- package/dist/is-cacheable.d.ts +4 -0
- package/dist/is-cacheable.js +9 -0
- package/dist/is-iterable.d.ts +1 -0
- package/dist/is-iterable.js +1 -0
- package/dist/oidc.d.ts +17 -0
- package/dist/oidc.js +151 -0
- package/dist/otplease.d.ts +3 -0
- package/dist/otplease.js +62 -0
- package/dist/raw-header.d.ts +8 -0
- package/dist/raw-header.js +33 -0
- package/dist/redirect.d.ts +22 -0
- package/dist/redirect.js +64 -0
- package/dist/revalidate.d.ts +4 -0
- package/dist/revalidate.js +50 -0
- package/dist/set-cache-headers.d.ts +3 -0
- package/dist/set-cache-headers.js +16 -0
- package/dist/set-raw-header.d.ts +5 -0
- package/dist/set-raw-header.js +20 -0
- package/dist/string-encoding.d.ts +8 -0
- package/dist/string-encoding.js +24 -0
- package/dist/token-response.d.ts +4 -0
- package/dist/token-response.js +7 -0
- package/dist/web-auth-challenge.d.ts +5 -0
- package/dist/web-auth-challenge.js +13 -0
- package/package.json +12 -12
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
// A response object in the cache.
|
|
2
|
+
//
|
|
3
|
+
// The cache stores Buffer objects, and it's convenient to have headers/body
|
|
4
|
+
// together, so we have a simple data structure for this.
|
|
5
|
+
//
|
|
6
|
+
// The shape of it is:
|
|
7
|
+
//
|
|
8
|
+
// [head length]
|
|
9
|
+
// <status code in ascii>
|
|
10
|
+
// [headers]
|
|
11
|
+
// [body]
|
|
12
|
+
//
|
|
13
|
+
// The [UInt32BE head length] is 4 bytes specifying the full length of the
|
|
14
|
+
// status code plus all header keys and values.
|
|
15
|
+
//
|
|
16
|
+
// The [headers] section is key/value/key2/value2/... where each key and value
|
|
17
|
+
// is a 4-byte Uint32BE length, followed by that many bytes.
|
|
18
|
+
//
|
|
19
|
+
// From there, the body can be of any indeterminate length, and is the rest
|
|
20
|
+
// of the file.
|
|
21
|
+
import { error } from '@vltpkg/error-cause';
|
|
22
|
+
import ccp from 'cache-control-parser';
|
|
23
|
+
import { createHash } from 'node:crypto';
|
|
24
|
+
import { inspect } from 'node:util';
|
|
25
|
+
import { gunzipSync } from 'node:zlib';
|
|
26
|
+
import { getRawHeader, setRawHeader } from "./raw-header.js";
|
|
27
|
+
import { getDecodedValue, getEncondedValue, } from "./string-encoding.js";
|
|
28
|
+
const readSize = (buf, offset) => {
|
|
29
|
+
const a = buf[offset];
|
|
30
|
+
const b = buf[offset + 1];
|
|
31
|
+
const c = buf[offset + 2];
|
|
32
|
+
const d = buf[offset + 3];
|
|
33
|
+
// not possible, we check the length
|
|
34
|
+
/* c8 ignore start */
|
|
35
|
+
if (a === undefined ||
|
|
36
|
+
b === undefined ||
|
|
37
|
+
c === undefined ||
|
|
38
|
+
d === undefined) {
|
|
39
|
+
throw error('Invalid buffer, not long enough to readSize', {
|
|
40
|
+
found: buf.length,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/* c8 ignore stop */
|
|
44
|
+
return (a << 24) | (b << 16) | (c << 8) | d;
|
|
45
|
+
};
|
|
46
|
+
const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
|
|
47
|
+
export class CacheEntry {
|
|
48
|
+
#statusCode;
|
|
49
|
+
/** The raw headers as an array of buffers */
|
|
50
|
+
#headers;
|
|
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 */
|
|
59
|
+
#bodyLength = 0;
|
|
60
|
+
/** The total length of the body, if known */
|
|
61
|
+
#contentLength;
|
|
62
|
+
#integrity;
|
|
63
|
+
#integrityActual;
|
|
64
|
+
#json;
|
|
65
|
+
#trustIntegrity;
|
|
66
|
+
#staleWhileRevalidateFactor;
|
|
67
|
+
constructor(statusCode, headers, { body, integrity, trustIntegrity = false, 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, contentLength, } = {}) {
|
|
68
|
+
this.#headers = headers;
|
|
69
|
+
this.#statusCode = statusCode;
|
|
70
|
+
this.#trustIntegrity = trustIntegrity;
|
|
71
|
+
this.#staleWhileRevalidateFactor = staleWhileRevalidateFactor;
|
|
72
|
+
if (integrity)
|
|
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 */
|
|
94
|
+
}
|
|
95
|
+
get #headersAsObject() {
|
|
96
|
+
const ret = [];
|
|
97
|
+
for (let i = 0; i < this.#headers.length - 1; i += 2) {
|
|
98
|
+
const key = getDecodedValue(this.#headers[i]);
|
|
99
|
+
const val = getDecodedValue(this.#headers[i + 1]);
|
|
100
|
+
ret.push([key, val]);
|
|
101
|
+
}
|
|
102
|
+
return ret;
|
|
103
|
+
}
|
|
104
|
+
toJSON() {
|
|
105
|
+
const { statusCode, valid, staleWhileRevalidate, cacheControl, date, contentType, integrity, maxAge, isGzip, isJSON, } = this;
|
|
106
|
+
/* c8 ignore start */
|
|
107
|
+
const age = date ?
|
|
108
|
+
Math.floor((Date.now() - date.getTime()) / 1000)
|
|
109
|
+
: undefined;
|
|
110
|
+
const expires = date ? new Date(date.getTime() + this.maxAge * 1000) : undefined;
|
|
111
|
+
/* c8 ignore end */
|
|
112
|
+
return Object.fromEntries(Object.entries({
|
|
113
|
+
statusCode,
|
|
114
|
+
headers: this.#headersAsObject,
|
|
115
|
+
contentType,
|
|
116
|
+
integrity,
|
|
117
|
+
date,
|
|
118
|
+
expires,
|
|
119
|
+
cacheControl,
|
|
120
|
+
valid,
|
|
121
|
+
staleWhileRevalidate,
|
|
122
|
+
age,
|
|
123
|
+
maxAge,
|
|
124
|
+
isGzip,
|
|
125
|
+
isJSON,
|
|
126
|
+
}).filter(([_, v]) => v !== undefined));
|
|
127
|
+
}
|
|
128
|
+
[kCustomInspect](depth, options) {
|
|
129
|
+
const str = inspect(this.toJSON(), {
|
|
130
|
+
depth,
|
|
131
|
+
...options,
|
|
132
|
+
});
|
|
133
|
+
return `@vltpkg/registry-client.CacheEntry ${str}`;
|
|
134
|
+
}
|
|
135
|
+
#date;
|
|
136
|
+
get date() {
|
|
137
|
+
if (this.#date)
|
|
138
|
+
return this.#date;
|
|
139
|
+
const dh = this.getHeaderString('date');
|
|
140
|
+
if (dh)
|
|
141
|
+
this.#date = new Date(dh);
|
|
142
|
+
return this.#date;
|
|
143
|
+
}
|
|
144
|
+
#maxAge;
|
|
145
|
+
get maxAge() {
|
|
146
|
+
if (this.#maxAge !== undefined)
|
|
147
|
+
return this.#maxAge;
|
|
148
|
+
// see if the max-age has not yet been crossed
|
|
149
|
+
// default to 5m if maxage is not set, as some registries
|
|
150
|
+
// do not set a cache control header at all.
|
|
151
|
+
const cc = this.cacheControl;
|
|
152
|
+
this.#maxAge = cc['max-age'] || cc['s-maxage'] || 300;
|
|
153
|
+
return this.#maxAge;
|
|
154
|
+
}
|
|
155
|
+
#cacheControl;
|
|
156
|
+
get cacheControl() {
|
|
157
|
+
if (this.#cacheControl)
|
|
158
|
+
return this.#cacheControl;
|
|
159
|
+
const cc = this.getHeaderString('cache-control');
|
|
160
|
+
this.#cacheControl = cc ? ccp.parse(cc) : {};
|
|
161
|
+
return this.#cacheControl;
|
|
162
|
+
}
|
|
163
|
+
#staleWhileRevalidate;
|
|
164
|
+
get staleWhileRevalidate() {
|
|
165
|
+
if (this.#staleWhileRevalidate !== undefined)
|
|
166
|
+
return this.#staleWhileRevalidate;
|
|
167
|
+
if (this.valid || !this.date)
|
|
168
|
+
return true;
|
|
169
|
+
const swv = this.cacheControl['stale-while-revalidate'] ??
|
|
170
|
+
this.maxAge * this.#staleWhileRevalidateFactor;
|
|
171
|
+
this.#staleWhileRevalidate =
|
|
172
|
+
this.date.getTime() + swv * 1000 > Date.now();
|
|
173
|
+
return this.#staleWhileRevalidate;
|
|
174
|
+
}
|
|
175
|
+
#contentType;
|
|
176
|
+
get contentType() {
|
|
177
|
+
if (this.#contentType !== undefined)
|
|
178
|
+
return this.#contentType;
|
|
179
|
+
this.#contentType = this.getHeaderString('content-type') ?? '';
|
|
180
|
+
return this.#contentType;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* `true` if the entry represents a cached response that is still
|
|
184
|
+
* valid to use.
|
|
185
|
+
*/
|
|
186
|
+
#valid;
|
|
187
|
+
get valid() {
|
|
188
|
+
if (this.#valid !== undefined)
|
|
189
|
+
return this.#valid;
|
|
190
|
+
// immutable = never changes
|
|
191
|
+
if (this.cacheControl.immutable)
|
|
192
|
+
return (this.#valid = true);
|
|
193
|
+
// some registries do text/json, some do application/json,
|
|
194
|
+
// some do application/vnd.npm.install-v1+json
|
|
195
|
+
// If it's NOT json, it's an immutable tarball
|
|
196
|
+
const ct = this.contentType;
|
|
197
|
+
if (ct && !/\bjson\b/.test(ct))
|
|
198
|
+
return (this.#valid = true);
|
|
199
|
+
// see if the max-age has not yet been crossed
|
|
200
|
+
// default to 5m if maxage is not set, as some registries
|
|
201
|
+
// do not set a cache control header at all.
|
|
202
|
+
if (!this.date)
|
|
203
|
+
return (this.#valid = false);
|
|
204
|
+
this.#valid =
|
|
205
|
+
this.date.getTime() + this.maxAge * 1000 > Date.now();
|
|
206
|
+
return this.#valid;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Add contents to the entry body.
|
|
210
|
+
*/
|
|
211
|
+
addBody(b) {
|
|
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
|
+
}
|
|
224
|
+
}
|
|
225
|
+
get statusCode() {
|
|
226
|
+
return this.#statusCode;
|
|
227
|
+
}
|
|
228
|
+
get headers() {
|
|
229
|
+
return this.#headers;
|
|
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
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Check that the sri integrity string that was provided to the ctor
|
|
250
|
+
* matches the body that we actually received. This should only be called
|
|
251
|
+
* AFTER the entire body has been completely downloaded.
|
|
252
|
+
*
|
|
253
|
+
* This method **will throw** if the integrity values do not match.
|
|
254
|
+
*
|
|
255
|
+
* Note that this will *usually* not be true if the value is coming out of
|
|
256
|
+
* the cache, because the cache entries are un-gzipped in place. It should
|
|
257
|
+
* _only_ be called for artifacts that come from an actual http response.
|
|
258
|
+
*
|
|
259
|
+
* Returns true if anything was actually verified.
|
|
260
|
+
*/
|
|
261
|
+
checkIntegrity(context = {}) {
|
|
262
|
+
if (!this.#integrity)
|
|
263
|
+
return false;
|
|
264
|
+
if (this.integrityActual !== this.#integrity) {
|
|
265
|
+
throw error('Integrity check failure', {
|
|
266
|
+
code: 'EINTEGRITY',
|
|
267
|
+
response: this,
|
|
268
|
+
wanted: this.#integrity,
|
|
269
|
+
found: this.integrityActual,
|
|
270
|
+
...context,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
get integrityActual() {
|
|
276
|
+
if (this.#integrityActual)
|
|
277
|
+
return this.#integrityActual;
|
|
278
|
+
const hash = createHash('sha512');
|
|
279
|
+
hash.update(this._body);
|
|
280
|
+
const i = `sha512-${hash.digest('base64')}`;
|
|
281
|
+
this.integrityActual = i;
|
|
282
|
+
return i;
|
|
283
|
+
}
|
|
284
|
+
set integrityActual(i) {
|
|
285
|
+
this.#integrityActual = i;
|
|
286
|
+
this.setHeader('integrity', i);
|
|
287
|
+
}
|
|
288
|
+
set integrity(i) {
|
|
289
|
+
if (!this.#integrity && i) {
|
|
290
|
+
this.#integrity = i;
|
|
291
|
+
if (this.#trustIntegrity)
|
|
292
|
+
this.integrityActual = i;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
get integrity() {
|
|
296
|
+
return this.#integrity;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Give it a key, and it'll return the buffer of that header value
|
|
300
|
+
*/
|
|
301
|
+
getHeader(h) {
|
|
302
|
+
return getRawHeader(this.#headers, h);
|
|
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
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Set a header to a specific value
|
|
315
|
+
*/
|
|
316
|
+
setHeader(h, value) {
|
|
317
|
+
this.#headers = setRawHeader(this.#headers, h, value);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Return the body of the entry as a Buffer
|
|
321
|
+
*/
|
|
322
|
+
buffer() {
|
|
323
|
+
return Buffer.from(this._body.buffer, this._body.byteOffset, this._body.byteLength);
|
|
324
|
+
}
|
|
325
|
+
// return the buffer if it's a tarball, or the parsed
|
|
326
|
+
// JSON if it's not.
|
|
327
|
+
get body() {
|
|
328
|
+
return this.isJSON ? this.json() : this.buffer();
|
|
329
|
+
}
|
|
330
|
+
#isJSON;
|
|
331
|
+
get isJSON() {
|
|
332
|
+
if (this.#isJSON !== undefined)
|
|
333
|
+
return this.#isJSON;
|
|
334
|
+
const ct = this.getHeaderString('content-type');
|
|
335
|
+
// if it says it's json, assume json
|
|
336
|
+
if (ct)
|
|
337
|
+
return (this.#isJSON = /\bjson\b/.test(ct));
|
|
338
|
+
const text = this.text();
|
|
339
|
+
// don't cache, because we might just not have it yet.
|
|
340
|
+
if (!text)
|
|
341
|
+
return false;
|
|
342
|
+
// all registry json starts with {, and no tarball ever can.
|
|
343
|
+
this.#isJSON = text.startsWith('{');
|
|
344
|
+
if (this.#isJSON)
|
|
345
|
+
this.setHeader('content-type', 'text/json');
|
|
346
|
+
return this.#isJSON;
|
|
347
|
+
}
|
|
348
|
+
#isGzip;
|
|
349
|
+
get isGzip() {
|
|
350
|
+
if (this.#isGzip !== undefined)
|
|
351
|
+
return this.#isGzip;
|
|
352
|
+
const ce = this.getHeaderString('content-encoding');
|
|
353
|
+
if (ce && !/\bgzip\b/.test(ce))
|
|
354
|
+
return (this.#isGzip = false);
|
|
355
|
+
const buf = this._body;
|
|
356
|
+
if (buf.length < 2)
|
|
357
|
+
return false;
|
|
358
|
+
this.#isGzip = buf[0] === 0x1f && buf[1] === 0x8b;
|
|
359
|
+
if (this.#isGzip) {
|
|
360
|
+
this.setHeader('content-encoding', 'gzip');
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
this.setHeader('content-encoding', 'identity');
|
|
364
|
+
this.setHeader('content-length', String(this.#bodyLength));
|
|
365
|
+
}
|
|
366
|
+
return this.#isGzip;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Un-gzip encode the body.
|
|
370
|
+
* Returns true if it was previously gzip (so something was done), otherwise
|
|
371
|
+
* returns false.
|
|
372
|
+
*/
|
|
373
|
+
unzip() {
|
|
374
|
+
if (this.isGzip) {
|
|
375
|
+
// we know that if we know it's gzip, that the body has been
|
|
376
|
+
// flattened to a single buffer, so save the extra call.
|
|
377
|
+
/* c8 ignore start */
|
|
378
|
+
if (this._body.length === 0)
|
|
379
|
+
throw error('Invalid buffer, cant unzip');
|
|
380
|
+
/* c8 ignore stop */
|
|
381
|
+
const b = gunzipSync(this._body);
|
|
382
|
+
this.setHeader('content-encoding', 'identity');
|
|
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));
|
|
388
|
+
this.#isGzip = false;
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Return the body of the entry as utf8 text
|
|
395
|
+
* Automatically unzips if the content is gzip encoded
|
|
396
|
+
*/
|
|
397
|
+
text() {
|
|
398
|
+
this.unzip();
|
|
399
|
+
return getDecodedValue(this._body);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Parse the entry body as JSON and return the result
|
|
403
|
+
*/
|
|
404
|
+
json() {
|
|
405
|
+
if (this.#json !== undefined)
|
|
406
|
+
return this.#json;
|
|
407
|
+
const text = this.text();
|
|
408
|
+
const obj = JSON.parse(text || '{}');
|
|
409
|
+
this.#json = obj;
|
|
410
|
+
return obj;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Pass the contents of a @vltpkg/cache.Cache object as a buffer,
|
|
414
|
+
* and this static method will decode it into a CacheEntry representing
|
|
415
|
+
* the cached response.
|
|
416
|
+
*/
|
|
417
|
+
static decode(buffer) {
|
|
418
|
+
if (buffer.length < 4) {
|
|
419
|
+
return emptyCacheEntry;
|
|
420
|
+
}
|
|
421
|
+
const headSize = readSize(buffer, 0);
|
|
422
|
+
if (buffer.length < headSize) {
|
|
423
|
+
return emptyCacheEntry;
|
|
424
|
+
}
|
|
425
|
+
const statusCode = Number(getDecodedValue(buffer.subarray(4, 7)));
|
|
426
|
+
const headersBuffer = buffer.subarray(7, headSize);
|
|
427
|
+
// walk through the headers array, building up the rawHeaders
|
|
428
|
+
const headers = [];
|
|
429
|
+
let i = 0;
|
|
430
|
+
let integrity = undefined;
|
|
431
|
+
while (i < headersBuffer.length - 4) {
|
|
432
|
+
const size = readSize(headersBuffer, i);
|
|
433
|
+
const val = headersBuffer.subarray(i + 4, i + size);
|
|
434
|
+
// if the last one was the key integrity, then this one is the value
|
|
435
|
+
if (headers.length % 2 === 1) {
|
|
436
|
+
const k = getDecodedValue(headers[headers.length - 1]).toLowerCase();
|
|
437
|
+
if (k === 'integrity')
|
|
438
|
+
integrity = getDecodedValue(val);
|
|
439
|
+
}
|
|
440
|
+
headers.push(val);
|
|
441
|
+
i += size;
|
|
442
|
+
}
|
|
443
|
+
const body = buffer.subarray(headSize);
|
|
444
|
+
const c = new CacheEntry(statusCode, setRawHeader(headers, 'content-length', String(body.byteLength)), {
|
|
445
|
+
body,
|
|
446
|
+
integrity,
|
|
447
|
+
trustIntegrity: true,
|
|
448
|
+
contentLength: body.byteLength,
|
|
449
|
+
});
|
|
450
|
+
if (c.isJSON) {
|
|
451
|
+
try {
|
|
452
|
+
c.json();
|
|
453
|
+
}
|
|
454
|
+
catch {
|
|
455
|
+
return emptyCacheEntry;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return c;
|
|
459
|
+
}
|
|
460
|
+
static isGzipEntry(buffer) {
|
|
461
|
+
if (buffer.length < 4)
|
|
462
|
+
return false;
|
|
463
|
+
const headSize = readSize(buffer, 0);
|
|
464
|
+
const gzipBytes = buffer.subarray(headSize, headSize + 2);
|
|
465
|
+
return gzipBytes[0] === 0x1f && gzipBytes[1] === 0x8b;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Encode the entry as a single Buffer for writing to the cache
|
|
469
|
+
*/
|
|
470
|
+
encode() {
|
|
471
|
+
if (this.isJSON)
|
|
472
|
+
this.json();
|
|
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 = [];
|
|
487
|
+
for (const h of this.#headers) {
|
|
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;
|
|
512
|
+
}
|
|
513
|
+
out.set(this._body, off);
|
|
514
|
+
return out;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const emptyCacheEntry = new CacheEntry(0, [], { contentLength: 0 });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const register: (path: string, method: "HEAD" | "GET", url: string | URL) => void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { __CODE_SPLIT_SCRIPT_NAME } from "./revalidate.js";
|
|
3
|
+
const isDeno = globalThis.Deno != undefined;
|
|
4
|
+
let didProcessBeforeExitHook = false;
|
|
5
|
+
const registered = new Map();
|
|
6
|
+
export const register = (path, method, url) => {
|
|
7
|
+
const r = registered.get(path) ?? new Set();
|
|
8
|
+
const key = `${method} ${url}`;
|
|
9
|
+
r.add(key);
|
|
10
|
+
registered.set(path, r);
|
|
11
|
+
if (!didProcessBeforeExitHook) {
|
|
12
|
+
didProcessBeforeExitHook = true;
|
|
13
|
+
process.on('beforeExit', handleBeforeExit);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const handleBeforeExit = () => {
|
|
17
|
+
for (const [path, r] of registered) {
|
|
18
|
+
/* c8 ignore next */
|
|
19
|
+
if (!r.size)
|
|
20
|
+
return;
|
|
21
|
+
const env = { ...process.env };
|
|
22
|
+
const args = [];
|
|
23
|
+
/* c8 ignore start */
|
|
24
|
+
// If we are running deno from source we need to add the
|
|
25
|
+
// unstable flags we need. The '-A' flag does not need
|
|
26
|
+
// to be passed in as Deno supplies that automatically.
|
|
27
|
+
if (isDeno) {
|
|
28
|
+
args.push('--unstable-node-globals', '--unstable-bare-node-builtins');
|
|
29
|
+
}
|
|
30
|
+
/* c8 ignore stop */
|
|
31
|
+
args.push(__CODE_SPLIT_SCRIPT_NAME, path);
|
|
32
|
+
registered.delete(path);
|
|
33
|
+
// Deno on Windows does not support detached processes
|
|
34
|
+
// https://github.com/denoland/deno/issues/25867
|
|
35
|
+
// TODO: figure out something better to do here?
|
|
36
|
+
/* c8 ignore next */
|
|
37
|
+
const detached = !(isDeno && process.platform === 'win32');
|
|
38
|
+
const proc = spawn(process.execPath, args, {
|
|
39
|
+
detached,
|
|
40
|
+
stdio: ['pipe', 'ignore', 'ignore'],
|
|
41
|
+
env,
|
|
42
|
+
});
|
|
43
|
+
for (const key of r) {
|
|
44
|
+
proc.stdin.write(`${key}\0`);
|
|
45
|
+
}
|
|
46
|
+
proc.stdin.end();
|
|
47
|
+
// Another Deno oddity. Calling unref on a spawned process will kill the
|
|
48
|
+
// process unless it is detached. https://github.com/denoland/deno/issues/21446
|
|
49
|
+
// So in this case Deno on Windows will be slower to exit the main process
|
|
50
|
+
// since it will wait for the child process to exit.
|
|
51
|
+
// TODO: figure out something better to do here?
|
|
52
|
+
if (detached) {
|
|
53
|
+
proc.unref();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const deleteHeader: <H extends [string, string[] | string][] | Iterable<[string, string[] | string | undefined]> | Record<string, string[] | string | undefined> | string[]>(headers: H | null | undefined, key: string) => H;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isIterable } from "./is-iterable.js";
|
|
2
|
+
export const deleteHeader = (headers, key) => {
|
|
3
|
+
if (!headers)
|
|
4
|
+
return {};
|
|
5
|
+
if (Array.isArray(headers)) {
|
|
6
|
+
if (!headers.length)
|
|
7
|
+
return headers;
|
|
8
|
+
if (Array.isArray(headers[0])) {
|
|
9
|
+
const index = headers.findIndex(([k]) => k.toLowerCase() === key.toLowerCase());
|
|
10
|
+
if (index !== -1)
|
|
11
|
+
headers.splice(index, 1);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
const h = headers;
|
|
15
|
+
for (let i = 0; i < h.length; i += 2) {
|
|
16
|
+
if (h[i]?.toLowerCase() === key.toLowerCase()) {
|
|
17
|
+
headers.splice(i, 2);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return headers;
|
|
23
|
+
}
|
|
24
|
+
else if (isIterable(headers)) {
|
|
25
|
+
return deleteHeader([...headers], key);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
delete headers[key];
|
|
29
|
+
return headers;
|
|
30
|
+
}
|
|
31
|
+
};
|
package/dist/env.d.ts
ADDED
package/dist/env.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import proc from 'node:process';
|
|
2
|
+
const { Deno, Bun } = globalThis;
|
|
3
|
+
const isObj = (v) => typeof v === 'object' && !!v;
|
|
4
|
+
export const isDeno = isObj(Deno);
|
|
5
|
+
export const isBun = !isDeno && isObj(Bun);
|
|
6
|
+
// bun and deno also report 'node' in process.versions so its only
|
|
7
|
+
// node if it is not bun or deno
|
|
8
|
+
export const isNode = !isDeno && !isBun && 'node' in proc.versions;
|
|
9
|
+
// All the runtimes put their versions into process.versions
|
|
10
|
+
export const bun = isBun ? proc.versions.bun : undefined;
|
|
11
|
+
export const deno = isDeno ? proc.versions.deno : undefined;
|
|
12
|
+
export const node = isNode ? proc.versions.node : undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getHeader: (headers: Iterable<[string, string[] | string | undefined]> | Record<string, string[] | string | undefined> | string[] | null | undefined, key: string) => string[] | string | undefined;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const isIterable = (o) => !!o && typeof o === 'object' && Symbol.iterator in o;
|
|
2
|
+
export const getHeader = (headers, key) => {
|
|
3
|
+
if (!headers)
|
|
4
|
+
return undefined;
|
|
5
|
+
key = key.toLowerCase();
|
|
6
|
+
if (Array.isArray(headers)) {
|
|
7
|
+
if (!headers.length)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (Array.isArray(headers[0])) {
|
|
10
|
+
// [string,HeaderValue][]
|
|
11
|
+
for (const [k, v] of headers) {
|
|
12
|
+
if (k.toLowerCase() === key)
|
|
13
|
+
return v;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else if (headers.length % 2 === 0) {
|
|
17
|
+
// [k, v, k2, v2, ...]
|
|
18
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
19
|
+
if (headers[i]?.toLowerCase() === key)
|
|
20
|
+
return headers[i + 1];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else if (isIterable(headers)) {
|
|
25
|
+
for (const [k, v] of headers) {
|
|
26
|
+
if (k.toLowerCase() === key)
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
32
|
+
if (k.toLowerCase() === key)
|
|
33
|
+
return v;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const handleCacheHitResponse = (resp, entry) => {
|
|
2
|
+
if ((resp.statusCode !== 304 && resp.statusCode !== 412) || !entry)
|
|
3
|
+
return false;
|
|
4
|
+
const d = String(resp.headers.date ?? '') || new Date().toUTCString();
|
|
5
|
+
entry.setHeader('date', d);
|
|
6
|
+
resp.body.resume();
|
|
7
|
+
return true;
|
|
8
|
+
};
|