@mswjs/interceptors 0.25.1 → 0.25.2
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-8d5fd4c6.d.ts +86 -0
- package/lib/browser/{chunk-4CFMDU7Z.js → chunk-CWVY2E3W.js} +20 -134
- package/lib/browser/{chunk-VMXB5F2J.mjs → chunk-HXJPKJY3.mjs} +25 -15
- package/lib/browser/{chunk-DBFLI5DJ.js → chunk-KITNLK66.js} +30 -20
- package/lib/browser/chunk-KK6APRON.mjs +58 -0
- package/lib/browser/{chunk-OSIUQA4X.js → chunk-NMG5MQJJ.js} +32 -29
- package/lib/browser/{chunk-GXJLJMOT.mjs → chunk-QPMXOLDO.mjs} +21 -135
- package/lib/browser/{chunk-ANLPTCZ5.mjs → chunk-TYEVJTWH.mjs} +27 -24
- package/lib/browser/chunk-X3NRJIZW.js +58 -0
- package/lib/browser/index.d.ts +7 -3
- package/lib/browser/index.js +24 -5
- package/lib/browser/index.mjs +22 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +4 -3
- 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 -1
- package/lib/browser/interceptors/fetch/index.js +4 -4
- package/lib/browser/interceptors/fetch/index.mjs +3 -3
- package/lib/browser/presets/browser.d.ts +2 -1
- package/lib/browser/presets/browser.js +6 -6
- package/lib/browser/presets/browser.mjs +4 -4
- package/lib/node/{BatchInterceptor-c841b068.d.ts → BatchInterceptor-9785c567.d.ts} +5 -2
- package/lib/node/Interceptor-7a701c1f.d.ts +86 -0
- package/lib/node/RemoteHttpInterceptor.d.ts +3 -2
- package/lib/node/RemoteHttpInterceptor.js +18 -18
- package/lib/node/RemoteHttpInterceptor.mjs +14 -14
- package/lib/node/{chunk-XYZRP5S2.js → chunk-3XFLRXRY.js} +20 -134
- package/lib/node/chunk-5PTPJLB7.js +58 -0
- package/lib/node/{chunk-E6YC337Q.js → chunk-5YAV7CXX.js} +29 -26
- package/lib/node/{chunk-HSCXCLVT.mjs → chunk-7RGC35CC.mjs} +27 -24
- package/lib/node/{chunk-3MYUI4B2.js → chunk-B2CIOP5B.js} +22 -16
- package/lib/node/{chunk-RGYCLCLK.mjs → chunk-GM3YBSM3.mjs} +21 -135
- package/lib/node/{chunk-OL7OR4RL.mjs → chunk-OMRBBJT7.mjs} +20 -14
- package/lib/node/{chunk-VS3GJPUE.mjs → chunk-UBEFEZXT.mjs} +22 -3
- package/lib/node/{chunk-MVPEJK4V.js → chunk-UF7QIAQ5.js} +23 -4
- package/lib/node/chunk-YQGTMMOZ.mjs +58 -0
- package/lib/node/index.d.ts +3 -2
- package/lib/node/index.js +3 -3
- package/lib/node/index.mjs +2 -2
- package/lib/node/interceptors/ClientRequest/index.d.ts +4 -3
- package/lib/node/interceptors/ClientRequest/index.js +4 -4
- package/lib/node/interceptors/ClientRequest/index.mjs +3 -3
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +4 -3
- 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 -1
- package/lib/node/interceptors/fetch/index.js +27 -17
- package/lib/node/interceptors/fetch/index.mjs +25 -15
- package/lib/node/presets/node.d.ts +3 -2
- package/lib/node/presets/node.js +6 -6
- package/lib/node/presets/node.mjs +4 -4
- package/package.json +1 -1
- package/src/BatchInterceptor.test.ts +141 -0
- package/src/BatchInterceptor.ts +38 -4
- package/src/Interceptor.test.ts +46 -0
- package/src/Interceptor.ts +35 -16
- package/src/RemoteHttpInterceptor.ts +11 -9
- package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +10 -10
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +35 -18
- package/src/interceptors/ClientRequest/index.test.ts +2 -3
- package/src/interceptors/ClientRequest/index.ts +2 -2
- package/src/interceptors/ClientRequest/utils/createRequest.test.ts +2 -2
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +29 -25
- package/src/interceptors/XMLHttpRequest/index.ts +2 -2
- package/src/interceptors/fetch/index.ts +26 -13
- package/src/utils/RequestController.ts +21 -0
- package/src/utils/emitAsync.ts +25 -0
- package/src/utils/toInteractiveRequest.ts +17 -23
- package/lib/browser/Interceptor-0a020bc4.d.ts +0 -116
- package/lib/browser/chunk-PCFJD76X.js +0 -64
- package/lib/browser/chunk-RT3ATOJH.mjs +0 -64
- package/lib/node/Interceptor-738f79c5.d.ts +0 -116
- package/lib/node/chunk-STA6QBYM.mjs +0 -64
- package/lib/node/chunk-ZJOF5MEZ.js +0 -64
- package/src/utils/AsyncEventEmitter.test.ts +0 -102
- package/src/utils/AsyncEventEmitter.ts +0 -193
- package/src/utils/createLazyCallback.ts +0 -49
|
@@ -7,12 +7,13 @@ import {
|
|
|
7
7
|
IS_PATCHED_MODULE
|
|
8
8
|
} from "./chunk-GFH37L5D.mjs";
|
|
9
9
|
import {
|
|
10
|
+
emitAsync,
|
|
10
11
|
toInteractiveRequest,
|
|
11
12
|
uuidv4
|
|
12
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-YQGTMMOZ.mjs";
|
|
13
14
|
import {
|
|
14
15
|
Interceptor
|
|
15
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-GM3YBSM3.mjs";
|
|
16
17
|
|
|
17
18
|
// src/interceptors/XMLHttpRequest/index.ts
|
|
18
19
|
import { invariant as invariant2 } from "outvariant";
|
|
@@ -654,30 +655,32 @@ function createXMLHttpRequestProxy({
|
|
|
654
655
|
prototypeDescriptors[propertyName]
|
|
655
656
|
);
|
|
656
657
|
}
|
|
657
|
-
const
|
|
658
|
+
const xhrRequestController = new XMLHttpRequestController(
|
|
658
659
|
originalRequest,
|
|
659
660
|
logger
|
|
660
661
|
);
|
|
661
|
-
|
|
662
|
-
const interactiveRequest = toInteractiveRequest(request);
|
|
663
|
-
this.logger.info(
|
|
664
|
-
'emitting the "request" event for %s listener(s)...',
|
|
665
|
-
emitter.listenerCount("request")
|
|
666
|
-
);
|
|
667
|
-
emitter.emit("request", {
|
|
668
|
-
request: interactiveRequest,
|
|
669
|
-
requestId
|
|
670
|
-
});
|
|
662
|
+
xhrRequestController.onRequest = async function({ request, requestId }) {
|
|
663
|
+
const { interactiveRequest, requestController } = toInteractiveRequest(request);
|
|
671
664
|
this.logger.info("awaiting mocked response...");
|
|
665
|
+
emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
666
|
+
if (pendingRequestId !== requestId) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (requestController.responsePromise.state === "pending") {
|
|
670
|
+
requestController.respondWith(void 0);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
672
673
|
const resolverResult = await until(async () => {
|
|
673
|
-
|
|
674
|
-
"request",
|
|
675
|
-
(
|
|
676
|
-
return pendingRequestId === requestId;
|
|
677
|
-
}
|
|
674
|
+
this.logger.info(
|
|
675
|
+
'emitting the "request" event for %s listener(s)...',
|
|
676
|
+
emitter.listenerCount("request")
|
|
678
677
|
);
|
|
678
|
+
await emitAsync(emitter, "request", {
|
|
679
|
+
request: interactiveRequest,
|
|
680
|
+
requestId
|
|
681
|
+
});
|
|
679
682
|
this.logger.info('all "request" listeners settled!');
|
|
680
|
-
const
|
|
683
|
+
const mockedResponse2 = await requestController.responsePromise;
|
|
681
684
|
this.logger.info("event.respondWith called with:", mockedResponse2);
|
|
682
685
|
return mockedResponse2;
|
|
683
686
|
});
|
|
@@ -686,7 +689,7 @@ function createXMLHttpRequestProxy({
|
|
|
686
689
|
"request listener threw an exception, aborting request...",
|
|
687
690
|
resolverResult.error
|
|
688
691
|
);
|
|
689
|
-
|
|
692
|
+
xhrRequestController.errorWith(resolverResult.error);
|
|
690
693
|
return;
|
|
691
694
|
}
|
|
692
695
|
const mockedResponse = resolverResult.data;
|
|
@@ -700,16 +703,16 @@ function createXMLHttpRequestProxy({
|
|
|
700
703
|
this.logger.info(
|
|
701
704
|
"received a network error response, rejecting the request promise..."
|
|
702
705
|
);
|
|
703
|
-
|
|
706
|
+
xhrRequestController.errorWith(new TypeError("Network error"));
|
|
704
707
|
return;
|
|
705
708
|
}
|
|
706
|
-
return
|
|
709
|
+
return xhrRequestController.respondWith(mockedResponse);
|
|
707
710
|
}
|
|
708
711
|
this.logger.info(
|
|
709
712
|
"no mocked response received, performing request as-is..."
|
|
710
713
|
);
|
|
711
714
|
};
|
|
712
|
-
|
|
715
|
+
xhrRequestController.onResponse = async function({
|
|
713
716
|
response,
|
|
714
717
|
isMockedResponse,
|
|
715
718
|
request,
|
|
@@ -726,7 +729,7 @@ function createXMLHttpRequestProxy({
|
|
|
726
729
|
requestId
|
|
727
730
|
});
|
|
728
731
|
};
|
|
729
|
-
return
|
|
732
|
+
return xhrRequestController.request;
|
|
730
733
|
}
|
|
731
734
|
});
|
|
732
735
|
return XMLHttpRequestProxy;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var _chunkZJOF5MEZjs = require('./chunk-ZJOF5MEZ.js');
|
|
5
4
|
|
|
5
|
+
var _chunk5PTPJLB7js = require('./chunk-5PTPJLB7.js');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
var _chunk3XFLRXRYjs = require('./chunk-3XFLRXRY.js');
|
|
8
9
|
|
|
9
10
|
// src/interceptors/ClientRequest/index.ts
|
|
10
11
|
var _http = require('http'); var _http2 = _interopRequireDefault(_http);
|
|
@@ -13,6 +14,7 @@ var _https = require('https'); var _https2 = _interopRequireDefault(_https);
|
|
|
13
14
|
// src/interceptors/ClientRequest/NodeClientRequest.ts
|
|
14
15
|
|
|
15
16
|
var _until = require('@open-draft/until');
|
|
17
|
+
var _deferredpromise = require('@open-draft/deferred-promise');
|
|
16
18
|
|
|
17
19
|
// src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts
|
|
18
20
|
var _logger = require('@open-draft/logger');
|
|
@@ -155,7 +157,6 @@ function createRequest(clientRequest) {
|
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
// src/interceptors/ClientRequest/NodeClientRequest.ts
|
|
158
|
-
var _deferredpromise = require('@open-draft/deferred-promise');
|
|
159
160
|
var _NodeClientRequest = class extends _http.ClientRequest {
|
|
160
161
|
constructor([url, requestOptions, callback], options) {
|
|
161
162
|
super(requestOptions, callback);
|
|
@@ -203,12 +204,15 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
203
204
|
}
|
|
204
205
|
end(...args) {
|
|
205
206
|
this.logger.info("end", args);
|
|
206
|
-
const requestId =
|
|
207
|
+
const requestId = _chunk5PTPJLB7js.uuidv4.call(void 0, );
|
|
207
208
|
const [chunk, encoding, callback] = normalizeClientRequestEndArgs(...args);
|
|
208
209
|
this.logger.info("normalized arguments:", { chunk, encoding, callback });
|
|
209
210
|
this.writeRequestBodyChunk(chunk, encoding || void 0);
|
|
210
211
|
const capturedRequest = createRequest(this);
|
|
211
|
-
const interactiveRequest =
|
|
212
|
+
const { interactiveRequest, requestController } = _chunk5PTPJLB7js.toInteractiveRequest.call(void 0, capturedRequest);
|
|
213
|
+
Object.defineProperty(capturedRequest, "respondWith", {
|
|
214
|
+
value: requestController.respondWith.bind(requestController)
|
|
215
|
+
});
|
|
212
216
|
if (this.getHeader("X-Request-Id") != null) {
|
|
213
217
|
this.removeHeader("X-Request-Id");
|
|
214
218
|
return this.passthrough(chunk, encoding, callback);
|
|
@@ -217,18 +221,20 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
217
221
|
'emitting the "request" event for %d listener(s)...',
|
|
218
222
|
this.emitter.listenerCount("request")
|
|
219
223
|
);
|
|
220
|
-
this.emitter.
|
|
221
|
-
|
|
222
|
-
|
|
224
|
+
this.emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
225
|
+
if (pendingRequestId !== requestId) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (requestController.responsePromise.state === "pending") {
|
|
229
|
+
requestController.responsePromise.resolve(void 0);
|
|
230
|
+
}
|
|
223
231
|
});
|
|
224
232
|
_until.until.call(void 0, async () => {
|
|
225
|
-
await this.emitter
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
);
|
|
231
|
-
const [mockedResponse] = await interactiveRequest.respondWith.invoked();
|
|
233
|
+
await _chunk5PTPJLB7js.emitAsync.call(void 0, this.emitter, "request", {
|
|
234
|
+
request: interactiveRequest,
|
|
235
|
+
requestId
|
|
236
|
+
});
|
|
237
|
+
const mockedResponse = await requestController.responsePromise;
|
|
232
238
|
this.logger.info("event.respondWith called with:", mockedResponse);
|
|
233
239
|
return mockedResponse;
|
|
234
240
|
}).then((resolverResult) => {
|
|
@@ -722,7 +728,7 @@ function request(protocol, options) {
|
|
|
722
728
|
}
|
|
723
729
|
|
|
724
730
|
// src/interceptors/ClientRequest/index.ts
|
|
725
|
-
var _ClientRequestInterceptor = class extends
|
|
731
|
+
var _ClientRequestInterceptor = class extends _chunk3XFLRXRYjs.Interceptor {
|
|
726
732
|
constructor() {
|
|
727
733
|
super(_ClientRequestInterceptor.interceptorSymbol);
|
|
728
734
|
this.modules = /* @__PURE__ */ new Map();
|
|
@@ -1,132 +1,6 @@
|
|
|
1
1
|
// src/Interceptor.ts
|
|
2
|
-
import { Logger as Logger2 } from "@open-draft/logger";
|
|
3
|
-
|
|
4
|
-
// src/utils/AsyncEventEmitter.ts
|
|
5
2
|
import { Logger } from "@open-draft/logger";
|
|
6
3
|
import { Emitter } from "strict-event-emitter";
|
|
7
|
-
|
|
8
|
-
// src/utils/nextTick.ts
|
|
9
|
-
function nextTick(callback) {
|
|
10
|
-
setTimeout(callback, 0);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// src/utils/AsyncEventEmitter.ts
|
|
14
|
-
var AsyncEventEmitter = class extends Emitter {
|
|
15
|
-
constructor() {
|
|
16
|
-
super();
|
|
17
|
-
this.logger = new Logger("async-event-emitter");
|
|
18
|
-
this.queue = /* @__PURE__ */ new Map();
|
|
19
|
-
this.readyState = "ACTIVE" /* ACTIVE */;
|
|
20
|
-
}
|
|
21
|
-
on(eventName, listener) {
|
|
22
|
-
const logger = this.logger.extend("on");
|
|
23
|
-
logger.info('adding "%s" listener...', eventName);
|
|
24
|
-
if (this.readyState === "DEACTIVATED" /* DEACTIVATED */) {
|
|
25
|
-
logger.info("the emitter is destroyed, skipping!");
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
return super.on(eventName, async (...args) => {
|
|
29
|
-
const queue = this.openListenerQueue(eventName);
|
|
30
|
-
logger.info('awaiting the "%s" listener...', eventName);
|
|
31
|
-
queue.push({
|
|
32
|
-
args,
|
|
33
|
-
done: new Promise(async (resolve, reject) => {
|
|
34
|
-
try {
|
|
35
|
-
await listener(...args);
|
|
36
|
-
resolve();
|
|
37
|
-
logger.info('"%s" listener has resolved!', eventName);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
logger.info('"%s" listener has rejected!', error);
|
|
40
|
-
reject(error);
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
emit(eventName, ...data) {
|
|
47
|
-
const logger = this.logger.extend("emit");
|
|
48
|
-
logger.info('emitting "%s" event...', eventName);
|
|
49
|
-
if (this.readyState === "DEACTIVATED" /* DEACTIVATED */) {
|
|
50
|
-
logger.info("the emitter is destroyed, skipping!");
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
if (this.isInternalEventName(eventName)) {
|
|
54
|
-
return super.emit(eventName, ...data);
|
|
55
|
-
}
|
|
56
|
-
this.openListenerQueue(eventName);
|
|
57
|
-
logger.info('appending a one-time cleanup "%s" listener...', eventName);
|
|
58
|
-
this.once(eventName, () => {
|
|
59
|
-
nextTick(() => {
|
|
60
|
-
this.queue.delete(eventName);
|
|
61
|
-
logger.info('cleaned up "%s" listeners queue!', eventName);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
return super.emit(eventName, ...data);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Returns a promise that resolves when all the listeners for the given event
|
|
68
|
-
* has been called. Awaits asynchronous listeners.
|
|
69
|
-
* If the event has no listeners, resolves immediately.
|
|
70
|
-
*/
|
|
71
|
-
async untilIdle(eventName, filter = () => true) {
|
|
72
|
-
const listenersQueue = this.queue.get(eventName) || [];
|
|
73
|
-
await Promise.all(
|
|
74
|
-
listenersQueue.filter(filter).map(({ done }) => done)
|
|
75
|
-
).finally(() => {
|
|
76
|
-
this.queue.delete(eventName);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
openListenerQueue(eventName) {
|
|
80
|
-
const logger = this.logger.extend("openListenerQueue");
|
|
81
|
-
logger.info('opening "%s" listeners queue...', eventName);
|
|
82
|
-
const queue = this.queue.get(eventName);
|
|
83
|
-
if (!queue) {
|
|
84
|
-
logger.info("no queue found, creating one...");
|
|
85
|
-
this.queue.set(eventName, []);
|
|
86
|
-
return [];
|
|
87
|
-
}
|
|
88
|
-
logger.info("returning an exising queue:", queue);
|
|
89
|
-
return queue;
|
|
90
|
-
}
|
|
91
|
-
removeAllListeners(eventName) {
|
|
92
|
-
const logger = this.logger.extend("removeAllListeners");
|
|
93
|
-
logger.info("event:", eventName);
|
|
94
|
-
if (eventName) {
|
|
95
|
-
this.queue.delete(eventName);
|
|
96
|
-
logger.info(
|
|
97
|
-
'cleared the "%s" listeners queue!',
|
|
98
|
-
eventName,
|
|
99
|
-
this.queue.get(eventName)
|
|
100
|
-
);
|
|
101
|
-
} else {
|
|
102
|
-
this.queue.clear();
|
|
103
|
-
logger.info("cleared the listeners queue!", this.queue);
|
|
104
|
-
}
|
|
105
|
-
return super.removeAllListeners(eventName);
|
|
106
|
-
}
|
|
107
|
-
activate() {
|
|
108
|
-
const logger = this.logger.extend("activate");
|
|
109
|
-
this.readyState = "ACTIVE" /* ACTIVE */;
|
|
110
|
-
logger.info("set state to:", this.readyState);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Deactivate this event emitter.
|
|
114
|
-
* Deactivated emitter can no longer emit and listen to events
|
|
115
|
-
* and needs to be activated again in order to do so.
|
|
116
|
-
*/
|
|
117
|
-
deactivate() {
|
|
118
|
-
const logger = this.logger.extend("deactivate");
|
|
119
|
-
logger.info("removing all listeners...");
|
|
120
|
-
this.removeAllListeners();
|
|
121
|
-
this.readyState = "DEACTIVATED" /* DEACTIVATED */;
|
|
122
|
-
logger.info("set state to:", this.readyState);
|
|
123
|
-
}
|
|
124
|
-
isInternalEventName(eventName) {
|
|
125
|
-
return eventName === "newListener" || eventName === "removeListener";
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
// src/Interceptor.ts
|
|
130
4
|
function getGlobalSymbol(symbol) {
|
|
131
5
|
return (
|
|
132
6
|
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/24587
|
|
@@ -151,9 +25,9 @@ var Interceptor = class {
|
|
|
151
25
|
constructor(symbol) {
|
|
152
26
|
this.symbol = symbol;
|
|
153
27
|
this.readyState = "INACTIVE" /* INACTIVE */;
|
|
154
|
-
this.emitter = new
|
|
28
|
+
this.emitter = new Emitter();
|
|
155
29
|
this.subscriptions = [];
|
|
156
|
-
this.logger = new
|
|
30
|
+
this.logger = new Logger(symbol.description);
|
|
157
31
|
this.emitter.setMaxListeners(0);
|
|
158
32
|
this.logger.info("constructing the interceptor...");
|
|
159
33
|
}
|
|
@@ -181,8 +55,6 @@ var Interceptor = class {
|
|
|
181
55
|
return;
|
|
182
56
|
}
|
|
183
57
|
this.readyState = "APPLYING" /* APPLYING */;
|
|
184
|
-
this.emitter.activate();
|
|
185
|
-
logger.info("activated the emiter!", this.emitter.readyState);
|
|
186
58
|
const runningInstance = this.getInstance();
|
|
187
59
|
if (runningInstance) {
|
|
188
60
|
logger.info("found a running instance, reusing...");
|
|
@@ -193,6 +65,7 @@ var Interceptor = class {
|
|
|
193
65
|
runningInstance.emitter.removeListener(event, listener);
|
|
194
66
|
logger.info('removed proxied "%s" listener!', event);
|
|
195
67
|
});
|
|
68
|
+
return this;
|
|
196
69
|
};
|
|
197
70
|
this.readyState = "APPLIED" /* APPLIED */;
|
|
198
71
|
return;
|
|
@@ -212,14 +85,27 @@ var Interceptor = class {
|
|
|
212
85
|
/**
|
|
213
86
|
* Listen to the interceptor's public events.
|
|
214
87
|
*/
|
|
215
|
-
on(
|
|
88
|
+
on(event, listener) {
|
|
216
89
|
const logger = this.logger.extend("on");
|
|
217
90
|
if (this.readyState === "DISPOSING" /* DISPOSING */ || this.readyState === "DISPOSED" /* DISPOSED */) {
|
|
218
91
|
logger.info("cannot listen to events, already disposed!");
|
|
219
|
-
return;
|
|
92
|
+
return this;
|
|
220
93
|
}
|
|
221
|
-
logger.info('adding "%s" event listener:',
|
|
222
|
-
this.emitter.on(
|
|
94
|
+
logger.info('adding "%s" event listener:', event, listener.name);
|
|
95
|
+
this.emitter.on(event, listener);
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
once(event, listener) {
|
|
99
|
+
this.emitter.once(event, listener);
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
off(event, listener) {
|
|
103
|
+
this.emitter.off(event, listener);
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
removeAllListeners(event) {
|
|
107
|
+
this.emitter.removeAllListeners(event);
|
|
108
|
+
return this;
|
|
223
109
|
}
|
|
224
110
|
/**
|
|
225
111
|
* Disposes of any side-effects this interceptor has introduced.
|
|
@@ -246,7 +132,7 @@ var Interceptor = class {
|
|
|
246
132
|
this.subscriptions = [];
|
|
247
133
|
logger.info("disposed of all subscriptions!", this.subscriptions.length);
|
|
248
134
|
}
|
|
249
|
-
this.emitter.
|
|
135
|
+
this.emitter.removeAllListeners();
|
|
250
136
|
logger.info("destroyed the listener!");
|
|
251
137
|
this.readyState = "DISPOSED" /* DISPOSED */;
|
|
252
138
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
+
emitAsync,
|
|
2
3
|
toInteractiveRequest,
|
|
3
4
|
uuidv4
|
|
4
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-YQGTMMOZ.mjs";
|
|
5
6
|
import {
|
|
6
7
|
Interceptor
|
|
7
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-GM3YBSM3.mjs";
|
|
8
9
|
|
|
9
10
|
// src/interceptors/ClientRequest/index.ts
|
|
10
11
|
import http from "http";
|
|
@@ -13,6 +14,7 @@ import https from "https";
|
|
|
13
14
|
// src/interceptors/ClientRequest/NodeClientRequest.ts
|
|
14
15
|
import { ClientRequest, IncomingMessage as IncomingMessage2 } from "http";
|
|
15
16
|
import { until } from "@open-draft/until";
|
|
17
|
+
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
16
18
|
|
|
17
19
|
// src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts
|
|
18
20
|
import { Logger } from "@open-draft/logger";
|
|
@@ -155,7 +157,6 @@ function createRequest(clientRequest) {
|
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
// src/interceptors/ClientRequest/NodeClientRequest.ts
|
|
158
|
-
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
159
160
|
var _NodeClientRequest = class extends ClientRequest {
|
|
160
161
|
constructor([url, requestOptions, callback], options) {
|
|
161
162
|
super(requestOptions, callback);
|
|
@@ -208,7 +209,10 @@ var _NodeClientRequest = class extends ClientRequest {
|
|
|
208
209
|
this.logger.info("normalized arguments:", { chunk, encoding, callback });
|
|
209
210
|
this.writeRequestBodyChunk(chunk, encoding || void 0);
|
|
210
211
|
const capturedRequest = createRequest(this);
|
|
211
|
-
const interactiveRequest = toInteractiveRequest(capturedRequest);
|
|
212
|
+
const { interactiveRequest, requestController } = toInteractiveRequest(capturedRequest);
|
|
213
|
+
Object.defineProperty(capturedRequest, "respondWith", {
|
|
214
|
+
value: requestController.respondWith.bind(requestController)
|
|
215
|
+
});
|
|
212
216
|
if (this.getHeader("X-Request-Id") != null) {
|
|
213
217
|
this.removeHeader("X-Request-Id");
|
|
214
218
|
return this.passthrough(chunk, encoding, callback);
|
|
@@ -217,18 +221,20 @@ var _NodeClientRequest = class extends ClientRequest {
|
|
|
217
221
|
'emitting the "request" event for %d listener(s)...',
|
|
218
222
|
this.emitter.listenerCount("request")
|
|
219
223
|
);
|
|
220
|
-
this.emitter.
|
|
221
|
-
|
|
222
|
-
|
|
224
|
+
this.emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
225
|
+
if (pendingRequestId !== requestId) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (requestController.responsePromise.state === "pending") {
|
|
229
|
+
requestController.responsePromise.resolve(void 0);
|
|
230
|
+
}
|
|
223
231
|
});
|
|
224
232
|
until(async () => {
|
|
225
|
-
await this.emitter
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
);
|
|
231
|
-
const [mockedResponse] = await interactiveRequest.respondWith.invoked();
|
|
233
|
+
await emitAsync(this.emitter, "request", {
|
|
234
|
+
request: interactiveRequest,
|
|
235
|
+
requestId
|
|
236
|
+
});
|
|
237
|
+
const mockedResponse = await requestController.responsePromise;
|
|
232
238
|
this.logger.info("event.respondWith called with:", mockedResponse);
|
|
233
239
|
return mockedResponse;
|
|
234
240
|
}).then((resolverResult) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Interceptor
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-GM3YBSM3.mjs";
|
|
4
4
|
|
|
5
5
|
// src/BatchInterceptor.ts
|
|
6
6
|
var BatchInterceptor = class extends Interceptor {
|
|
@@ -20,9 +20,28 @@ var BatchInterceptor = class extends Interceptor {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
on(event, listener) {
|
|
23
|
-
this.interceptors
|
|
23
|
+
for (const interceptor of this.interceptors) {
|
|
24
24
|
interceptor.on(event, listener);
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
once(event, listener) {
|
|
29
|
+
for (const interceptor of this.interceptors) {
|
|
30
|
+
interceptor.once(event, listener);
|
|
31
|
+
}
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
off(event, listener) {
|
|
35
|
+
for (const interceptor of this.interceptors) {
|
|
36
|
+
interceptor.off(event, listener);
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
removeAllListeners(event) {
|
|
41
|
+
for (const interceptors of this.interceptors) {
|
|
42
|
+
interceptors.removeAllListeners(event);
|
|
43
|
+
}
|
|
44
|
+
return this;
|
|
26
45
|
}
|
|
27
46
|
};
|
|
28
47
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk3XFLRXRYjs = require('./chunk-3XFLRXRY.js');
|
|
4
4
|
|
|
5
5
|
// src/BatchInterceptor.ts
|
|
6
|
-
var BatchInterceptor = class extends
|
|
6
|
+
var BatchInterceptor = class extends _chunk3XFLRXRYjs.Interceptor {
|
|
7
7
|
constructor(options) {
|
|
8
8
|
BatchInterceptor.symbol = Symbol(options.name);
|
|
9
9
|
super(BatchInterceptor.symbol);
|
|
@@ -20,9 +20,28 @@ var BatchInterceptor = class extends _chunkXYZRP5S2js.Interceptor {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
on(event, listener) {
|
|
23
|
-
this.interceptors
|
|
23
|
+
for (const interceptor of this.interceptors) {
|
|
24
24
|
interceptor.on(event, listener);
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
once(event, listener) {
|
|
29
|
+
for (const interceptor of this.interceptors) {
|
|
30
|
+
interceptor.once(event, listener);
|
|
31
|
+
}
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
off(event, listener) {
|
|
35
|
+
for (const interceptor of this.interceptors) {
|
|
36
|
+
interceptor.off(event, listener);
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
removeAllListeners(event) {
|
|
41
|
+
for (const interceptors of this.interceptors) {
|
|
42
|
+
interceptors.removeAllListeners(event);
|
|
43
|
+
}
|
|
44
|
+
return this;
|
|
26
45
|
}
|
|
27
46
|
};
|
|
28
47
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/utils/RequestController.ts
|
|
2
|
+
import { invariant } from "outvariant";
|
|
3
|
+
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
4
|
+
var RequestController = class {
|
|
5
|
+
constructor(request) {
|
|
6
|
+
this.request = request;
|
|
7
|
+
this.responsePromise = new DeferredPromise();
|
|
8
|
+
}
|
|
9
|
+
respondWith(response) {
|
|
10
|
+
invariant(
|
|
11
|
+
this.responsePromise.state === "pending",
|
|
12
|
+
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
13
|
+
this.request.method,
|
|
14
|
+
this.request.url
|
|
15
|
+
);
|
|
16
|
+
this.responsePromise.resolve(response);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/utils/toInteractiveRequest.ts
|
|
21
|
+
function toInteractiveRequest(request) {
|
|
22
|
+
const requestController = new RequestController(request);
|
|
23
|
+
Reflect.set(
|
|
24
|
+
request,
|
|
25
|
+
"respondWith",
|
|
26
|
+
requestController.respondWith.bind(requestController)
|
|
27
|
+
);
|
|
28
|
+
return {
|
|
29
|
+
interactiveRequest: request,
|
|
30
|
+
requestController
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/utils/uuid.ts
|
|
35
|
+
function uuidv4() {
|
|
36
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
37
|
+
const r = Math.random() * 16 | 0;
|
|
38
|
+
const v = c == "x" ? r : r & 3 | 8;
|
|
39
|
+
return v.toString(16);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/utils/emitAsync.ts
|
|
44
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
45
|
+
const listners = emitter.listeners(eventName);
|
|
46
|
+
if (listners.length === 0) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
for (const listener of listners) {
|
|
50
|
+
await listener.apply(emitter, data);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
toInteractiveRequest,
|
|
56
|
+
uuidv4,
|
|
57
|
+
emitAsync
|
|
58
|
+
};
|
package/lib/node/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { E as ExtractEventNames, H as HttpRequestEventMap, I as IS_PATCHED_MODULE, e as Interceptor, a as InterceptorEventMap, c as InterceptorReadyState, b as InterceptorSubscription, R as RequestCredentials, d as deleteGlobalSymbol, g as getGlobalSymbol } from './Interceptor-
|
|
2
|
-
export { a as BatchInterceptor, B as BatchInterceptorOptions, E as ExtractEventMapType } from './BatchInterceptor-
|
|
1
|
+
export { E as ExtractEventNames, H as HttpRequestEventMap, I as IS_PATCHED_MODULE, e as Interceptor, a as InterceptorEventMap, c as InterceptorReadyState, b as InterceptorSubscription, R as RequestCredentials, d as deleteGlobalSymbol, g as getGlobalSymbol } from './Interceptor-7a701c1f.js';
|
|
2
|
+
export { a as BatchInterceptor, B as BatchInterceptorOptions, E as ExtractEventMapType } from './BatchInterceptor-9785c567.js';
|
|
3
|
+
import '@open-draft/deferred-promise';
|
|
3
4
|
import '@open-draft/logger';
|
|
4
5
|
import 'strict-event-emitter';
|
|
5
6
|
|
package/lib/node/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkUF7QIAQ5js = require('./chunk-UF7QIAQ5.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
@@ -13,7 +13,7 @@ var _chunkVQ4DZOBBjs = require('./chunk-VQ4DZOBB.js');
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var _chunk3XFLRXRYjs = require('./chunk-3XFLRXRY.js');
|
|
17
17
|
|
|
18
18
|
// src/utils/getCleanUrl.ts
|
|
19
19
|
function getCleanUrl(url, isAbsolute = true) {
|
|
@@ -29,4 +29,4 @@ function getCleanUrl(url, isAbsolute = true) {
|
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
exports.BatchInterceptor =
|
|
32
|
+
exports.BatchInterceptor = _chunkUF7QIAQ5js.BatchInterceptor; exports.IS_PATCHED_MODULE = _chunkVQ4DZOBBjs.IS_PATCHED_MODULE; exports.Interceptor = _chunk3XFLRXRYjs.Interceptor; exports.InterceptorReadyState = _chunk3XFLRXRYjs.InterceptorReadyState; exports.decodeBuffer = _chunk3LFH2WCFjs.decodeBuffer; exports.deleteGlobalSymbol = _chunk3XFLRXRYjs.deleteGlobalSymbol; exports.encodeBuffer = _chunk3LFH2WCFjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunk3XFLRXRYjs.getGlobalSymbol;
|
package/lib/node/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BatchInterceptor
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-UBEFEZXT.mjs";
|
|
4
4
|
import {
|
|
5
5
|
decodeBuffer,
|
|
6
6
|
encodeBuffer
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
InterceptorReadyState,
|
|
14
14
|
deleteGlobalSymbol,
|
|
15
15
|
getGlobalSymbol
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-GM3YBSM3.mjs";
|
|
17
17
|
|
|
18
18
|
// src/utils/getCleanUrl.ts
|
|
19
19
|
function getCleanUrl(url, isAbsolute = true) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import http from 'http';
|
|
2
2
|
import https from 'https';
|
|
3
|
-
import {
|
|
3
|
+
import { Emitter } from 'strict-event-emitter';
|
|
4
|
+
import { H as HttpRequestEventMap, e as Interceptor } from '../../Interceptor-7a701c1f.js';
|
|
5
|
+
import '@open-draft/deferred-promise';
|
|
4
6
|
import '@open-draft/logger';
|
|
5
|
-
import 'strict-event-emitter';
|
|
6
7
|
|
|
7
8
|
type Protocol = 'http' | 'https';
|
|
8
9
|
|
|
9
|
-
type ClientRequestEmitter =
|
|
10
|
+
type ClientRequestEmitter = Emitter<HttpRequestEventMap>;
|
|
10
11
|
type ClientRequestModules = Map<Protocol, typeof http | typeof https>;
|
|
11
12
|
/**
|
|
12
13
|
* Intercept requests made via the `ClientRequest` class.
|