Skip to content

Commit b59c010

Browse files
authored
fix upgradeReq is undefined
an optimization in version 3.0 of the required proxy server [ websockets/ws#1114 ]drops the upgradeReq parameter to save 20% in memory. This breaks non-http(s) requests that use upgrade to switch - like web sockets (ws and wss). This was never a consideration for he mitm proxy (I guess since the package specifies version 3.2 and up - so it never worked). This matters for me so we re include it per the recommendation from the package maintainers [ websockets/ws#1099 ]
1 parent 8305361 commit b59c010

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/proxy.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ Proxy.prototype.listen = function(options, callback) {
6868
self.httpServer.on('request', self._onHttpServerRequest.bind(self, false));
6969
self.wsServer = new WebSocket.Server({ server: self.httpServer });
7070
self.wsServer.on('error', self._onError.bind(self, 'HTTP_SERVER_ERROR', null));
71-
self.wsServer.on('connection', self._onWebSocketServerConnect.bind(self, false));
71+
self.wsServer.on('connection', function(ws, req){
72+
ws.upgradeReq = req;
73+
self._onWebSocketServerConnect.bind(self, false);
74+
});
7275
const listenOptions = {
7376
host: self.httpHost,
7477
port: self.httpPort
@@ -100,7 +103,10 @@ Proxy.prototype._createHttpsServer = function (options, callback) {
100103
httpsServer.on('connect', this._onHttpServerConnect.bind(this));
101104
httpsServer.on('request', this._onHttpServerRequest.bind(this, true));
102105
var wssServer = new WebSocket.Server({ server: httpsServer });
103-
wssServer.on('connection', this._onWebSocketServerConnect.bind(this, true));
106+
wssServer.on('connection', function(ws, req){
107+
ws.upgradeReq = req;
108+
this._onWebSocketServerConnect.bind(this, true)
109+
});
104110
var listenArgs = [function() {
105111
if (callback) callback(httpsServer.address().port, httpsServer, wssServer);
106112
}];

0 commit comments

Comments
 (0)