Skip to content

Commit 94df7bc

Browse files
[fix] Fix middleware initialization (#2969)
Fix "TypeError: Cannot convert undefined or null to object" when a middleware is added before the engine is properly attached.
1 parent 9a014e2 commit 94df7bc

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

lib/index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,29 +244,43 @@ Server.prototype.attach = function(srv, opts){
244244
// set origins verification
245245
opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);
246246

247-
var self = this;
247+
if (this.sockets.fns.length > 0) {
248+
this.initEngine(srv, opts);
249+
return this;
250+
}
248251

252+
var self = this;
249253
var connectPacket = { type: parser.CONNECT, nsp: '/' };
250254
this.encoder.encode(connectPacket, function (encodedPacket){
251255
// the CONNECT packet will be merged with Engine.IO handshake,
252256
// to reduce the number of round trips
253257
opts.initialPacket = encodedPacket;
254258

255-
// initialize engine
256-
debug('creating engine.io instance with opts %j', opts);
257-
self.eio = engine.attach(srv, opts);
259+
self.initEngine(srv, opts);
260+
});
261+
return this;
262+
};
263+
264+
/**
265+
* Initialize engine
266+
*
267+
* @param {Object} options passed to engine.io
268+
* @api private
269+
*/
258270

259-
// attach static file serving
260-
if (self._serveClient) self.attachServe(srv);
271+
Server.prototype.initEngine = function(srv, opts){
272+
// initialize engine
273+
debug('creating engine.io instance with opts %j', opts);
274+
this.eio = engine.attach(srv, opts);
261275

262-
// Export http server
263-
self.httpServer = srv;
276+
// attach static file serving
277+
if (this._serveClient) this.attachServe(srv);
264278

265-
// bind to engine events
266-
self.bind(self.eio);
267-
});
279+
// Export http server
280+
this.httpServer = srv;
268281

269-
return this;
282+
// bind to engine events
283+
this.bind(this.eio);
270284
};
271285

272286
/**

lib/namespace.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ Namespace.prototype.initAdapter = function(){
9999
*/
100100

101101
Namespace.prototype.use = function(fn){
102-
debug('removing initial packet');
103-
delete this.server.eio.initialPacket;
102+
if (this.server.eio) {
103+
debug('removing initial packet');
104+
delete this.server.eio.initialPacket;
105+
}
104106
this.fns.push(fn);
105107
return this;
106108
};

test/socket.io.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,19 @@ describe('socket.io', function(){
22682268
});
22692269
});
22702270
});
2271+
2272+
it('should disable the merge of handshake packets', function(done){
2273+
var srv = http();
2274+
var sio = io();
2275+
sio.use(function(socket, next){
2276+
next();
2277+
});
2278+
sio.listen(srv);
2279+
var socket = client(srv);
2280+
socket.on('connect', function(){
2281+
done();
2282+
});
2283+
});
22712284
});
22722285

22732286
describe('socket middleware', function(done){

0 commit comments

Comments
 (0)