@vltpkg/registry-client 1.1.1 → 1.2.0
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 +11 -1
- package/dist/cache-entry.d.ts +28 -2
- package/dist/cache-entry.js +61 -2
- package/dist/index.d.ts +24 -1
- package/dist/index.js +48 -13
- package/dist/otplease.js +8 -0
- package/dist/registry-error.d.ts +8 -0
- package/dist/registry-error.js +62 -7
- package/dist/store-root.d.ts +2 -0
- package/dist/store-root.js +3 -0
- package/package.json +16 -11
package/README.md
CHANGED
|
@@ -59,12 +59,22 @@ So,
|
|
|
59
59
|
- json responses will always be un-zipped, in the response and in the
|
|
60
60
|
cache.
|
|
61
61
|
- artifact responses _may_ be gzipped (and thus, have to be unzipped
|
|
62
|
-
by the unpack operation)
|
|
62
|
+
by the unpack operation). Tarballs exploded into the global store
|
|
63
|
+
stay gzipped; the rest will eventually be cached as unzipped
|
|
63
64
|
tarballs.
|
|
64
65
|
|
|
65
66
|
Thus, the `content-length` response header will _usually_ not match
|
|
66
67
|
the actual byte length of the response body.
|
|
67
68
|
|
|
69
|
+
Tarball entries with an `integrity` header, gzipped or not, are also
|
|
70
|
+
passed to that child with the global store root (the `storeRoot`
|
|
71
|
+
option, default `storeRoot(cache)`, i.e. `<cache>/store/v1`, also at
|
|
72
|
+
`@vltpkg/registry-client/store-root`), so it can explode them there
|
|
73
|
+
when `VLT_STORE_LINKER` enables the store.
|
|
74
|
+
`queueForStore(key, integrity)` queues an already cached tarball (a
|
|
75
|
+
`cachedBody()` hit) the same way: exploded when the store is on, else
|
|
76
|
+
un-gzipped.
|
|
77
|
+
|
|
68
78
|
## Proxies
|
|
69
79
|
|
|
70
80
|
If `http_proxy` or `https_proxy` is set in the environment (either
|
package/dist/cache-entry.d.ts
CHANGED
|
@@ -99,18 +99,35 @@ export declare class CacheEntry {
|
|
|
99
99
|
* This method **will throw** if the integrity values do not match.
|
|
100
100
|
*
|
|
101
101
|
* Note that this will *usually* not be true if the value is coming out of
|
|
102
|
-
* the cache, because
|
|
103
|
-
* _only_ be called for artifacts that come
|
|
102
|
+
* the cache, because cache entries not exploded into the global store are
|
|
103
|
+
* un-gzipped in place. It should _only_ be called for artifacts that come
|
|
104
|
+
* from an actual http response.
|
|
104
105
|
*
|
|
105
106
|
* Returns true if anything was actually verified.
|
|
106
107
|
*/
|
|
107
108
|
checkIntegrity(context?: ErrorCauseOptions): this is CacheEntry & {
|
|
108
109
|
integrity: Integrity;
|
|
109
110
|
};
|
|
111
|
+
/**
|
|
112
|
+
* Check the body against the RFC 9530 digest the server sent with it,
|
|
113
|
+
* for a response that had no expected integrity. A registry that omits
|
|
114
|
+
* `dist.integrity` labels every tarball, so a missing digest fails when
|
|
115
|
+
* `required`. Like {@link checkIntegrity}, only for an actual http
|
|
116
|
+
* response: cached bodies are un-gzipped in place.
|
|
117
|
+
*
|
|
118
|
+
* **Will throw** on a mismatch. Returns the hash of the body.
|
|
119
|
+
*/
|
|
120
|
+
checkDigest(required: boolean, context?: ErrorCauseOptions): Integrity;
|
|
110
121
|
get integrityActual(): Integrity;
|
|
111
122
|
set integrityActual(i: Integrity);
|
|
112
123
|
set integrity(i: Integrity | undefined);
|
|
113
124
|
get integrity(): Integrity | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The sha-512 member of an RFC 9530 `Repr-Digest` (or `Content-Digest`)
|
|
127
|
+
* response header, as an SRI string. A registry that serves packuments
|
|
128
|
+
* without `dist.integrity` labels each tarball this way instead.
|
|
129
|
+
*/
|
|
130
|
+
get digest(): Integrity | undefined;
|
|
114
131
|
/**
|
|
115
132
|
* Give it a key, and it'll return the buffer of that header value
|
|
116
133
|
*/
|
|
@@ -123,6 +140,10 @@ export declare class CacheEntry {
|
|
|
123
140
|
* Set a header to a specific value
|
|
124
141
|
*/
|
|
125
142
|
setHeader(h: string, value: Uint8Array | string): void;
|
|
143
|
+
/**
|
|
144
|
+
* Remove a header, if present
|
|
145
|
+
*/
|
|
146
|
+
deleteHeader(h: string): void;
|
|
126
147
|
/**
|
|
127
148
|
* Return the body of the entry as a Buffer
|
|
128
149
|
*/
|
|
@@ -158,6 +179,11 @@ export declare class CacheEntry {
|
|
|
158
179
|
*/
|
|
159
180
|
static decode(buffer: Uint8Array, options?: CacheEntryOptions): CacheEntry;
|
|
160
181
|
static isGzipEntry(buffer: Uint8Array): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Head-only check for a 200 non-JSON body with an integrity header,
|
|
184
|
+
* i.e. a tarball the global store can be built from.
|
|
185
|
+
*/
|
|
186
|
+
static isTarballEntry(buffer: Uint8Array): boolean;
|
|
161
187
|
/**
|
|
162
188
|
* Encode status + headers (no body) as they appear at the start of
|
|
163
189
|
* an on-disk cache file.
|
package/dist/cache-entry.js
CHANGED
|
@@ -293,8 +293,9 @@ export class CacheEntry {
|
|
|
293
293
|
* This method **will throw** if the integrity values do not match.
|
|
294
294
|
*
|
|
295
295
|
* Note that this will *usually* not be true if the value is coming out of
|
|
296
|
-
* the cache, because
|
|
297
|
-
* _only_ be called for artifacts that come
|
|
296
|
+
* the cache, because cache entries not exploded into the global store are
|
|
297
|
+
* un-gzipped in place. It should _only_ be called for artifacts that come
|
|
298
|
+
* from an actual http response.
|
|
298
299
|
*
|
|
299
300
|
* Returns true if anything was actually verified.
|
|
300
301
|
*/
|
|
@@ -312,6 +313,29 @@ export class CacheEntry {
|
|
|
312
313
|
}
|
|
313
314
|
return true;
|
|
314
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* Check the body against the RFC 9530 digest the server sent with it,
|
|
318
|
+
* for a response that had no expected integrity. A registry that omits
|
|
319
|
+
* `dist.integrity` labels every tarball, so a missing digest fails when
|
|
320
|
+
* `required`. Like {@link checkIntegrity}, only for an actual http
|
|
321
|
+
* response: cached bodies are un-gzipped in place.
|
|
322
|
+
*
|
|
323
|
+
* **Will throw** on a mismatch. Returns the hash of the body.
|
|
324
|
+
*/
|
|
325
|
+
checkDigest(required, context = {}) {
|
|
326
|
+
const computed = this.integrityActual;
|
|
327
|
+
const { digest } = this;
|
|
328
|
+
if (digest ? digest !== computed : required) {
|
|
329
|
+
throw error('Integrity check failure', {
|
|
330
|
+
code: 'EINTEGRITY',
|
|
331
|
+
response: this,
|
|
332
|
+
wanted: digest,
|
|
333
|
+
found: computed,
|
|
334
|
+
...context,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
return computed;
|
|
338
|
+
}
|
|
315
339
|
get integrityActual() {
|
|
316
340
|
if (this.#integrityActual)
|
|
317
341
|
return this.#integrityActual;
|
|
@@ -335,6 +359,17 @@ export class CacheEntry {
|
|
|
335
359
|
get integrity() {
|
|
336
360
|
return this.#integrity;
|
|
337
361
|
}
|
|
362
|
+
/**
|
|
363
|
+
* The sha-512 member of an RFC 9530 `Repr-Digest` (or `Content-Digest`)
|
|
364
|
+
* response header, as an SRI string. A registry that serves packuments
|
|
365
|
+
* without `dist.integrity` labels each tarball this way instead.
|
|
366
|
+
*/
|
|
367
|
+
get digest() {
|
|
368
|
+
const value = this.getHeaderString('repr-digest') ??
|
|
369
|
+
this.getHeaderString('content-digest');
|
|
370
|
+
const m = value && /(?:^|,)\s*sha-512=:([A-Za-z0-9+/]{86}==):/.exec(value);
|
|
371
|
+
return m ? `sha512-${m[1]}` : undefined;
|
|
372
|
+
}
|
|
338
373
|
/**
|
|
339
374
|
* Give it a key, and it'll return the buffer of that header value
|
|
340
375
|
*/
|
|
@@ -356,6 +391,19 @@ export class CacheEntry {
|
|
|
356
391
|
setHeader(h, value) {
|
|
357
392
|
this.#headers = setRawHeader(this.#headers, h, value);
|
|
358
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* Remove a header, if present
|
|
396
|
+
*/
|
|
397
|
+
deleteHeader(h) {
|
|
398
|
+
const key = h.toLowerCase();
|
|
399
|
+
for (let i = 0; i < this.#headers.length; i += 2) {
|
|
400
|
+
const k = this.#headers[i];
|
|
401
|
+
if (k && getDecodedValue(k).toLowerCase() === key) {
|
|
402
|
+
this.#headers.splice(i, 2);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
359
407
|
/**
|
|
360
408
|
* Return the body of the entry as a Buffer
|
|
361
409
|
*/
|
|
@@ -546,6 +594,17 @@ export class CacheEntry {
|
|
|
546
594
|
const gzipBytes = buffer.subarray(headSize, headSize + 2);
|
|
547
595
|
return gzipBytes[0] === 0x1f && gzipBytes[1] === 0x8b;
|
|
548
596
|
}
|
|
597
|
+
/**
|
|
598
|
+
* Head-only check for a 200 non-JSON body with an integrity header,
|
|
599
|
+
* i.e. a tarball the global store can be built from.
|
|
600
|
+
*/
|
|
601
|
+
static isTarballEntry(buffer) {
|
|
602
|
+
const parsed = CacheEntry.#parseHead(buffer);
|
|
603
|
+
if (parsed?.statusCode !== 200 || !parsed.integrity)
|
|
604
|
+
return false;
|
|
605
|
+
const ct = getRawHeader(parsed.headers, 'content-type');
|
|
606
|
+
return !ct || !/\bjson\b/.test(getDecodedValue(ct));
|
|
607
|
+
}
|
|
549
608
|
/**
|
|
550
609
|
* Encode status + headers (no body) as they appear at the start of
|
|
551
610
|
* an on-disk cache file.
|
package/dist/index.d.ts
CHANGED
|
@@ -11,15 +11,22 @@ import { assertOk, registryErrorMessage, requestError } from './registry-error.t
|
|
|
11
11
|
import type { ErrorResponse } from './registry-error.ts';
|
|
12
12
|
import type { TokenResponse } from './token-response.ts';
|
|
13
13
|
import type { WebAuthChallenge } from './web-auth-challenge.ts';
|
|
14
|
+
import { storeRoot } from './store-root.ts';
|
|
14
15
|
import { oidc } from './oidc.ts';
|
|
15
16
|
import type { OidcOptions } from './oidc.ts';
|
|
16
|
-
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, type ErrorResponse, type JSONObj, type OidcOptions, type Token, type TokenResponse, type WebAuthChallenge, };
|
|
17
|
+
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, type ErrorResponse, type JSONObj, type OidcOptions, type Token, type TokenResponse, type WebAuthChallenge, };
|
|
17
18
|
/** A response body read straight out of the cache file. */
|
|
18
19
|
export type CachedBody = {
|
|
19
20
|
/** the cache file it came from */
|
|
20
21
|
path: string;
|
|
22
|
+
/** the cache key of the request */
|
|
23
|
+
key: string;
|
|
21
24
|
/** the body, a view into the file's bytes */
|
|
22
25
|
body: Buffer;
|
|
26
|
+
/** the hash the entry was stored under, if it has one */
|
|
27
|
+
integrity?: Integrity;
|
|
28
|
+
/** the body on disk is gzipped (`body` may be un-gzipped) */
|
|
29
|
+
gzip: boolean;
|
|
23
30
|
};
|
|
24
31
|
export type CacheableMethod = 'GET' | 'HEAD';
|
|
25
32
|
export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
|
|
@@ -31,6 +38,8 @@ export type RegistryClientOptions = {
|
|
|
31
38
|
* Defaults to the XDG cache folder for `vlt/registry-client`
|
|
32
39
|
*/
|
|
33
40
|
cache?: string;
|
|
41
|
+
/** Global store root. Defaults to {@link storeRoot} of `cache`. */
|
|
42
|
+
storeRoot?: string;
|
|
34
43
|
/**
|
|
35
44
|
* Number of retries to perform when encountering network errors or
|
|
36
45
|
* likely-transient errors from git hosts.
|
|
@@ -86,6 +95,13 @@ export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'meth
|
|
|
86
95
|
* also serves the tarball itself.
|
|
87
96
|
*/
|
|
88
97
|
trustIntegrity?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* With no `integrity` to expect, check the body against the RFC 9530
|
|
100
|
+
* `Repr-Digest` the server sent with it. `'required'` also rejects a
|
|
101
|
+
* response that carries no digest. Runs before the response is cached,
|
|
102
|
+
* so a body that fails is never served from the cache later.
|
|
103
|
+
*/
|
|
104
|
+
verifyDigest?: boolean | 'required';
|
|
89
105
|
/**
|
|
90
106
|
* Follow up to 10 redirections by default. Set this to 0 to just return
|
|
91
107
|
* the 3xx response. If the max redirections are expired, and we still get
|
|
@@ -187,5 +203,12 @@ export declare class RegistryClient {
|
|
|
187
203
|
integrity?: Integrity;
|
|
188
204
|
method?: CacheableMethod;
|
|
189
205
|
}): CachedBody | undefined;
|
|
206
|
+
/**
|
|
207
|
+
* Queue the cached tarball at `key` (a {@link CachedBody} key) for the
|
|
208
|
+
* background child: exploded into the global store when that is on,
|
|
209
|
+
* else un-gzipped. `integrity` finds it when only the integrity path
|
|
210
|
+
* holds it.
|
|
211
|
+
*/
|
|
212
|
+
queueForStore(key: string, integrity?: Integrity): void;
|
|
190
213
|
request(url: URL | string, options?: RegistryClientRequestOptions): Promise<CacheEntry>;
|
|
191
214
|
}
|
package/dist/index.js
CHANGED
|
@@ -27,10 +27,11 @@ import { setCacheHeaders } from "./set-cache-headers.js";
|
|
|
27
27
|
import { getTokenResponse } from "./token-response.js";
|
|
28
28
|
import { getWebAuthChallenge } from "./web-auth-challenge.js";
|
|
29
29
|
import { collectHeaders, readBody } from "./response.js";
|
|
30
|
+
import { storeRoot } from "./store-root.js";
|
|
30
31
|
import { oidc } from "./oidc.js";
|
|
31
32
|
// eslint-disable-next-line no-console
|
|
32
33
|
const log = (msg) => console.error(msg);
|
|
33
|
-
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, };
|
|
34
|
+
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
|
|
34
35
|
export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
|
|
35
36
|
export const cacheKey = (method, url) => `${method !== 'GET' ? method + ' ' : ''}${url}`;
|
|
36
37
|
/**
|
|
@@ -91,12 +92,15 @@ export class RegistryClient {
|
|
|
91
92
|
this.identity = identity;
|
|
92
93
|
this.staleWhileRevalidateFactor = staleWhileRevalidateFactor;
|
|
93
94
|
const path = resolve(cache, 'registry-client');
|
|
95
|
+
const store = options.storeRoot ?? storeRoot(cache);
|
|
94
96
|
this.cache = new Cache({
|
|
95
97
|
path,
|
|
98
|
+
store,
|
|
96
99
|
maxSize: parseCacheMaxSize(process.env.VLT_CACHE_MAX_SIZE),
|
|
97
100
|
onDiskWrite(_path, key, data) {
|
|
98
|
-
if (CacheEntry.isGzipEntry(data)
|
|
99
|
-
|
|
101
|
+
if (CacheEntry.isGzipEntry(data) ||
|
|
102
|
+
CacheEntry.isTarballEntry(data)) {
|
|
103
|
+
cacheUnzipRegister(path, key, store);
|
|
100
104
|
}
|
|
101
105
|
},
|
|
102
106
|
});
|
|
@@ -428,17 +432,32 @@ export class RegistryClient {
|
|
|
428
432
|
entry.isJSON) {
|
|
429
433
|
continue;
|
|
430
434
|
}
|
|
431
|
-
return {
|
|
435
|
+
return {
|
|
436
|
+
path,
|
|
437
|
+
key,
|
|
438
|
+
body: entry.buffer(),
|
|
439
|
+
integrity: entry.integrity,
|
|
440
|
+
gzip: CacheEntry.isGzipEntry(buf),
|
|
441
|
+
};
|
|
432
442
|
}
|
|
433
443
|
/* c8 ignore next */
|
|
434
444
|
}
|
|
435
445
|
catch { }
|
|
436
446
|
return undefined;
|
|
437
447
|
}
|
|
448
|
+
/**
|
|
449
|
+
* Queue the cached tarball at `key` (a {@link CachedBody} key) for the
|
|
450
|
+
* background child: exploded into the global store when that is on,
|
|
451
|
+
* else un-gzipped. `integrity` finds it when only the integrity path
|
|
452
|
+
* holds it.
|
|
453
|
+
*/
|
|
454
|
+
queueForStore(key, integrity) {
|
|
455
|
+
cacheUnzipRegister(this.cache.path(), key, this.cache.store, integrity);
|
|
456
|
+
}
|
|
438
457
|
async request(url, options = {}) {
|
|
439
458
|
const u = typeof url === 'string' ? new URL(url) : url;
|
|
440
459
|
const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, forceRevalidate = false, } = options;
|
|
441
|
-
|
|
460
|
+
const { trustIntegrity, verifyDigest } = options;
|
|
442
461
|
const m = isCacheableMethod(method) ? method : undefined;
|
|
443
462
|
const { useCache = !!m } = options;
|
|
444
463
|
signal?.throwIfAborted();
|
|
@@ -527,11 +546,22 @@ export class RegistryClient {
|
|
|
527
546
|
statusCode: response.statusCode,
|
|
528
547
|
durationMs: Date.now() - requestStart,
|
|
529
548
|
});
|
|
530
|
-
|
|
531
|
-
|
|
549
|
+
// a server-sent integrity header is not evidence: only the caller's
|
|
550
|
+
// expectation, or a body read back from the cache, is trusted. the
|
|
551
|
+
// header is dropped so it can never be stored as the entry's hash.
|
|
552
|
+
if (!trustIntegrity && !result.fromCache) {
|
|
553
|
+
result.deleteHeader('integrity');
|
|
554
|
+
if (result.isGzip)
|
|
555
|
+
result.checkIntegrity({ url });
|
|
532
556
|
}
|
|
533
|
-
|
|
534
|
-
|
|
557
|
+
// same for the digest the server labels an unlabelled artifact with.
|
|
558
|
+
// before the cache write below, or a rejected body would be served
|
|
559
|
+
// from the cache on the next run, unverified.
|
|
560
|
+
if (verifyDigest &&
|
|
561
|
+
!integrity &&
|
|
562
|
+
!result.fromCache &&
|
|
563
|
+
result.statusCode === 200) {
|
|
564
|
+
result.checkDigest(verifyDigest === 'required', { url });
|
|
535
565
|
}
|
|
536
566
|
// a forced revalidation must never replace a cached entry with an
|
|
537
567
|
// error response -- the flat 200-only rule revalidate-entry.ts has.
|
|
@@ -543,11 +573,16 @@ export class RegistryClient {
|
|
|
543
573
|
// still gets written back.
|
|
544
574
|
const clobbersCachedEntry = forceRevalidate && !!entry && result.statusCode !== 200;
|
|
545
575
|
if (useCache && !clobbersCachedEntry) {
|
|
546
|
-
//
|
|
576
|
+
// content-address an artifact the caller had no expected hash for,
|
|
577
|
+
// so a later lookup by hash still finds it; packuments are not
|
|
578
|
+
// worth hashing. integrityActual also records the hash in the
|
|
579
|
+
// entry's headers, so it runs before encode().
|
|
580
|
+
const integrity = result.integrity ??
|
|
581
|
+
(result.statusCode === 200 && !result.isJSON ?
|
|
582
|
+
result.integrityActual
|
|
583
|
+
: undefined);
|
|
547
584
|
const buffer = result.encode();
|
|
548
|
-
this.cache.set(key, Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength), {
|
|
549
|
-
integrity: result.integrity,
|
|
550
|
-
});
|
|
585
|
+
this.cache.set(key, Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength), { integrity });
|
|
551
586
|
}
|
|
552
587
|
return result;
|
|
553
588
|
}
|
package/dist/otplease.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { tokenRefusal } from "./registry-error.js";
|
|
2
3
|
import { getWebAuthChallenge } from "./web-auth-challenge.js";
|
|
3
4
|
import { urlOpen } from '@vltpkg/url-open';
|
|
4
5
|
import { createInterface } from 'node:readline/promises';
|
|
@@ -71,6 +72,13 @@ export const otplease = async (client, options, response) => {
|
|
|
71
72
|
});
|
|
72
73
|
}
|
|
73
74
|
if (wwwAuth.has('bearer')) {
|
|
75
|
+
// A token the registry says is dead explains itself in the body, so hand
|
|
76
|
+
// it back to be rendered like any other 401. Every other bearer challenge
|
|
77
|
+
// -- npm's own registry sends one with no body -- keeps the generic line.
|
|
78
|
+
const text = await responseBodyText(response).catch(() => '');
|
|
79
|
+
if (tokenRefusal({ statusCode: 401, text: () => text })) {
|
|
80
|
+
return { bodyConsumed: text };
|
|
81
|
+
}
|
|
74
82
|
throw error('Missing or invalid authentication token. Run `vlt login` or `vlt token add` to authenticate.', {
|
|
75
83
|
code: 'ENEEDAUTH',
|
|
76
84
|
status: response.statusCode,
|
package/dist/registry-error.d.ts
CHANGED
|
@@ -19,6 +19,14 @@ export type ErrorResponse = {
|
|
|
19
19
|
* decoded, and to the raw body text when it is not JSON.
|
|
20
20
|
*/
|
|
21
21
|
export declare const registryErrorMessage: (response: ErrorResponse) => string;
|
|
22
|
+
/** How a 401 says the token it refused died, if it does. */
|
|
23
|
+
export declare const tokenRefusal: (response: unknown) => "expired" | "revoked" | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* What to do about a 401 that says the token expired or was revoked, or
|
|
26
|
+
* `undefined` for every other response. The account is the first path segment
|
|
27
|
+
* of any request to a vlt registry, and one token covers every registry on it.
|
|
28
|
+
*/
|
|
29
|
+
export declare const tokenRefusalAdvice: (response: unknown, url?: URL | string) => string | undefined;
|
|
22
30
|
export type AssertOkOptions = {
|
|
23
31
|
/** what we were trying to do, eg `'Failed to list dist-tags'` */
|
|
24
32
|
message: string;
|
package/dist/registry-error.js
CHANGED
|
@@ -15,10 +15,7 @@ const stripTags = (s) => s
|
|
|
15
15
|
.replace(/<[^>]*>/g, ' ')
|
|
16
16
|
.replace(/\s+/g, ' ')
|
|
17
17
|
.trim();
|
|
18
|
-
|
|
19
|
-
// third-party registries write `{"message":"..."}`; a few nest it under
|
|
20
|
-
// `error.message`.
|
|
21
|
-
const detailFromJSON = (text) => {
|
|
18
|
+
const parseBody = (text) => {
|
|
22
19
|
let body;
|
|
23
20
|
try {
|
|
24
21
|
body = JSON.parse(text);
|
|
@@ -27,9 +24,17 @@ const detailFromJSON = (text) => {
|
|
|
27
24
|
// A non-JSON body is itself the most useful detail.
|
|
28
25
|
return undefined;
|
|
29
26
|
}
|
|
30
|
-
|
|
27
|
+
return !body || typeof body !== 'object' ?
|
|
28
|
+
undefined
|
|
29
|
+
: body;
|
|
30
|
+
};
|
|
31
|
+
// npm writes `{"error":"..."}`; the `-/npm/v1/*` endpoints and several
|
|
32
|
+
// third-party registries write `{"message":"..."}`; a few nest it under
|
|
33
|
+
// `error.message`.
|
|
34
|
+
const detailFromJSON = (text) => {
|
|
35
|
+
const rec = parseBody(text);
|
|
36
|
+
if (!rec)
|
|
31
37
|
return undefined;
|
|
32
|
-
const rec = body;
|
|
33
38
|
const nested = rec.error;
|
|
34
39
|
for (const v of [rec.error, rec.message, nested?.message]) {
|
|
35
40
|
if (typeof v === 'string' && v.trim())
|
|
@@ -60,6 +65,54 @@ export const registryErrorMessage = (response) => {
|
|
|
60
65
|
const detail = detailFromJSON(text) ?? (isHTML(text) ? stripTags(text) : text);
|
|
61
66
|
return detail ? `${status} — ${truncate(detail)}` : status;
|
|
62
67
|
};
|
|
68
|
+
/** What a vlt registry calls the two 401s a client can resolve on its own. */
|
|
69
|
+
const refusals = {
|
|
70
|
+
TokenExpiredError: 'expired',
|
|
71
|
+
TokenRevokedError: 'revoked',
|
|
72
|
+
};
|
|
73
|
+
/** vlt.io account and organization slugs, as the registry mints them. */
|
|
74
|
+
const accountSlug = /^(?=.{3,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
|
|
75
|
+
/** How a 401 says the token it refused died, if it does. */
|
|
76
|
+
export const tokenRefusal = (response) => {
|
|
77
|
+
// a CacheEntry, a node:http message, or a bare object off an error's cause
|
|
78
|
+
// bag; `isObject` would refuse the class instances
|
|
79
|
+
const res = response;
|
|
80
|
+
if (typeof res !== 'object' || res?.statusCode !== 401)
|
|
81
|
+
return undefined;
|
|
82
|
+
let text = '';
|
|
83
|
+
try {
|
|
84
|
+
if (typeof res.text === 'function')
|
|
85
|
+
text = res.text();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// an undecodable body (bad gzip) says nothing either way
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const code = parseBody(text)?.code;
|
|
92
|
+
return typeof code === 'string' ? refusals[code] : undefined;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* What to do about a 401 that says the token expired or was revoked, or
|
|
96
|
+
* `undefined` for every other response. The account is the first path segment
|
|
97
|
+
* of any request to a vlt registry, and one token covers every registry on it.
|
|
98
|
+
*/
|
|
99
|
+
export const tokenRefusalAdvice = (response, url) => {
|
|
100
|
+
const gone = tokenRefusal(response);
|
|
101
|
+
if (!gone || url === undefined)
|
|
102
|
+
return undefined;
|
|
103
|
+
let account;
|
|
104
|
+
try {
|
|
105
|
+
account = new URL(String(url)).pathname.split('/')[1];
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
if (!account || !accountSlug.test(account))
|
|
111
|
+
return undefined;
|
|
112
|
+
return (`Your token for the "${account}" account ` +
|
|
113
|
+
`${gone === 'expired' ? 'has expired' : 'was revoked'}. Run ` +
|
|
114
|
+
`\`vlt setup ${account}\` to log in again.`);
|
|
115
|
+
};
|
|
63
116
|
/**
|
|
64
117
|
* Throw a well-formed, user-facing error when a registry response is not
|
|
65
118
|
* a 2xx.
|
|
@@ -76,7 +129,9 @@ export const registryErrorMessage = (response) => {
|
|
|
76
129
|
const registryError = (response, { message, url, method = 'GET', advice }, from, cause) => {
|
|
77
130
|
const { statusCode } = response;
|
|
78
131
|
const denied = statusCode === 401 || statusCode === 403;
|
|
79
|
-
|
|
132
|
+
// A diagnosis off the response stands in for the caller's generic 401 line.
|
|
133
|
+
const refused = tokenRefusalAdvice(response, url);
|
|
134
|
+
const extra = refused ?? advice?.(statusCode);
|
|
80
135
|
const tips = (Array.isArray(extra) ? extra : [extra]).filter((t) => !!t);
|
|
81
136
|
const warn = tips.map(t => `\n⚠️ ${t}`).join('');
|
|
82
137
|
return error(`${message}: ${registryErrorMessage(response)}${warn}`, {
|
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.2.0",
|
|
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.2.0",
|
|
17
|
+
"@vltpkg/cache-unzip": "1.2.0",
|
|
18
|
+
"@vltpkg/error-cause": "1.2.0",
|
|
19
|
+
"@vltpkg/keychain": "1.2.0",
|
|
20
|
+
"@vltpkg/output": "1.2.0",
|
|
21
|
+
"@vltpkg/types": "1.2.0",
|
|
22
|
+
"@vltpkg/url-open": "1.2.0",
|
|
23
|
+
"@vltpkg/user-agent": "1.2.0",
|
|
24
|
+
"@vltpkg/xdg": "1.2.0",
|
|
25
25
|
"cache-control-parser": "^2.0.6",
|
|
26
26
|
"undici": "^7.16.0"
|
|
27
27
|
},
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@types/node": "^22.19.2",
|
|
31
31
|
"eslint": "^9.39.1",
|
|
32
32
|
"prettier": "^3.7.4",
|
|
33
|
-
"tap": "^21.
|
|
33
|
+
"tap": "^21.8.0",
|
|
34
34
|
"typedoc": "~0.27.9",
|
|
35
35
|
"typescript": "5.7.3",
|
|
36
36
|
"typescript-eslint": "^8.49.0"
|
|
@@ -72,6 +72,11 @@
|
|
|
72
72
|
"import": {
|
|
73
73
|
"default": "./dist/registry-error.js"
|
|
74
74
|
}
|
|
75
|
+
},
|
|
76
|
+
"./store-root": {
|
|
77
|
+
"import": {
|
|
78
|
+
"default": "./dist/store-root.js"
|
|
79
|
+
}
|
|
75
80
|
}
|
|
76
81
|
},
|
|
77
82
|
"files": [
|