@mswjs/interceptors 0.32.2 → 0.33.1

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 (120) hide show
  1. package/README.md +35 -7
  2. package/lib/browser/{chunk-732REFPX.mjs → chunk-5ETVT6GU.mjs} +28 -79
  3. package/lib/browser/chunk-5ETVT6GU.mjs.map +1 -0
  4. package/lib/browser/chunk-6MBJUL74.js +142 -0
  5. package/lib/browser/chunk-6MBJUL74.js.map +1 -0
  6. package/lib/browser/chunk-7A4UJNSW.mjs +196 -0
  7. package/lib/browser/chunk-7A4UJNSW.mjs.map +1 -0
  8. package/lib/browser/{chunk-PSX5J3RF.js → chunk-7GVJEW45.js} +30 -81
  9. package/lib/browser/chunk-7GVJEW45.js.map +1 -0
  10. package/lib/browser/{chunk-2CRB3JAQ.js → chunk-FXSPMSSQ.js} +1 -1
  11. package/lib/browser/chunk-FXSPMSSQ.js.map +1 -0
  12. package/lib/browser/{chunk-OMISYKWR.mjs → chunk-GGUENBDN.mjs} +1 -1
  13. package/lib/browser/chunk-GGUENBDN.mjs.map +1 -0
  14. package/lib/browser/chunk-NU2MPFD6.mjs +142 -0
  15. package/lib/browser/chunk-NU2MPFD6.mjs.map +1 -0
  16. package/lib/browser/chunk-VRKVKT62.js +196 -0
  17. package/lib/browser/chunk-VRKVKT62.js.map +1 -0
  18. package/lib/browser/glossary-7d7adb4b.d.ts +66 -0
  19. package/lib/browser/index.d.ts +1 -1
  20. package/lib/browser/index.js +2 -2
  21. package/lib/browser/index.mjs +1 -1
  22. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +2 -6
  23. package/lib/browser/interceptors/XMLHttpRequest/index.js +4 -4
  24. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +3 -3
  25. package/lib/browser/interceptors/fetch/index.d.ts +1 -1
  26. package/lib/browser/interceptors/fetch/index.js +4 -4
  27. package/lib/browser/interceptors/fetch/index.mjs +3 -3
  28. package/lib/browser/presets/browser.d.ts +1 -1
  29. package/lib/browser/presets/browser.js +6 -6
  30. package/lib/browser/presets/browser.mjs +4 -4
  31. package/lib/node/{BatchInterceptor-2badedde.d.ts → BatchInterceptor-13d40c95.d.ts} +1 -1
  32. package/lib/node/{Interceptor-88ee47c0.d.ts → Interceptor-a31b1217.d.ts} +35 -13
  33. package/lib/node/RemoteHttpInterceptor.d.ts +2 -2
  34. package/lib/node/RemoteHttpInterceptor.js +55 -52
  35. package/lib/node/RemoteHttpInterceptor.js.map +1 -1
  36. package/lib/node/RemoteHttpInterceptor.mjs +53 -50
  37. package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
  38. package/lib/node/{chunk-2COJKQQB.js → chunk-42632LKH.js} +3 -3
  39. package/lib/node/chunk-5WWNCLB3.js +196 -0
  40. package/lib/node/chunk-5WWNCLB3.js.map +1 -0
  41. package/lib/node/{chunk-TGTPXCLF.mjs → chunk-BUCULLYM.mjs} +1 -1
  42. package/lib/node/{chunk-TGTPXCLF.mjs.map → chunk-BUCULLYM.mjs.map} +1 -1
  43. package/lib/node/{chunk-OJ6O4LSC.mjs → chunk-BZ3Y7YV5.mjs} +1 -1
  44. package/lib/node/chunk-BZ3Y7YV5.mjs.map +1 -0
  45. package/lib/node/{chunk-PNWPIDEL.mjs → chunk-HGQLG7KE.mjs} +2 -2
  46. package/lib/node/{chunk-EIBTX65O.js → chunk-IDEEMJ3F.js} +1 -1
  47. package/lib/node/chunk-IDEEMJ3F.js.map +1 -0
  48. package/lib/node/chunk-KY3RJ2M3.mjs +196 -0
  49. package/lib/node/chunk-KY3RJ2M3.mjs.map +1 -0
  50. package/lib/node/{chunk-CFRXZJO4.js → chunk-MCSMMYNC.js} +81 -89
  51. package/lib/node/chunk-MCSMMYNC.js.map +1 -0
  52. package/lib/node/{chunk-CMVICWQS.mjs → chunk-MII7WYYZ.mjs} +73 -81
  53. package/lib/node/chunk-MII7WYYZ.mjs.map +1 -0
  54. package/lib/node/{chunk-PYD4E2EJ.js → chunk-P6QG76R3.js} +34 -85
  55. package/lib/node/chunk-P6QG76R3.js.map +1 -0
  56. package/lib/node/{chunk-DV4PBH4D.mjs → chunk-TOV4TYIX.mjs} +29 -80
  57. package/lib/node/chunk-TOV4TYIX.mjs.map +1 -0
  58. package/lib/node/{chunk-BFLYGQ6D.js → chunk-YGM3BCJU.js} +1 -1
  59. package/lib/node/chunk-YGM3BCJU.js.map +1 -0
  60. package/lib/node/index.d.ts +2 -2
  61. package/lib/node/index.js +4 -4
  62. package/lib/node/index.mjs +3 -3
  63. package/lib/node/interceptors/ClientRequest/index.d.ts +2 -2
  64. package/lib/node/interceptors/ClientRequest/index.js +4 -4
  65. package/lib/node/interceptors/ClientRequest/index.mjs +3 -3
  66. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +2 -6
  67. package/lib/node/interceptors/XMLHttpRequest/index.js +5 -5
  68. package/lib/node/interceptors/XMLHttpRequest/index.mjs +4 -4
  69. package/lib/node/interceptors/fetch/index.d.ts +1 -1
  70. package/lib/node/interceptors/fetch/index.js +52 -123
  71. package/lib/node/interceptors/fetch/index.js.map +1 -1
  72. package/lib/node/interceptors/fetch/index.mjs +50 -121
  73. package/lib/node/interceptors/fetch/index.mjs.map +1 -1
  74. package/lib/node/presets/node.d.ts +1 -1
  75. package/lib/node/presets/node.js +7 -7
  76. package/lib/node/presets/node.mjs +5 -5
  77. package/package.json +2 -2
  78. package/src/InterceptorError.ts +7 -0
  79. package/src/RemoteHttpInterceptor.ts +62 -57
  80. package/src/RequestController.test.ts +49 -0
  81. package/src/RequestController.ts +81 -0
  82. package/src/glossary.ts +4 -6
  83. package/src/interceptors/ClientRequest/MockHttpSocket.ts +1 -1
  84. package/src/interceptors/ClientRequest/index.test.ts +23 -34
  85. package/src/interceptors/ClientRequest/index.ts +21 -82
  86. package/src/interceptors/ClientRequest/utils/recordRawHeaders.test.ts +159 -0
  87. package/src/interceptors/ClientRequest/utils/recordRawHeaders.ts +80 -27
  88. package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +1 -1
  89. package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +27 -108
  90. package/src/interceptors/XMLHttpRequest/index.ts +0 -6
  91. package/src/interceptors/fetch/index.ts +52 -169
  92. package/src/utils/handleRequest.ts +213 -0
  93. package/src/utils/responseUtils.ts +4 -4
  94. package/lib/browser/chunk-2CRB3JAQ.js.map +0 -1
  95. package/lib/browser/chunk-732REFPX.mjs.map +0 -1
  96. package/lib/browser/chunk-MAEPOYB6.mjs +0 -213
  97. package/lib/browser/chunk-MAEPOYB6.mjs.map +0 -1
  98. package/lib/browser/chunk-MQJ3JOOK.js +0 -49
  99. package/lib/browser/chunk-MQJ3JOOK.js.map +0 -1
  100. package/lib/browser/chunk-OMISYKWR.mjs.map +0 -1
  101. package/lib/browser/chunk-OUWBQF3Z.mjs +0 -49
  102. package/lib/browser/chunk-OUWBQF3Z.mjs.map +0 -1
  103. package/lib/browser/chunk-PSX5J3RF.js.map +0 -1
  104. package/lib/browser/chunk-WBHIW62P.js +0 -213
  105. package/lib/browser/chunk-WBHIW62P.js.map +0 -1
  106. package/lib/browser/glossary-1c204f45.d.ts +0 -44
  107. package/lib/node/chunk-BFLYGQ6D.js.map +0 -1
  108. package/lib/node/chunk-CFRXZJO4.js.map +0 -1
  109. package/lib/node/chunk-CMVICWQS.mjs.map +0 -1
  110. package/lib/node/chunk-DV4PBH4D.mjs.map +0 -1
  111. package/lib/node/chunk-EIBTX65O.js.map +0 -1
  112. package/lib/node/chunk-KWV3JXSI.mjs +0 -49
  113. package/lib/node/chunk-KWV3JXSI.mjs.map +0 -1
  114. package/lib/node/chunk-OJ6O4LSC.mjs.map +0 -1
  115. package/lib/node/chunk-PYD4E2EJ.js.map +0 -1
  116. package/lib/node/chunk-UXCYRE4F.js +0 -49
  117. package/lib/node/chunk-UXCYRE4F.js.map +0 -1
  118. package/src/utils/toInteractiveRequest.ts +0 -23
  119. /package/lib/node/{chunk-2COJKQQB.js.map → chunk-42632LKH.js.map} +0 -0
  120. /package/lib/node/{chunk-PNWPIDEL.mjs.map → chunk-HGQLG7KE.mjs.map} +0 -0
