[Pkg-javascript-commits] [sockjs-client] 112/350: Add node version of crypto.

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:49 UTC 2016


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit 40433d2ecd89eb42c98c6805ccd5eb01afaf4d2c
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Fri Oct 10 14:59:20 2014 -0400

    Add node version of crypto.
---
 lib/utils/browser-crypto.js | 17 +++++++++++++++++
 lib/utils/random.js         | 20 +++-----------------
 package.json                |  5 +++--
 3 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/lib/utils/browser-crypto.js b/lib/utils/browser-crypto.js
new file mode 100644
index 0000000..32e1217
--- /dev/null
+++ b/lib/utils/browser-crypto.js
@@ -0,0 +1,17 @@
+'use strict';
+
+if (global.crypto && global.crypto.getRandomValues) {
+  module.exports = function (length) {
+    var bytes = new Uint8Array(length);
+    global.crypto.getRandomValues(bytes);
+    return bytes;
+  };
+} else {
+  module.exports = function (length) {
+    var bytes = new Array(length);
+    for (var i = 0; i < length; i++) {
+      bytes[i] = Math.floor(Math.random() * 256);
+    }
+    return bytes;
+  };
+}
diff --git a/lib/utils/random.js b/lib/utils/random.js
index 0e66e09..b760008 100644
--- a/lib/utils/random.js
+++ b/lib/utils/random.js
@@ -1,21 +1,7 @@
 'use strict';
 
-var getRandomBytes;
-if (global && global.crypto && global.crypto.getRandomValues) {
-  getRandomBytes = function (length) {
-    var bytes = new Uint8Array(length);
-    global.crypto.getRandomValues(bytes);
-    return bytes;
-  };
-} else {
-  getRandomBytes = function (length) {
-    var bytes = new Uint8Array(length);
-    for (var i = 0; i < length; i++) {
-      bytes[i] = Math.floor(Math.random() * 256);
-    }
-    return bytes;
-  };
-}
+/* global crypto:true */
+var crypto = require('crypto');
 
 // This string has length 32, a power of 2, so the modulus doesn't introduce a
 // bias.
@@ -23,7 +9,7 @@ var _randomStringChars = 'abcdefghijklmnopqrstuvwxyz012345';
 module.exports = {
   string: function(length) {
     var max = _randomStringChars.length;
-    var bytes = getRandomBytes(length);
+    var bytes = crypto.randomBytes(length);
     var ret = [];
     for (var i = 0; i < length; i++) {
       ret.push( _randomStringChars[bytes[i] % max] );
diff --git a/package.json b/package.json
index 30f73c1..fcc2bf6 100644
--- a/package.json
+++ b/package.json
@@ -4,8 +4,9 @@
   "version": "1.0.0-alpha1",
   "author": "Bryce Kahle",
   "browser": {
-    "./lib/transport/driver/websocket": "./lib/transport/browser/websocket",
-    "./lib/transport/driver/xhr": "./lib/transport/browser/abstract-xhr"
+    "./lib/transport/driver/websocket.js": "./lib/transport/browser/websocket.js",
+    "./lib/transport/driver/xhr.js": "./lib/transport/browser/abstract-xhr.js",
+    "crypto": "./lib/utils/browser-crypto.js"
   },
   "bugs": {
     "url": "https://github.com/sockjs/sockjs-client/issues"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list