@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
|
@@ -7,12 +7,12 @@ var _chunkVQ4DZOBBjs = require('../../chunk-VQ4DZOBB.js');
|
|
|
7
7
|
var _chunkZJOF5MEZjs = require('../../chunk-ZJOF5MEZ.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunk6KJ5M2VRjs = require('../../chunk-6KJ5M2VR.js');
|
|
11
11
|
|
|
12
12
|
// src/interceptors/fetch/index.ts
|
|
13
13
|
var _outvariant = require('outvariant');
|
|
14
14
|
var _until = require('@open-draft/until');
|
|
15
|
-
var _FetchInterceptor = class extends
|
|
15
|
+
var _FetchInterceptor = class extends _chunk6KJ5M2VRjs.Interceptor {
|
|
16
16
|
constructor() {
|
|
17
17
|
super(_FetchInterceptor.symbol);
|
|
18
18
|
}
|
|
@@ -26,16 +26,17 @@ var _FetchInterceptor = class extends _chunk62KFIM4Wjs.Interceptor {
|
|
|
26
26
|
'Failed to patch the "fetch" module: already patched.'
|
|
27
27
|
);
|
|
28
28
|
globalThis.fetch = async (input, init) => {
|
|
29
|
+
var _a;
|
|
29
30
|
const requestId = _chunkZJOF5MEZjs.uuidv4.call(void 0, );
|
|
30
31
|
const request = new Request(input, init);
|
|
31
|
-
this.
|
|
32
|
+
this.logger.info("[%s] %s", request.method, request.url);
|
|
32
33
|
const interactiveRequest = _chunkZJOF5MEZjs.toInteractiveRequest.call(void 0, request);
|
|
33
|
-
this.
|
|
34
|
+
this.logger.info(
|
|
34
35
|
'emitting the "request" event for %d listener(s)...',
|
|
35
36
|
this.emitter.listenerCount("request")
|
|
36
37
|
);
|
|
37
38
|
this.emitter.emit("request", interactiveRequest, requestId);
|
|
38
|
-
this.
|
|
39
|
+
this.logger.info("awaiting for the mocked response...");
|
|
39
40
|
const resolverResult = await _until.until.call(void 0, async () => {
|
|
40
41
|
await this.emitter.untilIdle(
|
|
41
42
|
"request",
|
|
@@ -43,9 +44,9 @@ var _FetchInterceptor = class extends _chunk62KFIM4Wjs.Interceptor {
|
|
|
43
44
|
return pendingRequestId === requestId;
|
|
44
45
|
}
|
|
45
46
|
);
|
|
46
|
-
this.
|
|
47
|
+
this.logger.info("all request listeners have been resolved!");
|
|
47
48
|
const [mockedResponse2] = await interactiveRequest.respondWith.invoked();
|
|
48
|
-
this.
|
|
49
|
+
this.logger.info("event.respondWith called with:", mockedResponse2);
|
|
49
50
|
return mockedResponse2;
|
|
50
51
|
});
|
|
51
52
|
if (resolverResult.error) {
|
|
@@ -56,8 +57,8 @@ var _FetchInterceptor = class extends _chunk62KFIM4Wjs.Interceptor {
|
|
|
56
57
|
return Promise.reject(error);
|
|
57
58
|
}
|
|
58
59
|
const mockedResponse = resolverResult.data;
|
|
59
|
-
if (mockedResponse) {
|
|
60
|
-
this.
|
|
60
|
+
if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
|
|
61
|
+
this.logger.info("received mocked response:", mockedResponse);
|
|
61
62
|
const responseCloine = mockedResponse.clone();
|
|
62
63
|
this.emitter.emit(
|
|
63
64
|
"response",
|
|
@@ -74,10 +75,10 @@ var _FetchInterceptor = class extends _chunk62KFIM4Wjs.Interceptor {
|
|
|
74
75
|
});
|
|
75
76
|
return response;
|
|
76
77
|
}
|
|
77
|
-
this.
|
|
78
|
+
this.logger.info("no mocked response received!");
|
|
78
79
|
return pureFetch(request).then((response) => {
|
|
79
80
|
const responseClone = response.clone();
|
|
80
|
-
this.
|
|
81
|
+
this.logger.info("original fetch performed", responseClone);
|
|
81
82
|
this.emitter.emit(
|
|
82
83
|
"response",
|
|
83
84
|
responseClone,
|
|
@@ -97,7 +98,10 @@ var _FetchInterceptor = class extends _chunk62KFIM4Wjs.Interceptor {
|
|
|
97
98
|
value: void 0
|
|
98
99
|
});
|
|
99
100
|
globalThis.fetch = pureFetch;
|
|
100
|
-
this.
|
|
101
|
+
this.logger.info(
|
|
102
|
+
'restored native "globalThis.fetch"!',
|
|
103
|
+
globalThis.fetch.name
|
|
104
|
+
);
|
|
101
105
|
});
|
|
102
106
|
}
|
|
103
107
|
};
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "../../chunk-STA6QBYM.mjs";
|
|
8
8
|
import {
|
|
9
9
|
Interceptor
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-3V5OWTY7.mjs";
|
|
11
11
|
|
|
12
12
|
// src/interceptors/fetch/index.ts
|
|
13
13
|
import { invariant } from "outvariant";
|
|
@@ -26,16 +26,17 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
26
26
|
'Failed to patch the "fetch" module: already patched.'
|
|
27
27
|
);
|
|
28
28
|
globalThis.fetch = async (input, init) => {
|
|
29
|
+
var _a;
|
|
29
30
|
const requestId = uuidv4();
|
|
30
31
|
const request = new Request(input, init);
|
|
31
|
-
this.
|
|
32
|
+
this.logger.info("[%s] %s", request.method, request.url);
|
|
32
33
|
const interactiveRequest = toInteractiveRequest(request);
|
|
33
|
-
this.
|
|
34
|
+
this.logger.info(
|
|
34
35
|
'emitting the "request" event for %d listener(s)...',
|
|
35
36
|
this.emitter.listenerCount("request")
|
|
36
37
|
);
|
|
37
38
|
this.emitter.emit("request", interactiveRequest, requestId);
|
|
38
|
-
this.
|
|
39
|
+
this.logger.info("awaiting for the mocked response...");
|
|
39
40
|
const resolverResult = await until(async () => {
|
|
40
41
|
await this.emitter.untilIdle(
|
|
41
42
|
"request",
|
|
@@ -43,9 +44,9 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
43
44
|
return pendingRequestId === requestId;
|
|
44
45
|
}
|
|
45
46
|
);
|
|
46
|
-
this.
|
|
47
|
+
this.logger.info("all request listeners have been resolved!");
|
|
47
48
|
const [mockedResponse2] = await interactiveRequest.respondWith.invoked();
|
|
48
|
-
this.
|
|
49
|
+
this.logger.info("event.respondWith called with:", mockedResponse2);
|
|
49
50
|
return mockedResponse2;
|
|
50
51
|
});
|
|
51
52
|
if (resolverResult.error) {
|
|
@@ -56,8 +57,8 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
56
57
|
return Promise.reject(error);
|
|
57
58
|
}
|
|
58
59
|
const mockedResponse = resolverResult.data;
|
|
59
|
-
if (mockedResponse) {
|
|
60
|
-
this.
|
|
60
|
+
if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
|
|
61
|
+
this.logger.info("received mocked response:", mockedResponse);
|
|
61
62
|
const responseCloine = mockedResponse.clone();
|
|
62
63
|
this.emitter.emit(
|
|
63
64
|
"response",
|
|
@@ -74,10 +75,10 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
74
75
|
});
|
|
75
76
|
return response;
|
|
76
77
|
}
|
|
77
|
-
this.
|
|
78
|
+
this.logger.info("no mocked response received!");
|
|
78
79
|
return pureFetch(request).then((response) => {
|
|
79
80
|
const responseClone = response.clone();
|
|
80
|
-
this.
|
|
81
|
+
this.logger.info("original fetch performed", responseClone);
|
|
81
82
|
this.emitter.emit(
|
|
82
83
|
"response",
|
|
83
84
|
responseClone,
|
|
@@ -97,7 +98,10 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
97
98
|
value: void 0
|
|
98
99
|
});
|
|
99
100
|
globalThis.fetch = pureFetch;
|
|
100
|
-
this.
|
|
101
|
+
this.logger.info(
|
|
102
|
+
'restored native "globalThis.fetch"!',
|
|
103
|
+
globalThis.fetch.name
|
|
104
|
+
);
|
|
101
105
|
});
|
|
102
106
|
}
|
|
103
107
|
};
|
|
@@ -2,9 +2,9 @@ import { ClientRequestInterceptor } from '../interceptors/ClientRequest/index.js
|
|
|
2
2
|
import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
|
|
3
3
|
import 'http';
|
|
4
4
|
import 'https';
|
|
5
|
-
import '../Interceptor-
|
|
5
|
+
import '../Interceptor-f9dfe016.js';
|
|
6
|
+
import '@open-draft/logger';
|
|
6
7
|
import 'strict-event-emitter';
|
|
7
|
-
import 'debug';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The default preset provisions the interception of requests
|
package/lib/node/presets/node.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk7XU7Q63Wjs = require('../chunk-7XU7Q63W.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
7
|
-
require('../chunk-
|
|
6
|
+
var _chunk2OJRZCGSjs = require('../chunk-2OJRZCGS.js');
|
|
7
|
+
require('../chunk-PKB2CXQV.js');
|
|
8
8
|
require('../chunk-VQ4DZOBB.js');
|
|
9
9
|
require('../chunk-ZJOF5MEZ.js');
|
|
10
|
-
require('../chunk-
|
|
10
|
+
require('../chunk-6KJ5M2VR.js');
|
|
11
11
|
|
|
12
12
|
// src/presets/node.ts
|
|
13
13
|
var node_default = [
|
|
14
|
-
new (0,
|
|
15
|
-
new (0,
|
|
14
|
+
new (0, _chunk7XU7Q63Wjs.ClientRequestInterceptor)(),
|
|
15
|
+
new (0, _chunk2OJRZCGSjs.XMLHttpRequestInterceptor)()
|
|
16
16
|
];
|
|
17
17
|
|
|
18
18
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ClientRequestInterceptor
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-XVHIXGXD.mjs";
|
|
4
4
|
import {
|
|
5
5
|
XMLHttpRequestInterceptor
|
|
6
|
-
} from "../chunk-
|
|
7
|
-
import "../chunk-
|
|
6
|
+
} from "../chunk-RL5IV5PL.mjs";
|
|
7
|
+
import "../chunk-S5A4P4BZ.mjs";
|
|
8
8
|
import "../chunk-GFH37L5D.mjs";
|
|
9
9
|
import "../chunk-STA6QBYM.mjs";
|
|
10
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-3V5OWTY7.mjs";
|
|
11
11
|
|
|
12
12
|
// src/presets/node.ts
|
|
13
13
|
var node_default = [
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mswjs/interceptors",
|
|
3
3
|
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
|
|
4
|
-
"version": "0.22.
|
|
4
|
+
"version": "0.22.11",
|
|
5
5
|
"main": "./lib/node/index.js",
|
|
6
6
|
"module": "./lib/node/index.mjs",
|
|
7
7
|
"types": "./lib/node/index.d.ts",
|
|
@@ -138,9 +138,8 @@
|
|
|
138
138
|
},
|
|
139
139
|
"dependencies": {
|
|
140
140
|
"@open-draft/deferred-promise": "^2.1.0",
|
|
141
|
+
"@open-draft/logger": "^0.2.0",
|
|
141
142
|
"@open-draft/until": "^2.0.0",
|
|
142
|
-
"@types/debug": "^4.1.7",
|
|
143
|
-
"debug": "^4.3.3",
|
|
144
143
|
"headers-polyfill": "^3.1.0",
|
|
145
144
|
"outvariant": "^1.2.1",
|
|
146
145
|
"strict-event-emitter": "^0.5.0"
|
package/src/BatchInterceptor.ts
CHANGED
|
@@ -35,15 +35,15 @@ export class BatchInterceptor<
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
protected setup() {
|
|
38
|
-
const
|
|
38
|
+
const logger = this.logger.extend('setup')
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
logger.info('applying all %d interceptors...', this.interceptors.length)
|
|
41
41
|
|
|
42
42
|
for (const interceptor of this.interceptors) {
|
|
43
|
-
|
|
43
|
+
logger.info('applying "%s" interceptor...', interceptor.constructor.name)
|
|
44
44
|
interceptor.apply()
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
logger.info('adding interceptor dispose subscription')
|
|
47
47
|
this.subscriptions.push(() => interceptor.dispose())
|
|
48
48
|
}
|
|
49
49
|
}
|
package/src/Interceptor.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { Logger } from '@open-draft/logger'
|
|
1
2
|
import { Listener } from 'strict-event-emitter'
|
|
2
3
|
import { AsyncEventEmitter } from './utils/AsyncEventEmitter'
|
|
3
4
|
import { nextTick } from './utils/nextTick'
|
|
4
|
-
import { debug, Debugger } from './utils/debug'
|
|
5
5
|
|
|
6
6
|
export type InterceptorEventMap = Record<string, any>
|
|
7
7
|
export type InterceptorSubscription = () => void
|
|
@@ -37,7 +37,7 @@ export type ExtractEventNames<Events extends Record<string, any>> =
|
|
|
37
37
|
export class Interceptor<Events extends InterceptorEventMap> {
|
|
38
38
|
protected emitter: AsyncEventEmitter<Events>
|
|
39
39
|
protected subscriptions: Array<InterceptorSubscription>
|
|
40
|
-
protected
|
|
40
|
+
protected logger: Logger
|
|
41
41
|
|
|
42
42
|
public readyState: InterceptorReadyState
|
|
43
43
|
|
|
@@ -46,13 +46,13 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
46
46
|
|
|
47
47
|
this.emitter = new AsyncEventEmitter()
|
|
48
48
|
this.subscriptions = []
|
|
49
|
-
this.
|
|
49
|
+
this.logger = new Logger(symbol.description!)
|
|
50
50
|
|
|
51
51
|
// Do not limit the maximum number of listeners
|
|
52
52
|
// so not to limit the maximum amount of parallel events emitted.
|
|
53
53
|
this.emitter.setMaxListeners(0)
|
|
54
54
|
|
|
55
|
-
this.
|
|
55
|
+
this.logger.info('constructing the interceptor...')
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
@@ -68,18 +68,18 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
68
68
|
* Returns an already running interceptor instance if it's present.
|
|
69
69
|
*/
|
|
70
70
|
public apply(): void {
|
|
71
|
-
const
|
|
72
|
-
|
|
71
|
+
const logger = this.logger.extend('apply')
|
|
72
|
+
logger.info('applying the interceptor...')
|
|
73
73
|
|
|
74
74
|
if (this.readyState === InterceptorReadyState.APPLIED) {
|
|
75
|
-
|
|
75
|
+
logger.info('intercepted already applied!')
|
|
76
76
|
return
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
const shouldApply = this.checkEnvironment()
|
|
80
80
|
|
|
81
81
|
if (!shouldApply) {
|
|
82
|
-
|
|
82
|
+
logger.info('the interceptor cannot be applied in this environment!')
|
|
83
83
|
return
|
|
84
84
|
}
|
|
85
85
|
|
|
@@ -89,7 +89,7 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
89
89
|
// This will ensure the interceptor can process events after it's
|
|
90
90
|
// been disposed and re-applied again (it may be a singleton).
|
|
91
91
|
this.emitter.activate()
|
|
92
|
-
|
|
92
|
+
logger.info('activated the emiter!', this.emitter.readyState)
|
|
93
93
|
|
|
94
94
|
// Whenever applying a new interceptor, check if it hasn't been applied already.
|
|
95
95
|
// This enables to apply the same interceptor multiple times, for example from a different
|
|
@@ -97,11 +97,11 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
97
97
|
const runningInstance = this.getInstance()
|
|
98
98
|
|
|
99
99
|
if (runningInstance) {
|
|
100
|
-
|
|
100
|
+
logger.info('found a running instance, reusing...')
|
|
101
101
|
|
|
102
102
|
// Proxy any listeners you set on this instance to the running instance.
|
|
103
103
|
this.on = (event, listener) => {
|
|
104
|
-
|
|
104
|
+
logger.info('proxying the "%s" listener', event)
|
|
105
105
|
|
|
106
106
|
// Add listeners to the running instance so they appear
|
|
107
107
|
// at the top of the event listeners list and are executed first.
|
|
@@ -111,7 +111,7 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
111
111
|
// it removes all listeners it has appended to the running interceptor instance.
|
|
112
112
|
this.subscriptions.push(() => {
|
|
113
113
|
runningInstance.emitter.removeListener(event, listener)
|
|
114
|
-
|
|
114
|
+
logger.info('removed proxied "%s" listener!', event)
|
|
115
115
|
})
|
|
116
116
|
}
|
|
117
117
|
|
|
@@ -122,7 +122,7 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
122
122
|
return
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
logger.info('no running instance found, setting up a new instance...')
|
|
126
126
|
|
|
127
127
|
// Setup the interceptor.
|
|
128
128
|
this.setup()
|
|
@@ -149,17 +149,17 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
149
149
|
eventName: EventName,
|
|
150
150
|
listener: Listener<Events[EventName]>
|
|
151
151
|
): void {
|
|
152
|
-
const
|
|
152
|
+
const logger = this.logger.extend('on')
|
|
153
153
|
|
|
154
154
|
if (
|
|
155
155
|
this.readyState === InterceptorReadyState.DISPOSING ||
|
|
156
156
|
this.readyState === InterceptorReadyState.DISPOSED
|
|
157
157
|
) {
|
|
158
|
-
|
|
158
|
+
logger.info('cannot listen to events, already disposed!')
|
|
159
159
|
return
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
logger.info('adding "%s" event listener:', eventName, listener.name)
|
|
163
163
|
|
|
164
164
|
this.emitter.on(eventName, listener)
|
|
165
165
|
}
|
|
@@ -168,18 +168,18 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
168
168
|
* Disposes of any side-effects this interceptor has introduced.
|
|
169
169
|
*/
|
|
170
170
|
public dispose(): void {
|
|
171
|
-
const
|
|
171
|
+
const logger = this.logger.extend('dispose')
|
|
172
172
|
|
|
173
173
|
if (this.readyState === InterceptorReadyState.DISPOSED) {
|
|
174
|
-
|
|
174
|
+
logger.info('cannot dispose, already disposed!')
|
|
175
175
|
return
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
logger.info('disposing the interceptor...')
|
|
179
179
|
this.readyState = InterceptorReadyState.DISPOSING
|
|
180
180
|
|
|
181
181
|
if (!this.getInstance()) {
|
|
182
|
-
|
|
182
|
+
logger.info('no interceptors running, skipping dispose...')
|
|
183
183
|
return
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -187,10 +187,10 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
187
187
|
// indicating that the interceptor is no longer running.
|
|
188
188
|
this.clearInstance()
|
|
189
189
|
|
|
190
|
-
|
|
190
|
+
logger.info('global symbol deleted:', getGlobalSymbol(this.symbol))
|
|
191
191
|
|
|
192
192
|
if (this.subscriptions.length > 0) {
|
|
193
|
-
|
|
193
|
+
logger.info('disposing of %d subscriptions...', this.subscriptions.length)
|
|
194
194
|
|
|
195
195
|
for (const dispose of this.subscriptions) {
|
|
196
196
|
dispose()
|
|
@@ -198,11 +198,11 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
198
198
|
|
|
199
199
|
this.subscriptions = []
|
|
200
200
|
|
|
201
|
-
|
|
201
|
+
logger.info('disposed of all subscriptions!', this.subscriptions.length)
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
this.emitter.deactivate()
|
|
205
|
-
|
|
205
|
+
logger.info('destroyed the listener!')
|
|
206
206
|
|
|
207
207
|
nextTick(() => {
|
|
208
208
|
this.readyState = InterceptorReadyState.DISPOSED
|
|
@@ -211,17 +211,17 @@ export class Interceptor<Events extends InterceptorEventMap> {
|
|
|
211
211
|
|
|
212
212
|
private getInstance(): this | undefined {
|
|
213
213
|
const instance = getGlobalSymbol<this>(this.symbol)
|
|
214
|
-
this.
|
|
214
|
+
this.logger.info('retrieved global instance:', instance?.constructor?.name)
|
|
215
215
|
return instance
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
private setInstance(): void {
|
|
219
219
|
setGlobalSymbol(this.symbol, this)
|
|
220
|
-
this.
|
|
220
|
+
this.logger.info('set global instance!', this.symbol.description)
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
private clearInstance(): void {
|
|
224
224
|
deleteGlobalSymbol(this.symbol)
|
|
225
|
-
this.
|
|
225
|
+
this.logger.info('cleared global instance!', this.symbol.description)
|
|
226
226
|
}
|
|
227
227
|
}
|
|
@@ -60,7 +60,10 @@ export class RemoteHttpInterceptor extends BatchInterceptor<
|
|
|
60
60
|
: await request.text(),
|
|
61
61
|
} as SerializedRequest)
|
|
62
62
|
|
|
63
|
-
this.
|
|
63
|
+
this.logger.info(
|
|
64
|
+
'sent serialized request to the child:',
|
|
65
|
+
serializedRequest
|
|
66
|
+
)
|
|
64
67
|
process.send?.(`request:${serializedRequest}`)
|
|
65
68
|
|
|
66
69
|
const responsePromise = new Promise<void>((resolve) => {
|
|
@@ -94,7 +97,7 @@ export class RemoteHttpInterceptor extends BatchInterceptor<
|
|
|
94
97
|
})
|
|
95
98
|
|
|
96
99
|
// Listen for the mocked response message from the parent.
|
|
97
|
-
this.
|
|
100
|
+
this.logger.info(
|
|
98
101
|
'add "message" listener to the parent process',
|
|
99
102
|
handleParentMessage
|
|
100
103
|
)
|
|
@@ -136,13 +139,13 @@ export class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {
|
|
|
136
139
|
}
|
|
137
140
|
|
|
138
141
|
protected setup() {
|
|
139
|
-
const
|
|
142
|
+
const logger = this.logger.extend('setup')
|
|
140
143
|
|
|
141
144
|
const handleChildMessage: NodeJS.MessageListener = async (message) => {
|
|
142
|
-
|
|
145
|
+
logger.info('received message from child!', message)
|
|
143
146
|
|
|
144
147
|
if (typeof message !== 'string' || !message.startsWith('request:')) {
|
|
145
|
-
|
|
148
|
+
logger.info('unknown message, ignoring...')
|
|
146
149
|
return
|
|
147
150
|
}
|
|
148
151
|
|
|
@@ -155,7 +158,7 @@ export class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {
|
|
|
155
158
|
serializedRequest,
|
|
156
159
|
requestReviver
|
|
157
160
|
) as RevivedRequest
|
|
158
|
-
|
|
161
|
+
logger.info('parsed intercepted request', requestJson)
|
|
159
162
|
|
|
160
163
|
const capturedRequest = new Request(requestJson.url, {
|
|
161
164
|
method: requestJson.method,
|
|
@@ -179,7 +182,7 @@ export class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {
|
|
|
179
182
|
return
|
|
180
183
|
}
|
|
181
184
|
|
|
182
|
-
|
|
185
|
+
logger.info('event.respondWith called with:', mockedResponse)
|
|
183
186
|
const responseClone = mockedResponse.clone()
|
|
184
187
|
const responseText = await mockedResponse.text()
|
|
185
188
|
|
|
@@ -209,15 +212,18 @@ export class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {
|
|
|
209
212
|
}
|
|
210
213
|
)
|
|
211
214
|
|
|
212
|
-
|
|
215
|
+
logger.info(
|
|
216
|
+
'sent serialized mocked response to the parent:',
|
|
217
|
+
serializedResponse
|
|
218
|
+
)
|
|
213
219
|
}
|
|
214
220
|
|
|
215
221
|
this.subscriptions.push(() => {
|
|
216
222
|
this.process.removeListener('message', handleChildMessage)
|
|
217
|
-
|
|
223
|
+
logger.info('removed the "message" listener from the child process!')
|
|
218
224
|
})
|
|
219
225
|
|
|
220
|
-
|
|
226
|
+
logger.info('adding a "message" listener to the child process')
|
|
221
227
|
this.process.addListener('message', handleChildMessage)
|
|
222
228
|
|
|
223
229
|
this.process.once('error', () => this.dispose())
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { vi, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
2
|
import express from 'express'
|
|
3
3
|
import { IncomingMessage } from 'http'
|
|
4
|
+
import { Logger } from '@open-draft/logger'
|
|
4
5
|
import { HttpServer } from '@open-draft/test-server/http'
|
|
5
6
|
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
6
7
|
import { NodeClientRequest } from './NodeClientRequest'
|
|
@@ -9,7 +10,6 @@ import { normalizeClientRequestArgs } from './utils/normalizeClientRequestArgs'
|
|
|
9
10
|
import { AsyncEventEmitter } from '../../utils/AsyncEventEmitter'
|
|
10
11
|
import { sleep } from '../../../test/helpers'
|
|
11
12
|
import { HttpRequestEventMap } from '../../glossary'
|
|
12
|
-
import { debug } from '../../utils/debug'
|
|
13
13
|
|
|
14
14
|
interface ErrorConnectionRefused extends NodeJS.ErrnoException {
|
|
15
15
|
address: string
|
|
@@ -26,7 +26,7 @@ const httpServer = new HttpServer((app) => {
|
|
|
26
26
|
})
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const logger = new Logger('test')
|
|
30
30
|
|
|
31
31
|
beforeAll(async () => {
|
|
32
32
|
await httpServer.listen()
|
|
@@ -44,7 +44,7 @@ it('gracefully finishes the request when it has a mocked response', async () =>
|
|
|
44
44
|
}),
|
|
45
45
|
{
|
|
46
46
|
emitter,
|
|
47
|
-
|
|
47
|
+
logger,
|
|
48
48
|
}
|
|
49
49
|
)
|
|
50
50
|
|
|
@@ -86,7 +86,7 @@ it('responds with a mocked response when requesting an existing hostname', async
|
|
|
86
86
|
normalizeClientRequestArgs('http:', httpServer.http.url('/comment')),
|
|
87
87
|
{
|
|
88
88
|
emitter,
|
|
89
|
-
|
|
89
|
+
logger,
|
|
90
90
|
}
|
|
91
91
|
)
|
|
92
92
|
|
|
@@ -116,7 +116,7 @@ it('performs the request as-is given resolver returned no mocked response', asyn
|
|
|
116
116
|
}),
|
|
117
117
|
{
|
|
118
118
|
emitter,
|
|
119
|
-
|
|
119
|
+
logger,
|
|
120
120
|
}
|
|
121
121
|
)
|
|
122
122
|
|
|
@@ -143,7 +143,7 @@ it('emits the ENOTFOUND error connecting to a non-existing hostname given no moc
|
|
|
143
143
|
const emitter = new AsyncEventEmitter<HttpRequestEventMap>()
|
|
144
144
|
const request = new NodeClientRequest(
|
|
145
145
|
normalizeClientRequestArgs('http:', 'http://non-existing-url.com'),
|
|
146
|
-
{ emitter,
|
|
146
|
+
{ emitter, logger }
|
|
147
147
|
)
|
|
148
148
|
request.end()
|
|
149
149
|
|
|
@@ -163,7 +163,7 @@ it('emits the ECONNREFUSED error connecting to an inactive server given no mocke
|
|
|
163
163
|
normalizeClientRequestArgs('http:', 'http://127.0.0.1:12345'),
|
|
164
164
|
{
|
|
165
165
|
emitter,
|
|
166
|
-
|
|
166
|
+
logger,
|
|
167
167
|
}
|
|
168
168
|
)
|
|
169
169
|
|
|
@@ -188,7 +188,7 @@ it('does not emit ENOTFOUND error connecting to an inactive server given mocked
|
|
|
188
188
|
const handleError = vi.fn()
|
|
189
189
|
const request = new NodeClientRequest(
|
|
190
190
|
normalizeClientRequestArgs('http:', 'http://non-existing-url.com'),
|
|
191
|
-
{ emitter,
|
|
191
|
+
{ emitter, logger }
|
|
192
192
|
)
|
|
193
193
|
|
|
194
194
|
emitter.on('request', async (request) => {
|
|
@@ -220,7 +220,7 @@ it('does not emit ECONNREFUSED error connecting to an inactive server given mock
|
|
|
220
220
|
normalizeClientRequestArgs('http:', 'http://localhost:9876'),
|
|
221
221
|
{
|
|
222
222
|
emitter,
|
|
223
|
-
|
|
223
|
+
logger,
|
|
224
224
|
}
|
|
225
225
|
)
|
|
226
226
|
|
|
@@ -256,7 +256,7 @@ it('sends the request body to the server given no mocked response', async () =>
|
|
|
256
256
|
}),
|
|
257
257
|
{
|
|
258
258
|
emitter,
|
|
259
|
-
|
|
259
|
+
logger,
|
|
260
260
|
}
|
|
261
261
|
)
|
|
262
262
|
|
|
@@ -284,7 +284,7 @@ it('does not send request body to the original server given mocked response', as
|
|
|
284
284
|
}),
|
|
285
285
|
{
|
|
286
286
|
emitter,
|
|
287
|
-
|
|
287
|
+
logger,
|
|
288
288
|
}
|
|
289
289
|
)
|
|
290
290
|
|