Skip to content

Commit c624751

Browse files
[revert] Make generateId method async (#535)
That is a breaking change, which mandates a major bump.
1 parent be3833b commit c624751

File tree

3 files changed

+43
-60
lines changed

3 files changed

+43
-60
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ to a single process.
261261
- Overwrite this method to generate your custom socket id.
262262
- **Parameters**
263263
- `http.IncomingMessage`: a node request object
264-
- `Function`: a callback method which contains an error (if there is) object and the generated id value
264+
- **Returns** A socket id for connected client.
265265

266266
<hr><br>
267267

lib/server.js

+40-44
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ function sendErrorMessage (req, res, code) {
277277
* @api public
278278
*/
279279

280-
Server.prototype.generateId = function (req, callback) {
281-
callback(null, base64id.generateId());
280+
Server.prototype.generateId = function (req) {
281+
return base64id.generateId();
282282
};
283283

284284
/**
@@ -290,56 +290,52 @@ Server.prototype.generateId = function (req, callback) {
290290
*/
291291

292292
Server.prototype.handshake = function (transportName, req) {
293-
var self = this;
294-
this.generateId(req, function (err, id) {
295-
if (err) {
296-
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
297-
return;
298-
}
299-
debug('handshaking client "%s"', id);
300-
301-
try {
302-
var transport = new transports[transportName](req);
303-
if ('polling' === transportName) {
304-
transport.maxHttpBufferSize = self.maxHttpBufferSize;
305-
transport.httpCompression = self.httpCompression;
306-
} else if ('websocket' === transportName) {
307-
transport.perMessageDeflate = self.perMessageDeflate;
308-
}
293+
var id = this.generateId(req);
309294

310-
if (req._query && req._query.b64) {
311-
transport.supportsBinary = false;
312-
} else {
313-
transport.supportsBinary = true;
314-
}
315-
} catch (e) {
316-
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
317-
return;
295+
debug('handshaking client "%s"', id);
296+
297+
try {
298+
var transport = new transports[transportName](req);
299+
if ('polling' === transportName) {
300+
transport.maxHttpBufferSize = this.maxHttpBufferSize;
301+
transport.httpCompression = this.httpCompression;
302+
} else if ('websocket' === transportName) {
303+
transport.perMessageDeflate = this.perMessageDeflate;
318304
}
319-
var socket = new Socket(id, self, transport, req);
320-
321-
if (false !== self.cookie) {
322-
transport.on('headers', function (headers) {
323-
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
324-
{
325-
path: self.cookiePath,
326-
httpOnly: self.cookiePath ? self.cookieHttpOnly : false
327-
});
328-
});
305+
306+
if (req._query && req._query.b64) {
307+
transport.supportsBinary = false;
308+
} else {
309+
transport.supportsBinary = true;
329310
}
311+
} catch (e) {
312+
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
313+
return;
314+
}
315+
var socket = new Socket(id, this, transport, req);
316+
var self = this;
330317

331-
transport.onRequest(req);
318+
if (false !== this.cookie) {
319+
transport.on('headers', function (headers) {
320+
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
321+
{
322+
path: self.cookiePath,
323+
httpOnly: self.cookiePath ? self.cookieHttpOnly : false
324+
});
325+
});
326+
}
332327

333-
self.clients[id] = socket;
334-
self.clientsCount++;
328+
transport.onRequest(req);
335329

336-
socket.once('close', function () {
337-
delete self.clients[id];
338-
self.clientsCount--;
339-
});
330+
this.clients[id] = socket;
331+
this.clientsCount++;
340332

341-
self.emit('connection', socket);
333+
socket.once('close', function () {
334+
delete self.clients[id];
335+
self.clientsCount--;
342336
});
337+
338+
this.emit('connection', socket);
343339
};
344340

345341
/**

test/server.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
/* eslint-disable standard/no-callback-literal */
32

43
/**
@@ -248,8 +247,8 @@ describe('server', function () {
248247

249248
var customId = 'CustomId' + Date.now();
250249

251-
engine.generateId = function (req, callback) {
252-
callback(null, customId);
250+
engine.generateId = function (req) {
251+
return customId;
253252
};
254253

255254
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
@@ -263,18 +262,6 @@ describe('server', function () {
263262
});
264263
});
265264

266-
it('should disallow connection when custom id cannot be generated', function (done) {
267-
let engine = listen({ allowUpgrades: false }, port => {
268-
engine.generateId = (req, callback) => {
269-
callback(new Error('no ID found'));
270-
};
271-
272-
let socket = new eioc.Socket('ws://localhost:%d'.s(port));
273-
socket.on('open', () => done(new Error('should not be able to connect')));
274-
socket.on('error', () => done());
275-
});
276-
});
277-
278265
it('should exchange handshake data', function (done) {
279266
listen({ allowUpgrades: false }, function (port) {
280267
var socket = new eioc.Socket('ws://localhost:%d'.s(port));

0 commit comments

Comments
 (0)