nanoid 2.0.2 → 2.1.1
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.
- package/CHANGELOG.md +12 -0
- package/README.md +105 -55
- package/async/format.js +1 -0
- package/async/random.rn.js +14 -0
- package/format.js +1 -0
- package/index.browser.js +7 -0
- package/package.json +11 -2
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
|
+
## 2.1.1
|
5
|
+
* Fix React Native support (by Shawn Hwei).
|
6
|
+
|
7
|
+
## 2.1
|
8
|
+
* Improve React Native support (by Sebastian Werner).
|
9
|
+
|
10
|
+
## 2.0.4
|
11
|
+
* Improve error text for React Native (by Sebastian Werner).
|
12
|
+
|
13
|
+
## 2.0.3
|
14
|
+
* Fix freeze on string in ID length.
|
15
|
+
|
4
16
|
## 2.0.2
|
5
17
|
* Improve docs (by Sylvanus Kateile and Mark Stosberg).
|
6
18
|
|
package/README.md
CHANGED
@@ -6,19 +6,19 @@
|
|
6
6
|
A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
|
7
7
|
|
8
8
|
* **Small.** 141 bytes (minified and gzipped). No dependencies.
|
9
|
-
[Size Limit] controls the size.
|
10
|
-
* **Safe.** It uses cryptographically strong random APIs
|
11
|
-
|
9
|
+
[Size Limit] controls the size.
|
10
|
+
* **Safe.** It uses cryptographically strong random APIs.
|
11
|
+
Can be used in clusters.
|
12
12
|
* **Fast.** It’s 16% faster than UUID.
|
13
13
|
* **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
|
14
|
-
So ID size was reduced from 36 to 21 symbols.
|
14
|
+
So ID size was reduced from 36 to 21 symbols.
|
15
15
|
|
16
16
|
```js
|
17
17
|
var nanoid = require('nanoid')
|
18
18
|
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
|
19
19
|
```
|
20
20
|
|
21
|
-
|
21
|
+
Supports [all browsers], Node.js and React Native.
|
22
22
|
|
23
23
|
[all browsers]: http://caniuse.com/#feat=getrandomvalues
|
24
24
|
[Size Limit]: https://github.com/ai/size-limit
|
@@ -28,6 +28,25 @@ The generator supports Node.js, React Native, and [all browsers].
|
|
28
28
|
alt="Sponsored by Evil Martians" width="236" height="54">
|
29
29
|
</a>
|
30
30
|
|
31
|
+
## Table of Contents
|
32
|
+
|
33
|
+
1. [Comparison with UUID](#comparison-with-uuid)
|
34
|
+
2. [Benchmark](#benchmark)
|
35
|
+
4. [Tools](#tools)
|
36
|
+
3. [Security](#security)
|
37
|
+
6. Usage
|
38
|
+
1. [JS](#js)
|
39
|
+
2. [React](#react)
|
40
|
+
3. [React Native](#react-native)
|
41
|
+
4. [Web Workers](#web-workers)
|
42
|
+
5. [PouchDB and CouchDB](#pouchdb-and-couchdb)
|
43
|
+
5. [Mongoose](#mongoose)
|
44
|
+
6. [Other Programming Languages](#other-programming-languages)
|
45
|
+
7. API
|
46
|
+
1. [Async](#async)
|
47
|
+
2. [Custom Alphabet or Length](#custom-alphabet-or-length)
|
48
|
+
3. [Custom Random Bytes Generator](#custom-random-bytes-generator)
|
49
|
+
|
31
50
|
|
32
51
|
## Comparison with UUID
|
33
52
|
|
@@ -44,7 +63,7 @@ There are three main differences between Nano ID and UUID v4:
|
|
44
63
|
are packed in just 21 symbols instead of 36.
|
45
64
|
2. Nano ID code is 3 times less than `uuid/v4` package:
|
46
65
|
141 bytes instead of 435.
|
47
|
-
3. Because of memory allocation tricks, Nano ID 16% faster than UUID.
|
66
|
+
3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.
|
48
67
|
|
49
68
|
|
50
69
|
## Benchmark
|
@@ -70,6 +89,19 @@ rndm 2,413,565 ops/sec
|
|
70
89
|
```
|
71
90
|
|
72
91
|
|
92
|
+
## Tools
|
93
|
+
|
94
|
+
* [ID size calculator] to choice smaller ID size depends on your case.
|
95
|
+
* [`nanoid-dictionary`] with popular alphabets to use with `nanoid/generate`.
|
96
|
+
* [`nanoid-cli`] to generate ID from CLI.
|
97
|
+
* [`nanoid-good`] to be sure that your ID doesn't contain any obscene words.
|
98
|
+
|
99
|
+
[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
|
100
|
+
[ID size calculator]: https://zelark.github.io/nano-id-cc/
|
101
|
+
[`nanoid-cli`]: https://github.com/twhitbeck/nanoid-cli
|
102
|
+
[`nanoid-good`]: https://github.com/y-gagar1n/nanoid-good
|
103
|
+
|
104
|
+
|
73
105
|
## Security
|
74
106
|
|
75
107
|
*See a good article about random generators theory:
|
@@ -99,43 +131,17 @@ Nano ID uses a [better algorithm] and is tested for uniformity.
|
|
99
131
|
[better algorithm]: https://github.com/ai/nanoid/blob/master/format.js
|
100
132
|
|
101
133
|
|
102
|
-
|
103
|
-
|
104
|
-
* [ID size calculator] to choice smaller ID size depends on your case.
|
105
|
-
* [`nanoid-dictionary`] with popular alphabets to use with `nanoid/generate`.
|
106
|
-
* [`nanoid-cli`] to generate ID from CLI.
|
107
|
-
* [`nanoid-good`] to be sure that your ID doesn't contain any obscene words.
|
108
|
-
|
109
|
-
[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
|
110
|
-
[ID size calculator]: https://zelark.github.io/nano-id-cc/
|
111
|
-
[`nanoid-cli`]: https://github.com/twhitbeck/nanoid-cli
|
112
|
-
[`nanoid-good`]: https://github.com/y-gagar1n/nanoid-good
|
113
|
-
|
114
|
-
|
115
|
-
## Other Programming Languages
|
116
|
-
|
117
|
-
* [C#](https://github.com/codeyu/nanoid-net)
|
118
|
-
* [Clojure and ClojureScript](https://github.com/zelark/nano-id)
|
119
|
-
* [Crystal](https://github.com/mamantoha/nanoid.cr)
|
120
|
-
* [Dart](https://github.com/pd4d10/nanoid)
|
121
|
-
* [Go](https://github.com/matoous/go-nanoid)
|
122
|
-
* [Elixir](https://github.com/railsmechanic/nanoid)
|
123
|
-
* [Haskell](https://github.com/4e6/nanoid-hs)
|
124
|
-
* [Java](https://github.com/aventrix/jnanoid)
|
125
|
-
* [Nim](https://github.com/icyphox/nanoid.nim)
|
126
|
-
* [PHP](https://github.com/hidehalo/nanoid-php)
|
127
|
-
* [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://github.com/aidarkhanov/py-nanoid-dictionary)
|
128
|
-
* [Ruby](https://github.com/radeno/nanoid.rb)
|
129
|
-
* [Rust](https://github.com/nikolay-govorov/nanoid)
|
130
|
-
* [Swift](https://github.com/antiflasher/NanoID)
|
131
|
-
|
132
|
-
Also, [CLI tool] is available to generate IDs from a command line.
|
134
|
+
### Vulnerabilities
|
133
135
|
|
134
|
-
|
136
|
+
To report a security vulnerability, please use the
|
137
|
+
[Tidelift security contact](https://tidelift.com/security).
|
138
|
+
Tidelift will coordinate the fix and disclosure.
|
135
139
|
|
136
140
|
|
137
141
|
## Usage
|
138
142
|
|
143
|
+
### JS
|
144
|
+
|
139
145
|
The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID
|
140
146
|
with 21 characters (to have a collision probability similar to UUID v4).
|
141
147
|
|
@@ -156,6 +162,7 @@ in our [ID collision probability] calculator.
|
|
156
162
|
|
157
163
|
[ID collision probability]: https://zelark.github.io/nano-id-cc/
|
158
164
|
|
165
|
+
|
159
166
|
### React
|
160
167
|
|
161
168
|
**Do not** use a nanoid for `key` prop. In React `key` should be consistence
|
@@ -165,38 +172,54 @@ between renders. This is bad code:
|
|
165
172
|
<Item key={nanoid()} /> /* DON’T DO IT */
|
166
173
|
```
|
167
174
|
|
168
|
-
This is good code.
|
169
|
-
because Nano ID could be started from number. HTML ID can’t be started
|
170
|
-
from the number.
|
175
|
+
This is good code. `this.id` will be generated only once:
|
171
176
|
|
172
177
|
```jsx
|
173
|
-
id =
|
174
|
-
|
178
|
+
id = nanoid()
|
175
179
|
render () {
|
176
|
-
return
|
177
|
-
<label htmlFor={this.id}>Label text</label>
|
178
|
-
<input id={this.id} type="text"/>
|
179
|
-
</>;
|
180
|
+
return <Item key={this.id}>;
|
180
181
|
}
|
181
182
|
}
|
182
183
|
```
|
183
184
|
|
185
|
+
If you want to use Nano ID for `id`, you must to set some string prefix.
|
186
|
+
Nano ID could be started from number. HTML ID can’t be started from the number.
|
187
|
+
|
188
|
+
```jsx
|
189
|
+
<input id={'id' + this.id} type="text"/>
|
190
|
+
```
|
191
|
+
|
192
|
+
|
184
193
|
### React Native
|
185
194
|
|
186
|
-
|
187
|
-
|
195
|
+
React Native doesn’t have built-in random generator.
|
196
|
+
|
197
|
+
1. Check [`expo-random`] docs and install it.
|
198
|
+
2. Use `nanoid/async` instead of synchronous `nanoid`.
|
188
199
|
|
189
200
|
```js
|
190
|
-
const
|
191
|
-
const format = require('nanoid/async/format')
|
192
|
-
const url = require('nanoid/url')
|
201
|
+
const nanoid = require('nanoid/async')
|
193
202
|
|
194
203
|
async function createUser () {
|
195
|
-
user.id = await
|
204
|
+
user.id = await nanoid()
|
196
205
|
}
|
197
206
|
```
|
198
207
|
|
199
|
-
|
208
|
+
|
209
|
+
### PouchDB and CouchDB
|
210
|
+
|
211
|
+
In PouchDB and CouchDB, IDs can’t start with an underscore `_`.
|
212
|
+
A prefix is required to prevent this issue, as Nano ID might use a `_`
|
213
|
+
at the start of the ID by default.
|
214
|
+
|
215
|
+
Override the default ID with the following option:
|
216
|
+
|
217
|
+
```js
|
218
|
+
db.put({
|
219
|
+
_id: 'id' + nanoid(),
|
220
|
+
…
|
221
|
+
})
|
222
|
+
```
|
200
223
|
|
201
224
|
|
202
225
|
### Mongoose
|
@@ -205,7 +228,7 @@ async function createUser () {
|
|
205
228
|
const mySchema = new Schema({
|
206
229
|
_id: {
|
207
230
|
type: String,
|
208
|
-
default: () => nanoid(
|
231
|
+
default: () => nanoid()
|
209
232
|
}
|
210
233
|
})
|
211
234
|
```
|
@@ -227,6 +250,33 @@ model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
|
|
227
250
|
```
|
228
251
|
|
229
252
|
|
253
|
+
### Other Programming Languages
|
254
|
+
|
255
|
+
Nano ID was ported to many languages. You can use these ports to have the same
|
256
|
+
ID generators on client and server side.
|
257
|
+
|
258
|
+
* [C#](https://github.com/codeyu/nanoid-net)
|
259
|
+
* [Clojure and ClojureScript](https://github.com/zelark/nano-id)
|
260
|
+
* [Crystal](https://github.com/mamantoha/nanoid.cr)
|
261
|
+
* [Dart](https://github.com/pd4d10/nanoid-dart)
|
262
|
+
* [Go](https://github.com/matoous/go-nanoid)
|
263
|
+
* [Elixir](https://github.com/railsmechanic/nanoid)
|
264
|
+
* [Haskell](https://github.com/4e6/nanoid-hs)
|
265
|
+
* [Java](https://github.com/aventrix/jnanoid)
|
266
|
+
* [Nim](https://github.com/icyphox/nanoid.nim)
|
267
|
+
* [PHP](https://github.com/hidehalo/nanoid-php)
|
268
|
+
* [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://pypi.org/project/nanoid-dictionary)
|
269
|
+
* [Ruby](https://github.com/radeno/nanoid.rb)
|
270
|
+
* [Rust](https://github.com/nikolay-govorov/nanoid)
|
271
|
+
* [Swift](https://github.com/antiflasher/NanoID)
|
272
|
+
|
273
|
+
Also, [CLI tool] is available to generate IDs from a command line.
|
274
|
+
|
275
|
+
[CLI tool]: https://github.com/twhitbeck/nanoid-cli
|
276
|
+
|
277
|
+
|
278
|
+
## API
|
279
|
+
|
230
280
|
### Async
|
231
281
|
|
232
282
|
To generate hardware random bytes, CPU will collect electromagnetic noise.
|
package/async/format.js
CHANGED
@@ -31,6 +31,7 @@
|
|
31
31
|
module.exports = function (random, alphabet, size) {
|
32
32
|
var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
|
33
33
|
var step = Math.ceil(1.6 * mask * size / alphabet.length)
|
34
|
+
size = +size
|
34
35
|
|
35
36
|
function tick (id) {
|
36
37
|
return random(step).then(function (bytes) {
|
@@ -0,0 +1,14 @@
|
|
1
|
+
var random
|
2
|
+
try {
|
3
|
+
random = require('expo-random')
|
4
|
+
} catch (e) {
|
5
|
+
throw new Error(
|
6
|
+
'React-Native does not have a built-in secure random generator. ' +
|
7
|
+
'Install `expo-random` locally or ' +
|
8
|
+
'if you don’t need unpredictable IDs, you can use `nanoid/non-secure`.'
|
9
|
+
)
|
10
|
+
}
|
11
|
+
|
12
|
+
module.exports = function (bytes) {
|
13
|
+
return random.getRandomBytesAsync(bytes)
|
14
|
+
}
|
package/format.js
CHANGED
package/index.browser.js
CHANGED
@@ -1,4 +1,11 @@
|
|
1
1
|
if (process.env.NODE_ENV !== 'production') {
|
2
|
+
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
3
|
+
throw new Error(
|
4
|
+
'React Native does not have a built-in secure random generator. ' +
|
5
|
+
'If you don’t need unpredictable IDs, you can use `nanoid/non-secure`. ' +
|
6
|
+
'For secure ID install `expo-random` locally and use `nanoid/async`.'
|
7
|
+
)
|
8
|
+
}
|
2
9
|
if (typeof self === 'undefined' || (!self.crypto && !self.msCrypto)) {
|
3
10
|
throw new Error(
|
4
11
|
'Your browser does not have secure random generator. ' +
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "nanoid",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.1.1",
|
4
4
|
"description": "A tiny (141 bytes), secure URL-friendly unique string ID generator",
|
5
5
|
"keywords": [
|
6
6
|
"uuid",
|
@@ -17,5 +17,14 @@
|
|
17
17
|
"./async/index.js": "./async/index.browser.js",
|
18
18
|
"./async/random.js": "./async/random.browser.js"
|
19
19
|
},
|
20
|
-
"
|
20
|
+
"react-native": {
|
21
|
+
"./async/random.js": "./async/random.rn.js"
|
22
|
+
},
|
23
|
+
"sideEffects": false,
|
24
|
+
"eslintIgnore": [
|
25
|
+
"test/demo/build"
|
26
|
+
],
|
27
|
+
"sharec": {
|
28
|
+
"version": "0.4.4"
|
29
|
+
}
|
21
30
|
}
|