nanoid 3.1.25 → 3.1.26

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
@@ -3,6 +3,8 @@
3
3
  <img src="https://ai.github.io/nanoid/logo.svg" align="right"
4
4
  alt="Nano ID logo by Anton Lovchikov" width="180" height="94">
5
5
 
6
+ **English** | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md)
7
+
6
8
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
7
9
 
8
10
  > “An amazing level of senseless perfectionism,
@@ -10,13 +12,12 @@ A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
10
12
 
11
13
  * **Small.** 108 bytes (minified and gzipped). No dependencies.
12
14
  [Size Limit] controls the size.
13
- * **Fast.** It is 60% faster than UUID.
14
- * **Safe.** It uses cryptographically strong random APIs.
15
- Can be used in clusters.
16
- * **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
15
+ * **Fast.** It is 2 times faster than UUID.
16
+ * **Safe.** It uses hardware random generator. Can be used in clusters.
17
+ * **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
17
18
  So ID size was reduced from 36 to 21 symbols.
18
19
  * **Portable.** Nano ID was ported
19
- to [14 programming languages](#other-programming-languages).
20
+ to [19 programming languages](#other-programming-languages).
20
21
 
21
22
  ```js
22
23
  import { nanoid } from 'nanoid'
@@ -34,483 +35,5 @@ Supports modern browsers, IE [with Babel], Node.js and React Native.
34
35
  alt="Sponsored by Evil Martians" width="236" height="54">
35
36
  </a>
36
37
 
37
- ## Table of Contents
38
-
39
- * [Comparison with UUID](#comparison-with-uuid)
40
- * [Benchmark](#benchmark)
41
- * [Tools](#tools)
42
- * [Security](#security)
43
- * [Usage](#usage)
44
- * [JS](#js)
45
- * [IE](#ie)
46
- * [React](#react)
47
- * [Create React App](#create-react-app)
48
- * [React Native](#react-native)
49
- * [Rollup](#rollup)
50
- * [PouchDB and CouchDB](#pouchdb-and-couchdb)
51
- * [Mongoose](#mongoose)
52
- * [ES Modules](#es-modules)
53
- * [Web Workers](#web-workers)
54
- * [CLI](#cli)
55
- * [Other Programming Languages](#other-programming-languages)
56
- * [API](#api)
57
- * [Async](#async)
58
- * [Non-Secure](#non-secure)
59
- * [Custom Alphabet or Size](#custom-alphabet-or-size)
60
- * [Custom Random Bytes Generator](#custom-random-bytes-generator)
61
-
62
-
63
- ## Comparison with UUID
64
-
65
- Nano ID is quite comparable to UUID v4 (random-based).
66
- It has a similar number of random bits in the ID
67
- (126 in Nano ID and 122 in UUID), so it has a similar collision probability:
68
-
69
- > For there to be a one in a billion chance of duplication,
70
- > 103 trillion version 4 IDs must be generated.
71
-
72
- There are three main differences between Nano ID and UUID v4:
73
-
74
- 1. Nano ID uses a bigger alphabet, so a similar number of random bits
75
- are packed in just 21 symbols instead of 36.
76
- 2. Nano ID code is **4.5 times less** than `uuid/v4` package:
77
- 108 bytes instead of 483.
78
- 3. Because of memory allocation tricks, Nano ID is **60%** faster than UUID.
79
-
80
-
81
- ## Benchmark
82
-
83
- ```rust
84
- $ node ./test/benchmark.js
85
- nanoid 2,280,683 ops/sec
86
- customAlphabet 1,851,117 ops/sec
87
- uuid v4 1,348,425 ops/sec
88
- uid.sync 313,306 ops/sec
89
- secure-random-string 294,161 ops/sec
90
- cuid 158,988 ops/sec
91
- shortid 37,222 ops/sec
92
-
93
- Async:
94
- async nanoid 95,500 ops/sec
95
- async customAlphabet 93,800 ops/sec
96
- async secure-random-string 90,316 ops/sec
97
- uid 85,583 ops/sec
98
-
99
- Non-secure:
100
- non-secure nanoid 2,641,654 ops/sec
101
- rndm 2,447,086 ops/sec
102
- ```
103
-
104
- Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1.
105
-
106
-
107
- ## Tools
108
-
109
- * [ID size calculator] shows collision probability when adjusting
110
- the ID alphabet or size.
111
- * [`nanoid-dictionary`] with popular alphabets to use with `customAlphabet`.
112
- * [`nanoid-good`] to be sure that your ID doesn’t contain any obscene words.
113
-
114
- [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
115
- [ID size calculator]: https://zelark.github.io/nano-id-cc/
116
- [`nanoid-good`]: https://github.com/y-gagar1n/nanoid-good
117
-
118
-
119
- ## Security
120
-
121
- *See a good article about random generators theory:
122
- [Secure random values (in Node.js)]*
123
-
124
- * **Unpredictability.** Instead of using the unsafe `Math.random()`, Nano ID
125
- uses the `crypto` module in Node.js and the Web Crypto API in browsers.
126
- These modules use unpredictable hardware random generator.
127
- * **Uniformity.** `random % alphabet` is a popular mistake to make when coding
128
- an ID generator. The distribution will not be even; there will be a lower
129
- chance for some symbols to appear compared to others. So, it will reduce
130
- the number of tries when brute-forcing. Nano ID uses a [better algorithm]
131
- and is tested for uniformity.
132
-
133
- <img src="img/distribution.png" alt="Nano ID uniformity"
134
- width="340" height="135">
135
-
136
- * **Vulnerabilities:** to report a security vulnerability, please use
137
- the [Tidelift security contact](https://tidelift.com/security).
138
- Tidelift will coordinate the fix and disclosure.
139
-
140
- [Secure random values (in Node.js)]: https://gist.github.com/joepie91/7105003c3b26e65efcea63f3db82dfba
141
- [better algorithm]: https://github.com/ai/nanoid/blob/main/index.js
142
-
143
-
144
- ## Usage
145
-
146
- ### JS
147
-
148
- The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID
149
- with 21 characters (to have a collision probability similar to UUID v4).
150
-
151
- ```js
152
- import { nanoid } from 'nanoid'
153
- model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
154
- ```
155
-
156
- In Node.js you can use CommonJS import:
157
-
158
- ```js
159
- const { nanoid } = require('nanoid')
160
- ```
161
-
162
- If you want to reduce the ID size (and increase collisions probability),
163
- you can pass the size as an argument.
164
-
165
- ```js
166
- nanoid(10) //=> "IRFa-VaY2b"
167
- ```
168
-
169
- Don’t forget to check the safety of your ID size
170
- in our [ID collision probability] calculator.
171
-
172
- You can also use a [custom alphabet](#custom-alphabet-or-size)
173
- or a [random generator](#custom-random-bytes-generator).
174
-
175
- [ID collision probability]: https://zelark.github.io/nano-id-cc/
176
-
177
-
178
- ### IE
179
-
180
- If you support IE, you need to [transpile `node_modules`] by Babel
181
- and add `crypto` alias:
182
-
183
- ```js
184
- // polyfills.js
185
- if (!window.crypto) {
186
- window.crypto = window.msCrypto
187
- }
188
- ```
189
-
190
- ```js
191
- import './polyfills.js'
192
- import { nanoid } from 'nanoid'
193
- ```
194
-
195
- [transpile `node_modules`]: https://developer.epages.com/blog/coding/how-to-transpile-node-modules-with-babel-and-webpack-in-a-monorepo/
196
-
197
-
198
- ### React
199
-
200
- There’s currently no correct way to use nanoid for React `key` prop
201
- since it should be consistent among renders.
202
-
203
- ```jsx
204
- function Todos({todos}) {
205
- return (
206
- <ul>
207
- {todos.map(todo => (
208
- <li key={nanoid()}> /* DON’T DO IT */
209
- {todo.text}
210
- </li>
211
- ))}
212
- </ul>
213
- )
214
- }
215
- ```
216
-
217
- You should rather try to reach for stable id inside your list item.
218
-
219
- ```jsx
220
- const todoItems = todos.map((todo) =>
221
- <li key={todo.id}>
222
- {todo.text}
223
- </li>
224
- )
225
- ```
226
-
227
- In case you don’t have stable ids you'd rather use index as `key`
228
- instead of `nanoid()`:
229
-
230
- ```jsx
231
- const todoItems = todos.map((text, index) =>
232
- <li key={index}> /* Still not recommended but preferred over nanoid().
233
- Only do this if items have no stable IDs. */
234
- {text}
235
- </li>
236
- )
237
- ```
238
-
239
- If you want to use Nano ID in the `id` prop, you must set some string prefix
240
- (it is invalid for the HTML ID to start with a number).
241
-
242
- ```jsx
243
- <input id={'id' + this.id} type="text"/>
244
- ```
245
-
246
-
247
- ### Create React App
248
-
249
- Create React App < 4.0.0 had
250
- [a problem](https://github.com/ai/nanoid/issues/205) with ES modules packages.
251
-
252
- ```
253
- TypeError: (0 , _nanoid.nanoid) is not a function
254
- ```
255
-
256
- Use Nano ID 2 `npm i nanoid@^2.0.0` if you're using a version below
257
- CRA 4.0.
258
-
259
-
260
- ### React Native
261
-
262
- React Native does not have built-in random generator. The following polyfill
263
- works for plain React Native and Expo starting with `39.x`.
264
-
265
- 1. Check [`react-native-get-random-values`] docs and install it.
266
- 2. Import it before Nano ID.
267
-
268
- ```js
269
- import 'react-native-get-random-values'
270
- import { nanoid } from 'nanoid'
271
- ```
272
-
273
- For Expo framework see the next section.
274
-
275
- [`react-native-get-random-values`]: https://github.com/LinusU/react-native-get-random-values
276
-
277
-
278
- ### Rollup
279
-
280
- For Rollup you will need [`@rollup/plugin-node-resolve`] to bundle browser version
281
- of this library and [`@rollup/plugin-replace`] to replace
282
- `process.env.NODE_ENV`:
283
-
284
- ```js
285
- plugins: [
286
- nodeResolve({
287
- browser: true
288
- }),
289
- replace({
290
- 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
291
- })
292
- ]
293
- ```
294
-
295
- [`@rollup/plugin-node-resolve`]: https://github.com/rollup/plugins/tree/master/packages/node-resolve
296
- [`@rollup/plugin-replace`]: https://github.com/rollup/plugins/tree/master/packages/replace
297
-
298
-
299
- ### PouchDB and CouchDB
300
-
301
- In PouchDB and CouchDB, IDs can’t start with an underscore `_`.
302
- A prefix is required to prevent this issue, as Nano ID might use a `_`
303
- at the start of the ID by default.
304
-
305
- Override the default ID with the following option:
306
-
307
- ```js
308
- db.put({
309
- _id: 'id' + nanoid(),
310
-
311
- })
312
- ```
313
-
314
-
315
- ### Mongoose
316
-
317
- ```js
318
- const mySchema = new Schema({
319
- _id: {
320
- type: String,
321
- default: () => nanoid()
322
- }
323
- })
324
- ```
325
-
326
-
327
- ### ES Modules
328
-
329
- Nano ID provides ES modules. You do not need to do anything to use Nano ID
330
- as ESM in webpack, Rollup, Parcel, or Node.js.
331
-
332
- ```js
333
- import { nanoid } from 'nanoid'
334
- ```
335
-
336
- For quick hacks, you can load Nano ID from CDN. Special minified
337
- `nanoid.js` module is available on jsDelivr.
338
-
339
- Though, it is not recommended to be used in production
340
- because of the lower loading performance.
341
-
342
- ```js
343
- import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
344
- ```
345
-
346
-
347
- ### Web Workers
348
-
349
- Web Workers do not have access to a secure random generator.
350
-
351
- Security is important in IDs when IDs should be unpredictable.
352
- For instance, in "access by URL" link generation.
353
- If you do not need unpredictable IDs, but you need to use Web Workers,
354
- you can use the non‑secure ID generator.
355
-
356
- ```js
357
- import { nanoid } from 'nanoid/non-secure'
358
- nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
359
- ```
360
-
361
- Note: non-secure IDs are more prone to collision attacks.
362
-
363
-
364
- ### CLI
365
-
366
- You can get unique ID in terminal by calling `npx nanoid`. You need only
367
- Node.js in the system. You do not need Nano ID to be installed anywhere.
368
-
369
- ```sh
370
- $ npx nanoid
371
- npx: installed 1 in 0.63s
372
- LZfXLFzPPR4NNrgjlWDxn
373
- ```
374
-
375
- If you want to change alphabet or ID size, you should use [`nanoid-cli`].
376
-
377
- [`nanoid-cli`]: https://github.com/twhitbeck/nanoid-cli
378
-
379
-
380
- ### Other Programming Languages
381
-
382
- Nano ID was ported to many languages. You can use these ports to have
383
- the same ID generator on the client and server side.
384
-
385
- * [C#](https://github.com/codeyu/nanoid-net)
386
- * [C++](https://github.com/mcmikecreations/nanoid_cpp)
387
- * [Clojure and ClojureScript](https://github.com/zelark/nano-id)
388
- * [Crystal](https://github.com/mamantoha/nanoid.cr)
389
- * [Dart & Flutter](https://github.com/pd4d10/nanoid-dart)
390
- * [Deno](https://github.com/ianfabs/nanoid)
391
- * [Go](https://github.com/matoous/go-nanoid)
392
- * [Elixir](https://github.com/railsmechanic/nanoid)
393
- * [Haskell](https://github.com/4e6/nanoid-hs)
394
- * [Janet](https://sr.ht/~statianzo/janet-nanoid/)
395
- * [Java](https://github.com/aventrix/jnanoid)
396
- * [Nim](https://github.com/icyphox/nanoid.nim)
397
- * [Perl](https://github.com/tkzwtks/Nanoid-perl)
398
- * [PHP](https://github.com/hidehalo/nanoid-php)
399
- * [Python](https://github.com/puyuan/py-nanoid)
400
- with [dictionaries](https://pypi.org/project/nanoid-dictionary)
401
- * [Ruby](https://github.com/radeno/nanoid.rb)
402
- * [Rust](https://github.com/nikolay-govorov/nanoid)
403
- * [Swift](https://github.com/antiflasher/NanoID)
404
- * [V](https://github.com/invipal/nanoid)
405
-
406
- Also, [CLI] is available to generate IDs from a command line.
407
-
408
- [CLI]: #cli
409
-
410
-
411
- ## API
412
-
413
- ### Async
414
-
415
- To generate hardware random bytes, CPU collects electromagnetic noise.
416
- In the synchronous API during the noise collection, the CPU is busy and
417
- cannot do anything useful in parallel.
418
-
419
- Using the asynchronous API of Nano ID, another code can run during
420
- the entropy collection.
421
-
422
- ```js
423
- import { nanoid } from 'nanoid/async'
424
-
425
- async function createUser () {
426
- user.id = await nanoid()
427
- }
428
- ```
429
-
430
- Unfortunately, you will lose Web Crypto API advantages in a browser
431
- if you use the asynchronous API. So, currently, in the browser, you are limited
432
- with either security or asynchronous behavior.
433
-
434
-
435
- ### Non-Secure
436
-
437
- By default, Nano ID uses hardware random bytes generation for security
438
- and low collision probability. If you are not so concerned with security
439
- and more concerned with performance, you can use the faster non-secure generator.
440
-
441
- ```js
442
- import { nanoid } from 'nanoid/non-secure'
443
- const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
444
- ```
445
-
446
- Note: your IDs will be more predictable and prone to collision attacks.
447
-
448
-
449
- ### Custom Alphabet or Size
450
-
451
- `customAlphabet` allows you to create `nanoid` with your own alphabet
452
- and ID size.
453
-
454
- ```js
455
- import { customAlphabet } from 'nanoid'
456
- const nanoid = customAlphabet('1234567890abcdef', 10)
457
- model.id = nanoid() //=> "4f90d13a42"
458
- ```
459
-
460
- Check the safety of your custom alphabet and ID size in our
461
- [ID collision probability] calculator. For more alphabets, check out the options
462
- in [`nanoid-dictionary`].
463
-
464
- Alphabet must contain 256 symbols or less.
465
- Otherwise, the security of the internal generator algorithm is not guaranteed.
466
-
467
- Customizable asynchronous and non-secure APIs are also available:
468
-
469
- ```js
470
- import { customAlphabet } from 'nanoid/async'
471
- const nanoid = customAlphabet('1234567890abcdef', 10)
472
- async function createUser () {
473
- user.id = await nanoid()
474
- }
475
- ```
476
-
477
- ```js
478
- import { customAlphabet } from 'nanoid/non-secure'
479
- const nanoid = customAlphabet('1234567890abcdef', 10)
480
- user.id = nanoid()
481
- ```
482
-
483
- [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/
484
- [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
485
-
486
-
487
- ### Custom Random Bytes Generator
488
-
489
- `customRandom` allows you to create a `nanoid` and replace alphabet
490
- and the default random bytes generator.
491
-
492
- In this example, a seed-based generator is used:
493
-
494
- ```js
495
- import { customRandom } from 'nanoid'
496
-
497
- const rng = seedrandom(seed)
498
- const nanoid = customRandom('abcdef', 10, size => {
499
- return (new Uint8Array(size)).map(() => 256 * rng())
500
- })
501
-
502
- nanoid() //=> "fbaefaadeb"
503
- ```
504
-
505
- `random` callback must accept the array size and return an array
506
- with random numbers.
507
-
508
- If you want to use the same URL-friendly symbols with `customRandom`,
509
- you can get the default alphabet using the `urlAlphabet`.
510
-
511
- ```js
512
- const { customRandom, urlAlphabet } = require('nanoid')
513
- const nanoid = customRandom(urlAlphabet, 10, random)
514
- ```
515
-
516
- Asynchronous and non-secure APIs are not available for `customRandom`.
38
+ ## Docs
39
+ Read **[full docs](https://github.com/ai/nanoid#readme)** on GitHub.
package/index.cjs CHANGED
@@ -11,6 +11,11 @@ const POOL_SIZE_MULTIPLIER = 32
11
11
  let pool, poolOffset
12
12
 
13
13
  let random = bytes => {
14
+ fillPool(bytes)
15
+ return pool.subarray(poolOffset - bytes, poolOffset)
16
+ }
17
+
18
+ let fillPool = bytes => {
14
19
  if (!pool || pool.length < bytes) {
15
20
  pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16
21
  crypto.randomFillSync(pool)
@@ -19,10 +24,7 @@ let random = bytes => {
19
24
  crypto.randomFillSync(pool)
20
25
  poolOffset = 0
21
26
  }
22
-
23
- let res = pool.subarray(poolOffset, poolOffset + bytes)
24
27
  poolOffset += bytes
25
- return res
26
28
  }
27
29
 
28
30
  let customRandom = (alphabet, size, getRandom) => {
@@ -63,16 +65,15 @@ let customRandom = (alphabet, size, getRandom) => {
63
65
  let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
64
66
 
65
67
  let nanoid = (size = 21) => {
66
- let bytes = random(size)
68
+ fillPool(size)
67
69
  let id = ''
68
- // A compact alternative for `for (let i = 0; i < size; i++)`.
69
- while (size--) {
70
+ for (let i = poolOffset - size; i < poolOffset; i++) {
70
71
  // It is incorrect to use bytes exceeding the alphabet size.
71
72
  // The following mask reduces the random byte in the 0-255 value
72
73
  // range to the 0-63 value range. Therefore, adding hacks, such
73
74
  // as empty string fallback or magic numbers, is unneccessary because
74
75
  // the bitmask trims bytes down to the alphabet size.
75
- id += urlAlphabet[bytes[size] & 63]
76
+ id += urlAlphabet[pool[i] & 63]
76
77
  }
77
78
  return id
78
79
  }
package/index.js CHANGED
@@ -11,6 +11,11 @@ const POOL_SIZE_MULTIPLIER = 32
11
11
  let pool, poolOffset
12
12
 
13
13
  let random = bytes => {
14
+ fillPool(bytes)
15
+ return pool.subarray(poolOffset - bytes, poolOffset)
16
+ }
17
+
18
+ let fillPool = bytes => {
14
19
  if (!pool || pool.length < bytes) {
15
20
  pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16
21
  crypto.randomFillSync(pool)
@@ -19,10 +24,7 @@ let random = bytes => {
19
24
  crypto.randomFillSync(pool)
20
25
  poolOffset = 0
21
26
  }
22
-
23
- let res = pool.subarray(poolOffset, poolOffset + bytes)
24
27
  poolOffset += bytes
25
- return res
26
28
  }
27
29
 
28
30
  let customRandom = (alphabet, size, getRandom) => {
@@ -63,16 +65,15 @@ let customRandom = (alphabet, size, getRandom) => {
63
65
  let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
64
66
 
65
67
  let nanoid = (size = 21) => {
66
- let bytes = random(size)
68
+ fillPool(size)
67
69
  let id = ''
68
- // A compact alternative for `for (let i = 0; i < size; i++)`.
69
- while (size--) {
70
+ for (let i = poolOffset - size; i < poolOffset; i++) {
70
71
  // It is incorrect to use bytes exceeding the alphabet size.
71
72
  // The following mask reduces the random byte in the 0-255 value
72
73
  // range to the 0-63 value range. Therefore, adding hacks, such
73
74
  // as empty string fallback or magic numbers, is unneccessary because
74
75
  // the bitmask trims bytes down to the alphabet size.
75
- id += urlAlphabet[bytes[size] & 63]
76
+ id += urlAlphabet[pool[i] & 63]
76
77
  }
77
78
  return id
78
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.25",
3
+ "version": "3.1.26",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -24,6 +24,9 @@
24
24
  "bin": "./bin/nanoid.cjs",
25
25
  "sideEffects": false,
26
26
  "types": "./index.d.ts",
27
+ "dependencies": {
28
+ "nanocolors": "^0.2.6"
29
+ },
27
30
  "type": "module",
28
31
  "main": "index.cjs",
29
32
  "module": "index.js",