@vltpkg/registry-client 1.0.4 → 1.0.5
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/cache-entry.d.ts +16 -0
- package/dist/cache-entry.js +77 -53
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -18
- package/dist/response.d.ts +4 -0
- package/dist/response.js +20 -0
- package/dist/revalidate-entry.d.ts +2 -0
- package/dist/revalidate-entry.js +107 -0
- package/dist/revalidate.d.ts +2 -3
- package/dist/revalidate.js +32 -10
- package/package.json +9 -9
package/dist/cache-entry.d.ts
CHANGED
|
@@ -69,6 +69,11 @@ export declare class CacheEntry {
|
|
|
69
69
|
addBody(b: Uint8Array): void;
|
|
70
70
|
/** True when this entry was decoded from the on-disk cache. */
|
|
71
71
|
get fromCache(): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Byte length of the encoded head (4-byte length prefix + status +
|
|
74
|
+
* headers). Set when decoded from cache or after `encodeHead()`.
|
|
75
|
+
*/
|
|
76
|
+
get headSize(): number | undefined;
|
|
72
77
|
get statusCode(): number;
|
|
73
78
|
get headers(): Uint8Array[];
|
|
74
79
|
/**
|
|
@@ -129,6 +134,12 @@ export declare class CacheEntry {
|
|
|
129
134
|
* Parse the entry body as JSON and return the result
|
|
130
135
|
*/
|
|
131
136
|
json(): JSONObj;
|
|
137
|
+
/**
|
|
138
|
+
* Decode only the status/headers from an encoded cache buffer.
|
|
139
|
+
* The body is not read; `headSize` records how many bytes the head
|
|
140
|
+
* occupies so a later `encodeHead()` can be patched in place.
|
|
141
|
+
*/
|
|
142
|
+
static decodeHead(buffer: Uint8Array): CacheEntry;
|
|
132
143
|
/**
|
|
133
144
|
* Pass the contents of a @vltpkg/cache.Cache object as a buffer,
|
|
134
145
|
* and this static method will decode it into a CacheEntry representing
|
|
@@ -136,6 +147,11 @@ export declare class CacheEntry {
|
|
|
136
147
|
*/
|
|
137
148
|
static decode(buffer: Uint8Array): CacheEntry;
|
|
138
149
|
static isGzipEntry(buffer: Uint8Array): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Encode status + headers (no body) as they appear at the start of
|
|
152
|
+
* an on-disk cache file.
|
|
153
|
+
*/
|
|
154
|
+
encodeHead(): Buffer;
|
|
139
155
|
/**
|
|
140
156
|
* Encode the entry as a single Buffer for writing to the cache
|
|
141
157
|
*/
|
package/dist/cache-entry.js
CHANGED
|
@@ -76,6 +76,7 @@ export class CacheEntry {
|
|
|
76
76
|
#trustIntegrity;
|
|
77
77
|
#staleWhileRevalidateFactor;
|
|
78
78
|
#fromCache = false;
|
|
79
|
+
#headSize;
|
|
79
80
|
constructor(statusCode, headers, { body, integrity, trustIntegrity = false, 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 60, contentLength, } = {}) {
|
|
80
81
|
this.#headers = headers;
|
|
81
82
|
this.#statusCode = statusCode;
|
|
@@ -241,6 +242,13 @@ export class CacheEntry {
|
|
|
241
242
|
get fromCache() {
|
|
242
243
|
return this.#fromCache;
|
|
243
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Byte length of the encoded head (4-byte length prefix + status +
|
|
247
|
+
* headers). Set when decoded from cache or after `encodeHead()`.
|
|
248
|
+
*/
|
|
249
|
+
get headSize() {
|
|
250
|
+
return this.#headSize;
|
|
251
|
+
}
|
|
244
252
|
get statusCode() {
|
|
245
253
|
return this.#statusCode;
|
|
246
254
|
}
|
|
@@ -445,29 +453,20 @@ export class CacheEntry {
|
|
|
445
453
|
this.#json = obj;
|
|
446
454
|
return obj;
|
|
447
455
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
* the cached response.
|
|
452
|
-
*/
|
|
453
|
-
static decode(buffer) {
|
|
454
|
-
if (buffer.length < 4) {
|
|
455
|
-
return emptyCacheEntry;
|
|
456
|
-
}
|
|
456
|
+
static #parseHead(buffer) {
|
|
457
|
+
if (buffer.length < 4)
|
|
458
|
+
return undefined;
|
|
457
459
|
const headSize = readSize(buffer, 0);
|
|
458
|
-
if (buffer.length < headSize)
|
|
459
|
-
return
|
|
460
|
-
}
|
|
460
|
+
if (buffer.length < headSize)
|
|
461
|
+
return undefined;
|
|
461
462
|
const statusCode = Number(getDecodedValue(buffer.subarray(4, 7)));
|
|
462
463
|
const headersBuffer = buffer.subarray(7, headSize);
|
|
463
|
-
// walk through the headers array, building up the rawHeaders
|
|
464
464
|
const headers = [];
|
|
465
465
|
let i = 0;
|
|
466
466
|
let integrity = undefined;
|
|
467
467
|
while (i < headersBuffer.length - 4) {
|
|
468
468
|
const size = readSize(headersBuffer, i);
|
|
469
469
|
const val = headersBuffer.subarray(i + 4, i + size);
|
|
470
|
-
// if the last one was the key integrity, then this one is the value
|
|
471
470
|
if (headers.length % 2 === 1) {
|
|
472
471
|
const k = getDecodedValue(headers[headers.length - 1]).toLowerCase();
|
|
473
472
|
if (k === 'integrity')
|
|
@@ -476,14 +475,44 @@ export class CacheEntry {
|
|
|
476
475
|
headers.push(val);
|
|
477
476
|
i += size;
|
|
478
477
|
}
|
|
479
|
-
|
|
480
|
-
|
|
478
|
+
return { statusCode, headers, integrity, headSize };
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Decode only the status/headers from an encoded cache buffer.
|
|
482
|
+
* The body is not read; `headSize` records how many bytes the head
|
|
483
|
+
* occupies so a later `encodeHead()` can be patched in place.
|
|
484
|
+
*/
|
|
485
|
+
static decodeHead(buffer) {
|
|
486
|
+
const parsed = CacheEntry.#parseHead(buffer);
|
|
487
|
+
if (!parsed)
|
|
488
|
+
return emptyCacheEntry;
|
|
489
|
+
const c = new CacheEntry(parsed.statusCode, parsed.headers, {
|
|
490
|
+
body: new Uint8Array(0),
|
|
491
|
+
integrity: parsed.integrity,
|
|
492
|
+
trustIntegrity: true,
|
|
493
|
+
});
|
|
494
|
+
c.#fromCache = true;
|
|
495
|
+
c.#headSize = parsed.headSize;
|
|
496
|
+
return c;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Pass the contents of a @vltpkg/cache.Cache object as a buffer,
|
|
500
|
+
* and this static method will decode it into a CacheEntry representing
|
|
501
|
+
* the cached response.
|
|
502
|
+
*/
|
|
503
|
+
static decode(buffer) {
|
|
504
|
+
const parsed = CacheEntry.#parseHead(buffer);
|
|
505
|
+
if (!parsed)
|
|
506
|
+
return emptyCacheEntry;
|
|
507
|
+
const body = buffer.subarray(parsed.headSize);
|
|
508
|
+
const c = new CacheEntry(parsed.statusCode, setRawHeader(parsed.headers, 'content-length', String(body.byteLength)), {
|
|
481
509
|
body,
|
|
482
|
-
integrity,
|
|
510
|
+
integrity: parsed.integrity,
|
|
483
511
|
trustIntegrity: true,
|
|
484
512
|
contentLength: body.byteLength,
|
|
485
513
|
});
|
|
486
514
|
c.#fromCache = true;
|
|
515
|
+
c.#headSize = parsed.headSize;
|
|
487
516
|
try {
|
|
488
517
|
if (c.isJSON) {
|
|
489
518
|
c.unzip();
|
|
@@ -504,52 +533,47 @@ export class CacheEntry {
|
|
|
504
533
|
return gzipBytes[0] === 0x1f && gzipBytes[1] === 0x8b;
|
|
505
534
|
}
|
|
506
535
|
/**
|
|
507
|
-
* Encode
|
|
536
|
+
* Encode status + headers (no body) as they appear at the start of
|
|
537
|
+
* an on-disk cache file.
|
|
508
538
|
*/
|
|
509
|
-
|
|
510
|
-
if (this.isJSON)
|
|
511
|
-
this.json();
|
|
539
|
+
encodeHead() {
|
|
512
540
|
const statusStr = String(this.#statusCode);
|
|
513
541
|
const statusBytes = getEncondedValue(statusStr);
|
|
514
|
-
// compute headLength = 4 (length field itself) + statusBytes + Σ(4 + headerLen) for each header item
|
|
515
542
|
let headLength = 4 + statusBytes.byteLength;
|
|
516
543
|
for (const h of this.#headers)
|
|
517
544
|
headLength += 4 + h.byteLength;
|
|
518
|
-
|
|
519
|
-
const headLenBytes = new Uint8Array(4);
|
|
520
|
-
headLenBytes[0] = (headLength >> 24) & 0xff;
|
|
521
|
-
headLenBytes[1] = (headLength >> 16) & 0xff;
|
|
522
|
-
headLenBytes[2] = (headLength >> 8) & 0xff;
|
|
523
|
-
headLenBytes[3] = headLength & 0xff;
|
|
524
|
-
// header chunks: [len, bytes] for each header item
|
|
525
|
-
const headerChunks = [];
|
|
526
|
-
for (const h of this.#headers) {
|
|
527
|
-
const l = headLenBytes.byteLength + h.byteLength;
|
|
528
|
-
const lb = new Uint8Array(4);
|
|
529
|
-
lb[0] = (l >> 24) & 0xff;
|
|
530
|
-
lb[1] = (l >> 16) & 0xff;
|
|
531
|
-
lb[2] = (l >> 8) & 0xff;
|
|
532
|
-
lb[3] = l & 0xff;
|
|
533
|
-
headerChunks.push(lb, h);
|
|
534
|
-
}
|
|
535
|
-
// total size
|
|
536
|
-
const total = headLenBytes.byteLength +
|
|
537
|
-
statusBytes.byteLength +
|
|
538
|
-
headerChunks.reduce((n, b) => n + b.byteLength, 0) +
|
|
539
|
-
this._body.byteLength;
|
|
540
|
-
// returns the concatenate buffer with all the pieces
|
|
541
|
-
const outBuffer = new ArrayBuffer(total);
|
|
542
|
-
const out = Buffer.from(outBuffer, 0, total);
|
|
545
|
+
const out = Buffer.from(new ArrayBuffer(headLength), 0, headLength);
|
|
543
546
|
let off = 0;
|
|
544
|
-
out
|
|
545
|
-
off
|
|
547
|
+
out[off++] = (headLength >> 24) & 0xff;
|
|
548
|
+
out[off++] = (headLength >> 16) & 0xff;
|
|
549
|
+
out[off++] = (headLength >> 8) & 0xff;
|
|
550
|
+
out[off++] = headLength & 0xff;
|
|
546
551
|
out.set(statusBytes, off);
|
|
547
552
|
off += statusBytes.byteLength;
|
|
548
|
-
for (const
|
|
549
|
-
|
|
550
|
-
off
|
|
553
|
+
for (const h of this.#headers) {
|
|
554
|
+
const l = 4 + h.byteLength;
|
|
555
|
+
out[off++] = (l >> 24) & 0xff;
|
|
556
|
+
out[off++] = (l >> 16) & 0xff;
|
|
557
|
+
out[off++] = (l >> 8) & 0xff;
|
|
558
|
+
out[off++] = l & 0xff;
|
|
559
|
+
out.set(h, off);
|
|
560
|
+
off += h.byteLength;
|
|
551
561
|
}
|
|
552
|
-
|
|
562
|
+
this.#headSize = headLength;
|
|
563
|
+
return out;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Encode the entry as a single Buffer for writing to the cache
|
|
567
|
+
*/
|
|
568
|
+
encode() {
|
|
569
|
+
if (this.isJSON)
|
|
570
|
+
this.json();
|
|
571
|
+
const head = this.encodeHead();
|
|
572
|
+
const body = this._body;
|
|
573
|
+
const total = head.byteLength + body.byteLength;
|
|
574
|
+
const out = Buffer.from(new ArrayBuffer(total), 0, total);
|
|
575
|
+
out.set(head, 0);
|
|
576
|
+
out.set(body, head.byteLength);
|
|
553
577
|
return out;
|
|
554
578
|
}
|
|
555
579
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { OidcOptions } from './oidc.ts';
|
|
|
13
13
|
export { CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, runtimeTokens, setRuntimeToken, setToken, type JSONObj, type OidcOptions, type Token, type TokenResponse, type WebAuthChallenge, };
|
|
14
14
|
export type CacheableMethod = 'GET' | 'HEAD';
|
|
15
15
|
export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
|
|
16
|
+
export declare const cacheKey: (method: string, url: URL | string) => string;
|
|
16
17
|
export type RegistryClientOptions = {
|
|
17
18
|
/**
|
|
18
19
|
* Path on disk where the cache should be stored
|
package/dist/index.js
CHANGED
|
@@ -23,10 +23,11 @@ import { isRedirect, redirect } from "./redirect.js";
|
|
|
23
23
|
import { setCacheHeaders } from "./set-cache-headers.js";
|
|
24
24
|
import { getTokenResponse } from "./token-response.js";
|
|
25
25
|
import { getWebAuthChallenge } from "./web-auth-challenge.js";
|
|
26
|
-
import {
|
|
26
|
+
import { collectHeaders, readBody } from "./response.js";
|
|
27
27
|
import { oidc } from "./oidc.js";
|
|
28
28
|
export { CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, runtimeTokens, setRuntimeToken, setToken, };
|
|
29
29
|
export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
|
|
30
|
+
export const cacheKey = (method, url) => `${method !== 'GET' ? method + ' ' : ''}${url}`;
|
|
30
31
|
const { version } = loadPackageJson(import.meta.filename, process.env.__VLT_INTERNAL_REGISTRY_CLIENT_PACKAGE_JSON);
|
|
31
32
|
const nua = globalThis.navigator?.userAgent ??
|
|
32
33
|
(bun ? `Bun/${bun}`
|
|
@@ -267,7 +268,7 @@ export class RegistryClient {
|
|
|
267
268
|
// Method + URL only. Headers (including accept) are not part of the
|
|
268
269
|
// key and there is no Vary handling — callers must not vary the
|
|
269
270
|
// response representation for the same URL.
|
|
270
|
-
const key =
|
|
271
|
+
const key = cacheKey(method, u);
|
|
271
272
|
const buffer = useCache ?
|
|
272
273
|
await this.cache.fetch(key, { context: { integrity } })
|
|
273
274
|
: undefined;
|
|
@@ -349,17 +350,7 @@ export class RegistryClient {
|
|
|
349
350
|
consumedBody = otpResult.bodyConsumed;
|
|
350
351
|
}
|
|
351
352
|
}
|
|
352
|
-
const h =
|
|
353
|
-
for (const [key, value] of Object.entries(response.headers)) {
|
|
354
|
-
/* c8 ignore start - theoretical */
|
|
355
|
-
if (Array.isArray(value)) {
|
|
356
|
-
h.push(getEncondedValue(key), getEncondedValue(value.join(', ')));
|
|
357
|
-
/* c8 ignore stop */
|
|
358
|
-
}
|
|
359
|
-
else if (typeof value === 'string') {
|
|
360
|
-
h.push(getEncondedValue(key), getEncondedValue(value));
|
|
361
|
-
}
|
|
362
|
-
}
|
|
353
|
+
const h = collectHeaders(response);
|
|
363
354
|
const { integrity, trustIntegrity } = options;
|
|
364
355
|
// When otplease already consumed the body, use its length
|
|
365
356
|
// instead of the Content-Length header to size the CacheEntry
|
|
@@ -393,10 +384,6 @@ export class RegistryClient {
|
|
|
393
384
|
}
|
|
394
385
|
return result;
|
|
395
386
|
}
|
|
396
|
-
response
|
|
397
|
-
return await new Promise((res, rej) => {
|
|
398
|
-
response.body.on('error', rej);
|
|
399
|
-
response.body.on('end', () => res(result));
|
|
400
|
-
});
|
|
387
|
+
return await readBody(response, result);
|
|
401
388
|
}
|
|
402
389
|
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Dispatcher } from 'undici';
|
|
2
|
+
import type { CacheEntry } from './cache-entry.ts';
|
|
3
|
+
export declare const collectHeaders: (response: Dispatcher.ResponseData) => Uint8Array[];
|
|
4
|
+
export declare const readBody: (response: Dispatcher.ResponseData, entry: CacheEntry) => Promise<CacheEntry>;
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { getEncondedValue } from "./string-encoding.js";
|
|
2
|
+
export const collectHeaders = (response) => {
|
|
3
|
+
const h = [];
|
|
4
|
+
for (const [key, value] of Object.entries(response.headers)) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
h.push(getEncondedValue(key), getEncondedValue(value.join(', ')));
|
|
7
|
+
}
|
|
8
|
+
else if (typeof value === 'string') {
|
|
9
|
+
h.push(getEncondedValue(key), getEncondedValue(value));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return h;
|
|
13
|
+
};
|
|
14
|
+
export const readBody = async (response, entry) => {
|
|
15
|
+
response.body.on('data', (chunk) => entry.addBody(chunk));
|
|
16
|
+
return await new Promise((res, rej) => {
|
|
17
|
+
response.body.on('error', rej);
|
|
18
|
+
response.body.on('end', () => res(entry));
|
|
19
|
+
});
|
|
20
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { open, readFile } from 'node:fs/promises';
|
|
3
|
+
import { addHeader } from "./add-header.js";
|
|
4
|
+
import { getTokenByURL } from "./auth.js";
|
|
5
|
+
import { CacheEntry } from "./cache-entry.js";
|
|
6
|
+
import { handleCacheHitResponse } from "./handle-304-response.js";
|
|
7
|
+
import { cacheKey, userAgent } from "./index.js";
|
|
8
|
+
import { collectHeaders, readBody } from "./response.js";
|
|
9
|
+
import { setCacheHeaders } from "./set-cache-headers.js";
|
|
10
|
+
const patchDateFallback = async (rc, file, key, head) => {
|
|
11
|
+
const date = head.getHeaderString('date');
|
|
12
|
+
/* c8 ignore next */
|
|
13
|
+
if (!date)
|
|
14
|
+
return;
|
|
15
|
+
const full = await readFile(file);
|
|
16
|
+
const fullEntry = CacheEntry.decode(full);
|
|
17
|
+
if (!fullEntry.statusCode)
|
|
18
|
+
return;
|
|
19
|
+
fullEntry.setHeader('date', date);
|
|
20
|
+
const buf = fullEntry.encode();
|
|
21
|
+
rc.cache.set(key, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength), { integrity: fullEntry.integrity });
|
|
22
|
+
};
|
|
23
|
+
const readHead = async (fh) => {
|
|
24
|
+
const sizeBuf = Buffer.alloc(4);
|
|
25
|
+
const { bytesRead } = await fh.read(sizeBuf, 0, 4, 0);
|
|
26
|
+
if (bytesRead < 4)
|
|
27
|
+
return;
|
|
28
|
+
const headSize = sizeBuf.readUInt32BE(0);
|
|
29
|
+
if (headSize < 7)
|
|
30
|
+
return;
|
|
31
|
+
const headBuf = Buffer.alloc(headSize);
|
|
32
|
+
sizeBuf.copy(headBuf, 0, 0, 4);
|
|
33
|
+
const { bytesRead: n } = await fh.read(headBuf, 4, headSize - 4, 4);
|
|
34
|
+
if (n < headSize - 4)
|
|
35
|
+
return;
|
|
36
|
+
const entry = CacheEntry.decodeHead(headBuf);
|
|
37
|
+
return entry.statusCode ? entry : undefined;
|
|
38
|
+
};
|
|
39
|
+
export const revalidateEntry = async (rc, method, url) => {
|
|
40
|
+
try {
|
|
41
|
+
const u = typeof url === 'string' ? new URL(url) : url;
|
|
42
|
+
const key = cacheKey(method, u);
|
|
43
|
+
const file = rc.cache.path(key);
|
|
44
|
+
let fh;
|
|
45
|
+
try {
|
|
46
|
+
fh = await open(file, 'r+');
|
|
47
|
+
const entry = await readHead(fh);
|
|
48
|
+
if (!entry)
|
|
49
|
+
return;
|
|
50
|
+
const origHeadSize = entry.headSize;
|
|
51
|
+
const options = {
|
|
52
|
+
method,
|
|
53
|
+
origin: u.origin,
|
|
54
|
+
};
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated -- dispatcher requires path
|
|
56
|
+
options.path = u.pathname.replace(/\/+$/, '') + u.search;
|
|
57
|
+
options.headers = addHeader(addHeader(addHeader(options.headers, 'accept-encoding', 'gzip;q=1.0, identity;q=0.5'), 'user-agent', userAgent), 'npm-session', randomUUID());
|
|
58
|
+
setCacheHeaders(options, entry);
|
|
59
|
+
options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), rc.identity));
|
|
60
|
+
const response = await rc.agent.request(options);
|
|
61
|
+
if (handleCacheHitResponse(response, entry)) {
|
|
62
|
+
const newHead = entry.encodeHead();
|
|
63
|
+
if (newHead.byteLength === origHeadSize) {
|
|
64
|
+
await fh.write(newHead, 0, newHead.byteLength, 0);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
await fh.close();
|
|
68
|
+
fh = undefined;
|
|
69
|
+
await patchDateFallback(rc, file, key, entry);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (response.statusCode === 200 && method === 'GET') {
|
|
73
|
+
const result = new CacheEntry(response.statusCode, collectHeaders(response), {
|
|
74
|
+
'stale-while-revalidate-factor': rc.staleWhileRevalidateFactor,
|
|
75
|
+
contentLength: response.headers['content-length'] ?
|
|
76
|
+
Number(response.headers['content-length'])
|
|
77
|
+
: undefined,
|
|
78
|
+
});
|
|
79
|
+
await readBody(response, result);
|
|
80
|
+
result.unzip();
|
|
81
|
+
const buf = result.encode();
|
|
82
|
+
rc.cache.set(key, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength), { integrity: result.integrity });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Lean path uses bare undici (no redirect following). Hand
|
|
86
|
+
// 3xx back to RegistryClient so per-origin auth, cycle limits,
|
|
87
|
+
// and the final cache write stay in the tested request() path.
|
|
88
|
+
const status = response.statusCode;
|
|
89
|
+
if (status === 301 ||
|
|
90
|
+
status === 302 ||
|
|
91
|
+
status === 303 ||
|
|
92
|
+
status === 307 ||
|
|
93
|
+
status === 308) {
|
|
94
|
+
response.body.resume();
|
|
95
|
+
await fh.close();
|
|
96
|
+
fh = undefined;
|
|
97
|
+
await rc.request(u, { method, staleWhileRevalidate: false });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
response.body.resume();
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
await fh?.close();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch { }
|
|
107
|
+
};
|
package/dist/revalidate.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
+
import type EventEmitter from 'node:events';
|
|
1
2
|
export declare const __CODE_SPLIT_SCRIPT_NAME: string;
|
|
2
|
-
export declare const main: (cache?: string, input?:
|
|
3
|
-
fd: 0;
|
|
4
|
-
}) => Promise<boolean>;
|
|
3
|
+
export declare const main: (cache?: string, input?: EventEmitter) => Promise<boolean>;
|
package/dist/revalidate.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
// otherwise we have a cyclical dependency cycle of dependencies in a cycle,
|
|
3
|
-
// which is even more cyclical than this description describing it.
|
|
1
|
+
import { setPriority } from 'node:os';
|
|
4
2
|
import { pathToFileURL } from 'node:url';
|
|
5
3
|
import { RegistryClient } from "./index.js";
|
|
4
|
+
import { revalidateEntry } from "./revalidate-entry.js";
|
|
6
5
|
export const __CODE_SPLIT_SCRIPT_NAME = import.meta.filename;
|
|
7
6
|
const isMain = (path) => path === __CODE_SPLIT_SCRIPT_NAME ||
|
|
8
7
|
path === pathToFileURL(__CODE_SPLIT_SCRIPT_NAME).toString();
|
|
8
|
+
const defaultConcurrency = 6;
|
|
9
|
+
const revalidateConcurrency = (raw) => {
|
|
10
|
+
const n = Number(raw);
|
|
11
|
+
return Number.isFinite(n) && n >= 1 ?
|
|
12
|
+
Math.min(32, Math.floor(n))
|
|
13
|
+
: defaultConcurrency;
|
|
14
|
+
};
|
|
15
|
+
const runPool = async (reqs, concurrency, fn) => {
|
|
16
|
+
let next = 0;
|
|
17
|
+
const n = Math.min(concurrency, reqs.length);
|
|
18
|
+
await Promise.all(Array.from({ length: n }, async () => {
|
|
19
|
+
for (;;) {
|
|
20
|
+
const i = next++;
|
|
21
|
+
if (i >= reqs.length)
|
|
22
|
+
return;
|
|
23
|
+
const req = reqs[i];
|
|
24
|
+
/* c8 ignore next */
|
|
25
|
+
if (req === undefined)
|
|
26
|
+
return;
|
|
27
|
+
await fn(req[0], req[1]);
|
|
28
|
+
}
|
|
29
|
+
}));
|
|
30
|
+
};
|
|
9
31
|
export const main = async (cache, input = process.stdin) => {
|
|
10
32
|
if (!cache) {
|
|
11
33
|
return false;
|
|
@@ -13,7 +35,7 @@ export const main = async (cache, input = process.stdin) => {
|
|
|
13
35
|
const reqs = await new Promise(res => {
|
|
14
36
|
const chunks = [];
|
|
15
37
|
let chunkLen = 0;
|
|
16
|
-
input.on('data', chunk => {
|
|
38
|
+
input.on('data', (chunk) => {
|
|
17
39
|
chunks.push(chunk);
|
|
18
40
|
chunkLen += chunk.length;
|
|
19
41
|
});
|
|
@@ -32,16 +54,16 @@ export const main = async (cache, input = process.stdin) => {
|
|
|
32
54
|
return false;
|
|
33
55
|
}
|
|
34
56
|
const rc = new RegistryClient({ cache });
|
|
35
|
-
await
|
|
36
|
-
await rc.request(url, {
|
|
37
|
-
method,
|
|
38
|
-
staleWhileRevalidate: false,
|
|
39
|
-
});
|
|
40
|
-
}));
|
|
57
|
+
await runPool(reqs, revalidateConcurrency(process.env.VLT_REVALIDATE_CONCURRENCY), (method, url) => revalidateEntry(rc, method, url));
|
|
41
58
|
return true;
|
|
42
59
|
};
|
|
43
60
|
if (isMain(process.argv[1])) {
|
|
44
61
|
process.title = 'vlt-cache-revalidate';
|
|
62
|
+
try {
|
|
63
|
+
setPriority(19);
|
|
64
|
+
/* c8 ignore next */
|
|
65
|
+
}
|
|
66
|
+
catch { }
|
|
45
67
|
const cacheFolder = process.argv.length === 2 ? undefined : process.argv.at(-1);
|
|
46
68
|
const res = await main(cacheFolder, process.stdin);
|
|
47
69
|
if (!res) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/registry-client",
|
|
3
3
|
"description": "Fetch package artifacts and metadata from registries",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/cache": "1.0.
|
|
17
|
-
"@vltpkg/cache-unzip": "1.0.
|
|
18
|
-
"@vltpkg/error-cause": "1.0.
|
|
19
|
-
"@vltpkg/keychain": "1.0.
|
|
20
|
-
"@vltpkg/output": "1.0.
|
|
21
|
-
"@vltpkg/types": "1.0.
|
|
22
|
-
"@vltpkg/url-open": "1.0.
|
|
23
|
-
"@vltpkg/xdg": "1.0.
|
|
16
|
+
"@vltpkg/cache": "1.0.5",
|
|
17
|
+
"@vltpkg/cache-unzip": "1.0.5",
|
|
18
|
+
"@vltpkg/error-cause": "1.0.5",
|
|
19
|
+
"@vltpkg/keychain": "1.0.5",
|
|
20
|
+
"@vltpkg/output": "1.0.5",
|
|
21
|
+
"@vltpkg/types": "1.0.5",
|
|
22
|
+
"@vltpkg/url-open": "1.0.5",
|
|
23
|
+
"@vltpkg/xdg": "1.0.5",
|
|
24
24
|
"cache-control-parser": "^2.0.6",
|
|
25
25
|
"package-json-from-dist": "^1.0.1",
|
|
26
26
|
"undici": "^7.16.0"
|