@mswjs/interceptors 0.39.8 → 0.40.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 (133) hide show
  1. package/lib/browser/{chunk-E3CCOBRX.js → chunk-2MCNQOY3.js} +54 -49
  2. package/lib/browser/chunk-2MCNQOY3.js.map +1 -0
  3. package/lib/browser/chunk-57RIRQUY.js +218 -0
  4. package/lib/browser/chunk-57RIRQUY.js.map +1 -0
  5. package/lib/browser/chunk-FW45TRCB.js +178 -0
  6. package/lib/browser/chunk-FW45TRCB.js.map +1 -0
  7. package/lib/browser/{chunk-TIPR373R.js → chunk-JQ2S7G56.js} +19 -3
  8. package/lib/browser/chunk-JQ2S7G56.js.map +1 -0
  9. package/lib/browser/{chunk-3RXCRGL2.mjs → chunk-LIKZF2VU.mjs} +102 -1
  10. package/lib/browser/chunk-LIKZF2VU.mjs.map +1 -0
  11. package/lib/browser/{chunk-H74PGQ4Y.js → chunk-MNT2FUCH.js} +58 -53
  12. package/lib/browser/chunk-MNT2FUCH.js.map +1 -0
  13. package/lib/browser/chunk-VOUOVDAW.mjs +178 -0
  14. package/lib/browser/chunk-VOUOVDAW.mjs.map +1 -0
  15. package/lib/browser/{chunk-E7UVBHVO.mjs → chunk-WADP6VHN.mjs} +48 -43
  16. package/lib/browser/chunk-WADP6VHN.mjs.map +1 -0
  17. package/lib/browser/{chunk-Q7K2XAEP.mjs → chunk-WOWPV4GR.mjs} +50 -45
  18. package/lib/browser/chunk-WOWPV4GR.mjs.map +1 -0
  19. package/lib/browser/{chunk-QED3Q6Z2.mjs → chunk-Z5TSB3T6.mjs} +17 -1
  20. package/lib/browser/{chunk-QED3Q6Z2.mjs.map → chunk-Z5TSB3T6.mjs.map} +1 -1
  21. package/lib/browser/{glossary-7152281e.d.ts → glossary-f7ee1c9d.d.ts} +22 -17
  22. package/lib/browser/index.d.ts +1 -2
  23. package/lib/browser/index.js +6 -4
  24. package/lib/browser/index.js.map +1 -1
  25. package/lib/browser/index.mjs +4 -2
  26. package/lib/browser/index.mjs.map +1 -1
  27. package/lib/browser/interceptors/WebSocket/index.js +3 -3
  28. package/lib/browser/interceptors/WebSocket/index.mjs +1 -1
  29. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -2
  30. package/lib/browser/interceptors/XMLHttpRequest/index.js +5 -5
  31. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +4 -4
  32. package/lib/browser/interceptors/fetch/index.d.ts +1 -2
  33. package/lib/browser/interceptors/fetch/index.js +5 -5
  34. package/lib/browser/interceptors/fetch/index.mjs +4 -4
  35. package/lib/browser/presets/browser.d.ts +1 -2
  36. package/lib/browser/presets/browser.js +7 -7
  37. package/lib/browser/presets/browser.mjs +5 -5
  38. package/lib/node/{BatchInterceptor-5b72232f.d.ts → BatchInterceptor-cb9a2eee.d.ts} +1 -1
  39. package/lib/node/{Interceptor-bc5a9d8e.d.ts → Interceptor-dc0a39b5.d.ts} +22 -16
  40. package/lib/node/RemoteHttpInterceptor.d.ts +2 -3
  41. package/lib/node/RemoteHttpInterceptor.js +31 -27
  42. package/lib/node/RemoteHttpInterceptor.js.map +1 -1
  43. package/lib/node/RemoteHttpInterceptor.mjs +28 -24
  44. package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
  45. package/lib/node/{chunk-EKNRB5ZS.mjs → chunk-5UGIB6OX.mjs} +40 -29
  46. package/lib/node/chunk-5UGIB6OX.mjs.map +1 -0
  47. package/lib/node/{chunk-4NEYTVWD.mjs → chunk-5V3SIIW2.mjs} +48 -43
  48. package/lib/node/chunk-5V3SIIW2.mjs.map +1 -0
  49. package/lib/node/{chunk-VV2LUF5K.js → chunk-6B3ZQOO2.js} +51 -46
  50. package/lib/node/chunk-6B3ZQOO2.js.map +1 -0
  51. package/lib/node/chunk-7Q53NNPV.js +189 -0
  52. package/lib/node/chunk-7Q53NNPV.js.map +1 -0
  53. package/lib/node/{chunk-A7U44ARP.js → chunk-DOWWQYXZ.js} +104 -3
  54. package/lib/node/chunk-DOWWQYXZ.js.map +1 -0
  55. package/lib/node/{chunk-Z5LWCBZS.js → chunk-FRZQJNBO.js} +56 -51
  56. package/lib/node/chunk-FRZQJNBO.js.map +1 -0
  57. package/lib/node/{chunk-TJDMZZXE.mjs → chunk-GKN5RBVR.mjs} +2 -2
  58. package/lib/node/{chunk-R6JVCM7X.js → chunk-J5MULIHT.js} +3 -3
  59. package/lib/node/{chunk-IHJSPMYM.mjs → chunk-JXGB54LE.mjs} +102 -1
  60. package/lib/node/chunk-JXGB54LE.mjs.map +1 -0
  61. package/lib/node/{chunk-3CNGDJFB.mjs → chunk-OFW5L5ET.mjs} +50 -45
  62. package/lib/node/chunk-OFW5L5ET.mjs.map +1 -0
  63. package/lib/node/{chunk-A7Q4RTDJ.mjs → chunk-R6T7CL5E.mjs} +55 -115
  64. package/lib/node/chunk-R6T7CL5E.mjs.map +1 -0
  65. package/lib/node/{chunk-RC2XPCC4.mjs → chunk-SQ6RHTJR.mjs} +2 -2
  66. package/lib/node/chunk-SRMAQGPM.js +30 -0
  67. package/lib/node/chunk-SRMAQGPM.js.map +1 -0
  68. package/lib/node/{chunk-4YBV77DG.js → chunk-T3TW4P64.js} +3 -3
  69. package/lib/node/{chunk-N4ZZFE24.js → chunk-VYO5XDY2.js} +56 -45
  70. package/lib/node/chunk-VYO5XDY2.js.map +1 -0
  71. package/lib/node/chunk-YWNGXXUQ.mjs +30 -0
  72. package/lib/node/{chunk-3GJB4JDF.mjs.map → chunk-YWNGXXUQ.mjs.map} +1 -1
  73. package/lib/node/index.d.ts +2 -3
  74. package/lib/node/index.js +6 -4
  75. package/lib/node/index.js.map +1 -1
  76. package/lib/node/index.mjs +5 -3
  77. package/lib/node/index.mjs.map +1 -1
  78. package/lib/node/interceptors/ClientRequest/index.d.ts +1 -2
  79. package/lib/node/interceptors/ClientRequest/index.js +6 -6
  80. package/lib/node/interceptors/ClientRequest/index.mjs +5 -5
  81. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -2
  82. package/lib/node/interceptors/XMLHttpRequest/index.js +5 -5
  83. package/lib/node/interceptors/XMLHttpRequest/index.mjs +4 -4
  84. package/lib/node/interceptors/fetch/index.d.ts +1 -2
  85. package/lib/node/interceptors/fetch/index.js +5 -5
  86. package/lib/node/interceptors/fetch/index.mjs +4 -4
  87. package/lib/node/presets/node.d.ts +1 -2
  88. package/lib/node/presets/node.js +10 -10
  89. package/lib/node/presets/node.mjs +7 -7
  90. package/lib/node/utils/node/index.js +3 -3
  91. package/lib/node/utils/node/index.mjs +2 -2
  92. package/package.json +2 -1
  93. package/src/RemoteHttpInterceptor.ts +18 -13
  94. package/src/RequestController.test.ts +78 -31
  95. package/src/RequestController.ts +63 -39
  96. package/src/index.ts +4 -0
  97. package/src/interceptors/ClientRequest/MockHttpSocket.ts +24 -3
  98. package/src/interceptors/ClientRequest/index.ts +14 -18
  99. package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +45 -35
  100. package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +24 -21
  101. package/src/interceptors/fetch/index.ts +61 -50
  102. package/src/utils/handleRequest.ts +65 -95
  103. package/lib/browser/chunk-2QICSCCS.js +0 -238
  104. package/lib/browser/chunk-2QICSCCS.js.map +0 -1
  105. package/lib/browser/chunk-3RXCRGL2.mjs.map +0 -1
  106. package/lib/browser/chunk-E3CCOBRX.js.map +0 -1
  107. package/lib/browser/chunk-E7UVBHVO.mjs.map +0 -1
  108. package/lib/browser/chunk-H74PGQ4Y.js.map +0 -1
  109. package/lib/browser/chunk-PTTUYYVR.mjs +0 -238
  110. package/lib/browser/chunk-PTTUYYVR.mjs.map +0 -1
  111. package/lib/browser/chunk-Q7K2XAEP.mjs.map +0 -1
  112. package/lib/browser/chunk-T7TBRNJZ.js +0 -117
  113. package/lib/browser/chunk-T7TBRNJZ.js.map +0 -1
  114. package/lib/browser/chunk-TIPR373R.js.map +0 -1
  115. package/lib/node/chunk-3CNGDJFB.mjs.map +0 -1
  116. package/lib/node/chunk-3GJB4JDF.mjs +0 -14
  117. package/lib/node/chunk-4NEYTVWD.mjs.map +0 -1
  118. package/lib/node/chunk-72ZIHMEB.js +0 -249
  119. package/lib/node/chunk-72ZIHMEB.js.map +0 -1
  120. package/lib/node/chunk-A7Q4RTDJ.mjs.map +0 -1
  121. package/lib/node/chunk-A7U44ARP.js.map +0 -1
  122. package/lib/node/chunk-EKNRB5ZS.mjs.map +0 -1
  123. package/lib/node/chunk-IHJSPMYM.mjs.map +0 -1
  124. package/lib/node/chunk-N4ZZFE24.js.map +0 -1
  125. package/lib/node/chunk-SMXZPJEA.js +0 -14
  126. package/lib/node/chunk-SMXZPJEA.js.map +0 -1
  127. package/lib/node/chunk-VV2LUF5K.js.map +0 -1
  128. package/lib/node/chunk-Z5LWCBZS.js.map +0 -1
  129. package/src/utils/RequestController.ts +0 -21
  130. /package/lib/node/{chunk-TJDMZZXE.mjs.map → chunk-GKN5RBVR.mjs.map} +0 -0
  131. /package/lib/node/{chunk-R6JVCM7X.js.map → chunk-J5MULIHT.js.map} +0 -0
  132. /package/lib/node/{chunk-RC2XPCC4.mjs.map → chunk-SQ6RHTJR.mjs.map} +0 -0
  133. /package/lib/node/{chunk-4YBV77DG.js.map → chunk-T3TW4P64.js.map} +0 -0
