@mswjs/interceptors 0.40.0 → 0.41.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 (272) hide show
  1. package/ClientRequest/package.json +7 -2
  2. package/RemoteHttpInterceptor/package.json +7 -2
  3. package/WebSocket/package.json +9 -2
  4. package/XMLHttpRequest/package.json +9 -3
  5. package/fetch/package.json +9 -3
  6. package/lib/browser/Interceptor-2mUoKZL1.d.mts +65 -0
  7. package/lib/browser/Interceptor-Deczogc8.d.cts +65 -0
  8. package/lib/browser/XMLHttpRequest-BUfglQD1.cjs +761 -0
  9. package/lib/browser/XMLHttpRequest-BUfglQD1.cjs.map +1 -0
  10. package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs +756 -0
  11. package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs.map +1 -0
  12. package/lib/browser/bufferUtils-BiiO6HZv.mjs +20 -0
  13. package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +1 -0
  14. package/lib/browser/bufferUtils-Uc0eRItL.cjs +38 -0
  15. package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +1 -0
  16. package/lib/browser/createRequestId-Cs4oXfa1.cjs +205 -0
  17. package/lib/browser/createRequestId-Cs4oXfa1.cjs.map +1 -0
  18. package/lib/browser/createRequestId-DQcIlohW.mjs +170 -0
  19. package/lib/browser/createRequestId-DQcIlohW.mjs.map +1 -0
  20. package/lib/browser/fetch-BHcqM3z7.cjs +253 -0
  21. package/lib/browser/fetch-BHcqM3z7.cjs.map +1 -0
  22. package/lib/browser/fetch-DSJoynSF.mjs +248 -0
  23. package/lib/browser/fetch-DSJoynSF.mjs.map +1 -0
  24. package/lib/browser/getRawRequest-BTaNLFr0.mjs +218 -0
  25. package/lib/browser/getRawRequest-BTaNLFr0.mjs.map +1 -0
  26. package/lib/browser/getRawRequest-zx8rUJL2.cjs +259 -0
  27. package/lib/browser/getRawRequest-zx8rUJL2.cjs.map +1 -0
  28. package/lib/browser/glossary-BdLS4k1H.d.cts +70 -0
  29. package/lib/browser/glossary-gEEJhK4S.d.mts +70 -0
  30. package/lib/browser/handleRequest-DI6a7Dty.cjs +189 -0
  31. package/lib/browser/handleRequest-DI6a7Dty.cjs.map +1 -0
  32. package/lib/browser/handleRequest-DxGbCTbb.mjs +178 -0
  33. package/lib/browser/handleRequest-DxGbCTbb.mjs.map +1 -0
  34. package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs +33 -0
  35. package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs.map +1 -0
  36. package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs +45 -0
  37. package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs.map +1 -0
  38. package/lib/browser/index.cjs +68 -0
  39. package/lib/browser/index.cjs.map +1 -0
  40. package/lib/browser/index.d.cts +87 -0
  41. package/lib/browser/index.d.mts +87 -0
  42. package/lib/browser/index.mjs +49 -77
  43. package/lib/browser/index.mjs.map +1 -1
  44. package/lib/browser/interceptors/WebSocket/index.cjs +621 -0
  45. package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -0
  46. package/lib/browser/interceptors/WebSocket/index.d.cts +277 -0
  47. package/lib/browser/interceptors/WebSocket/index.d.mts +277 -0
  48. package/lib/browser/interceptors/WebSocket/index.mjs +587 -694
  49. package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
  50. package/lib/browser/interceptors/XMLHttpRequest/index.cjs +7 -0
  51. package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +15 -0
  52. package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +15 -0
  53. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +7 -12
  54. package/lib/browser/interceptors/fetch/index.cjs +6 -0
  55. package/lib/browser/interceptors/fetch/index.d.cts +13 -0
  56. package/lib/browser/interceptors/fetch/index.d.mts +13 -0
  57. package/lib/browser/interceptors/fetch/index.mjs +6 -11
  58. package/lib/browser/presets/browser.cjs +17 -0
  59. package/lib/browser/presets/browser.cjs.map +1 -0
  60. package/lib/browser/presets/browser.d.cts +12 -0
  61. package/lib/browser/presets/browser.d.mts +14 -0
  62. package/lib/browser/presets/browser.mjs +15 -19
  63. package/lib/browser/presets/browser.mjs.map +1 -1
  64. package/lib/node/BatchInterceptor-3LnAnLTx.cjs +49 -0
  65. package/lib/node/BatchInterceptor-3LnAnLTx.cjs.map +1 -0
  66. package/lib/node/BatchInterceptor-D7mXzHcQ.d.mts +26 -0
  67. package/lib/node/BatchInterceptor-DFaBPilf.mjs +44 -0
  68. package/lib/node/BatchInterceptor-DFaBPilf.mjs.map +1 -0
  69. package/lib/node/BatchInterceptor-D_YqR8qU.d.cts +26 -0
  70. package/lib/node/ClientRequest-2rDe54Ui.cjs +1043 -0
  71. package/lib/node/ClientRequest-2rDe54Ui.cjs.map +1 -0
  72. package/lib/node/ClientRequest-Ca8Qykuv.mjs +1034 -0
  73. package/lib/node/ClientRequest-Ca8Qykuv.mjs.map +1 -0
  74. package/lib/node/Interceptor-DEazpLJd.d.mts +133 -0
  75. package/lib/node/Interceptor-DJ2akVWI.d.cts +133 -0
  76. package/lib/node/RemoteHttpInterceptor.cjs +154 -0
  77. package/lib/node/RemoteHttpInterceptor.cjs.map +1 -0
  78. package/lib/node/RemoteHttpInterceptor.d.cts +39 -0
  79. package/lib/node/RemoteHttpInterceptor.d.mts +39 -0
  80. package/lib/node/RemoteHttpInterceptor.mjs +145 -190
  81. package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
  82. package/lib/node/XMLHttpRequest-B7kJdYYI.cjs +763 -0
  83. package/lib/node/XMLHttpRequest-B7kJdYYI.cjs.map +1 -0
  84. package/lib/node/XMLHttpRequest-C8dIZpds.mjs +757 -0
  85. package/lib/node/XMLHttpRequest-C8dIZpds.mjs.map +1 -0
  86. package/lib/node/bufferUtils-DiCTqG-7.cjs +38 -0
  87. package/lib/node/bufferUtils-DiCTqG-7.cjs.map +1 -0
  88. package/lib/node/bufferUtils-_8XfKIfX.mjs +20 -0
  89. package/lib/node/bufferUtils-_8XfKIfX.mjs.map +1 -0
  90. package/lib/node/chunk-CbDLau6x.cjs +34 -0
  91. package/lib/node/fetch-BmXpK10r.cjs +272 -0
  92. package/lib/node/fetch-BmXpK10r.cjs.map +1 -0
  93. package/lib/node/fetch-G1DVwDKG.mjs +265 -0
  94. package/lib/node/fetch-G1DVwDKG.mjs.map +1 -0
  95. package/lib/node/fetchUtils-BaY5iWXw.cjs +419 -0
  96. package/lib/node/fetchUtils-BaY5iWXw.cjs.map +1 -0
  97. package/lib/node/fetchUtils-CoU35g3M.mjs +359 -0
  98. package/lib/node/fetchUtils-CoU35g3M.mjs.map +1 -0
  99. package/lib/node/getRawRequest-BavnMWh_.cjs +36 -0
  100. package/lib/node/getRawRequest-BavnMWh_.cjs.map +1 -0
  101. package/lib/node/getRawRequest-DnwmXyOW.mjs +24 -0
  102. package/lib/node/getRawRequest-DnwmXyOW.mjs.map +1 -0
  103. package/lib/node/glossary-BLKRyLBd.cjs +12 -0
  104. package/lib/node/glossary-BLKRyLBd.cjs.map +1 -0
  105. package/lib/node/glossary-glQBRnVD.mjs +6 -0
  106. package/lib/node/glossary-glQBRnVD.mjs.map +1 -0
  107. package/lib/node/handleRequest-Bb7Y-XLw.cjs +220 -0
  108. package/lib/node/handleRequest-Bb7Y-XLw.cjs.map +1 -0
  109. package/lib/node/handleRequest-Y97UwBbF.mjs +190 -0
  110. package/lib/node/handleRequest-Y97UwBbF.mjs.map +1 -0
  111. package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs +26 -0
  112. package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs.map +1 -0
  113. package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs +20 -0
  114. package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs.map +1 -0
  115. package/lib/node/index-BMbJ8FXL.d.cts +113 -0
  116. package/lib/node/index-C0YAQ36w.d.mts +113 -0
  117. package/lib/node/index.cjs +30 -0
  118. package/lib/node/index.cjs.map +1 -0
  119. package/lib/node/index.d.cts +66 -0
  120. package/lib/node/index.d.mts +66 -0
  121. package/lib/node/index.mjs +13 -41
  122. package/lib/node/index.mjs.map +1 -1
  123. package/lib/node/interceptors/ClientRequest/index.cjs +6 -0
  124. package/lib/node/interceptors/ClientRequest/index.d.cts +2 -0
  125. package/lib/node/interceptors/ClientRequest/index.d.mts +3 -0
  126. package/lib/node/interceptors/ClientRequest/index.mjs +6 -11
  127. package/lib/node/interceptors/XMLHttpRequest/index.cjs +6 -0
  128. package/lib/node/interceptors/XMLHttpRequest/index.d.cts +14 -0
  129. package/lib/node/interceptors/XMLHttpRequest/index.d.mts +14 -0
  130. package/lib/node/interceptors/XMLHttpRequest/index.mjs +6 -13
  131. package/lib/node/interceptors/fetch/index.cjs +5 -0
  132. package/lib/node/interceptors/fetch/index.d.cts +12 -0
  133. package/lib/node/interceptors/fetch/index.d.mts +12 -0
  134. package/lib/node/interceptors/fetch/index.mjs +5 -12
  135. package/lib/node/node-DwCc6iuP.mjs +27 -0
  136. package/lib/node/node-DwCc6iuP.mjs.map +1 -0
  137. package/lib/node/node-dKdAf3tC.cjs +39 -0
  138. package/lib/node/node-dKdAf3tC.cjs.map +1 -0
  139. package/lib/node/presets/node.cjs +22 -0
  140. package/lib/node/presets/node.cjs.map +1 -0
  141. package/lib/node/presets/node.d.cts +13 -0
  142. package/lib/node/presets/node.d.mts +15 -0
  143. package/lib/node/presets/node.mjs +18 -23
  144. package/lib/node/presets/node.mjs.map +1 -1
  145. package/lib/node/utils/node/index.cjs +4 -0
  146. package/lib/node/utils/node/{index.d.ts → index.d.cts} +5 -2
  147. package/lib/node/utils/node/index.d.mts +16 -0
  148. package/lib/node/utils/node/index.mjs +3 -10
  149. package/package.json +33 -59
  150. package/presets/browser/package.json +2 -3
  151. package/presets/node/package.json +7 -2
  152. package/src/interceptors/ClientRequest/MockHttpSocket.ts +21 -8
  153. package/src/interceptors/ClientRequest/utils/parserUtils.ts +48 -0
  154. package/src/interceptors/WebSocket/index.ts +4 -1
  155. package/src/interceptors/XMLHttpRequest/utils/getBodyByteLength.test.ts +2 -2
  156. package/lib/browser/Interceptor-af98b768.d.ts +0 -63
  157. package/lib/browser/chunk-2HUMWGRD.js +0 -37
  158. package/lib/browser/chunk-2HUMWGRD.js.map +0 -1
  159. package/lib/browser/chunk-2MCNQOY3.js +0 -851
  160. package/lib/browser/chunk-2MCNQOY3.js.map +0 -1
  161. package/lib/browser/chunk-57RIRQUY.js +0 -218
  162. package/lib/browser/chunk-57RIRQUY.js.map +0 -1
  163. package/lib/browser/chunk-6HYIRFX2.mjs +0 -22
  164. package/lib/browser/chunk-6HYIRFX2.mjs.map +0 -1
  165. package/lib/browser/chunk-FW45TRCB.js +0 -178
  166. package/lib/browser/chunk-FW45TRCB.js.map +0 -1
  167. package/lib/browser/chunk-JQ2S7G56.js +0 -185
  168. package/lib/browser/chunk-JQ2S7G56.js.map +0 -1
  169. package/lib/browser/chunk-LIKZF2VU.mjs +0 -218
  170. package/lib/browser/chunk-LIKZF2VU.mjs.map +0 -1
  171. package/lib/browser/chunk-LK6DILFK.js +0 -22
  172. package/lib/browser/chunk-LK6DILFK.js.map +0 -1
  173. package/lib/browser/chunk-MNT2FUCH.js +0 -301
  174. package/lib/browser/chunk-MNT2FUCH.js.map +0 -1
  175. package/lib/browser/chunk-VOUOVDAW.mjs +0 -178
  176. package/lib/browser/chunk-VOUOVDAW.mjs.map +0 -1
  177. package/lib/browser/chunk-VYSDLBSS.mjs +0 -37
  178. package/lib/browser/chunk-VYSDLBSS.mjs.map +0 -1
  179. package/lib/browser/chunk-WADP6VHN.mjs +0 -851
  180. package/lib/browser/chunk-WADP6VHN.mjs.map +0 -1
  181. package/lib/browser/chunk-WOWPV4GR.mjs +0 -301
  182. package/lib/browser/chunk-WOWPV4GR.mjs.map +0 -1
  183. package/lib/browser/chunk-Z5TSB3T6.mjs +0 -185
  184. package/lib/browser/chunk-Z5TSB3T6.mjs.map +0 -1
  185. package/lib/browser/glossary-f7ee1c9d.d.ts +0 -74
  186. package/lib/browser/index.d.ts +0 -82
  187. package/lib/browser/index.js +0 -83
  188. package/lib/browser/index.js.map +0 -1
  189. package/lib/browser/interceptors/WebSocket/index.d.ts +0 -271
  190. package/lib/browser/interceptors/WebSocket/index.js +0 -721
  191. package/lib/browser/interceptors/WebSocket/index.js.map +0 -1
  192. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +0 -14
  193. package/lib/browser/interceptors/XMLHttpRequest/index.js +0 -12
  194. package/lib/browser/interceptors/XMLHttpRequest/index.js.map +0 -1
  195. package/lib/browser/interceptors/XMLHttpRequest/index.mjs.map +0 -1
  196. package/lib/browser/interceptors/fetch/index.d.ts +0 -13
  197. package/lib/browser/interceptors/fetch/index.js +0 -11
  198. package/lib/browser/interceptors/fetch/index.js.map +0 -1
  199. package/lib/browser/interceptors/fetch/index.mjs.map +0 -1
  200. package/lib/browser/presets/browser.d.ts +0 -14
  201. package/lib/browser/presets/browser.js +0 -21
  202. package/lib/browser/presets/browser.js.map +0 -1
  203. package/lib/node/BatchInterceptor-cb9a2eee.d.ts +0 -24
  204. package/lib/node/Interceptor-dc0a39b5.d.ts +0 -136
  205. package/lib/node/RemoteHttpInterceptor.d.ts +0 -44
  206. package/lib/node/RemoteHttpInterceptor.js +0 -197
  207. package/lib/node/RemoteHttpInterceptor.js.map +0 -1
  208. package/lib/node/chunk-5UGIB6OX.mjs +0 -1126
  209. package/lib/node/chunk-5UGIB6OX.mjs.map +0 -1
  210. package/lib/node/chunk-5V3SIIW2.mjs +0 -853
  211. package/lib/node/chunk-5V3SIIW2.mjs.map +0 -1
  212. package/lib/node/chunk-6B3ZQOO2.js +0 -853
  213. package/lib/node/chunk-6B3ZQOO2.js.map +0 -1
  214. package/lib/node/chunk-6HYIRFX2.mjs +0 -22
  215. package/lib/node/chunk-6HYIRFX2.mjs.map +0 -1
  216. package/lib/node/chunk-6YM4PLBI.mjs +0 -7
  217. package/lib/node/chunk-6YM4PLBI.mjs.map +0 -1
  218. package/lib/node/chunk-73NOP3T5.js +0 -7
  219. package/lib/node/chunk-73NOP3T5.js.map +0 -1
  220. package/lib/node/chunk-7Q53NNPV.js +0 -189
  221. package/lib/node/chunk-7Q53NNPV.js.map +0 -1
  222. package/lib/node/chunk-DOWWQYXZ.js +0 -369
  223. package/lib/node/chunk-DOWWQYXZ.js.map +0 -1
  224. package/lib/node/chunk-FRZQJNBO.js +0 -318
  225. package/lib/node/chunk-FRZQJNBO.js.map +0 -1
  226. package/lib/node/chunk-GKN5RBVR.mjs +0 -32
  227. package/lib/node/chunk-GKN5RBVR.mjs.map +0 -1
  228. package/lib/node/chunk-J5MULIHT.js +0 -51
  229. package/lib/node/chunk-J5MULIHT.js.map +0 -1
  230. package/lib/node/chunk-JXGB54LE.mjs +0 -369
  231. package/lib/node/chunk-JXGB54LE.mjs.map +0 -1
  232. package/lib/node/chunk-LK6DILFK.js +0 -22
  233. package/lib/node/chunk-LK6DILFK.js.map +0 -1
  234. package/lib/node/chunk-OFW5L5ET.mjs +0 -318
  235. package/lib/node/chunk-OFW5L5ET.mjs.map +0 -1
  236. package/lib/node/chunk-PFGO5BSM.js +0 -25
  237. package/lib/node/chunk-PFGO5BSM.js.map +0 -1
  238. package/lib/node/chunk-R6T7CL5E.mjs +0 -189
  239. package/lib/node/chunk-R6T7CL5E.mjs.map +0 -1
  240. package/lib/node/chunk-SQ6RHTJR.mjs +0 -51
  241. package/lib/node/chunk-SQ6RHTJR.mjs.map +0 -1
  242. package/lib/node/chunk-SRMAQGPM.js +0 -30
  243. package/lib/node/chunk-SRMAQGPM.js.map +0 -1
  244. package/lib/node/chunk-T3TW4P64.js +0 -32
  245. package/lib/node/chunk-T3TW4P64.js.map +0 -1
  246. package/lib/node/chunk-TX5GBTFY.mjs +0 -25
  247. package/lib/node/chunk-TX5GBTFY.mjs.map +0 -1
  248. package/lib/node/chunk-VYO5XDY2.js +0 -1126
  249. package/lib/node/chunk-VYO5XDY2.js.map +0 -1
  250. package/lib/node/chunk-YWNGXXUQ.mjs +0 -30
  251. package/lib/node/chunk-YWNGXXUQ.mjs.map +0 -1
  252. package/lib/node/index.d.ts +0 -61
  253. package/lib/node/index.js +0 -45
  254. package/lib/node/index.js.map +0 -1
  255. package/lib/node/interceptors/ClientRequest/index.d.ts +0 -110
  256. package/lib/node/interceptors/ClientRequest/index.js +0 -11
  257. package/lib/node/interceptors/ClientRequest/index.js.map +0 -1
  258. package/lib/node/interceptors/ClientRequest/index.mjs.map +0 -1
  259. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +0 -13
  260. package/lib/node/interceptors/XMLHttpRequest/index.js +0 -13
  261. package/lib/node/interceptors/XMLHttpRequest/index.js.map +0 -1
  262. package/lib/node/interceptors/XMLHttpRequest/index.mjs.map +0 -1
  263. package/lib/node/interceptors/fetch/index.d.ts +0 -12
  264. package/lib/node/interceptors/fetch/index.js +0 -12
  265. package/lib/node/interceptors/fetch/index.js.map +0 -1
  266. package/lib/node/interceptors/fetch/index.mjs.map +0 -1
  267. package/lib/node/presets/node.d.ts +0 -15
  268. package/lib/node/presets/node.js +0 -27
  269. package/lib/node/presets/node.js.map +0 -1
  270. package/lib/node/utils/node/index.js +0 -10
  271. package/lib/node/utils/node/index.js.map +0 -1
  272. package/lib/node/utils/node/index.mjs.map +0 -1
