@vltpkg/registry-client 1.3.6 → 1.3.8
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/auth.d.ts +23 -7
- package/dist/auth.js +32 -8
- package/dist/index.d.ts +11 -4
- package/dist/index.js +38 -7
- package/package.json +10 -10
package/dist/auth.d.ts
CHANGED
|
@@ -29,21 +29,37 @@ export declare const setRuntimeToken: (registry: string, token: Token) => void;
|
|
|
29
29
|
export declare const clearRuntimeTokens: () => void;
|
|
30
30
|
export declare const getKC: (identity: string) => Keychain<Token>;
|
|
31
31
|
export declare const isToken: (t: any) => t is Token;
|
|
32
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Delete the token for `registry`. With `token`, also delete every other
|
|
34
|
+
* key holding it: one login can save a token under many registries.
|
|
35
|
+
*/
|
|
36
|
+
export declare const deleteToken: (registry: string, identity: string, token?: Token) => Promise<void>;
|
|
33
37
|
export declare const setToken: (registry: string, token: Token, identity: string) => Promise<void>;
|
|
34
38
|
export declare const getToken: (registry: string, identity: string) => Promise<Token | undefined>;
|
|
39
|
+
/** Configured registry URLs, by config option name. */
|
|
40
|
+
export type RegistryURLs = {
|
|
41
|
+
registry?: string;
|
|
42
|
+
registries?: Record<string, string>;
|
|
43
|
+
'scoped-registries'?: Record<string, string>;
|
|
44
|
+
'jsr-registries'?: Record<string, string>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Normalized, deduped keys of the configured registries.
|
|
48
|
+
* Unparseable URLs are skipped.
|
|
49
|
+
*/
|
|
50
|
+
export declare const registryKeys: (o: RegistryURLs) => string[];
|
|
35
51
|
/**
|
|
36
52
|
* Find the best matching token for a request URL by performing a
|
|
37
53
|
* longest-prefix match against all known registry keys (runtime
|
|
38
|
-
* tokens,
|
|
39
|
-
*
|
|
54
|
+
* tokens, `VLT_REGISTRY`, keychain entries, and `keys`: normalized
|
|
55
|
+
* configured registry keys, e.g. from {@link registryKeys}). A key
|
|
56
|
+
* that resolves no token is skipped.
|
|
40
57
|
*
|
|
41
58
|
* `VLT_TOKEN_<key>` env vars are only read for a known key: their
|
|
42
59
|
* names are lossy (`.`, `-`, `/` all become `_`), so probing them by
|
|
43
60
|
* request URL would hand the token to lookalike hosts.
|
|
44
61
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* to construct it.
|
|
62
|
+
* Used by `RegistryClient.request()`, which has the request URL, not
|
|
63
|
+
* the registry URL it was built from.
|
|
48
64
|
*/
|
|
49
|
-
export declare const getTokenByURL: (requestUrl: string, identity: string) => Promise<Token | undefined>;
|
|
65
|
+
export declare const getTokenByURL: (requestUrl: string, identity: string, keys?: readonly string[]) => Promise<Token | undefined>;
|
package/dist/auth.js
CHANGED
|
@@ -45,10 +45,20 @@ export const getKC = (identity) => {
|
|
|
45
45
|
};
|
|
46
46
|
export const isToken = (t) => typeof t === 'string' &&
|
|
47
47
|
(t.startsWith('Bearer ') || t.startsWith('Basic '));
|
|
48
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Delete the token for `registry`. With `token`, also delete every other
|
|
50
|
+
* key holding it: one login can save a token under many registries.
|
|
51
|
+
*/
|
|
52
|
+
export const deleteToken = async (registry, identity, token) => {
|
|
49
53
|
const kc = getKC(identity);
|
|
50
54
|
await kc.load();
|
|
51
55
|
kc.delete(normalizeRegistryKey(registry));
|
|
56
|
+
if (token) {
|
|
57
|
+
for (const key of kc.keysSync()) {
|
|
58
|
+
if (kc.getSync(key) === token)
|
|
59
|
+
kc.delete(key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
52
62
|
await kc.save();
|
|
53
63
|
};
|
|
54
64
|
export const setToken = async (registry, token, identity) => {
|
|
@@ -75,24 +85,38 @@ export const getToken = async (registry, identity) => {
|
|
|
75
85
|
return `Bearer ${tok}`;
|
|
76
86
|
return kc.get(key);
|
|
77
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* Normalized, deduped keys of the configured registries.
|
|
90
|
+
* Unparseable URLs are skipped.
|
|
91
|
+
*/
|
|
92
|
+
export const registryKeys = (o) => [
|
|
93
|
+
...new Set([
|
|
94
|
+
o.registry,
|
|
95
|
+
...Object.values(o.registries ?? {}),
|
|
96
|
+
...Object.values(o['scoped-registries'] ?? {}),
|
|
97
|
+
...Object.values(o['jsr-registries'] ?? {}),
|
|
98
|
+
]
|
|
99
|
+
.filter((u) => !!u && URL.canParse(u))
|
|
100
|
+
.map(normalizeRegistryKey)),
|
|
101
|
+
];
|
|
78
102
|
/**
|
|
79
103
|
* Find the best matching token for a request URL by performing a
|
|
80
104
|
* longest-prefix match against all known registry keys (runtime
|
|
81
|
-
* tokens,
|
|
82
|
-
*
|
|
105
|
+
* tokens, `VLT_REGISTRY`, keychain entries, and `keys`: normalized
|
|
106
|
+
* configured registry keys, e.g. from {@link registryKeys}). A key
|
|
107
|
+
* that resolves no token is skipped.
|
|
83
108
|
*
|
|
84
109
|
* `VLT_TOKEN_<key>` env vars are only read for a known key: their
|
|
85
110
|
* names are lossy (`.`, `-`, `/` all become `_`), so probing them by
|
|
86
111
|
* request URL would hand the token to lookalike hosts.
|
|
87
112
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* to construct it.
|
|
113
|
+
* Used by `RegistryClient.request()`, which has the request URL, not
|
|
114
|
+
* the registry URL it was built from.
|
|
91
115
|
*/
|
|
92
|
-
export const getTokenByURL = async (requestUrl, identity) => {
|
|
116
|
+
export const getTokenByURL = async (requestUrl, identity, keys = []) => {
|
|
93
117
|
const normalized = normalizeRegistryKey(requestUrl);
|
|
94
118
|
// Collect all known registry keys.
|
|
95
|
-
const candidates = [...runtimeTokens.keys()];
|
|
119
|
+
const candidates = [...runtimeTokens.keys(), ...keys];
|
|
96
120
|
const envReg = process.env.VLT_REGISTRY;
|
|
97
121
|
if (envReg) {
|
|
98
122
|
candidates.push(normalizeRegistryKey(envReg));
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ import type { Readable } from 'node:stream';
|
|
|
4
4
|
import type { Dispatcher } from 'undici';
|
|
5
5
|
import { RetryAgent } from 'undici';
|
|
6
6
|
import { userAgent } from '@vltpkg/user-agent';
|
|
7
|
-
import type { Token } from './auth.ts';
|
|
8
|
-
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, runtimeTokens, setRuntimeToken, setToken } from './auth.ts';
|
|
7
|
+
import type { RegistryURLs, Token } from './auth.ts';
|
|
8
|
+
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, registryKeys, runtimeTokens, setRuntimeToken, setToken } from './auth.ts';
|
|
9
9
|
import type { JSONObj } from './cache-entry.ts';
|
|
10
10
|
import { CacheEntry } from './cache-entry.ts';
|
|
11
11
|
import { assertOk, registryErrorMessage, requestError } from './registry-error.ts';
|
|
@@ -15,7 +15,7 @@ import type { WebAuthChallenge } from './web-auth-challenge.ts';
|
|
|
15
15
|
import { storeRoot } from './store-root.ts';
|
|
16
16
|
import { oidc } from './oidc.ts';
|
|
17
17
|
import type { OidcOptions } from './oidc.ts';
|
|
18
|
-
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, };
|
|
18
|
+
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, registryKeys, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, type ErrorResponse, type JSONObj, type OidcOptions, type RegistryURLs, type Token, type TokenResponse, type WebAuthChallenge, };
|
|
19
19
|
/** A response body read straight out of the cache file. */
|
|
20
20
|
export type CachedBody = {
|
|
21
21
|
/** the cache file it came from */
|
|
@@ -32,7 +32,7 @@ export type CachedBody = {
|
|
|
32
32
|
export type CacheableMethod = 'GET' | 'HEAD';
|
|
33
33
|
export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
|
|
34
34
|
export declare const cacheKey: (method: string, url: URL | string) => string;
|
|
35
|
-
export type RegistryClientOptions = {
|
|
35
|
+
export type RegistryClientOptions = RegistryURLs & {
|
|
36
36
|
/**
|
|
37
37
|
* Path on disk where the cache should be stored
|
|
38
38
|
*
|
|
@@ -167,6 +167,8 @@ export declare class RegistryClient {
|
|
|
167
167
|
agent: RetryAgent;
|
|
168
168
|
cache: Cache;
|
|
169
169
|
identity: string;
|
|
170
|
+
/** configured registry keys; `VLT_TOKEN_<key>` applies under each */
|
|
171
|
+
readonly registryKeys: readonly string[];
|
|
170
172
|
staleWhileRevalidateFactor: number;
|
|
171
173
|
constructor(options: RegistryClientOptions);
|
|
172
174
|
/**
|
|
@@ -256,5 +258,10 @@ export declare class RegistryClient {
|
|
|
256
258
|
statusCode: number;
|
|
257
259
|
body: Readable;
|
|
258
260
|
}>;
|
|
261
|
+
/**
|
|
262
|
+
* Settle in-flight requests and their cache writes. Await before
|
|
263
|
+
* removing the cache dir.
|
|
264
|
+
*/
|
|
265
|
+
drain(): Promise<void>;
|
|
259
266
|
request(url: URL | string, options?: RegistryClientRequestOptions): Promise<CacheEntry>;
|
|
260
267
|
}
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { createGunzip } from 'node:zlib';
|
|
|
15
15
|
import { RetryAgent } from 'undici';
|
|
16
16
|
import { userAgent } from '@vltpkg/user-agent';
|
|
17
17
|
import { addHeader } from "./add-header.js";
|
|
18
|
-
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, runtimeTokens, setRuntimeToken, setToken, } from "./auth.js";
|
|
18
|
+
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, registryKeys, runtimeTokens, setRuntimeToken, setToken, } from "./auth.js";
|
|
19
19
|
import { CacheEntry } from "./cache-entry.js";
|
|
20
20
|
import { register } from "./cache-revalidate.js";
|
|
21
21
|
import { getHeader } from "./get-header.js";
|
|
@@ -32,7 +32,7 @@ import { storeRoot } from "./store-root.js";
|
|
|
32
32
|
import { oidc } from "./oidc.js";
|
|
33
33
|
// eslint-disable-next-line no-console
|
|
34
34
|
const log = (msg) => console.error(msg);
|
|
35
|
-
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
|
|
35
|
+
export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, registryKeys, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
|
|
36
36
|
export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
|
|
37
37
|
// A head is about a kilobyte. This limit exists only so that a corrupt
|
|
38
38
|
// length cannot make us allocate something enormous.
|
|
@@ -88,13 +88,18 @@ export class RegistryClient {
|
|
|
88
88
|
agent;
|
|
89
89
|
cache;
|
|
90
90
|
identity;
|
|
91
|
+
/** configured registry keys; `VLT_TOKEN_<key>` applies under each */
|
|
92
|
+
registryKeys;
|
|
91
93
|
staleWhileRevalidateFactor;
|
|
92
94
|
#session = randomUUID();
|
|
93
95
|
#decoded = new WeakMap();
|
|
96
|
+
// request() calls not settled yet, awaited by drain()
|
|
97
|
+
#inFlight = new Set();
|
|
94
98
|
constructor(options) {
|
|
95
99
|
const { cache = xdg.cache(), 'fetch-retry-factor': timeoutFactor = 2, 'fetch-retry-mintimeout': minTimeout = 0, 'fetch-retry-maxtimeout': maxTimeout = 30_000, 'fetch-retries': maxRetries = 3, identity = '', 'stale-while-revalidate-factor': staleWhileRevalidateFactor = 576, // 48h for a 5min cache
|
|
96
100
|
} = options;
|
|
97
101
|
this.identity = identity;
|
|
102
|
+
this.registryKeys = registryKeys(options);
|
|
98
103
|
this.staleWhileRevalidateFactor = staleWhileRevalidateFactor;
|
|
99
104
|
const path = resolve(cache, 'registry-client');
|
|
100
105
|
const store = options.storeRoot ?? storeRoot(cache);
|
|
@@ -240,7 +245,7 @@ export class RegistryClient {
|
|
|
240
245
|
}
|
|
241
246
|
}
|
|
242
247
|
finally {
|
|
243
|
-
await deleteToken(registry, this.identity);
|
|
248
|
+
await deleteToken(registry, this.identity, tok);
|
|
244
249
|
}
|
|
245
250
|
}
|
|
246
251
|
/**
|
|
@@ -529,7 +534,7 @@ export class RegistryClient {
|
|
|
529
534
|
origin: u.origin,
|
|
530
535
|
headers: addHeader(addHeader(addHeader(options.headers, 'accept-encoding', 'gzip;q=1.0, identity;q=0.5'), 'user-agent', userAgent), 'npm-session', this.#session),
|
|
531
536
|
};
|
|
532
|
-
o.headers = addHeader(o.headers, 'authorization', await getTokenByURL(String(u), this.identity));
|
|
537
|
+
o.headers = addHeader(o.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys));
|
|
533
538
|
logRequest(url, 'start', { method: o.method });
|
|
534
539
|
const requestStart = Date.now();
|
|
535
540
|
const response = await this.agent.request(o);
|
|
@@ -550,7 +555,33 @@ export class RegistryClient {
|
|
|
550
555
|
}
|
|
551
556
|
return { statusCode: response.statusCode, body };
|
|
552
557
|
}
|
|
558
|
+
/**
|
|
559
|
+
* Settle in-flight requests and their cache writes. Await before
|
|
560
|
+
* removing the cache dir.
|
|
561
|
+
*/
|
|
562
|
+
async drain() {
|
|
563
|
+
do {
|
|
564
|
+
await Promise.all(this.#inFlight);
|
|
565
|
+
await this.cache.promise();
|
|
566
|
+
} while (this.#inFlight.size);
|
|
567
|
+
}
|
|
553
568
|
async request(url, options = {}) {
|
|
569
|
+
// tracked apart from the #request() promise: a second reaction on
|
|
570
|
+
// that one would make V8 drop the caller's async frames from the
|
|
571
|
+
// stack of any error #request() throws. redirect and otp hops call
|
|
572
|
+
// #request() directly, this call already tracks them
|
|
573
|
+
let settle;
|
|
574
|
+
const tracked = new Promise(res => (settle = res));
|
|
575
|
+
this.#inFlight.add(tracked);
|
|
576
|
+
try {
|
|
577
|
+
return await this.#request(url, options);
|
|
578
|
+
}
|
|
579
|
+
finally {
|
|
580
|
+
this.#inFlight.delete(tracked);
|
|
581
|
+
settle();
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
async #request(url, options) {
|
|
554
585
|
const u = typeof url === 'string' ? new URL(url) : url;
|
|
555
586
|
const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, forceRevalidate = false, } = options;
|
|
556
587
|
const { trustIntegrity, verifyDigest } = options;
|
|
@@ -612,7 +643,7 @@ export class RegistryClient {
|
|
|
612
643
|
// redirect() strips it when the origin changes.
|
|
613
644
|
if (!redirected || !getHeader(options.headers, 'authorization')) {
|
|
614
645
|
// will remove if we don't have a token.
|
|
615
|
-
options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), this.identity));
|
|
646
|
+
options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys));
|
|
616
647
|
}
|
|
617
648
|
let response = null;
|
|
618
649
|
try {
|
|
@@ -717,7 +748,7 @@ export class RegistryClient {
|
|
|
717
748
|
if (response.statusCode === 401) {
|
|
718
749
|
const otpResult = await otplease(this, options, response);
|
|
719
750
|
if (otpResult && 'retry' in otpResult) {
|
|
720
|
-
return await this
|
|
751
|
+
return await this.#request(url, otpResult.retry);
|
|
721
752
|
}
|
|
722
753
|
if (otpResult && 'bodyConsumed' in otpResult) {
|
|
723
754
|
consumedBody = otpResult.bodyConsumed;
|
|
@@ -751,7 +782,7 @@ export class RegistryClient {
|
|
|
751
782
|
response.body.resume();
|
|
752
783
|
const [nextURL, nextOptions] = redirect(options, result, url);
|
|
753
784
|
if (nextOptions && nextURL) {
|
|
754
|
-
return await this
|
|
785
|
+
return await this.#request(nextURL, nextOptions);
|
|
755
786
|
}
|
|
756
787
|
return result;
|
|
757
788
|
}
|
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.3.
|
|
4
|
+
"version": "1.3.8",
|
|
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.3.
|
|
17
|
-
"@vltpkg/cache-unzip": "1.3.
|
|
18
|
-
"@vltpkg/error-cause": "1.3.
|
|
19
|
-
"@vltpkg/keychain": "1.3.
|
|
20
|
-
"@vltpkg/output": "1.3.
|
|
21
|
-
"@vltpkg/types": "1.3.
|
|
22
|
-
"@vltpkg/url-open": "1.3.
|
|
23
|
-
"@vltpkg/user-agent": "1.3.
|
|
24
|
-
"@vltpkg/xdg": "1.3.
|
|
16
|
+
"@vltpkg/cache": "1.3.8",
|
|
17
|
+
"@vltpkg/cache-unzip": "1.3.8",
|
|
18
|
+
"@vltpkg/error-cause": "1.3.8",
|
|
19
|
+
"@vltpkg/keychain": "1.3.8",
|
|
20
|
+
"@vltpkg/output": "1.3.8",
|
|
21
|
+
"@vltpkg/types": "1.3.8",
|
|
22
|
+
"@vltpkg/url-open": "1.3.8",
|
|
23
|
+
"@vltpkg/user-agent": "1.3.8",
|
|
24
|
+
"@vltpkg/xdg": "1.3.8",
|
|
25
25
|
"cache-control-parser": "^2.0.6",
|
|
26
26
|
"undici": "^7.16.0"
|
|
27
27
|
},
|