[Pkg-javascript-commits] [node-random-bytes] 01/02: Import Upstream version 1.0.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Tue Sep 19 20:28:56 UTC 2017


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

alteholz pushed a commit to branch master
in repository node-random-bytes.

commit 44a69026a35062046a7d738b686c6c3bee09e9cf
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Tue Sep 19 22:28:44 2017 +0200

    Import Upstream version 1.0.0
---
 .gitignore   |   7 +++
 .travis.yml  |  20 ++++++
 HISTORY.md   |   4 ++
 LICENSE      |  21 +++++++
 README.md    |  77 ++++++++++++++++++++++++
 index.js     | 101 +++++++++++++++++++++++++++++++
 package.json |  36 +++++++++++
 test/test.js | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 460 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..226f465
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+
+.DS_Store*
+*.log
+*.gz
+
+node_modules
+coverage
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..0280df8
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,20 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.12"
+  - "1.8"
+  - "2.5"
+  - "3.3"
+  - "4.2"
+  - "5.4"
+sudo: false
+before_install:
+  # Setup Node.js version-specific dependencies
+  - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
+script:
+  # Run test script, depending on istanbul install
+  - "test ! -z $(npm -ps ls istanbul) || npm test"
+  - "test   -z $(npm -ps ls istanbul) || npm run-script test-travis"
+after_script:
+  - "test -e ./coverage/lcov.info && npm install coveralls at 2 && cat ./coverage/lcov.info | coveralls"
diff --git a/HISTORY.md b/HISTORY.md
new file mode 100644
index 0000000..8cabd9d
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,4 @@
+1.0.0 / 2016-01-17
+==================
+
+  * Initial release
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c24dbe3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Douglas Christopher Wilson <doug at somethingdoug.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..df5aacc
--- /dev/null
+++ b/README.md
@@ -0,0 +1,77 @@
+# random-bytes
+
+[![NPM Version][npm-image]][npm-url]
+[![NPM Downloads][downloads-image]][downloads-url]
+[![Node.js Version][node-version-image]][node-version-url]
+[![Build Status][travis-image]][travis-url]
+[![Test Coverage][coveralls-image]][coveralls-url]
+
+Generate strong pseudo-random bytes.
+
+This module is a simple wrapper around the Node.js core `crypto.randomBytes` API,
+with the following additions:
+
+  * A `Promise` interface for environments with promises.
+  * For Node.js versions that do not wait for the PRNG to be seeded, this module
+    will wait a bit.
+
+## Installation
+
+```sh
+$ npm install random-bytes
+```
+
+## API
+
+```js
+var randomBytes = require('random-bytes')
+```
+
+### randomBytes(size, callback)
+
+Generates strong pseudo-random bytes. The `size` argument is a number indicating
+the number of bytes to generate.
+
+```js
+randomBytes(12, function (error, bytes) {
+  if (error) throw error
+  // do something with the bytes
+})
+```
+
+### randomBytes(size)
+
+Generates strong pseudo-random bytes and return a `Promise`. The `size` argument is
+a number indicating the number of bytes to generate.
+
+**Note**: To use promises in Node.js _prior to 0.12_, promises must be
+"polyfilled" using `global.Promise = require('bluebird')`.
+
+```js
+randomBytes(18).then(function (string) {
+  // do something with the string
+})
+```
+
+### randomBytes.sync(size)
+
+A synchronous version of above.
+
+```js
+var bytes = randomBytes.sync(18)
+```
+
+## License
+
+[MIT](LICENSE)
+
+[npm-image]: https://img.shields.io/npm/v/random-bytes.svg
+[npm-url]: https://npmjs.org/package/random-bytes
+[node-version-image]: https://img.shields.io/node/v/random-bytes.svg
+[node-version-url]: http://nodejs.org/download/
+[travis-image]: https://img.shields.io/travis/crypto-utils/random-bytes/master.svg
+[travis-url]: https://travis-ci.org/crypto-utils/random-bytes
+[coveralls-image]: https://img.shields.io/coveralls/crypto-utils/random-bytes/master.svg
+[coveralls-url]: https://coveralls.io/r/crypto-utils/random-bytes?branch=master
+[downloads-image]: https://img.shields.io/npm/dm/random-bytes.svg
+[downloads-url]: https://npmjs.org/package/random-bytes
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..9ad930f
--- /dev/null
+++ b/index.js
@@ -0,0 +1,101 @@
+/*!
+ * random-bytes
+ * Copyright(c) 2016 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+'use strict'
+
+/**
+ * Module dependencies.
+ * @private
+ */
+
+var crypto = require('crypto')
+
+/**
+ * Module variables.
+ * @private
+ */
+
+var generateAttempts = crypto.randomBytes === crypto.pseudoRandomBytes ? 1 : 3
+
+/**
+ * Module exports.
+ * @public
+ */
+
+module.exports = randomBytes
+module.exports.sync = randomBytesSync
+
+/**
+ * Generates strong pseudo-random bytes.
+ *
+ * @param {number} size
+ * @param {function} [callback]
+ * @return {Promise}
+ * @public
+ */
+
+function randomBytes(size, callback) {
+  // validate callback is a function, if provided
+  if (callback !== undefined && typeof callback !== 'function') {
+    throw new TypeError('argument callback must be a function')
+  }
+
+  // require the callback without promises
+  if (!callback && !global.Promise) {
+    throw new TypeError('argument callback is required')
+  }
+
+  if (callback) {
+    // classic callback style
+    return generateRandomBytes(size, generateAttempts, callback)
+  }
+
+  return new Promise(function executor(resolve, reject) {
+    generateRandomBytes(size, generateAttempts, function onRandomBytes(err, str) {
+      if (err) return reject(err)
+      resolve(str)
+    })
+  })
+}
+
+/**
+ * Generates strong pseudo-random bytes sync.
+ *
+ * @param {number} size
+ * @return {Buffer}
+ * @public
+ */
+
+function randomBytesSync(size) {
+  var err = null
+
+  for (var i = 0; i < generateAttempts; i++) {
+    try {
+      return crypto.randomBytes(size)
+    } catch (e) {
+      err = e
+    }
+  }
+
+  throw err
+}
+
+/**
+ * Generates strong pseudo-random bytes.
+ *
+ * @param {number} size
+ * @param {number} attempts
+ * @param {function} callback
+ * @private
+ */
+
+function generateRandomBytes(size, attempts, callback) {
+  crypto.randomBytes(size, function onRandomBytes(err, buf) {
+    if (!err) return callback(null, buf)
+    if (!--attempts) return callback(err)
+    setTimeout(generateRandomBytes.bind(null, size, attempts, callback), 10)
+  })
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c67e0e8
--- /dev/null
+++ b/package.json
@@ -0,0 +1,36 @@
+{
+  "name": "random-bytes",
+  "description": "URL and cookie safe UIDs",
+  "version": "1.0.0",
+  "contributors": [
+    "Douglas Christopher Wilson <doug at somethingdoug.com>"
+  ],
+  "license": "MIT",
+  "repository": "crypto-utils/random-bytes",
+  "devDependencies": {
+    "bluebird": "3.1.1",
+    "istanbul": "0.4.2",
+    "mocha": "2.3.4",
+    "proxyquire": "1.2.0"
+  },
+  "files": [
+    "LICENSE",
+    "HISTORY.md",
+    "README.md",
+    "index.js"
+  ],
+  "engines": {
+    "node": ">= 0.8"
+  },
+  "scripts": {
+    "test": "mocha --trace-deprecation --reporter spec --bail --check-leaks test/",
+    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --trace-deprecation --reporter dot --check-leaks test/",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --trace-deprecation --reporter spec --check-leaks test/"
+  },
+  "keywords": [
+    "bytes",
+    "generator",
+    "random",
+    "safe"
+  ]
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..ec7ba8e
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,194 @@
+var assert = require('assert')
+var crypto = require('crypto')
+var proxyquire = require('proxyquire')
+
+var Promise = global.Promise || require('bluebird')
+
+var randomBytes = proxyquire('..', {
+  crypto: {
+    randomBytes: cryptoRandomBytes
+  }
+})
+
+// Add Promise to mocha's global list
+global.Promise = global.Promise
+
+describe('randomBytes(size)', function () {
+  describe('with global Promise', function () {
+    before(function () {
+      global.Promise = Promise
+    })
+
+    after(function () {
+      global.Promise = undefined
+    })
+
+    it('should return a Buffer of the correct length', function () {
+      return randomBytes(18).then(function (buf) {
+        assert.ok(Buffer.isBuffer(buf))
+        assert.equal(18, buf.length)
+      })
+    })
+
+    describe('when PRNG not seeded', function () {
+      afterEach(function () {
+        cryptoRandomBytes.seeded = undefined
+      })
+
+      it('should still generate bytes when later seeded', function () {
+        cryptoRandomBytes.seeded = 1
+
+        return randomBytes(18).then(function (buf) {
+          assert.ok(Buffer.isBuffer(buf))
+          assert.equal(18, buf.length)
+        })
+      })
+
+      it('should attempt generation three times', function () {
+        cryptoRandomBytes.seeded = 2
+
+        return randomBytes(18).then(function (buf) {
+          assert.ok(Buffer.isBuffer(buf))
+          assert.equal(18, buf.length)
+        })
+      })
+
+      it('should reject if never seeded', function () {
+        cryptoRandomBytes.seeded = false
+
+        return randomBytes(18).then(unexpectedResolve, function (err) {
+          assert.notEqual(err.message.indexOf('PRNG not seeded'), -1)
+        })
+      })
+    })
+  })
+
+  describe('without global Promise', function () {
+    before(function () {
+      global.Promise = undefined
+    })
+
+    after(function () {
+      global.Promise = Promise
+    })
+
+    it('should require callback', function () {
+      assert.throws(function () {
+        randomBytes(18)
+      }, /argument callback.*required/)
+    })
+
+    it('should error for bad callback', function () {
+      assert.throws(function () {
+        randomBytes(18, 'silly')
+      }, /argument callback.*function/)
+    })
+
+    it('should return a Buffer of the correct length', function (done) {
+      randomBytes(18, function (err, buf) {
+        if (err) return done(err)
+        assert.ok(Buffer.isBuffer(buf))
+        assert.equal(18, buf.length)
+        done()
+      })
+    })
+
+    describe('when PRNG not seeded', function () {
+      afterEach(function () {
+        cryptoRandomBytes.seeded = undefined
+      })
+
+      it('should still generate bytes when later seeded', function () {
+        cryptoRandomBytes.seeded = 1
+
+        randomBytes(18, function (err, buf) {
+          if (err) return done(err)
+          assert.ok(Buffer.isBuffer(buf))
+          assert.equal(18, buf.length)
+          done()
+        })
+      })
+
+      it('should attempt generation three times', function () {
+        cryptoRandomBytes.seeded = 2
+
+        randomBytes(18, function (err, buf) {
+          if (err) return done(err)
+          assert.ok(Buffer.isBuffer(buf))
+          assert.equal(18, buf.length)
+          done()
+        })
+      })
+
+      it('should error if never seeded', function () {
+        cryptoRandomBytes.seeded = false
+
+        randomBytes(18, function (err) {
+          assert.ok(err)
+          assert.notEqual(err.message.indexOf('PRNG not seeded'), -1)
+          done()
+        })
+      })
+    })
+  })
+})
+
+describe('randomBytes.sync()', function () {
+  it('should return a Buffer of the correct length', function () {
+    var buf = randomBytes.sync(18)
+    assert.ok(Buffer.isBuffer(buf))
+    assert.equal(18, buf.length)
+  })
+
+  describe('when PRNG not seeded', function () {
+    afterEach(function () {
+      cryptoRandomBytes.seeded = undefined
+    })
+
+    it('should still generate bytes when later seeded', function () {
+      cryptoRandomBytes.seeded = 1
+
+      var buf = randomBytes.sync(18)
+      assert.ok(Buffer.isBuffer(buf))
+      assert.equal(18, buf.length)
+    })
+
+    it('should attempt generation three times', function () {
+      cryptoRandomBytes.seeded = 2
+
+      var buf = randomBytes.sync(18)
+      assert.ok(Buffer.isBuffer(buf))
+      assert.equal(18, buf.length)
+    })
+
+    it('should error if never seeded', function () {
+      cryptoRandomBytes.seeded = false
+
+      assert.throws(randomBytes.sync.bind(randomBytes, 18), /PRNG not seeded/)
+    })
+  })
+})
+
+function cryptoRandomBytes(size, callback) {
+  if (cryptoRandomBytes.seeded === undefined) {
+    return crypto.randomBytes(size, callback)
+  }
+
+  // The crazy not seeded error
+  var err = new Error('error:24064064:random number generator:SSLEAY_RAND_BYTES:PRNG not seeded')
+
+  if (typeof cryptoRandomBytes.seeded === 'number' && !--cryptoRandomBytes.seeded) {
+    // Reset seeded state
+    cryptoRandomBytes.seeded = undefined
+  }
+
+  if (!callback) {
+    throw err
+  }
+
+  callback(err)
+}
+
+function unexpectedResolve() {
+  throw new Error('unexpected resolve')
+}

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



More information about the Pkg-javascript-commits mailing list