@mswjs/interceptors 0.28.4 → 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.
Files changed (77) hide show
  1. package/lib/browser/{chunk-LR5DGYDZ.js → chunk-2CRB3JAQ.js} +1 -1
  2. package/lib/browser/chunk-2CRB3JAQ.js.map +1 -0
  3. package/lib/browser/{chunk-FIBZ2AT2.mjs → chunk-732REFPX.mjs} +16 -2
  4. package/lib/browser/chunk-732REFPX.mjs.map +1 -0
  5. package/lib/browser/{chunk-ZQVP33QW.mjs → chunk-MAEPOYB6.mjs} +62 -24
  6. package/lib/browser/chunk-MAEPOYB6.mjs.map +1 -0
  7. package/lib/browser/{chunk-6JZQ6WGN.mjs → chunk-OMISYKWR.mjs} +1 -1
  8. package/lib/browser/chunk-OMISYKWR.mjs.map +1 -0
  9. package/lib/browser/{chunk-WKPHOWK2.js → chunk-PSX5J3RF.js} +23 -9
  10. package/lib/browser/chunk-PSX5J3RF.js.map +1 -0
  11. package/lib/browser/{chunk-5Q77IUWF.js → chunk-WBHIW62P.js} +67 -29
  12. package/lib/browser/chunk-WBHIW62P.js.map +1 -0
  13. package/lib/browser/{glossary-640c9679.d.ts → glossary-1c204f45.d.ts} +11 -0
  14. package/lib/browser/index.d.ts +1 -1
  15. package/lib/browser/index.js +2 -2
  16. package/lib/browser/index.mjs +1 -1
  17. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -1
  18. package/lib/browser/interceptors/XMLHttpRequest/index.js +3 -3
  19. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +2 -2
  20. package/lib/browser/interceptors/fetch/index.d.ts +1 -1
  21. package/lib/browser/interceptors/fetch/index.js +3 -3
  22. package/lib/browser/interceptors/fetch/index.mjs +2 -2
  23. package/lib/browser/presets/browser.d.ts +1 -1
  24. package/lib/browser/presets/browser.js +5 -5
  25. package/lib/browser/presets/browser.mjs +3 -3
  26. package/lib/node/{BatchInterceptor-cb145daa.d.ts → BatchInterceptor-2badedde.d.ts} +1 -1
  27. package/lib/node/{Interceptor-6696a18d.d.ts → Interceptor-88ee47c0.d.ts} +11 -0
  28. package/lib/node/RemoteHttpInterceptor.d.ts +2 -2
  29. package/lib/node/RemoteHttpInterceptor.js +5 -5
  30. package/lib/node/RemoteHttpInterceptor.mjs +3 -3
  31. package/lib/node/{chunk-KHEPLFR3.js → chunk-APT7KA3B.js} +19 -5
  32. package/lib/node/chunk-APT7KA3B.js.map +1 -0
  33. package/lib/node/{chunk-FZJKKO5H.js → chunk-EIBTX65O.js} +1 -1
  34. package/lib/node/{chunk-FZJKKO5H.js.map → chunk-EIBTX65O.js.map} +1 -1
  35. package/lib/node/{chunk-UUKG6TUD.js → chunk-HAIWBQD5.js} +30 -28
  36. package/lib/node/chunk-HAIWBQD5.js.map +1 -0
  37. package/lib/node/{chunk-TCATD46T.mjs → chunk-JMNEFEYU.mjs} +30 -28
  38. package/lib/node/chunk-JMNEFEYU.mjs.map +1 -0
  39. package/lib/node/{chunk-OXXEWTTM.mjs → chunk-KSHIDGUL.mjs} +16 -2
  40. package/lib/node/chunk-KSHIDGUL.mjs.map +1 -0
  41. package/lib/node/{chunk-HAGW22AN.mjs → chunk-OJ6O4LSC.mjs} +1 -1
  42. package/lib/node/{chunk-HAGW22AN.mjs.map → chunk-OJ6O4LSC.mjs.map} +1 -1
  43. package/lib/node/index.d.ts +2 -2
  44. package/lib/node/index.js +2 -2
  45. package/lib/node/index.mjs +1 -1
  46. package/lib/node/interceptors/ClientRequest/index.d.ts +1 -1
  47. package/lib/node/interceptors/ClientRequest/index.js +2 -2
  48. package/lib/node/interceptors/ClientRequest/index.mjs +1 -1
  49. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -1
  50. package/lib/node/interceptors/XMLHttpRequest/index.js +3 -3
  51. package/lib/node/interceptors/XMLHttpRequest/index.mjs +2 -2
  52. package/lib/node/interceptors/fetch/index.d.ts +1 -1
  53. package/lib/node/interceptors/fetch/index.js +64 -26
  54. package/lib/node/interceptors/fetch/index.js.map +1 -1
  55. package/lib/node/interceptors/fetch/index.mjs +61 -23
  56. package/lib/node/interceptors/fetch/index.mjs.map +1 -1
  57. package/lib/node/presets/node.d.ts +1 -1
  58. package/lib/node/presets/node.js +5 -5
  59. package/lib/node/presets/node.mjs +3 -3
  60. package/package.json +1 -1
  61. package/src/glossary.ts +11 -0
  62. package/src/interceptors/ClientRequest/NodeClientRequest.ts +28 -4
  63. package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +4 -7
  64. package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +25 -0
  65. package/src/interceptors/fetch/index.ts +79 -29
  66. package/src/utils/getUrlByRequestOptions.test.ts +31 -3
  67. package/src/utils/getUrlByRequestOptions.ts +14 -38
  68. package/lib/browser/chunk-5Q77IUWF.js.map +0 -1
  69. package/lib/browser/chunk-6JZQ6WGN.mjs.map +0 -1
  70. package/lib/browser/chunk-FIBZ2AT2.mjs.map +0 -1
  71. package/lib/browser/chunk-LR5DGYDZ.js.map +0 -1
  72. package/lib/browser/chunk-WKPHOWK2.js.map +0 -1
  73. package/lib/browser/chunk-ZQVP33QW.mjs.map +0 -1
  74. package/lib/node/chunk-KHEPLFR3.js.map +0 -1
  75. package/lib/node/chunk-OXXEWTTM.mjs.map +0 -1
  76. package/lib/node/chunk-TCATD46T.mjs.map +0 -1
  77. package/lib/node/chunk-UUKG6TUD.js.map +0 -1
