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/index.prod.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 (false) {
|
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 (false) {
|
|
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/non-secure/index.cjs
CHANGED
@@ -1,30 +1,21 @@
|
|
1
|
-
// This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped
|
2
|
-
// optimize the gzip compression for this alphabet.
|
3
1
|
let urlAlphabet =
|
4
2
|
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
|
5
|
-
|
6
3
|
let customAlphabet = (alphabet, size) => {
|
7
4
|
return () => {
|
8
5
|
let id = ''
|
9
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
10
6
|
let i = size
|
11
7
|
while (i--) {
|
12
|
-
// `| 0` is more compact and faster than `Math.floor()`.
|
13
8
|
id += alphabet[(Math.random() * alphabet.length) | 0]
|
14
9
|
}
|
15
10
|
return id
|
16
11
|
}
|
17
12
|
}
|
18
|
-
|
19
13
|
let nanoid = (size = 21) => {
|
20
14
|
let id = ''
|
21
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
22
15
|
let i = size
|
23
16
|
while (i--) {
|
24
|
-
// `| 0` is more compact and faster than `Math.floor()`.
|
25
17
|
id += urlAlphabet[(Math.random() * 64) | 0]
|
26
18
|
}
|
27
19
|
return id
|
28
20
|
}
|
29
|
-
|
30
21
|
module.exports = { nanoid, customAlphabet }
|
package/non-secure/index.js
CHANGED
@@ -1,30 +1,21 @@
|
|
1
|
-
// This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped
|
2
|
-
// optimize the gzip compression for this alphabet.
|
3
1
|
let urlAlphabet =
|
4
2
|
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
|
5
|
-
|
6
3
|
let customAlphabet = (alphabet, size) => {
|
7
4
|
return () => {
|
8
5
|
let id = ''
|
9
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
10
6
|
let i = size
|
11
7
|
while (i--) {
|
12
|
-
// `| 0` is more compact and faster than `Math.floor()`.
|
13
8
|
id += alphabet[(Math.random() * alphabet.length) | 0]
|
14
9
|
}
|
15
10
|
return id
|
16
11
|
}
|
17
12
|
}
|
18
|
-
|
19
13
|
let nanoid = (size = 21) => {
|
20
14
|
let id = ''
|
21
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
22
15
|
let i = size
|
23
16
|
while (i--) {
|
24
|
-
// `| 0` is more compact and faster than `Math.floor()`.
|
25
17
|
id += urlAlphabet[(Math.random() * 64) | 0]
|
26
18
|
}
|
27
19
|
return id
|
28
20
|
}
|
29
|
-
|
30
21
|
export { nanoid, customAlphabet }
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "nanoid",
|
3
|
-
"version": "3.1.
|
3
|
+
"version": "3.1.29",
|
4
4
|
"description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
|
5
5
|
"keywords": [
|
6
6
|
"uuid",
|
@@ -47,18 +47,18 @@
|
|
47
47
|
"import": "./async/index.js",
|
48
48
|
"default": "./async/index.js"
|
49
49
|
},
|
50
|
-
"./url-alphabet/package.json": "./url-alphabet/package.json",
|
51
|
-
"./url-alphabet": {
|
52
|
-
"require": "./url-alphabet/index.cjs",
|
53
|
-
"import": "./url-alphabet/index.js",
|
54
|
-
"default": "./url-alphabet/index.js"
|
55
|
-
},
|
56
50
|
"./non-secure/package.json": "./non-secure/package.json",
|
57
51
|
"./non-secure": {
|
58
52
|
"require": "./non-secure/index.cjs",
|
59
53
|
"import": "./non-secure/index.js",
|
60
54
|
"default": "./non-secure/index.js"
|
61
55
|
},
|
56
|
+
"./url-alphabet/package.json": "./url-alphabet/package.json",
|
57
|
+
"./url-alphabet": {
|
58
|
+
"require": "./url-alphabet/index.cjs",
|
59
|
+
"import": "./url-alphabet/index.js",
|
60
|
+
"default": "./url-alphabet/index.js"
|
61
|
+
},
|
62
62
|
"./index.d.ts": "./index.d.ts"
|
63
63
|
}
|
64
64
|
}
|
package/url-alphabet/index.cjs
CHANGED
package/url-alphabet/index.js
CHANGED