[d565449] | 1 | import { getRandomBytesAsync } from 'expo-random'
|
---|
[0c6b92a] | 2 |
|
---|
[d565449] | 3 | import { urlAlphabet } from '../url-alphabet/index.js'
|
---|
[0c6b92a] | 4 |
|
---|
[d565449] | 5 | let random = getRandomBytesAsync
|
---|
[0c6b92a] | 6 |
|
---|
[d565449] | 7 | let customAlphabet = (alphabet, defaultSize = 21) => {
|
---|
[0c6b92a] | 8 | // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
---|
| 9 | // values closer to the alphabet size. The bitmask calculates the closest
|
---|
| 10 | // `2^31 - 1` number, which exceeds the alphabet size.
|
---|
| 11 | // For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
---|
[d565449] | 12 | let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
---|
[0c6b92a] | 13 | // Though, the bitmask solution is not perfect since the bytes exceeding
|
---|
| 14 | // the alphabet size are refused. Therefore, to reliably generate the ID,
|
---|
| 15 | // the random bytes redundancy has to be satisfied.
|
---|
| 16 |
|
---|
| 17 | // Note: every hardware random generator call is performance expensive,
|
---|
| 18 | // because the system call for entropy collection takes a lot of time.
|
---|
| 19 | // So, to avoid additional system calls, extra bytes are requested in advance.
|
---|
| 20 |
|
---|
| 21 | // Next, a step determines how many random bytes to generate.
|
---|
| 22 | // The number of random bytes gets decided upon the ID size, mask,
|
---|
| 23 | // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
---|
| 24 | // according to benchmarks).
|
---|
[d565449] | 25 | let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
---|
[0c6b92a] | 26 |
|
---|
[d565449] | 27 | let tick = (id, size = defaultSize) =>
|
---|
| 28 | random(step).then(bytes => {
|
---|
[0c6b92a] | 29 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
[d565449] | 30 | let i = step
|
---|
| 31 | while (i--) {
|
---|
[0c6b92a] | 32 | // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
---|
[d565449] | 33 | id += alphabet[bytes[i] & mask] || ''
|
---|
[0c6b92a] | 34 | if (id.length >= size) return id
|
---|
[d565449] | 35 | }
|
---|
| 36 | return tick(id, size)
|
---|
| 37 | })
|
---|
[0c6b92a] | 38 |
|
---|
[d565449] | 39 | return size => tick('', size)
|
---|
| 40 | }
|
---|
[0c6b92a] | 41 |
|
---|
[d565449] | 42 | let nanoid = (size = 21) =>
|
---|
[0c6b92a] | 43 | random((size |= 0)).then(bytes => {
|
---|
[d565449] | 44 | let id = ''
|
---|
[0c6b92a] | 45 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
[d565449] | 46 | while (size--) {
|
---|
[0c6b92a] | 47 | // It is incorrect to use bytes exceeding the alphabet size.
|
---|
| 48 | // The following mask reduces the random byte in the 0-255 value
|
---|
| 49 | // range to the 0-63 value range. Therefore, adding hacks, such
|
---|
| 50 | // as empty string fallback or magic numbers, is unneccessary because
|
---|
| 51 | // the bitmask trims bytes down to the alphabet size.
|
---|
[d565449] | 52 | id += urlAlphabet[bytes[size] & 63]
|
---|
| 53 | }
|
---|
| 54 | return id
|
---|
| 55 | })
|
---|
[0c6b92a] | 56 |
|
---|
[d565449] | 57 | export { nanoid, customAlphabet, random }
|
---|