nanoid 3.1.8 → 3.1.12

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,6 +1,18 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 3.1.12
5
+ * Improve IE 11 docs.
6
+
7
+ ## 3.1.11
8
+ * Fixed asynchronous `customAlphabet` in browser (by @LoneRifle).
9
+
10
+ ## 3.1.10
11
+ * Fix ES modules support.
12
+
13
+ ## 3.1.9
14
+ * Try to fix React Native Expo support.
15
+
4
16
  ## 3.1.8
5
17
  * Add React Native Expo support.
6
18
 
package/README.md CHANGED
@@ -138,7 +138,7 @@ Test configuration: Dell XPS 2-in-a 7390, Fedora 32, Node.js 13.11.
138
138
  Tidelift will coordinate the fix and disclosure.
139
139
 
140
140
  [Secure random values (in Node.js)]: https://gist.github.com/joepie91/7105003c3b26e65efcea63f3db82dfba
141
- [better algorithm]: https://github.com/ai/nanoid/blob/master/format.js
141
+ [better algorithm]: https://github.com/ai/nanoid/blob/master/index.js
142
142
 
143
143
 
144
144
  ## Usage
@@ -175,10 +175,14 @@ If you support IE, you need to [transpile `node_modules`] by Babel
175
175
  and add `crypto` alias:
176
176
 
177
177
  ```js
178
+ // polyfills.js
178
179
  if (!window.crypto) {
179
180
  window.crypto = window.msCrypto
180
181
  }
182
+ ```
181
183
 
184
+ ```js
185
+ import './polyfills.js'
182
186
  import { nanoid } from 'nanoid'
183
187
  ```
184
188
 
@@ -253,9 +257,10 @@ If you use Expo in React Native, you need a different workaround.
253
257
 
