nanoid
Advanced tools
Comparing version 1.2.6 to 1.3.0
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 1.3 | ||
* Add `nanoid/async/format` and `nanoid/async/generate`. | ||
* Improve synchronous API performance. | ||
* Reduce `url` size (by Daniil Poroshin). | ||
* Improve React Native docs (by joelgetaction). | ||
## 1.2.6 | ||
@@ -5,0 +11,0 @@ * Reduce library size (by rqrqrqrq). |
{ | ||
"name": "nanoid", | ||
"version": "1.2.6", | ||
"version": "1.3.0", | ||
"description": "A tiny (143 bytes), secure URL-friendly unique string ID generator", | ||
@@ -15,6 +15,7 @@ "keywords": [ | ||
"browser": { | ||
"./index.js": "./index.browser.js", | ||
"./random.js": "./random.browser.js", | ||
"./index.js": "./index.browser.js", | ||
"./async.js": "./async.browser.js" | ||
"./async/index.js": "./async/index.browser.js", | ||
"./async/random.js": "./async/random.browser.js" | ||
} | ||
} |
var crypto = require('crypto') | ||
if (crypto.randomFillSync) { | ||
var buffers = { } | ||
module.exports = function (bytes) { | ||
return crypto.randomFillSync(Buffer.allocUnsafe(bytes)) | ||
var buffer = buffers[bytes] | ||
if (!buffer) { | ||
buffer = Buffer.allocUnsafe(bytes) | ||
if (bytes <= 255) buffers[bytes] = buffer | ||
} | ||
return crypto.randomFillSync(buffer) | ||
} | ||
@@ -7,0 +13,0 @@ } else { |
@@ -83,15 +83,16 @@ # Nano ID | ||
$ ./test/benchmark | ||
nanoid 400,771 ops/sec | ||
nanoid/generate 401,995 ops/sec | ||
uid.sync 357,449 ops/sec | ||
uuid/v4 353,771 ops/sec | ||
shortid 33,758 ops/sec | ||
nanoid 413,579 ops/sec | ||
nanoid/generate 401,349 ops/sec | ||
uid.sync 354,882 ops/sec | ||
uuid/v4 353,836 ops/sec | ||
shortid 39,152 ops/sec | ||
Async: | ||
nanoid/async 85,251 ops/sec | ||
uid 76,991 ops/sec | ||
nanoid/async 85,168 ops/sec | ||
nanoid/async/generate 81,037 ops/sec | ||
uid 78,426 ops/sec | ||
Non-secure: | ||
nanoid/non-secure 2,807,009 ops/sec | ||
rndm 2,629,172 ops/sec | ||
nanoid/non-secure 2,718,186 ops/sec | ||
rndm 2,544,612 ops/sec | ||
``` | ||
@@ -128,11 +129,29 @@ | ||
### React Native and Web Workers | ||
### React Native | ||
React Native and Web Worker don’t have access to secure random generator. | ||
To generate secure random IDs in React Native, you must use | ||
[a native random generator] like and asynchronous API: | ||
Security is important in ID, when ID should be unpredictable. For instance, | ||
```js | ||
const generateSecureRandom = require('react-native-securerandom').generateSecureRandom | ||
const format = require('nanoid/async/format') | ||
const url = require('nanoid/url') | ||
async function createUser () { | ||
user.id = await format(generateSecureRandom, url, 21); | ||
} | ||
``` | ||
[a native random generator]: https://github.com/rh389/react-native-securerandom | ||
### Web Workers | ||
Web Workers don’t have access to a secure random generator. | ||
Security is important in IDs, when IDs should be unpredictable. For instance, | ||
in “access by URL” link generation. | ||
If you don’t need unpredictable IDs, but you need React Native | ||
or Web Workers support, you can use non‑secure ID generator. | ||
If you don’t need unpredictable IDs, but you need Web Workers support, | ||
you can use non‑secure ID generator. | ||
@@ -181,2 +200,11 @@ ```js | ||
Asynchronous API is also available: | ||
```js | ||
const generate = require('nanoid/async/generate') | ||
async function createUser () { | ||
user.id = await generate('1234567890abcdef', 10) | ||
} | ||
``` | ||
[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/ | ||
@@ -216,3 +244,18 @@ [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary | ||
Asynchronous API is also available: | ||
```js | ||
const format = require('nanoid/async/format') | ||
const url = require('nanoid/url') | ||
function random (size) { | ||
return new Promise(…) | ||
} | ||
async function createUser () { | ||
user.id = await format(random, url, 10) | ||
} | ||
``` | ||
## Tools | ||
@@ -219,0 +262,0 @@ |
/** | ||
* URL safe symbols. | ||
* | ||
* This alphabet uses a-z A-Z 0-9 _~ symbols. | ||
* (Symbols order was changed for better gzip compression.) | ||
* | ||
* @name url | ||
@@ -12,4 +15,2 @@ * @type {string} | ||
module.exports = | ||
'_~0123456789' + | ||
'abcdefghijklmnopqrstuvwxyz' + | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
'functio_~0123456789abdeghjklmpqrsvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
20733
18
353
290