nanoid 3.1.30 → 3.3.8
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/.devcontainer.json +23 -0
- package/README.md +519 -4
- package/async/index.browser.cjs +44 -10
- package/async/index.browser.js +44 -10
- package/async/index.cjs +43 -7
- package/async/index.d.ts +4 -4
- package/async/index.js +43 -7
- package/async/index.native.js +38 -7
- package/bin/nanoid.cjs +52 -2
- package/index.browser.cjs +52 -42
- package/index.browser.js +52 -42
- package/index.cjs +47 -6
- package/index.d.cts +91 -0
- package/index.d.ts +5 -2
- package/index.js +47 -6
- package/nanoid.js +1 -1
- package/non-secure/index.cjs +17 -4
- package/non-secure/index.d.ts +7 -4
- package/non-secure/index.js +17 -4
- package/package.json +42 -18
- package/url-alphabet/index.cjs +4 -0
- package/url-alphabet/index.js +4 -0
- package/index.dev.js +0 -62
- package/index.prod.js +0 -62
package/index.cjs
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
let crypto = require('crypto')
|
2
|
+
|
2
3
|
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
4
|
+
|
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.
|
3
10
|
const POOL_SIZE_MULTIPLIER = 128
|
4
11
|
let pool, poolOffset
|
12
|
+
|
5
13
|
let fillPool = bytes => {
|
6
14
|
if (!pool || pool.length < bytes) {
|
7
15
|
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
@@ -13,32 +21,65 @@ let fillPool = bytes => {
|
|
13
21
|
}
|
14
22
|
poolOffset += bytes
|
15
23
|
}
|
24
|
+
|
16
25
|
let random = bytes => {
|
17
|
-
|
26
|
+
// `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
|
27
|
+
fillPool((bytes |= 0))
|
18
28
|
return pool.subarray(poolOffset - bytes, poolOffset)
|
19
29
|
}
|
20
|
-
|
30
|
+
|
31
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
32
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
33
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
34
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
35
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
21
36
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
22
|
-
|
23
|
-
|
37
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
38
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
39
|
+
// the random bytes redundancy has to be satisfied.
|
40
|
+
|
41
|
+
// Note: every hardware random generator call is performance expensive,
|
42
|
+
// because the system call for entropy collection takes a lot of time.
|
43
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
44
|
+
|
45
|
+
// Next, a step determines how many random bytes to generate.
|
46
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
47
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
48
|
+
// according to benchmarks).
|
49
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
50
|
+
|
51
|
+
return (size = defaultSize) => {
|
24
52
|
let id = ''
|
25
53
|
while (true) {
|
26
54
|
let bytes = getRandom(step)
|
55
|
+
// A compact alternative for `for (let i = 0; i < step; i++)`.
|
27
56
|
let i = step
|
28
57
|
while (i--) {
|
58
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
29
59
|
id += alphabet[bytes[i] & mask] || ''
|
30
60
|
if (id.length === size) return id
|
31
61
|
}
|
32
62
|
}
|
33
63
|
}
|
34
64
|
}
|
35
|
-
|
65
|
+
|
66
|
+
let customAlphabet = (alphabet, size = 21) =>
|
67
|
+
customRandom(alphabet, size, random)
|
68
|
+
|
36
69
|
let nanoid = (size = 21) => {
|
37
|
-
|
70
|
+
// `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
|
71
|
+
fillPool((size |= 0))
|
38
72
|
let id = ''
|
73
|
+
// We are reading directly from the random pool to avoid creating new array
|
39
74
|
for (let i = poolOffset - size; i < poolOffset; i++) {
|
75
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
76
|
+
// The following mask reduces the random byte in the 0-255 value
|
77
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
78
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
79
|
+
// the bitmask trims bytes down to the alphabet size.
|
40
80
|
id += urlAlphabet[pool[i] & 63]
|
41
81
|
}
|
42
82
|
return id
|
43
83
|
}
|
84
|
+
|
44
85
|
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.d.cts
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
/**
|
2
|
+
* Generate secure URL-friendly unique ID.
|
3
|
+
*
|
4
|
+
* By default, the ID will have 21 symbols to have a collision probability
|
5
|
+
* similar to UUID v4.
|
6
|
+
*
|
7
|
+
* ```js
|
8
|
+
* import { nanoid } from 'nanoid'
|
9
|
+
* model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
|
10
|
+
* ```
|
11
|
+
*
|
12
|
+
* @param size Size of the ID. The default size is 21.
|
13
|
+
* @returns A random string.
|
14
|
+
*/
|
15
|
+
export function nanoid(size?: number): string
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Generate secure unique ID with custom alphabet.
|
19
|
+
*
|
20
|
+
* Alphabet must contain 256 symbols or less. Otherwise, the generator
|
21
|
+
* will not be secure.
|
22
|
+
*
|
23
|
+
* @param alphabet Alphabet used to generate the ID.
|
24
|
+
* @param defaultSize Size of the ID. The default size is 21.
|
25
|
+
* @returns A random string generator.
|
26
|
+
*
|
27
|
+
* ```js
|
28
|
+
* const { customAlphabet } = require('nanoid')
|
29
|
+
* const nanoid = customAlphabet('0123456789абвгдеё', 5)
|
30
|
+
* nanoid() //=> "8ё56а"
|
31
|
+
* ```
|
32
|
+
*/
|
33
|
+
export function customAlphabet(
|
34
|
+
alphabet: string,
|
35
|
+
defaultSize?: number
|
36
|
+
): (size?: number) => string
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Generate unique ID with custom random generator and alphabet.
|
40
|
+
*
|
41
|
+
* Alphabet must contain 256 symbols or less. Otherwise, the generator
|
42
|
+
* will not be secure.
|
43
|
+
*
|
44
|
+
* ```js
|
45
|
+
* import { customRandom } from 'nanoid/format'
|
46
|
+
*
|
47
|
+
* const nanoid = customRandom('abcdef', 5, size => {
|
48
|
+
* const random = []
|
49
|
+
* for (let i = 0; i < size; i++) {
|
50
|
+
* random.push(randomByte())
|
51
|
+
* }
|
52
|
+
* return random
|
53
|
+
* })
|
54
|
+
*
|
55
|
+
* nanoid() //=> "fbaef"
|
56
|
+
* ```
|
57
|
+
*
|
58
|
+
* @param alphabet Alphabet used to generate a random string.
|
59
|
+
* @param size Size of the random string.
|
60
|
+
* @param random A random bytes generator.
|
61
|
+
* @returns A random string generator.
|
62
|
+
*/
|
63
|
+
export function customRandom(
|
64
|
+
alphabet: string,
|
65
|
+
size: number,
|
66
|
+
random: (bytes: number) => Uint8Array
|
67
|
+
): () => string
|
68
|
+
|
69
|
+
/**
|
70
|
+
* URL safe symbols.
|
71
|
+
*
|
72
|
+
* ```js
|
73
|
+
* import { urlAlphabet } from 'nanoid'
|
74
|
+
* const nanoid = customAlphabet(urlAlphabet, 10)
|
75
|
+
* nanoid() //=> "Uakgb_J5m9"
|
76
|
+
* ```
|
77
|
+
*/
|
78
|
+
export const urlAlphabet: string
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Generate an array of random bytes collected from hardware noise.
|
82
|
+
*
|
83
|
+
* ```js
|
84
|
+
* import { customRandom, random } from 'nanoid'
|
85
|
+
* const nanoid = customRandom("abcdef", 5, random)
|
86
|
+
* ```
|
87
|
+
*
|
88
|
+
* @param bytes Size of the array.
|
89
|
+
* @returns An array of random bytes.
|
90
|
+
*/
|
91
|
+
export function random(bytes: number): Uint8Array
|
package/index.d.ts
CHANGED
@@ -21,7 +21,7 @@ export function nanoid(size?: number): string
|
|
21
21
|
* will not be secure.
|
22
22
|
*
|
23
23
|
* @param alphabet Alphabet used to generate the ID.
|
24
|
-
* @param
|
24
|
+
* @param defaultSize Size of the ID. The default size is 21.
|
25
25
|
* @returns A random string generator.
|
26
26
|
*
|
27
27
|
* ```js
|
@@ -30,7 +30,10 @@ export function nanoid(size?: number): string
|
|
30
30
|
* nanoid() //=> "8ё56а"
|
31
31
|
* ```
|
32
32
|
*/
|
33
|
-
export function customAlphabet(
|
33
|
+
export function customAlphabet(
|
34
|
+
alphabet: string,
|
35
|
+
defaultSize?: number
|
36
|
+
): (size?: number) => string
|
34
37
|
|
35
38
|
/**
|
36
39
|
* Generate unique ID with custom random generator and alphabet.
|
package/index.js
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
import crypto from 'crypto'
|
2
|
+
|
2
3
|
import { urlAlphabet } from './url-alphabet/index.js'
|
4
|
+
|
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.
|
3
10
|
const POOL_SIZE_MULTIPLIER = 128
|
4
11
|
let pool, poolOffset
|
12
|
+
|
5
13
|
let fillPool = bytes => {
|
6
14
|
if (!pool || pool.length < bytes) {
|
7
15
|
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
@@ -13,32 +21,65 @@ let fillPool = bytes => {
|
|
13
21
|
}
|
14
22
|
poolOffset += bytes
|
15
23
|
}
|
24
|
+
|
16
25
|
let random = bytes => {
|
17
|
-
|
26
|
+
// `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
|
27
|
+
fillPool((bytes |= 0))
|
18
28
|
return pool.subarray(poolOffset - bytes, poolOffset)
|
19
29
|
}
|
20
|
-
|
30
|
+
|
31
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
32
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
33
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
34
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
35
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
21
36
|
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
22
|
-
|
23
|
-
|
37
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
38
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
39
|
+
// the random bytes redundancy has to be satisfied.
|
40
|
+
|
41
|
+
// Note: every hardware random generator call is performance expensive,
|
42
|
+
// because the system call for entropy collection takes a lot of time.
|
43
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
44
|
+
|
45
|
+
// Next, a step determines how many random bytes to generate.
|
46
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
47
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
48
|
+
// according to benchmarks).
|
49
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
50
|
+
|
51
|
+
return (size = defaultSize) => {
|
24
52
|
let id = ''
|
25
53
|
while (true) {
|
26
54
|
let bytes = getRandom(step)
|
55
|
+
// A compact alternative for `for (let i = 0; i < step; i++)`.
|
27
56
|
let i = step
|
28
57
|
while (i--) {
|
58
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
29
59
|
id += alphabet[bytes[i] & mask] || ''
|
30
60
|
if (id.length === size) return id
|
31
61
|
}
|
32
62
|
}
|
33
63
|
}
|
34
64
|
}
|
35
|
-
|
65
|
+
|
66
|
+
let customAlphabet = (alphabet, size = 21) =>
|
67
|
+
customRandom(alphabet, size, random)
|
68
|
+
|
36
69
|
let nanoid = (size = 21) => {
|
37
|
-
|
70
|
+
// `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
|
71
|
+
fillPool((size |= 0))
|
38
72
|
let id = ''
|
73
|
+
// We are reading directly from the random pool to avoid creating new array
|
39
74
|
for (let i = poolOffset - size; i < poolOffset; i++) {
|
75
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
76
|
+
// The following mask reduces the random byte in the 0-255 value
|
77
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
78
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
79
|
+
// the bitmask trims bytes down to the alphabet size.
|
40
80
|
id += urlAlphabet[pool[i] & 63]
|
41
81
|
}
|
42
82
|
return id
|
43
83
|
}
|
84
|
+
|
44
85
|
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/nanoid.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let nanoid=(t=21)=>
|
1
|
+
export let nanoid=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce(((t,e)=>t+=(e&=63)<36?e.toString(36):e<62?(e-26).toString(36).toUpperCase():e<63?"_":"-"),"");
|
package/non-secure/index.cjs
CHANGED
@@ -1,21 +1,34 @@
|
|
1
|
+
// This alphabet uses `A-Za-z0-9_-` symbols.
|
2
|
+
// The order of characters is optimized for better gzip and brotli compression.
|
3
|
+
// References to the same file (works both for gzip and brotli):
|
4
|
+
// `'use`, `andom`, and `rict'`
|
5
|
+
// References to the brotli default dictionary:
|
6
|
+
// `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
|
1
7
|
let urlAlphabet =
|
2
8
|
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
|
3
|
-
|
4
|
-
|
9
|
+
|
10
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
11
|
+
return (size = defaultSize) => {
|
5
12
|
let id = ''
|
6
|
-
|
13
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
14
|
+
let i = size | 0
|
7
15
|
while (i--) {
|
16
|
+
// `| 0` is more compact and faster than `Math.floor()`.
|
8
17
|
id += alphabet[(Math.random() * alphabet.length) | 0]
|
9
18
|
}
|
10
19
|
return id
|
11
20
|
}
|
12
21
|
}
|
22
|
+
|
13
23
|
let nanoid = (size = 21) => {
|
14
24
|
let id = ''
|
15
|
-
|
25
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
26
|
+
let i = size | 0
|
16
27
|
while (i--) {
|
28
|
+
// `| 0` is more compact and faster than `Math.floor()`.
|
17
29
|
id += urlAlphabet[(Math.random() * 64) | 0]
|
18
30
|
}
|
19
31
|
return id
|
20
32
|
}
|
33
|
+
|
21
34
|
module.exports = { nanoid, customAlphabet }
|
package/non-secure/index.d.ts
CHANGED
@@ -13,13 +13,13 @@
|
|
13
13
|
export function nanoid(size?: number): string
|
14
14
|
|
15
15
|
/**
|
16
|
-
* Generate
|
16
|
+
* Generate a unique ID based on a custom alphabet.
|
17
17
|
* This method uses the non-secure predictable random generator
|
18
18
|
* with bigger collision probability.
|
19
19
|
*
|
20
20
|
* @param alphabet Alphabet used to generate the ID.
|
21
|
-
* @param
|
22
|
-
* @returns A random string.
|
21
|
+
* @param defaultSize Size of the ID. The default size is 21.
|
22
|
+
* @returns A random string generator.
|
23
23
|
*
|
24
24
|
* ```js
|
25
25
|
* import { customAlphabet } from 'nanoid/non-secure'
|
@@ -27,4 +27,7 @@ export function nanoid(size?: number): string
|
|
27
27
|
* model.id = //=> "8ё56а"
|
28
28
|
* ```
|
29
29
|
*/
|
30
|
-
export function customAlphabet(
|
30
|
+
export function customAlphabet(
|
31
|
+
alphabet: string,
|
32
|
+
defaultSize?: number
|
33
|
+
): (size?: number) => string
|
package/non-secure/index.js
CHANGED
@@ -1,21 +1,34 @@
|
|
1
|
+
// This alphabet uses `A-Za-z0-9_-` symbols.
|
2
|
+
// The order of characters is optimized for better gzip and brotli compression.
|
3
|
+
// References to the same file (works both for gzip and brotli):
|
4
|
+
// `'use`, `andom`, and `rict'`
|
5
|
+
// References to the brotli default dictionary:
|
6
|
+
// `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
|
1
7
|
let urlAlphabet =
|
2
8
|
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
|
3
|
-
|
4
|
-
|
9
|
+
|
10
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
11
|
+
return (size = defaultSize) => {
|
5
12
|
let id = ''
|
6
|
-
|
13
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
14
|
+
let i = size | 0
|
7
15
|
while (i--) {
|
16
|
+
// `| 0` is more compact and faster than `Math.floor()`.
|
8
17
|
id += alphabet[(Math.random() * alphabet.length) | 0]
|
9
18
|
}
|
10
19
|
return id
|
11
20
|
}
|
12
21
|
}
|
22
|
+
|
13
23
|
let nanoid = (size = 21) => {
|
14
24
|
let id = ''
|
15
|
-
|
25
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
26
|
+
let i = size | 0
|
16
27
|
while (i--) {
|
28
|
+
// `| 0` is more compact and faster than `Math.floor()`.
|
17
29
|
id += urlAlphabet[(Math.random() * 64) | 0]
|
18
30
|
}
|
19
31
|
return id
|
20
32
|
}
|
33
|
+
|
21
34
|
export { nanoid, customAlphabet }
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "nanoid",
|
3
|
-
"version": "3.
|
4
|
-
"description": "A tiny (
|
3
|
+
"version": "3.3.8",
|
4
|
+
"description": "A tiny (116 bytes), secure URL-friendly unique string ID generator",
|
5
5
|
"keywords": [
|
6
6
|
"uuid",
|
7
7
|
"random",
|
@@ -11,6 +11,12 @@
|
|
11
11
|
"engines": {
|
12
12
|
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
|
13
13
|
},
|
14
|
+
"funding": [
|
15
|
+
{
|
16
|
+
"type": "github",
|
17
|
+
"url": "https://github.com/sponsors/ai"
|
18
|
+
}
|
19
|
+
],
|
14
20
|
"author": "Andrey Sitnik <andrey@sitnik.ru>",
|
15
21
|
"license": "MIT",
|
16
22
|
"repository": "ai/nanoid",
|
@@ -29,36 +35,54 @@
|
|
29
35
|
"module": "index.js",
|
30
36
|
"exports": {
|
31
37
|
".": {
|
32
|
-
"browser":
|
33
|
-
|
34
|
-
"
|
35
|
-
"default": "./index.
|
38
|
+
"browser": "./index.browser.js",
|
39
|
+
"require": {
|
40
|
+
"types": "./index.d.cts",
|
41
|
+
"default": "./index.cjs"
|
42
|
+
},
|
43
|
+
"import": {
|
44
|
+
"types": "./index.d.ts",
|
45
|
+
"default": "./index.js"
|
36
46
|
},
|
37
|
-
"
|
38
|
-
"import": "./index.js",
|
39
|
-
"default": "./index.js",
|
40
|
-
"types": "./index.d.ts"
|
47
|
+
"default": "./index.js"
|
41
48
|
},
|
42
49
|
"./package.json": "./package.json",
|
43
50
|
"./async/package.json": "./async/package.json",
|
44
51
|
"./async": {
|
45
52
|
"browser": "./async/index.browser.js",
|
46
|
-
"require":
|
47
|
-
|
53
|
+
"require": {
|
54
|
+
"types": "./index.d.cts",
|
55
|
+
"default": "./async/index.cjs"
|
56
|
+
},
|
57
|
+
"import": {
|
58
|
+
"types": "./index.d.ts",
|
59
|
+
"default": "./async/index.js"
|
60
|
+
},
|
48
61
|
"default": "./async/index.js"
|
49
62
|
},
|
50
63
|
"./non-secure/package.json": "./non-secure/package.json",
|
51
64
|
"./non-secure": {
|
52
|
-
"require":
|
53
|
-
|
65
|
+
"require": {
|
66
|
+
"types": "./index.d.cts",
|
67
|
+
"default": "./non-secure/index.cjs"
|
68
|
+
},
|
69
|
+
"import": {
|
70
|
+
"types": "./index.d.ts",
|
71
|
+
"default": "./non-secure/index.js"
|
72
|
+
},
|
54
73
|
"default": "./non-secure/index.js"
|
55
74
|
},
|
56
75
|
"./url-alphabet/package.json": "./url-alphabet/package.json",
|
57
76
|
"./url-alphabet": {
|
58
|
-
"require":
|
59
|
-
|
77
|
+
"require": {
|
78
|
+
"types": "./index.d.cts",
|
79
|
+
"default": "./url-alphabet/index.cjs"
|
80
|
+
},
|
81
|
+
"import": {
|
82
|
+
"types": "./index.d.ts",
|
83
|
+
"default": "./url-alphabet/index.js"
|
84
|
+
},
|
60
85
|
"default": "./url-alphabet/index.js"
|
61
|
-
}
|
62
|
-
"./index.d.ts": "./index.d.ts"
|
86
|
+
}
|
63
87
|
}
|
64
88
|
}
|
package/url-alphabet/index.cjs
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
// This alphabet uses `A-Za-z0-9_-` symbols.
|
2
|
+
// The order of characters is optimized for better gzip and brotli compression.
|
3
|
+
// Same as in non-secure/index.js
|
1
4
|
let urlAlphabet =
|
2
5
|
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
|
6
|
+
|
3
7
|
module.exports = { urlAlphabet }
|
package/url-alphabet/index.js
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
// This alphabet uses `A-Za-z0-9_-` symbols.
|
2
|
+
// The order of characters is optimized for better gzip and brotli compression.
|
3
|
+
// Same as in non-secure/index.js
|
1
4
|
let urlAlphabet =
|
2
5
|
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
|
6
|
+
|
3
7
|
export { urlAlphabet }
|
package/index.dev.js
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
import { urlAlphabet } from './url-alphabet/index.js'
|
2
|
-
if (true) {
|
3
|
-
if (
|
4
|
-
typeof navigator !== 'undefined' &&
|
5
|
-
navigator.product === 'ReactNative' &&
|
6
|
-
typeof crypto === 'undefined'
|
7
|
-
) {
|
8
|
-
throw new Error(
|
9
|
-
'React Native does not have a built-in secure random generator. ' +
|
10
|
-
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
11
|
-
'For secure IDs, import `react-native-get-random-values` ' +
|
12
|
-
'before Nano ID.'
|
13
|
-
)
|
14
|
-
}
|
15
|
-
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
16
|
-
throw new Error(
|
17
|
-
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
18
|
-
' before importing Nano ID to fix IE 11 support'
|
19
|
-
)
|
20
|
-
}
|
21
|
-
if (typeof crypto === 'undefined') {
|
22
|
-
throw new Error(
|
23
|
-
'Your browser does not have secure random generator. ' +
|
24
|
-
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
25
|
-
)
|
26
|
-
}
|
27
|
-
}
|
28
|
-
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
29
|
-
let customRandom = (alphabet, size, getRandom) => {
|
30
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
31
|
-
let step = -~((1.6 * mask * size) / alphabet.length)
|
32
|
-
return () => {
|
33
|
-
let id = ''
|
34
|
-
while (true) {
|
35
|
-
let bytes = getRandom(step)
|
36
|
-
let j = step
|
37
|
-
while (j--) {
|
38
|
-
id += alphabet[bytes[j] & mask] || ''
|
39
|
-
if (id.length === size) return id
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
}
|
44
|
-
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
45
|
-
let nanoid = (size = 21) => {
|
46
|
-
let id = ''
|
47
|
-
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
48
|
-
while (size--) {
|
49
|
-
let byte = bytes[size] & 63
|
50
|
-
if (byte < 36) {
|
51
|
-
id += byte.toString(36)
|
52
|
-
} else if (byte < 62) {
|
53
|
-
id += (byte - 26).toString(36).toUpperCase()
|
54
|
-
} else if (byte < 63) {
|
55
|
-
id += '_'
|
56
|
-
} else {
|
57
|
-
id += '-'
|
58
|
-
}
|
59
|
-
}
|
60
|
-
return id
|
61
|
-
}
|
62
|
-
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
package/index.prod.js
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
import { urlAlphabet } from './url-alphabet/index.js'
|
2
|
-
if (false) {
|
3
|
-
if (
|
4
|
-
typeof navigator !== 'undefined' &&
|
5
|
-
navigator.product === 'ReactNative' &&
|
6
|
-
typeof crypto === 'undefined'
|
7
|
-
) {
|
8
|
-
throw new Error(
|
9
|
-
'React Native does not have a built-in secure random generator. ' +
|
10
|
-
'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
|
11
|
-
'For secure IDs, import `react-native-get-random-values` ' +
|
12
|
-
'before Nano ID.'
|
13
|
-
)
|
14
|
-
}
|
15
|
-
if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
|
16
|
-
throw new Error(
|
17
|
-
'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
|
18
|
-
' before importing Nano ID to fix IE 11 support'
|
19
|
-
)
|
20
|
-
}
|
21
|
-
if (typeof crypto === 'undefined') {
|
22
|
-
throw new Error(
|
23
|
-
'Your browser does not have secure random generator. ' +
|
24
|
-
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
|
25
|
-
)
|
26
|
-
}
|
27
|
-
}
|
28
|
-
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
29
|
-
let customRandom = (alphabet, size, getRandom) => {
|
30
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
31
|
-
let step = -~((1.6 * mask * size) / alphabet.length)
|
32
|
-
return () => {
|
33
|
-
let id = ''
|
34
|
-
while (true) {
|
35
|
-
let bytes = getRandom(step)
|
36
|
-
let j = step
|
37
|
-
while (j--) {
|
38
|
-
id += alphabet[bytes[j] & mask] || ''
|
39
|
-
if (id.length === size) return id
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
}
|
44
|
-
let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
45
|
-
let nanoid = (size = 21) => {
|
46
|
-
let id = ''
|
47
|
-
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
48
|
-
while (size--) {
|
49
|
-
let byte = bytes[size] & 63
|
50
|
-
if (byte < 36) {
|
51
|
-
id += byte.toString(36)
|
52
|
-
} else if (byte < 62) {
|
53
|
-
id += (byte - 26).toString(36).toUpperCase()
|
54
|
-
} else if (byte < 63) {
|
55
|
-
id += '_'
|
56
|
-
} else {
|
57
|
-
id += '-'
|
58
|
-
}
|
59
|
-
}
|
60
|
-
return id
|
61
|
-
}
|
62
|
-
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|