@hyperttp/transport-undici 0.1.7 → 0.2.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/undici.js CHANGED
@@ -1,539 +1,255 @@
1
1
  import { Pool, Dispatcher } from "undici";
2
- import { decodeBodyByEncoding } from "./utils/decompress.js";
3
- import { ReadableStream } from "stream/web";
4
- const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
5
- const DEFAULT_RETRY_STATUS_CODES = [502, 503, 504];
6
- const RETRYABLE_ERROR_CODES = new Set([
7
- "ECONNREFUSED",
8
- "ETIMEDOUT",
9
- "ECONNRESET",
10
- "EPIPE",
11
- "UND_ERR_SOCKET",
12
- ]);
13
- const IDENTITY_HEADERS_TO_DROP_ON_BODYLESS_REDIRECT = new Set([
14
- "content-type",
15
- "content-length",
16
- "transfer-encoding",
17
- "expect",
18
- ]);
19
- const AUTH_HEADERS_TO_DROP_ON_CROSS_ORIGIN_REDIRECT = new Set([
20
- "authorization",
21
- "proxy-authorization",
22
- "cookie",
23
- "cookie2",
24
- ]);
25
- function toOrigin(url) {
26
- return new URL(url, "http://localhost").origin;
27
- }
28
- export function isRedirect(status) {
29
- return REDIRECT_STATUS_CODES.has(status);
30
- }
31
- export function shouldRetry(status, retryOptions) {
32
- const codes = retryOptions.retryStatusCodes;
33
- if (codes && codes.length > 0) {
34
- return codes.includes(status);
35
- }
36
- return DEFAULT_RETRY_STATUS_CODES.includes(status);
37
- }
38
- export function calcDelay(attempt, retryOptions) {
39
- const { baseDelay = 1000, maxDelay = 10000, jitter = true } = retryOptions;
40
- const base = Math.min(baseDelay * 2 ** attempt, maxDelay);
41
- return jitter ? base * (0.75 + Math.random() * 0.5) : base;
42
- }
43
- function isReadableStreamLike(value) {
44
- return (!!value &&
45
- typeof value === "object" &&
46
- typeof value.getReader === "function");
47
- }
48
- function isAsyncIterable(value) {
49
- if (value == null || typeof value !== "object")
50
- return false;
51
- const asyncIterator = Reflect.get(value, Symbol.asyncIterator);
52
- return typeof asyncIterator === "function";
53
- }
54
- function isReplayableBody(body) {
55
- if (body === undefined || body === null)
56
- return true;
57
- if (typeof body === "string")
58
- return true;
59
- if (body instanceof Buffer)
60
- return true;
61
- if (body instanceof Uint8Array)
62
- return true;
63
- if (body instanceof ArrayBuffer)
64
- return true;
65
- if (ArrayBuffer.isView(body))
66
- return true;
67
- if (typeof URLSearchParams !== "undefined" &&
68
- body instanceof URLSearchParams) {
69
- return true;
2
+ import { abortError, createPoolOptions, fastParseUrl, normalizeHeaders } from "./utils/helpers.js";
3
+ import { createDecompressStream } from "./utils/decompress.js";
4
+ const DEFAULT_BASE_URL = "http://localhost:3000";
5
+ const FALLBACK_ORIGIN = "http://localhost";
6
+ export class UndiciDispatchHandler {
7
+ resolve;
8
+ reject;
9
+ url;
10
+ signal;
11
+ undiciController = null;
12
+ streamController = null;
13
+ onAbortRef;
14
+ isResolved = false;
15
+ finished = false;
16
+ constructor(resolve, reject, url, signal) {
17
+ this.resolve = resolve;
18
+ this.reject = reject;
19
+ this.url = url;
20
+ this.signal = signal;
70
21
  }
71
- if (typeof Blob !== "undefined" && body instanceof Blob)
72
- return true;
73
- if (typeof FormData !== "undefined" && body instanceof FormData)
74
- return true;
75
- if (isReadableStreamLike(body))
76
- return false;
77
- if (isAsyncIterable(body))
78
- return false;
79
- if (typeof body === "function")
80
- return false;
81
- return true;
82
- }
83
- function isBodyAllowedForRetry(method, body) {
84
- if (body === undefined || body === null)
85
- return true;
86
- return isReplayableBody(body) || method === "GET" || method === "HEAD";
87
- }
88
- function isBodyAllowedForRedirect(method, body) {
89
- if (body === undefined || body === null)
90
- return true;
91
- return isReplayableBody(body) || method === "GET" || method === "HEAD";
92
- }
93
- export async function drainBody(body) {
94
- if (!body || typeof body !== "object")
95
- return;
96
- try {
97
- const stream = body;
98
- if (typeof stream.dump === "function") {
99
- await stream.dump();
22
+ onRequestStart(controller) {
23
+ this.undiciController = controller;
24
+ const signal = this.signal;
25
+ if (!signal)
100
26
  return;
101
- }
102
- if (typeof stream.cancel === "function") {
103
- await stream.cancel();
104
- return;
105
- }
106
- if (typeof stream.resume === "function") {
107
- stream.resume();
27
+ if (signal.aborted) {
28
+ controller.abort(abortError(signal.reason));
108
29
  return;
109
30
  }
110
- if (typeof stream.destroy === "function") {
111
- stream.destroy();
112
- }
113
- }
114
- catch {
115
- //
116
- }
117
- }
118
- export function sleep(ms, signal) {
119
- if (ms <= 0)
120
- return Promise.resolve();
121
- return new Promise((resolve, reject) => {
122
- const timer = setTimeout(() => {
123
- cleanup();
124
- resolve();
125
- }, ms);
126
- const onAbort = () => {
127
- cleanup();
128
- const err = new Error("The operation was aborted.");
129
- err.name = "AbortError";
130
- reject(err);
131
- };
132
- const cleanup = () => {
133
- clearTimeout(timer);
134
- signal?.removeEventListener("abort", onAbort);
135
- };
136
- if (signal) {
137
- if (signal.aborted) {
138
- cleanup();
139
- const err = new Error("The operation was aborted.");
140
- err.name = "AbortError";
141
- reject(err);
142
- return;
31
+ this.onAbortRef = () => {
32
+ const err = abortError(signal.reason);
33
+ controller.abort(err);
34
+ const stream = this.streamController;
35
+ if (stream) {
36
+ try {
37
+ stream.error(err);
38
+ }
39
+ catch { }
143
40
  }
144
- signal.addEventListener("abort", onAbort, { once: true });
145
- }
146
- });
147
- }
148
- export function combineSignal(signal, timeoutMs) {
149
- if (timeoutMs <= 0) {
150
- return {
151
- signal,
152
- cancelTimer: () => { },
153
- cleanup: () => { },
154
- isTimeoutAbort: () => false,
155
- };
156
- }
157
- const timeoutController = new AbortController();
158
- const timer = setTimeout(() => timeoutController.abort(), timeoutMs);
159
- if (!signal) {
160
- return {
161
- signal: timeoutController.signal,
162
- cancelTimer: () => clearTimeout(timer),
163
- cleanup: () => clearTimeout(timer),
164
- isTimeoutAbort: () => timeoutController.signal.aborted,
165
41
  };
42
+ signal.addEventListener("abort", this.onAbortRef, { once: true });
166
43
  }
167
- if (typeof AbortSignal.any === "function") {
168
- const combined = AbortSignal.any([signal, timeoutController.signal]);
169
- return {
170
- signal: combined,
171
- cancelTimer: () => clearTimeout(timer),
172
- cleanup: () => clearTimeout(timer),
173
- isTimeoutAbort: () => timeoutController.signal.aborted,
174
- };
175
- }
176
- const controller = new AbortController();
177
- const abortFromUser = () => {
178
- if (!controller.signal.aborted)
179
- controller.abort();
180
- };
181
- const abortFromTimeout = () => {
182
- if (!controller.signal.aborted)
183
- controller.abort();
184
- };
185
- if (signal.aborted) {
186
- abortFromUser();
187
- }
188
- else {
189
- signal.addEventListener("abort", abortFromUser, { once: true });
190
- }
191
- timeoutController.signal.addEventListener("abort", abortFromTimeout, {
192
- once: true,
193
- });
194
- return {
195
- signal: controller.signal,
196
- cancelTimer: () => clearTimeout(timer),
197
- cleanup: () => {
198
- clearTimeout(timer);
199
- signal.removeEventListener("abort", abortFromUser);
200
- timeoutController.signal.removeEventListener("abort", abortFromTimeout);
201
- },
202
- isTimeoutAbort: () => timeoutController.signal.aborted,
44
+ dumpBody = async () => {
45
+ this.cleanup();
46
+ const controller = this.undiciController;
47
+ if (controller) {
48
+ controller.abort(new Error("Stream dumped"));
49
+ }
50
+ const streamCtrl = this.streamController;
51
+ if (streamCtrl && !this.finished) {
52
+ this.finished = true;
53
+ try {
54
+ streamCtrl.close();
55
+ }
56
+ catch { }
57
+ }
58
+ this.release();
203
59
  };
204
- }
205
- class UndiciTransportResponse {
206
- status;
207
- headers;
208
- url;
209
- body;
210
- _rawBody;
211
- _cachedText;
212
- _cachedJson;
213
- _cachedJsonReady = false;
214
- _cachedWebStream;
215
- constructor(result) {
216
- this.status = result.status;
217
- this.headers = result.headers;
218
- this.url = result.url;
219
- this._rawBody = decodeBodyByEncoding(result.body, result.headers);
220
- this.text = this.text.bind(this);
221
- this.json = this.json.bind(this);
222
- this.dump = this.dump.bind(this);
223
- const stream = new ReadableStream({
224
- start: (controller) => {
225
- controller.enqueue(new Uint8Array(this._rawBody.buffer, this._rawBody.byteOffset, this._rawBody.byteLength));
226
- controller.close();
60
+ onResponseStart(_controller, statusCode, headers) {
61
+ this.isResolved = true;
62
+ const normalizedHeaders = normalizeHeaders(headers);
63
+ const rawStream = new ReadableStream({
64
+ start: (ctrl) => {
65
+ this.streamController = ctrl;
66
+ const signal = this.signal;
67
+ if (signal?.aborted) {
68
+ const err = abortError(signal.reason);
69
+ try {
70
+ ctrl.error(err);
71
+ }
72
+ catch { }
73
+ }
74
+ },
75
+ cancel: (reason) => {
76
+ this.cleanup();
77
+ const controller = this.undiciController;
78
+ if (controller) {
79
+ controller.abort(reason instanceof Error ? reason : abortError(reason));
80
+ }
81
+ this.release();
227
82
  },
228
83
  });
229
- const bodyPayload = stream;
230
- Object.defineProperty(bodyPayload, "dump", {
231
- value: this.dump.bind(this),
232
- writable: false,
233
- enumerable: false,
234
- configurable: true,
235
- });
236
- this._cachedWebStream = bodyPayload;
237
- this.body = bodyPayload;
238
- }
239
- async dump() {
240
- if (this.body && typeof this.body.cancel === "function") {
241
- await this.body.cancel();
84
+ const ce = normalizedHeaders["content-encoding"] || normalizedHeaders["Content-Encoding"];
85
+ const encoding = Array.isArray(ce) ? ce[0] : ce;
86
+ if (encoding) {
87
+ createDecompressStream(rawStream, encoding)
88
+ .then((decompressedStream) => {
89
+ delete normalizedHeaders["content-encoding"];
90
+ delete normalizedHeaders["Content-Encoding"];
91
+ this.resolve({
92
+ status: statusCode,
93
+ headers: normalizedHeaders,
94
+ url: this.url,
95
+ body: decompressedStream,
96
+ });
97
+ })
98
+ .catch((err) => {
99
+ if (this.streamController) {
100
+ try {
101
+ this.streamController.error(err);
102
+ }
103
+ catch { }
104
+ }
105
+ this.reject(err);
106
+ });
242
107
  }
243
- }
244
- async text() {
245
- if (this._cachedText === undefined) {
246
- this._cachedText = this._rawBody.toString("utf-8");
108
+ else {
109
+ const payload = rawStream;
110
+ payload.dump = this.dumpBody;
111
+ this.resolve({
112
+ status: statusCode,
113
+ headers: normalizedHeaders,
114
+ url: this.url,
115
+ body: payload,
116
+ });
247
117
  }
248
- return this._cachedText;
249
118
  }
250
- async json() {
251
- if (this._cachedJsonReady) {
252
- return this._cachedJson;
253
- }
254
- const text = await this.text();
255
- if (!text.trim()) {
256
- this._cachedJson = null;
257
- this._cachedJsonReady = true;
258
- return null;
119
+ onResponseData(_controller, chunk) {
120
+ if (this.signal?.aborted)
121
+ return false;
122
+ const ctrl = this.streamController;
123
+ if (!ctrl)
124
+ return true;
125
+ try {
126
+ ctrl.enqueue(chunk);
127
+ return true;
259
128
  }
260
- this._cachedJson = JSON.parse(text);
261
- this._cachedJsonReady = true;
262
- return this._cachedJson;
263
- }
264
- }
265
- export class UndiciDispatchHandler {
266
- resolve;
267
- reject;
268
- url;
269
- signal;
270
- statusCode = 200;
271
- headers = Object.create(null);
272
- chunks = [];
273
- constructor(resolve, reject, url, signal) {
274
- this.resolve = resolve;
275
- this.reject = reject;
276
- this.url = url;
277
- this.signal = signal;
278
- }
279
- onRequestStart(_controller, _context) { }
280
- onResponseStart(_controller, statusCode, headers, _statusMessage) {
281
- this.statusCode = statusCode;
282
- this.headers = headers;
283
- }
284
- onResponseData(controller, chunk) {
285
- if (this.signal?.aborted) {
286
- const abortError = new Error("The operation was aborted.");
287
- abortError.name = "AbortError";
288
- controller.abort(abortError);
129
+ catch {
289
130
  return false;
290
131
  }
291
- this.chunks.push(Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength));
292
- return true;
293
132
  }
294
- onResponseEnd(_controller, _trailers) {
295
- const body = Buffer.concat(this.chunks);
296
- this.resolve({
297
- status: this.statusCode,
298
- headers: this.headers,
299
- body,
300
- url: this.url,
301
- });
133
+ onResponseEnd() {
134
+ if (this.finished)
135
+ return;
136
+ this.finished = true;
137
+ this.cleanup();
138
+ const ctrl = this.streamController;
139
+ if (ctrl) {
140
+ try {
141
+ ctrl.close();
142
+ }
143
+ catch { }
144
+ }
145
+ this.release();
302
146
  }
303
147
  onResponseError(_controller, error) {
304
- this.reject(error);
305
- }
306
- }
307
- function stripHeadersOnRedirect(headers, bodyless, crossOrigin) {
308
- const next = Object.create(null);
309
- for (const [key, value] of Object.entries(headers)) {
310
- const lower = key.toLowerCase();
311
- if (bodyless && IDENTITY_HEADERS_TO_DROP_ON_BODYLESS_REDIRECT.has(lower)) {
312
- continue;
148
+ if (this.finished)
149
+ return;
150
+ this.finished = true;
151
+ this.cleanup();
152
+ if (!this.isResolved) {
153
+ this.reject(error);
154
+ this.release();
155
+ return;
313
156
  }
314
- if (crossOrigin &&
315
- AUTH_HEADERS_TO_DROP_ON_CROSS_ORIGIN_REDIRECT.has(lower)) {
316
- continue;
157
+ const ctrl = this.streamController;
158
+ if (ctrl) {
159
+ try {
160
+ ctrl.error(error);
161
+ }
162
+ catch { }
163
+ }
164
+ this.release();
165
+ }
166
+ cleanup() {
167
+ const signal = this.signal;
168
+ const abortRef = this.onAbortRef;
169
+ if (signal && abortRef) {
170
+ signal.removeEventListener("abort", abortRef);
317
171
  }
318
- next[lower] = Array.isArray(value) ? value.join(", ") : value;
172
+ this.onAbortRef = undefined;
173
+ }
174
+ release() {
175
+ this.undiciController = null;
176
+ this.streamController = null;
177
+ this.onAbortRef = undefined;
319
178
  }
320
- return next;
321
179
  }
322
180
  export class UndiciTransport {
323
181
  config;
324
182
  pool;
325
183
  isExternal;
326
184
  pools = new Map();
185
+ poolOptions;
327
186
  constructor(config) {
328
187
  this.config = config;
329
188
  this.isExternal = config.dispatcher !== undefined;
330
- const origin = toOrigin(this.baseUrl);
189
+ this.poolOptions = createPoolOptions(config);
190
+ const parsed = fastParseUrl(this.baseUrl, FALLBACK_ORIGIN);
331
191
  this.pool =
332
192
  config.dispatcher ??
333
- new Pool(origin, {
334
- connections: config.network?.maxConcurrent ?? 500,
335
- pipelining: config.network?.pipelining ?? 8,
336
- keepAliveTimeout: config.network?.keepAliveTimeout ?? 30000,
193
+ new Pool(parsed.origin, {
194
+ connections: this.poolOptions.connections,
195
+ pipelining: this.poolOptions.pipelining,
196
+ keepAliveTimeout: this.poolOptions.keepAliveTimeout,
337
197
  });
338
198
  }
339
199
  get baseUrl() {
340
- return this.config.baseUrl ?? "http://localhost:3000";
341
- }
342
- get timeout() {
343
- return this.config.network?.timeout ?? 30000;
344
- }
345
- get followRedirects() {
346
- return this.config.network?.followRedirects ?? true;
347
- }
348
- get maxRedirects() {
349
- return this.config.network?.maxRedirects ?? 5;
350
- }
351
- get retryOptions() {
352
- return this.config.retry ?? {};
353
- }
354
- get maxRetries() {
355
- return this.config.retry?.maxRetries ?? 3;
200
+ return this.config.baseUrl ?? DEFAULT_BASE_URL;
356
201
  }
357
202
  async execute(req) {
358
- return this.executeWithPolicy({
359
- url: req.url,
360
- method: req.method,
361
- headers: req.headers,
362
- body: req.body,
363
- signal: req.signal,
364
- });
365
- }
366
- async executeWithPolicy(req) {
367
- let currentUrl = req.url;
368
- let currentMethod = req.method;
369
- let currentHeaders = req.headers;
370
- let currentBody = currentMethod === "GET" || currentMethod === "HEAD"
371
- ? undefined
372
- : req.body;
373
- let redirects = 0;
374
- let attempt = 0;
375
- const { signal, cancelTimer, cleanup, isTimeoutAbort } = combineSignal(req.signal, this.timeout);
376
- try {
377
- while (true) {
378
- const startedAt = Date.now();
379
- try {
380
- const result = await this.dispatchOnce({
381
- ...req,
382
- url: currentUrl,
383
- method: currentMethod,
384
- headers: currentHeaders,
385
- body: currentBody,
386
- signal,
387
- });
388
- if (this.followRedirects && isRedirect(result.status)) {
389
- if (redirects >= this.maxRedirects) {
390
- throw new Error("Too many redirects");
391
- }
392
- const location = result.headers.location;
393
- if (location) {
394
- const nextUrl = new URL(location, result.url).toString();
395
- const nextOrigin = new URL(nextUrl).origin;
396
- const currentOrigin = new URL(result.url).origin;
397
- let nextMethod = currentMethod;
398
- let nextBody = currentBody;
399
- let bodylessRedirect = false;
400
- if (result.status === 303 ||
401
- ((result.status === 301 || result.status === 302) &&
402
- currentMethod === "POST")) {
403
- nextMethod = "GET";
404
- nextBody = undefined;
405
- bodylessRedirect = true;
406
- }
407
- if (nextMethod !== "GET" && nextMethod !== "HEAD") {
408
- if (!isBodyAllowedForRedirect(nextMethod, nextBody)) {
409
- throw new Error("Cannot resend non-replayable request body after redirect");
410
- }
411
- }
412
- currentUrl = nextUrl;
413
- currentMethod = nextMethod;
414
- currentBody = nextBody;
415
- currentHeaders = stripHeadersOnRedirect(currentHeaders, bodylessRedirect ||
416
- nextMethod === "GET" ||
417
- nextMethod === "HEAD", nextOrigin !== currentOrigin);
418
- redirects += 1;
419
- await drainBody(result.body);
420
- continue;
421
- }
422
- }
423
- if (shouldRetry(result.status, this.retryOptions)) {
424
- if (attempt < this.maxRetries &&
425
- isBodyAllowedForRetry(currentMethod, currentBody)) {
426
- await drainBody(result.body);
427
- const delay = calcDelay(attempt, this.retryOptions);
428
- const elapsed = Date.now() - startedAt;
429
- const remainingDelay = Math.max(0, delay - elapsed);
430
- await sleep(remainingDelay, signal);
431
- attempt += 1;
432
- continue;
433
- }
434
- }
435
- return this.createResponse(result);
436
- }
437
- catch (err) {
438
- if (this.isAbortError(err)) {
439
- if (req.signal?.aborted) {
440
- throw err;
441
- }
442
- if (isTimeoutAbort()) {
443
- throw new Error(`Request timeout after ${this.timeout}ms`, {
444
- cause: err,
445
- });
446
- }
447
- throw new Error("Transport closed or aborted", { cause: err });
448
- }
449
- const code = this.getErrorCode(err);
450
- if (attempt < this.maxRetries &&
451
- code &&
452
- RETRYABLE_ERROR_CODES.has(code) &&
453
- isBodyAllowedForRetry(currentMethod, currentBody)) {
454
- const delay = calcDelay(attempt, this.retryOptions);
455
- await sleep(delay, signal);
456
- attempt += 1;
457
- continue;
458
- }
459
- throw err;
460
- }
461
- }
462
- }
463
- finally {
464
- cancelTimer();
465
- cleanup();
203
+ const signal = req.signal;
204
+ if (signal?.aborted) {
205
+ throw abortError(signal.reason);
466
206
  }
467
- }
468
- getPool(origin) {
469
- const existing = this.pools.get(origin);
470
- if (existing)
471
- return existing;
472
- const pool = new Pool(origin, {
473
- connections: this.config.network?.maxConcurrent ?? 500,
474
- pipelining: this.config.network?.pipelining ?? 8,
475
- keepAliveTimeout: this.config.network?.keepAliveTimeout ?? 30000,
476
- });
477
- this.pools.set(origin, pool);
478
- return pool;
479
- }
480
- async dispatchOnce(req) {
481
- const fullUrl = new URL(req.url, this.baseUrl);
207
+ const parsed = fastParseUrl(req.url, this.baseUrl);
482
208
  const body = (req.body ?? null);
483
- const pool = this.isExternal ? this.pool : this.getPool(fullUrl.origin);
484
- if (req.signal?.aborted) {
485
- const abortError = new Error("The operation was aborted.");
486
- abortError.name = "AbortError";
487
- throw abortError;
488
- }
209
+ const pool = this.isExternal ? this.pool : this.getPool(parsed.origin);
489
210
  return new Promise((resolve, reject) => {
490
211
  try {
491
212
  pool.dispatch({
492
- path: fullUrl.pathname + fullUrl.search,
213
+ path: parsed.path,
493
214
  method: req.method,
494
215
  headers: req.headers,
495
216
  body,
496
- }, new UndiciDispatchHandler(resolve, reject, fullUrl.toString(), req.signal));
217
+ }, new UndiciDispatchHandler(resolve, reject, parsed.fullUrl, signal));
497
218
  }
498
219
  catch (error) {
499
220
  reject(error instanceof Error ? error : new Error(String(error)));
500
221
  }
501
222
  });
502
223
  }
503
- createResponse(result) {
504
- return new UndiciTransportResponse(result);
505
- }
506
- isAbortError(err) {
507
- if (!err || typeof err !== "object")
508
- return false;
509
- const name = err.name;
510
- const code = err.code;
511
- const cause = err.cause;
512
- return (name === "AbortError" ||
513
- code === "UND_ERR_HEADERS_TIMEOUT" ||
514
- code === "UND_ERR_BODY_TIMEOUT" ||
515
- (cause !== undefined && this.isAbortError(cause)));
516
- }
517
- getErrorCode(err) {
518
- if (!err || typeof err !== "object")
519
- return undefined;
520
- const code = err.code;
521
- if (typeof code === "string")
522
- return code;
523
- const cause = err.cause;
524
- if (cause !== undefined)
525
- return this.getErrorCode(cause);
526
- return undefined;
224
+ getPool(origin) {
225
+ const existing = this.pools.get(origin);
226
+ if (existing)
227
+ return existing;
228
+ const created = new Pool(origin, {
229
+ connections: this.poolOptions.connections,
230
+ pipelining: this.poolOptions.pipelining,
231
+ keepAliveTimeout: this.poolOptions.keepAliveTimeout,
232
+ });
233
+ this.pools.set(origin, created);
234
+ return created;
527
235
  }
528
236
  async close() {
529
237
  if (this.isExternal)
530
238
  return;
531
- await this.pool.close();
239
+ const promises = [this.pool.close()];
240
+ for (const p of this.pools.values())
241
+ promises.push(p.close());
242
+ this.pools.clear();
243
+ await Promise.all(promises);
532
244
  }
533
245
  async destroy() {
534
246
  if (this.isExternal)
535
247
  return;
536
- await this.pool.destroy();
248
+ const promises = [this.pool.destroy()];
249
+ for (const p of this.pools.values())
250
+ promises.push(p.destroy());
251
+ this.pools.clear();
252
+ await Promise.all(promises);
537
253
  }
538
254
  }
539
255
  //# sourceMappingURL=undici.js.map