[d565449] | 1 | let crypto = require('crypto')
|
---|
[0c6b92a] | 2 |
|
---|
[d565449] | 3 | let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
---|
[0c6b92a] | 4 |
|
---|
| 5 | // `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
---|
| 6 | // because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
---|
[d565449] | 7 | let random = bytes =>
|
---|
| 8 | new Promise((resolve, reject) => {
|
---|
[0c6b92a] | 9 | // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
---|
| 10 | // Memory flushing is unnecessary since the buffer allocation itself resets
|
---|
| 11 | // the memory with the new bytes.
|
---|
[d565449] | 12 | crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
---|
| 13 | if (err) {
|
---|
| 14 | reject(err)
|
---|
| 15 | } else {
|
---|
| 16 | resolve(buf)
|
---|
| 17 | }
|
---|
| 18 | })
|
---|
| 19 | })
|
---|
[0c6b92a] | 20 |
|
---|
[d565449] | 21 | let customAlphabet = (alphabet, defaultSize = 21) => {
|
---|
[0c6b92a] | 22 | // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
---|
| 23 | // values closer to the alphabet size. The bitmask calculates the closest
|
---|
| 24 | // `2^31 - 1` number, which exceeds the alphabet size.
|
---|
| 25 | // For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
---|
[d565449] | 26 | let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
---|
[0c6b92a] | 27 | // Though, the bitmask solution is not perfect since the bytes exceeding
|
---|
| 28 | // the alphabet size are refused. Therefore, to reliably generate the ID,
|
---|
| 29 | // the random bytes redundancy has to be satisfied.
|
---|
| 30 |
|
---|
| 31 | // Note: every hardware random generator call is performance expensive,
|
---|
| 32 | // because the system call for entropy collection takes a lot of time.
|
---|
| 33 | // So, to avoid additional system calls, extra bytes are requested in advance.
|
---|
| 34 |
|
---|
| 35 | // Next, a step determines how many random bytes to generate.
|
---|
| 36 | // The number of random bytes gets decided upon the ID size, mask,
|
---|
| 37 | // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
---|
| 38 | // according to benchmarks).
|
---|
[d565449] | 39 | let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
---|
[0c6b92a] | 40 |
|
---|
[d565449] | 41 | let tick = (id, size = defaultSize) =>
|
---|
| 42 | random(step).then(bytes => {
|
---|
[0c6b92a] | 43 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
[d565449] | 44 | let i = step
|
---|
| 45 | while (i--) {
|
---|
[0c6b92a] | 46 | // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
---|
[d565449] | 47 | id += alphabet[bytes[i] & mask] || ''
|
---|
[0c6b92a] | 48 | if (id.length >= size) return id
|
---|
[d565449] | 49 | }
|
---|
| 50 | return tick(id, size)
|
---|
| 51 | })
|
---|
[0c6b92a] | 52 |
|
---|
[d565449] | 53 | return size => tick('', size)
|
---|
| 54 | }
|
---|
[0c6b92a] | 55 |
|
---|
[d565449] | 56 | let nanoid = (size = 21) =>
|
---|
[0c6b92a] | 57 | random((size |= 0)).then(bytes => {
|
---|
[d565449] | 58 | let id = ''
|
---|
[0c6b92a] | 59 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
[d565449] | 60 | while (size--) {
|
---|
[0c6b92a] | 61 | // It is incorrect to use bytes exceeding the alphabet size.
|
---|
| 62 | // The following mask reduces the random byte in the 0-255 value
|
---|
| 63 | // range to the 0-63 value range. Therefore, adding hacks, such
|
---|
| 64 | // as empty string fallback or magic numbers, is unneccessary because
|
---|
| 65 | // the bitmask trims bytes down to the alphabet size.
|
---|
[d565449] | 66 | id += urlAlphabet[bytes[size] & 63]
|
---|
| 67 | }
|
---|
| 68 | return id
|
---|
| 69 | })
|
---|
[0c6b92a] | 70 |
|
---|
[d565449] | 71 | module.exports = { nanoid, customAlphabet, random }
|
---|