@mswjs/interceptors 0.22.16 → 0.24.0
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/README.md +29 -11
- package/lib/browser/{Interceptor-c8fc448a.d.ts → Interceptor-0a020bc4.d.ts} +14 -2
- package/lib/browser/{chunk-OQKDMHBL.js → chunk-2TENISKM.js} +21 -18
- package/lib/{node/chunk-IHW3ERPT.js → browser/chunk-3LFH2WCF.js} +1 -9
- package/lib/{node/chunk-MPFSBY4S.js → browser/chunk-4CFMDU7Z.js} +36 -9
- package/lib/{node/chunk-LTX5IGCQ.mjs → browser/chunk-7II4SWKS.mjs} +0 -8
- package/lib/browser/{chunk-CCRODCJY.mjs → chunk-B74BGPYH.mjs} +89 -24
- package/lib/browser/{chunk-MW6NCDWE.mjs → chunk-GXJLJMOT.mjs} +33 -7
- package/lib/browser/{chunk-G2NATYCY.mjs → chunk-LHYX2GOM.mjs} +17 -14
- package/lib/browser/{chunk-RODLGD6G.js → chunk-UGP4JOAM.js} +96 -31
- package/lib/browser/index.d.ts +3 -3
- package/lib/browser/index.js +4 -4
- package/lib/browser/index.mjs +2 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +5 -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 +1 -1
- 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 +1 -1
- package/lib/browser/presets/browser.js +6 -6
- package/lib/browser/presets/browser.mjs +4 -4
- package/lib/node/{BatchInterceptor-fe69020d.d.ts → BatchInterceptor-c841b068.d.ts} +1 -1
- package/lib/node/{Interceptor-f9dfe016.d.ts → Interceptor-738f79c5.d.ts} +14 -2
- package/lib/node/RemoteHttpInterceptor.d.ts +4 -5
- package/lib/node/RemoteHttpInterceptor.js +27 -25
- package/lib/node/RemoteHttpInterceptor.mjs +21 -19
- package/lib/node/chunk-3LFH2WCF.js +21 -0
- package/lib/node/chunk-7II4SWKS.mjs +21 -0
- package/lib/node/{chunk-VJDB3MIV.js → chunk-MVPEJK4V.js} +2 -2
- package/lib/node/{chunk-QRCYLMPE.js → chunk-OOSIWXHX.js} +57 -16
- package/lib/node/{chunk-R6MTHW6S.mjs → chunk-PSIO3L7D.mjs} +55 -14
- package/lib/node/{chunk-Y5QA6OEZ.mjs → chunk-RGYCLCLK.mjs} +33 -10
- package/lib/node/{chunk-IAIFDHPP.mjs → chunk-UWSK5F3S.mjs} +89 -24
- package/lib/node/{chunk-NUSH7ACE.mjs → chunk-VS3GJPUE.mjs} +1 -1
- package/lib/{browser/chunk-7VJMJSIJ.js → node/chunk-XYZRP5S2.js} +35 -13
- package/lib/node/{chunk-YFXBOTHW.js → chunk-YCEMBJEM.js} +93 -28
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +4 -4
- package/lib/node/index.mjs +3 -3
- package/lib/node/interceptors/ClientRequest/index.d.ts +1 -1
- 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 +5 -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 +1 -1
- package/lib/node/interceptors/fetch/index.js +18 -15
- package/lib/node/interceptors/fetch/index.mjs +17 -14
- package/lib/node/presets/node.d.ts +1 -1
- package/lib/node/presets/node.js +6 -6
- package/lib/node/presets/node.mjs +4 -4
- package/package.json +15 -18
- package/src/RemoteHttpInterceptor.ts +19 -16
- package/src/glossary.ts +14 -2
- package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +5 -5
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +17 -9
- package/src/interceptors/ClientRequest/index.test.ts +2 -2
- package/src/interceptors/ClientRequest/utils/createRequest.ts +0 -1
- package/src/interceptors/ClientRequest/utils/createResponse.ts +34 -3
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +53 -23
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +16 -7
- package/src/interceptors/XMLHttpRequest/index.ts +3 -3
- package/src/interceptors/XMLHttpRequest/utils/createResponse.ts +27 -5
- package/src/interceptors/fetch/index.ts +18 -15
- package/src/utils/bufferUtils.ts +0 -2
- package/lib/browser/chunk-MQA5WAD4.mjs +0 -2139
- package/lib/browser/chunk-QAZ3SPQZ.js +0 -2139
- package/src/shims/webEncoding.ts +0 -9
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
1
|
// src/glossary.ts
|
|
7
2
|
var IS_PATCHED_MODULE = Symbol("isPatchedModule");
|
|
8
3
|
|
|
@@ -71,6 +66,11 @@ var AsyncEventEmitter = class extends Emitter {
|
|
|
71
66
|
});
|
|
72
67
|
return super.emit(eventName, ...data);
|
|
73
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a promise that resolves when all the listeners for the given event
|
|
71
|
+
* has been called. Awaits asynchronous listeners.
|
|
72
|
+
* If the event has no listeners, resolves immediately.
|
|
73
|
+
*/
|
|
74
74
|
async untilIdle(eventName, filter = () => true) {
|
|
75
75
|
const listenersQueue = this.queue.get(eventName) || [];
|
|
76
76
|
await Promise.all(
|
|
@@ -112,6 +112,11 @@ var AsyncEventEmitter = class extends Emitter {
|
|
|
112
112
|
this.readyState = "ACTIVE" /* ACTIVE */;
|
|
113
113
|
logger.info("set state to:", this.readyState);
|
|
114
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Deactivate this event emitter.
|
|
117
|
+
* Deactivated emitter can no longer emit and listen to events
|
|
118
|
+
* and needs to be activated again in order to do so.
|
|
119
|
+
*/
|
|
115
120
|
deactivate() {
|
|
116
121
|
const logger = this.logger.extend("deactivate");
|
|
117
122
|
logger.info("removing all listeners...");
|
|
@@ -126,7 +131,10 @@ var AsyncEventEmitter = class extends Emitter {
|
|
|
126
131
|
|
|
127
132
|
// src/Interceptor.ts
|
|
128
133
|
function getGlobalSymbol(symbol) {
|
|
129
|
-
return
|
|
134
|
+
return (
|
|
135
|
+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/24587
|
|
136
|
+
globalThis[symbol] || void 0
|
|
137
|
+
);
|
|
130
138
|
}
|
|
131
139
|
function setGlobalSymbol(symbol, value) {
|
|
132
140
|
globalThis[symbol] = value;
|
|
@@ -152,9 +160,17 @@ var Interceptor = class {
|
|
|
152
160
|
this.emitter.setMaxListeners(0);
|
|
153
161
|
this.logger.info("constructing the interceptor...");
|
|
154
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Determine if this interceptor can be applied
|
|
165
|
+
* in the current environment.
|
|
166
|
+
*/
|
|
155
167
|
checkEnvironment() {
|
|
156
168
|
return true;
|
|
157
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Apply this interceptor to the current process.
|
|
172
|
+
* Returns an already running interceptor instance if it's present.
|
|
173
|
+
*/
|
|
158
174
|
apply() {
|
|
159
175
|
const logger = this.logger.extend("apply");
|
|
160
176
|
logger.info("applying the interceptor...");
|
|
@@ -189,8 +205,16 @@ var Interceptor = class {
|
|
|
189
205
|
this.setInstance();
|
|
190
206
|
this.readyState = "APPLIED" /* APPLIED */;
|
|
191
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Setup the module augments and stubs necessary for this interceptor.
|
|
210
|
+
* This method is not run if there's a running interceptor instance
|
|
211
|
+
* to prevent instantiating an interceptor multiple times.
|
|
212
|
+
*/
|
|
192
213
|
setup() {
|
|
193
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Listen to the interceptor's public events.
|
|
217
|
+
*/
|
|
194
218
|
on(eventName, listener) {
|
|
195
219
|
const logger = this.logger.extend("on");
|
|
196
220
|
if (this.readyState === "DISPOSING" /* DISPOSING */ || this.readyState === "DISPOSED" /* DISPOSED */) {
|
|
@@ -200,6 +224,9 @@ var Interceptor = class {
|
|
|
200
224
|
logger.info('adding "%s" event listener:', eventName, listener.name);
|
|
201
225
|
this.emitter.on(eventName, listener);
|
|
202
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Disposes of any side-effects this interceptor has introduced.
|
|
229
|
+
*/
|
|
203
230
|
dispose() {
|
|
204
231
|
const logger = this.logger.extend("dispose");
|
|
205
232
|
if (this.readyState === "DISPOSED" /* DISPOSED */) {
|
|
@@ -243,7 +270,6 @@ var Interceptor = class {
|
|
|
243
270
|
};
|
|
244
271
|
|
|
245
272
|
export {
|
|
246
|
-
__commonJS,
|
|
247
273
|
IS_PATCHED_MODULE,
|
|
248
274
|
getGlobalSymbol,
|
|
249
275
|
deleteGlobalSymbol,
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
IS_PATCHED_MODULE,
|
|
7
7
|
Interceptor
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-GXJLJMOT.mjs";
|
|
9
9
|
|
|
10
10
|
// src/interceptors/fetch/index.ts
|
|
11
11
|
import { invariant } from "outvariant";
|
|
@@ -33,12 +33,15 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
33
33
|
'emitting the "request" event for %d listener(s)...',
|
|
34
34
|
this.emitter.listenerCount("request")
|
|
35
35
|
);
|
|
36
|
-
this.emitter.emit("request",
|
|
36
|
+
this.emitter.emit("request", {
|
|
37
|
+
request: interactiveRequest,
|
|
38
|
+
requestId
|
|
39
|
+
});
|
|
37
40
|
this.logger.info("awaiting for the mocked response...");
|
|
38
41
|
const resolverResult = await until(async () => {
|
|
39
42
|
await this.emitter.untilIdle(
|
|
40
43
|
"request",
|
|
41
|
-
({ args: [
|
|
44
|
+
({ args: [{ requestId: pendingRequestId }] }) => {
|
|
42
45
|
return pendingRequestId === requestId;
|
|
43
46
|
}
|
|
44
47
|
);
|
|
@@ -56,13 +59,13 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
56
59
|
const mockedResponse = resolverResult.data;
|
|
57
60
|
if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
|
|
58
61
|
this.logger.info("received mocked response:", mockedResponse);
|
|
59
|
-
const
|
|
60
|
-
this.emitter.emit(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
interactiveRequest,
|
|
62
|
+
const responseClone = mockedResponse.clone();
|
|
63
|
+
this.emitter.emit("response", {
|
|
64
|
+
response: responseClone,
|
|
65
|
+
isMockedResponse: true,
|
|
66
|
+
request: interactiveRequest,
|
|
64
67
|
requestId
|
|
65
|
-
);
|
|
68
|
+
});
|
|
66
69
|
const response = new Response(mockedResponse.body, mockedResponse);
|
|
67
70
|
Object.defineProperty(response, "url", {
|
|
68
71
|
writable: false,
|
|
@@ -76,12 +79,12 @@ var _FetchInterceptor = class extends Interceptor {
|
|
|
76
79
|
return pureFetch(request).then((response) => {
|
|
77
80
|
const responseClone = response.clone();
|
|
78
81
|
this.logger.info("original fetch performed", responseClone);
|
|
79
|
-
this.emitter.emit(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
interactiveRequest,
|
|
82
|
+
this.emitter.emit("response", {
|
|
83
|
+
response: responseClone,
|
|
84
|
+
isMockedResponse: false,
|
|
85
|
+
request: interactiveRequest,
|
|
83
86
|
requestId
|
|
84
|
-
);
|
|
87
|
+
});
|
|
85
88
|
return response;
|
|
86
89
|
});
|
|
87
90
|
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunk3LFH2WCFjs = require('./chunk-3LFH2WCF.js');
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
@@ -10,7 +10,7 @@ var _chunkPCFJD76Xjs = require('./chunk-PCFJD76X.js');
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
var
|
|
13
|
+
var _chunk4CFMDU7Zjs = require('./chunk-4CFMDU7Z.js');
|
|
14
14
|
|
|
15
15
|
// src/interceptors/XMLHttpRequest/index.ts
|
|
16
16
|
var _outvariant = require('outvariant');
|
|
@@ -19,7 +19,8 @@ var _outvariant = require('outvariant');
|
|
|
19
19
|
var _until = require('@open-draft/until');
|
|
20
20
|
|
|
21
21
|
// src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
var _isnodeprocess = require('is-node-process');
|
|
23
24
|
|
|
24
25
|
// src/interceptors/XMLHttpRequest/utils/concatArrayBuffer.ts
|
|
25
26
|
function concatArrayBuffer(left, right) {
|
|
@@ -201,17 +202,32 @@ function parseJson(data) {
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
// src/interceptors/XMLHttpRequest/utils/createResponse.ts
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return new Response(responseBody, {
|
|
205
|
+
function createResponse(request, body) {
|
|
206
|
+
return new Response(body, {
|
|
207
207
|
status: request.status,
|
|
208
208
|
statusText: request.statusText,
|
|
209
|
-
headers:
|
|
209
|
+
headers: createHeadersFromXMLHttpReqestHeaders(
|
|
210
|
+
request.getAllResponseHeaders()
|
|
211
|
+
)
|
|
210
212
|
});
|
|
211
213
|
}
|
|
214
|
+
function createHeadersFromXMLHttpReqestHeaders(headersString) {
|
|
215
|
+
const headers = new Headers();
|
|
216
|
+
const lines = headersString.split(/[\r\n]+/);
|
|
217
|
+
for (const line of lines) {
|
|
218
|
+
if (line.trim() === "") {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const [name, ...parts] = line.split(": ");
|
|
222
|
+
const value = parts.join(": ");
|
|
223
|
+
headers.append(name, value);
|
|
224
|
+
}
|
|
225
|
+
return headers;
|
|
226
|
+
}
|
|
212
227
|
|
|
213
228
|
// src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts
|
|
214
|
-
|
|
229
|
+
var IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
|
|
230
|
+
var IS_NODE = _isnodeprocess.isNodeProcess.call(void 0, );
|
|
215
231
|
var XMLHttpRequestController = class {
|
|
216
232
|
constructor(initialRequest, logger) {
|
|
217
233
|
this.initialRequest = initialRequest;
|
|
@@ -219,6 +235,7 @@ var XMLHttpRequestController = class {
|
|
|
219
235
|
this.method = "GET";
|
|
220
236
|
this.url = null;
|
|
221
237
|
this.events = /* @__PURE__ */ new Map();
|
|
238
|
+
this.requestId = _chunkPCFJD76Xjs.uuidv4.call(void 0, );
|
|
222
239
|
this.requestHeaders = new Headers();
|
|
223
240
|
this.responseBuffer = new Uint8Array();
|
|
224
241
|
this.request = createProxy(initialRequest, {
|
|
@@ -241,7 +258,6 @@ var XMLHttpRequestController = class {
|
|
|
241
258
|
switch (methodName) {
|
|
242
259
|
case "open": {
|
|
243
260
|
const [method, url] = args;
|
|
244
|
-
this.requestId = _chunkPCFJD76Xjs.uuidv4.call(void 0, );
|
|
245
261
|
if (typeof url === "undefined") {
|
|
246
262
|
this.method = "GET";
|
|
247
263
|
this.url = toAbsoluteUrl(method);
|
|
@@ -268,31 +284,41 @@ var XMLHttpRequestController = class {
|
|
|
268
284
|
case "send": {
|
|
269
285
|
const [body] = args;
|
|
270
286
|
if (body != null) {
|
|
271
|
-
this.requestBody = typeof body === "string" ?
|
|
287
|
+
this.requestBody = typeof body === "string" ? _chunk3LFH2WCFjs.encodeBuffer.call(void 0, body) : body;
|
|
272
288
|
}
|
|
273
289
|
this.request.addEventListener("load", () => {
|
|
274
290
|
if (typeof this.onResponse !== "undefined") {
|
|
275
291
|
const fetchResponse = createResponse(
|
|
276
292
|
this.request,
|
|
293
|
+
/**
|
|
294
|
+
* The `response` property is the right way to read
|
|
295
|
+
* the ambiguous response body, as the request's "responseType" may differ.
|
|
296
|
+
* @see https://xhr.spec.whatwg.org/#the-response-attribute
|
|
297
|
+
*/
|
|
277
298
|
this.request.response
|
|
278
299
|
);
|
|
279
|
-
this.onResponse.call(
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
fetchRequest,
|
|
283
|
-
this.requestId
|
|
284
|
-
);
|
|
300
|
+
this.onResponse.call(this, {
|
|
301
|
+
response: fetchResponse,
|
|
302
|
+
isMockedResponse: IS_MOCKED_RESPONSE in this.request,
|
|
303
|
+
request: fetchRequest,
|
|
304
|
+
requestId: this.requestId
|
|
305
|
+
});
|
|
285
306
|
}
|
|
286
307
|
});
|
|
287
308
|
const fetchRequest = this.toFetchApiRequest();
|
|
288
|
-
const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this,
|
|
309
|
+
const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
|
|
310
|
+
request: fetchRequest,
|
|
311
|
+
requestId: this.requestId
|
|
312
|
+
})) || Promise.resolve();
|
|
289
313
|
onceRequestSettled.finally(() => {
|
|
290
314
|
if (this.request.readyState < this.request.LOADING) {
|
|
291
315
|
this.logger.info(
|
|
292
316
|
"request callback settled but request has not been handled (readystate %d), performing as-is...",
|
|
293
317
|
this.request.readyState
|
|
294
318
|
);
|
|
295
|
-
|
|
319
|
+
if (IS_NODE) {
|
|
320
|
+
this.request.setRequestHeader("X-Request-Id", this.requestId);
|
|
321
|
+
}
|
|
296
322
|
return invoke();
|
|
297
323
|
}
|
|
298
324
|
});
|
|
@@ -311,12 +337,17 @@ var XMLHttpRequestController = class {
|
|
|
311
337
|
this.events.set(eventName, nextEvents);
|
|
312
338
|
this.logger.info('registered event "%s"', eventName, listener.name);
|
|
313
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Responds to the current request with the given
|
|
342
|
+
* Fetch API `Response` instance.
|
|
343
|
+
*/
|
|
314
344
|
respondWith(response) {
|
|
315
345
|
this.logger.info(
|
|
316
346
|
"responding with a mocked response: %d %s",
|
|
317
347
|
response.status,
|
|
318
348
|
response.statusText
|
|
319
349
|
);
|
|
350
|
+
define(this.request, IS_MOCKED_RESPONSE, true);
|
|
320
351
|
define(this.request, "status", response.status);
|
|
321
352
|
define(this.request, "statusText", response.statusText);
|
|
322
353
|
define(this.request, "responseURL", this.url.href);
|
|
@@ -345,7 +376,10 @@ var XMLHttpRequestController = class {
|
|
|
345
376
|
this.logger.info("headers not received yet, returning empty string");
|
|
346
377
|
return "";
|
|
347
378
|
}
|
|
348
|
-
const
|
|
379
|
+
const headersList = Array.from(response.headers.entries());
|
|
380
|
+
const allHeaders = headersList.map(([headerName, headerValue]) => {
|
|
381
|
+
return `${headerName}: ${headerValue}`;
|
|
382
|
+
}).join("\r\n");
|
|
349
383
|
this.logger.info("resolved all response headers to", allHeaders);
|
|
350
384
|
return allHeaders;
|
|
351
385
|
}
|
|
@@ -368,7 +402,12 @@ var XMLHttpRequestController = class {
|
|
|
368
402
|
get: () => this.responseXML
|
|
369
403
|
}
|
|
370
404
|
});
|
|
371
|
-
const totalResponseBodyLength = response.headers.has("Content-Length") ? Number(response.headers.get("Content-Length")) :
|
|
405
|
+
const totalResponseBodyLength = response.headers.has("Content-Length") ? Number(response.headers.get("Content-Length")) : (
|
|
406
|
+
/**
|
|
407
|
+
* @todo Infer the response body length from the response body.
|
|
408
|
+
*/
|
|
409
|
+
void 0
|
|
410
|
+
);
|
|
372
411
|
this.logger.info("calculated response body length", totalResponseBodyLength);
|
|
373
412
|
this.trigger("loadstart", {
|
|
374
413
|
loaded: 0,
|
|
@@ -414,7 +453,7 @@ var XMLHttpRequestController = class {
|
|
|
414
453
|
}
|
|
415
454
|
}
|
|
416
455
|
responseBufferToText() {
|
|
417
|
-
return
|
|
456
|
+
return _chunk3LFH2WCFjs.decodeBuffer.call(void 0, this.responseBuffer);
|
|
418
457
|
}
|
|
419
458
|
get response() {
|
|
420
459
|
this.logger.info(
|
|
@@ -431,7 +470,7 @@ var XMLHttpRequestController = class {
|
|
|
431
470
|
return responseJson;
|
|
432
471
|
}
|
|
433
472
|
case "arraybuffer": {
|
|
434
|
-
const arrayBuffer =
|
|
473
|
+
const arrayBuffer = _chunk3LFH2WCFjs.toArrayBuffer.call(void 0, this.responseBuffer);
|
|
435
474
|
this.logger.info("resolved response ArrayBuffer", arrayBuffer);
|
|
436
475
|
return arrayBuffer;
|
|
437
476
|
}
|
|
@@ -499,6 +538,9 @@ var XMLHttpRequestController = class {
|
|
|
499
538
|
this.trigger("error");
|
|
500
539
|
this.trigger("loadend");
|
|
501
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Transitions this request's `readyState` to the given one.
|
|
543
|
+
*/
|
|
502
544
|
setReadyState(nextReadyState) {
|
|
503
545
|
this.logger.info(
|
|
504
546
|
"setReadyState: %d -> %d",
|
|
@@ -516,6 +558,9 @@ var XMLHttpRequestController = class {
|
|
|
516
558
|
this.trigger("readystatechange");
|
|
517
559
|
}
|
|
518
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* Triggers given event on the `XMLHttpRequest` instance.
|
|
563
|
+
*/
|
|
519
564
|
trigger(eventName, options) {
|
|
520
565
|
const callback = this.request[`on${eventName}`];
|
|
521
566
|
const event = createEvent(this.request, eventName, options);
|
|
@@ -535,11 +580,17 @@ var XMLHttpRequestController = class {
|
|
|
535
580
|
}
|
|
536
581
|
}
|
|
537
582
|
}
|
|
583
|
+
/**
|
|
584
|
+
* Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.
|
|
585
|
+
*/
|
|
538
586
|
toFetchApiRequest() {
|
|
539
587
|
this.logger.info("converting request to a Fetch API Request...");
|
|
540
588
|
const fetchRequest = new Request(this.url.href, {
|
|
541
589
|
method: this.method,
|
|
542
590
|
headers: this.requestHeaders,
|
|
591
|
+
/**
|
|
592
|
+
* @see https://xhr.spec.whatwg.org/#cross-origin-credentials
|
|
593
|
+
*/
|
|
543
594
|
credentials: this.request.withCredentials ? "include" : "same-origin",
|
|
544
595
|
body: ["GET", "HEAD"].includes(this.method) ? null : this.requestBody
|
|
545
596
|
});
|
|
@@ -573,6 +624,7 @@ function toAbsoluteUrl(url) {
|
|
|
573
624
|
}
|
|
574
625
|
function define(target, property, value) {
|
|
575
626
|
Reflect.defineProperty(target, property, {
|
|
627
|
+
// Ensure writable properties to allow redefining readonly properties.
|
|
576
628
|
writable: true,
|
|
577
629
|
enumerable: true,
|
|
578
630
|
value
|
|
@@ -602,18 +654,21 @@ function createXMLHttpRequestProxy({
|
|
|
602
654
|
originalRequest,
|
|
603
655
|
logger
|
|
604
656
|
);
|
|
605
|
-
requestController.onRequest = async function(request, requestId) {
|
|
657
|
+
requestController.onRequest = async function({ request, requestId }) {
|
|
606
658
|
const interactiveRequest = _chunkPCFJD76Xjs.toInteractiveRequest.call(void 0, request);
|
|
607
659
|
this.logger.info(
|
|
608
660
|
'emitting the "request" event for %s listener(s)...',
|
|
609
661
|
emitter.listenerCount("request")
|
|
610
662
|
);
|
|
611
|
-
emitter.emit("request",
|
|
663
|
+
emitter.emit("request", {
|
|
664
|
+
request: interactiveRequest,
|
|
665
|
+
requestId
|
|
666
|
+
});
|
|
612
667
|
this.logger.info("awaiting mocked response...");
|
|
613
668
|
const resolverResult = await _until.until.call(void 0, async () => {
|
|
614
669
|
await emitter.untilIdle(
|
|
615
670
|
"request",
|
|
616
|
-
({ args: [
|
|
671
|
+
({ args: [{ requestId: pendingRequestId }] }) => {
|
|
617
672
|
return pendingRequestId === requestId;
|
|
618
673
|
}
|
|
619
674
|
);
|
|
@@ -643,12 +698,22 @@ function createXMLHttpRequestProxy({
|
|
|
643
698
|
"no mocked response received, performing request as-is..."
|
|
644
699
|
);
|
|
645
700
|
};
|
|
646
|
-
requestController.onResponse = async function(
|
|
701
|
+
requestController.onResponse = async function({
|
|
702
|
+
response,
|
|
703
|
+
isMockedResponse,
|
|
704
|
+
request,
|
|
705
|
+
requestId
|
|
706
|
+
}) {
|
|
647
707
|
this.logger.info(
|
|
648
708
|
'emitting the "response" event for %s listener(s)...',
|
|
649
709
|
emitter.listenerCount("response")
|
|
650
710
|
);
|
|
651
|
-
emitter.emit("response",
|
|
711
|
+
emitter.emit("response", {
|
|
712
|
+
response,
|
|
713
|
+
isMockedResponse,
|
|
714
|
+
request,
|
|
715
|
+
requestId
|
|
716
|
+
});
|
|
652
717
|
};
|
|
653
718
|
return requestController.request;
|
|
654
719
|
}
|
|
@@ -657,7 +722,7 @@ function createXMLHttpRequestProxy({
|
|
|
657
722
|
}
|
|
658
723
|
|
|
659
724
|
// src/interceptors/XMLHttpRequest/index.ts
|
|
660
|
-
var _XMLHttpRequestInterceptor = class extends
|
|
725
|
+
var _XMLHttpRequestInterceptor = class extends _chunk4CFMDU7Zjs.Interceptor {
|
|
661
726
|
constructor() {
|
|
662
727
|
super(_XMLHttpRequestInterceptor.interceptorSymbol);
|
|
663
728
|
}
|
|
@@ -669,7 +734,7 @@ var _XMLHttpRequestInterceptor = class extends _chunk7VJMJSIJjs.Interceptor {
|
|
|
669
734
|
logger.info('patching "XMLHttpRequest" module...');
|
|
670
735
|
const PureXMLHttpRequest = globalThis.XMLHttpRequest;
|
|
671
736
|
_outvariant.invariant.call(void 0,
|
|
672
|
-
!PureXMLHttpRequest[
|
|
737
|
+
!PureXMLHttpRequest[_chunk4CFMDU7Zjs.IS_PATCHED_MODULE],
|
|
673
738
|
'Failed to patch the "XMLHttpRequest" module: already patched.'
|
|
674
739
|
);
|
|
675
740
|
globalThis.XMLHttpRequest = createXMLHttpRequestProxy({
|
|
@@ -680,13 +745,13 @@ var _XMLHttpRequestInterceptor = class extends _chunk7VJMJSIJjs.Interceptor {
|
|
|
680
745
|
'native "XMLHttpRequest" module patched!',
|
|
681
746
|
globalThis.XMLHttpRequest.name
|
|
682
747
|
);
|
|
683
|
-
Object.defineProperty(globalThis.XMLHttpRequest,
|
|
748
|
+
Object.defineProperty(globalThis.XMLHttpRequest, _chunk4CFMDU7Zjs.IS_PATCHED_MODULE, {
|
|
684
749
|
enumerable: true,
|
|
685
750
|
configurable: true,
|
|
686
751
|
value: true
|
|
687
752
|
});
|
|
688
753
|
this.subscriptions.push(() => {
|
|
689
|
-
Object.defineProperty(globalThis.XMLHttpRequest,
|
|
754
|
+
Object.defineProperty(globalThis.XMLHttpRequest, _chunk4CFMDU7Zjs.IS_PATCHED_MODULE, {
|
|
690
755
|
value: void 0
|
|
691
756
|
});
|
|
692
757
|
globalThis.XMLHttpRequest = PureXMLHttpRequest;
|
package/lib/browser/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as Interceptor, E as ExtractEventNames } from './Interceptor-
|
|
2
|
-
export {
|
|
1
|
+
import { I as Interceptor, E as ExtractEventNames } from './Interceptor-0a020bc4.js';
|
|
2
|
+
export { H as HttpRequestEventMap, a as IS_PATCHED_MODULE, b as InterceptorEventMap, e as InterceptorReadyState, c as InterceptorSubscription, R as RequestCredentials, d as deleteGlobalSymbol, g as getGlobalSymbol } from './Interceptor-0a020bc4.js';
|
|
3
3
|
import { EventMap, Listener } from 'strict-event-emitter';
|
|
4
4
|
import '@open-draft/logger';
|
|
5
5
|
|
|
@@ -28,4 +28,4 @@ declare function getCleanUrl(url: URL, isAbsolute?: boolean): string;
|
|
|
28
28
|
declare function encodeBuffer(text: string): Uint8Array;
|
|
29
29
|
declare function decodeBuffer(buffer: ArrayBuffer, encoding?: string): string;
|
|
30
30
|
|
|
31
|
-
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, decodeBuffer, encodeBuffer, getCleanUrl };
|
|
31
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, Interceptor, decodeBuffer, encodeBuffer, getCleanUrl };
|
package/lib/browser/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunk3LFH2WCFjs = require('./chunk-3LFH2WCF.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
var
|
|
11
|
+
var _chunk4CFMDU7Zjs = require('./chunk-4CFMDU7Z.js');
|
|
12
12
|
|
|
13
13
|
// src/BatchInterceptor.ts
|
|
14
|
-
var BatchInterceptor = class extends
|
|
14
|
+
var BatchInterceptor = class extends _chunk4CFMDU7Zjs.Interceptor {
|
|
15
15
|
constructor(options) {
|
|
16
16
|
BatchInterceptor.symbol = Symbol(options.name);
|
|
17
17
|
super(BatchInterceptor.symbol);
|
|
@@ -48,4 +48,4 @@ function getCleanUrl(url, isAbsolute = true) {
|
|
|
48
48
|
|
|
49
49
|
|
|
50
50
|
|
|
51
|
-
exports.BatchInterceptor = BatchInterceptor; exports.IS_PATCHED_MODULE =
|
|
51
|
+
exports.BatchInterceptor = BatchInterceptor; exports.IS_PATCHED_MODULE = _chunk4CFMDU7Zjs.IS_PATCHED_MODULE; exports.Interceptor = _chunk4CFMDU7Zjs.Interceptor; exports.InterceptorReadyState = _chunk4CFMDU7Zjs.InterceptorReadyState; exports.decodeBuffer = _chunk3LFH2WCFjs.decodeBuffer; exports.deleteGlobalSymbol = _chunk4CFMDU7Zjs.deleteGlobalSymbol; exports.encodeBuffer = _chunk3LFH2WCFjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunk4CFMDU7Zjs.getGlobalSymbol;
|
package/lib/browser/index.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
decodeBuffer,
|
|
3
3
|
encodeBuffer
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-7II4SWKS.mjs";
|
|
5
5
|
import {
|
|
6
6
|
IS_PATCHED_MODULE,
|
|
7
7
|
Interceptor,
|
|
8
8
|
InterceptorReadyState,
|
|
9
9
|
deleteGlobalSymbol,
|
|
10
10
|
getGlobalSymbol
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-GXJLJMOT.mjs";
|
|
12
12
|
|
|
13
13
|
// src/BatchInterceptor.ts
|
|
14
14
|
var BatchInterceptor = class extends Interceptor {
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { f as InteractiveRequest, A as AsyncEventEmitter, H as HttpRequestEventMap, I as Interceptor } from '../../Interceptor-
|
|
1
|
+
import { f as InteractiveRequest, A as AsyncEventEmitter, H as HttpRequestEventMap, I as Interceptor } from '../../Interceptor-0a020bc4.js';
|
|
2
2
|
import '@open-draft/logger';
|
|
3
3
|
import 'strict-event-emitter';
|
|
4
4
|
|
|
5
|
-
type XMLHttpRequestEventListener = (
|
|
5
|
+
type XMLHttpRequestEventListener = (args: {
|
|
6
|
+
request: InteractiveRequest;
|
|
7
|
+
requestId: string;
|
|
8
|
+
}) => Promise<void> | void;
|
|
6
9
|
type XMLHttpRequestEmitter = AsyncEventEmitter<HttpRequestEventMap>;
|
|
7
10
|
declare class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
8
11
|
static interceptorSymbol: symbol;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('../../chunk-
|
|
3
|
+
var _chunkUGP4JOAMjs = require('../../chunk-UGP4JOAM.js');
|
|
4
|
+
require('../../chunk-3LFH2WCF.js');
|
|
5
5
|
require('../../chunk-PCFJD76X.js');
|
|
6
|
-
require('../../chunk-
|
|
6
|
+
require('../../chunk-4CFMDU7Z.js');
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
exports.XMLHttpRequestInterceptor =
|
|
9
|
+
exports.XMLHttpRequestInterceptor = _chunkUGP4JOAMjs.XMLHttpRequestInterceptor;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
XMLHttpRequestInterceptor
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import "../../chunk-
|
|
3
|
+
} from "../../chunk-B74BGPYH.mjs";
|
|
4
|
+
import "../../chunk-7II4SWKS.mjs";
|
|
5
5
|
import "../../chunk-RT3ATOJH.mjs";
|
|
6
|
-
import "../../chunk-
|
|
6
|
+
import "../../chunk-GXJLJMOT.mjs";
|
|
7
7
|
export {
|
|
8
8
|
XMLHttpRequestInterceptor
|
|
9
9
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk2TENISKMjs = require('../../chunk-2TENISKM.js');
|
|
4
4
|
require('../../chunk-PCFJD76X.js');
|
|
5
|
-
require('../../chunk-
|
|
5
|
+
require('../../chunk-4CFMDU7Z.js');
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
exports.FetchInterceptor =
|
|
8
|
+
exports.FetchInterceptor = _chunk2TENISKMjs.FetchInterceptor;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FetchInterceptor } from '../interceptors/fetch/index.js';
|
|
2
2
|
import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
|
|
3
|
-
import '../Interceptor-
|
|
3
|
+
import '../Interceptor-0a020bc4.js';
|
|
4
4
|
import '@open-draft/logger';
|
|
5
5
|
import 'strict-event-emitter';
|
|
6
6
|
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('../chunk-
|
|
3
|
+
var _chunkUGP4JOAMjs = require('../chunk-UGP4JOAM.js');
|
|
4
|
+
require('../chunk-3LFH2WCF.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunk2TENISKMjs = require('../chunk-2TENISKM.js');
|
|
8
8
|
require('../chunk-PCFJD76X.js');
|
|
9
|
-
require('../chunk-
|
|
9
|
+
require('../chunk-4CFMDU7Z.js');
|
|
10
10
|
|
|
11
11
|
// src/presets/browser.ts
|
|
12
12
|
var browser_default = [
|
|
13
|
-
new (0,
|
|
14
|
-
new (0,
|
|
13
|
+
new (0, _chunk2TENISKMjs.FetchInterceptor)(),
|
|
14
|
+
new (0, _chunkUGP4JOAMjs.XMLHttpRequestInterceptor)()
|
|
15
15
|
];
|
|
16
16
|
|
|
17
17
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
XMLHttpRequestInterceptor
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-B74BGPYH.mjs";
|
|
4
|
+
import "../chunk-7II4SWKS.mjs";
|
|
5
5
|
import {
|
|
6
6
|
FetchInterceptor
|
|
7
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-LHYX2GOM.mjs";
|
|
8
8
|
import "../chunk-RT3ATOJH.mjs";
|
|
9
|
-
import "../chunk-
|
|
9
|
+
import "../chunk-GXJLJMOT.mjs";
|
|
10
10
|
|
|
11
11
|
// src/presets/browser.ts
|
|
12
12
|
var browser_default = [
|