-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
NodeClientRequest.ts
384 lines (316 loc) · 11.7 KB
/
NodeClientRequest.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
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
import { Debugger, debug } from 'debug'
import type { RequestOptions } from 'http'
import { ClientRequest, IncomingMessage } from 'http'
import { until } from '@open-draft/until'
import { Headers, objectToHeaders } from 'headers-polyfill/lib'
import type {
IsomorphicRequest,
MockedResponse,
Observer,
Resolver,
} from '../../createInterceptor'
import { uuidv4 } from '../../utils/uuid'
import { concatChunkToBuffer } from './utils/concatChunkToBuffer'
import {
ClientRequestEndChunk,
normalizeClientRequestEndArgs,
} from './utils/normalizeClientRequestEndArgs'
import { NormalizedClientRequestArgs } from './utils/normalizeClientRequestArgs'
import { toIsoResponse } from '../../utils/toIsoResponse'
import { getIncomingMessageBody } from './utils/getIncomingMessageBody'
import { bodyBufferToString } from './utils/bodyBufferToString'
import {
ClientRequestWriteArgs,
ClientRequestWriteCallback,
normalizeClientRequestWriteArgs,
} from './utils/normalizeClientRequestWriteArgs'
import { cloneIncomingMessage } from './utils/cloneIncomingMessage'
export type Protocol = 'http' | 'https'
export interface NodeClientOptions {
observer: Observer
resolver: Resolver
}
export class NodeClientRequest extends ClientRequest {
/**
* The list of internal Node.js errors to suppress while
* using the "mock" response source.
*/
static suppressErrorCodes = ['ENOTFOUND', 'ECONNREFUSED', 'ECONNRESET']
private url: URL
private options: RequestOptions
private response: IncomingMessage
private resolver: Resolver
private observer: Observer
private log: Debugger
private chunks: Array<{
chunk?: string | Buffer
encoding?: BufferEncoding
callback?: ClientRequestWriteCallback
}> = []
private responseSource: 'mock' | 'bypass' = 'mock'
private capturedError?: NodeJS.ErrnoException
public requestBody: Buffer[] = []
constructor(
[url, requestOptions, callback]: NormalizedClientRequestArgs,
options: NodeClientOptions
) {
super(requestOptions, callback)
this.log = debug(`http ${requestOptions.method} ${url.href}`)
this.log('constructing ClientRequest...', {
url,
requestOptions,
callback,
})
this.url = url
this.options = requestOptions
this.resolver = options.resolver
this.observer = options.observer
// Construct a mocked response message.
this.response = new IncomingMessage(this.socket!)
}
write(...args: ClientRequestWriteArgs): boolean {
const [chunk, encoding, callback] = normalizeClientRequestWriteArgs(args)
this.log('write:', { chunk, encoding, callback })
this.chunks.push({
chunk,
encoding,
callback: (error?: Error | null) => {
if (error) {
this.log('error while writing chunk!', error)
} else {
this.log('request chunk successfully written!')
}
callback?.(error)
},
})
this.log('chunk successfully stored!')
this.requestBody = concatChunkToBuffer(chunk, this.requestBody)
// Do not write the request body chunks to prevent
// the Socket from sending data to a potentially existing
// server when there is a mocked response defined.
return true
}
end(...args: any): this {
this.log('end', args)
const [chunk, encoding, callback] = normalizeClientRequestEndArgs(...args)
this.log('normalized arguments:', { chunk, encoding, callback })
const requestBody = this.getRequestBody(chunk)
const isomorphicRequest = this.toIsomorphicRequest(requestBody)
this.observer.emit('request', isomorphicRequest)
this.log('executing response resolver...')
// Execute the resolver Promise like a side-effect.
// Node.js 16 forces "ClientRequest.end" to be synchronous and return "this".
until(async () => this.resolver(isomorphicRequest, this.response)).then(
([resolverException, mockedResponse]) => {
// Halt the request whenever the resolver throws an exception.
if (resolverException) {
this.log(
'encountered resolver exception, aborting request...',
resolverException
)
this.emit('error', resolverException)
this.terminate()
return this
}
if (mockedResponse) {
this.log('received mocked response:', mockedResponse)
this.responseSource = 'mock'
const isomorphicResponse = toIsoResponse(mockedResponse)
this.respondWith(mockedResponse)
this.log(
isomorphicResponse.status,
isomorphicResponse.statusText,
isomorphicResponse.body,
'(MOCKED)'
)
callback?.()
this.log('emitting the custom "response" event...')
this.observer.emit('response', isomorphicRequest, isomorphicResponse)
return this
}
this.log('no mocked response found!')
// Set the response source to "bypass".
// Any errors emitted past this point are not suppressed.
this.responseSource = 'bypass'
// Propagate previously captured errors.
// For example, a ECONNREFUSED error when connecting to a non-existing host.
if (this.capturedError) {
this.emit('error', this.capturedError)
return this
}
// Write the request body chunks in the order of ".write()" calls.
// Note that no request body has been written prior to this point
// in order to prevent the Socket to communicate with a potentially
// existing server.
this.log('writing request chunks...', this.chunks)
for (const { chunk, encoding, callback } of this.chunks) {
encoding
? super.write(chunk, encoding, callback)
: super.write(chunk, callback)
}
this.once('error', (error) => {
this.log('original request error:', error)
})
this.once('abort', () => {
this.log('original request aborted!')
})
this.once('response-internal', async (response: IncomingMessage) => {
const responseBody = await getIncomingMessageBody(response)
this.log(response.statusCode, response.statusMessage, responseBody)
this.log('original response headers:', response.headers)
this.log('emitting the custom "response" event...')
this.observer.emit('response', isomorphicRequest, {
status: response.statusCode || 200,
statusText: response.statusMessage || 'OK',
headers: objectToHeaders(response.headers),
body: responseBody,
})
})
this.log('performing original request...')
return super.end(
...[
chunk,
encoding as any,
() => {
this.log('original request end!')
callback?.()
},
].filter(Boolean)
)
}
)
return this
}
emit(event: string, ...data: any[]) {
this.log('event:%s', event)
if (event === 'response') {
this.log('found "response" event, cloning the response...')
try {
/**
* Clone the response object when emitting the "response" event.
* This prevents the response body stream from locking
* and allows reading it twice:
* 1. Internal "response" event from the observer.
* 2. Any external response body listeners.
* @see https://github.com/mswjs/interceptors/issues/161
*/
const response = data[0] as IncomingMessage
const firstClone = cloneIncomingMessage(response)
const secondClone = cloneIncomingMessage(response)
this.emit('response-internal', secondClone)
this.log('response successfuly cloned, emitting "response" event...')
return super.emit(event, firstClone, ...data.slice(1))
} catch (error) {
this.log('error when cloning response:', error)
return super.emit(event, ...data)
}
}
if (event === 'error') {
const error = data[0] as NodeJS.ErrnoException
const errorCode = error.code || ''
this.log('error:\n', error)
// Supress certain errors while using the "mock" source.
// For example, no need to destroy this request if it connects
// to a non-existing hostname but has a mocked response.
if (
this.responseSource === 'mock' &&
NodeClientRequest.suppressErrorCodes.includes(errorCode)
) {
// Capture the first emitted error in order to replay
// it later if this request won't have any mocked response.
if (!this.capturedError) {
this.capturedError = error
}
return false
}
}
return super.emit(event, ...data)
}
private respondWith(mockedResponse: MockedResponse): void {
this.log('responding with a mocked response...', mockedResponse)
const { status, statusText, headers, body } = mockedResponse
this.response.statusCode = status
this.response.statusMessage = statusText
if (headers) {
this.response.headers = {}
for (const [headerName, headerValue] of Object.entries(headers)) {
this.response.rawHeaders.push(
headerName,
...(Array.isArray(headerValue) ? headerValue : [headerValue])
)
const insensitiveHeaderName = headerName.toLowerCase()
const prevHeaders = this.response.headers[insensitiveHeaderName]
this.response.headers[insensitiveHeaderName] = prevHeaders
? Array.prototype.concat([], prevHeaders, headerValue)
: headerValue
}
}
this.log('mocked response headers ready:', headers)
if (body) {
this.response.push(Buffer.from(body))
}
// Push "null" to indicate that the response body is complete
// and shouldn't be written to anymore.
this.response.push(null)
this.response.complete = true
/**
* Set the internal "res" property to the mocked "OutgoingMessage"
* to make the "ClientRequest" instance think there's data received
* from the socket.
* @see https://github.com/nodejs/node/blob/9c405f2591f5833d0247ed0fafdcd68c5b14ce7a/lib/_http_client.js#L501
*/
// @ts-ignore
this.res = this.response
this.finished = true
Object.defineProperty(this, 'writableEnded', {
value: true,
})
this.emit('finish')
this.emit('response', this.response)
this.terminate()
}
/**
* Terminates a pending request.
*/
private terminate(): void {
// @ts-ignore
this.agent.destroy()
}
private getRequestBody(chunk: ClientRequestEndChunk | null): string {
const writtenRequestBody = bodyBufferToString(
Buffer.concat(this.requestBody)
)
this.log('written request body:', writtenRequestBody)
// Write the last request body chunk to the internal request body buffer.
if (chunk) {
this.requestBody = concatChunkToBuffer(chunk, this.requestBody)
}
const resolvedRequestBody = bodyBufferToString(
Buffer.concat(this.requestBody)
)
this.log('resolved request body:', resolvedRequestBody)
return resolvedRequestBody
}
private toIsomorphicRequest(body: string): IsomorphicRequest {
this.log('creating isomorphic request object...')
const outgoingHeaders = this.getHeaders()
this.log('request outgoing headers:', outgoingHeaders)
const headers = new Headers()
for (const [headerName, headerValue] of Object.entries(outgoingHeaders)) {
if (!headerValue) {
continue
}
headers.set(headerName.toLowerCase(), headerValue.toString())
}
const isomorphicRequest: IsomorphicRequest = {
id: uuidv4(),
url: this.url,
method: this.options.method || 'GET',
credentials: 'same-origin',
headers,
body,
}
this.log('successfully created isomorphic request!', isomorphicRequest)
return isomorphicRequest
}
}