nanoid 4.0.2 → 5.0.0

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,8 +3,6 @@
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) | [Bahasa Indonesia](./README.id-ID.md)
7
-
8
6
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
9
7
 
10
8
  > “An amazing level of senseless perfectionism,
@@ -16,23 +14,23 @@ A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
16
14
  * **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
17
15
  So ID size was reduced from 36 to 21 symbols.
18
16
  * **Portable.** Nano ID was ported
19
- to [20 programming languages](./README.md#other-programming-languages).
17
+ to over [20 programming languages](./README.md#other-programming-languages).
20
18
 
21
19
  ```js
22
20
  import { nanoid } from 'nanoid'
23
21
  model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
24
22
  ```
25
23
 
26
- Supports modern browsers, IE [with Babel], Node.js and React Native.
24
+ ---
25
+
26
+ <img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="" width="22" height="16" />  Made in <b><a href="https://evilmartians.com/?utm_source=nanoid&utm_campaign=devtools-button&utm_medium=github">Evil Martians</a></b>, product consulting for <b>developer tools</b>.
27
+
28
+ ---
27
29
 
28
30
  [online tool]: https://gitpod.io/#https://github.com/ai/nanoid/
29
31
  [with Babel]: https://developer.epages.com/blog/coding/how-to-transpile-node-modules-with-babel-and-webpack-in-a-monorepo/
30
32
  [Size Limit]: https://github.com/ai/size-limit
31
33
 
32
- <a href="https://evilmartians.com/?utm_source=nanoid">
33
- <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
34
- alt="Sponsored by Evil Martians" width="236" height="54">
35
- </a>
36
34
 
37
35
  ## Docs
38
36
  Read full docs **[here](https://github.com/ai/nanoid#readme)**.
package/index.js CHANGED
@@ -1,24 +1,23 @@
1
- import { randomFillSync } from 'crypto'
2
1
  import { urlAlphabet } from './url-alphabet/index.js'
3
2
  export { urlAlphabet }
4
3
  const POOL_SIZE_MULTIPLIER = 128
5
4
  let pool, poolOffset
6
- let fillPool = bytes => {
5
+ function fillPool(bytes) {
7
6
  if (!pool || pool.length < bytes) {
8
7
  pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
9
- randomFillSync(pool)
8
+ crypto.getRandomValues(pool)
10
9
  poolOffset = 0
11
10
  } else if (poolOffset + bytes > pool.length) {
12
- randomFillSync(pool)
11
+ crypto.getRandomValues(pool)
13
12
  poolOffset = 0
14
13
  }
15
14
  poolOffset += bytes
16
15
  }
17
- export let random = bytes => {
16
+ export function random(bytes) {
18
17
  fillPool((bytes -= 0))
19
18
  return pool.subarray(poolOffset - bytes, poolOffset)
20
19
  }
21
- export let customRandom = (alphabet, defaultSize, getRandom) => {
20
+ export function customRandom(alphabet, defaultSize, getRandom) {
22
21
  let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
23
22
  let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
24
23
  return (size = defaultSize) => {
@@ -33,9 +32,10 @@ export let customRandom = (alphabet, defaultSize, getRandom) => {
33
32
  }
34
33
  }
35
34
  }
36
- export let customAlphabet = (alphabet, size = 21) =>
37
- customRandom(alphabet, size, random)
38
- export let nanoid = (size = 21) => {
35
+ export function customAlphabet(alphabet, size = 21) {
36
+ return customRandom(alphabet, size, random)
37
+ }
38
+ export function nanoid(size = 21) {
39
39
  fillPool((size -= 0))
40
40
  let id = ''
41
41
  for (let i = poolOffset - size; i < poolOffset; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "4.0.2",
3
+ "version": "5.0.0",
4
4
  "description": "A tiny (116 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -10,7 +10,7 @@
10
10
  ],
11
11
  "type": "module",
12
12
  "engines": {
13
- "node": "^14 || ^16 || >=18"
13
+ "node": "^18 || >=20"
14
14
  },
15
15
  "funding": [
16
16
  {
@@ -26,19 +26,11 @@
26
26
  "browser": "./index.browser.js",
27
27
  "default": "./index.js"
28
28
  },
29
- "./async": {
30
- "browser": "./async/index.browser.js",
31
- "default": "./async/index.js"
32
- },
33
29
  "./non-secure": "./non-secure/index.js",
34
30
  "./package.json": "./package.json"
35
31
  },
36
32
  "browser": {
37
- "./index.js": "./index.browser.js",
38
- "./async/index.js": "./async/index.browser.js"
39
- },
40
- "react-native": {
41
- "./async/index.js": "./async/index.native.js"
33
+ "./index.js": "./index.browser.js"
42
34
  },
43
35
  "bin": "./bin/nanoid.js",
44
36
  "sideEffects": false,
@@ -1,33 +0,0 @@
1
- export let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
2
- export 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
- export 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
- }
package/async/index.d.ts DELETED
@@ -1,56 +0,0 @@
1
- /**
2
- * Generate secure URL-friendly unique ID. The non-blocking version.
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/async'
9
- * nanoid().then(id => {
10
- * model.id = id
11
- * })
12
- * ```
13
- *
14
- * @param size Size of the ID. The default size is 21.
15
- * @returns A promise with a random string.
16
- */
17
- export function nanoid(size?: number): Promise<string>
18
-
19
- /**
20
- * A low-level function.
21
- * Generate secure unique ID with custom alphabet. The non-blocking version.
22
- *
23
- * Alphabet must contain 256 symbols or less. Otherwise, the generator
24
- * will not be secure.
25
- *
26
- * @param alphabet Alphabet used to generate the ID.
27
- * @param defaultSize Size of the ID. The default size is 21.
28
- * @returns A function that returns a promise with a random string.
29
- *
30
- * ```js
31
- * import { customAlphabet } from 'nanoid/async'
32
- * const nanoid = customAlphabet('0123456789абвгдеё', 5)
33
- * nanoid().then(id => {
34
- * model.id = id //=> "8ё56а"
35
- * })
36
- * ```
37
- */
38
- export function customAlphabet(
39
- alphabet: string,
40
- defaultSize?: number
41
- ): (size?: number) => Promise<string>
42
-
43
- /**
44
- * Generate an array of random bytes collected from hardware noise.
45
- *
46
- * ```js
47
- * import { random } from 'nanoid/async'
48
- * random(5).then(bytes => {
49
- * bytes //=> [10, 67, 212, 67, 89]
50
- * })
51
- * ```
52
- *
53
- * @param bytes Size of the array.
54
- * @returns A promise with a random bytes array.
55
- */
56
- export function random(bytes: number): Promise<Uint8Array>
package/async/index.js DELETED
@@ -1,34 +0,0 @@
1
- import { randomFill } from 'crypto'
2
- import { urlAlphabet } from '../url-alphabet/index.js'
3
- export let random = bytes =>
4
- new Promise((resolve, reject) => {
5
- randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
6
- if (err) {
7
- reject(err)
8
- } else {
9
- resolve(buf)
10
- }
11
- })
12
- })
13
- export 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
- export 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
- })
@@ -1,25 +0,0 @@
1
- import { getRandomBytesAsync } from 'expo-random'
2
- import { urlAlphabet } from '../url-alphabet/index.js'
3
- export let random = getRandomBytesAsync
4
- export let customAlphabet = (alphabet, defaultSize = 21) => {
5
- let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
6
- let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
7
- let tick = (id, size = defaultSize) =>
8
- random(step).then(bytes => {
9
- let i = step
10
- while (i--) {
11
- id += alphabet[bytes[i] & mask] || ''
12
- if (id.length === size) return id
13
- }
14
- return tick(id, size)
15
- })
16
- return size => tick('', size)
17
- }
18
- export let nanoid = (size = 21) =>
19
- random(size).then(bytes => {
20
- let id = ''
21
- while (size--) {
22
- id += urlAlphabet[bytes[size] & 63]
23
- }
24
- return id
25
- })