254
258
  1. Install [`expo-random`](https://www.npmjs.com/package/expo-random).
255
259
  2. Use `nanoid/async` instead of `nanoid`.
260
+ 3. Import `index.native.js` file directly.
256
261
 
257
262
  ```js
258
- import { nanoid } from 'nanoid/async'
263
+ import { nanoid } from 'nanoid/async/index.native'
259
264
 
260
265
  async function createUser () {
261
266
  user.id = await nanoid()
@@ -352,12 +357,15 @@ Nano ID was ported to many languages. You can use these ports to have
352
357
  the same ID generator on the client and server side.
353
358
 
354
359
  * [C#](https://github.com/codeyu/nanoid-net)
360
+ * [C++](https://github.com/mcmikecreations/nanoid_cpp)
355
361
  * [Clojure and ClojureScript](https://github.com/zelark/nano-id)
356
362
  * [Crystal](https://github.com/mamantoha/nanoid.cr)
357
363
  * [Dart](https://github.com/pd4d10/nanoid-dart)
364
+ * [Deno](https://github.com/ianfabs/nanoid)
358
365
  * [Go](https://github.com/matoous/go-nanoid)
359
366
  * [Elixir](https://github.com/railsmechanic/nanoid)
360
367
  * [Haskell](https://github.com/4e6/nanoid-hs)
368
+ * [Janet](https://sr.ht/~statianzo/janet-nanoid/)
361
369
  * [Java](https://github.com/aventrix/jnanoid)
362
370
  * [Nim](https://github.com/icyphox/nanoid.nim)
363
371
  * [PHP](https://github.com/hidehalo/nanoid-php)
@@ -1,4 +1,5 @@
1
- import { random } from './random/index.js'
1
+ let random = bytes =>
2
+ Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
2
3
 
3
4
  let customAlphabet = (alphabet, size) => {
4
5
  // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
@@ -34,7 +35,7 @@ let customAlphabet = (alphabet, size) => {
34
35
  // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
35
36
  id += alphabet[bytes[i] & mask] || ''
36
37
  // `id.length + 1 === size` is a more compact option.
37
- if (id.length === +size) return id
38
+ if (id.length === +size) return Promise.resolve(id)
38
39
  }
39
40
  }
40
41
  }
package/async/index.cjs CHANGED
@@ -1,5 +1,22 @@
1
+ let crypto = require('crypto')
2
+
1
3
  let { urlAlphabet } = require('../url-alphabet/index.cjs')
2
- let { random } = require('./random/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
+ let random = bytes =>
8
+ 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
+ crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
13
+ if (err) {
14
+ reject(err)
15
+ } else {
16
+ resolve(buf)
17
+ }
18
+ })
19
+ })
3
20
 
4
21
  let customAlphabet = (alphabet, size) => {
5
22
  // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
package/async/index.js CHANGED
@@ -1,5 +1,22 @@
1
+ import crypto from 'crypto'
2
+
1
3
  import { urlAlphabet } from '../url-alphabet/index.js'
2
- import { random } from './random/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
+ let random = bytes =>
8
+ 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
+ crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
13
+ if (err) {
14
+ reject(err)
15
+ } else {
16
+ resolve(buf)
17
+ }
18
+ })
19
+ })
3
20
 
4
21
  let customAlphabet = (alphabet, size) => {
5
22
  // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
@@ -0,0 +1,58 @@
1
+ import { getRandomBytesAsync } from 'expo-random'
2
+
3
+ import { urlAlphabet } from '../url-alphabet/index.js'
4
+
5
+ let random = getRandomBytesAsync
6
+
7
+ 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
+ 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
+ let step = Math.ceil((1.6 * mask * size) / alphabet.length)
26
+
27
+ let tick = id =>
28
+ random(step).then(bytes => {
29
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
30
+ let i = step
31
+ while (i--) {
32
+ // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
33
+ id += alphabet[bytes[i] & mask] || ''
34
+ // `id.length + 1 === size` is a more compact option.
35
+ if (id.length === +size) return id
36
+ }
37
+ return tick(id)
38
+ })
39
+
40
+ return () => tick('')
41
+ }
42
+
43
+ let nanoid = (size = 21) =>
44
+ random(size).then(bytes => {
45
+ let id = ''
46
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
47
+ while (size--) {
48
+ // It is incorrect to use bytes exceeding the alphabet size.
49
+ // The following mask reduces the random byte in the 0-255 value
50
+ // range to the 0-63 value range. Therefore, adding hacks, such
51
+ // as empty string fallback or magic numbers, is unneccessary because
52
+ // the bitmask trims bytes down to the alphabet size.
53
+ id += urlAlphabet[bytes[size] & 63]
54
+ }
55
+ return id
56
+ })
57
+
58
+ export { nanoid, customAlphabet, random }
@@ -2,7 +2,9 @@
2
2
  "type": "module",
3
3
  "main": "index.cjs",
4
4
  "module": "index.js",
5
- "react-native": "index.js",
5
+ "react-native": {
6
+ "./index.js": "./index.native.js"
7
+ },
6
8
  "browser": {
7
9
  "./index.js": "./index.browser.js"
8
10
  }
package/index.browser.js CHANGED
@@ -20,8 +20,8 @@ if (process.env.NODE_ENV !== 'production') {
20
20
  }
21
21
  if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
22
22
  throw new Error(
23
- 'Add `if (!window.crypto) window.crypto = window.msCrypto` ' +
24
- 'before Nano ID to fix IE 11 support'
23
+ 'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
24
+ ' before importing Nano ID to fix IE 11 support'
25
25
  )
26
26
  }
27
27
  if (typeof crypto === 'undefined') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.8",
3
+ "version": "3.1.12",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -24,17 +24,17 @@
24
24
  "main": "index.cjs",
25
25
  "module": "index.js",
26
26
  "exports": {
27
- "./package.json": "./package.json",
28
27
  ".": {
28
+ "browser": "./index.browser.js",
29
29
  "require": "./index.cjs",
30
- "import": "./index.js",
31
- "browser": "./index.browser.js"
30
+ "import": "./index.js"
32
31
  },
32
+ "./package.json": "./package.json",
33
33
  "./async/package.json": "./async/package.json",
34
34
  "./async": {
35
+ "browser": "./async/index.browser.js",
35
36
  "require": "./async/index.cjs",
36
- "import": "./async/index.js",
37
- "browser": "./async/index.browser.js"
37
+ "import": "./async/index.js"
38
38
  },
39
39
  "./non-secure/package.json": "./non-secure/package.json",
40
40
  "./non-secure": {
@@ -45,12 +45,6 @@
45
45
  "./url-alphabet": {
46
46
  "require": "./url-alphabet/index.cjs",
47
47
  "import": "./url-alphabet/index.js"
48
- },
49
- "./async/random/package.json": "./async/random/package.json",
50
- "./async/random": {
51
- "require": "./async/random/index.cjs",
52
- "import": "./async/random/index.js",
53
- "browser": "./async/random/index.browser.js"
54
48
  }
55
49
  }
56
50
  }
@@ -1,4 +0,0 @@
1
- let random = bytes =>
2
- Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
3
-
4
- export { random }
@@ -1,19 +0,0 @@
1
- let crypto = require('crypto')
2
-
3
- // `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
4
- // because it is possible to use in combination with `Buffer.allocUnsafe()`.
5
- let random = bytes =>
6
- new Promise((resolve, reject) => {
7
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
8
- // Memory flushing is unnecessary since the buffer allocation itself resets
9
- // the memory with the new bytes.
10
- crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
11
- if (err) {
12
- reject(err)
13
- } else {
14
- resolve(buf)
15
- }
16
- })
17
- })
18
-
19
- module.exports = { random }
@@ -1,12 +0,0 @@
1
- /**
2
- * Generate an array of random bytes collected from hardware noise.
3
- *
4
- * ```js
5
- * import { customRandom, random } from 'nanoid'
6
- * const nanoid = customRandom("abcdef", 5, random)
7
- * ```
8
- *
9
- * @param bytes Size of the array.
10
- * @returns An array of random bytes.
11
- */
12
- export function random (bytes: number): Promise<Uint8Array>
@@ -1,19 +0,0 @@
1
- import crypto from 'crypto'
2
-
3
- // `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
4
- // because it is possible to use in combination with `Buffer.allocUnsafe()`.
5
- let random = bytes =>
6
- new Promise((resolve, reject) => {
7
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
8
- // Memory flushing is unnecessary since the buffer allocation itself resets
9
- // the memory with the new bytes.
10
- crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
11
- if (err) {
12
- reject(err)
13
- } else {
14
- resolve(buf)
15
- }
16
- })
17
- })
18
-
19
- export { random }
@@ -1,5 +0,0 @@
1
- import { getRandomBytesAsync } from 'expo-random'
2
-
3
- let random = getRandomBytesAsync
4
-
5
- export { random }
@@ -1,11 +0,0 @@
1
- {
2
- "type": "module",
3
- "main": "index.cjs",
4
- "module": "index.js",
5
- "react-native": {
6
- "./index.js": "./index.native.js"
7
- },
8
- "browser": {
9
- "./index.js": "./index.browser.js"
10
- }
11
- }