nanoid 3.3.5 → 3.3.7

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.
@@ -0,0 +1,34 @@
1
+ let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
2
+ let customAlphabet = (alphabet, defaultSize = 21) => {
3
+ let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
4
+ let step = -~((1.6 * mask * defaultSize) / alphabet.length)
5
+ return async (size = defaultSize) => {
6
+ let id = ''
7
+ while (true) {
8
+ let bytes = crypto.getRandomValues(new Uint8Array(step))
9
+ let i = step
10
+ while (i--) {
11
+ id += alphabet[bytes[i] & mask] || ''
12
+ if (id.length === size) return id
13
+ }
14
+ }
15
+ }
16
+ }
17
+ let nanoid = async (size = 21) => {
18
+ let id = ''
19
+ let bytes = crypto.getRandomValues(new Uint8Array(size))
20
+ while (size--) {
21
+ let byte = bytes[size] & 63
22
+ if (byte < 36) {
23
+ id += byte.toString(36)
24
+ } else if (byte < 62) {
25
+ id += (byte - 26).toString(36).toUpperCase()
26
+ } else if (byte < 63) {
27
+ id += '_'
28
+ } else {
29
+ id += '-'
30
+ }
31
+ }
32
+ return id
33
+ }
34
+ module.exports = { nanoid, customAlphabet, random }
@@ -31,4 +31,4 @@ let nanoid = async (size = 21) => {
31
31
  }
32
32
  return id
33
33
  }
34
- module.exports = { nanoid, customAlphabet, random }
34
+ export { nanoid, customAlphabet, random }
@@ -0,0 +1,35 @@
1
+ let crypto = require('crypto')
2
+ let { urlAlphabet } = require('../url-alphabet/index.cjs')
3
+ let random = bytes =>
4
+ new Promise((resolve, reject) => {
5
+ crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
6
+ if (err) {
7
+ reject(err)
8
+ } else {
9
+ resolve(buf)
10
+ }
11
+ })
12
+ })
13
+ let customAlphabet = (alphabet, defaultSize = 21) => {
14
+ let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
15
+ let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
16
+ let tick = (id, size = defaultSize) =>
17
+ random(step).then(bytes => {
18
+ let i = step
19
+ while (i--) {
20
+ id += alphabet[bytes[i] & mask] || ''
21
+ if (id.length === size) return id
22
+ }
23
+ return tick(id, size)
24
+ })
25
+ return size => tick('', size)
26
+ }
27
+ let nanoid = (size = 21) =>
28
+ random(size).then(bytes => {
29
+ let id = ''
30
+ while (size--) {
31
+ id += urlAlphabet[bytes[size] & 63]
32
+ }
33
+ return id
34
+ })
35
+ module.exports = { nanoid, customAlphabet, random }
package/async/index.js CHANGED
@@ -1,5 +1,5 @@
1
- let crypto = require('crypto')
2
- let { urlAlphabet } = require('../url-alphabet')
1
+ import crypto from 'crypto'
2
+ import { urlAlphabet } from '../url-alphabet/index.js'
3
3
  let random = bytes =>
4
4
  new Promise((resolve, reject) => {
5
5
  crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
@@ -32,4 +32,4 @@ let nanoid = (size = 21) =>
32
32
  }
33
33
  return id
34
34
  })
35
- module.exports = { nanoid, customAlphabet, random }
35
+ export { nanoid, customAlphabet, random }
@@ -1,5 +1,5 @@
1
- let { getRandomBytesAsync } = require('expo-random')
2
- let { urlAlphabet } = require('../url-alphabet')
1
+ import { getRandomBytesAsync } from 'expo-random'
2
+ import { urlAlphabet } from '../url-alphabet/index.js'
3
3
  let random = getRandomBytesAsync
4
4
  let customAlphabet = (alphabet, defaultSize = 21) => {
5
5
  let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
@@ -23,4 +23,4 @@ let nanoid = (size = 21) =>
23
23
  }
