pax_global_header00006660000000000000000000000064130055170640014513gustar00rootroot0000000000000052 comment=3bc2de784695ddca022d3068231161f50dfb584d uid-safe-2.1.3/000077500000000000000000000000001300551706400132135ustar00rootroot00000000000000uid-safe-2.1.3/.eslintignore000066400000000000000000000000261300551706400157140ustar00rootroot00000000000000coverage node_modules uid-safe-2.1.3/.eslintrc000066400000000000000000000000341300551706400150340ustar00rootroot00000000000000{ "extends": "standard" } uid-safe-2.1.3/.gitignore000066400000000000000000000000561300551706400152040ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage uid-safe-2.1.3/.travis.yml000066400000000000000000000016501300551706400153260ustar00rootroot00000000000000language: 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@2 && cat ./coverage/lcov.info | coveralls" uid-safe-2.1.3/HISTORY.md000066400000000000000000000014321300551706400146760ustar00rootroot000000000000002.1.3 / 2016-10-30 ================== * deps: base64-url@1.3.3 2.1.2 / 2016-08-15 ================== * deps: base64-url@1.3.2 2.1.1 / 2016-05-04 ================== * deps: base64-url@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@1.2.1 1.0.3 / 2015-01-31 ================== * Fix error branch that would throw * deps: base64-url@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 uid-safe-2.1.3/LICENSE000066400000000000000000000022301300551706400142150ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Jonathan Ong Copyright (c) 2015-2016 Douglas Christopher Wilson 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. uid-safe-2.1.3/README.md000066400000000000000000000041551300551706400144770ustar00rootroot00000000000000# 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://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, callback) 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, function (err, string) { if (err) throw err // do something with the string }) ``` ### 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 }) ``` ### uid.sync(byteLength) 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 uid-safe-2.1.3/index.js000066400000000000000000000033061300551706400146620ustar00rootroot00000000000000/*! * uid-safe * Copyright(c) 2014 Jonathan Ong * Copyright(c) 2015-2016 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var escape = require('base64-url').escape var randomBytes = require('random-bytes') /** * Module exports. * @public */ module.exports = uid module.exports.sync = uidSync /** * 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) } return new Promise(function executor (resolve, reject) { generateUid(length, function onUid (err, str) { if (err) return reject(err) resolve(str) }) }) } /** * Create a unique ID sync. * * @param {number} length * @return {string} * @public */ function uidSync (length) { return toString(randomBytes.sync(length)) } /** * 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')) } uid-safe-2.1.3/package.json000066400000000000000000000022741300551706400155060ustar00rootroot00000000000000{ "name": "uid-safe", "description": "URL and cookie safe UIDs", "version": "2.1.3", "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)" ], "license": "MIT", "repository": "crypto-utils/uid-safe", "dependencies": { "base64-url": "1.3.3", "random-bytes": "~1.0.0" }, "devDependencies": { "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": { "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" ] } uid-safe-2.1.3/test/000077500000000000000000000000001300551706400141725ustar00rootroot00000000000000uid-safe-2.1.3/test/.eslintrc000066400000000000000000000000451300551706400160150ustar00rootroot00000000000000{ "env": { "mocha": true } } uid-safe-2.1.3/test/test.js000066400000000000000000000041351300551706400155120ustar00rootroot00000000000000var 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('=')) }) })