@mswjs/interceptors 0.41.9 → 0.42.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +274 -228
- package/lib/browser/{bufferUtils-BiiO6HZv.mjs → buffer-utils-DJj7YzLG.js} +3 -3
- package/lib/browser/buffer-utils-DJj7YzLG.js.map +1 -0
- package/lib/browser/create-request-id-Bk5YX1AM.js +720 -0
- package/lib/browser/create-request-id-Bk5YX1AM.js.map +1 -0
- package/lib/browser/{getRawRequest-B1BqgWG6.mjs → fetch-utils-CUOrwEQf.js} +123 -52
- package/lib/browser/fetch-utils-CUOrwEQf.js.map +1 -0
- package/lib/browser/handle-request-CqVvBhcw.js +167 -0
- package/lib/browser/handle-request-CqVvBhcw.js.map +1 -0
- package/lib/{node/hasConfigurableGlobal-BiTmog1u.mjs → browser/has-configurable-global-Cew-dYqk.js} +5 -7
- package/lib/browser/has-configurable-global-Cew-dYqk.js.map +1 -0
- package/lib/browser/http-CB9rQ7i_.d.ts +105 -0
- package/lib/browser/{index.d.cts → index.d.ts} +33 -38
- package/lib/browser/index.js +130 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/browser/interceptors/WebSocket/index.d.ts +2 -0
- package/lib/browser/interceptors/WebSocket/{index.mjs → index.js} +39 -30
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.d.ts +11 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.js +2 -0
- package/lib/browser/interceptors/fetch/web.d.ts +14 -0
- package/lib/browser/interceptors/fetch/web.js +2 -0
- package/lib/browser/presets/browser.d.ts +11 -0
- package/lib/browser/presets/browser.js +12 -0
- package/lib/browser/presets/browser.js.map +1 -0
- package/lib/browser/{resolveWebSocketUrl-C83-x9iE.mjs → resolve-web-socket-url-CSvNPLGi.js} +3 -3
- package/lib/browser/resolve-web-socket-url-CSvNPLGi.js.map +1 -0
- package/lib/browser/{XMLHttpRequest-DrZAf3w6.mjs → web-BcKSQghf.js} +327 -255
- package/lib/browser/web-BcKSQghf.js.map +1 -0
- package/lib/browser/web-CdcYFjgm.js +248 -0
- package/lib/browser/web-CdcYFjgm.js.map +1 -0
- package/lib/browser/{interceptors/WebSocket/index.d.cts → websocket-CC0nB0md.d.ts} +41 -33
- package/lib/node/batch-interceptor-ByEZVih3.js +119 -0
- package/lib/node/batch-interceptor-ByEZVih3.js.map +1 -0
- package/lib/node/batch-interceptor-CPjFK2td.d.ts +28 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js +187 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js.map +1 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js +568 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js.map +1 -0
- package/lib/node/has-configurable-global-CJCh_g1I.js +22 -0
- package/lib/node/has-configurable-global-CJCh_g1I.js.map +1 -0
- package/lib/node/http-CB9rQ7i_.d.ts +105 -0
- package/lib/{browser/interceptors/WebSocket/index.d.mts → node/index.d.ts} +120 -60
- package/lib/node/{index.mjs → index.js} +8 -11
- package/lib/node/index.js.map +1 -0
- package/lib/node/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/node/interceptors/ClientRequest/index.d.ts +14 -0
- package/lib/node/interceptors/ClientRequest/index.js +67 -0
- package/lib/node/interceptors/ClientRequest/index.js.map +1 -0
- package/lib/node/interceptors/XMLHttpRequest/node.d.ts +12 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js +74 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js.map +1 -0
- package/lib/node/interceptors/fetch/node.d.ts +18 -0
- package/lib/node/interceptors/fetch/node.js +83 -0
- package/lib/node/interceptors/fetch/node.js.map +1 -0
- package/lib/node/interceptors/http/index.d.ts +14 -0
- package/lib/node/interceptors/http/index.js +32 -0
- package/lib/node/interceptors/http/index.js.map +1 -0
- package/lib/node/interceptors/net/index.d.ts +203 -0
- package/lib/node/interceptors/net/index.js +2 -0
- package/lib/node/llhttp/constants.cjs +485 -0
- package/lib/node/llhttp/constants.d.cts +957 -0
- package/lib/node/llhttp/llhttp.wasm +0 -0
- package/lib/node/net-DtMnyEeg.js +1388 -0
- package/lib/node/net-DtMnyEeg.js.map +1 -0
- package/lib/node/presets/node.d.ts +12 -0
- package/lib/node/presets/node.js +17 -0
- package/lib/node/presets/node.js.map +1 -0
- package/lib/node/{RemoteHttpInterceptor.d.cts → remote-http-interceptor.d.ts} +12 -11
- package/lib/node/{RemoteHttpInterceptor.mjs → remote-http-interceptor.js} +38 -33
- package/lib/node/remote-http-interceptor.js.map +1 -0
- package/lib/node/source-lP0yyEtA.js +1990 -0
- package/lib/node/source-lP0yyEtA.js.map +1 -0
- package/package.json +79 -118
- package/src/batch-interceptor.test.ts +475 -0
- package/src/batch-interceptor.ts +244 -0
- package/src/{createRequestId.test.ts → create-request-id.test.ts} +1 -2
- package/src/disposable.ts +13 -0
- package/src/events/http.ts +92 -0
- package/src/events/websocket.ts +47 -0
- package/src/glossary.ts +1 -27
- package/src/index.ts +18 -10
- package/src/interceptor.test.ts +279 -0
- package/src/interceptor.ts +139 -0
- package/src/interceptors/ClientRequest/index.test.ts +21 -39
- package/src/interceptors/ClientRequest/index.ts +64 -179
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.test.ts → record-raw-headers.test.ts} +1 -2
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.ts → record-raw-headers.ts} +29 -2
- package/src/interceptors/WebSocket/index.ts +33 -50
- package/src/interceptors/WebSocket/utils/{bindEvent.test.ts → bind-event.test.ts} +1 -2
- package/src/interceptors/WebSocket/utils/events.test.ts +0 -1
- package/src/interceptors/WebSocket/{WebSocketClassTransport.ts → web-socket-class-transport.ts} +3 -3
- package/src/interceptors/WebSocket/{WebSocketClientConnection.ts → web-socket-client-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketOverride.ts → web-socket-override.ts} +6 -7
- package/src/interceptors/WebSocket/{WebSocketServerConnection.ts → web-socket-server-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketTransport.ts → web-socket-transport.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/node.ts +104 -0
- package/src/interceptors/XMLHttpRequest/polyfills/{ProgressEventPolyfill.ts → progress-event-polyfill.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{concateArrayBuffer.test.ts → concat-array-buffer.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/utils/{concatArrayBuffer.ts → concat-array-buffer.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.test.ts → create-event.test.ts} +3 -4
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.ts → create-event.ts} +2 -2
- package/src/interceptors/XMLHttpRequest/utils/{createResponse.ts → create-response.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.test.ts → get-body-byte-length.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/web.ts +34 -0
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestController.ts → xml-http-request-controller.ts} +363 -204
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestProxy.ts → xml-http-request-proxy.ts} +28 -25
- package/src/interceptors/fetch/node.ts +103 -0
- package/src/interceptors/fetch/utils/{followRedirect.ts → follow-redirect.ts} +1 -1
- package/src/interceptors/fetch/web.ts +217 -0
- package/src/interceptors/http/forward-events.test.ts +86 -0
- package/src/interceptors/http/forward-events.ts +139 -0
- package/src/interceptors/http/http-parser/index.ts +310 -0
- package/src/interceptors/http/http-parser/llhttp/constants.cjs +485 -0
- package/src/interceptors/http/http-parser/llhttp/constants.d.cts +957 -0
- package/src/interceptors/http/http-parser/llhttp/llhttp.wasm +0 -0
- package/src/interceptors/http/http-parser.ts +138 -0
- package/src/interceptors/http/index.ts +34 -0
- package/src/interceptors/http/source.ts +688 -0
- package/src/interceptors/net/index.ts +368 -0
- package/src/interceptors/net/socket-controller.ts +1476 -0
- package/src/interceptors/net/utils/address-info.ts +54 -0
- package/src/interceptors/net/utils/connection-options-to-url.ts +56 -0
- package/src/interceptors/net/utils/flush-writes.ts +57 -0
- package/src/interceptors/net/utils/get-tls-connect-options.ts +23 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.test.ts +233 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.ts +121 -0
- package/src/interceptors/net/utils/normalize-tls-connect-args.ts +50 -0
- package/src/presets/browser.ts +4 -4
- package/src/presets/node.ts +4 -4
- package/src/{RemoteHttpInterceptor.ts → remote-http-interceptor.ts} +55 -42
- package/src/request-context.ts +40 -0
- package/src/{RequestController.test.ts → request-controller.test.ts} +37 -3
- package/src/{RequestController.ts → request-controller.ts} +33 -11
- package/src/utils/{bufferUtils.test.ts → buffer-utils.test.ts} +1 -2
- package/src/utils/buffer-utils.ts +37 -0
- package/src/utils/{cloneObject.test.ts → clone-object.test.ts} +1 -2
- package/src/utils/{cloneObject.ts → clone-object.ts} +8 -8
- package/src/utils/{createProxy.test.ts → create-proxy.test.ts} +6 -4
- package/src/utils/{createProxy.ts → create-proxy.ts} +1 -1
- package/src/utils/{fetchUtils.test.ts → fetch-utils.test.ts} +1 -1
- package/src/utils/{fetchUtils.ts → fetch-utils.ts} +28 -3
- package/src/utils/{findPropertySource.test.ts → find-property-source.test.ts} +1 -2
- package/src/utils/{getCleanUrl.test.ts → get-clean-url.test.ts} +1 -2
- package/src/utils/{getUrlByRequestOptions.test.ts → get-url-by-request-options.test.ts} +4 -8
- package/src/utils/{getUrlByRequestOptions.ts → get-url-by-request-options.ts} +16 -16
- package/src/utils/{getValueBySymbol.test.ts → get-value-by-symbol.test.ts} +1 -2
- package/src/utils/{handleRequest.ts → handle-request.ts} +61 -41
- package/src/utils/{hasConfigurableGlobal.test.ts → has-configurable-global.test.ts} +1 -2
- package/src/utils/{hasConfigurableGlobal.ts → has-configurable-global.ts} +2 -4
- package/src/utils/{isObject.test.ts → is-object.test.ts} +1 -2
- package/src/utils/logger.test.ts +154 -0
- package/src/utils/logger.ts +154 -0
- package/src/utils/{parseJson.test.ts → parse-json.test.ts} +1 -2
- package/src/utils/{patchesRegistry.test.ts → patches-registry.test.ts} +1 -1
- package/src/utils/{patchesRegistry.ts → patches-registry.ts} +1 -1
- package/src/utils/{responseUtils.ts → response-utils.ts} +25 -2
- package/ClientRequest/package.json +0 -11
- package/RemoteHttpInterceptor/package.json +0 -11
- package/WebSocket/package.json +0 -12
- package/XMLHttpRequest/package.json +0 -12
- package/fetch/package.json +0 -12
- package/lib/browser/Interceptor-Deczogc8.d.cts +0 -65
- package/lib/browser/Interceptor-gqKgs-aF.d.mts +0 -65
- package/lib/browser/XMLHttpRequest-DrZAf3w6.mjs.map +0 -1
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs +0 -751
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs.map +0 -1
- package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +0 -1
- package/lib/browser/bufferUtils-Uc0eRItL.cjs +0 -38
- package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +0 -1
- package/lib/browser/createRequestId-DOf8Ktjs.cjs +0 -205
- package/lib/browser/createRequestId-DOf8Ktjs.cjs.map +0 -1
- package/lib/browser/createRequestId-DYCsFHOi.mjs +0 -170
- package/lib/browser/createRequestId-DYCsFHOi.mjs.map +0 -1
- package/lib/browser/fetch-5IMPqr9e.mjs +0 -236
- package/lib/browser/fetch-5IMPqr9e.mjs.map +0 -1
- package/lib/browser/fetch-Bt9rMY9c.cjs +0 -241
- package/lib/browser/fetch-Bt9rMY9c.cjs.map +0 -1
- package/lib/browser/getRawRequest-B1BqgWG6.mjs.map +0 -1
- package/lib/browser/getRawRequest-DdfaiPVH.cjs +0 -376
- package/lib/browser/getRawRequest-DdfaiPVH.cjs.map +0 -1
- package/lib/browser/glossary-1lhZx6Aw.d.cts +0 -69
- package/lib/browser/glossary-nkHWcwvY.d.mts +0 -69
- package/lib/browser/handleRequest-CJim54xG.cjs +0 -191
- package/lib/browser/handleRequest-CJim54xG.cjs.map +0 -1
- package/lib/browser/handleRequest-CPPgsG7s.mjs +0 -180
- package/lib/browser/handleRequest-CPPgsG7s.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs +0 -109
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs +0 -126
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs.map +0 -1
- package/lib/browser/index.cjs +0 -70
- package/lib/browser/index.cjs.map +0 -1
- package/lib/browser/index.d.mts +0 -125
- package/lib/browser/index.mjs +0 -56
- package/lib/browser/index.mjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +0 -618
- package/lib/browser/interceptors/WebSocket/index.cjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.mjs.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +0 -8
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +0 -8
- package/lib/browser/interceptors/fetch/index.cjs +0 -7
- package/lib/browser/interceptors/fetch/index.d.cts +0 -13
- package/lib/browser/interceptors/fetch/index.d.mts +0 -13
- package/lib/browser/interceptors/fetch/index.mjs +0 -7
- package/lib/browser/presets/browser.cjs +0 -18
- package/lib/browser/presets/browser.cjs.map +0 -1
- package/lib/browser/presets/browser.d.cts +0 -12
- package/lib/browser/presets/browser.d.mts +0 -14
- package/lib/browser/presets/browser.mjs +0 -18
- package/lib/browser/presets/browser.mjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs +0 -31
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs.map +0 -1
- package/lib/node/BatchInterceptor-BGhH8z1z.cjs +0 -49
- package/lib/node/BatchInterceptor-BGhH8z1z.cjs.map +0 -1
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs +0 -44
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs.map +0 -1
- package/lib/node/BatchInterceptor-KKF8Q47I.d.cts +0 -26
- package/lib/node/BatchInterceptor-rSr-NH_H.d.mts +0 -26
- package/lib/node/ClientRequest-2loAH7xM.cjs +0 -1058
- package/lib/node/ClientRequest-2loAH7xM.cjs.map +0 -1
- package/lib/node/ClientRequest-DGhBX21V.mjs +0 -1049
- package/lib/node/ClientRequest-DGhBX21V.mjs.map +0 -1
- package/lib/node/Interceptor-BsZ21ue0.d.cts +0 -132
- package/lib/node/Interceptor-soD-WpQO.d.mts +0 -132
- package/lib/node/RemoteHttpInterceptor.cjs +0 -155
- package/lib/node/RemoteHttpInterceptor.cjs.map +0 -1
- package/lib/node/RemoteHttpInterceptor.d.mts +0 -39
- package/lib/node/RemoteHttpInterceptor.mjs.map +0 -1
- package/lib/node/XMLHttpRequest-BtDNZipW.cjs +0 -752
- package/lib/node/XMLHttpRequest-BtDNZipW.cjs.map +0 -1
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs +0 -746
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs.map +0 -1
- package/lib/node/bufferUtils-DxPxwff_.mjs +0 -20
- package/lib/node/bufferUtils-DxPxwff_.mjs.map +0 -1
- package/lib/node/bufferUtils-S5_-2eN4.cjs +0 -38
- package/lib/node/bufferUtils-S5_-2eN4.cjs.map +0 -1
- package/lib/node/chunk-CbDLau6x.cjs +0 -34
- package/lib/node/fetch-CaC3rGEu.mjs +0 -252
- package/lib/node/fetch-CaC3rGEu.mjs.map +0 -1
- package/lib/node/fetch-D2ClLNN-.cjs +0 -259
- package/lib/node/fetch-D2ClLNN-.cjs.map +0 -1
- package/lib/node/fetchUtils-BKJ1XmiO.mjs +0 -480
- package/lib/node/fetchUtils-BKJ1XmiO.mjs.map +0 -1
- package/lib/node/fetchUtils-umV5xXBy.cjs +0 -546
- package/lib/node/fetchUtils-umV5xXBy.cjs.map +0 -1
- package/lib/node/getRawRequest-B6znU6il.cjs +0 -36
- package/lib/node/getRawRequest-B6znU6il.cjs.map +0 -1
- package/lib/node/getRawRequest-C2-1urzA.mjs +0 -24
- package/lib/node/getRawRequest-C2-1urzA.mjs.map +0 -1
- package/lib/node/handleRequest-C-cKYjIh.cjs +0 -222
- package/lib/node/handleRequest-C-cKYjIh.cjs.map +0 -1
- package/lib/node/handleRequest-FIQv5pwH.mjs +0 -192
- package/lib/node/handleRequest-FIQv5pwH.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-BiTmog1u.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs +0 -108
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs.map +0 -1
- package/lib/node/index-BJ5E5aW-.d.cts +0 -113
- package/lib/node/index-DfoF45ih.d.mts +0 -113
- package/lib/node/index.cjs +0 -53
- package/lib/node/index.cjs.map +0 -1
- package/lib/node/index.d.cts +0 -104
- package/lib/node/index.d.mts +0 -104
- package/lib/node/index.mjs.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +0 -6
- package/lib/node/interceptors/ClientRequest/index.d.cts +0 -2
- package/lib/node/interceptors/ClientRequest/index.d.mts +0 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +0 -6
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +0 -7
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +0 -7
- package/lib/node/interceptors/fetch/index.cjs +0 -6
- package/lib/node/interceptors/fetch/index.d.cts +0 -12
- package/lib/node/interceptors/fetch/index.d.mts +0 -12
- package/lib/node/interceptors/fetch/index.mjs +0 -6
- package/lib/node/node-DIKcnzhK.cjs +0 -39
- package/lib/node/node-DIKcnzhK.cjs.map +0 -1
- package/lib/node/node-lsdNwZEW.mjs +0 -27
- package/lib/node/node-lsdNwZEW.mjs.map +0 -1
- package/lib/node/presets/node.cjs +0 -23
- package/lib/node/presets/node.cjs.map +0 -1
- package/lib/node/presets/node.d.cts +0 -13
- package/lib/node/presets/node.d.mts +0 -15
- package/lib/node/presets/node.mjs +0 -23
- package/lib/node/presets/node.mjs.map +0 -1
- package/lib/node/utils/node/index.cjs +0 -4
- package/lib/node/utils/node/index.d.cts +0 -16
- package/lib/node/utils/node/index.d.mts +0 -16
- package/lib/node/utils/node/index.mjs +0 -3
- package/presets/browser/package.json +0 -5
- package/presets/node/package.json +0 -11
- package/src/BatchInterceptor.test.ts +0 -255
- package/src/BatchInterceptor.ts +0 -96
- package/src/Interceptor.test.ts +0 -205
- package/src/Interceptor.ts +0 -249
- package/src/getRawRequest.ts +0 -21
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +0 -724
- package/src/interceptors/ClientRequest/agents.ts +0 -110
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.test.ts +0 -54
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +0 -45
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +0 -427
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +0 -268
- package/src/interceptors/ClientRequest/utils/parserUtils.ts +0 -48
- package/src/interceptors/Socket/MockSocket.test.ts +0 -264
- package/src/interceptors/Socket/MockSocket.ts +0 -58
- package/src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts +0 -26
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.test.ts +0 -52
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts +0 -33
- package/src/interceptors/XMLHttpRequest/index.ts +0 -40
- package/src/interceptors/fetch/index.ts +0 -203
- package/src/utils/bufferUtils.ts +0 -22
- package/src/utils/canParseUrl.ts +0 -13
- package/src/utils/emitAsync.ts +0 -25
- package/src/utils/nextTick.ts +0 -11
- package/src/utils/node/index.ts +0 -39
- /package/src/{createRequestId.ts → create-request-id.ts} +0 -0
- /package/src/{InterceptorError.ts → interceptor-error.ts} +0 -0
- /package/src/interceptors/WebSocket/utils/{bindEvent.ts → bind-event.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/polyfills/{EventPolyfill.ts → event-polyfill.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.ts → get-body-byte-length.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{isDomParserSupportedType.ts → is-dom-parser-supported-type.ts} +0 -0
- /package/src/interceptors/fetch/utils/{createNetworkError.ts → create-network-error.ts} +0 -0
- /package/src/utils/{findPropertySource.ts → find-property-source.ts} +0 -0
- /package/src/utils/{getCleanUrl.ts → get-clean-url.ts} +0 -0
- /package/src/utils/{getValueBySymbol.ts → get-value-by-symbol.ts} +0 -0
- /package/src/utils/{isNodeLikeError.ts → is-node-like-error.ts} +0 -0
- /package/src/utils/{isObject.ts → is-object.ts} +0 -0
- /package/src/utils/{isPropertyAccessible.ts → is-property-accessible.ts} +0 -0
- /package/src/utils/{parseJson.ts → parse-json.ts} +0 -0
- /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
|
+
}
|