@mswjs/interceptors 0.41.9 → 0.42.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 (335) hide show
  1. package/README.md +274 -228
  2. package/lib/browser/{bufferUtils-BiiO6HZv.mjs → buffer-utils-DJj7YzLG.js} +3 -3
  3. package/lib/browser/buffer-utils-DJj7YzLG.js.map +1 -0
  4. package/lib/browser/create-request-id-DlEd4GOA.js +184 -0
  5. package/lib/browser/create-request-id-DlEd4GOA.js.map +1 -0
  6. package/lib/browser/{getRawRequest-B1BqgWG6.mjs → fetch-utils-zxA_SD66.js} +123 -52
  7. package/lib/browser/fetch-utils-zxA_SD66.js.map +1 -0
  8. package/lib/browser/handle-request-CIOa9O-N.js +167 -0
  9. package/lib/browser/handle-request-CIOa9O-N.js.map +1 -0
  10. package/lib/{node/hasConfigurableGlobal-BiTmog1u.mjs → browser/has-configurable-global-Cew-dYqk.js} +5 -7
  11. package/lib/browser/has-configurable-global-Cew-dYqk.js.map +1 -0
  12. package/lib/browser/http-CB9rQ7i_.d.ts +105 -0
  13. package/lib/browser/{index.d.cts → index.d.ts} +33 -38
  14. package/lib/browser/index.js +130 -0
  15. package/lib/browser/index.js.map +1 -0
  16. package/lib/browser/interceptor-dtJZ_9QT.d.ts +50 -0
  17. package/lib/browser/interceptors/WebSocket/index.d.ts +2 -0
  18. package/lib/browser/interceptors/WebSocket/{index.mjs → index.js} +39 -30
  19. package/lib/browser/interceptors/WebSocket/index.js.map +1 -0
  20. package/lib/browser/interceptors/XMLHttpRequest/web.d.ts +11 -0
  21. package/lib/browser/interceptors/XMLHttpRequest/web.js +2 -0
  22. package/lib/browser/interceptors/fetch/web.d.ts +14 -0
  23. package/lib/browser/interceptors/fetch/web.js +2 -0
  24. package/lib/browser/presets/browser.d.ts +11 -0
  25. package/lib/browser/presets/browser.js +12 -0
  26. package/lib/browser/presets/browser.js.map +1 -0
  27. package/lib/browser/{resolveWebSocketUrl-C83-x9iE.mjs → resolve-web-socket-url-CSvNPLGi.js} +3 -3
  28. package/lib/browser/resolve-web-socket-url-CSvNPLGi.js.map +1 -0
  29. package/lib/browser/web-BjVDkiX0.js +248 -0
  30. package/lib/browser/web-BjVDkiX0.js.map +1 -0
  31. package/lib/browser/{XMLHttpRequest-DrZAf3w6.mjs → web-C2oKsT3Q.js} +327 -255
  32. package/lib/browser/web-C2oKsT3Q.js.map +1 -0
  33. package/lib/browser/{interceptors/WebSocket/index.d.cts → websocket-CC0nB0md.d.ts} +41 -33
  34. package/lib/node/batch-interceptor-ByEZVih3.js +119 -0
  35. package/lib/node/batch-interceptor-ByEZVih3.js.map +1 -0
  36. package/lib/node/batch-interceptor-CPjFK2td.d.ts +28 -0
  37. package/lib/node/buffer-utils-BvPY1Tc-.js +187 -0
  38. package/lib/node/buffer-utils-BvPY1Tc-.js.map +1 -0
  39. package/lib/node/fetch-utils-Dw1PsmtX.js +568 -0
  40. package/lib/node/fetch-utils-Dw1PsmtX.js.map +1 -0
  41. package/lib/node/has-configurable-global-CJCh_g1I.js +22 -0
  42. package/lib/node/has-configurable-global-CJCh_g1I.js.map +1 -0
  43. package/lib/node/http-CB9rQ7i_.d.ts +105 -0
  44. package/lib/{browser/interceptors/WebSocket/index.d.mts → node/index.d.ts} +120 -60
  45. package/lib/node/{index.mjs → index.js} +8 -11
  46. package/lib/node/index.js.map +1 -0
  47. package/lib/node/interceptor-dtJZ_9QT.d.ts +50 -0
  48. package/lib/node/interceptors/ClientRequest/index.d.ts +14 -0
  49. package/lib/node/interceptors/ClientRequest/index.js +67 -0
  50. package/lib/node/interceptors/ClientRequest/index.js.map +1 -0
  51. package/lib/node/interceptors/XMLHttpRequest/node.d.ts +12 -0
  52. package/lib/node/interceptors/XMLHttpRequest/node.js +74 -0
  53. package/lib/node/interceptors/XMLHttpRequest/node.js.map +1 -0
  54. package/lib/node/interceptors/fetch/node.d.ts +18 -0
  55. package/lib/node/interceptors/fetch/node.js +83 -0
  56. package/lib/node/interceptors/fetch/node.js.map +1 -0
  57. package/lib/node/interceptors/http/index.d.ts +14 -0
  58. package/lib/node/interceptors/http/index.js +32 -0
  59. package/lib/node/interceptors/http/index.js.map +1 -0
  60. package/lib/node/interceptors/net/index.d.ts +203 -0
  61. package/lib/node/interceptors/net/index.js +2 -0
  62. package/lib/node/llhttp/constants.cjs +485 -0
  63. package/lib/node/llhttp/constants.d.cts +957 -0
  64. package/lib/node/llhttp/llhttp.wasm +0 -0
  65. package/lib/node/net-DtMnyEeg.js +1388 -0
  66. package/lib/node/net-DtMnyEeg.js.map +1 -0
  67. package/lib/node/presets/node.d.ts +12 -0
  68. package/lib/node/presets/node.js +17 -0
  69. package/lib/node/presets/node.js.map +1 -0
  70. package/lib/node/{RemoteHttpInterceptor.d.cts → remote-http-interceptor.d.ts} +12 -11
  71. package/lib/node/{RemoteHttpInterceptor.mjs → remote-http-interceptor.js} +38 -33
  72. package/lib/node/remote-http-interceptor.js.map +1 -0
  73. package/lib/node/source-lP0yyEtA.js +1990 -0
  74. package/lib/node/source-lP0yyEtA.js.map +1 -0
  75. package/package.json +79 -118
  76. package/src/batch-interceptor.test.ts +475 -0
  77. package/src/batch-interceptor.ts +244 -0
  78. package/src/{createRequestId.test.ts → create-request-id.test.ts} +1 -2
  79. package/src/disposable.ts +13 -0
  80. package/src/events/http.ts +92 -0
  81. package/src/events/websocket.ts +47 -0
  82. package/src/glossary.ts +1 -27
  83. package/src/index.ts +18 -10
  84. package/src/interceptor.test.ts +279 -0
  85. package/src/interceptor.ts +139 -0
  86. package/src/interceptors/ClientRequest/index.test.ts +21 -39
  87. package/src/interceptors/ClientRequest/index.ts +64 -179
  88. package/src/interceptors/ClientRequest/utils/{recordRawHeaders.test.ts → record-raw-headers.test.ts} +1 -2
  89. package/src/interceptors/ClientRequest/utils/{recordRawHeaders.ts → record-raw-headers.ts} +29 -2
  90. package/src/interceptors/WebSocket/index.ts +33 -50
  91. package/src/interceptors/WebSocket/utils/{bindEvent.test.ts → bind-event.test.ts} +1 -2
  92. package/src/interceptors/WebSocket/utils/events.test.ts +0 -1
  93. package/src/interceptors/WebSocket/{WebSocketClassTransport.ts → web-socket-class-transport.ts} +3 -3
  94. package/src/interceptors/WebSocket/{WebSocketClientConnection.ts → web-socket-client-connection.ts} +4 -4
  95. package/src/interceptors/WebSocket/{WebSocketOverride.ts → web-socket-override.ts} +6 -7
  96. package/src/interceptors/WebSocket/{WebSocketServerConnection.ts → web-socket-server-connection.ts} +4 -4
  97. package/src/interceptors/WebSocket/{WebSocketTransport.ts → web-socket-transport.ts} +1 -1
  98. package/src/interceptors/XMLHttpRequest/node.ts +104 -0
  99. package/src/interceptors/XMLHttpRequest/polyfills/{ProgressEventPolyfill.ts → progress-event-polyfill.ts} +1 -1
  100. package/src/interceptors/XMLHttpRequest/utils/{concateArrayBuffer.test.ts → concat-array-buffer.test.ts} +1 -2
  101. package/src/interceptors/XMLHttpRequest/utils/{concatArrayBuffer.ts → concat-array-buffer.ts} +1 -1
  102. package/src/interceptors/XMLHttpRequest/utils/{createEvent.test.ts → create-event.test.ts} +3 -4
  103. package/src/interceptors/XMLHttpRequest/utils/{createEvent.ts → create-event.ts} +2 -2
  104. package/src/interceptors/XMLHttpRequest/utils/{createResponse.ts → create-response.ts} +1 -1
  105. package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.test.ts → get-body-byte-length.test.ts} +1 -2
  106. package/src/interceptors/XMLHttpRequest/web.ts +34 -0
  107. package/src/interceptors/XMLHttpRequest/{XMLHttpRequestController.ts → xml-http-request-controller.ts} +363 -204
  108. package/src/interceptors/XMLHttpRequest/{XMLHttpRequestProxy.ts → xml-http-request-proxy.ts} +28 -25
  109. package/src/interceptors/fetch/node.ts +103 -0
  110. package/src/interceptors/fetch/utils/{followRedirect.ts → follow-redirect.ts} +1 -1
  111. package/src/interceptors/fetch/web.ts +217 -0
  112. package/src/interceptors/http/forward-events.test.ts +86 -0
  113. package/src/interceptors/http/forward-events.ts +139 -0
  114. package/src/interceptors/http/http-parser/index.ts +310 -0
  115. package/src/interceptors/http/http-parser/llhttp/constants.cjs +485 -0
  116. package/src/interceptors/http/http-parser/llhttp/constants.d.cts +957 -0
  117. package/src/interceptors/http/http-parser/llhttp/llhttp.wasm +0 -0
  118. package/src/interceptors/http/http-parser.ts +138 -0
  119. package/src/interceptors/http/index.ts +34 -0
  120. package/src/interceptors/http/source.ts +688 -0
  121. package/src/interceptors/net/index.ts +368 -0
  122. package/src/interceptors/net/socket-controller.ts +1476 -0
  123. package/src/interceptors/net/utils/address-info.ts +54 -0
  124. package/src/interceptors/net/utils/connection-options-to-url.ts +56 -0
  125. package/src/interceptors/net/utils/flush-writes.ts +57 -0
  126. package/src/interceptors/net/utils/get-tls-connect-options.ts +23 -0
  127. package/src/interceptors/net/utils/normalize-net-connect-args.test.ts +233 -0
  128. package/src/interceptors/net/utils/normalize-net-connect-args.ts +121 -0
  129. package/src/interceptors/net/utils/normalize-tls-connect-args.ts +50 -0
  130. package/src/presets/browser.ts +4 -4
  131. package/src/presets/node.ts +4 -4
  132. package/src/{RemoteHttpInterceptor.ts → remote-http-interceptor.ts} +55 -42
  133. package/src/request-context.ts +40 -0
  134. package/src/{RequestController.test.ts → request-controller.test.ts} +37 -3
  135. package/src/{RequestController.ts → request-controller.ts} +33 -11
  136. package/src/utils/{bufferUtils.test.ts → buffer-utils.test.ts} +1 -2
  137. package/src/utils/buffer-utils.ts +37 -0
  138. package/src/utils/{cloneObject.test.ts → clone-object.test.ts} +1 -2
  139. package/src/utils/{cloneObject.ts → clone-object.ts} +8 -8
  140. package/src/utils/{createProxy.test.ts → create-proxy.test.ts} +6 -4
  141. package/src/utils/{createProxy.ts → create-proxy.ts} +1 -1
  142. package/src/utils/{fetchUtils.test.ts → fetch-utils.test.ts} +1 -1
  143. package/src/utils/{fetchUtils.ts → fetch-utils.ts} +28 -3
  144. package/src/utils/{findPropertySource.test.ts → find-property-source.test.ts} +1 -2
  145. package/src/utils/{getCleanUrl.test.ts → get-clean-url.test.ts} +1 -2
  146. package/src/utils/{getUrlByRequestOptions.test.ts → get-url-by-request-options.test.ts} +4 -8
  147. package/src/utils/{getUrlByRequestOptions.ts → get-url-by-request-options.ts} +16 -16
  148. package/src/utils/{getValueBySymbol.test.ts → get-value-by-symbol.test.ts} +1 -2
  149. package/src/utils/{handleRequest.ts → handle-request.ts} +61 -41
  150. package/src/utils/{hasConfigurableGlobal.test.ts → has-configurable-global.test.ts} +1 -2
  151. package/src/utils/{hasConfigurableGlobal.ts → has-configurable-global.ts} +2 -4
  152. package/src/utils/{isObject.test.ts → is-object.test.ts} +1 -2
  153. package/src/utils/logger.test.ts +154 -0
  154. package/src/utils/logger.ts +154 -0
  155. package/src/utils/{parseJson.test.ts → parse-json.test.ts} +1 -2
  156. package/src/utils/{patchesRegistry.test.ts → patches-registry.test.ts} +1 -1
  157. package/src/utils/{patchesRegistry.ts → patches-registry.ts} +1 -1
  158. package/src/utils/{responseUtils.ts → response-utils.ts} +25 -2
  159. package/ClientRequest/package.json +0 -11
  160. package/RemoteHttpInterceptor/package.json +0 -11
  161. package/WebSocket/package.json +0 -12
  162. package/XMLHttpRequest/package.json +0 -12
  163. package/fetch/package.json +0 -12
  164. package/lib/browser/Interceptor-Deczogc8.d.cts +0 -65
  165. package/lib/browser/Interceptor-gqKgs-aF.d.mts +0 -65
  166. package/lib/browser/XMLHttpRequest-DrZAf3w6.mjs.map +0 -1
  167. package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs +0 -751
  168. package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs.map +0 -1
  169. package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +0 -1
  170. package/lib/browser/bufferUtils-Uc0eRItL.cjs +0 -38
  171. package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +0 -1
  172. package/lib/browser/createRequestId-DOf8Ktjs.cjs +0 -205
  173. package/lib/browser/createRequestId-DOf8Ktjs.cjs.map +0 -1
  174. package/lib/browser/createRequestId-DYCsFHOi.mjs +0 -170
  175. package/lib/browser/createRequestId-DYCsFHOi.mjs.map +0 -1
  176. package/lib/browser/fetch-5IMPqr9e.mjs +0 -236
  177. package/lib/browser/fetch-5IMPqr9e.mjs.map +0 -1
  178. package/lib/browser/fetch-Bt9rMY9c.cjs +0 -241
  179. package/lib/browser/fetch-Bt9rMY9c.cjs.map +0 -1
  180. package/lib/browser/getRawRequest-B1BqgWG6.mjs.map +0 -1
  181. package/lib/browser/getRawRequest-DdfaiPVH.cjs +0 -376
  182. package/lib/browser/getRawRequest-DdfaiPVH.cjs.map +0 -1
  183. package/lib/browser/glossary-1lhZx6Aw.d.cts +0 -69
  184. package/lib/browser/glossary-nkHWcwvY.d.mts +0 -69
  185. package/lib/browser/handleRequest-CJim54xG.cjs +0 -191
  186. package/lib/browser/handleRequest-CJim54xG.cjs.map +0 -1
  187. package/lib/browser/handleRequest-CPPgsG7s.mjs +0 -180
  188. package/lib/browser/handleRequest-CPPgsG7s.mjs.map +0 -1
  189. package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs +0 -109
  190. package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs.map +0 -1
  191. package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs +0 -126
  192. package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs.map +0 -1
  193. package/lib/browser/index.cjs +0 -70
  194. package/lib/browser/index.cjs.map +0 -1
  195. package/lib/browser/index.d.mts +0 -125
  196. package/lib/browser/index.mjs +0 -56
  197. package/lib/browser/index.mjs.map +0 -1
  198. package/lib/browser/interceptors/WebSocket/index.cjs +0 -618
  199. package/lib/browser/interceptors/WebSocket/index.cjs.map +0 -1
  200. package/lib/browser/interceptors/WebSocket/index.mjs.map +0 -1
  201. package/lib/browser/interceptors/XMLHttpRequest/index.cjs +0 -8
  202. package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +0 -15
  203. package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +0 -15
  204. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +0 -8
  205. package/lib/browser/interceptors/fetch/index.cjs +0 -7
  206. package/lib/browser/interceptors/fetch/index.d.cts +0 -13
  207. package/lib/browser/interceptors/fetch/index.d.mts +0 -13
  208. package/lib/browser/interceptors/fetch/index.mjs +0 -7
  209. package/lib/browser/presets/browser.cjs +0 -18
  210. package/lib/browser/presets/browser.cjs.map +0 -1
  211. package/lib/browser/presets/browser.d.cts +0 -12
  212. package/lib/browser/presets/browser.d.mts +0 -14
  213. package/lib/browser/presets/browser.mjs +0 -18
  214. package/lib/browser/presets/browser.mjs.map +0 -1
  215. package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs +0 -31
  216. package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs.map +0 -1
  217. package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs.map +0 -1
  218. package/lib/node/BatchInterceptor-BGhH8z1z.cjs +0 -49
  219. package/lib/node/BatchInterceptor-BGhH8z1z.cjs.map +0 -1
  220. package/lib/node/BatchInterceptor-C4-RX5rg.mjs +0 -44
  221. package/lib/node/BatchInterceptor-C4-RX5rg.mjs.map +0 -1
  222. package/lib/node/BatchInterceptor-KKF8Q47I.d.cts +0 -26
  223. package/lib/node/BatchInterceptor-rSr-NH_H.d.mts +0 -26
  224. package/lib/node/ClientRequest-2loAH7xM.cjs +0 -1058
  225. package/lib/node/ClientRequest-2loAH7xM.cjs.map +0 -1
  226. package/lib/node/ClientRequest-DGhBX21V.mjs +0 -1049
  227. package/lib/node/ClientRequest-DGhBX21V.mjs.map +0 -1
  228. package/lib/node/Interceptor-BsZ21ue0.d.cts +0 -132
  229. package/lib/node/Interceptor-soD-WpQO.d.mts +0 -132
  230. package/lib/node/RemoteHttpInterceptor.cjs +0 -155
  231. package/lib/node/RemoteHttpInterceptor.cjs.map +0 -1
  232. package/lib/node/RemoteHttpInterceptor.d.mts +0 -39
  233. package/lib/node/RemoteHttpInterceptor.mjs.map +0 -1
  234. package/lib/node/XMLHttpRequest-BtDNZipW.cjs +0 -752
  235. package/lib/node/XMLHttpRequest-BtDNZipW.cjs.map +0 -1
  236. package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs +0 -746
  237. package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs.map +0 -1
  238. package/lib/node/bufferUtils-DxPxwff_.mjs +0 -20
  239. package/lib/node/bufferUtils-DxPxwff_.mjs.map +0 -1
  240. package/lib/node/bufferUtils-S5_-2eN4.cjs +0 -38
  241. package/lib/node/bufferUtils-S5_-2eN4.cjs.map +0 -1
  242. package/lib/node/chunk-CbDLau6x.cjs +0 -34
  243. package/lib/node/fetch-CaC3rGEu.mjs +0 -252
  244. package/lib/node/fetch-CaC3rGEu.mjs.map +0 -1
  245. package/lib/node/fetch-D2ClLNN-.cjs +0 -259
  246. package/lib/node/fetch-D2ClLNN-.cjs.map +0 -1
  247. package/lib/node/fetchUtils-BKJ1XmiO.mjs +0 -480
  248. package/lib/node/fetchUtils-BKJ1XmiO.mjs.map +0 -1
  249. package/lib/node/fetchUtils-umV5xXBy.cjs +0 -546
  250. package/lib/node/fetchUtils-umV5xXBy.cjs.map +0 -1
  251. package/lib/node/getRawRequest-B6znU6il.cjs +0 -36
  252. package/lib/node/getRawRequest-B6znU6il.cjs.map +0 -1
  253. package/lib/node/getRawRequest-C2-1urzA.mjs +0 -24
  254. package/lib/node/getRawRequest-C2-1urzA.mjs.map +0 -1
  255. package/lib/node/handleRequest-C-cKYjIh.cjs +0 -222
  256. package/lib/node/handleRequest-C-cKYjIh.cjs.map +0 -1
  257. package/lib/node/handleRequest-FIQv5pwH.mjs +0 -192
  258. package/lib/node/handleRequest-FIQv5pwH.mjs.map +0 -1
  259. package/lib/node/hasConfigurableGlobal-BiTmog1u.mjs.map +0 -1
  260. package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs +0 -108
  261. package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs.map +0 -1
  262. package/lib/node/index-BJ5E5aW-.d.cts +0 -113
  263. package/lib/node/index-DfoF45ih.d.mts +0 -113
  264. package/lib/node/index.cjs +0 -53
  265. package/lib/node/index.cjs.map +0 -1
  266. package/lib/node/index.d.cts +0 -104
  267. package/lib/node/index.d.mts +0 -104
  268. package/lib/node/index.mjs.map +0 -1
  269. package/lib/node/interceptors/ClientRequest/index.cjs +0 -6
  270. package/lib/node/interceptors/ClientRequest/index.d.cts +0 -2
  271. package/lib/node/interceptors/ClientRequest/index.d.mts +0 -3
  272. package/lib/node/interceptors/ClientRequest/index.mjs +0 -6
  273. package/lib/node/interceptors/XMLHttpRequest/index.cjs +0 -7
  274. package/lib/node/interceptors/XMLHttpRequest/index.d.cts +0 -14
  275. package/lib/node/interceptors/XMLHttpRequest/index.d.mts +0 -14
  276. package/lib/node/interceptors/XMLHttpRequest/index.mjs +0 -7
  277. package/lib/node/interceptors/fetch/index.cjs +0 -6
  278. package/lib/node/interceptors/fetch/index.d.cts +0 -12
  279. package/lib/node/interceptors/fetch/index.d.mts +0 -12
  280. package/lib/node/interceptors/fetch/index.mjs +0 -6
  281. package/lib/node/node-DIKcnzhK.cjs +0 -39
  282. package/lib/node/node-DIKcnzhK.cjs.map +0 -1
  283. package/lib/node/node-lsdNwZEW.mjs +0 -27
  284. package/lib/node/node-lsdNwZEW.mjs.map +0 -1
  285. package/lib/node/presets/node.cjs +0 -23
  286. package/lib/node/presets/node.cjs.map +0 -1
  287. package/lib/node/presets/node.d.cts +0 -13
  288. package/lib/node/presets/node.d.mts +0 -15
  289. package/lib/node/presets/node.mjs +0 -23
  290. package/lib/node/presets/node.mjs.map +0 -1
  291. package/lib/node/utils/node/index.cjs +0 -4
  292. package/lib/node/utils/node/index.d.cts +0 -16
  293. package/lib/node/utils/node/index.d.mts +0 -16
  294. package/lib/node/utils/node/index.mjs +0 -3
  295. package/presets/browser/package.json +0 -5
  296. package/presets/node/package.json +0 -11
  297. package/src/BatchInterceptor.test.ts +0 -255
  298. package/src/BatchInterceptor.ts +0 -96
  299. package/src/Interceptor.test.ts +0 -205
  300. package/src/Interceptor.ts +0 -249
  301. package/src/getRawRequest.ts +0 -21
  302. package/src/interceptors/ClientRequest/MockHttpSocket.ts +0 -724
  303. package/src/interceptors/ClientRequest/agents.ts +0 -110
  304. package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.test.ts +0 -54
  305. package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +0 -45
  306. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +0 -427
  307. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +0 -268
  308. package/src/interceptors/ClientRequest/utils/parserUtils.ts +0 -48
  309. package/src/interceptors/Socket/MockSocket.test.ts +0 -264
  310. package/src/interceptors/Socket/MockSocket.ts +0 -58
  311. package/src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts +0 -26
  312. package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.test.ts +0 -52
  313. package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts +0 -33
  314. package/src/interceptors/XMLHttpRequest/index.ts +0 -40
  315. package/src/interceptors/fetch/index.ts +0 -203
  316. package/src/utils/bufferUtils.ts +0 -22
  317. package/src/utils/canParseUrl.ts +0 -13
  318. package/src/utils/emitAsync.ts +0 -25
  319. package/src/utils/nextTick.ts +0 -11
  320. package/src/utils/node/index.ts +0 -39
  321. /package/src/{createRequestId.ts → create-request-id.ts} +0 -0
  322. /package/src/{InterceptorError.ts → interceptor-error.ts} +0 -0
  323. /package/src/interceptors/WebSocket/utils/{bindEvent.ts → bind-event.ts} +0 -0
  324. /package/src/interceptors/XMLHttpRequest/polyfills/{EventPolyfill.ts → event-polyfill.ts} +0 -0
  325. /package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.ts → get-body-byte-length.ts} +0 -0
  326. /package/src/interceptors/XMLHttpRequest/utils/{isDomParserSupportedType.ts → is-dom-parser-supported-type.ts} +0 -0
  327. /package/src/interceptors/fetch/utils/{createNetworkError.ts → create-network-error.ts} +0 -0
  328. /package/src/utils/{findPropertySource.ts → find-property-source.ts} +0 -0
  329. /package/src/utils/{getCleanUrl.ts → get-clean-url.ts} +0 -0
  330. /package/src/utils/{getValueBySymbol.ts → get-value-by-symbol.ts} +0 -0
  331. /package/src/utils/{isNodeLikeError.ts → is-node-like-error.ts} +0 -0
  332. /package/src/utils/{isObject.ts → is-object.ts} +0 -0
  333. /package/src/utils/{isPropertyAccessible.ts → is-property-accessible.ts} +0 -0
  334. /package/src/utils/{parseJson.ts → parse-json.ts} +0 -0
  335. /package/src/utils/{resolveWebSocketUrl.ts → resolve-web-socket-url.ts} +0 -0
