From 86f96515a26fae893b32a4fc23cf6e21db7cb001 Mon Sep 17 00:00:00 2001 From: james-zinger <5473651+james-zinger@users.noreply.github.com> Date: Thu, 18 Jun 2020 15:08:07 -0400 Subject: [PATCH] Fixing http-proxy error handling to not crash on error (#7) --- src/daemon/group.js | 4 ++++ src/daemon/index.js | 4 ++++ test/daemon/group.js | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/src/daemon/group.js b/src/daemon/group.js index fed0324a..e57af9c7 100644 --- a/src/daemon/group.js +++ b/src/daemon/group.js @@ -24,6 +24,10 @@ class Group extends EventEmitter { this._proxy = httpProxy.createProxyServer({ xfwd: true }); + + // `http-proxy` requires that at least 1 listener exists to not raise + // an exception. See https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/index.js#L119 + this._proxy.on("error", err => console.error(err)); } _output(id, data) { diff --git a/src/daemon/index.js b/src/daemon/index.js index 2e2b3133..a6692330 100644 --- a/src/daemon/index.js +++ b/src/daemon/index.js @@ -40,6 +40,10 @@ const proxy = httpProxy.createServer({ xfwd: true }); +// `http-proxy` requires that at least 1 listener exists to not raise +// an exception. See https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/index.js#L119 +proxy.on("error", err => console.error(err)); + // Start HTTPS proxy and HTTP server proxy.listen(conf.port + 1); diff --git a/test/daemon/group.js b/test/daemon/group.js index cffbdc6b..bdc07f7a 100644 --- a/test/daemon/group.js +++ b/test/daemon/group.js @@ -156,3 +156,10 @@ test("group.handleConnect on port 443", t => { sinon.assert.calledWith(tcpProxy.proxy, socket, conf.port + 1); t.pass(); }); + +test("group proxy doesnt raise exception on error", t => { + const group = Group(); + // This line will raise if http-proxy doesn't have at least 1 listener + group._proxy.emit("error", "an error that occured"); + t.pass(); +});