nanoid 3.1.14 → 3.1.18
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/CHANGELOG.md +12 -0
- package/README.md +17 -17
- package/index.cjs +19 -11
- package/index.dev.js +106 -0
- package/index.js +19 -11
- package/index.prod.js +106 -0
- package/package.json +8 -5
package/CHANGELOG.md
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
# Change Log
|
2
2
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
3
3
|
|
4
|
+
## 3.1.18
|
5
|
+
* Fixed `package.exports`.
|
6
|
+
|
7
|
+
## 3.1.17
|
8
|
+
* Added files without `process`.
|
9
|
+
|
10
|
+
## 3.1.16
|
11
|
+
* Speeded up Nano ID 4 times (by Peter Boyer).
|
12
|
+
|
13
|
+
## 3.1.15
|
14
|
+
* Fixed `package.types` path.
|
15
|
+
|
4
16
|
## 3.1.14
|
5
17
|
* Added `package.types`.
|
6
18
|
|
package/README.md
CHANGED
@@ -10,7 +10,7 @@ A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
|
|
10
10
|
|
11
11
|
* **Small.** 108 bytes (minified and gzipped). No dependencies.
|
12
12
|
[Size Limit] controls the size.
|
13
|
-
* **Fast.** It is
|
13
|
+
* **Fast.** It is 60% faster than UUID.
|
14
14
|
* **Safe.** It uses cryptographically strong random APIs.
|
15
15
|
Can be used in clusters.
|
16
16
|
* **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
|
@@ -82,27 +82,27 @@ There are three main differences between Nano ID and UUID v4:
|
|
82
82
|
## Benchmark
|
83
83
|
|
84
84
|
```rust
|
85
|
-
$ ./test/benchmark
|
86
|
-
nanoid
|
87
|
-
customAlphabet
|
88
|
-
uid.sync
|
89
|
-
uuid v4
|
90
|
-
secure-random-string
|
91
|
-
cuid
|
92
|
-
shortid
|
85
|
+
$ node ./test/benchmark.js
|
86
|
+
nanoid 2,280,683 ops/sec
|
87
|
+
customAlphabet 1,851,117 ops/sec
|
88
|
+
uid.sync 313,306 ops/sec
|
89
|
+
uuid v4 1,348,425 ops/sec
|
90
|
+
secure-random-string 294,161 ops/sec
|
91
|
+
cuid 158,988 ops/sec
|
92
|
+
shortid 37,222 ops/sec
|
93
93
|
|
94
94
|
Async:
|
95
|
-
async nanoid
|
96
|
-
async customAlphabet
|
97
|
-
async secure-random-string
|
98
|
-
uid
|
95
|
+
async nanoid 95,500 ops/sec
|
96
|
+
async customAlphabet 93,800 ops/sec
|
97
|
+
async secure-random-string 90,316 ops/sec
|
98
|
+
uid 85,583 ops/sec
|
99
99
|
|
100
100
|
Non-secure:
|
101
|
-
non-secure nanoid 2,
|
102
|
-
rndm 2,
|
101
|
+
non-secure nanoid 2,641,654 ops/sec
|
102
|
+
rndm 2,447,086 ops/sec
|
103
103
|
```
|
104
104
|
|
105
|
-
Test configuration: Dell XPS 2-in-
|
105
|
+
Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1.
|
106
106
|
|
107
107
|
|
108
108
|
## Tools
|
@@ -151,7 +151,7 @@ with 21 characters (to have a collision probability similar to UUID v4).
|
|
151
151
|
|
152
152
|
```js
|
153
153
|
import { nanoid } from 'nanoid'
|
154
|
-
model.id = nanoid() //=> "
|
154
|
+
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
|
155
155
|
```
|
156
156
|
|
157
157
|
If you want to reduce the ID size (and increase collisions probability),
|
package/index.cjs
CHANGED
@@ -2,19 +2,27 @@ let crypto = require('crypto')
|
|
2
2
|
|
3
3
|
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
4
4
|
|
5
|
-
//
|
6
|
-
//
|
7
|
-
|
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
|
11
|
+
let pool, poolOffset
|
12
|
+
|
8
13
|
let random = bytes => {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
if (!pool || pool.length < bytes) {
|
15
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
16
|
+
crypto.randomFillSync(pool)
|
17
|
+
poolOffset = 0
|
18
|
+
} else if (poolOffset + bytes > pool.length) {
|
19
|
+
crypto.randomFillSync(pool)
|
20
|
+
poolOffset = 0
|
16
21
|
}
|
17
|
-
|
22
|
+
|
23
|
+
let res = pool.subarray(poolOffset, poolOffset + bytes)
|
24
|
+
poolOffset += bytes
|
25
|
+
return res
|
18
26
|
}
|
19
27
|
|
20
28
|
let customRandom = (alphabet, size, getRandom) => {
|
package/index.dev.js
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
+
// according to `browser` config in `package.json`.
|
3
|
+
|
4
|
+
import { urlAlphabet } from './url-alphabet/index.js'
|
5
|
+
|
6
|
+
if (true) {
|
7
|
+
// All bundlers will remove this block in the production bundle.
|
8
|
+
if (
|
9
|
+
typeof navigator !== 'undefined' &&
|
10
|
+
navigator.product === 'ReactNative' &&
|
11
|
+
typeof crypto === 'undefined'
|
12
|
+
) {
|
13
|
+
throw new Error(
|
14
|
+
'React Native does not have a built-in secure random generator. ' +
|
15
|
+
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
16
|
+
'For secure IDs, import `react-native-get-random-values` ' +
|
17
|
+
'before Nano ID. If you use Expo, install `expo-random` ' +
|
18
|
+
'and use `nanoid/async`.'
|
19
|
+
)
|
20
|
+
}
|
21
|
+
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
22
|
+
throw new Error(
|
23
|
+
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
24
|
+
' before importing Nano ID to fix IE 11 support'
|
25
|
+
)
|
26
|
+
}
|
27
|
+
if (typeof crypto === 'undefined') {
|
28
|
+
throw new Error(
|
29
|
+
'Your browser does not have secure random generator. ' +
|
30
|
+
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
31
|
+
)
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
36
|
+
|
37
|
+
let customRandom = (alphabet, size, getRandom) => {
|
38
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
39
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
40
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
41
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
42
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
43
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
44
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
45
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
46
|
+
// the random bytes redundancy has to be satisfied.
|
47
|
+
|
48
|
+
// Note: every hardware random generator call is performance expensive,
|
49
|
+
// because the system call for entropy collection takes a lot of time.
|
50
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
51
|
+
|
52
|
+
// Next, a step determines how many random bytes to generate.
|
53
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
54
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
55
|
+
// according to benchmarks).
|
56
|
+
|
57
|
+
// `-~f => Math.ceil(f)` if f is a float
|
58
|
+
// `-~i => i + 1` if i is an integer
|
59
|
+
let step = -~((1.6 * mask * size) / alphabet.length)
|
60
|
+
|
61
|
+
return () => {
|
62
|
+
let id = ''
|
63
|
+
while (true) {
|
64
|
+
let bytes = getRandom(step)
|
65
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
66
|
+
let j = step
|
67
|
+
while (j--) {
|
68
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
69
|
+
id += alphabet[bytes[j] & mask] || ''
|
70
|
+
// `id.length + 1 === size` is a more compact option.
|
71
|
+
if (id.length === +size) return id
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
78
|
+
|
79
|
+
let nanoid = (size = 21) => {
|
80
|
+
let id = ''
|
81
|
+
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
82
|
+
|
83
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
84
|
+
while (size--) {
|
85
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
86
|
+
// The following mask reduces the random byte in the 0-255 value
|
87
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
88
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
89
|
+
// the bitmask trims bytes down to the alphabet size.
|
90
|
+
let byte = bytes[size] & 63
|
91
|
+
if (byte < 36) {
|
92
|
+
// `0-9a-z`
|
93
|
+
id += byte.toString(36)
|
94
|
+
} else if (byte < 62) {
|
95
|
+
// `A-Z`
|
96
|
+
id += (byte - 26).toString(36).toUpperCase()
|
97
|
+
} else if (byte < 63) {
|
98
|
+
id += '_'
|
99
|
+
} else {
|
100
|
+
id += '-'
|
101
|
+
}
|
102
|
+
}
|
103
|
+
return id
|
104
|
+
}
|
105
|
+
|
106
|
+
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.js
CHANGED
@@ -2,19 +2,27 @@ import crypto from 'crypto'
|
|
2
2
|
|
3
3
|
import { urlAlphabet } from './url-alphabet/index.js'
|
4
4
|
|
5
|
-
//
|
6
|
-
//
|
7
|
-
|
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
|
11
|
+
let pool, poolOffset
|
12
|
+
|
8
13
|
let random = bytes => {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
if (!pool || pool.length < bytes) {
|
15
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
16
|
+
crypto.randomFillSync(pool)
|
17
|
+
poolOffset = 0
|
18
|
+
} else if (poolOffset + bytes > pool.length) {
|
19
|
+
crypto.randomFillSync(pool)
|
20
|
+
poolOffset = 0
|
16
21
|
}
|
17
|
-
|
22
|
+
|
23
|
+
let res = pool.subarray(poolOffset, poolOffset + bytes)
|
24
|
+
poolOffset += bytes
|
25
|
+
return res
|
18
26
|
}
|
19
27
|
|
20
28
|
let customRandom = (alphabet, size, getRandom) => {
|
package/index.prod.js
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
2
|
+
// according to `browser` config in `package.json`.
|
3
|
+
|
4
|
+
import { urlAlphabet } from './url-alphabet/index.js'
|
5
|
+
|
6
|
+
if (false) {
|
7
|
+
// All bundlers will remove this block in the production bundle.
|
8
|
+
if (
|
9
|
+
typeof navigator !== 'undefined' &&
|
10
|
+
navigator.product === 'ReactNative' &&
|
11
|
+
typeof crypto === 'undefined'
|
12
|
+
) {
|
13
|
+
throw new Error(
|
14
|
+
'React Native does not have a built-in secure random generator. ' +
|
15
|
+
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
16
|
+
'For secure IDs, import `react-native-get-random-values` ' +
|
17
|
+
'before Nano ID. If you use Expo, install `expo-random` ' +
|
18
|
+
'and use `nanoid/async`.'
|
19
|
+
)
|
20
|
+
}
|
21
|
+
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
22
|
+
throw new Error(
|
23
|
+
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
24
|
+
' before importing Nano ID to fix IE 11 support'
|
25
|
+
)
|
26
|
+
}
|
27
|
+
if (typeof crypto === 'undefined') {
|
28
|
+
throw new Error(
|
29
|
+
'Your browser does not have secure random generator. ' +
|
30
|
+
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
31
|
+
)
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
36
|
+
|
37
|
+
let customRandom = (alphabet, size, getRandom) => {
|
38
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
39
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
40
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
41
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
42
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
43
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
44
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
45
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
46
|
+
// the random bytes redundancy has to be satisfied.
|
47
|
+
|
48
|
+
// Note: every hardware random generator call is performance expensive,
|
49
|
+
// because the system call for entropy collection takes a lot of time.
|
50
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
51
|
+
|
52
|
+
// Next, a step determines how many random bytes to generate.
|
53
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
54
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
55
|
+
// according to benchmarks).
|
56
|
+
|
57
|
+
// `-~f => Math.ceil(f)` if f is a float
|
58
|
+
// `-~i => i + 1` if i is an integer
|
59
|
+
let step = -~((1.6 * mask * size) / alphabet.length)
|
60
|
+
|
61
|
+
return () => {
|
62
|
+
let id = ''
|
63
|
+
while (true) {
|
64
|
+
let bytes = getRandom(step)
|
65
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
66
|
+
let j = step
|
67
|
+
while (j--) {
|
68
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
69
|
+
id += alphabet[bytes[j] & mask] || ''
|
70
|
+
// `id.length + 1 === size` is a more compact option.
|
71
|
+
if (id.length === +size) return id
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
78
|
+
|
79
|
+
let nanoid = (size = 21) => {
|
80
|
+
let id = ''
|
81
|
+
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
82
|
+
|
83
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
84
|
+
while (size--) {
|
85
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
86
|
+
// The following mask reduces the random byte in the 0-255 value
|
87
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
88
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
89
|
+
// the bitmask trims bytes down to the alphabet size.
|
90
|
+
let byte = bytes[size] & 63
|
91
|
+
if (byte < 36) {
|
92
|
+
// `0-9a-z`
|
93
|
+
id += byte.toString(36)
|
94
|
+
} else if (byte < 62) {
|
95
|
+
// `A-Z`
|
96
|
+
id += (byte - 26).toString(36).toUpperCase()
|
97
|
+
} else if (byte < 63) {
|
98
|
+
id += '_'
|
99
|
+
} else {
|
100
|
+
id += '-'
|
101
|
+
}
|
102
|
+
}
|
103
|
+
return id
|
104
|
+
}
|
105
|
+
|
106
|
+
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "nanoid",
|
3
|
-
"version": "3.1.
|
3
|
+
"version": "3.1.18",
|
4
4
|
"description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
|
5
5
|
"keywords": [
|
6
6
|
"uuid",
|
@@ -20,16 +20,19 @@
|
|
20
20
|
"react-native": "index.js",
|
21
21
|
"bin": "./bin/nanoid.cjs",
|
22
22
|
"sideEffects": false,
|
23
|
-
"types": "index.d.ts",
|
23
|
+
"types": "./index.d.ts",
|
24
24
|
"type": "module",
|
25
25
|
"main": "index.cjs",
|
26
26
|
"module": "index.js",
|
27
27
|
"exports": {
|
28
28
|
".": {
|
29
|
-
"browser":
|
29
|
+
"browser": {
|
30
|
+
"development": "./index.dev.js",
|
31
|
+
"production": "./index.prod.js"
|
32
|
+
},
|
30
33
|
"require": "./index.cjs",
|
31
34
|
"import": "./index.js",
|
32
|
-
"types": "index.d.ts"
|
35
|
+
"types": "./index.d.ts"
|
33
36
|
},
|
34
37
|
"./package.json": "./package.json",
|
35
38
|
"./async/package.json": "./async/package.json",
|
@@ -48,6 +51,6 @@
|
|
48
51
|
"require": "./url-alphabet/index.cjs",
|
49
52
|
"import": "./url-alphabet/index.js"
|
50
53
|
},
|
51
|
-
"index.d.ts": "index.d.ts"
|
54
|
+
"./index.d.ts": "./index.d.ts"
|
52
55
|
}
|
53
56
|
}
|