@vltpkg/registry-client 1.0.0-rc.9 → 1.0.2
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 +14 -1
- package/dist/{esm/add-header.d.ts → add-header.d.ts} +0 -1
- package/dist/{esm/add-header.js → add-header.js} +0 -1
- package/dist/auth.d.ts +44 -0
- package/dist/auth.js +118 -0
- package/dist/{esm/cache-entry.d.ts → cache-entry.d.ts} +0 -1
- package/dist/{esm/cache-entry.js → cache-entry.js} +0 -1
- package/dist/{esm/cache-revalidate.d.ts → cache-revalidate.d.ts} +0 -1
- package/dist/{esm/cache-revalidate.js → cache-revalidate.js} +7 -17
- package/dist/{esm/delete-header.d.ts → delete-header.d.ts} +0 -1
- package/dist/{esm/delete-header.js → delete-header.js} +0 -1
- package/dist/{esm/env.d.ts → env.d.ts} +0 -1
- package/dist/{esm/env.js → env.js} +0 -1
- package/dist/{esm/get-header.d.ts → get-header.d.ts} +0 -1
- package/dist/{esm/get-header.js → get-header.js} +0 -1
- package/dist/handle-304-response.d.ts +3 -0
- package/dist/handle-304-response.js +8 -0
- package/dist/{esm/index.d.ts → index.d.ts} +4 -3
- package/dist/{esm/index.js → index.js} +50 -22
- package/dist/{esm/is-cacheable.d.ts → is-cacheable.d.ts} +0 -1
- package/dist/{esm/is-cacheable.js → is-cacheable.js} +0 -1
- package/dist/{esm/is-iterable.d.ts → is-iterable.d.ts} +0 -1
- package/dist/{esm/is-iterable.js → is-iterable.js} +0 -1
- package/dist/oidc.d.ts +17 -0
- package/dist/oidc.js +150 -0
- package/dist/{esm/otplease.d.ts → otplease.d.ts} +6 -2
- package/dist/{esm/otplease.js → otplease.js} +31 -10
- package/dist/proxy.d.ts +14 -0
- package/dist/proxy.js +18 -0
- package/dist/{esm/raw-header.d.ts → raw-header.d.ts} +0 -1
- package/dist/{esm/raw-header.js → raw-header.js} +2 -5
- package/dist/{esm/redirect.d.ts → redirect.d.ts} +0 -1
- package/dist/{esm/redirect.js → redirect.js} +0 -1
- package/dist/{esm/revalidate.d.ts → revalidate.d.ts} +0 -1
- package/dist/{esm/revalidate.js → revalidate.js} +1 -6
- package/dist/{esm/set-cache-headers.d.ts → set-cache-headers.d.ts} +0 -1
- package/dist/{esm/set-cache-headers.js → set-cache-headers.js} +0 -1
- package/dist/{esm/set-raw-header.d.ts → set-raw-header.d.ts} +0 -1
- package/dist/{esm/set-raw-header.js → set-raw-header.js} +1 -3
- package/dist/{esm/string-encoding.d.ts → string-encoding.d.ts} +0 -1
- package/dist/{esm/string-encoding.js → string-encoding.js} +0 -1
- package/dist/{esm/token-response.d.ts → token-response.d.ts} +0 -1
- package/dist/{esm/token-response.js → token-response.js} +0 -1
- package/dist/{esm/web-auth-challenge.d.ts → web-auth-challenge.d.ts} +0 -1
- package/dist/{esm/web-auth-challenge.js → web-auth-challenge.js} +0 -1
- package/package.json +43 -47
- package/dist/esm/add-header.d.ts.map +0 -1
- package/dist/esm/add-header.js.map +0 -1
- package/dist/esm/auth.d.ts +0 -9
- package/dist/esm/auth.d.ts.map +0 -1
- package/dist/esm/auth.js +0 -39
- package/dist/esm/auth.js.map +0 -1
- package/dist/esm/cache-entry.d.ts.map +0 -1
- package/dist/esm/cache-entry.js.map +0 -1
- package/dist/esm/cache-revalidate.d.ts.map +0 -1
- package/dist/esm/cache-revalidate.js.map +0 -1
- package/dist/esm/delete-header.d.ts.map +0 -1
- package/dist/esm/delete-header.js.map +0 -1
- package/dist/esm/env.d.ts.map +0 -1
- package/dist/esm/env.js.map +0 -1
- package/dist/esm/get-header.d.ts.map +0 -1
- package/dist/esm/get-header.js.map +0 -1
- package/dist/esm/handle-304-response.d.ts +0 -4
- package/dist/esm/handle-304-response.d.ts.map +0 -1
- package/dist/esm/handle-304-response.js +0 -10
- package/dist/esm/handle-304-response.js.map +0 -1
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/is-cacheable.d.ts.map +0 -1
- package/dist/esm/is-cacheable.js.map +0 -1
- package/dist/esm/is-iterable.d.ts.map +0 -1
- package/dist/esm/is-iterable.js.map +0 -1
- package/dist/esm/otplease.d.ts.map +0 -1
- package/dist/esm/otplease.js.map +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/esm/raw-header.d.ts.map +0 -1
- package/dist/esm/raw-header.js.map +0 -1
- package/dist/esm/redirect.d.ts.map +0 -1
- package/dist/esm/redirect.js.map +0 -1
- package/dist/esm/revalidate.d.ts.map +0 -1
- package/dist/esm/revalidate.js.map +0 -1
- package/dist/esm/set-cache-headers.d.ts.map +0 -1
- package/dist/esm/set-cache-headers.js.map +0 -1
- package/dist/esm/set-raw-header.d.ts.map +0 -1
- package/dist/esm/set-raw-header.js.map +0 -1
- package/dist/esm/string-encoding.d.ts.map +0 -1
- package/dist/esm/string-encoding.js.map +0 -1
- package/dist/esm/token-response.d.ts.map +0 -1
- package/dist/esm/token-response.js.map +0 -1
- package/dist/esm/web-auth-challenge.d.ts.map +0 -1
- package/dist/esm/web-auth-challenge.js.map +0 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
This is a very light wrapper around undici, optimized for interfacing
|
|
6
6
|
with an npm registry.
|
|
7
7
|
|
|
8
|
-
**[Cache Unzipped](#cache-unzipped)** ·
|
|
8
|
+
**[Cache Unzipped](#cache-unzipped)** · **[Proxies](#proxies)** ·
|
|
9
9
|
**[Integrity Options](#integrity-options)** · **[Usage](#usage)**
|
|
10
10
|
|
|
11
11
|
## Overview
|
|
@@ -65,6 +65,19 @@ So,
|
|
|
65
65
|
Thus, the `content-length` response header will _usually_ not match
|
|
66
66
|
the actual byte length of the response body.
|
|
67
67
|
|
|
68
|
+
## Proxies
|
|
69
|
+
|
|
70
|
+
If `http_proxy` or `https_proxy` is set in the environment (either
|
|
71
|
+
case), requests are routed through that proxy using undici's
|
|
72
|
+
[`EnvHttpProxyAgent`](https://undici.nodejs.org/#/docs/api/EnvHttpProxyAgent).
|
|
73
|
+
Hosts listed in `no_proxy` are still dialed directly. When neither
|
|
74
|
+
variable is set, the client uses the same plain undici `Agent` it
|
|
75
|
+
always has, so there is no change to the default path.
|
|
76
|
+
|
|
77
|
+
A proxy that terminates TLS needs its certificate authority to be
|
|
78
|
+
trusted. Point `NODE_EXTRA_CA_CERTS` at the CA file and node will
|
|
79
|
+
trust it for the tunneled connection.
|
|
80
|
+
|
|
68
81
|
## Integrity Options
|
|
69
82
|
|
|
70
83
|
An `integrity` option may be specified in a `fetchOptions.context`
|
|
@@ -1,2 +1 @@
|
|
|
1
1
|
export declare const addHeader: <H extends [string, string[] | string][] | Iterable<[string, string[] | string | undefined]> | Record<string, string[] | string | undefined> | string[]>(headers: H | null | undefined, key: string, value?: string) => H;
|
|
2
|
-
//# sourceMappingURL=add-header.d.ts.map
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Keychain } from '@vltpkg/keychain';
|
|
2
|
+
export type Token = `Bearer ${string}` | `Basic ${string}`;
|
|
3
|
+
/**
|
|
4
|
+
* Normalize a registry URL into a stable key that preserves the
|
|
5
|
+
* path prefix. The result is `origin + pathname` with trailing
|
|
6
|
+
* slashes stripped so that
|
|
7
|
+
* `https://r.io/luke/` and `https://r.io/luke`
|
|
8
|
+
* both produce the same key.
|
|
9
|
+
*
|
|
10
|
+
* For plain-origin registries the result is identical to the old
|
|
11
|
+
* `new URL(url).origin` behaviour (e.g. `https://registry.npmjs.org`).
|
|
12
|
+
*/
|
|
13
|
+
export declare const normalizeRegistryKey: (url: string) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Ensure a registry URL ends with `/` so that `new URL(path, base)`
|
|
16
|
+
* appends under the full path instead of replacing the last segment.
|
|
17
|
+
*
|
|
18
|
+
* registryBase('https://r.io/scope/name')
|
|
19
|
+
* // → 'https://r.io/scope/name/'
|
|
20
|
+
*/
|
|
21
|
+
export declare const registryBase: (url: string) => string;
|
|
22
|
+
export declare const keychains: Map<string, Keychain<Token>>;
|
|
23
|
+
/**
|
|
24
|
+
* In-memory token store for OIDC-exchanged tokens.
|
|
25
|
+
* These take precedence over env vars and keychain.
|
|
26
|
+
*/
|
|
27
|
+
export declare const runtimeTokens: Map<string, Token>;
|
|
28
|
+
export declare const setRuntimeToken: (registry: string, token: Token) => void;
|
|
29
|
+
export declare const clearRuntimeTokens: () => void;
|
|
30
|
+
export declare const getKC: (identity: string) => Keychain<Token>;
|
|
31
|
+
export declare const isToken: (t: any) => t is Token;
|
|
32
|
+
export declare const deleteToken: (registry: string, identity: string) => Promise<void>;
|
|
33
|
+
export declare const setToken: (registry: string, token: Token, identity: string) => Promise<void>;
|
|
34
|
+
export declare const getToken: (registry: string, identity: string) => Promise<Token | undefined>;
|
|
35
|
+
/**
|
|
36
|
+
* Find the best matching token for a request URL by performing a
|
|
37
|
+
* longest-prefix match against all known registry keys (runtime
|
|
38
|
+
* tokens, env-var registries, and keychain entries).
|
|
39
|
+
*
|
|
40
|
+
* This is used by `RegistryClient.request()` which only has the
|
|
41
|
+
* full request URL — not the configured registry URL that was used
|
|
42
|
+
* to construct it.
|
|
43
|
+
*/
|
|
44
|
+
export declare const getTokenByURL: (requestUrl: string, identity: string) => Promise<Token | undefined>;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Keychain } from '@vltpkg/keychain';
|
|
2
|
+
/**
|
|
3
|
+
* Normalize a registry URL into a stable key that preserves the
|
|
4
|
+
* path prefix. The result is `origin + pathname` with trailing
|
|
5
|
+
* slashes stripped so that
|
|
6
|
+
* `https://r.io/luke/` and `https://r.io/luke`
|
|
7
|
+
* both produce the same key.
|
|
8
|
+
*
|
|
9
|
+
* For plain-origin registries the result is identical to the old
|
|
10
|
+
* `new URL(url).origin` behaviour (e.g. `https://registry.npmjs.org`).
|
|
11
|
+
*/
|
|
12
|
+
export const normalizeRegistryKey = (url) => {
|
|
13
|
+
const u = new URL(url);
|
|
14
|
+
return (u.origin + u.pathname).replace(/\/+$/, '');
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Ensure a registry URL ends with `/` so that `new URL(path, base)`
|
|
18
|
+
* appends under the full path instead of replacing the last segment.
|
|
19
|
+
*
|
|
20
|
+
* registryBase('https://r.io/scope/name')
|
|
21
|
+
* // → 'https://r.io/scope/name/'
|
|
22
|
+
*/
|
|
23
|
+
export const registryBase = (url) => url.endsWith('/') ? url : url + '/';
|
|
24
|
+
// just exported for testing
|
|
25
|
+
export const keychains = new Map();
|
|
26
|
+
/**
|
|
27
|
+
* In-memory token store for OIDC-exchanged tokens.
|
|
28
|
+
* These take precedence over env vars and keychain.
|
|
29
|
+
*/
|
|
30
|
+
export const runtimeTokens = new Map();
|
|
31
|
+
export const setRuntimeToken = (registry, token) => {
|
|
32
|
+
runtimeTokens.set(normalizeRegistryKey(registry), token);
|
|
33
|
+
};
|
|
34
|
+
export const clearRuntimeTokens = () => {
|
|
35
|
+
runtimeTokens.clear();
|
|
36
|
+
};
|
|
37
|
+
export const getKC = (identity) => {
|
|
38
|
+
const kc = keychains.get(identity);
|
|
39
|
+
if (kc)
|
|
40
|
+
return kc;
|
|
41
|
+
const i = identity ? `vlt/auth/${identity}` : 'vlt/auth';
|
|
42
|
+
const nkc = new Keychain(i);
|
|
43
|
+
keychains.set(identity, nkc);
|
|
44
|
+
return nkc;
|
|
45
|
+
};
|
|
46
|
+
export const isToken = (t) => typeof t === 'string' &&
|
|
47
|
+
(t.startsWith('Bearer ') || t.startsWith('Basic '));
|
|
48
|
+
export const deleteToken = async (registry, identity) => {
|
|
49
|
+
const kc = getKC(identity);
|
|
50
|
+
await kc.load();
|
|
51
|
+
kc.delete(normalizeRegistryKey(registry));
|
|
52
|
+
await kc.save();
|
|
53
|
+
};
|
|
54
|
+
export const setToken = async (registry, token, identity) => {
|
|
55
|
+
const kc = getKC(identity);
|
|
56
|
+
await kc.load();
|
|
57
|
+
kc.set(normalizeRegistryKey(registry), token);
|
|
58
|
+
await kc.save();
|
|
59
|
+
};
|
|
60
|
+
export const getToken = async (registry, identity) => {
|
|
61
|
+
const kc = getKC(identity);
|
|
62
|
+
const key = normalizeRegistryKey(registry);
|
|
63
|
+
// Runtime tokens (e.g. from OIDC exchange) take precedence
|
|
64
|
+
const rt = runtimeTokens.get(key);
|
|
65
|
+
if (rt)
|
|
66
|
+
return rt;
|
|
67
|
+
const envReg = process.env.VLT_REGISTRY;
|
|
68
|
+
if (envReg && key === normalizeRegistryKey(envReg)) {
|
|
69
|
+
const envTok = process.env.VLT_TOKEN;
|
|
70
|
+
if (envTok)
|
|
71
|
+
return `Bearer ${envTok}`;
|
|
72
|
+
}
|
|
73
|
+
const tok = process.env[`VLT_TOKEN_${key.replace(/[^a-zA-Z0-9]+/g, '_')}`];
|
|
74
|
+
if (tok)
|
|
75
|
+
return `Bearer ${tok}`;
|
|
76
|
+
return kc.get(key);
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Find the best matching token for a request URL by performing a
|
|
80
|
+
* longest-prefix match against all known registry keys (runtime
|
|
81
|
+
* tokens, env-var registries, and keychain entries).
|
|
82
|
+
*
|
|
83
|
+
* This is used by `RegistryClient.request()` which only has the
|
|
84
|
+
* full request URL — not the configured registry URL that was used
|
|
85
|
+
* to construct it.
|
|
86
|
+
*/
|
|
87
|
+
export const getTokenByURL = async (requestUrl, identity) => {
|
|
88
|
+
const normalized = normalizeRegistryKey(requestUrl);
|
|
89
|
+
// Collect all known registry keys.
|
|
90
|
+
const candidates = [...runtimeTokens.keys()];
|
|
91
|
+
const envReg = process.env.VLT_REGISTRY;
|
|
92
|
+
if (envReg) {
|
|
93
|
+
candidates.push(normalizeRegistryKey(envReg));
|
|
94
|
+
}
|
|
95
|
+
const kc = getKC(identity);
|
|
96
|
+
// Keychain entries
|
|
97
|
+
for (const k of await kc.keys()) {
|
|
98
|
+
candidates.push(k);
|
|
99
|
+
}
|
|
100
|
+
// Find the longest candidate key that is a prefix of the
|
|
101
|
+
// normalized request URL.
|
|
102
|
+
let bestKey;
|
|
103
|
+
let bestLen = 0;
|
|
104
|
+
for (const candidate of candidates) {
|
|
105
|
+
if (candidate.length > bestLen &&
|
|
106
|
+
(normalized === candidate ||
|
|
107
|
+
normalized.startsWith(candidate + '/'))) {
|
|
108
|
+
bestKey = candidate;
|
|
109
|
+
bestLen = candidate.length;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (bestKey) {
|
|
113
|
+
return getToken(bestKey, identity);
|
|
114
|
+
}
|
|
115
|
+
// Fall back to origin-only match (handles VLT_TOKEN_* env vars
|
|
116
|
+
// which we can't enumerate by URL).
|
|
117
|
+
return getToken(requestUrl, identity);
|
|
118
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { __CODE_SPLIT_SCRIPT_NAME } from "./revalidate.js";
|
|
3
|
-
import { pathToFileURL } from 'node:url';
|
|
4
3
|
const isDeno = globalThis.Deno != undefined;
|
|
5
4
|
let didProcessBeforeExitHook = false;
|
|
6
5
|
const registered = new Map();
|
|
@@ -22,22 +21,14 @@ const handleBeforeExit = () => {
|
|
|
22
21
|
const env = { ...process.env };
|
|
23
22
|
const args = [];
|
|
24
23
|
/* c8 ignore start */
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
args.push(
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
// If we are running deno from source we need to add the
|
|
33
|
-
// unstable flags we need. The '-A' flag does not need
|
|
34
|
-
// to be passed in as Deno supplies that automatically.
|
|
35
|
-
if (isDeno) {
|
|
36
|
-
args.push('--unstable-node-globals', '--unstable-bare-node-builtins');
|
|
37
|
-
}
|
|
38
|
-
/* c8 ignore stop */
|
|
39
|
-
args.push(__CODE_SPLIT_SCRIPT_NAME, path);
|
|
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');
|
|
40
29
|
}
|
|
30
|
+
/* c8 ignore stop */
|
|
31
|
+
args.push(__CODE_SPLIT_SCRIPT_NAME, path);
|
|
41
32
|
registered.delete(path);
|
|
42
33
|
// Deno on Windows does not support detached processes
|
|
43
34
|
// https://github.com/denoland/deno/issues/25867
|
|
@@ -63,4 +54,3 @@ const handleBeforeExit = () => {
|
|
|
63
54
|
}
|
|
64
55
|
}
|
|
65
56
|
};
|
|
66
|
-
//# sourceMappingURL=cache-revalidate.js.map
|
|
@@ -1,2 +1 @@
|
|
|
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;
|
|
2
|
-
//# sourceMappingURL=delete-header.d.ts.map
|
|
@@ -10,4 +10,3 @@ export const isNode = !isDeno && !isBun && 'node' in proc.versions;
|
|
|
10
10
|
export const bun = isBun ? proc.versions.bun : undefined;
|
|
11
11
|
export const deno = isDeno ? proc.versions.deno : undefined;
|
|
12
12
|
export const node = isNode ? proc.versions.node : undefined;
|
|
13
|
-
//# sourceMappingURL=env.js.map
|
|
@@ -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
|
+
};
|
|
@@ -3,12 +3,14 @@ import type { Integrity } from '@vltpkg/types';
|
|
|
3
3
|
import type { Dispatcher } from 'undici';
|
|
4
4
|
import { RetryAgent } from 'undici';
|
|
5
5
|
import type { Token } from './auth.ts';
|
|
6
|
-
import { deleteToken, getKC, isToken, keychains, setToken } from './auth.ts';
|
|
6
|
+
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, runtimeTokens, setRuntimeToken, setToken } from './auth.ts';
|
|
7
7
|
import type { JSONObj } from './cache-entry.ts';
|
|
8
8
|
import { CacheEntry } from './cache-entry.ts';
|
|
9
9
|
import type { TokenResponse } from './token-response.ts';
|
|
10
10
|
import type { WebAuthChallenge } from './web-auth-challenge.ts';
|
|
11
|
-
|
|
11
|
+
import { oidc } from './oidc.ts';
|
|
12
|
+
import type { OidcOptions } from './oidc.ts';
|
|
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, };
|
|
12
14
|
export type CacheableMethod = 'GET' | 'HEAD';
|
|
13
15
|
export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
|
|
14
16
|
export type RegistryClientOptions = {
|
|
@@ -141,4 +143,3 @@ export declare class RegistryClient {
|
|
|
141
143
|
webAuthOpener({ doneUrl, authUrl }: WebAuthChallenge): Promise<TokenResponse>;
|
|
142
144
|
request(url: URL | string, options?: RegistryClientRequestOptions): Promise<CacheEntry>;
|
|
143
145
|
}
|
|
144
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -5,23 +5,26 @@ import { asError } from '@vltpkg/types';
|
|
|
5
5
|
import { logRequest } from '@vltpkg/output';
|
|
6
6
|
import { urlOpen } from '@vltpkg/url-open';
|
|
7
7
|
import { XDG } from '@vltpkg/xdg';
|
|
8
|
+
import { randomUUID } from 'node:crypto';
|
|
8
9
|
import { dirname, resolve } from 'node:path';
|
|
9
10
|
import { setTimeout } from 'node:timers/promises';
|
|
10
11
|
import { loadPackageJson } from 'package-json-from-dist';
|
|
11
|
-
import {
|
|
12
|
+
import { RetryAgent } from 'undici';
|
|
12
13
|
import { addHeader } from "./add-header.js";
|
|
13
|
-
import { deleteToken, getKC, getToken, isToken, keychains, setToken, } from "./auth.js";
|
|
14
|
+
import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, runtimeTokens, setRuntimeToken, setToken, } from "./auth.js";
|
|
14
15
|
import { CacheEntry } from "./cache-entry.js";
|
|
15
16
|
import { register } from "./cache-revalidate.js";
|
|
16
17
|
import { bun, deno, node } from "./env.js";
|
|
17
|
-
import {
|
|
18
|
+
import { handleCacheHitResponse } from "./handle-304-response.js";
|
|
18
19
|
import { otplease } from "./otplease.js";
|
|
20
|
+
import { getDispatcher } from "./proxy.js";
|
|
19
21
|
import { isRedirect, redirect } from "./redirect.js";
|
|
20
22
|
import { setCacheHeaders } from "./set-cache-headers.js";
|
|
21
23
|
import { getTokenResponse } from "./token-response.js";
|
|
22
24
|
import { getWebAuthChallenge } from "./web-auth-challenge.js";
|
|
23
25
|
import { getEncondedValue } from "./string-encoding.js";
|
|
24
|
-
|
|
26
|
+
import { oidc } from "./oidc.js";
|
|
27
|
+
export { CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, runtimeTokens, setRuntimeToken, setToken, };
|
|
25
28
|
export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
|
|
26
29
|
const { version } = loadPackageJson(import.meta.filename, process.env.__VLT_INTERNAL_REGISTRY_CLIENT_PACKAGE_JSON);
|
|
27
30
|
const nua = globalThis.navigator?.userAgent ??
|
|
@@ -51,6 +54,7 @@ export class RegistryClient {
|
|
|
51
54
|
cache;
|
|
52
55
|
identity;
|
|
53
56
|
staleWhileRevalidateFactor;
|
|
57
|
+
#session = randomUUID();
|
|
54
58
|
constructor(options) {
|
|
55
59
|
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
|
|
56
60
|
} = options;
|
|
@@ -65,7 +69,7 @@ export class RegistryClient {
|
|
|
65
69
|
}
|
|
66
70
|
},
|
|
67
71
|
});
|
|
68
|
-
const dispatch =
|
|
72
|
+
const dispatch = getDispatcher(agentOptions);
|
|
69
73
|
this.agent = new RetryAgent(dispatch, {
|
|
70
74
|
maxRetries,
|
|
71
75
|
timeoutFactor,
|
|
@@ -111,13 +115,14 @@ export class RegistryClient {
|
|
|
111
115
|
if (!tok)
|
|
112
116
|
return;
|
|
113
117
|
const s = tok.replace(/^(Bearer|Basic) /i, '');
|
|
114
|
-
const
|
|
118
|
+
const base = registryBase(registry);
|
|
119
|
+
const tokensUrl = new URL('-/npm/v1/tokens', base);
|
|
115
120
|
const record = await this.seek(tokensUrl, ({ token }) => s.startsWith(token), {
|
|
116
121
|
useCache: false,
|
|
117
122
|
}).catch(() => undefined);
|
|
118
123
|
if (record) {
|
|
119
124
|
const { key } = record;
|
|
120
|
-
await this.request(new URL(`-/npm/v1/tokens/token/${key}`,
|
|
125
|
+
await this.request(new URL(`-/npm/v1/tokens/token/${key}`, base), { useCache: false, method: 'DELETE' });
|
|
121
126
|
}
|
|
122
127
|
await deleteToken(registry, this.identity);
|
|
123
128
|
}
|
|
@@ -135,7 +140,7 @@ export class RegistryClient {
|
|
|
135
140
|
// - hang on the doneUrl until done
|
|
136
141
|
//
|
|
137
142
|
// if that fails: fall back to couchdb login
|
|
138
|
-
const webLoginURL = new URL('-/v1/login', registry);
|
|
143
|
+
const webLoginURL = new URL('-/v1/login', registryBase(registry));
|
|
139
144
|
const response = await this.request(webLoginURL, {
|
|
140
145
|
method: 'POST',
|
|
141
146
|
useCache: false,
|
|
@@ -212,23 +217,23 @@ export class RegistryClient {
|
|
|
212
217
|
const { useCache = !!m } = options;
|
|
213
218
|
signal?.throwIfAborted();
|
|
214
219
|
// first, try to get from the cache before making any request.
|
|
215
|
-
const { origin } = u;
|
|
216
220
|
const key = `${method !== 'GET' ? method + ' ' : ''}${u}`;
|
|
217
221
|
const buffer = useCache ?
|
|
218
222
|
await this.cache.fetch(key, { context: { integrity } })
|
|
219
223
|
: undefined;
|
|
220
224
|
const entry = buffer ? CacheEntry.decode(buffer) : undefined;
|
|
221
225
|
if (entry?.valid) {
|
|
222
|
-
logRequest(url, 'cache');
|
|
226
|
+
logRequest(url, 'cache', { method });
|
|
223
227
|
return entry;
|
|
224
228
|
}
|
|
225
229
|
if (staleWhileRevalidate && entry?.staleWhileRevalidate && m) {
|
|
226
230
|
// revalidate while returning the stale entry
|
|
227
231
|
register(dirname(this.cache.path()), m, url);
|
|
228
|
-
logRequest(url, 'stale');
|
|
232
|
+
logRequest(url, 'stale', { method });
|
|
229
233
|
return entry;
|
|
230
234
|
}
|
|
231
|
-
logRequest(url, 'start');
|
|
235
|
+
logRequest(url, 'start', { method });
|
|
236
|
+
const requestStart = Date.now();
|
|
232
237
|
// either no cache entry, or need to revalidate before use.
|
|
233
238
|
setCacheHeaders(options, entry);
|
|
234
239
|
redirections.add(String(url));
|
|
@@ -237,7 +242,7 @@ export class RegistryClient {
|
|
|
237
242
|
...agentOptions,
|
|
238
243
|
});
|
|
239
244
|
options.origin = u.origin;
|
|
240
|
-
options.headers = addHeader(addHeader(options.headers, 'accept-encoding', 'gzip;q=1.0, identity;q=0.5'), 'user-agent', userAgent);
|
|
245
|
+
options.headers = addHeader(addHeader(addHeader(options.headers, 'accept-encoding', 'gzip;q=1.0, identity;q=0.5'), 'user-agent', userAgent), 'npm-session', this.#session);
|
|
241
246
|
if (otp) {
|
|
242
247
|
options.headers = addHeader(options.headers, 'npm-otp', otp);
|
|
243
248
|
}
|
|
@@ -246,7 +251,7 @@ export class RegistryClient {
|
|
|
246
251
|
}
|
|
247
252
|
options.method = options.method ?? 'GET';
|
|
248
253
|
// will remove if we don't have a token.
|
|
249
|
-
options.headers = addHeader(options.headers, 'authorization', await
|
|
254
|
+
options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), this.identity));
|
|
250
255
|
let response = null;
|
|
251
256
|
try {
|
|
252
257
|
response = await this.agent.request(options);
|
|
@@ -263,6 +268,11 @@ export class RegistryClient {
|
|
|
263
268
|
}
|
|
264
269
|
/* c8 ignore stop */
|
|
265
270
|
const result = await this.#handleResponse(u, options, response, entry);
|
|
271
|
+
logRequest(url, response.statusCode === 304 ? '304' : 'complete', {
|
|
272
|
+
method,
|
|
273
|
+
statusCode: response.statusCode,
|
|
274
|
+
durationMs: Date.now() - requestStart,
|
|
275
|
+
});
|
|
266
276
|
if (result.getHeader('integrity')) {
|
|
267
277
|
trustIntegrity = true;
|
|
268
278
|
}
|
|
@@ -279,12 +289,17 @@ export class RegistryClient {
|
|
|
279
289
|
return result;
|
|
280
290
|
}
|
|
281
291
|
async #handleResponse(url, options, response, entry) {
|
|
282
|
-
if (
|
|
292
|
+
if (handleCacheHitResponse(response, entry))
|
|
283
293
|
return entry;
|
|
294
|
+
let consumedBody;
|
|
284
295
|
if (response.statusCode === 401) {
|
|
285
|
-
const
|
|
286
|
-
if (
|
|
287
|
-
return await this.request(url,
|
|
296
|
+
const otpResult = await otplease(this, options, response);
|
|
297
|
+
if (otpResult && 'retry' in otpResult) {
|
|
298
|
+
return await this.request(url, otpResult.retry);
|
|
299
|
+
}
|
|
300
|
+
if (otpResult && 'bodyConsumed' in otpResult) {
|
|
301
|
+
consumedBody = otpResult.bodyConsumed;
|
|
302
|
+
}
|
|
288
303
|
}
|
|
289
304
|
const h = [];
|
|
290
305
|
for (const [key, value] of Object.entries(response.headers)) {
|
|
@@ -298,15 +313,20 @@ export class RegistryClient {
|
|
|
298
313
|
}
|
|
299
314
|
}
|
|
300
315
|
const { integrity, trustIntegrity } = options;
|
|
316
|
+
// When otplease already consumed the body, use its length
|
|
317
|
+
// instead of the Content-Length header to size the CacheEntry
|
|
318
|
+
// buffer correctly.
|
|
319
|
+
const contentLength = consumedBody !== undefined ? consumedBody.length
|
|
320
|
+
: response.headers['content-length'] ?
|
|
321
|
+
Number(response.headers['content-length'])
|
|
322
|
+
: /* c8 ignore next */ undefined;
|
|
301
323
|
const result = new CacheEntry(
|
|
302
324
|
/* c8 ignore next - should always have a status code */
|
|
303
325
|
response.statusCode || 200, h, {
|
|
304
326
|
integrity,
|
|
305
327
|
trustIntegrity,
|
|
306
328
|
'stale-while-revalidate-factor': this.staleWhileRevalidateFactor,
|
|
307
|
-
contentLength
|
|
308
|
-
Number(response.headers['content-length'])
|
|
309
|
-
: /* c8 ignore next */ undefined,
|
|
329
|
+
contentLength,
|
|
310
330
|
});
|
|
311
331
|
if (isRedirect(result)) {
|
|
312
332
|
response.body.resume();
|
|
@@ -316,6 +336,15 @@ export class RegistryClient {
|
|
|
316
336
|
}
|
|
317
337
|
return result;
|
|
318
338
|
}
|
|
339
|
+
// If otplease already consumed the body (e.g. checking for OTP
|
|
340
|
+
// prompt on a plain 401), use the text it read rather than trying
|
|
341
|
+
// to re-read from the already-drained stream.
|
|
342
|
+
if (consumedBody !== undefined) {
|
|
343
|
+
if (consumedBody.length > 0) {
|
|
344
|
+
result.addBody(new TextEncoder().encode(consumedBody));
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
319
348
|
response.body.on('data', (chunk) => result.addBody(chunk));
|
|
320
349
|
return await new Promise((res, rej) => {
|
|
321
350
|
response.body.on('error', rej);
|
|
@@ -323,4 +352,3 @@ export class RegistryClient {
|
|
|
323
352
|
});
|
|
324
353
|
}
|
|
325
354
|
}
|
|
326
|
-
//# sourceMappingURL=index.js.map
|
package/dist/oidc.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Token } from './auth.ts';
|
|
2
|
+
export type OidcOptions = {
|
|
3
|
+
/** The package name being published, e.g. `@scope/name` */
|
|
4
|
+
packageName: string;
|
|
5
|
+
/** The full registry URL, e.g. `https://registry.npmjs.org/` */
|
|
6
|
+
registry: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Detect CI environment and exchange an OIDC ID token for a
|
|
10
|
+
* registry auth token. Sets the token into the runtime token
|
|
11
|
+
* store so subsequent requests are authenticated.
|
|
12
|
+
*
|
|
13
|
+
* This function **never throws**. OIDC is always optional — if
|
|
14
|
+
* it is unavailable or any step fails the function returns
|
|
15
|
+
* `undefined` silently.
|
|
16
|
+
*/
|
|
17
|
+
export declare const oidc: (opts: OidcOptions) => Promise<Token | undefined>;
|