@mswjs/interceptors 0.37.4 → 0.37.6

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.
Files changed (66) hide show
  1. package/lib/browser/{chunk-F4BN745U.mjs → chunk-5UK33FSU.mjs} +11 -1
  2. package/lib/browser/chunk-5UK33FSU.mjs.map +1 -0
  3. package/lib/browser/{chunk-CANMNIJA.js → chunk-BC2BLJQN.js} +11 -1
  4. package/lib/browser/chunk-BC2BLJQN.js.map +1 -0
  5. package/lib/browser/{chunk-7SL7RZNU.mjs → chunk-DODHRDV6.mjs} +2 -2
  6. package/lib/browser/{chunk-74GCTBGJ.js → chunk-QVOTKFTB.js} +8 -8
  7. package/lib/browser/{chunk-HER3NHBG.mjs → chunk-XTX2SIN6.mjs} +2 -2
  8. package/lib/browser/{chunk-UJNACJXF.js → chunk-ZIT2QX7D.js} +7 -7
  9. package/lib/browser/index.d.ts +4 -0
  10. package/lib/browser/index.js +2 -2
  11. package/lib/browser/index.mjs +1 -1
  12. package/lib/browser/interceptors/XMLHttpRequest/index.js +3 -3
  13. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +2 -2
  14. package/lib/browser/interceptors/fetch/index.js +3 -3
  15. package/lib/browser/interceptors/fetch/index.mjs +2 -2
  16. package/lib/browser/presets/browser.js +5 -5
  17. package/lib/browser/presets/browser.mjs +3 -3
  18. package/lib/node/RemoteHttpInterceptor.js +9 -9
  19. package/lib/node/RemoteHttpInterceptor.mjs +4 -4
  20. package/lib/node/{chunk-ZFSEKZ4Y.js → chunk-3HCE66HZ.js} +27 -25
  21. package/lib/node/chunk-3HCE66HZ.js.map +1 -0
  22. package/lib/node/{chunk-7XUH4W74.mjs → chunk-CTGTMEFD.mjs} +2 -2
  23. package/lib/node/{chunk-ZHMQHI2Z.mjs → chunk-FWJSC2QD.mjs} +20 -18
  24. package/lib/node/chunk-FWJSC2QD.mjs.map +1 -0
  25. package/lib/node/{chunk-ELLF26MW.mjs → chunk-I7HQIBT7.mjs} +11 -1
  26. package/lib/node/{chunk-ELLF26MW.mjs.map → chunk-I7HQIBT7.mjs.map} +1 -1
  27. package/lib/node/{chunk-XP2VS4IL.js → chunk-LCA4FKWY.js} +7 -7
  28. package/lib/node/{chunk-C2QGGUHP.js → chunk-LEA3MUU3.js} +7 -7
  29. package/lib/node/{chunk-WISH7OVU.mjs → chunk-OMWE7UVM.mjs} +2 -2
  30. package/lib/node/{chunk-3CPL565R.mjs → chunk-PJA4E426.mjs} +2 -2
  31. package/lib/node/{chunk-WGMDPBIX.js → chunk-WZTE4PCO.js} +11 -1
  32. package/lib/node/chunk-WZTE4PCO.js.map +1 -0
  33. package/lib/node/{chunk-YMPTXPHD.js → chunk-YBN5MFAP.js} +3 -3
  34. package/lib/node/index.d.ts +4 -0
  35. package/lib/node/index.js +3 -3
  36. package/lib/node/index.mjs +2 -2
  37. package/lib/node/interceptors/ClientRequest/index.js +3 -3
  38. package/lib/node/interceptors/ClientRequest/index.mjs +2 -2
  39. package/lib/node/interceptors/XMLHttpRequest/index.js +3 -3
  40. package/lib/node/interceptors/XMLHttpRequest/index.mjs +2 -2
  41. package/lib/node/interceptors/fetch/index.js +3 -3
  42. package/lib/node/interceptors/fetch/index.mjs +2 -2
  43. package/lib/node/presets/node.js +7 -7
  44. package/lib/node/presets/node.mjs +4 -4
  45. package/package.json +1 -1
  46. package/src/interceptors/ClientRequest/MockHttpSocket.ts +2 -3
  47. package/src/interceptors/ClientRequest/agents.ts +25 -8
  48. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +44 -0
  49. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +4 -1
  50. package/src/utils/fetchUtils.ts +11 -0
  51. package/lib/browser/chunk-CANMNIJA.js.map +0 -1
  52. package/lib/browser/chunk-F4BN745U.mjs.map +0 -1
  53. package/lib/node/chunk-WGMDPBIX.js.map +0 -1
  54. package/lib/node/chunk-ZFSEKZ4Y.js.map +0 -1
  55. package/lib/node/chunk-ZHMQHI2Z.mjs.map +0 -1
  56. package/src/interceptors/Socket/utils/parseRawHeaders.ts +0 -10
  57. /package/lib/browser/{chunk-7SL7RZNU.mjs.map → chunk-DODHRDV6.mjs.map} +0 -0
  58. /package/lib/browser/{chunk-74GCTBGJ.js.map → chunk-QVOTKFTB.js.map} +0 -0
  59. /package/lib/browser/{chunk-HER3NHBG.mjs.map → chunk-XTX2SIN6.mjs.map} +0 -0
  60. /package/lib/browser/{chunk-UJNACJXF.js.map → chunk-ZIT2QX7D.js.map} +0 -0
  61. /package/lib/node/{chunk-7XUH4W74.mjs.map → chunk-CTGTMEFD.mjs.map} +0 -0
  62. /package/lib/node/{chunk-XP2VS4IL.js.map → chunk-LCA4FKWY.js.map} +0 -0
  63. /package/lib/node/{chunk-C2QGGUHP.js.map → chunk-LEA3MUU3.js.map} +0 -0
  64. /package/lib/node/{chunk-WISH7OVU.mjs.map → chunk-OMWE7UVM.mjs.map} +0 -0
  65. /package/lib/node/{chunk-3CPL565R.mjs.map → chunk-PJA4E426.mjs.map} +0 -0
  66. /package/lib/node/{chunk-YMPTXPHD.js.map → chunk-YBN5MFAP.js.map} +0 -0
@@ -18,7 +18,7 @@ var _chunk6L3PFBGTjs = require('./chunk-6L3PFBGT.js');
18
18
 
19
19
 
20
20
 
21
- var _chunkWGMDPBIXjs = require('./chunk-WGMDPBIX.js');
21
+ var _chunkWZTE4PCOjs = require('./chunk-WZTE4PCO.js');
22
22
 
23
23
  // src/interceptors/XMLHttpRequest/index.ts
24
24
  var _outvariant = require('outvariant');
