Commit 5d2192e6 authored by indexzero's avatar indexzero

[api minor] Small refactor to emit `webSocketProxyError` from a single helper...

[api minor] Small refactor to emit `webSocketProxyError` from a single helper function on any of the various `error` events in the proxy chain
parent 652cca37
......@@ -640,6 +640,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// Make the outgoing WebSocket request
var reverseProxy = agent.appendMessage(outgoing);
function proxyError (err) {
reverseProxy.end();
if (self.emit('webSocketProxyError', req, socket, head)) {
return;
}
socket.end();
}
//
// Here we set the incoming `req`, `socket` and `head` data to the outgoing
// request so that we can reuse this data later on in the closure scope
......@@ -665,7 +674,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
agent.on('upgrade', function (_, remoteSocket, head) {
//
// Prepare the socket for the reverseProxy request and begin to
// stream data between the two sockets
// stream data between the two sockets. Here it is important to
// note that `remoteSocket._httpMessage === reverseProxy`.
//
_socket(remoteSocket, true);
onUpgrade(remoteSocket._httpMessage, remoteSocket);
......@@ -705,26 +715,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
socket.write(sdata);
socket.write(data);
}
catch (e) {
reverseProxy.end();
socket.end();
catch (ex) {
proxyError(ex)
}
// Catch socket errors
socket.on('error', function() {
reverseProxy.end();
socket.end();
});
socket.on('error', proxyError);
// Remove data listener now that the 'handshake' is complete
reverseProxy.socket.removeListener('data', handshake);
});
}
reverseProxy.on('error', function (err) {
reverseProxy.end();
socket.end();
});
reverseProxy.on('error', proxyError);
try {
//
......@@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
reverseProxy.write(head);
}
catch (ex) {
reverseProxy.end();
socket.end();
proxyError(ex);
}
//
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment