Skip to content

Commit

Permalink
Merge pull request socketio#1008 from faeldt/master
Browse files Browse the repository at this point in the history
Increase performance of generateId by avoiding sync calls to crypto.randomBytes
  • Loading branch information
rauchg committed Sep 10, 2012
2 parents 70b328f + f7d40d5 commit c3ba8a7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 26 deletions.
28 changes: 2 additions & 26 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fs = require('fs')
, url = require('url')
, tty = require('tty')
, crypto = require('crypto')
, base64id = require('base64id')
, util = require('./util')
, store = require('./store')
, client = require('socket.io-client')
Expand Down Expand Up @@ -729,31 +730,6 @@ Manager.prototype.handleClient = function (data, req) {
}
};

/**
* Generates a session id.
*
* @api private
*/

Manager.prototype.generateId = function () {
var rand = new Buffer(15); // multiple of 3 for base64
if (!rand.writeInt32BE) {
return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
+ Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
}
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
if (crypto.randomBytes) {
crypto.randomBytes(12).copy(rand);
} else {
// not secure for node 0.4
[0, 4, 8].forEach(function(i) {
rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
});
}
return rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
};

/**
* Handles a handshake request.
*
Expand Down Expand Up @@ -799,7 +775,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
if (err) return error(err);

if (authorized) {
var id = self.generateId()
var id = base64id.generateId()
, hs = [
id
, self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"socket.io-client": "0.9.10"
, "policyfile": "0.0.4"
, "ws": "~0.4.21"
, "base64id": "0.1.0"
}
, "devDependencies": {
"expresso": "0.9.2"
Expand Down

0 comments on commit c3ba8a7

Please sign in to comment.