@vltpkg/registry-client 1.2.0 → 1.3.1
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 +5 -0
- package/dist/cache-entry.js +13 -3
- package/dist/index.d.ts +15 -0
- package/dist/index.js +73 -9
- package/dist/registry-error.d.ts +1 -0
- package/dist/registry-error.js +3 -2
- package/package.json +10 -10
package/dist/cache-entry.d.ts
CHANGED
|
@@ -184,6 +184,11 @@ export declare class CacheEntry {
|
|
|
184
184
|
* i.e. a tarball the global store can be built from.
|
|
185
185
|
*/
|
|
186
186
|
static isTarballEntry(buffer: Uint8Array): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* The hash recorded on such an entry, or undefined if this is not one.
|
|
189
|
+
* {@link isTarballEntry} is built on this, and both only need the head.
|
|
190
|
+
*/
|
|
191
|
+
static tarballIntegrity(buffer: Uint8Array): Integrity | undefined;
|
|
187
192
|
/**
|
|
188
193
|
* Encode status + headers (no body) as they appear at the start of
|
|
189
194
|
* an on-disk cache file.
|
package/dist/cache-entry.js
CHANGED
|
@@ -599,11 +599,21 @@ export class CacheEntry {
|
|
|
599
599
|
* i.e. a tarball the global store can be built from.
|
|
600
600
|
*/
|
|
601
601
|
static isTarballEntry(buffer) {
|
|
602
|
+
return !!CacheEntry.tarballIntegrity(buffer);
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* The hash recorded on such an entry, or undefined if this is not one.
|
|
606
|
+
* {@link isTarballEntry} is built on this, and both only need the head.
|
|
607
|
+
*/
|
|
608
|
+
static tarballIntegrity(buffer) {
|
|
602
609
|
const parsed = CacheEntry.#parseHead(buffer);
|
|
603
|
-
if (parsed?.statusCode !== 200 || !parsed.integrity)
|
|
604
|
-
return
|
|
610
|
+
if (parsed?.statusCode !== 200 || !parsed.integrity) {
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
605
613
|
const ct = getRawHeader(parsed.headers, 'content-type');
|
|
606
|
-
return !ct || !/\bjson\b/.test(getDecodedValue(ct))
|
|
614
|
+
return !ct || !/\bjson\b/.test(getDecodedValue(ct)) ?
|
|
615
|
+
parsed.integrity
|
|
616
|
+
: undefined;
|
|
607
617
|
}
|
|
608
618
|
/**
|
|
609
619
|
* Encode status + headers (no body) as they appear at the start of
|
package/dist/index.d.ts
CHANGED
|
@@ -185,6 +185,21 @@ export declare class RegistryClient {
|
|
|
185
185
|
* hang on the `doneUrl` until it returns a {@link TokenResponse} object.
|
|
186
186
|
*/
|
|
187
187
|
webAuthOpener({ doneUrl, authUrl }: WebAuthChallenge): Promise<TokenResponse>;
|
|
188
|
+
/**
|
|
189
|
+
* The hash the cache recorded for `url`. This reads only the entry's
|
|
190
|
+
* head, which is about a kilobyte, rather than the body, which can be a
|
|
191
|
+
* megabyte. A caller that is about to look the package up in the global
|
|
192
|
+
* store needs nothing else, and if the store misses it can still read
|
|
193
|
+
* the body afterwards.
|
|
194
|
+
*
|
|
195
|
+
* Returns undefined if there is no entry, or if the entry is not a
|
|
196
|
+
* tarball. This never reads the body, so if `cache-unzip` rewrote the
|
|
197
|
+
* entry underneath us the worst case is a stale hash. That misses in the
|
|
198
|
+
* store; it cannot return the wrong bytes.
|
|
199
|
+
*/
|
|
200
|
+
cachedIntegrity(url: URL | string, options?: {
|
|
201
|
+
method?: CacheableMethod;
|
|
202
|
+
}): Integrity | undefined;
|
|
188
203
|
/**
|
|
189
204
|
* Read a valid cached non-JSON body straight off disk, bypassing the
|
|
190
205
|
* in-memory cache LRU so that a tarball is never held in it.
|
package/dist/index.js
CHANGED
|
@@ -6,8 +6,7 @@ import { logRequest } from '@vltpkg/output';
|
|
|
6
6
|
import { urlOpen } from '@vltpkg/url-open';
|
|
7
7
|
import { XDG } from '@vltpkg/xdg';
|
|
8
8
|
import { randomUUID } from 'node:crypto';
|
|
9
|
-
import { readFileSync } from 'node:fs';
|
|
10
|
-
import { STATUS_CODES } from 'node:http';
|
|
9
|
+
import { closeSync, openSync, readFileSync, readSync } from 'node:fs';
|
|
11
10
|
import { availableParallelism } from 'node:os';
|
|
12
11
|
import { dirname, resolve } from 'node:path';
|
|
13
12
|
import { setTimeout } from 'node:timers/promises';
|
|
@@ -22,7 +21,7 @@ import { handleCacheHitResponse } from "./handle-304-response.js";
|
|
|
22
21
|
import { otplease } from "./otplease.js";
|
|
23
22
|
import { getDispatcher } from "./proxy.js";
|
|
24
23
|
import { isRedirect, redirect } from "./redirect.js";
|
|
25
|
-
import { assertOk, registryErrorMessage, requestError, } from "./registry-error.js";
|
|
24
|
+
import { assertOk, registryErrorMessage, requestError, statusText, } from "./registry-error.js";
|
|
26
25
|
import { setCacheHeaders } from "./set-cache-headers.js";
|
|
27
26
|
import { getTokenResponse } from "./token-response.js";
|
|
28
27
|
import { getWebAuthChallenge } from "./web-auth-challenge.js";
|
|
@@ -33,6 +32,9 @@ import { oidc } from "./oidc.js";
|
|
|
33
32
|
const log = (msg) => console.error(msg);
|
|
34
33
|
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
|
|
35
34
|
export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
|
|
35
|
+
// A head is about a kilobyte. This limit exists only so that a corrupt
|
|
36
|
+
// length cannot make us allocate something enormous.
|
|
37
|
+
const maxHeadSize = 1024 * 1024;
|
|
36
38
|
export const cacheKey = (method, url) => `${method !== 'GET' ? method + ' ' : ''}${url}`;
|
|
37
39
|
/**
|
|
38
40
|
* The `User-Agent` header sent with every request. Re-exported from
|
|
@@ -56,12 +58,13 @@ export { userAgent };
|
|
|
56
58
|
// CDN throttling beyond what reify can consume.
|
|
57
59
|
// HTTP/2 (allowH2) is untested and unjustified while transport CPU is ~1/4
|
|
58
60
|
// of body handling.
|
|
61
|
+
// keepAliveTimeoutThreshold: undici default. Values >= the server's
|
|
62
|
+
// Keep-Alive hint (Node http: timeout=5) reset the socket every response.
|
|
59
63
|
const agentOptions = {
|
|
60
64
|
bodyTimeout: 600_000,
|
|
61
65
|
headersTimeout: 600_000,
|
|
62
66
|
keepAliveMaxTimeout: 1_200_000,
|
|
63
67
|
keepAliveTimeout: 600_000,
|
|
64
|
-
keepAliveTimeoutThreshold: 30_000,
|
|
65
68
|
connect: {
|
|
66
69
|
timeout: 600_000,
|
|
67
70
|
keepAlive: true,
|
|
@@ -140,7 +143,7 @@ export class RegistryClient {
|
|
|
140
143
|
catch {
|
|
141
144
|
// Registries and proxies can return plain text or HTML errors.
|
|
142
145
|
}
|
|
143
|
-
const status =
|
|
146
|
+
const status = statusText(statusCode);
|
|
144
147
|
throw error(`Failed to fetch paginated results: ${statusCode}${status ? ` ${status}` : ''}${typeof detail === 'string' && detail ? `: ${detail}` : ''}`, { code: 'EREQUEST', status: statusCode, url: String(url) });
|
|
145
148
|
}
|
|
146
149
|
let body;
|
|
@@ -286,7 +289,7 @@ export class RegistryClient {
|
|
|
286
289
|
}
|
|
287
290
|
// TODO: fall back to username/password login, and/or couchdb PUT login
|
|
288
291
|
const { statusCode } = response;
|
|
289
|
-
const status =
|
|
292
|
+
const status = statusText(statusCode);
|
|
290
293
|
throw error(`Failed to perform web login: ${statusCode}${status ? ` ${status}` : ''}`, {
|
|
291
294
|
code: 'EREQUEST',
|
|
292
295
|
url: webLoginURL,
|
|
@@ -378,6 +381,50 @@ export class RegistryClient {
|
|
|
378
381
|
response,
|
|
379
382
|
});
|
|
380
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* The hash the cache recorded for `url`. This reads only the entry's
|
|
386
|
+
* head, which is about a kilobyte, rather than the body, which can be a
|
|
387
|
+
* megabyte. A caller that is about to look the package up in the global
|
|
388
|
+
* store needs nothing else, and if the store misses it can still read
|
|
389
|
+
* the body afterwards.
|
|
390
|
+
*
|
|
391
|
+
* Returns undefined if there is no entry, or if the entry is not a
|
|
392
|
+
* tarball. This never reads the body, so if `cache-unzip` rewrote the
|
|
393
|
+
* entry underneath us the worst case is a stale hash. That misses in the
|
|
394
|
+
* store; it cannot return the wrong bytes.
|
|
395
|
+
*/
|
|
396
|
+
cachedIntegrity(url, options = {}) {
|
|
397
|
+
const { method = 'GET' } = options;
|
|
398
|
+
try {
|
|
399
|
+
const key = cacheKey(method, typeof url === 'string' ? new URL(url) : url);
|
|
400
|
+
// The in-memory copy can be newer than the file on disk, so let
|
|
401
|
+
// request() serve those. Same rule as cachedBody.
|
|
402
|
+
if (this.cache.peek(key))
|
|
403
|
+
return undefined;
|
|
404
|
+
const fd = openSync(this.cache.path(key), 'r');
|
|
405
|
+
try {
|
|
406
|
+
const size = Buffer.allocUnsafe(4);
|
|
407
|
+
if (readSync(fd, size, 0, 4, 0) < 4)
|
|
408
|
+
return undefined;
|
|
409
|
+
const headSize = size.readUInt32BE(0);
|
|
410
|
+
// Reject a length we do not believe, so a corrupt file cannot
|
|
411
|
+
// make us allocate a huge buffer.
|
|
412
|
+
if (headSize < 7 || headSize > maxHeadSize)
|
|
413
|
+
return undefined;
|
|
414
|
+
const head = Buffer.allocUnsafe(headSize);
|
|
415
|
+
if (readSync(fd, head, 0, headSize, 0) < headSize) {
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
return CacheEntry.tarballIntegrity(head);
|
|
419
|
+
}
|
|
420
|
+
finally {
|
|
421
|
+
closeSync(fd);
|
|
422
|
+
}
|
|
423
|
+
/* c8 ignore next */
|
|
424
|
+
}
|
|
425
|
+
catch { }
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
381
428
|
/**
|
|
382
429
|
* Read a valid cached non-JSON body straight off disk, bypassing the
|
|
383
430
|
* in-memory cache LRU so that a tarball is never held in it.
|
|
@@ -405,7 +452,11 @@ export class RegistryClient {
|
|
|
405
452
|
// disk underneath it -- benign, tarball urls are immutable.)
|
|
406
453
|
if (this.cache.peek(key))
|
|
407
454
|
return undefined;
|
|
408
|
-
//
|
|
455
|
+
// a pin reads only the integrity path: cache.set hardlinks every
|
|
456
|
+
// artifact under its hash, while the key holds whatever was last
|
|
457
|
+
// fetched from that url. nothing here hashes the body, so a miss
|
|
458
|
+
// -- which sends the caller to request(), which verifies -- is
|
|
459
|
+
// the safe answer.
|
|
409
460
|
const paths = new Set();
|
|
410
461
|
try {
|
|
411
462
|
const i = this.cache.integrityPath(integrity);
|
|
@@ -413,7 +464,8 @@ export class RegistryClient {
|
|
|
413
464
|
paths.add(i);
|
|
414
465
|
}
|
|
415
466
|
catch { }
|
|
416
|
-
|
|
467
|
+
if (!integrity)
|
|
468
|
+
paths.add(this.cache.path(key));
|
|
417
469
|
for (const path of paths) {
|
|
418
470
|
let buf;
|
|
419
471
|
try {
|
|
@@ -549,9 +601,21 @@ export class RegistryClient {
|
|
|
549
601
|
// a server-sent integrity header is not evidence: only the caller's
|
|
550
602
|
// expectation, or a body read back from the cache, is trusted. the
|
|
551
603
|
// header is dropped so it can never be stored as the entry's hash.
|
|
604
|
+
//
|
|
605
|
+
// the check has to happen here, before the cache write below, or a
|
|
606
|
+
// body that fails it is still stored under the hash it was supposed
|
|
607
|
+
// to have -- and served from there, unverified, on the next run.
|
|
608
|
+
// `!fromCache` is the whole precondition: only a cached entry can
|
|
609
|
+
// have been rewritten (cache-unzip un-gzips in place), so anything
|
|
610
|
+
// else is the bytes as they came off the wire and hashes as such.
|
|
611
|
+
// this used to also require `isGzip`, which was a proxy for that
|
|
612
|
+
// and silently exempted every artifact that is not gzip -- a
|
|
613
|
+
// `.tar.br` among them.
|
|
552
614
|
if (!trustIntegrity && !result.fromCache) {
|
|
553
615
|
result.deleteHeader('integrity');
|
|
554
|
-
|
|
616
|
+
// 200 only: an error body is not the artifact and was never
|
|
617
|
+
// supposed to hash to it, and it is not what gets cached either.
|
|
618
|
+
if (result.statusCode === 200)
|
|
555
619
|
result.checkIntegrity({ url });
|
|
556
620
|
}
|
|
557
621
|
// same for the digest the server labels an unlabelled artifact with.
|
package/dist/registry-error.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export type ErrorResponse = {
|
|
|
11
11
|
text?: () => string;
|
|
12
12
|
[k: number | string | symbol]: any;
|
|
13
13
|
};
|
|
14
|
+
export declare const statusText: (code: number) => string | undefined;
|
|
14
15
|
/**
|
|
15
16
|
* Format a non-2xx registry response as `"403 Forbidden — <detail>"`,
|
|
16
17
|
* where the detail is whatever the registry actually said.
|
package/dist/registry-error.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { error } from '@vltpkg/error-cause';
|
|
2
2
|
import { asError, isObject } from '@vltpkg/types';
|
|
3
|
-
|
|
3
|
+
// read on use: a static node:http import loads undici, http2, tls at startup
|
|
4
|
+
export const statusText = (code) => process.getBuiltinModule('node:http').STATUS_CODES[code];
|
|
4
5
|
/** longest registry-supplied detail spliced into an error message */
|
|
5
6
|
const MAX_DETAIL_LENGTH = 512;
|
|
6
7
|
const truncate = (s) => s.length <= MAX_DETAIL_LENGTH ?
|
|
@@ -50,7 +51,7 @@ const detailFromJSON = (text) => {
|
|
|
50
51
|
* decoded, and to the raw body text when it is not JSON.
|
|
51
52
|
*/
|
|
52
53
|
export const registryErrorMessage = (response) => {
|
|
53
|
-
const statusMessage =
|
|
54
|
+
const statusMessage = statusText(response.statusCode);
|
|
54
55
|
const status = `${response.statusCode}${statusMessage ? ` ${statusMessage}` : ''}`;
|
|
55
56
|
let text;
|
|
56
57
|
try {
|
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.
|
|
4
|
+
"version": "1.3.1",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/cache": "1.
|
|
17
|
-
"@vltpkg/cache-unzip": "1.
|
|
18
|
-
"@vltpkg/error-cause": "1.
|
|
19
|
-
"@vltpkg/keychain": "1.
|
|
20
|
-
"@vltpkg/output": "1.
|
|
21
|
-
"@vltpkg/types": "1.
|
|
22
|
-
"@vltpkg/url-open": "1.
|
|
23
|
-
"@vltpkg/user-agent": "1.
|
|
24
|
-
"@vltpkg/xdg": "1.
|
|
16
|
+
"@vltpkg/cache": "1.3.1",
|
|
17
|
+
"@vltpkg/cache-unzip": "1.3.1",
|
|
18
|
+
"@vltpkg/error-cause": "1.3.1",
|
|
19
|
+
"@vltpkg/keychain": "1.3.1",
|
|
20
|
+
"@vltpkg/output": "1.3.1",
|
|
21
|
+
"@vltpkg/types": "1.3.1",
|
|
22
|
+
"@vltpkg/url-open": "1.3.1",
|
|
23
|
+
"@vltpkg/user-agent": "1.3.1",
|
|
24
|
+
"@vltpkg/xdg": "1.3.1",
|
|
25
25
|
"cache-control-parser": "^2.0.6",
|
|
26
26
|
"undici": "^7.16.0"
|
|
27
27
|
},
|