@@ -6,7 +6,7 @@ var _chunkMQJ3JOOKjs = require('./chunk-MQJ3JOOK.js');
6
6
 
7
7
 
8
8
 
9
- var _chunkLR5DGYDZjs = require('./chunk-LR5DGYDZ.js');
9
+ var _chunk2CRB3JAQjs = require('./chunk-2CRB3JAQ.js');
10
10
 
11
11
 
12
12
 
@@ -38,7 +38,7 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
38
38
  async setup() {
39
39
  const pureFetch = globalThis.fetch;
40
40
  _outvariant.invariant.call(void 0,
41
- !pureFetch[_chunkLR5DGYDZjs.IS_PATCHED_MODULE],
41
+ !pureFetch[_chunk2CRB3JAQjs.IS_PATCHED_MODULE],
42
42
  'Failed to patch the "fetch" module: already patched.'
43
43
  );
44
44
  globalThis.fetch = async (input, init) => {
@@ -72,21 +72,29 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
72
72
  { once: true }
73
73
  );
74
74
  }
75
+ const responsePromise = new (0, _deferredpromise.DeferredPromise)();
75
76
  const respondWith = (response) => {
76
- const responseClone = response.clone();
77
- this.emitter.emit("response", {
78
- response: responseClone,
79
- isMockedResponse: true,
80
- request: interactiveRequest,
81
- requestId
82
- });
77
+ this.logger.info("responding with a mock response:", response);
78
+ if (this.emitter.listenerCount("response") > 0) {
79
+ this.logger.info('emitting the "response" event...');
80
+ const responseClone = response.clone();
81
+ this.emitter.emit("response", {
82
+ response: responseClone,
83
+ isMockedResponse: true,
84
+ request: interactiveRequest,
85
+ requestId
86
+ });
87
+ }
83
88
  Object.defineProperty(response, "url", {
84
89
  writable: false,
85
90
  enumerable: true,
86
91
  configurable: false,
87
92
  value: request.url
88
93
  });
89
- return response;
94
+ responsePromise.resolve(response);
95
+ };
96
+ const errorWith = (reason) => {
97
+ responsePromise.reject(reason);
90
98
  };
91
99
  const resolverResult = await _until.until.call(void 0,
92
100
  async () => {
@@ -109,48 +117,78 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
109
117
  }
110
118
  );
111
119
  if (requestAborted.state === "rejected") {
112
- return Promise.reject(requestAborted.rejectionReason);
120
+ this.logger.info(
121
+ "request has been aborted:",
122
+ requestAborted.rejectionReason
123
+ );
124
+ responsePromise.reject(requestAborted.rejectionReason);
125
+ return responsePromise;
113
126
  }
