[Pkg-javascript-commits] [node-uid-safe] 01/06: Imported Upstream version 2.1.3

Paolo Greppi paolog-guest at moszumanska.debian.org
Thu Dec 15 12:52:24 UTC 2016


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

paolog-guest pushed a commit to branch master
in repository node-uid-safe.

commit e1f4885226d4d40b1aae47078de7e762dffbafd5
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Thu Dec 15 11:42:21 2016 +0100

    Imported Upstream version 2.1.3
---
 .eslintignore  |  2 ++
 .eslintrc      |  3 ++
 .gitignore     |  7 +++++
 .travis.yml    | 29 ++++++++++++++++++
 HISTORY.md     | 51 +++++++++++++++++++++++++++++++
 LICENSE        |  4 +--
 README.md      | 63 ++++++++++++++++++++++++++++----------
 index.js       | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 package.json   | 47 +++++++++++++++++++++--------
 test.js        | 46 ----------------------------
 test/.eslintrc |  5 ++++
 test/test.js   | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 12 files changed, 350 insertions(+), 89 deletions(-)

diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 0000000..62562b7
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1,2 @@
+coverage
+node_modules
diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..e3578aa
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,3 @@
+{
+  "extends": "standard"
+}
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..26feb28
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,29 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.12"
+  - "1.8"
+  - "2.5"
+  - "3.3"
+  - "4.6"
+  - "5.12"
+  - "6.9"
+sudo: false
+cache:
+  directories:
+    - node_modules
+before_install:
+  # Setup Node.js version-specific dependencies
+  - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
+  - "test $(echo $TRAVIS_NODE_VERSION | cut -d'.' -f1) -ge 4 || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard"
+  # Update Node.js modules
+  - "test ! -d node_modules || npm prune"
+  - "test ! -d node_modules || npm rebuild"
+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"
+  - "test   -z $(npm -ps ls eslint  ) || npm run-script lint"
+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..ae0ff05
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,51 @@
+2.1.3 / 2016-10-30
+==================
+
+  * deps: base64-url at 1.3.3
+
+2.1.2 / 2016-08-15
+==================
+
+  * deps: base64-url at 1.3.2
+
+2.1.1 / 2016-05-04
+==================
+
+  * deps: base64-url at 1.2.2
+
+2.1.0 / 2016-01-17
+==================
+
+  * Use `random-bytes` for byte source
+
+2.0.0 / 2015-05-08
+==================
+
+  * Use global `Promise` when returning a promise
+
+1.1.0 / 2015-02-01
+==================
+
+  * Use `crypto.randomBytes`, if available
+  * deps: base64-url at 1.2.1
+
+1.0.3 / 2015-01-31
+==================
+
+  * Fix error branch that would throw
+  * deps: base64-url at 1.2.0
+
+1.0.2 / 2015-01-08
+==================
+
+  * Remove dependency on `mz`
+
+1.0.1 / 2014-06-18
+==================
+
+  * Remove direct `bluebird` dependency
+
+1.0.0 / 2014-06-18
+==================
+
+  * Initial release
diff --git a/LICENSE b/LICENSE
index a7ae8ee..7a7a4e1 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,7 +1,7 @@
-
 The MIT License (MIT)
 
-Copyright (c) 2014 Jonathan Ong me at jongleberry.com
+Copyright (c) 2014 Jonathan Ong <me at jongleberry.com>
+Copyright (c) 2015-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
diff --git a/README.md b/README.md
index 2178c6c..fa02be8 100644
--- a/README.md
+++ b/README.md
@@ -1,36 +1,54 @@
+# uid-safe
 
