@mswjs/interceptors 0.28.3 → 0.29.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-F2F5QHHJ.js → chunk-2CRB3JAQ.js} +16 -2
- package/lib/browser/chunk-2CRB3JAQ.js.map +1 -0
- package/lib/browser/{chunk-PXSYFJ7G.mjs → chunk-732REFPX.mjs} +23 -4
- package/lib/browser/chunk-732REFPX.mjs.map +1 -0
- package/lib/browser/{chunk-NIWUC7GF.mjs → chunk-MAEPOYB6.mjs} +67 -35
- package/lib/browser/chunk-MAEPOYB6.mjs.map +1 -0
- package/lib/browser/{chunk-VISYSKLR.mjs → chunk-OMISYKWR.mjs} +16 -2
- package/lib/browser/chunk-OMISYKWR.mjs.map +1 -0
- package/lib/browser/{chunk-LAEV5ZGV.js → chunk-PSX5J3RF.js} +28 -9
- package/lib/browser/chunk-PSX5J3RF.js.map +1 -0
- package/lib/browser/{chunk-RLGVQZ5O.js → chunk-WBHIW62P.js} +69 -37
- package/lib/browser/chunk-WBHIW62P.js.map +1 -0
- package/lib/browser/{glossary-640c9679.d.ts → glossary-1c204f45.d.ts} +11 -0
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/index.js +2 -2
- package/lib/browser/index.mjs +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.js +3 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +2 -2
- package/lib/browser/interceptors/fetch/index.d.ts +1 -1
- package/lib/browser/interceptors/fetch/index.js +3 -3
- package/lib/browser/interceptors/fetch/index.mjs +2 -2
- package/lib/browser/presets/browser.d.ts +1 -1
- package/lib/browser/presets/browser.js +5 -5
- package/lib/browser/presets/browser.mjs +3 -3
- package/lib/node/{BatchInterceptor-cb145daa.d.ts → BatchInterceptor-2badedde.d.ts} +1 -1
- package/lib/node/{Interceptor-6696a18d.d.ts → Interceptor-88ee47c0.d.ts} +11 -0
- package/lib/node/RemoteHttpInterceptor.d.ts +2 -2
- package/lib/node/RemoteHttpInterceptor.js +9 -10
- package/lib/node/RemoteHttpInterceptor.js.map +1 -1
- package/lib/node/RemoteHttpInterceptor.mjs +5 -6
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/{chunk-2SC4AD6S.mjs → chunk-6FRASLM3.mjs} +2 -2
- package/lib/node/{chunk-M4JXH4RP.js → chunk-APT7KA3B.js} +32 -13
- package/lib/node/chunk-APT7KA3B.js.map +1 -0
- package/lib/node/{chunk-KRDNUBDZ.js → chunk-E4AC7YAC.js} +16 -2
- package/lib/node/chunk-E4AC7YAC.js.map +1 -0
- package/lib/node/{chunk-FZJKKO5H.js → chunk-EIBTX65O.js} +1 -1
- package/lib/node/{chunk-FZJKKO5H.js.map → chunk-EIBTX65O.js.map} +1 -1
- package/lib/node/{chunk-UXEUSYDY.js → chunk-HAIWBQD5.js} +48 -49
- package/lib/node/chunk-HAIWBQD5.js.map +1 -0
- package/lib/node/{chunk-L576JLIX.mjs → chunk-JMNEFEYU.mjs} +43 -44
- package/lib/node/chunk-JMNEFEYU.mjs.map +1 -0
- package/lib/node/{chunk-KGNKRQ7B.mjs → chunk-KSHIDGUL.mjs} +24 -5
- package/lib/node/chunk-KSHIDGUL.mjs.map +1 -0
- package/lib/node/{chunk-Z2DPXZWN.js → chunk-LTEXDYJ6.js} +3 -3
- package/lib/node/{chunk-HAGW22AN.mjs → chunk-OJ6O4LSC.mjs} +1 -1
- package/lib/node/{chunk-HAGW22AN.mjs.map → chunk-OJ6O4LSC.mjs.map} +1 -1
- package/lib/node/{chunk-DQ5DO3KN.mjs → chunk-Q7POAM5N.mjs} +16 -2
- package/lib/node/chunk-Q7POAM5N.mjs.map +1 -0
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +4 -4
- package/lib/node/index.mjs +3 -3
- package/lib/node/interceptors/ClientRequest/index.d.ts +1 -1
- package/lib/node/interceptors/ClientRequest/index.js +3 -4
- package/lib/node/interceptors/ClientRequest/index.mjs +2 -3
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -1
- package/lib/node/interceptors/XMLHttpRequest/index.js +4 -4
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/node/interceptors/fetch/index.d.ts +1 -1
- package/lib/node/interceptors/fetch/index.js +71 -32
- package/lib/node/interceptors/fetch/index.js.map +1 -1
- package/lib/node/interceptors/fetch/index.mjs +67 -28
- package/lib/node/interceptors/fetch/index.mjs.map +1 -1
- package/lib/node/presets/node.d.ts +1 -1
- package/lib/node/presets/node.js +6 -7
- package/lib/node/presets/node.js.map +1 -1
- package/lib/node/presets/node.mjs +4 -5
- package/lib/node/presets/node.mjs.map +1 -1
- package/package.json +1 -1
- package/src/glossary.ts +11 -0
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +46 -18
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +4 -7
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +36 -3
- package/src/interceptors/fetch/index.ts +88 -33
- package/src/utils/getUrlByRequestOptions.test.ts +31 -3
- package/src/utils/getUrlByRequestOptions.ts +14 -38
- package/src/utils/isObject.test.ts +4 -3
- package/src/utils/isObject.ts +4 -2
- package/src/utils/responseUtils.ts +16 -0
- package/lib/browser/chunk-F2F5QHHJ.js.map +0 -1
- package/lib/browser/chunk-LAEV5ZGV.js.map +0 -1
- package/lib/browser/chunk-NIWUC7GF.mjs.map +0 -1
- package/lib/browser/chunk-PXSYFJ7G.mjs.map +0 -1
- package/lib/browser/chunk-RLGVQZ5O.js.map +0 -1
- package/lib/browser/chunk-VISYSKLR.mjs.map +0 -1
- package/lib/node/chunk-DERTLGL3.mjs +0 -14
- package/lib/node/chunk-DERTLGL3.mjs.map +0 -1
- package/lib/node/chunk-DQ5DO3KN.mjs.map +0 -1
- package/lib/node/chunk-KGNKRQ7B.mjs.map +0 -1
- package/lib/node/chunk-KRDNUBDZ.js.map +0 -1
- package/lib/node/chunk-L576JLIX.mjs.map +0 -1
- package/lib/node/chunk-M4JXH4RP.js.map +0 -1
- package/lib/node/chunk-UXEUSYDY.js.map +0 -1
- package/lib/node/chunk-Y6GRL6UD.js +0 -14
- package/lib/node/chunk-Y6GRL6UD.js.map +0 -1
- /package/lib/node/{chunk-2SC4AD6S.mjs.map → chunk-6FRASLM3.mjs.map} +0 -0
- /package/lib/node/{chunk-Z2DPXZWN.js.map → chunk-LTEXDYJ6.js.map} +0 -0
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
2
|
|
|
3
|
-
var _chunkY6GRL6UDjs = require('./chunk-Y6GRL6UD.js');
|
|
4
|
-
|
|
5
|
-
|
|
6
3
|
|
|
7
4
|
var _chunkMQJ3JOOKjs = require('./chunk-MQJ3JOOK.js');
|
|
8
5
|
|
|
@@ -11,7 +8,8 @@ var _chunkMQJ3JOOKjs = require('./chunk-MQJ3JOOK.js');
|
|
|
11
8
|
|
|
12
9
|
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
|
|
12
|
+
var _chunkE4AC7YACjs = require('./chunk-E4AC7YAC.js');
|
|
15
13
|
|
|
16
14
|
// src/interceptors/ClientRequest/index.ts
|
|
17
15
|
var _http = require('http'); var _http2 = _interopRequireDefault(_http);
|
|
@@ -109,7 +107,7 @@ function inheritProperties(source, target) {
|
|
|
109
107
|
|
|
110
108
|
// src/interceptors/ClientRequest/utils/createResponse.ts
|
|
111
109
|
function createResponse(message) {
|
|
112
|
-
const responseBodyOrNull =
|
|
110
|
+
const responseBodyOrNull = _chunkE4AC7YACjs.isResponseWithoutBody.call(void 0, message.statusCode || 200) ? null : new ReadableStream({
|
|
113
111
|
start(controller) {
|
|
114
112
|
message.on("data", (chunk) => controller.enqueue(chunk));
|
|
115
113
|
message.on("end", () => controller.close());
|
|
@@ -181,8 +179,8 @@ function getValueBySymbol(symbolName, source) {
|
|
|
181
179
|
}
|
|
182
180
|
|
|
183
181
|
// src/utils/isObject.ts
|
|
184
|
-
function isObject(value) {
|
|
185
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
182
|
+
function isObject(value, loose = false) {
|
|
183
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
186
184
|
}
|
|
187
185
|
|
|
188
186
|
// src/utils/getRawFetchHeaders.ts
|
|
@@ -268,7 +266,7 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
268
266
|
}
|
|
269
267
|
end(...args) {
|
|
270
268
|
this.logger.info("end", args);
|
|
271
|
-
const requestId =
|
|
269
|
+
const requestId = _chunkE4AC7YACjs.createRequestId.call(void 0, );
|
|
272
270
|
const [chunk, encoding, callback] = normalizeClientRequestEndArgs(...args);
|
|
273
271
|
this.logger.info("normalized arguments:", { chunk, encoding, callback });
|
|
274
272
|
this.writeRequestBodyChunk(chunk, encoding || void 0);
|
|
@@ -278,8 +276,8 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
278
276
|
Object.defineProperty(capturedRequest, "respondWith", {
|
|
279
277
|
value: requestController.respondWith.bind(requestController)
|
|
280
278
|
});
|
|
281
|
-
if (this.hasHeader(
|
|
282
|
-
this.removeHeader(
|
|
279
|
+
if (this.hasHeader(_chunkE4AC7YACjs.INTERNAL_REQUEST_ID_HEADER_NAME)) {
|
|
280
|
+
this.removeHeader(_chunkE4AC7YACjs.INTERNAL_REQUEST_ID_HEADER_NAME);
|
|
283
281
|
return this.passthrough(chunk, encoding, callback);
|
|
284
282
|
}
|
|
285
283
|
this.emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
@@ -321,14 +319,37 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
321
319
|
resolverResult.error
|
|
322
320
|
);
|
|
323
321
|
if (resolverResult.error instanceof Response) {
|
|
324
|
-
|
|
322
|
+
if (_chunkE4AC7YACjs.isResponseError.call(void 0, resolverResult.error)) {
|
|
323
|
+
this.logger.info(
|
|
324
|
+
"received network error response, erroring request..."
|
|
325
|
+
);
|
|
326
|
+
this.errorWith(new TypeError("Network error"));
|
|
327
|
+
} else {
|
|
328
|
+
this.respondWith(resolverResult.error);
|
|
329
|
+
}
|
|
325
330
|
return;
|
|
326
331
|
}
|
|
327
332
|
if (isNodeLikeError(resolverResult.error)) {
|
|
328
333
|
this.errorWith(resolverResult.error);
|
|
329
334
|
return this;
|
|
330
335
|
}
|
|
331
|
-
|
|
336
|
+
_until.until.call(void 0, async () => {
|
|
337
|
+
if (this.emitter.listenerCount("unhandledException") > 0) {
|
|
338
|
+
await _chunkMQJ3JOOKjs.emitAsync.call(void 0, this.emitter, "unhandledException", {
|
|
339
|
+
error: resolverResult.error,
|
|
340
|
+
request: capturedRequest,
|
|
341
|
+
requestId,
|
|
342
|
+
controller: {
|
|
343
|
+
respondWith: this.respondWith.bind(this),
|
|
344
|
+
errorWith: this.errorWith.bind(this)
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
if (this.writableEnded || this.destroyed) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
this.respondWith(_chunkE4AC7YACjs.createServerErrorResponse.call(void 0, resolverResult.error));
|
|
352
|
+
});
|
|
332
353
|
return this;
|
|
333
354
|
}
|
|
334
355
|
const mockedResponse = resolverResult.data;
|
|
@@ -339,15 +360,7 @@ var _NodeClientRequest = class extends _http.ClientRequest {
|
|
|
339
360
|
mockedResponse.statusText
|
|
340
361
|
);
|
|
341
362
|
this.destroyed = false;
|
|
342
|
-
if (
|
|
343
|
-
/**
|
|
344
|
-
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
345
|
-
* implement the "Response.type" property and throw on its access.
|
|
346
|
-
* Safely check if we can access "type" on "Response" before continuing.
|
|
347
|
-
* @see https://github.com/mswjs/msw/issues/1834
|
|
348
|
-
*/
|
|
349
|
-
_chunkY6GRL6UDjs.isPropertyAccessible.call(void 0, mockedResponse, "type") && mockedResponse.type === "error"
|
|
350
|
-
) {
|
|
363
|
+
if (_chunkE4AC7YACjs.isResponseError.call(void 0, mockedResponse)) {
|
|
351
364
|
this.logger.info(
|
|
352
365
|
"received network error response, erroring request..."
|
|
353
366
|
);
|
|
@@ -579,7 +592,7 @@ function getRequestOptionsByUrl(url) {
|
|
|
579
592
|
var logger3 = new (0, _logger.Logger)("utils getUrlByRequestOptions");
|
|
580
593
|
var DEFAULT_PATH = "/";
|
|
581
594
|
var DEFAULT_PROTOCOL = "http:";
|
|
582
|
-
var
|
|
595
|
+
var DEFAULT_HOSTNAME = "localhost";
|
|
583
596
|
var SSL_PORT = 443;
|
|
584
597
|
function getAgent(options) {
|
|
585
598
|
return options.agent instanceof _http.Agent ? options.agent : void 0;
|
|
@@ -602,12 +615,6 @@ function getPortByRequestOptions(options) {
|
|
|
602
615
|
if (options.port) {
|
|
603
616
|
return Number(options.port);
|
|
604
617
|
}
|
|
605
|
-
if (options.hostname != null) {
|
|
606
|
-
const [, extractedPort] = options.hostname.match(/:(\d+)$/) || [];
|
|
607
|
-
if (extractedPort != null) {
|
|
608
|
-
return Number(extractedPort);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
618
|
const agent = getAgent(options);
|
|
612
619
|
if (agent == null ? void 0 : agent.options.port) {
|
|
613
620
|
return Number(agent.options.port);
|
|
@@ -617,13 +624,6 @@ function getPortByRequestOptions(options) {
|
|
|
617
624
|
}
|
|
618
625
|
return void 0;
|
|
619
626
|
}
|
|
620
|
-
function getHostByRequestOptions(options) {
|
|
621
|
-
const { hostname, host } = options;
|
|
622
|
-
if (hostname != null) {
|
|
623
|
-
return hostname.replace(/:\d+$/, "");
|
|
624
|
-
}
|
|
625
|
-
return host || DEFAULT_HOST;
|
|
626
|
-
}
|
|
627
627
|
function getAuthByRequestOptions(options) {
|
|
628
628
|
if (options.auth) {
|
|
629
629
|
const [username, password] = options.auth.split(":");
|
|
@@ -633,15 +633,15 @@ function getAuthByRequestOptions(options) {
|
|
|
633
633
|
function isRawIPv6Address(host) {
|
|
634
634
|
return host.includes(":") && !host.startsWith("[") && !host.endsWith("]");
|
|
635
635
|
}
|
|
636
|
-
function getHostname(
|
|
637
|
-
|
|
638
|
-
if (
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
return host;
|
|
636
|
+
function getHostname(options) {
|
|
637
|
+
let host = options.hostname || options.host;
|
|
638
|
+
if (host) {
|
|
639
|
+
if (isRawIPv6Address(host)) {
|
|
640
|
+
host = `[${host}]`;
|
|
641
|
+
}
|
|
642
|
+
return new URL(`http://${host}`).hostname;
|
|
643
643
|
}
|
|
644
|
-
return
|
|
644
|
+
return DEFAULT_HOSTNAME;
|
|
645
645
|
}
|
|
646
646
|
function getUrlByRequestOptions(options) {
|
|
647
647
|
logger3.info("request options", options);
|
|
@@ -655,11 +655,9 @@ function getUrlByRequestOptions(options) {
|
|
|
655
655
|
logger3.info("figuring out url from request options...");
|
|
656
656
|
const protocol = getProtocolByRequestOptions(options);
|
|
657
657
|
logger3.info("protocol", protocol);
|
|
658
|
-
const host = getHostByRequestOptions(options);
|
|
659
|
-
logger3.info("host", host);
|
|
660
658
|
const port = getPortByRequestOptions(options);
|
|
661
659
|
logger3.info("port", port);
|
|
662
|
-
const hostname = getHostname(
|
|
660
|
+
const hostname = getHostname(options);
|
|
663
661
|
logger3.info("hostname", hostname);
|
|
664
662
|
const path = options.path || DEFAULT_PATH;
|
|
665
663
|
logger3.info("path", path);
|
|
@@ -667,7 +665,8 @@ function getUrlByRequestOptions(options) {
|
|
|
667
665
|
logger3.info("credentials", credentials);
|
|
668
666
|
const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
|
|
669
667
|
logger3.info("auth string:", authString);
|
|
670
|
-
const
|
|
668
|
+
const portString = typeof port !== "undefined" ? `:${port}` : "";
|
|
669
|
+
const url = new URL(`${protocol}//${hostname}${portString}${path}`);
|
|
671
670
|
url.username = (credentials == null ? void 0 : credentials.username) || "";
|
|
672
671
|
url.password = (credentials == null ? void 0 : credentials.password) || "";
|
|
673
672
|
logger3.info("created url:", url);
|
|
@@ -851,7 +850,7 @@ function request(protocol, options) {
|
|
|
851
850
|
}
|
|
852
851
|
|
|
853
852
|
// src/interceptors/ClientRequest/index.ts
|
|
854
|
-
var _ClientRequestInterceptor = class extends
|
|
853
|
+
var _ClientRequestInterceptor = class extends _chunkE4AC7YACjs.Interceptor {
|
|
855
854
|
constructor() {
|
|
856
855
|
super(_ClientRequestInterceptor.interceptorSymbol);
|
|
857
856
|
this.modules = /* @__PURE__ */ new Map();
|
|
@@ -885,4 +884,4 @@ ClientRequestInterceptor.interceptorSymbol = Symbol("http");
|
|
|
885
884
|
|
|
886
885
|
|
|
887
886
|
exports.ClientRequestInterceptor = ClientRequestInterceptor;
|
|
888
|
-
//# sourceMappingURL=chunk-
|
|
887
|
+
//# sourceMappingURL=chunk-HAIWBQD5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/interceptors/ClientRequest/index.ts","../../src/interceptors/ClientRequest/NodeClientRequest.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts","../../src/interceptors/ClientRequest/utils/cloneIncomingMessage.ts","../../src/interceptors/ClientRequest/utils/createResponse.ts","../../src/interceptors/ClientRequest/utils/createRequest.ts","../../src/utils/getValueBySymbol.ts","../../src/utils/isObject.ts","../../src/utils/getRawFetchHeaders.ts","../../src/utils/isNodeLikeError.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts","../../src/utils/getRequestOptionsByUrl.ts","../../src/utils/getUrlByRequestOptions.ts","../../src/utils/cloneObject.ts","../../src/interceptors/ClientRequest/http.get.ts","../../src/interceptors/ClientRequest/http.request.ts"],"names":["IncomingMessage","Logger","logger","symbol","chunk","encoding","url","options","request"],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;AAAA,MACH,GAAG;AAAA,IACL;AACA,WAAO,IAAI,kBAAkB,mBAAmB,OAAO;AAAA,EACzD;AACF;;;AhBTO,IAAM,4BAAN,cAAuC,YAAiC;AAAA,EAI7E,cAAc;AACZ,UAAM,0BAAyB,iBAAiB;AAEhD,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,QAAQ,IAAI,QAAQ,IAAI;AAC7B,SAAK,QAAQ,IAAI,SAAS,KAAK;AAAA,EACjC;AAAA,EAEU,QAAc;AACtB,UAAMA,UAAS,KAAK,OAAO,OAAO,OAAO;AAEzC,eAAW,CAAC,UAAU,aAAa,KAAK,KAAK,SAAS;AACpD,YAAM,EAAE,SAAS,aAAa,KAAK,QAAQ,IAAI;AAE/C,WAAK,cAAc,KAAK,MAAM;AAC5B,sBAAc,UAAU;AACxB,sBAAc,MAAM;AAEpB,QAAAA,QAAO,KAAK,gCAAgC,QAAQ;AAAA,MACtD,CAAC;AAED,YAAM,UAA6B;AAAA,QACjC,SAAS,KAAK;AAAA,QACd,QAAQ,KAAK;AAAA,MACf;AAGA,oBAAc;AAAA,MAEZ,QAAQ,UAAU,OAAO;AAG3B,oBAAc;AAAA,MAEZ,IAAI,UAAU,OAAO;AAEvB,MAAAA,QAAO,KAAK,+BAA+B,QAAQ;AAAA,IACrD;AAAA,EACF;AACF;AA3CO,IAAM,2BAAN;AAAM,yBACJ,oBAAoB,OAAO,MAAM","sourcesContent":["import http from 'http'\nimport https from 'https'\nimport type { Emitter } from 'strict-event-emitter'\nimport { HttpRequestEventMap } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { get } from './http.get'\nimport { request } from './http.request'\nimport { NodeClientOptions, Protocol } from './NodeClientRequest'\n\nexport type ClientRequestEmitter = Emitter<HttpRequestEventMap>\n\nexport type ClientRequestModules = Map<Protocol, typeof http | typeof https>\n\n/**\n * Intercept requests made via the `ClientRequest` class.\n * Such requests include `http.get`, `https.request`, etc.\n */\nexport class ClientRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static interceptorSymbol = Symbol('http')\n private modules: ClientRequestModules\n\n constructor() {\n super(ClientRequestInterceptor.interceptorSymbol)\n\n this.modules = new Map()\n this.modules.set('http', http)\n this.modules.set('https', https)\n }\n\n protected setup(): void {\n const logger = this.logger.extend('setup')\n\n for (const [protocol, requestModule] of this.modules) {\n const { request: pureRequest, get: pureGet } = requestModule\n\n this.subscriptions.push(() => {\n requestModule.request = pureRequest\n requestModule.get = pureGet\n\n logger.info('native \"%s\" module restored!', protocol)\n })\n\n const options: NodeClientOptions = {\n emitter: this.emitter,\n logger: this.logger,\n }\n\n // @ts-ignore\n requestModule.request =\n // Force a line break.\n request(protocol, options)\n\n // @ts-ignore\n requestModule.get =\n // Force a line break.\n get(protocol, options)\n\n logger.info('native \"%s\" module patched!', protocol)\n }\n }\n}\n","import { ClientRequest, IncomingMessage, STATUS_CODES } from 'node:http'\nimport type { Logger } from '@open-draft/logger'\nimport { until } from '@open-draft/until'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport type { ClientRequestEmitter } from '.'\nimport {\n ClientRequestEndCallback,\n ClientRequestEndChunk,\n normalizeClientRequestEndArgs,\n} from './utils/normalizeClientRequestEndArgs'\nimport { NormalizedClientRequestArgs } from './utils/normalizeClientRequestArgs'\nimport {\n ClientRequestWriteArgs,\n normalizeClientRequestWriteArgs,\n} from './utils/normalizeClientRequestWriteArgs'\nimport { cloneIncomingMessage } from './utils/cloneIncomingMessage'\nimport { createResponse } from './utils/createResponse'\nimport { createRequest } from './utils/createRequest'\nimport { toInteractiveRequest } from '../../utils/toInteractiveRequest'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { getRawFetchHeaders } from '../../utils/getRawFetchHeaders'\nimport { isNodeLikeError } from '../../utils/isNodeLikeError'\nimport { INTERNAL_REQUEST_ID_HEADER_NAME } from '../../Interceptor'\nimport { createRequestId } from '../../createRequestId'\nimport {\n createServerErrorResponse,\n isResponseError,\n} from '../../utils/responseUtils'\n\nexport type Protocol = 'http' | 'https'\n\nenum HttpClientInternalState {\n // Have the concept of an idle request because different\n // request methods can kick off request sending\n // (e.g. \".end()\" or \".flushHeaders()\").\n Idle,\n Sending,\n Sent,\n MockLookupStart,\n MockLookupEnd,\n ResponseReceived,\n}\n\nexport interface NodeClientOptions {\n emitter: ClientRequestEmitter\n logger: Logger\n}\n\nexport class NodeClientRequest extends ClientRequest {\n /**\n * The list of internal Node.js errors to suppress while\n * using the \"mock\" response source.\n */\n static suppressErrorCodes = [\n 'ENOTFOUND',\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'EAI_AGAIN',\n 'ENETUNREACH',\n 'EHOSTUNREACH',\n ]\n\n /**\n * Internal state of the request.\n */\n private state: HttpClientInternalState\n private responseType?: 'mock' | 'passthrough'\n private response: IncomingMessage\n private emitter: ClientRequestEmitter\n private logger: Logger\n private chunks: Array<{\n chunk?: string | Buffer\n encoding?: BufferEncoding\n }> = []\n private capturedError?: NodeJS.ErrnoException\n\n public url: URL\n public requestBuffer: Buffer | null\n\n constructor(\n [url, requestOptions, callback]: NormalizedClientRequestArgs,\n options: NodeClientOptions\n ) {\n super(requestOptions, callback)\n\n this.logger = options.logger.extend(\n `request ${requestOptions.method} ${url.href}`\n )\n\n this.logger.info('constructing ClientRequest using options:', {\n url,\n requestOptions,\n callback,\n })\n\n this.state = HttpClientInternalState.Idle\n this.url = url\n this.emitter = options.emitter\n\n // Set request buffer to null by default so that GET/HEAD requests\n // without a body wouldn't suddenly get one.\n this.requestBuffer = null\n\n // Construct a mocked response message.\n this.response = new IncomingMessage(this.socket!)\n }\n\n private writeRequestBodyChunk(\n chunk: string | Buffer | null,\n encoding?: BufferEncoding\n ): void {\n if (chunk == null) {\n return\n }\n\n if (this.requestBuffer == null) {\n this.requestBuffer = Buffer.from([])\n }\n\n const resolvedChunk = Buffer.isBuffer(chunk)\n ? chunk\n : Buffer.from(chunk, encoding)\n\n this.requestBuffer = Buffer.concat([this.requestBuffer, resolvedChunk])\n }\n\n write(...args: ClientRequestWriteArgs): boolean {\n const [chunk, encoding, callback] = normalizeClientRequestWriteArgs(args)\n this.logger.info('write:', { chunk, encoding, callback })\n this.chunks.push({ chunk, encoding })\n\n // Write each request body chunk to the internal buffer.\n this.writeRequestBodyChunk(chunk, encoding)\n\n this.logger.info(\n 'chunk successfully stored!',\n this.requestBuffer?.byteLength\n )\n\n /**\n * Prevent invoking the callback if the written chunk is empty.\n * @see https://nodejs.org/api/http.html#requestwritechunk-encoding-callback\n */\n if (!chunk || chunk.length === 0) {\n this.logger.info('written chunk is empty, skipping callback...')\n } else {\n callback?.()\n }\n\n // Do not write the request body chunks to prevent\n // the Socket from sending data to a potentially existing\n // server when there is a mocked response defined.\n return true\n }\n\n end(...args: any): this {\n this.logger.info('end', args)\n\n const requestId = createRequestId()\n\n const [chunk, encoding, callback] = normalizeClientRequestEndArgs(...args)\n this.logger.info('normalized arguments:', { chunk, encoding, callback })\n\n // Write the last request body chunk passed to the \"end()\" method.\n this.writeRequestBodyChunk(chunk, encoding || undefined)\n\n /**\n * @note Mark the request as sent immediately when invoking \".end()\".\n * In Node.js, calling \".end()\" will flush the remaining request body\n * and mark the request as \"finished\" immediately (\"end\" is synchronous)\n * but we delegate that property update to:\n *\n * - respondWith(), in the case of mocked responses;\n * - super.end(), in the case of bypassed responses.\n *\n * For that reason, we have to keep an internal flag for a finished request.\n */\n this.state = HttpClientInternalState.Sent\n\n const capturedRequest = createRequest(this)\n const { interactiveRequest, requestController } =\n toInteractiveRequest(capturedRequest)\n\n /**\n * @todo Remove this modification of the original request\n * and expose the controller alongside it in the \"request\"\n * listener argument.\n */\n Object.defineProperty(capturedRequest, 'respondWith', {\n value: requestController.respondWith.bind(requestController),\n })\n\n // Prevent handling this request if it has already been handled\n // in another (parent) interceptor (like XMLHttpRequest -> ClientRequest).\n // That means some interceptor up the chain has concluded that\n // this request must be performed as-is.\n if (this.hasHeader(INTERNAL_REQUEST_ID_HEADER_NAME)) {\n this.removeHeader(INTERNAL_REQUEST_ID_HEADER_NAME)\n return this.passthrough(chunk, encoding, callback)\n }\n\n // Add the last \"request\" listener that always resolves\n // the pending response Promise. This way if the consumer\n // hasn't handled the request themselves, we will prevent\n // the response Promise from pending indefinitely.\n this.emitter.once('request', ({ requestId: pendingRequestId }) => {\n /**\n * @note Ignore request events emitted by irrelevant\n * requests. This happens when response patching.\n */\n if (pendingRequestId !== requestId) {\n return\n }\n\n if (requestController.responsePromise.state === 'pending') {\n this.logger.info(\n 'request has not been handled in listeners, executing fail-safe listener...'\n )\n\n requestController.responsePromise.resolve(undefined)\n }\n })\n\n // Execute the resolver Promise like a side-effect.\n // Node.js 16 forces \"ClientRequest.end\" to be synchronous and return \"this\".\n until<unknown, Response | undefined>(async () => {\n // Notify the interceptor about the request.\n // This will call any \"request\" listeners the users have.\n this.logger.info(\n 'emitting the \"request\" event for %d listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n this.state = HttpClientInternalState.MockLookupStart\n\n await emitAsync(this.emitter, 'request', {\n request: interactiveRequest,\n requestId,\n })\n\n this.logger.info('all \"request\" listeners done!')\n\n const mockedResponse = await requestController.responsePromise\n this.logger.info('event.respondWith called with:', mockedResponse)\n\n return mockedResponse\n }).then((resolverResult) => {\n this.logger.info('the listeners promise awaited!')\n\n this.state = HttpClientInternalState.MockLookupEnd\n\n /**\n * @fixme We are in the \"end()\" method that still executes in parallel\n * to our mocking logic here. This can be solved by migrating to the\n * Proxy-based approach and deferring the passthrough \"end()\" properly.\n * @see https://github.com/mswjs/interceptors/issues/346\n */\n if (!this.headersSent) {\n // Forward any request headers that the \"request\" listener\n // may have modified before proceeding with this request.\n for (const [headerName, headerValue] of capturedRequest.headers) {\n this.setHeader(headerName, headerValue)\n }\n }\n\n if (resolverResult.error) {\n this.logger.info(\n 'unhandled resolver exception, coercing to an error response...',\n resolverResult.error\n )\n\n // Handle thrown Response instances.\n if (resolverResult.error instanceof Response) {\n // Treat thrown Response.error() as a request error.\n if (isResponseError(resolverResult.error)) {\n this.logger.info(\n 'received network error response, erroring request...'\n )\n\n this.errorWith(new TypeError('Network error'))\n } else {\n // Handle a thrown Response as a mocked response.\n this.respondWith(resolverResult.error)\n }\n\n return\n }\n\n // Allow throwing Node.js-like errors, like connection rejection errors.\n // Treat them as request errors.\n if (isNodeLikeError(resolverResult.error)) {\n this.errorWith(resolverResult.error)\n return this\n }\n\n until(async () => {\n if (this.emitter.listenerCount('unhandledException') > 0) {\n // Emit the \"unhandledException\" event to allow the client\n // to opt-out from the default handling of exceptions\n // as 500 error responses.\n await emitAsync(this.emitter, 'unhandledException', {\n error: resolverResult.error,\n request: capturedRequest,\n requestId,\n controller: {\n respondWith: this.respondWith.bind(this),\n errorWith: this.errorWith.bind(this),\n },\n })\n\n // If after the \"unhandledException\" listeners are done,\n // the request is either not writable (was mocked) or\n // destroyed (has errored), do nothing.\n if (this.writableEnded || this.destroyed) {\n return\n }\n }\n\n // Unhandled exceptions in the request listeners are\n // synonymous to unhandled exceptions on the server.\n // Those are represented as 500 error responses.\n this.respondWith(createServerErrorResponse(resolverResult.error))\n })\n\n return this\n }\n\n const mockedResponse = resolverResult.data\n\n if (mockedResponse) {\n this.logger.info(\n 'received mocked response:',\n mockedResponse.status,\n mockedResponse.statusText\n )\n\n /**\n * @note Ignore this request being destroyed by TLS in Node.js\n * due to connection errors.\n */\n this.destroyed = false\n\n // Handle mocked \"Response.error\" network error responses.\n if (isResponseError(mockedResponse)) {\n this.logger.info(\n 'received network error response, erroring request...'\n )\n\n /**\n * There is no standardized error format for network errors\n * in Node.js. Instead, emit a generic TypeError.\n */\n this.errorWith(new TypeError('Network error'))\n\n return this\n }\n\n const responseClone = mockedResponse.clone()\n\n this.respondWith(mockedResponse)\n this.logger.info(\n mockedResponse.status,\n mockedResponse.statusText,\n '(MOCKED)'\n )\n\n callback?.()\n\n this.logger.info('emitting the custom \"response\" event...')\n this.emitter.emit('response', {\n response: responseClone,\n isMockedResponse: true,\n request: capturedRequest,\n requestId,\n })\n\n this.logger.info('request (mock) is completed')\n\n return this\n }\n\n this.logger.info('no mocked response received!')\n\n this.once('response-internal', (message: IncomingMessage) => {\n this.logger.info(message.statusCode, message.statusMessage)\n this.logger.info('original response headers:', message.headers)\n\n this.logger.info('emitting the custom \"response\" event...')\n this.emitter.emit('response', {\n response: createResponse(message),\n isMockedResponse: false,\n request: capturedRequest,\n requestId,\n })\n })\n\n return this.passthrough(chunk, encoding, callback)\n })\n\n return this\n }\n\n emit(event: string, ...data: any[]) {\n this.logger.info('emit: %s', event)\n\n if (event === 'response') {\n this.logger.info('found \"response\" event, cloning the response...')\n\n try {\n /**\n * Clone the response object when emitting the \"response\" event.\n * This prevents the response body stream from locking\n * and allows reading it twice:\n * 1. Internal \"response\" event from the observer.\n * 2. Any external response body listeners.\n * @see https://github.com/mswjs/interceptors/issues/161\n */\n const response = data[0] as IncomingMessage\n const firstClone = cloneIncomingMessage(response)\n const secondClone = cloneIncomingMessage(response)\n\n this.emit('response-internal', secondClone)\n\n this.logger.info(\n 'response successfully cloned, emitting \"response\" event...'\n )\n return super.emit(event, firstClone, ...data.slice(1))\n } catch (error) {\n this.logger.info('error when cloning response:', error)\n return super.emit(event, ...data)\n }\n }\n\n if (event === 'error') {\n const error = data[0] as NodeJS.ErrnoException\n const errorCode = error.code || ''\n\n this.logger.info('error:\\n', error)\n\n // Suppress only specific Node.js connection errors.\n if (NodeClientRequest.suppressErrorCodes.includes(errorCode)) {\n // Until we aren't sure whether the request will be\n // passthrough, capture the first emitted connection\n // error in case we have to replay it for this request.\n if (this.state < HttpClientInternalState.MockLookupEnd) {\n if (!this.capturedError) {\n this.capturedError = error\n this.logger.info('captured the first error:', this.capturedError)\n }\n return false\n }\n\n // Ignore any connection errors once we know the request\n // has been resolved with a mocked response. Don't capture\n // them as they won't ever be replayed.\n if (\n this.state === HttpClientInternalState.ResponseReceived &&\n this.responseType === 'mock'\n ) {\n return false\n }\n }\n }\n\n return super.emit(event, ...data)\n }\n\n /**\n * Performs the intercepted request as-is.\n * Replays the captured request body chunks,\n * still emits the internal events, and wraps\n * up the request with `super.end()`.\n */\n private passthrough(\n chunk: ClientRequestEndChunk | null,\n encoding?: BufferEncoding | null,\n callback?: ClientRequestEndCallback | null\n ): this {\n this.state = HttpClientInternalState.ResponseReceived\n this.responseType = 'passthrough'\n\n // Propagate previously captured errors.\n // For example, a ECONNREFUSED error when connecting to a non-existing host.\n if (this.capturedError) {\n this.emit('error', this.capturedError)\n return this\n }\n\n this.logger.info('writing request chunks...', this.chunks)\n\n // Write the request body chunks in the order of \".write()\" calls.\n // Note that no request body has been written prior to this point\n // in order to prevent the Socket to communicate with a potentially\n // existing server.\n for (const { chunk, encoding } of this.chunks) {\n if (encoding) {\n super.write(chunk, encoding)\n } else {\n super.write(chunk)\n }\n }\n\n this.once('error', (error) => {\n this.logger.info('original request error:', error)\n })\n\n this.once('abort', () => {\n this.logger.info('original request aborted!')\n })\n\n this.once('response-internal', (message: IncomingMessage) => {\n this.logger.info(message.statusCode, message.statusMessage)\n this.logger.info('original response headers:', message.headers)\n })\n\n this.logger.info('performing original request...')\n\n // This call signature is way too dynamic.\n return super.end(...[chunk, encoding as any, callback].filter(Boolean))\n }\n\n /**\n * Responds to this request instance using a mocked response.\n */\n private respondWith(mockedResponse: Response): void {\n this.logger.info('responding with a mocked response...', mockedResponse)\n\n this.state = HttpClientInternalState.ResponseReceived\n this.responseType = 'mock'\n\n /**\n * Mark the request as finished right before streaming back the response.\n * This is not entirely conventional but this will allow the consumer to\n * modify the outoging request in the interceptor.\n *\n * The request is finished when its headers and bodies have been sent.\n * @see https://nodejs.org/api/http.html#event-finish\n */\n Object.defineProperties(this, {\n writableFinished: { value: true },\n writableEnded: { value: true },\n })\n this.emit('finish')\n\n const { status, statusText, headers, body } = mockedResponse\n this.response.statusCode = status\n this.response.statusMessage = statusText || STATUS_CODES[status]\n\n // Try extracting the raw headers from the headers instance.\n // If not possible, fallback to the headers instance as-is.\n const rawHeaders = getRawFetchHeaders(headers) || headers\n\n if (rawHeaders) {\n this.response.headers = {}\n\n rawHeaders.forEach((headerValue, headerName) => {\n /**\n * @note Make sure that multi-value headers are appended correctly.\n */\n this.response.rawHeaders.push(headerName, headerValue)\n\n const insensitiveHeaderName = headerName.toLowerCase()\n const prevHeaders = this.response.headers[insensitiveHeaderName]\n this.response.headers[insensitiveHeaderName] = prevHeaders\n ? Array.prototype.concat([], prevHeaders, headerValue)\n : headerValue\n })\n }\n this.logger.info('mocked response headers ready:', headers)\n\n /**\n * Set the internal \"res\" property to the mocked \"OutgoingMessage\"\n * to make the \"ClientRequest\" instance think there's data received\n * from the socket.\n * @see https://github.com/nodejs/node/blob/9c405f2591f5833d0247ed0fafdcd68c5b14ce7a/lib/_http_client.js#L501\n *\n * Set the response immediately so the interceptor could stream data\n * chunks to the request client as they come in.\n */\n // @ts-ignore\n this.res = this.response\n this.emit('response', this.response)\n\n const isResponseStreamFinished = new DeferredPromise<void>()\n\n const finishResponseStream = () => {\n this.logger.info('finished response stream!')\n\n // Push \"null\" to indicate that the response body is complete\n // and shouldn't be written to anymore.\n this.response.push(null)\n this.response.complete = true\n\n isResponseStreamFinished.resolve()\n }\n\n if (body) {\n const bodyReader = body.getReader()\n const readNextChunk = async (): Promise<void> => {\n const { done, value } = await bodyReader.read()\n\n if (done) {\n finishResponseStream()\n return\n }\n\n this.response.emit('data', value)\n\n return readNextChunk()\n }\n\n readNextChunk()\n } else {\n finishResponseStream()\n }\n\n isResponseStreamFinished.then(() => {\n this.logger.info('finalizing response...')\n this.response.emit('end')\n this.terminate()\n\n this.logger.info('request complete!')\n })\n }\n\n private errorWith(error: Error): void {\n this.destroyed = true\n this.emit('error', error)\n this.terminate()\n }\n\n /**\n * Terminates a pending request.\n */\n private terminate(): void {\n /**\n * @note Some request clients (e.g. Octokit) create a ClientRequest\n * in a way that it has no Agent set. Now, whether that's correct is\n * debatable, but we should still handle this case gracefully.\n * @see https://github.com/mswjs/interceptors/issues/304\n */\n // @ts-ignore\n this.agent?.destroy()\n }\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('utils getUrlByRequestOptions')\n\nexport type ClientRequestEndChunk = string | Buffer\nexport type ClientRequestEndCallback = () => void\n\ntype HttpRequestEndArgs =\n | []\n | [ClientRequestEndCallback]\n | [ClientRequestEndChunk, ClientRequestEndCallback?]\n | [ClientRequestEndChunk, BufferEncoding, ClientRequestEndCallback?]\n\ntype NormalizedHttpRequestEndParams = [\n ClientRequestEndChunk | null,\n BufferEncoding | null,\n ClientRequestEndCallback | null\n]\n\n/**\n * Normalizes a list of arguments given to the `ClientRequest.end()`\n * method to always include `chunk`, `encoding`, and `callback`.\n */\nexport function normalizeClientRequestEndArgs(\n ...args: HttpRequestEndArgs\n): NormalizedHttpRequestEndParams {\n logger.info('arguments', args)\n const normalizedArgs = new Array(3)\n .fill(null)\n .map((value, index) => args[index] || value)\n\n normalizedArgs.sort((a, b) => {\n // If first element is a function, move it rightwards.\n if (typeof a === 'function') {\n return 1\n }\n\n // If second element is a function, move the first leftwards.\n if (typeof b === 'function') {\n return -1\n }\n\n // If both elements are strings, preserve their original index.\n if (typeof a === 'string' && typeof b === 'string') {\n return normalizedArgs.indexOf(a) - normalizedArgs.indexOf(b)\n }\n\n return 0\n })\n\n logger.info('normalized args', normalizedArgs)\n return normalizedArgs as NormalizedHttpRequestEndParams\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('http normalizeWriteArgs')\n\nexport type ClientRequestWriteCallback = (error?: Error | null) => void\nexport type ClientRequestWriteArgs = [\n chunk: string | Buffer,\n encoding?: BufferEncoding | ClientRequestWriteCallback,\n callback?: ClientRequestWriteCallback\n]\n\nexport type NormalizedClientRequestWriteArgs = [\n chunk: string | Buffer,\n encoding?: BufferEncoding,\n callback?: ClientRequestWriteCallback\n]\n\nexport function normalizeClientRequestWriteArgs(\n args: ClientRequestWriteArgs\n): NormalizedClientRequestWriteArgs {\n logger.info('normalizing ClientRequest.write arguments...', args)\n\n const chunk = args[0]\n const encoding =\n typeof args[1] === 'string' ? (args[1] as BufferEncoding) : undefined\n const callback = typeof args[1] === 'function' ? args[1] : args[2]\n\n const writeArgs: NormalizedClientRequestWriteArgs = [\n chunk,\n encoding,\n callback,\n ]\n logger.info(\n 'successfully normalized ClientRequest.write arguments:',\n writeArgs\n )\n\n return writeArgs\n}\n","import { IncomingMessage } from 'http'\nimport { PassThrough } from 'stream'\n\nexport const IS_CLONE = Symbol('isClone')\n\nexport interface ClonedIncomingMessage extends IncomingMessage {\n [IS_CLONE]: boolean\n}\n\n/**\n * Clones a given `http.IncomingMessage` instance.\n */\nexport function cloneIncomingMessage(\n message: IncomingMessage\n): ClonedIncomingMessage {\n const clone = message.pipe(new PassThrough())\n\n // Inherit all direct \"IncomingMessage\" properties.\n inheritProperties(message, clone)\n\n // Deeply inherit the message prototypes (Readable, Stream, EventEmitter, etc.).\n const clonedPrototype = Object.create(IncomingMessage.prototype)\n getPrototypes(clone).forEach((prototype) => {\n inheritProperties(prototype, clonedPrototype)\n })\n Object.setPrototypeOf(clone, clonedPrototype)\n\n Object.defineProperty(clone, IS_CLONE, {\n enumerable: true,\n value: true,\n })\n\n return clone as unknown as ClonedIncomingMessage\n}\n\n/**\n * Returns a list of all prototypes the given object extends.\n */\nfunction getPrototypes(source: object): object[] {\n const prototypes: object[] = []\n let current = source\n\n while ((current = Object.getPrototypeOf(current))) {\n prototypes.push(current)\n }\n\n return prototypes\n}\n\n/**\n * Inherits a given target object properties and symbols\n * onto the given source object.\n * @param source Object which should acquire properties.\n * @param target Object to inherit the properties from.\n */\nfunction inheritProperties(source: object, target: object): void {\n const properties = [\n ...Object.getOwnPropertyNames(source),\n ...Object.getOwnPropertySymbols(source),\n ]\n\n for (const property of properties) {\n if (target.hasOwnProperty(property)) {\n continue\n }\n\n const descriptor = Object.getOwnPropertyDescriptor(source, property)\n if (!descriptor) {\n continue\n }\n\n Object.defineProperty(target, property, descriptor)\n }\n}\n","import type { IncomingHttpHeaders, IncomingMessage } from 'http'\nimport { isResponseWithoutBody } from '../../../utils/responseUtils'\n\n/**\n * Creates a Fetch API `Response` instance from the given\n * `http.IncomingMessage` instance.\n */\nexport function createResponse(message: IncomingMessage): Response {\n const responseBodyOrNull = isResponseWithoutBody(message.statusCode || 200)\n ? null\n : new ReadableStream({\n start(controller) {\n message.on('data', (chunk) => controller.enqueue(chunk))\n message.on('end', () => controller.close())\n\n /**\n * @todo Should also listen to the \"error\" on the message\n * and forward it to the controller. Otherwise the stream\n * will pend indefinitely.\n */\n },\n })\n\n return new Response(responseBodyOrNull, {\n status: message.statusCode,\n statusText: message.statusMessage,\n headers: createHeadersFromIncomingHttpHeaders(message.headers),\n })\n}\n\nfunction createHeadersFromIncomingHttpHeaders(\n httpHeaders: IncomingHttpHeaders\n): Headers {\n const headers = new Headers()\n\n for (const headerName in httpHeaders) {\n const headerValues = httpHeaders[headerName]\n\n if (typeof headerValues === 'undefined') {\n continue\n }\n\n if (Array.isArray(headerValues)) {\n headerValues.forEach((headerValue) => {\n headers.append(headerName, headerValue)\n })\n\n continue\n }\n\n headers.set(headerName, headerValues)\n }\n\n return headers\n}\n","import type { NodeClientRequest } from '../NodeClientRequest'\n\n/**\n * Creates a Fetch API `Request` instance from the given `http.ClientRequest`.\n */\nexport function createRequest(clientRequest: NodeClientRequest): Request {\n const headers = new Headers()\n\n const outgoingHeaders = clientRequest.getHeaders()\n for (const headerName in outgoingHeaders) {\n const headerValue = outgoingHeaders[headerName]\n\n if (typeof headerValue === 'undefined') {\n continue\n }\n\n const valuesList = Array.prototype.concat([], headerValue)\n for (const value of valuesList) {\n headers.append(headerName, value.toString())\n }\n }\n\n /**\n * Translate the authentication from the request URL to\n * the request \"Authorization\" header.\n * @see https://github.com/mswjs/interceptors/issues/438\n */\n if (clientRequest.url.username || clientRequest.url.password) {\n const auth = `${clientRequest.url.username || ''}:${clientRequest.url.password || ''}`\n headers.set('Authorization', `Basic ${btoa(auth)}`)\n\n // Remove the credentials from the URL since you cannot\n // construct a Request instance with such a URL.\n clientRequest.url.username = ''\n clientRequest.url.password = ''\n }\n\n const method = clientRequest.method || 'GET'\n\n return new Request(clientRequest.url, {\n method,\n headers,\n credentials: 'same-origin',\n body:\n method === 'HEAD' || method === 'GET'\n ? null\n : clientRequest.requestBuffer,\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","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","import { getValueBySymbol } from './getValueBySymbol'\nimport { isObject } from './isObject'\n\ntype RawHeadersMap = Map<string, string>\ntype HeadersMapHeader = { name: string; value: string }\n\n/**\n * Returns raw headers from the given `Headers` instance.\n * @example\n * const headers = new Headers([\n * ['X-HeadeR-NamE', 'Value']\n * ])\n * getRawFetchHeaders(headers)\n * // { 'X-HeadeR-NamE': 'Value' }\n */\nexport function getRawFetchHeaders(\n headers: Headers\n): RawHeadersMap | undefined {\n const headersList = getValueBySymbol<object>('headers list', headers)\n\n if (!headersList) {\n return\n }\n\n const headersMap = getValueBySymbol<\n Map<string, string> | Map<string, HeadersMapHeader>\n >('headers map', headersList)\n\n /**\n * @note Older versions of Node.js (e.g. 18.8.0) keep headers map\n * as Map<normalizedHeaderName, value> without any means to tap\n * into raw header values. Detect that and return undefined.\n */\n if (!headersMap || !isHeadersMapWithRawHeaderNames(headersMap)) {\n return\n }\n\n // Raw headers is a map of { rawHeaderName: rawHeaderValue }\n const rawHeaders: RawHeadersMap = new Map<string, string>()\n\n headersMap.forEach(({ name, value }) => {\n rawHeaders.set(name, value)\n })\n\n return rawHeaders\n}\n\nfunction isHeadersMapWithRawHeaderNames(\n headersMap: Map<string, string> | Map<string, HeadersMapHeader>\n): headersMap is Map<string, HeadersMapHeader> {\n return Array.from(\n headersMap.values() as Iterable<string | HeadersMapHeader>\n ).every((value) => {\n return isObject<HeadersMapHeader>(value) && 'name' in value\n })\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import {\n Agent as HttpAgent,\n globalAgent as httpGlobalAgent,\n IncomingMessage,\n} from 'http'\nimport {\n RequestOptions,\n Agent as HttpsAgent,\n globalAgent as httpsGlobalAgent,\n} from 'https'\nimport { Url as LegacyURL, parse as parseUrl } from 'url'\nimport { Logger } from '@open-draft/logger'\nimport { getRequestOptionsByUrl } from '../../../utils/getRequestOptionsByUrl'\nimport {\n ResolvedRequestOptions,\n getUrlByRequestOptions,\n} from '../../../utils/getUrlByRequestOptions'\nimport { cloneObject } from '../../../utils/cloneObject'\nimport { isObject } from '../../../utils/isObject'\n\nconst logger = new Logger('http normalizeClientRequestArgs')\n\nexport type HttpRequestCallback = (response: IncomingMessage) => void\n\nexport type ClientRequestArgs =\n // Request without any arguments is also possible.\n | []\n | [string | URL | LegacyURL, HttpRequestCallback?]\n | [string | URL | LegacyURL, RequestOptions, HttpRequestCallback?]\n | [RequestOptions, HttpRequestCallback?]\n\nfunction resolveRequestOptions(\n args: ClientRequestArgs,\n url: URL\n): RequestOptions {\n // Calling `fetch` provides only URL to `ClientRequest`\n // without any `RequestOptions` or callback.\n if (typeof args[1] === 'undefined' || typeof args[1] === 'function') {\n logger.info('request options not provided, deriving from the url', url)\n return getRequestOptionsByUrl(url)\n }\n\n if (args[1]) {\n logger.info('has custom RequestOptions!', args[1])\n const requestOptionsFromUrl = getRequestOptionsByUrl(url)\n\n logger.info('derived RequestOptions from the URL:', requestOptionsFromUrl)\n\n /**\n * Clone the request options to lock their state\n * at the moment they are provided to `ClientRequest`.\n * @see https://github.com/mswjs/interceptors/issues/86\n */\n logger.info('cloning RequestOptions...')\n const clonedRequestOptions = cloneObject(args[1])\n logger.info('successfully cloned RequestOptions!', clonedRequestOptions)\n\n return {\n ...requestOptionsFromUrl,\n ...clonedRequestOptions,\n }\n }\n\n logger.info('using an empty object as request options')\n return {} as RequestOptions\n}\n\n/**\n * Overrides the given `URL` instance with the explicit properties provided\n * on the `RequestOptions` object. The options object takes precedence,\n * and will replace URL properties like \"host\", \"path\", and \"port\", if specified.\n */\nfunction overrideUrlByRequestOptions(url: URL, options: RequestOptions): URL {\n url.host = options.host || url.host\n url.hostname = options.hostname || url.hostname\n url.port = options.port ? options.port.toString() : url.port\n\n if (options.path) {\n const parsedOptionsPath = parseUrl(options.path, false)\n url.pathname = parsedOptionsPath.pathname || ''\n url.search = parsedOptionsPath.search || ''\n }\n\n return url\n}\n\nfunction resolveCallback(\n args: ClientRequestArgs\n): HttpRequestCallback | undefined {\n return typeof args[1] === 'function' ? args[1] : args[2]\n}\n\nexport type NormalizedClientRequestArgs = [\n url: URL,\n options: ResolvedRequestOptions,\n callback?: HttpRequestCallback\n]\n\n/**\n * Normalizes parameters given to a `http.request` call\n * so it always has a `URL` and `RequestOptions`.\n */\nexport function normalizeClientRequestArgs(\n defaultProtocol: string,\n ...args: ClientRequestArgs\n): NormalizedClientRequestArgs {\n let url: URL\n let options: ResolvedRequestOptions\n let callback: HttpRequestCallback | undefined\n\n logger.info('arguments', args)\n logger.info('using default protocol:', defaultProtocol)\n\n // Support \"http.request()\" calls without any arguments.\n // That call results in a \"GET http://localhost\" request.\n if (args.length === 0) {\n const url = new URL('http://localhost')\n const options = resolveRequestOptions(args, url)\n return [url, options]\n }\n\n // Convert a url string into a URL instance\n // and derive request options from it.\n if (typeof args[0] === 'string') {\n logger.info('first argument is a location string:', args[0])\n\n url = new URL(args[0])\n logger.info('created a url:', url)\n\n const requestOptionsFromUrl = getRequestOptionsByUrl(url)\n logger.info('request options from url:', requestOptionsFromUrl)\n\n options = resolveRequestOptions(args, url)\n logger.info('resolved request options:', options)\n\n callback = resolveCallback(args)\n }\n // Handle a given URL instance as-is\n // and derive request options from it.\n else if (args[0] instanceof URL) {\n url = args[0]\n logger.info('first argument is a URL:', url)\n\n // Check if the second provided argument is RequestOptions.\n // If it is, check if \"options.path\" was set and rewrite it\n // on the input URL.\n // Do this before resolving options from the URL below\n // to prevent query string from being duplicated in the path.\n if (typeof args[1] !== 'undefined' && isObject<RequestOptions>(args[1])) {\n url = overrideUrlByRequestOptions(url, args[1])\n }\n\n options = resolveRequestOptions(args, url)\n logger.info('derived request options:', options)\n\n callback = resolveCallback(args)\n }\n // Handle a legacy URL instance and re-normalize from either a RequestOptions object\n // or a WHATWG URL.\n else if ('hash' in args[0] && !('method' in args[0])) {\n const [legacyUrl] = args\n logger.info('first argument is a legacy URL:', legacyUrl)\n\n if (legacyUrl.hostname === null) {\n /**\n * We are dealing with a relative url, so use the path as an \"option\" and\n * merge in any existing options, giving priority to exising options -- i.e. a path in any\n * existing options will take precedence over the one contained in the url. This is consistent\n * with the behaviour in ClientRequest.\n * @see https://github.com/nodejs/node/blob/d84f1312915fe45fe0febe888db692c74894c382/lib/_http_client.js#L122\n */\n logger.info('given legacy URL is relative (no hostname)')\n\n return isObject(args[1])\n ? normalizeClientRequestArgs(\n defaultProtocol,\n { path: legacyUrl.path, ...args[1] },\n args[2]\n )\n : normalizeClientRequestArgs(\n defaultProtocol,\n { path: legacyUrl.path },\n args[1] as HttpRequestCallback\n )\n }\n\n logger.info('given legacy url is absolute')\n\n // We are dealing with an absolute URL, so convert to WHATWG and try again.\n const resolvedUrl = new URL(legacyUrl.href)\n\n return args[1] === undefined\n ? normalizeClientRequestArgs(defaultProtocol, resolvedUrl)\n : typeof args[1] === 'function'\n ? normalizeClientRequestArgs(defaultProtocol, resolvedUrl, args[1])\n : normalizeClientRequestArgs(\n defaultProtocol,\n resolvedUrl,\n args[1],\n args[2]\n )\n }\n // Handle a given \"RequestOptions\" object as-is\n // and derive the URL instance from it.\n else if (isObject(args[0])) {\n options = args[0] as any\n logger.info('first argument is RequestOptions:', options)\n\n // When handling a \"RequestOptions\" object without an explicit \"protocol\",\n // infer the protocol from the request issuing module (http/https).\n options.protocol = options.protocol || defaultProtocol\n logger.info('normalized request options:', options)\n\n url = getUrlByRequestOptions(options)\n logger.info('created a URL from RequestOptions:', url.href)\n\n callback = resolveCallback(args)\n } else {\n throw new Error(\n `Failed to construct ClientRequest with these parameters: ${args}`\n )\n }\n\n options.protocol = options.protocol || url.protocol\n options.method = options.method || 'GET'\n\n /**\n * Infer a fallback agent from the URL protocol.\n * The interception is done on the \"ClientRequest\" level (\"NodeClientRequest\")\n * and it may miss the correct agent. Always align the agent\n * with the URL protocol, if not provided.\n *\n * @note Respect the \"agent: false\" value.\n */\n if (typeof options.agent === 'undefined') {\n const agent =\n options.protocol === 'https:'\n ? new HttpsAgent({\n rejectUnauthorized: options.rejectUnauthorized,\n })\n : new HttpAgent()\n\n options.agent = agent\n logger.info('resolved fallback agent:', agent)\n }\n\n /**\n * Ensure that the default Agent is always set.\n * This prevents the protocol mismatch for requests with { agent: false },\n * where the global Agent is inferred.\n * @see https://github.com/mswjs/msw/issues/1150\n * @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L130\n * @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L157-L159\n */\n if (!options._defaultAgent) {\n logger.info(\n 'has no default agent, setting the default agent for \"%s\"',\n options.protocol\n )\n\n options._defaultAgent =\n options.protocol === 'https:' ? httpsGlobalAgent : httpGlobalAgent\n }\n\n logger.info('successfully resolved url:', url.href)\n logger.info('successfully resolved options:', options)\n logger.info('successfully resolved callback:', callback)\n\n return [url, options, callback]\n}\n","import { RequestOptions } from 'http'\n\n/**\n * Converts a URL instance into the RequestOptions object expected by\n * the `ClientRequest` class.\n * @see https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257\n */\nexport function getRequestOptionsByUrl(url: URL): RequestOptions {\n const options: RequestOptions = {\n method: 'GET',\n protocol: url.protocol,\n hostname:\n typeof url.hostname === 'string' && url.hostname.startsWith('[')\n ? url.hostname.slice(1, -1)\n : url.hostname,\n host: url.host,\n path: `${url.pathname}${url.search || ''}`,\n }\n\n if (!!url.port) {\n options.port = Number(url.port)\n }\n\n if (url.username || url.password) {\n options.auth = `${url.username}:${url.password}`\n }\n\n return options\n}\n","import { Agent } from 'http'\nimport { RequestOptions, Agent as HttpsAgent } from 'https'\nimport { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('utils getUrlByRequestOptions')\n\n// Request instance constructed by the \"request\" library\n// has a \"self\" property that has a \"uri\" field. This is\n// reproducible by performing a \"XMLHttpRequest\" request in JSDOM.\nexport interface RequestSelf {\n uri?: URL\n}\n\nexport type ResolvedRequestOptions = RequestOptions & RequestSelf\n\nexport const DEFAULT_PATH = '/'\nconst DEFAULT_PROTOCOL = 'http:'\nconst DEFAULT_HOSTNAME = 'localhost'\nconst SSL_PORT = 443\n\nfunction getAgent(\n options: ResolvedRequestOptions\n): Agent | HttpsAgent | undefined {\n return options.agent instanceof Agent ? options.agent : undefined\n}\n\nfunction getProtocolByRequestOptions(options: ResolvedRequestOptions): string {\n if (options.protocol) {\n return options.protocol\n }\n\n const agent = getAgent(options)\n const agentProtocol = (agent as RequestOptions)?.protocol\n\n if (agentProtocol) {\n return agentProtocol\n }\n\n const port = getPortByRequestOptions(options)\n const isSecureRequest = options.cert || port === SSL_PORT\n\n return isSecureRequest ? 'https:' : options.uri?.protocol || DEFAULT_PROTOCOL\n}\n\nfunction getPortByRequestOptions(\n options: ResolvedRequestOptions\n): number | undefined {\n // Use the explicitly provided port.\n if (options.port) {\n return Number(options.port)\n }\n\n // Otherwise, try to resolve port from the agent.\n const agent = getAgent(options)\n\n if ((agent as HttpsAgent)?.options.port) {\n return Number((agent as HttpsAgent).options.port)\n }\n\n if ((agent as RequestOptions)?.defaultPort) {\n return Number((agent as RequestOptions).defaultPort)\n }\n\n // Lastly, return undefined indicating that the port\n // must inferred from the protocol. Do not infer it here.\n return undefined\n}\n\ninterface RequestAuth {\n username: string\n password: string\n}\n\nfunction getAuthByRequestOptions(\n options: ResolvedRequestOptions\n): RequestAuth | undefined {\n if (options.auth) {\n const [username, password] = options.auth.split(':')\n return { username, password }\n }\n}\n\n/**\n * Returns true if host looks like an IPv6 address without surrounding brackets\n * It assumes any host containing `:` is definitely not IPv4 and probably IPv6,\n * but note that this could include invalid IPv6 addresses as well.\n */\nfunction isRawIPv6Address(host: string): boolean {\n return host.includes(':') && !host.startsWith('[') && !host.endsWith(']')\n}\n\nfunction getHostname(options: ResolvedRequestOptions): string | undefined {\n let host = options.hostname || options.host\n\n if (host) {\n if (isRawIPv6Address(host)) {\n host = `[${host}]`\n }\n\n // Check the presence of the port, and if it's present,\n // remove it from the host, returning a hostname.\n return new URL(`http://${host}`).hostname\n }\n\n return DEFAULT_HOSTNAME\n}\n\n/**\n * Creates a `URL` instance from a given `RequestOptions` object.\n */\nexport function getUrlByRequestOptions(options: ResolvedRequestOptions): URL {\n logger.info('request options', options)\n\n if (options.uri) {\n logger.info(\n 'constructing url from explicitly provided \"options.uri\": %s',\n options.uri\n )\n return new URL(options.uri.href)\n }\n\n logger.info('figuring out url from request options...')\n\n const protocol = getProtocolByRequestOptions(options)\n logger.info('protocol', protocol)\n\n const port = getPortByRequestOptions(options)\n logger.info('port', port)\n\n const hostname = getHostname(options)\n logger.info('hostname', hostname)\n\n const path = options.path || DEFAULT_PATH\n logger.info('path', path)\n\n const credentials = getAuthByRequestOptions(options)\n logger.info('credentials', credentials)\n\n const authString = credentials\n ? `${credentials.username}:${credentials.password}@`\n : ''\n logger.info('auth string:', authString)\n\n const portString = typeof port !== 'undefined' ? `:${port}` : ''\n const url = new URL(`${protocol}//${hostname}${portString}${path}`)\n url.username = credentials?.username || ''\n url.password = credentials?.password || ''\n\n logger.info('created url:', url)\n\n return url\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('cloneObject')\n\nfunction isPlainObject(obj?: Record<string, any>): boolean {\n logger.info('is plain object?', obj)\n\n if (obj == null || !obj.constructor?.name) {\n logger.info('given object is undefined, not a plain object...')\n return false\n }\n\n logger.info('checking the object constructor:', obj.constructor.name)\n return obj.constructor.name === 'Object'\n}\n\nexport function cloneObject<ObjectType extends Record<string, any>>(\n obj: ObjectType\n): ObjectType {\n logger.info('cloning object:', obj)\n\n const enumerableProperties = Object.entries(obj).reduce<Record<string, any>>(\n (acc, [key, value]) => {\n logger.info('analyzing key-value pair:', key, value)\n\n // Recursively clone only plain objects, omitting class instances.\n acc[key] = isPlainObject(value) ? cloneObject(value) : value\n return acc\n },\n {}\n )\n\n return isPlainObject(obj)\n ? enumerableProperties\n : Object.assign(Object.getPrototypeOf(obj), enumerableProperties)\n}\n","import { ClientRequest } from 'node:http'\nimport {\n NodeClientOptions,\n NodeClientRequest,\n Protocol,\n} from './NodeClientRequest'\nimport {\n ClientRequestArgs,\n normalizeClientRequestArgs,\n} from './utils/normalizeClientRequestArgs'\n\nexport function get(protocol: Protocol, options: NodeClientOptions) {\n return function interceptorsHttpGet(\n ...args: ClientRequestArgs\n ): ClientRequest {\n const clientRequestArgs = normalizeClientRequestArgs(\n `${protocol}:`,\n ...args\n )\n const request = new NodeClientRequest(clientRequestArgs, options)\n\n /**\n * @note https://nodejs.org/api/http.html#httpgetoptions-callback\n * \"http.get\" sets the method to \"GET\" and calls \"req.end()\" automatically.\n */\n request.end()\n\n return request\n }\n}\n","import { ClientRequest } from 'http'\nimport { Logger } from '@open-draft/logger'\nimport {\n NodeClientOptions,\n NodeClientRequest,\n Protocol,\n} from './NodeClientRequest'\nimport {\n normalizeClientRequestArgs,\n ClientRequestArgs,\n} from './utils/normalizeClientRequestArgs'\n\nconst logger = new Logger('http request')\n\nexport function request(protocol: Protocol, options: NodeClientOptions) {\n return function interceptorsHttpRequest(\n ...args: ClientRequestArgs\n ): ClientRequest {\n logger.info('request call (protocol \"%s\"):', protocol, args)\n\n const clientRequestArgs = normalizeClientRequestArgs(\n `${protocol}:`,\n ...args\n )\n return new NodeClientRequest(clientRequestArgs, options)\n }\n}\n"]}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isPropertyAccessible
|
|
3
|
-
} from "./chunk-DERTLGL3.mjs";
|
|
4
1
|
import {
|
|
5
2
|
emitAsync,
|
|
6
3
|
toInteractiveRequest
|
|
@@ -10,8 +7,9 @@ import {
|
|
|
10
7
|
Interceptor,
|
|
11
8
|
createRequestId,
|
|
12
9
|
createServerErrorResponse,
|
|
10
|
+
isResponseError,
|
|
13
11
|
isResponseWithoutBody
|
|
14
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-Q7POAM5N.mjs";
|
|
15
13
|
|
|
16
14
|
// src/interceptors/ClientRequest/index.ts
|
|
17
15
|
import http from "http";
|
|
@@ -181,8 +179,8 @@ function getValueBySymbol(symbolName, source) {
|
|
|
181
179
|
}
|
|
182
180
|
|
|
183
181
|
// src/utils/isObject.ts
|
|
184
|
-
function isObject(value) {
|
|
185
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
182
|
+
function isObject(value, loose = false) {
|
|
183
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
186
184
|
}
|
|
187
185
|
|
|
188
186
|
// src/utils/getRawFetchHeaders.ts
|
|
@@ -321,14 +319,37 @@ var _NodeClientRequest = class extends ClientRequest {
|
|
|
321
319
|
resolverResult.error
|
|
322
320
|
);
|
|
323
321
|
if (resolverResult.error instanceof Response) {
|
|
324
|
-
|
|
322
|
+
if (isResponseError(resolverResult.error)) {
|
|
323
|
+
this.logger.info(
|
|
324
|
+
"received network error response, erroring request..."
|
|
325
|
+
);
|
|
326
|
+
this.errorWith(new TypeError("Network error"));
|
|
327
|
+
} else {
|
|
328
|
+
this.respondWith(resolverResult.error);
|
|
329
|
+
}
|
|
325
330
|
return;
|
|
326
331
|
}
|
|
327
332
|
if (isNodeLikeError(resolverResult.error)) {
|
|
328
333
|
this.errorWith(resolverResult.error);
|
|
329
334
|
return this;
|
|
330
335
|
}
|
|
331
|
-
|
|
336
|
+
until(async () => {
|
|
337
|
+
if (this.emitter.listenerCount("unhandledException") > 0) {
|
|
338
|
+
await emitAsync(this.emitter, "unhandledException", {
|
|
339
|
+
error: resolverResult.error,
|
|
340
|
+
request: capturedRequest,
|
|
341
|
+
requestId,
|
|
342
|
+
controller: {
|
|
343
|
+
respondWith: this.respondWith.bind(this),
|
|
344
|
+
errorWith: this.errorWith.bind(this)
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
if (this.writableEnded || this.destroyed) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
this.respondWith(createServerErrorResponse(resolverResult.error));
|
|
352
|
+
});
|
|
332
353
|
return this;
|
|
333
354
|
}
|
|
334
355
|
const mockedResponse = resolverResult.data;
|
|
@@ -339,15 +360,7 @@ var _NodeClientRequest = class extends ClientRequest {
|
|
|
339
360
|
mockedResponse.statusText
|
|
340
361
|
);
|
|
341
362
|
this.destroyed = false;
|
|
342
|
-
if (
|
|
343
|
-
/**
|
|
344
|
-
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
345
|
-
* implement the "Response.type" property and throw on its access.
|
|
346
|
-
* Safely check if we can access "type" on "Response" before continuing.
|
|
347
|
-
* @see https://github.com/mswjs/msw/issues/1834
|
|
348
|
-
*/
|
|
349
|
-
isPropertyAccessible(mockedResponse, "type") && mockedResponse.type === "error"
|
|
350
|
-
) {
|
|
363
|
+
if (isResponseError(mockedResponse)) {
|
|
351
364
|
this.logger.info(
|
|
352
365
|
"received network error response, erroring request..."
|
|
353
366
|
);
|
|
@@ -579,7 +592,7 @@ import { Logger as Logger3 } from "@open-draft/logger";
|
|
|
579
592
|
var logger3 = new Logger3("utils getUrlByRequestOptions");
|
|
580
593
|
var DEFAULT_PATH = "/";
|
|
581
594
|
var DEFAULT_PROTOCOL = "http:";
|
|
582
|
-
var
|
|
595
|
+
var DEFAULT_HOSTNAME = "localhost";
|
|
583
596
|
var SSL_PORT = 443;
|
|
584
597
|
function getAgent(options) {
|
|
585
598
|
return options.agent instanceof Agent ? options.agent : void 0;
|
|
@@ -602,12 +615,6 @@ function getPortByRequestOptions(options) {
|
|
|
602
615
|
if (options.port) {
|
|
603
616
|
return Number(options.port);
|
|
604
617
|
}
|
|
605
|
-
if (options.hostname != null) {
|
|
606
|
-
const [, extractedPort] = options.hostname.match(/:(\d+)$/) || [];
|
|
607
|
-
if (extractedPort != null) {
|
|
608
|
-
return Number(extractedPort);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
618
|
const agent = getAgent(options);
|
|
612
619
|
if (agent == null ? void 0 : agent.options.port) {
|
|
613
620
|
return Number(agent.options.port);
|
|
@@ -617,13 +624,6 @@ function getPortByRequestOptions(options) {
|
|
|
617
624
|
}
|
|
618
625
|
return void 0;
|
|
619
626
|
}
|
|
620
|
-
function getHostByRequestOptions(options) {
|
|
621
|
-
const { hostname, host } = options;
|
|
622
|
-
if (hostname != null) {
|
|
623
|
-
return hostname.replace(/:\d+$/, "");
|
|
624
|
-
}
|
|
625
|
-
return host || DEFAULT_HOST;
|
|
626
|
-
}
|
|
627
627
|
function getAuthByRequestOptions(options) {
|
|
628
628
|
if (options.auth) {
|
|
629
629
|
const [username, password] = options.auth.split(":");
|
|
@@ -633,15 +633,15 @@ function getAuthByRequestOptions(options) {
|
|
|
633
633
|
function isRawIPv6Address(host) {
|
|
634
634
|
return host.includes(":") && !host.startsWith("[") && !host.endsWith("]");
|
|
635
635
|
}
|
|
636
|
-
function getHostname(
|
|
637
|
-
|
|
638
|
-
if (
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
return host;
|
|
636
|
+
function getHostname(options) {
|
|
637
|
+
let host = options.hostname || options.host;
|
|
638
|
+
if (host) {
|
|
639
|
+
if (isRawIPv6Address(host)) {
|
|
640
|
+
host = `[${host}]`;
|
|
641
|
+
}
|
|
642
|
+
return new URL(`http://${host}`).hostname;
|
|
643
643
|
}
|
|
644
|
-
return
|
|
644
|
+
return DEFAULT_HOSTNAME;
|
|
645
645
|
}
|
|
646
646
|
function getUrlByRequestOptions(options) {
|
|
647
647
|
logger3.info("request options", options);
|
|
@@ -655,11 +655,9 @@ function getUrlByRequestOptions(options) {
|
|
|
655
655
|
logger3.info("figuring out url from request options...");
|
|
656
656
|
const protocol = getProtocolByRequestOptions(options);
|
|
657
657
|
logger3.info("protocol", protocol);
|
|
658
|
-
const host = getHostByRequestOptions(options);
|
|
659
|
-
logger3.info("host", host);
|
|
660
658
|
const port = getPortByRequestOptions(options);
|
|
661
659
|
logger3.info("port", port);
|
|
662
|
-
const hostname = getHostname(
|
|
660
|
+
const hostname = getHostname(options);
|
|
663
661
|
logger3.info("hostname", hostname);
|
|
664
662
|
const path = options.path || DEFAULT_PATH;
|
|
665
663
|
logger3.info("path", path);
|
|
@@ -667,7 +665,8 @@ function getUrlByRequestOptions(options) {
|
|
|
667
665
|
logger3.info("credentials", credentials);
|
|
668
666
|
const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
|
|
669
667
|
logger3.info("auth string:", authString);
|
|
670
|
-
const
|
|
668
|
+
const portString = typeof port !== "undefined" ? `:${port}` : "";
|
|
669
|
+
const url = new URL(`${protocol}//${hostname}${portString}${path}`);
|
|
671
670
|
url.username = (credentials == null ? void 0 : credentials.username) || "";
|
|
672
671
|
url.password = (credentials == null ? void 0 : credentials.password) || "";
|
|
673
672
|
logger3.info("created url:", url);
|
|
@@ -885,4 +884,4 @@ ClientRequestInterceptor.interceptorSymbol = Symbol("http");
|
|
|
885
884
|
export {
|
|
886
885
|
ClientRequestInterceptor
|
|
887
886
|
};
|
|
888
|
-
//# sourceMappingURL=chunk-
|
|
887
|
+
//# sourceMappingURL=chunk-JMNEFEYU.mjs.map
|