@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/package.json +4 -5
- package/types/index.d.ts +17 -1
- package/types/index.d.ts.map +1 -1
- package/undici.d.ts +18 -47
- package/undici.d.ts.map +1 -1
- package/undici.js +191 -475
- package/undici.js.map +1 -1
- package/utils/decompress.d.ts +6 -5
- package/utils/decompress.d.ts.map +1 -1
- package/utils/decompress.js +127 -40
- package/utils/decompress.js.map +1 -1
- package/utils/helpers.d.ts +14 -0
- package/utils/helpers.d.ts.map +1 -0
- package/utils/helpers.js +95 -0
- package/utils/helpers.js.map +1 -0
- package/utils/helper.d.ts +0 -13
- package/utils/helper.d.ts.map +0 -1
- package/utils/helper.js +0 -103
- package/utils/helper.js.map +0 -1
package/undici.js
CHANGED
|
@@ -1,539 +1,255 @@
|
|
|
1
1
|
import { Pool, Dispatcher } from "undici";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
245
|
-
|
|
246
|
-
this.
|
|
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
|
-
|
|
251
|
-
if (this.
|
|
252
|
-
return
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
return
|
|
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
|
-
|
|
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(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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.
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
335
|
-
pipelining:
|
|
336
|
-
keepAliveTimeout:
|
|
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 ??
|
|
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
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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(
|
|
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:
|
|
213
|
+
path: parsed.path,
|
|
493
214
|
method: req.method,
|
|
494
215
|
headers: req.headers,
|
|
495
216
|
body,
|
|
496
|
-
}, new UndiciDispatchHandler(resolve, reject, fullUrl
|
|
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
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|