114
127
  if (resolverResult.error) {
128
+ this.logger.info(
129
+ "request listerner threw an error:",
130
+ resolverResult.error
131
+ );
115
132
  if (resolverResult.error instanceof Response) {
116
- if (_chunkLR5DGYDZjs.isResponseError.call(void 0, resolverResult.error)) {
117
- return Promise.reject(createNetworkError(resolverResult.error));
133
+ if (_chunk2CRB3JAQjs.isResponseError.call(void 0, resolverResult.error)) {
134
+ errorWith(createNetworkError(resolverResult.error));
135
+ } else {
136
+ respondWith(resolverResult.error);
118
137
  }
119
- return respondWith(resolverResult.error);
120
138
  }
121
- return _chunkLR5DGYDZjs.createServerErrorResponse.call(void 0, resolverResult.error);
139
+ if (this.emitter.listenerCount("unhandledException") > 0) {
140
+ await _chunkMQJ3JOOKjs.emitAsync.call(void 0, this.emitter, "unhandledException", {
141
+ error: resolverResult.error,
142
+ request,
143
+ requestId,
144
+ controller: {
145
+ respondWith,
146
+ errorWith
147
+ }
148
+ });
149
+ if (responsePromise.state !== "pending") {
150
+ return responsePromise;
151
+ }
152
+ }
153
+ respondWith(_chunk2CRB3JAQjs.createServerErrorResponse.call(void 0, resolverResult.error));
154
+ return responsePromise;
122
155
  }
123
156
  const mockedResponse = resolverResult.data;
124
157
  if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
125
158
  this.logger.info("received mocked response:", mockedResponse);
126
- if (_chunkLR5DGYDZjs.isResponseError.call(void 0, mockedResponse)) {
159
+ if (_chunk2CRB3JAQjs.isResponseError.call(void 0, mockedResponse)) {
127
160
  this.logger.info(
128
161
  "received a network error response, rejecting the request promise..."
129
162
  );
130
- return Promise.reject(createNetworkError(mockedResponse));
163
+ errorWith(createNetworkError(mockedResponse));
164
+ } else {
165
+ respondWith(mockedResponse);
131
166
  }
132
- return respondWith(mockedResponse);
167
+ return responsePromise;
133
168
  }
134
169
  this.logger.info("no mocked response received!");
135
170
  return pureFetch(request).then((response) => {
136
- const responseClone = response.clone();
137
- this.logger.info("original fetch performed", responseClone);
138
- this.emitter.emit("response", {
139
- response: responseClone,
140
- isMockedResponse: false,
141
- request: interactiveRequest,
142
- requestId
143
- });
171
+ this.logger.info("original fetch performed", response);
172
+ if (this.emitter.listenerCount("response") > 0) {
173
+ this.logger.info('emitting the "response" event...');
174
+ const responseClone = response.clone();
175
+ this.emitter.emit("response", {
176
+ response: responseClone,
177
+ isMockedResponse: false,
178
+ request: interactiveRequest,
179
+ requestId
180
+ });
181
+ }
144
182
  return response;
145
183
  });
146
184
  };
