Skip to content

Commit

Permalink
Merge pull request #3 from mgesmundo/rfc4122uuid
Browse files Browse the repository at this point in the history
Request id generation with rigorous implementation of RFC4122 UUIDs
  • Loading branch information
swissmanu committed Jun 3, 2014
2 parents 9346b0f + 3b6074c commit 443a4fa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- 0.8
- 0.10.28

script: "make lint test"
46 changes: 9 additions & 37 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global Primus, window */
/* global Primus */

var uuid = require('node-uuid'),
fs = require('fs');

function responder() {

Expand Down Expand Up @@ -130,48 +132,18 @@ Primus.prototype.writeAndWait = function writeAndWait(data, callback) {


/* jshint latedef: false */
var generateGUID = (typeof(window) !== 'undefined' &&
typeof(window.crypto) !== 'undefined' &&
typeof(window.crypto.getRandomValues) !== 'undefined') ?
function() {
// If we have a cryptographically secure PRNG, use that
// http://stackoverflow.com/questions/6906916
// /collisions-when-generating-uuids-in-javascript
var buf = new Uint16Array(8);
window.crypto.getRandomValues(buf);
var S4 = function(num) {
var ret = num.toString(16);
while(ret.length < 4){
ret = "0"+ret;
}
return ret;
};

return (
S4(buf[0])+S4(buf[1])+"-"+S4(buf[2])+"-"+S4(buf[3])+"-"+
S4(buf[4])+"-"+S4(buf[5])+S4(buf[6])+S4(buf[7])
);
}

:

function() {
// Otherwise, just use Math.random
// http://stackoverflow.com/questions/105034
// /how-to-create-a-guid-uuid-in-javascript/2117523#2117523
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
}
);
};
var generateGUID = function (){
return uuid.v4();
};

}

var uuidSource = fs.readFileSync(require.resolve('node-uuid'), 'utf-8');

responder.source = [
';(function (Primus, undefined) {',
'if (undefined === Primus) return;',
uuidSource,
responder.toString(),
'responder();',
'})(Primus);'
Expand Down
17 changes: 2 additions & 15 deletions lib/server/spark.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var debug = require('debug')('primus-responder.spark')
, crypto = require('crypto');
, uuid = require('node-uuid');

/** PrivateFunction: generateGuid()
* Generates a GUID (globally unique identifier) using node.js' crypto library.
Expand All @@ -12,20 +12,7 @@ var debug = require('debug')('primus-responder.spark')
* (String)
*/
function generateGUID() {
var buf = new Uint16Array(8);
buf = crypto.randomBytes(8);
var S4 = function(num) {
var ret = num.toString(16);
while(ret.length < 4){
ret = "0"+ret;
}
return ret;
};

return (
S4(buf[0])+S4(buf[1])+"-"+S4(buf[2])+"-"+S4(buf[3])+"-"+
S4(buf[4])+"-"+S4(buf[5])+S4(buf[6])+S4(buf[7])
);
return uuid.v4();
}

/** Function: initialise
Expand Down
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
"url": "https://github.com/swissmanu/primus-responder/issues"
},
"dependencies": {
"debug": "~0.7.2"
"debug": "~0.8.1",
"node-uuid": "~1.4.1"
}
, "devDependencies": {
"primus": "~1.4.0",
"jshint": "~2.1.10",
"mocha": "~1.12.1",
"ws": "~0.4.30",
"expect.js": "~0.2.0"
"primus": "~2.2.4",
"jshint": "~2.5.1",
"mocha": "~1.19.0",
"ws": "~0.4.31",
"expect.js": "~0.3.1"
},
"engines": {
"node": ">=0.10.28"
}
}

0 comments on commit 443a4fa

Please sign in to comment.