{"id":15479846,"url":"https://github.com/gr2m/node-http-interceptor","last_synced_at":"2026-01-23T18:33:20.173Z","repository":{"id":57114066,"uuid":"442149411","full_name":"gr2m/node-http-interceptor","owner":"gr2m","description":"Intercept and mock outgoing http/https requests","archived":false,"fork":false,"pushed_at":"2022-09-23T19:40:33.000Z","size":63,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-20T01:21:10.927Z","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-27T12:07:23.000Z","updated_at":"2024-03-21T18:27:29.000Z","dependencies_parsed_at":"2022-08-22T09:10:25.371Z","dependency_job_id":null,"html_url":"https://github.com/gr2m/node-http-interceptor","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/gr2m/node-http-interceptor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-http-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-http-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-http-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-http-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gr2m","download_url":"https://codeload.github.com/gr2m/node-http-interceptor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fnode-http-interceptor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28697428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T17:25:48.045Z","status":"ssl_error","status_checked_at":"2026-01-23T17:25:47.153Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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:28:04.163Z","updated_at":"2026-01-23T18:33:20.155Z","avatar_url":"https://github.com/gr2m.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `@gr2m/http-interceptor`\n\n[![Test](https://github.com/gr2m/node-http-interceptor/actions/workflows/test.yml/badge.svg)](https://github.com/gr2m/node-http-interceptor/actions/workflows/test.yml)\n\n\u003e Intercept and mock outgoing http/https requests\n\n## Install\n\n```\nnpm install @gr2m/http-interceptor\n```\n\n## Usage\n\n```js\nimport httpInterceptor from \"@gr2m/http-interceptor\";\n\nhttpInterceptor.start();\nhttpInterceptor.on(\"connect\", (socket, options, bypass) =\u003e {\n  // call bypass() to continue the unintercepted request\n  if (options.host === \"db.example.com\") return bypass();\n});\n\nhttpInterceptor.on(\"request\", (request, response) =\u003e {\n  response.end(\"Hello World!\");\n});\n```\n\n## API\n\n`httpInterceptor` is a singleton API.\n\n### `httpInterceptor.start()`\n\nHooks into the request life cycle and emits `connect` events for each request that connects to a server as well as `request` events for all intercepted requests.\n\n### `httpInterceptor.stop()`\n\nStops intercepting. No `connect` or `request` events will be emitted.\n\n### `httpInterceptor.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#### `request` event\n\nThe `listener` callback is called with 2 arguments\n\n- `request`: `Http.IncomingMessage`\n- `response`: `Http.ServerResponse`\n\nIt's the same arguments as e.g. `http.createServer(listener)` receives.\n\n### `httpInterceptor.removeListener(event, listener)`\n\nRemove an event listener.\n\n### `httpInterceptor.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## See also\n\n- [`@gr2m/net-interceptor`](https://github.com/gr2m/node-net-interceptor) - Intercept outgoing network TCP/TLS connections\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\n`@gr2m/http-interceptor` is using [`@gr2m/net-interceptor`](https://github.com/gr2m/node-net-interceptor/#readme) to intercept TCP/TLS connections, and to permit to bypass the interception.\n\n`@gr2m/http-interceptor` also hooks into `http.ClientRequest.prototype.onSocket` which is called in the `http.ClientRequest` constructor. Each time `http.ClientRequest` is instantiated with a socket, we check if the socket is intercepted and if so, emit the `request` event with a `request`/`response` pair for mocking.\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## Credits\n\n`@gr2m/http-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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fnode-http-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgr2m%2Fnode-http-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fnode-http-interceptor/lists"}