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

Commit 2ceec88

Browse files
committed
chore: just prettier
1 parent 30f7d75 commit 2ceec88

9 files changed

Lines changed: 84 additions & 69 deletions

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ const proxy = createProxyServer({});
4848
const target = "http://example.com"; /* address of your proxy server here */
4949

5050
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-
}
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+
}
5858
});
5959

6060
server.listen(80, () => console.log("Proxy is listening on http://localhost"));
@@ -69,16 +69,18 @@ const { createProxyServer } = require("@squarecloud/http-proxy");
6969
const proxy = createProxyServer({ ws: true });
7070
const target = "ws://example.com"; /* address of your proxy server here */
7171

72-
const server = createServer(async (req, res) => { /* ... */ });
72+
const server = createServer(async (req, res) => {
73+
/* ... */
74+
});
7375

7476
server.on("upgrade", async (req, socket, head) => {
75-
try {
76-
// use proxy.ws() instead of proxy.web() for proxying WebSocket requests.
77-
await proxy.ws(req, socket, head, { target });
78-
} catch (error) {
79-
console.error(error);
80-
socket.end();
81-
}
77+
try {
78+
// use proxy.ws() instead of proxy.web() for proxying WebSocket requests.
79+
await proxy.ws(req, socket, head, { target });
80+
} catch (error) {
81+
console.error(error);
82+
socket.end();
83+
}
8284
});
8385

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

