Skip to content

Commit b26b434

Browse files
committed
Fix RoutingProxy hanging when there is an error
1 parent 4f2bc58 commit b26b434

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Diff for: lib/node-http-proxy/routing-proxy.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ var RoutingProxy = exports.RoutingProxy = function (options) {
5151
this.https = this.source.https || options.https;
5252
this.enable = options.enable;
5353
this.forward = options.forward;
54+
55+
//
56+
// Listen for 'newListener' events so that we can bind 'proxyError'
57+
// listeners to each HttpProxy's 'proxyError' event.
58+
//
59+
this.on('newListener', function (evt) {
60+
if (evt === 'proxyError' || evt === 'webSocketProxyError') {
61+
Object.keys(self.proxies).forEach(function (key) {
62+
self.proxies[key].on(evt, this.emit.bind(this, evt));
63+
});
64+
}
65+
});
5466
};
5567

5668

@@ -90,8 +102,12 @@ RoutingProxy.prototype.add = function (options) {
90102
});
91103

92104
this.proxies[key] = new HttpProxy(options);
93-
this.proxies[key].on('proxyError', this.emit.bind(this, 'proxyError'));
94-
this.proxies[key].on('webSocketProxyError', this.emit.bind(this, 'webSocketProxyError'));
105+
if (this.listeners('proxyError').length > 0) {
106+
this.proxies[key].on('proxyError', this.emit.bind(this, 'proxyError'));
107+
}
108+
if (this.listeners('webSocketProxyError').length > 0) {
109+
this.proxies[key].on('webSocketProxyError', this.emit.bind(this, 'webSocketProxyError'));
110+
}
95111
this.proxies[key].on('start', this.emit.bind(this, 'start'));
96112
this.proxies[key].on('forward', this.emit.bind(this, 'forward'));
97113
this.proxies[key].on('end', this.emit.bind(this, 'end'));
@@ -188,7 +204,6 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
188204

189205
if (!this.proxies[key]) {
190206
this.add(options);
191-
192207
}
193208

194209
proxy = this.proxies[key];

0 commit comments

Comments
 (0)