@vltpkg/registry-client 1.5.0 → 1.6.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/auth.d.ts CHANGED
@@ -35,14 +35,25 @@ export declare const isToken: (t: any) => t is Token;
35
35
  */
36
36
  export declare const deleteToken: (registry: string, identity: string, token?: Token) => Promise<void>;
37
37
  export declare const setToken: (registry: string, token: Token, identity: string) => Promise<void>;
38
- export declare const getToken: (registry: string, identity: string) => Promise<Token | undefined>;
38
+ /**
39
+ * `VLT_TOKEN` applies to `VLT_REGISTRY` if set, else `defaultKey`
40
+ * (see {@link defaultRegistryKey}).
41
+ */
42
+ export declare const getToken: (registry: string, identity: string, defaultKey?: string) => Promise<Token | undefined>;
39
43
  /** Configured registry URLs, by config option name. */
40
44
  export type RegistryURLs = {
41
45
  registry?: string;
42
46
  registries?: Record<string, string>;
43
47
  'scoped-registries'?: Record<string, string>;
44
48
  'jsr-registries'?: Record<string, string>;
49
+ /** `registries` alias for bare specs, default `npm` */
50
+ 'default-registry-alias'?: string;
45
51
  };
52
+ /**
53
+ * Normalized key of `registries[default-registry-alias]` (a `registry`
54
+ * scalar applies via `VLT_REGISTRY`). Undefined if unset/unparseable.
55
+ */
56
+ export declare const defaultRegistryKey: (o: RegistryURLs) => string | undefined;
46
57
  /**
47
58
  * Normalized, deduped keys of the configured registries.
48
59
  * Unparseable URLs are skipped.
@@ -51,9 +62,10 @@ export declare const registryKeys: (o: RegistryURLs) => string[];
51
62
  /**
52
63
  * Find the best matching token for a request URL by performing a
53
64
  * longest-prefix match against all known registry keys (runtime
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.
65
+ * tokens, `VLT_REGISTRY`, `defaultKey`, keychain entries, and `keys`:
66
+ * normalized configured registry keys, e.g. from {@link registryKeys}).
67
+ * A key that resolves no token is skipped. `VLT_TOKEN` applies to
68
+ * `VLT_REGISTRY` if set, else `defaultKey`.
57
69
  *
58
70
  * `VLT_TOKEN_<key>` env vars are only read for a known key: their
59
71
  * names are lossy (`.`, `-`, `/` all become `_`), so probing them by
@@ -62,4 +74,4 @@ export declare const registryKeys: (o: RegistryURLs) => string[];
62
74
  * Used by `RegistryClient.request()`, which has the request URL, not
63
75
  * the registry URL it was built from.
64
76
  */
65
- export declare const getTokenByURL: (requestUrl: string, identity: string, keys?: readonly string[]) => Promise<Token | undefined>;
77
+ export declare const getTokenByURL: (requestUrl: string, identity: string, keys?: readonly string[], defaultKey?: string) => Promise<Token | undefined>;
package/dist/auth.js CHANGED
@@ -67,7 +67,11 @@ export const setToken = async (registry, token, identity) => {
67
67
  kc.set(normalizeRegistryKey(registry), token);
68
68
  await kc.save();
69
69
  };
