nanoid 3.1.30 → 3.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devcontainer.json +23 -0
- package/README.md +519 -4
- package/async/index.browser.cjs +44 -10
- package/async/index.browser.js +44 -10
- package/async/index.cjs +43 -7
- package/async/index.d.ts +4 -4
- package/async/index.js +43 -7
- package/async/index.native.js +38 -7
- package/bin/nanoid.cjs +52 -2
- package/index.browser.cjs +52 -42
- package/index.browser.js +52 -42
- package/index.cjs +47 -6
- package/index.d.cts +91 -0
- package/index.d.ts +5 -2
- package/index.js +47 -6
- package/nanoid.js +1 -1
- package/non-secure/index.cjs +17 -4
- package/non-secure/index.d.ts +7 -4
- package/non-secure/index.js +17 -4
- package/package.json +42 -18
- package/url-alphabet/index.cjs +4 -0
- package/url-alphabet/index.js +4 -0
- package/index.dev.js +0 -62
- package/index.prod.js +0 -62
package/async/index.browser.js
CHANGED
@@ -1,28 +1,61 @@
|
|
1
|
-
let random = bytes =>
|
2
|
-
|
3
|
-
let customAlphabet = (alphabet,
|
1
|
+
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
2
|
+
|
3
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
4
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
5
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
6
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
7
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
8
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
4
9
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
5
|
-
|
6
|
-
|
10
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
11
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
12
|
+
// the random bytes redundancy has to be satisfied.
|
13
|
+
|
14
|
+
// Note: every hardware random generator call is performance expensive,
|
15
|
+
// because the system call for entropy collection takes a lot of time.
|
16
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
17
|
+
|
18
|
+
// Next, a step determines how many random bytes to generate.
|
19
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
20
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
21
|
+
// according to benchmarks).
|
22
|
+
|
23
|
+
// `-~f => Math.ceil(f)` if f is a float
|
24
|
+
// `-~i => i + 1` if i is an integer
|
25
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
26
|
+
|
27
|
+
return async (size = defaultSize) => {
|
7
28
|
let id = ''
|
8
29
|
while (true) {
|
9
30
|
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
10
|
-
|
31
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
32
|
+
let i = step | 0
|
11
33
|
while (i--) {
|
34
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
12
35
|
id += alphabet[bytes[i] & mask] || ''
|
13
|
-
if (id.length === size) return
|
36
|
+
if (id.length === size) return id
|
14
37
|
}
|
15
38
|
}
|
16
39
|
}
|
17
40
|
}
|
18
|
-
|
41
|
+
|
42
|
+
let nanoid = async (size = 21) => {
|
19
43
|
let id = ''
|
20
|
-
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
44
|
+
let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
45
|
+
|
46
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
21
47
|
while (size--) {
|
48
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
49
|
+
// The following mask reduces the random byte in the 0-255 value
|
50
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
51
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
52
|
+
// the bitmask trims bytes down to the alphabet size.
|
22
53
|
let byte = bytes[size] & 63
|
23
54
|
if (byte < 36) {
|
55
|
+
// `0-9a-z`
|
24
56
|
id += byte.toString(36)
|
25
57
|
} else if (byte < 62) {
|
58
|
+
// `A-Z`
|
26
59
|
id += (byte - 26).toString(36).toUpperCase()
|
27
60
|
} else if (byte < 63) {
|
28
61
|
id += '_'
|
@@ -30,6 +63,7 @@ let nanoid = (size = 21) => {
|
|
30
63
|
id += '-'
|
31
64
|
}
|
32
65
|
}
|
33
|
-
return
|
66
|
+
return id
|
34
67
|
}
|
68
|
+
|
35
69
|
export { nanoid, customAlphabet, random }
|
package/async/index.cjs
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
let crypto = require('crypto')
|
2
|
+
|
2
3
|
let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
4
|
+
|
5
|
+
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
6
|
+
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
3
7
|
let random = bytes =>
|
4
8
|
new Promise((resolve, reject) => {
|
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.
|
5
12
|
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
6
13
|
if (err) {
|
7
14
|
reject(err)
|
@@ -10,26 +17,55 @@ let random = bytes =>
|
|
10
17
|
}
|
11
18
|
})
|
12
19
|
})
|
13
|
-
|
20
|
+
|
21
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
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).
|
14
26
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
15
|
-
|
16
|
-
|
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).
|
39
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
40
|
+
|
41
|
+
let tick = (id, size = defaultSize) =>
|
17
42
|
random(step).then(bytes => {
|
43
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
18
44
|
let i = step
|
19
45
|
while (i--) {
|
46
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
20
47
|
id += alphabet[bytes[i] & mask] || ''
|
21
|
-
if (id.length
|
48
|
+
if (id.length >= size) return id
|
22
49
|
}
|
23
|
-
return tick(id)
|
50
|
+
return tick(id, size)
|
24
51
|
})
|
25
|
-
|
52
|
+
|
53
|
+
return size => tick('', size)
|
26
54
|
}
|
55
|
+
|
27
56
|
let nanoid = (size = 21) =>
|
28
|
-
random(size).then(bytes => {
|
57
|
+
random((size |= 0)).then(bytes => {
|
29
58
|
let id = ''
|
59
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
30
60
|
while (size--) {
|
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.
|
31
66
|
id += urlAlphabet[bytes[size] & 63]
|
32
67
|
}
|
33
68
|
return id
|
34
69
|
})
|
70
|
+
|
35
71
|
module.exports = { nanoid, customAlphabet, random }
|
package/async/index.d.ts
CHANGED
@@ -24,8 +24,8 @@ export function nanoid(size?: number): Promise<string>
|
|
24
24
|
* will not be secure.
|
25
25
|
*
|
26
26
|
* @param alphabet Alphabet used to generate the ID.
|
27
|
-
* @param
|
28
|
-
* @returns A promise with a random string.
|
27
|
+
* @param defaultSize Size of the ID. The default size is 21.
|
28
|
+
* @returns A function that returns a promise with a random string.
|
29
29
|
*
|
30
30
|
* ```js
|
31
31
|
* import { customAlphabet } from 'nanoid/async'
|
@@ -37,8 +37,8 @@ export function nanoid(size?: number): Promise<string>
|
|
37
37
|
*/
|
38
38
|
export function customAlphabet(
|
39
39
|
alphabet: string,
|
40
|
-
|
41
|
-
): () => Promise<string>
|
40
|
+
defaultSize?: number
|
41
|
+
): (size?: number) => Promise<string>
|
42
42
|
|
43
43
|
/**
|
44
44
|
* Generate an array of random bytes collected from hardware noise.
|
package/async/index.js
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
import crypto from 'crypto'
|
2
|
+
|
2
3
|
import { urlAlphabet } from '../url-alphabet/index.js'
|
4
|
+
|
5
|
+
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
6
|
+
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
3
7
|
let random = bytes =>
|
4
8
|
new Promise((resolve, reject) => {
|
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.
|
5
12
|
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
6
13
|
if (err) {
|
7
14
|
reject(err)
|
@@ -10,26 +17,55 @@ let random = bytes =>
|
|
10
17
|
}
|
11
18
|
})
|
12
19
|
})
|
13
|
-
|
20
|
+
|
21
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
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).
|
14
26
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
15
|
-
|
16
|
-
|
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).
|
39
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
40
|
+
|
41
|
+
let tick = (id, size = defaultSize) =>
|
17
42
|
random(step).then(bytes => {
|
43
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
18
44
|
let i = step
|
19
45
|
while (i--) {
|
46
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
20
47
|
id += alphabet[bytes[i] & mask] || ''
|
21
|
-
if (id.length
|
48
|
+
if (id.length >= size) return id
|
22
49
|
}
|
23
|
-
return tick(id)
|
50
|
+
return tick(id, size)
|
24
51
|
})
|
25
|
-
|
52
|
+
|
53
|
+
return size => tick('', size)
|
26
54
|
}
|
55
|
+
|
27
56
|
let nanoid = (size = 21) =>
|
28
|
-
random(size).then(bytes => {
|
57
|
+
random((size |= 0)).then(bytes => {
|
29
58
|
let id = ''
|
59
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
30
60
|
while (size--) {
|
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.
|
31
66
|
id += urlAlphabet[bytes[size] & 63]
|
32
67
|
}
|
33
68
|
return id
|
34
69
|
})
|
70
|
+
|
35
71
|
export { nanoid, customAlphabet, random }
|
package/async/index.native.js
CHANGED
@@ -1,26 +1,57 @@
|
|
1
1
|
import { getRandomBytesAsync } from 'expo-random'
|
2
|
+
|
2
3
|
import { urlAlphabet } from '../url-alphabet/index.js'
|
4
|
+
|
3
5
|
let random = getRandomBytesAsync
|
4
|
-
|
6
|
+
|
7
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
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).
|
5
12
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
6
|
-
|
7
|
-
|
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).
|
25
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
26
|
+
|
27
|
+
let tick = (id, size = defaultSize) =>
|
8
28
|
random(step).then(bytes => {
|
29
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
9
30
|
let i = step
|
10
31
|
while (i--) {
|
32
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
11
33
|
id += alphabet[bytes[i] & mask] || ''
|
12
|
-
if (id.length
|
34
|
+
if (id.length >= size) return id
|
13
35
|
}
|
14
|
-
return tick(id)
|
36
|
+
return tick(id, size)
|
15
37
|
})
|
16
|
-
|
38
|
+
|
39
|
+
return size => tick('', size)
|
17
40
|
}
|
41
|
+
|
18
42
|
let nanoid = (size = 21) =>
|
19
|
-
random(size).then(bytes => {
|
43
|
+
random((size |= 0)).then(bytes => {
|
20
44
|
let id = ''
|
45
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
21
46
|
while (size--) {
|
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.
|
22
52
|
id += urlAlphabet[bytes[size] & 63]
|
23
53
|
}
|
24
54
|
return id
|
25
55
|
})
|
56
|
+
|
26
57
|
export { nanoid, customAlphabet, random }
|
package/bin/nanoid.cjs
CHANGED
@@ -1,5 +1,55 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
|
3
|
-
let { nanoid } = require('..')
|
3
|
+
let { nanoid, customAlphabet } = require('..')
|
4
4
|
|
5
|
-
|
5
|
+
function print(msg) {
|
6
|
+
process.stdout.write(msg + '\n')
|
7
|
+
}
|
8
|
+
|
9
|
+
function error(msg) {
|
10
|
+
process.stderr.write(msg + '\n')
|
11
|
+
process.exit(1)
|
12
|
+
}
|
13
|
+
|
14
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
15
|
+
print(`
|
16
|
+
Usage
|
17
|
+
$ nanoid [options]
|
18
|
+
|
19
|
+
Options
|
20
|
+
-s, --size Generated ID size
|
21
|
+
-a, --alphabet Alphabet to use
|
22
|
+
-h, --help Show this help
|
23
|
+
|
24
|
+
Examples
|
25
|
+
$ nanoid --s 15
|
26
|
+
S9sBF77U6sDB8Yg
|
27
|
+
|
28
|
+
$ nanoid --size 10 --alphabet abc
|
29
|
+
bcabababca`)
|
30
|
+
process.exit()
|
31
|
+
}
|
32
|
+
|
33
|
+
let alphabet, size
|
34
|
+
for (let i = 2; i < process.argv.length; i++) {
|
35
|
+
let arg = process.argv[i]
|
36
|
+
if (arg === '--size' || arg === '-s') {
|
37
|
+
size = Number(process.argv[i + 1])
|
38
|
+
i += 1
|
39
|
+
if (Number.isNaN(size) || size <= 0) {
|
40
|
+
error('Size must be positive integer')
|
41
|
+
}
|
42
|
+
} else if (arg === '--alphabet' || arg === '-a') {
|
43
|
+
alphabet = process.argv[i + 1]
|
44
|
+
i += 1
|
45
|
+
} else {
|
46
|
+
error('Unknown argument ' + arg)
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
if (alphabet) {
|
51
|
+
let customNanoid = customAlphabet(alphabet, size)
|
52
|
+
print(customNanoid())
|
53
|
+
} else {
|
54
|
+
print(nanoid(size))
|
55
|
+
}
|
package/index.browser.cjs
CHANGED
@@ -1,62 +1,72 @@
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
+
// according to `browser` config in `package.json`.
|
3
|
+
|
1
4
|
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
2
|
-
|
3
|
-
if (
|
4
|
-
typeof navigator !== 'undefined' &&
|
5
|
-
navigator.product === 'ReactNative' &&
|
6
|
-
typeof crypto === 'undefined'
|
7
|
-
) {
|
8
|
-
throw new Error(
|
9
|
-
'React Native does not have a built-in secure random generator. ' +
|
10
|
-
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
11
|
-
'For secure IDs, import `react-native-get-random-values` ' +
|
12
|
-
'before Nano ID.'
|
13
|
-
)
|
14
|
-
}
|
15
|
-
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
16
|
-
throw new Error(
|
17
|
-
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
18
|
-
' before importing Nano ID to fix IE 11 support'
|
19
|
-
)
|
20
|
-
}
|
21
|
-
if (typeof crypto === 'undefined') {
|
22
|
-
throw new Error(
|
23
|
-
'Your browser does not have secure random generator. ' +
|
24
|
-
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
25
|
-
)
|
26
|
-
}
|
27
|
-
}
|
5
|
+
|
28
6
|
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
29
|
-
|
7
|
+
|
8
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
9
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
10
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
11
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
12
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
13
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
30
14
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
31
|
-
|
32
|
-
|
15
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
16
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
17
|
+
// the random bytes redundancy has to be satisfied.
|
18
|
+
|
19
|
+
// Note: every hardware random generator call is performance expensive,
|
20
|
+
// because the system call for entropy collection takes a lot of time.
|
21
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
22
|
+
|
23
|
+
// Next, a step determines how many random bytes to generate.
|
24
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
25
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
26
|
+
// according to benchmarks).
|
27
|
+
|
28
|
+
// `-~f => Math.ceil(f)` if f is a float
|
29
|
+
// `-~i => i + 1` if i is an integer
|
30
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
31
|
+
|
32
|
+
return (size = defaultSize) => {
|
33
33
|
let id = ''
|
34
34
|
while (true) {
|
35
35
|
let bytes = getRandom(step)
|
36
|
-
|
36
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
37
|
+
let j = step | 0
|
37
38
|
while (j--) {
|
39
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
38
40
|
id += alphabet[bytes[j] & mask] || ''
|
39
41
|
if (id.length === size) return id
|
40
42
|
}
|
41
43
|
}
|
42
44
|
}
|
43
45
|
}
|
44
|
-
|
45
|
-
let
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
|
47
|
+
let customAlphabet = (alphabet, size = 21) =>
|
48
|
+
customRandom(alphabet, size, random)
|
49
|
+
|
50
|
+
let nanoid = (size = 21) =>
|
51
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
52
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
53
|
+
// The following mask reduces the random byte in the 0-255 value
|
54
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
55
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
56
|
+
// the bitmask trims bytes down to the alphabet size.
|
57
|
+
byte &= 63
|
50
58
|
if (byte < 36) {
|
59
|
+
// `0-9a-z`
|
51
60
|
id += byte.toString(36)
|
52
61
|
} else if (byte < 62) {
|
62
|
+
// `A-Z`
|
53
63
|
id += (byte - 26).toString(36).toUpperCase()
|
54
|
-
} else if (byte
|
55
|
-
id += '_'
|
56
|
-
} else {
|
64
|
+
} else if (byte > 62) {
|
57
65
|
id += '-'
|
66
|
+
} else {
|
67
|
+
id += '_'
|
58
68
|
}
|
59
|
-
|
60
|
-
|
61
|
-
|
69
|
+
return id
|
70
|
+
}, '')
|
71
|
+
|
62
72
|
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.browser.js
CHANGED
@@ -1,62 +1,72 @@
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
+
// according to `browser` config in `package.json`.
|
3
|
+
|
1
4
|
import { urlAlphabet } from './url-alphabet/index.js'
|
2
|
-
|
3
|
-
if (
|
4
|
-
typeof navigator !== 'undefined' &&
|
5
|
-
navigator.product === 'ReactNative' &&
|
6
|
-
typeof crypto === 'undefined'
|
7
|
-
) {
|
8
|
-
throw new Error(
|
9
|
-
'React Native does not have a built-in secure random generator. ' +
|
10
|
-
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
11
|
-
'For secure IDs, import `react-native-get-random-values` ' +
|
12
|
-
'before Nano ID.'
|
13
|
-
)
|
14
|
-
}
|
15
|
-
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
16
|
-
throw new Error(
|
17
|
-
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
18
|
-
' before importing Nano ID to fix IE 11 support'
|
19
|
-
)
|
20
|
-
}
|
21
|
-
if (typeof crypto === 'undefined') {
|
22
|
-
throw new Error(
|
23
|
-
'Your browser does not have secure random generator. ' +
|
24
|
-
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
25
|
-
)
|
26
|
-
}
|
27
|
-
}
|
5
|
+
|
28
6
|
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
29
|
-
|
7
|
+
|
8
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
9
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
10
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
11
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
12
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
13
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
30
14
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
31
|
-
|
32
|
-
|
15
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
16
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
17
|
+
// the random bytes redundancy has to be satisfied.
|
18
|
+
|
19
|
+
// Note: every hardware random generator call is performance expensive,
|
20
|
+
// because the system call for entropy collection takes a lot of time.
|
21
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
22
|
+
|
23
|
+
// Next, a step determines how many random bytes to generate.
|
24
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
25
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
26
|
+
// according to benchmarks).
|
27
|
+
|
28
|
+
// `-~f => Math.ceil(f)` if f is a float
|
29
|
+
// `-~i => i + 1` if i is an integer
|
30
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
31
|
+
|
32
|
+
return (size = defaultSize) => {
|
33
33
|
let id = ''
|
34
34
|
while (true) {
|
35
35
|
let bytes = getRandom(step)
|
36
|
-
|
36
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
37
|
+
let j = step | 0
|
37
38
|
while (j--) {
|
39
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
38
40
|
id += alphabet[bytes[j] & mask] || ''
|
39
41
|
if (id.length === size) return id
|
40
42
|
}
|
41
43
|
}
|
42
44
|
}
|
43
45
|
}
|
44
|
-
|
45
|
-
let
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
|
47
|
+
let customAlphabet = (alphabet, size = 21) =>
|
48
|
+
customRandom(alphabet, size, random)
|
49
|
+
|
50
|
+
let nanoid = (size = 21) =>
|
51
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
52
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
53
|
+
// The following mask reduces the random byte in the 0-255 value
|
54
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
55
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
56
|
+
// the bitmask trims bytes down to the alphabet size.
|
57
|
+
byte &= 63
|
50
58
|
if (byte < 36) {
|
59
|
+
// `0-9a-z`
|
51
60
|
id += byte.toString(36)
|
52
61
|
} else if (byte < 62) {
|
62
|
+
// `A-Z`
|
53
63
|
id += (byte - 26).toString(36).toUpperCase()
|
54
|
-
} else if (byte
|
55
|
-
id += '_'
|
56
|
-
} else {
|
64
|
+
} else if (byte > 62) {
|
57
65
|
id += '-'
|
66
|
+
} else {
|
67
|
+
id += '_'
|
58
68
|
}
|
59
|
-
|
60
|
-
|
61
|
-
|
69
|
+
return id
|
70
|
+
}, '')
|
71
|
+
|
62
72
|
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|