@mswjs/interceptors 0.39.8 → 0.41.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/ClientRequest/package.json +7 -2
- package/RemoteHttpInterceptor/package.json +7 -2
- package/WebSocket/package.json +9 -2
- package/XMLHttpRequest/package.json +9 -3
- package/fetch/package.json +9 -3
- package/lib/browser/Interceptor-2mUoKZL1.d.mts +65 -0
- package/lib/browser/Interceptor-Deczogc8.d.cts +65 -0
- package/lib/browser/XMLHttpRequest-BUfglQD1.cjs +761 -0
- package/lib/browser/XMLHttpRequest-BUfglQD1.cjs.map +1 -0
- package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs +756 -0
- package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs.map +1 -0
- package/lib/browser/bufferUtils-BiiO6HZv.mjs +20 -0
- package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +1 -0
- package/lib/browser/bufferUtils-Uc0eRItL.cjs +38 -0
- package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +1 -0
- package/lib/browser/createRequestId-Cs4oXfa1.cjs +205 -0
- package/lib/browser/createRequestId-Cs4oXfa1.cjs.map +1 -0
- package/lib/browser/createRequestId-DQcIlohW.mjs +170 -0
- package/lib/browser/createRequestId-DQcIlohW.mjs.map +1 -0
- package/lib/browser/fetch-BHcqM3z7.cjs +253 -0
- package/lib/browser/fetch-BHcqM3z7.cjs.map +1 -0
- package/lib/browser/fetch-DSJoynSF.mjs +248 -0
- package/lib/browser/fetch-DSJoynSF.mjs.map +1 -0
- package/lib/browser/getRawRequest-BTaNLFr0.mjs +218 -0
- package/lib/browser/getRawRequest-BTaNLFr0.mjs.map +1 -0
- package/lib/browser/getRawRequest-zx8rUJL2.cjs +259 -0
- package/lib/browser/getRawRequest-zx8rUJL2.cjs.map +1 -0
- package/lib/browser/glossary-BdLS4k1H.d.cts +70 -0
- package/lib/browser/glossary-gEEJhK4S.d.mts +70 -0
- package/lib/browser/handleRequest-DI6a7Dty.cjs +189 -0
- package/lib/browser/handleRequest-DI6a7Dty.cjs.map +1 -0
- package/lib/browser/handleRequest-DxGbCTbb.mjs +178 -0
- package/lib/browser/handleRequest-DxGbCTbb.mjs.map +1 -0
- package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs +33 -0
- package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs.map +1 -0
- package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs +45 -0
- package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs.map +1 -0
- package/lib/browser/index.cjs +68 -0
- package/lib/browser/index.cjs.map +1 -0
- package/lib/browser/index.d.cts +87 -0
- package/lib/browser/index.d.mts +87 -0
- package/lib/browser/index.mjs +49 -75
- package/lib/browser/index.mjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +621 -0
- package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -0
- package/lib/browser/interceptors/WebSocket/index.d.cts +277 -0
- package/lib/browser/interceptors/WebSocket/index.d.mts +277 -0
- package/lib/browser/interceptors/WebSocket/index.mjs +587 -694
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +7 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +15 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +15 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +7 -12
- package/lib/browser/interceptors/fetch/index.cjs +6 -0
- package/lib/browser/interceptors/fetch/index.d.cts +13 -0
- package/lib/browser/interceptors/fetch/index.d.mts +13 -0
- package/lib/browser/interceptors/fetch/index.mjs +6 -11
- package/lib/browser/presets/browser.cjs +17 -0
- package/lib/browser/presets/browser.cjs.map +1 -0
- package/lib/browser/presets/browser.d.cts +12 -0
- package/lib/browser/presets/browser.d.mts +14 -0
- package/lib/browser/presets/browser.mjs +15 -19
- package/lib/browser/presets/browser.mjs.map +1 -1
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs +49 -0
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs.map +1 -0
- package/lib/node/BatchInterceptor-D7mXzHcQ.d.mts +26 -0
- package/lib/node/BatchInterceptor-DFaBPilf.mjs +44 -0
- package/lib/node/BatchInterceptor-DFaBPilf.mjs.map +1 -0
- package/lib/node/BatchInterceptor-D_YqR8qU.d.cts +26 -0
- package/lib/node/ClientRequest-2rDe54Ui.cjs +1043 -0
- package/lib/node/ClientRequest-2rDe54Ui.cjs.map +1 -0
- package/lib/node/ClientRequest-Ca8Qykuv.mjs +1034 -0
- package/lib/node/ClientRequest-Ca8Qykuv.mjs.map +1 -0
- package/lib/node/Interceptor-DEazpLJd.d.mts +133 -0
- package/lib/node/Interceptor-DJ2akVWI.d.cts +133 -0
- package/lib/node/RemoteHttpInterceptor.cjs +154 -0
- package/lib/node/RemoteHttpInterceptor.cjs.map +1 -0
- package/lib/node/RemoteHttpInterceptor.d.cts +39 -0
- package/lib/node/RemoteHttpInterceptor.d.mts +39 -0
- package/lib/node/RemoteHttpInterceptor.mjs +145 -186
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs +763 -0
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs.map +1 -0
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs +757 -0
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs.map +1 -0
- package/lib/node/bufferUtils-DiCTqG-7.cjs +38 -0
- package/lib/node/bufferUtils-DiCTqG-7.cjs.map +1 -0
- package/lib/node/bufferUtils-_8XfKIfX.mjs +20 -0
- package/lib/node/bufferUtils-_8XfKIfX.mjs.map +1 -0
- package/lib/node/chunk-CbDLau6x.cjs +34 -0
- package/lib/node/fetch-BmXpK10r.cjs +272 -0
- package/lib/node/fetch-BmXpK10r.cjs.map +1 -0
- package/lib/node/fetch-G1DVwDKG.mjs +265 -0
- package/lib/node/fetch-G1DVwDKG.mjs.map +1 -0
- package/lib/node/fetchUtils-BaY5iWXw.cjs +419 -0
- package/lib/node/fetchUtils-BaY5iWXw.cjs.map +1 -0
- package/lib/node/fetchUtils-CoU35g3M.mjs +359 -0
- package/lib/node/fetchUtils-CoU35g3M.mjs.map +1 -0
- package/lib/node/getRawRequest-BavnMWh_.cjs +36 -0
- package/lib/node/getRawRequest-BavnMWh_.cjs.map +1 -0
- package/lib/node/getRawRequest-DnwmXyOW.mjs +24 -0
- package/lib/node/getRawRequest-DnwmXyOW.mjs.map +1 -0
- package/lib/node/glossary-BLKRyLBd.cjs +12 -0
- package/lib/node/glossary-BLKRyLBd.cjs.map +1 -0
- package/lib/node/glossary-glQBRnVD.mjs +6 -0
- package/lib/node/glossary-glQBRnVD.mjs.map +1 -0
- package/lib/node/handleRequest-Bb7Y-XLw.cjs +220 -0
- package/lib/node/handleRequest-Bb7Y-XLw.cjs.map +1 -0
- package/lib/node/handleRequest-Y97UwBbF.mjs +190 -0
- package/lib/node/handleRequest-Y97UwBbF.mjs.map +1 -0
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs +26 -0
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs.map +1 -0
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs +20 -0
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs.map +1 -0
- package/lib/node/index-BMbJ8FXL.d.cts +113 -0
- package/lib/node/index-C0YAQ36w.d.mts +113 -0
- package/lib/node/index.cjs +30 -0
- package/lib/node/index.cjs.map +1 -0
- package/lib/node/index.d.cts +66 -0
- package/lib/node/index.d.mts +66 -0
- package/lib/node/index.mjs +13 -39
- package/lib/node/index.mjs.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +6 -0
- package/lib/node/interceptors/ClientRequest/index.d.cts +2 -0
- package/lib/node/interceptors/ClientRequest/index.d.mts +3 -0
- package/lib/node/interceptors/ClientRequest/index.mjs +6 -11
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +6 -0
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +14 -0
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +14 -0
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +6 -13
- package/lib/node/interceptors/fetch/index.cjs +5 -0
- package/lib/node/interceptors/fetch/index.d.cts +12 -0
- package/lib/node/interceptors/fetch/index.d.mts +12 -0
- package/lib/node/interceptors/fetch/index.mjs +5 -12
- package/lib/node/node-DwCc6iuP.mjs +27 -0
- package/lib/node/node-DwCc6iuP.mjs.map +1 -0
- package/lib/node/node-dKdAf3tC.cjs +39 -0
- package/lib/node/node-dKdAf3tC.cjs.map +1 -0
- package/lib/node/presets/node.cjs +22 -0
- package/lib/node/presets/node.cjs.map +1 -0
- package/lib/node/presets/node.d.cts +13 -0
- package/lib/node/presets/node.d.mts +15 -0
- package/lib/node/presets/node.mjs +18 -23
- package/lib/node/presets/node.mjs.map +1 -1
- package/lib/node/utils/node/index.cjs +4 -0
- package/lib/node/utils/node/{index.d.ts → index.d.cts} +5 -2
- package/lib/node/utils/node/index.d.mts +16 -0
- package/lib/node/utils/node/index.mjs +3 -10
- package/package.json +34 -59
- package/presets/browser/package.json +2 -3
- package/presets/node/package.json +7 -2
- 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 +43 -9
- package/src/interceptors/ClientRequest/index.ts +14 -18
- package/src/interceptors/ClientRequest/utils/parserUtils.ts +48 -0
- package/src/interceptors/WebSocket/index.ts +4 -1
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +45 -35
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +24 -21
- package/src/interceptors/XMLHttpRequest/utils/getBodyByteLength.test.ts +2 -2
- package/src/interceptors/fetch/index.ts +61 -50
- package/src/utils/handleRequest.ts +65 -95
- package/lib/browser/Interceptor-af98b768.d.ts +0 -63
- package/lib/browser/chunk-2HUMWGRD.js +0 -37
- package/lib/browser/chunk-2HUMWGRD.js.map +0 -1
- package/lib/browser/chunk-2QICSCCS.js +0 -238
- package/lib/browser/chunk-2QICSCCS.js.map +0 -1
- package/lib/browser/chunk-3RXCRGL2.mjs +0 -117
- package/lib/browser/chunk-3RXCRGL2.mjs.map +0 -1
- package/lib/browser/chunk-6HYIRFX2.mjs +0 -22
- package/lib/browser/chunk-6HYIRFX2.mjs.map +0 -1
- package/lib/browser/chunk-E3CCOBRX.js +0 -846
- package/lib/browser/chunk-E3CCOBRX.js.map +0 -1
- package/lib/browser/chunk-E7UVBHVO.mjs +0 -846
- package/lib/browser/chunk-E7UVBHVO.mjs.map +0 -1
- package/lib/browser/chunk-H74PGQ4Y.js +0 -296
- package/lib/browser/chunk-H74PGQ4Y.js.map +0 -1
- package/lib/browser/chunk-LK6DILFK.js +0 -22
- package/lib/browser/chunk-LK6DILFK.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 +0 -296
- package/lib/browser/chunk-Q7K2XAEP.mjs.map +0 -1
- package/lib/browser/chunk-QED3Q6Z2.mjs +0 -169
- package/lib/browser/chunk-QED3Q6Z2.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 +0 -169
- package/lib/browser/chunk-TIPR373R.js.map +0 -1
- package/lib/browser/chunk-VYSDLBSS.mjs +0 -37
- package/lib/browser/chunk-VYSDLBSS.mjs.map +0 -1
- package/lib/browser/glossary-7152281e.d.ts +0 -69
- package/lib/browser/index.d.ts +0 -83
- package/lib/browser/index.js +0 -81
- package/lib/browser/index.js.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.d.ts +0 -271
- package/lib/browser/interceptors/WebSocket/index.js +0 -721
- package/lib/browser/interceptors/WebSocket/index.js.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.js +0 -12
- package/lib/browser/interceptors/XMLHttpRequest/index.js.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs.map +0 -1
- package/lib/browser/interceptors/fetch/index.d.ts +0 -14
- package/lib/browser/interceptors/fetch/index.js +0 -11
- package/lib/browser/interceptors/fetch/index.js.map +0 -1
- package/lib/browser/interceptors/fetch/index.mjs.map +0 -1
- package/lib/browser/presets/browser.d.ts +0 -15
- package/lib/browser/presets/browser.js +0 -21
- package/lib/browser/presets/browser.js.map +0 -1
- package/lib/node/BatchInterceptor-5b72232f.d.ts +0 -24
- package/lib/node/Interceptor-bc5a9d8e.d.ts +0 -130
- package/lib/node/RemoteHttpInterceptor.d.ts +0 -45
- package/lib/node/RemoteHttpInterceptor.js +0 -193
- package/lib/node/RemoteHttpInterceptor.js.map +0 -1
- package/lib/node/chunk-3CNGDJFB.mjs +0 -313
- package/lib/node/chunk-3CNGDJFB.mjs.map +0 -1
- package/lib/node/chunk-3GJB4JDF.mjs +0 -14
- package/lib/node/chunk-3GJB4JDF.mjs.map +0 -1
- package/lib/node/chunk-4NEYTVWD.mjs +0 -848
- package/lib/node/chunk-4NEYTVWD.mjs.map +0 -1
- package/lib/node/chunk-4YBV77DG.js +0 -32
- package/lib/node/chunk-4YBV77DG.js.map +0 -1
- package/lib/node/chunk-6HYIRFX2.mjs +0 -22
- package/lib/node/chunk-6HYIRFX2.mjs.map +0 -1
- package/lib/node/chunk-6YM4PLBI.mjs +0 -7
- package/lib/node/chunk-6YM4PLBI.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-73NOP3T5.js +0 -7
- package/lib/node/chunk-73NOP3T5.js.map +0 -1
- package/lib/node/chunk-A7Q4RTDJ.mjs +0 -249
- package/lib/node/chunk-A7Q4RTDJ.mjs.map +0 -1
- package/lib/node/chunk-A7U44ARP.js +0 -268
- package/lib/node/chunk-A7U44ARP.js.map +0 -1
- package/lib/node/chunk-EKNRB5ZS.mjs +0 -1115
- package/lib/node/chunk-EKNRB5ZS.mjs.map +0 -1
- package/lib/node/chunk-IHJSPMYM.mjs +0 -268
- package/lib/node/chunk-IHJSPMYM.mjs.map +0 -1
- package/lib/node/chunk-LK6DILFK.js +0 -22
- package/lib/node/chunk-LK6DILFK.js.map +0 -1
- package/lib/node/chunk-N4ZZFE24.js +0 -1115
- package/lib/node/chunk-N4ZZFE24.js.map +0 -1
- package/lib/node/chunk-PFGO5BSM.js +0 -25
- package/lib/node/chunk-PFGO5BSM.js.map +0 -1
- package/lib/node/chunk-R6JVCM7X.js +0 -51
- package/lib/node/chunk-R6JVCM7X.js.map +0 -1
- package/lib/node/chunk-RC2XPCC4.mjs +0 -51
- package/lib/node/chunk-RC2XPCC4.mjs.map +0 -1
- package/lib/node/chunk-SMXZPJEA.js +0 -14
- package/lib/node/chunk-SMXZPJEA.js.map +0 -1
- package/lib/node/chunk-TJDMZZXE.mjs +0 -32
- package/lib/node/chunk-TJDMZZXE.mjs.map +0 -1
- package/lib/node/chunk-TX5GBTFY.mjs +0 -25
- package/lib/node/chunk-TX5GBTFY.mjs.map +0 -1
- package/lib/node/chunk-VV2LUF5K.js +0 -848
- package/lib/node/chunk-VV2LUF5K.js.map +0 -1
- package/lib/node/chunk-Z5LWCBZS.js +0 -313
- package/lib/node/chunk-Z5LWCBZS.js.map +0 -1
- package/lib/node/index.d.ts +0 -62
- package/lib/node/index.js +0 -43
- package/lib/node/index.js.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.d.ts +0 -111
- package/lib/node/interceptors/ClientRequest/index.js +0 -11
- package/lib/node/interceptors/ClientRequest/index.js.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.mjs.map +0 -1
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.js +0 -13
- package/lib/node/interceptors/XMLHttpRequest/index.js.map +0 -1
- package/lib/node/interceptors/XMLHttpRequest/index.mjs.map +0 -1
- package/lib/node/interceptors/fetch/index.d.ts +0 -13
- package/lib/node/interceptors/fetch/index.js +0 -12
- package/lib/node/interceptors/fetch/index.js.map +0 -1
- package/lib/node/interceptors/fetch/index.mjs.map +0 -1
- package/lib/node/presets/node.d.ts +0 -16
- package/lib/node/presets/node.js +0 -27
- package/lib/node/presets/node.js.map +0 -1
- package/lib/node/utils/node/index.js +0 -10
- package/lib/node/utils/node/index.js.map +0 -1
- package/lib/node/utils/node/index.mjs.map +0 -1
- package/src/utils/RequestController.ts +0 -21
|
@@ -1,1115 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
setRawRequestBodyStream
|
|
3
|
-
} from "./chunk-TJDMZZXE.mjs";
|
|
4
|
-
import {
|
|
5
|
-
RequestController,
|
|
6
|
-
createServerErrorResponse,
|
|
7
|
-
emitAsync,
|
|
8
|
-
handleRequest,
|
|
9
|
-
isObject,
|
|
10
|
-
isPropertyAccessible
|
|
11
|
-
} from "./chunk-A7Q4RTDJ.mjs";
|
|
12
|
-
import {
|
|
13
|
-
FetchResponse,
|
|
14
|
-
INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
15
|
-
Interceptor,
|
|
16
|
-
createRequestId
|
|
17
|
-
} from "./chunk-IHJSPMYM.mjs";
|
|
18
|
-
import {
|
|
19
|
-
setRawRequest
|
|
20
|
-
} from "./chunk-3GJB4JDF.mjs";
|
|
21
|
-
|
|
22
|
-
// src/interceptors/ClientRequest/index.ts
|
|
23
|
-
import http2 from "http";
|
|
24
|
-
import https2 from "https";
|
|
25
|
-
|
|
26
|
-
// src/interceptors/ClientRequest/MockHttpSocket.ts
|
|
27
|
-
import net2 from "net";
|
|
28
|
-
import {
|
|
29
|
-
HTTPParser
|
|
30
|
-
} from "_http_common";
|
|
31
|
-
import { STATUS_CODES, IncomingMessage, ServerResponse } from "http";
|
|
32
|
-
import { Readable } from "stream";
|
|
33
|
-
import { invariant } from "outvariant";
|
|
34
|
-
|
|
35
|
-
// src/interceptors/Socket/MockSocket.ts
|
|
36
|
-
import net from "net";
|
|
37
|
-
|
|
38
|
-
// src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts
|
|
39
|
-
function normalizeSocketWriteArgs(args) {
|
|
40
|
-
const normalized = [args[0], void 0, void 0];
|
|
41
|
-
if (typeof args[1] === "string") {
|
|
42
|
-
normalized[1] = args[1];
|
|
43
|
-
} else if (typeof args[1] === "function") {
|
|
44
|
-
normalized[2] = args[1];
|
|
45
|
-
}
|
|
46
|
-
if (typeof args[2] === "function") {
|
|
47
|
-
normalized[2] = args[2];
|
|
48
|
-
}
|
|
49
|
-
return normalized;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/interceptors/Socket/MockSocket.ts
|
|
53
|
-
var MockSocket = class extends net.Socket {
|
|
54
|
-
constructor(options) {
|
|
55
|
-
super();
|
|
56
|
-
this.options = options;
|
|
57
|
-
this.connecting = false;
|
|
58
|
-
this.connect();
|
|
59
|
-
this._final = (callback) => {
|
|
60
|
-
callback(null);
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
connect() {
|
|
64
|
-
this.connecting = true;
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
write(...args) {
|
|
68
|
-
const [chunk, encoding, callback] = normalizeSocketWriteArgs(
|
|
69
|
-
args
|
|
70
|
-
);
|
|
71
|
-
this.options.write(chunk, encoding, callback);
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
end(...args) {
|
|
75
|
-
const [chunk, encoding, callback] = normalizeSocketWriteArgs(
|
|
76
|
-
args
|
|
77
|
-
);
|
|
78
|
-
this.options.write(chunk, encoding, callback);
|
|
79
|
-
return super.end.apply(this, args);
|
|
80
|
-
}
|
|
81
|
-
push(chunk, encoding) {
|
|
82
|
-
this.options.read(chunk, encoding);
|
|
83
|
-
return super.push(chunk, encoding);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts
|
|
88
|
-
function baseUrlFromConnectionOptions(options) {
|
|
89
|
-
if ("href" in options) {
|
|
90
|
-
return new URL(options.href);
|
|
91
|
-
}
|
|
92
|
-
const protocol = options.port === 443 ? "https:" : "http:";
|
|
93
|
-
const host = options.host;
|
|
94
|
-
const url = new URL(`${protocol}//${host}`);
|
|
95
|
-
if (options.port) {
|
|
96
|
-
url.port = options.port.toString();
|
|
97
|
-
}
|
|
98
|
-
if (options.path) {
|
|
99
|
-
url.pathname = options.path;
|
|
100
|
-
}
|
|
101
|
-
if (options.auth) {
|
|
102
|
-
const [username, password] = options.auth.split(":");
|
|
103
|
-
url.username = username;
|
|
104
|
-
url.password = password;
|
|
105
|
-
}
|
|
106
|
-
return url;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/interceptors/ClientRequest/utils/recordRawHeaders.ts
|
|
110
|
-
var kRawHeaders = Symbol("kRawHeaders");
|
|
111
|
-
var kRestorePatches = Symbol("kRestorePatches");
|
|
112
|
-
function recordRawHeader(headers, args, behavior) {
|
|
113
|
-
ensureRawHeadersSymbol(headers, []);
|
|
114
|
-
const rawHeaders = Reflect.get(headers, kRawHeaders);
|
|
115
|
-
if (behavior === "set") {
|
|
116
|
-
for (let index = rawHeaders.length - 1; index >= 0; index--) {
|
|
117
|
-
if (rawHeaders[index][0].toLowerCase() === args[0].toLowerCase()) {
|
|
118
|
-
rawHeaders.splice(index, 1);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
rawHeaders.push(args);
|
|
123
|
-
}
|
|
124
|
-
function ensureRawHeadersSymbol(headers, rawHeaders) {
|
|
125
|
-
if (Reflect.has(headers, kRawHeaders)) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
defineRawHeadersSymbol(headers, rawHeaders);
|
|
129
|
-
}
|
|
130
|
-
function defineRawHeadersSymbol(headers, rawHeaders) {
|
|
131
|
-
Object.defineProperty(headers, kRawHeaders, {
|
|
132
|
-
value: rawHeaders,
|
|
133
|
-
enumerable: false,
|
|
134
|
-
// Mark the symbol as configurable so its value can be overridden.
|
|
135
|
-
// Overrides happen when merging raw headers from multiple sources.
|
|
136
|
-
// E.g. new Request(new Request(url, { headers }), { headers })
|
|
137
|
-
configurable: true
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
function recordRawFetchHeaders() {
|
|
141
|
-
if (Reflect.get(Headers, kRestorePatches)) {
|
|
142
|
-
return Reflect.get(Headers, kRestorePatches);
|
|
143
|
-
}
|
|
144
|
-
const {
|
|
145
|
-
Headers: OriginalHeaders,
|
|
146
|
-
Request: OriginalRequest,
|
|
147
|
-
Response: OriginalResponse
|
|
148
|
-
} = globalThis;
|
|
149
|
-
const { set, append, delete: headersDeleteMethod } = Headers.prototype;
|
|
150
|
-
Object.defineProperty(Headers, kRestorePatches, {
|
|
151
|
-
value: () => {
|
|
152
|
-
Headers.prototype.set = set;
|
|
153
|
-
Headers.prototype.append = append;
|
|
154
|
-
Headers.prototype.delete = headersDeleteMethod;
|
|
155
|
-
globalThis.Headers = OriginalHeaders;
|
|
156
|
-
globalThis.Request = OriginalRequest;
|
|
157
|
-
globalThis.Response = OriginalResponse;
|
|
158
|
-
Reflect.deleteProperty(Headers, kRestorePatches);
|
|
159
|
-
},
|
|
160
|
-
enumerable: false,
|
|
161
|
-
/**
|
|
162
|
-
* @note Mark this property as configurable
|
|
163
|
-
* so we can delete it using `Reflect.delete` during cleanup.
|
|
164
|
-
*/
|
|
165
|
-
configurable: true
|
|
166
|
-
});
|
|
167
|
-
Object.defineProperty(globalThis, "Headers", {
|
|
168
|
-
enumerable: true,
|
|
169
|
-
writable: true,
|
|
170
|
-
value: new Proxy(Headers, {
|
|
171
|
-
construct(target, args, newTarget) {
|
|
172
|
-
const headersInit = args[0] || [];
|
|
173
|
-
if (headersInit instanceof Headers && Reflect.has(headersInit, kRawHeaders)) {
|
|
174
|
-
const headers2 = Reflect.construct(
|
|
175
|
-
target,
|
|
176
|
-
[Reflect.get(headersInit, kRawHeaders)],
|
|
177
|
-
newTarget
|
|
178
|
-
);
|
|
179
|
-
ensureRawHeadersSymbol(headers2, [
|
|
180
|
-
/**
|
|
181
|
-
* @note Spread the retrieved headers to clone them.
|
|
182
|
-
* This prevents multiple Headers instances from pointing
|
|
183
|
-
* at the same internal "rawHeaders" array.
|
|
184
|
-
*/
|
|
185
|
-
...Reflect.get(headersInit, kRawHeaders)
|
|
186
|
-
]);
|
|
187
|
-
return headers2;
|
|
188
|
-
}
|
|
189
|
-
const headers = Reflect.construct(target, args, newTarget);
|
|
190
|
-
if (!Reflect.has(headers, kRawHeaders)) {
|
|
191
|
-
const rawHeadersInit = Array.isArray(headersInit) ? headersInit : Object.entries(headersInit);
|
|
192
|
-
ensureRawHeadersSymbol(headers, rawHeadersInit);
|
|
193
|
-
}
|
|
194
|
-
return headers;
|
|
195
|
-
}
|
|
196
|
-
})
|
|
197
|
-
});
|
|
198
|
-
Headers.prototype.set = new Proxy(Headers.prototype.set, {
|
|
199
|
-
apply(target, thisArg, args) {
|
|
200
|
-
recordRawHeader(thisArg, args, "set");
|
|
201
|
-
return Reflect.apply(target, thisArg, args);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
Headers.prototype.append = new Proxy(Headers.prototype.append, {
|
|
205
|
-
apply(target, thisArg, args) {
|
|
206
|
-
recordRawHeader(thisArg, args, "append");
|
|
207
|
-
return Reflect.apply(target, thisArg, args);
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
Headers.prototype.delete = new Proxy(Headers.prototype.delete, {
|
|
211
|
-
apply(target, thisArg, args) {
|
|
212
|
-
const rawHeaders = Reflect.get(thisArg, kRawHeaders);
|
|
213
|
-
if (rawHeaders) {
|
|
214
|
-
for (let index = rawHeaders.length - 1; index >= 0; index--) {
|
|
215
|
-
if (rawHeaders[index][0].toLowerCase() === args[0].toLowerCase()) {
|
|
216
|
-
rawHeaders.splice(index, 1);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return Reflect.apply(target, thisArg, args);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
Object.defineProperty(globalThis, "Request", {
|
|
224
|
-
enumerable: true,
|
|
225
|
-
writable: true,
|
|
226
|
-
value: new Proxy(Request, {
|
|
227
|
-
construct(target, args, newTarget) {
|
|
228
|
-
const request = Reflect.construct(target, args, newTarget);
|
|
229
|
-
const inferredRawHeaders = [];
|
|
230
|
-
if (typeof args[0] === "object" && args[0].headers != null) {
|
|
231
|
-
inferredRawHeaders.push(...inferRawHeaders(args[0].headers));
|
|
232
|
-
}
|
|
233
|
-
if (typeof args[1] === "object" && args[1].headers != null) {
|
|
234
|
-
inferredRawHeaders.push(...inferRawHeaders(args[1].headers));
|
|
235
|
-
}
|
|
236
|
-
if (inferredRawHeaders.length > 0) {
|
|
237
|
-
ensureRawHeadersSymbol(request.headers, inferredRawHeaders);
|
|
238
|
-
}
|
|
239
|
-
return request;
|
|
240
|
-
}
|
|
241
|
-
})
|
|
242
|
-
});
|
|
243
|
-
Object.defineProperty(globalThis, "Response", {
|
|
244
|
-
enumerable: true,
|
|
245
|
-
writable: true,
|
|
246
|
-
value: new Proxy(Response, {
|
|
247
|
-
construct(target, args, newTarget) {
|
|
248
|
-
const response = Reflect.construct(target, args, newTarget);
|
|
249
|
-
if (typeof args[1] === "object" && args[1].headers != null) {
|
|
250
|
-
ensureRawHeadersSymbol(
|
|
251
|
-
response.headers,
|
|
252
|
-
inferRawHeaders(args[1].headers)
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
return response;
|
|
256
|
-
}
|
|
257
|
-
})
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
function restoreHeadersPrototype() {
|
|
261
|
-
if (!Reflect.get(Headers, kRestorePatches)) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
Reflect.get(Headers, kRestorePatches)();
|
|
265
|
-
}
|
|
266
|
-
function getRawFetchHeaders(headers) {
|
|
267
|
-
if (!Reflect.has(headers, kRawHeaders)) {
|
|
268
|
-
return Array.from(headers.entries());
|
|
269
|
-
}
|
|
270
|
-
const rawHeaders = Reflect.get(headers, kRawHeaders);
|
|
271
|
-
return rawHeaders.length > 0 ? rawHeaders : Array.from(headers.entries());
|
|
272
|
-
}
|
|
273
|
-
function inferRawHeaders(headers) {
|
|
274
|
-
if (headers instanceof Headers) {
|
|
275
|
-
return Reflect.get(headers, kRawHeaders) || [];
|
|
276
|
-
}
|
|
277
|
-
return Reflect.get(new Headers(headers), kRawHeaders);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// src/interceptors/ClientRequest/MockHttpSocket.ts
|
|
281
|
-
var kRequestId = Symbol("kRequestId");
|
|
282
|
-
var MockHttpSocket = class extends MockSocket {
|
|
283
|
-
constructor(options) {
|
|
284
|
-
super({
|
|
285
|
-
write: (chunk, encoding, callback) => {
|
|
286
|
-
var _a;
|
|
287
|
-
if (this.socketState !== "passthrough") {
|
|
288
|
-
this.writeBuffer.push([chunk, encoding, callback]);
|
|
289
|
-
}
|
|
290
|
-
if (chunk) {
|
|
291
|
-
if (this.socketState === "passthrough") {
|
|
292
|
-
(_a = this.originalSocket) == null ? void 0 : _a.write(chunk, encoding, callback);
|
|
293
|
-
}
|
|
294
|
-
this.requestParser.execute(
|
|
295
|
-
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding)
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
read: (chunk) => {
|
|
300
|
-
if (chunk !== null) {
|
|
301
|
-
this.responseParser.execute(
|
|
302
|
-
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
this.requestRawHeadersBuffer = [];
|
|
308
|
-
this.responseRawHeadersBuffer = [];
|
|
309
|
-
this.writeBuffer = [];
|
|
310
|
-
this.socketState = "unknown";
|
|
311
|
-
/**
|
|
312
|
-
* This callback might be called when the request is "slow":
|
|
313
|
-
* - Request headers were fragmented across multiple TCP packages;
|
|
314
|
-
* - Request headers were too large to be processed in a single run
|
|
315
|
-
* (e.g. more than 30 request headers).
|
|
316
|
-
* @note This is called before request start.
|
|
317
|
-
*/
|
|
318
|
-
this.onRequestHeaders = (rawHeaders) => {
|
|
319
|
-
this.requestRawHeadersBuffer.push(...rawHeaders);
|
|
320
|
-
};
|
|
321
|
-
this.onRequestStart = (versionMajor, versionMinor, rawHeaders, _, path, __, ___, ____, shouldKeepAlive) => {
|
|
322
|
-
var _a;
|
|
323
|
-
this.shouldKeepAlive = shouldKeepAlive;
|
|
324
|
-
const url = new URL(path || "", this.baseUrl);
|
|
325
|
-
const method = ((_a = this.connectionOptions.method) == null ? void 0 : _a.toUpperCase()) || "GET";
|
|
326
|
-
const headers = FetchResponse.parseRawHeaders([
|
|
327
|
-
...this.requestRawHeadersBuffer,
|
|
328
|
-
...rawHeaders || []
|
|
329
|
-
]);
|
|
330
|
-
this.requestRawHeadersBuffer.length = 0;
|
|
331
|
-
const canHaveBody = method !== "GET" && method !== "HEAD";
|
|
332
|
-
if (url.username || url.password) {
|
|
333
|
-
if (!headers.has("authorization")) {
|
|
334
|
-
headers.set("authorization", `Basic ${url.username}:${url.password}`);
|
|
335
|
-
}
|
|
336
|
-
url.username = "";
|
|
337
|
-
url.password = "";
|
|
338
|
-
}
|
|
339
|
-
this.requestStream = new Readable({
|
|
340
|
-
/**
|
|
341
|
-
* @note Provide the `read()` method so a `Readable` could be
|
|
342
|
-
* used as the actual request body (the stream calls "read()").
|
|
343
|
-
* We control the queue in the onRequestBody/End functions.
|
|
344
|
-
*/
|
|
345
|
-
read: () => {
|
|
346
|
-
this.flushWriteBuffer();
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
const requestId = createRequestId();
|
|
350
|
-
this.request = new Request(url, {
|
|
351
|
-
method,
|
|
352
|
-
headers,
|
|
353
|
-
credentials: "same-origin",
|
|
354
|
-
// @ts-expect-error Undocumented Fetch property.
|
|
355
|
-
duplex: canHaveBody ? "half" : void 0,
|
|
356
|
-
body: canHaveBody ? Readable.toWeb(this.requestStream) : null
|
|
357
|
-
});
|
|
358
|
-
Reflect.set(this.request, kRequestId, requestId);
|
|
359
|
-
setRawRequest(this.request, Reflect.get(this, "_httpMessage"));
|
|
360
|
-
setRawRequestBodyStream(this.request, this.requestStream);
|
|
361
|
-
if (this.request.headers.has(INTERNAL_REQUEST_ID_HEADER_NAME)) {
|
|
362
|
-
this.passthrough();
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
this.onRequest({
|
|
366
|
-
requestId,
|
|
367
|
-
request: this.request,
|
|
368
|
-
socket: this
|
|
369
|
-
});
|
|
370
|
-
};
|
|
371
|
-
/**
|
|
372
|
-
* This callback might be called when the response is "slow":
|
|
373
|
-
* - Response headers were fragmented across multiple TCP packages;
|
|
374
|
-
* - Response headers were too large to be processed in a single run
|
|
375
|
-
* (e.g. more than 30 response headers).
|
|
376
|
-
* @note This is called before response start.
|
|
377
|
-
*/
|
|
378
|
-
this.onResponseHeaders = (rawHeaders) => {
|
|
379
|
-
this.responseRawHeadersBuffer.push(...rawHeaders);
|
|
380
|
-
};
|
|
381
|
-
this.onResponseStart = (versionMajor, versionMinor, rawHeaders, method, url, status, statusText) => {
|
|
382
|
-
const headers = FetchResponse.parseRawHeaders([
|
|
383
|
-
...this.responseRawHeadersBuffer,
|
|
384
|
-
...rawHeaders || []
|
|
385
|
-
]);
|
|
386
|
-
this.responseRawHeadersBuffer.length = 0;
|
|
387
|
-
const response = new FetchResponse(
|
|
388
|
-
/**
|
|
389
|
-
* @note The Fetch API response instance exposed to the consumer
|
|
390
|
-
* is created over the response stream of the HTTP parser. It is NOT
|
|
391
|
-
* related to the Socket instance. This way, you can read response body
|
|
392
|
-
* in response listener while the Socket instance delays the emission
|
|
393
|
-
* of "end" and other events until those response listeners are finished.
|
|
394
|
-
*/
|
|
395
|
-
FetchResponse.isResponseWithBody(status) ? Readable.toWeb(
|
|
396
|
-
this.responseStream = new Readable({ read() {
|
|
397
|
-
} })
|
|
398
|
-
) : null,
|
|
399
|
-
{
|
|
400
|
-
url,
|
|
401
|
-
status,
|
|
402
|
-
statusText,
|
|
403
|
-
headers
|
|
404
|
-
}
|
|
405
|
-
);
|
|
406
|
-
invariant(
|
|
407
|
-
this.request,
|
|
408
|
-
"Failed to handle a response: request does not exist"
|
|
409
|
-
);
|
|
410
|
-
FetchResponse.setUrl(this.request.url, response);
|
|
411
|
-
if (this.request.headers.has(INTERNAL_REQUEST_ID_HEADER_NAME)) {
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
this.responseListenersPromise = this.onResponse({
|
|
415
|
-
response,
|
|
416
|
-
isMockedResponse: this.socketState === "mock",
|
|
417
|
-
requestId: Reflect.get(this.request, kRequestId),
|
|
418
|
-
request: this.request,
|
|
419
|
-
socket: this
|
|
420
|
-
});
|
|
421
|
-
};
|
|
422
|
-
this.connectionOptions = options.connectionOptions;
|
|
423
|
-
this.createConnection = options.createConnection;
|
|
424
|
-
this.onRequest = options.onRequest;
|
|
425
|
-
this.onResponse = options.onResponse;
|
|
426
|
-
this.baseUrl = baseUrlFromConnectionOptions(this.connectionOptions);
|
|
427
|
-
this.requestParser = new HTTPParser();
|
|
428
|
-
this.requestParser.initialize(HTTPParser.REQUEST, {});
|
|
429
|
-
this.requestParser[HTTPParser.kOnHeaders] = this.onRequestHeaders.bind(this);
|
|
430
|
-
this.requestParser[HTTPParser.kOnHeadersComplete] = this.onRequestStart.bind(this);
|
|
431
|
-
this.requestParser[HTTPParser.kOnBody] = this.onRequestBody.bind(this);
|
|
432
|
-
this.requestParser[HTTPParser.kOnMessageComplete] = this.onRequestEnd.bind(this);
|
|
433
|
-
this.responseParser = new HTTPParser();
|
|
434
|
-
this.responseParser.initialize(HTTPParser.RESPONSE, {});
|
|
435
|
-
this.responseParser[HTTPParser.kOnHeaders] = this.onResponseHeaders.bind(this);
|
|
436
|
-
this.responseParser[HTTPParser.kOnHeadersComplete] = this.onResponseStart.bind(this);
|
|
437
|
-
this.responseParser[HTTPParser.kOnBody] = this.onResponseBody.bind(this);
|
|
438
|
-
this.responseParser[HTTPParser.kOnMessageComplete] = this.onResponseEnd.bind(this);
|
|
439
|
-
this.once("finish", () => this.requestParser.free());
|
|
440
|
-
if (this.baseUrl.protocol === "https:") {
|
|
441
|
-
Reflect.set(this, "encrypted", true);
|
|
442
|
-
Reflect.set(this, "authorized", false);
|
|
443
|
-
Reflect.set(this, "getProtocol", () => "TLSv1.3");
|
|
444
|
-
Reflect.set(this, "getSession", () => void 0);
|
|
445
|
-
Reflect.set(this, "isSessionReused", () => false);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
emit(event, ...args) {
|
|
449
|
-
const emitEvent = super.emit.bind(this, event, ...args);
|
|
450
|
-
if (this.responseListenersPromise) {
|
|
451
|
-
this.responseListenersPromise.finally(emitEvent);
|
|
452
|
-
return this.listenerCount(event) > 0;
|
|
453
|
-
}
|
|
454
|
-
return emitEvent();
|
|
455
|
-
}
|
|
456
|
-
destroy(error) {
|
|
457
|
-
this.responseParser.free();
|
|
458
|
-
if (error) {
|
|
459
|
-
this.emit("error", error);
|
|
460
|
-
}
|
|
461
|
-
return super.destroy(error);
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* Establish this Socket connection as-is and pipe
|
|
465
|
-
* its data/events through this Socket.
|
|
466
|
-
*/
|
|
467
|
-
passthrough() {
|
|
468
|
-
this.socketState = "passthrough";
|
|
469
|
-
if (this.destroyed) {
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
const socket = this.createConnection();
|
|
473
|
-
this.originalSocket = socket;
|
|
474
|
-
if ("_handle" in socket) {
|
|
475
|
-
Object.defineProperty(this, "_handle", {
|
|
476
|
-
value: socket._handle,
|
|
477
|
-
enumerable: true,
|
|
478
|
-
writable: true
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
this.once("error", (error) => {
|
|
482
|
-
socket.destroy(error);
|
|
483
|
-
});
|
|
484
|
-
this.address = socket.address.bind(socket);
|
|
485
|
-
let writeArgs;
|
|
486
|
-
let headersWritten = false;
|
|
487
|
-
while (writeArgs = this.writeBuffer.shift()) {
|
|
488
|
-
if (writeArgs !== void 0) {
|
|
489
|
-
if (!headersWritten) {
|
|
490
|
-
const [chunk, encoding, callback] = writeArgs;
|
|
491
|
-
const chunkString = chunk.toString();
|
|
492
|
-
const chunkBeforeRequestHeaders = chunkString.slice(
|
|
493
|
-
0,
|
|
494
|
-
chunkString.indexOf("\r\n") + 2
|
|
495
|
-
);
|
|
496
|
-
const chunkAfterRequestHeaders = chunkString.slice(
|
|
497
|
-
chunk.indexOf("\r\n\r\n")
|
|
498
|
-
);
|
|
499
|
-
const rawRequestHeaders = getRawFetchHeaders(this.request.headers);
|
|
500
|
-
const requestHeadersString = rawRequestHeaders.filter(([name]) => {
|
|
501
|
-
return name.toLowerCase() !== INTERNAL_REQUEST_ID_HEADER_NAME;
|
|
502
|
-
}).map(([name, value]) => `${name}: ${value}`).join("\r\n");
|
|
503
|
-
const headersChunk = `${chunkBeforeRequestHeaders}${requestHeadersString}${chunkAfterRequestHeaders}`;
|
|
504
|
-
socket.write(headersChunk, encoding, callback);
|
|
505
|
-
headersWritten = true;
|
|
506
|
-
continue;
|
|
507
|
-
}
|
|
508
|
-
socket.write(...writeArgs);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
if (Reflect.get(socket, "encrypted")) {
|
|
512
|
-
const tlsProperties = [
|
|
513
|
-
"encrypted",
|
|
514
|
-
"authorized",
|
|
515
|
-
"getProtocol",
|
|
516
|
-
"getSession",
|
|
517
|
-
"isSessionReused"
|
|
518
|
-
];
|
|
519
|
-
tlsProperties.forEach((propertyName) => {
|
|
520
|
-
Object.defineProperty(this, propertyName, {
|
|
521
|
-
enumerable: true,
|
|
522
|
-
get: () => {
|
|
523
|
-
const value = Reflect.get(socket, propertyName);
|
|
524
|
-
return typeof value === "function" ? value.bind(socket) : value;
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
socket.on("lookup", (...args) => this.emit("lookup", ...args)).on("connect", () => {
|
|
530
|
-
this.connecting = socket.connecting;
|
|
531
|
-
this.emit("connect");
|
|
532
|
-
}).on("secureConnect", () => this.emit("secureConnect")).on("secure", () => this.emit("secure")).on("session", (session) => this.emit("session", session)).on("ready", () => this.emit("ready")).on("drain", () => this.emit("drain")).on("data", (chunk) => {
|
|
533
|
-
this.push(chunk);
|
|
534
|
-
}).on("error", (error) => {
|
|
535
|
-
Reflect.set(this, "_hadError", Reflect.get(socket, "_hadError"));
|
|
536
|
-
this.emit("error", error);
|
|
537
|
-
}).on("resume", () => this.emit("resume")).on("timeout", () => this.emit("timeout")).on("prefinish", () => this.emit("prefinish")).on("finish", () => this.emit("finish")).on("close", (hadError) => this.emit("close", hadError)).on("end", () => this.emit("end"));
|
|
538
|
-
}
|
|
539
|
-
/**
|
|
540
|
-
* Convert the given Fetch API `Response` instance to an
|
|
541
|
-
* HTTP message and push it to the socket.
|
|
542
|
-
*/
|
|
543
|
-
async respondWith(response) {
|
|
544
|
-
var _a;
|
|
545
|
-
if (this.destroyed) {
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
if (isPropertyAccessible(response, "type") && response.type === "error") {
|
|
549
|
-
this.errorWith(new TypeError("Network error"));
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
this.mockConnect();
|
|
553
|
-
this.socketState = "mock";
|
|
554
|
-
this.flushWriteBuffer();
|
|
555
|
-
const serverResponse = new ServerResponse(new IncomingMessage(this));
|
|
556
|
-
serverResponse.assignSocket(
|
|
557
|
-
new MockSocket({
|
|
558
|
-
write: (chunk, encoding, callback) => {
|
|
559
|
-
this.push(chunk, encoding);
|
|
560
|
-
callback == null ? void 0 : callback();
|
|
561
|
-
},
|
|
562
|
-
read() {
|
|
563
|
-
}
|
|
564
|
-
})
|
|
565
|
-
);
|
|
566
|
-
serverResponse.removeHeader("connection");
|
|
567
|
-
serverResponse.removeHeader("date");
|
|
568
|
-
const rawResponseHeaders = getRawFetchHeaders(response.headers);
|
|
569
|
-
serverResponse.writeHead(
|
|
570
|
-
response.status,
|
|
571
|
-
response.statusText || STATUS_CODES[response.status],
|
|
572
|
-
rawResponseHeaders
|
|
573
|
-
);
|
|
574
|
-
this.once("error", () => {
|
|
575
|
-
serverResponse.destroy();
|
|
576
|
-
});
|
|
577
|
-
if (response.body) {
|
|
578
|
-
try {
|
|
579
|
-
const reader = response.body.getReader();
|
|
580
|
-
while (true) {
|
|
581
|
-
const { done, value } = await reader.read();
|
|
582
|
-
if (done) {
|
|
583
|
-
serverResponse.end();
|
|
584
|
-
break;
|
|
585
|
-
}
|
|
586
|
-
serverResponse.write(value);
|
|
587
|
-
}
|
|
588
|
-
} catch (error) {
|
|
589
|
-
this.respondWith(createServerErrorResponse(error));
|
|
590
|
-
return;
|
|
591
|
-
}
|
|
592
|
-
} else {
|
|
593
|
-
serverResponse.end();
|
|
594
|
-
}
|
|
595
|
-
if (!this.shouldKeepAlive) {
|
|
596
|
-
this.emit("readable");
|
|
597
|
-
(_a = this.responseStream) == null ? void 0 : _a.push(null);
|
|
598
|
-
this.push(null);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* Close this socket connection with the given error.
|
|
603
|
-
*/
|
|
604
|
-
errorWith(error) {
|
|
605
|
-
this.destroy(error);
|
|
606
|
-
}
|
|
607
|
-
mockConnect() {
|
|
608
|
-
this.connecting = false;
|
|
609
|
-
const isIPv6 = net2.isIPv6(this.connectionOptions.hostname) || this.connectionOptions.family === 6;
|
|
610
|
-
const addressInfo = {
|
|
611
|
-
address: isIPv6 ? "::1" : "127.0.0.1",
|
|
612
|
-
family: isIPv6 ? "IPv6" : "IPv4",
|
|
613
|
-
port: this.connectionOptions.port
|
|
614
|
-
};
|
|
615
|
-
this.address = () => addressInfo;
|
|
616
|
-
this.emit(
|
|
617
|
-
"lookup",
|
|
618
|
-
null,
|
|
619
|
-
addressInfo.address,
|
|
620
|
-
addressInfo.family === "IPv6" ? 6 : 4,
|
|
621
|
-
this.connectionOptions.host
|
|
622
|
-
);
|
|
623
|
-
this.emit("connect");
|
|
624
|
-
this.emit("ready");
|
|
625
|
-
if (this.baseUrl.protocol === "https:") {
|
|
626
|
-
this.emit("secure");
|
|
627
|
-
this.emit("secureConnect");
|
|
628
|
-
this.emit(
|
|
629
|
-
"session",
|
|
630
|
-
this.connectionOptions.session || Buffer.from("mock-session-renegotiate")
|
|
631
|
-
);
|
|
632
|
-
this.emit("session", Buffer.from("mock-session-resume"));
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
flushWriteBuffer() {
|
|
636
|
-
for (const writeCall of this.writeBuffer) {
|
|
637
|
-
if (typeof writeCall[2] === "function") {
|
|
638
|
-
writeCall[2]();
|
|
639
|
-
writeCall[2] = void 0;
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
onRequestBody(chunk) {
|
|
644
|
-
invariant(
|
|
645
|
-
this.requestStream,
|
|
646
|
-
"Failed to write to a request stream: stream does not exist"
|
|
647
|
-
);
|
|
648
|
-
this.requestStream.push(chunk);
|
|
649
|
-
}
|
|
650
|
-
onRequestEnd() {
|
|
651
|
-
if (this.requestStream) {
|
|
652
|
-
this.requestStream.push(null);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
onResponseBody(chunk) {
|
|
656
|
-
invariant(
|
|
657
|
-
this.responseStream,
|
|
658
|
-
"Failed to write to a response stream: stream does not exist"
|
|
659
|
-
);
|
|
660
|
-
this.responseStream.push(chunk);
|
|
661
|
-
}
|
|
662
|
-
onResponseEnd() {
|
|
663
|
-
if (this.responseStream) {
|
|
664
|
-
this.responseStream.push(null);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
};
|
|
668
|
-
|
|
669
|
-
// src/interceptors/ClientRequest/agents.ts
|
|
670
|
-
import http from "http";
|
|
671
|
-
import https from "https";
|
|
672
|
-
var MockAgent = class extends http.Agent {
|
|
673
|
-
constructor(options) {
|
|
674
|
-
super();
|
|
675
|
-
this.customAgent = options.customAgent;
|
|
676
|
-
this.onRequest = options.onRequest;
|
|
677
|
-
this.onResponse = options.onResponse;
|
|
678
|
-
}
|
|
679
|
-
createConnection(options, callback) {
|
|
680
|
-
const createConnection = this.customAgent instanceof http.Agent ? this.customAgent.createConnection : super.createConnection;
|
|
681
|
-
const createConnectionOptions = this.customAgent instanceof http.Agent ? {
|
|
682
|
-
...options,
|
|
683
|
-
...this.customAgent.options
|
|
684
|
-
} : options;
|
|
685
|
-
const socket = new MockHttpSocket({
|
|
686
|
-
connectionOptions: options,
|
|
687
|
-
createConnection: createConnection.bind(
|
|
688
|
-
this.customAgent || this,
|
|
689
|
-
createConnectionOptions,
|
|
690
|
-
callback
|
|
691
|
-
),
|
|
692
|
-
onRequest: this.onRequest.bind(this),
|
|
693
|
-
onResponse: this.onResponse.bind(this)
|
|
694
|
-
});
|
|
695
|
-
return socket;
|
|
696
|
-
}
|
|
697
|
-
};
|
|
698
|
-
var MockHttpsAgent = class extends https.Agent {
|
|
699
|
-
constructor(options) {
|
|
700
|
-
super();
|
|
701
|
-
this.customAgent = options.customAgent;
|
|
702
|
-
this.onRequest = options.onRequest;
|
|
703
|
-
this.onResponse = options.onResponse;
|
|
704
|
-
}
|
|
705
|
-
createConnection(options, callback) {
|
|
706
|
-
const createConnection = this.customAgent instanceof http.Agent ? this.customAgent.createConnection : super.createConnection;
|
|
707
|
-
const createConnectionOptions = this.customAgent instanceof http.Agent ? {
|
|
708
|
-
...options,
|
|
709
|
-
...this.customAgent.options
|
|
710
|
-
} : options;
|
|
711
|
-
const socket = new MockHttpSocket({
|
|
712
|
-
connectionOptions: options,
|
|
713
|
-
createConnection: createConnection.bind(
|
|
714
|
-
this.customAgent || this,
|
|
715
|
-
createConnectionOptions,
|
|
716
|
-
callback
|
|
717
|
-
),
|
|
718
|
-
onRequest: this.onRequest.bind(this),
|
|
719
|
-
onResponse: this.onResponse.bind(this)
|
|
720
|
-
});
|
|
721
|
-
return socket;
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
|
|
725
|
-
// src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts
|
|
726
|
-
import { urlToHttpOptions } from "url";
|
|
727
|
-
import {
|
|
728
|
-
globalAgent as httpGlobalAgent
|
|
729
|
-
} from "http";
|
|
730
|
-
import {
|
|
731
|
-
globalAgent as httpsGlobalAgent
|
|
732
|
-
} from "https";
|
|
733
|
-
import {
|
|
734
|
-
URL as URL2,
|
|
735
|
-
parse as parseUrl
|
|
736
|
-
} from "url";
|
|
737
|
-
import { Logger as Logger3 } from "@open-draft/logger";
|
|
738
|
-
|
|
739
|
-
// src/utils/getUrlByRequestOptions.ts
|
|
740
|
-
import { Agent } from "http";
|
|
741
|
-
import { Logger } from "@open-draft/logger";
|
|
742
|
-
var logger = new Logger("utils getUrlByRequestOptions");
|
|
743
|
-
var DEFAULT_PATH = "/";
|
|
744
|
-
var DEFAULT_PROTOCOL = "http:";
|
|
745
|
-
var DEFAULT_HOSTNAME = "localhost";
|
|
746
|
-
var SSL_PORT = 443;
|
|
747
|
-
function getAgent(options) {
|
|
748
|
-
return options.agent instanceof Agent ? options.agent : void 0;
|
|
749
|
-
}
|
|
750
|
-
function getProtocolByRequestOptions(options) {
|
|
751
|
-
var _a;
|
|
752
|
-
if (options.protocol) {
|
|
753
|
-
return options.protocol;
|
|
754
|
-
}
|
|
755
|
-
const agent = getAgent(options);
|
|
756
|
-
const agentProtocol = agent == null ? void 0 : agent.protocol;
|
|
757
|
-
if (agentProtocol) {
|
|
758
|
-
return agentProtocol;
|
|
759
|
-
}
|
|
760
|
-
const port = getPortByRequestOptions(options);
|
|
761
|
-
const isSecureRequest = options.cert || port === SSL_PORT;
|
|
762
|
-
return isSecureRequest ? "https:" : ((_a = options.uri) == null ? void 0 : _a.protocol) || DEFAULT_PROTOCOL;
|
|
763
|
-
}
|
|
764
|
-
function getPortByRequestOptions(options) {
|
|
765
|
-
if (options.port) {
|
|
766
|
-
return Number(options.port);
|
|
767
|
-
}
|
|
768
|
-
const agent = getAgent(options);
|
|
769
|
-
if (agent == null ? void 0 : agent.options.port) {
|
|
770
|
-
return Number(agent.options.port);
|
|
771
|
-
}
|
|
772
|
-
if (agent == null ? void 0 : agent.defaultPort) {
|
|
773
|
-
return Number(agent.defaultPort);
|
|
774
|
-
}
|
|
775
|
-
return void 0;
|
|
776
|
-
}
|
|
777
|
-
function getAuthByRequestOptions(options) {
|
|
778
|
-
if (options.auth) {
|
|
779
|
-
const [username, password] = options.auth.split(":");
|
|
780
|
-
return { username, password };
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
function isRawIPv6Address(host) {
|
|
784
|
-
return host.includes(":") && !host.startsWith("[") && !host.endsWith("]");
|
|
785
|
-
}
|
|
786
|
-
function getHostname(options) {
|
|
787
|
-
let host = options.hostname || options.host;
|
|
788
|
-
if (host) {
|
|
789
|
-
if (isRawIPv6Address(host)) {
|
|
790
|
-
host = `[${host}]`;
|
|
791
|
-
}
|
|
792
|
-
return new URL(`http://${host}`).hostname;
|
|
793
|
-
}
|
|
794
|
-
return DEFAULT_HOSTNAME;
|
|
795
|
-
}
|
|
796
|
-
function getUrlByRequestOptions(options) {
|
|
797
|
-
logger.info("request options", options);
|
|
798
|
-
if (options.uri) {
|
|
799
|
-
logger.info(
|
|
800
|
-
'constructing url from explicitly provided "options.uri": %s',
|
|
801
|
-
options.uri
|
|
802
|
-
);
|
|
803
|
-
return new URL(options.uri.href);
|
|
804
|
-
}
|
|
805
|
-
logger.info("figuring out url from request options...");
|
|
806
|
-
const protocol = getProtocolByRequestOptions(options);
|
|
807
|
-
logger.info("protocol", protocol);
|
|
808
|
-
const port = getPortByRequestOptions(options);
|
|
809
|
-
logger.info("port", port);
|
|
810
|
-
const hostname = getHostname(options);
|
|
811
|
-
logger.info("hostname", hostname);
|
|
812
|
-
const path = options.path || DEFAULT_PATH;
|
|
813
|
-
logger.info("path", path);
|
|
814
|
-
const credentials = getAuthByRequestOptions(options);
|
|
815
|
-
logger.info("credentials", credentials);
|
|
816
|
-
const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
|
|
817
|
-
logger.info("auth string:", authString);
|
|
818
|
-
const portString = typeof port !== "undefined" ? `:${port}` : "";
|
|
819
|
-
const url = new URL(`${protocol}//${hostname}${portString}${path}`);
|
|
820
|
-
url.username = (credentials == null ? void 0 : credentials.username) || "";
|
|
821
|
-
url.password = (credentials == null ? void 0 : credentials.password) || "";
|
|
822
|
-
logger.info("created url:", url);
|
|
823
|
-
return url;
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
// src/utils/cloneObject.ts
|
|
827
|
-
import { Logger as Logger2 } from "@open-draft/logger";
|
|
828
|
-
var logger2 = new Logger2("cloneObject");
|
|
829
|
-
function isPlainObject(obj) {
|
|
830
|
-
var _a;
|
|
831
|
-
logger2.info("is plain object?", obj);
|
|
832
|
-
if (obj == null || !((_a = obj.constructor) == null ? void 0 : _a.name)) {
|
|
833
|
-
logger2.info("given object is undefined, not a plain object...");
|
|
834
|
-
return false;
|
|
835
|
-
}
|
|
836
|
-
logger2.info("checking the object constructor:", obj.constructor.name);
|
|
837
|
-
return obj.constructor.name === "Object";
|
|
838
|
-
}
|
|
839
|
-
function cloneObject(obj) {
|
|
840
|
-
logger2.info("cloning object:", obj);
|
|
841
|
-
const enumerableProperties = Object.entries(obj).reduce(
|
|
842
|
-
(acc, [key, value]) => {
|
|
843
|
-
logger2.info("analyzing key-value pair:", key, value);
|
|
844
|
-
acc[key] = isPlainObject(value) ? cloneObject(value) : value;
|
|
845
|
-
return acc;
|
|
846
|
-
},
|
|
847
|
-
{}
|
|
848
|
-
);
|
|
849
|
-
return isPlainObject(obj) ? enumerableProperties : Object.assign(Object.getPrototypeOf(obj), enumerableProperties);
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
// src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts
|
|
853
|
-
var logger3 = new Logger3("http normalizeClientRequestArgs");
|
|
854
|
-
function resolveRequestOptions(args, url) {
|
|
855
|
-
if (typeof args[1] === "undefined" || typeof args[1] === "function") {
|
|
856
|
-
logger3.info("request options not provided, deriving from the url", url);
|
|
857
|
-
return urlToHttpOptions(url);
|
|
858
|
-
}
|
|
859
|
-
if (args[1]) {
|
|
860
|
-
logger3.info("has custom RequestOptions!", args[1]);
|
|
861
|
-
const requestOptionsFromUrl = urlToHttpOptions(url);
|
|
862
|
-
logger3.info("derived RequestOptions from the URL:", requestOptionsFromUrl);
|
|
863
|
-
logger3.info("cloning RequestOptions...");
|
|
864
|
-
const clonedRequestOptions = cloneObject(args[1]);
|
|
865
|
-
logger3.info("successfully cloned RequestOptions!", clonedRequestOptions);
|
|
866
|
-
return {
|
|
867
|
-
...requestOptionsFromUrl,
|
|
868
|
-
...clonedRequestOptions
|
|
869
|
-
};
|
|
870
|
-
}
|
|
871
|
-
logger3.info("using an empty object as request options");
|
|
872
|
-
return {};
|
|
873
|
-
}
|
|
874
|
-
function overrideUrlByRequestOptions(url, options) {
|
|
875
|
-
url.host = options.host || url.host;
|
|
876
|
-
url.hostname = options.hostname || url.hostname;
|
|
877
|
-
url.port = options.port ? options.port.toString() : url.port;
|
|
878
|
-
if (options.path) {
|
|
879
|
-
const parsedOptionsPath = parseUrl(options.path, false);
|
|
880
|
-
url.pathname = parsedOptionsPath.pathname || "";
|
|
881
|
-
url.search = parsedOptionsPath.search || "";
|
|
882
|
-
}
|
|
883
|
-
return url;
|
|
884
|
-
}
|
|
885
|
-
function resolveCallback(args) {
|
|
886
|
-
return typeof args[1] === "function" ? args[1] : args[2];
|
|
887
|
-
}
|
|
888
|
-
function normalizeClientRequestArgs(defaultProtocol, args) {
|
|
889
|
-
let url;
|
|
890
|
-
let options;
|
|
891
|
-
let callback;
|
|
892
|
-
logger3.info("arguments", args);
|
|
893
|
-
logger3.info("using default protocol:", defaultProtocol);
|
|
894
|
-
if (args.length === 0) {
|
|
895
|
-
const url2 = new URL2("http://localhost");
|
|
896
|
-
const options2 = resolveRequestOptions(args, url2);
|
|
897
|
-
return [url2, options2];
|
|
898
|
-
}
|
|
899
|
-
if (typeof args[0] === "string") {
|
|
900
|
-
logger3.info("first argument is a location string:", args[0]);
|
|
901
|
-
url = new URL2(args[0]);
|
|
902
|
-
logger3.info("created a url:", url);
|
|
903
|
-
const requestOptionsFromUrl = urlToHttpOptions(url);
|
|
904
|
-
logger3.info("request options from url:", requestOptionsFromUrl);
|
|
905
|
-
options = resolveRequestOptions(args, url);
|
|
906
|
-
logger3.info("resolved request options:", options);
|
|
907
|
-
callback = resolveCallback(args);
|
|
908
|
-
} else if (args[0] instanceof URL2) {
|
|
909
|
-
url = args[0];
|
|
910
|
-
logger3.info("first argument is a URL:", url);
|
|
911
|
-
if (typeof args[1] !== "undefined" && isObject(args[1])) {
|
|
912
|
-
url = overrideUrlByRequestOptions(url, args[1]);
|
|
913
|
-
}
|
|
914
|
-
options = resolveRequestOptions(args, url);
|
|
915
|
-
logger3.info("derived request options:", options);
|
|
916
|
-
callback = resolveCallback(args);
|
|
917
|
-
} else if ("hash" in args[0] && !("method" in args[0])) {
|
|
918
|
-
const [legacyUrl] = args;
|
|
919
|
-
logger3.info("first argument is a legacy URL:", legacyUrl);
|
|
920
|
-
if (legacyUrl.hostname === null) {
|
|
921
|
-
logger3.info("given legacy URL is relative (no hostname)");
|
|
922
|
-
return isObject(args[1]) ? normalizeClientRequestArgs(defaultProtocol, [
|
|
923
|
-
{ path: legacyUrl.path, ...args[1] },
|
|
924
|
-
args[2]
|
|
925
|
-
]) : normalizeClientRequestArgs(defaultProtocol, [
|
|
926
|
-
{ path: legacyUrl.path },
|
|
927
|
-
args[1]
|
|
928
|
-
]);
|
|
929
|
-
}
|
|
930
|
-
logger3.info("given legacy url is absolute");
|
|
931
|
-
const resolvedUrl = new URL2(legacyUrl.href);
|
|
932
|
-
return args[1] === void 0 ? normalizeClientRequestArgs(defaultProtocol, [resolvedUrl]) : typeof args[1] === "function" ? normalizeClientRequestArgs(defaultProtocol, [resolvedUrl, args[1]]) : normalizeClientRequestArgs(defaultProtocol, [
|
|
933
|
-
resolvedUrl,
|
|
934
|
-
args[1],
|
|
935
|
-
args[2]
|
|
936
|
-
]);
|
|
937
|
-
} else if (isObject(args[0])) {
|
|
938
|
-
options = { ...args[0] };
|
|
939
|
-
logger3.info("first argument is RequestOptions:", options);
|
|
940
|
-
options.protocol = options.protocol || defaultProtocol;
|
|
941
|
-
logger3.info("normalized request options:", options);
|
|
942
|
-
url = getUrlByRequestOptions(options);
|
|
943
|
-
logger3.info("created a URL from RequestOptions:", url.href);
|
|
944
|
-
callback = resolveCallback(args);
|
|
945
|
-
} else {
|
|
946
|
-
throw new Error(
|
|
947
|
-
`Failed to construct ClientRequest with these parameters: ${args}`
|
|
948
|
-
);
|
|
949
|
-
}
|
|
950
|
-
options.protocol = options.protocol || url.protocol;
|
|
951
|
-
options.method = options.method || "GET";
|
|
952
|
-
if (!options._defaultAgent) {
|
|
953
|
-
logger3.info(
|
|
954
|
-
'has no default agent, setting the default agent for "%s"',
|
|
955
|
-
options.protocol
|
|
956
|
-
);
|
|
957
|
-
options._defaultAgent = options.protocol === "https:" ? httpsGlobalAgent : httpGlobalAgent;
|
|
958
|
-
}
|
|
959
|
-
logger3.info("successfully resolved url:", url.href);
|
|
960
|
-
logger3.info("successfully resolved options:", options);
|
|
961
|
-
logger3.info("successfully resolved callback:", callback);
|
|
962
|
-
if (!(url instanceof URL2)) {
|
|
963
|
-
url = url.toString();
|
|
964
|
-
}
|
|
965
|
-
return [url, options, callback];
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
// src/interceptors/ClientRequest/index.ts
|
|
969
|
-
var _ClientRequestInterceptor = class extends Interceptor {
|
|
970
|
-
constructor() {
|
|
971
|
-
super(_ClientRequestInterceptor.symbol);
|
|
972
|
-
this.onRequest = async ({
|
|
973
|
-
request,
|
|
974
|
-
socket
|
|
975
|
-
}) => {
|
|
976
|
-
const requestId = Reflect.get(request, kRequestId);
|
|
977
|
-
const controller = new RequestController(request);
|
|
978
|
-
const isRequestHandled = await handleRequest({
|
|
979
|
-
request,
|
|
980
|
-
requestId,
|
|
981
|
-
controller,
|
|
982
|
-
emitter: this.emitter,
|
|
983
|
-
onResponse: (response) => {
|
|
984
|
-
socket.respondWith(response);
|
|
985
|
-
},
|
|
986
|
-
onRequestError: (response) => {
|
|
987
|
-
socket.respondWith(response);
|
|
988
|
-
},
|
|
989
|
-
onError: (error) => {
|
|
990
|
-
if (error instanceof Error) {
|
|
991
|
-
socket.errorWith(error);
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
});
|
|
995
|
-
if (!isRequestHandled) {
|
|
996
|
-
return socket.passthrough();
|
|
997
|
-
}
|
|
998
|
-
};
|
|
999
|
-
this.onResponse = async ({
|
|
1000
|
-
requestId,
|
|
1001
|
-
request,
|
|
1002
|
-
response,
|
|
1003
|
-
isMockedResponse
|
|
1004
|
-
}) => {
|
|
1005
|
-
return emitAsync(this.emitter, "response", {
|
|
1006
|
-
requestId,
|
|
1007
|
-
request,
|
|
1008
|
-
response,
|
|
1009
|
-
isMockedResponse
|
|
1010
|
-
});
|
|
1011
|
-
};
|
|
1012
|
-
}
|
|
1013
|
-
setup() {
|
|
1014
|
-
const {
|
|
1015
|
-
ClientRequest: OriginalClientRequest,
|
|
1016
|
-
get: originalGet,
|
|
1017
|
-
request: originalRequest
|
|
1018
|
-
} = http2;
|
|
1019
|
-
const { get: originalHttpsGet, request: originalHttpsRequest } = https2;
|
|
1020
|
-
const onRequest = this.onRequest.bind(this);
|
|
1021
|
-
const onResponse = this.onResponse.bind(this);
|
|
1022
|
-
http2.ClientRequest = new Proxy(http2.ClientRequest, {
|
|
1023
|
-
construct: (target, args) => {
|
|
1024
|
-
const [url, options, callback] = normalizeClientRequestArgs(
|
|
1025
|
-
"http:",
|
|
1026
|
-
args
|
|
1027
|
-
);
|
|
1028
|
-
const Agent2 = options.protocol === "https:" ? MockHttpsAgent : MockAgent;
|
|
1029
|
-
const mockAgent = new Agent2({
|
|
1030
|
-
customAgent: options.agent,
|
|
1031
|
-
onRequest,
|
|
1032
|
-
onResponse
|
|
1033
|
-
});
|
|
1034
|
-
options.agent = mockAgent;
|
|
1035
|
-
return Reflect.construct(target, [url, options, callback]);
|
|
1036
|
-
}
|
|
1037
|
-
});
|
|
1038
|
-
http2.request = new Proxy(http2.request, {
|
|
1039
|
-
apply: (target, thisArg, args) => {
|
|
1040
|
-
const [url, options, callback] = normalizeClientRequestArgs(
|
|
1041
|
-
"http:",
|
|
1042
|
-
args
|
|
1043
|
-
);
|
|
1044
|
-
const mockAgent = new MockAgent({
|
|
1045
|
-
customAgent: options.agent,
|
|
1046
|
-
onRequest,
|
|
1047
|
-
onResponse
|
|
1048
|
-
});
|
|
1049
|
-
options.agent = mockAgent;
|
|
1050
|
-
return Reflect.apply(target, thisArg, [url, options, callback]);
|
|
1051
|
-
}
|
|
1052
|
-
});
|
|
1053
|
-
http2.get = new Proxy(http2.get, {
|
|
1054
|
-
apply: (target, thisArg, args) => {
|
|
1055
|
-
const [url, options, callback] = normalizeClientRequestArgs(
|
|
1056
|
-
"http:",
|
|
1057
|
-
args
|
|
1058
|
-
);
|
|
1059
|
-
const mockAgent = new MockAgent({
|
|
1060
|
-
customAgent: options.agent,
|
|
1061
|
-
onRequest,
|
|
1062
|
-
onResponse
|
|
1063
|
-
});
|
|
1064
|
-
options.agent = mockAgent;
|
|
1065
|
-
return Reflect.apply(target, thisArg, [url, options, callback]);
|
|
1066
|
-
}
|
|
1067
|
-
});
|
|
1068
|
-
https2.request = new Proxy(https2.request, {
|
|
1069
|
-
apply: (target, thisArg, args) => {
|
|
1070
|
-
const [url, options, callback] = normalizeClientRequestArgs(
|
|
1071
|
-
"https:",
|
|
1072
|
-
args
|
|
1073
|
-
);
|
|
1074
|
-
const mockAgent = new MockHttpsAgent({
|
|
1075
|
-
customAgent: options.agent,
|
|
1076
|
-
onRequest,
|
|
1077
|
-
onResponse
|
|
1078
|
-
});
|
|
1079
|
-
options.agent = mockAgent;
|
|
1080
|
-
return Reflect.apply(target, thisArg, [url, options, callback]);
|
|
1081
|
-
}
|
|
1082
|
-
});
|
|
1083
|
-
https2.get = new Proxy(https2.get, {
|
|
1084
|
-
apply: (target, thisArg, args) => {
|
|
1085
|
-
const [url, options, callback] = normalizeClientRequestArgs(
|
|
1086
|
-
"https:",
|
|
1087
|
-
args
|
|
1088
|
-
);
|
|
1089
|
-
const mockAgent = new MockHttpsAgent({
|
|
1090
|
-
customAgent: options.agent,
|
|
1091
|
-
onRequest,
|
|
1092
|
-
onResponse
|
|
1093
|
-
});
|
|
1094
|
-
options.agent = mockAgent;
|
|
1095
|
-
return Reflect.apply(target, thisArg, [url, options, callback]);
|
|
1096
|
-
}
|
|
1097
|
-
});
|
|
1098
|
-
recordRawFetchHeaders();
|
|
1099
|
-
this.subscriptions.push(() => {
|
|
1100
|
-
http2.ClientRequest = OriginalClientRequest;
|
|
1101
|
-
http2.get = originalGet;
|
|
1102
|
-
http2.request = originalRequest;
|
|
1103
|
-
https2.get = originalHttpsGet;
|
|
1104
|
-
https2.request = originalHttpsRequest;
|
|
1105
|
-
restoreHeadersPrototype();
|
|
1106
|
-
});
|
|
1107
|
-
}
|
|
1108
|
-
};
|
|
1109
|
-
var ClientRequestInterceptor = _ClientRequestInterceptor;
|
|
1110
|
-
ClientRequestInterceptor.symbol = Symbol("client-request-interceptor");
|
|
1111
|
-
|
|
1112
|
-
export {
|
|
1113
|
-
ClientRequestInterceptor
|
|
1114
|
-
};
|
|
1115
|
-
//# sourceMappingURL=chunk-EKNRB5ZS.mjs.map
|