70
- export const getToken = async (registry, identity) => {
70
+ /**
71
+ * `VLT_TOKEN` applies to `VLT_REGISTRY` if set, else `defaultKey`
72
+ * (see {@link defaultRegistryKey}).
73
+ */
74
+ export const getToken = async (registry, identity, defaultKey) => {
71
75
  const kc = getKC(identity);
72
76
  const key = normalizeRegistryKey(registry);
73
77
  // Runtime tokens (e.g. from OIDC exchange) take precedence
@@ -75,7 +79,8 @@ export const getToken = async (registry, identity) => {
75
79
  if (rt)
76
80
  return rt;
77
81
  const envReg = process.env.VLT_REGISTRY;
78
- if (envReg && key === normalizeRegistryKey(envReg)) {
82
+ const tokKey = envReg ? normalizeRegistryKey(envReg) : defaultKey;
83
+ if (key === tokKey) {
79
84
  const envTok = process.env.VLT_TOKEN;
80
85
  if (envTok)
81
86
  return `Bearer ${envTok}`;
@@ -85,6 +90,15 @@ export const getToken = async (registry, identity) => {
85
90
  return `Bearer ${tok}`;
86
91
  return kc.get(key);
87
92
  };
93
+ /**
94
+ * Normalized key of `registries[default-registry-alias]` (a `registry`
95
+ * scalar applies via `VLT_REGISTRY`). Undefined if unset/unparseable.
96
+ */
97
+ export const defaultRegistryKey = (o) => {
98
+ // 'npm': default of `default-registry-alias`
99
+ const u = o.registries?.[o['default-registry-alias'] ?? 'npm'];
100
+ return u && URL.canParse(u) ? normalizeRegistryKey(u) : undefined;
101
+ };
88
102
  /**
89
103
  * Normalized, deduped keys of the configured registries.
90
104
  * Unparseable URLs are skipped.
@@ -102,9 +116,10 @@ export const registryKeys = (o) => [
102
116
  /**
103
117
  * Find the best matching token for a request URL by performing a
104
118
  * longest-prefix match against all known registry keys (runtime
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.
119
+ * tokens, `VLT_REGISTRY`, `defaultKey`, keychain entries, and `keys`:
120
+ * normalized configured registry keys, e.g. from {@link registryKeys}).
121
+ * A key that resolves no token is skipped. `VLT_TOKEN` applies to
122
+ * `VLT_REGISTRY` if set, else `defaultKey`.
108
123
  *
109
124
  * `VLT_TOKEN_<key>` env vars are only read for a known key: their
110
125
  * names are lossy (`.`, `-`, `/` all become `_`), so probing them by
@@ -113,7 +128,7 @@ export const registryKeys = (o) => [
113
128
  * Used by `RegistryClient.request()`, which has the request URL, not
114
129
  * the registry URL it was built from.
115
130
  */
116
- export const getTokenByURL = async (requestUrl, identity, keys = []) => {
131
+ export const getTokenByURL = async (requestUrl, identity, keys = [], defaultKey) => {
117
132
  const normalized = normalizeRegistryKey(requestUrl);
118
133
  // Collect all known registry keys.
119
134
  const candidates = [...runtimeTokens.keys(), ...keys];
@@ -121,6 +136,8 @@ export const getTokenByURL = async (requestUrl, identity, keys = []) => {
121
136
  if (envReg) {
122
137
  candidates.push(normalizeRegistryKey(envReg));
123
138
  }
139
+ if (defaultKey)
140
+ candidates.push(defaultKey);
124
141
  const kc = getKC(identity);
125
142
  // Keychain entries
126
143
  for (const k of await kc.keys()) {
@@ -132,7 +149,7 @@ export const getTokenByURL = async (requestUrl, identity, keys = []) => {
132
149
  .filter(c => normalized === c || normalized.startsWith(c + '/'))
133
150
  .sort((a, b) => b.length - a.length);
134
151
  for (const key of matches) {
135
- const tok = await getToken(key, identity);
152
+ const tok = await getToken(key, identity, defaultKey);
136
153
  if (tok)
137
154
  return tok;
138
155
  }
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import type { Dispatcher } from 'undici';
5
5
  import { RetryAgent } from 'undici';
6
6
  import { userAgent } from '@vltpkg/user-agent';
7
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';
8
+ import { clearRuntimeTokens, defaultRegistryKey, 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, registryKeys, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, type ErrorResponse, type JSONObj, type OidcOptions, type RegistryURLs, type Token, type TokenResponse, type WebAuthChallenge, };
18
+ export { assertOk, CacheEntry, clearRuntimeTokens, defaultRegistryKey, 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 */
@@ -171,6 +171,8 @@ export declare class RegistryClient {
171
171
  identity: string;
172
172
  /** configured registry keys; `VLT_TOKEN_<key>` applies under each */
173
173
  readonly registryKeys: readonly string[];
174
+ /** default registry key: gets `VLT_TOKEN` if no `VLT_REGISTRY` */
175
+ readonly defaultRegistryKey: string | undefined;
174
176
  staleWhileRevalidateFactor: number;
175
177
  constructor(options: RegistryClientOptions);
176
178
  /**
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ import { createGunzip } from 'node:zlib';
16
16
  import { RetryAgent } from 'undici';
17
17
  import { userAgent } from '@vltpkg/user-agent';
18
18
  import { addHeader } from "./add-header.js";
19
- import { clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, registryKeys, runtimeTokens, setRuntimeToken, setToken, } from "./auth.js";
19
+ import { clearRuntimeTokens, defaultRegistryKey, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, registryBase, registryKeys, runtimeTokens, setRuntimeToken, setToken, } from "./auth.js";
20
20
  import { CacheEntry } from "./cache-entry.js";
21
21
  import { register } from "./cache-revalidate.js";
22
22
  import { getHeader } from "./get-header.js";
@@ -34,7 +34,7 @@ import { storeRoot } from "./store-root.js";
34
34
  import { oidc } from "./oidc.js";
35
35
  // eslint-disable-next-line no-console
36
36
  const log = (msg) => console.error(msg);
37
- export { assertOk, CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, registryKeys, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
37
+ export { assertOk, CacheEntry, clearRuntimeTokens, defaultRegistryKey, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, registryErrorMessage, registryKeys, requestError, runtimeTokens, setRuntimeToken, setToken, storeRoot, };
38
38
  export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
39
39
  // A head is about a kilobyte. This limit exists only so that a corrupt
40
40
  // length cannot make us allocate something enormous.
@@ -92,6 +92,8 @@ export class RegistryClient {
92
92
  identity;
93
93
  /** configured registry keys; `VLT_TOKEN_<key>` applies under each */
94
94
  registryKeys;
95
+ /** default registry key: gets `VLT_TOKEN` if no `VLT_REGISTRY` */
96
+ defaultRegistryKey;
95
97
  staleWhileRevalidateFactor;
96
98
  #session = randomUUID();
97
99
  #decoded = new WeakMap();
@@ -114,6 +116,7 @@ export class RegistryClient {
114
116
  } = options;
115
117
  this.identity = identity;
116
118
  this.registryKeys = registryKeys(options);
119
+ this.defaultRegistryKey = defaultRegistryKey(options);
117
120
  this.staleWhileRevalidateFactor = staleWhileRevalidateFactor;
118
121
  const path = resolve(cache, 'registry-client');
119
122
  const store = options.storeRoot ?? storeRoot(cache);
@@ -550,7 +553,7 @@ export class RegistryClient {
550
553
  origin: u.origin,
551
554
  headers: addHeader(addHeader(addHeader(options.headers, 'accept-encoding', 'gzip;q=1.0, identity;q=0.5'), 'user-agent', userAgent), 'npm-session', this.#session),
552
555
  };
553
- o.headers = addHeader(o.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys));
556
+ o.headers = addHeader(o.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys, this.defaultRegistryKey));
554
557
  logRequest(url, 'start', { method: o.method });
555
558
  const requestStart = Date.now();
556
559
  const response = await this.agent.request(o);
@@ -660,7 +663,7 @@ export class RegistryClient {
660
663
  // redirect() strips it when the origin changes.
661
664
  if (!redirected || !getHeader(options.headers, 'authorization')) {
662
665
  // will remove if we don't have a token.
663
- options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys));
666
+ options.headers = addHeader(options.headers, 'authorization', await getTokenByURL(String(u), this.identity, this.registryKeys, this.defaultRegistryKey));
664
667
  }
665
668
  let response = null;
666
669
  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.5.0",
4
+ "version": "1.6.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.5.0",
17
- "@vltpkg/cache-unzip": "1.5.0",
18
- "@vltpkg/error-cause": "1.5.0",
19
- "@vltpkg/keychain": "1.5.0",
20
- "@vltpkg/output": "1.5.0",
21
- "@vltpkg/types": "1.5.0",
22
- "@vltpkg/url-open": "1.5.0",
23
- "@vltpkg/user-agent": "1.5.0",
24
- "@vltpkg/xdg": "1.5.0",
16
+ "@vltpkg/cache": "1.6.1",
17
+ "@vltpkg/cache-unzip": "1.6.1",
18
+ "@vltpkg/error-cause": "1.6.1",
19
+ "@vltpkg/keychain": "1.6.1",
20
+ "@vltpkg/output": "1.6.1",
21
+ "@vltpkg/types": "1.6.1",
22
+ "@vltpkg/url-open": "1.6.1",
23
+ "@vltpkg/user-agent": "1.6.1",
24
+ "@vltpkg/xdg": "1.6.1",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },