| let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes)) | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
| // First, a bitmask is necessary to generate the ID. The bitmask makes bytes | ||
| // values closer to the alphabet size. The bitmask calculates the closest | ||
| // `2^31 - 1` number, which exceeds the alphabet size. | ||
| // For example, the bitmask for the alphabet size 30 is 31 (00011111). | ||
| // `Math.clz32` is not used, because it is not available in browsers. | ||
| let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1 | ||
| // Though, the bitmask solution is not perfect since the bytes exceeding | ||
| // the alphabet size are refused. Therefore, to reliably generate the ID, | ||
| // the random bytes redundancy has to be satisfied. | ||
| // Note: every hardware random generator call is performance expensive, | ||
| // because the system call for entropy collection takes a lot of time. | ||
| // So, to avoid additional system calls, extra bytes are requested in advance. | ||
| // Next, a step determines how many random bytes to generate. | ||
| // The number of random bytes gets decided upon the ID size, mask, | ||
| // alphabet size, and magic number 1.6 (using 1.6 peaks at performance | ||
| // according to benchmarks). | ||
| // `-~f => Math.ceil(f)` if f is a float | ||
| // `-~i => i + 1` if i is an integer | ||
| let step = -~((1.6 * mask * defaultSize) / alphabet.length) | ||
@@ -31,6 +14,4 @@ | ||
| let bytes = crypto.getRandomValues(new Uint8Array(step)) | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| let i = step | 0 | ||
| while (i--) { | ||
| // Adding `|| ''` refuses a random byte that exceeds the alphabet size. | ||
| id += alphabet[bytes[i] & mask] || '' | ||
@@ -47,15 +28,7 @@ if (id.length === size) return id | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| while (size--) { | ||
| // It is incorrect to use bytes exceeding the alphabet size. | ||
| // The following mask reduces the random byte in the 0-255 value | ||
| // range to the 0-63 value range. Therefore, adding hacks, such | ||
| // as empty string fallback or magic numbers, is unneccessary because | ||
| // the bitmask trims bytes down to the alphabet size. | ||
| let byte = bytes[size] & 63 | ||
| if (byte < 36) { | ||
| // `0-9a-z` | ||
| id += byte.toString(36) | ||
| } else if (byte < 62) { | ||
| // `A-Z` | ||
| id += (byte - 26).toString(36).toUpperCase() | ||
@@ -62,0 +35,0 @@ } else if (byte < 63) { |
| let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes)) | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
| let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1 | ||
| let step = -~((1.6 * mask * defaultSize) / alphabet.length) | ||
| return async (size = defaultSize) => { | ||
@@ -17,5 +22,7 @@ let id = '' | ||
| } | ||
| let nanoid = async (size = 21) => { | ||
| let id = '' | ||
| let bytes = crypto.getRandomValues(new Uint8Array((size |= 0))) | ||
| while (size--) { | ||
@@ -35,2 +42,3 @@ let byte = bytes[size] & 63 | ||
| } | ||
| export { nanoid, customAlphabet, random } |
+0
-27
@@ -5,9 +5,4 @@ let crypto = require('crypto') | ||
| // `crypto.randomFill()` is a little faster than `crypto.randomBytes()`, | ||
| // because it is possible to use in combination with `Buffer.allocUnsafe()`. | ||
| let random = bytes => | ||
| new Promise((resolve, reject) => { | ||
| // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory. | ||
| // Memory flushing is unnecessary since the buffer allocation itself resets | ||
| // the memory with the new bytes. | ||
| crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => { | ||
@@ -23,19 +18,5 @@ if (err) { | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
| // First, a bitmask is necessary to generate the ID. The bitmask makes bytes | ||
| // values closer to the alphabet size. The bitmask calculates the closest | ||
| // `2^31 - 1` number, which exceeds the alphabet size. | ||
| // For example, the bitmask for the alphabet size 30 is 31 (00011111). | ||
| let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 | ||
| // Though, the bitmask solution is not perfect since the bytes exceeding | ||
| // the alphabet size are refused. Therefore, to reliably generate the ID, | ||
| // the random bytes redundancy has to be satisfied. | ||
| // Note: every hardware random generator call is performance expensive, | ||
| // because the system call for entropy collection takes a lot of time. | ||
| // So, to avoid additional system calls, extra bytes are requested in advance. | ||
| // Next, a step determines how many random bytes to generate. | ||
| // The number of random bytes gets decided upon the ID size, mask, | ||
| // alphabet size, and magic number 1.6 (using 1.6 peaks at performance | ||
| // according to benchmarks). | ||
| let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) | ||
@@ -45,6 +26,4 @@ | ||
| random(step).then(bytes => { | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| let i = step | ||
| while (i--) { | ||
| // Adding `|| ''` refuses a random byte that exceeds the alphabet size. | ||
| id += alphabet[bytes[i] & mask] || '' | ||
@@ -62,9 +41,3 @@ if (id.length >= size) return id | ||
| let id = '' | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| while (size--) { | ||
| // It is incorrect to use bytes exceeding the alphabet size. | ||
| // The following mask reduces the random byte in the 0-255 value | ||
| // range to the 0-63 value range. Therefore, adding hacks, such | ||
| // as empty string fallback or magic numbers, is unneccessary because | ||
| // the bitmask trims bytes down to the alphabet size. | ||
| id += urlAlphabet[bytes[size] & 63] | ||
@@ -71,0 +44,0 @@ } |
+9
-0
| import crypto from 'crypto' | ||
| import { urlAlphabet } from '../url-alphabet/index.js' | ||
| let random = bytes => | ||
@@ -13,5 +15,9 @@ new Promise((resolve, reject) => { | ||
| }) | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
| let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 | ||
| let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) | ||
| let tick = (id, size = defaultSize) => | ||
@@ -26,4 +32,6 @@ random(step).then(bytes => { | ||
| }) | ||
| return size => tick('', size) | ||
| } | ||
| let nanoid = (size = 21) => | ||
@@ -37,2 +45,3 @@ random((size |= 0)).then(bytes => { | ||
| }) | ||
| export { nanoid, customAlphabet, random } |
| import { getRandomBytesAsync } from 'expo-random' | ||
| import { urlAlphabet } from '../url-alphabet/index.js' | ||
| let random = getRandomBytesAsync | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
| let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 | ||
| let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) | ||
| let tick = (id, size = defaultSize) => | ||
@@ -16,4 +22,6 @@ random(step).then(bytes => { | ||
| }) | ||
| return size => tick('', size) | ||
| } | ||
| let nanoid = (size = 21) => | ||
@@ -27,2 +35,3 @@ random((size |= 0)).then(bytes => { | ||
| }) | ||
| export { nanoid, customAlphabet, random } |
+0
-28
@@ -1,3 +0,1 @@ | ||
| // This file replaces `index.js` in bundlers like webpack or Rollup, | ||
| // according to `browser` config in `package.json`. | ||
@@ -9,23 +7,6 @@ let { urlAlphabet } = require('./url-alphabet/index.cjs') | ||
| let customRandom = (alphabet, defaultSize, getRandom) => { | ||
| // First, a bitmask is necessary to generate the ID. The bitmask makes bytes | ||
| // values closer to the alphabet size. The bitmask calculates the closest | ||
| // `2^31 - 1` number, which exceeds the alphabet size. | ||
| // For example, the bitmask for the alphabet size 30 is 31 (00011111). | ||
| // `Math.clz32` is not used, because it is not available in browsers. | ||
| let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1 | ||
| // Though, the bitmask solution is not perfect since the bytes exceeding | ||
| // the alphabet size are refused. Therefore, to reliably generate the ID, | ||
| // the random bytes redundancy has to be satisfied. | ||
| // Note: every hardware random generator call is performance expensive, | ||
| // because the system call for entropy collection takes a lot of time. | ||
| // So, to avoid additional system calls, extra bytes are requested in advance. | ||
| // Next, a step determines how many random bytes to generate. | ||
| // The number of random bytes gets decided upon the ID size, mask, | ||
| // alphabet size, and magic number 1.6 (using 1.6 peaks at performance | ||
| // according to benchmarks). | ||
| // `-~f => Math.ceil(f)` if f is a float | ||
| // `-~i => i + 1` if i is an integer | ||
| let step = -~((1.6 * mask * defaultSize) / alphabet.length) | ||
@@ -37,6 +18,4 @@ | ||
| let bytes = getRandom(step) | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| let j = step | 0 | ||
| while (j--) { | ||
| // Adding `|| ''` refuses a random byte that exceeds the alphabet size. | ||
| id += alphabet[bytes[j] & mask] || '' | ||
@@ -54,13 +33,6 @@ if (id.length === size) return id | ||
| crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => { | ||
| // It is incorrect to use bytes exceeding the alphabet size. | ||
| // The following mask reduces the random byte in the 0-255 value | ||
| // range to the 0-63 value range. Therefore, adding hacks, such | ||
| // as empty string fallback or magic numbers, is unneccessary because | ||
| // the bitmask trims bytes down to the alphabet size. | ||
| byte &= 63 | ||
| if (byte < 36) { | ||
| // `0-9a-z` | ||
| id += byte.toString(36) | ||
| } else if (byte < 62) { | ||
| // `A-Z` | ||
| id += (byte - 26).toString(36).toUpperCase() | ||
@@ -67,0 +39,0 @@ } else if (byte > 62) { |
+10
-0
@@ -0,6 +1,13 @@ | ||
| import { urlAlphabet } from './url-alphabet/index.js' | ||
| let random = bytes => crypto.getRandomValues(new Uint8Array(bytes)) | ||
| let customRandom = (alphabet, defaultSize, getRandom) => { | ||
| let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1 | ||
| let step = -~((1.6 * mask * defaultSize) / alphabet.length) | ||
| return (size = defaultSize) => { | ||
@@ -18,4 +25,6 @@ let id = '' | ||
| } | ||
| let customAlphabet = (alphabet, size = 21) => | ||
| customRandom(alphabet, size, random) | ||
| let nanoid = (size = 21) => | ||
@@ -35,2 +44,3 @@ crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => { | ||
| }, '') | ||
| export { nanoid, customAlphabet, customRandom, urlAlphabet, random } |
+0
-29
@@ -5,7 +5,2 @@ let crypto = require('crypto') | ||
| // It is best to make fewer, larger requests to the crypto module to | ||
| // avoid system call overhead. So, random numbers are generated in a | ||
| // pool. The pool is a Buffer that is larger than the initial random | ||
| // request size by this multiplier. The pool is enlarged if subsequent | ||
| // requests exceed the maximum buffer size. | ||
| const POOL_SIZE_MULTIPLIER = 128 | ||
@@ -28,3 +23,2 @@ let pool, poolOffset | ||
| let random = bytes => { | ||
| // `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution | ||
| fillPool((bytes |= 0)) | ||
@@ -35,19 +29,5 @@ return pool.subarray(poolOffset - bytes, poolOffset) | ||
| let customRandom = (alphabet, defaultSize, getRandom) => { | ||
| // First, a bitmask is necessary to generate the ID. The bitmask makes bytes | ||
| // values closer to the alphabet size. The bitmask calculates the closest | ||
| // `2^31 - 1` number, which exceeds the alphabet size. | ||
| // For example, the bitmask for the alphabet size 30 is 31 (00011111). | ||
| let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 | ||
| // Though, the bitmask solution is not perfect since the bytes exceeding | ||
| // the alphabet size are refused. Therefore, to reliably generate the ID, | ||
| // the random bytes redundancy has to be satisfied. | ||
| // Note: every hardware random generator call is performance expensive, | ||
| // because the system call for entropy collection takes a lot of time. | ||
| // So, to avoid additional system calls, extra bytes are requested in advance. | ||
| // Next, a step determines how many random bytes to generate. | ||
| // The number of random bytes gets decided upon the ID size, mask, | ||
| // alphabet size, and magic number 1.6 (using 1.6 peaks at performance | ||
| // according to benchmarks). | ||
| let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) | ||
@@ -59,6 +39,4 @@ | ||
| let bytes = getRandom(step) | ||
| // A compact alternative for `for (let i = 0; i < step; i++)`. | ||
| let i = step | ||
| while (i--) { | ||
| // Adding `|| ''` refuses a random byte that exceeds the alphabet size. | ||
| id += alphabet[bytes[i] & mask] || '' | ||
@@ -75,12 +53,5 @@ if (id.length === size) return id | ||
| let nanoid = (size = 21) => { | ||
| // `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution | ||
| fillPool((size |= 0)) | ||
| let id = '' | ||
| // We are reading directly from the random pool to avoid creating new array | ||
| for (let i = poolOffset - size; i < poolOffset; i++) { | ||
| // It is incorrect to use bytes exceeding the alphabet size. | ||
| // The following mask reduces the random byte in the 0-255 value | ||
| // range to the 0-63 value range. Therefore, adding hacks, such | ||
| // as empty string fallback or magic numbers, is unneccessary because | ||
| // the bitmask trims bytes down to the alphabet size. | ||
| id += urlAlphabet[pool[i] & 63] | ||
@@ -87,0 +58,0 @@ } |
+11
-0
| import crypto from 'crypto' | ||
| import { urlAlphabet } from './url-alphabet/index.js' | ||
| const POOL_SIZE_MULTIPLIER = 128 | ||
| let pool, poolOffset | ||
| let fillPool = bytes => { | ||
@@ -22,2 +25,3 @@ if (bytes < 0) throw new RangeError('Wrong ID size') | ||
| } | ||
| let random = bytes => { | ||
@@ -27,5 +31,9 @@ fillPool((bytes |= 0)) | ||
| } | ||
| let customRandom = (alphabet, defaultSize, getRandom) => { | ||
| let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 | ||
| let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) | ||
| return (size = defaultSize) => { | ||
@@ -43,4 +51,6 @@ let id = '' | ||
| } | ||
| let customAlphabet = (alphabet, size = 21) => | ||
| customRandom(alphabet, size, random) | ||
| let nanoid = (size = 21) => { | ||
@@ -54,2 +64,3 @@ fillPool((size |= 0)) | ||
| } | ||
| export { nanoid, customAlphabet, customRandom, urlAlphabet, random } |
+0
-10
@@ -1,7 +0,1 @@ | ||
| // This alphabet uses `A-Za-z0-9_-` symbols. | ||
| // The order of characters is optimized for better gzip and brotli compression. | ||
| // References to the same file (works both for gzip and brotli): | ||
| // `'use`, `andom`, and `rict'` | ||
| // References to the brotli default dictionary: | ||
| // `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf` | ||
| let urlAlphabet = | ||
@@ -13,6 +7,4 @@ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' | ||
| let id = '' | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| let i = size | 0 | ||
| while (i--) { | ||
| // `| 0` is more compact and faster than `Math.floor()`. | ||
| id += alphabet[(Math.random() * alphabet.length) | 0] | ||
@@ -26,6 +18,4 @@ } | ||
| let id = '' | ||
| // A compact alternative for `for (var i = 0; i < step; i++)`. | ||
| let i = size | 0 | ||
| while (i--) { | ||
| // `| 0` is more compact and faster than `Math.floor()`. | ||
| id += urlAlphabet[(Math.random() * 64) | 0] | ||
@@ -32,0 +22,0 @@ } |
| let urlAlphabet = | ||
| 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' | ||
| let customAlphabet = (alphabet, defaultSize = 21) => { | ||
@@ -13,2 +14,3 @@ return (size = defaultSize) => { | ||
| } | ||
| let nanoid = (size = 21) => { | ||
@@ -22,2 +24,3 @@ let id = '' | ||
| } | ||
| export { nanoid, customAlphabet } |
+1
-1
| { | ||
| "name": "nanoid", | ||
| "version": "3.3.14", | ||
| "version": "3.3.15", | ||
| "description": "A tiny (116 bytes), secure URL-friendly unique string ID generator", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
+0
-3
@@ -37,4 +37,1 @@ # Nano ID | ||
| </a> | ||
| ## Docs | ||
| Read full docs **[here](https://github.com/ai/nanoid#readme)**. |
@@ -1,4 +0,1 @@ | ||
| // This alphabet uses `A-Za-z0-9_-` symbols. | ||
| // The order of characters is optimized for better gzip and brotli compression. | ||
| // Same as in non-secure/index.js | ||
| let urlAlphabet = | ||
@@ -5,0 +2,0 @@ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' |
| let urlAlphabet = | ||
| 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' | ||
| export { urlAlphabet } |
24698
-24.67%596
-17.22%37
-7.5%