24
24
  return id
25
25
  })
26
- module.exports = { nanoid, customAlphabet, random }
26
+ export { nanoid, customAlphabet, random }
@@ -0,0 +1,12 @@
1
+ {
2
+ "type": "module",
3
+ "main": "index.cjs",
4
+ "module": "index.js",
5
+ "react-native": {
6
+ "./index.js": "./index.native.js"
7
+ },
8
+ "browser": {
9
+ "./index.js": "./index.browser.js",
10
+ "./index.cjs": "./index.browser.cjs"
11
+ }
12
+ }
@@ -0,0 +1,34 @@
1
+ let { urlAlphabet } = require('./url-alphabet/index.cjs')
2
+ let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
3
+ let customRandom = (alphabet, defaultSize, getRandom) => {
4
+ let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
5
+ let step = -~((1.6 * mask * defaultSize) / alphabet.length)
6
+ return (size = defaultSize) => {
7
+ let id = ''
8
+ while (true) {
9
+ let bytes = getRandom(step)
10
+ let j = step
11
+ while (j--) {
12
+ id += alphabet[bytes[j] & mask] || ''
13
+ if (id.length === size) return id
14
+ }
15
+ }
16
+ }
17
+ }
18
+ let customAlphabet = (alphabet, size = 21) =>
19
+ customRandom(alphabet, size, random)
20
+ let nanoid = (size = 21) =>
21
+ crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
22
+ byte &= 63
23
+ if (byte < 36) {
24
+ id += byte.toString(36)
25
+ } else if (byte < 62) {
26
+ id += (byte - 26).toString(36).toUpperCase()
27
+ } else if (byte > 62) {
28
+ id += '-'
29
+ } else {
30
+ id += '_'
31
+ }
32
+ return id
33
+ }, '')
34
+ module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
package/index.browser.js CHANGED
@@ -1,4 +1,4 @@
1
- let { urlAlphabet } = require('./url-alphabet')
1
+ import { urlAlphabet } from './url-alphabet/index.js'
2
2
  let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
3
3
  let customRandom = (alphabet, defaultSize, getRandom) => {
4
4
  let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
@@ -31,4 +31,4 @@ let nanoid = (size = 21) =>
31
31
  }
32
32
  return id
33
33
  }, '')
34
- module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
34
+ export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
package/index.cjs ADDED
@@ -0,0 +1,45 @@
1
+ let crypto = require('crypto')
2
+ let { urlAlphabet } = require('./url-alphabet/index.cjs')
3
+ const POOL_SIZE_MULTIPLIER = 128
4
+ let pool, poolOffset
5
+ let fillPool = bytes => {
6
+ if (!pool || pool.length < bytes) {
7
+ pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
8
+ crypto.randomFillSync(pool)
9
+ poolOffset = 0
10
+ } else if (poolOffset + bytes > pool.length) {
11
+ crypto.randomFillSync(pool)
12
+ poolOffset = 0
13
+ }
14
+ poolOffset += bytes
15
+ }
16
+ let random = bytes => {
17
+ fillPool((bytes -= 0))
18
+ return pool.subarray(poolOffset - bytes, poolOffset)
19
+ }
20
+ let customRandom = (alphabet, defaultSize, getRandom) => {
21
+ let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
22
+ let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
23
+ return (size = defaultSize) => {
24
+ let id = ''
25
+ while (true) {
26
+ let bytes = getRandom(step)
27
+ let i = step
28
+ while (i--) {
29
+ id += alphabet[bytes[i] & mask] || ''
30
+ if (id.length === size) return id
31
+ }
32
+ }
33
+ }
34
+ }
35
+ let customAlphabet = (alphabet, size = 21) =>
36
+ customRandom(alphabet, size, random)
37
+ let nanoid = (size = 21) => {
38
+ fillPool((size -= 0))
39
+ let id = ''
40
+ for (let i = poolOffset - size; i < poolOffset; i++) {
41
+ id += urlAlphabet[pool[i] & 63]
42
+ }
43
+ return id
44
+ }
45
+ 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.js CHANGED
@@ -1,5 +1,5 @@
1
- let crypto = require('crypto')
2
- let { urlAlphabet } = require('./url-alphabet')
1
+ import crypto from 'crypto'
2
+ import { urlAlphabet } from './url-alphabet/index.js'
3
3
  const POOL_SIZE_MULTIPLIER = 128