@@ -1,318 +0,0 @@
1
- import {
2
- hasConfigurableGlobal
3
- } from "./chunk-TX5GBTFY.mjs";
4
- import {
5
- IS_PATCHED_MODULE
6
- } from "./chunk-6YM4PLBI.mjs";
7
- import {
8
- emitAsync,
9
- handleRequest,
10
- isResponseError
11
- } from "./chunk-R6T7CL5E.mjs";
12
- import {
13
- FetchResponse,
14
- Interceptor,
15
- RequestController,
16
- canParseUrl,
17
- createRequestId
18
- } from "./chunk-JXGB54LE.mjs";
19
- import {
20
- setRawRequest
21
- } from "./chunk-YWNGXXUQ.mjs";
22
-
23
- // src/interceptors/fetch/index.ts
24
- import { invariant } from "outvariant";
25
- import { until } from "@open-draft/until";
26
- import { DeferredPromise } from "@open-draft/deferred-promise";
27
-
28
- // src/interceptors/fetch/utils/createNetworkError.ts
29
- function createNetworkError(cause) {
30
- return Object.assign(new TypeError("Failed to fetch"), {
31
- cause
32
- });
33
- }
34
-
35
- // src/interceptors/fetch/utils/followRedirect.ts
36
- var REQUEST_BODY_HEADERS = [
37
- "content-encoding",
38
- "content-language",
39
- "content-location",
40
- "content-type",
41
- "content-length"
42
- ];
43
- var kRedirectCount = Symbol("kRedirectCount");
44
- async function followFetchRedirect(request, response) {
45
- if (response.status !== 303 && request.body != null) {
46
- return Promise.reject(createNetworkError());
47
- }
48
- const requestUrl = new URL(request.url);
49
- let locationUrl;
50
- try {
51
- locationUrl = new URL(response.headers.get("location"), request.url);
52
- } catch (error) {
53
- return Promise.reject(createNetworkError(error));
54
- }
55
- if (!(locationUrl.protocol === "http:" || locationUrl.protocol === "https:")) {
56
- return Promise.reject(
57
- createNetworkError("URL scheme must be a HTTP(S) scheme")
58
- );
59
- }
60
- if (Reflect.get(request, kRedirectCount) > 20) {
61
- return Promise.reject(createNetworkError("redirect count exceeded"));
62
- }
63
- Object.defineProperty(request, kRedirectCount, {
64
- value: (Reflect.get(request, kRedirectCount) || 0) + 1
65
- });
66
- if (request.mode === "cors" && (locationUrl.username || locationUrl.password) && !sameOrigin(requestUrl, locationUrl)) {
67
- return Promise.reject(
68
- createNetworkError('cross origin not allowed for request mode "cors"')
69
- );
70
- }
71
- const requestInit = {};
72
- if ([301, 302].includes(response.status) && request.method === "POST" || response.status === 303 && !["HEAD", "GET"].includes(request.method)) {
73
- requestInit.method = "GET";
74
- requestInit.body = null;
75
- REQUEST_BODY_HEADERS.forEach((headerName) => {
76
- request.headers.delete(headerName);
77
- });
78
- }
79
- if (!sameOrigin(requestUrl, locationUrl)) {
80
- request.headers.delete("authorization");
81
- request.headers.delete("proxy-authorization");
82
- request.headers.delete("cookie");
83
- request.headers.delete("host");
84
- }
85
- requestInit.headers = request.headers;
86
- const finalResponse = await fetch(new Request(locationUrl, requestInit));
87
- Object.defineProperty(finalResponse, "redirected", {
88
- value: true,
89
- configurable: true
90
- });
91
- return finalResponse;
92
- }
93
- function sameOrigin(left, right) {
94
- if (left.origin === right.origin && left.origin === "null") {
95
- return true;
96
- }
97
- if (left.protocol === right.protocol && left.hostname === right.hostname && left.port === right.port) {
98
- return true;
99
- }
100
- return false;
101
- }
102
-
103
- // src/interceptors/fetch/utils/brotli-decompress.ts
104
- import zlib from "zlib";
105
- var BrotliDecompressionStream = class extends TransformStream {
106
- constructor() {
107
- const decompress = zlib.createBrotliDecompress({
108
- flush: zlib.constants.BROTLI_OPERATION_FLUSH,
109
- finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
110
- });
111
- super({
112
- async transform(chunk, controller) {
113
- const buffer = Buffer.from(chunk);
114
- const decompressed = await new Promise((resolve, reject) => {
115
- decompress.write(buffer, (error) => {
116
- if (error)
117
- reject(error);
118
- });
119
- decompress.flush();
120
- decompress.once("data", (data) => resolve(data));
121
- decompress.once("error", (error) => reject(error));
122
- decompress.once("end", () => controller.terminate());
123
- }).catch((error) => {
124
- controller.error(error);
125
- });
126
- controller.enqueue(decompressed);
127
- }
128
- });
129
- }
130
- };
131
-
132
- // src/interceptors/fetch/utils/decompression.ts
133
- var PipelineStream = class extends TransformStream {
134
- constructor(transformStreams, ...strategies) {
135
- super({}, ...strategies);
136
- const readable = [super.readable, ...transformStreams].reduce(
137
- (readable2, transform) => readable2.pipeThrough(transform)
138
- );
139
- Object.defineProperty(this, "readable", {
140
- get() {
141
- return readable;
142
- }
143
- });
144
- }
145
- };
146
- function parseContentEncoding(contentEncoding) {
147
- return contentEncoding.toLowerCase().split(",").map((coding) => coding.trim());
148
- }
149
- function createDecompressionStream(contentEncoding) {
150
- if (contentEncoding === "") {
151
- return null;
152
- }
153
- const codings = parseContentEncoding(contentEncoding);
154
- if (codings.length === 0) {
155
- return null;
156
- }
157
- const transformers = codings.reduceRight(
158
- (transformers2, coding) => {
159
- if (coding === "gzip" || coding === "x-gzip") {
160
- return transformers2.concat(new DecompressionStream("gzip"));
161
- } else if (coding === "deflate") {
162
- return transformers2.concat(new DecompressionStream("deflate"));
163
- } else if (coding === "br") {
164
- return transformers2.concat(new BrotliDecompressionStream());
165
- } else {
166
- transformers2.length = 0;
167
- }
168
- return transformers2;
169
- },
170
- []
171
- );
172
- return new PipelineStream(transformers);
173
- }
174
- function decompressResponse(response) {
175
- if (response.body === null) {
176
- return null;
177
- }
178
- const decompressionStream = createDecompressionStream(
179
- response.headers.get("content-encoding") || ""
180
- );
181
- if (!decompressionStream) {
182
- return null;
183
- }
184
- response.body.pipeTo(decompressionStream.writable);
185
- return decompressionStream.readable;
186
- }
187
-
188
- // src/interceptors/fetch/index.ts
189
- var _FetchInterceptor = class extends Interceptor {
190
- constructor() {
191
- super(_FetchInterceptor.symbol);
192
- }
193
- checkEnvironment() {
194
- return hasConfigurableGlobal("fetch");
195
- }
196
- async setup() {
197
- const pureFetch = globalThis.fetch;
198
- invariant(
199
- !pureFetch[IS_PATCHED_MODULE],
200
- 'Failed to patch the "fetch" module: already patched.'
201
- );
202
- globalThis.fetch = async (input, init) => {
203
- const requestId = createRequestId();
204
- const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.href) : input;
205
- const request = new Request(resolvedInput, init);
206
- if (input instanceof Request) {
207
- setRawRequest(request, input);
208
- }
209
- const responsePromise = new DeferredPromise();
210
- const controller = new RequestController(request, {
211
- passthrough: async () => {
212
- this.logger.info("request has not been handled, passthrough...");
213
- const requestCloneForResponseEvent = request.clone();
214
- const { error: responseError, data: originalResponse } = await until(
215
- () => pureFetch(request)
216
- );
217
- if (responseError) {
218
- return responsePromise.reject(responseError);
219
- }
220
- this.logger.info("original fetch performed", originalResponse);
221
- if (this.emitter.listenerCount("response") > 0) {
222
- this.logger.info('emitting the "response" event...');
223
- const responseClone = originalResponse.clone();
224
- await emitAsync(this.emitter, "response", {
225
- response: responseClone,
226
- isMockedResponse: false,
227
- request: requestCloneForResponseEvent,
228
- requestId
229
- });
230
- }
231
- responsePromise.resolve(originalResponse);
232
- },
233
- respondWith: async (rawResponse) => {
234
- if (isResponseError(rawResponse)) {
235
- this.logger.info("request has errored!", { response: rawResponse });
236
- responsePromise.reject(createNetworkError(rawResponse));
237
- return;
238
- }
239
- this.logger.info("received mocked response!", {
240
- rawResponse
241
- });
242
- const decompressedStream = decompressResponse(rawResponse);
243
- const response = decompressedStream === null ? rawResponse : new FetchResponse(decompressedStream, rawResponse);
244
- FetchResponse.setUrl(request.url, response);
245
- if (FetchResponse.isRedirectResponse(response.status)) {
246
- if (request.redirect === "error") {
247
- responsePromise.reject(createNetworkError("unexpected redirect"));
248
- return;
249
- }
250
- if (request.redirect === "follow") {
251
- followFetchRedirect(request, response).then(
252
- (response2) => {
253
- responsePromise.resolve(response2);
254
- },
255
- (reason) => {
256
- responsePromise.reject(reason);
257
- }
258
- );
259
- return;
260
- }
261
- }
262
- if (this.emitter.listenerCount("response") > 0) {
263
- this.logger.info('emitting the "response" event...');
264
- await emitAsync(this.emitter, "response", {
265
- // Clone the mocked response for the "response" event listener.
266
- // This way, the listener can read the response and not lock its body
267
- // for the actual fetch consumer.
268
- response: response.clone(),
269
- isMockedResponse: true,
270
- request,
271
- requestId
272
- });
273
- }
274
- responsePromise.resolve(response);
275
- },
276
- errorWith: (reason) => {
277
- this.logger.info("request has been aborted!", { reason });
278
- responsePromise.reject(reason);
279
- }
280
- });
281
- this.logger.info("[%s] %s", request.method, request.url);
282
- this.logger.info("awaiting for the mocked response...");
283
- this.logger.info(
284
- 'emitting the "request" event for %s listener(s)...',
285
- this.emitter.listenerCount("request")
286
- );
287
- await handleRequest({
288
- request,
289
- requestId,
290
- emitter: this.emitter,
291
- controller
292
- });
293
- return responsePromise;
294
- };
295
- Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
296
- enumerable: true,
297
- configurable: true,
298
- value: true
299
- });
300
- this.subscriptions.push(() => {
301
- Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
302
- value: void 0
303
- });
304
- globalThis.fetch = pureFetch;
305
- this.logger.info(
306
- 'restored native "globalThis.fetch"!',
307
- globalThis.fetch.name
308
- );
309
- });
310
- }
311
- };
312
- var FetchInterceptor = _FetchInterceptor;
313
- FetchInterceptor.symbol = Symbol("fetch");
314
-
315
- export {
316
- FetchInterceptor
317
- };
318
- //# sourceMappingURL=chunk-OFW5L5ET.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/interceptors/fetch/index.ts","../../src/interceptors/fetch/utils/createNetworkError.ts","../../src/interceptors/fetch/utils/followRedirect.ts","../../src/interceptors/fetch/utils/brotli-decompress.ts","../../src/interceptors/fetch/utils/decompression.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { until } from '@open-draft/until'\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'\nimport { createNetworkError } from './utils/createNetworkError'\nimport { followFetchRedirect } from './utils/followRedirect'\nimport { decompressResponse } from './utils/decompression'\nimport { hasConfigurableGlobal } from '../../utils/hasConfigurableGlobal'\nimport { FetchResponse } from '../../utils/fetchUtils'\nimport { setRawRequest } from '../../getRawRequest'\nimport { isResponseError } from '../../utils/responseUtils'\n\nexport class FetchInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('fetch')\n\n constructor() {\n super(FetchInterceptor.symbol)\n }\n\n protected checkEnvironment() {\n return hasConfigurableGlobal('fetch')\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.href)\n : input\n\n const request = new Request(resolvedInput, init)\n\n /**\n * @note Set the raw request only if a Request instance was provided to fetch.\n */\n if (input instanceof Request) {\n setRawRequest(request, input)\n }\n\n const responsePromise = new DeferredPromise<Response>()\n\n const controller = new RequestController(request, {\n passthrough: async () => {\n this.logger.info('request has not been handled, passthrough...')\n\n /**\n * @note Clone the request instance right before performing it.\n * This preserves any modifications made to the intercepted request\n * in the \"request\" listener. This also allows the user to read the\n * request body in the \"response\" listener (otherwise \"unusable\").\n */\n const requestCloneForResponseEvent = request.clone()\n\n // Perform the intercepted request as-is.\n const { error: responseError, data: originalResponse } = await until(\n () => pureFetch(request)\n )\n\n if (responseError) {\n return responsePromise.reject(responseError)\n }\n\n this.logger.info('original fetch performed', originalResponse)\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n const responseClone = originalResponse.clone()\n await emitAsync(this.emitter, 'response', {\n response: responseClone,\n isMockedResponse: false,\n request: requestCloneForResponseEvent,\n requestId,\n })\n }\n\n // Resolve the response promise with the original response\n // since the `fetch()` return this internal promise.\n responsePromise.resolve(originalResponse)\n },\n respondWith: async (rawResponse) => {\n // Handle mocked `Response.error()` (i.e. request errors).\n if (isResponseError(rawResponse)) {\n this.logger.info('request has errored!', { response: rawResponse })\n responsePromise.reject(createNetworkError(rawResponse))\n return\n }\n\n this.logger.info('received mocked response!', {\n rawResponse,\n })\n\n // Decompress the mocked response body, if applicable.\n const decompressedStream = decompressResponse(rawResponse)\n const response =\n decompressedStream === null\n ? rawResponse\n : new FetchResponse(decompressedStream, rawResponse)\n\n FetchResponse.setUrl(request.url, response)\n\n /**\n * Undici's handling of following redirect responses.\n * Treat the \"manual\" redirect mode as a regular mocked response.\n * This way, the client can manually follow the redirect it receives.\n * @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/index.js#L1173\n */\n if (FetchResponse.isRedirectResponse(response.status)) {\n // Reject the request promise if its `redirect` is set to `error`\n // and it receives a mocked redirect response.\n if (request.redirect === 'error') {\n responsePromise.reject(createNetworkError('unexpected redirect'))\n return\n }\n\n if (request.redirect === 'follow') {\n followFetchRedirect(request, response).then(\n (response) => {\n responsePromise.resolve(response)\n },\n (reason) => {\n responsePromise.reject(reason)\n }\n )\n return\n }\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 responsePromise.resolve(response)\n },\n errorWith: (reason) => {\n this.logger.info('request has been aborted!', { reason })\n responsePromise.reject(reason)\n },\n })\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 await handleRequest({\n request,\n requestId,\n emitter: this.emitter,\n controller,\n })\n\n return responsePromise\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","export function createNetworkError(cause?: unknown) {\n return Object.assign(new TypeError('Failed to fetch'), {\n cause,\n })\n}\n","import { createNetworkError } from './createNetworkError'\n\nconst REQUEST_BODY_HEADERS = [\n 'content-encoding',\n 'content-language',\n 'content-location',\n 'content-type',\n 'content-length',\n]\n\nconst kRedirectCount = Symbol('kRedirectCount')\n\n/**\n * @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/index.js#L1210\n */\nexport async function followFetchRedirect(\n request: Request,\n response: Response\n): Promise<Response> {\n if (response.status !== 303 && request.body != null) {\n return Promise.reject(createNetworkError())\n }\n\n const requestUrl = new URL(request.url)\n\n let locationUrl: URL\n try {\n // If the location is a relative URL, use the request URL as the base URL.\n locationUrl = new URL(response.headers.get('location')!, request.url) \n } catch (error) {\n return Promise.reject(createNetworkError(error))\n }\n\n if (\n !(locationUrl.protocol === 'http:' || locationUrl.protocol === 'https:')\n ) {\n return Promise.reject(\n createNetworkError('URL scheme must be a HTTP(S) scheme')\n )\n }\n\n if (Reflect.get(request, kRedirectCount) > 20) {\n return Promise.reject(createNetworkError('redirect count exceeded'))\n }\n\n Object.defineProperty(request, kRedirectCount, {\n value: (Reflect.get(request, kRedirectCount) || 0) + 1,\n })\n\n if (\n request.mode === 'cors' &&\n (locationUrl.username || locationUrl.password) &&\n !sameOrigin(requestUrl, locationUrl)\n ) {\n return Promise.reject(\n createNetworkError('cross origin not allowed for request mode \"cors\"')\n )\n }\n\n const requestInit: RequestInit = {}\n\n if (\n ([301, 302].includes(response.status) && request.method === 'POST') ||\n (response.status === 303 && !['HEAD', 'GET'].includes(request.method))\n ) {\n requestInit.method = 'GET'\n requestInit.body = null\n\n REQUEST_BODY_HEADERS.forEach((headerName) => {\n request.headers.delete(headerName)\n })\n }\n\n if (!sameOrigin(requestUrl, locationUrl)) {\n request.headers.delete('authorization')\n request.headers.delete('proxy-authorization')\n request.headers.delete('cookie')\n request.headers.delete('host')\n }\n\n /**\n * @note Undici \"safely\" extracts the request body.\n * I suspect we cannot dispatch this request again\n * since its body has been read and the stream is locked.\n */\n\n requestInit.headers = request.headers\n const finalResponse = await fetch(new Request(locationUrl, requestInit))\n Object.defineProperty(finalResponse, 'redirected', {\n value: true,\n configurable: true,\n })\n\n return finalResponse\n}\n\n/**\n * @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/util.js#L761\n */\nfunction sameOrigin(left: URL, right: URL): boolean {\n if (left.origin === right.origin && left.origin === 'null') {\n return true\n }\n\n if (\n left.protocol === right.protocol &&\n left.hostname === right.hostname &&\n left.port === right.port\n ) {\n return true\n }\n\n return false\n}\n","import zlib from 'node:zlib'\n\nexport class BrotliDecompressionStream extends TransformStream {\n constructor() {\n const decompress = zlib.createBrotliDecompress({\n flush: zlib.constants.BROTLI_OPERATION_FLUSH,\n finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,\n })\n\n super({\n async transform(chunk, controller) {\n const buffer = Buffer.from(chunk)\n\n const decompressed = await new Promise<Buffer>((resolve, reject) => {\n decompress.write(buffer, (error) => {\n if (error) reject(error)\n })\n\n decompress.flush()\n decompress.once('data', (data) => resolve(data))\n decompress.once('error', (error) => reject(error))\n decompress.once('end', () => controller.terminate())\n }).catch((error) => {\n controller.error(error)\n })\n\n controller.enqueue(decompressed)\n },\n })\n }\n}\n","// Import from an internal alias that resolves to different modules\n// depending on the environment. This way, we can keep the fetch interceptor\n// intact while using different strategies for Brotli decompression.\nimport { BrotliDecompressionStream } from 'internal:brotli-decompress'\n\nclass PipelineStream extends TransformStream {\n constructor(\n transformStreams: Array<TransformStream>,\n ...strategies: Array<QueuingStrategy>\n ) {\n super({}, ...strategies)\n\n const readable = [super.readable as any, ...transformStreams].reduce(\n (readable, transform) => readable.pipeThrough(transform)\n )\n\n Object.defineProperty(this, 'readable', {\n get() {\n return readable\n },\n })\n }\n}\n\nexport function parseContentEncoding(contentEncoding: string): Array<string> {\n return contentEncoding\n .toLowerCase()\n .split(',')\n .map((coding) => coding.trim())\n}\n\nfunction createDecompressionStream(\n contentEncoding: string\n): TransformStream | null {\n if (contentEncoding === '') {\n return null\n }\n\n const codings = parseContentEncoding(contentEncoding)\n\n if (codings.length === 0) {\n return null\n }\n\n const transformers = codings.reduceRight<Array<TransformStream>>(\n (transformers, coding) => {\n if (coding === 'gzip' || coding === 'x-gzip') {\n return transformers.concat(new DecompressionStream('gzip'))\n } else if (coding === 'deflate') {\n return transformers.concat(new DecompressionStream('deflate'))\n } else if (coding === 'br') {\n return transformers.concat(new BrotliDecompressionStream())\n } else {\n transformers.length = 0\n }\n\n return transformers\n },\n []\n )\n\n return new PipelineStream(transformers)\n}\n\nexport function decompressResponse(\n response: Response\n): ReadableStream<any> | null {\n if (response.body === null) {\n return null\n }\n\n const decompressionStream = createDecompressionStream(\n response.headers.get('content-encoding') || ''\n )\n\n if (!decompressionStream) {\n return null\n }\n\n // Use `pipeTo` and return the decompression stream's readable\n // instead of `pipeThrough` because that will lock the original\n // response stream, making it unusable as the input to Response.\n response.body.pipeTo(decompressionStream.writable)\n return decompressionStream.readable\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,aAAa;AACtB,SAAS,uBAAuB;;;ACFzB,SAAS,mBAAmB,OAAiB;AAClD,SAAO,OAAO,OAAO,IAAI,UAAU,iBAAiB,GAAG;AAAA,IACrD;AAAA,EACF,CAAC;AACH;;;ACFA,IAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,iBAAiB,OAAO,gBAAgB;AAK9C,eAAsB,oBACpB,SACA,UACmB;AACnB,MAAI,SAAS,WAAW,OAAO,QAAQ,QAAQ,MAAM;AACnD,WAAO,QAAQ,OAAO,mBAAmB,CAAC;AAAA,EAC5C;AAEA,QAAM,aAAa,IAAI,IAAI,QAAQ,GAAG;AAEtC,MAAI;AACJ,MAAI;AAEF,kBAAc,IAAI,IAAI,SAAS,QAAQ,IAAI,UAAU,GAAI,QAAQ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,cAAc,SAAS;AAAA,MACtC;AAEA,YAAM,cAAc;AAAA,QAClB;AAAA,QACA;AAAA,QACA,SAAS,KAAK;AAAA,QACd;AAAA,MACF,CAAC;AAED,aAAO;AAAA,IACT;AAEA,WAAO,eAAe,WAAW,OAAO,mBAAmB;AAAA,MACzD,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,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;AAnMO,IAAM,mBAAN;AAAM,iBACJ,SAAS,OAAO,OAAO;","names":["readable","transformers","response"]}
@@ -1,25 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/utils/hasConfigurableGlobal.ts
2
- function hasConfigurableGlobal(propertyName) {
3
- const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
4
- if (typeof descriptor === "undefined") {
5
- return false;
6
- }
7
- if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") {
8
- return false;
9
- }
10
- if (typeof descriptor.get === "undefined" && descriptor.value == null) {
11
- return false;
12
- }
13
- if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
14
- console.error(
15
- `[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`
16
- );
17
- return false;
18
- }
19
- return true;
20
- }
21
-
22
-
23
-
24
- exports.hasConfigurableGlobal = hasConfigurableGlobal;
25
- //# sourceMappingURL=chunk-PFGO5BSM.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/hasConfigurableGlobal.ts"],"names":[],"mappings":";AAIO,SAAS,sBAAsB,cAA+B;AACnE,QAAM,aAAa,OAAO,yBAAyB,YAAY,YAAY;AAG3E,MAAI,OAAO,eAAe,aAAa;AACrC,WAAO;AAAA,EACT;AAGA,MACE,OAAO,WAAW,QAAQ,cAC1B,OAAO,WAAW,IAAI,MAAM,aAC5B;AACA,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,WAAW,QAAQ,eAAe,WAAW,SAAS,MAAM;AACrE,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,WAAW,QAAQ,eAAe,CAAC,WAAW,cAAc;AACrE,YAAQ;AAAA,MACN,mDAAmD;AAAA,IACrD;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT","sourcesContent":["/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(propertyName: string): boolean {\n const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName)\n\n // The property is not set at all.\n if (typeof descriptor === 'undefined') {\n return false\n }\n\n // The property is set to a getter that returns undefined.\n if (\n typeof descriptor.get === 'function' &&\n typeof descriptor.get() === 'undefined'\n ) {\n return false\n }\n\n // The property is set to a value equal to undefined.\n if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n return false\n }\n\n if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n console.error(\n `[MSW] Failed to apply interceptor: the global \\`${propertyName}\\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`\n )\n return false\n }\n\n return true\n}\n"]}
@@ -1,189 +0,0 @@
1
- import {
2
- InterceptorError,
3
- RequestController
4
- } from "./chunk-JXGB54LE.mjs";
5
-
6
- // src/utils/emitAsync.ts
7
- async function emitAsync(emitter, eventName, ...data) {
8
- const listeners = emitter.listeners(eventName);
9
- if (listeners.length === 0) {
10
- return;
11
- }
12
- for (const listener of listeners) {
13
- await listener.apply(emitter, data);
14
- }
15
- }
16
-
17
- // src/utils/isObject.ts
18
- function isObject(value, loose = false) {
19
- return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
20
- }
21
-
22
- // src/utils/isPropertyAccessible.ts
23
- function isPropertyAccessible(obj, key) {
24
- try {
25
- obj[key];
26
- return true;
27
- } catch (e) {
28
- return false;
29
- }
30
- }
31
-
32
- // src/utils/responseUtils.ts
33
- function createServerErrorResponse(body) {
34
- return new Response(
35
- JSON.stringify(
36
- body instanceof Error ? {
37
- name: body.name,
38
- message: body.message,
39
- stack: body.stack
40
- } : body
41
- ),
42
- {
43
- status: 500,
44
- statusText: "Unhandled Exception",
45
- headers: {
46
- "Content-Type": "application/json"
47
- }
48
- }
49
- );
50
- }
51
- function isResponseError(response) {
52
- return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
53
- }
54
- function isResponseLike(value) {
55
- return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
56
- }
57
-
58
- // src/utils/handleRequest.ts
59
- import { DeferredPromise } from "@open-draft/deferred-promise";
60
- import { until } from "@open-draft/until";
61
-
62
- // src/utils/isNodeLikeError.ts
63
- function isNodeLikeError(error) {
64
- if (error == null) {
65
- return false;
66
- }
67
- if (!(error instanceof Error)) {
68
- return false;
69
- }
70
- return "code" in error && "errno" in error;
71
- }
72
-
73
- // src/utils/handleRequest.ts
74
- async function handleRequest(options) {
75
- const handleResponse = async (response) => {
76
- if (response instanceof Error) {
77
- await options.controller.errorWith(response);
78
- return true;
79
- }
80
- if (isResponseError(response)) {
81
- await options.controller.respondWith(response);
82
- return true;
83
- }
84
- if (isResponseLike(response)) {
85
- await options.controller.respondWith(response);
86
- return true;
87
- }
88
- if (isObject(response)) {
89
- await options.controller.errorWith(response);
90
- return true;
91
- }
92
- return false;
93
- };
94
- const handleResponseError = async (error) => {
95
- if (error instanceof InterceptorError) {
96
- throw result.error;
97
- }
98
- if (isNodeLikeError(error)) {
99
- await options.controller.errorWith(error);
100
- return true;
101
- }
102
- if (error instanceof Response) {
103
- return await handleResponse(error);
104
- }
105
- return false;
106
- };
107
- const requestAbortPromise = new DeferredPromise();
108
- if (options.request.signal) {
109
- if (options.request.signal.aborted) {
110
- await options.controller.errorWith(options.request.signal.reason);
111
- return;
112
- }
113
- options.request.signal.addEventListener(
114
- "abort",
115
- () => {
116
- requestAbortPromise.reject(options.request.signal.reason);
117
- },
118
- { once: true }
119
- );
120
- }
121
- const result = await until(async () => {
122
- const requestListenersPromise = emitAsync(options.emitter, "request", {
123
- requestId: options.requestId,
124
- request: options.request,
125
- controller: options.controller
126
- });
127
- await Promise.race([
128
- // Short-circuit the request handling promise if the request gets aborted.
129
- requestAbortPromise,
130
- requestListenersPromise,
131
- options.controller.handled
132
- ]);
133
- });
134
- if (requestAbortPromise.state === "rejected") {
135
- await options.controller.errorWith(requestAbortPromise.rejectionReason);
136
- return;
137
- }
138
- if (result.error) {
139
- if (await handleResponseError(result.error)) {
140
- return;
141
- }
142
- if (options.emitter.listenerCount("unhandledException") > 0) {
143
- const unhandledExceptionController = new RequestController(
144
- options.request,
145
- {
146
- /**
147
- * @note Intentionally empty passthrough handle.
148
- * This controller is created within another controller and we only need
149
- * to know if `unhandledException` listeners handled the request.
150
- */
151
- passthrough() {
152
- },
153
- async respondWith(response) {
154
- await handleResponse(response);
155
- },
156
- async errorWith(reason) {
157
- await options.controller.errorWith(reason);
158
- }
159
- }
160
- );
161
- await emitAsync(options.emitter, "unhandledException", {
162
- error: result.error,
163
- request: options.request,
164
- requestId: options.requestId,
165
- controller: unhandledExceptionController
166
- });
167
- if (unhandledExceptionController.readyState !== RequestController.PENDING) {
168
- return;
169
- }
170
- }
171
- await options.controller.respondWith(
172
- createServerErrorResponse(result.error)
173
- );
174
- return;
175
- }
176
- if (options.controller.readyState === RequestController.PENDING) {
177
- return await options.controller.passthrough();
178
- }
179
- return options.controller.handled;
180
- }
181
-
182
- export {
183
- isPropertyAccessible,
184
- emitAsync,
185
- isObject,
186
- isResponseError,
187
- handleRequest
188
- };
189
- //# sourceMappingURL=chunk-R6T7CL5E.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/emitAsync.ts","../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/handleRequest.ts","../../src/utils/isNodeLikeError.ts"],"sourcesContent":["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 listeners = emitter.listeners(eventName)\n\n if (listeners.length === 0) {\n return\n }\n\n for (const listener of listeners) {\n await listener.apply(emitter, data)\n }\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\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":";;;;;;AAOA,eAAsB,UAIpB,SACA,cACG,MACY;AACf,QAAM,YAAY,QAAQ,UAAU,SAAS;AAE7C,MAAI,UAAU,WAAW,GAAG;AAC1B;AAAA,EACF;AAEA,aAAW,YAAY,WAAW;AAChC,UAAM,SAAS,MAAM,SAAS,IAAI;AAAA,EACpC;AACF;;;ACrBO,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,EAC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QAAQ;AAAA,QACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,UAME,cAAc;AAAA,UAAC;AAAA,UACf,MAAM,YAAY,UAAU;AAC1B,kBAAM,eAAe,QAAQ;AAAA,UAC/B;AAAA,UACA,MAAM,UAAU,QAAQ;AAQtB,kBAAM,QAAQ,WAAW,UAAU,MAAM;AAAA,UAC3C;AAAA,QACF;AAAA,MACF;AAEA,YAAM,UAAU,QAAQ,SAAS,sBAAsB;AAAA,QACrD,OAAO,OAAO;AAAA,QACd,SAAS,QAAQ;AAAA,QACjB,WAAW,QAAQ;AAAA,QACnB,YAAY;AAAA,MACd,CAAC;AAID,UACE,6BAA6B,eAAe,kBAAkB,SAC9D;AACA;AAAA,MACF;AAAA,IACF;AAGA,UAAM,QAAQ,WAAW;AAAA,MACvB,0BAA0B,OAAO,KAAK;AAAA,IACxC;AACA;AAAA,EACF;AAGA,MAAI,QAAQ,WAAW,eAAe,kBAAkB,SAAS;AAC/D,WAAO,MAAM,QAAQ,WAAW,YAAY;AAAA,EAC9C;AAEA,SAAO,QAAQ,WAAW;AAC5B;","names":[]}
@@ -1,51 +0,0 @@
1
- import {
2
- Interceptor
3
- } from "./chunk-JXGB54LE.mjs";
4
-
5
- // src/BatchInterceptor.ts
6
- var BatchInterceptor = class extends Interceptor {
7
- constructor(options) {
8
- BatchInterceptor.symbol = Symbol(options.name);
9
- super(BatchInterceptor.symbol);
10
- this.interceptors = options.interceptors;
11
- }
12
- setup() {
13
- const logger = this.logger.extend("setup");
14
- logger.info("applying all %d interceptors...", this.interceptors.length);
15
- for (const interceptor of this.interceptors) {
16
- logger.info('applying "%s" interceptor...', interceptor.constructor.name);
17
- interceptor.apply();
18
- logger.info("adding interceptor dispose subscription");
19
- this.subscriptions.push(() => interceptor.dispose());
20
- }
21
- }
22
- on(event, listener) {
23
- for (const interceptor of this.interceptors) {
24
- interceptor.on(event, listener);
25
- }
26
- return this;
27
- }
28
- once(event, listener) {
29
- for (const interceptor of this.interceptors) {
30
- interceptor.once(event, listener);
31
- }
32
- return this;
33
- }
34
- off(event, listener) {
35
- for (const interceptor of this.interceptors) {
36
- interceptor.off(event, listener);
37
- }
38
- return this;
39
- }
40
- removeAllListeners(event) {
41
- for (const interceptors of this.interceptors) {
42
- interceptors.removeAllListeners(event);
43
- }
44
- return this;
45
- }
46
- };
47
-
48
- export {
49
- BatchInterceptor
50
- };
51
- //# sourceMappingURL=chunk-SQ6RHTJR.mjs.map