[d565449] | 1 | import crypto from 'crypto'
|
---|
[0c6b92a] | 2 |
|
---|
[d565449] | 3 | import { urlAlphabet } from './url-alphabet/index.js'
|
---|
[0c6b92a] | 4 |
|
---|
| 5 | // It is best to make fewer, larger requests to the crypto module to
|
---|
| 6 | // avoid system call overhead. So, random numbers are generated in a
|
---|
| 7 | // pool. The pool is a Buffer that is larger than the initial random
|
---|
| 8 | // request size by this multiplier. The pool is enlarged if subsequent
|
---|
| 9 | // requests exceed the maximum buffer size.
|
---|
[d565449] | 10 | const POOL_SIZE_MULTIPLIER = 128
|
---|
| 11 | let pool, poolOffset
|
---|
[0c6b92a] | 12 |
|
---|
[d565449] | 13 | let fillPool = bytes => {
|
---|
| 14 | if (!pool || pool.length < bytes) {
|
---|
| 15 | pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
---|
| 16 | crypto.randomFillSync(pool)
|
---|
| 17 | poolOffset = 0
|
---|
| 18 | } else if (poolOffset + bytes > pool.length) {
|
---|
| 19 | crypto.randomFillSync(pool)
|
---|
| 20 | poolOffset = 0
|
---|
| 21 | }
|
---|
| 22 | poolOffset += bytes
|
---|
| 23 | }
|
---|
[0c6b92a] | 24 |
|
---|
[d565449] | 25 | let random = bytes => {
|
---|
[0c6b92a] | 26 | // `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
|
---|
| 27 | fillPool((bytes |= 0))
|
---|
[d565449] | 28 | return pool.subarray(poolOffset - bytes, poolOffset)
|
---|
| 29 | }
|
---|
[0c6b92a] | 30 |
|
---|
[d565449] | 31 | let customRandom = (alphabet, defaultSize, getRandom) => {
|
---|
[0c6b92a] | 32 | // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
---|
| 33 | // values closer to the alphabet size. The bitmask calculates the closest
|
---|
| 34 | // `2^31 - 1` number, which exceeds the alphabet size.
|
---|
| 35 | // For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
---|
[d565449] | 36 | let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
---|
[0c6b92a] | 37 | // Though, the bitmask solution is not perfect since the bytes exceeding
|
---|
| 38 | // the alphabet size are refused. Therefore, to reliably generate the ID,
|
---|
| 39 | // the random bytes redundancy has to be satisfied.
|
---|
| 40 |
|
---|
| 41 | // Note: every hardware random generator call is performance expensive,
|
---|
| 42 | // because the system call for entropy collection takes a lot of time.
|
---|
| 43 | // So, to avoid additional system calls, extra bytes are requested in advance.
|
---|
| 44 |
|
---|
| 45 | // Next, a step determines how many random bytes to generate.
|
---|
| 46 | // The number of random bytes gets decided upon the ID size, mask,
|
---|
| 47 | // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
---|
| 48 | // according to benchmarks).
|
---|
[d565449] | 49 | let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
---|
[0c6b92a] | 50 |
|
---|
[d565449] | 51 | return (size = defaultSize) => {
|
---|
| 52 | let id = ''
|
---|
| 53 | while (true) {
|
---|
| 54 | let bytes = getRandom(step)
|
---|
[0c6b92a] | 55 | // A compact alternative for `for (let i = 0; i < step; i++)`.
|
---|
[d565449] | 56 | let i = step
|
---|
| 57 | while (i--) {
|
---|
[0c6b92a] | 58 | // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
---|
[d565449] | 59 | id += alphabet[bytes[i] & mask] || ''
|
---|
| 60 | if (id.length === size) return id
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
[0c6b92a] | 65 |
|
---|
[d565449] | 66 | let customAlphabet = (alphabet, size = 21) =>
|
---|
| 67 | customRandom(alphabet, size, random)
|
---|
[0c6b92a] | 68 |
|
---|
[d565449] | 69 | let nanoid = (size = 21) => {
|
---|
[0c6b92a] | 70 | // `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
|
---|
| 71 | fillPool((size |= 0))
|
---|
[d565449] | 72 | let id = ''
|
---|
[0c6b92a] | 73 | // We are reading directly from the random pool to avoid creating new array
|
---|
[d565449] | 74 | for (let i = poolOffset - size; i < poolOffset; i++) {
|
---|
[0c6b92a] | 75 | // It is incorrect to use bytes exceeding the alphabet size.
|
---|
| 76 | // The following mask reduces the random byte in the 0-255 value
|
---|
| 77 | // range to the 0-63 value range. Therefore, adding hacks, such
|
---|
| 78 | // as empty string fallback or magic numbers, is unneccessary because
|
---|
| 79 | // the bitmask trims bytes down to the alphabet size.
|
---|
[d565449] | 80 | id += urlAlphabet[pool[i] & 63]
|
---|
| 81 | }
|
---|
| 82 | return id
|
---|
| 83 | }
|
---|
[0c6b92a] | 84 |
|
---|
[d565449] | 85 | export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
---|