4
4
  let pool, poolOffset
5
5
  let fillPool = bytes => {
@@ -42,4 +42,4 @@ let nanoid = (size = 21) => {
42
42
  }
43
43
  return id
44
44
  }
45
- module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
45
+ export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
@@ -0,0 +1,21 @@
1
+ let urlAlphabet =
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
+ let customAlphabet = (alphabet, defaultSize = 21) => {
4
+ return (size = defaultSize) => {
5
+ let id = ''
6
+ let i = size
7
+ while (i--) {
8
+ id += alphabet[(Math.random() * alphabet.length) | 0]
9
+ }
10
+ return id
11
+ }
12
+ }
13
+ let nanoid = (size = 21) => {
14
+ let id = ''
15
+ let i = size
16
+ while (i--) {
17
+ id += urlAlphabet[(Math.random() * 64) | 0]
18
+ }
19
+ return id
20
+ }
21
+ module.exports = { nanoid, customAlphabet }
@@ -18,4 +18,4 @@ let nanoid = (size = 21) => {
18
18
  }
19
19
  return id
20
20
  }
21
- module.exports = { nanoid, customAlphabet }
21
+ export { nanoid, customAlphabet }
@@ -0,0 +1,6 @@
1
+ {
2
+ "type": "module",
3
+ "main": "index.cjs",
4
+ "module": "index.js",
5
+ "react-native": "index.js"
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.3.5",
3
+ "version": "3.3.7",
4
4
  "description": "A tiny (116 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -23,12 +23,66 @@
23
23
  "browser": {
24
24
  "./index.js": "./index.browser.js",
25
25
  "./async/index.js": "./async/index.browser.js",
26
- "./async/index.cjs": "./async/index.browser.cjs"
27
- },
28
- "react-native": {
29
- "./async/index.js": "./async/index.native.js"
26
+ "./async/index.cjs": "./async/index.browser.cjs",
27
+ "./index.cjs": "./index.browser.cjs"
30
28
  },
29
+ "react-native": "index.js",
31
30
  "bin": "./bin/nanoid.cjs",
32
31
  "sideEffects": false,
33
- "types": "./index.d.ts"
34
- }
32
+ "types": "./index.d.ts",
33
+ "type": "module",
34
+ "main": "index.cjs",
35
+ "module": "index.js",
36
+ "exports": {
37
+ ".": {
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"
46
+ },
47
+ "default": "./index.js"
48
+ },
49
+ "./package.json": "./package.json",
50
+ "./async/package.json": "./async/package.json",
51
+ "./async": {
52
+ "browser": "./async/index.browser.js",
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
+ },
61
+ "default": "./async/index.js"
62
+ },
63
+ "./non-secure/package.json": "./non-secure/package.json",
64
+ "./non-secure": {
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
+ },
73
+ "default": "./non-secure/index.js"
74
+ },
75
+ "./url-alphabet/package.json": "./url-alphabet/package.json",
76
+ "./url-alphabet": {
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
+ },
85
+ "default": "./url-alphabet/index.js"
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,3 @@
1
+ let urlAlphabet =
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
+ module.exports = { urlAlphabet }
@@ -1,3 +1,3 @@
1
1
  let urlAlphabet =
2
2
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
- module.exports = { urlAlphabet }
3
+ export { urlAlphabet }
@@ -0,0 +1,6 @@
1
+ {
2
+ "type": "module",
3
+ "main": "index.cjs",
4
+ "module": "index.js",
5
+ "react-native": "index.js"
6
+ }