@depup/msw 2.12.13-depup.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 (590) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +40 -0
  3. package/browser/package.json +31 -0
  4. package/changes.json +46 -0
  5. package/cli/index.js +35 -0
  6. package/cli/init.js +226 -0
  7. package/cli/invariant.js +9 -0
  8. package/cli/package.json +3 -0
  9. package/config/constants.js +13 -0
  10. package/config/package.json +3 -0
  11. package/config/scripts/postinstall.js +36 -0
  12. package/lib/browser/index.d.mts +102 -0
  13. package/lib/browser/index.d.ts +102 -0
  14. package/lib/browser/index.js +2911 -0
  15. package/lib/browser/index.js.map +1 -0
  16. package/lib/browser/index.mjs +2888 -0
  17. package/lib/browser/index.mjs.map +1 -0
  18. package/lib/core/HttpResponse-Be4eT3x6.d.mts +338 -0
  19. package/lib/core/HttpResponse-Dj6ibgFJ.d.ts +338 -0
  20. package/lib/core/HttpResponse.d.mts +6 -0
  21. package/lib/core/HttpResponse.d.ts +6 -0
  22. package/lib/core/HttpResponse.js +176 -0
  23. package/lib/core/HttpResponse.js.map +1 -0
  24. package/lib/core/HttpResponse.mjs +159 -0
  25. package/lib/core/HttpResponse.mjs.map +1 -0
  26. package/lib/core/SetupApi.d.mts +44 -0
  27. package/lib/core/SetupApi.d.ts +44 -0
  28. package/lib/core/SetupApi.js +112 -0
  29. package/lib/core/SetupApi.js.map +1 -0
  30. package/lib/core/SetupApi.mjs +92 -0
  31. package/lib/core/SetupApi.mjs.map +1 -0
  32. package/lib/core/bypass.d.mts +16 -0
  33. package/lib/core/bypass.d.ts +16 -0
  34. package/lib/core/bypass.js +42 -0
  35. package/lib/core/bypass.js.map +1 -0
  36. package/lib/core/bypass.mjs +22 -0
  37. package/lib/core/bypass.mjs.map +1 -0
  38. package/lib/core/delay.d.mts +18 -0
  39. package/lib/core/delay.d.ts +18 -0
  40. package/lib/core/delay.js +77 -0
  41. package/lib/core/delay.js.map +1 -0
  42. package/lib/core/delay.mjs +57 -0
  43. package/lib/core/delay.mjs.map +1 -0
  44. package/lib/core/getResponse.d.mts +18 -0
  45. package/lib/core/getResponse.d.ts +18 -0
  46. package/lib/core/getResponse.js +35 -0
  47. package/lib/core/getResponse.js.map +1 -0
  48. package/lib/core/getResponse.mjs +17 -0
  49. package/lib/core/getResponse.mjs.map +1 -0
  50. package/lib/core/graphql.d.mts +76 -0
  51. package/lib/core/graphql.d.ts +76 -0
  52. package/lib/core/graphql.js +90 -0
  53. package/lib/core/graphql.js.map +1 -0
  54. package/lib/core/graphql.mjs +72 -0
  55. package/lib/core/graphql.mjs.map +1 -0
  56. package/lib/core/handlers/GraphQLHandler.d.mts +6 -0
  57. package/lib/core/handlers/GraphQLHandler.d.ts +6 -0
  58. package/lib/core/handlers/GraphQLHandler.js +231 -0
  59. package/lib/core/handlers/GraphQLHandler.js.map +1 -0
  60. package/lib/core/handlers/GraphQLHandler.mjs +218 -0
  61. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -0
  62. package/lib/core/handlers/HttpHandler.d.mts +75 -0
  63. package/lib/core/handlers/HttpHandler.d.ts +75 -0
  64. package/lib/core/handlers/HttpHandler.js +128 -0
  65. package/lib/core/handlers/HttpHandler.js.map +1 -0
  66. package/lib/core/handlers/HttpHandler.mjs +112 -0
  67. package/lib/core/handlers/HttpHandler.mjs.map +1 -0
  68. package/lib/core/handlers/RequestHandler.d.mts +6 -0
  69. package/lib/core/handlers/RequestHandler.d.ts +6 -0
  70. package/lib/core/handlers/RequestHandler.js +173 -0
  71. package/lib/core/handlers/RequestHandler.js.map +1 -0
  72. package/lib/core/handlers/RequestHandler.mjs +155 -0
  73. package/lib/core/handlers/RequestHandler.mjs.map +1 -0
  74. package/lib/core/handlers/WebSocketHandler.d.mts +42 -0
  75. package/lib/core/handlers/WebSocketHandler.d.ts +42 -0
  76. package/lib/core/handlers/WebSocketHandler.js +147 -0
  77. package/lib/core/handlers/WebSocketHandler.js.map +1 -0
  78. package/lib/core/handlers/WebSocketHandler.mjs +129 -0
  79. package/lib/core/handlers/WebSocketHandler.mjs.map +1 -0
  80. package/lib/core/handlers/common.d.mts +3 -0
  81. package/lib/core/handlers/common.d.ts +3 -0
  82. package/lib/core/handlers/common.js +17 -0
  83. package/lib/core/handlers/common.js.map +1 -0
  84. package/lib/core/handlers/common.mjs +1 -0
  85. package/lib/core/handlers/common.mjs.map +1 -0
  86. package/lib/core/http.d.mts +31 -0
  87. package/lib/core/http.d.ts +31 -0
  88. package/lib/core/http.js +40 -0
  89. package/lib/core/http.js.map +1 -0
  90. package/lib/core/http.mjs +23 -0
  91. package/lib/core/http.mjs.map +1 -0
  92. package/lib/core/index.d.mts +32 -0
  93. package/lib/core/index.d.ts +32 -0
  94. package/lib/core/index.js +64 -0
  95. package/lib/core/index.js.map +1 -0
  96. package/lib/core/index.mjs +52 -0
  97. package/lib/core/index.mjs.map +1 -0
  98. package/lib/core/isCommonAssetRequest.d.mts +20 -0
  99. package/lib/core/isCommonAssetRequest.d.ts +20 -0
  100. package/lib/core/isCommonAssetRequest.js +42 -0
  101. package/lib/core/isCommonAssetRequest.js.map +1 -0
  102. package/lib/core/isCommonAssetRequest.mjs +22 -0
  103. package/lib/core/isCommonAssetRequest.mjs.map +1 -0
  104. package/lib/core/passthrough.d.mts +24 -0
  105. package/lib/core/passthrough.d.ts +24 -0
  106. package/lib/core/passthrough.js +33 -0
  107. package/lib/core/passthrough.js.map +1 -0
  108. package/lib/core/passthrough.mjs +13 -0
  109. package/lib/core/passthrough.mjs.map +1 -0
  110. package/lib/core/sharedOptions.d.mts +64 -0
  111. package/lib/core/sharedOptions.d.ts +64 -0
  112. package/lib/core/sharedOptions.js +17 -0
  113. package/lib/core/sharedOptions.js.map +1 -0
  114. package/lib/core/sharedOptions.mjs +1 -0
  115. package/lib/core/sharedOptions.mjs.map +1 -0
  116. package/lib/core/sse.d.mts +116 -0
  117. package/lib/core/sse.d.ts +116 -0
  118. package/lib/core/sse.js +607 -0
  119. package/lib/core/sse.js.map +1 -0
  120. package/lib/core/sse.mjs +589 -0
  121. package/lib/core/sse.mjs.map +1 -0
  122. package/lib/core/typeUtils.d.mts +12 -0
  123. package/lib/core/typeUtils.d.ts +12 -0
  124. package/lib/core/typeUtils.js +17 -0
  125. package/lib/core/typeUtils.js.map +1 -0
  126. package/lib/core/typeUtils.mjs +1 -0
  127. package/lib/core/typeUtils.mjs.map +1 -0
  128. package/lib/core/utils/HttpResponse/decorators.d.mts +17 -0
  129. package/lib/core/utils/HttpResponse/decorators.d.ts +17 -0
  130. package/lib/core/utils/HttpResponse/decorators.js +77 -0
  131. package/lib/core/utils/HttpResponse/decorators.js.map +1 -0
  132. package/lib/core/utils/HttpResponse/decorators.mjs +47 -0
  133. package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -0
  134. package/lib/core/utils/cookieStore.d.mts +13 -0
  135. package/lib/core/utils/cookieStore.d.ts +13 -0
  136. package/lib/core/utils/cookieStore.js +90 -0
  137. package/lib/core/utils/cookieStore.js.map +1 -0
  138. package/lib/core/utils/cookieStore.mjs +74 -0
  139. package/lib/core/utils/cookieStore.mjs.map +1 -0
  140. package/lib/core/utils/executeHandlers.d.mts +6 -0
  141. package/lib/core/utils/executeHandlers.d.ts +6 -0
  142. package/lib/core/utils/executeHandlers.js +50 -0
  143. package/lib/core/utils/executeHandlers.js.map +1 -0
  144. package/lib/core/utils/executeHandlers.mjs +30 -0
  145. package/lib/core/utils/executeHandlers.mjs.map +1 -0
  146. package/lib/core/utils/handleRequest.d.mts +29 -0
  147. package/lib/core/utils/handleRequest.d.ts +29 -0
  148. package/lib/core/utils/handleRequest.js +76 -0
  149. package/lib/core/utils/handleRequest.js.map +1 -0
  150. package/lib/core/utils/handleRequest.mjs +58 -0
  151. package/lib/core/utils/handleRequest.mjs.map +1 -0
  152. package/lib/core/utils/internal/Disposable.d.mts +7 -0
  153. package/lib/core/utils/internal/Disposable.d.ts +7 -0
  154. package/lib/core/utils/internal/Disposable.js +33 -0
  155. package/lib/core/utils/internal/Disposable.js.map +1 -0
  156. package/lib/core/utils/internal/Disposable.mjs +13 -0
  157. package/lib/core/utils/internal/Disposable.mjs.map +1 -0
  158. package/lib/core/utils/internal/checkGlobals.d.mts +3 -0
  159. package/lib/core/utils/internal/checkGlobals.d.ts +3 -0
  160. package/lib/core/utils/internal/checkGlobals.js +34 -0
  161. package/lib/core/utils/internal/checkGlobals.js.map +1 -0
  162. package/lib/core/utils/internal/checkGlobals.mjs +14 -0
  163. package/lib/core/utils/internal/checkGlobals.mjs.map +1 -0
  164. package/lib/core/utils/internal/devUtils.d.mts +28 -0
  165. package/lib/core/utils/internal/devUtils.d.ts +28 -0
  166. package/lib/core/utils/internal/devUtils.js +48 -0
  167. package/lib/core/utils/internal/devUtils.js.map +1 -0
  168. package/lib/core/utils/internal/devUtils.mjs +28 -0
  169. package/lib/core/utils/internal/devUtils.mjs.map +1 -0
  170. package/lib/core/utils/internal/getCallFrame.d.mts +6 -0
  171. package/lib/core/utils/internal/getCallFrame.d.ts +6 -0
  172. package/lib/core/utils/internal/getCallFrame.js +41 -0
  173. package/lib/core/utils/internal/getCallFrame.js.map +1 -0
  174. package/lib/core/utils/internal/getCallFrame.mjs +21 -0
  175. package/lib/core/utils/internal/getCallFrame.mjs.map +1 -0
  176. package/lib/core/utils/internal/hasRefCounted.d.mts +3 -0
  177. package/lib/core/utils/internal/hasRefCounted.d.ts +3 -0
  178. package/lib/core/utils/internal/hasRefCounted.js +27 -0
  179. package/lib/core/utils/internal/hasRefCounted.js.map +1 -0
  180. package/lib/core/utils/internal/hasRefCounted.mjs +7 -0
  181. package/lib/core/utils/internal/hasRefCounted.mjs.map +1 -0
  182. package/lib/core/utils/internal/isHandlerKind.d.mts +19 -0
  183. package/lib/core/utils/internal/isHandlerKind.d.ts +19 -0
  184. package/lib/core/utils/internal/isHandlerKind.js +29 -0
  185. package/lib/core/utils/internal/isHandlerKind.js.map +1 -0
  186. package/lib/core/utils/internal/isHandlerKind.mjs +9 -0
  187. package/lib/core/utils/internal/isHandlerKind.mjs.map +1 -0
  188. package/lib/core/utils/internal/isIterable.d.mts +20 -0
  189. package/lib/core/utils/internal/isIterable.d.ts +20 -0
  190. package/lib/core/utils/internal/isIterable.js +30 -0
  191. package/lib/core/utils/internal/isIterable.js.map +1 -0
  192. package/lib/core/utils/internal/isIterable.mjs +10 -0
  193. package/lib/core/utils/internal/isIterable.mjs.map +1 -0
  194. package/lib/core/utils/internal/isObject.d.mts +6 -0
  195. package/lib/core/utils/internal/isObject.d.ts +6 -0
  196. package/lib/core/utils/internal/isObject.js +27 -0
  197. package/lib/core/utils/internal/isObject.js.map +1 -0
  198. package/lib/core/utils/internal/isObject.mjs +7 -0
  199. package/lib/core/utils/internal/isObject.mjs.map +1 -0
  200. package/lib/core/utils/internal/isStringEqual.d.mts +6 -0
  201. package/lib/core/utils/internal/isStringEqual.d.ts +6 -0
  202. package/lib/core/utils/internal/isStringEqual.js +27 -0
  203. package/lib/core/utils/internal/isStringEqual.js.map +1 -0
  204. package/lib/core/utils/internal/isStringEqual.mjs +7 -0
  205. package/lib/core/utils/internal/isStringEqual.mjs.map +1 -0
  206. package/lib/core/utils/internal/jsonParse.d.mts +7 -0
  207. package/lib/core/utils/internal/jsonParse.d.ts +7 -0
  208. package/lib/core/utils/internal/jsonParse.js +31 -0
  209. package/lib/core/utils/internal/jsonParse.js.map +1 -0
  210. package/lib/core/utils/internal/jsonParse.mjs +11 -0
  211. package/lib/core/utils/internal/jsonParse.mjs.map +1 -0
  212. package/lib/core/utils/internal/mergeRight.d.mts +7 -0
  213. package/lib/core/utils/internal/mergeRight.d.ts +7 -0
  214. package/lib/core/utils/internal/mergeRight.js +43 -0
  215. package/lib/core/utils/internal/mergeRight.js.map +1 -0
  216. package/lib/core/utils/internal/mergeRight.mjs +23 -0
  217. package/lib/core/utils/internal/mergeRight.mjs.map +1 -0
  218. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +6 -0
  219. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +6 -0
  220. package/lib/core/utils/internal/parseGraphQLRequest.js +146 -0
  221. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -0
  222. package/lib/core/utils/internal/parseGraphQLRequest.mjs +129 -0
  223. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -0
  224. package/lib/core/utils/internal/parseMultipartData.d.mts +14 -0
  225. package/lib/core/utils/internal/parseMultipartData.d.ts +14 -0
  226. package/lib/core/utils/internal/parseMultipartData.js +83 -0
  227. package/lib/core/utils/internal/parseMultipartData.js.map +1 -0
  228. package/lib/core/utils/internal/parseMultipartData.mjs +63 -0
  229. package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -0
  230. package/lib/core/utils/internal/pipeEvents.d.mts +8 -0
  231. package/lib/core/utils/internal/pipeEvents.d.ts +8 -0
  232. package/lib/core/utils/internal/pipeEvents.js +36 -0
  233. package/lib/core/utils/internal/pipeEvents.js.map +1 -0
  234. package/lib/core/utils/internal/pipeEvents.mjs +16 -0
  235. package/lib/core/utils/internal/pipeEvents.mjs.map +1 -0
  236. package/lib/core/utils/internal/requestHandlerUtils.d.mts +12 -0
  237. package/lib/core/utils/internal/requestHandlerUtils.d.ts +12 -0
  238. package/lib/core/utils/internal/requestHandlerUtils.js +37 -0
  239. package/lib/core/utils/internal/requestHandlerUtils.js.map +1 -0
  240. package/lib/core/utils/internal/requestHandlerUtils.mjs +17 -0
  241. package/lib/core/utils/internal/requestHandlerUtils.mjs.map +1 -0
  242. package/lib/core/utils/internal/toReadonlyArray.d.mts +6 -0
  243. package/lib/core/utils/internal/toReadonlyArray.d.ts +6 -0
  244. package/lib/core/utils/internal/toReadonlyArray.js +29 -0
  245. package/lib/core/utils/internal/toReadonlyArray.js.map +1 -0
  246. package/lib/core/utils/internal/toReadonlyArray.mjs +9 -0
  247. package/lib/core/utils/internal/toReadonlyArray.mjs.map +1 -0
  248. package/lib/core/utils/internal/tryCatch.d.mts +3 -0
  249. package/lib/core/utils/internal/tryCatch.d.ts +3 -0
  250. package/lib/core/utils/internal/tryCatch.js +32 -0
  251. package/lib/core/utils/internal/tryCatch.js.map +1 -0
  252. package/lib/core/utils/internal/tryCatch.mjs +12 -0
  253. package/lib/core/utils/internal/tryCatch.mjs.map +1 -0
  254. package/lib/core/utils/logging/getStatusCodeColor.d.mts +11 -0
  255. package/lib/core/utils/logging/getStatusCodeColor.d.ts +11 -0
  256. package/lib/core/utils/logging/getStatusCodeColor.js +40 -0
  257. package/lib/core/utils/logging/getStatusCodeColor.js.map +1 -0
  258. package/lib/core/utils/logging/getStatusCodeColor.mjs +20 -0
  259. package/lib/core/utils/logging/getStatusCodeColor.mjs.map +1 -0
  260. package/lib/core/utils/logging/getTimestamp.d.mts +9 -0
  261. package/lib/core/utils/logging/getTimestamp.d.ts +9 -0
  262. package/lib/core/utils/logging/getTimestamp.js +32 -0
  263. package/lib/core/utils/logging/getTimestamp.js.map +1 -0
  264. package/lib/core/utils/logging/getTimestamp.mjs +12 -0
  265. package/lib/core/utils/logging/getTimestamp.mjs.map +1 -0
  266. package/lib/core/utils/logging/serializeRequest.d.mts +12 -0
  267. package/lib/core/utils/logging/serializeRequest.d.ts +12 -0
  268. package/lib/core/utils/logging/serializeRequest.js +34 -0
  269. package/lib/core/utils/logging/serializeRequest.js.map +1 -0
  270. package/lib/core/utils/logging/serializeRequest.mjs +14 -0
  271. package/lib/core/utils/logging/serializeRequest.mjs.map +1 -0
  272. package/lib/core/utils/logging/serializeResponse.d.mts +9 -0
  273. package/lib/core/utils/logging/serializeResponse.d.ts +9 -0
  274. package/lib/core/utils/logging/serializeResponse.js +48 -0
  275. package/lib/core/utils/logging/serializeResponse.js.map +1 -0
  276. package/lib/core/utils/logging/serializeResponse.mjs +18 -0
  277. package/lib/core/utils/logging/serializeResponse.mjs.map +1 -0
  278. package/lib/core/utils/matching/matchRequestUrl.d.mts +20 -0
  279. package/lib/core/utils/matching/matchRequestUrl.d.ts +20 -0
  280. package/lib/core/utils/matching/matchRequestUrl.js +55 -0
  281. package/lib/core/utils/matching/matchRequestUrl.js.map +1 -0
  282. package/lib/core/utils/matching/matchRequestUrl.mjs +35 -0
  283. package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -0
  284. package/lib/core/utils/matching/normalizePath.d.mts +13 -0
  285. package/lib/core/utils/matching/normalizePath.d.ts +13 -0
  286. package/lib/core/utils/matching/normalizePath.js +33 -0
  287. package/lib/core/utils/matching/normalizePath.js.map +1 -0
  288. package/lib/core/utils/matching/normalizePath.mjs +13 -0
  289. package/lib/core/utils/matching/normalizePath.mjs.map +1 -0
  290. package/lib/core/utils/request/getAllAcceptedMimeTypes.d.mts +14 -0
  291. package/lib/core/utils/request/getAllAcceptedMimeTypes.d.ts +14 -0
  292. package/lib/core/utils/request/getAllAcceptedMimeTypes.js +61 -0
  293. package/lib/core/utils/request/getAllAcceptedMimeTypes.js.map +1 -0
  294. package/lib/core/utils/request/getAllAcceptedMimeTypes.mjs +41 -0
  295. package/lib/core/utils/request/getAllAcceptedMimeTypes.mjs.map +1 -0
  296. package/lib/core/utils/request/getRequestCookies.d.mts +3 -0
  297. package/lib/core/utils/request/getRequestCookies.d.ts +3 -0
  298. package/lib/core/utils/request/getRequestCookies.js +79 -0
  299. package/lib/core/utils/request/getRequestCookies.js.map +1 -0
  300. package/lib/core/utils/request/getRequestCookies.mjs +62 -0
  301. package/lib/core/utils/request/getRequestCookies.mjs.map +1 -0
  302. package/lib/core/utils/request/onUnhandledRequest.d.mts +9 -0
  303. package/lib/core/utils/request/onUnhandledRequest.d.ts +9 -0
  304. package/lib/core/utils/request/onUnhandledRequest.js +76 -0
  305. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -0
  306. package/lib/core/utils/request/onUnhandledRequest.mjs +56 -0
  307. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -0
  308. package/lib/core/utils/request/storeResponseCookies.d.mts +3 -0
  309. package/lib/core/utils/request/storeResponseCookies.d.ts +3 -0
  310. package/lib/core/utils/request/storeResponseCookies.js +32 -0
  311. package/lib/core/utils/request/storeResponseCookies.js.map +1 -0
  312. package/lib/core/utils/request/storeResponseCookies.mjs +12 -0
  313. package/lib/core/utils/request/storeResponseCookies.mjs.map +1 -0
  314. package/lib/core/utils/request/toPublicUrl.d.mts +7 -0
  315. package/lib/core/utils/request/toPublicUrl.d.ts +7 -0
  316. package/lib/core/utils/request/toPublicUrl.js +31 -0
  317. package/lib/core/utils/request/toPublicUrl.js.map +1 -0
  318. package/lib/core/utils/request/toPublicUrl.mjs +11 -0
  319. package/lib/core/utils/request/toPublicUrl.mjs.map +1 -0
  320. package/lib/core/utils/toResponseInit.d.mts +3 -0
  321. package/lib/core/utils/toResponseInit.d.ts +3 -0
  322. package/lib/core/utils/toResponseInit.js +31 -0
  323. package/lib/core/utils/toResponseInit.js.map +1 -0
  324. package/lib/core/utils/toResponseInit.mjs +11 -0
  325. package/lib/core/utils/toResponseInit.mjs.map +1 -0
  326. package/lib/core/utils/url/cleanUrl.d.mts +7 -0
  327. package/lib/core/utils/url/cleanUrl.d.ts +7 -0
  328. package/lib/core/utils/url/cleanUrl.js +31 -0
  329. package/lib/core/utils/url/cleanUrl.js.map +1 -0
  330. package/lib/core/utils/url/cleanUrl.mjs +11 -0
  331. package/lib/core/utils/url/cleanUrl.mjs.map +1 -0
  332. package/lib/core/utils/url/getAbsoluteUrl.d.mts +6 -0
  333. package/lib/core/utils/url/getAbsoluteUrl.d.ts +6 -0
  334. package/lib/core/utils/url/getAbsoluteUrl.js +38 -0
  335. package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -0
  336. package/lib/core/utils/url/getAbsoluteUrl.mjs +18 -0
  337. package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -0
  338. package/lib/core/utils/url/isAbsoluteUrl.d.mts +6 -0
  339. package/lib/core/utils/url/isAbsoluteUrl.d.ts +6 -0
  340. package/lib/core/utils/url/isAbsoluteUrl.js +27 -0
  341. package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -0
  342. package/lib/core/utils/url/isAbsoluteUrl.mjs +7 -0
  343. package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -0
  344. package/lib/core/ws/WebSocketClientManager.d.mts +65 -0
  345. package/lib/core/ws/WebSocketClientManager.d.ts +65 -0
  346. package/lib/core/ws/WebSocketClientManager.js +159 -0
  347. package/lib/core/ws/WebSocketClientManager.js.map +1 -0
  348. package/lib/core/ws/WebSocketClientManager.mjs +139 -0
  349. package/lib/core/ws/WebSocketClientManager.mjs.map +1 -0
  350. package/lib/core/ws/WebSocketClientStore.d.mts +13 -0
  351. package/lib/core/ws/WebSocketClientStore.d.ts +13 -0
  352. package/lib/core/ws/WebSocketClientStore.js +26 -0
  353. package/lib/core/ws/WebSocketClientStore.js.map +1 -0
  354. package/lib/core/ws/WebSocketClientStore.mjs +6 -0
  355. package/lib/core/ws/WebSocketClientStore.mjs.map +1 -0
  356. package/lib/core/ws/WebSocketIndexedDBClientStore.d.mts +14 -0
  357. package/lib/core/ws/WebSocketIndexedDBClientStore.d.ts +14 -0
  358. package/lib/core/ws/WebSocketIndexedDBClientStore.js +130 -0
  359. package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -0
  360. package/lib/core/ws/WebSocketIndexedDBClientStore.mjs +110 -0
  361. package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -0
  362. package/lib/core/ws/WebSocketMemoryClientStore.d.mts +12 -0
  363. package/lib/core/ws/WebSocketMemoryClientStore.d.ts +12 -0
  364. package/lib/core/ws/WebSocketMemoryClientStore.js +41 -0
  365. package/lib/core/ws/WebSocketMemoryClientStore.js.map +1 -0
  366. package/lib/core/ws/WebSocketMemoryClientStore.mjs +21 -0
  367. package/lib/core/ws/WebSocketMemoryClientStore.mjs.map +1 -0
  368. package/lib/core/ws/handleWebSocketEvent.d.mts +20 -0
  369. package/lib/core/ws/handleWebSocketEvent.d.ts +20 -0
  370. package/lib/core/ws/handleWebSocketEvent.js +61 -0
  371. package/lib/core/ws/handleWebSocketEvent.js.map +1 -0
  372. package/lib/core/ws/handleWebSocketEvent.mjs +43 -0
  373. package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -0
  374. package/lib/core/ws/utils/attachWebSocketLogger.d.mts +18 -0
  375. package/lib/core/ws/utils/attachWebSocketLogger.d.ts +18 -0
  376. package/lib/core/ws/utils/attachWebSocketLogger.js +199 -0
  377. package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -0
  378. package/lib/core/ws/utils/attachWebSocketLogger.mjs +179 -0
  379. package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -0
  380. package/lib/core/ws/utils/getMessageLength.d.mts +11 -0
  381. package/lib/core/ws/utils/getMessageLength.d.ts +11 -0
  382. package/lib/core/ws/utils/getMessageLength.js +34 -0
  383. package/lib/core/ws/utils/getMessageLength.js.map +1 -0
  384. package/lib/core/ws/utils/getMessageLength.mjs +14 -0
  385. package/lib/core/ws/utils/getMessageLength.mjs.map +1 -0
  386. package/lib/core/ws/utils/getPublicData.d.mts +5 -0
  387. package/lib/core/ws/utils/getPublicData.d.ts +5 -0
  388. package/lib/core/ws/utils/getPublicData.js +37 -0
  389. package/lib/core/ws/utils/getPublicData.js.map +1 -0
  390. package/lib/core/ws/utils/getPublicData.mjs +17 -0
  391. package/lib/core/ws/utils/getPublicData.mjs.map +1 -0
  392. package/lib/core/ws/utils/truncateMessage.d.mts +3 -0
  393. package/lib/core/ws/utils/truncateMessage.d.ts +3 -0
  394. package/lib/core/ws/utils/truncateMessage.js +31 -0
  395. package/lib/core/ws/utils/truncateMessage.js.map +1 -0
  396. package/lib/core/ws/utils/truncateMessage.mjs +11 -0
  397. package/lib/core/ws/utils/truncateMessage.mjs.map +1 -0
  398. package/lib/core/ws/webSocketInterceptor.d.mts +5 -0
  399. package/lib/core/ws/webSocketInterceptor.d.ts +5 -0
  400. package/lib/core/ws/webSocketInterceptor.js +26 -0
  401. package/lib/core/ws/webSocketInterceptor.js.map +1 -0
  402. package/lib/core/ws/webSocketInterceptor.mjs +6 -0
  403. package/lib/core/ws/webSocketInterceptor.mjs.map +1 -0
  404. package/lib/core/ws.d.mts +75 -0
  405. package/lib/core/ws.d.ts +75 -0
  406. package/lib/core/ws.js +69 -0
  407. package/lib/core/ws.js.map +1 -0
  408. package/lib/core/ws.mjs +52 -0
  409. package/lib/core/ws.mjs.map +1 -0
  410. package/lib/iife/index.js +23752 -0
  411. package/lib/iife/index.js.map +1 -0
  412. package/lib/mockServiceWorker.js +349 -0
  413. package/lib/native/index.d.mts +74 -0
  414. package/lib/native/index.d.ts +74 -0
  415. package/lib/native/index.js +156 -0
  416. package/lib/native/index.js.map +1 -0
  417. package/lib/native/index.mjs +134 -0
  418. package/lib/native/index.mjs.map +1 -0
  419. package/lib/node/index.d.mts +91 -0
  420. package/lib/node/index.d.ts +91 -0
  421. package/lib/node/index.js +209 -0
  422. package/lib/node/index.js.map +1 -0
  423. package/lib/node/index.mjs +184 -0
  424. package/lib/node/index.mjs.map +1 -0
  425. package/lib/shims/cookie.d.mts +7 -0
  426. package/lib/shims/cookie.d.ts +7 -0
  427. package/lib/shims/cookie.js +204 -0
  428. package/lib/shims/cookie.mjs +196 -0
  429. package/lib/shims/statuses.d.mts +6 -0
  430. package/lib/shims/statuses.d.ts +6 -0
  431. package/lib/shims/statuses.js +195 -0
  432. package/lib/shims/statuses.mjs +187 -0
  433. package/native/package.json +30 -0
  434. package/node/package.json +33 -0
  435. package/package.json +381 -0
  436. package/src/browser/global.browser.d.ts +1 -0
  437. package/src/browser/index.ts +3 -0
  438. package/src/browser/setupWorker/glossary.ts +138 -0
  439. package/src/browser/setupWorker/setupWorker.node.test.ts +10 -0
  440. package/src/browser/setupWorker/setupWorker.ts +184 -0
  441. package/src/browser/setupWorker/start/createFallbackRequestListener.ts +71 -0
  442. package/src/browser/setupWorker/start/createRequestListener.ts +138 -0
  443. package/src/browser/setupWorker/start/createResponseListener.ts +57 -0
  444. package/src/browser/setupWorker/start/createStartHandler.ts +137 -0
  445. package/src/browser/setupWorker/start/utils/enableMocking.ts +30 -0
  446. package/src/browser/setupWorker/start/utils/getWorkerByRegistration.ts +25 -0
  447. package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +97 -0
  448. package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +59 -0
  449. package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +44 -0
  450. package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +84 -0
  451. package/src/browser/setupWorker/start/utils/printStartMessage.ts +51 -0
  452. package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +18 -0
  453. package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +26 -0
  454. package/src/browser/setupWorker/stop/utils/printStopMessage.ts +13 -0
  455. package/src/browser/tsconfig.browser.build.json +6 -0
  456. package/src/browser/tsconfig.browser.json +9 -0
  457. package/src/browser/utils/checkWorkerIntegrity.ts +42 -0
  458. package/src/browser/utils/deserializeRequest.ts +15 -0
  459. package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +29 -0
  460. package/src/browser/utils/getAbsoluteWorkerUrl.ts +7 -0
  461. package/src/browser/utils/pruneGetRequestBody.test.ts +53 -0
  462. package/src/browser/utils/pruneGetRequestBody.ts +21 -0
  463. package/src/browser/utils/supports.ts +30 -0
  464. package/src/browser/utils/workerChannel.ts +159 -0
  465. package/src/core/HttpResponse.test.ts +378 -0
  466. package/src/core/HttpResponse.ts +248 -0
  467. package/src/core/SetupApi.ts +127 -0
  468. package/src/core/bypass.test.ts +80 -0
  469. package/src/core/bypass.ts +46 -0
  470. package/src/core/delay.ts +82 -0
  471. package/src/core/getResponse.test.ts +74 -0
  472. package/src/core/getResponse.ts +29 -0
  473. package/src/core/graphql.test.ts +11 -0
  474. package/src/core/graphql.ts +136 -0
  475. package/src/core/handlers/GraphQLHandler.test.ts +873 -0
  476. package/src/core/handlers/GraphQLHandler.ts +437 -0
  477. package/src/core/handlers/HttpHandler.test.ts +253 -0
  478. package/src/core/handlers/HttpHandler.ts +214 -0
  479. package/src/core/handlers/RequestHandler.ts +391 -0
  480. package/src/core/handlers/WebSocketHandler.test.ts +127 -0
  481. package/src/core/handlers/WebSocketHandler.ts +209 -0
  482. package/src/core/handlers/common.ts +1 -0
  483. package/src/core/http.test.ts +15 -0
  484. package/src/core/http.ts +64 -0
  485. package/src/core/index.ts +110 -0
  486. package/src/core/isCommonAssetRequest.ts +45 -0
  487. package/src/core/passthrough.test.ts +11 -0
  488. package/src/core/passthrough.ts +25 -0
  489. package/src/core/sharedOptions.ts +66 -0
  490. package/src/core/sse.ts +916 -0
  491. package/src/core/typeUtils.ts +26 -0
  492. package/src/core/utils/HttpResponse/decorators.ts +76 -0
  493. package/src/core/utils/cookieStore.ts +94 -0
  494. package/src/core/utils/executeHandlers.ts +69 -0
  495. package/src/core/utils/handleRequest.test.ts +573 -0
  496. package/src/core/utils/handleRequest.ts +117 -0
  497. package/src/core/utils/internal/Disposable.ts +12 -0
  498. package/src/core/utils/internal/checkGlobals.ts +17 -0
  499. package/src/core/utils/internal/devUtils.test.ts +21 -0
  500. package/src/core/utils/internal/devUtils.ts +44 -0
  501. package/src/core/utils/internal/getCallFrame.test.ts +154 -0
  502. package/src/core/utils/internal/getCallFrame.ts +35 -0
  503. package/src/core/utils/internal/hasRefCounted.test.ts +16 -0
  504. package/src/core/utils/internal/hasRefCounted.ts +8 -0
  505. package/src/core/utils/internal/isHandlerKind.test.ts +64 -0
  506. package/src/core/utils/internal/isHandlerKind.ts +21 -0
  507. package/src/core/utils/internal/isIterable.test.ts +23 -0
  508. package/src/core/utils/internal/isIterable.ts +32 -0
  509. package/src/core/utils/internal/isObject.test.ts +20 -0
  510. package/src/core/utils/internal/isObject.ts +6 -0
  511. package/src/core/utils/internal/isStringEqual.test.ts +45 -0
  512. package/src/core/utils/internal/isStringEqual.ts +6 -0
  513. package/src/core/utils/internal/jsonParse.test.ts +13 -0
  514. package/src/core/utils/internal/jsonParse.ts +13 -0
  515. package/src/core/utils/internal/mergeRight.test.ts +43 -0
  516. package/src/core/utils/internal/mergeRight.ts +30 -0
  517. package/src/core/utils/internal/parseGraphQLRequest.test.ts +99 -0
  518. package/src/core/utils/internal/parseGraphQLRequest.ts +214 -0
  519. package/src/core/utils/internal/parseMultipartData.test.ts +76 -0
  520. package/src/core/utils/internal/parseMultipartData.ts +106 -0
  521. package/src/core/utils/internal/pipeEvents.test.ts +14 -0
  522. package/src/core/utils/internal/pipeEvents.ts +25 -0
  523. package/src/core/utils/internal/requestHandlerUtils.ts +21 -0
  524. package/src/core/utils/internal/toReadonlyArray.test.ts +30 -0
  525. package/src/core/utils/internal/toReadonlyArray.ts +8 -0
  526. package/src/core/utils/internal/tryCatch.test.ts +29 -0
  527. package/src/core/utils/internal/tryCatch.ts +11 -0
  528. package/src/core/utils/logging/getStatusCodeColor.test.ts +22 -0
  529. package/src/core/utils/logging/getStatusCodeColor.ts +20 -0
  530. package/src/core/utils/logging/getTimestamp.test.ts +32 -0
  531. package/src/core/utils/logging/getTimestamp.ts +17 -0
  532. package/src/core/utils/logging/serializeRequest.test.ts +23 -0
  533. package/src/core/utils/logging/serializeRequest.ts +23 -0
  534. package/src/core/utils/logging/serializeResponse.test.ts +77 -0
  535. package/src/core/utils/logging/serializeResponse.ts +31 -0
  536. package/src/core/utils/matching/matchRequestUrl.test.ts +183 -0
  537. package/src/core/utils/matching/matchRequestUrl.ts +77 -0
  538. package/src/core/utils/matching/normalizePath.node.test.ts +44 -0
  539. package/src/core/utils/matching/normalizePath.test.ts +56 -0
  540. package/src/core/utils/matching/normalizePath.ts +22 -0
  541. package/src/core/utils/request/getAllAcceptedMimeTypes.test.ts +86 -0
  542. package/src/core/utils/request/getAllAcceptedMimeTypes.ts +70 -0
  543. package/src/core/utils/request/getRequestCookies.ts +88 -0
  544. package/src/core/utils/request/onUnhandledRequest.node.test.ts +62 -0
  545. package/src/core/utils/request/onUnhandledRequest.test.ts +228 -0
  546. package/src/core/utils/request/onUnhandledRequest.ts +81 -0
  547. package/src/core/utils/request/storeResponseCookies.ts +17 -0
  548. package/src/core/utils/request/toPublicUrl.node.test.ts +17 -0
  549. package/src/core/utils/request/toPublicUrl.test.ts +16 -0
  550. package/src/core/utils/request/toPublicUrl.ts +16 -0
  551. package/src/core/utils/toResponseInit.ts +7 -0
  552. package/src/core/utils/url/cleanUrl.test.ts +22 -0
  553. package/src/core/utils/url/cleanUrl.ts +16 -0
  554. package/src/core/utils/url/getAbsoluteUrl.node.test.ts +19 -0
  555. package/src/core/utils/url/getAbsoluteUrl.test.ts +47 -0
  556. package/src/core/utils/url/getAbsoluteUrl.ts +25 -0
  557. package/src/core/utils/url/isAbsoluteUrl.test.ts +32 -0
  558. package/src/core/utils/url/isAbsoluteUrl.ts +6 -0
  559. package/src/core/ws/WebSocketClientManager.test.ts +164 -0
  560. package/src/core/ws/WebSocketClientManager.ts +241 -0
  561. package/src/core/ws/WebSocketClientStore.ts +14 -0
  562. package/src/core/ws/WebSocketIndexedDBClientStore.ts +140 -0
  563. package/src/core/ws/WebSocketMemoryClientStore.ts +27 -0
  564. package/src/core/ws/handleWebSocketEvent.ts +64 -0
  565. package/src/core/ws/utils/attachWebSocketLogger.ts +245 -0
  566. package/src/core/ws/utils/getMessageLength.test.ts +16 -0
  567. package/src/core/ws/utils/getMessageLength.ts +20 -0
  568. package/src/core/ws/utils/getPublicData.test.ts +38 -0
  569. package/src/core/ws/utils/getPublicData.ts +18 -0
  570. package/src/core/ws/utils/truncateMessage.test.ts +12 -0
  571. package/src/core/ws/utils/truncateMessage.ts +9 -0
  572. package/src/core/ws/webSocketInterceptor.ts +3 -0
  573. package/src/core/ws.test.ts +23 -0
  574. package/src/core/ws.ts +161 -0
  575. package/src/iife/index.ts +2 -0
  576. package/src/mockServiceWorker.js +349 -0
  577. package/src/native/index.ts +21 -0
  578. package/src/node/SetupServerApi.ts +87 -0
  579. package/src/node/SetupServerCommonApi.ts +169 -0
  580. package/src/node/glossary.ts +74 -0
  581. package/src/node/index.ts +3 -0
  582. package/src/node/setupServer.ts +15 -0
  583. package/src/package.json +3 -0
  584. package/src/shims/cookie.ts +7 -0
  585. package/src/shims/statuses.ts +6 -0
  586. package/src/tsconfig.core.build.json +6 -0
  587. package/src/tsconfig.node.build.json +6 -0
  588. package/src/tsconfig.node.json +8 -0
  589. package/src/tsconfig.src.json +10 -0
  590. package/src/tsconfig.worker.json +13 -0
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { encodeBuffer } from '@mswjs/interceptors'
5
+ import { OperationTypeNode } from 'graphql'
6
+ import {
7
+ ParsedGraphQLRequest,
8
+ parseGraphQLRequest,
9
+ } from './parseGraphQLRequest'
10
+
11
+ test('returns true given a GraphQL-compatible request', async () => {
12
+ const getRequest = new Request(
13
+ new URL(
14
+ 'http://localhost:8080/graphql?query=mutation Login { user { id } }',
15
+ ),
16
+ )
17
+ expect(await parseGraphQLRequest(getRequest)).toEqual<
18
+ ParsedGraphQLRequest<any>
19
+ >({
20
+ operationType: OperationTypeNode.MUTATION,
21
+ operationName: 'Login',
22
+ query: `mutation Login { user { id } }`,
23
+ variables: undefined,
24
+ })
25
+
26
+ const postRequest = new Request(new URL('http://localhost:8080/graphql'), {
27
+ method: 'POST',
28
+ headers: new Headers({ 'Content-Type': 'application/json' }),
29
+ body: encodeBuffer(
30
+ JSON.stringify({
31
+ query: `query GetUser { user { firstName } }`,
32
+ }),
33
+ ),
34
+ })
35
+
36
+ expect(await parseGraphQLRequest(postRequest)).toEqual<
37
+ ParsedGraphQLRequest<any>
38
+ >({
39
+ operationType: OperationTypeNode.QUERY,
40
+ operationName: 'GetUser',
41
+ query: `query GetUser { user { firstName } }`,
42
+ variables: undefined,
43
+ })
44
+ })
45
+
46
+ test('throws an exception given an invalid GraphQL request', async () => {
47
+ const getRequest = new Request(
48
+ new URL('http://localhost:8080/graphql?query=mutation Login() { user { {}'),
49
+ )
50
+
51
+ await expect(parseGraphQLRequest(getRequest)).rejects.toThrowError(
52
+ '[MSW] Failed to intercept a GraphQL request to "GET http://localhost:8080/graphql": cannot parse query. See the error message from the parser below.',
53
+ )
54
+
55
+ const postRequest = new Request(new URL('http://localhost:8080/graphql'), {
56
+ method: 'POST',
57
+ headers: new Headers({ 'Content-Type': 'application/json' }),
58
+ body: encodeBuffer(
59
+ JSON.stringify({
60
+ query: `query GetUser() { user {{}`,
61
+ }),
62
+ ),
63
+ })
64
+
65
+ await expect(parseGraphQLRequest(postRequest)).rejects.toThrowError(
66
+ '[MSW] Failed to intercept a GraphQL request to "POST http://localhost:8080/graphql": cannot parse query. See the error message from the parser below.\n\nSyntax Error: Expected "$", found ")".',
67
+ )
68
+ })
69
+
70
+ test('returns false given a GraphQL-incompatible request', async () => {
71
+ const getRequest = new Request(new URL('http://localhost:8080/graphql'), {
72
+ headers: new Headers({ 'Content-Type': 'application/json' }),
73
+ })
74
+ expect(await parseGraphQLRequest(getRequest)).toBeUndefined()
75
+
76
+ const postRequest = new Request(new URL('http://localhost:8080/graphql'), {
77
+ method: 'POST',
78
+ headers: new Headers({ 'Content-Type': 'application/json' }),
79
+ body: encodeBuffer(
80
+ JSON.stringify({
81
+ queryUser: true,
82
+ }),
83
+ ),
84
+ })
85
+ expect(await parseGraphQLRequest(postRequest)).toBeUndefined()
86
+ })
87
+
88
+ test('does not read the original request body', async () => {
89
+ const request = new Request(new URL('http://localhost/api'), {
90
+ method: 'POST',
91
+ body: JSON.stringify({ payload: 'value' }),
92
+ })
93
+
94
+ await parseGraphQLRequest(request)
95
+
96
+ // Must not read the original request body because GraphQL parsing
97
+ // is an internal operation that must not lock the body stream.
98
+ expect(request.bodyUsed).toBe(false)
99
+ })
@@ -0,0 +1,214 @@
1
+ import type {
2
+ DocumentNode,
3
+ OperationDefinitionNode,
4
+ OperationTypeNode,
5
+ } from 'graphql'
6
+ import type { GraphQLVariables } from '../../handlers/GraphQLHandler'
7
+ import { toPublicUrl } from '../request/toPublicUrl'
8
+ import { devUtils } from './devUtils'
9
+ import { jsonParse } from './jsonParse'
10
+ import { parseMultipartData } from './parseMultipartData'
11
+
12
+ interface GraphQLInput {
13
+ query: string | null
14
+ variables?: GraphQLVariables
15
+ }
16
+
17
+ export interface ParsedGraphQLQuery {
18
+ operationType: OperationTypeNode
19
+ operationName?: string
20
+ }
21
+
22
+ export type ParsedGraphQLRequest<
23
+ VariablesType extends GraphQLVariables = GraphQLVariables,
24
+ > =
25
+ | (ParsedGraphQLQuery & {
26
+ query: string
27
+ variables?: VariablesType
28
+ })
29
+ | undefined
30
+
31
+ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery {
32
+ const operationDef = node.definitions.find((definition) => {
33
+ return definition.kind === 'OperationDefinition'
34
+ }) as OperationDefinitionNode
35
+
36
+ return {
37
+ operationType: operationDef?.operation,
38
+ operationName: operationDef?.name?.value,
39
+ }
40
+ }
41
+
42
+ async function parseQuery(query: string): Promise<ParsedGraphQLQuery | Error> {
43
+ /**
44
+ * @note Use `require` to get the "graphql" module here.
45
+ * It has to be scoped to this function because this module leaks to the
46
+ * root export. It has to be `require` because tools like Jest have trouble
47
+ * handling dynamic imports. It gets replaced with a dynamic import on build time.
48
+ */
49
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
50
+ const { parse } = require('graphql')
51
+
52
+ try {
53
+ const ast = parse(query)
54
+ return parseDocumentNode(ast)
55
+ } catch (error) {
56
+ return error as Error
57
+ }
58
+ }
59
+
60
+ export type GraphQLParsedOperationsMap = Record<string, string[]>
61
+ export type GraphQLMultipartRequestBody = {
62
+ operations: string
63
+ map?: string
64
+ } & {
65
+ [fileName: string]: File
66
+ }
67
+
68
+ function extractMultipartVariables<VariablesType extends GraphQLVariables>(
69
+ variables: VariablesType,
70
+ map: GraphQLParsedOperationsMap,
71
+ files: Record<string, File>,
72
+ ) {
73
+ const operations = { variables }
74
+
75
+ for (const [key, pathArray] of Object.entries(map)) {
76
+ if (!(key in files)) {
77
+ throw new Error(`Given files do not have a key '${key}' .`)
78
+ }
79
+
80
+ for (const dotPath of pathArray) {
81
+ const [lastPath, ...reversedPaths] = dotPath.split('.').reverse()
82
+ const paths = reversedPaths.reverse()
83
+ let target: Record<string, any> = operations
84
+
85
+ for (const path of paths) {
86
+ if (!(path in target)) {
87
+ throw new Error(`Property '${path}' is not in operations.`)
88
+ }
89
+
90
+ target = target[path]
91
+ }
92
+
93
+ target[lastPath] = files[key]
94
+ }
95
+ }
96
+
97
+ return operations.variables
98
+ }
99
+
100
+ async function getGraphQLInput(request: Request): Promise<GraphQLInput | null> {
101
+ switch (request.method) {
102
+ case 'GET': {
103
+ const url = new URL(request.url)
104
+ const query = url.searchParams.get('query')
105
+ const variables = url.searchParams.get('variables') || ''
106
+
107
+ return {
108
+ query,
109
+ variables: jsonParse(variables),
110
+ }
111
+ }
112
+
113
+ case 'POST': {
114
+ // Clone the request so we could read its body without locking
115
+ // the body stream to the downward consumers.
116
+ const requestClone = request.clone()
117
+
118
+ // Handle multipart body GraphQL operations.
119
+ if (
120
+ request.headers.get('content-type')?.includes('multipart/form-data')
121
+ ) {
122
+ const responseJson = parseMultipartData<GraphQLMultipartRequestBody>(
123
+ await requestClone.text(),
124
+ request.headers,
125
+ )
126
+
127
+ if (!responseJson) {
128
+ return null
129
+ }
130
+
131
+ const { operations, map, ...files } = responseJson
132
+ const parsedOperations =
133
+ jsonParse<{ query?: string; variables?: GraphQLVariables }>(
134
+ operations,
135
+ ) || {}
136
+
137
+ if (!parsedOperations.query) {
138
+ return null
139
+ }
140
+
141
+ const parsedMap = jsonParse<GraphQLParsedOperationsMap>(map || '') || {}
142
+ const variables = parsedOperations.variables
143
+ ? extractMultipartVariables(
144
+ parsedOperations.variables,
145
+ parsedMap,
146
+ files,
147
+ )
148
+ : {}
149
+
150
+ return {
151
+ query: parsedOperations.query,
152
+ variables,
153
+ }
154
+ }
155
+
156
+ // Handle plain POST GraphQL operations.
157
+ const requestJson: {
158
+ query: string
159
+ variables?: GraphQLVariables
160
+ operations?: any /** @todo Annotate this */
161
+ } = await requestClone.json().catch(() => null)
162
+
163
+ if (requestJson?.query) {
164
+ const { query, variables } = requestJson
165
+
166
+ return {
167
+ query,
168
+ variables,
169
+ }
170
+ }
171
+ return null
172
+ }
173
+
174
+ default:
175
+ return null
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Determines if a given request can be considered a GraphQL request.
181
+ * Does not parse the query and does not guarantee its validity.
182
+ */
183
+ export async function parseGraphQLRequest(
184
+ request: Request,
185
+ ): Promise<ParsedGraphQLRequest> {
186
+ const input = await getGraphQLInput(request)
187
+
188
+ if (!input || !input.query) {
189
+ return
190
+ }
191
+
192
+ const { query, variables } = input
193
+ const parsedResult = await parseQuery(query)
194
+
195
+ if (parsedResult instanceof Error) {
196
+ const requestPublicUrl = toPublicUrl(request.url)
197
+
198
+ throw new Error(
199
+ devUtils.formatMessage(
200
+ 'Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s',
201
+ request.method,
202
+ requestPublicUrl,
203
+ parsedResult.message,
204
+ ),
205
+ )
206
+ }
207
+
208
+ return {
209
+ query: input.query,
210
+ operationType: parsedResult.operationType,
211
+ operationName: parsedResult.operationName,
212
+ variables,
213
+ }
214
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { parseMultipartData } from './parseMultipartData'
5
+
6
+ test('parses a given valid multipart string', async () => {
7
+ expect.assertions(3)
8
+ await testMultipartDataWithContentType(
9
+ 'multipart/form-data; boundary=WebKitFormBoundaryvZ1cVXWyK0ilQdab',
10
+ )
11
+ })
12
+
13
+ test('parses a given valid multipart string given non-pretty content-type', async () => {
14
+ expect.assertions(3)
15
+ // node-fetch will serialize content-type in this format, which is valid according to HTTP
16
+ // https://github.com/node-fetch/node-fetch/blob/d8fc32d6b29bd43d1ad377e80b3e439fe37f2904/test/main.js#L1438
17
+ await testMultipartDataWithContentType(
18
+ 'multipart/form-data;boundary=WebKitFormBoundaryvZ1cVXWyK0ilQdab',
19
+ )
20
+ })
21
+
22
+ test('parses a given valid multipart string given content-type with extra spaces', async () => {
23
+ expect.assertions(3)
24
+ await testMultipartDataWithContentType(
25
+ 'multipart/form-data; boundary=WebKitFormBoundaryvZ1cVXWyK0ilQdab',
26
+ )
27
+ })
28
+
29
+ async function testMultipartDataWithContentType(
30
+ contentType: string,
31
+ ): Promise<void> {
32
+ const body = `\
33
+ ------WebKitFormBoundaryvZ1cVXWyK0ilQdab\r
34
+ Content-Disposition: form-data; name="file"; filename="file1.txt"\r
35
+ Content-Type: application/octet-stream\r
36
+ \r
37
+ file content\r
38
+ ------WebKitFormBoundaryvZ1cVXWyK0ilQdab\r
39
+ Content-Disposition: form-data; name="text"\r
40
+ \r
41
+ text content\r
42
+ ------WebKitFormBoundaryvZ1cVXWyK0ilQdab\r
43
+ Content-Disposition: form-data; name="text2"\r
44
+ \r
45
+ another text content\r
46
+ ------WebKitFormBoundaryvZ1cVXWyK0ilQdab\r
47
+ Content-Disposition: form-data; name="text2"\r
48
+ \r
49
+ \r
50
+ another text content 2\r
51
+ \r
52
+ ------WebKitFormBoundaryvZ1cVXWyK0ilQdab--`
53
+ const headers = new Headers({
54
+ 'content-type': contentType,
55
+ })
56
+ const parsed = parseMultipartData(body, headers)
57
+
58
+ // Workaround: JSDOM does not have `Blob.text` implementation.
59
+ // see https://github.com/jsdom/jsdom/issues/2555
60
+ expect(parsed).toHaveProperty('file.name', 'file1.txt')
61
+
62
+ expect(parsed).toHaveProperty('text', 'text content')
63
+ expect(parsed).toHaveProperty('text2', [
64
+ 'another text content',
65
+ '\r\nanother text content 2\r\n',
66
+ ])
67
+ }
68
+
69
+ test('returns undefined without an error given an invalid multipart string', () => {
70
+ const headers = new Headers({
71
+ 'content-type': 'multipart/form-data; boundary=dummyBoundary',
72
+ })
73
+ const parse = () => parseMultipartData(`{"invalid": ["multipart"]}`, headers)
74
+ expect(parse).not.toThrow()
75
+ expect(parse()).toBeUndefined()
76
+ })
@@ -0,0 +1,106 @@
1
+ import { stringToHeaders } from 'headers-polyfill'
2
+ import { DefaultRequestMultipartBody } from '../../handlers/RequestHandler'
3
+
4
+ interface ParsedContentHeaders {
5
+ name: string
6
+ filename?: string
7
+ contentType: string
8
+ }
9
+
10
+ interface ContentDispositionDirective {
11
+ [key: string]: string | undefined
12
+ name: string
13
+ filename?: string
14
+ 'form-data': string
15
+ }
16
+
17
+ function parseContentHeaders(headersString: string): ParsedContentHeaders {
18
+ const headers = stringToHeaders(headersString)
19
+ const contentType = headers.get('content-type') || 'text/plain'
20
+ const disposition = headers.get('content-disposition')
21
+
22
+ if (!disposition) {
23
+ throw new Error('"Content-Disposition" header is required.')
24
+ }
25
+
26
+ const directives = disposition.split(';').reduce((acc, chunk) => {
27
+ const [name, ...rest] = chunk.trim().split('=')
28
+ acc[name] = rest.join('=')
29
+ return acc
30
+ }, {} as ContentDispositionDirective)
31
+
32
+ const name = directives.name?.slice(1, -1)
33
+ const filename = directives.filename?.slice(1, -1)
34
+
35
+ return {
36
+ name,
37
+ filename,
38
+ contentType,
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Parses a given string as a multipart/form-data.
44
+ * Does not throw an exception on an invalid multipart string.
45
+ */
46
+ export function parseMultipartData<T extends DefaultRequestMultipartBody>(
47
+ data: string,
48
+ headers?: Headers,
49
+ ): T | undefined {
50
+ const contentType = headers?.get('content-type')
51
+
52
+ if (!contentType) {
53
+ return undefined
54
+ }
55
+
56
+ const [, ...directives] = contentType.split(/; */)
57
+ const boundary = directives
58
+ .filter((d) => d.startsWith('boundary='))
59
+ .map((s) => s.replace(/^boundary=/, ''))[0]
60
+
61
+ if (!boundary) {
62
+ return undefined
63
+ }
64
+
65
+ const boundaryRegExp = new RegExp(
66
+ `--+${boundary.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`,
67
+ )
68
+ const fields = data
69
+ .split(boundaryRegExp)
70
+ .filter((chunk) => chunk.startsWith('\r\n') && chunk.endsWith('\r\n'))
71
+ .map((chunk) => chunk.trimStart().replace(/\r\n$/, ''))
72
+
73
+ if (!fields.length) {
74
+ return undefined
75
+ }
76
+
77
+ const parsedBody: DefaultRequestMultipartBody = {}
78
+
79
+ try {
80
+ for (const field of fields) {
81
+ const [contentHeaders, ...rest] = field.split('\r\n\r\n')
82
+ const contentBody = rest.join('\r\n\r\n')
83
+ const { contentType, filename, name } =
84
+ parseContentHeaders(contentHeaders)
85
+
86
+ const value =
87
+ filename === undefined
88
+ ? contentBody
89
+ : new File([contentBody], filename, { type: contentType })
90
+
91
+ const parsedValue = parsedBody[name]
92
+
93
+ if (parsedValue === undefined) {
94
+ parsedBody[name] = value
95
+ } else if (Array.isArray(parsedValue)) {
96
+ parsedBody[name] = [...parsedValue, value]
97
+ } else {
98
+ parsedBody[name] = [parsedValue, value]
99
+ }
100
+ }
101
+
102
+ return parsedBody as T
103
+ } catch {
104
+ return undefined
105
+ }
106
+ }
@@ -0,0 +1,14 @@
1
+ import { Emitter } from 'strict-event-emitter'
2
+ import { pipeEvents } from './pipeEvents'
3
+
4
+ it('pipes events from the source emitter to the destination emitter', () => {
5
+ const source = new Emitter()
6
+ const destination = new Emitter()
7
+ pipeEvents(source, destination)
8
+
9
+ const callback = vi.fn()
10
+ destination.on('hello', callback)
11
+
12
+ source.emit('hello', 'world', { data: true })
13
+ expect(callback).toHaveBeenNthCalledWith(1, 'world', { data: true })
14
+ })
@@ -0,0 +1,25 @@
1
+ import { Emitter, EventMap } from 'strict-event-emitter'
2
+
3
+ /**
4
+ * Pipes all emitted events from one emitter to another.
5
+ */
6
+ export function pipeEvents<Events extends EventMap>(
7
+ source: Emitter<Events>,
8
+ destination: Emitter<Events>,
9
+ ): void {
10
+ const rawEmit: typeof source.emit & { _isPiped?: boolean } = source.emit
11
+
12
+ if (rawEmit._isPiped) {
13
+ return
14
+ }
15
+
16
+ const sourceEmit: typeof source.emit & { _isPiped?: boolean } =
17
+ function sourceEmit(this: typeof source, event, ...data) {
18
+ destination.emit(event, ...data)
19
+ return rawEmit.call(this, event, ...data)
20
+ }
21
+
22
+ sourceEmit._isPiped = true
23
+
24
+ source.emit = sourceEmit
25
+ }
@@ -0,0 +1,21 @@
1
+ import { RequestHandler } from '../../handlers/RequestHandler'
2
+
3
+ export function use(
4
+ currentHandlers: Array<RequestHandler>,
5
+ ...handlers: Array<RequestHandler>
6
+ ): void {
7
+ currentHandlers.unshift(...handlers)
8
+ }
9
+
10
+ export function restoreHandlers(handlers: Array<RequestHandler>): void {
11
+ handlers.forEach((handler) => {
12
+ handler.isUsed = false
13
+ })
14
+ }
15
+
16
+ export function resetHandlers(
17
+ initialHandlers: Array<RequestHandler>,
18
+ ...nextHandlers: Array<RequestHandler>
19
+ ) {
20
+ return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers]
21
+ }
@@ -0,0 +1,30 @@
1
+ import { toReadonlyArray } from './toReadonlyArray'
2
+
3
+ it('creates a copy of an array', () => {
4
+ expect(toReadonlyArray([1, 2, 3])).toEqual([1, 2, 3])
5
+ })
6
+
7
+ it('does not affect the source array', () => {
8
+ const source = ['a', 'b', 'c']
9
+ toReadonlyArray(source)
10
+
11
+ expect(source.push('d')).toBe(4)
12
+ expect(source).toEqual(['a', 'b', 'c', 'd'])
13
+ })
14
+
15
+ it('forbids modifying the array copy', () => {
16
+ const source = [1, 2, 3]
17
+ const copy = toReadonlyArray(source)
18
+
19
+ expect(() => {
20
+ // @ts-expect-error Intentional runtime misusage.
21
+ copy[2] = 1
22
+ }).toThrow(/Cannot assign to read only property '\d+' of object/)
23
+
24
+ expect(() => {
25
+ // @ts-expect-error Intentional runtime misusage.
26
+ copy.push(4)
27
+ }).toThrow(/Cannot add property \d+, object is not extensible/)
28
+
29
+ expect(source).toEqual([1, 2, 3])
30
+ })
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Creates an immutable copy of the given array.
3
+ */
4
+ export function toReadonlyArray<T>(source: Array<T>): ReadonlyArray<T> {
5
+ const clone = [...source] as Array<T>
6
+ Object.freeze(clone)
7
+ return clone
8
+ }
@@ -0,0 +1,29 @@
1
+ import { tryCatch } from './tryCatch'
2
+
3
+ test('returns the function payload', () => {
4
+ const result = tryCatch(() => 'hello')
5
+ expect(result).toEqual('hello')
6
+ })
7
+
8
+ test('silences exceptions by default', () => {
9
+ const result = tryCatch(() => {
10
+ throw new Error('Exception')
11
+ })
12
+
13
+ expect(result).toBeUndefined()
14
+ })
15
+
16
+ test('executes a custom callback function when an exception occurs', async () => {
17
+ await new Promise<void>((resolve) => {
18
+ tryCatch(
19
+ () => {
20
+ throw new Error('Exception')
21
+ },
22
+ (error) => {
23
+ expect(error).toBeInstanceOf(Error)
24
+ expect(error.message).toEqual('Exception')
25
+ resolve()
26
+ },
27
+ )
28
+ })
29
+ })
@@ -0,0 +1,11 @@
1
+ export function tryCatch<Fn extends (...args: any[]) => any>(
2
+ fn: Fn,
3
+ onException?: (error: Error) => void,
4
+ ): ReturnType<Fn> | undefined {
5
+ try {
6
+ const result = fn()
7
+ return result
8
+ } catch (error) {
9
+ onException?.(error as Error)
10
+ }
11
+ }
@@ -0,0 +1,22 @@
1
+ import { getStatusCodeColor } from './getStatusCodeColor'
2
+
3
+ test('returns a green color for status codes lower than 300', () => {
4
+ expect(getStatusCodeColor(100)).toBe('#69AB32')
5
+ expect(getStatusCodeColor(200)).toBe('#69AB32')
6
+ expect(getStatusCodeColor(204)).toBe('#69AB32')
7
+ })
8
+
9
+ test('returns a yellow color for status codes between 201 and 400', () => {
10
+ expect(getStatusCodeColor(300)).toBe('#F0BB4B')
11
+ expect(getStatusCodeColor(304)).toBe('#F0BB4B')
12
+ })
13
+
14
+ test('returns a red color for status codes higher than 400', () => {
15
+ expect(getStatusCodeColor(400)).toBe('#E95F5D')
16
+ expect(getStatusCodeColor(404)).toBe('#E95F5D')
17
+ expect(getStatusCodeColor(500)).toBe('#E95F5D')
18
+ })
19
+
20
+ test('returns a red color for unknown status code', () => {
21
+ expect(getStatusCodeColor(700)).toBe('#E95F5D')
22
+ })
@@ -0,0 +1,20 @@
1
+ export enum StatusCodeColor {
2
+ Success = '#69AB32',
3
+ Warning = '#F0BB4B',
4
+ Danger = '#E95F5D',
5
+ }
6
+
7
+ /**
8
+ * Returns a HEX color for a given response status code number.
9
+ */
10
+ export function getStatusCodeColor(status: number): StatusCodeColor {
11
+ if (status < 300) {
12
+ return StatusCodeColor.Success
13
+ }
14
+
15
+ if (status < 400) {
16
+ return StatusCodeColor.Warning
17
+ }
18
+
19
+ return StatusCodeColor.Danger
20
+ }