diff --git a/docs/modules/http-proxy.md b/docs/modules/http-proxy.md new file mode 100644 index 00000000..a2b059f2 --- /dev/null +++ b/docs/modules/http-proxy.md @@ -0,0 +1,56 @@ +--- +description: Modern alternatives to the http-proxy package for HTTP and WebSocket proxying in Node.js +--- + +# Replacements for `http-proxy` + +[`http-proxy`](https://github.com/http-party/node-http-proxy) has not been maintained since 2020 and has known crashes, memory leaks, and socket issues on modern Node.js versions. + +## `httpxy` + +[`httpxy`](https://github.com/unjs/httpxy) is a maintained fork of `http-proxy` with critical upstream bug fixes, zero dependencies, and additional helpers such as `proxyFetch` and `proxyUpgrade`. + +Example: + +```ts +import httpProxy from 'http-proxy' // [!code --] +import { createProxyServer } from 'httpxy' // [!code ++] +import { createServer } from 'node:http' + +const proxy = httpProxy.createProxyServer({}) // [!code --] +const proxy = createProxyServer({}) // [!code ++] + +const server = createServer((req, res) => { + proxy.web(req, res, { target: 'http://localhost:8080' }) // [!code --] + proxy.web(req, res, { target: 'http://localhost:8080' }) // [!code ++] +}) + +server.on('upgrade', (req, socket, head) => { + proxy.ws(req, socket, head, { target: 'http://localhost:8080' }) // [!code --] + proxy.ws(req, socket, { target: 'http://localhost:8080' }, head) // [!code ++] +}) +``` + +## `http-proxy-3` + +[`http-proxy-3`](https://github.com/sagemathinc/http-proxy-3) is a TypeScript rewrite with API compatibility, HTTP/2 support, and production use in Vite, JupyterHub, and CoCalc. + +Example: + +```ts +import httpProxy from 'http-proxy' // [!code --] +import { createProxyServer } from 'http-proxy-3' // [!code ++] +import { createServer } from 'node:http' + +const proxy = httpProxy.createProxyServer({}) // [!code --] +const proxy = createProxyServer({}) // [!code ++] + +const server = createServer((req, res) => { + proxy.web(req, res, { target: 'http://localhost:8080' }) +}) + +server.on('upgrade', (req, socket, head) => { + proxy.ws(req, socket, head, { target: 'http://localhost:8080' }) // [!code --] + proxy.ws(req, socket, { target: 'http://localhost:8080' }, head) // [!code ++] +}) +``` diff --git a/manifests/preferred.json b/manifests/preferred.json index 95031189..1860d349 100644 --- a/manifests/preferred.json +++ b/manifests/preferred.json @@ -492,6 +492,12 @@ "replacements": ["html-minifier-next"], "url": {"type": "e18e", "id": "html-minifier"} }, + "http-proxy": { + "type": "module", + "moduleName": "http-proxy", + "replacements": ["httpxy", "http-proxy-3"], + "url": {"type": "e18e", "id": "http-proxy"} + }, "inherits": { "type": "module", "moduleName": "inherits", @@ -3175,6 +3181,16 @@ "type": "documented", "replacementModule": "html-minifier-next" }, + "http-proxy-3": { + "id": "http-proxy-3", + "type": "documented", + "replacementModule": "http-proxy-3" + }, + "httpxy": { + "id": "httpxy", + "type": "documented", + "replacementModule": "httpxy" + }, "import.meta.resolve": { "id": "import.meta.resolve", "type": "native",