Skip to content

Commit 7ac322c

Browse files
authored
Merge pull request #11 from overleaf/jpa-req-socket-null
Node 18 support
2 parents 9841335 + 00d3d71 commit 7ac322c

16 files changed

+75
-57
lines changed

History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.9.19-overleaf-10 / 2023-04-25
2+
===============================
3+
4+
* Overleaf: Add support for Node 18 by gracefully handling `req.socket == null`.
5+
16
0.9.19-overleaf-9 / 2022-08-18
27
==============================
38

lib/manager.js

+31-22
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,21 @@ function Manager (server, options) {
115115
server.removeAllListeners('request');
116116

117117
server.on('request', function (req, res) {
118-
self.handleRequest(req, res);
118+
var socket = req.socket;
119+
if (!socket) {
120+
// aborted request
121+
self.log.warn('bail out from aborted request (req.socket missing)');
122+
// nginx uses status code 499 when clients close the connection before a response could be sent.
123+
// https://github.com/nginx/nginx/blob/bfc5b35827903a3c543b58e4562db8b62021c164/src/http/ngx_http_request.h#L128-L134
124+
try {
125+
res.writeHead(499);
126+
res.end();
127+
} catch (e) {
128+
self.log.warn('cannot reply to aborted request: ' + e.message);
129+
}
130+
return;
131+
}
132+
self.handleRequest(req, res, socket);
119133
});
120134

121135
server.on('upgrade', function (req, socket, head) {
@@ -579,7 +593,7 @@ Manager.prototype.onDisconnect = function (id) {
579593
* @api private
580594
*/
581595

582-
Manager.prototype.handleRequest = function (req, res) {
596+
Manager.prototype.handleRequest = function (req, res, socket) {
583597
var data = this.checkRequest(req);
584598

585599
if (!data) {
@@ -610,9 +624,9 @@ Manager.prototype.handleRequest = function (req, res) {
610624
this.log.info('client protocol version unsupported');
611625
} else {
612626
if (data.id) {
613-
this.handleHTTPRequest(data, req, res);
627+
this.handleHTTPRequest(data, req, res, socket);
614628
} else {
615-
this.handleHandshake(data, req, res);
629+
this.handleHandshake(data, req, res, socket);
616630
}
617631
}
618632
};
@@ -637,7 +651,7 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
637651
}
638652

639653
req.head = head;
640-
this.handleClient(data, req);
654+
this.handleClient(data, req, socket);
641655
req.head = null;
642656
};
643657

@@ -647,9 +661,9 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
647661
* @api private
648662
*/
649663

650-
Manager.prototype.handleHTTPRequest = function (data, req, res) {
664+
Manager.prototype.handleHTTPRequest = function (data, req, res, socket) {
651665
req.res = res;
652-
this.handleClient(data, req);
666+
this.handleClient(data, req, socket);
653667
};
654668

655669
/**
@@ -658,11 +672,7 @@ Manager.prototype.handleHTTPRequest = function (data, req, res) {
658672
* @api private
659673
*/
660674

661-
Manager.prototype.handleClient = function (data, req) {
662-
var socket = req.socket
663-
, store = this.store
664-
, self = this;
665-
675+
Manager.prototype.handleClient = function (data, req, socket) {
666676
// handle sync disconnect xhrs
667677
if (undefined != data.query.disconnect) {
668678
if (this.transports[data.id] && this.transports[data.id].open) {
@@ -677,16 +687,16 @@ Manager.prototype.handleClient = function (data, req) {
677687

678688
if (!~this.get('transports').indexOf(data.transport)) {
679689
this.log.warn('unknown transport: "' + data.transport + '"');
680-
req.connection.end();
690+
socket.end();
681691
return;
682692
}
683693

684-
var transport = new transports[data.transport](this, data, req)
694+
var transport = new transports[data.transport](this, data, req, socket)
685695
, handshaken = this.handshaken[data.id];
686696

687697
if (transport.disconnected) {
688698
// failed during transport setup
689-
req.connection.end();
699+
socket.end();
690700
return;
691701
}
692702
if (handshaken) {
@@ -713,7 +723,7 @@ Manager.prototype.handleClient = function (data, req) {
713723
// initialize the socket for all namespaces
714724
for (var i in this.namespaces) {
715725
if (this.namespaces.hasOwnProperty(i)) {
716-
var socket = this.namespaces[i].socket(data.id, true);
726+
this.namespaces[i].socket(data.id, true);
717727

718728
// echo back connect packet and fire connection event
719729
if (i === '') {
@@ -762,7 +772,7 @@ Manager.prototype.generateId = function () {
762772
* @api private
763773
*/
764774

765-
Manager.prototype.handleHandshake = function (data, req, res) {
775+
Manager.prototype.handleHandshake = function (data, req, res, socket) {
766776
var self = this
767777
, origin = req.headers.origin
768778
, headers = {
@@ -789,7 +799,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
789799
return;
790800
}
791801

792-
var handshakeData = this.handshakeData(data);
802+
var handshakeData = this.handshakeData(data, socket);
793803

794804
if (origin) {
795805
// https://developer.mozilla.org/En/HTTP_Access_Control
@@ -835,9 +845,8 @@ Manager.prototype.handleHandshake = function (data, req, res) {
835845
* @api private
836846
*/
837847

838-
Manager.prototype.handshakeData = function (data) {
839-
var connection = data.request.connection
840-
, connectionAddress
848+
Manager.prototype.handshakeData = function (data, connection) {
849+
var connectionAddress
841850
, date = new Date;
842851

843852
if (connection.remoteAddress) {
@@ -859,7 +868,7 @@ Manager.prototype.handshakeData = function (data) {
859868
, query: data.query
860869
, url: data.request.url
861870
, xdomain: !!data.request.headers.origin
862-
, secure: data.request.connection.secure
871+
, secure: connection.secure
863872
, issued: +date
864873
};
865874
};

lib/socket.io.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Version.
1717
*/
1818

19-
exports.version = '0.9.19-overleaf-4';
19+
exports.version = '0.9.19-overleaf-10';
2020

2121
/**
2222
* Supported protocol version.

lib/transport.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ exports = module.exports = Transport;
2323
* @api public
2424
*/
2525

26-
function Transport (mng, data, req) {
26+
function Transport (mng, data, req, socket) {
2727
this.manager = mng;
2828
this.id = data.id;
2929
this.disconnected = false;
3030
this.drained = true;
31-
this.handleRequest(req);
31+
this.handleRequest(req, socket);
3232
};
3333

3434
/**
@@ -57,12 +57,12 @@ Transport.prototype.__defineGetter__('store', function () {
5757
* @api private
5858
*/
5959

60-
Transport.prototype.handleRequest = function (req) {
60+
Transport.prototype.handleRequest = function (req, socket) {
6161
this.log.debug('setting request', req.method, req.url);
6262
this.req = req;
6363

6464
if (req.method == 'GET') {
65-
this.socket = req.socket;
65+
this.socket = socket;
6666
this.open = true;
6767
this.drained = true;
6868
this.setHeartbeatInterval();

lib/transports/flashsocket.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ exports = module.exports = FlashSocket;
4242
* @api public
4343
*/
4444

45-
function FlashSocket (mng, data, req) {
46-
return WebSocket.call(this, mng, data, req);
45+
function FlashSocket (mng, data, req, socket) {
46+
return WebSocket.call(this, mng, data, req, socket);
4747
}
4848

4949
/**

lib/transports/htmlfile.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ exports = module.exports = HTMLFile;
2323
* @api public
2424
*/
2525

26-
function HTMLFile (mng, data, req) {
27-
HTTPTransport.call(this, mng, data, req);
26+
function HTMLFile (mng, data, req, socket) {
27+
HTTPTransport.call(this, mng, data, req, socket);
2828
};
2929

3030
/**
@@ -47,8 +47,8 @@ HTMLFile.prototype.name = 'htmlfile';
4747
* @api private
4848
*/
4949

50-
HTMLFile.prototype.handleRequest = function (req) {
51-
HTTPTransport.prototype.handleRequest.call(this, req);
50+
HTMLFile.prototype.handleRequest = function (req, socket) {
51+
HTTPTransport.prototype.handleRequest.call(this, req, socket);
5252

5353
if (req.method == 'GET') {
5454
req.res.writeHead(200, {

lib/transports/http-polling.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ exports = module.exports = HTTPPolling;
2323
* @api public.
2424
*/
2525

26-
function HTTPPolling (mng, data, req) {
27-
HTTPTransport.call(this, mng, data, req);
26+
function HTTPPolling (mng, data, req, socket) {
27+
HTTPTransport.call(this, mng, data, req, socket);
2828
};
2929

3030
/**
@@ -69,8 +69,8 @@ HTTPPolling.prototype.setHeartbeatInterval = function () {
6969
* @api private
7070
*/
7171

72-
HTTPPolling.prototype.handleRequest = function (req) {
73-
HTTPTransport.prototype.handleRequest.call(this, req);
72+
HTTPPolling.prototype.handleRequest = function (req, socket) {
73+
HTTPTransport.prototype.handleRequest.call(this, req, socket);
7474

7575
if (req.method == 'GET') {
7676
var self = this;

lib/transports/http.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ exports = module.exports = HTTPTransport;
2525
* @api public
2626
*/
2727

28-
function HTTPTransport (mng, data, req) {
29-
Transport.call(this, mng, data, req);
28+
function HTTPTransport (mng, data, req, socket) {
29+
Transport.call(this, mng, data, req, socket);
3030
};
3131

3232
/**
@@ -41,7 +41,7 @@ HTTPTransport.prototype.__proto__ = Transport.prototype;
4141
* @api private
4242
*/
4343

44-
HTTPTransport.prototype.handleRequest = function (req) {
44+
HTTPTransport.prototype.handleRequest = function (req, socket) {
4545

4646
// Always set the response in case an error is returned to the client
4747
this.response = req.res;
@@ -58,7 +58,11 @@ HTTPTransport.prototype.handleRequest = function (req) {
5858

5959
if (Buffer.byteLength(buffer) >= self.manager.get('destroy buffer size')) {
6060
buffer = '';
61-
req.connection.destroy();
61+
if (socket.destroy) {
62+
socket.destroy();
63+
} else if (socket.end) {
64+
socket.end();
65+
}
6266
}
6367
});
6468

@@ -82,7 +86,7 @@ HTTPTransport.prototype.handleRequest = function (req) {
8286
headers['X-XSS-Protection'] = '0';
8387
}
8488
} else {
85-
Transport.prototype.handleRequest.call(this, req);
89+
Transport.prototype.handleRequest.call(this, req, socket);
8690
}
8791
};
8892

lib/transports/jsonp-polling.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ exports = module.exports = JSONPPolling;
2424
* @api public
2525
*/
2626

27-
function JSONPPolling (mng, data, req) {
28-
HTTPPolling.call(this, mng, data, req);
27+
function JSONPPolling (mng, data, req, socket) {
28+
HTTPPolling.call(this, mng, data, req, socket);
2929

3030
this.head = 'io.j[0](';
3131
this.foot = ');';

lib/transports/websocket.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ exports = module.exports = WebSocket;
2424
* @api public
2525
*/
2626

27-
function WebSocket (mng, data, req) {
27+
function WebSocket (mng, data, req, socket) {
2828
var transport
2929
, version = req.headers['sec-websocket-version'];
3030
if (typeof version !== 'undefined' && typeof protocolVersions[version] !== 'undefined') {
31-
transport = new protocolVersions[version](mng, data, req);
31+
transport = new protocolVersions[version](mng, data, req, socket);
3232
}
33-
else transport = new protocolVersions['default'](mng, data, req);
33+
else transport = new protocolVersions['default'](mng, data, req, socket);
3434
if (typeof this.name !== 'undefined') transport.name = this.name;
3535
return transport;
3636
};

lib/transports/websocket/default.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exports = module.exports = WebSocket;
2626
* @api public
2727
*/
2828

29-
function WebSocket (mng, data, req) {
29+
function WebSocket (mng, data, req, socket) {
3030
// parser
3131
var self = this;
3232

@@ -47,7 +47,7 @@ function WebSocket (mng, data, req) {
4747
self.end();
4848
});
4949

50-
Transport.call(this, mng, data, req);
50+
Transport.call(this, mng, data, req, socket);
5151
};
5252

5353
/**

lib/transports/websocket/hybi-07-12.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.Parser = Parser;
3030
* @api public
3131
*/
3232

33-
function WebSocket (mng, data, req) {
33+
function WebSocket (mng, data, req, socket) {
3434
// parser
3535
var self = this;
3636

@@ -62,7 +62,7 @@ function WebSocket (mng, data, req) {
6262
self.end();
6363
});
6464

65-
Transport.call(this, mng, data, req);
65+
Transport.call(this, mng, data, req, socket);
6666
};
6767

6868
/**

lib/transports/websocket/hybi-16.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.Parser = Parser;
2929
* @api public
3030
*/
3131

32-
function WebSocket (mng, data, req) {
32+
function WebSocket (mng, data, req, socket) {
3333
// parser
3434
var self = this;
3535

@@ -61,7 +61,7 @@ function WebSocket (mng, data, req) {
6161
self.end();
6262
});
6363

64-
Transport.call(this, mng, data, req);
64+
Transport.call(this, mng, data, req, socket);
6565
};
6666

6767
/**

lib/transports/xhr-polling.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ exports = module.exports = XHRPolling;
2323
* @api public
2424
*/
2525

26-
function XHRPolling (mng, data, req) {
27-
HTTPPolling.call(this, mng, data, req);
26+
function XHRPolling (mng, data, req, socket) {
27+
HTTPPolling.call(this, mng, data, req, socket);
2828
};
2929

3030
/**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "socket.io"
3-
, "version": "0.9.19-overleaf-9"
3+
, "version": "0.9.19-overleaf-10"
44
, "description": "Real-time apps made cross-browser & easy with a WebSocket-like API"
55
, "homepage": "http://socket.io"
66
, "keywords": ["websocket", "socket", "realtime", "socket.io", "comet", "ajax"]

0 commit comments

Comments
 (0)