Skip to content

Commit

Permalink
Moved generateId into an external module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Faeldt committed Sep 7, 2012
1 parent da91c89 commit f7d40d5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 83 deletions.
85 changes: 2 additions & 83 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,88 +730,6 @@ Manager.prototype.handleClient = function (data, req) {
}
};

/**
* Get random bytes
*
* @api private
*/

Manager.prototype.getRandomBytes = function(bytes) {

var BUFFER_SIZE = 4096
var self = this;

bytes = bytes || 12;

if (bytes > BUFFER_SIZE) {
return crypto.randomBytes(bytes);
}

var bytesInBuffer = parseInt(BUFFER_SIZE/bytes);
var threshold = parseInt(bytesInBuffer*0.85);

if (!threshold) {
return crypto.randomBytes(bytes);
}

if (this.bytesBufferIndex == null) {
this.bytesBufferIndex = -1;
}

if (this.bytesBufferIndex == bytesInBuffer) {
this.bytesBuffer = null;
this.bytesBufferIndex = -1;
}

// No buffered bytes available or index above threshold
if (this.bytesBufferIndex == -1 || this.bytesBufferIndex > threshold) {

if (!this.isGeneratingBytes) {
this.isGeneratingBytes = true;
crypto.randomBytes(BUFFER_SIZE, function(err, bytes) {
self.bytesBuffer = bytes;
self.bytesBufferIndex = 0;
self.isGeneratingBytes = false;
});
}

// Fall back to sync call when no buffered bytes are available
if (this.bytesBufferIndex == -1) {
return crypto.randomBytes(bytes);
}
}

var result = this.bytesBuffer.slice(bytes*this.bytesBufferIndex, bytes*(this.bytesBufferIndex+1));
this.bytesBufferIndex++;

return result;
}

/**
* 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) {
this.getRandomBytes(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 @@ -856,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 @@ -20,6 +20,7 @@
, "policyfile": "0.0.4"
, "redis": "0.7.2"
, "ws": "~0.4.21"
, "base64id": "0.1.0"
}
, "devDependencies": {
"expresso": "0.9.2"
Expand Down

0 comments on commit f7d40d5

Please sign in to comment.