nanoid 3.1.21 → 3.1.25

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 CHANGED
@@ -246,17 +246,15 @@ If you want to use Nano ID in the `id` prop, you must set some string prefix
246
246
 
247
247
  ### Create React App
248
248
 
249
- Create React App has [a problem](https://github.com/ai/nanoid/issues/205)
250
- with ES modules packages.
249
+ Create React App < 4.0.0 had
250
+ [a problem](https://github.com/ai/nanoid/issues/205) with ES modules packages.
251
251
 
252
252
  ```
253
253
  TypeError: (0 , _nanoid.nanoid) is not a function
254
254
  ```
255
255
 
256
- [Pull request](https://github.com/facebook/create-react-app/pull/8768) was sent,
257
- but it was still not released.
258
-
259
- Use Nano ID 2 `npm i nanoid@^2.0.0` until Create React App 4.0 release.
256
+ Use Nano ID 2 `npm i nanoid@^2.0.0` if you're using a version below
257
+ CRA 4.0.
260
258
 
261
259
 
262
260
  ### React Native
@@ -388,7 +386,7 @@ the same ID generator on the client and server side.
388
386
  * [C++](https://github.com/mcmikecreations/nanoid_cpp)
389
387
  * [Clojure and ClojureScript](https://github.com/zelark/nano-id)
390
388
  * [Crystal](https://github.com/mamantoha/nanoid.cr)
391
- * [Dart](https://github.com/pd4d10/nanoid-dart)
389
+ * [Dart & Flutter](https://github.com/pd4d10/nanoid-dart)
392
390
  * [Deno](https://github.com/ianfabs/nanoid)
393
391
  * [Go](https://github.com/matoous/go-nanoid)
394
392
  * [Elixir](https://github.com/railsmechanic/nanoid)
@@ -403,6 +401,7 @@ the same ID generator on the client and server side.
403
401
  * [Ruby](https://github.com/radeno/nanoid.rb)
404
402
  * [Rust](https://github.com/nikolay-govorov/nanoid)
405
403
  * [Swift](https://github.com/antiflasher/NanoID)
404
+ * [V](https://github.com/invipal/nanoid)
406
405
 
407
406
  Also, [CLI] is available to generate IDs from a command line.
408
407
 
@@ -0,0 +1,70 @@
1
+ let random = bytes =>
2
+ Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
3
+
4
+ let customAlphabet = (alphabet, size) => {
5
+ // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
6
+ // values closer to the alphabet size. The bitmask calculates the closest
7
+ // `2^31 - 1` number, which exceeds the alphabet size.
8
+ // For example, the bitmask for the alphabet size 30 is 31 (00011111).
9
+ // `Math.clz32` is not used, because it is not available in browsers.
10
+ let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
11
+ // Though, the bitmask solution is not perfect since the bytes exceeding
12
+ // the alphabet size are refused. Therefore, to reliably generate the ID,
13
+ // the random bytes redundancy has to be satisfied.
14
+
15
+ // Note: every hardware random generator call is performance expensive,
16
+ // because the system call for entropy collection takes a lot of time.
17
+ // So, to avoid additional system calls, extra bytes are requested in advance.
18
+
19
+ // Next, a step determines how many random bytes to generate.
20
+ // The number of random bytes gets decided upon the ID size, mask,
21
+ // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
22
+ // according to benchmarks).
23
+
24
+ // `-~f => Math.ceil(f)` if f is a float
25
+ // `-~i => i + 1` if i is an integer
26
+ let step = -~((1.6 * mask * size) / alphabet.length)
27
+
28
+ return () => {
29
+ let id = ''
30
+ while (true) {
31
+ let bytes = crypto.getRandomValues(new Uint8Array(step))
32
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
33
+ let i = step
34
+ while (i--) {
35
+ // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
36
+ id += alphabet[bytes[i] & mask] || ''
37
+ if (id.length === size) return Promise.resolve(id)
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ let nanoid = (size = 21) => {
44
+ let id = ''
45
+ let bytes = crypto.getRandomValues(new Uint8Array(size))
46
+
47
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
48
+ while (size--) {
49
+ // It is incorrect to use bytes exceeding the alphabet size.
50
+ // The following mask reduces the random byte in the 0-255 value
51
+ // range to the 0-63 value range. Therefore, adding hacks, such
52
+ // as empty string fallback or magic numbers, is unneccessary because
53
+ // the bitmask trims bytes down to the alphabet size.
54
+ let byte = bytes[size] & 63
55
+ if (byte < 36) {
56
+ // `0-9a-z`
57
+ id += byte.toString(36)
58
+ } else if (byte < 62) {
59
+ // `A-Z`
60
+ id += (byte - 26).toString(36).toUpperCase()
61
+ } else if (byte < 63) {
62
+ id += '_'
63
+ } else {
64
+ id += '-'
65
+ }
66
+ }
67
+ return Promise.resolve(id)
68
+ }
69
+
70
+ module.exports = { nanoid, customAlphabet, random }
@@ -6,6 +6,7 @@
6
6
  "./index.js": "./index.native.js"
7
7
  },
8
8
  "browser": {
9
- "./index.js": "./index.browser.js"
9
+ "./index.js": "./index.browser.js",
10
+ "./index.cjs": "./index.browser.cjs"
10
11
  }
11
12
  }
@@ -0,0 +1,104 @@
1
+ // This file replaces `index.js` in bundlers like webpack or Rollup,
2
+ // according to `browser` config in `package.json`.
3
+
4
+ let { urlAlphabet } = require('./url-alphabet/index.cjs')
5
+
6
+ if (process.env.NODE_ENV !== 'production') {
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.'
18
+ )
19
+ }
20
+ if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
21
+ throw new Error(
22
+ 'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
23
+ ' before importing Nano ID to fix IE 11 support'
24
+ )
25
+ }
26
+ if (typeof crypto === 'undefined') {
27
+ throw new Error(
28
+ 'Your browser does not have secure random generator. ' +
29
+ 'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
30
+ )
31
+ }
32
+ }
33
+
34
+ let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
35
+
36
+ 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
+ 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
+ let step = -~((1.6 * mask * size) / alphabet.length)
59
+
60
+ return () => {
61
+ let id = ''
62
+ while (true) {
63
+ let bytes = getRandom(step)
64
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
65
+ let j = step
66
+ while (j--) {
67
+ // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
68
+ id += alphabet[bytes[j] & mask] || ''
69
+ if (id.length === size) return id
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
76
+
77
+ let nanoid = (size = 21) => {
78
+ let id = ''
79
+ let bytes = crypto.getRandomValues(new Uint8Array(size))
80
+
81
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
82
+ 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
+ let byte = bytes[size] & 63
89
+ if (byte < 36) {
90
+ // `0-9a-z`
91
+ id += byte.toString(36)
92
+ } else if (byte < 62) {
93
+ // `A-Z`
94
+ id += (byte - 26).toString(36).toUpperCase()
95
+ } else if (byte < 63) {
96
+ id += '_'
97
+ } else {
98
+ id += '-'
99
+ }
100
+ }
101
+ return id
102
+ }
103
+
104
+ module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.21",
3
+ "version": "3.1.25",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -15,7 +15,10 @@
15
15
  "license": "MIT",
16
16
  "repository": "ai/nanoid",
17
17
  "browser": {
18
- "./index.js": "./index.browser.js"
18
+ "./index.js": "./index.browser.js",
19
+ "./async/index.js": "./async/index.browser.js",
20
+ "./async/index.cjs": "./async/index.browser.cjs",
21
+ "./index.cjs": "./index.browser.cjs"
19
22
  },
20
23
  "react-native": "index.js",
21
24
  "bin": "./bin/nanoid.cjs",
@@ -28,10 +31,12 @@
28
31
  ".": {
29
32
  "browser": {
30
33
  "development": "./index.dev.js",
31
- "production": "./index.prod.js"
34
+ "production": "./index.prod.js",
35
+ "default": "./index.prod.js"
32
36
  },
33
37
  "require": "./index.cjs",
34
38
  "import": "./index.js",
39
+ "default": "./index.js",
35
40
  "types": "./index.d.ts"
36
41
  },
37
42
  "./package.json": "./package.json",
@@ -39,17 +44,20 @@
39
44
  "./async": {
40
45
  "browser": "./async/index.browser.js",
41
46
  "require": "./async/index.cjs",
42
- "import": "./async/index.js"
43
- },
44
- "./non-secure/package.json": "./non-secure/package.json",
45
- "./non-secure": {
46
- "require": "./non-secure/index.cjs",
47
- "import": "./non-secure/index.js"
47
+ "import": "./async/index.js",
48
+ "default": "./async/index.js"
48
49
  },
49
50
  "./url-alphabet/package.json": "./url-alphabet/package.json",
50
51
  "./url-alphabet": {
51
52
  "require": "./url-alphabet/index.cjs",
52
- "import": "./url-alphabet/index.js"
53
+ "import": "./url-alphabet/index.js",
54
+ "default": "./url-alphabet/index.js"
55
+ },
56
+ "./non-secure/package.json": "./non-secure/package.json",
57
+ "./non-secure": {
58
+ "require": "./non-secure/index.cjs",
59
+ "import": "./non-secure/index.js",
60
+ "default": "./non-secure/index.js"
53
61
  },
54
62
  "./index.d.ts": "./index.d.ts"
55
63
  }