@mswjs/interceptors 0.20.0 → 0.21.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 +7 -7
- package/lib/browser/Interceptor-959f650e.d.ts +104 -0
- package/lib/browser/chunk-5B525MKF.mjs +2139 -0
- package/lib/browser/chunk-BQYA7ER5.mjs +258 -0
- package/lib/browser/chunk-IIY3SHU3.js +2139 -0
- package/lib/browser/chunk-QWL3EOEY.js +258 -0
- package/lib/browser/chunk-STA6QBYM.mjs +64 -0
- package/lib/browser/chunk-ZJOF5MEZ.js +64 -0
- package/lib/browser/index.d.ts +31 -0
- package/lib/browser/index.js +51 -0
- package/lib/browser/index.mjs +51 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +14 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.js +657 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +657 -0
- package/lib/browser/interceptors/fetch/index.d.ts +12 -0
- package/lib/browser/interceptors/fetch/index.js +94 -0
- package/lib/browser/interceptors/fetch/index.mjs +94 -0
- package/lib/node/BatchInterceptor-a7261b26.d.ts +21 -0
- package/lib/node/Interceptor-997045eb.d.ts +104 -0
- package/lib/{RemoteHttpInterceptor.d.ts → node/RemoteHttpInterceptor.d.ts} +17 -12
- package/lib/node/RemoteHttpInterceptor.js +176 -0
- package/lib/node/RemoteHttpInterceptor.mjs +176 -0
- package/lib/node/chunk-6GWWOJ23.js +29 -0
- package/lib/node/chunk-6V3JXLBF.js +6093 -0
- package/lib/node/chunk-CIN5URNI.mjs +29 -0
- package/lib/node/chunk-FCIAOT7W.mjs +660 -0
- package/lib/node/chunk-GFH37L5D.mjs +6 -0
- package/lib/node/chunk-HDUJCCWF.js +661 -0
- package/lib/node/chunk-JISWS3Y3.mjs +31 -0
- package/lib/node/chunk-KZJG2UW7.js +31 -0
- package/lib/node/chunk-NNVTJLQA.mjs +6093 -0
- package/lib/node/chunk-QMIXLBOU.js +291 -0
- package/lib/node/chunk-STA6QBYM.mjs +64 -0
- package/lib/node/chunk-VKKFXSTL.js +660 -0
- package/lib/node/chunk-VQ4DZOBB.js +6 -0
- package/lib/node/chunk-ZJOF5MEZ.js +64 -0
- package/lib/node/chunk-ZSI7MX3V.mjs +291 -0
- package/lib/node/chunk-ZWCZGO3W.mjs +661 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.js +32 -0
- package/lib/node/index.mjs +32 -0
- package/lib/node/interceptors/ClientRequest/index.d.ts +22 -0
- package/lib/node/interceptors/ClientRequest/index.js +9 -0
- package/lib/node/interceptors/ClientRequest/index.mjs +9 -0
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +14 -0
- package/lib/node/interceptors/XMLHttpRequest/index.js +11 -0
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +11 -0
- package/lib/node/interceptors/fetch/index.d.ts +12 -0
- package/lib/node/interceptors/fetch/index.js +96 -0
- package/lib/node/interceptors/fetch/index.mjs +96 -0
- package/package.json +58 -9
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +1 -1
- package/src/shims/webEncoding.ts +9 -0
- package/src/utils/bufferUtils.ts +1 -1
- package/lib/BatchInterceptor.d.ts +0 -18
- package/lib/BatchInterceptor.js +0 -34
- package/lib/BatchInterceptor.js.map +0 -1
- package/lib/Interceptor.d.ts +0 -50
- package/lib/Interceptor.js +0 -172
- package/lib/Interceptor.js.map +0 -1
- package/lib/RemoteHttpInterceptor.js +0 -158
- package/lib/RemoteHttpInterceptor.js.map +0 -1
- package/lib/glossary.d.ts +0 -7
- package/lib/glossary.js +0 -5
- package/lib/glossary.js.map +0 -1
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -23
- package/lib/index.js.map +0 -1
- package/lib/interceptors/ClientRequest/NodeClientRequest.d.ts +0 -46
- package/lib/interceptors/ClientRequest/NodeClientRequest.js +0 -325
- package/lib/interceptors/ClientRequest/NodeClientRequest.js.map +0 -1
- package/lib/interceptors/ClientRequest/http.get.d.ts +0 -4
- package/lib/interceptors/ClientRequest/http.get.js +0 -19
- package/lib/interceptors/ClientRequest/http.get.js.map +0 -1
- package/lib/interceptors/ClientRequest/http.request.d.ts +0 -4
- package/lib/interceptors/ClientRequest/http.request.js +0 -16
- package/lib/interceptors/ClientRequest/http.request.js.map +0 -1
- package/lib/interceptors/ClientRequest/index.d.ts +0 -19
- package/lib/interceptors/ClientRequest/index.js +0 -50
- package/lib/interceptors/ClientRequest/index.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.d.ts +0 -10
- package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js +0 -60
- package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/createRequest.d.ts +0 -6
- package/lib/interceptors/ClientRequest/utils/createRequest.js +0 -30
- package/lib/interceptors/ClientRequest/utils/createRequest.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/createResponse.d.ts +0 -8
- package/lib/interceptors/ClientRequest/utils/createResponse.js +0 -24
- package/lib/interceptors/ClientRequest/utils/createResponse.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.d.ts +0 -3
- package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js +0 -57
- package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.d.ts +0 -17
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js +0 -151
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.d.ts +0 -15
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.js +0 -33
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.js.map +0 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.d.ts +0 -13
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js +0 -20
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestController.d.ts +0 -44
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestController.js +0 -413
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestController.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestProxy.d.ts +0 -20
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestProxy.js +0 -75
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestProxy.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/index.d.ts +0 -12
- package/lib/interceptors/XMLHttpRequest/index.js +0 -41
- package/lib/interceptors/XMLHttpRequest/index.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/polyfills/EventPolyfill.d.ts +0 -31
- package/lib/interceptors/XMLHttpRequest/polyfills/EventPolyfill.js +0 -44
- package/lib/interceptors/XMLHttpRequest/polyfills/EventPolyfill.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.d.ts +0 -8
- package/lib/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.js +0 -15
- package/lib/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.d.ts +0 -4
- package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js +0 -14
- package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/utils/createEvent.d.ts +0 -2
- package/lib/interceptors/XMLHttpRequest/utils/createEvent.js +0 -37
- package/lib/interceptors/XMLHttpRequest/utils/createEvent.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/utils/createResponse.d.ts +0 -2
- package/lib/interceptors/XMLHttpRequest/utils/createResponse.js +0 -14
- package/lib/interceptors/XMLHttpRequest/utils/createResponse.js.map +0 -1
- package/lib/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.d.ts +0 -1
- package/lib/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.js +0 -17
- package/lib/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.js.map +0 -1
- package/lib/interceptors/fetch/index.d.ts +0 -8
- package/lib/interceptors/fetch/index.js +0 -81
- package/lib/interceptors/fetch/index.js.map +0 -1
- package/lib/presets/browser.d.ts +0 -8
- package/lib/presets/browser.js +0 -10
- package/lib/presets/browser.js.map +0 -1
- package/lib/presets/node.d.ts +0 -8
- package/lib/presets/node.js +0 -10
- package/lib/presets/node.js.map +0 -1
- package/lib/utils/AsyncEventEmitter.d.ts +0 -33
- package/lib/utils/AsyncEventEmitter.js +0 -148
- package/lib/utils/AsyncEventEmitter.js.map +0 -1
- package/lib/utils/bufferUtils.d.ts +0 -8
- package/lib/utils/bufferUtils.js +0 -24
- package/lib/utils/bufferUtils.js.map +0 -1
- package/lib/utils/cloneObject.d.ts +0 -1
- package/lib/utils/cloneObject.js +0 -28
- package/lib/utils/cloneObject.js.map +0 -1
- package/lib/utils/createLazyCallback.d.ts +0 -11
- package/lib/utils/createLazyCallback.js +0 -40
- package/lib/utils/createLazyCallback.js.map +0 -1
- package/lib/utils/createProxy.d.ts +0 -8
- package/lib/utils/createProxy.js +0 -45
- package/lib/utils/createProxy.js.map +0 -1
- package/lib/utils/getCleanUrl.d.ts +0 -4
- package/lib/utils/getCleanUrl.js +0 -11
- package/lib/utils/getCleanUrl.js.map +0 -1
- package/lib/utils/getRequestOptionsByUrl.d.ts +0 -8
- package/lib/utils/getRequestOptionsByUrl.js +0 -28
- package/lib/utils/getRequestOptionsByUrl.js.map +0 -1
- package/lib/utils/getUrlByRequestOptions.d.ts +0 -11
- package/lib/utils/getUrlByRequestOptions.js +0 -78
- package/lib/utils/getUrlByRequestOptions.js.map +0 -1
- package/lib/utils/isObject.d.ts +0 -4
- package/lib/utils/isObject.js +0 -11
- package/lib/utils/isObject.js.map +0 -1
- package/lib/utils/nextTick.d.ts +0 -2
- package/lib/utils/nextTick.js +0 -16
- package/lib/utils/nextTick.js.map +0 -1
- package/lib/utils/parseJson.d.ts +0 -5
- package/lib/utils/parseJson.js +0 -18
- package/lib/utils/parseJson.js.map +0 -1
- package/lib/utils/toInteractiveRequest.d.ts +0 -7
- package/lib/utils/toInteractiveRequest.js +0 -20
- package/lib/utils/toInteractiveRequest.js.map +0 -1
- package/lib/utils/uuid.d.ts +0 -1
- package/lib/utils/uuid.js +0 -12
- package/lib/utils/uuid.js.map +0 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BatchInterceptor
|
|
3
|
+
} from "./chunk-JISWS3Y3.mjs";
|
|
4
|
+
import {
|
|
5
|
+
decodeBuffer,
|
|
6
|
+
encodeBuffer
|
|
7
|
+
} from "./chunk-CIN5URNI.mjs";
|
|
8
|
+
import {
|
|
9
|
+
IS_PATCHED_MODULE
|
|
10
|
+
} from "./chunk-GFH37L5D.mjs";
|
|
11
|
+
import {
|
|
12
|
+
Interceptor,
|
|
13
|
+
InterceptorReadyState,
|
|
14
|
+
deleteGlobalSymbol,
|
|
15
|
+
getGlobalSymbol
|
|
16
|
+
} from "./chunk-ZSI7MX3V.mjs";
|
|
17
|
+
|
|
18
|
+
// src/utils/getCleanUrl.ts
|
|
19
|
+
function getCleanUrl(url, isAbsolute = true) {
|
|
20
|
+
return [isAbsolute && url.origin, url.pathname].filter(Boolean).join("");
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
BatchInterceptor,
|
|
24
|
+
IS_PATCHED_MODULE,
|
|
25
|
+
Interceptor,
|
|
26
|
+
InterceptorReadyState,
|
|
27
|
+
decodeBuffer,
|
|
28
|
+
deleteGlobalSymbol,
|
|
29
|
+
encodeBuffer,
|
|
30
|
+
getCleanUrl,
|
|
31
|
+
getGlobalSymbol
|
|
32
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import https from 'https';
|
|
3
|
+
import { A as AsyncEventEmitter, H as HttpRequestEventMap, e as Interceptor } from '../../Interceptor-997045eb.js';
|
|
4
|
+
import 'debug';
|
|
5
|
+
import 'strict-event-emitter';
|
|
6
|
+
|
|
7
|
+
type Protocol = 'http' | 'https';
|
|
8
|
+
|
|
9
|
+
type ClientRequestEmitter = AsyncEventEmitter<HttpRequestEventMap>;
|
|
10
|
+
type ClientRequestModules = Map<Protocol, typeof http | typeof https>;
|
|
11
|
+
/**
|
|
12
|
+
* Intercept requests made via the `ClientRequest` class.
|
|
13
|
+
* Such requests include `http.get`, `https.request`, etc.
|
|
14
|
+
*/
|
|
15
|
+
declare class ClientRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
16
|
+
static symbol: symbol;
|
|
17
|
+
private modules;
|
|
18
|
+
constructor();
|
|
19
|
+
protected setup(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { ClientRequestEmitter, ClientRequestInterceptor, ClientRequestModules };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkVKKFXSTLjs = require('../../chunk-VKKFXSTL.js');
|
|
4
|
+
require('../../chunk-6V3JXLBF.js');
|
|
5
|
+
require('../../chunk-ZJOF5MEZ.js');
|
|
6
|
+
require('../../chunk-QMIXLBOU.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.ClientRequestInterceptor = _chunkVKKFXSTLjs.ClientRequestInterceptor;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { f as InteractiveRequest, A as AsyncEventEmitter, H as HttpRequestEventMap, e as Interceptor } from '../../Interceptor-997045eb.js';
|
|
2
|
+
import 'debug';
|
|
3
|
+
import 'strict-event-emitter';
|
|
4
|
+
|
|
5
|
+
type XMLHttpRequestEventListener = (request: InteractiveRequest, requestId: string) => Promise<void> | void;
|
|
6
|
+
type XMLHttpRequestEmitter = AsyncEventEmitter<HttpRequestEventMap>;
|
|
7
|
+
declare class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
8
|
+
static symbol: symbol;
|
|
9
|
+
constructor();
|
|
10
|
+
protected checkEnvironment(): boolean;
|
|
11
|
+
protected setup(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { XMLHttpRequestEmitter, XMLHttpRequestEventListener, XMLHttpRequestInterceptor };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkHDUJCCWFjs = require('../../chunk-HDUJCCWF.js');
|
|
4
|
+
require('../../chunk-6GWWOJ23.js');
|
|
5
|
+
require('../../chunk-6V3JXLBF.js');
|
|
6
|
+
require('../../chunk-VQ4DZOBB.js');
|
|
7
|
+
require('../../chunk-ZJOF5MEZ.js');
|
|
8
|
+
require('../../chunk-QMIXLBOU.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.XMLHttpRequestInterceptor = _chunkHDUJCCWFjs.XMLHttpRequestInterceptor;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
XMLHttpRequestInterceptor
|
|
3
|
+
} from "../../chunk-ZWCZGO3W.mjs";
|
|
4
|
+
import "../../chunk-CIN5URNI.mjs";
|
|
5
|
+
import "../../chunk-NNVTJLQA.mjs";
|
|
6
|
+
import "../../chunk-GFH37L5D.mjs";
|
|
7
|
+
import "../../chunk-STA6QBYM.mjs";
|
|
8
|
+
import "../../chunk-ZSI7MX3V.mjs";
|
|
9
|
+
export {
|
|
10
|
+
XMLHttpRequestInterceptor
|
|
11
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { e as Interceptor, H as HttpRequestEventMap } from '../../Interceptor-997045eb.js';
|
|
2
|
+
import 'debug';
|
|
3
|
+
import 'strict-event-emitter';
|
|
4
|
+
|
|
5
|
+
declare class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
6
|
+
static symbol: symbol;
|
|
7
|
+
constructor();
|
|
8
|
+
protected checkEnvironment(): boolean;
|
|
9
|
+
protected setup(): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { FetchInterceptor };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkVQ4DZOBBjs = require('../../chunk-VQ4DZOBB.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkZJOF5MEZjs = require('../../chunk-ZJOF5MEZ.js');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkQMIXLBOUjs = require('../../chunk-QMIXLBOU.js');
|
|
11
|
+
|
|
12
|
+
// src/interceptors/fetch/index.ts
|
|
13
|
+
var _outvariant = require('outvariant');
|
|
14
|
+
var _FetchInterceptor = class extends _chunkQMIXLBOUjs.Interceptor {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(_FetchInterceptor.symbol);
|
|
17
|
+
}
|
|
18
|
+
checkEnvironment() {
|
|
19
|
+
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
20
|
+
}
|
|
21
|
+
setup() {
|
|
22
|
+
const pureFetch = globalThis.fetch;
|
|
23
|
+
_outvariant.invariant.call(void 0,
|
|
24
|
+
!pureFetch[_chunkVQ4DZOBBjs.IS_PATCHED_MODULE],
|
|
25
|
+
'Failed to patch the "fetch" module: already patched.'
|
|
26
|
+
);
|
|
27
|
+
globalThis.fetch = async (input, init) => {
|
|
28
|
+
const requestId = _chunkZJOF5MEZjs.uuidv4.call(void 0, );
|
|
29
|
+
const request = new Request(input, init);
|
|
30
|
+
this.log("[%s] %s", request.method, request.url);
|
|
31
|
+
const interactiveRequest = _chunkZJOF5MEZjs.toInteractiveRequest.call(void 0, request);
|
|
32
|
+
this.log(
|
|
33
|
+
'emitting the "request" event for %d listener(s)...',
|
|
34
|
+
this.emitter.listenerCount("request")
|
|
35
|
+
);
|
|
36
|
+
this.emitter.emit("request", interactiveRequest, requestId);
|
|
37
|
+
this.log("awaiting for the mocked response...");
|
|
38
|
+
await this.emitter.untilIdle(
|
|
39
|
+
"request",
|
|
40
|
+
({ args: [, pendingRequestId] }) => {
|
|
41
|
+
return pendingRequestId === requestId;
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
this.log("all request listeners have been resolved!");
|
|
45
|
+
const [mockedResponse] = await interactiveRequest.respondWith.invoked();
|
|
46
|
+
this.log("event.respondWith called with:", mockedResponse);
|
|
47
|
+
if (mockedResponse) {
|
|
48
|
+
this.log("received mocked response:", mockedResponse);
|
|
49
|
+
const responseCloine = mockedResponse.clone();
|
|
50
|
+
this.emitter.emit(
|
|
51
|
+
"response",
|
|
52
|
+
responseCloine,
|
|
53
|
+
interactiveRequest,
|
|
54
|
+
requestId
|
|
55
|
+
);
|
|
56
|
+
const response = new Response(mockedResponse.body, mockedResponse);
|
|
57
|
+
Object.defineProperty(response, "url", {
|
|
58
|
+
writable: false,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: false,
|
|
61
|
+
value: request.url
|
|
62
|
+
});
|
|
63
|
+
return response;
|
|
64
|
+
}
|
|
65
|
+
this.log("no mocked response received!");
|
|
66
|
+
return pureFetch(request).then((response) => {
|
|
67
|
+
const responseClone = response.clone();
|
|
68
|
+
this.log("original fetch performed", responseClone);
|
|
69
|
+
this.emitter.emit(
|
|
70
|
+
"response",
|
|
71
|
+
responseClone,
|
|
72
|
+
interactiveRequest,
|
|
73
|
+
requestId
|
|
74
|
+
);
|
|
75
|
+
return response;
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
Object.defineProperty(globalThis.fetch, _chunkVQ4DZOBBjs.IS_PATCHED_MODULE, {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
value: true
|
|
82
|
+
});
|
|
83
|
+
this.subscriptions.push(() => {
|
|
84
|
+
Object.defineProperty(globalThis.fetch, _chunkVQ4DZOBBjs.IS_PATCHED_MODULE, {
|
|
85
|
+
value: void 0
|
|
86
|
+
});
|
|
87
|
+
globalThis.fetch = pureFetch;
|
|
88
|
+
this.log('restored native "globalThis.fetch"!', globalThis.fetch.name);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var FetchInterceptor = _FetchInterceptor;
|
|
93
|
+
FetchInterceptor.symbol = Symbol("fetch");
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
exports.FetchInterceptor = FetchInterceptor;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IS_PATCHED_MODULE
|
|
3
|
+
} from "../../chunk-GFH37L5D.mjs";
|
|
4
|
+
import {
|
|
5
|
+
toInteractiveRequest,
|
|
6
|
+
uuidv4
|
|
7
|
+
} from "../../chunk-STA6QBYM.mjs";
|
|
8
|
+
import {
|
|
9
|
+
Interceptor
|
|
10
|
+
} from "../../chunk-ZSI7MX3V.mjs";
|
|
11
|
+
|
|
12
|
+
// src/interceptors/fetch/index.ts
|
|
13
|
+
import { invariant } from "outvariant";
|
|
14
|
+
var _FetchInterceptor = class extends Interceptor {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(_FetchInterceptor.symbol);
|
|
17
|
+
}
|
|
18
|
+
checkEnvironment() {
|
|
19
|
+
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
20
|
+
}
|
|
21
|
+
setup() {
|
|
22
|
+
const pureFetch = globalThis.fetch;
|
|
23
|
+
invariant(
|
|
24
|
+
!pureFetch[IS_PATCHED_MODULE],
|
|
25
|
+
'Failed to patch the "fetch" module: already patched.'
|
|
26
|
+
);
|
|
27
|
+
globalThis.fetch = async (input, init) => {
|
|
28
|
+
const requestId = uuidv4();
|
|
29
|
+
const request = new Request(input, init);
|
|
30
|
+
this.log("[%s] %s", request.method, request.url);
|
|
31
|
+
const interactiveRequest = toInteractiveRequest(request);
|
|
32
|
+
this.log(
|
|
33
|
+
'emitting the "request" event for %d listener(s)...',
|
|
34
|
+
this.emitter.listenerCount("request")
|
|
35
|
+
);
|
|
36
|
+
this.emitter.emit("request", interactiveRequest, requestId);
|
|
37
|
+
this.log("awaiting for the mocked response...");
|
|
38
|
+
await this.emitter.untilIdle(
|
|
39
|
+
"request",
|
|
40
|
+
({ args: [, pendingRequestId] }) => {
|
|
41
|
+
return pendingRequestId === requestId;
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
this.log("all request listeners have been resolved!");
|
|
45
|
+
const [mockedResponse] = await interactiveRequest.respondWith.invoked();
|
|
46
|
+
this.log("event.respondWith called with:", mockedResponse);
|
|
47
|
+
if (mockedResponse) {
|
|
48
|
+
this.log("received mocked response:", mockedResponse);
|
|
49
|
+
const responseCloine = mockedResponse.clone();
|
|
50
|
+
this.emitter.emit(
|
|
51
|
+
"response",
|
|
52
|
+
responseCloine,
|
|
53
|
+
interactiveRequest,
|
|
54
|
+
requestId
|
|
55
|
+
);
|
|
56
|
+
const response = new Response(mockedResponse.body, mockedResponse);
|
|
57
|
+
Object.defineProperty(response, "url", {
|
|
58
|
+
writable: false,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: false,
|
|
61
|
+
value: request.url
|
|
62
|
+
});
|
|
63
|
+
return response;
|
|
64
|
+
}
|
|
65
|
+
this.log("no mocked response received!");
|
|
66
|
+
return pureFetch(request).then((response) => {
|
|
67
|
+
const responseClone = response.clone();
|
|
68
|
+
this.log("original fetch performed", responseClone);
|
|
69
|
+
this.emitter.emit(
|
|
70
|
+
"response",
|
|
71
|
+
responseClone,
|
|
72
|
+
interactiveRequest,
|
|
73
|
+
requestId
|
|
74
|
+
);
|
|
75
|
+
return response;
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
value: true
|
|
82
|
+
});
|
|
83
|
+
this.subscriptions.push(() => {
|
|
84
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
85
|
+
value: void 0
|
|
86
|
+
});
|
|
87
|
+
globalThis.fetch = pureFetch;
|
|
88
|
+
this.log('restored native "globalThis.fetch"!', globalThis.fetch.name);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var FetchInterceptor = _FetchInterceptor;
|
|
93
|
+
FetchInterceptor.symbol = Symbol("fetch");
|
|
94
|
+
export {
|
|
95
|
+
FetchInterceptor
|
|
96
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mswjs/interceptors",
|
|
3
3
|
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"
|
|
4
|
+
"version": "0.21.0",
|
|
5
|
+
"main": "./lib/node/index.js",
|
|
6
|
+
"module": "./lib/node/index.mjs",
|
|
7
|
+
"types": "./lib/node/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/node/index.d.ts",
|
|
11
|
+
"require": "./lib/node/index.js",
|
|
12
|
+
"import": "./lib/node/index.mjs",
|
|
13
|
+
"default": "./lib/node/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./ClientRequest": {
|
|
16
|
+
"browser": null,
|
|
17
|
+
"types": "./lib/node/interceptors/ClientRequest/index.d.ts",
|
|
18
|
+
"require": "./lib/node/interceptors/ClientRequest/index.js",
|
|
19
|
+
"import": "./lib/node/interceptors/ClientRequest/index.mjs",
|
|
20
|
+
"default": "./lib/node/interceptors/ClientRequest/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./XMLHttpRequest": {
|
|
23
|
+
"browser": {
|
|
24
|
+
"types": "./lib/browser/interceptors/XMLHttpRequest/index.d.ts",
|
|
25
|
+
"require": "./lib/browser/interceptors/XMLHttpRequest/index.js",
|
|
26
|
+
"import": "./lib/browser/interceptors/XMLHttpRequest/index.mjs",
|
|
27
|
+
"default": "./lib/browser/interceptors/XMLHttpRequest/index.js"
|
|
28
|
+
},
|
|
29
|
+
"types": "./lib/node/interceptors/XMLHttpRequest/index.d.ts",
|
|
30
|
+
"require": "./lib/node/interceptors/XMLHttpRequest/index.js",
|
|
31
|
+
"import": "./lib/node/interceptors/XMLHttpRequest/index.mjs",
|
|
32
|
+
"default": "./lib/node/interceptors/XMLHttpRequest/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./fetch": {
|
|
35
|
+
"browser": {
|
|
36
|
+
"types": "./lib/browser/interceptors/fetch/index.d.ts",
|
|
37
|
+
"require": "./lib/browser/interceptors/fetch/index.js",
|
|
38
|
+
"default": "./lib/browser/interceptors/fetch/index.js",
|
|
39
|
+
"import": "./lib/browser/interceptors/fetch/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"types": "./lib/node/interceptors/fetch/index.d.ts",
|
|
42
|
+
"require": "./lib/node/interceptors/fetch/index.js",
|
|
43
|
+
"import": "./lib/node/interceptors/fetch/index.mjs",
|
|
44
|
+
"default": "./lib/node/interceptors/fetch/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./RemoteHttpInterceptor": {
|
|
47
|
+
"browser": null,
|
|
48
|
+
"types": "./lib/node/interceptors/RemoteHttpInterceptor.d.ts",
|
|
49
|
+
"require": "./lib/node/interceptors/RemoteHttpInterceptor.js",
|
|
50
|
+
"import": "./lib/node/interceptors/RemoteHttpInterceptor.mjs",
|
|
51
|
+
"default": "./lib/node/interceptors/RemoteHttpInterceptor.js"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
7
54
|
"author": "Artem Zakharchenko",
|
|
8
55
|
"license": "MIT",
|
|
9
56
|
"engines": {
|
|
@@ -17,7 +64,7 @@
|
|
|
17
64
|
"test:integration:node": "jest --c test/jest.node.config.js --runInBand",
|
|
18
65
|
"test:integration:browser": "jest --c test/jest.browser.config.js",
|
|
19
66
|
"clean": "rimraf lib",
|
|
20
|
-
"build": "yarn clean &&
|
|
67
|
+
"build": "yarn clean && cross-env NODE_ENV=production tsup --splitting",
|
|
21
68
|
"prepare": "yarn simple-git-hooks init",
|
|
22
69
|
"release": "release publish",
|
|
23
70
|
"prepublishOnly": "yarn build && yarn test"
|
|
@@ -36,6 +83,7 @@
|
|
|
36
83
|
"@commitlint/config-conventional": "^16.0.0",
|
|
37
84
|
"@open-draft/test-server": "^0.4.2",
|
|
38
85
|
"@ossjs/release": "^0.4.0",
|
|
86
|
+
"@remix-run/web-fetch": "^4.3.1",
|
|
39
87
|
"@types/cors": "^2.8.12",
|
|
40
88
|
"@types/express": "^4.17.13",
|
|
41
89
|
"@types/express-rate-limit": "^6.0.0",
|
|
@@ -48,6 +96,7 @@
|
|
|
48
96
|
"body-parser": "^1.19.0",
|
|
49
97
|
"commitizen": "^4.2.4",
|
|
50
98
|
"cors": "^2.8.5",
|
|
99
|
+
"cross-env": "^7.0.3",
|
|
51
100
|
"cz-conventional-changelog": "3.3.0",
|
|
52
101
|
"express": "^4.17.3",
|
|
53
102
|
"express-rate-limit": "^6.3.0",
|
|
@@ -61,18 +110,18 @@
|
|
|
61
110
|
"superagent": "^6.1.0",
|
|
62
111
|
"supertest": "^6.1.6",
|
|
63
112
|
"ts-jest": "^27.1.1",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
113
|
+
"tsup": "^6.5.0",
|
|
114
|
+
"typescript": "^4.9.4",
|
|
115
|
+
"wait-for-expect": "^3.0.2",
|
|
116
|
+
"web-encoding": "^1.1.5"
|
|
66
117
|
},
|
|
67
118
|
"dependencies": {
|
|
68
119
|
"@open-draft/until": "^1.0.3",
|
|
69
|
-
"@remix-run/web-fetch": "^4.3.2",
|
|
70
120
|
"@types/debug": "^4.1.7",
|
|
71
121
|
"debug": "^4.3.3",
|
|
72
122
|
"headers-polyfill": "^3.1.0",
|
|
73
123
|
"outvariant": "^1.2.1",
|
|
74
|
-
"strict-event-emitter": "^0.4.6"
|
|
75
|
-
"web-encoding": "^1.1.5"
|
|
124
|
+
"strict-event-emitter": "^0.4.6"
|
|
76
125
|
},
|
|
77
126
|
"keywords": [
|
|
78
127
|
"request",
|
|
@@ -165,7 +165,7 @@ export function normalizeClientRequestArgs(
|
|
|
165
165
|
// Handle a given "RequestOptions" object as-is
|
|
166
166
|
// and derive the URL instance from it.
|
|
167
167
|
else if (isObject(args[0])) {
|
|
168
|
-
options = args[0]
|
|
168
|
+
options = args[0] as any
|
|
169
169
|
log('first argument is RequestOptions:', options)
|
|
170
170
|
|
|
171
171
|
// When handling a "RequestOptions" object without an explicit "protocol",
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const TextEncoder: typeof globalThis.TextEncoder =
|
|
2
|
+
typeof globalThis.TextEncoder === 'undefined'
|
|
3
|
+
? require('util').TextEncoder
|
|
4
|
+
: globalThis.TextEncoder
|
|
5
|
+
|
|
6
|
+
export const TextDecoder: typeof globalThis.TextDecoder =
|
|
7
|
+
typeof globalThis.TextDecoder === 'undefined'
|
|
8
|
+
? require('util').TextDecoder
|
|
9
|
+
: globalThis.TextDecoder
|
package/src/utils/bufferUtils.ts
CHANGED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { EventMap, Listener } from 'strict-event-emitter';
|
|
2
|
-
import { ExtractEventNames, Interceptor } from './Interceptor';
|
|
3
|
-
export interface BatchInterceptorOptions<InterceptorList extends Array<Interceptor<any>>> {
|
|
4
|
-
name: string;
|
|
5
|
-
interceptors: InterceptorList;
|
|
6
|
-
}
|
|
7
|
-
export declare type ExtractEventMapType<InterceptorList extends Array<Interceptor<any>>> = InterceptorList extends Array<infer InterceptorType> ? InterceptorType extends Interceptor<infer EventMap> ? EventMap : never : never;
|
|
8
|
-
/**
|
|
9
|
-
* A batch interceptor that exposes a single interface
|
|
10
|
-
* to apply and operate with multiple interceptors at once.
|
|
11
|
-
*/
|
|
12
|
-
export declare class BatchInterceptor<InterceptorList extends Array<Interceptor<any>>, Events extends EventMap = ExtractEventMapType<InterceptorList>> extends Interceptor<Events> {
|
|
13
|
-
static symbol: Symbol;
|
|
14
|
-
private interceptors;
|
|
15
|
-
constructor(options: BatchInterceptorOptions<InterceptorList>);
|
|
16
|
-
protected setup(): void;
|
|
17
|
-
on<EventName extends ExtractEventNames<Events>>(event: EventName, listener: Listener<Events[EventName]>): void;
|
|
18
|
-
}
|
package/lib/BatchInterceptor.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BatchInterceptor = void 0;
|
|
4
|
-
const Interceptor_1 = require("./Interceptor");
|
|
5
|
-
/**
|
|
6
|
-
* A batch interceptor that exposes a single interface
|
|
7
|
-
* to apply and operate with multiple interceptors at once.
|
|
8
|
-
*/
|
|
9
|
-
class BatchInterceptor extends Interceptor_1.Interceptor {
|
|
10
|
-
constructor(options) {
|
|
11
|
-
BatchInterceptor.symbol = Symbol(options.name);
|
|
12
|
-
super(BatchInterceptor.symbol);
|
|
13
|
-
this.interceptors = options.interceptors;
|
|
14
|
-
}
|
|
15
|
-
setup() {
|
|
16
|
-
const log = this.log.extend('setup');
|
|
17
|
-
log('applying all %d interceptors...', this.interceptors.length);
|
|
18
|
-
for (const interceptor of this.interceptors) {
|
|
19
|
-
log('applying "%s" interceptor...', interceptor.constructor.name);
|
|
20
|
-
interceptor.apply();
|
|
21
|
-
log('adding interceptor dispose subscription');
|
|
22
|
-
this.subscriptions.push(() => interceptor.dispose());
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
on(event, listener) {
|
|
26
|
-
// Instead of adding a listener to the batch interceptor,
|
|
27
|
-
// propagate the listener to each of the individual interceptors.
|
|
28
|
-
this.interceptors.forEach((interceptor) => {
|
|
29
|
-
interceptor.on(event, listener);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.BatchInterceptor = BatchInterceptor;
|
|
34
|
-
//# sourceMappingURL=BatchInterceptor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BatchInterceptor.js","sourceRoot":"","sources":["../src/BatchInterceptor.ts"],"names":[],"mappings":";;;AACA,+CAA8D;AAiB9D;;;GAGG;AACH,MAAa,gBAGX,SAAQ,yBAAmB;IAK3B,YAAY,OAAiD;QAC3D,gBAAgB,CAAC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC9C,KAAK,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAA;QAC9B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAA;IAC1C,CAAC;IAES,KAAK;QACb,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAEpC,GAAG,CAAC,iCAAiC,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;QAEhE,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,YAAY,EAAE;YAC3C,GAAG,CAAC,8BAA8B,EAAE,WAAW,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;YACjE,WAAW,CAAC,KAAK,EAAE,CAAA;YAEnB,GAAG,CAAC,yCAAyC,CAAC,CAAA;YAC9C,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC,CAAA;SACrD;IACH,CAAC;IAEM,EAAE,CACP,KAAgB,EAChB,QAAqC;QAErC,yDAAyD;QACzD,iEAAiE;QACjE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACxC,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAtCD,4CAsCC"}
|
package/lib/Interceptor.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { Debugger } from 'debug';
|
|
2
|
-
import { Listener } from 'strict-event-emitter';
|
|
3
|
-
import { AsyncEventEmitter } from './utils/AsyncEventEmitter';
|
|
4
|
-
export declare type InterceptorEventMap = Record<string, any>;
|
|
5
|
-
export declare type InterceptorSubscription = () => void;
|
|
6
|
-
export declare function getGlobalSymbol<V>(symbol: Symbol): V | undefined;
|
|
7
|
-
export declare function deleteGlobalSymbol(symbol: Symbol): void;
|
|
8
|
-
export declare enum InterceptorReadyState {
|
|
9
|
-
INACTIVE = "INACTIVE",
|
|
10
|
-
APPLYING = "APPLYING",
|
|
11
|
-
APPLIED = "APPLIED",
|
|
12
|
-
DISPOSING = "DISPOSING",
|
|
13
|
-
DISPOSED = "DISPOSED"
|
|
14
|
-
}
|
|
15
|
-
export declare type ExtractEventNames<Events extends Record<string, any>> = Events extends Record<infer EventName, any> ? EventName : never;
|
|
16
|
-
export declare class Interceptor<Events extends InterceptorEventMap> {
|
|
17
|
-
private readonly symbol;
|
|
18
|
-
protected emitter: AsyncEventEmitter<Events>;
|
|
19
|
-
protected subscriptions: Array<InterceptorSubscription>;
|
|
20
|
-
protected log: Debugger;
|
|
21
|
-
readyState: InterceptorReadyState;
|
|
22
|
-
constructor(symbol: Symbol);
|
|
23
|
-
/**
|
|
24
|
-
* Determine if this interceptor can be applied
|
|
25
|
-
* in the current environment.
|
|
26
|
-
*/
|
|
27
|
-
protected checkEnvironment(): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Apply this interceptor to the current process.
|
|
30
|
-
* Returns an already running interceptor instance if it's present.
|
|
31
|
-
*/
|
|
32
|
-
apply(): void;
|
|
33
|
-
/**
|
|
34
|
-
* Setup the module augments and stubs necessary for this interceptor.
|
|
35
|
-
* This method is not run if there's a running interceptor instance
|
|
36
|
-
* to prevent instantiating an interceptor multiple times.
|
|
37
|
-
*/
|
|
38
|
-
protected setup(): void;
|
|
39
|
-
/**
|
|
40
|
-
* Listen to the interceptor's public events.
|
|
41
|
-
*/
|
|
42
|
-
on<EventName extends ExtractEventNames<Events>>(eventName: EventName, listener: Listener<Events[EventName]>): void;
|
|
43
|
-
/**
|
|
44
|
-
* Disposes of any side-effects this interceptor has introduced.
|
|
45
|
-
*/
|
|
46
|
-
dispose(): void;
|
|
47
|
-
private getInstance;
|
|
48
|
-
private setInstance;
|
|
49
|
-
private clearInstance;
|
|
50
|
-
}
|