@@ -0,0 +1,368 @@
1
+ import net from 'node:net'
2
+ import tls from 'node:tls'
3
+ import http from 'node:http'
4
+ import { TypedEvent } from 'rettime'
5
+ import {
6
+ type NetworkConnectionOptions,
7
+ normalizeNetConnectArgs,
8
+ } from './utils/normalize-net-connect-args'
9
+ import {
10
+ kPatched,
11
+ TcpSocketController,
12
+ TlsSocketController,
13
+ } from './socket-controller'
14
+ import { normalizeTlsConnectArgs } from './utils/normalize-tls-connect-args'
15
+ import { getTlsConnectOptions } from './utils/get-tls-connect-options'
16
+ import { createLogger } from '../../utils/logger'
17
+ import { patchesRegistry } from '../../utils/patches-registry'
18
+ import { Interceptor } from '#/src/interceptor'
19
+
20
+ declare module 'node:http' {
21
+ interface Agent {
22
+ /**
23
+ * @note An undocumented method backing every agent-driven request
24
+ * (see "#stopReusingUnpatchedSockets").
25
+ */
26
+ addRequest?: (
27
+ request: http.ClientRequest,
28
+ ...args: Array<unknown>
29
+ ) => void
30
+ }
31
+ }
32
+
33
+ interface SocketConnectionEventData {
34
+ socket: net.Socket | tls.TLSSocket
35
+ connectionOptions: NetworkConnectionOptions
36
+ controller: TcpSocketController | TlsSocketController
37
+ }
38
+
39
+ class SocketConnectionEvent<
40
+ DataType extends SocketConnectionEventData = SocketConnectionEventData,
41
+ > extends TypedEvent<DataType, void, 'connection'> {
42
+ public socket: net.Socket | tls.TLSSocket
43
+ public connectionOptions: NetworkConnectionOptions
44
+ public controller: TcpSocketController | TlsSocketController
45
+
46
+ constructor(data: DataType) {
47
+ super(...(['connection', {}] as any))
48
+
49
+ this.socket = data.socket
50
+ this.connectionOptions = data.connectionOptions
51
+ this.controller = data.controller
52
+ }
53
+ }
54
+
55
+ type SocketEventMap = {
56
+ connection: SocketConnectionEvent
57
+ }
58
+
59
+ const logger = createLogger('socket')
60
+
61
+ /**
62
+ * A DNS lookup function for intercepted sockets. It always succeeds,
63
+ * resolving any hostname to the loopback address. This ensures the
64
+ * "lookup"/"connectionAttempt" socket events fire even for non-existent
65
+ * hosts, and no real DNS resolution is performed. Passthrough
66
+ * connections are created with the original options and use the real
67
+ * (or the caller's custom) lookup instead.
68
+ */
69
+ const mockLookup: net.LookupFunction = (hostname, dnsOptions, callback) => {
70
+ const family = dnsOptions.family === 6 ? 6 : 4
71
+ const address = family === 6 ? '::1' : '127.0.0.1'
72
+
73
+ /**
74
+ * @note Call back asynchronously since DNS lookup is always
75
+ * asynchronous in Node.js. Calling back synchronously emits
76
+ * the "lookup"/"connectionAttempt" socket events before the
77
+ * consumer gets a chance to add listeners for them.
78
+ */
79
+ process.nextTick(() => {
80
+ /**
81
+ * @note Honor the Node.js lookup contract: the callback receives
82
+ * an array of addresses only when the "all" option is set
83
+ * (e.g. during the family autoselection). Otherwise, it receives
84
+ * a single address and its family. Node.js rejects an array in
85
+ * the latter case with "ERR_INVALID_IP_ADDRESS".
86
+ */
87
+ if (dnsOptions.all) {
88
+ callback(null, [{ address, family }])
89
+ return
90
+ }
91
+
92
+ callback(null, address, family)
93
+ })
94
+ }
95
+
96
+ /**
97
+ * Interceptor for `net.Socket` connections.
98
+ */
99
+ export class SocketInterceptor extends Interceptor<SocketEventMap> {
100
+ static symbol = Symbol.for('socket-interceptor')
101
+
102
+ protected predicate(): boolean {
103
+ return true
104
+ }
105
+
106
+ protected setup(): void {
107
+ const interceptor = this
108
+
109
+ /**
110
+ * @note A synchronous re-entrancy latch for creating passthrough
111
+ * TLS connections. The original "tls.connect()" constructs a TLS
112
+ * socket and calls "socket.connect()" on it synchronously, and that
113
+ * call must reach Node.js as-is instead of being intercepted again.
114
+ */
115
+ let isCreatingPassthroughConnection = false
116
+
117
+ this.subscriptions.push(
118
+ /**
119
+ * @note Intercept connections at the "net.Socket.prototype.connect"
120
+ * level instead of patching the "net.connect()" module function.
121
+ * ESM consumers snapshot the module bindings at import time
122
+ * ("import * as net from 'node:net'"), so reassigning "net.connect"
123
+ * is invisible to them. Every client connection ends up calling
124
+ * "Socket.prototype.connect" (including the one made by the original
125
+ * "net.connect()"), and prototype mutations are visible regardless
126
+ * of how the module was imported.
127
+ */
128
+ patchesRegistry.applyPatch(
129
+ net.Socket.prototype,
130
+ 'connect',
131
+ (realSocketConnect) => {
132
+ return function connect(this: net.Socket, ...args: [any, any]) {
133
+ const socket = this
134
+
135
+ /**
136
+ * @note Skip the sockets this interceptor already controls
137
+ * (the mock connect call below, re-connects of an intercepted
138
+ * socket, passthrough sockets) and the sockets created while
139
+ * dialing a passthrough TLS connection. Their connects must
140
+ * reach Node.js as-is.
141
+ */
142
+ if (socket[kPatched] || isCreatingPassthroughConnection) {
143
+ return realSocketConnect.apply(socket, args)
144
+ }
145
+
146
+ logger.verbose('socket.connect() %o', args)
147
+
148
+ /**
149
+ * @note The original "net.connect()"/"tls.connect()" normalize
150
+ * the arguments themselves and call this method with the
151
+ * normalized array instead of the individual arguments.
152
+ */
153
+ const connectArgs = (
154
+ Array.isArray(args[0]) ? args[0] : args
155
+ ) as typeof args
156
+
157
+ const [transportConnectionOptions, connectionCallback] =
158
+ normalizeNetConnectArgs(connectArgs)
159
+
160
+ logger.verbose('connection options %o', {
161
+ transportConnectionOptions,
162
+ connectionCallback,
163
+ })
164
+
165
+ let controller: TcpSocketController
166
+ let connectionOptions: NetworkConnectionOptions
167
+
168
+ if (socket instanceof tls.TLSSocket) {
169
+ /**
170
+ * @note TLS sockets arrive here from the original
171
+ * "tls.connect()", which connects the transport of the TLS
172
+ * socket it constructs ("connectionCallback" is its internal
173
+ * "_start" handshake trigger). The original TLS connection
174
+ * options are recovered from the socket instance since the
175
+ * construction has already happened.
176
+ */
177
+ const realTlsConnectionOptions = getTlsConnectOptions(socket)
178
+ const [tlsConnectionOptions] = normalizeTlsConnectArgs([
179
+ {
180
+ ...transportConnectionOptions,
181
+ ...realTlsConnectionOptions,
182
+ } as tls.ConnectionOptions,
183
+ ])
184
+
185
+ connectionOptions = tlsConnectionOptions
186
+ controller = new TlsSocketController(
187
+ socket,
188
+ () => {
189
+ /**
190
+ * @note Create the passthrough connection via the original
191
+ * "tls.connect()" with the original connection options
192
+ * (the real DNS lookup and the caller's certificate
193
+ * validation included). The latch exempts the transport
194
+ * connect of that connection from interception.
195
+ */
196
+ isCreatingPassthroughConnection = true
197
+
198
+ try {
199
+ return tls.connect(
200
+ (realTlsConnectionOptions ??
201
+ tlsConnectionOptions) as tls.ConnectionOptions
202
+ )
203
+ } finally {
204
+ isCreatingPassthroughConnection = false
205
+ }
206
+ },
207
+ tlsConnectionOptions
208
+ )
209
+ } else {
210
+ /**
211
+ * @note Create passthrough connections without the connection
212
+ * callback. The callback is already registered as a "connect"
213
+ * listener on the consumer's socket. Passing it to the
214
+ * passthrough connection would invoke it twice.
215
+ */
216
+ const passthroughArgs = connectArgs.filter((arg) => {
217
+ return typeof arg !== 'function'
218
+ }) as typeof args
219
+
220
+ /**
221
+ * @note Create the passthrough socket with the original
222
+ * options, the same way "net.connect()" does. Connect it via
223
+ * the unpatched method so the passthrough connection is not
224
+ * intercepted again.
225
+ */
226
+ const socketOptions =
227
+ connectArgs[0] !== null &&
228
+ typeof connectArgs[0] === 'object' &&
229
+ !('href' in connectArgs[0])
230
+ ? connectArgs[0]
231
+ : {}
232
+
233
+ connectionOptions = transportConnectionOptions
234
+ controller = new TcpSocketController(socket, () => {
235
+ const passthroughSocket = new net.Socket(socketOptions)
236
+ Reflect.apply(
237
+ realSocketConnect,
238
+ passthroughSocket,
239
+ passthroughArgs
240
+ )
241
+ return passthroughSocket
242
+ })
243
+ }
244
+
245
+ process.nextTick(() => {
246
+ if (socket.destroyed) {
247
+ return
248
+ }
249
+
250
+ if (
251
+ !interceptor.emitter.emit(
252
+ new SocketConnectionEvent({
253
+ socket: controller.serverSocket,
254
+ controller,
255
+ connectionOptions,
256
+ })
257
+ )
258
+ ) {
259
+ logger.verbose(
260
+ 'no "connection" listeners found on the interceptor, passthrough...'
261
+ )
262
+
263
+ controller.passthrough()
264
+ return
265
+ }
266
+
267
+ logger.verbose('emitted "connection" event!')
268
+ })
269
+
270
+ logger.verbose('connecting the socket...')
271
+
272
+ /**
273
+ * @note The requested local address/port are stripped from the
274
+ * actual "socket.connect()" call by the controller to prevent
275
+ * binding the intercepted socket (see the "connect" proxy).
276
+ */
277
+ const mockConnectionOptions = {
278
+ ...transportConnectionOptions,
279
+ }
280
+
281
+ // Patch the lookup option so DNS lookup always succeeds.
282
+ mockConnectionOptions.lookup = mockLookup
283
+
284
+ try {
285
+ /**
286
+ * @note The normalized options are looser than the declared
287
+ * "SocketConnectOpts" (e.g. the port may be a string when a
288
+ * URL is passed). Node.js validates them at runtime.
289
+ * This call goes through the controller's "connect" proxy
290
+ * and lands back in this patch, where the "kPatched" check
291
+ * above delegates it to the unpatched method.
292
+ */
293
+ return socket.connect(
294
+ mockConnectionOptions as net.SocketConnectOpts,
295
+ connectionCallback ?? undefined
296
+ )
297
+ } catch (error) {
298
+ /**
299
+ * @note "socket.connect()" can throw synchronously on invalid
300
+ * input (e.g. a bad port). Destroy the socket so the pending
301
+ * interception tick does not act on it, then let the error
302
+ * propagate to the consumer like in Node.js.
303
+ */
304
+ socket.destroy()
305
+ throw error
306
+ }
307
+ }
308
+ }
309
+ ),
310
+ this.#stopReusingUnpatchedSockets()
311
+ )
312
+
313
+ /**
314
+ * @note "net.connect()"/"net.createConnection()" need no patching of
315
+ * their own: both construct a "net.Socket" and call the patched
316
+ * "Socket.prototype.connect" on it.
317
+ */
318
+ }
319
+
320
+ /**
321
+ * Prevent the `net.Socket` instances created before this interceptor
322
+ * was applied from getting reused by an `Agent`. Purging them from the
323
+ * keep-alive pool forces the agent to establish new (intercepted)
324
+ * connections instead.
325
+ */
326
+ #stopReusingUnpatchedSockets(): () => void {
327
+ /**
328
+ * @note "Agent.prototype.addRequest" is undocumented but stable:
329
+ * its signature changed once (the keep-alive Agent rewrite in
330
+ * Node.js 0.12), and the ecosystem (agent-base, agentkeepalive,
331
+ * APM wrappers) has relied on it ever since. "https.Agent"
332
+ * inherits it, so a single patch covers both.
333
+ */
334
+ if (typeof http.Agent.prototype.addRequest !== 'function') {
335
+ return () => {}
336
+ }
337
+
338
+ return patchesRegistry.applyPatch(
339
+ http.Agent.prototype,
340
+ 'addRequest',
341
+ (realAddRequest) => {
342
+ return function (this: http.Agent, ...args) {
343
+ /**
344
+ * @note Destroy the free sockets created before the interceptor
345
+ * was applied. Destroying flips their "destroyed" state
346
+ * synchronously, so the original "addRequest" below discards
347
+ * them and dials a new (intercepted) connection instead.
348
+ * The "freeSockets" pool only contains idle sockets by
349
+ * definition, so destroying them aborts nothing in-flight.
350
+ */
351
+ for (const sockets of Object.values(this.freeSockets)) {
352
+ if (sockets == null) {
353
+ continue
354
+ }
355
+
356
+ for (const socket of sockets) {
357
+ if (!socket[kPatched]) {
358
+ socket.destroy()
359
+ }
360
+ }
361
+ }
362
+
363
+ return realAddRequest?.apply(this, args)
364
+ }
365
+ }
366
+ )
367
+ }
368
+ }