[Pkg-javascript-commits] [node-randomfill] 02/47: readme

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:52:51 UTC 2017


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

rouca pushed a commit to branch master
in repository node-randomfill.

commit 9e99f197e01a17b47c88ae0e6eaf1a2199737c2a
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Thu Jan 15 09:31:04 2015 -0500

    readme
---
 randomBytes.js |  7 ++++++-
 readme.md      | 12 ++++++++++++
 test.js        | 20 ++++++++++++++++----
 3 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/randomBytes.js b/randomBytes.js
index de5f7b9..7ba1c5c 100644
--- a/randomBytes.js
+++ b/randomBytes.js
@@ -6,13 +6,18 @@ if(crypto.getRandomValues) {
 } else {
   module.exports = oldBrowser;
 }
-function randomBytes(size) {
+function randomBytes(size, cb) {
   var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
     /* This will not work in older browsers.
      * See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
      */
 
   crypto.getRandomValues(bytes);
+  if (typeof cb === 'function') {
+    return process.nextTick(function () {
+      cb(null, bytes);
+    });
+  }
   return bytes;
 }
 function oldBrowser() {
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..01e36fc
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,12 @@
+randombytes
+===
+
+randombytes from node that works in the browser.  In node you just get crypto.randomBytes, but in the browser it uses .crypto/msCrypto.getRandomValues
+
+```js
+var randomBytes = require('randombytes');
+randomBytes(16);//get 16 random bytes
+randomBytes(16, function (err, resp) {
+  // resp is 16 random bytes
+});
+```
\ No newline at end of file
diff --git a/test.js b/test.js
index 061f010..9b44a3b 100644
--- a/test.js
+++ b/test.js
@@ -1,9 +1,21 @@
 var test = require("tape");
 var randomBytes = require('./');
 
-test('correct length', function (t) {
+test('sync', function (t) {
     t.plan(3);
-    t.equals(randomBytes(3).length, 3, 3);
-    t.equals(randomBytes(30).length, 30, 30);
-    t.equals(randomBytes(300).length, 300, 300);
+    t.equals(randomBytes(3).length, 3, 'len: ' + 3);
+    t.equals(randomBytes(30).length, 30, 'len: ' + 30);
+    t.equals(randomBytes(300).length, 300, 'len: ' + 300);
+});
+test('async', function (t) {
+    t.plan(3);
+    randomBytes(3, function (err, resp) {
+      t.equals(resp.length, 3, 'len: ' + 3);
+    });
+    randomBytes(30, function (err, resp) {
+      t.equals(resp.length, 30, 'len: ' + 30);
+    });
+    randomBytes(300, function (err, resp) {
+      t.equals(resp.length, 300, 'len: ' + 300);
+    });
 });
\ No newline at end of file

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



More information about the Pkg-javascript-commits mailing list