@@ -0,0 +1,142 @@
1
+ import {
2
+ RequestController,
3
+ emitAsync,
4
+ handleRequest
5
+ } from "./chunk-7A4UJNSW.mjs";
6
+ import {
7
+ IS_PATCHED_MODULE
8
+ } from "./chunk-GGUENBDN.mjs";
9
+ import {
10
+ Interceptor,
11
+ createRequestId
12
+ } from "./chunk-QED3Q6Z2.mjs";
13
+
14
+ // src/interceptors/fetch/index.ts
15
+ import { invariant } from "outvariant";
16
+ import { DeferredPromise } from "@open-draft/deferred-promise";
17
+
18
+ // src/utils/canParseUrl.ts
19
+ function canParseUrl(url) {
20
+ try {
21
+ new URL(url);
22
+ return true;
23
+ } catch (_error) {
24
+ return false;
25
+ }
26
+ }
27
+
28
+ // src/interceptors/fetch/index.ts
29
+ var _FetchInterceptor = class extends Interceptor {
30
+ constructor() {
31
+ super(_FetchInterceptor.symbol);
32
+ }
33
+ checkEnvironment() {
34
+ return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
35
+ }
36
+ async setup() {
37
+ const pureFetch = globalThis.fetch;
38
+ invariant(
39
+ !pureFetch[IS_PATCHED_MODULE],
40
+ 'Failed to patch the "fetch" module: already patched.'
41
+ );
42
+ globalThis.fetch = async (input, init) => {
43
+ const requestId = createRequestId();
44
+ const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
45
+ const request = new Request(resolvedInput, init);
46
+ const responsePromise = new DeferredPromise();
47
+ const controller = new RequestController(request);
48
+ this.logger.info("[%s] %s", request.method, request.url);
49
+ this.logger.info("awaiting for the mocked response...");
50
+ this.logger.info(
51
+ 'emitting the "request" event for %s listener(s)...',
52
+ this.emitter.listenerCount("request")
53
+ );
54
+ const isRequestHandled = await handleRequest({
55
+ request,
56
+ requestId,
57
+ emitter: this.emitter,
58
+ controller,
59
+ onResponse: async (response) => {
60
+ this.logger.info("received mocked response!", {
61
+ response
62
+ });
63
+ if (this.emitter.listenerCount("response") > 0) {
64
+ this.logger.info('emitting the "response" event...');
65
+ await emitAsync(this.emitter, "response", {
66
+ // Clone the mocked response for the "response" event listener.
67
+ // This way, the listener can read the response and not lock its body
68
+ // for the actual fetch consumer.
69
+ response: response.clone(),
70
+ isMockedResponse: true,
71
+ request,
72
+ requestId
73
+ });
74
+ }
75
+ Object.defineProperty(response, "url", {
76
+ writable: false,
77
+ enumerable: true,
78
+ configurable: false,
79
+ value: request.url
80
+ });
81
+ responsePromise.resolve(response);
82
+ },
83
+ onRequestError: (response) => {
84
+ this.logger.info("request has errored!", { response });
85
+ responsePromise.reject(createNetworkError(response));
86
+ },
87
+ onError: (error) => {
88
+ this.logger.info("request has been aborted!", { error });
89
+ responsePromise.reject(error);
90
+ }
91
+ });
92
+ if (isRequestHandled) {
93
+ this.logger.info("request has been handled, returning mock promise...");
94
+ return responsePromise;
95
+ }
96
+ this.logger.info(
97
+ "no mocked response received, performing request as-is..."
98
+ );
99
+ return pureFetch(request).then((response) => {
100
+ this.logger.info("original fetch performed", response);
101
+ if (this.emitter.listenerCount("response") > 0) {
102
+ this.logger.info('emitting the "response" event...');
103
+ const responseClone = response.clone();
104
+ this.emitter.emit("response", {
105
+ response: responseClone,
106
+ isMockedResponse: false,
107
+ request,
108
+ requestId
109
+ });
110
+ }
111
+ return response;
112
+ });
113
+ };
114
+ Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
115
+ enumerable: true,
116
+ configurable: true,
117
+ value: true
118
+ });
119
+ this.subscriptions.push(() => {
120
+ Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
121
+ value: void 0
122
+ });
123
+ globalThis.fetch = pureFetch;
124
+ this.logger.info(
125
+ 'restored native "globalThis.fetch"!',
126
+ globalThis.fetch.name
127
+ );
128
+ });
129
+ }
130
+ };
131
+ var FetchInterceptor = _FetchInterceptor;
132
+ FetchInterceptor.symbol = Symbol("fetch");
133
+ function createNetworkError(cause) {
134
+ return Object.assign(new TypeError("Failed to fetch"), {
135
+ cause
136
+ });
137
+ }
138
+
139
+ export {
140
+ FetchInterceptor
141
+ };
142
+ //# sourceMappingURL=chunk-NU2MPFD6.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/interceptors/fetch/index.ts","../../src/utils/canParseUrl.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { RequestController } from '../../RequestController'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { handleRequest } from '../../utils/handleRequest'\nimport { canParseUrl } from '../../utils/canParseUrl'\nimport { createRequestId } from '../../createRequestId'\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 const responsePromise = new DeferredPromise<Response>()\n const controller = new RequestController(request)\n\n this.logger.info('[%s] %s', request.method, request.url)\n this.logger.info('awaiting for the mocked response...')\n\n this.logger.info(\n 'emitting the \"request\" event for %s listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n const isRequestHandled = await handleRequest({\n request,\n requestId,\n emitter: this.emitter,\n controller,\n onResponse: async (response) => {\n this.logger.info('received mocked response!', {\n response,\n })\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n // Await the response listeners to finish before resolving\n // the response promise. This ensures all your logic finishes\n // before the interceptor resolves the pending response.\n await emitAsync(this.emitter, 'response', {\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 response: response.clone(),\n isMockedResponse: true,\n request,\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 onRequestError: (response) => {\n this.logger.info('request has errored!', { response })\n responsePromise.reject(createNetworkError(response))\n },\n onError: (error) => {\n this.logger.info('request has been aborted!', { error })\n responsePromise.reject(error)\n },\n })\n\n if (isRequestHandled) {\n this.logger.info('request has been handled, returning mock promise...')\n return responsePromise\n }\n\n this.logger.info(\n 'no mocked response received, performing request as-is...'\n )\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,\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"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;ACIzB,SAAS,YAAY,KAAsB;AAChD,MAAI;AACF,QAAI,IAAI,GAAG;AACX,WAAO;AAAA,EACT,SAAS,QAAP;AACA,WAAO;AAAA,EACT;AACF;;;ADFO,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;AAAA,IACF;AAEA,eAAW,QAAQ,OAAO,OAAO,SAAS;AACxC,YAAM,YAAY,gBAAgB;AAQlC,YAAM,gBACJ,OAAO,UAAU,YACjB,OAAO,aAAa,eACpB,CAAC,YAAY,KAAK,IACd,IAAI,IAAI,OAAO,SAAS,MAAM,IAC9B;AAEN,YAAM,UAAU,IAAI,QAAQ,eAAe,IAAI;AAC/C,YAAM,kBAAkB,IAAI,gBAA0B;AACtD,YAAM,aAAa,IAAI,kBAAkB,OAAO;AAEhD,WAAK,OAAO,KAAK,WAAW,QAAQ,QAAQ,QAAQ,GAAG;AACvD,WAAK,OAAO,KAAK,qCAAqC;AAEtD,WAAK,OAAO;AAAA,QACV;AAAA,QACA,KAAK,QAAQ,cAAc,SAAS;AAAA,MACtC;AAEA,YAAM,mBAAmB,MAAM,cAAc;AAAA,QAC3C;AAAA,QACA;AAAA,QACA,SAAS,KAAK;AAAA,QACd;AAAA,QACA,YAAY,OAAO,aAAa;AAC9B,eAAK,OAAO,KAAK,6BAA6B;AAAA,YAC5C;AAAA,UACF,CAAC;AAED,cAAI,KA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d,OAAO;AAAA,IACT,CAAC;AAED,SAAK,cAAc,KAAK,MAAM;AAC5B,aAAO,eAAe,WAAW,OAAO,mBAAmB;AAAA,QACzD,OAAO;AAAA,MACT,CAAC;AAED,iBAAW,QAAQ;AAEnB,WAAK,OAAO;AAAA,QACV;AAAA,QACA,WAAW,MAAM;AAAA,MACnB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAjJO,IAAM,mBAAN;AAAM,iBACJ,SAAS,OAAO,OAAO;AAkJhC,SAAS,mBAAmB,OAAgB;AAC1C,SAAO,OAAO,OAAO,IAAI,UAAU,iBAAiB,GAAG;AAAA,IACrD;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -0,0 +1,196 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+ var _chunkFXSPMSSQjs = require('./chunk-FXSPMSSQ.js');
5
+
6
+ // src/RequestController.ts
7
+ var _outvariant = require('outvariant');
8
+ var _deferredpromise = require('@open-draft/deferred-promise');
9
+
10
+ // src/InterceptorError.ts
11
+ var InterceptorError = class extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ this.name = "InterceptorError";
15
+ Object.setPrototypeOf(this, InterceptorError.prototype);
16
+ }
17
+ };
18
+
19
+ // src/RequestController.ts
20
+ var kRequestHandled = Symbol("kRequestHandled");
21
+ var kResponsePromise = Symbol("kResponsePromise");
22
+ var RequestController = class {
23
+ constructor(request) {
24
+ this.request = request;
25
+ this[kRequestHandled] = false;
26
+ this[kResponsePromise] = new (0, _deferredpromise.DeferredPromise)();
27
+ }
28
+ /**
29
+ * Respond to this request with the given `Response` instance.
30
+ * @example
31
+ * controller.respondWith(new Response())
32
+ * controller.respondWith(Response.json({ id }))
33
+ * controller.respondWith(Response.error())
34
+ */
35
+ respondWith(response) {
36
+ _outvariant.invariant.as(
37
+ InterceptorError,
38
+ !this[kRequestHandled],
39
+ 'Failed to respond to the "%s %s" request: the "request" event has already been handled.',
40
+ this.request.method,
41
+ this.request.url
42
+ );
43
+ this[kRequestHandled] = true;
44
+ this[kResponsePromise].resolve(response);
45
+ }
46
+ /**
47
+ * Error this request with the given error.
48
+ * @example
49
+ * controller.errorWith()
50
+ * controller.errorWith(new Error('Oops!'))
51
+ */
52
+ errorWith(error) {
53
+ _outvariant.invariant.as(
54
+ InterceptorError,
55
+ !this[kRequestHandled],
56
+ 'Failed to error the "%s %s" request: the "request" event has already been handled.',
57
+ this.request.method,
58
+ this.request.url
59
+ );
60
+ this[kRequestHandled] = true;
61
+ this[kResponsePromise].resolve(error);
62
+ }
63
+ };
64
+ kResponsePromise, kRequestHandled;
65
+
66
+ // src/utils/emitAsync.ts
67
+ async function emitAsync(emitter, eventName, ...data) {
68
+ const listners = emitter.listeners(eventName);
69
+ if (listners.length === 0) {
70
+ return;
71
+ }
72
+ for (const listener of listners) {
73
+ await listener.apply(emitter, data);
74
+ }
75
+ }
76
+
77
+ // src/utils/handleRequest.ts
78
+
79
+ var _until = require('@open-draft/until');
80
+
81
+ // src/utils/isNodeLikeError.ts
82
+ function isNodeLikeError(error) {
83
+ if (error == null) {
84
+ return false;
85
+ }
86
+ if (!(error instanceof Error)) {
87
+ return false;
88
+ }
89
+ return "code" in error && "errno" in error;
90
+ }
91
+
92
+ // src/utils/handleRequest.ts
93
+ async function handleRequest(options) {
94
+ const handleResponse = (response) => {
95
+ if (response instanceof Error) {
96
+ options.onError(response);
97
+ } else if (_chunkFXSPMSSQjs.isResponseError.call(void 0, response)) {
98
+ options.onRequestError(response);
99
+ } else {
100
+ options.onResponse(response);
101
+ }
102
+ return true;
103
+ };
104
+ const handleResponseError = (error) => {
105
+ if (error instanceof InterceptorError) {
106
+ throw result.error;
107
+ }
108
+ if (isNodeLikeError(error)) {
109
+ options.onError(error);
110
+ return true;
111
+ }
112
+ if (error instanceof Response) {
113
+ return handleResponse(error);
114
+ }
115
+ return false;
116
+ };
117
+ options.emitter.once("request", ({ requestId: pendingRequestId }) => {
118
+ if (pendingRequestId !== options.requestId) {
119
+ return;
120
+ }
121
+ if (options.controller[kResponsePromise].state === "pending") {
122
+ options.controller[kResponsePromise].resolve(void 0);
123
+ }
124
+ });
125
+ const requestAbortPromise = new (0, _deferredpromise.DeferredPromise)();
126
+ if (options.request.signal) {
127
+ options.request.signal.addEventListener(
128
+ "abort",
129
+ () => {
130
+ requestAbortPromise.reject(options.request.signal.reason);
131
+ },
132
+ { once: true }
133
+ );
134
+ }
135
+ const result = await _until.until.call(void 0, async () => {
136
+ const requestListtenersPromise = emitAsync(options.emitter, "request", {
137
+ requestId: options.requestId,
138
+ request: options.request,
139
+ controller: options.controller
140
+ });
141
+ await Promise.race([
142
+ // Short-circuit the request handling promise if the request gets aborted.
143
+ requestAbortPromise,
144
+ requestListtenersPromise,
145
+ options.controller[kResponsePromise]
146
+ ]);
147
+ const mockedResponse = await options.controller[kResponsePromise];
148
+ return mockedResponse;
149
+ });
150
+ if (requestAbortPromise.state === "rejected") {
151
+ options.onError(requestAbortPromise.rejectionReason);
152
+ return true;
153
+ }
154
+ if (result.error) {
155
+ if (handleResponseError(result.error)) {
156
+ return true;
157
+ }
158
+ if (options.emitter.listenerCount("unhandledException") > 0) {
159
+ const unhandledExceptionController = new RequestController(
160
+ options.request
161
+ );
162
+ await emitAsync(options.emitter, "unhandledException", {
163
+ error: result.error,
164
+ request: options.request,
165
+ requestId: options.requestId,
166
+ controller: unhandledExceptionController
167
+ }).then(() => {
168
+ if (unhandledExceptionController[kResponsePromise].state === "pending") {
169
+ unhandledExceptionController[kResponsePromise].resolve(void 0);
170
+ }
171
+ });
172
+ const nextResult = await _until.until.call(void 0,
173
+ () => unhandledExceptionController[kResponsePromise]
174
+ );
175
+ if (nextResult.error) {
176
+ return handleResponseError(nextResult.error);
177
+ }
178
+ if (nextResult.data) {
179
+ return handleResponse(nextResult.data);
180
+ }
181
+ }
182
+ options.onResponse(_chunkFXSPMSSQjs.createServerErrorResponse.call(void 0, result.error));
183
+ return true;
184
+ }
185
+ if (result.data) {
186
+ return handleResponse(result.data);
187
+ }
188
+ return false;
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ exports.RequestController = RequestController; exports.emitAsync = emitAsync; exports.handleRequest = handleRequest;
196
+ //# sourceMappingURL=chunk-VRKVKT62.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/RequestController.ts","../../src/InterceptorError.ts","../../src/utils/emitAsync.ts","../../src/utils/handleRequest.ts","../../src/utils/isNodeLikeError.ts"],"names":["DeferredPromise"],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;ACDzB,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAY,SAAkB;AAC5B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,iBAAiB,SAAS;AAAA,EACxD;AACF;;;ADFA,IAAM,kBAAkB,OAAO,iBAAiB;AACzC,IAAM,mBAAmB,OAAO,kBAAkB;AAElD,IAAM,oBAAN,MAAwB;AAAA,EAgB7B,YAAoB,SAAkB;AAAlB;AAClB,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,IAAI,IAAI,gBAAgB;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,YAAY,UAA0B;AAC3C,cAAU;AAAA,MACR;AAAA,MACA,CAAC,KAAK,eAAe;AAAA,MACrB;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,EAAE,QAAQ,QAAQ;AAAA,EASzC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,UAAU,OAAqB;AACpC,cAAU;AAAA,MACR;AAAA,MACA,CAAC,KAAK,eAAe;AAAA,MACrB;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,eAAe,IAAI;AAOxB,SAAK,gBAAgB,EAAE,QAAQ,KAAK;AAA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I,WAAW,OAAO;AACpB,eAAO,oBAAoB,WAAW,KAAK;AAAA,MAC7C;AAEA,UAAI,WAAW,MAAM;AACnB,eAAO,eAAe,WAAW,IAAI;AAAA,MACvC;AAAA,IACF;AAGA,YAAQ,WAAW,0BAA0B,OAAO,KAAK,CAAC;AAC1D,WAAO;AAAA,EACT;AAQA,MAAI,OAAO,MAAM;AACf,WAAO,eAAe,OAAO,IAAI;AAAA,EACnC;AAIA,SAAO;AACT","sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { InterceptorError } from './InterceptorError'\n\nconst kRequestHandled = Symbol('kRequestHandled')\nexport const kResponsePromise = Symbol('kResponsePromise')\n\nexport class RequestController {\n /**\n * Internal response promise.\n * Available only for the library internals to grab the\n * response instance provided by the developer.\n * @note This promise cannot be rejected. It's either infinitely\n * pending or resolved with whichever Response was passed to `respondWith()`.\n */\n [kResponsePromise]: DeferredPromise<Response | Error | undefined>;\n\n /**\n * Internal flag indicating if this request has been handled.\n * @note The response promise becomes \"fulfilled\" on the next tick.\n */\n [kRequestHandled]: boolean\n\n constructor(private request: Request) {\n this[kRequestHandled] = false\n this[kResponsePromise] = new DeferredPromise()\n }\n\n /**\n * Respond to this request with the given `Response` instance.\n * @example\n * controller.respondWith(new Response())\n * controller.respondWith(Response.json({ id }))\n * controller.respondWith(Response.error())\n */\n public respondWith(response: Response): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to respond to the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n this[kResponsePromise].resolve(response)\n\n /**\n * @note The request conrtoller doesn't do anything\n * apart from letting the interceptor await the response\n * provided by the developer through the response promise.\n * Each interceptor implements the actual respondWith/errorWith\n * logic based on that interceptor's needs.\n */\n }\n\n /**\n * Error this request with the given error.\n * @example\n * controller.errorWith()\n * controller.errorWith(new Error('Oops!'))\n */\n public errorWith(error?: Error): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to error the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n\n /**\n * @note Resolve the response promise, not reject.\n * This helps us differentiate between unhandled exceptions\n * and intended errors (\"errorWith\") while waiting for the response.\n */\n this[kResponsePromise].resolve(error)\n }\n}\n","export class InterceptorError extends Error {\n constructor(message?: string) {\n super(message)\n this.name = 'InterceptorError'\n Object.setPrototypeOf(this, InterceptorError.prototype)\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listners = emitter.listeners(eventName)\n\n if (listners.length === 0) {\n return\n }\n\n for (const listener of listners) {\n await listener.apply(emitter, data)\n }\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { kResponsePromise, RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n ResponseError,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n\n /**\n * Called when the request has been handled\n * with the given `Response` instance.\n */\n onResponse: (response: Response) => void\n\n /**\n * Called when the request has been handled\n * with the given `Response.error()` instance.\n */\n onRequestError: (response: ResponseError) => void\n\n /**\n * Called when an unhandled error happens during the\n * request handling. This is never a thrown error/response.\n */\n onError: (error: unknown) => void\n}\n\n/**\n * @returns {Promise<boolean>} Indicates whether the request has been handled.\n */\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<boolean> {\n const handleResponse = (response: Response | Error): true => {\n if (response instanceof Error) {\n options.onError(response)\n }\n\n // Handle \"Response.error()\" instances.\n else if (isResponseError(response)) {\n options.onRequestError(response)\n } else {\n options.onResponse(response)\n }\n\n return true\n }\n\n const handleResponseError = (error: unknown): boolean => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n options.onError(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n options.emitter.once('request', ({ requestId: pendingRequestId }) => {\n if (pendingRequestId !== options.requestId) {\n return\n }\n\n if (options.controller[kResponsePromise].state === 'pending') {\n options.controller[kResponsePromise].resolve(undefined)\n }\n })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListtenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListtenersPromise,\n options.controller[kResponsePromise],\n ])\n\n // The response promise will settle immediately once\n // the developer calls either \"respondWith\" or \"errorWith\".\n const mockedResponse = await options.controller[kResponsePromise]\n return mockedResponse\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n options.onError(requestAbortPromise.rejectionReason)\n return true\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (handleResponseError(result.error)) {\n return true\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n }).then(() => {\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the response in any way, preemptively\n // resolve the pending response promise from the new controller.\n // This prevents it from hanging forever.\n if (\n unhandledExceptionController[kResponsePromise].state === 'pending'\n ) {\n unhandledExceptionController[kResponsePromise].resolve(undefined)\n }\n })\n\n const nextResult = await until(\n () => unhandledExceptionController[kResponsePromise]\n )\n\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n if (nextResult.error) {\n return handleResponseError(nextResult.error)\n }\n\n if (nextResult.data) {\n return handleResponse(nextResult.data)\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n options.onResponse(createServerErrorResponse(result.error))\n return true\n }\n\n /**\n * Handle a mocked Response instance.\n * @note That this can also be an Error in case\n * the developer called \"errorWith\". This differentiates\n * unhandled exceptions from intended errors.\n */\n if (result.data) {\n return handleResponse(result.data)\n }\n\n // In all other cases, consider the request unhandled.\n // The interceptor must perform it as-is.\n return false\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"]}
@@ -0,0 +1,66 @@
1
+ import { DeferredPromise } from '@open-draft/deferred-promise';
2
+
3
+ declare const kRequestHandled: unique symbol;
4
+ declare const kResponsePromise: unique symbol;
5
+ declare class RequestController {
6
+ private request;
7
+ /**
8
+ * Internal response promise.
9
+ * Available only for the library internals to grab the
10
+ * response instance provided by the developer.
11
+ * @note This promise cannot be rejected. It's either infinitely
12
+ * pending or resolved with whichever Response was passed to `respondWith()`.
13
+ */
14
+ [kResponsePromise]: DeferredPromise<Response | Error | undefined>;
15
+ /**
16
+ * Internal flag indicating if this request has been handled.
17
+ * @note The response promise becomes "fulfilled" on the next tick.
18
+ */
19
+ [kRequestHandled]: boolean;
20
+ constructor(request: Request);
21
+ /**
22
+ * Respond to this request with the given `Response` instance.
23
+ * @example
24
+ * controller.respondWith(new Response())
25
+ * controller.respondWith(Response.json({ id }))
26
+ * controller.respondWith(Response.error())
27
+ */
28
+ respondWith(response: Response): void;
29
+ /**
30
+ * Error this request with the given error.
31
+ * @example
32
+ * controller.errorWith()
33
+ * controller.errorWith(new Error('Oops!'))
34
+ */
35
+ errorWith(error?: Error): void;
36
+ }
37
+
38
+ declare const IS_PATCHED_MODULE: unique symbol;
39
+ type RequestCredentials = 'omit' | 'include' | 'same-origin';
40
+ type HttpRequestEventMap = {
41
+ request: [
42
+ args: {
43
+ request: Request;
44
+ requestId: string;
45
+ controller: RequestController;
46
+ }
47
+ ];
48
+ response: [
49
+ args: {
50
+ response: Response;
51
+ isMockedResponse: boolean;
52
+ request: Request;
53
+ requestId: string;
54
+ }
55
+ ];
56
+ unhandledException: [
57
+ args: {
58
+ error: unknown;
59
+ request: Request;
60
+ requestId: string;
61
+ controller: RequestController;
62
+ }
63
+ ];
64
+ };
65
+
66
+ export { HttpRequestEventMap as H, IS_PATCHED_MODULE as I, RequestCredentials as R };
@@ -1,4 +1,4 @@
1
- export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestCredentials } from './glossary-1c204f45.js';
1
+ export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestCredentials } from './glossary-7d7adb4b.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 _chunk2CRB3JAQjs = require('./chunk-2CRB3JAQ.js');
8
+ var _chunkFXSPMSSQjs = require('./chunk-FXSPMSSQ.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 = _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;
78
+ exports.BatchInterceptor = BatchInterceptor; exports.INTERNAL_REQUEST_ID_HEADER_NAME = _chunkTIPR373Rjs.INTERNAL_REQUEST_ID_HEADER_NAME; exports.IS_PATCHED_MODULE = _chunkFXSPMSSQjs.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 = _chunkFXSPMSSQjs.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-OMISYKWR.mjs";
8
+ } from "./chunk-GGUENBDN.mjs";
9
9
  import {
10
10
  INTERNAL_REQUEST_ID_HEADER_NAME,
11
11
  Interceptor,
@@ -1,13 +1,9 @@
1
1
  import { Emitter } from 'strict-event-emitter';
2
- import { a as InteractiveRequest, H as HttpRequestEventMap } from '../../glossary-1c204f45.js';
2
+ import { H as HttpRequestEventMap } from '../../glossary-7d7adb4b.js';
3
3
  import { I as Interceptor } from '../../Interceptor-af98b768.js';
4
4
  import '@open-draft/deferred-promise';
5
5
  import '@open-draft/logger';
6
6
 
7
- type XMLHttpRequestEventListener = (args: {
8
- request: InteractiveRequest;
9
- requestId: string;
10
- }) => Promise<void> | void;
11
7
  type XMLHttpRequestEmitter = Emitter<HttpRequestEventMap>;
12
8
  declare class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {
13
9
  static interceptorSymbol: symbol;
@@ -16,4 +12,4 @@ declare class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap>
16
12
  protected setup(): void;
17
13
  }
18
14
 
19
- export { XMLHttpRequestEmitter, XMLHttpRequestEventListener, XMLHttpRequestInterceptor };
15
+ export { XMLHttpRequestEmitter, XMLHttpRequestInterceptor };
@@ -1,11 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkPSX5J3RFjs = require('../../chunk-PSX5J3RF.js');
3
+ var _chunk7GVJEW45js = require('../../chunk-7GVJEW45.js');
4
4
  require('../../chunk-LK6DILFK.js');
5
- require('../../chunk-MQJ3JOOK.js');
6
- require('../../chunk-2CRB3JAQ.js');
5
+ require('../../chunk-VRKVKT62.js');
6
+ require('../../chunk-FXSPMSSQ.js');
7
7
  require('../../chunk-TIPR373R.js');
8
8
 
9
9
 
10
- exports.XMLHttpRequestInterceptor = _chunkPSX5J3RFjs.XMLHttpRequestInterceptor;
10
+ exports.XMLHttpRequestInterceptor = _chunk7GVJEW45js.XMLHttpRequestInterceptor;
11
11
  //# sourceMappingURL=index.js.map
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  XMLHttpRequestInterceptor
3
- } from "../../chunk-732REFPX.mjs";
3
+ } from "../../chunk-5ETVT6GU.mjs";
4
4
  import "../../chunk-6HYIRFX2.mjs";
