nanoid 3.1.25 → 3.1.29
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.
Potentially problematic release.
This version of nanoid might be problematic. Click here for more details.
- package/README.md +8 -485
- package/async/index.browser.cjs +0 -35
- package/async/index.browser.js +0 -35
- package/async/index.cjs +0 -36
- package/async/index.js +0 -36
- package/async/index.native.js +0 -31
- package/index.browser.cjs +0 -42
- package/index.browser.js +0 -42
- package/index.cjs +9 -45
- package/index.dev.js +0 -42
- package/index.js +9 -45
- package/index.prod.js +0 -42
- package/non-secure/index.cjs +0 -9
- package/non-secure/index.js +0 -9
- package/package.json +7 -7
- package/url-alphabet/index.cjs +0 -3
- package/url-alphabet/index.js +0 -3
package/async/index.cjs
CHANGED
@@ -1,14 +1,7 @@
|
|
1
1
|
let crypto = require('crypto')
|
2
|
-
|
3
2
|
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()`.
|
7
3
|
let random = bytes =>
|
8
4
|
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.
|
12
5
|
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
13
6
|
if (err) {
|
14
7
|
reject(err)
|
@@ -17,55 +10,26 @@ let random = bytes =>
|
|
17
10
|
}
|
18
11
|
})
|
19
12
|
})
|
20
|
-
|
21
13
|
let customAlphabet = (alphabet, size) => {
|
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).
|
26
14
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
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
15
|
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
40
|
-
|
41
16
|
let tick = id =>
|
42
17
|
random(step).then(bytes => {
|
43
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
44
18
|
let i = step
|
45
19
|
while (i--) {
|
46
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
47
20
|
id += alphabet[bytes[i] & mask] || ''
|
48
21
|
if (id.length === size) return id
|
49
22
|
}
|
50
23
|
return tick(id)
|
51
24
|
})
|
52
|
-
|
53
25
|
return () => tick('')
|
54
26
|
}
|
55
|
-
|
56
27
|
let nanoid = (size = 21) =>
|
57
28
|
random(size).then(bytes => {
|
58
29
|
let id = ''
|
59
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
60
30
|
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.
|
66
31
|
id += urlAlphabet[bytes[size] & 63]
|
67
32
|
}
|
68
33
|
return id
|
69
34
|
})
|
70
|
-
|
71
35
|
module.exports = { nanoid, customAlphabet, random }
|
package/async/index.js
CHANGED
@@ -1,14 +1,7 @@
|
|
1
1
|
import crypto from 'crypto'
|
2
|
-
|
3
2
|
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()`.
|
7
3
|
let random = bytes =>
|
8
4
|
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.
|
12
5
|
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
13
6
|
if (err) {
|
14
7
|
reject(err)
|
@@ -17,55 +10,26 @@ let random = bytes =>
|
|
17
10
|
}
|
18
11
|
})
|
19
12
|
})
|
20
|
-
|
21
13
|
let customAlphabet = (alphabet, size) => {
|
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).
|
26
14
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
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
15
|
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
40
|
-
|
41
16
|
let tick = id =>
|
42
17
|
random(step).then(bytes => {
|
43
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
44
18
|
let i = step
|
45
19
|
while (i--) {
|
46
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
47
20
|
id += alphabet[bytes[i] & mask] || ''
|
48
21
|
if (id.length === size) return id
|
49
22
|
}
|
50
23
|
return tick(id)
|
51
24
|
})
|
52
|
-
|
53
25
|
return () => tick('')
|
54
26
|
}
|
55
|
-
|
56
27
|
let nanoid = (size = 21) =>
|
57
28
|
random(size).then(bytes => {
|
58
29
|
let id = ''
|
59
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
60
30
|
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.
|
66
31
|
id += urlAlphabet[bytes[size] & 63]
|
67
32
|
}
|
68
33
|
return id
|
69
34
|
})
|
70
|
-
|
71
35
|
export { nanoid, customAlphabet, random }
|
package/async/index.native.js
CHANGED
@@ -1,57 +1,26 @@
|
|
1
1
|
import { getRandomBytesAsync } from 'expo-random'
|
2
|
-
|
3
2
|
import { urlAlphabet } from '../url-alphabet/index.js'
|
4
|
-
|
5
3
|
let random = getRandomBytesAsync
|
6
|
-
|
7
4
|
let customAlphabet = (alphabet, size) => {
|
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).
|
12
5
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
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
6
|
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
26
|
-
|
27
7
|
let tick = id =>
|
28
8
|
random(step).then(bytes => {
|
29
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
30
9
|
let i = step
|
31
10
|
while (i--) {
|
32
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
33
11
|
id += alphabet[bytes[i] & mask] || ''
|
34
12
|
if (id.length === size) return id
|
35
13
|
}
|
36
14
|
return tick(id)
|
37
15
|
})
|
38
|
-
|
39
16
|
return () => tick('')
|
40
17
|
}
|
41
|
-
|
42
18
|
let nanoid = (size = 21) =>
|
43
19
|
random(size).then(bytes => {
|
44
20
|
let id = ''
|
45
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
46
21
|
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.
|
52
22
|
id += urlAlphabet[bytes[size] & 63]
|
53
23
|
}
|
54
24
|
return id
|
55
25
|
})
|
56
|
-
|
57
26
|
export { nanoid, customAlphabet, random }
|
package/index.browser.cjs
CHANGED
@@ -1,10 +1,5 @@
|
|
1
|
-
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
-
// according to `browser` config in `package.json`.
|
3
|
-
|
4
1
|
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
5
|
-
|
6
2
|
if (process.env.NODE_ENV !== 'production') {
|
7
|
-
// All bundlers will remove this block in the production bundle.
|
8
3
|
if (
|
9
4
|
typeof navigator !== 'undefined' &&
|
10
5
|
navigator.product === 'ReactNative' &&
|
@@ -30,67 +25,31 @@ if (process.env.NODE_ENV !== 'production') {
|
|
30
25
|
)
|
31
26
|
}
|
32
27
|
}
|
33
|
-
|
34
28
|
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
35
|
-
|
36
29
|
let customRandom = (alphabet, size, getRandom) => {
|
37
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
38
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
39
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
40
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
41
|
-
// `Math.clz32` is not used, because it is not available in browsers.
|
42
30
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
43
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
44
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
45
|
-
// the random bytes redundancy has to be satisfied.
|
46
|
-
|
47
|
-
// Note: every hardware random generator call is performance expensive,
|
48
|
-
// because the system call for entropy collection takes a lot of time.
|
49
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
50
|
-
|
51
|
-
// Next, a step determines how many random bytes to generate.
|
52
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
53
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
54
|
-
// according to benchmarks).
|
55
|
-
|
56
|
-
// `-~f => Math.ceil(f)` if f is a float
|
57
|
-
// `-~i => i + 1` if i is an integer
|
58
31
|
let step = -~((1.6 * mask * size) / alphabet.length)
|
59
|
-
|
60
32
|
return () => {
|
61
33
|
let id = ''
|
62
34
|
while (true) {
|
63
35
|
let bytes = getRandom(step)
|
64
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
65
36
|
let j = step
|
66
37
|
while (j--) {
|
67
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
68
38
|
id += alphabet[bytes[j] & mask] || ''
|
69
39
|
if (id.length === size) return id
|
70
40
|
}
|
71
41
|
}
|
72
42
|
}
|
73
43
|
}
|
74
|
-
|
75
44
|
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
76
|
-
|
77
45
|
let nanoid = (size = 21) => {
|
78
46
|
let id = ''
|
79
47
|
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
80
|
-
|
81
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
82
48
|
while (size--) {
|
83
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
84
|
-
// The following mask reduces the random byte in the 0-255 value
|
85
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
86
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
87
|
-
// the bitmask trims bytes down to the alphabet size.
|
88
49
|
let byte = bytes[size] & 63
|
89
50
|
if (byte < 36) {
|
90
|
-
// `0-9a-z`
|
91
51
|
id += byte.toString(36)
|
92
52
|
} else if (byte < 62) {
|
93
|
-
// `A-Z`
|
94
53
|
id += (byte - 26).toString(36).toUpperCase()
|
95
54
|
} else if (byte < 63) {
|
96
55
|
id += '_'
|
@@ -100,5 +59,4 @@ let nanoid = (size = 21) => {
|
|
100
59
|
}
|
101
60
|
return id
|
102
61
|
}
|
103
|
-
|
104
62
|
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.browser.js
CHANGED
@@ -1,10 +1,5 @@
|
|
1
|
-
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
-
// according to `browser` config in `package.json`.
|
3
|
-
|
4
1
|
import { urlAlphabet } from './url-alphabet/index.js'
|
5
|
-
|
6
2
|
if (process.env.NODE_ENV !== 'production') {
|
7
|
-
// All bundlers will remove this block in the production bundle.
|
8
3
|
if (
|
9
4
|
typeof navigator !== 'undefined' &&
|
10
5
|
navigator.product === 'ReactNative' &&
|
@@ -30,67 +25,31 @@ if (process.env.NODE_ENV !== 'production') {
|
|
30
25
|
)
|
31
26
|
}
|
32
27
|
}
|
33
|
-
|
34
28
|
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
35
|
-
|
36
29
|
let customRandom = (alphabet, size, getRandom) => {
|
37
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
38
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
39
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
40
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
41
|
-
// `Math.clz32` is not used, because it is not available in browsers.
|
42
30
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
43
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
44
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
45
|
-
// the random bytes redundancy has to be satisfied.
|
46
|
-
|
47
|
-
// Note: every hardware random generator call is performance expensive,
|
48
|
-
// because the system call for entropy collection takes a lot of time.
|
49
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
50
|
-
|
51
|
-
// Next, a step determines how many random bytes to generate.
|
52
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
53
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
54
|
-
// according to benchmarks).
|
55
|
-
|
56
|
-
// `-~f => Math.ceil(f)` if f is a float
|
57
|
-
// `-~i => i + 1` if i is an integer
|
58
31
|
let step = -~((1.6 * mask * size) / alphabet.length)
|
59
|
-
|
60
32
|
return () => {
|
61
33
|
let id = ''
|
62
34
|
while (true) {
|
63
35
|
let bytes = getRandom(step)
|
64
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
65
36
|
let j = step
|
66
37
|
while (j--) {
|
67
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
68
38
|
id += alphabet[bytes[j] & mask] || ''
|
69
39
|
if (id.length === size) return id
|
70
40
|
}
|
71
41
|
}
|
72
42
|
}
|
73
43
|
}
|
74
|
-
|
75
44
|
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
76
|
-
|
77
45
|
let nanoid = (size = 21) => {
|
78
46
|
let id = ''
|
79
47
|
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
80
|
-
|
81
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
82
48
|
while (size--) {
|
83
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
84
|
-
// The following mask reduces the random byte in the 0-255 value
|
85
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
86
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
87
|
-
// the bitmask trims bytes down to the alphabet size.
|
88
49
|
let byte = bytes[size] & 63
|
89
50
|
if (byte < 36) {
|
90
|
-
// `0-9a-z`
|
91
51
|
id += byte.toString(36)
|
92
52
|
} else if (byte < 62) {
|
93
|
-
// `A-Z`
|
94
53
|
id += (byte - 26).toString(36).toUpperCase()
|
95
54
|
} else if (byte < 63) {
|
96
55
|
id += '_'
|
@@ -100,5 +59,4 @@ let nanoid = (size = 21) => {
|
|
100
59
|
}
|
101
60
|
return id
|
102
61
|
}
|
103
|
-
|
104
62
|
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.cjs
CHANGED
@@ -1,16 +1,8 @@
|
|
1
1
|
let crypto = require('crypto')
|
2
|
-
|
3
2
|
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
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.
|
10
|
-
const POOL_SIZE_MULTIPLIER = 32
|
3
|
+
const POOL_SIZE_MULTIPLIER = 128
|
11
4
|
let pool, poolOffset
|
12
|
-
|
13
|
-
let random = bytes => {
|
5
|
+
let fillPool = bytes => {
|
14
6
|
if (!pool || pool.length < bytes) {
|
15
7
|
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
16
8
|
crypto.randomFillSync(pool)
|
@@ -19,62 +11,34 @@ let random = bytes => {
|
|
19
11
|
crypto.randomFillSync(pool)
|
20
12
|
poolOffset = 0
|
21
13
|
}
|
22
|
-
|
23
|
-
let res = pool.subarray(poolOffset, poolOffset + bytes)
|
24
14
|
poolOffset += bytes
|
25
|
-
return res
|
26
15
|
}
|
27
|
-
|
16
|
+
let random = bytes => {
|
17
|
+
fillPool(bytes)
|
18
|
+
return pool.subarray(poolOffset - bytes, poolOffset)
|
19
|
+
}
|
28
20
|
let customRandom = (alphabet, size, getRandom) => {
|
29
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
30
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
31
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
32
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
33
21
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
34
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
35
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
36
|
-
// the random bytes redundancy has to be satisfied.
|
37
|
-
|
38
|
-
// Note: every hardware random generator call is performance expensive,
|
39
|
-
// because the system call for entropy collection takes a lot of time.
|
40
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
41
|
-
|
42
|
-
// Next, a step determines how many random bytes to generate.
|
43
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
44
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
45
|
-
// according to benchmarks).
|
46
22
|
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
47
|
-
|
48
23
|
return () => {
|
49
24
|
let id = ''
|
50
25
|
while (true) {
|
51
26
|
let bytes = getRandom(step)
|
52
|
-
// A compact alternative for `for (let i = 0; i < step; i++)`.
|
53
27
|
let i = step
|
54
28
|
while (i--) {
|
55
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
56
29
|
id += alphabet[bytes[i] & mask] || ''
|
57
30
|
if (id.length === size) return id
|
58
31
|
}
|
59
32
|
}
|
60
33
|
}
|
61
34
|
}
|
62
|
-
|
63
35
|
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
64
|
-
|
65
36
|
let nanoid = (size = 21) => {
|
66
|
-
|
37
|
+
fillPool(size)
|
67
38
|
let id = ''
|
68
|
-
|
69
|
-
|
70
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
71
|
-
// The following mask reduces the random byte in the 0-255 value
|
72
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
73
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
74
|
-
// the bitmask trims bytes down to the alphabet size.
|
75
|
-
id += urlAlphabet[bytes[size] & 63]
|
39
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
40
|
+
id += urlAlphabet[pool[i] & 63]
|
76
41
|
}
|
77
42
|
return id
|
78
43
|
}
|
79
|
-
|
80
44
|
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.dev.js
CHANGED
@@ -1,10 +1,5 @@
|
|
1
|
-
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
-
// according to `browser` config in `package.json`.
|
3
|
-
|
4
1
|
import { urlAlphabet } from './url-alphabet/index.js'
|
5
|
-
|
6
2
|
if (true) {
|
7
|
-
// All bundlers will remove this block in the production bundle.
|
8
3
|
if (
|
9
4
|
typeof navigator !== 'undefined' &&
|
10
5
|
navigator.product === 'ReactNative' &&
|
@@ -30,67 +25,31 @@ if (true) {
|
|
30
25
|
)
|
31
26
|
}
|
32
27
|
}
|
33
|
-
|
34
28
|
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
35
|
-
|
36
29
|
let customRandom = (alphabet, size, getRandom) => {
|
37
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
38
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
39
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
40
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
41
|
-
// `Math.clz32` is not used, because it is not available in browsers.
|
42
30
|
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
43
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
44
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
45
|
-
// the random bytes redundancy has to be satisfied.
|
46
|
-
|
47
|
-
// Note: every hardware random generator call is performance expensive,
|
48
|
-
// because the system call for entropy collection takes a lot of time.
|
49
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
50
|
-
|
51
|
-
// Next, a step determines how many random bytes to generate.
|
52
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
53
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
54
|
-
// according to benchmarks).
|
55
|
-
|
56
|
-
// `-~f => Math.ceil(f)` if f is a float
|
57
|
-
// `-~i => i + 1` if i is an integer
|
58
31
|
let step = -~((1.6 * mask * size) / alphabet.length)
|
59
|
-
|
60
32
|
return () => {
|
61
33
|
let id = ''
|
62
34
|
while (true) {
|
63
35
|
let bytes = getRandom(step)
|
64
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
65
36
|
let j = step
|
66
37
|
while (j--) {
|
67
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
68
38
|
id += alphabet[bytes[j] & mask] || ''
|
69
39
|
if (id.length === size) return id
|
70
40
|
}
|
71
41
|
}
|
72
42
|
}
|
73
43
|
}
|
74
|
-
|
75
44
|
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
76
|
-
|
77
45
|
let nanoid = (size = 21) => {
|
78
46
|
let id = ''
|
79
47
|
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
80
|
-
|
81
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
82
48
|
while (size--) {
|
83
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
84
|
-
// The following mask reduces the random byte in the 0-255 value
|
85
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
86
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
87
|
-
// the bitmask trims bytes down to the alphabet size.
|
88
49
|
let byte = bytes[size] & 63
|
89
50
|
if (byte < 36) {
|
90
|
-
// `0-9a-z`
|
91
51
|
id += byte.toString(36)
|
92
52
|
} else if (byte < 62) {
|
93
|
-
// `A-Z`
|
94
53
|
id += (byte - 26).toString(36).toUpperCase()
|
95
54
|
} else if (byte < 63) {
|
96
55
|
id += '_'
|
@@ -100,5 +59,4 @@ let nanoid = (size = 21) => {
|
|
100
59
|
}
|
101
60
|
return id
|
102
61
|
}
|
103
|
-
|
104
62
|
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.js
CHANGED
@@ -1,16 +1,8 @@
|
|
1
1
|
import crypto from 'crypto'
|
2
|
-
|
3
2
|
import { urlAlphabet } from './url-alphabet/index.js'
|
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.
|
10
|
-
const POOL_SIZE_MULTIPLIER = 32
|
3
|
+
const POOL_SIZE_MULTIPLIER = 128
|
11
4
|
let pool, poolOffset
|
12
|
-
|
13
|
-
let random = bytes => {
|
5
|
+
let fillPool = bytes => {
|
14
6
|
if (!pool || pool.length < bytes) {
|
15
7
|
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
16
8
|
crypto.randomFillSync(pool)
|
@@ -19,62 +11,34 @@ let random = bytes => {
|
|
19
11
|
crypto.randomFillSync(pool)
|
20
12
|
poolOffset = 0
|
21
13
|
}
|
22
|
-
|
23
|
-
let res = pool.subarray(poolOffset, poolOffset + bytes)
|
24
14
|
poolOffset += bytes
|
25
|
-
return res
|
26
15
|
}
|
27
|
-
|
16
|
+
let random = bytes => {
|
17
|
+
fillPool(bytes)
|
18
|
+
return pool.subarray(poolOffset - bytes, poolOffset)
|
19
|
+
}
|
28
20
|
let customRandom = (alphabet, size, getRandom) => {
|
29
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
30
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
31
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
32
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
33
21
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
34
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
35
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
36
|
-
// the random bytes redundancy has to be satisfied.
|
37
|
-
|
38
|
-
// Note: every hardware random generator call is performance expensive,
|
39
|
-
// because the system call for entropy collection takes a lot of time.
|
40
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
41
|
-
|
42
|
-
// Next, a step determines how many random bytes to generate.
|
43
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
44
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
45
|
-
// according to benchmarks).
|
46
22
|
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
47
|
-
|
48
23
|
return () => {
|
49
24
|
let id = ''
|
50
25
|
while (true) {
|
51
26
|
let bytes = getRandom(step)
|
52
|
-
// A compact alternative for `for (let i = 0; i < step; i++)`.
|
53
27
|
let i = step
|
54
28
|
while (i--) {
|
55
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
56
29
|
id += alphabet[bytes[i] & mask] || ''
|
57
30
|
if (id.length === size) return id
|
58
31
|
}
|
59
32
|
}
|
60
33
|
}
|
61
34
|
}
|
62
|
-
|
63
35
|
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
64
|
-
|
65
36
|
let nanoid = (size = 21) => {
|
66
|
-
|
37
|
+
fillPool(size)
|
67
38
|
let id = ''
|
68
|
-
|
69
|
-
|
70
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
71
|
-
// The following mask reduces the random byte in the 0-255 value
|
72
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
73
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
74
|
-
// the bitmask trims bytes down to the alphabet size.
|
75
|
-
id += urlAlphabet[bytes[size] & 63]
|
39
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
40
|
+
id += urlAlphabet[pool[i] & 63]
|
76
41
|
}
|
77
42
|
return id
|
78
43
|
}
|
79
|
-
|
80
44
|
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|