@vltpkg/registry-client 1.3.8 → 1.4.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/dist/index.d.ts CHANGED
@@ -70,6 +70,8 @@ export type RegistryClientOptions = RegistryURLs & {
70
70
  * explicitly allowed by the server's `cache-control` header.
71
71
  */
72
72
  'stale-while-revalidate-factor'?: number;
73
+ /** aborts every request this client makes */
74
+ signal?: AbortSignal;
73
75
  };
74
76
  export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'method' | 'path'> & {
75
77
  /**
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ import { randomUUID } from 'node:crypto';
9
9
  import { closeSync, openSync, readFileSync, readSync } from 'node:fs';
10
10
  import { availableParallelism } from 'node:os';
11
11
  import { dirname, resolve } from 'node:path';
12
+ import { setMaxListeners } from 'node:events';
12
13
  import { pipeline } from 'node:stream';
13
14
  import { setTimeout } from 'node:timers/promises';
14
15
  import { createGunzip } from 'node:zlib';
@@ -28,6 +29,7 @@ import { setCacheHeaders } from "./set-cache-headers.js";
28
29
  import { getTokenResponse } from "./token-response.js";
29
30
  import { getWebAuthChallenge } from "./web-auth-challenge.js";
30
31
  import { collectHeaders, readBody } from "./response.js";
32
+ import { retry } from "./retry.js";
31
33
  import { storeRoot } from "./store-root.js";
32
34
  import { oidc } from "./oidc.js";
33
35
  // eslint-disable-next-line no-console
@@ -95,7 +97,19 @@ export class RegistryClient {
95
97
  #decoded = new WeakMap();
96
98
  // request() calls not settled yet, awaited by drain()
97
99
  #inFlight = new Set();
100
+ #signal;
101
+ /** the request's own signal joined with the client's */
102
+ #withSignal(own) {
103
+ const c = this.#signal;
104
+ return !c || own === c || !(own instanceof AbortSignal) ?
105
+ (own ?? c)
106
+ : AbortSignal.any([own, c]);
107
+ }
98
108
  constructor(options) {
109
+ this.#signal = options.signal;
110
+ // undici adds a listener per request in flight
111
+ if (options.signal)
112
+ setMaxListeners(0, options.signal);
99
113
  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
100
114
  } = options;
101
115
  this.identity = identity;
@@ -121,6 +135,7 @@ export class RegistryClient {
121
135
  minTimeout,
122
136
  maxTimeout,
123
137
  retryAfter: true,
138
+ retry,
124
139
  errorCodes: [
125
140
  'ECONNREFUSED',
126
141
  'ECONNRESET',
@@ -529,6 +544,7 @@ export class RegistryClient {
529
544
  const u = typeof url === 'string' ? new URL(url) : url;
530
545
  const o = {
531
546
  ...options,
547
+ signal: this.#withSignal(options.signal),
532
548
  method: options.method ?? 'GET',
533
549
  path: u.pathname.replace(/\/+$/, '') + u.search,
534
550
  origin: u.origin,
@@ -566,6 +582,7 @@ export class RegistryClient {
566
582
  } while (this.#inFlight.size);
567
583
  }
568
584
  async request(url, options = {}) {
585
+ options.signal = this.#withSignal(options.signal);
569
586
  // tracked apart from the #request() promise: a second reaction on
570
587
  // that one would make V8 drop the caller's async frames from the
571
588
  // stack of any error #request() throws. redirect and otp hops call
@@ -0,0 +1,6 @@
1
+ import type { RetryHandler } from 'undici';
2
+ /**
3
+ * undici's default retry policy, but aborting the request ends the wait
4
+ * between attempts, so a long Retry-After can't keep the process alive
5
+ */
6
+ export declare const retry: RetryHandler.RetryCallback;
package/dist/retry.js ADDED
@@ -0,0 +1,30 @@
1
+ import { setTimeout } from 'node:timers/promises';
2
+ /**
3
+ * undici's default retry policy, but aborting the request ends the wait
4
+ * between attempts, so a long Retry-After can't keep the process alive
5
+ */
6
+ export const retry = (err, { state: { counter }, opts }, cb) => {
7
+ const { code, statusCode, headers } = err;
8
+ const { method } = opts;
9
+ // request options, which the dispatch options type leaves out
10
+ const { signal } = opts;
11
+ // undici fills in every default
12
+ const { maxRetries, minTimeout, maxTimeout, timeoutFactor, methods, statusCodes, errorCodes, } = opts.retryOptions;
13
+ if ((code &&
14
+ code !== 'UND_ERR_REQ_RETRY' &&
15
+ !errorCodes.includes(code)) ||
16
+ !methods.includes(method) ||
17
+ (typeof statusCode === 'number' &&
18
+ !statusCodes.includes(statusCode)) ||
19
+ counter > maxRetries) {
20
+ cb(err);
21
+ return;
22
+ }
23
+ const ra = String(headers?.['retry-after'] ?? '');
24
+ const secs = Number(ra);
25
+ const after = Number.isNaN(secs) ? Date.parse(ra) - Date.now() : secs * 1000;
26
+ const wait = Math.min(after > 0 ? after : minTimeout * timeoutFactor ** (counter - 1), maxTimeout);
27
+ setTimeout(wait, null, {
28
+ signal: signal instanceof AbortSignal ? signal : undefined,
29
+ }).then(() => cb(null), (er) => cb(er));
30
+ };
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.8",
4
+ "version": "1.4.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.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",
16
+ "@vltpkg/cache": "1.4.0",
17
+ "@vltpkg/cache-unzip": "1.4.0",
18
+ "@vltpkg/error-cause": "1.4.0",
19
+ "@vltpkg/keychain": "1.4.0",
20
+ "@vltpkg/output": "1.4.0",
21
+ "@vltpkg/types": "1.4.0",
22
+ "@vltpkg/url-open": "1.4.0",
23
+ "@vltpkg/user-agent": "1.4.0",
24
+ "@vltpkg/xdg": "1.4.0",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },