Skip to content

Commit

Permalink
Adding node 0.4 backward compat for id gen
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthomson committed Apr 26, 2012
1 parent f850ddc commit aaad106
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,14 @@ Manager.prototype.generateId = function () {
var rand = new Buffer(15); // multiple of 3 for base64
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
crypto.randomBytes(12).copy(rand);
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, '-');
};

Expand Down

0 comments on commit aaad106

Please sign in to comment.