base62-random
Advanced tools
Comparing version 0.1.1-2 to 0.1.1-3
78
index.js
@@ -1,13 +0,15 @@ | ||
"use strict"; | ||
'use strict'; | ||
/* global window */ | ||
(function(){ | ||
var buf; | ||
var str = ''; | ||
var strIdx = 0; | ||
var i; | ||
var buf | ||
var str = '' | ||
var strIdx = 0 | ||
var i | ||
var chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
// Reduce calls to crypto by increasing this numberr (>=16) | ||
// Reduce calls to `crypto` by increasing this number (>=16) | ||
// Uses a tiny bit more memory to store the random bytes (try 16384) | ||
var BUFFER_SIZE = 8192; | ||
var BUFFER_SIZE = 8192 | ||
@@ -18,16 +20,21 @@ | ||
// Test for uuid | ||
base62.test = isbase62; | ||
base62.test = isbase62 | ||
base62.generateBase62Math = generateBase62Math | ||
base62.generateBase62Node = generateBase62Node | ||
base62.generateBase62Browser = generateBase62Browser | ||
base62.initMath = initMath | ||
base62.initNode = initNode | ||
base62.initBrowser = initBrowser | ||
// Node & Browser support | ||
if ((typeof module !== 'undefined') && (typeof require === 'function')) { | ||
var crypto = require('crypto'); | ||
module.exports = base62; | ||
var crypto = require('crypto') | ||
module.exports = base62 | ||
} else if (typeof window !== 'undefined') { | ||
window.base62 = base62; | ||
window.base62 = base62 | ||
} | ||
// Backup method | ||
var chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
function getRandomChar() { | ||
return chars[Math.floor(Math.random() * (62 - 0)) + 0]; | ||
return chars[Math.floor(Math.random() * (62 - 0)) + 0] | ||
} | ||
@@ -38,3 +45,3 @@ | ||
if (typeof str === 'string') { | ||
return /^[0-9a-zA-Z]+$/.test(str); | ||
return /^[0-9a-zA-Z]+$/.test(str) | ||
} | ||
@@ -44,9 +51,8 @@ return false | ||
function generateBase62Math(){ | ||
for (i = 0; i < BUFFER_SIZE; i++) { | ||
buf[i] = getRandomChar(); | ||
buf[i] = getRandomChar() | ||
} | ||
str = buf.join('') | ||
strIdx = 0 | ||
return str = buf.join('') | ||
} | ||
@@ -56,4 +62,4 @@ | ||
//console.error('generating str',strIdx) | ||
str = crypto.randomBytes(BUFFER_SIZE).toString('base64').replace(/[\+\=\/]/g,''); | ||
strIdx = 0 | ||
return str = crypto.randomBytes(BUFFER_SIZE).toString('base64').replace(/[\+\=\/]/g,'') | ||
} | ||
@@ -63,28 +69,42 @@ | ||
function generateBase62Browser(){ | ||
buf = crypto.getRandomValues(buf); | ||
buf = crypto.getRandomValues(buf) | ||
var tmp = Array(BUFFER_SIZE) | ||
for (i=0; i<BUFFER_SIZE; i++){ | ||
tmp.push(chars[buf[i] % 62]); | ||
// wastes some bits, some bit pushing should save the extra 4 | ||
tmp.push(chars[buf[i] % 62]) | ||
} | ||
str = tmp.join(''); | ||
strIdx = 0 | ||
return str = tmp.join('') | ||
} | ||
// Use best RNG as possible | ||
var generateBase62; | ||
var generateBase62 | ||
strIdx = BUFFER_SIZE | ||
function initMath(){ | ||
str = '' | ||
buf = new Array(BUFFER_SIZE) | ||
generateBase62 = generateBase62Math | ||
} | ||
function initBrowser(){ | ||
str = '' | ||
buf = new Uint8Array(BUFFER_SIZE) | ||
generateBase62 = generateBase62Browser | ||
} | ||
function initNode(){ | ||
str = '' | ||
generateBase62 = generateBase62Node | ||
} | ||
if (typeof crypto === 'undefined') { | ||
buf = new Array(BUFFER_SIZE); | ||
generateBase62 = generateBase62Math | ||
initMath() | ||
} | ||
else if (crypto.getRandomValues) { | ||
buf = new Uint8Array(BUFFER_SIZE); | ||
generateBase62 = generateBase62Browser | ||
initBrowser() | ||
} | ||
else if (crypto.randomBytes) { | ||
generateBase62 = generateBase62Node | ||
initNode() | ||
} | ||
else { | ||
throw new Error('Non-standard crypto library'); | ||
throw new Error('Non-standard crypto library') | ||
} | ||
@@ -99,2 +119,2 @@ | ||
})(); | ||
})() |
{ | ||
"name": "base62-random", | ||
"version": "0.1.1-2", | ||
"version": "0.1.1-3", | ||
"description": "Fast base62 random", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test/test.js", | ||
"test": "mocha test/unit* test/perf*", | ||
"test:coverage": "istanbul cover _mocha test/unit* && istanbul check-coverage", | ||
"release:test": "set -uex; npm test; npm version prerelease; npm publish --tag testing --access public", | ||
"release:patch": "set -uex; rm -rf node_modules; yarn install; yarn test; yarn run test:gulp; yarn run test:coverage; npm version patch; git push; npm publish --access public; git push --tags;" | ||
"release:patch": "set -uex; rm -rf node_modules; yarn install; yarn test; yarn run test:coverage; npm version patch; git push; npm publish --access public; git push --tags;" | ||
}, | ||
@@ -21,3 +22,3 @@ "repository": { | ||
], | ||
"author": "Wes Roberts", | ||
"author": "Matt Hoyle", | ||
"license": "MIT", | ||
@@ -27,3 +28,9 @@ "bugs": { | ||
}, | ||
"homepage": "https://github.com/deployable/base62-random#readme" | ||
"homepage": "https://github.com/deployable/base62-random#readme", | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"eslint": "^4.6.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.5.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
47369
11
185
4
2
1