10000 [refactor] Use `pipe` for websockets · seanhussey/node-http-proxy@6742402 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6742402

Browse files
committed
[refactor] Use pipe for websockets
1 parent fd8dfda commit 6742402

File tree

1 file changed

+3
-80
lines changed

1 file changed

+3
-80
lines changed

lib/http-proxy/http-proxy.js

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
348348
HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) {
349349
var self = this,
350350
outgoing = new(this.target.base),
351-
listeners = {},
352351
errState = false,
353352
CRLF = '\r\n';
354353

@@ -439,93 +438,17 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
439438
// Any incoming data on this WebSocket to the proxy target
440439
// will be written to the `reverseProxy` socket.
441440
//
442-
proxySocket.on('data', listeners.onIncoming = function (data) {
443-
if (reverseProxy.incoming.socket.writable) {
444-
try {
445-
self.emit('websocket:outgoing', req, socket, head, data);
446-
var flushed = reverseProxy.incoming.socket.write(data);
447-
if (!flushed) {
448-
proxySocket.pause();
449-
reverseProxy.incoming.socket.once('drain', function () {
450-
try { proxySocket.resume() }
451-
catch (er) { console.error("proxySocket.resume error: %s", er.message) }
452-
});
453-
454-
//
455-
// Force the `drain` event in 100ms if it hasn't
456-
// happened on its own.
457-
//
458-
setTimeout(function () {
459-
reverseProxy.incoming.socket.emit('drain');
460-
}, 100);
461-
}
462-
}
463-
catch (ex) {
464-
detach();
465-
}
466-
}
467-
});
441+
proxySocket.pipe(reverseProxy.incoming.socket);
468442

469443
//
470444
// Any outgoing data on this Websocket from the proxy target
471445
// will be written to the `proxySocket` socket.
472446
//
473-
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function (data) {
474-
try {
475-
self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data);
476-
var flushed = proxySocket.write(data);
477-
if (!flushed) {
478-
reverseProxy.incoming.socket.pause();
479-
proxySocket.once('drain', function () {
480-
try { reverseProxy.incoming.socket.resume() }
481-
catch (er) { console.error("reverseProxy.incoming.socket.resume error: %s", er.message) }
482-
});
447+
reverseProxy.incoming.socket.pipe(proxySocket);
483448

484-
//
485-
// Force the `drain` event in 100ms if it hasn't
486-
// happened on its own.
487-
//
488-
setTimeout(function () {
489-
proxySocket.emit('drain');
490-
}, 100);
491-
}
492-
}
493-
catch (ex) {
494-
detach();
495-
}
496-
});
497-
498-
//
499-
// Helper function to detach all event listeners
500-
// from `reverseProxy` and `proxySocket`.
501-
//
502-
function detach() {
503-
proxySocket.destroySoon();
504-
proxySocket.removeListener('end', listeners.onIncomingClose);
505-
proxySocket.removeListener('data', listeners.onIncoming);
506-
reverseProxy.incoming.socket.destroySoon();
507-
reverseProxy.incoming.socket.removeListener('end', listeners.onOutgoingClose);
508-
reverseProxy.incoming.socket.removeListener('data', listeners.onOutgoing);
509-
}
510-
511-
//
512-
// If the incoming `proxySocket` socket closes, then
513-
// detach all event listeners.
514-
//
515-
proxySocket.on('end', listeners.onIncomingClose = function() {
516-
detach();
517-
518-
// Emit the `end` event now that we have completed proxying
449+
proxySocket.on('end', function () {
519450
self.emit('websocket:end', req, socket, head);
520451
});
521-
522-
//
523-
// If the `reverseProxy` socket closes, then detach all
524-
// event listeners.
525-
//
526-
reverseProxy.incoming.socket.on('end', listeners.onOutgoingClose = function() {
527-
detach();
528-
});
529452
}
530453

531454
function getPort (port) {

0 commit comments

Comments
 (0)
0