Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 394966a

Browse files
committed
update code reliability
1 parent d0a128e commit 394966a

9 files changed

Lines changed: 227 additions & 404 deletions

File tree

README.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,62 @@ bun install @squarecloud/http-proxy
3939

4040
Create proxy:
4141

42-
```ts
43-
import { createServer } from "node:http";
44-
45-
import { createProxyServer } from "@squarecloud/http-proxy";
42+
```js
43+
const { createServer } = require("node:http");
44+
const { createProxyServer } = require("@squarecloud/http-proxy");
4645

4746
const proxy = createProxyServer({});
4847
const target = "http://example.com"; /* address of your proxy server here */
4948

5049
const server = createServer(async (req, res) => {
51-
try {
52-
await proxy.web(req, res, { target });
53-
} catch (error) {
54-
console.error(error);
55-
res.statusCode = 500;
56-
res.end("Proxy error: " + error.toString());
57-
}
50+
try {
51+
await proxy.web(req, res, { target });
52+
} catch (error) {
53+
console.error(error);
54+
res.statusCode = 500;
55+
res.end("Proxy error: " + error.toString());
56+
}
5857
});
5958

6059
server.listen(80, () => console.log("Proxy is listening on http://localhost"));
6160
```
6261

63-
Checkout [http-party/node-http-proxy](https://github.com/http-party/node-http-proxy) for more options and examples.
62+
Example with WebSocket:
63+
64+
```js
65+
const { createServer } = require("node:http");
66+
const { createProxyServer } = require("@squarecloud/http-proxy");
67+
68+
const proxy = createProxyServer({ ws: true });
69+
const target = "ws://example.com"; /* address of your proxy server here */
70+
71+
const server = createServer(async (req, res) => { /* ... */ });
72+
73+
server.on("upgrade", async (req, socket, head) => {
74+
try {
75+
// use proxy.ws() instead of proxy.web() for proxying WebSocket requests.
76+
await proxy.ws(req, socket, head, { target });
77+
} catch (error) {
78+
console.error(error);
79+
socket.end();
80+
}
81+
});
82+
83+
server.listen(80, () => console.log("Proxy is listening on http://localhost"));
84+
```
85+
86+
Some options:
87+
88+
```js
89+
// Options most used in the proxy configuration:
90+
// * ws : <true/false, if you want to proxy websockets>
91+
// * xfwd : <true/false, adds X-Forward headers>
92+
// * secure : <true/false, verify SSL certificate>
93+
// * prependPath: <true/false, Default: true - specify whether you want to prepend the target"s path to the proxy path>
94+
// * ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
95+
```
96+
97+
Checkout [http-party/node-http-proxy](https://github.com/http-party/node-http-proxy#options) for more options and examples.
6498

6599
## Development
66100

lib/http-proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function createProxyServer(options) {
2525
* agent : <object to be passed to http(s).request>
2626
* ssl : <object to be passed to https.createServer()>
2727
* ws : <true/false, if you want to proxy websockets>
28-
* xfwd : <true/false, adds x-forward headers>
28+
* xfwd : <true/false, adds X-Forward headers>
2929
* secure : <true/false, verify SSL certificate>
3030
* toProxy: <true/false, explicitly specify if we are proxying to another proxy>
3131
* prependPath: <true/false, Default: true - specify whether you want to prepend the target"s path to the proxy path>

lib/http-proxy/common.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ common.rewriteCookieProperty = function rewriteCookieProperty(header, config, pr
229229
// replace value
230230
return prefix + newValue;
231231
} else {
232-
// remove value
233232
return "";
234233
}
235234
});

lib/http-proxy/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const ws = require("./passes/ws-incoming");
88
* Returns a function that creates the loader for
99
* either `ws` or `web`"s passes.
1010
*
11-
* Examples:
12-
*
13-
* httpProxy.createRightProxy("ws")
14-
* // => [Function]
1511
*
1612
* @param {String} Type Either "ws" or "web"
1713
*
@@ -87,13 +83,8 @@ class ProxyServer {
8783
this.ws = this.proxyWebsocketRequest = createRightProxy("ws")(options);
8884
this.options = options;
8985

90-
this.webPasses = Object.keys(web).map(function (pass) {
91-
return web[pass];
92-
});
93-
94-
this.wsPasses = Object.keys(ws).map(function (pass) {
95-
return ws[pass];
96-
});
86+
this.webPasses = Object.keys(web).map((pass) => web[pass]);
87+
this.wsPasses = Object.keys(ws).map((pass) => ws[pass]);
9788

9889
this.on("error", this.onError, this);
9990
}

lib/http-proxy/passes/web-incoming.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ module.exports = {
136136
}
137137

138138
// Ensure we abort proxy if request is aborted
139-
req.on("aborted", function () {
140-
proxyReq.destroy();
141-
});
139+
req.on("aborted", proxyReq.destroy);
142140

143141
// handle errors in proxy and incoming request, just like for forward proxy
144142
const proxyError = createErrorHandler(proxyReq, options.target);
@@ -175,13 +173,12 @@ module.exports = {
175173

176174
if (!res.finished) {
177175
// Allow us to listen when the proxy has completed
178-
proxyRes.on("end", function () {
179-
if (server) server.emit("end", req, res, proxyRes);
180-
});
176+
proxyRes.on("end", () => (server ? server.emit("end", req, res, proxyRes) : null));
177+
181178
// We pipe to the response unless its expected to be handled by the user
182179
if (!options.selfHandleResponse) proxyRes.pipe(res);
183-
} else {
184-
if (server) server.emit("end", req, res, proxyRes);
180+
} else if (server) {
181+
server.emit("end", req, res, proxyRes);
185182
}
186183
});
187184
},

lib/http-proxy/passes/web-outgoing.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ const redirectRegex = /^201|30(1|2|7|8)$/;
1010
*/
1111

1212
module.exports = {
13-
// <--
14-
1513
/**
1614
* If is a HTTP 1.0 request, remove chunk headers
1715
*

lib/http-proxy/passes/ws-incoming.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ module.exports = {
110110
proxyReq.on("response", function (res) {
111111
// if upgrade event isn"t going to happen, close the socket
112112
if (!res.upgrade && socket.readyState !== "closed") {
113-
socket.write(createHttpHeader("HTTP/" + res.httpVersion + " " + res.statusCode + " " + res.statusMessage, res.headers));
113+
socket.write(createHttpHeader(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`, res.headers));
114114
res.pipe(socket);
115115
}
116116
});
@@ -126,9 +126,7 @@ module.exports = {
126126
// The pipe below will end proxySocket if socket closes cleanly, but not
127127
// if it errors (eg, vanishes from the net and starts returning
128128
// EHOSTUNREACH). We need to do that explicitly.
129-
socket.on("error", function () {
130-
proxySocket.end();
131-
});
129+
socket.on("error", proxySocket.end);
132130

133131
common.setupSocket(proxySocket);
134132

0 commit comments

Comments
 (0)