147
- Object.defineProperty(globalThis.fetch, _chunkLR5DGYDZjs.IS_PATCHED_MODULE, {
185
+ Object.defineProperty(globalThis.fetch, _chunk2CRB3JAQjs.IS_PATCHED_MODULE, {
148
186
  enumerable: true,
149
187
  configurable: true,
150
188
  value: true
151
189
  });
152
190
  this.subscriptions.push(() => {
153
- Object.defineProperty(globalThis.fetch, _chunkLR5DGYDZjs.IS_PATCHED_MODULE, {
191
+ Object.defineProperty(globalThis.fetch, _chunk2CRB3JAQjs.IS_PATCHED_MODULE, {
154
192
  value: void 0
155
193
  });
156
194
  globalThis.fetch = pureFetch;
@@ -172,4 +210,4 @@ function createNetworkError(cause) {
172
210
 
173
211
 
174
212
  exports.FetchInterceptor = FetchInterceptor;
175
- //# sourceMappingURL=chunk-5Q77IUWF.js.map
213
+ //# sourceMappingURL=chunk-WBHIW62P.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/interceptors/fetch/index.ts","../../src/utils/canParseUrl.ts"],"names":["mockedResponse"],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,aAAa;;;ACGf,SAAS,YAAY,KAAsB;AAChD,MAAI;AACF,QAAI,IAAI,GAAG;AACX,WAAO;AAAA,EACT,SAAS,QAAP;AACA,WAAO;AAAA,EACT;AACF;;;ADEO,IAAM,oBAAN,cAA+B,YAAiC;AAAA,EAGrE,cAAc;AACZ,UAAM,kBAAiB,MAAM;AAAA,EAC/B;AAAA,EAEU,mBAAmB;AAC3B,WACE,OAAO,eAAe,eACtB,OAAO,WAAW,UAAU;AAAA,EAEhC;AAAA,EAEA,MAAgB,QAAQ;AACtB,UAAM,YAAY,WAAW;AAE7B;AAAA,MACE,CAAE,UAAkB,iBAAiB;AAAA,MACrC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O,OAAO;AAmQhC,SAAS,mBAAmB,OAAgB;AAC1C,SAAO,OAAO,OAAO,IAAI,UAAU,iBAAiB,GAAG;AAAA,IACrD;AAAA,EACF,CAAC;AACH","sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { toInteractiveRequest } from '../../utils/toInteractiveRequest'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { canParseUrl } from '../../utils/canParseUrl'\nimport { createRequestId } from '../../createRequestId'\nimport {\n createServerErrorResponse,\n isResponseError,\n} from '../../utils/responseUtils'\n\nexport class FetchInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('fetch')\n\n constructor() {\n super(FetchInterceptor.symbol)\n }\n\n protected checkEnvironment() {\n return (\n typeof globalThis !== 'undefined' &&\n typeof globalThis.fetch !== 'undefined'\n )\n }\n\n protected async setup() {\n const pureFetch = globalThis.fetch\n\n invariant(\n !(pureFetch as any)[IS_PATCHED_MODULE],\n 'Failed to patch the \"fetch\" module: already patched.'\n )\n\n globalThis.fetch = async (input, init) => {\n const requestId = createRequestId()\n\n /**\n * @note Resolve potentially relative request URL\n * against the present `location`. This is mainly\n * for native `fetch` in JSDOM.\n * @see https://github.com/mswjs/msw/issues/1625\n */\n const resolvedInput =\n typeof input === 'string' &&\n typeof location !== 'undefined' &&\n !canParseUrl(input)\n ? new URL(input, location.origin)\n : input\n\n const request = new Request(resolvedInput, init)\n\n this.logger.info('[%s] %s', request.method, request.url)\n\n const { interactiveRequest, requestController } =\n toInteractiveRequest(request)\n\n this.logger.info(\n 'emitting the \"request\" event for %d listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n this.emitter.once('request', ({ requestId: pendingRequestId }) => {\n if (pendingRequestId !== requestId) {\n return\n }\n\n if (requestController.responsePromise.state === 'pending') {\n requestController.responsePromise.resolve(undefined)\n }\n })\n\n this.logger.info('awaiting for the mocked response...')\n\n const signal = interactiveRequest.signal\n const requestAborted = new DeferredPromise()\n\n // Signal isn't always defined in react-native.\n if (signal) {\n signal.addEventListener(\n 'abort',\n () => {\n requestAborted.reject(signal.reason)\n },\n { once: true }\n )\n }\n\n const responsePromise = new DeferredPromise<Response>()\n\n const respondWith = (response: Response): void => {\n this.logger.info('responding with a mock response:', response)\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n // Clone the mocked response for the \"response\" event listener.\n // This way, the listener can read the response and not lock its body\n // for the actual fetch consumer.\n const responseClone = response.clone()\n\n this.emitter.emit('response', {\n response: responseClone,\n isMockedResponse: true,\n request: interactiveRequest,\n requestId,\n })\n }\n\n // Set the \"response.url\" property to equal the intercepted request URL.\n Object.defineProperty(response, 'url', {\n writable: false,\n enumerable: true,\n configurable: false,\n value: request.url,\n })\n\n responsePromise.resolve(response)\n }\n\n const errorWith = (reason: unknown): void => {\n responsePromise.reject(reason)\n }\n\n const resolverResult = await until<unknown, Response | undefined>(\n async () => {\n const listenersFinished = emitAsync(this.emitter, 'request', {\n request: interactiveRequest,\n requestId,\n })\n\n await Promise.race([\n requestAborted,\n // Put the listeners invocation Promise in the same race condition\n // with the request abort Promise because otherwise awaiting the listeners\n // would always yield some response (or undefined).\n listenersFinished,\n requestController.responsePromise,\n ])\n\n this.logger.info('all request listeners have been resolved!')\n\n const mockedResponse = await requestController.responsePromise\n this.logger.info('event.respondWith called with:', mockedResponse)\n\n return mockedResponse\n }\n )\n\n if (requestAborted.state === 'rejected') {\n this.logger.info(\n 'request has been aborted:',\n requestAborted.rejectionReason\n )\n\n responsePromise.reject(requestAborted.rejectionReason)\n return responsePromise\n }\n\n if (resolverResult.error) {\n this.logger.info(\n 'request listerner threw an error:',\n resolverResult.error\n )\n\n // Treat thrown Responses as mocked responses.\n if (resolverResult.error instanceof Response) {\n // Treat thrown Response.error() as a request error.\n if (isResponseError(resolverResult.error)) {\n errorWith(createNetworkError(resolverResult.error))\n } else {\n // Treat the rest of thrown Responses as mocked responses.\n respondWith(resolverResult.error)\n }\n }\n\n // Emit the \"unhandledException\" interceptor event so the client\n // can opt-out from exceptions translating to 500 error responses.\n\n if (this.emitter.listenerCount('unhandledException') > 0) {\n await emitAsync(this.emitter, 'unhandledException', {\n error: resolverResult.error,\n request,\n requestId,\n controller: {\n respondWith,\n errorWith,\n },\n })\n\n if (responsePromise.state !== 'pending') {\n return responsePromise\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 respondWith(createServerErrorResponse(resolverResult.error))\n return responsePromise\n }\n\n const mockedResponse = resolverResult.data\n\n if (mockedResponse && !request.signal?.aborted) {\n this.logger.info('received mocked response:', mockedResponse)\n\n // Reject the request Promise on mocked \"Response.error\" responses.\n if (isResponseError(mockedResponse)) {\n this.logger.info(\n 'received a network error response, rejecting the request promise...'\n )\n\n /**\n * Set the cause of the request promise rejection to the\n * network error Response instance. This differs from Undici.\n * Undici will forward the \"response.error\" custom property\n * as the rejection reason but for \"Response.error()\" static method\n * \"response.error\" will equal to undefined, making \"cause\" an empty Error.\n * @see https://github.com/nodejs/undici/blob/83cb522ae0157a19d149d72c7d03d46e34510d0a/lib/fetch/response.js#L344\n */\n errorWith(createNetworkError(mockedResponse))\n } else {\n respondWith(mockedResponse)\n }\n\n return responsePromise\n }\n\n this.logger.info('no mocked response received!')\n\n return pureFetch(request).then((response) => {\n this.logger.info('original fetch performed', response)\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n const responseClone = response.clone()\n\n this.emitter.emit('response', {\n response: responseClone,\n isMockedResponse: false,\n request: interactiveRequest,\n requestId,\n })\n }\n\n return response\n })\n }\n\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n enumerable: true,\n configurable: true,\n value: true,\n })\n\n this.subscriptions.push(() => {\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n value: undefined,\n })\n\n globalThis.fetch = pureFetch\n\n this.logger.info(\n 'restored native \"globalThis.fetch\"!',\n globalThis.fetch.name\n )\n })\n }\n}\n\nfunction createNetworkError(cause: unknown) {\n return Object.assign(new TypeError('Failed to fetch'), {\n cause,\n })\n}\n","/**\n * Returns a boolean indicating whether the given URL string\n * can be parsed into a `URL` instance.\n * A substitute for `URL.canParse()` for Node.js 18.\n */\nexport function canParseUrl(url: string): boolean {\n try {\n new URL(url)\n return true\n } catch (_error) {\n return false\n }\n}\n"]}
@@ -28,6 +28,17 @@ type HttpRequestEventMap = {
28
28
  requestId: string;
29
29
  }
30
30
  ];
31
+ unhandledException: [
32
+ args: {
33
+ error: unknown;
34
+ request: Request;
35
+ requestId: string;
36
+ controller: {
37
+ respondWith(response: Response): void;
38
+ errorWith(error?: Error): void;
39
+ };
40
+ }
41
+ ];
31
42
  };
32
43
 
33
44
  export { HttpRequestEventMap as H, IS_PATCHED_MODULE as I, RequestCredentials as R, InteractiveRequest as a };
@@ -1,4 +1,4 @@
1
- export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestCredentials } from './glossary-640c9679.js';
1
+ export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestCredentials } from './glossary-1c204f45.js';
2
2
  import { I as Interceptor, E as ExtractEventNames } from './Interceptor-af98b768.js';
3
3
  export { c as INTERNAL_REQUEST_ID_HEADER_NAME, a as InterceptorEventMap, e as InterceptorReadyState, b as InterceptorSubscription, d as deleteGlobalSymbol, g as getGlobalSymbol } from './Interceptor-af98b768.js';
4
4
  import { EventMap, Listener } from 'strict-event-emitter';
@@ -5,7 +5,7 @@ var _chunkLK6DILFKjs = require('./chunk-LK6DILFK.js');
5
5
 
6
6
 
7
7
 
8
- var _chunkLR5DGYDZjs = require('./chunk-LR5DGYDZ.js');
8
+ var _chunk2CRB3JAQjs = require('./chunk-2CRB3JAQ.js');
9
9
 
10
10
 
11
11
 
@@ -75,5 +75,5 @@ function getCleanUrl(url, isAbsolute = true) {
75
75
 
76
76
 
77
77
 
78
- exports.BatchInterceptor = BatchInterceptor; exports.INTERNAL_REQUEST_ID_HEADER_NAME = _chunkTIPR373Rjs.INTERNAL_REQUEST_ID_HEADER_NAME; exports.IS_PATCHED_MODULE = _chunkLR5DGYDZjs.IS_PATCHED_MODULE; exports.Interceptor = _chunkTIPR373Rjs.Interceptor; exports.InterceptorReadyState = _chunkTIPR373Rjs.InterceptorReadyState; exports.createRequestId = _chunkTIPR373Rjs.createRequestId; exports.decodeBuffer = _chunkLK6DILFKjs.decodeBuffer; exports.deleteGlobalSymbol = _chunkTIPR373Rjs.deleteGlobalSymbol; exports.encodeBuffer = _chunkLK6DILFKjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunkTIPR373Rjs.getGlobalSymbol; exports.isResponseWithoutBody = _chunkLR5DGYDZjs.isResponseWithoutBody;
78
+ exports.BatchInterceptor = BatchInterceptor; exports.INTERNAL_REQUEST_ID_HEADER_NAME = _chunkTIPR373Rjs.INTERNAL_REQUEST_ID_HEADER_NAME; exports.IS_PATCHED_MODULE = _chunk2CRB3JAQjs.IS_PATCHED_MODULE; exports.Interceptor = _chunkTIPR373Rjs.Interceptor; exports.InterceptorReadyState = _chunkTIPR373Rjs.InterceptorReadyState; exports.createRequestId = _chunkTIPR373Rjs.createRequestId; exports.decodeBuffer = _chunkLK6DILFKjs.decodeBuffer; exports.deleteGlobalSymbol = _chunkTIPR373Rjs.deleteGlobalSymbol; exports.encodeBuffer = _chunkLK6DILFKjs.encodeBuffer; exports.getCleanUrl = getCleanUrl; exports.getGlobalSymbol = _chunkTIPR373Rjs.getGlobalSymbol; exports.isResponseWithoutBody = _chunk2CRB3JAQjs.isResponseWithoutBody;
79
79
  //# sourceMappingURL=index.js.map
@@ -5,7 +5,7 @@ import {
5
5
  import {
6
6
  IS_PATCHED_MODULE,
7
7
  isResponseWithoutBody
8
- } from "./chunk-6JZQ6WGN.mjs";
8
+ } from "./chunk-OMISYKWR.mjs";
9
9
  import {
10
10
  INTERNAL_REQUEST_ID_HEADER_NAME,
11
11
  Interceptor,
@@ -1,5 +1,5 @@
1
1
  import { Emitter } from 'strict-event-emitter';
2
- import { a as InteractiveRequest, H as HttpRequestEventMap } from '../../glossary-640c9679.js';
2
+ import { a as InteractiveRequest, H as HttpRequestEventMap } from '../../glossary-1c204f45.js';
3
3
  import { I as Interceptor } from '../../Interceptor-af98b768.js';
4
4
  import '@open-draft/deferred-promise';
5
5
  import '@open-draft/logger';
@@ -1,11 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkWKPHOWK2js = require('../../chunk-WKPHOWK2.js');
3
+ var _chunkPSX5J3RFjs = require('../../chunk-PSX5J3RF.js');
4
4
  require('../../chunk-LK6DILFK.js');
5
5
  require('../../chunk-MQJ3JOOK.js');
6
- require('../../chunk-LR5DGYDZ.js');
6
+ require('../../chunk-2CRB3JAQ.js');
7
7
  require('../../chunk-TIPR373R.js');
8
8
 
9
9
 
10
- exports.XMLHttpRequestInterceptor = _chunkWKPHOWK2js.XMLHttpRequestInterceptor;
10
+ exports.XMLHttpRequestInterceptor = _chunkPSX5J3RFjs.XMLHttpRequestInterceptor;
11
11
  //# sourceMappingURL=index.js.map
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  XMLHttpRequestInterceptor
3
- } from "../../chunk-FIBZ2AT2.mjs";
3
+ } from "../../chunk-732REFPX.mjs";
4
4
  import "../../chunk-6HYIRFX2.mjs";
5
5
  import "../../chunk-OUWBQF3Z.mjs";
6
- import "../../chunk-6JZQ6WGN.mjs";
6
+ import "../../chunk-OMISYKWR.mjs";
7
7
  import "../../chunk-QED3Q6Z2.mjs";
8
8
  export {
9
9
  XMLHttpRequestInterceptor
@@ -1,4 +1,4 @@
1
- import { H as HttpRequestEventMap } from '../../glossary-640c9679.js';
1
+ import { H as HttpRequestEventMap } from '../../glossary-1c204f45.js';
2
2
  import { I as Interceptor } from '../../Interceptor-af98b768.js';
3
3
  import '@open-draft/deferred-promise';
4
4
  import '@open-draft/logger';
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk5Q77IUWFjs = require('../../chunk-5Q77IUWF.js');
3
+ var _chunkWBHIW62Pjs = require('../../chunk-WBHIW62P.js');
4
4
  require('../../chunk-MQJ3JOOK.js');
5
- require('../../chunk-LR5DGYDZ.js');
5
+ require('../../chunk-2CRB3JAQ.js');
6
6
  require('../../chunk-TIPR373R.js');
7
7
 
8
8
 
9
- exports.FetchInterceptor = _chunk5Q77IUWFjs.FetchInterceptor;
9
+ exports.FetchInterceptor = _chunkWBHIW62Pjs.FetchInterceptor;
10
10
  //# sourceMappingURL=index.js.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  FetchInterceptor
3
- } from "../../chunk-ZQVP33QW.mjs";
3
+ } from "../../chunk-MAEPOYB6.mjs";
4
4
  import "../../chunk-OUWBQF3Z.mjs";
5
- import "../../chunk-6JZQ6WGN.mjs";
5
+ import "../../chunk-OMISYKWR.mjs";
6
6
  import "../../chunk-QED3Q6Z2.mjs";
7
7
  export {
8
8
  FetchInterceptor
@@ -1,6 +1,6 @@
1
1
  import { FetchInterceptor } from '../interceptors/fetch/index.js';
2
2
  import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
3
- import '../glossary-640c9679.js';
3
+ import '../glossary-1c204f45.js';
4
4
  import '@open-draft/deferred-promise';
5
5
  import '../Interceptor-af98b768.js';
6
6
  import '@open-draft/logger';
@@ -1,18 +1,18 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkWKPHOWK2js = require('../chunk-WKPHOWK2.js');
3
+ var _chunkPSX5J3RFjs = require('../chunk-PSX5J3RF.js');
4
4
  require('../chunk-LK6DILFK.js');
5
5
 
6
6
 
7
- var _chunk5Q77IUWFjs = require('../chunk-5Q77IUWF.js');
7
+ var _chunkWBHIW62Pjs = require('../chunk-WBHIW62P.js');
8
8
  require('../chunk-MQJ3JOOK.js');
9
- require('../chunk-LR5DGYDZ.js');
9
+ require('../chunk-2CRB3JAQ.js');
10
10
  require('../chunk-TIPR373R.js');
11
11
 
12
12
  // src/presets/browser.ts
13
13
  var browser_default = [
14
- new (0, _chunk5Q77IUWFjs.FetchInterceptor)(),
15
- new (0, _chunkWKPHOWK2js.XMLHttpRequestInterceptor)()
14
+ new (0, _chunkWBHIW62Pjs.FetchInterceptor)(),
15
+ new (0, _chunkPSX5J3RFjs.XMLHttpRequestInterceptor)()
16
16
  ];
17
17
 
18
18
 
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  XMLHttpRequestInterceptor
3
- } from "../chunk-FIBZ2AT2.mjs";
3
+ } from "../chunk-732REFPX.mjs";
4
4
  import "../chunk-6HYIRFX2.mjs";
5
5
  import {
6
6
  FetchInterceptor
7
- } from "../chunk-ZQVP33QW.mjs";
7
+ } from "../chunk-MAEPOYB6.mjs";
8
8
  import "../chunk-OUWBQF3Z.mjs";
