-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathClientRequestOverride.ts
358 lines (285 loc) · 10.4 KB
/
ClientRequestOverride.ts
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
import { inherits } from 'util'
import { Socket } from 'net'
import http from 'http'
import { until } from '@open-draft/until'
import { HeadersObject, reduceHeadersObject } from 'headers-utils'
import {
RequestMiddleware,
InterceptedRequest,
RequestInterceptorContext,
MockedResponse,
} from '../../glossary'
import { SocketPolyfill } from './polyfills/SocketPolyfill'
/* Utils */
import { DEFAULT_PATH } from '../../utils/getUrlByRequestOptions'
import { bodyBufferToString } from './utils/bodyBufferToString'
import { concatChunkToBuffer } from './utils/concatChunkToBuffer'
import { inheritRequestHeaders } from './utils/inheritRequestHeaders'
import { normalizeHttpRequestParams } from './utils/normalizeHttpRequestParams'
import { normalizeHttpRequestEndParams } from './utils/normalizeHttpRequestEndParams'
import { getIncomingMessageBody } from './utils/getIncomingMessageBody'
const createDebug = require('debug')
export function createClientRequestOverrideClass(
middleware: RequestMiddleware,
context: RequestInterceptorContext,
performOriginalRequest: typeof http.request,
originalClientRequest: typeof http.ClientRequest
) {
function ClientRequestOverride(
this: http.ClientRequest,
...args: Parameters<typeof http.request>
) {
const [url, options, callback] = normalizeHttpRequestParams(...args)
const usesHttps = url.protocol === 'https:'
let requestBodyBuffer: Buffer[] = []
const debug = createDebug(`http ${options.method} ${url.href}`)
// Inherit ClientRequest properties from RequestOptions.
this.method = options.method || 'GET'
this.path = options.path || DEFAULT_PATH
debug('intercepted %s %s (%s)', options.method, url.href, url.protocol)
http.OutgoingMessage.call(this)
// Propagate options headers to the request instance.
inheritRequestHeaders(this, options.headers)
const socket = (new SocketPolyfill(options, {
usesHttps,
}) as any) as Socket & {
authorized: boolean
}
this.socket = this.connection = socket
if (options.timeout) {
debug('setting socket timeout to %a', options.timeout)
socket.setTimeout(options.timeout)
}
// Create a mocked response instance.
const response = new http.IncomingMessage(socket)
if (options.headers?.expect === '100-continue') {
debug('encountered "100 Continue" header')
this.emit('continue')
}
process.nextTick(() => {
this.emit('socket', socket)
socket.emit('connect')
if (socket.authorized) {
debug('emitting authorized socket event')
socket.emit('secureConnect')
}
})
if (callback) {
this.once('response', callback)
}
const emitError = (error: Error) => {
process.nextTick(() => {
this.emit('error', error)
})
}
this.write = (chunk: string | Buffer, ...args: any[]): boolean => {
debug('write', chunk, args)
const callback = typeof args[1] === 'function' ? args[1] : args[2]
if (this.aborted) {
debug('cannot write: request aborted')
emitError(new Error('Request aborted'))
} else {
if (chunk) {
debug('request write: concat chunk to buffer', chunk)
requestBodyBuffer = concatChunkToBuffer(chunk, requestBodyBuffer)
}
if (typeof callback === 'function') {
callback()
}
}
setImmediate(() => {
this.emit('drain')
})
return false
}
this.end = async (...args: any) => {
const [chunk, encoding, callback] = normalizeHttpRequestEndParams(...args)
debug('end', { chunk, encoding, callback })
debug('request headers', options.headers)
const writtenRequestBody = bodyBufferToString(
Buffer.concat(requestBodyBuffer)
)
debug('request written body', writtenRequestBody)
// Resolve the entire request body, including:
// - buffer written via `req.write()`
// - chunk provided to `req.end(chunk)`
// So that the request middleware has access to the resolved body.
const resolvedRequestBody = bodyBufferToString(
Buffer.concat(
chunk
? concatChunkToBuffer(chunk, requestBodyBuffer)
: requestBodyBuffer
)
)
debug('request resolved body', resolvedRequestBody)
const outHeaders = this.getHeaders()
const resolvedRequestHeaders = Object.assign(
{},
outHeaders,
options.headers
)
const requestHeaders = resolvedRequestHeaders
? reduceHeadersObject<HeadersObject>(
resolvedRequestHeaders as HeadersObject,
(headers, name, value) => {
headers[name.toLowerCase()] = value
return headers
},
{}
)
: {}
debug('request headers', requestHeaders)
// Construct the intercepted request instance exposed to the request middleware.
const formattedRequest: InterceptedRequest = {
url,
method: options.method || 'GET',
headers: requestHeaders,
body: resolvedRequestBody,
}
debug('awaiting mocked response...')
const [middlewareException, mockedResponse] = await until(async () =>
middleware(formattedRequest, response)
)
// When the request middleware throws an exception, error the request.
// This cancels the request and is similar to a network error.
if (middlewareException) {
debug('middleware function threw an exception!', middlewareException)
this.emit('error', middlewareException)
return this
}
if (mockedResponse) {
debug('received mocked response:', mockedResponse)
// Prevent modifying an already finished response.
if (!response.complete) {
const { headers = {} } = mockedResponse
response.statusCode = mockedResponse.status
response.statusMessage = mockedResponse.statusText
debug('writing response headers...')
// Converts mocked response headers to actual headers
// (lowercases header names and merges duplicates).
response.headers = Object.entries(
headers
).reduce<http.IncomingHttpHeaders>((acc, [name, value]) => {
const headerName = name.toLowerCase()
const headerValue = acc.hasOwnProperty(headerName)
? ([] as string[]).concat(acc[headerName] as string, value)
: value
acc[headerName] = headerValue
return acc
}, {})
// Converts mocked response headers to raw headers.
// @see https://nodejs.org/api/http.html#http_message_rawheaders
response.rawHeaders = Object.entries(headers).reduce<string[]>(
(acc, [name, value]) => {
return acc.concat(name, value)
},
[]
)
if (mockedResponse.body) {
debug('writing response body...')
response.push(Buffer.from(mockedResponse.body))
}
}
debug('response is complete, finishing request...')
// Invoke the "req.end()" callback.
callback?.()
this.finished = true
this.emit('finish')
this.emit('response', response)
// Pushing `null` indicates that the response body is complete
// and must not be modified anymore.
response.push(null)
response.complete = true
context.emitter.emit('response', formattedRequest, {
status: mockedResponse.status || 200,
statusText: mockedResponse.statusText || 'OK',
headers: mockedResponse.headers || {},
body: mockedResponse.body,
})
return this
}
debug('no mocked response received')
debug(
'performing original %s %s (%s)',
options.method,
url.href,
url.protocol
)
debug('original request options', options)
debug('original request body (written)', writtenRequestBody)
debug('original request body (end)', chunk)
let req: http.ClientRequest
debug('using', performOriginalRequest)
// Decide whether to use HTTPS based on the URL protocol.
// XHR can trigger http.request for HTTPS URL.
if (url.protocol === 'https:') {
debug('reverting patches...')
const { ClientRequest } = http
// @ts-ignore
http.ClientRequest = originalClientRequest
req = performOriginalRequest(options)
debug('re-applying patches...')
// @ts-ignore
http.ClientRequest = ClientRequest
} else {
req = performOriginalRequest(options)
}
// Propagate headers set after `ClientRequest` is constructed
// onto the original request instance.
inheritRequestHeaders(req, outHeaders)
// Propagate a request body buffer written via `req.write()`
// to the original request.
if (requestBodyBuffer.length > 0 && req.writable) {
req.write(Buffer.concat(requestBodyBuffer))
}
req.on('finish', () => {
this.emit('finish')
})
req.on('response', async (response) => {
context.emitter.emit('response', formattedRequest, {
status: response.statusCode || 200,
statusText: response.statusMessage || 'OK',
headers: response.headers as MockedResponse['headers'],
body: await getIncomingMessageBody(response),
})
})
req.on('response', (response) => {
debug(response.statusCode, options.method, url.href)
this.emit('response', response)
})
req.on('error', (error) => {
debug('original request error', error)
this.emit('error', error)
})
// Provide a callback when an original request is finished,
// so it can be debugged.
req.end(
...[
chunk,
encoding as any,
() => {
debug('request ended', this.method, url.href)
callback?.()
},
].filter(Boolean)
)
return req
}
this.abort = () => {
debug('abort')
if (this.aborted) {
debug('already aborted')
return
}
this.aborted = Date.now()
const error = new Error() as NodeJS.ErrnoException
error.code = 'aborted'
response.emit('close', error)
socket.destroy()
this.emit('abort')
}
return this
}
inherits(ClientRequestOverride, originalClientRequest)
return ClientRequestOverride
}