I ran into a problem using the node-uuid library. I was building with browserify, and I found that despite running in a modern browser that supported the crypto API, it wasn't actually using the crypto.getRandomValues method to generate randomness.
Short answer:
We found that the problem was caused by an incorrect configuration of browserify-shim. In package.json, add the following line:
"browserify-shim": {
"node-uuid": "uuid"
}
Longer answer:
In uuid.js, it has the following initialization:
if (!_rng && _global.crypto && crypto.getRandomValues) {
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
//
// Moderately fast, high quality
var _rnds8 = new Uint8Array(16);
_rng = function whatwgRNG() {
crypto.getRandomValues(_rnds8);
return _rnds8;
};
}
The value of _global
was an anonymous object, and the value of _global.crypto
was undefined. After we added the above configuration, the value of _global
was window
and _global.crypto
had the expected value.
The reason is, that when you configure browserify-shim, we are telling it that the node-uuid library will export the variable uuid
into the calling environment. In a browser, the calling environment will be window
, so when it runs the initialization for node-uuid, it sets things up so that the calling environment is the window.
If you don't configure it as such, then browserify-shim thinks node-uuid isn't returning any values, so the calling environment is set up as an anonymous object.