@@ -0,0 +1,178 @@
1
+ import {
2
+ InterceptorError,
3
+ RequestController
4
+ } from "./chunk-LIKZF2VU.mjs";
5
+ import {
6
+ emitAsync
7
+ } from "./chunk-VYSDLBSS.mjs";
8
+
9
+ // src/utils/isObject.ts
10
+ function isObject(value, loose = false) {
11
+ return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
12
+ }
13
+
14
+ // src/utils/isPropertyAccessible.ts
15
+ function isPropertyAccessible(obj, key) {
16
+ try {
17
+ obj[key];
18
+ return true;
19
+ } catch (e) {
20
+ return false;
21
+ }
22
+ }
23
+
24
+ // src/utils/responseUtils.ts
25
+ function createServerErrorResponse(body) {
26
+ return new Response(
27
+ JSON.stringify(
28
+ body instanceof Error ? {
29
+ name: body.name,
30
+ message: body.message,
31
+ stack: body.stack
32
+ } : body
33
+ ),
34
+ {
35
+ status: 500,
36
+ statusText: "Unhandled Exception",
37
+ headers: {
38
+ "Content-Type": "application/json"
39
+ }
40
+ }
41
+ );
42
+ }
43
+ function isResponseError(response) {
44
+ return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
45
+ }
46
+ function isResponseLike(value) {
47
+ return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
48
+ }
49
+
50
+ // src/utils/handleRequest.ts
51
+ import { DeferredPromise } from "@open-draft/deferred-promise";
52
+ import { until } from "@open-draft/until";
53
+
54
+ // src/utils/isNodeLikeError.ts
55
+ function isNodeLikeError(error) {
56
+ if (error == null) {
57
+ return false;
58
+ }
59
+ if (!(error instanceof Error)) {
60
+ return false;
61
+ }
62
+ return "code" in error && "errno" in error;
63
+ }
64
+
65
+ // src/utils/handleRequest.ts
66
+ async function handleRequest(options) {
67
+ const handleResponse = async (response) => {
68
+ if (response instanceof Error) {
69
+ await options.controller.errorWith(response);
70
+ return true;
71
+ }
72
+ if (isResponseError(response)) {
73
+ await options.controller.respondWith(response);
74
+ return true;
75
+ }
76
+ if (isResponseLike(response)) {
77
+ await options.controller.respondWith(response);
78
+ return true;
79
+ }
80
+ if (isObject(response)) {
81
+ await options.controller.errorWith(response);
82
+ return true;
83
+ }
84
+ return false;
85
+ };
86
+ const handleResponseError = async (error) => {
87
+ if (error instanceof InterceptorError) {
88
+ throw result.error;
89
+ }
90
+ if (isNodeLikeError(error)) {
91
+ await options.controller.errorWith(error);
92
+ return true;
93
+ }
94
+ if (error instanceof Response) {
95
+ return await handleResponse(error);
96
+ }
97
+ return false;
98
+ };
99
+ const requestAbortPromise = new DeferredPromise();
100
+ if (options.request.signal) {
101
+ if (options.request.signal.aborted) {
102
+ await options.controller.errorWith(options.request.signal.reason);
103
+ return;
104
+ }
105
+ options.request.signal.addEventListener(
106
+ "abort",
107
+ () => {
108
+ requestAbortPromise.reject(options.request.signal.reason);
109
+ },
110
+ { once: true }
111
+ );
112
+ }
113
+ const result = await until(async () => {
114
+ const requestListenersPromise = emitAsync(options.emitter, "request", {
115
+ requestId: options.requestId,
116
+ request: options.request,
117
+ controller: options.controller
118
+ });
119
+ await Promise.race([
120
+ // Short-circuit the request handling promise if the request gets aborted.
121
+ requestAbortPromise,
122
+ requestListenersPromise,
123
+ options.controller.handled
124
+ ]);
125
+ });
126
+ if (requestAbortPromise.state === "rejected") {
127
+ await options.controller.errorWith(requestAbortPromise.rejectionReason);
128
+ return;
129
+ }
130
+ if (result.error) {
131
+ if (await handleResponseError(result.error)) {
132
+ return;
133
+ }
134
+ if (options.emitter.listenerCount("unhandledException") > 0) {
135
+ const unhandledExceptionController = new RequestController(
136
+ options.request,
137
+ {
138
+ /**
139
+ * @note Intentionally empty passthrough handle.
140
+ * This controller is created within another controller and we only need
141
+ * to know if `unhandledException` listeners handled the request.
142
+ */
143
+ passthrough() {
144
+ },
145
+ async respondWith(response) {
146
+ await handleResponse(response);
147
+ },
148
+ async errorWith(reason) {
149
+ await options.controller.errorWith(reason);
150
+ }
151
+ }
152
+ );
153
+ await emitAsync(options.emitter, "unhandledException", {
154
+ error: result.error,
155
+ request: options.request,
156
+ requestId: options.requestId,
157
+ controller: unhandledExceptionController
158
+ });
159
+ if (unhandledExceptionController.readyState !== RequestController.PENDING) {
160
+ return;
161
+ }
162
+ }
163
+ await options.controller.respondWith(
164
+ createServerErrorResponse(result.error)
165
+ );
166
+ return;
167
+ }
168
+ if (options.controller.readyState === RequestController.PENDING) {
169
+ return await options.controller.passthrough();
170
+ }
171
+ return options.controller.handled;
172
+ }
173
+
174
+ export {
175
+ isResponseError,
176
+ handleRequest
177
+ };
178
+ //# sourceMappingURL=chunk-VOUOVDAW.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/handleRequest.ts","../../src/utils/isNodeLikeError.ts"],"sourcesContent":["/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\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 { RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n}\n\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<void> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n await options.controller.errorWith(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n await options.controller.errorWith(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<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 await options.controller.errorWith(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await 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', async ({ requestId: pendingRequestId }) => {\n // if (\n // pendingRequestId === options.requestId &&\n // options.controller.readyState === RequestController.PENDING\n // ) {\n // await options.controller.passthrough()\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 if (options.request.signal.aborted) {\n await options.controller.errorWith(options.request.signal.reason)\n return\n }\n\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 requestListenersPromise = 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 requestListenersPromise,\n options.controller.handled,\n ])\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n await options.controller.errorWith(requestAbortPromise.rejectionReason)\n return\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 (await handleResponseError(result.error)) {\n return\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 * @note Intentionally empty passthrough handle.\n * This controller is created within another controller and we only need\n * to know if `unhandledException` listeners handled the request.\n */\n passthrough() {},\n async respondWith(response) {\n await handleResponse(response)\n },\n async errorWith(reason) {\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 await options.controller.errorWith(reason)\n },\n }\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n })\n\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the request in any way, passthrough.\n if (\n unhandledExceptionController.readyState !== RequestController.PENDING\n ) {\n return\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n await options.controller.respondWith(\n createServerErrorResponse(result.error)\n )\n return\n }\n\n // If the request hasn't been handled by this point, passthrough.\n if (options.controller.readyState === RequestController.PENDING) {\n return await options.controller.passthrough()\n }\n\n return options.controller.handled\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"],"mappings":";;;;;;;;;AAGO,SAAS,SAAY,OAAY,QAAQ,OAAmB;AACjE,SAAO,QACH,OAAO,UAAU,SAAS,KAAK,KAAK,EAAE,WAAW,UAAU,IAC3D,OAAO,UAAU,SAAS,KAAK,KAAK,MAAM;AAChD;;;ACCO,SAAS,qBACd,KACA,KACA;AACA,MAAI;AACF,QAAI,GAAG;AACP,WAAO;AAAA,EACT,SAAQ,GAAN;AACA,WAAO;AAAA,EACT;AACF;;;ACZO,SAAS,0BAA0B,MAAyB;AACjE,SAAO,IAAI;AAAA,IACT,KAAK;AAAA,MACH,gBAAgB,QACZ;AAAA,QACE,MAAM,KAAK;AAAA,QACX,SAAS,KAAK;AAAA,QACd,OAAO,KAAK;AAAA,MACd,IACA;AAAA,IACN;AAAA,IACA;AAAA,MACE,QAAQ;AAAA,MACR,YAAY;AAAA,MACZ,SAAS;AAAA,QACP,gBAAgB;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AACF;AAYO,SAAS,gBAAgB,UAA8C;AAC5E,SACE,YAAY,QACZ,oBAAoB,YACpB,qBAAqB,UAAU,MAAM,KACrC,SAAS,SAAS;AAEtB;AAOO,SAAS,eAAe,OAAmC;AAChE,SACE,SAA8B,OAAO,IAAI,KACzC,qBAAqB,OAAO,QAAQ,KACpC,qBAAqB,OAAO,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AAQ,WAAW,eAAe,kBAAkB,SAAS;AAC/D,WAAO,MAAM,QAAQ,WAAW,YAAY;AAAA,EAC9C;AAEA,SAAO,QAAQ,WAAW;AAC5B;","names":[]}
@@ -4,14 +4,15 @@ import {
4
4
  toArrayBuffer
5
5
  } from "./chunk-6HYIRFX2.mjs";
