From dea733911d9ace2164b71863b1903e6f1a1b1ece Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 18 Mar 2016 17:01:00 -0400 Subject: [PATCH] Load crypto functionality on node.js. --- js/src/randomid.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/js/src/randomid.js b/js/src/randomid.js index 9e749c3..18b6061 100644 --- a/js/src/randomid.js +++ b/js/src/randomid.js @@ -8,13 +8,19 @@ if ((typeof window !== 'undefined') && window.crypto.getRandomValues(buf); return btoa(String.fromCharCode.apply(null, buf)).replace(/=/g,''); }; -} else if ((typeof crypto !== 'undefined') && - (typeof crypto.randomBytes !== 'undefined')) { - randomId = function (byteCount) { - return crypto.randomBytes(byteCount).base64Slice().replace(/=/g,''); - }; } else { - console.warn('No suitable implementation for RandomID.randomId available.'); + var crypto; + try { + crypto = require('crypto'); + } catch (e) {} + if ((typeof crypto !== 'undefined') && + (typeof crypto.randomBytes !== 'undefined')) { + randomId = function (byteCount) { + return crypto.randomBytes(byteCount).base64Slice().replace(/=/g,''); + }; + } else { + console.warn('No suitable implementation for RandomID.randomId available.'); + } } module.exports.randomId = randomId;