{"id":15478701,"url":"https://github.com/gr2m/node-net-interceptor","last_synced_at":"2025-09-14T19:57:52.615Z","repository":{"id":45257811,"uuid":"442099412","full_name":"gr2m/node-net-interceptor","owner":"gr2m","description":"Intercept outgoing network TCP/TLS connections","archived":false,"fork":false,"pushed_at":"2022-09-23T19:40:37.000Z","size":69,"stargazers_count":14,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-18T06:41:48.293Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gr2m.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-27T08:33:13.000Z","updated_at":"2024-10-31T10:03:16.000Z","dependencies_parsed_at":"2022-08-27T22:51:38.121Z","dependency_job_id":null,"html_url":"https://github.com/gr2m/node-net-interceptor","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/gr2m/node-net-interceptor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-net-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-net-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-net-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-net-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gr2m","download_url":"https://codeload.github.com/gr2m/node-net-interceptor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-net-interceptor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275069073,"owners_count":25400003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-14T02:00:10.474Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T04:06:57.674Z","updated_at":"2025-09-14T19:57:52.593Z","avatar_url":"https://github.com/gr2m.png","language":"JavaScript","readme":"# `@gr2m/net-interceptor`\n\n[![Test](https://github.com/gr2m/node-net-interceptor/actions/workflows/test.yml/badge.svg)](https://github.com/gr2m/node-net-interceptor/actions/workflows/test.yml)\n\n\u003e Intercept outgoing network TCP/TLS connections\n\n## Install\n\n```\nnpm install @gr2m/net-interceptor\n```\n\n## Usage\n\n```js\nimport netInterceptor from \"@gr2m/net-interceptor\";\n\nnetInterceptor.start();\nnetInterceptor.on(\"connect\", (socket, options, bypass) =\u003e {\n  // call bypass() to continue the unintercepted connection\n  if (options.host === \"db.example.com\") return bypass();\n});\n\nnetInterceptor.on(\"connection\", (socket) =\u003e {\n  // do something with the socket\n  socket.write(\"Hello from @gr2m/net-interceptor!\");\n});\n```\n\n## API\n\n`netInterceptor` is a singleton API.\n\n### `netInterceptor.start()`\n\nHooks into the request life cycle and emits `connect` events for each socket that connects to a server as well as `connection` events for all intercepted sockets.\n\n### `netInterceptor.stop()`\n\nStops interceptiong. No `connect` or `connection` events will be emitted.\n\n### `netInterceptor.addListener(event, listener)`\n\n#### `connect` event\n\nThe `listener` callback is called with 3 arguments\n\n- `socket`: the intercepted net or TLS socket\n- `options`: socket options: `{port, /* host, localAddress, localPort, family, allowHalfOpen */}`\n- `bypass`: a function to call to continue the unintercepted connection\n\n#### `connection` event\n\nThe `listener` callback is called with 2 arguments\n\n- `socket`: the response net or TLS socket\n- `options`: socket options: `{port, /* host, localAddress, localPort, family, allowHalfOpen */}`\n\n### `netInterceptor.removeListener(event, listener)`\n\nRemove an event listener.\n\n### `netInterceptor.removeAllListeners(event)`\n\nRemoves all event listeners for the given event. Or when called without the `event` argument, remove all listeners for all events.\n\n### `kRemote`\n\n```js\nimport { kRemote } from \"@gr2m/net-interceptor\";\nrequestSocket[kRemote]; // response socket\n```\n\n`kRemote` is a symbol that can be used to access the response socket from the request socket when handling intercepted requests.\n\n## See also\n\n- [`@gr2m/http-interceptor`](https://github.com/gr2m/node-http-interceptor) - Intercept and mock outgoing http/https requests\n- [`@gr2m/http-recorder`](https://github.com/gr2m/node-http-recorder) - Library agnostic in-process recording of http(s) requests and responses\n\n## How it works\n\nOnce started, `netInterceptor` hooks itself into [the `net.connect`](https://nodejs.org/api/net.html#netconnect) and [the `tls.connect`](https://nodejs.org/api/tls.html#tlsconnectoptions-callback) methods\n\nWhen a socket is intercepted, we\n\n1. we create a mock net/TLS socket\n2. emit the `connect` event with the mock socket\n3. if `bypass()` was called in the `connect` event listener, we let the socket continue unintercepted\n4. if `bypass()` was not called\n   1. we create another mock socket for the response and emit the \"connection\" event\n   2. we emit `connect` on both mock sockets\n\nand then emit a `record` event with the `request`, `response`, `requestBody` and `responseBody` options.\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## Credits\n\n`@gr2m/net-interceptor` is built upon code and concepts from [moll/node-mitm](https://github.com/moll/node-mitm) by [Andri Möll](http://themoll.com). [Monday Calendar](https://mondayapp.com) supported that engineering work.\n\n**[Gregor Martynus](https://github.com/gr2m)** removed all `http(s)`-related code and made its focus on intercepting connections that use the lower-level `net` and `tls` modules.\n\n## License\n\n[LGPL](LICENSE.md)\n","funding_links":[],"categories":["Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fnode-net-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgr2m%2Fnode-net-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fnode-net-interceptor/lists"}