6
6
  import {
7
- RequestController,
8
- handleRequest
9
- } from "./chunk-PTTUYYVR.mjs";
7
+ handleRequest,
8
+ isResponseError
9
+ } from "./chunk-VOUOVDAW.mjs";
10
10
  import {
11
11
  FetchResponse,
12
12
  IS_PATCHED_MODULE,
13
+ RequestController,
13
14
  setRawRequest
14
- } from "./chunk-3RXCRGL2.mjs";
15
+ } from "./chunk-LIKZF2VU.mjs";
15
16
  import {
16
17
  hasConfigurableGlobal
17
18
  } from "./chunk-VYSDLBSS.mjs";
@@ -19,7 +20,7 @@ import {
19
20
  INTERNAL_REQUEST_ID_HEADER_NAME,
20
21
  Interceptor,
21
22
  createRequestId
22
- } from "./chunk-QED3Q6Z2.mjs";
23
+ } from "./chunk-Z5TSB3T6.mjs";
23
24
 
24
25
  // src/interceptors/XMLHttpRequest/index.ts
25
26
  import { invariant as invariant2 } from "outvariant";
@@ -273,7 +274,6 @@ var XMLHttpRequestController = class {
273
274
  }
274
275
  },
275
276
  methodCall: ([methodName, args], invoke) => {
276
- var _a;
277
277
  switch (methodName) {
278
278
  case "open": {
279
279
  const [method, url] = args;
@@ -324,24 +324,27 @@ var XMLHttpRequestController = class {
324
324
  const requestBody = typeof body === "string" ? encodeBuffer(body) : body;
325
325
  const fetchRequest = this.toFetchApiRequest(requestBody);
326
326
  this[kFetchRequest] = fetchRequest.clone();
327
- const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
328
- request: fetchRequest,
329
- requestId: this.requestId
330
- })) || Promise.resolve();
331
- onceRequestSettled.finally(() => {
332
- if (!this[kIsRequestHandled]) {
333
- this.logger.info(
334
- "request callback settled but request has not been handled (readystate %d), performing as-is...",
335
- this.request.readyState
336
- );
337
- if (IS_NODE) {
338
- this.request.setRequestHeader(
339
- INTERNAL_REQUEST_ID_HEADER_NAME,
340
- this.requestId
327
+ queueMicrotask(() => {
328
+ var _a;
329
+ const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
330
+ request: fetchRequest,
331
+ requestId: this.requestId
332
+ })) || Promise.resolve();
333
+ onceRequestSettled.finally(() => {
334
+ if (!this[kIsRequestHandled]) {
335
+ this.logger.info(
336
+ "request callback settled but request has not been handled (readystate %d), performing as-is...",
337
+ this.request.readyState
341
338
  );
339
+ if (IS_NODE) {
340
+ this.request.setRequestHeader(
341
+ INTERNAL_REQUEST_ID_HEADER_NAME,
342
+ this.requestId
343
+ );
344
+ }
345
+ return invoke();
342
346
  }
343
- return invoke();
344
- }
347
+ });
345
348
  });
346
349
  break;
347
350
  }
@@ -743,35 +746,37 @@ function createXMLHttpRequestProxy({
743
746
  logger
744
747
  );
745
748
  xhrRequestController.onRequest = async function({ request, requestId }) {
746
- const controller = new RequestController(request);
749
+ const controller = new RequestController(request, {
750
+ passthrough: () => {
751
+ this.logger.info(
752
+ "no mocked response received, performing request as-is..."
753
+ );
754
+ },
755
+ respondWith: async (response) => {
756
+ if (isResponseError(response)) {
757
+ this.errorWith(new TypeError("Network error"));
758
+ return;
759
+ }
760
+ await this.respondWith(response);
761
+ },
762
+ errorWith: (reason) => {
763
+ this.logger.info("request errored!", { error: reason });
764
+ if (reason instanceof Error) {
765
+ this.errorWith(reason);
766
+ }
767
+ }
768
+ });
747
769
  this.logger.info("awaiting mocked response...");
748
770
  this.logger.info(
749
771
  'emitting the "request" event for %s listener(s)...',
750
772
  emitter.listenerCount("request")
751
773
  );
752
- const isRequestHandled = await handleRequest({
774
+ await handleRequest({
753
775
  request,
754
776
  requestId,
755
777
  controller,
756
- emitter,
757
- onResponse: async (response) => {
758
- await this.respondWith(response);
759
- },
760
- onRequestError: () => {
761
- this.errorWith(new TypeError("Network error"));
762
- },
763
- onError: (error) => {
764
- this.logger.info("request errored!", { error });
765
- if (error instanceof Error) {
766
- this.errorWith(error);
767
- }
768
- }
778
+ emitter
769
779
  });
770
- if (!isRequestHandled) {
771
- this.logger.info(
772
- "no mocked response received, performing request as-is..."
773
- );
774
- }
775
780
  };
776
781
  xhrRequestController.onResponse = async function({
777
782
  response,
@@ -843,4 +848,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
843
848
  export {
844
849
  XMLHttpRequestInterceptor
845
850
  };
846
- //# sourceMappingURL=chunk-E7UVBHVO.mjs.map
851
+ //# sourceMappingURL=chunk-WADP6VHN.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/interceptors/XMLHttpRequest/index.ts","../../src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts","../../src/interceptors/XMLHttpRequest/utils/concatArrayBuffer.ts","../../src/interceptors/XMLHttpRequest/polyfills/EventPolyfill.ts","../../src/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.ts","../../src/interceptors/XMLHttpRequest/utils/createEvent.ts","../../src/utils/findPropertySource.ts","../../src/utils/createProxy.ts","../../src/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.ts","../../src/utils/parseJson.ts","../../src/interceptors/XMLHttpRequest/utils/createResponse.ts","../../src/interceptors/XMLHttpRequest/utils/getBodyByteLength.ts","../../src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { Emitter } from 'strict-event-emitter'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { createXMLHttpRequestProxy } from './XMLHttpRequestProxy'\nimport { hasConfigurableGlobal } from '../../utils/hasConfigurableGlobal'\n\nexport type XMLHttpRequestEmitter = Emitter<HttpRequestEventMap>\n\nexport class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static interceptorSymbol = Symbol('xhr')\n\n constructor() {\n super(XMLHttpRequestInterceptor.interceptorSymbol)\n }\n\n protected checkEnvironment() {\n return hasConfigurableGlobal('XMLHttpRequest')\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n logger.info('patching \"XMLHttpRequest\" module...')\n\n const PureXMLHttpRequest = globalThis.XMLHttpRequest\n\n invariant(\n !(PureXMLHttpRequest as any)[IS_PATCHED_MODULE],\n 'Failed to patch the \"XMLHttpRequest\" module: already patched.'\n )\n\n globalThis.XMLHttpRequest = createXMLHttpRequestProxy({\n emitter: this.emitter,\n logger: this.logger,\n })\n\n logger.info(\n 'native \"XMLHttpRequest\" module patched!',\n globalThis.XMLHttpRequest.name\n )\n\n Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {\n enumerable: true,\n configurable: true,\n value: true,\n })\n\n this.subscriptions.push(() => {\n Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {\n value: undefined,\n })\n\n globalThis.XMLHttpRequest = PureXMLHttpRequest\n logger.info(\n 'native \"XMLHttpRequest\" module restored!',\n globalThis.XMLHttpRequest.name\n )\n })\n }\n}\n","import { invariant } from 'outvariant'\nimport { isNodeProcess } from 'is-node-process'\nimport type { Logger } from '@open-draft/logger'\nimport { concatArrayBuffer } from './utils/concatArrayBuffer'\nimport { createEvent } from './utils/createEvent'\nimport {\n decodeBuffer,\n encodeBuffer,\n toArrayBuffer,\n} from '../../utils/bufferUtils'\nimport { createProxy } from '../../utils/createProxy'\nimport { isDomParserSupportedType } from './utils/isDomParserSupportedType'\nimport { parseJson } from '../../utils/parseJson'\nimport { createResponse } from './utils/createResponse'\nimport { INTERNAL_REQUEST_ID_HEADER_NAME } from '../../Interceptor'\nimport { createRequestId } from '../../createRequestId'\nimport { getBodyByteLength } from './utils/getBodyByteLength'\nimport { setRawRequest } from '../../getRawRequest'\n\nconst kIsRequestHandled = Symbol('kIsRequestHandled')\nconst IS_NODE = isNodeProcess()\nconst kFetchRequest = Symbol('kFetchRequest')\n\n/**\n * An `XMLHttpRequest` instance controller that allows us\n * to handle any given request instance (e.g. responding to it).\n */\nexport class XMLHttpRequestController {\n public request: XMLHttpRequest\n public requestId: string\n public onRequest?: (\n this: XMLHttpRequestController,\n args: {\n request: Request\n requestId: string\n }\n ) => Promise<void>\n public onResponse?: (\n this: XMLHttpRequestController,\n args: {\n response: Response\n isMockedResponse: boolean\n request: Request\n requestId: string\n }\n ) => void;\n\n [kIsRequestHandled]: boolean;\n [kFetchRequest]?: Request\n private method: string = 'GET'\n private url: URL = null as any\n private requestHeaders: Headers\n private responseBuffer: Uint8Array\n private events: Map<keyof XMLHttpRequestEventTargetEventMap, Array<Function>>\n private uploadEvents: Map<\n keyof XMLHttpRequestEventTargetEventMap,\n Array<Function>\n >\n\n constructor(\n readonly initialRequest: XMLHttpRequest,\n public logger: Logger\n ) {\n this[kIsRequestHandled] = false\n\n this.events = new Map()\n this.uploadEvents = new Map()\n this.requestId = createRequestId()\n this.requestHeaders = new Headers()\n this.responseBuffer = new Uint8Array()\n\n this.request = createProxy(initialRequest, {\n setProperty: ([propertyName, nextValue], invoke) => {\n switch (propertyName) {\n case 'ontimeout': {\n const eventName = propertyName.slice(\n 2\n ) as keyof XMLHttpRequestEventTargetEventMap\n\n /**\n * @note Proxy callbacks to event listeners because JSDOM has trouble\n * translating these properties to callbacks. It seemed to be operating\n * on events exclusively.\n */\n this.request.addEventListener(eventName, nextValue as any)\n\n return invoke()\n }\n\n default: {\n return invoke()\n }\n }\n },\n methodCall: ([methodName, args], invoke) => {\n switch (methodName) {\n case 'open': {\n const [method, url] = args as [string, string | undefined]\n\n if (typeof url === 'undefined') {\n this.method = 'GET'\n this.url = toAbsoluteUrl(method)\n } else {\n this.method = method\n this.url = toAbsoluteUrl(url)\n }\n\n this.logger = this.logger.extend(`${this.method} ${this.url.href}`)\n this.logger.info('open', this.method, this.url.href)\n\n return invoke()\n }\n\n case 'addEventListener': {\n const [eventName, listener] = args as [\n keyof XMLHttpRequestEventTargetEventMap,\n Function,\n ]\n\n this.registerEvent(eventName, listener)\n this.logger.info('addEventListener', eventName, listener)\n\n return invoke()\n }\n\n case 'setRequestHeader': {\n const [name, value] = args as [string, string]\n this.requestHeaders.set(name, value)\n\n this.logger.info('setRequestHeader', name, value)\n\n return invoke()\n }\n\n case 'send': {\n const [body] = args as [\n body?: XMLHttpRequestBodyInit | Document | null,\n ]\n\n this.request.addEventListener('load', () => {\n if (typeof this.onResponse !== 'undefined') {\n // Create a Fetch API Response representation of whichever\n // response this XMLHttpRequest received. Note those may\n // be either a mocked and the original response.\n const fetchResponse = createResponse(\n this.request,\n /**\n * The `response` property is the right way to read\n * the ambiguous response body, as the request's \"responseType\" may differ.\n * @see https://xhr.spec.whatwg.org/#the-response-attribute\n */\n this.request.response\n )\n\n // Notify the consumer about the response.\n this.onResponse.call(this, {\n response: fetchResponse,\n isMockedResponse: this[kIsRequestHandled],\n request: fetchRequest,\n requestId: this.requestId!,\n })\n }\n })\n\n const requestBody =\n typeof body === 'string' ? encodeBuffer(body) : body\n\n // Delegate request handling to the consumer.\n const fetchRequest = this.toFetchApiRequest(requestBody)\n this[kFetchRequest] = fetchRequest.clone()\n\n /**\n * @note Start request handling on the next tick so that the user\n * could add event listeners for \"loadend\" before the interceptor fires it.\n */\n queueMicrotask(() => {\n const onceRequestSettled =\n this.onRequest?.call(this, {\n request: fetchRequest,\n requestId: this.requestId!,\n }) || Promise.resolve()\n\n onceRequestSettled.finally(() => {\n // If the consumer didn't handle the request (called `.respondWith()`) perform it as-is.\n if (!this[kIsRequestHandled]) {\n this.logger.info(\n 'request callback settled but request has not been handled (readystate %d), performing as-is...',\n this.request.readyState\n )\n\n /**\n * @note Set the intercepted request ID on the original request in Node.js\n * so that if it triggers any other interceptors, they don't attempt\n * to process it once again.\n *\n * For instance, XMLHttpRequest is often implemented via \"http.ClientRequest\"\n * and we don't want for both XHR and ClientRequest interceptors to\n * handle the same request at the same time (e.g. emit the \"response\" event twice).\n */\n if (IS_NODE) {\n this.request.setRequestHeader(\n INTERNAL_REQUEST_ID_HEADER_NAME,\n this.requestId!\n )\n }\n\n return invoke()\n }\n })\n })\n\n break\n }\n\n default: {\n return invoke()\n }\n }\n },\n })\n\n /**\n * Proxy the `.upload` property to gather the event listeners/callbacks.\n */\n define(\n this.request,\n 'upload',\n createProxy(this.request.upload, {\n setProperty: ([propertyName, nextValue], invoke) => {\n switch (propertyName) {\n case 'onloadstart':\n case 'onprogress':\n case 'onaboart':\n case 'onerror':\n case 'onload':\n case 'ontimeout':\n case 'onloadend': {\n const eventName = propertyName.slice(\n 2\n ) as keyof XMLHttpRequestEventTargetEventMap\n\n this.registerUploadEvent(eventName, nextValue as Function)\n }\n }\n\n return invoke()\n },\n methodCall: ([methodName, args], invoke) => {\n switch (methodName) {\n case 'addEventListener': {\n const [eventName, listener] = args as [\n keyof XMLHttpRequestEventTargetEventMap,\n Function,\n ]\n this.registerUploadEvent(eventName, listener)\n this.logger.info('upload.addEventListener', eventName, listener)\n\n return invoke()\n }\n }\n },\n })\n )\n }\n\n private registerEvent(\n eventName: keyof XMLHttpRequestEventTargetEventMap,\n listener: Function\n ): void {\n const prevEvents = this.events.get(eventName) || []\n const nextEvents = prevEvents.concat(listener)\n this.events.set(eventName, nextEvents)\n\n this.logger.info('registered event \"%s\"', eventName, listener)\n }\n\n private registerUploadEvent(\n eventName: keyof XMLHttpRequestEventTargetEventMap,\n listener: Function\n ): void {\n const prevEvents = this.uploadEvents.get(eventName) || []\n const nextEvents = prevEvents.concat(listener)\n this.uploadEvents.set(eventName, nextEvents)\n\n this.logger.info('registered upload event \"%s\"', eventName, listener)\n }\n\n /**\n * Responds to the current request with the given\n * Fetch API `Response` instance.\n */\n public async respondWith(response: Response): Promise<void> {\n /**\n * @note Since `XMLHttpRequestController` delegates the handling of the responses\n * to the \"load\" event listener that doesn't distinguish between the mocked and original\n * responses, mark the request that had a mocked response with a corresponding symbol.\n *\n * Mark this request as having a mocked response immediately since\n * calculating request/response total body length is asynchronous.\n */\n this[kIsRequestHandled] = true\n\n /**\n * Dispatch request upload events for requests with a body.\n * @see https://github.com/mswjs/interceptors/issues/573\n */\n if (this[kFetchRequest]) {\n const totalRequestBodyLength = await getBodyByteLength(\n this[kFetchRequest]\n )\n\n this.trigger('loadstart', this.request.upload, {\n loaded: 0,\n total: totalRequestBodyLength,\n })\n this.trigger('progress', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n this.trigger('load', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n\n this.trigger('loadend', this.request.upload, {\n loaded: totalRequestBodyLength,\n total: totalRequestBodyLength,\n })\n }\n\n this.logger.info(\n 'responding with a mocked response: %d %s',\n response.status,\n response.statusText\n )\n\n define(this.request, 'status', response.status)\n define(this.request, 'statusText', response.statusText)\n define(this.request, 'responseURL', this.url.href)\n\n this.request.getResponseHeader = new Proxy(this.request.getResponseHeader, {\n apply: (_, __, args: [name: string]) => {\n this.logger.info('getResponseHeader', args[0])\n\n if (this.request.readyState < this.request.HEADERS_RECEIVED) {\n this.logger.info('headers not received yet, returning null')\n\n // Headers not received yet, nothing to return.\n return null\n }\n\n const headerValue = response.headers.get(args[0])\n this.logger.info(\n 'resolved response header \"%s\" to',\n args[0],\n headerValue\n )\n\n return headerValue\n },\n })\n\n this.request.getAllResponseHeaders = new Proxy(\n this.request.getAllResponseHeaders,\n {\n apply: () => {\n this.logger.info('getAllResponseHeaders')\n\n if (this.request.readyState < this.request.HEADERS_RECEIVED) {\n this.logger.info('headers not received yet, returning empty string')\n\n // Headers not received yet, nothing to return.\n return ''\n }\n\n const headersList = Array.from(response.headers.entries())\n const allHeaders = headersList\n .map(([headerName, headerValue]) => {\n return `${headerName}: ${headerValue}`\n })\n .join('\\r\\n')\n\n this.logger.info('resolved all response headers to', allHeaders)\n\n return allHeaders\n },\n }\n )\n\n // Update the response getters to resolve against the mocked response.\n Object.defineProperties(this.request, {\n response: {\n enumerable: true,\n configurable: false,\n get: () => this.response,\n },\n responseText: {\n enumerable: true,\n configurable: false,\n get: () => this.responseText,\n },\n responseXML: {\n enumerable: true,\n configurable: false,\n get: () => this.responseXML,\n },\n })\n\n const totalResponseBodyLength = await getBodyByteLength(response.clone())\n\n this.logger.info('calculated response body length', totalResponseBodyLength)\n\n this.trigger('loadstart', this.request, {\n loaded: 0,\n total: totalResponseBodyLength,\n })\n\n this.setReadyState(this.request.HEADERS_RECEIVED)\n this.setReadyState(this.request.LOADING)\n\n const finalizeResponse = () => {\n this.logger.info('finalizing the mocked response...')\n\n this.setReadyState(this.request.DONE)\n\n this.trigger('load', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n\n this.trigger('loadend', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n }\n\n if (response.body) {\n this.logger.info('mocked response has body, streaming...')\n\n const reader = response.body.getReader()\n\n const readNextResponseBodyChunk = async () => {\n const { value, done } = await reader.read()\n\n if (done) {\n this.logger.info('response body stream done!')\n finalizeResponse()\n return\n }\n\n if (value) {\n this.logger.info('read response body chunk:', value)\n this.responseBuffer = concatArrayBuffer(this.responseBuffer, value)\n\n this.trigger('progress', this.request, {\n loaded: this.responseBuffer.byteLength,\n total: totalResponseBodyLength,\n })\n }\n\n readNextResponseBodyChunk()\n }\n\n readNextResponseBodyChunk()\n } else {\n finalizeResponse()\n }\n }\n\n private responseBufferToText(): string {\n return decodeBuffer(this.responseBuffer)\n }\n\n get response(): unknown {\n this.logger.info(\n 'getResponse (responseType: %s)',\n this.request.responseType\n )\n\n if (this.request.readyState !== this.request.DONE) {\n return null\n }\n\n switch (this.request.responseType) {\n case 'json': {\n const responseJson = parseJson(this.responseBufferToText())\n this.logger.info('resolved response JSON', responseJson)\n\n return responseJson\n }\n\n case 'arraybuffer': {\n const arrayBuffer = toArrayBuffer(this.responseBuffer)\n this.logger.info('resolved response ArrayBuffer', arrayBuffer)\n\n return arrayBuffer\n }\n\n case 'blob': {\n const mimeType =\n this.request.getResponseHeader('Content-Type') || 'text/plain'\n const responseBlob = new Blob([this.responseBufferToText()], {\n type: mimeType,\n })\n\n this.logger.info(\n 'resolved response Blob (mime type: %s)',\n responseBlob,\n mimeType\n )\n\n return responseBlob\n }\n\n default: {\n const responseText = this.responseBufferToText()\n this.logger.info(\n 'resolving \"%s\" response type as text',\n this.request.responseType,\n responseText\n )\n\n return responseText\n }\n }\n }\n\n get responseText(): string {\n /**\n * Throw when trying to read the response body as text when the\n * \"responseType\" doesn't expect text. This just respects the spec better.\n * @see https://xhr.spec.whatwg.org/#the-responsetext-attribute\n */\n invariant(\n this.request.responseType === '' || this.request.responseType === 'text',\n 'InvalidStateError: The object is in invalid state.'\n )\n\n if (\n this.request.readyState !== this.request.LOADING &&\n this.request.readyState !== this.request.DONE\n ) {\n return ''\n }\n\n const responseText = this.responseBufferToText()\n this.logger.info('getResponseText: \"%s\"', responseText)\n\n return responseText\n }\n\n get responseXML(): Document | null {\n invariant(\n this.request.responseType === '' ||\n this.request.responseType === 'document',\n 'InvalidStateError: The object is in invalid state.'\n )\n\n if (this.request.readyState !== this.request.DONE) {\n return null\n }\n\n const contentType = this.request.getResponseHeader('Content-Type') || ''\n\n if (typeof DOMParser === 'undefined') {\n console.warn(\n 'Cannot retrieve XMLHttpRequest response body as XML: DOMParser is not defined. You are likely using an environment that is not browser or does not polyfill browser globals correctly.'\n )\n return null\n }\n\n if (isDomParserSupportedType(contentType)) {\n return new DOMParser().parseFromString(\n this.responseBufferToText(),\n contentType\n )\n }\n\n return null\n }\n\n public errorWith(error?: Error): void {\n /**\n * @note Mark this request as handled even if it received a mock error.\n * This prevents the controller from trying to perform this request as-is.\n */\n this[kIsRequestHandled] = true\n this.logger.info('responding with an error')\n\n this.setReadyState(this.request.DONE)\n this.trigger('error', this.request)\n this.trigger('loadend', this.request)\n }\n\n /**\n * Transitions this request's `readyState` to the given one.\n */\n private setReadyState(nextReadyState: number): void {\n this.logger.info(\n 'setReadyState: %d -> %d',\n this.request.readyState,\n nextReadyState\n )\n\n if (this.request.readyState === nextReadyState) {\n this.logger.info('ready state identical, skipping transition...')\n return\n }\n\n define(this.request, 'readyState', nextReadyState)\n\n this.logger.info('set readyState to: %d', nextReadyState)\n\n if (nextReadyState !== this.request.UNSENT) {\n this.logger.info('triggering \"readystatechange\" event...')\n\n this.trigger('readystatechange', this.request)\n }\n }\n\n /**\n * Triggers given event on the `XMLHttpRequest` instance.\n */\n private trigger<\n EventName extends keyof (XMLHttpRequestEventTargetEventMap & {\n readystatechange: ProgressEvent<XMLHttpRequestEventTarget>\n }),\n >(\n eventName: EventName,\n target: XMLHttpRequest | XMLHttpRequestUpload,\n options?: ProgressEventInit\n ): void {\n const callback = (target as XMLHttpRequest)[`on${eventName}`]\n const event = createEvent(target, eventName, options)\n\n this.logger.info('trigger \"%s\"', eventName, options || '')\n\n // Invoke direct callbacks.\n if (typeof callback === 'function') {\n this.logger.info('found a direct \"%s\" callback, calling...', eventName)\n callback.call(target as XMLHttpRequest, event)\n }\n\n // Invoke event listeners.\n const events =\n target instanceof XMLHttpRequestUpload ? this.uploadEvents : this.events\n\n for (const [registeredEventName, listeners] of events) {\n if (registeredEventName === eventName) {\n this.logger.info(\n 'found %d listener(s) for \"%s\" event, calling...',\n listeners.length,\n eventName\n )\n\n listeners.forEach((listener) => listener.call(target, event))\n }\n }\n }\n\n /**\n * Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.\n */\n private toFetchApiRequest(\n body: XMLHttpRequestBodyInit | Document | null | undefined\n ): Request {\n this.logger.info('converting request to a Fetch API Request...')\n\n // If the `Document` is used as the body of this XMLHttpRequest,\n // set its inner text as the Fetch API Request body.\n const resolvedBody =\n body instanceof Document ? body.documentElement.innerText : body\n\n const fetchRequest = new Request(this.url.href, {\n method: this.method,\n headers: this.requestHeaders,\n /**\n * @see https://xhr.spec.whatwg.org/#cross-origin-credentials\n */\n credentials: this.request.withCredentials ? 'include' : 'same-origin',\n body: ['GET', 'HEAD'].includes(this.method.toUpperCase())\n ? null\n : resolvedBody,\n })\n\n const proxyHeaders = createProxy(fetchRequest.headers, {\n methodCall: ([methodName, args], invoke) => {\n // Forward the latest state of the internal request headers\n // because the interceptor might have modified them\n // without responding to the request.\n switch (methodName) {\n case 'append':\n case 'set': {\n const [headerName, headerValue] = args as [string, string]\n this.request.setRequestHeader(headerName, headerValue)\n break\n }\n\n case 'delete': {\n const [headerName] = args as [string]\n console.warn(\n `XMLHttpRequest: Cannot remove a \"${headerName}\" header from the Fetch API representation of the \"${fetchRequest.method} ${fetchRequest.url}\" request. XMLHttpRequest headers cannot be removed.`\n )\n break\n }\n }\n\n return invoke()\n },\n })\n define(fetchRequest, 'headers', proxyHeaders)\n setRawRequest(fetchRequest, this.request)\n\n this.logger.info('converted request to a Fetch API Request!', fetchRequest)\n\n return fetchRequest\n }\n}\n\nfunction toAbsoluteUrl(url: string | URL): URL {\n /**\n * @note XMLHttpRequest interceptor may run in environments\n * that implement XMLHttpRequest but don't implement \"location\"\n * (for example, React Native). If that's the case, return the\n * input URL as-is (nothing to be relative to).\n * @see https://github.com/mswjs/msw/issues/1777\n */\n if (typeof location === 'undefined') {\n return new URL(url)\n }\n\n return new URL(url.toString(), location.href)\n}\n\nfunction define(\n target: object,\n property: string | symbol,\n value: unknown\n): void {\n Reflect.defineProperty(target, property, {\n // Ensure writable properties to allow redefining readonly properties.\n writable: true,\n enumerable: true,\n value,\n })\n}\n","/**\n * Concatenate two `Uint8Array` buffers.\n */\nexport function concatArrayBuffer(\n left: Uint8Array,\n right: Uint8Array\n): Uint8Array {\n const result = new Uint8Array(left.byteLength + right.byteLength)\n result.set(left, 0)\n result.set(right, left.byteLength)\n return result\n}\n","export class EventPolyfill implements Event {\n readonly NONE = 0\n readonly CAPTURING_PHASE = 1\n readonly AT_TARGET = 2\n readonly BUBBLING_PHASE = 3\n\n public type: string = ''\n public srcElement: EventTarget | null = null\n public target: EventTarget | null\n public currentTarget: EventTarget | null = null\n public eventPhase: number = 0\n public timeStamp: number\n public isTrusted: boolean = true\n public composed: boolean = false\n public cancelable: boolean = true\n public defaultPrevented: boolean = false\n public bubbles: boolean = true\n public lengthComputable: boolean = true\n public loaded: number = 0\n public total: number = 0\n\n cancelBubble: boolean = false\n returnValue: boolean = true\n\n constructor(\n type: string,\n options?: { target: EventTarget; currentTarget: EventTarget }\n ) {\n this.type = type\n this.target = options?.target || null\n this.currentTarget = options?.currentTarget || null\n this.timeStamp = Date.now()\n }\n\n public composedPath(): EventTarget[] {\n return []\n }\n\n public initEvent(type: string, bubbles?: boolean, cancelable?: boolean) {\n this.type = type\n this.bubbles = !!bubbles\n this.cancelable = !!cancelable\n }\n\n public preventDefault() {\n this.defaultPrevented = true\n }\n\n public stopPropagation() {}\n public stopImmediatePropagation() {}\n}\n","import { EventPolyfill } from './EventPolyfill'\n\nexport class ProgressEventPolyfill extends EventPolyfill {\n readonly lengthComputable: boolean\n readonly composed: boolean\n readonly loaded: number\n readonly total: number\n\n constructor(type: string, init?: ProgressEventInit) {\n super(type)\n\n this.lengthComputable = init?.lengthComputable || false\n this.composed = init?.composed || false\n this.loaded = init?.loaded || 0\n this.total = init?.total || 0\n }\n}\n","import { EventPolyfill } from '../polyfills/EventPolyfill'\nimport { ProgressEventPolyfill } from '../polyfills/ProgressEventPolyfill'\n\nconst SUPPORTS_PROGRESS_EVENT = typeof ProgressEvent !== 'undefined'\n\nexport function createEvent(\n target: XMLHttpRequest | XMLHttpRequestUpload,\n type: string,\n init?: ProgressEventInit\n): EventPolyfill | ProgressEvent {\n const progressEvents = [\n 'error',\n 'progress',\n 'loadstart',\n 'loadend',\n 'load',\n 'timeout',\n 'abort',\n ]\n\n /**\n * `ProgressEvent` is not supported in React Native.\n * @see https://github.com/mswjs/interceptors/issues/40\n */\n const ProgressEventClass = SUPPORTS_PROGRESS_EVENT\n ? ProgressEvent\n : ProgressEventPolyfill\n\n const event = progressEvents.includes(type)\n ? new ProgressEventClass(type, {\n lengthComputable: true,\n loaded: init?.loaded || 0,\n total: init?.total || 0,\n })\n : new EventPolyfill(type, {\n target,\n currentTarget: target,\n })\n\n return event\n}\n","/**\n * Returns the source object of the given property on the target object\n * (the target itself, any parent in its prototype, or null).\n */\nexport function findPropertySource(\n target: object,\n propertyName: string | symbol\n): object | null {\n if (!(propertyName in target)) {\n return null\n }\n\n const hasProperty = Object.prototype.hasOwnProperty.call(target, propertyName)\n if (hasProperty) {\n return target\n }\n\n const prototype = Reflect.getPrototypeOf(target)\n return prototype ? findPropertySource(prototype, propertyName) : null\n}\n","import { findPropertySource } from './findPropertySource'\n\nexport interface ProxyOptions<Target extends Record<string, any>> {\n constructorCall?(args: Array<unknown>, next: NextFunction<Target>): Target\n\n methodCall?<F extends keyof Target>(\n this: Target,\n data: [methodName: F, args: Array<unknown>],\n next: NextFunction<void>\n ): void\n\n setProperty?(\n data: [propertyName: string | symbol, nextValue: unknown],\n next: NextFunction<boolean>\n ): boolean\n\n getProperty?(\n data: [propertyName: string | symbol, receiver: Target],\n next: NextFunction<void>\n ): void\n}\n\nexport type NextFunction<ReturnType> = () => ReturnType\n\nexport function createProxy<Target extends object>(\n target: Target,\n options: ProxyOptions<Target>\n): Target {\n const proxy = new Proxy(target, optionsToProxyHandler(options))\n\n return proxy\n}\n\nfunction optionsToProxyHandler<T extends Record<string, any>>(\n options: ProxyOptions<T>\n): ProxyHandler<T> {\n const { constructorCall, methodCall, getProperty, setProperty } = options\n const handler: ProxyHandler<T> = {}\n\n if (typeof constructorCall !== 'undefined') {\n handler.construct = function (target, args, newTarget) {\n const next = Reflect.construct.bind(null, target as any, args, newTarget)\n return constructorCall.call(newTarget, args, next)\n }\n }\n\n handler.set = function (target, propertyName, nextValue) {\n const next = () => {\n const propertySource = findPropertySource(target, propertyName) || target\n const ownDescriptors = Reflect.getOwnPropertyDescriptor(\n propertySource,\n propertyName\n )\n\n // Respect any custom setters present for this property.\n if (typeof ownDescriptors?.set !== 'undefined') {\n ownDescriptors.set.apply(target, [nextValue])\n return true\n }\n\n // Otherwise, set the property on the source.\n return Reflect.defineProperty(propertySource, propertyName, {\n writable: true,\n enumerable: true,\n configurable: true,\n value: nextValue,\n })\n }\n\n if (typeof setProperty !== 'undefined') {\n return setProperty.call(target, [propertyName, nextValue], next)\n }\n\n return next()\n }\n\n handler.get = function (target, propertyName, receiver) {\n /**\n * @note Using `Reflect.get()` here causes \"TypeError: Illegal invocation\".\n */\n const next = () => target[propertyName as any]\n\n const value =\n typeof getProperty !== 'undefined'\n ? getProperty.call(target, [propertyName, receiver], next)\n : next()\n\n if (typeof value === 'function') {\n return (...args: Array<any>) => {\n const next = value.bind(target, ...args)\n\n if (typeof methodCall !== 'undefined') {\n return methodCall.call(target, [propertyName as any, args], next)\n }\n\n return next()\n }\n }\n\n return value\n }\n\n return handler\n}\n","export function isDomParserSupportedType(\n type: string\n): type is DOMParserSupportedType {\n const supportedTypes: Array<DOMParserSupportedType> = [\n 'application/xhtml+xml',\n 'application/xml',\n 'image/svg+xml',\n 'text/html',\n 'text/xml',\n ]\n return supportedTypes.some((supportedType) => {\n return type.startsWith(supportedType)\n })\n}\n","/**\n * Parses a given string into JSON.\n * Gracefully handles invalid JSON by returning `null`.\n */\nexport function parseJson(data: string): Record<string, unknown> | null {\n try {\n const json = JSON.parse(data)\n return json\n } catch (_) {\n return null\n }\n}\n","import { FetchResponse } from '../../../utils/fetchUtils'\n\n/**\n * Creates a Fetch API `Response` instance from the given\n * `XMLHttpRequest` instance and a response body.\n */\nexport function createResponse(\n request: XMLHttpRequest,\n body: BodyInit | null\n): Response {\n /**\n * Handle XMLHttpRequest responses that must have null as the\n * response body when represented using Fetch API Response.\n * XMLHttpRequest response will always have an empty string\n * as the \"request.response\" in those cases, resulting in an error\n * when constructing a Response instance.\n * @see https://github.com/mswjs/interceptors/issues/379\n */\n const responseBodyOrNull = FetchResponse.isResponseWithBody(request.status)\n ? body\n : null\n\n return new FetchResponse(responseBodyOrNull, {\n url: request.responseURL,\n status: request.status,\n statusText: request.statusText,\n headers: createHeadersFromXMLHttpRequestHeaders(\n request.getAllResponseHeaders()\n ),\n })\n}\n\nfunction createHeadersFromXMLHttpRequestHeaders(headersString: string): Headers {\n const headers = new Headers()\n\n const lines = headersString.split(/[\\r\\n]+/)\n for (const line of lines) {\n if (line.trim() === '') {\n continue\n }\n\n const [name, ...parts] = line.split(': ')\n const value = parts.join(': ')\n\n headers.append(name, value)\n }\n\n return headers\n}\n","/**\n * Return a total byte length of the given request/response body.\n * If the `Content-Length` header is present, it will be used as the byte length.\n */\nexport async function getBodyByteLength(\n input: Request | Response\n): Promise<number> {\n const explicitContentLength = input.headers.get('content-length')\n\n if (explicitContentLength != null && explicitContentLength !== '') {\n return Number(explicitContentLength)\n }\n\n const buffer = await input.arrayBuffer()\n return buffer.byteLength\n}\n","import type { Logger } from '@open-draft/logger'\nimport { XMLHttpRequestEmitter } from '.'\nimport { RequestController } from '../../RequestController'\nimport { XMLHttpRequestController } from './XMLHttpRequestController'\nimport { handleRequest } from '../../utils/handleRequest'\nimport { isResponseError } from '../../utils/responseUtils'\n\nexport interface XMLHttpRequestProxyOptions {\n emitter: XMLHttpRequestEmitter\n logger: Logger\n}\n\n/**\n * Create a proxied `XMLHttpRequest` class.\n * The proxied class establishes spies on certain methods,\n * allowing us to intercept requests and respond to them.\n */\nexport function createXMLHttpRequestProxy({\n emitter,\n logger,\n}: XMLHttpRequestProxyOptions) {\n const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {\n construct(target, args, newTarget) {\n logger.info('constructed new XMLHttpRequest')\n\n const originalRequest = Reflect.construct(\n target,\n args,\n newTarget\n ) as XMLHttpRequest\n\n /**\n * @note Forward prototype descriptors onto the proxied object.\n * XMLHttpRequest is implemented in JSDOM in a way that assigns\n * a bunch of descriptors, like \"set responseType()\" on the prototype.\n * With this propagation, we make sure that those descriptors trigger\n * when the user operates with the proxied request instance.\n */\n const prototypeDescriptors = Object.getOwnPropertyDescriptors(\n target.prototype\n )\n for (const propertyName in prototypeDescriptors) {\n Reflect.defineProperty(\n originalRequest,\n propertyName,\n prototypeDescriptors[propertyName]\n )\n }\n\n const xhrRequestController = new XMLHttpRequestController(\n originalRequest,\n logger\n )\n\n xhrRequestController.onRequest = async function ({ request, requestId }) {\n const controller = new RequestController(request, {\n passthrough: () => {\n this.logger.info(\n 'no mocked response received, performing request as-is...'\n )\n },\n respondWith: async (response) => {\n if (isResponseError(response)) {\n this.errorWith(new TypeError('Network error'))\n return\n }\n\n await this.respondWith(response)\n },\n errorWith: (reason) => {\n this.logger.info('request errored!', { error: reason })\n\n if (reason instanceof Error) {\n this.errorWith(reason)\n }\n },\n })\n\n this.logger.info('awaiting mocked response...')\n\n this.logger.info(\n 'emitting the \"request\" event for %s listener(s)...',\n emitter.listenerCount('request')\n )\n\n await handleRequest({\n request,\n requestId,\n controller,\n emitter,\n })\n }\n\n xhrRequestController.onResponse = async function ({\n response,\n isMockedResponse,\n request,\n requestId,\n }) {\n this.logger.info(\n 'emitting the \"response\" event for %s listener(s)...',\n emitter.listenerCount('response')\n )\n\n emitter.emit('response', {\n response,\n isMockedResponse,\n request,\n requestId,\n })\n }\n\n // Return the proxied request from the controller\n // so that the controller can react to the consumer's interactions\n // with this request (opening/sending/etc).\n return xhrRequestController.request\n },\n })\n\n return XMLHttpRequestProxy\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,aAAAA,kBAAiB;;;ACA1B,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB;;;ACEvB,SAAS,kBACd,MACA,OACY;AACZ,QAAM,SAAS,IAAI,WAAW,KAAK,aAAa,MAAM,UAAU;AAChE,SAAO,IAAI,MAAM,CAAC;AAClB,SAAO,IAAI,OAAO,KAAK,UAAU;AACjC,SAAO;AACT;;;ACXO,IAAM,gBAAN,MAAqC;AAAA,EAwB1C,YACE,MACA,SACA;AA1BF,SAAS,OAAO;AAChB,SAAS,kBAAkB;AAC3B,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAE1B,SAAO,OAAe;AACtB,SAAO,aAAi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U,QAAQ;AAChB,UAAM,SAAS,KAAK,OAAO,OAAO,OAAO;AAEzC,WAAO,KAAK,qCAAqC;AAEjD,UAAM,qBAAqB,WAAW;AAEtC,IAAAC;AAAA,MACE,CAAE,mBAA2B,iBAAiB;AAAA,MAC9C;AAAA,IACF;AAEA,eAAW,iBAAiB,0BAA0B;AAAA,MACpD,SAAS,KAAK;AAAA,MACd,QAAQ,KAAK;AAAA,IACf,CAAC;AAED,WAAO;AAAA,MACL;AAAA,MACA,WAAW,eAAe;AAAA,IAC5B;AAEA,WAAO,eAAe,WAAW,gBAAgB,mBAAmB;AAAA,MAClE,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,OAAO;AAAA,IACT,CAAC;AAED,SAAK,cAAc,KAAK,MAAM;AAC5B,aAAO,eAAe,WAAW,gBAAgB,mBAAmB;AAAA,QAClE,OAAO;AAAA,MACT,CAAC;AAED,iBAAW,iBAAiB;AAC5B,aAAO;AAAA,QACL;AAAA,QACA,WAAW,eAAe;AAAA,MAC5B;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAnDO,IAAM,4BAAN;AAAM,0BACJ,oBAAoB,OAAO,KAAK;","names":["invariant","next","invariant"]}
@@ -1,13 +1,14 @@
1
1
  import {
2
- RequestController,
3
- handleRequest
4
- } from "./chunk-PTTUYYVR.mjs";
2
+ handleRequest,
3
+ isResponseError
4
+ } from "./chunk-VOUOVDAW.mjs";
5
5
  import {
6
6
  FetchResponse,
7
7
  IS_PATCHED_MODULE,
8
+ RequestController,
8
9
  canParseUrl,
9
10
  setRawRequest
10
- } from "./chunk-3RXCRGL2.mjs";
11
+ } from "./chunk-LIKZF2VU.mjs";
11
12
  import {
12
13
  emitAsync,
13
14
  hasConfigurableGlobal
@@ -15,10 +16,11 @@ import {
15
16
  import {
16
17
  Interceptor,
17
18
  createRequestId
18
- } from "./chunk-QED3Q6Z2.mjs";
19
+ } from "./chunk-Z5TSB3T6.mjs";
19
20
 
20
21
  // src/interceptors/fetch/index.ts
21
22
  import { invariant } from "outvariant";
23
+ import { until } from "@open-draft/until";
22
24
  import { DeferredPromise } from "@open-draft/deferred-promise";
23
25
 
24
26
  // src/interceptors/fetch/utils/createNetworkError.ts
@@ -188,19 +190,35 @@ var _FetchInterceptor = class extends Interceptor {
188
190
  setRawRequest(request, input);
189
191
  }
190
192
  const responsePromise = new DeferredPromise();
191
- const controller = new RequestController(request);
192
- this.logger.info("[%s] %s", request.method, request.url);
193
- this.logger.info("awaiting for the mocked response...");
194
- this.logger.info(
195
- 'emitting the "request" event for %s listener(s)...',
196
- this.emitter.listenerCount("request")
197
- );
198
- const isRequestHandled = await handleRequest({
199
- request,
200
- requestId,
201
- emitter: this.emitter,
202
- controller,
203
- onResponse: async (rawResponse) => {
193
+ const controller = new RequestController(request, {
194
+ passthrough: async () => {
195
+ this.logger.info("request has not been handled, passthrough...");
196
+ const requestCloneForResponseEvent = request.clone();
197
+ const { error: responseError, data: originalResponse } = await until(
198
+ () => pureFetch(request)
199
+ );
200
+ if (responseError) {
201
+ return responsePromise.reject(responseError);
202
+ }
203
+ this.logger.info("original fetch performed", originalResponse);
204
+ if (this.emitter.listenerCount("response") > 0) {
205
+ this.logger.info('emitting the "response" event...');
206
+ const responseClone = originalResponse.clone();
207
+ await emitAsync(this.emitter, "response", {
208
+ response: responseClone,
209
+ isMockedResponse: false,
210
+ request: requestCloneForResponseEvent,
211
+ requestId
212
+ });
213
+ }
214
+ responsePromise.resolve(originalResponse);
215
+ },
216
+ respondWith: async (rawResponse) => {
217
+ if (isResponseError(rawResponse)) {
218
+ this.logger.info("request has errored!", { response: rawResponse });
219
+ responsePromise.reject(createNetworkError(rawResponse));
220
+ return;
221
+ }
204
222
  this.logger.info("received mocked response!", {
205
223
  rawResponse
206
224
  });
@@ -238,37 +256,24 @@ var _FetchInterceptor = class extends Interceptor {
238
256
  }
239
257
  responsePromise.resolve(response);
240
258
  },
241
- onRequestError: (response) => {
242
- this.logger.info("request has errored!", { response });
243
- responsePromise.reject(createNetworkError(response));
244
- },
245
- onError: (error) => {
246
- this.logger.info("request has been aborted!", { error });
247
- responsePromise.reject(error);
259
+ errorWith: (reason) => {
260
+ this.logger.info("request has been aborted!", { reason });
261
+ responsePromise.reject(reason);
248
262
  }
249
263
  });
250
- if (isRequestHandled) {
251
- this.logger.info("request has been handled, returning mock promise...");
252
- return responsePromise;
253
- }
264
+ this.logger.info("[%s] %s", request.method, request.url);
265
+ this.logger.info("awaiting for the mocked response...");
254
266
  this.logger.info(
255
- "no mocked response received, performing request as-is..."
267
+ 'emitting the "request" event for %s listener(s)...',
268
+ this.emitter.listenerCount("request")
256
269
  );
257
- const requestCloneForResponseEvent = request.clone();
258
- return pureFetch(request).then(async (response) => {
259
- this.logger.info("original fetch performed", response);
260
- if (this.emitter.listenerCount("response") > 0) {
261
- this.logger.info('emitting the "response" event...');
262
- const responseClone = response.clone();
263
- await emitAsync(this.emitter, "response", {
264
- response: responseClone,
265
- isMockedResponse: false,
266
- request: requestCloneForResponseEvent,
267
- requestId
268
- });
269
- }
270
- return response;
270
+ await handleRequest({
271
+ request,
272
+ requestId,
273
+ emitter: this.emitter,
274
+ controller
271
275
  });
276
+ return responsePromise;
272
277
  };
273
278
  Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
274
279
  enumerable: true,
@@ -293,4 +298,4 @@ FetchInterceptor.symbol = Symbol("fetch");
293
298
  export {
294
299
  FetchInterceptor
295
300
  };
296
- //# sourceMappingURL=chunk-Q7K2XAEP.mjs.map
301
+ //# sourceMappingURL=chunk-WOWPV4GR.mjs.map