-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.ts
More file actions
556 lines (511 loc) · 16.7 KB
/
request.ts
File metadata and controls
556 lines (511 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* @fileoverview Core HTTP/HTTPS request loop.
*
* `httpRequest` is the main entry point — it wraps `httpRequestAttempt`
* with retry / exponential-backoff logic and an optional `onRetry`
* callback. `httpRequestAttempt` performs a single request and
* resolves an `HttpResponse` (the fetch-like wrapper) or rejects with
* an enriched `Error`. It also handles redirect chasing,
* stream-mode bodies, FormData duck-typed header merging, and the
* `maxResponseSize` guard.
*
* `readIncomingResponse` is the convertor for callers that already
* have a raw `IncomingMessage` (e.g. multipart uploads via
* `http.request()` directly) and want the same `HttpResponse` shape.
*
* `httpRequestAttempt` is exported (not private) per the
* `export-top-level-functions` lint rule — it is shared with
* `download.ts` which uses it in stream mode to drive
* `httpDownloadAttempt`.
*/
import { setTimeout as delay } from 'node:timers/promises'
import { SOCKET_LIB_USER_AGENT } from '../constants/socket'
import { BufferConcat } from '../primordials/buffer'
import { DateNow } from '../primordials/date'
import { ErrorCtor } from '../primordials/error'
import { JSONParse } from '../primordials/json'
import { MathMax } from '../primordials/math'
import { NumberIsNaN } from '../primordials/number'
import { PromiseCtor } from '../primordials/promise'
import { URLCtor } from '../primordials/url'
import { getHttp, getHttps } from './_internal'
import { enrichErrorMessage } from './errors'
import { HttpResponseError } from './types'
import type {
HttpHookResponseInfo,
HttpRequestOptions,
HttpResponse,
IncomingResponse,
} from './types'
/**
* Make an HTTP/HTTPS request with retry logic and redirect support.
* Provides a fetch-like API using Node.js native http/https modules.
*
* This is the main entry point for making HTTP requests. It handles retries,
* redirects, timeouts, and provides a fetch-compatible response interface.
*
* @param url - The URL to request (must start with http:// or https://)
* @param options - Request configuration options
* @returns Promise resolving to response object with `.json()`, `.text()`, etc.
* @throws {Error} When all retries are exhausted, timeout occurs, or non-retryable error happens
*
* @example
* ```ts
* // Simple GET request
* const response = await httpRequest('https://api.example.com/data')
* const data = response.json()
*
* // POST with JSON body
* const response = await httpRequest('https://api.example.com/users', {
* method: 'POST',
* headers: { 'Content-Type': 'application/json' },
* body: JSON.stringify({ name: 'Alice', email: 'alice@example.com' })
* })
*
* // With retries and timeout
* const response = await httpRequest('https://api.example.com/data', {
* retries: 3,
* retryDelay: 1000,
* timeout: 60000
* })
*
* // Don't follow redirects
* const response = await httpRequest('https://example.com/redirect', {
* followRedirects: false
* })
* console.log(response.status) // 301, 302, etc.
* ```
*/
export async function httpRequest(
url: string,
options?: HttpRequestOptions | undefined,
): Promise<HttpResponse> {
const {
body,
ca,
followRedirects = true,
headers = {},
hooks,
maxRedirects = 5,
maxResponseSize,
method = 'GET',
onRetry,
retries = 0,
retryDelay = 1000,
stream = false,
throwOnError = false,
timeout = 30_000,
} = { __proto__: null, ...options } as HttpRequestOptions
// Readable streams are one-shot — they cannot be replayed on retry or redirect.
// Duck-type check: streams have a `pipe` method.
const isStreamBody =
body !== undefined &&
typeof body === 'object' &&
typeof (body as { pipe?: unknown }).pipe === 'function'
if (isStreamBody && retries > 0) {
throw new ErrorCtor(
'Streaming body (Readable/FormData) cannot be used with retries. ' +
'Streams are consumed on first attempt and cannot be replayed. ' +
'Set retries: 0 or buffer the body as a string/Buffer.',
)
}
const attemptOpts: HttpRequestOptions = {
body,
ca,
// Disable redirect following for stream bodies — the stream is consumed
// on the first request and cannot be re-piped to the redirect target.
followRedirects: isStreamBody ? false : followRedirects,
headers,
hooks,
maxRedirects,
maxResponseSize,
method,
stream,
timeout,
}
// Retry logic with exponential backoff
let lastError: Error | undefined
for (let attempt = 0; attempt <= retries; attempt++) {
try {
// eslint-disable-next-line no-await-in-loop
const response = await httpRequestAttempt(url, attemptOpts)
// When throwOnError is enabled, non-2xx responses become errors
// so they can be retried or caught by callers.
if (throwOnError && !response.ok) {
throw new HttpResponseError(response)
}
return response
} catch (e) {
lastError = e as Error
// Last attempt - throw error
if (attempt === retries) {
break
}
// Consult onRetry callback if provided.
const delayMs = retryDelay * 2 ** attempt
if (onRetry) {
const retryResult = onRetry(attempt + 1, e, delayMs)
// false = stop retrying, rethrow immediately.
if (retryResult === false) {
break
}
// A number overrides the delay (clamped to >= 0; NaN falls back to default).
const actualDelay =
typeof retryResult === 'number' && !NumberIsNaN(retryResult)
? MathMax(0, retryResult)
: delayMs
// eslint-disable-next-line no-await-in-loop
await delay(actualDelay)
} else {
// Default: retry with exponential backoff
// eslint-disable-next-line no-await-in-loop
await delay(delayMs)
}
}
}
throw lastError || new ErrorCtor('Request failed after retries')
}
/**
* Single HTTP request attempt (used internally by httpRequest with retry logic).
* Supports hooks (fire per-attempt), maxResponseSize, and rawResponse.
*/
export async function httpRequestAttempt(
url: string,
options: HttpRequestOptions,
): Promise<HttpResponse> {
const {
body,
ca,
followRedirects = true,
headers = {},
hooks,
maxRedirects = 5,
maxResponseSize,
method = 'GET',
stream = false,
timeout = 30_000,
} = { __proto__: null, ...options } as HttpRequestOptions
const startTime = DateNow()
// Auto-merge FormData headers (Content-Type with boundary).
const streamHeaders =
body &&
typeof body === 'object' &&
'getHeaders' in body &&
typeof (body as { getHeaders?: unknown }).getHeaders === 'function'
? (body as { getHeaders: () => Record<string, string> }).getHeaders()
: undefined
const mergedHeaders = {
'User-Agent': SOCKET_LIB_USER_AGENT,
...streamHeaders,
...headers,
}
hooks?.onRequest?.({ method, url, headers: mergedHeaders, timeout })
return await new PromiseCtor((resolve, reject) => {
// Settled flag guards all resolve/reject paths so that at most one
// fires, even when destroy() cascades multiple events.
let settled = false
const resolveOnce = (response: HttpResponse) => {
// settled-already arm fires only on destroy() races where two
// events fire after the first. Defensive.
/* c8 ignore start */
if (settled) {
return
}
/* c8 ignore stop */
settled = true
resolve(response)
}
const rejectOnce = (err: Error) => {
/* c8 ignore start */
if (settled) {
return
}
/* c8 ignore stop */
settled = true
// Clean up streaming body if still active to avoid leaked descriptors.
if (
body &&
typeof body === 'object' &&
typeof (body as { destroy?: unknown }).destroy === 'function'
) {
;(body as { destroy: () => void }).destroy()
}
emitResponse({ error: err })
reject(err)
}
const parsedUrl = new URLCtor(url)
const isHttps = parsedUrl.protocol === 'https:'
const httpModule = isHttps ? getHttps() : getHttp()
const requestOptions: Record<string, unknown> = {
headers: mergedHeaders,
hostname: parsedUrl.hostname,
method,
path: parsedUrl.pathname + parsedUrl.search,
port: parsedUrl.port,
timeout,
}
// ca + isHttps both required; tested individually but not always paired.
/* c8 ignore next 3 */
if (ca && isHttps) {
requestOptions['ca'] = ca
}
const emitResponse = (info: Partial<HttpHookResponseInfo>) => {
try {
hooks?.onResponse?.({
duration: DateNow() - startTime,
method,
url,
...info,
})
} catch {
// User-provided hook threw — swallow to avoid leaving the promise pending.
}
}
/* c8 ignore start - External HTTP/HTTPS request */
const request = httpModule.request(
requestOptions,
(res: IncomingResponse) => {
if (
followRedirects &&
res.statusCode &&
res.statusCode >= 300 &&
res.statusCode < 400 &&
res.headers.location
) {
// Drain the redirect response body to free the socket.
res.resume()
emitResponse({
headers: res.headers,
status: res.statusCode,
statusText: res.statusMessage,
})
if (maxRedirects <= 0) {
// Hook already emitted above — reject directly to avoid double-fire.
settled = true
reject(
new Error(
`Too many redirects (exceeded maximum: ${maxRedirects})`,
),
)
return
}
const redirectUrl = res.headers.location.startsWith('http')
? res.headers.location
: new URL(res.headers.location, url).toString()
const redirectParsed = new URL(redirectUrl)
if (isHttps && redirectParsed.protocol !== 'https:') {
// Hook already emitted above — reject directly to avoid double-fire.
settled = true
reject(
new Error(
`Redirect from HTTPS to HTTP is not allowed: ${redirectUrl}`,
),
)
return
}
// Strip auth/session headers on cross-origin redirects to prevent
// leaking credentials to third-party hosts (e.g., GitHub -> S3).
let redirectHeaders = headers
if (new URL(url).origin !== redirectParsed.origin) {
redirectHeaders = { __proto__: null } as unknown as typeof headers
const stripped = new Set([
'authorization',
'cookie',
'proxy-authenticate',
'proxy-authorization',
])
for (const key of Object.keys(headers)) {
if (!stripped.has(key.toLowerCase())) {
;(redirectHeaders as Record<string, unknown>)[key] = (
headers as Record<string, unknown>
)[key]
}
}
}
// Redirect chaining — Promise adoption handles the inner result.
settled = true
resolve(
httpRequestAttempt(redirectUrl, {
body,
ca,
followRedirects,
headers: redirectHeaders,
hooks,
maxRedirects: maxRedirects - 1,
maxResponseSize,
method,
stream,
timeout,
}),
)
return
}
// Stream mode: resolve immediately with unconsumed response.
if (stream) {
const status = res.statusCode || 0
const statusText = res.statusMessage || ''
const ok = status >= 200 && status < 300
emitResponse({
headers: res.headers,
status,
statusText,
})
const emptyBody = Buffer.alloc(0)
resolveOnce({
arrayBuffer: () => emptyBody.buffer as ArrayBuffer,
body: emptyBody,
headers: res.headers,
json: () => {
throw new Error('Cannot parse JSON from a streaming response')
},
ok,
rawResponse: res,
status,
statusText,
text: () => '',
})
return
}
const chunks: Buffer[] = []
let totalBytes = 0
res.on('data', (chunk: Buffer) => {
totalBytes += chunk.length
if (maxResponseSize && totalBytes > maxResponseSize) {
res.destroy()
request.destroy()
const sizeMB = (totalBytes / (1024 * 1024)).toFixed(2)
const maxMB = (maxResponseSize / (1024 * 1024)).toFixed(2)
rejectOnce(
new Error(
`Response exceeds maximum size limit (${sizeMB}MB > ${maxMB}MB)`,
),
)
return
}
chunks.push(chunk)
})
res.on('end', () => {
if (settled) {
return
}
const responseBody = Buffer.concat(chunks)
const ok =
res.statusCode !== undefined &&
res.statusCode >= 200 &&
res.statusCode < 300
const response: HttpResponse = {
arrayBuffer(): ArrayBuffer {
return responseBody.buffer.slice(
responseBody.byteOffset,
responseBody.byteOffset + responseBody.byteLength,
)
},
body: responseBody,
headers: res.headers,
json<T = unknown>(): T {
return JSON.parse(responseBody.toString('utf8')) as T
},
ok,
rawResponse: res,
status: res.statusCode || 0,
statusText: res.statusMessage || '',
text(): string {
return responseBody.toString('utf8')
},
}
emitResponse({
headers: res.headers,
status: res.statusCode,
statusText: res.statusMessage,
})
resolveOnce(response)
})
res.on('error', (error: Error) => {
rejectOnce(error)
})
},
)
request.on('error', (error: Error) => {
const message = enrichErrorMessage(
url,
method,
error as NodeJS.ErrnoException,
)
rejectOnce(new Error(message, { cause: error }))
})
request.on('timeout', () => {
request.destroy()
rejectOnce(
new Error(
`${method} request timed out after ${timeout}ms: ${url}\n→ Server did not respond in time.\n→ Try: Increase timeout or check network connectivity.`,
),
)
})
if (body) {
// Duck-type: streams have a `pipe` method.
if (
typeof body === 'object' &&
typeof (body as { pipe?: unknown }).pipe === 'function'
) {
// Readable stream (including FormData) — pipe it.
// The error listener is cleaned up implicitly: on failure rejectOnce
// destroys the stream, and on success the stream is fully consumed.
// Both cases prevent further error events.
const stream = body as import('node:stream').Readable
stream.on('error', (err: Error) => {
request.destroy()
rejectOnce(err)
})
stream.pipe(request)
return
}
// String or Buffer.
request.write(body)
request.end()
} else {
request.end()
}
/* c8 ignore stop */
})
}
/**
* Read and buffer a client-side IncomingResponse into an HttpResponse.
*
* Useful when you have a raw response from code that bypasses
* `httpRequest()` (e.g., multipart form-data uploads via `http.request()`,
* or responses from third-party HTTP libraries) and need to convert it
* into the standard HttpResponse interface.
*
* @example
* ```typescript
* const raw = await makeRawRequest('https://example.com/api')
* const response = await readIncomingResponse(raw)
* console.log(response.status, response.body.toString('utf8'))
* ```
*/
export async function readIncomingResponse(
msg: IncomingResponse,
): Promise<HttpResponse> {
const chunks: Buffer[] = []
for await (const chunk of msg) {
chunks.push(chunk as Buffer)
}
const body = BufferConcat!(chunks)
const status = msg.statusCode ?? 0
const statusText = msg.statusMessage ?? ''
return {
arrayBuffer: () =>
body.buffer.slice(
body.byteOffset,
body.byteOffset + body.byteLength,
) as ArrayBuffer,
body,
headers: msg.headers,
json: <T = unknown>() => JSONParse(body.toString('utf8')) as T,
ok: status >= 200 && status < 300,
rawResponse: msg,
status,
statusText,
text: () => body.toString('utf8'),
}
}