9
- import "../chunk-6JZQ6WGN.mjs";
9
+ import "../chunk-OMISYKWR.mjs";
10
10
  import "../chunk-QED3Q6Z2.mjs";
11
11
 
12
12
  // src/presets/browser.ts
@@ -1,5 +1,5 @@
1
1
  import { EventMap, Listener } from 'strict-event-emitter';
2
- import { f as Interceptor, E as ExtractEventNames } from './Interceptor-6696a18d.js';
2
+ import { f as Interceptor, E as ExtractEventNames } from './Interceptor-88ee47c0.js';
3
3
 
4
4
  interface BatchInterceptorOptions<InterceptorList extends ReadonlyArray<Interceptor<any>>> {
5
5
  name: string;
@@ -30,6 +30,17 @@ type HttpRequestEventMap = {
30
30
  requestId: string;
31
31
  }
32
32
  ];
33
+ unhandledException: [
34
+ args: {
35
+ error: unknown;
36
+ request: Request;
37
+ requestId: string;
38
+ controller: {
39
+ respondWith(response: Response): void;
40
+ errorWith(error?: Error): void;
41
+ };
42
+ }
43
+ ];
33
44
  };
34
45
 
35
46
  type InterceptorEventMap = Record<string, any>;
@@ -1,6 +1,6 @@
1
1
  import { ChildProcess } from 'child_process';
