@mswjs/interceptors 0.39.8 → 0.40.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/lib/browser/{chunk-E3CCOBRX.js → chunk-2MCNQOY3.js} +54 -49
- package/lib/browser/chunk-2MCNQOY3.js.map +1 -0
- package/lib/browser/chunk-57RIRQUY.js +218 -0
- package/lib/browser/chunk-57RIRQUY.js.map +1 -0
- package/lib/browser/chunk-FW45TRCB.js +178 -0
- package/lib/browser/chunk-FW45TRCB.js.map +1 -0
- package/lib/browser/{chunk-TIPR373R.js → chunk-JQ2S7G56.js} +19 -3
- package/lib/browser/chunk-JQ2S7G56.js.map +1 -0
- package/lib/browser/{chunk-3RXCRGL2.mjs → chunk-LIKZF2VU.mjs} +102 -1
- package/lib/browser/chunk-LIKZF2VU.mjs.map +1 -0
- package/lib/browser/{chunk-H74PGQ4Y.js → chunk-MNT2FUCH.js} +58 -53
- package/lib/browser/chunk-MNT2FUCH.js.map +1 -0
- package/lib/browser/chunk-VOUOVDAW.mjs +178 -0
- package/lib/browser/chunk-VOUOVDAW.mjs.map +1 -0
- package/lib/browser/{chunk-E7UVBHVO.mjs → chunk-WADP6VHN.mjs} +48 -43
- package/lib/browser/chunk-WADP6VHN.mjs.map +1 -0
- package/lib/browser/{chunk-Q7K2XAEP.mjs → chunk-WOWPV4GR.mjs} +50 -45
- package/lib/browser/chunk-WOWPV4GR.mjs.map +1 -0
- package/lib/browser/{chunk-QED3Q6Z2.mjs → chunk-Z5TSB3T6.mjs} +17 -1
- package/lib/browser/{chunk-QED3Q6Z2.mjs.map → chunk-Z5TSB3T6.mjs.map} +1 -1
- package/lib/browser/{glossary-7152281e.d.ts → glossary-f7ee1c9d.d.ts} +22 -17
- package/lib/browser/index.d.ts +1 -2
- package/lib/browser/index.js +6 -4
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +4 -2
- package/lib/browser/index.mjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.js +3 -3
- package/lib/browser/interceptors/WebSocket/index.mjs +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.js +5 -5
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +4 -4
- package/lib/browser/interceptors/fetch/index.d.ts +1 -2
- package/lib/browser/interceptors/fetch/index.js +5 -5
- package/lib/browser/interceptors/fetch/index.mjs +4 -4
- package/lib/browser/presets/browser.d.ts +1 -2
- package/lib/browser/presets/browser.js +7 -7
- package/lib/browser/presets/browser.mjs +5 -5
- package/lib/node/{BatchInterceptor-5b72232f.d.ts → BatchInterceptor-cb9a2eee.d.ts} +1 -1
- package/lib/node/{Interceptor-bc5a9d8e.d.ts → Interceptor-dc0a39b5.d.ts} +22 -16
- package/lib/node/RemoteHttpInterceptor.d.ts +2 -3
- package/lib/node/RemoteHttpInterceptor.js +31 -27
- package/lib/node/RemoteHttpInterceptor.js.map +1 -1
- package/lib/node/RemoteHttpInterceptor.mjs +28 -24
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/{chunk-EKNRB5ZS.mjs → chunk-5UGIB6OX.mjs} +40 -29
- package/lib/node/chunk-5UGIB6OX.mjs.map +1 -0
- package/lib/node/{chunk-4NEYTVWD.mjs → chunk-5V3SIIW2.mjs} +48 -43
- package/lib/node/chunk-5V3SIIW2.mjs.map +1 -0
- package/lib/node/{chunk-VV2LUF5K.js → chunk-6B3ZQOO2.js} +51 -46
- package/lib/node/chunk-6B3ZQOO2.js.map +1 -0
- package/lib/node/chunk-7Q53NNPV.js +189 -0
- package/lib/node/chunk-7Q53NNPV.js.map +1 -0
- package/lib/node/{chunk-A7U44ARP.js → chunk-DOWWQYXZ.js} +104 -3
- package/lib/node/chunk-DOWWQYXZ.js.map +1 -0
- package/lib/node/{chunk-Z5LWCBZS.js → chunk-FRZQJNBO.js} +56 -51
- package/lib/node/chunk-FRZQJNBO.js.map +1 -0
- package/lib/node/{chunk-TJDMZZXE.mjs → chunk-GKN5RBVR.mjs} +2 -2
- package/lib/node/{chunk-R6JVCM7X.js → chunk-J5MULIHT.js} +3 -3
- package/lib/node/{chunk-IHJSPMYM.mjs → chunk-JXGB54LE.mjs} +102 -1
- package/lib/node/chunk-JXGB54LE.mjs.map +1 -0
- package/lib/node/{chunk-3CNGDJFB.mjs → chunk-OFW5L5ET.mjs} +50 -45
- package/lib/node/chunk-OFW5L5ET.mjs.map +1 -0
- package/lib/node/{chunk-A7Q4RTDJ.mjs → chunk-R6T7CL5E.mjs} +55 -115
- package/lib/node/chunk-R6T7CL5E.mjs.map +1 -0
- package/lib/node/{chunk-RC2XPCC4.mjs → chunk-SQ6RHTJR.mjs} +2 -2
- package/lib/node/chunk-SRMAQGPM.js +30 -0
- package/lib/node/chunk-SRMAQGPM.js.map +1 -0
- package/lib/node/{chunk-4YBV77DG.js → chunk-T3TW4P64.js} +3 -3
- package/lib/node/{chunk-N4ZZFE24.js → chunk-VYO5XDY2.js} +56 -45
- package/lib/node/chunk-VYO5XDY2.js.map +1 -0
- package/lib/node/chunk-YWNGXXUQ.mjs +30 -0
- package/lib/node/{chunk-3GJB4JDF.mjs.map → chunk-YWNGXXUQ.mjs.map} +1 -1
- package/lib/node/index.d.ts +2 -3
- package/lib/node/index.js +6 -4
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +5 -3
- package/lib/node/index.mjs.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.d.ts +1 -2
- package/lib/node/interceptors/ClientRequest/index.js +6 -6
- package/lib/node/interceptors/ClientRequest/index.mjs +5 -5
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -2
- package/lib/node/interceptors/XMLHttpRequest/index.js +5 -5
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +4 -4
- package/lib/node/interceptors/fetch/index.d.ts +1 -2
- package/lib/node/interceptors/fetch/index.js +5 -5
- package/lib/node/interceptors/fetch/index.mjs +4 -4
- package/lib/node/presets/node.d.ts +1 -2
- package/lib/node/presets/node.js +10 -10
- package/lib/node/presets/node.mjs +7 -7
- package/lib/node/utils/node/index.js +3 -3
- package/lib/node/utils/node/index.mjs +2 -2
- package/package.json +2 -1
- package/src/RemoteHttpInterceptor.ts +18 -13
- package/src/RequestController.test.ts +78 -31
- package/src/RequestController.ts +63 -39
- package/src/index.ts +4 -0
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +24 -3
- package/src/interceptors/ClientRequest/index.ts +14 -18
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +45 -35
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +24 -21
- package/src/interceptors/fetch/index.ts +61 -50
- package/src/utils/handleRequest.ts +65 -95
- package/lib/browser/chunk-2QICSCCS.js +0 -238
- package/lib/browser/chunk-2QICSCCS.js.map +0 -1
- package/lib/browser/chunk-3RXCRGL2.mjs.map +0 -1
- package/lib/browser/chunk-E3CCOBRX.js.map +0 -1
- package/lib/browser/chunk-E7UVBHVO.mjs.map +0 -1
- package/lib/browser/chunk-H74PGQ4Y.js.map +0 -1
- package/lib/browser/chunk-PTTUYYVR.mjs +0 -238
- package/lib/browser/chunk-PTTUYYVR.mjs.map +0 -1
- package/lib/browser/chunk-Q7K2XAEP.mjs.map +0 -1
- package/lib/browser/chunk-T7TBRNJZ.js +0 -117
- package/lib/browser/chunk-T7TBRNJZ.js.map +0 -1
- package/lib/browser/chunk-TIPR373R.js.map +0 -1
- package/lib/node/chunk-3CNGDJFB.mjs.map +0 -1
- package/lib/node/chunk-3GJB4JDF.mjs +0 -14
- package/lib/node/chunk-4NEYTVWD.mjs.map +0 -1
- package/lib/node/chunk-72ZIHMEB.js +0 -249
- package/lib/node/chunk-72ZIHMEB.js.map +0 -1
- package/lib/node/chunk-A7Q4RTDJ.mjs.map +0 -1
- package/lib/node/chunk-A7U44ARP.js.map +0 -1
- package/lib/node/chunk-EKNRB5ZS.mjs.map +0 -1
- package/lib/node/chunk-IHJSPMYM.mjs.map +0 -1
- package/lib/node/chunk-N4ZZFE24.js.map +0 -1
- package/lib/node/chunk-SMXZPJEA.js +0 -14
- package/lib/node/chunk-SMXZPJEA.js.map +0 -1
- package/lib/node/chunk-VV2LUF5K.js.map +0 -1
- package/lib/node/chunk-Z5LWCBZS.js.map +0 -1
- package/src/utils/RequestController.ts +0 -21
- /package/lib/node/{chunk-TJDMZZXE.mjs.map → chunk-GKN5RBVR.mjs.map} +0 -0
- /package/lib/node/{chunk-R6JVCM7X.js.map → chunk-J5MULIHT.js.map} +0 -0
- /package/lib/node/{chunk-RC2XPCC4.mjs.map → chunk-SQ6RHTJR.mjs.map} +0 -0
- /package/lib/node/{chunk-4YBV77DG.js.map → chunk-T3TW4P64.js.map} +0 -0
|
@@ -6,12 +6,13 @@ var _chunkLK6DILFKjs = require('./chunk-LK6DILFK.js');
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _chunkFW45TRCBjs = require('./chunk-FW45TRCB.js');
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
var _chunk57RIRQUYjs = require('./chunk-57RIRQUY.js');
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
var _chunk2HUMWGRDjs = require('./chunk-2HUMWGRD.js');
|
|
@@ -19,7 +20,7 @@ var _chunk2HUMWGRDjs = require('./chunk-2HUMWGRD.js');
|
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
|
|
22
|
-
var
|
|
23
|
+
var _chunkJQ2S7G56js = require('./chunk-JQ2S7G56.js');
|
|
23
24
|
|
|
24
25
|
// src/interceptors/XMLHttpRequest/index.ts
|
|
25
26
|
var _outvariant = require('outvariant');
|
|
@@ -207,8 +208,8 @@ function parseJson(data) {
|
|
|
207
208
|
|
|
208
209
|
// src/interceptors/XMLHttpRequest/utils/createResponse.ts
|
|
209
210
|
function createResponse(request, body) {
|
|
210
|
-
const responseBodyOrNull =
|
|
211
|
-
return new (0,
|
|
211
|
+
const responseBodyOrNull = _chunk57RIRQUYjs.FetchResponse.isResponseWithBody(request.status) ? body : null;
|
|
212
|
+
return new (0, _chunk57RIRQUYjs.FetchResponse)(responseBodyOrNull, {
|
|
212
213
|
url: request.responseURL,
|
|
213
214
|
status: request.status,
|
|
214
215
|
statusText: request.statusText,
|
|
@@ -254,7 +255,7 @@ var XMLHttpRequestController = class {
|
|
|
254
255
|
this[kIsRequestHandled] = false;
|
|
255
256
|
this.events = /* @__PURE__ */ new Map();
|
|
256
257
|
this.uploadEvents = /* @__PURE__ */ new Map();
|
|
257
|
-
this.requestId =
|
|
258
|
+
this.requestId = _chunkJQ2S7G56js.createRequestId.call(void 0, );
|
|
258
259
|
this.requestHeaders = new Headers();
|
|
259
260
|
this.responseBuffer = new Uint8Array();
|
|
260
261
|
this.request = createProxy(initialRequest, {
|
|
@@ -273,7 +274,6 @@ var XMLHttpRequestController = class {
|
|
|
273
274
|
}
|
|
274
275
|
},
|
|
275
276
|
methodCall: ([methodName, args], invoke) => {
|
|
276
|
-
var _a;
|
|
277
277
|
switch (methodName) {
|
|
278
278
|
case "open": {
|
|
279
279
|
const [method, url] = args;
|
|
@@ -324,24 +324,27 @@ var XMLHttpRequestController = class {
|
|
|
324
324
|
const requestBody = typeof body === "string" ? _chunkLK6DILFKjs.encodeBuffer.call(void 0, body) : body;
|
|
325
325
|
const fetchRequest = this.toFetchApiRequest(requestBody);
|
|
326
326
|
this[kFetchRequest] = fetchRequest.clone();
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
this.
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
this.request.setRequestHeader(
|
|
339
|
-
_chunkTIPR373Rjs.INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
340
|
-
this.requestId
|
|
327
|
+
queueMicrotask(() => {
|
|
328
|
+
var _a;
|
|
329
|
+
const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
|
|
330
|
+
request: fetchRequest,
|
|
331
|
+
requestId: this.requestId
|
|
332
|
+
})) || Promise.resolve();
|
|
333
|
+
onceRequestSettled.finally(() => {
|
|
334
|
+
if (!this[kIsRequestHandled]) {
|
|
335
|
+
this.logger.info(
|
|
336
|
+
"request callback settled but request has not been handled (readystate %d), performing as-is...",
|
|
337
|
+
this.request.readyState
|
|
341
338
|
);
|
|
339
|
+
if (IS_NODE) {
|
|
340
|
+
this.request.setRequestHeader(
|
|
341
|
+
_chunkJQ2S7G56js.INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
342
|
+
this.requestId
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return invoke();
|
|
342
346
|
}
|
|
343
|
-
|
|
344
|
-
}
|
|
347
|
+
});
|
|
345
348
|
});
|
|
346
349
|
break;
|
|
347
350
|
}
|
|
@@ -694,7 +697,7 @@ var XMLHttpRequestController = class {
|
|
|
694
697
|
}
|
|
695
698
|
});
|
|
696
699
|
define(fetchRequest, "headers", proxyHeaders);
|
|
697
|
-
|
|
700
|
+
_chunk57RIRQUYjs.setRawRequest.call(void 0, fetchRequest, this.request);
|
|
698
701
|
this.logger.info("converted request to a Fetch API Request!", fetchRequest);
|
|
699
702
|
return fetchRequest;
|
|
700
703
|
}
|
|
@@ -743,35 +746,37 @@ function createXMLHttpRequestProxy({
|
|
|
743
746
|
logger
|
|
744
747
|
);
|
|
745
748
|
xhrRequestController.onRequest = async function({ request, requestId }) {
|
|
746
|
-
const controller = new (0,
|
|
749
|
+
const controller = new (0, _chunk57RIRQUYjs.RequestController)(request, {
|
|
750
|
+
passthrough: () => {
|
|
751
|
+
this.logger.info(
|
|
752
|
+
"no mocked response received, performing request as-is..."
|
|
753
|
+
);
|
|
754
|
+
},
|
|
755
|
+
respondWith: async (response) => {
|
|
756
|
+
if (_chunkFW45TRCBjs.isResponseError.call(void 0, response)) {
|
|
757
|
+
this.errorWith(new TypeError("Network error"));
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
await this.respondWith(response);
|
|
761
|
+
},
|
|
762
|
+
errorWith: (reason) => {
|
|
763
|
+
this.logger.info("request errored!", { error: reason });
|
|
764
|
+
if (reason instanceof Error) {
|
|
765
|
+
this.errorWith(reason);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
});
|
|
747
769
|
this.logger.info("awaiting mocked response...");
|
|
748
770
|
this.logger.info(
|
|
749
771
|
'emitting the "request" event for %s listener(s)...',
|
|
750
772
|
emitter.listenerCount("request")
|
|
751
773
|
);
|
|
752
|
-
|
|
774
|
+
await _chunkFW45TRCBjs.handleRequest.call(void 0, {
|
|
753
775
|
request,
|
|
754
776
|
requestId,
|
|
755
777
|
controller,
|
|
756
|
-
emitter
|
|
757
|
-
onResponse: async (response) => {
|
|
758
|
-
await this.respondWith(response);
|
|
759
|
-
},
|
|
760
|
-
onRequestError: () => {
|
|
761
|
-
this.errorWith(new TypeError("Network error"));
|
|
762
|
-
},
|
|
763
|
-
onError: (error) => {
|
|
764
|
-
this.logger.info("request errored!", { error });
|
|
765
|
-
if (error instanceof Error) {
|
|
766
|
-
this.errorWith(error);
|
|
767
|
-
}
|
|
768
|
-
}
|
|
778
|
+
emitter
|
|
769
779
|
});
|
|
770
|
-
if (!isRequestHandled) {
|
|
771
|
-
this.logger.info(
|
|
772
|
-
"no mocked response received, performing request as-is..."
|
|
773
|
-
);
|
|
774
|
-
}
|
|
775
780
|
};
|
|
776
781
|
xhrRequestController.onResponse = async function({
|
|
777
782
|
response,
|
|
@@ -797,7 +802,7 @@ function createXMLHttpRequestProxy({
|
|
|
797
802
|
}
|
|
798
803
|
|
|
799
804
|
// src/interceptors/XMLHttpRequest/index.ts
|
|
800
|
-
var _XMLHttpRequestInterceptor = class extends
|
|
805
|
+
var _XMLHttpRequestInterceptor = class extends _chunkJQ2S7G56js.Interceptor {
|
|
801
806
|
constructor() {
|
|
802
807
|
super(_XMLHttpRequestInterceptor.interceptorSymbol);
|
|
803
808
|
}
|
|
@@ -809,7 +814,7 @@ var _XMLHttpRequestInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
|
|
|
809
814
|
logger.info('patching "XMLHttpRequest" module...');
|
|
810
815
|
const PureXMLHttpRequest = globalThis.XMLHttpRequest;
|
|
811
816
|
_outvariant.invariant.call(void 0,
|
|
812
|
-
!PureXMLHttpRequest[
|
|
817
|
+
!PureXMLHttpRequest[_chunk57RIRQUYjs.IS_PATCHED_MODULE],
|
|
813
818
|
'Failed to patch the "XMLHttpRequest" module: already patched.'
|
|
814
819
|
);
|
|
815
820
|
globalThis.XMLHttpRequest = createXMLHttpRequestProxy({
|
|
@@ -820,13 +825,13 @@ var _XMLHttpRequestInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
|
|
|
820
825
|
'native "XMLHttpRequest" module patched!',
|
|
821
826
|
globalThis.XMLHttpRequest.name
|
|
822
827
|
);
|
|
823
|
-
Object.defineProperty(globalThis.XMLHttpRequest,
|
|
828
|
+
Object.defineProperty(globalThis.XMLHttpRequest, _chunk57RIRQUYjs.IS_PATCHED_MODULE, {
|
|
824
829
|
enumerable: true,
|
|
825
830
|
configurable: true,
|
|
826
831
|
value: true
|
|
827
832
|
});
|
|
828
833
|
this.subscriptions.push(() => {
|
|
829
|
-
Object.defineProperty(globalThis.XMLHttpRequest,
|
|
834
|
+
Object.defineProperty(globalThis.XMLHttpRequest, _chunk57RIRQUYjs.IS_PATCHED_MODULE, {
|
|
830
835
|
value: void 0
|
|
831
836
|
});
|
|
832
837
|
globalThis.XMLHttpRequest = PureXMLHttpRequest;
|
|
@@ -843,4 +848,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
|
|
|
843
848
|
|
|
844
849
|
|
|
845
850
|
exports.XMLHttpRequestInterceptor = XMLHttpRequestInterceptor;
|
|
846
|
-
//# sourceMappingURL=chunk-
|
|
851
|
+
//# sourceMappingURL=chunk-2MCNQOY3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/interceptors/XMLHttpRequest/index.ts","../../src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts","../../src/interceptors/XMLHttpRequest/utils/concatArrayBuffer.ts","../../src/interceptors/XMLHttpRequest/polyfills/EventPolyfill.ts","../../src/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.ts","../../src/interceptors/XMLHttpRequest/utils/createEvent.ts","../../src/utils/findPropertySource.ts","../../src/utils/createProxy.ts","../../src/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.ts","../../src/utils/parseJson.ts","../../src/interceptors/XMLHttpRequest/utils/createResponse.ts","../../src/interceptors/XMLHttpRequest/utils/getBodyByteLength.ts","../../src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts"],"names":["invariant","next"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,aAAAA,kBAAiB;;;ACA1B,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB;;;ACEvB,SAAS,kBACd,MACA,OACY;AACZ,QAAM,SAAS,IAAI,WAAW,KAAK,aAAa,MAAM,UAAU;AAChE,SAAO,IAAI,MAAM,CAAC;AAClB,SAAO,IAAI,OAAO,KAAK,UAAU;AACjC,SAAO;AACT;;;ACXO,IAAM,gBAAN,MAAqC;AAAA,EAwB1C,YACE,MACA,SACA;AA1BF,SAAS,OAAO;AAChB,SAAS,kBAAkB;AAC3B,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAE1B,SAAO,OAAe;AACtB,SAAO,aAAiC;AAExC,SAAO,gBAAoC;AAC3C,SAAO,aAAqB;AAE5B,SAAO,YAAqB;AAC5B,SAAO,WAAoB;AAC3B,SAAO,aAAsB;AAC7B,SAAO,mBAA4B;AACnC,SAAO,UAAmB;AAC1B,SAAO,mBAA4B;AACnC,SAAO,SAAiB;AACxB,SAAO,QAAgB;AAEvB,wBAAwB;AACxB,uBAAuB;AAMrB,SAAK,OA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AAK;AAAA,IACf,CAAC;AAED,WAAO;AAAA,MACL;AAAA,MACA,WAAW,eAAe;AAAA,IAC5B;AAEA,WAAO,eAAe,WAAW,gBAAgB,mBAAmB;AAAA,MAClE,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,OAAO;AAAA,IACT,CAAC;AAED,SAAK,cAAc,KAAK,MAAM;AAC5B,aAAO,eAAe,WAAW,gBAAgB,mBAAmB;AAAA,QAClE,OAAO;AAAA,MACT,CAAC;AAED,iBAAW,iBAAiB;AAC5B,aAAO;AAAA,QACL;AAAA,QACA,WAAW,eAAe;AAAA,MAC5B;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAnDO,IAAM,4BAAN;AAAM,0BACJ,oBAAoB,OAAO,KAAK","sourcesContent":["import { invariant } from 'outvariant'\nimport { Emitter } from 'strict-event-emitter'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { createXMLHttpRequestProxy } from './XMLHttpRequestProxy'\nimport { hasConfigurableGlobal } from '../../utils/hasConfigurableGlobal'\n\nexport type XMLHttpRequestEmitter = Emitter<HttpRequestEventMap>\n\nexport class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static interceptorSymbol = Symbol('xhr')\n\n constructor() {\n super(XMLHttpRequestInterceptor.interceptorSymbol)\n }\n\n protected checkEnvironment() {\n return hasConfigurableGlobal('XMLHttpRequest')\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n logger.info('patching \"XMLHttpRequest\" module...')\n\n const PureXMLHttpRequest = globalThis.XMLHttpRequest\n\n invariant(\n !(PureXMLHttpRequest as any)[IS_PATCHED_MODULE],\n 'Failed to patch the \"XMLHttpRequest\" module: already patched.'\n )\n\n globalThis.XMLHttpRequest = createXMLHttpRequestProxy({\n emitter: this.emitter,\n logger: this.logger,\n })\n\n logger.info(\n 'native \"XMLHttpRequest\" module patched!',\n globalThis.XMLHttpRequest.name\n )\n\n Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {\n enumerable: true,\n configurable: true,\n value: true,\n })\n\n this.subscriptions.push(() => {\n Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {\n value: undefined,\n })\n\n globalThis.XMLHttpRequest = PureXMLHttpRequest\n logger.info(\n 'native \"XMLHttpRequest\" module restored!',\n globalThis.XMLHttpRequest.name\n )\n })\n }\n}\n","import { invariant } from 'outvariant'\nimport { isNodeProcess } from 'is-node-process'\nimport type { Logger } from '@open-draft/logger'\nimport { concatArrayBuffer } from './utils/concatArrayBuffer'\nimport { createEvent } from './utils/createEvent'\nimport {\n decodeBuffer,\n encodeBuffer,\n toArrayBuffer,\n} from '../../utils/bufferUtils'\nimport { createProxy } from '../../utils/createProxy'\nimport { isDomParserSupportedType } from './utils/isDomParserSupportedType'\nimport { parseJson } from '../../utils/parseJson'\nimport { createResponse } from './utils/createResponse'\nimport { INTERNAL_REQUEST_ID_HEADER_NAME } from '../../Interceptor'\nimport { createRequestId } from '../../createRequestId'\nimport { getBodyByteLength } from './utils/getBodyByteLength'\nimport { setRawRequest } from '../../getRawRequest'\n\nconst kIsRequestHandled = Symbol('kIsRequestHandled')\nconst IS_NODE = isNodeProcess()\nconst kFetchRequest = Symbol('kFetchRequest')\n\n/**\n * An `XMLHttpRequest` instance controller that allows us\n * to handle any given request instance (e.g. responding to it).\n */\nexport class XMLHttpRequestController {\n public request: XMLHttpRequest\n public requestId: string\n public onRequest?: (\n this: XMLHttpRequestController,\n args: {\n request: Request\n requestId: string\n }\n ) => Promise<void>\n public onResponse?: (\n this: XMLHttpRequestController,\n args: {\n response: Response\n isMockedResponse: boolean\n request: Request\n requestId: string\n }\n ) => void;\n\n [kIsRequestHandled]: boolean;\n [kFetchRequest]?: Request\n private method: string = 'GET'\n private url: URL = null as any\n private requestHeaders: Headers\n private responseBuffer: Uint8Array\n private events: Map<keyof XMLHttpRequestEventTargetEventMap, Array<Function>>\n private uploadEvents: Map<\n keyof XMLHttpRequestEventTargetEventMap,\n Array<Function>\n >\n\n constructor(\n readonly initialRequest: XMLHttpRequest,\n public logger: Logger\n ) {\n this[kIsRequestHandled] = false\n\n this.events = new Map()\n this.uploadEvents = new Map()\n this.requestId = createRequestId()\n this.requestHeaders = new Headers()\n this.responseBuffer = new Uint8Array()\n\n this.request = createProxy(initialRequest, {\n setProperty: ([propertyName, nextValue], invoke) => {\n switch (propertyName) {\n case 'ontimeout': {\n const eventName = propertyName.slice(\n 2\n ) as keyof XMLHttpRequestEventTargetEventMap\n\n /**\n * @note Proxy callbacks to event listeners because JSDOM has trouble\n * translating these properties to callbacks. It seemed to be operating\n * on events exclusively.\n */\n this.request.addEventListener(eventName, nextValue as any)\n\n return invoke()\n }\n\n default: {\n return invoke()\n }\n }\n },\n methodCall: ([methodName, args], invoke) => {\n switch (methodName) {\n case 'open': {\n const [method, url] = args as [string, string | undefined]\n\n if (typeof url === 'undefined') {\n this.method = 'GET'\n this.url = toAbsoluteUrl(method)\n } else {\n this.method = method\n this.url = toAbsoluteUrl(url)\n }\n\n this.logger = this.logger.extend(`${this.method} ${this.url.href}`)\n this.logger.info('open', this.method, this.url.href)\n\n return invoke()\n }\n\n case 'addEventListener': {\n const [eventName, listener] = args as [\n keyof XMLHttpRequestEventTargetEventMap,\n Function,\n ]\n\n this.registerEvent(eventName, listener)\n this.logger.info('addEventListener', eventName, listener)\n\n return invoke()\n }\n\n case 'setRequestHeader': {\n const [name, value] = args as [string, string]\n this.requestHeaders.set(name, value)\n\n this.logger.info('setRequestHeader', name, value)\n\n return invoke()\n }\n\n case 'send': {\n const [body] = args as [\n body?: XMLHttpRequestBodyInit | Document | null,\n ]\n\n this.request.addEventListener('load', () => {\n if (typeof this.onResponse !== 'undefined') {\n // Create a Fetch API Response representation of whichever\n // response this XMLHttpRequest received. Note those may\n // be either a mocked and the original response.\n const fetchResponse = createResponse(\n this.request,\n /**\n * The `response` property is the right way to read\n * the ambiguous response body, as the request's \"responseType\" may differ.\n * @see https://xhr.spec.whatwg.org/#the-response-attribute\n */\n this.request.response\n )\n\n // Notify the consumer about the response.\n this.onResponse.call(this, {\n response: fetchResponse,\n isMockedResponse: this[kIsRequestHandled],\n request: fetchRequest,\n requestId: this.requestId!,\n })\n }\n })\n\n const requestBody =\n typeof body === 'string' ? encodeBuffer(body) : body\n\n // Delegate request handling to the consumer.\n const fetchRequest = this.toFetchApiRequest(requestBody)\n this[kFetchRequest] = fetchRequest.clone()\n\n /**\n * @note Start request handling on the next tick so that the user\n * could add event listeners for \"loadend\" before the interceptor fires it.\n */\n queueMicrotask(() => {\n const onceRequestSettled =\n this.onRequest?.call(this, {\n request: fetchRequest,\n requestId: this.requestId!,\n }) || Promise.resolve()\n\n onceRequestSettled.finally(() => {\n // If the consumer didn't handle the request (called `.respondWith()`) perform it as-is.\n if (!this[kIsRequestHandled]) {\n this.logger.info(\n 'request callback settled but request has not been handled (readystate %d), performing as-is...',\n this.request.readyState\n )\n\n /**\n * @note Set the intercepted request ID on the original request in Node.js\n * so that if it triggers any other interceptors, they don't attempt\n * to process it once again.\n *\n * For instance, XMLHttpRequest is often implemented via \"http.ClientRequest\"\n * and we don't want for both XHR and ClientRequest interceptors to\n * handle the same request at the same time (e.g. emit the \"response\" event twice).\n */\n if (IS_NODE) {\n this.request.setRequestHeader(\n INTERNAL_REQUEST_ID_HEADER_NAME,\n this.requestId!\n )\n }\n\n return invoke()\n }\n })\n })\n\n break\n }\n\n default: {\n return invoke()\n }\n }\n },\n })\n\n /**\n * Proxy the `.upload` property to gather the event listeners/callbacks.\n */\n define(\n this.request,\n 'upload',\n createProxy(this.request.upload, {\n setProperty: ([propertyName, nextValue], invoke) => {\n switch (propertyName) {\n case 'onloadstart':\n case 'onprogress':\n case 'onaboart':\n case 'onerror':\n case 'onload':\n case 'ontimeout':\n case 'onloadend': {\n const eventName = propertyName.slice(\n 2\n ) as keyof XMLHttpRequestEventTargetEventMap\n\n this.registerUploadEvent(eventName, nextValue as Function)\n }\n }\n\n return invoke()\n },\n methodCall: ([methodName, args], invoke) => {\n switch (methodName) {\n case 'addEventListener': {\n const [eventName, listener] = args as [\n keyof XMLHttpRequestEventTargetEventMap,\n Function,\n ]\n this.registerUploadEvent(eventName, listener)\n this.logger.info('upload.addEventListener', eventName, listener)\n\n return invoke()\n }\n }\n },\n })\n )\n }\n\n private registerEvent(\n eventName: keyof XMLHttpRequestEventTargetEventMap,\n listener: Function\n ): void {\n const prevEvents = this.events.get(eventName) || []\n const nextEvents = prevEvents.concat(listener)\n this.events.set(eventName, nextEvents)\n\n this.logger.info('registered event \"%s\"', eventName, listener)\n }\n\n private registerUploadEvent(\n eventName: keyof XMLHttpRequestEventTargetEventMap,\n listener: Function\n ): void {\n const prevEvents = this.uploadEvents.get(eventName) || []\n const nextEvents = prevEvents.concat(listener)\n this.uploadEvents.set(eventName, nextEvents)\n\n this.logger.info('registered upload event \"%s\"', eventName, listener)\n }\n\n /**\n * Responds to the current request with the given\n * Fetch API `Response` instance.\n */\n public async respondWith(response: Response): Promise<void> {\n /**\n * @note Since `XMLHttpRequestController` delegates the handling of the responses\n * to the \"load\" event listener that doesn't distinguish between the mocked and original\n * responses, mark the request that had a mocked response with a corresponding symbol.\n *\n * Mark this request as having a mocked response immediately since\n * calculating request/response total body length is asynchronous.\n */\n this[kIsRequestHandled] = true\n\n /**\n * Dispatch request upload events for requests with a body.\n * @see https://github.com/mswjs/interceptors/issues/573\n */\n if (this[kFetchRequest]) {\n const totalRequestBodyLength = await getBodyByteLength(\n this[kFetchRequest]\n )\n\n this.trigger('loadstart', this.request.upload, {\n loaded: 0,\n total: totalRequestBodyLength,\n })\n this.trigger('progress', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n this.trigger('load', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n\n this.trigger('loadend', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n }\n\n this.logger.info(\n 'responding with a mocked response: %d %s',\n response.status,\n response.statusText\n )\n\n define(this.request, 'status', response.status)\n define(this.request, 'statusText', response.statusText)\n define(this.request, 'responseURL', this.url.href)\n\n this.request.getResponseHeader = new Proxy(this.request.getResponseHeader, {\n apply: (_, __, args: [name: string]) => {\n this.logger.info('getResponseHeader', args[0])\n\n if (this.request.readyState < this.request.HEADERS_RECEIVED) {\n this.logger.info('headers not received yet, returning null')\n\n // Headers not received yet, nothing to return.\n return null\n }\n\n const headerValue = response.headers.get(args[0])\n this.logger.info(\n 'resolved response header \"%s\" to',\n args[0],\n headerValue\n )\n\n return headerValue\n },\n })\n\n this.request.getAllResponseHeaders = new Proxy(\n this.request.getAllResponseHeaders,\n {\n apply: () => {\n this.logger.info('getAllResponseHeaders')\n\n if (this.request.readyState < this.request.HEADERS_RECEIVED) {\n this.logger.info('headers not received yet, returning empty string')\n\n // Headers not received yet, nothing to return.\n return ''\n }\n\n const headersList = Array.from(response.headers.entries())\n const allHeaders = headersList\n .map(([headerName, headerValue]) => {\n return `${headerName}: ${headerValue}`\n })\n .join('\\r\\n')\n\n this.logger.info('resolved all response headers to', allHeaders)\n\n return allHeaders\n },\n }\n )\n\n // Update the response getters to resolve against the mocked response.\n Object.defineProperties(this.request, {\n response: {\n enumerable: true,\n configurable: false,\n get: () => this.response,\n },\n responseText: {\n enumerable: true,\n configurable: false,\n get: () => this.responseText,\n },\n responseXML: {\n enumerable: true,\n configurable: false,\n get: () => this.responseXML,\n },\n })\n\n const totalResponseBodyLength = await getBodyByteLength(response.clone())\n\n this.logger.info('calculated response body length', totalResponseBodyLength)\n\n this.trigger('loadstart', this.request, {\n loaded: 0,\n total: totalResponseBodyLength,\n })\n\n this.setReadyState(this.request.HEADERS_RECEIVED)\n this.setReadyState(this.request.LOADING)\n\n const finalizeResponse = () => {\n this.logger.info('finalizing the mocked response...')\n\n this.setReadyState(this.request.DONE)\n\n this.trigger('load', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n\n this.trigger('loadend', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n }\n\n if (response.body) {\n this.logger.info('mocked response has body, streaming...')\n\n const reader = response.body.getReader()\n\n const readNextResponseBodyChunk = async () => {\n const { value, done } = await reader.read()\n\n if (done) {\n this.logger.info('response body stream done!')\n finalizeResponse()\n return\n }\n\n if (value) {\n this.logger.info('read response body chunk:', value)\n this.responseBuffer = concatArrayBuffer(this.responseBuffer, value)\n\n this.trigger('progress', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n }\n\n readNextResponseBodyChunk()\n }\n\n readNextResponseBodyChunk()\n } else {\n finalizeResponse()\n }\n }\n\n private responseBufferToText(): string {\n return decodeBuffer(this.responseBuffer)\n }\n\n get response(): unknown {\n this.logger.info(\n 'getResponse (responseType: %s)',\n this.request.responseType\n )\n\n if (this.request.readyState !== this.request.DONE) {\n return null\n }\n\n switch (this.request.responseType) {\n case 'json': {\n const responseJson = parseJson(this.responseBufferToText())\n this.logger.info('resolved response JSON', responseJson)\n\n return responseJson\n }\n\n case 'arraybuffer': {\n const arrayBuffer = toArrayBuffer(this.responseBuffer)\n this.logger.info('resolved response ArrayBuffer', arrayBuffer)\n\n return arrayBuffer\n }\n\n case 'blob': {\n const mimeType =\n this.request.getResponseHeader('Content-Type') || 'text/plain'\n const responseBlob = new Blob([this.responseBufferToText()], {\n type: mimeType,\n })\n\n this.logger.info(\n 'resolved response Blob (mime type: %s)',\n responseBlob,\n mimeType\n )\n\n return responseBlob\n }\n\n default: {\n const responseText = this.responseBufferToText()\n this.logger.info(\n 'resolving \"%s\" response type as text',\n this.request.responseType,\n responseText\n )\n\n return responseText\n }\n }\n }\n\n get responseText(): string {\n /**\n * Throw when trying to read the response body as text when the\n * \"responseType\" doesn't expect text. This just respects the spec better.\n * @see https://xhr.spec.whatwg.org/#the-responsetext-attribute\n */\n invariant(\n this.request.responseType === '' || this.request.responseType === 'text',\n 'InvalidStateError: The object is in invalid state.'\n )\n\n if (\n this.request.readyState !== this.request.LOADING &&\n this.request.readyState !== this.request.DONE\n ) {\n return ''\n }\n\n const responseText = this.responseBufferToText()\n this.logger.info('getResponseText: \"%s\"', responseText)\n\n return responseText\n }\n\n get responseXML(): Document | null {\n invariant(\n this.request.responseType === '' ||\n this.request.responseType === 'document',\n 'InvalidStateError: The object is in invalid state.'\n )\n\n if (this.request.readyState !== this.request.DONE) {\n return null\n }\n\n const contentType = this.request.getResponseHeader('Content-Type') || ''\n\n if (typeof DOMParser === 'undefined') {\n console.warn(\n 'Cannot retrieve XMLHttpRequest response body as XML: DOMParser is not defined. You are likely using an environment that is not browser or does not polyfill browser globals correctly.'\n )\n return null\n }\n\n if (isDomParserSupportedType(contentType)) {\n return new DOMParser().parseFromString(\n this.responseBufferToText(),\n contentType\n )\n }\n\n return null\n }\n\n public errorWith(error?: Error): void {\n /**\n * @note Mark this request as handled even if it received a mock error.\n * This prevents the controller from trying to perform this request as-is.\n */\n this[kIsRequestHandled] = true\n this.logger.info('responding with an error')\n\n this.setReadyState(this.request.DONE)\n this.trigger('error', this.request)\n this.trigger('loadend', this.request)\n }\n\n /**\n * Transitions this request's `readyState` to the given one.\n */\n private setReadyState(nextReadyState: number): void {\n this.logger.info(\n 'setReadyState: %d -> %d',\n this.request.readyState,\n nextReadyState\n )\n\n if (this.request.readyState === nextReadyState) {\n this.logger.info('ready state identical, skipping transition...')\n return\n }\n\n define(this.request, 'readyState', nextReadyState)\n\n this.logger.info('set readyState to: %d', nextReadyState)\n\n if (nextReadyState !== this.request.UNSENT) {\n this.logger.info('triggering \"readystatechange\" event...')\n\n this.trigger('readystatechange', this.request)\n }\n }\n\n /**\n * Triggers given event on the `XMLHttpRequest` instance.\n */\n private trigger<\n EventName extends keyof (XMLHttpRequestEventTargetEventMap & {\n readystatechange: ProgressEvent<XMLHttpRequestEventTarget>\n }),\n >(\n eventName: EventName,\n target: XMLHttpRequest | XMLHttpRequestUpload,\n options?: ProgressEventInit\n ): void {\n const callback = (target as XMLHttpRequest)[`on${eventName}`]\n const event = createEvent(target, eventName, options)\n\n this.logger.info('trigger \"%s\"', eventName, options || '')\n\n // Invoke direct callbacks.\n if (typeof callback === 'function') {\n this.logger.info('found a direct \"%s\" callback, calling...', eventName)\n callback.call(target as XMLHttpRequest, event)\n }\n\n // Invoke event listeners.\n const events =\n target instanceof XMLHttpRequestUpload ? this.uploadEvents : this.events\n\n for (const [registeredEventName, listeners] of events) {\n if (registeredEventName === eventName) {\n this.logger.info(\n 'found %d listener(s) for \"%s\" event, calling...',\n listeners.length,\n eventName\n )\n\n listeners.forEach((listener) => listener.call(target, event))\n }\n }\n }\n\n /**\n * Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.\n */\n private toFetchApiRequest(\n body: XMLHttpRequestBodyInit | Document | null | undefined\n ): Request {\n this.logger.info('converting request to a Fetch API Request...')\n\n // If the `Document` is used as the body of this XMLHttpRequest,\n // set its inner text as the Fetch API Request body.\n const resolvedBody =\n body instanceof Document ? body.documentElement.innerText : body\n\n const fetchRequest = new Request(this.url.href, {\n method: this.method,\n headers: this.requestHeaders,\n /**\n * @see https://xhr.spec.whatwg.org/#cross-origin-credentials\n */\n credentials: this.request.withCredentials ? 'include' : 'same-origin',\n body: ['GET', 'HEAD'].includes(this.method.toUpperCase())\n ? null\n : resolvedBody,\n })\n\n const proxyHeaders = createProxy(fetchRequest.headers, {\n methodCall: ([methodName, args], invoke) => {\n // Forward the latest state of the internal request headers\n // because the interceptor might have modified them\n // without responding to the request.\n switch (methodName) {\n case 'append':\n case 'set': {\n const [headerName, headerValue] = args as [string, string]\n this.request.setRequestHeader(headerName, headerValue)\n break\n }\n\n case 'delete': {\n const [headerName] = args as [string]\n console.warn(\n `XMLHttpRequest: Cannot remove a \"${headerName}\" header from the Fetch API representation of the \"${fetchRequest.method} ${fetchRequest.url}\" request. XMLHttpRequest headers cannot be removed.`\n )\n break\n }\n }\n\n return invoke()\n },\n })\n define(fetchRequest, 'headers', proxyHeaders)\n setRawRequest(fetchRequest, this.request)\n\n this.logger.info('converted request to a Fetch API Request!', fetchRequest)\n\n return fetchRequest\n }\n}\n\nfunction toAbsoluteUrl(url: string | URL): URL {\n /**\n * @note XMLHttpRequest interceptor may run in environments\n * that implement XMLHttpRequest but don't implement \"location\"\n * (for example, React Native). If that's the case, return the\n * input URL as-is (nothing to be relative to).\n * @see https://github.com/mswjs/msw/issues/1777\n */\n if (typeof location === 'undefined') {\n return new URL(url)\n }\n\n return new URL(url.toString(), location.href)\n}\n\nfunction define(\n target: object,\n property: string | symbol,\n value: unknown\n): void {\n Reflect.defineProperty(target, property, {\n // Ensure writable properties to allow redefining readonly properties.\n writable: true,\n enumerable: true,\n value,\n })\n}\n","/**\n * Concatenate two `Uint8Array` buffers.\n */\nexport function concatArrayBuffer(\n left: Uint8Array,\n right: Uint8Array\n): Uint8Array {\n const result = new Uint8Array(left.byteLength + right.byteLength)\n result.set(left, 0)\n result.set(right, left.byteLength)\n return result\n}\n","export class EventPolyfill implements Event {\n readonly NONE = 0\n readonly CAPTURING_PHASE = 1\n readonly AT_TARGET = 2\n readonly BUBBLING_PHASE = 3\n\n public type: string = ''\n public srcElement: EventTarget | null = null\n public target: EventTarget | null\n public currentTarget: EventTarget | null = null\n public eventPhase: number = 0\n public timeStamp: number\n public isTrusted: boolean = true\n public composed: boolean = false\n public cancelable: boolean = true\n public defaultPrevented: boolean = false\n public bubbles: boolean = true\n public lengthComputable: boolean = true\n public loaded: number = 0\n public total: number = 0\n\n cancelBubble: boolean = false\n returnValue: boolean = true\n\n constructor(\n type: string,\n options?: { target: EventTarget; currentTarget: EventTarget }\n ) {\n this.type = type\n this.target = options?.target || null\n this.currentTarget = options?.currentTarget || null\n this.timeStamp = Date.now()\n }\n\n public composedPath(): EventTarget[] {\n return []\n }\n\n public initEvent(type: string, bubbles?: boolean, cancelable?: boolean) {\n this.type = type\n this.bubbles = !!bubbles\n this.cancelable = !!cancelable\n }\n\n public preventDefault() {\n this.defaultPrevented = true\n }\n\n public stopPropagation() {}\n public stopImmediatePropagation() {}\n}\n","import { EventPolyfill } from './EventPolyfill'\n\nexport class ProgressEventPolyfill extends EventPolyfill {\n readonly lengthComputable: boolean\n readonly composed: boolean\n readonly loaded: number\n readonly total: number\n\n constructor(type: string, init?: ProgressEventInit) {\n super(type)\n\n this.lengthComputable = init?.lengthComputable || false\n this.composed = init?.composed || false\n this.loaded = init?.loaded || 0\n this.total = init?.total || 0\n }\n}\n","import { EventPolyfill } from '../polyfills/EventPolyfill'\nimport { ProgressEventPolyfill } from '../polyfills/ProgressEventPolyfill'\n\nconst SUPPORTS_PROGRESS_EVENT = typeof ProgressEvent !== 'undefined'\n\nexport function createEvent(\n target: XMLHttpRequest | XMLHttpRequestUpload,\n type: string,\n init?: ProgressEventInit\n): EventPolyfill | ProgressEvent {\n const progressEvents = [\n 'error',\n 'progress',\n 'loadstart',\n 'loadend',\n 'load',\n 'timeout',\n 'abort',\n ]\n\n /**\n * `ProgressEvent` is not supported in React Native.\n * @see https://github.com/mswjs/interceptors/issues/40\n */\n const ProgressEventClass = SUPPORTS_PROGRESS_EVENT\n ? ProgressEvent\n : ProgressEventPolyfill\n\n const event = progressEvents.includes(type)\n ? new ProgressEventClass(type, {\n lengthComputable: true,\n loaded: init?.loaded || 0,\n total: init?.total || 0,\n })\n : new EventPolyfill(type, {\n target,\n currentTarget: target,\n })\n\n return event\n}\n","/**\n * Returns the source object of the given property on the target object\n * (the target itself, any parent in its prototype, or null).\n */\nexport function findPropertySource(\n target: object,\n propertyName: string | symbol\n): object | null {\n if (!(propertyName in target)) {\n return null\n }\n\n const hasProperty = Object.prototype.hasOwnProperty.call(target, propertyName)\n if (hasProperty) {\n return target\n }\n\n const prototype = Reflect.getPrototypeOf(target)\n return prototype ? findPropertySource(prototype, propertyName) : null\n}\n","import { findPropertySource } from './findPropertySource'\n\nexport interface ProxyOptions<Target extends Record<string, any>> {\n constructorCall?(args: Array<unknown>, next: NextFunction<Target>): Target\n\n methodCall?<F extends keyof Target>(\n this: Target,\n data: [methodName: F, args: Array<unknown>],\n next: NextFunction<void>\n ): void\n\n setProperty?(\n data: [propertyName: string | symbol, nextValue: unknown],\n next: NextFunction<boolean>\n ): boolean\n\n getProperty?(\n data: [propertyName: string | symbol, receiver: Target],\n next: NextFunction<void>\n ): void\n}\n\nexport type NextFunction<ReturnType> = () => ReturnType\n\nexport function createProxy<Target extends object>(\n target: Target,\n options: ProxyOptions<Target>\n): Target {\n const proxy = new Proxy(target, optionsToProxyHandler(options))\n\n return proxy\n}\n\nfunction optionsToProxyHandler<T extends Record<string, any>>(\n options: ProxyOptions<T>\n): ProxyHandler<T> {\n const { constructorCall, methodCall, getProperty, setProperty } = options\n const handler: ProxyHandler<T> = {}\n\n if (typeof constructorCall !== 'undefined') {\n handler.construct = function (target, args, newTarget) {\n const next = Reflect.construct.bind(null, target as any, args, newTarget)\n return constructorCall.call(newTarget, args, next)\n }\n }\n\n handler.set = function (target, propertyName, nextValue) {\n const next = () => {\n const propertySource = findPropertySource(target, propertyName) || target\n const ownDescriptors = Reflect.getOwnPropertyDescriptor(\n propertySource,\n propertyName\n )\n\n // Respect any custom setters present for this property.\n if (typeof ownDescriptors?.set !== 'undefined') {\n ownDescriptors.set.apply(target, [nextValue])\n return true\n }\n\n // Otherwise, set the property on the source.\n return Reflect.defineProperty(propertySource, propertyName, {\n writable: true,\n enumerable: true,\n configurable: true,\n value: nextValue,\n })\n }\n\n if (typeof setProperty !== 'undefined') {\n return setProperty.call(target, [propertyName, nextValue], next)\n }\n\n return next()\n }\n\n handler.get = function (target, propertyName, receiver) {\n /**\n * @note Using `Reflect.get()` here causes \"TypeError: Illegal invocation\".\n */\n const next = () => target[propertyName as any]\n\n const value =\n typeof getProperty !== 'undefined'\n ? getProperty.call(target, [propertyName, receiver], next)\n : next()\n\n if (typeof value === 'function') {\n return (...args: Array<any>) => {\n const next = value.bind(target, ...args)\n\n if (typeof methodCall !== 'undefined') {\n return methodCall.call(target, [propertyName as any, args], next)\n }\n\n return next()\n }\n }\n\n return value\n }\n\n return handler\n}\n","export function isDomParserSupportedType(\n type: string\n): type is DOMParserSupportedType {\n const supportedTypes: Array<DOMParserSupportedType> = [\n 'application/xhtml+xml',\n 'application/xml',\n 'image/svg+xml',\n 'text/html',\n 'text/xml',\n ]\n return supportedTypes.some((supportedType) => {\n return type.startsWith(supportedType)\n })\n}\n","/**\n * Parses a given string into JSON.\n * Gracefully handles invalid JSON by returning `null`.\n */\nexport function parseJson(data: string): Record<string, unknown> | null {\n try {\n const json = JSON.parse(data)\n return json\n } catch (_) {\n return null\n }\n}\n","import { FetchResponse } from '../../../utils/fetchUtils'\n\n/**\n * Creates a Fetch API `Response` instance from the given\n * `XMLHttpRequest` instance and a response body.\n */\nexport function createResponse(\n request: XMLHttpRequest,\n body: BodyInit | null\n): Response {\n /**\n * Handle XMLHttpRequest responses that must have null as the\n * response body when represented using Fetch API Response.\n * XMLHttpRequest response will always have an empty string\n * as the \"request.response\" in those cases, resulting in an error\n * when constructing a Response instance.\n * @see https://github.com/mswjs/interceptors/issues/379\n */\n const responseBodyOrNull = FetchResponse.isResponseWithBody(request.status)\n ? body\n : null\n\n return new FetchResponse(responseBodyOrNull, {\n url: request.responseURL,\n status: request.status,\n statusText: request.statusText,\n headers: createHeadersFromXMLHttpRequestHeaders(\n request.getAllResponseHeaders()\n ),\n })\n}\n\nfunction createHeadersFromXMLHttpRequestHeaders(headersString: string): Headers {\n const headers = new Headers()\n\n const lines = headersString.split(/[\\r\\n]+/)\n for (const line of lines) {\n if (line.trim() === '') {\n continue\n }\n\n const [name, ...parts] = line.split(': ')\n const value = parts.join(': ')\n\n headers.append(name, value)\n }\n\n return headers\n}\n","/**\n * Return a total byte length of the given request/response body.\n * If the `Content-Length` header is present, it will be used as the byte length.\n */\nexport async function getBodyByteLength(\n input: Request | Response\n): Promise<number> {\n const explicitContentLength = input.headers.get('content-length')\n\n if (explicitContentLength != null && explicitContentLength !== '') {\n return Number(explicitContentLength)\n }\n\n const buffer = await input.arrayBuffer()\n return buffer.byteLength\n}\n","import type { Logger } from '@open-draft/logger'\nimport { XMLHttpRequestEmitter } from '.'\nimport { RequestController } from '../../RequestController'\nimport { XMLHttpRequestController } from './XMLHttpRequestController'\nimport { handleRequest } from '../../utils/handleRequest'\nimport { isResponseError } from '../../utils/responseUtils'\n\nexport interface XMLHttpRequestProxyOptions {\n emitter: XMLHttpRequestEmitter\n logger: Logger\n}\n\n/**\n * Create a proxied `XMLHttpRequest` class.\n * The proxied class establishes spies on certain methods,\n * allowing us to intercept requests and respond to them.\n */\nexport function createXMLHttpRequestProxy({\n emitter,\n logger,\n}: XMLHttpRequestProxyOptions) {\n const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {\n construct(target, args, newTarget) {\n logger.info('constructed new XMLHttpRequest')\n\n const originalRequest = Reflect.construct(\n target,\n args,\n newTarget\n ) as XMLHttpRequest\n\n /**\n * @note Forward prototype descriptors onto the proxied object.\n * XMLHttpRequest is implemented in JSDOM in a way that assigns\n * a bunch of descriptors, like \"set responseType()\" on the prototype.\n * With this propagation, we make sure that those descriptors trigger\n * when the user operates with the proxied request instance.\n */\n const prototypeDescriptors = Object.getOwnPropertyDescriptors(\n target.prototype\n )\n for (const propertyName in prototypeDescriptors) {\n Reflect.defineProperty(\n originalRequest,\n propertyName,\n prototypeDescriptors[propertyName]\n )\n }\n\n const xhrRequestController = new XMLHttpRequestController(\n originalRequest,\n logger\n )\n\n xhrRequestController.onRequest = async function ({ request, requestId }) {\n const controller = new RequestController(request, {\n passthrough: () => {\n this.logger.info(\n 'no mocked response received, performing request as-is...'\n )\n },\n respondWith: async (response) => {\n if (isResponseError(response)) {\n this.errorWith(new TypeError('Network error'))\n return\n }\n\n await this.respondWith(response)\n },\n errorWith: (reason) => {\n this.logger.info('request errored!', { error: reason })\n\n if (reason instanceof Error) {\n this.errorWith(reason)\n }\n },\n })\n\n this.logger.info('awaiting mocked response...')\n\n this.logger.info(\n 'emitting the \"request\" event for %s listener(s)...',\n emitter.listenerCount('request')\n )\n\n await handleRequest({\n request,\n requestId,\n controller,\n emitter,\n })\n }\n\n xhrRequestController.onResponse = async function ({\n response,\n isMockedResponse,\n request,\n requestId,\n }) {\n this.logger.info(\n 'emitting the \"response\" event for %s listener(s)...',\n emitter.listenerCount('response')\n )\n\n emitter.emit('response', {\n response,\n isMockedResponse,\n request,\n requestId,\n })\n }\n\n // Return the proxied request from the controller\n // so that the controller can react to the consumer's interactions\n // with this request (opening/sending/etc).\n return xhrRequestController.request\n },\n })\n\n return XMLHttpRequestProxy\n}\n"]}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkJQ2S7G56js = require('./chunk-JQ2S7G56.js');
|
|
5
|
+
|
|
6
|
+
// src/glossary.ts
|
|
7
|
+
var IS_PATCHED_MODULE = Symbol("isPatchedModule");
|
|
8
|
+
|
|
9
|
+
// src/RequestController.ts
|
|
10
|
+
var _deferredpromise = require('@open-draft/deferred-promise');
|
|
11
|
+
var _outvariant = require('outvariant');
|
|
12
|
+
|
|
13
|
+
// src/InterceptorError.ts
|
|
14
|
+
var InterceptorError = class extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "InterceptorError";
|
|
18
|
+
Object.setPrototypeOf(this, InterceptorError.prototype);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/RequestController.ts
|
|
23
|
+
var _handled, handled_get;
|
|
24
|
+
var _RequestController = class {
|
|
25
|
+
constructor(request, source) {
|
|
26
|
+
this.request = request;
|
|
27
|
+
this.source = source;
|
|
28
|
+
_chunkJQ2S7G56js.__privateAdd.call(void 0, this, _handled);
|
|
29
|
+
this.readyState = _RequestController.PENDING;
|
|
30
|
+
this.handled = new (0, _deferredpromise.DeferredPromise)();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Perform this request as-is.
|
|
34
|
+
*/
|
|
35
|
+
async passthrough() {
|
|
36
|
+
_outvariant.invariant.as(
|
|
37
|
+
InterceptorError,
|
|
38
|
+
this.readyState === _RequestController.PENDING,
|
|
39
|
+
'Failed to passthrough the "%s %s" request: the request has already been handled',
|
|
40
|
+
this.request.method,
|
|
41
|
+
this.request.url
|
|
42
|
+
);
|
|
43
|
+
this.readyState = _RequestController.PASSTHROUGH;
|
|
44
|
+
await this.source.passthrough();
|
|
45
|
+
_chunkJQ2S7G56js.__privateGet.call(void 0, this, _handled, handled_get).resolve();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Respond to this request with the given `Response` instance.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* controller.respondWith(new Response())
|
|
52
|
+
* controller.respondWith(Response.json({ id }))
|
|
53
|
+
* controller.respondWith(Response.error())
|
|
54
|
+
*/
|
|
55
|
+
respondWith(response) {
|
|
56
|
+
_outvariant.invariant.as(
|
|
57
|
+
InterceptorError,
|
|
58
|
+
this.readyState === _RequestController.PENDING,
|
|
59
|
+
'Failed to respond to the "%s %s" request with "%d %s": the request has already been handled (%d)',
|
|
60
|
+
this.request.method,
|
|
61
|
+
this.request.url,
|
|
62
|
+
response.status,
|
|
63
|
+
response.statusText || "OK",
|
|
64
|
+
this.readyState
|
|
65
|
+
);
|
|
66
|
+
this.readyState = _RequestController.RESPONSE;
|
|
67
|
+
_chunkJQ2S7G56js.__privateGet.call(void 0, this, _handled, handled_get).resolve();
|
|
68
|
+
this.source.respondWith(response);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Error this request with the given reason.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* controller.errorWith()
|
|
75
|
+
* controller.errorWith(new Error('Oops!'))
|
|
76
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
77
|
+
*/
|
|
78
|
+
errorWith(reason) {
|
|
79
|
+
_outvariant.invariant.as(
|
|
80
|
+
InterceptorError,
|
|
81
|
+
this.readyState === _RequestController.PENDING,
|
|
82
|
+
'Failed to error the "%s %s" request with "%s": the request has already been handled (%d)',
|
|
83
|
+
this.request.method,
|
|
84
|
+
this.request.url,
|
|
85
|
+
reason == null ? void 0 : reason.toString(),
|
|
86
|
+
this.readyState
|
|
87
|
+
);
|
|
88
|
+
this.readyState = _RequestController.ERROR;
|
|
89
|
+
this.source.errorWith(reason);
|
|
90
|
+
_chunkJQ2S7G56js.__privateGet.call(void 0, this, _handled, handled_get).resolve();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var RequestController = _RequestController;
|
|
94
|
+
_handled = new WeakSet();
|
|
95
|
+
handled_get = function() {
|
|
96
|
+
return this.handled;
|
|
97
|
+
};
|
|
98
|
+
RequestController.PENDING = 0;
|
|
99
|
+
RequestController.PASSTHROUGH = 1;
|
|
100
|
+
RequestController.RESPONSE = 2;
|
|
101
|
+
RequestController.ERROR = 3;
|
|
102
|
+
|
|
103
|
+
// src/utils/canParseUrl.ts
|
|
104
|
+
function canParseUrl(url) {
|
|
105
|
+
try {
|
|
106
|
+
new URL(url);
|
|
107
|
+
return true;
|
|
108
|
+
} catch (_error) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/utils/getValueBySymbol.ts
|
|
114
|
+
function getValueBySymbol(symbolName, source) {
|
|
115
|
+
const ownSymbols = Object.getOwnPropertySymbols(source);
|
|
116
|
+
const symbol = ownSymbols.find((symbol2) => {
|
|
117
|
+
return symbol2.description === symbolName;
|
|
118
|
+
});
|
|
119
|
+
if (symbol) {
|
|
120
|
+
return Reflect.get(source, symbol);
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/utils/fetchUtils.ts
|
|
126
|
+
var _FetchResponse = class extends Response {
|
|
127
|
+
static isConfigurableStatusCode(status) {
|
|
128
|
+
return status >= 200 && status <= 599;
|
|
129
|
+
}
|
|
130
|
+
static isRedirectResponse(status) {
|
|
131
|
+
return _FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns a boolean indicating whether the given response status
|
|
135
|
+
* code represents a response that can have a body.
|
|
136
|
+
*/
|
|
137
|
+
static isResponseWithBody(status) {
|
|
138
|
+
return !_FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status);
|
|
139
|
+
}
|
|
140
|
+
static setUrl(url, response) {
|
|
141
|
+
if (!url || url === "about:" || !canParseUrl(url)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const state = getValueBySymbol("state", response);
|
|
145
|
+
if (state) {
|
|
146
|
+
state.urlList.push(new URL(url));
|
|
147
|
+
} else {
|
|
148
|
+
Object.defineProperty(response, "url", {
|
|
149
|
+
value: url,
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true,
|
|
152
|
+
writable: false
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
|
|
158
|
+
*/
|
|
159
|
+
static parseRawHeaders(rawHeaders) {
|
|
160
|
+
const headers = new Headers();
|
|
161
|
+
for (let line = 0; line < rawHeaders.length; line += 2) {
|
|
162
|
+
headers.append(rawHeaders[line], rawHeaders[line + 1]);
|
|
163
|
+
}
|
|
164
|
+
return headers;
|
|
165
|
+
}
|
|
166
|
+
constructor(body, init = {}) {
|
|
167
|
+
var _a;
|
|
168
|
+
const status = (_a = init.status) != null ? _a : 200;
|
|
169
|
+
const safeStatus = _FetchResponse.isConfigurableStatusCode(status) ? status : 200;
|
|
170
|
+
const finalBody = _FetchResponse.isResponseWithBody(status) ? body : null;
|
|
171
|
+
super(finalBody, {
|
|
172
|
+
status: safeStatus,
|
|
173
|
+
statusText: init.statusText,
|
|
174
|
+
headers: init.headers
|
|
175
|
+
});
|
|
176
|
+
if (status !== safeStatus) {
|
|
177
|
+
const state = getValueBySymbol("state", this);
|
|
178
|
+
if (state) {
|
|
179
|
+
state.status = status;
|
|
180
|
+
} else {
|
|
181
|
+
Object.defineProperty(this, "status", {
|
|
182
|
+
value: status,
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: false
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
_FetchResponse.setUrl(init.url, this);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var FetchResponse = _FetchResponse;
|
|
193
|
+
/**
|
|
194
|
+
* Response status codes for responses that cannot have body.
|
|
195
|
+
* @see https://fetch.spec.whatwg.org/#statuses
|
|
196
|
+
*/
|
|
197
|
+
FetchResponse.STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304];
|
|
198
|
+
FetchResponse.STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308];
|
|
199
|
+
|
|
200
|
+
// src/getRawRequest.ts
|
|
201
|
+
var kRawRequest = Symbol("kRawRequest");
|
|
202
|
+
function getRawRequest(request) {
|
|
203
|
+
return Reflect.get(request, kRawRequest);
|
|
204
|
+
}
|
|
205
|
+
function setRawRequest(request, rawRequest) {
|
|
206
|
+
Reflect.set(request, kRawRequest, rawRequest);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
exports.IS_PATCHED_MODULE = IS_PATCHED_MODULE; exports.InterceptorError = InterceptorError; exports.RequestController = RequestController; exports.canParseUrl = canParseUrl; exports.FetchResponse = FetchResponse; exports.getRawRequest = getRawRequest; exports.setRawRequest = setRawRequest;
|
|
218
|
+
//# sourceMappingURL=chunk-57RIRQUY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/glossary.ts","../../src/RequestController.ts","../../src/InterceptorError.ts","../../src/utils/canParseUrl.ts","../../src/utils/getValueBySymbol.ts","../../src/utils/fetchUtils.ts","../../src/getRawRequest.ts"],"names":["symbol"],"mappings":";;;;;;AAEO,IAAM,oBAAmC,OAAO,iBAAiB;;;ACFxE,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;;;ACDnB,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAY,SAAkB;AAC5B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,iBAAiB,SAAS;AAAA,EACxD;AACF;;;ADNA;AAUO,IAAM,qBAAN,MAAwB;AAAA,EAc7B,YACqB,SACA,QACnB;AAFmB;AACA;AAMrB,uBAAI;AAJF,SAAK,aAAa,mBAAkB;AACpC,SAAK,UAAU,IAAI,gBAAsB;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EASA,MAAa,cAA6B;AACxC,cAAU;AAAA,MACR;AAAA,MACA,KAAK,eAAe,mBAAkB;AAAA,MACtC;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,aAAa,mBAAkB;AACpC,UAAM,KAAK,OAAO,YAAY;AAC9B,uBAAK,uBAAS,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUO,YAAY,UAA0B;AAC3C,cAAU;AAAA,MACR;AAAA,MACA,KAAK,eAAe,mBAAkB;AAAA,MACtC;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,MACb,SAAS;AAAA,MACT,SAAS,cAAc;AAAA,MACvB,KAAK;AAAA,IACP;AAEA,SAAK,aAAa,mBAAkB;AACpC,uBAAK,uBAAS,QAAQ;AAQtB,SAAK,OAAO,YAAY,QAAQ;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUO,UAAU,QAAwB;AACvC,cAAU;AAAA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AAc,OAAO,aAAa;AAcjC,SAAS,cAAc,SAAuC;AACnE,SAAO,QAAQ,IAAI,SAAS,WAAW;AACzC;AAEO,SAAS,cAAc,SAAkB,YAA2B;AACzE,UAAQ,IAAI,SAAS,aAAa,UAAU;AAC9C","sourcesContent":["import type { RequestController } from './RequestController'\n\nexport const IS_PATCHED_MODULE: unique symbol = Symbol('isPatchedModule')\n\n/**\n * @note Export `RequestController` as a type only.\n * It's never meant to be created in the userland.\n */\nexport type { RequestController }\n\nexport type RequestCredentials = 'omit' | 'include' | 'same-origin'\n\nexport type HttpRequestEventMap = {\n request: [\n args: {\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n response: [\n args: {\n response: Response\n isMockedResponse: boolean\n request: Request\n requestId: string\n }\n ]\n unhandledException: [\n args: {\n error: unknown\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n}\n","import { DeferredPromise } from '@open-draft/deferred-promise'\nimport { invariant } from 'outvariant'\nimport { InterceptorError } from './InterceptorError'\n\nexport interface RequestControllerSource {\n passthrough(): void\n respondWith(response: Response): void\n errorWith(reason?: unknown): void\n}\n\nexport class RequestController {\n static PENDING = 0 as const\n static PASSTHROUGH = 1 as const\n static RESPONSE = 2 as const\n static ERROR = 3 as const\n\n public readyState: number\n\n /**\n * A Promise that resolves when this controller handles a request.\n * See `controller.readyState` for more information on the handling result.\n */\n public handled: Promise<void>\n\n constructor(\n protected readonly request: Request,\n protected readonly source: RequestControllerSource\n ) {\n this.readyState = RequestController.PENDING\n this.handled = new DeferredPromise<void>()\n }\n\n get #handled() {\n return this.handled as DeferredPromise<void>\n }\n\n /**\n * Perform this request as-is.\n */\n public async passthrough(): Promise<void> {\n invariant.as(\n InterceptorError,\n this.readyState === RequestController.PENDING,\n 'Failed to passthrough the \"%s %s\" request: the request has already been handled',\n this.request.method,\n this.request.url\n )\n\n this.readyState = RequestController.PASSTHROUGH\n await this.source.passthrough()\n this.#handled.resolve()\n }\n\n /**\n * Respond to this request with the given `Response` instance.\n *\n * @example\n * controller.respondWith(new Response())\n * controller.respondWith(Response.json({ id }))\n * controller.respondWith(Response.error())\n */\n public respondWith(response: Response): void {\n invariant.as(\n InterceptorError,\n this.readyState === RequestController.PENDING,\n 'Failed to respond to the \"%s %s\" request with \"%d %s\": the request has already been handled (%d)',\n this.request.method,\n this.request.url,\n response.status,\n response.statusText || 'OK',\n this.readyState\n )\n\n this.readyState = RequestController.RESPONSE\n this.#handled.resolve()\n\n /**\n * @note Although `source.respondWith()` is potentially asynchronous,\n * do NOT await it for backward-compatibility. Awaiting it will short-circuit\n * the request listener invocation as soon as a listener responds to a request.\n * Ideally, that's what we want, but that's not what we promise the user.\n */\n this.source.respondWith(response)\n }\n\n /**\n * Error this request with the given reason.\n *\n * @example\n * controller.errorWith()\n * controller.errorWith(new Error('Oops!'))\n * controller.errorWith({ message: 'Oops!'})\n */\n public errorWith(reason?: unknown): void {\n invariant.as(\n InterceptorError,\n this.readyState === RequestController.PENDING,\n 'Failed to error the \"%s %s\" request with \"%s\": the request has already been handled (%d)',\n this.request.method,\n this.request.url,\n reason?.toString(),\n this.readyState\n )\n\n this.readyState = RequestController.ERROR\n this.source.errorWith(reason)\n this.#handled.resolve()\n }\n}\n","export class InterceptorError extends Error {\n constructor(message?: string) {\n super(message)\n this.name = 'InterceptorError'\n Object.setPrototypeOf(this, InterceptorError.prototype)\n }\n}\n","/**\n * Returns a boolean indicating whether the given URL string\n * can be parsed into a `URL` instance.\n * A substitute for `URL.canParse()` for Node.js 18.\n */\nexport function canParseUrl(url: string): boolean {\n try {\n new URL(url)\n return true\n } catch (_error) {\n return false\n }\n}\n","/**\n * Returns the value behind the symbol with the given name.\n */\nexport function getValueBySymbol<T>(\n symbolName: string,\n source: object\n): T | undefined {\n const ownSymbols = Object.getOwnPropertySymbols(source)\n\n const symbol = ownSymbols.find((symbol) => {\n return symbol.description === symbolName\n })\n\n if (symbol) {\n return Reflect.get(source, symbol)\n }\n\n return\n}\n","import { canParseUrl } from './canParseUrl'\nimport { getValueBySymbol } from './getValueBySymbol'\n\nexport interface FetchResponseInit extends ResponseInit {\n url?: string\n}\n\ninterface UndiciFetchInternalState {\n aborted: boolean\n rangeRequested: boolean\n timingAllowPassed: boolean\n requestIncludesCredentials: boolean\n type: ResponseType\n status: number\n statusText: string\n timingInfo: unknown\n cacheState: unknown\n headersList: Record<symbol, Map<string, unknown>>\n urlList: Array<URL>\n body?: {\n stream: ReadableStream\n source: unknown\n length: number\n }\n}\n\nexport class FetchResponse extends Response {\n /**\n * Response status codes for responses that cannot have body.\n * @see https://fetch.spec.whatwg.org/#statuses\n */\n static readonly STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304]\n\n static readonly STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308]\n\n static isConfigurableStatusCode(status: number): boolean {\n return status >= 200 && status <= 599\n }\n\n static isRedirectResponse(status: number): boolean {\n return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status)\n }\n\n /**\n * Returns a boolean indicating whether the given response status\n * code represents a response that can have a body.\n */\n static isResponseWithBody(status: number): boolean {\n return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)\n }\n\n static setUrl(url: string | undefined, response: Response): void {\n if (!url || url === 'about:' || !canParseUrl(url)) {\n return\n }\n\n const state = getValueBySymbol<UndiciFetchInternalState>('state', response)\n\n if (state) {\n // In Undici, push the URL to the internal list of URLs.\n // This will respect the `response.url` getter logic correctly.\n state.urlList.push(new URL(url))\n } else {\n // In other libraries, redefine the `url` property directly.\n Object.defineProperty(response, 'url', {\n value: url,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n }\n\n /**\n * Parses the given raw HTTP headers into a Fetch API `Headers` instance.\n */\n static parseRawHeaders(rawHeaders: Array<string>): Headers {\n const headers = new Headers()\n for (let line = 0; line < rawHeaders.length; line += 2) {\n headers.append(rawHeaders[line], rawHeaders[line + 1])\n }\n return headers\n }\n\n constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {\n const status = init.status ?? 200\n const safeStatus = FetchResponse.isConfigurableStatusCode(status)\n ? status\n : 200\n const finalBody = FetchResponse.isResponseWithBody(status) ? body : null\n\n super(finalBody, {\n status: safeStatus,\n statusText: init.statusText,\n headers: init.headers,\n })\n\n if (status !== safeStatus) {\n /**\n * @note Undici keeps an internal \"Symbol(state)\" that holds\n * the actual value of response status. Update that in Node.js.\n */\n const state = getValueBySymbol<UndiciFetchInternalState>('state', this)\n\n if (state) {\n state.status = status\n } else {\n Object.defineProperty(this, 'status', {\n value: status,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n }\n\n FetchResponse.setUrl(init.url, this)\n }\n}\n","const kRawRequest = Symbol('kRawRequest')\n\n/**\n * Returns a raw request instance associated with this request.\n *\n * @example\n * interceptor.on('request', ({ request }) => {\n * const rawRequest = getRawRequest(request)\n *\n * if (rawRequest instanceof http.ClientRequest) {\n * console.log(rawRequest.rawHeaders)\n * }\n * })\n */\nexport function getRawRequest(request: Request): unknown | undefined {\n return Reflect.get(request, kRawRequest)\n}\n\nexport function setRawRequest(request: Request, rawRequest: unknown): void {\n Reflect.set(request, kRawRequest, rawRequest)\n}\n"]}
|