nanoid 3.1.12 → 3.1.16

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.16
5
+ * Speed up Nano ID 4 times (by Peter Boyer).
6
+
7
+ ## 3.1.15
8
+ * Fixed `package.types` path.
9
+
10
+ ## 3.1.14
11
+ * Added `package.types`.
12
+
13
+ ## 3.1.13
14
+ * Removed Node.js 15.0.0 with `randomFillSync` regression from `engines.node`.
15
+
4
16
  ## 3.1.12
5
- * Improve IE 11 docs.
17
+ * Improved IE 11 docs.
6
18
 
7
19
  ## 3.1.11
8
20
  * Fixed asynchronous `customAlphabet` in browser (by @LoneRifle).
package/README.md CHANGED
@@ -46,6 +46,7 @@ Supports modern browsers, IE [with Babel], Node.js and React Native.
46
46
  * [React](#react)
47
47
  * [Create React App](#create-react-app)
48
48
  * [React Native](#react-native)
49
+ * [Rollup](#rollup)
49
50
  * [Expo](#expo)
50
51
  * [PouchDB and CouchDB](#pouchdb-and-couchdb)
51
52
  * [Mongoose](#mongoose)
@@ -81,27 +82,27 @@ There are three main differences between Nano ID and UUID v4:
81
82
  ## Benchmark
82
83
 
83
84
  ```rust
84
- $ ./test/benchmark
85
- nanoid 655,798 ops/sec
86
- customAlphabet 635,421 ops/sec
87
- uid.sync 375,816 ops/sec
88
- uuid v4 396,756 ops/sec
89
- secure-random-string 366,434 ops/sec
90
- cuid 183,998 ops/sec
91
- shortid 59,343 ops/sec
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
92
93
 
93
94
  Async:
94
- async nanoid 101,966 ops/sec
95
- async customAlphabet 102,471 ops/sec
96
- async secure-random-string 97,206 ops/sec
97
- uid 91,291 ops/sec
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
98
99
 
99
100
  Non-secure:
100
- non-secure nanoid 2,754,423 ops/sec
101
- rndm 2,437,262 ops/sec
101
+ non-secure nanoid 2,641,654 ops/sec
102
+ rndm 2,447,086 ops/sec
102
103
  ```
103
104
 
104
- Test configuration: Dell XPS 2-in-a 7390, Fedora 32, Node.js 13.11.
105
+ Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1.
105
106
 
106
107
 
107
108
  ## Tools
@@ -150,7 +151,7 @@ with 21 characters (to have a collision probability similar to UUID v4).
150
151
 
151
152
  ```js
152
153
  import { nanoid } from 'nanoid'
153
- model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
154
+ model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
154
155
  ```
155
156
 
156
157
  If you want to reduce the ID size (and increase collisions probability),
@@ -226,12 +227,10 @@ with ES modules packages.
226
227
  TypeError: (0 , _nanoid.nanoid) is not a function
227
228
  ```
228
229
 
229
- If you have an error above, here is temporary fix:
230
+ [Pull request](https://github.com/facebook/create-react-app/pull/8768) was sent,
231
+ but it was still not released.
230
232
 
231
- 1. Use Nano ID 2 instead of 3: `npm i nanoid@^2.0.0`.
232
- 2. Vote for
233
- [pull request](https://github.com/facebook/create-react-app/pull/8768),
234
- that fix dual packages support.
233
+ Use Nano ID 2 `npm i nanoid@^2.0.0` until Create React App 4.0 release.
235
234
 
236
235
 
237
236
  ### React Native
@@ -251,6 +250,22 @@ For Expo framework see the next section.
251
250
  [`react-native-get-random-values`]: https://github.com/LinusU/react-native-get-random-values
252
251
 
253
252
 
253
+ ### Rollup
254
+
255
+ For Rollup you will need [`@rollup/plugin-replace`] to replace
256
+ `process.env.NODE_ENV`:
257
+
258
+ ```js
259
+ plugins: [
260
+ replace({
261
+ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE)
262
+ })
263
+ ]
264
+ ```
265
+
266
+ [`@rollup/plugin-replace`]: https://github.com/rollup/plugins/tree/master/packages/replace
267
+
268
+
254
269
  ### Expo
255
270
 
256
271
  If you use Expo in React Native, you need a different workaround.
@@ -400,7 +415,7 @@ async function createUser () {
400
415
  ```
401
416
 
402
417
  Unfortunately, you will lose Web Crypto API advantages in a browser
403
- if you the asynchronous API. So, currently, in the browser, you are limited
418
+ if you use the asynchronous API. So, currently, in the browser, you are limited
404
419
  with either security or asynchronous behavior.
405
420
 
406
421
 
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
- // We reuse buffers with the same size to avoid memory fragmentations
6
- // for better performance.
7
- let buffers = {}
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
- let buffer = buffers[bytes]
10
- if (!buffer) {
11
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
12
- // Memory flushing is unnecessary since the buffer allocation itself resets
13
- // the memory with the new bytes.
14
- buffer = Buffer.allocUnsafe(bytes)
15
- if (bytes <= 255) buffers[bytes] = buffer
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
- return crypto.randomFillSync(buffer)
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.js CHANGED
@@ -2,19 +2,27 @@ import crypto from 'crypto'
2
2
 
3
3
  import { urlAlphabet } from './url-alphabet/index.js'
4
4
 
5
- // We reuse buffers with the same size to avoid memory fragmentations
6
- // for better performance.
7
- let buffers = {}
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
- let buffer = buffers[bytes]
10
- if (!buffer) {
11
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
12
- // Memory flushing is unnecessary since the buffer allocation itself resets
13
- // the memory with the new bytes.
14
- buffer = Buffer.allocUnsafe(bytes)
15
- if (bytes <= 255) buffers[bytes] = buffer
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
- return crypto.randomFillSync(buffer)
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.12",
3
+ "version": "3.1.16",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -9,7 +9,7 @@
9
9
  "url"
10
10
  ],
11
11
  "engines": {
12
- "node": "^10 || ^12 || >=13.7"
12
+ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
13
13
  },
14
14
  "author": "Andrey Sitnik <andrey@sitnik.ru>",
15
15
  "license": "MIT",
@@ -20,6 +20,7 @@
20
20
  "react-native": "index.js",
21
21
  "bin": "./bin/nanoid.cjs",
22
22
  "sideEffects": false,
23
+ "types": "./index.d.ts",
23
24
  "type": "module",
24
25
  "main": "index.cjs",
25
26
  "module": "index.js",
@@ -27,7 +28,8 @@
27
28
  ".": {
28
29
  "browser": "./index.browser.js",
29
30
  "require": "./index.cjs",
30
- "import": "./index.js"
31
+ "import": "./index.js",
32
+ "types": "./index.d.ts"
31
33
  },
32
34
  "./package.json": "./package.json",
33
35
  "./async/package.json": "./async/package.json",
@@ -45,6 +47,7 @@
45
47
  "./url-alphabet": {
46
48
  "require": "./url-alphabet/index.cjs",
47
49
  "import": "./url-alphabet/index.js"
48
- }
50
+ },
51
+ "./index.d.ts": "./index.d.ts"
49
52
  }
50
53
  }