lib/http-proxy/common.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ common.isSSL = isSSL;
5454
common.setupOutgoing = function (outgoing, options, req, forward) {
5555
outgoing.port = options[forward || "target"].port || (isSSL.test(options[forward || "target"].protocol) ? 443 : 80);
5656
["host", "hostname", "socketPath", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "secureProtocol"].forEach(
57-
(e) => (outgoing[e] = outgoing[e] || options[forward || "target"][e]),
57+
(e) => (outgoing[e] = outgoing[e] || options[forward || "target"][e])
5858
);
5959

6060
outgoing.method = options.method || req.method;
@@ -75,8 +75,10 @@ common.setupOutgoing = function (outgoing, options, req, forward) {
7575
if (outgoing.port === 443) {
7676
// Respect `NODE_TLS_REJECT_UNAUTHORIZED` environment variable (https://nodejs.org/docs/latest/api/cli.html#node_tls_reject_unauthorizedvalue)
7777
const NODE_TLS_REJECT_UNAUTHORIZED = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
78-
const rejectUnauthorizedEnv = typeof NODE_TLS_REJECT_UNAUTHORIZED !== "undefined" ? NODE_TLS_REJECT_UNAUTHORIZED.toString() : undefined;
79-
outgoing.rejectUnauthorized = typeof options.secure === "undefined" ? rejectUnauthorizedEnv !== "0" : options.secure;
78+
const rejectUnauthorizedEnv =
79+
typeof NODE_TLS_REJECT_UNAUTHORIZED !== "undefined" ? NODE_TLS_REJECT_UNAUTHORIZED.toString() : undefined;
80+
outgoing.rejectUnauthorized =
81+
typeof options.secure === "undefined" ? rejectUnauthorizedEnv !== "0" : options.secure;
8082
}
8183

8284
outgoing.agent = options.agent || false;
@@ -98,7 +100,11 @@ common.setupOutgoing = function (outgoing, options, req, forward) {
98100
const targetPath = target && options.prependPath !== false ? target.path || target.pathname || "" : "";
99101
const pathToAppend = req.url.split("?")[0] || "";
100102
const query = req.url.split("?").slice(1).join("?");
101-
const outgoingPath = !options.ignorePath ? (!options.toProxy ? pathToAppend + (query ? `?${query}` : "") : req.url) : "";
103+
const outgoingPath = !options.ignorePath
104+
? !options.toProxy
105+
? pathToAppend + (query ? `?${query}` : "")
106+
: req.url
107+
: "";
102108

103109
outgoing.path = common.urlJoin(targetPath, outgoingPath);
104110

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ module.exports = {
6464

6565
const encrypted = req.isSpdy || common.hasEncryptedConnection(req);
6666
const values = {
67-
For: req.headers["cf-connecting-ip"] || req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress,
67+
For:
68+
req.headers["cf-connecting-ip"] ||
69+
req.headers["x-forwarded-for"] ||
70+
req.connection.remoteAddress ||
71+
req.socket.remoteAddress,
6872
Port: common.getPort(req),
6973
Proto: encrypted ? "https" : "http",
7074
};
@@ -100,23 +104,25 @@ module.exports = {
100104
if (options.forward) {
101105
// If forward enable, so just pipe the request
102106
const forwardReq = (options.forward.protocol === "https:" ? https : http).request(
103-
common.setupOutgoing(options.ssl || {}, options, req, "forward"),
107+
common.setupOutgoing(options.ssl || {}, options, req, "forward")
104108
);
105109
// error handler (e.g. ECONNRESET, ECONNREFUSED)
106110
// Handle errors on incoming request as well as it makes sense to
107111
const forwardError = createErrorHandler(forwardReq, options.forward);
108112
req.on("error", forwardError);
109113
forwardReq.on("error", forwardError);
110114

111-
pipeline(options.buffer || req, forwardReq, () => { });
115+
pipeline(options.buffer || req, forwardReq, () => {});
112116

113117
if (!options.target) {
114118
return res.end();
115119
}
116120
}
117121

118122
// Request initalization
119-
const proxyReq = (options.target.protocol === "https:" ? https : http).request(common.setupOutgoing(options.ssl || {}, options, req));
123+
const proxyReq = (options.target.protocol === "https:" ? https : http).request(
124+
common.setupOutgoing(options.ssl || {}, options, req)
125+
);
120126

121127
// Enable developers to modify the proxyReq before headers are sent
122128
proxyReq.on("socket", (socket) => {
@@ -126,15 +132,15 @@ module.exports = {
126132
if (server && !proxyReq.getHeader("expect")) {
127133
server.emit("proxyReq", proxyReq, req, res, options);
128134
}
129-
pipeline(options.buffer || req, proxyReq, () => { });
135+
pipeline(options.buffer || req, proxyReq, () => {});
130136
});
131137
} else {
132138
if (server && !proxyReq.getHeader("expect")) {
133139
server.emit("proxyReq", proxyReq, req, res, options);
134140
}
135141

136142
req.on("close", proxyReq.destroy);
137-
pipeline(options.buffer || req, proxyReq, () => { });
143+
pipeline(options.buffer || req, proxyReq, () => {});
138144
}
139145
});
140146

@@ -178,7 +184,7 @@ module.exports = {
178184
if (!res.writableEnded) {
179185
// Allow us to listen when the proxy has completed
180186
proxyRes.on("end", () => (server ? server.emit("end", req, res, proxyRes) : null));
181-
pipeline(proxyRes, res, () => { });
187+
pipeline(proxyRes, res, () => {});
182188
} else {
183189
if (server) {
184190
server.emit("end", req, res, proxyRes);
@@ -191,7 +197,7 @@ module.exports = {
191197
if ((req.destroyed || req.socket?.destroyed) && err.code === "ECONNRESET") {
192198
server.emit("econnreset", err, req, res, url);
193199

194-
if (typeof proxyReq.destroy === "function" && !proxyReq.destroyed) {
200+
if (typeof proxyReq.destroy === "function" && !proxyReq.destroyed && !proxyReq.socket?.destroyed) {
195201
proxyReq.destroy();
196202
}
197203

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ module.exports = {
4343
if (!options.xfwd) return;
4444

4545
const remoteAddress =
46-
req.headers["cf-connecting-ip"] || req.headers["x-forwarded-for"] || req.connection?.remoteAddress || req.socket?.remoteAddress;
46+
req.headers["cf-connecting-ip"] ||
47+
req.headers["x-forwarded-for"] ||
48+
req.connection?.remoteAddress ||
49+
req.socket?.remoteAddress;
4750

4851
const values = {
4952
For: remoteAddress,
@@ -85,7 +88,7 @@ module.exports = {
8588
}
8689
return head;
8790
},
88-
[line],
91+
[line]
8992
)
9093
.join("\r\n") + "\r\n\r\n"
9194
);
@@ -96,7 +99,7 @@ module.exports = {
9699
if (head && head.length) socket.unshift(head);
97100

98101
const proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
99-
common.setupOutgoing(options.ssl || {}, options, req),
102+
common.setupOutgoing(options.ssl || {}, options, req)
100103
);
101104

102105
// Enable developers to modify the proxyReq before headers are sent
@@ -118,9 +121,7 @@ module.exports = {
118121
proxySocket.on("error", onOutgoingError);
119122

120123
// Allow us to listen when the websocket has completed
121-
proxySocket.on("end", () => {
122-
server.emit("close", proxyRes, proxySocket, proxyHead);
123-
});
124+
proxySocket.on("end", () => server.emit("close", proxyRes, proxySocket, proxyHead));
124125

125126
// The pipe below will end proxySocket if socket closes cleanly, but not
126127
// if it errors (eg, vanishes from the net and starts returning

test/lib-http-proxy-common-test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("lib/http-proxy/common.js", () => {
2424
method: "i",
2525
url: "am",
2626
headers: { pro: "xy", overwritten: false },
27-
},
27+
}
2828
);
2929

3030
expect(outgoing.host).to.eql("hey");
@@ -61,7 +61,7 @@ describe("lib/http-proxy/common.js", () => {
6161
method: "i",
6262
url: "am",
6363
headers: { pro: "xy", overwritten: false },
64-
},
64+
}
6565
);
6666
expect(outgoing.headers.connection).to.eql("upgrade");
6767
});
@@ -84,7 +84,7 @@ describe("lib/http-proxy/common.js", () => {
8484
method: "i",
8585
url: "am",
8686
headers: { pro: "xy", overwritten: false },
87-
},
87+
}
8888
);
8989
expect(outgoing.headers.connection).to.eql("keep-alive, upgrade");
9090
});
@@ -108,7 +108,7 @@ describe("lib/http-proxy/common.js", () => {
108108
method: "i",
109109
url: "am",
110110
headers: { pro: "xy", overwritten: false },
111-
},
111+
}
112112
);
113113
expect(outgoing.headers.connection).to.eql("close");
114114
});
@@ -131,7 +131,7 @@ describe("lib/http-proxy/common.js", () => {
131131
method: "i",
132132
url: "am",
133133
headers: { pro: "xy", overwritten: false },
134-
},
134+
}
135135
);
136136
expect(outgoing.headers.connection).to.eql("close");
137137
});
@@ -159,7 +159,7 @@ describe("lib/http-proxy/common.js", () => {
159159
method: "i",
160160
url: "am",
161161
headers: { pro: "xy" },
162-
},
162+
}
163163
);
164164

