Skip to content

Commit

Permalink
feat: generateId method can now return a Promise
Browse files Browse the repository at this point in the history
Related:

- #538
- #535
  • Loading branch information
darrachequesne committed Jan 14, 2020
1 parent 33564b2 commit f3c291f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,15 @@ class Server extends EventEmitter {
* @param {Object} request object
* @api private
*/
handshake(transportName, req) {
const id = this.generateId(req);
async handshake(transportName, req) {
let id;
try {
id = await this.generateId(req);
} catch (e) {
debug("error while generating an id");
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
return;
}

debug('handshaking client "%s"', id);

Expand Down
30 changes: 30 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,36 @@ describe("server", function() {
});
});

it("should register a new client with custom id (with a Promise)", function(done) {
const engine = listen({ allowUpgrades: false }, port => {
const customId = "CustomId" + Date.now();

engine.generateId = function() {
return Promise.resolve(customId);
};

const socket = new eioc.Socket("ws://localhost:%d".s(port));
socket.once("open", () => {
expect(socket.id).to.be(customId);
expect(engine.clients[customId].id).to.be(customId);
done();
});
});
});

it("should disallow connection that are rejected by `generateId`", function(done) {
const engine = listen({ allowUpgrades: false }, port => {
engine.generateId = () => {
return Promise.reject(new Error("nope"));
};

const socket = new eioc.Socket("ws://localhost:%d".s(port));
socket.on("error", () => {
done();
});
});
});

it("should exchange handshake data", function(done) {
listen({ allowUpgrades: false }, function(port) {
var socket = new eioc.Socket("ws://localhost:%d".s(port));
Expand Down

0 comments on commit f3c291f

Please sign in to comment.