2
- import { f as Interceptor, H as HttpRequestEventMap } from './Interceptor-6696a18d.js';
3
- import { a as BatchInterceptor } from './BatchInterceptor-cb145daa.js';
2
+ import { f as Interceptor, H as HttpRequestEventMap } from './Interceptor-88ee47c0.js';
3
+ import { a as BatchInterceptor } from './BatchInterceptor-2badedde.js';
4
4
  import { ClientRequestInterceptor } from './interceptors/ClientRequest/index.js';
5
5
  import { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest/index.js';
6
6
  import '@open-draft/deferred-promise';
@@ -3,12 +3,12 @@
3
3
  var _chunkLTEXDYJ6js = require('./chunk-LTEXDYJ6.js');
4
4
 
5
5
 
6
- var _chunkUUKG6TUDjs = require('./chunk-UUKG6TUD.js');
6
+ var _chunkHAIWBQD5js = require('./chunk-HAIWBQD5.js');
7
7
 
8
8
 
9
- var _chunkKHEPLFR3js = require('./chunk-KHEPLFR3.js');
9
+ var _chunkAPT7KA3Bjs = require('./chunk-APT7KA3B.js');
10
10
  require('./chunk-LK6DILFK.js');
11
- require('./chunk-FZJKKO5H.js');
11
+ require('./chunk-EIBTX65O.js');
12
12
 
13
13
 
14
14
 
@@ -23,8 +23,8 @@ var RemoteHttpInterceptor = class extends _chunkLTEXDYJ6js.BatchInterceptor {
23
23
  super({
24
24
  name: "remote-interceptor",
25
25
  interceptors: [
26
- new (0, _chunkUUKG6TUDjs.ClientRequestInterceptor)(),
27
- new (0, _chunkKHEPLFR3js.XMLHttpRequestInterceptor)()
26
+ new (0, _chunkHAIWBQD5js.ClientRequestInterceptor)(),
27
+ new (0, _chunkAPT7KA3Bjs.XMLHttpRequestInterceptor)()
28
28
  ]
29
29
  });
30
30
  }
@@ -3,12 +3,12 @@ import {
3
3
  } from "./chunk-6FRASLM3.mjs";
4
4
  import {
5
5
  ClientRequestInterceptor
6
- } from "./chunk-TCATD46T.mjs";
6
+ } from "./chunk-JMNEFEYU.mjs";
7
7
  import {
8
8
  XMLHttpRequestInterceptor
9
- } from "./chunk-OXXEWTTM.mjs";
9
+ } from "./chunk-KSHIDGUL.mjs";
10
10
  import "./chunk-6HYIRFX2.mjs";