5
- import "../../chunk-OUWBQF3Z.mjs";
6
- import "../../chunk-OMISYKWR.mjs";
5
+ import "../../chunk-7A4UJNSW.mjs";
6
+ import "../../chunk-GGUENBDN.mjs";
7
7
  import "../../chunk-QED3Q6Z2.mjs";
8
8
  export {
9
9
  XMLHttpRequestInterceptor
@@ -1,4 +1,4 @@
1
- import { H as HttpRequestEventMap } from '../../glossary-1c204f45.js';
1
+ import { H as HttpRequestEventMap } from '../../glossary-7d7adb4b.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 _chunkWBHIW62Pjs = require('../../chunk-WBHIW62P.js');
4
- require('../../chunk-MQJ3JOOK.js');
5
- require('../../chunk-2CRB3JAQ.js');
3
+ var _chunk6MBJUL74js = require('../../chunk-6MBJUL74.js');
4
+ require('../../chunk-VRKVKT62.js');
5
+ require('../../chunk-FXSPMSSQ.js');
6
6
  require('../../chunk-TIPR373R.js');
7
7
 
8
8
 
9
- exports.FetchInterceptor = _chunkWBHIW62Pjs.FetchInterceptor;
9
+ exports.FetchInterceptor = _chunk6MBJUL74js.FetchInterceptor;
10
10
  //# sourceMappingURL=index.js.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  FetchInterceptor
3
- } from "../../chunk-MAEPOYB6.mjs";
4
- import "../../chunk-OUWBQF3Z.mjs";
5
- import "../../chunk-OMISYKWR.mjs";
3
+ } from "../../chunk-NU2MPFD6.mjs";
4
+ import "../../chunk-7A4UJNSW.mjs";
5
+ import "../../chunk-GGUENBDN.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-1c204f45.js';
3
+ import '../glossary-7d7adb4b.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 _chunkPSX5J3RFjs = require('../chunk-PSX5J3RF.js');
3
+ var _chunk7GVJEW45js = require('../chunk-7GVJEW45.js');
4
4
  require('../chunk-LK6DILFK.js');
