nanoid 3.1.16 → 3.1.20

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 CHANGED
@@ -1,8 +1,20 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 3.1.20
5
+ * Fix ES modules support.
6
+
7
+ ## 3.1.19
8
+ * Reduced `customAlphabet` size (by Enrico Scherlies).
9
+
10
+ ## 3.1.18
11
+ * Fixed `package.exports`.
12
+
13
+ ## 3.1.17
14
+ * Added files without `process`.
15
+
4
16
  ## 3.1.16
5
- * Speed up Nano ID 4 times (by Peter Boyer).
17
+ * Speeded up Nano ID 4 times (by Peter Boyer).
6
18
 
7
19
  ## 3.1.15
8
20
  * Fixed `package.types` path.
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 40% faster than UUID.
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_-`).
@@ -74,9 +74,9 @@ There are three main differences between Nano ID and UUID v4:
74
74
 
75
75
  1. Nano ID uses a bigger alphabet, so a similar number of random bits
76
76
  are packed in just 21 symbols instead of 36.
77
- 2. Nano ID code is 3 times less than `uuid/v4` package:
78
- 108 bytes instead of 345.
79
- 3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.
77
+ 2. Nano ID code is **4.5 times less** than `uuid/v4` package:
78
+ 108 bytes instead of 483.
79
+ 3. Because of memory allocation tricks, Nano ID is **60%** faster than UUID.
80
80
 
81
81
 
82
82
  ## Benchmark
@@ -85,8 +85,8 @@ There are three main differences between Nano ID and UUID v4:
85
85
  $ node ./test/benchmark.js
86
86
  nanoid 2,280,683 ops/sec
87
87
  customAlphabet 1,851,117 ops/sec
88
- uid.sync 313,306 ops/sec
89
88
  uuid v4 1,348,425 ops/sec
89
+ uid.sync 313,306 ops/sec
90
90
  secure-random-string 294,161 ops/sec
91
91
  cuid 158,988 ops/sec
92
92
  shortid 37,222 ops/sec
@@ -34,8 +34,7 @@ let customAlphabet = (alphabet, size) => {
34
34
  while (i--) {
35
35
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
36
36
  id += alphabet[bytes[i] & mask] || ''
37
- // `id.length + 1 === size` is a more compact option.
38
- if (id.length === +size) return Promise.resolve(id)
37
+ if (id.length === size) return Promise.resolve(id)
39
38
  }
40
39
  }
41
40
  }
package/async/index.cjs CHANGED
@@ -45,8 +45,7 @@ let customAlphabet = (alphabet, size) => {
45
45
  while (i--) {
46
46
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
47
47
  id += alphabet[bytes[i] & mask] || ''
48
- // `id.length + 1 === size` is a more compact option.
49
- if (id.length === +size) return id
48
+ if (id.length === size) return id
50
49
  }
51
50
  return tick(id)
52
51
  })
package/async/index.js CHANGED
@@ -45,8 +45,7 @@ let customAlphabet = (alphabet, size) => {
45
45
  while (i--) {
46
46
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
47
47
  id += alphabet[bytes[i] & mask] || ''
48
- // `id.length + 1 === size` is a more compact option.
49
- if (id.length === +size) return id
48
+ if (id.length === size) return id
50
49
  }
51
50
  return tick(id)
52
51
  })
@@ -31,8 +31,7 @@ let customAlphabet = (alphabet, size) => {
31
31
  while (i--) {
32
32
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
33
33
  id += alphabet[bytes[i] & mask] || ''
34
- // `id.length + 1 === size` is a more compact option.
35
- if (id.length === +size) return id
34
+ if (id.length === size) return id
36
35
  }
37
36
  return tick(id)
38
37
  })
package/index.browser.js CHANGED
@@ -67,8 +67,7 @@ let customRandom = (alphabet, size, getRandom) => {
67
67
  while (j--) {
68
68
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
69
69
  id += alphabet[bytes[j] & mask] || ''
70
- // `id.length + 1 === size` is a more compact option.
71
- if (id.length === +size) return id
70
+ if (id.length === size) return id
72
71
  }
73
72
  }
74
73
  }
package/index.cjs CHANGED
@@ -54,8 +54,7 @@ let customRandom = (alphabet, size, getRandom) => {
54
54
  while (i--) {
55
55
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
56
56
  id += alphabet[bytes[i] & mask] || ''
57
- // `id.length + 1 === size` is a more compact option.
58
- if (id.length === +size) return id
57
+ if (id.length === size) return id
59
58
  }
60
59
  }
61
60
  }
package/index.dev.js ADDED
@@ -0,0 +1,105 @@
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
+ if (id.length === size) return id
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
77
+
78
+ let nanoid = (size = 21) => {
79
+ let id = ''
80
+ let bytes = crypto.getRandomValues(new Uint8Array(size))
81
+
82
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
83
+ while (size--) {
84
+ // It is incorrect to use bytes exceeding the alphabet size.
85
+ // The following mask reduces the random byte in the 0-255 value
86
+ // range to the 0-63 value range. Therefore, adding hacks, such
87
+ // as empty string fallback or magic numbers, is unneccessary because
88
+ // the bitmask trims bytes down to the alphabet size.
89
+ let byte = bytes[size] & 63
90
+ if (byte < 36) {
91
+ // `0-9a-z`
92
+ id += byte.toString(36)
93
+ } else if (byte < 62) {
94
+ // `A-Z`
95
+ id += (byte - 26).toString(36).toUpperCase()
96
+ } else if (byte < 63) {
97
+ id += '_'
98
+ } else {
99
+ id += '-'
100
+ }
101
+ }
102
+ return id
103
+ }
104
+
105
+ export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
package/index.js CHANGED
@@ -54,8 +54,7 @@ let customRandom = (alphabet, size, getRandom) => {
54
54
  while (i--) {
55
55
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
56
56
  id += alphabet[bytes[i] & mask] || ''
57
- // `id.length + 1 === size` is a more compact option.
58
- if (id.length === +size) return id
57
+ if (id.length === size) return id
59
58
  }
60
59
  }
61
60
  }
package/index.prod.js ADDED
@@ -0,0 +1,105 @@
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
+ if (id.length === size) return id
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
77
+
78
+ let nanoid = (size = 21) => {
79
+ let id = ''
80
+ let bytes = crypto.getRandomValues(new Uint8Array(size))
81
+
82
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
83
+ while (size--) {
84
+ // It is incorrect to use bytes exceeding the alphabet size.
85
+ // The following mask reduces the random byte in the 0-255 value
86
+ // range to the 0-63 value range. Therefore, adding hacks, such
87
+ // as empty string fallback or magic numbers, is unneccessary because
88
+ // the bitmask trims bytes down to the alphabet size.
89
+ let byte = bytes[size] & 63
90
+ if (byte < 36) {
91
+ // `0-9a-z`
92
+ id += byte.toString(36)
93
+ } else if (byte < 62) {
94
+ // `A-Z`
95
+ id += (byte - 26).toString(36).toUpperCase()
96
+ } else if (byte < 63) {
97
+ id += '_'
98
+ } else {
99
+ id += '-'
100
+ }
101
+ }
102
+ return id
103
+ }
104
+
105
+ export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.16",
3
+ "version": "3.1.20",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -26,7 +26,10 @@
26
26
  "module": "index.js",
27
27
  "exports": {
28
28
  ".": {
29
- "browser": "./index.browser.js",
29
+ "browser": {
30
+ "development": "./index.dev.js",
31
+ "production": "./index.prod.js"
32
+ },
30
33
  "require": "./index.cjs",
31
34
  "import": "./index.js",
32
35
  "types": "./index.d.ts"