-# UID Safe
+[![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]
+
+URL and cookie safe UIDs
 
 Create cryptographically secure UIDs safe for both cookie and URL usage.
-This is in contrast to modules such as [rand-token](https://github.com/sehrope/node-rand-token)
-and [uid2](https://github.com/coreh/uid2) whose UIDs are actually skewed
+This is in contrast to modules such as [rand-token](https://www.npmjs.com/package/rand-token)
+and [uid2](https://www.npmjs.com/package/uid2) whose UIDs are actually skewed
 due to the use of `%` and unnecessarily truncate the UID.
 Use this if you could still use UIDs with `-` and `_` in them.
 
+## Installation
+
+```sh
+$ npm install uid-safe
+```
+
 ## API
 
 ```js
 var uid = require('uid-safe')
 ```
 
-### uid(byteLength, [cb])
-
-Asynchronously create a UID with a specific byte length.
-Because `base64` encoding is used underneath, this is not the string length!
-For example, to create a UID of length 24, you want a byte length of 18!
+### uid(byteLength, callback)
 
-If `cb` is not defined, a promise is returned.
-However, to use promises, you must either install [bluebird](https://github.com/petkaantonov/bluebird)
-or use a version of node.js that has native promises,
-otherwise your process will crash and die.
+Asynchronously create a UID with a specific byte length. Because `base64`
+encoding is used underneath, this is not the string length. For example,
+to create a UID of length 24, you want a byte length of 18.
 
 ```js
-uid(18).then(function (string) {
+uid(18, function (err, string) {
+  if (err) throw err
   // do something with the string
 })
+```
 
-uid(18, function (err, string) {
-  if (err) throw err
+### uid(byteLength)
+
+Asynchronously create a UID with a specific byte length and return a
+`Promise`.
+
+**Note**: To use promises in Node.js _prior to 0.12_, promises must be
+"polyfilled" using `global.Promise = require('bluebird')`.
+
+```js
+uid(18).then(function (string) {
   // do something with the string
 })
 ```
@@ -42,3 +60,18 @@ A synchronous version of above.
 ```js
 var string = uid.sync(18)
 ```
+
+## License
+
+[MIT](LICENSE)
+
+[npm-image]: https://img.shields.io/npm/v/uid-safe.svg
+[npm-url]: https://npmjs.org/package/uid-safe
+[node-version-image]: https://img.shields.io/node/v/uid-safe.svg
+[node-version-url]: https://nodejs.org/en/download/
+[travis-image]: https://img.shields.io/travis/crypto-utils/uid-safe/master.svg
+[travis-url]: https://travis-ci.org/crypto-utils/uid-safe
+[coveralls-image]: https://img.shields.io/coveralls/crypto-utils/uid-safe/master.svg
+[coveralls-url]: https://coveralls.io/r/crypto-utils/uid-safe?branch=master
+[downloads-image]: https://img.shields.io/npm/dm/uid-safe.svg
+[downloads-url]: https://npmjs.org/package/uid-safe
diff --git a/index.js b/index.js
index 9e26461..92ab744 100644
--- a/index.js
+++ b/index.js
@@ -1,27 +1,96 @@
+/*!
+ * uid-safe
+ * Copyright(c) 2014 Jonathan Ong
+ * Copyright(c) 2015-2016 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+'use strict'
+
+/**
+ * Module dependencies.
+ * @private
+ */
 
-var pseudoRandomBytes = require('crypto').pseudoRandomBytes
 var escape = require('base64-url').escape
+var randomBytes = require('random-bytes')
 
-var pseudoRandomBytesProm
+/**
+ * Module exports.
+ * @public
+ */
 
 module.exports = uid
+module.exports.sync = uidSync
 
-function uid(length, cb) {
-  if (cb) {
-    return pseudoRandomBytes(length, function (err, buf) {
-      if (err) return cb(err)
-      cb(null, escapeBuffer(buf))
-    })
+/**
+ * Create a unique ID.
+ *
+ * @param {number} length
+ * @param {function} [callback]
+ * @return {Promise}
+ * @public
+ */
+
+function uid (length, 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 generateUid(length, callback)
   }
 
-  pseudoRandomBytesProm || (pseudoRandomBytesProm = require('mz/crypto').pseudoRandomBytes)
-  return pseudoRandomBytesProm(length).then(escapeBuffer)
+  return new Promise(function executor (resolve, reject) {
+    generateUid(length, function onUid (err, str) {
+      if (err) return reject(err)
+      resolve(str)
+    })
+  })
 }
 
-uid.sync = function uid_sync(length) {
-  return escapeBuffer(pseudoRandomBytes(length))
+/**
+ * Create a unique ID sync.
+ *
+ * @param {number} length
+ * @return {string}
+ * @public
+ */
+
+function uidSync (length) {
+  return toString(randomBytes.sync(length))
 }
 
-function escapeBuffer(buf) {
+/**
+ * Generate a unique ID string.
+ *
+ * @param {number} length
+ * @param {function} callback
+ * @private
+ */
+
+function generateUid (length, callback) {
+  randomBytes(length, function (err, buf) {
+    if (err) return callback(err)
+    callback(null, toString(buf))
+  })
+}
+
+/**
+ * Change a Buffer into a string.
+ *
+ * @param {Buffer} buf
+ * @return {string}
+ * @private
+ */
+
+function toString (buf) {
   return escape(buf.toString('base64'))
 }
diff --git a/package.json b/package.json
index f82510d..4843f50 100644
--- a/package.json
+++ b/package.json
@@ -1,24 +1,45 @@
 {
   "name": "uid-safe",
   "description": "URL and cookie safe UIDs",
-  "version": "1.0.1",
-  "author": {
-    "name": "Jonathan Ong",
-    "email": "me at jongleberry.com",
-    "url": "http://jongleberry.com",
-    "twitter": "https://twitter.com/jongleberry"
-  },
+  "version": "2.1.3",
+  "contributors": [
+    "Douglas Christopher Wilson <doug at somethingdoug.com>",
+    "Jonathan Ong <me at jongleberry.com> (http://jongleberry.com)"
+  ],
   "license": "MIT",
   "repository": "crypto-utils/uid-safe",
   "dependencies": {
-    "mz": "1",
-    "base64-url": "1"
+    "base64-url": "1.3.3",
+    "random-bytes": "~1.0.0"
   },
   "devDependencies": {
-    "bluebird": "2",
-    "mocha": "1"
+    "bluebird": "3.4.6",
+    "eslint": "3.9.0",
+    "eslint-config-standard": "6.2.1",
+    "eslint-plugin-promise": "3.3.0",
+    "eslint-plugin-standard": "2.0.1",
+    "istanbul": "0.4.5",
+    "mocha": "2.5.3"
+  },
+  "files": [
+    "LICENSE",
+    "HISTORY.md",
+    "README.md",
+    "index.js"
+  ],
+  "engines": {
+    "node": ">= 0.8"
   },
   "scripts": {
-    "test": "mocha --reporter spec --bail"
-  }
+    "lint": "eslint .",
+    "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": [
+    "random",
+    "generator",
+    "uid",
+    "safe"
+  ]
 }
diff --git a/test.js b/test.js
deleted file mode 100644
index 3665fb9..0000000
--- a/test.js
+++ /dev/null
@@ -1,46 +0,0 @@
-
-var assert = require('assert')
-
-var uid = require('./')
-
-describe('uid-url', function () {
-  describe('uid()', function () {
-    it('should return a uid of the correct length', function () {
-      return uid(18).then(function (val) {
-        assert.equal(24, Buffer.byteLength(val))
-      })
-    })
-
-    it('should not contain +, /, or =', function () {
-      return uid(100000).then(function (val) {
-        assert(!~val.indexOf('+'))
-        assert(!~val.indexOf('/'))
-        assert(!~val.indexOf('='))
-      })
-    })
-
-    it('should support callbacks', function (done) {
-      uid(1000000, function (err, val) {
-        if (err) return done(err)
-        assert(!~val.indexOf('+'))
-        assert(!~val.indexOf('/'))
-        assert(!~val.indexOf('='))
-        done()
-      })
-    })
-  })
-
-  describe('uid.sync()', function () {
-    it('should return a uid of the correct length', function () {
-      var val = uid.sync(18)
-      assert.equal(24, Buffer.byteLength(val))
-    })
-
-    it('should not contain +, /, or =', function () {
-      var val = uid.sync(100000)
-      assert(!~val.indexOf('+'))
-      assert(!~val.indexOf('/'))
-      assert(!~val.indexOf('='))
-    })
-  })
-})
diff --git a/test/.eslintrc b/test/.eslintrc
new file mode 100644
index 0000000..7eeefc3
--- /dev/null
+++ b/test/.eslintrc
@@ -0,0 +1,5 @@
+{
+  "env": {
+    "mocha": true
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..dd8e939
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,87 @@
+var assert = require('assert')
+var uid = require('..')
+
+var Promise = global.Promise || require('bluebird')
+
+// Add Promise to mocha's global list
+global.Promise = global.Promise
+
+describe('uid()', function () {
+  describe('with global Promise', function () {
+    before(function () {
+      global.Promise = Promise
+    })
+
+    after(function () {
+      global.Promise = undefined
+    })
+
+    it('should return a uid of the correct length', function () {
+      return uid(18).then(function (val) {
+        assert.equal(24, Buffer.byteLength(val))
+      })
+    })
+
+    it('should not contain +, /, or =', function () {
+      return uid(100000).then(function (val) {
+        assert(!~val.indexOf('+'))
+        assert(!~val.indexOf('/'))
+        assert(!~val.indexOf('='))
+      })
+    })
+  })
+
+  describe('without global Promise', function () {
+    before(function () {
+      global.Promise = undefined
+    })
+
+    after(function () {
+      global.Promise = Promise
+    })
+
+    it('should require callback', function () {
+      assert.throws(function () {
+        uid(18)
+      }, /argument callback.*required/)
+    })
+
+    it('should error for bad callback', function () {
+      assert.throws(function () {
+        uid(18, 'silly')
+      }, /argument callback.*function/)
+    })
+
+    it('should return a uid of the correct length', function (done) {
+      uid(18, function (err, val) {
+        if (err) return done(err)
+        assert.equal(24, Buffer.byteLength(val))
+        done()
+      })
+    })
+
+    it('should not contain +, /, or =', function (done) {
+      uid(1000000, function (err, val) {
+        if (err) return done(err)
+        assert(!~val.indexOf('+'))
+        assert(!~val.indexOf('/'))
+        assert(!~val.indexOf('='))
+        done()
+      })
+    })
+  })
+})
+
+describe('uid.sync()', function () {
+  it('should return a uid of the correct length', function () {
+    var val = uid.sync(18)
+    assert.equal(24, Buffer.byteLength(val))
+  })
+
+  it('should not contain +, /, or =', function () {
+    var val = uid.sync(100000)
+    assert(!~val.indexOf('+'))
+    assert(!~val.indexOf('/'))
+    assert(!~val.indexOf('='))
+  })
+})

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



More information about the Pkg-javascript-commits mailing list