165165
expect(outgoing.host).to.eql("how");
@@ -194,7 +194,7 @@ describe("lib/http-proxy/common.js", () => {
194194
{
195195
url: "am",
196196
},
197-
"forward",
197+
"forward"
198198
);
199199

200200
expect(outgoing.path).to.eql("some-path/am");
@@ -210,7 +210,7 @@ describe("lib/http-proxy/common.js", () => {
210210
host: "whatever.com",
211211
},
212212
},
213-
{ url: "/" },
213+
{ url: "/" }
214214
);
215215

216216
expect(outgoing.port).to.eql(443);
@@ -224,7 +224,7 @@ describe("lib/http-proxy/common.js", () => {
224224
target: { path: "hellothere" },
225225
prependPath: false,
226226
},
227-
{ url: "hi" },
227+
{ url: "hi" }
228228
);
229229

230230
expect(outgoing.path).to.eql("hi");
@@ -237,7 +237,7 @@ describe("lib/http-proxy/common.js", () => {
237237
{
238238
target: { path: "/forward" },
239239
},
240-
{ url: "/static/path" },
240+
{ url: "/static/path" }
241241
);
242242

243243
expect(outgoing.path).to.eql("/forward/static/path");
@@ -250,7 +250,7 @@ describe("lib/http-proxy/common.js", () => {
250250
{
251251
target: { path: "/forward" },
252252
},
253-
{ url: "/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2" },
253+
{ url: "/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2" }
254254
);
255255

256256
expect(outgoing.path).to.eql("/forward/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2");
@@ -268,7 +268,7 @@ describe("lib/http-proxy/common.js", () => {
268268
target: new URL("http://sometarget.com:80"),
269269
toProxy: true,
270270
},
271-
{ url: google },
271+
{ url: google }
272272
);
273273

274274
expect(outgoing.path).to.eql("/" + google);
@@ -283,7 +283,7 @@ describe("lib/http-proxy/common.js", () => {
283283
target: new URL("http://sometarget.com:80"),
284284
toProxy: true,
285285
},
286-
{ url: google },
286+
{ url: google }
287287
);
288288

289289
expect(outgoing.path).to.eql("/" + google);
@@ -298,7 +298,7 @@ describe("lib/http-proxy/common.js", () => {
298298
target: new URL("http://sometarget.com:80"),
299299
toProxy: true,
300300
},
301-
{ url: google },
301+
{ url: google }
302302
);
303303

304304
expect(outgoing.path).to.eql("/" + google);
@@ -314,7 +314,7 @@ describe("lib/http-proxy/common.js", () => {
314314
target: new URL(myEndpoint),
315315
ignorePath: true,
316316
},
317-
{ url: "/more/crazy/pathness" },
317+
{ url: "/more/crazy/pathness" }
318318
);
319319

320320
expect(outgoing.path).to.eql("/some/crazy/path/whoooo");
@@ -330,7 +330,7 @@ describe("lib/http-proxy/common.js", () => {
330330
ignorePath: true,
331331
prependPath: false,
332332
},
333-
{ url: "/more/crazy/pathness" },
333+
{ url: "/more/crazy/pathness" }
334334
);
335335

336336
expect(outgoing.path).to.eql("");
@@ -347,7 +347,7 @@ describe("lib/http-proxy/common.js", () => {
347347
target: new URL(myEndpoint),
348348
changeOrigin: true,
349349
},
350-
{ url: "/" },
350+
{ url: "/" }
351351
);
352352

353353
expect(outgoing.headers.host).to.eql("mycouch.com:6984");
@@ -365,7 +365,7 @@ describe("lib/http-proxy/common.js", () => {
365365
},
366366
changeOrigin: true,
367367
},
368-
{ url: "/" },
368+
{ url: "/" }
369369
);
370370
expect(outgoing.headers.host).to.eql("mycouch.com:6984");
371371
});
@@ -394,7 +394,7 @@ describe("lib/http-proxy/common.js", () => {
394394
{
395395
method: "i",
396396
url: "am",
397-
},
397+
}
398398
);
399399

400400
expect(outgoing.pfx).eql("my-pfx");
@@ -414,7 +414,7 @@ describe("lib/http-proxy/common.js", () => {
414414
target: new URL("https://whooooo.com"),
415415
method: "POST",
416416
},
417-
{ method: "GET", url: "" },
417+
{ method: "GET", url: "" }
418418
);
419419

420420
expect(outgoing.method).eql("POST");

0 commit comments

Comments
 (0)