@@ -206,8 +206,8 @@ function parseJson(data) {
206
206
 
207
207
  // src/interceptors/XMLHttpRequest/utils/createResponse.ts
208
208
  function createResponse(request, body) {
209
- const responseBodyOrNull = _chunkWGMDPBIXjs.FetchResponse.isResponseWithBody(request.status) ? body : null;
210
- return new (0, _chunkWGMDPBIXjs.FetchResponse)(responseBodyOrNull, {
209
+ const responseBodyOrNull = _chunkWZTE4PCOjs.FetchResponse.isResponseWithBody(request.status) ? body : null;
210
+ return new (0, _chunkWZTE4PCOjs.FetchResponse)(responseBodyOrNull, {
211
211
  url: request.responseURL,
212
212
  status: request.status,
213
213
  statusText: request.statusText,
@@ -253,7 +253,7 @@ var XMLHttpRequestController = class {
253
253
  this[kIsRequestHandled] = false;
254
254
  this.events = /* @__PURE__ */ new Map();
255
255
  this.uploadEvents = /* @__PURE__ */ new Map();
256
- this.requestId = _chunkWGMDPBIXjs.createRequestId.call(void 0, );
256
+ this.requestId = _chunkWZTE4PCOjs.createRequestId.call(void 0, );
257
257
  this.requestHeaders = new Headers();
258
258
  this.responseBuffer = new Uint8Array();
259
259
  this.request = createProxy(initialRequest, {
@@ -335,7 +335,7 @@ var XMLHttpRequestController = class {
335
335
  );
336
336
  if (IS_NODE) {
337
337
  this.request.setRequestHeader(
338
- _chunkWGMDPBIXjs.INTERNAL_REQUEST_ID_HEADER_NAME,
338
+ _chunkWZTE4PCOjs.INTERNAL_REQUEST_ID_HEADER_NAME,
339
339
  this.requestId
340
340
  );
341
341
  }
@@ -795,7 +795,7 @@ function createXMLHttpRequestProxy({
795
795
  }
796
796
 
797
797
  // src/interceptors/XMLHttpRequest/index.ts
798
- var _XMLHttpRequestInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
798
+ var _XMLHttpRequestInterceptor = class extends _chunkWZTE4PCOjs.Interceptor {
799
799
  constructor() {
800
800
  super(_XMLHttpRequestInterceptor.interceptorSymbol);
801
801
  }
@@ -841,4 +841,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
841
841
 
842
842
 
843
843
  exports.XMLHttpRequestInterceptor = XMLHttpRequestInterceptor;
844
- //# sourceMappingURL=chunk-XP2VS4IL.js.map
844
+ //# sourceMappingURL=chunk-LCA4FKWY.js.map
@@ -13,7 +13,7 @@ var _chunk6L3PFBGTjs = require('./chunk-6L3PFBGT.js');
13
13
 
14
14
 
15
15
 
16
- var _chunkWGMDPBIXjs = require('./chunk-WGMDPBIX.js');
16
+ var _chunkWZTE4PCOjs = require('./chunk-WZTE4PCO.js');
17
17
 
18
18
  // src/interceptors/fetch/index.ts
19
19
  var _outvariant = require('outvariant');
@@ -185,7 +185,7 @@ function decompressResponse(response) {
185
185
  }
186
186
 
187
187
  // src/interceptors/fetch/index.ts
188
- var _FetchInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
188
+ var _FetchInterceptor = class extends _chunkWZTE4PCOjs.Interceptor {
189
189
  constructor() {
190
190
  super(_FetchInterceptor.symbol);
191
191
  }
@@ -199,7 +199,7 @@ var _FetchInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
199
199
  'Failed to patch the "fetch" module: already patched.'
200
200
  );
201
201
  globalThis.fetch = async (input, init) => {
202
- const requestId = _chunkWGMDPBIXjs.createRequestId.call(void 0, );
202
+ const requestId = _chunkWZTE4PCOjs.createRequestId.call(void 0, );
203
203
  const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
204
204
  const request = new Request(resolvedInput, init);
205
205
  const responsePromise = new (0, _deferredpromise.DeferredPromise)();
@@ -220,9 +220,9 @@ var _FetchInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
220
220
  rawResponse
221
221
  });
222
222
  const decompressedStream = decompressResponse(rawResponse);
223
- const response = decompressedStream === null ? rawResponse : new (0, _chunkWGMDPBIXjs.FetchResponse)(decompressedStream, rawResponse);
224
- _chunkWGMDPBIXjs.FetchResponse.setUrl(request.url, response);
225
- if (_chunkWGMDPBIXjs.FetchResponse.isRedirectResponse(response.status)) {
223
+ const response = decompressedStream === null ? rawResponse : new (0, _chunkWZTE4PCOjs.FetchResponse)(decompressedStream, rawResponse);
224
+ _chunkWZTE4PCOjs.FetchResponse.setUrl(request.url, response);
225
+ if (_chunkWZTE4PCOjs.FetchResponse.isRedirectResponse(response.status)) {
226
226
  if (request.redirect === "error") {
227
227
  responsePromise.reject(createNetworkError("unexpected redirect"));
228
228
  return;
@@ -307,4 +307,4 @@ FetchInterceptor.symbol = Symbol("fetch");
307
307
 
308
308
 
309
309
  exports.FetchInterceptor = FetchInterceptor;
310
- //# sourceMappingURL=chunk-C2QGGUHP.js.map
310
+ //# sourceMappingURL=chunk-LEA3MUU3.js.map
@@ -18,7 +18,7 @@ import {
18
18
  INTERNAL_REQUEST_ID_HEADER_NAME,
19
19
  Interceptor,
20
20
  createRequestId
21
- } from "./chunk-ELLF26MW.mjs";
21
+ } from "./chunk-I7HQIBT7.mjs";
22
22
 
23
23
  // src/interceptors/XMLHttpRequest/index.ts
24
24
  import { invariant as invariant2 } from "outvariant";
@@ -841,4 +841,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
841
841
  export {
842
842
  XMLHttpRequestInterceptor
843
843
  };
844
- //# sourceMappingURL=chunk-WISH7OVU.mjs.map
844
+ //# sourceMappingURL=chunk-OMWE7UVM.mjs.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Interceptor
3
- } from "./chunk-ELLF26MW.mjs";
3
+ } from "./chunk-I7HQIBT7.mjs";
4
4
 
5
5
  // src/BatchInterceptor.ts
6
6
  var BatchInterceptor = class extends Interceptor {
@@ -48,4 +48,4 @@ var BatchInterceptor = class extends Interceptor {
48
48
  export {
49
49
  BatchInterceptor
50
50
  };
51
- //# sourceMappingURL=chunk-3CPL565R.mjs.map
51
+ //# sourceMappingURL=chunk-PJA4E426.mjs.map
@@ -187,6 +187,16 @@ var _FetchResponse = class extends Response {
187
187
  writable: false
188
188
  });
189
189
  }
190
+ /**
191
+ * Parses the given raw HTTP headers into a Fetch API `Headers` instance.
192
+ */
193
+ static parseRawHeaders(rawHeaders) {
194
+ const headers = new Headers();
195
+ for (let line = 0; line < rawHeaders.length; line += 2) {
196
+ headers.append(rawHeaders[line], rawHeaders[line + 1]);
197
+ }
198
+ return headers;
199
+ }
190
200
  constructor(body, init = {}) {
191
201
  var _a;
192
202
  const status = (_a = init.status) != null ? _a : 200;
@@ -232,4 +242,4 @@ FetchResponse.STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308];
232
242
 
233
243
 
234
244
  exports.INTERNAL_REQUEST_ID_HEADER_NAME = INTERNAL_REQUEST_ID_HEADER_NAME; exports.getGlobalSymbol = getGlobalSymbol; exports.deleteGlobalSymbol = deleteGlobalSymbol; exports.InterceptorReadyState = InterceptorReadyState; exports.Interceptor = Interceptor; exports.createRequestId = createRequestId; exports.FetchResponse = FetchResponse;
235
- //# sourceMappingURL=chunk-WGMDPBIX.js.map
245
+ //# sourceMappingURL=chunk-WZTE4PCO.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Interceptor.ts","../../src/createRequestId.ts","../../src/utils/fetchUtils.ts"],"names":["InterceptorReadyState"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,eAAyB;AAY3B,IAAM,kCACX;AAEK,SAAS,gBAAmB,QAA+B;AAChE;AAAA;AAAA,IAEE,WAAW,MAAM,KAAK;AAAA;AAE1B;AAEA,SAAS,gBAAgB,QAAgB,OAAkB;AAEzD,aAAW,MAAM,IAAI;AACvB;AAEO,SAAS,mBAAmB,QAAsB;AAEvD,SAAO,WAAW,MAAM;AAC1B;AAEO,IAAK,wBAAL,kBAAKA,2BAAL;AACL,EAAAA,uBAAA,cAAW;AACX,EAAAA,uBAAA,cAAW;AACX,EAAAA,uBAAA,aAAU;AACV,EAAAA,uBAAA,eAAY;AACZ,EAAAA,uBAAA,cAAW;AALD,SAAAA;AAAA,GAAA;AAWL,IAAM,cAAN,MAAsD;AAAA,EAO3D,YAA6B,QAAgB;AAAhB;AAC3B,SAAK,aAAa;AAElB,SAAK,UAAU,IAAI,QAAQ;AAC3B,SAAK,gBAAgB,CAAC;AACtB,SAAK,SAAS,IAAI,OAAO,OAAO,WAAY;AAI5C,SAAK,QAAQ,gBAAgB,CAAC;AAE9B,SAAK,OAAO,KAAK,iCAAiC;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,mBAA4B;AACpC,WAA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O,gBAAgB;AAAA,MACrC;AACA,UAAI,aAAa;AACf,cAAM,QAAQ,QAAQ,IAAI,MAAM,WAAW;AAC3C,gBAAQ,IAAI,OAAO,UAAU,MAAM;AAAA,MACrC,OAAO;AACL,eAAO,eAAe,MAAM,UAAU;AAAA,UACpC,OAAO;AAAA,UACP,YAAY;AAAA,UACZ,cAAc;AAAA,UACd,UAAU;AAAA,QACZ,CAAC;AAAA,MACH;AAAA,IACF;AAEA,mBAAc,OAAO,KAAK,KAAK,IAAI;AAAA,EACrC;AACF;AAxFO,IAAM,gBAAN;AAAA;AAAA;AAAA;AAAA;AAAM,cAKK,4BAA4B,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG;AALzD,cAOK,6BAA6B,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG","sourcesContent":["import { Logger } from '@open-draft/logger'\nimport { Emitter, Listener } from 'strict-event-emitter'\n\nexport type InterceptorEventMap = Record<string, any>\nexport type InterceptorSubscription = () => void\n\n/**\n * Request header name to detect when a single request\n * is being handled by nested interceptors (XHR -> ClientRequest).\n * Obscure by design to prevent collisions with user-defined headers.\n * Ideally, come up with the Interceptor-level mechanism for this.\n * @see https://github.com/mswjs/interceptors/issues/378\n */\nexport const INTERNAL_REQUEST_ID_HEADER_NAME =\n 'x-interceptors-internal-request-id'\n\nexport function getGlobalSymbol<V>(symbol: Symbol): V | undefined {\n return (\n // @ts-ignore https://github.com/Microsoft/TypeScript/issues/24587\n globalThis[symbol] || undefined\n )\n}\n\nfunction setGlobalSymbol(symbol: Symbol, value: any): void {\n // @ts-ignore\n globalThis[symbol] = value\n}\n\nexport function deleteGlobalSymbol(symbol: Symbol): void {\n // @ts-ignore\n delete globalThis[symbol]\n}\n\nexport enum InterceptorReadyState {\n INACTIVE = 'INACTIVE',\n APPLYING = 'APPLYING',\n APPLIED = 'APPLIED',\n DISPOSING = 'DISPOSING',\n DISPOSED = 'DISPOSED',\n}\n\nexport type ExtractEventNames<Events extends Record<string, any>> =\n Events extends Record<infer EventName, any> ? EventName : never\n\nexport class Interceptor<Events extends InterceptorEventMap> {\n protected emitter: Emitter<Events>\n protected subscriptions: Array<InterceptorSubscription>\n protected logger: Logger\n\n public readyState: InterceptorReadyState\n\n constructor(private readonly symbol: symbol) {\n this.readyState = InterceptorReadyState.INACTIVE\n\n this.emitter = new Emitter()\n this.subscriptions = []\n this.logger = new Logger(symbol.description!)\n\n // Do not limit the maximum number of listeners\n // so not to limit the maximum amount of parallel events emitted.\n this.emitter.setMaxListeners(0)\n\n this.logger.info('constructing the interceptor...')\n }\n\n /**\n * Determine if this interceptor can be applied\n * in the current environment.\n */\n protected checkEnvironment(): boolean {\n return true\n }\n\n /**\n * Apply this interceptor to the current process.\n * Returns an already running interceptor instance if it's present.\n */\n public apply(): void {\n const logger = this.logger.extend('apply')\n logger.info('applying the interceptor...')\n\n if (this.readyState === InterceptorReadyState.APPLIED) {\n logger.info('intercepted already applied!')\n return\n }\n\n const shouldApply = this.checkEnvironment()\n\n if (!shouldApply) {\n logger.info('the interceptor cannot be applied in this environment!')\n return\n }\n\n this.readyState = InterceptorReadyState.APPLYING\n\n // Whenever applying a new interceptor, check if it hasn't been applied already.\n // This enables to apply the same interceptor multiple times, for example from a different\n // interceptor, only proxying events but keeping the stubs in a single place.\n const runningInstance = this.getInstance()\n\n if (runningInstance) {\n logger.info('found a running instance, reusing...')\n\n // Proxy any listeners you set on this instance to the running instance.\n this.on = (event, listener) => {\n logger.info('proxying the \"%s\" listener', event)\n\n // Add listeners to the running instance so they appear\n // at the top of the event listeners list and are executed first.\n runningInstance.emitter.addListener(event, listener)\n\n // Ensure that once this interceptor instance is disposed,\n // it removes all listeners it has appended to the running interceptor instance.\n this.subscriptions.push(() => {\n runningInstance.emitter.removeListener(event, listener)\n logger.info('removed proxied \"%s\" listener!', event)\n })\n\n return this\n }\n\n this.readyState = InterceptorReadyState.APPLIED\n\n return\n }\n\n logger.info('no running instance found, setting up a new instance...')\n\n // Setup the interceptor.\n this.setup()\n\n // Store the newly applied interceptor instance globally.\n this.setInstance()\n\n this.readyState = InterceptorReadyState.APPLIED\n }\n\n /**\n * Setup the module augments and stubs necessary for this interceptor.\n * This method is not run if there's a running interceptor instance\n * to prevent instantiating an interceptor multiple times.\n */\n protected setup(): void {}\n\n /**\n * Listen to the interceptor's public events.\n */\n public on<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n const logger = this.logger.extend('on')\n\n if (\n this.readyState === InterceptorReadyState.DISPOSING ||\n this.readyState === InterceptorReadyState.DISPOSED\n ) {\n logger.info('cannot listen to events, already disposed!')\n return this\n }\n\n logger.info('adding \"%s\" event listener:', event, listener)\n\n this.emitter.on(event, listener)\n return this\n }\n\n public once<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n this.emitter.once(event, listener)\n return this\n }\n\n public off<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n this.emitter.off(event, listener)\n return this\n }\n\n public removeAllListeners<EventName extends ExtractEventNames<Events>>(\n event?: EventName\n ): this {\n this.emitter.removeAllListeners(event)\n return this\n }\n\n /**\n * Disposes of any side-effects this interceptor has introduced.\n */\n public dispose(): void {\n const logger = this.logger.extend('dispose')\n\n if (this.readyState === InterceptorReadyState.DISPOSED) {\n logger.info('cannot dispose, already disposed!')\n return\n }\n\n logger.info('disposing the interceptor...')\n this.readyState = InterceptorReadyState.DISPOSING\n\n if (!this.getInstance()) {\n logger.info('no interceptors running, skipping dispose...')\n return\n }\n\n // Delete the global symbol as soon as possible,\n // indicating that the interceptor is no longer running.\n this.clearInstance()\n\n logger.info('global symbol deleted:', getGlobalSymbol(this.symbol))\n\n if (this.subscriptions.length > 0) {\n logger.info('disposing of %d subscriptions...', this.subscriptions.length)\n\n for (const dispose of this.subscriptions) {\n dispose()\n }\n\n this.subscriptions = []\n\n logger.info('disposed of all subscriptions!', this.subscriptions.length)\n }\n\n this.emitter.removeAllListeners()\n logger.info('destroyed the listener!')\n\n this.readyState = InterceptorReadyState.DISPOSED\n }\n\n private getInstance(): this | undefined {\n const instance = getGlobalSymbol<this>(this.symbol)\n this.logger.info('retrieved global instance:', instance?.constructor?.name)\n return instance\n }\n\n private setInstance(): void {\n setGlobalSymbol(this.symbol, this)\n this.logger.info('set global instance!', this.symbol.description)\n }\n\n private clearInstance(): void {\n deleteGlobalSymbol(this.symbol)\n this.logger.info('cleared global instance!', this.symbol.description)\n }\n}\n","/**\n * Generate a random ID string to represent a request.\n * @example\n * createRequestId()\n * // \"f774b6c9c600f\"\n */\nexport function createRequestId(): string {\n return Math.random().toString(16).slice(2)\n}\n","export interface FetchResponseInit extends ResponseInit {\n url?: string\n}\n\nexport class FetchResponse extends Response {\n /**\n * Response status codes for responses that cannot have body.\n * @see https://fetch.spec.whatwg.org/#statuses\n */\n static readonly STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304]\n\n static readonly STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308]\n\n static isConfigurableStatusCode(status: number): boolean {\n return status >= 200 && status <= 599\n }\n\n static isRedirectResponse(status: number): boolean {\n return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status)\n }\n\n /**\n * Returns a boolean indicating whether the given response status\n * code represents a response that can have a body.\n */\n static isResponseWithBody(status: number): boolean {\n return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)\n }\n\n static setUrl(url: string | undefined, response: Response): void {\n if (!url) {\n return\n }\n\n if (response.url != '') {\n return\n }\n\n Object.defineProperty(response, 'url', {\n value: url,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n\n /**\n * Parses the given raw HTTP headers into a Fetch API `Headers` instance.\n */\n static parseRawHeaders(rawHeaders: Array<string>): Headers {\n const headers = new Headers()\n for (let line = 0; line < rawHeaders.length; line += 2) {\n headers.append(rawHeaders[line], rawHeaders[line + 1])\n }\n return headers\n }\n\n constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {\n const status = init.status ?? 200\n const safeStatus = FetchResponse.isConfigurableStatusCode(status)\n ? status\n : 200\n const finalBody = FetchResponse.isResponseWithBody(status) ? body : null\n\n super(finalBody, {\n ...init,\n status: safeStatus,\n })\n\n if (status !== safeStatus) {\n /**\n * @note Undici keeps an internal \"Symbol(state)\" that holds\n * the actual value of response status. Update that in Node.js.\n */\n const stateSymbol = Object.getOwnPropertySymbols(this).find(\n (symbol) => symbol.description === 'state'\n )\n if (stateSymbol) {\n const state = Reflect.get(this, stateSymbol) as object\n Reflect.set(state, 'status', status)\n } else {\n Object.defineProperty(this, 'status', {\n value: status,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n }\n\n FetchResponse.setUrl(init.url, this)\n }\n}\n"]}
@@ -1,9 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkWGMDPBIXjs = require('./chunk-WGMDPBIX.js');
3
+ var _chunkWZTE4PCOjs = require('./chunk-WZTE4PCO.js');
4
4
 
5
5
  // src/BatchInterceptor.ts
6
- var BatchInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
6
+ var BatchInterceptor = class extends _chunkWZTE4PCOjs.Interceptor {
7
7
  constructor(options) {
8
8
  BatchInterceptor.symbol = Symbol(options.name);
9
9
  super(BatchInterceptor.symbol);
@@ -48,4 +48,4 @@ var BatchInterceptor = class extends _chunkWGMDPBIXjs.Interceptor {
48
48
 
49
49
 
50
50
  exports.BatchInterceptor = BatchInterceptor;
51
- //# sourceMappingURL=chunk-YMPTXPHD.js.map
51
+ //# sourceMappingURL=chunk-YBN5MFAP.js.map
@@ -38,6 +38,10 @@ declare class FetchResponse extends Response {
38
38
  */
39
39
  static isResponseWithBody(status: number): boolean;
40
40
  static setUrl(url: string | undefined, response: Response): void;
41
+ /**
42
+ * Parses the given raw HTTP headers into a Fetch API `Headers` instance.
43
+ */
44
+ static parseRawHeaders(rawHeaders: Array<string>): Headers;
41
45
  constructor(body?: BodyInit | null, init?: FetchResponseInit);
42
46
  }
43
47
 
package/lib/node/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkYMPTXPHDjs = require('./chunk-YMPTXPHD.js');
3
+ var _chunkYBN5MFAPjs = require('./chunk-YBN5MFAP.js');
4
4
 
5
5
 
6
6
 
@@ -16,7 +16,7 @@ var _chunk73NOP3T5js = require('./chunk-73NOP3T5.js');
16
16
 
17
17
 
18
18
 
19
- var _chunkWGMDPBIXjs = require('./chunk-WGMDPBIX.js');
19
+ var _chunkWZTE4PCOjs = require('./chunk-WZTE4PCO.js');
20
20
 
21
21
  // src/utils/getCleanUrl.ts
22
22
  function getCleanUrl(url, isAbsolute = true) {
@@ -35,5 +35,5 @@ function getCleanUrl(url, isAbsolute = true) {
35
35
 
36
36
 
37
37
 
38
- exports.BatchInterceptor = _chunkYMPTXPHDjs.BatchInterceptor; exports.FetchResponse = _chunkWGMDPBIXjs.FetchResponse; exports.INTERNAL_REQUEST_ID_HEADER_NAME = _chunkWGMDPBIXjs.INTERNAL_REQUEST_ID_HEADER_NAME; exports.IS_PATCHED_MODULE = _chunk73NOP3T5js.IS_PATCHED_MODULE; exports.Interceptor = _chunkWGMDPBIXjs.Interceptor; exports.InterceptorReadyState = _chunkWGMDPBIXjs.InterceptorReadyState; exports.createRequestId = _chunkWGMDPBIXjs.createRequestId; exports.decodeBuffer = _chunkLK6DILFKjs.decodeBuffer; exports.deleteGlobalSymbol = _chunkWGMDPBIXjs.deleteGlobalSymbol; exports.encodeBuffer = _chunkLK6DILFKjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunkWGMDPBIXjs.getGlobalSymbol;
38
+ exports.BatchInterceptor = _chunkYBN5MFAPjs.BatchInterceptor; exports.FetchResponse = _chunkWZTE4PCOjs.FetchResponse; exports.INTERNAL_REQUEST_ID_HEADER_NAME = _chunkWZTE4PCOjs.INTERNAL_REQUEST_ID_HEADER_NAME; exports.IS_PATCHED_MODULE = _chunk73NOP3T5js.IS_PATCHED_MODULE; exports.Interceptor = _chunkWZTE4PCOjs.Interceptor; exports.InterceptorReadyState = _chunkWZTE4PCOjs.InterceptorReadyState; exports.createRequestId = _chunkWZTE4PCOjs.createRequestId; exports.decodeBuffer = _chunkLK6DILFKjs.decodeBuffer; exports.deleteGlobalSymbol = _chunkWZTE4PCOjs.deleteGlobalSymbol; exports.encodeBuffer = _chunkLK6DILFKjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunkWZTE4PCOjs.getGlobalSymbol;
39
39
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  BatchInterceptor
3
- } from "./chunk-3CPL565R.mjs";
3
+ } from "./chunk-PJA4E426.mjs";
4
4
  import {
5
5
  decodeBuffer,
6
6
  encodeBuffer
@@ -16,7 +16,7 @@ import {
16
16
  createRequestId,
17
17
  deleteGlobalSymbol,
18
18
  getGlobalSymbol
19
- } from "./chunk-ELLF26MW.mjs";
19
+ } from "./chunk-I7HQIBT7.mjs";
20
20
 
21
21
  // src/utils/getCleanUrl.ts
22
22
  function getCleanUrl(url, isAbsolute = true) {
@@ -1,9 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkZFSEKZ4Yjs = require('../../chunk-ZFSEKZ4Y.js');
3
+ var _chunk3HCE66HZjs = require('../../chunk-3HCE66HZ.js');
4
4
  require('../../chunk-6L3PFBGT.js');
5
- require('../../chunk-WGMDPBIX.js');
5
+ require('../../chunk-WZTE4PCO.js');
6
6
 
7
7
 
8
- exports.ClientRequestInterceptor = _chunkZFSEKZ4Yjs.ClientRequestInterceptor;
8
+ exports.ClientRequestInterceptor = _chunk3HCE66HZjs.ClientRequestInterceptor;
9
9
  //# sourceMappingURL=index.js.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  ClientRequestInterceptor
3
- } from "../../chunk-ZHMQHI2Z.mjs";
3
+ } from "../../chunk-FWJSC2QD.mjs";
4
4
  import "../../chunk-5KMS5CTP.mjs";
5
- import "../../chunk-ELLF26MW.mjs";
5
+ import "../../chunk-I7HQIBT7.mjs";
6
6
  export {
7
7
  ClientRequestInterceptor
8
8
  };
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkXP2VS4ILjs = require('../../chunk-XP2VS4IL.js');
3
+ var _chunkLCA4FKWYjs = require('../../chunk-LCA4FKWY.js');
4
4
  require('../../chunk-LK6DILFK.js');
5
5
  require('../../chunk-PFGO5BSM.js');
6
6
  require('../../chunk-73NOP3T5.js');
7
7
  require('../../chunk-6L3PFBGT.js');
8
- require('../../chunk-WGMDPBIX.js');
8
+ require('../../chunk-WZTE4PCO.js');
9
9
 
10
10
 
11
- exports.XMLHttpRequestInterceptor = _chunkXP2VS4ILjs.XMLHttpRequestInterceptor;
11
+ exports.XMLHttpRequestInterceptor = _chunkLCA4FKWYjs.XMLHttpRequestInterceptor;
12
12
  //# sourceMappingURL=index.js.map
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  XMLHttpRequestInterceptor
3
- } from "../../chunk-WISH7OVU.mjs";
3
+ } from "../../chunk-OMWE7UVM.mjs";
4
4
  import "../../chunk-6HYIRFX2.mjs";
5
5
  import "../../chunk-TX5GBTFY.mjs";
6
6
  import "../../chunk-6YM4PLBI.mjs";
7
7
  import "../../chunk-5KMS5CTP.mjs";
8
- import "../../chunk-ELLF26MW.mjs";
8
+ import "../../chunk-I7HQIBT7.mjs";
9
9
  export {
10
10
  XMLHttpRequestInterceptor
11
11
  };
@@ -1,11 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkC2QGGUHPjs = require('../../chunk-C2QGGUHP.js');
3
+ var _chunkLEA3MUU3js = require('../../chunk-LEA3MUU3.js');
4
4
  require('../../chunk-PFGO5BSM.js');
5
5
  require('../../chunk-73NOP3T5.js');
6
6
  require('../../chunk-6L3PFBGT.js');
7
- require('../../chunk-WGMDPBIX.js');
7
+ require('../../chunk-WZTE4PCO.js');
8
8
 
9
9
 
10
- exports.FetchInterceptor = _chunkC2QGGUHPjs.FetchInterceptor;
10
+ exports.FetchInterceptor = _chunkLEA3MUU3js.FetchInterceptor;
11
11
  //# sourceMappingURL=index.js.map
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  FetchInterceptor
3
- } from "../../chunk-7XUH4W74.mjs";
3
+ } from "../../chunk-CTGTMEFD.mjs";
4
4
  import "../../chunk-TX5GBTFY.mjs";
5
5
  import "../../chunk-6YM4PLBI.mjs";
6
6
  import "../../chunk-5KMS5CTP.mjs";
7
- import "../../chunk-ELLF26MW.mjs";
7
+ import "../../chunk-I7HQIBT7.mjs";
8
8
  export {
9
9
  FetchInterceptor
10
10
  };
@@ -1,23 +1,23 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkZFSEKZ4Yjs = require('../chunk-ZFSEKZ4Y.js');
3
+ var _chunk3HCE66HZjs = require('../chunk-3HCE66HZ.js');
4
4
 
5
5
 
6
- var _chunkXP2VS4ILjs = require('../chunk-XP2VS4IL.js');
6
+ var _chunkLCA4FKWYjs = require('../chunk-LCA4FKWY.js');
7
7
  require('../chunk-LK6DILFK.js');
8
8
 
9
9
 
10
- var _chunkC2QGGUHPjs = require('../chunk-C2QGGUHP.js');
10
+ var _chunkLEA3MUU3js = require('../chunk-LEA3MUU3.js');
11
11
  require('../chunk-PFGO5BSM.js');
12
12
  require('../chunk-73NOP3T5.js');
13
13
  require('../chunk-6L3PFBGT.js');
14
- require('../chunk-WGMDPBIX.js');
14
+ require('../chunk-WZTE4PCO.js');
15
15
 
16
16
  // src/presets/node.ts
17
17
  var node_default = [
18
- new (0, _chunkZFSEKZ4Yjs.ClientRequestInterceptor)(),
19
- new (0, _chunkXP2VS4ILjs.XMLHttpRequestInterceptor)(),
20
- new (0, _chunkC2QGGUHPjs.FetchInterceptor)()
18
+ new (0, _chunk3HCE66HZjs.ClientRequestInterceptor)(),
19
+ new (0, _chunkLCA4FKWYjs.XMLHttpRequestInterceptor)(),
20
+ new (0, _chunkLEA3MUU3js.FetchInterceptor)()
21
21
  ];
22
22
 
23
23
 
@@ -1,17 +1,17 @@
1
1
  import {
2
2
  ClientRequestInterceptor
3
- } from "../chunk-ZHMQHI2Z.mjs";
3
+ } from "../chunk-FWJSC2QD.mjs";
4
4
  import {
5
5
  XMLHttpRequestInterceptor
6
- } from "../chunk-WISH7OVU.mjs";
6
+ } from "../chunk-OMWE7UVM.mjs";
7
7
  import "../chunk-6HYIRFX2.mjs";
8
8
  import {
9
9
  FetchInterceptor
10
- } from "../chunk-7XUH4W74.mjs";
10
+ } from "../chunk-CTGTMEFD.mjs";
11
11
  import "../chunk-TX5GBTFY.mjs";
12
12
  import "../chunk-6YM4PLBI.mjs";
13
13
  import "../chunk-5KMS5CTP.mjs";
14
- import "../chunk-ELLF26MW.mjs";
14
+ import "../chunk-I7HQIBT7.mjs";
15
15
 
16
16
  // src/presets/node.ts
17
17
  var node_default = [
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mswjs/interceptors",
3
3
  "description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
4
- "version": "0.37.4",
4
+ "version": "0.37.6",
5
5
  "main": "./lib/node/index.js",
6
6
  "module": "./lib/node/index.mjs",
7
7
  "types": "./lib/node/index.d.ts",
@@ -12,7 +12,6 @@ import { MockSocket } from '../Socket/MockSocket'
12
12
  import type { NormalizedSocketWriteArgs } from '../Socket/utils/normalizeSocketWriteArgs'
13
13
  import { isPropertyAccessible } from '../../utils/isPropertyAccessible'
14
14
  import { baseUrlFromConnectionOptions } from '../Socket/utils/baseUrlFromConnectionOptions'
15
- import { parseRawHeaders } from '../Socket/utils/parseRawHeaders'
16
15
  import { createServerErrorResponse } from '../../utils/responseUtils'
17
16
  import { createRequestId } from '../../createRequestId'
18
17
  import { getRawFetchHeaders } from './utils/recordRawHeaders'
@@ -472,7 +471,7 @@ export class MockHttpSocket extends MockSocket {
472
471
 
473
472
  const url = new URL(path, this.baseUrl)
474
473
  const method = this.connectionOptions.method?.toUpperCase() || 'GET'
475
- const headers = parseRawHeaders(rawHeaders)
474
+ const headers = FetchResponse.parseRawHeaders(rawHeaders)
476
475
  const canHaveBody = method !== 'GET' && method !== 'HEAD'
477
476
 
478
477
  // Translate the basic authorization in the URL to the request header.
@@ -565,7 +564,7 @@ export class MockHttpSocket extends MockSocket {
565
564
  status,
566
565
  statusText
567
566
  ) => {
568
- const headers = parseRawHeaders(rawHeaders)
567
+ const headers = FetchResponse.parseRawHeaders(rawHeaders)
569
568
 
570
569
  const response = new FetchResponse(
571
570
  /**
@@ -9,6 +9,7 @@ import {
9
9
 
10
10
  declare module 'node:http' {
11
11
  interface Agent {
12
+ options?: http.AgentOptions
12
13
  createConnection(options: any, callback: any): net.Socket
13
14
  }
14
15
  }
@@ -33,15 +34,23 @@ export class MockAgent extends http.Agent {
33
34
 
34
35
  public createConnection(options: any, callback: any): net.Socket {
35
36
  const createConnection =
36
- (this.customAgent instanceof http.Agent &&
37
- this.customAgent.createConnection) ||
38
- super.createConnection
37
+ this.customAgent instanceof http.Agent
38
+ ? this.customAgent.createConnection
39
+ : super.createConnection
40
+
41
+ const createConnectionOptions =
42
+ this.customAgent instanceof http.Agent
43
+ ? {
44
+ ...options,
45
+ ...this.customAgent.options,
46
+ }
47
+ : options
39
48
 
40
49
  const socket = new MockHttpSocket({
41
50
  connectionOptions: options,
42
51
  createConnection: createConnection.bind(
43
52
  this.customAgent || this,
44
- options,
53
+ createConnectionOptions,
45
54
  callback
46
55
  ),
47
56
  onRequest: this.onRequest.bind(this),
@@ -66,15 +75,23 @@ export class MockHttpsAgent extends https.Agent {
66
75
 
67
76
  public createConnection(options: any, callback: any): net.Socket {
68
77
  const createConnection =
69
- (this.customAgent instanceof https.Agent &&
70
- this.customAgent.createConnection) ||
71
- super.createConnection
78
+ this.customAgent instanceof https.Agent
79
+ ? this.customAgent.createConnection
80
+ : super.createConnection
81
+
82
+ const createConnectionOptions =
83
+ this.customAgent instanceof https.Agent
84
+ ? {
85
+ ...options,
86
+ ...this.customAgent.options,
87
+ }
88
+ : options
72
89
 
73
90
  const socket = new MockHttpSocket({
74
91
  connectionOptions: options,
75
92
  createConnection: createConnection.bind(
76
93
  this.customAgent || this,
77
- options,
94
+ createConnectionOptions,
78
95
  callback
79
96
  ),
80
97
  onRequest: this.onRequest.bind(this),
@@ -341,6 +341,50 @@ it('sets fallback Agent based on the URL protocol', () => {
341
341
  expect(agent).toHaveProperty('protocol', url.protocol)
342
342
  })
343
343
 
344
+ it('preserves `requestUnauthorized` option set to undefined', () => {
345
+ const [, options] = normalizeClientRequestArgs('https:', [
346
+ 'https://github.com',
347
+ { rejectUnauthorized: undefined },
348
+ ])
349
+
350
+ expect(options.rejectUnauthorized).toBe(undefined)
351
+ expect((options.agent as HttpsAgent).options.rejectUnauthorized).toBe(
352
+ undefined
353
+ )
354
+ })
355
+
356
+ it('preserves `requestUnauthorized` option set to true', () => {
357
+ const [, options] = normalizeClientRequestArgs('https:', [
358
+ 'https://github.com',
359
+ { rejectUnauthorized: true },
360
+ ])
361
+
362
+ expect(options.rejectUnauthorized).toBe(true)
363
+ expect((options.agent as HttpsAgent).options.rejectUnauthorized).toBe(true)
364
+ })
365
+
366
+ it('preserves `requestUnauthorized` option set to false', () => {
367
+ const [, options] = normalizeClientRequestArgs('https:', [
368
+ 'https://github.com',
369
+ { rejectUnauthorized: false },
370
+ ])
371
+
372
+ expect(options.rejectUnauthorized).toBe(false)
373
+ expect((options.agent as HttpsAgent).options.rejectUnauthorized).toBe(false)
374
+ })
375
+
376
+ it('does not add `rejectUnauthorized` value if not set', () => {
377
+ const agent = new HttpsAgent()
378
+ const [, options] = normalizeClientRequestArgs('https:', [
379
+ 'https://github.com',
380
+ ])
381
+
382
+ expect(options).not.toHaveProperty('rejectUnauthorized')
383
+ expect((options.agent as HttpsAgent).options).not.toHaveProperty(
384
+ 'rejectUnauthorized'
385
+ )
386
+ })
387
+
344
388
  it('does not set any fallback Agent given "agent: false" option', () => {
345
389
  const [, options] = normalizeClientRequestArgs('https:', [
346
390
  'https://github.com',
@@ -243,7 +243,10 @@ export function normalizeClientRequestArgs(
243
243
  const agent =
244
244
  options.protocol === 'https:'
245
245
  ? new HttpsAgent({
246
- rejectUnauthorized: options.rejectUnauthorized,
246
+ // Any other value other than false is considered as true, so we don't add this property if undefined.
247
+ ...('rejectUnauthorized' in options && {
248
+ rejectUnauthorized: options.rejectUnauthorized,
249
+ }),
247
250
  })
248
251
  : new HttpAgent()
249
252
 
@@ -44,6 +44,17 @@ export class FetchResponse extends Response {
44
44
  })
45
45
  }
46
46
 
47
+ /**
48
+ * Parses the given raw HTTP headers into a Fetch API `Headers` instance.
49
+ */
50
+ static parseRawHeaders(rawHeaders: Array<string>): Headers {
51
+ const headers = new Headers()
52
+ for (let line = 0; line < rawHeaders.length; line += 2) {
53
+ headers.append(rawHeaders[line], rawHeaders[line + 1])
54
+ }
55
+ return headers
56
+ }
57
+
47
58
  constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {
48
59
  const status = init.status ?? 200
49
60
  const safeStatus = FetchResponse.isConfigurableStatusCode(status)
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/glossary.ts","../../src/utils/fetchUtils.ts"],"names":[],"mappings":";AAEO,IAAM,oBAAmC,OAAO,iBAAiB;;;ACEjE,IAAM,iBAAN,cAA4B,SAAS;AAAA,EAS1C,OAAO,yBAAyB,QAAyB;AACvD,WAAO,UAAU,OAAO,UAAU;AAAA,EACpC;AAAA,EAEA,OAAO,mBAAmB,QAAyB;AACjD,WAAO,eAAc,2BAA2B,SAAS,MAAM;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,mBAAmB,QAAyB;AACjD,WAAO,CAAC,eAAc,0BAA0B,SAAS,MAAM;AAAA,EACjE;AAAA,EAEA,OAAO,OAAO,KAAyB,UAA0B;AAC/D,QAAI,CAAC,KAAK;AACR;AAAA,IACF;AAEA,QAAI,SAAS,OAAO,IAAI;AACtB;AAAA,IACF;AAEA,WAAO,eAAe,UAAU,OAAO;AAAA,MACrC,OAAO;AAAA,MACP,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AAAA,EAEA,YAAY,MAAwB,OAA0B,CAAC,GAAG;AA9CpE;AA+CI,UAAM,UAAS,UAAK,WAAL,YAAe;AAC9B,UAAM,aAAa,eAAc,yBAAyB,MAAM,IAC5D,SACA;AACJ,UAAM,YAAY,eAAc,mBAAmB,MAAM,IAAI,OAAO;AAEpE,UAAM,WAAW;AAAA,MACf,GAAG;AAAA,MACH,QAAQ;AAAA,IACV,CAAC;AAED,QAAI,WAAW,YAAY;AAKzB,YAAM,cAAc,OAAO,sBAAsB,IAAI,EAAE;AAAA,QACrD,CAAC,WAAW,OAAO,gBAAgB;AAAA,MACrC;AACA,UAAI,aAAa;AACf,cAAM,QAAQ,QAAQ,IAAI,MAAM,WAAW;AAC3C,gBAAQ,IAAI,OAAO,UAAU,MAAM;AAAA,MACrC,OAAO;AACL,eAAO,eAAe,MAAM,UAAU;AAAA,UACpC,OAAO;AAAA,UACP,YAAY;AAAA,UACZ,cAAc;AAAA,UACd,UAAU;AAAA,QACZ,CAAC;AAAA,MACH;AAAA,IACF;AAEA,mBAAc,OAAO,KAAK,KAAK,IAAI;AAAA,EACrC;AACF;AA7EO,IAAM,gBAAN;AAAA;AAAA;AAAA;AAAA;AAAM,cAKK,4BAA4B,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG;AALzD,cAOK,6BAA6B,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG","sourcesContent":["import type { RequestController } from './RequestController'\n\nexport const IS_PATCHED_MODULE: unique symbol = Symbol('isPatchedModule')\n\n/**\n * @note Export `RequestController` as a type only.\n * It's never meant to be created in the userland.\n */\nexport type { RequestController }\n\nexport type RequestCredentials = 'omit' | 'include' | 'same-origin'\n\nexport type HttpRequestEventMap = {\n request: [\n args: {\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n response: [\n args: {\n response: Response\n isMockedResponse: boolean\n request: Request\n requestId: string\n }\n ]\n unhandledException: [\n args: {\n error: unknown\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n}\n","export interface FetchResponseInit extends ResponseInit {\n url?: string\n}\n\nexport class FetchResponse extends Response {\n /**\n * Response status codes for responses that cannot have body.\n * @see https://fetch.spec.whatwg.org/#statuses\n */\n static readonly STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304]\n\n static readonly STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308]\n\n static isConfigurableStatusCode(status: number): boolean {\n return status >= 200 && status <= 599\n }\n\n static isRedirectResponse(status: number): boolean {\n return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status)\n }\n\n /**\n * Returns a boolean indicating whether the given response status\n * code represents a response that can have a body.\n */\n static isResponseWithBody(status: number): boolean {\n return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)\n }\n\n static setUrl(url: string | undefined, response: Response): void {\n if (!url) {\n return\n }\n\n if (response.url != '') {\n return\n }\n\n Object.defineProperty(response, 'url', {\n value: url,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n\n constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {\n const status = init.status ?? 200\n const safeStatus = FetchResponse.isConfigurableStatusCode(status)\n ? status\n : 200\n const finalBody = FetchResponse.isResponseWithBody(status) ? body : null\n\n super(finalBody, {\n ...init,\n status: safeStatus,\n })\n\n if (status !== safeStatus) {\n /**\n * @note Undici keeps an internal \"Symbol(state)\" that holds\n * the actual value of response status. Update that in Node.js.\n */\n const stateSymbol = Object.getOwnPropertySymbols(this).find(\n (symbol) => symbol.description === 'state'\n )\n if (stateSymbol) {\n const state = Reflect.get(this, stateSymbol) as object\n Reflect.set(state, 'status', status)\n } else {\n Object.defineProperty(this, 'status', {\n value: status,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n }\n\n FetchResponse.setUrl(init.url, this)\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/glossary.ts","../../src/utils/fetchUtils.ts"],"sourcesContent":["import type { RequestController } from './RequestController'\n\nexport const IS_PATCHED_MODULE: unique symbol = Symbol('isPatchedModule')\n\n/**\n * @note Export `RequestController` as a type only.\n * It's never meant to be created in the userland.\n */\nexport type { RequestController }\n\nexport type RequestCredentials = 'omit' | 'include' | 'same-origin'\n\nexport type HttpRequestEventMap = {\n request: [\n args: {\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n response: [\n args: {\n response: Response\n isMockedResponse: boolean\n request: Request\n requestId: string\n }\n ]\n unhandledException: [\n args: {\n error: unknown\n request: Request\n requestId: string\n controller: RequestController\n }\n ]\n}\n","export interface FetchResponseInit extends ResponseInit {\n url?: string\n}\n\nexport class FetchResponse extends Response {\n /**\n * Response status codes for responses that cannot have body.\n * @see https://fetch.spec.whatwg.org/#statuses\n */\n static readonly STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304]\n\n static readonly STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308]\n\n static isConfigurableStatusCode(status: number): boolean {\n return status >= 200 && status <= 599\n }\n\n static isRedirectResponse(status: number): boolean {\n return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status)\n }\n\n /**\n * Returns a boolean indicating whether the given response status\n * code represents a response that can have a body.\n */\n static isResponseWithBody(status: number): boolean {\n return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)\n }\n\n static setUrl(url: string | undefined, response: Response): void {\n if (!url) {\n return\n }\n\n if (response.url != '') {\n return\n }\n\n Object.defineProperty(response, 'url', {\n value: url,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n\n constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {\n const status = init.status ?? 200\n const safeStatus = FetchResponse.isConfigurableStatusCode(status)\n ? status\n : 200\n const finalBody = FetchResponse.isResponseWithBody(status) ? body : null\n\n super(finalBody, {\n ...init,\n status: safeStatus,\n })\n\n if (status !== safeStatus) {\n /**\n * @note Undici keeps an internal \"Symbol(state)\" that holds\n * the actual value of response status. Update that in Node.js.\n */\n const stateSymbol = Object.getOwnPropertySymbols(this).find(\n (symbol) => symbol.description === 'state'\n )\n if (stateSymbol) {\n const state = Reflect.get(this, stateSymbol) as object\n Reflect.set(state, 'status', status)\n } else {\n Object.defineProperty(this, 'status', {\n value: status,\n enumerable: true,\n configurable: true,\n writable: false,\n })\n }\n }\n\n FetchResponse.setUrl(init.url, this)\n }\n}\n"],"mappings":";AAEO,IAAM,oBAAmC,OAAO,iBAAiB;;;ACEjE,IAAM,iBAAN,cAA4B,SAAS;AAAA,EAS1C,OAAO,yBAAyB,QAAyB;AACvD,WAAO,UAAU,OAAO,UAAU;AAAA,EACpC;AAAA,EAEA,OAAO,mBAAmB,QAAyB;AACjD,WAAO,eAAc,2BAA2B,SAAS,MAAM;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,mBAAmB,QAAyB;AACjD,WAAO,CAAC,eAAc,0BAA0B,SAAS,MAAM;AAAA,EACjE;AAAA,EAEA,OAAO,OAAO,KAAyB,UAA0B;AAC/D,QAAI,CAAC,KAAK;AACR;AAAA,IACF;AAEA,QAAI,SAAS,OAAO,IAAI;AACtB;AAAA,IACF;AAEA,WAAO,eAAe,UAAU,OAAO;AAAA,MACrC,OAAO;AAAA,MACP,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AAAA,EAEA,YAAY,MAAwB,OAA0B,CAAC,GAAG;AA9CpE;AA+CI,UAAM,UAAS,UAAK,WAAL,YAAe;AAC9B,UAAM,aAAa,eAAc,yBAAyB,MAAM,IAC5D,SACA;AACJ,UAAM,YAAY,eAAc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