11
- import "./chunk-HAGW22AN.mjs";
11
+ import "./chunk-OJ6O4LSC.mjs";
12
12
  import {
13
13
  emitAsync,
14
14
  toInteractiveRequest
@@ -5,7 +5,7 @@
5
5
  var _chunkLK6DILFKjs = require('./chunk-LK6DILFK.js');
6
6
 
7
7
 
8
- var _chunkFZJKKO5Hjs = require('./chunk-FZJKKO5H.js');
8
+ var _chunkEIBTX65Ojs = require('./chunk-EIBTX65O.js');
9
9
 
10
10
 
11
11
 
@@ -708,6 +708,20 @@ function createXMLHttpRequestProxy({
708
708
  }
709
709
  return;
710
710
  }
711
+ if (emitter.listenerCount("unhandledException") > 0) {
712
+ await _chunkMQJ3JOOKjs.emitAsync.call(void 0, emitter, "unhandledException", {
713
+ error: resolverResult.error,
714
+ request,
715
+ requestId,
716
+ controller: {
717
+ respondWith: xhrRequestController.respondWith.bind(xhrRequestController),
718
+ errorWith: xhrRequestController.errorWith.bind(xhrRequestController)
719
+ }
720
+ });
721
+ if (originalRequest.readyState > XMLHttpRequest.OPENED) {
722
+ return;
723
+ }
724
+ }
711
725
  xhrRequestController.respondWith(
712
726
  _chunkE4AC7YACjs.createServerErrorResponse.call(void 0, resolverResult.error)
713
727
  );
@@ -769,7 +783,7 @@ var _XMLHttpRequestInterceptor = class extends _chunkE4AC7YACjs.Interceptor {
769
783
  logger.info('patching "XMLHttpRequest" module...');
770
784
  const PureXMLHttpRequest = globalThis.XMLHttpRequest;
771
785
  _outvariant.invariant.call(void 0,
772
- !PureXMLHttpRequest[_chunkFZJKKO5Hjs.IS_PATCHED_MODULE],
786
+ !PureXMLHttpRequest[_chunkEIBTX65Ojs.IS_PATCHED_MODULE],
773
787
  'Failed to patch the "XMLHttpRequest" module: already patched.'
774
788
  );
775
789
  globalThis.XMLHttpRequest = createXMLHttpRequestProxy({
@@ -780,13 +794,13 @@ var _XMLHttpRequestInterceptor = class extends _chunkE4AC7YACjs.Interceptor {
780
794
  'native "XMLHttpRequest" module patched!',
781
795
  globalThis.XMLHttpRequest.name
782
796
  );
783
- Object.defineProperty(globalThis.XMLHttpRequest, _chunkFZJKKO5Hjs.IS_PATCHED_MODULE, {
797
+ Object.defineProperty(globalThis.XMLHttpRequest, _chunkEIBTX65Ojs.IS_PATCHED_MODULE, {
784
798
  enumerable: true,
785
799
  configurable: true,
786
800
  value: true
787
801
  });
788
802
  this.subscriptions.push(() => {
789
- Object.defineProperty(globalThis.XMLHttpRequest, _chunkFZJKKO5Hjs.IS_PATCHED_MODULE, {
803
+ Object.defineProperty(globalThis.XMLHttpRequest, _chunkEIBTX65Ojs.IS_PATCHED_MODULE, {
790
804
  value: void 0
791
805
  });
792
806
  globalThis.XMLHttpRequest = PureXMLHttpRequest;
@@ -803,4 +817,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
803
817
 
804
818
 
805
819
  exports.XMLHttpRequestInterceptor = XMLHttpRequestInterceptor;
806
- //# sourceMappingURL=chunk-KHEPLFR3.js.map
820
+ //# sourceMappingURL=chunk-APT7KA3B.js.map