@mswjs/interceptors 0.22.10 → 0.22.11
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/lib/browser/{Interceptor-c794917d.d.ts → Interceptor-c8fc448a.d.ts} +3 -3
- package/lib/browser/{chunk-KX4GZMPY.mjs → chunk-4W72RMFC.mjs} +15 -11
- package/lib/browser/{chunk-OK5YCL7L.js → chunk-ECRLBCQN.js} +55 -55
- package/lib/browser/{chunk-KQGVZIDK.js → chunk-EGQUVEJ3.js} +19 -15
- package/lib/browser/{chunk-2EIH6L6D.js → chunk-KCAWLR7J.js} +25 -25
- package/lib/browser/{chunk-CKVJ2NZB.mjs → chunk-MJZRQFUP.mjs} +67 -54
- package/lib/browser/{chunk-H4SFMU2L.js → chunk-NHFEA72Q.js} +74 -61
- package/lib/browser/{chunk-2GVXYEMC.mjs → chunk-O7B67YBY.mjs} +54 -54
- package/lib/browser/{chunk-65HGG3CV.mjs → chunk-UYUNRMLU.mjs} +1 -1
- package/lib/browser/index.d.ts +3 -3
- package/lib/browser/index.js +8 -8
- package/lib/browser/index.mjs +6 -6
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +2 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.js +4 -4
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/browser/interceptors/fetch/index.d.ts +2 -2
- package/lib/browser/interceptors/fetch/index.js +3 -3
- package/lib/browser/interceptors/fetch/index.mjs +2 -2
- package/lib/browser/presets/browser.d.ts +2 -2
- package/lib/browser/presets/browser.js +6 -6
- package/lib/browser/presets/browser.mjs +4 -4
- package/lib/node/{BatchInterceptor-5c1e5de3.d.ts → BatchInterceptor-fe69020d.d.ts} +1 -1
- package/lib/node/{Interceptor-b3a4098c.d.ts → Interceptor-f9dfe016.d.ts} +3 -3
- package/lib/node/RemoteHttpInterceptor.d.ts +3 -3
- package/lib/node/RemoteHttpInterceptor.js +25 -19
- package/lib/node/RemoteHttpInterceptor.mjs +21 -15
- package/lib/node/{chunk-VTP5KY7W.js → chunk-2OJRZCGS.js} +71 -58
- package/lib/node/{chunk-6CRMKMDL.mjs → chunk-3V5OWTY7.mjs} +54 -55
- package/lib/node/{chunk-62KFIM4W.js → chunk-6KJ5M2VR.js} +56 -57
- package/lib/node/{chunk-A3X67VOL.js → chunk-7XU7Q63W.js} +111 -94
- package/lib/node/{chunk-MUUQLKVJ.js → chunk-PKB2CXQV.js} +3 -3
- package/lib/node/{chunk-SFLY7F52.js → chunk-RJMXHEGJ.js} +6 -6
- package/lib/node/{chunk-EHQJSMSD.mjs → chunk-RL5IV5PL.mjs} +67 -54
- package/lib/node/{chunk-RFVEKRYP.mjs → chunk-S5A4P4BZ.mjs} +1 -1
- package/lib/node/{chunk-IC6Y7RGW.mjs → chunk-SBAVVQIW.mjs} +5 -5
- package/lib/node/{chunk-W27FTX2A.mjs → chunk-XVHIXGXD.mjs} +111 -94
- package/lib/node/index.d.ts +3 -3
- package/lib/node/index.js +4 -4
- package/lib/node/index.mjs +3 -3
- package/lib/node/interceptors/ClientRequest/index.d.ts +2 -2
- package/lib/node/interceptors/ClientRequest/index.js +3 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +2 -2
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +2 -2
- package/lib/node/interceptors/XMLHttpRequest/index.js +4 -4
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/node/interceptors/fetch/index.d.ts +2 -2
- package/lib/node/interceptors/fetch/index.js +16 -12
- package/lib/node/interceptors/fetch/index.mjs +15 -11
- package/lib/node/presets/node.d.ts +2 -2
- package/lib/node/presets/node.js +6 -6
- package/lib/node/presets/node.mjs +4 -4
- package/package.json +2 -3
- package/src/BatchInterceptor.ts +4 -4
- package/src/Interceptor.ts +27 -27
- package/src/RemoteHttpInterceptor.ts +16 -10
- package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +11 -11
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +47 -38
- package/src/interceptors/ClientRequest/http.request.ts +3 -3
- package/src/interceptors/ClientRequest/index.ts +4 -4
- package/src/interceptors/ClientRequest/utils/createRequest.test.ts +5 -5
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +7 -7
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +27 -27
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts +4 -4
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts +7 -4
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +47 -36
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +15 -13
- package/src/interceptors/XMLHttpRequest/index.ts +5 -5
- package/src/interceptors/fetch/index.ts +14 -11
- package/src/utils/AsyncEventEmitter.ts +27 -27
- package/src/utils/cloneObject.ts +7 -7
- package/src/utils/getUrlByRequestOptions.ts +13 -13
- package/src/utils/debug.ts +0 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Debugger } from 'debug'
|
|
2
1
|
import { headersToString } from 'headers-polyfill'
|
|
2
|
+
import type { Logger } from '@open-draft/logger'
|
|
3
3
|
import { concatArrayBuffer } from './utils/concatArrayBuffer'
|
|
4
4
|
import { createEvent } from './utils/createEvent'
|
|
5
5
|
import {
|
|
@@ -40,7 +40,7 @@ export class XMLHttpRequestController {
|
|
|
40
40
|
private responseBuffer: Uint8Array
|
|
41
41
|
private events: Map<keyof XMLHttpRequestEventTargetEventMap, Array<Function>>
|
|
42
42
|
|
|
43
|
-
constructor(readonly initialRequest: XMLHttpRequest, public
|
|
43
|
+
constructor(readonly initialRequest: XMLHttpRequest, public logger: Logger) {
|
|
44
44
|
this.events = new Map()
|
|
45
45
|
this.requestHeaders = new Headers()
|
|
46
46
|
this.responseBuffer = new Uint8Array()
|
|
@@ -83,8 +83,8 @@ export class XMLHttpRequestController {
|
|
|
83
83
|
this.url = toAbsoluteUrl(url)
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
this.
|
|
87
|
-
this.
|
|
86
|
+
this.logger = this.logger.extend(`${this.method} ${this.url.href}`)
|
|
87
|
+
this.logger.info('open', this.method, this.url.href)
|
|
88
88
|
|
|
89
89
|
return invoke()
|
|
90
90
|
}
|
|
@@ -96,7 +96,7 @@ export class XMLHttpRequestController {
|
|
|
96
96
|
]
|
|
97
97
|
|
|
98
98
|
this.registerEvent(eventName, listener)
|
|
99
|
-
this.
|
|
99
|
+
this.logger.info('addEventListener', eventName, listener.name)
|
|
100
100
|
|
|
101
101
|
return invoke()
|
|
102
102
|
}
|
|
@@ -105,7 +105,7 @@ export class XMLHttpRequestController {
|
|
|
105
105
|
const [name, value] = args as [string, string]
|
|
106
106
|
this.requestHeaders.set(name, value)
|
|
107
107
|
|
|
108
|
-
this.
|
|
108
|
+
this.logger.info('setRequestHeader', name, value)
|
|
109
109
|
|
|
110
110
|
return invoke()
|
|
111
111
|
}
|
|
@@ -156,7 +156,7 @@ export class XMLHttpRequestController {
|
|
|
156
156
|
// Note that the request may not yet be DONE and may, in fact,
|
|
157
157
|
// be LOADING while the "respondWith" method does its magic.
|
|
158
158
|
if (this.request.readyState < this.request.LOADING) {
|
|
159
|
-
this.
|
|
159
|
+
this.logger.info(
|
|
160
160
|
'request callback settled but request has not been handled (readystate %d), performing as-is...',
|
|
161
161
|
this.request.readyState
|
|
162
162
|
)
|
|
@@ -195,7 +195,7 @@ export class XMLHttpRequestController {
|
|
|
195
195
|
const nextEvents = prevEvents.concat(listener)
|
|
196
196
|
this.events.set(eventName, nextEvents)
|
|
197
197
|
|
|
198
|
-
this.
|
|
198
|
+
this.logger.info('registered event "%s"', eventName, listener.name)
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
/**
|
|
@@ -203,7 +203,7 @@ export class XMLHttpRequestController {
|
|
|
203
203
|
* Fetch API `Response` instance.
|
|
204
204
|
*/
|
|
205
205
|
public respondWith(response: Response): void {
|
|
206
|
-
this.
|
|
206
|
+
this.logger.info(
|
|
207
207
|
'responding with a mocked response: %d %s',
|
|
208
208
|
response.status,
|
|
209
209
|
response.statusText
|
|
@@ -215,17 +215,21 @@ export class XMLHttpRequestController {
|
|
|
215
215
|
|
|
216
216
|
this.request.getResponseHeader = new Proxy(this.request.getResponseHeader, {
|
|
217
217
|
apply: (_, __, args: [name: string]) => {
|
|
218
|
-
this.
|
|
218
|
+
this.logger.info('getResponseHeader', args[0])
|
|
219
219
|
|
|
220
220
|
if (this.request.readyState < this.request.HEADERS_RECEIVED) {
|
|
221
|
-
this.
|
|
221
|
+
this.logger.info('headers not received yet, returning null')
|
|
222
222
|
|
|
223
223
|
// Headers not received yet, nothing to return.
|
|
224
224
|
return null
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
const headerValue = response.headers.get(args[0])
|
|
228
|
-
this.
|
|
228
|
+
this.logger.info(
|
|
229
|
+
'resolved response header "%s" to',
|
|
230
|
+
args[0],
|
|
231
|
+
headerValue
|
|
232
|
+
)
|
|
229
233
|
|
|
230
234
|
return headerValue
|
|
231
235
|
},
|
|
@@ -235,17 +239,17 @@ export class XMLHttpRequestController {
|
|
|
235
239
|
this.request.getAllResponseHeaders,
|
|
236
240
|
{
|
|
237
241
|
apply: () => {
|
|
238
|
-
this.
|
|
242
|
+
this.logger.info('getAllResponseHeaders')
|
|
239
243
|
|
|
240
244
|
if (this.request.readyState < this.request.HEADERS_RECEIVED) {
|
|
241
|
-
this.
|
|
245
|
+
this.logger.info('headers not received yet, returning empty string')
|
|
242
246
|
|
|
243
247
|
// Headers not received yet, nothing to return.
|
|
244
248
|
return ''
|
|
245
249
|
}
|
|
246
250
|
|
|
247
251
|
const allHeaders = headersToString(response.headers)
|
|
248
|
-
this.
|
|
252
|
+
this.logger.info('resolved all response headers to', allHeaders)
|
|
249
253
|
|
|
250
254
|
return allHeaders
|
|
251
255
|
},
|
|
@@ -278,7 +282,7 @@ export class XMLHttpRequestController {
|
|
|
278
282
|
*/
|
|
279
283
|
undefined
|
|
280
284
|
|
|
281
|
-
this.
|
|
285
|
+
this.logger.info('calculated response body length', totalResponseBodyLength)
|
|
282
286
|
|
|
283
287
|
this.trigger('loadstart', {
|
|
284
288
|
loaded: 0,
|
|
@@ -289,7 +293,7 @@ export class XMLHttpRequestController {
|
|
|
289
293
|
this.setReadyState(this.request.LOADING)
|
|
290
294
|
|
|
291
295
|
const finalizeResponse = () => {
|
|
292
|
-
this.
|
|
296
|
+
this.logger.info('finalizing the mocked response...')
|
|
293
297
|
|
|
294
298
|
this.setReadyState(this.request.DONE)
|
|
295
299
|
|
|
@@ -305,7 +309,7 @@ export class XMLHttpRequestController {
|
|
|
305
309
|
}
|
|
306
310
|
|
|
307
311
|
if (response.body) {
|
|
308
|
-
this.
|
|
312
|
+
this.logger.info('mocked response has body, streaming...')
|
|
309
313
|
|
|
310
314
|
const reader = response.body.getReader()
|
|
311
315
|
|
|
@@ -313,13 +317,13 @@ export class XMLHttpRequestController {
|
|
|
313
317
|
const { value, done } = await reader.read()
|
|
314
318
|
|
|
315
319
|
if (done) {
|
|
316
|
-
this.
|
|
320
|
+
this.logger.info('response body stream done!')
|
|
317
321
|
finalizeResponse()
|
|
318
322
|
return
|
|
319
323
|
}
|
|
320
324
|
|
|
321
325
|
if (value) {
|
|
322
|
-
this.
|
|
326
|
+
this.logger.info('read response body chunk:', value)
|
|
323
327
|
this.responseBuffer = concatArrayBuffer(this.responseBuffer, value)
|
|
324
328
|
|
|
325
329
|
this.trigger('progress', {
|
|
@@ -342,7 +346,10 @@ export class XMLHttpRequestController {
|
|
|
342
346
|
}
|
|
343
347
|
|
|
344
348
|
get response(): unknown {
|
|
345
|
-
this.
|
|
349
|
+
this.logger.info(
|
|
350
|
+
'getResponse (responseType: %s)',
|
|
351
|
+
this.request.responseType
|
|
352
|
+
)
|
|
346
353
|
|
|
347
354
|
if (this.request.readyState !== this.request.DONE) {
|
|
348
355
|
return null
|
|
@@ -351,14 +358,14 @@ export class XMLHttpRequestController {
|
|
|
351
358
|
switch (this.request.responseType) {
|
|
352
359
|
case 'json': {
|
|
353
360
|
const responseJson = parseJson(this.responseBufferToText())
|
|
354
|
-
this.
|
|
361
|
+
this.logger.info('resolved response JSON', responseJson)
|
|
355
362
|
|
|
356
363
|
return responseJson
|
|
357
364
|
}
|
|
358
365
|
|
|
359
366
|
case 'arraybuffer': {
|
|
360
367
|
const arrayBuffer = toArrayBuffer(this.responseBuffer)
|
|
361
|
-
this.
|
|
368
|
+
this.logger.info('resolved response ArrayBuffer', arrayBuffer)
|
|
362
369
|
|
|
363
370
|
return arrayBuffer
|
|
364
371
|
}
|
|
@@ -370,7 +377,7 @@ export class XMLHttpRequestController {
|
|
|
370
377
|
type: mimeType,
|
|
371
378
|
})
|
|
372
379
|
|
|
373
|
-
this.
|
|
380
|
+
this.logger.info(
|
|
374
381
|
'resolved response Blob (mime type: %s)',
|
|
375
382
|
responseBlob,
|
|
376
383
|
mimeType
|
|
@@ -381,7 +388,7 @@ export class XMLHttpRequestController {
|
|
|
381
388
|
|
|
382
389
|
default: {
|
|
383
390
|
const responseText = this.responseBufferToText()
|
|
384
|
-
this.
|
|
391
|
+
this.logger.info(
|
|
385
392
|
'resolving "%s" response type as text',
|
|
386
393
|
this.request.responseType,
|
|
387
394
|
responseText
|
|
@@ -411,7 +418,7 @@ export class XMLHttpRequestController {
|
|
|
411
418
|
}
|
|
412
419
|
|
|
413
420
|
const responseText = this.responseBufferToText()
|
|
414
|
-
this.
|
|
421
|
+
this.logger.info('getResponseText: "%s"', responseText)
|
|
415
422
|
|
|
416
423
|
return responseText
|
|
417
424
|
}
|
|
@@ -447,7 +454,7 @@ export class XMLHttpRequestController {
|
|
|
447
454
|
}
|
|
448
455
|
|
|
449
456
|
public errorWith(error: Error): void {
|
|
450
|
-
this.
|
|
457
|
+
this.logger.info('responding with an error')
|
|
451
458
|
|
|
452
459
|
this.setReadyState(this.request.DONE)
|
|
453
460
|
this.trigger('error')
|
|
@@ -458,19 +465,23 @@ export class XMLHttpRequestController {
|
|
|
458
465
|
* Transitions this request's `readyState` to the given one.
|
|
459
466
|
*/
|
|
460
467
|
private setReadyState(nextReadyState: number): void {
|
|
461
|
-
this.
|
|
468
|
+
this.logger.info(
|
|
469
|
+
'setReadyState: %d -> %d',
|
|
470
|
+
this.request.readyState,
|
|
471
|
+
nextReadyState
|
|
472
|
+
)
|
|
462
473
|
|
|
463
474
|
if (this.request.readyState === nextReadyState) {
|
|
464
|
-
this.
|
|
475
|
+
this.logger.info('ready state identical, skipping transition...')
|
|
465
476
|
return
|
|
466
477
|
}
|
|
467
478
|
|
|
468
479
|
define(this.request, 'readyState', nextReadyState)
|
|
469
480
|
|
|
470
|
-
this.
|
|
481
|
+
this.logger.info('set readyState to: %d', nextReadyState)
|
|
471
482
|
|
|
472
483
|
if (nextReadyState !== this.request.UNSENT) {
|
|
473
|
-
this.
|
|
484
|
+
this.logger.info('triggerring "readystatechange" event...')
|
|
474
485
|
|
|
475
486
|
this.trigger('readystatechange')
|
|
476
487
|
}
|
|
@@ -487,18 +498,18 @@ export class XMLHttpRequestController {
|
|
|
487
498
|
const callback = this.request[`on${eventName}`]
|
|
488
499
|
const event = createEvent(this.request, eventName, options)
|
|
489
500
|
|
|
490
|
-
this.
|
|
501
|
+
this.logger.info('trigger "%s"', eventName, options || '')
|
|
491
502
|
|
|
492
503
|
// Invoke direct callbacks.
|
|
493
504
|
if (typeof callback === 'function') {
|
|
494
|
-
this.
|
|
505
|
+
this.logger.info('found a direct "%s" callback, calling...', eventName)
|
|
495
506
|
callback.call(this.request, event)
|
|
496
507
|
}
|
|
497
508
|
|
|
498
509
|
// Invoke event listeners.
|
|
499
510
|
for (const [registeredEventName, listeners] of this.events) {
|
|
500
511
|
if (registeredEventName === eventName) {
|
|
501
|
-
this.
|
|
512
|
+
this.logger.info(
|
|
502
513
|
'found %d listener(s) for "%s" event, calling...',
|
|
503
514
|
listeners.length,
|
|
504
515
|
eventName
|
|
@@ -513,7 +524,7 @@ export class XMLHttpRequestController {
|
|
|
513
524
|
* Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.
|
|
514
525
|
*/
|
|
515
526
|
public toFetchApiRequest(): Request {
|
|
516
|
-
this.
|
|
527
|
+
this.logger.info('converting request to a Fetch API Request...')
|
|
517
528
|
|
|
518
529
|
const fetchRequest = new Request(this.url.href, {
|
|
519
530
|
method: this.method,
|
|
@@ -552,7 +563,7 @@ export class XMLHttpRequestController {
|
|
|
552
563
|
})
|
|
553
564
|
define(fetchRequest, 'headers', proxyHeaders)
|
|
554
565
|
|
|
555
|
-
this.
|
|
566
|
+
this.logger.info('converted request to a Fetch API Request!', fetchRequest)
|
|
556
567
|
|
|
557
568
|
return fetchRequest
|
|
558
569
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type { Debugger } from 'debug'
|
|
2
1
|
import { until } from '@open-draft/until'
|
|
2
|
+
import type { Logger } from '@open-draft/logger'
|
|
3
3
|
import { XMLHttpRequestEmitter } from '.'
|
|
4
4
|
import { toInteractiveRequest } from '../../utils/toInteractiveRequest'
|
|
5
5
|
import { XMLHttpRequestController } from './XMLHttpRequestController'
|
|
6
6
|
|
|
7
7
|
export interface XMLHttpRequestProxyOptions {
|
|
8
8
|
emitter: XMLHttpRequestEmitter
|
|
9
|
-
|
|
9
|
+
logger: Logger
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -16,11 +16,11 @@ export interface XMLHttpRequestProxyOptions {
|
|
|
16
16
|
*/
|
|
17
17
|
export function createXMLHttpRequestProxy({
|
|
18
18
|
emitter,
|
|
19
|
-
|
|
19
|
+
logger,
|
|
20
20
|
}: XMLHttpRequestProxyOptions) {
|
|
21
21
|
const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {
|
|
22
22
|
construct(target, args, newTarget) {
|
|
23
|
-
|
|
23
|
+
logger.info('constructed new XMLHttpRequest')
|
|
24
24
|
|
|
25
25
|
const originalRequest = Reflect.construct(target, args, newTarget)
|
|
26
26
|
|
|
@@ -44,20 +44,20 @@ export function createXMLHttpRequestProxy({
|
|
|
44
44
|
|
|
45
45
|
const requestController = new XMLHttpRequestController(
|
|
46
46
|
originalRequest,
|
|
47
|
-
|
|
47
|
+
logger
|
|
48
48
|
)
|
|
49
49
|
|
|
50
50
|
requestController.onRequest = async function (request, requestId) {
|
|
51
51
|
// Notify the consumer about a new request.
|
|
52
52
|
const interactiveRequest = toInteractiveRequest(request)
|
|
53
53
|
|
|
54
|
-
this.
|
|
54
|
+
this.logger.info(
|
|
55
55
|
'emitting the "request" event for %s listener(s)...',
|
|
56
56
|
emitter.listenerCount('request')
|
|
57
57
|
)
|
|
58
58
|
emitter.emit('request', interactiveRequest, requestId)
|
|
59
59
|
|
|
60
|
-
this.
|
|
60
|
+
this.logger.info('awaiting mocked response...')
|
|
61
61
|
|
|
62
62
|
const resolverResult = await until(async () => {
|
|
63
63
|
await emitter.untilIdle(
|
|
@@ -67,18 +67,18 @@ export function createXMLHttpRequestProxy({
|
|
|
67
67
|
}
|
|
68
68
|
)
|
|
69
69
|
|
|
70
|
-
this.
|
|
70
|
+
this.logger.info('all "request" listeners settled!')
|
|
71
71
|
|
|
72
72
|
const [mockedResponse] =
|
|
73
73
|
await interactiveRequest.respondWith.invoked()
|
|
74
74
|
|
|
75
|
-
this.
|
|
75
|
+
this.logger.info('event.respondWith called with:', mockedResponse)
|
|
76
76
|
|
|
77
77
|
return mockedResponse
|
|
78
78
|
})
|
|
79
79
|
|
|
80
80
|
if (resolverResult.error) {
|
|
81
|
-
this.
|
|
81
|
+
this.logger.info(
|
|
82
82
|
'request listener threw an exception, aborting request...',
|
|
83
83
|
resolverResult.error
|
|
84
84
|
)
|
|
@@ -95,7 +95,7 @@ export function createXMLHttpRequestProxy({
|
|
|
95
95
|
const mockedResponse = resolverResult.data
|
|
96
96
|
|
|
97
97
|
if (typeof mockedResponse !== 'undefined') {
|
|
98
|
-
this.
|
|
98
|
+
this.logger.info(
|
|
99
99
|
'received mocked response: %d %s',
|
|
100
100
|
mockedResponse.status,
|
|
101
101
|
mockedResponse.statusText
|
|
@@ -104,7 +104,9 @@ export function createXMLHttpRequestProxy({
|
|
|
104
104
|
return requestController.respondWith(mockedResponse)
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
this.
|
|
107
|
+
this.logger.info(
|
|
108
|
+
'no mocked response received, performing request as-is...'
|
|
109
|
+
)
|
|
108
110
|
}
|
|
109
111
|
|
|
110
112
|
requestController.onResponse = async function (
|
|
@@ -112,7 +114,7 @@ export function createXMLHttpRequestProxy({
|
|
|
112
114
|
request,
|
|
113
115
|
requestId
|
|
114
116
|
) {
|
|
115
|
-
this.
|
|
117
|
+
this.logger.info(
|
|
116
118
|
'emitting the "response" event for %s listener(s)...',
|
|
117
119
|
emitter.listenerCount('response')
|
|
118
120
|
)
|
|
@@ -24,9 +24,9 @@ export class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap>
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
protected setup() {
|
|
27
|
-
const
|
|
27
|
+
const logger = this.logger.extend('setup')
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
logger.info('patching "XMLHttpRequest" module...')
|
|
30
30
|
|
|
31
31
|
const PureXMLHttpRequest = globalThis.XMLHttpRequest
|
|
32
32
|
|
|
@@ -37,10 +37,10 @@ export class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap>
|
|
|
37
37
|
|
|
38
38
|
globalThis.XMLHttpRequest = createXMLHttpRequestProxy({
|
|
39
39
|
emitter: this.emitter,
|
|
40
|
-
|
|
40
|
+
logger: this.logger,
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
logger.info(
|
|
44
44
|
'native "XMLHttpRequest" module patched!',
|
|
45
45
|
globalThis.XMLHttpRequest.name
|
|
46
46
|
)
|
|
@@ -57,7 +57,7 @@ export class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap>
|
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
globalThis.XMLHttpRequest = PureXMLHttpRequest
|
|
60
|
-
|
|
60
|
+
logger.info(
|
|
61
61
|
'native "XMLHttpRequest" module restored!',
|
|
62
62
|
globalThis.XMLHttpRequest.name
|
|
63
63
|
)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { invariant } from 'outvariant'
|
|
2
|
+
import { until } from '@open-draft/until'
|
|
2
3
|
import { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'
|
|
3
4
|
import { Interceptor } from '../../Interceptor'
|
|
4
5
|
import { uuidv4 } from '../../utils/uuid'
|
|
5
6
|
import { toInteractiveRequest } from '../../utils/toInteractiveRequest'
|
|
6
|
-
import { until } from '@open-draft/until'
|
|
7
7
|
|
|
8
8
|
export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
9
9
|
static symbol = Symbol('fetch')
|
|
@@ -31,17 +31,17 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
31
31
|
const requestId = uuidv4()
|
|
32
32
|
const request = new Request(input, init)
|
|
33
33
|
|
|
34
|
-
this.
|
|
34
|
+
this.logger.info('[%s] %s', request.method, request.url)
|
|
35
35
|
|
|
36
36
|
const interactiveRequest = toInteractiveRequest(request)
|
|
37
37
|
|
|
38
|
-
this.
|
|
38
|
+
this.logger.info(
|
|
39
39
|
'emitting the "request" event for %d listener(s)...',
|
|
40
40
|
this.emitter.listenerCount('request')
|
|
41
41
|
)
|
|
42
42
|
this.emitter.emit('request', interactiveRequest, requestId)
|
|
43
43
|
|
|
44
|
-
this.
|
|
44
|
+
this.logger.info('awaiting for the mocked response...')
|
|
45
45
|
|
|
46
46
|
const resolverResult = await until(async () => {
|
|
47
47
|
await this.emitter.untilIdle(
|
|
@@ -50,10 +50,10 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
50
50
|
return pendingRequestId === requestId
|
|
51
51
|
}
|
|
52
52
|
)
|
|
53
|
-
this.
|
|
53
|
+
this.logger.info('all request listeners have been resolved!')
|
|
54
54
|
|
|
55
55
|
const [mockedResponse] = await interactiveRequest.respondWith.invoked()
|
|
56
|
-
this.
|
|
56
|
+
this.logger.info('event.respondWith called with:', mockedResponse)
|
|
57
57
|
|
|
58
58
|
return mockedResponse
|
|
59
59
|
})
|
|
@@ -68,8 +68,8 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
68
68
|
|
|
69
69
|
const mockedResponse = resolverResult.data
|
|
70
70
|
|
|
71
|
-
if (mockedResponse) {
|
|
72
|
-
this.
|
|
71
|
+
if (mockedResponse && !request.signal?.aborted) {
|
|
72
|
+
this.logger.info('received mocked response:', mockedResponse)
|
|
73
73
|
const responseCloine = mockedResponse.clone()
|
|
74
74
|
|
|
75
75
|
this.emitter.emit(
|
|
@@ -92,11 +92,11 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
92
92
|
return response
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
this.
|
|
95
|
+
this.logger.info('no mocked response received!')
|
|
96
96
|
|
|
97
97
|
return pureFetch(request).then((response) => {
|
|
98
98
|
const responseClone = response.clone()
|
|
99
|
-
this.
|
|
99
|
+
this.logger.info('original fetch performed', responseClone)
|
|
100
100
|
|
|
101
101
|
this.emitter.emit(
|
|
102
102
|
'response',
|
|
@@ -122,7 +122,10 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
122
122
|
|
|
123
123
|
globalThis.fetch = pureFetch
|
|
124
124
|
|
|
125
|
-
this.
|
|
125
|
+
this.logger.info(
|
|
126
|
+
'restored native "globalThis.fetch"!',
|
|
127
|
+
globalThis.fetch.name
|
|
128
|
+
)
|
|
126
129
|
})
|
|
127
130
|
}
|
|
128
131
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { Logger } from '@open-draft/logger'
|
|
1
2
|
import { Emitter, EventMap, Listener } from 'strict-event-emitter'
|
|
2
3
|
import { nextTick } from './nextTick'
|
|
3
|
-
import { debug, Debugger } from './debug'
|
|
4
4
|
|
|
5
5
|
export interface QueueItem<Args extends Array<unknown>> {
|
|
6
6
|
args: Args
|
|
@@ -17,13 +17,13 @@ export class AsyncEventEmitter<
|
|
|
17
17
|
> extends Emitter<Events> {
|
|
18
18
|
public readyState: AsyncEventEmitterReadyState
|
|
19
19
|
|
|
20
|
-
private
|
|
20
|
+
private logger: Logger
|
|
21
21
|
protected queue: Map<keyof Events, Array<QueueItem<Events[any]>>>
|
|
22
22
|
|
|
23
23
|
constructor() {
|
|
24
24
|
super()
|
|
25
25
|
|
|
26
|
-
this.
|
|
26
|
+
this.logger = new Logger('async-event-emitter')
|
|
27
27
|
this.queue = new Map()
|
|
28
28
|
|
|
29
29
|
this.readyState = AsyncEventEmitterReadyState.ACTIVE
|
|
@@ -33,12 +33,12 @@ export class AsyncEventEmitter<
|
|
|
33
33
|
eventName: EventName,
|
|
34
34
|
listener: Listener<any>
|
|
35
35
|
) {
|
|
36
|
-
const
|
|
36
|
+
const logger = this.logger.extend('on')
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
logger.info('adding "%s" listener...', eventName)
|
|
39
39
|
|
|
40
40
|
if (this.readyState === AsyncEventEmitterReadyState.DEACTIVATED) {
|
|
41
|
-
|
|
41
|
+
logger.info('the emitter is destroyed, skipping!')
|
|
42
42
|
return this
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -46,7 +46,7 @@ export class AsyncEventEmitter<
|
|
|
46
46
|
// Event queue is always established when calling ".emit()".
|
|
47
47
|
const queue = this.openListenerQueue(eventName)
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
logger.info('awaiting the "%s" listener...', eventName)
|
|
50
50
|
|
|
51
51
|
// Whenever a listener is called, create a new Promise
|
|
52
52
|
// that resolves when that listener function completes its execution.
|
|
@@ -59,9 +59,9 @@ export class AsyncEventEmitter<
|
|
|
59
59
|
await listener(...args)
|
|
60
60
|
resolve()
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
logger.info('"%s" listener has resolved!', eventName)
|
|
63
63
|
} catch (error) {
|
|
64
|
-
|
|
64
|
+
logger.info('"%s" listener has rejected!', error)
|
|
65
65
|
reject(error)
|
|
66
66
|
}
|
|
67
67
|
}),
|
|
@@ -73,12 +73,12 @@ export class AsyncEventEmitter<
|
|
|
73
73
|
eventName: EventName,
|
|
74
74
|
...data: Events[EventName]
|
|
75
75
|
): boolean {
|
|
76
|
-
const
|
|
76
|
+
const logger = this.logger.extend('emit')
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
logger.info('emitting "%s" event...', eventName)
|
|
79
79
|
|
|
80
80
|
if (this.readyState === AsyncEventEmitterReadyState.DEACTIVATED) {
|
|
81
|
-
|
|
81
|
+
logger.info('the emitter is destroyed, skipping!')
|
|
82
82
|
return false
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -91,7 +91,7 @@ export class AsyncEventEmitter<
|
|
|
91
91
|
// Establish the Promise queue for this particular event.
|
|
92
92
|
this.openListenerQueue(eventName)
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
logger.info('appending a one-time cleanup "%s" listener...', eventName)
|
|
95
95
|
|
|
96
96
|
// Append a one-time clean up listener.
|
|
97
97
|
this.once(eventName, () => {
|
|
@@ -100,7 +100,7 @@ export class AsyncEventEmitter<
|
|
|
100
100
|
// the time to properly resolve.
|
|
101
101
|
nextTick(() => {
|
|
102
102
|
this.queue.delete(eventName)
|
|
103
|
-
|
|
103
|
+
logger.info('cleaned up "%s" listeners queue!', eventName)
|
|
104
104
|
})
|
|
105
105
|
})
|
|
106
106
|
|
|
@@ -130,46 +130,46 @@ export class AsyncEventEmitter<
|
|
|
130
130
|
private openListenerQueue<EventName extends keyof Events>(
|
|
131
131
|
eventName: EventName
|
|
132
132
|
): Array<QueueItem<Events[EventName]>> {
|
|
133
|
-
const
|
|
134
|
-
|
|
133
|
+
const logger = this.logger.extend('openListenerQueue')
|
|
134
|
+
logger.info('opening "%s" listeners queue...', eventName)
|
|
135
135
|
|
|
136
136
|
const queue = this.queue.get(eventName)
|
|
137
137
|
|
|
138
138
|
if (!queue) {
|
|
139
|
-
|
|
139
|
+
logger.info('no queue found, creating one...')
|
|
140
140
|
this.queue.set(eventName, [])
|
|
141
141
|
return []
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
logger.info('returning an exising queue:', queue)
|
|
145
145
|
return queue
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
public removeAllListeners<EventName extends keyof Events>(
|
|
149
149
|
eventName?: EventName
|
|
150
150
|
) {
|
|
151
|
-
const
|
|
152
|
-
|
|
151
|
+
const logger = this.logger.extend('removeAllListeners')
|
|
152
|
+
logger.info('event:', eventName)
|
|
153
153
|
|
|
154
154
|
if (eventName) {
|
|
155
155
|
this.queue.delete(eventName)
|
|
156
|
-
|
|
156
|
+
logger.info(
|
|
157
157
|
'cleared the "%s" listeners queue!',
|
|
158
158
|
eventName,
|
|
159
159
|
this.queue.get(eventName)
|
|
160
160
|
)
|
|
161
161
|
} else {
|
|
162
162
|
this.queue.clear()
|
|
163
|
-
|
|
163
|
+
logger.info('cleared the listeners queue!', this.queue)
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
return super.removeAllListeners(eventName)
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
public activate(): void {
|
|
170
|
-
const
|
|
170
|
+
const logger = this.logger.extend('activate')
|
|
171
171
|
this.readyState = AsyncEventEmitterReadyState.ACTIVE
|
|
172
|
-
|
|
172
|
+
logger.info('set state to:', this.readyState)
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
/**
|
|
@@ -178,13 +178,13 @@ export class AsyncEventEmitter<
|
|
|
178
178
|
* and needs to be activated again in order to do so.
|
|
179
179
|
*/
|
|
180
180
|
public deactivate(): void {
|
|
181
|
-
const
|
|
181
|
+
const logger = this.logger.extend('deactivate')
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
logger.info('removing all listeners...')
|
|
184
184
|
this.removeAllListeners()
|
|
185
185
|
|
|
186
186
|
this.readyState = AsyncEventEmitterReadyState.DEACTIVATED
|
|
187
|
-
|
|
187
|
+
logger.info('set state to:', this.readyState)
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
private isInternalEventName(eventName: string | number | symbol): boolean {
|