@vltpkg/registry-client 1.0.0-rc.32 → 1.0.0-rc.34

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 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`
package/dist/index.js CHANGED
@@ -5,10 +5,11 @@ 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 { Agent, RetryAgent } from 'undici';
12
+ import { RetryAgent } from 'undici';
12
13
  import { addHeader } from "./add-header.js";
13
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";
@@ -16,6 +17,7 @@ import { register } from "./cache-revalidate.js";
16
17
  import { bun, deno, node } from "./env.js";
17
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";
@@ -52,6 +54,7 @@ export class RegistryClient {
52
54
  cache;
53
55
  identity;
54
56
  staleWhileRevalidateFactor;
57
+ #session = randomUUID();
55
58
  constructor(options) {
56
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
57
60
  } = options;
@@ -66,7 +69,7 @@ export class RegistryClient {
66
69
  }
67
70
  },
68
71
  });
69
- const dispatch = new Agent(agentOptions);
72
+ const dispatch = getDispatcher(agentOptions);
70
73
  this.agent = new RetryAgent(dispatch, {
71
74
  maxRetries,
72
75
  timeoutFactor,
@@ -220,16 +223,17 @@ export class RegistryClient {
220
223
  : undefined;
221
224
  const entry = buffer ? CacheEntry.decode(buffer) : undefined;
222
225
  if (entry?.valid) {
223
- logRequest(url, 'cache');
226
+ logRequest(url, 'cache', { method });
224
227
  return entry;
225
228
  }
226
229
  if (staleWhileRevalidate && entry?.staleWhileRevalidate && m) {
227
230
  // revalidate while returning the stale entry
228
231
  register(dirname(this.cache.path()), m, url);
229
- logRequest(url, 'stale');
232
+ logRequest(url, 'stale', { method });
230
233
  return entry;
231
234
  }
232
- logRequest(url, 'start');
235
+ logRequest(url, 'start', { method });
236
+ const requestStart = Date.now();
233
237
  // either no cache entry, or need to revalidate before use.
234
238
  setCacheHeaders(options, entry);
235
239
  redirections.add(String(url));
@@ -238,7 +242,7 @@ export class RegistryClient {
238
242
  ...agentOptions,
239
243
  });
240
244
  options.origin = u.origin;
241
- 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);
242
246
  if (otp) {
243
247
  options.headers = addHeader(options.headers, 'npm-otp', otp);
244
248
  }
@@ -264,6 +268,11 @@ export class RegistryClient {
264
268
  }
265
269
  /* c8 ignore stop */
266
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
+ });
267
276
  if (result.getHeader('integrity')) {
268
277
  trustIntegrity = true;
269
278
  }
package/dist/otplease.js CHANGED
@@ -2,6 +2,7 @@ import { error } from '@vltpkg/error-cause';
2
2
  import { getWebAuthChallenge } from "./web-auth-challenge.js";
3
3
  import { urlOpen } from '@vltpkg/url-open';
4
4
  import { createInterface } from 'node:readline/promises';
5
+ import { gunzipSync } from 'node:zlib';
5
6
  // eslint-disable-next-line no-console
6
7
  const log = (msg) => console.error(msg);
7
8
  const question = async (text) => {
@@ -14,6 +15,13 @@ const question = async (text) => {
14
15
  return answer;
15
16
  };
16
17
  const otpChallengeNotice = /^Open ([^ ]+) to use your security key for authentication or enter OTP from your authenticator app/i;
18
+ const responseBodyText = async (response) => {
19
+ const contentEncoding = String(response.headers['content-encoding'] ?? '');
20
+ if (/\bgzip\b/i.test(contentEncoding)) {
21
+ return gunzipSync(await response.body.arrayBuffer()).toString();
22
+ }
23
+ return await response.body.text();
24
+ };
17
25
  export const otplease = async (client, options, response) => {
18
26
  const waHeader = String(response.headers['www-authenticate'] ?? '');
19
27
  const wwwAuth = new Set(waHeader ? waHeader.toLowerCase().split(/,\s*/) : []);
@@ -24,7 +32,9 @@ export const otplease = async (client, options, response) => {
24
32
  }
25
33
  if (wwwAuth.has('otp')) {
26
34
  // do a web auth opener to get otp token
27
- const challenge = getWebAuthChallenge(await response.body.json().catch(() => null));
35
+ const challenge = getWebAuthChallenge(await responseBodyText(response)
36
+ .then(text => JSON.parse(text))
37
+ .catch(() => null));
28
38
  if (challenge) {
29
39
  return {
30
40
  retry: {
@@ -61,7 +71,7 @@ export const otplease = async (client, options, response) => {
61
71
  // Consume the body to check if it's prompting for OTP.
62
72
  // We must return the consumed text so the caller doesn't try to
63
73
  // re-read from the already-drained stream.
64
- const text = await response.body.text().catch(() => '');
74
+ const text = await responseBodyText(response).catch(() => '');
65
75
  if (text.toLowerCase().includes('one-time pass')) {
66
76
  return {
67
77
  retry: {
@@ -0,0 +1,14 @@
1
+ import type { Dispatcher } from 'undici';
2
+ import { Agent } from 'undici';
3
+ /**
4
+ * Build the dispatcher that all registry traffic goes through.
5
+ *
6
+ * With no proxy in the environment this is the same plain {@link Agent}
7
+ * vlt has always used, so the default path is unchanged. When
8
+ * `http_proxy` or `https_proxy` is set, undici's
9
+ * {@link EnvHttpProxyAgent} takes over. It tunnels proxied origins with
10
+ * `CONNECT`, still dials `no_proxy` origins directly, and receives the
11
+ * identical options, so timeouts, keepalive, and pooling are the same
12
+ * either way.
13
+ */
14
+ export declare const getDispatcher: (options: Agent.Options) => Dispatcher;
package/dist/proxy.js ADDED
@@ -0,0 +1,18 @@
1
+ import proc from 'node:process';
2
+ import { Agent, EnvHttpProxyAgent } from 'undici';
3
+ // Matches how undici's EnvHttpProxyAgent reads these: lowercase wins
4
+ // over uppercase, and an empty string counts as unset.
5
+ const hasProxyEnv = () => !!(proc.env.http_proxy ?? proc.env.HTTP_PROXY) ||
6
+ !!(proc.env.https_proxy ?? proc.env.HTTPS_PROXY);
7
+ /**
8
+ * Build the dispatcher that all registry traffic goes through.
9
+ *
10
+ * With no proxy in the environment this is the same plain {@link Agent}
11
+ * vlt has always used, so the default path is unchanged. When
12
+ * `http_proxy` or `https_proxy` is set, undici's
13
+ * {@link EnvHttpProxyAgent} takes over. It tunnels proxied origins with
14
+ * `CONNECT`, still dials `no_proxy` origins directly, and receives the
15
+ * identical options, so timeouts, keepalive, and pooling are the same
16
+ * either way.
17
+ */
18
+ export const getDispatcher = (options) => hasProxyEnv() ? new EnvHttpProxyAgent(options) : new Agent(options);
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.0.0-rc.32",
4
+ "version": "1.0.0-rc.34",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,14 +13,14 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/cache": "1.0.0-rc.32",
17
- "@vltpkg/cache-unzip": "1.0.0-rc.32",
18
- "@vltpkg/error-cause": "1.0.0-rc.32",
19
- "@vltpkg/keychain": "1.0.0-rc.32",
20
- "@vltpkg/output": "1.0.0-rc.32",
21
- "@vltpkg/types": "1.0.0-rc.32",
22
- "@vltpkg/url-open": "1.0.0-rc.32",
23
- "@vltpkg/xdg": "1.0.0-rc.32",
16
+ "@vltpkg/cache": "1.0.0-rc.34",
17
+ "@vltpkg/cache-unzip": "1.0.0-rc.34",
18
+ "@vltpkg/error-cause": "1.0.0-rc.34",
19
+ "@vltpkg/keychain": "1.0.0-rc.34",
20
+ "@vltpkg/output": "1.0.0-rc.34",
21
+ "@vltpkg/types": "1.0.0-rc.34",
22
+ "@vltpkg/url-open": "1.0.0-rc.34",
23
+ "@vltpkg/xdg": "1.0.0-rc.34",
24
24
  "cache-control-parser": "^2.0.6",
25
25
  "package-json-from-dist": "^1.0.1",
26
26
  "undici": "^7.16.0"
@@ -71,5 +71,12 @@
71
71
  },
72
72
  "files": [
73
73
  "dist"
74
- ]
74
+ ],
75
+ "bugs": [
76
+ {
77
+ "type": "link",
78
+ "url": "https://github.com/vltpkg/vltpkg/issues"
79
+ }
80
+ ],
81
+ "homepage": "https://github.com/vltpkg/vltpkg/tree/main/src/registry-client#readme"
75
82
  }