5
5
 
6
6
 
7
- var _chunkWBHIW62Pjs = require('../chunk-WBHIW62P.js');
8
- require('../chunk-MQJ3JOOK.js');
9
- require('../chunk-2CRB3JAQ.js');
7
+ var _chunk6MBJUL74js = require('../chunk-6MBJUL74.js');
8
+ require('../chunk-VRKVKT62.js');
9
+ require('../chunk-FXSPMSSQ.js');
10
10
  require('../chunk-TIPR373R.js');
11
11
 
12
12
  // src/presets/browser.ts
13
13
  var browser_default = [
14
- new (0, _chunkWBHIW62Pjs.FetchInterceptor)(),
15
- new (0, _chunkPSX5J3RFjs.XMLHttpRequestInterceptor)()
14
+ new (0, _chunk6MBJUL74js.FetchInterceptor)(),
15
+ new (0, _chunk7GVJEW45js.XMLHttpRequestInterceptor)()
16
16
  ];
17
17
 
18
18
 
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  XMLHttpRequestInterceptor
3
- } from "../chunk-732REFPX.mjs";
3
+ } from "../chunk-5ETVT6GU.mjs";
4
4
  import "../chunk-6HYIRFX2.mjs";
5
5
  import {
6
6
  FetchInterceptor
7
- } from "../chunk-MAEPOYB6.mjs";
8
- import "../chunk-OUWBQF3Z.mjs";
9
- import "../chunk-OMISYKWR.mjs";
7
+ } from "../chunk-NU2MPFD6.mjs";
8
+ import "../chunk-7A4UJNSW.mjs";
9
+ import "../chunk-GGUENBDN.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-88ee47c0.js';
2
+ import { f as Interceptor, E as ExtractEventNames } from './Interceptor-a31b1217.js';
3
3
 
4
4
  interface BatchInterceptorOptions<InterceptorList extends ReadonlyArray<Interceptor<any>>> {
5
5
  name: string;