nanoid 5.1.6 → 5.1.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright 2017 Andrey Sitnik <andrey@sitnik.ru>
3
+ Copyright 2017 Andrey Sitnik <andrey@sitnik.es>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
package/README.md CHANGED
@@ -3,19 +3,19 @@
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.ja.md) | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md) | [Bahasa Indonesia](./README.id-ID.md) | [한국어](./README.ko.md)
6
+ **English** | [日本語](./README.ja.md) | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md) | [Bahasa Indonesia](./README.id-ID.md) | [한국어](./README.ko.md) | [العربية](./README.ar.md)
7
7
 
8
8
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
9
9
 
10
10
  > “An amazing level of senseless perfectionism,
11
11
  > which is simply impossible not to respect.”
12
12
 
13
- * **Small.** 118 bytes (minified and brotlied). No dependencies.
13
+ - **Small.** 118 bytes (minified and brotlied). No dependencies.
14
14
  [Size Limit] controls the size.
15
- * **Safe.** It uses hardware random generator. Can be used in clusters.
16
- * **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
15
+ - **Safe.** It uses hardware random generator. Can be used in clusters.
16
+ - **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
17
17
  So ID size was reduced from 36 to 21 symbols.
18
- * **Portable.** Nano ID was ported
18
+ - **Portable.** Nano ID was ported
19
19
  to over [20 programming languages](./README.md#other-programming-languages).
20
20
 
21
21
  ```js
@@ -30,9 +30,8 @@ model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
30
30
  ---
31
31
 
32
32
  [online tool]: https://gitpod.io/#https://github.com/ai/nanoid/
33
- [with Babel]: https://developer.epages.com/blog/coding/how-to-transpile-node-modules-with-babel-and-webpack-in-a-monorepo/
34
- [Size Limit]: https://github.com/ai/size-limit
35
-
33
+ [with Babel]: https://developer.epages.com/blog/coding/how-to-transpile-node-modules-with-babel-and-webpack-in-a-monorepo/
34
+ [Size Limit]: https://github.com/ai/size-limit
36
35
 
37
36
  ## Docs
38
37
  Read full docs **[here](https://github.com/ai/nanoid#readme)**.
package/SECURITY.md ADDED
@@ -0,0 +1,21 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ We supports only the latest version and `3.x` branch.
6
+
7
+ | Version | Supported |
8
+ | ------- | ------------------ |
9
+ | 5.1.x | :white_check_mark: |
10
+ | 5.0.x | :x: |
11
+ | 4.0.x | :x: |
12
+ | 3.3.x | :white_check_mark: |
13
+ | 3.0.x | :x: |
14
+ | < 3.0 | :x: |
15
+
16
+ ## Reporting a Vulnerability
17
+
18
+ To report a security vulnerability, please use the [Tidelift security contact].
19
+ Tidelift will coordinate the fix and disclosure.
20
+
21
+ [Tidelift security contact]: https://tidelift.com/security
package/bin/nanoid.js CHANGED
@@ -1,4 +1,7 @@
1
1
  #!/usr/bin/env node
2
+ import { readFileSync } from 'node:fs'
3
+ import { dirname, join } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
2
5
  import { customAlphabet, nanoid } from '../index.js'
3
6
  function print(msg) {
4
7
  process.stdout.write(msg + '\n')
@@ -7,12 +10,19 @@ function error(msg) {
7
10
  process.stderr.write(msg + '\n')
8
11
  process.exit(1)
9
12
  }
13
+ if (process.argv.includes('--version') || process.argv.includes('-v')) {
14
+ let root = dirname(fileURLToPath(import.meta.url))
15
+ let pkg = JSON.parse(readFileSync(join(root, '..', 'package.json'), 'utf8'))
16
+ print(pkg.version)
17
+ process.exit()
18
+ }
10
19
  if (process.argv.includes('--help') || process.argv.includes('-h')) {
11
20
  print(`Usage
12
21
  $ nanoid [options]
13
22
  Options
14
23
  -s, --size Generated ID size
15
24
  -a, --alphabet Alphabet to use
25
+ -v, --version Show version number
16
26
  -h, --help Show this help
17
27
  Examples
18
28
  $ nanoid -s 15
package/index.browser.js CHANGED
@@ -3,16 +3,34 @@ import { urlAlphabet as scopedUrlAlphabet } from './url-alphabet/index.js'
3
3
  export { urlAlphabet } from './url-alphabet/index.js'
4
4
  export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
5
5
  export let customRandom = (alphabet, defaultSize, getRandom) => {
6
- let mask = (2 << Math.log2(alphabet.length - 1)) - 1
7
- let step = -~((1.6 * mask * defaultSize) / alphabet.length)
6
+ let safeByteCutoff = 256 - (256 % alphabet.length)
7
+ if (safeByteCutoff === 256) {
8
+ let mask = alphabet.length - 1
9
+ return (size = defaultSize) => {
10
+ if (!size) return ''
11
+ let id = ''
12
+ while (true) {
13
+ let bytes = getRandom(size)
14
+ let j = size
15
+ while (j--) {
16
+ id += alphabet[bytes[j] & mask]
17
+ if (id.length >= size) return id
18
+ }
19
+ }
20
+ }
21
+ }
22
+ let step = Math.ceil((1.6 * 256 * defaultSize) / safeByteCutoff)
8
23
  return (size = defaultSize) => {
24
+ if (!size) return ''
9
25
  let id = ''
10
26
  while (true) {
11
27
  let bytes = getRandom(step)
12
- let j = step | 0
28
+ let j = step
13
29
  while (j--) {
14
- id += alphabet[bytes[j] & mask] || ''
15
- if (id.length >= size) return id
30
+ if (bytes[j] < safeByteCutoff) {
31
+ id += alphabet[bytes[j] % alphabet.length]
32
+ if (id.length >= size) return id
33
+ }
16
34
  }
17
35
  }
18
36
  }
package/index.d.ts CHANGED
@@ -39,7 +39,7 @@ export function nanoid<Type extends string>(size?: number): Type
39
39
  * @returns A random string generator.
40
40
  *
41
41
  * ```js
42
- * const { customAlphabet } = require('nanoid')
42
+ * import { customAlphabet } from 'nanoid'
43
43
  * const nanoid = customAlphabet('0123456789абвгдеё', 5)
44
44
  * nanoid() //=> "8ё56а"
45
45
  * ```
@@ -56,7 +56,7 @@ export function customAlphabet<Type extends string>(
56
56
  * will not be secure.
57
57
  *
58
58
  * ```js
59
- * import { customRandom } from 'nanoid/format'
59
+ * import { customRandom } from 'nanoid'
60
60
  *
61
61
  * const nanoid = customRandom('abcdef', 5, size => {
62
62
  * const random = []
@@ -79,7 +79,7 @@ export function customRandom<Type extends string>(
79
79
  alphabet: string,
80
80
  size: number,
81
81
  random: (bytes: number) => Uint8Array
82
- ): () => Type
82
+ ): (size?: number) => Type
83
83
 
84
84
  /**
85
85
  * URL safe symbols.
package/index.js CHANGED
@@ -19,8 +19,23 @@ export function random(bytes) {
19
19
  return pool.subarray(poolOffset - bytes, poolOffset)
20
20
  }
21
21
  export function customRandom(alphabet, defaultSize, getRandom) {
22
- let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
23
- let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
22
+ let safeByteCutoff = 256 - (256 % alphabet.length)
23
+ if (safeByteCutoff === 256) {
24
+ let mask = alphabet.length - 1
25
+ return (size = defaultSize) => {
26
+ if (!size) return ''
27
+ let id = ''
28
+ while (true) {
29
+ let bytes = getRandom(size)
30
+ let i = size
31
+ while (i--) {
32
+ id += alphabet[bytes[i] & mask]
33
+ if (id.length >= size) return id
34
+ }
35
+ }
36
+ }
37
+ }
38
+ let step = Math.ceil((1.6 * 256 * defaultSize) / safeByteCutoff)
24
39
  return (size = defaultSize) => {
25
40
  if (!size) return ''
26
41
  let id = ''
@@ -28,8 +43,10 @@ export function customRandom(alphabet, defaultSize, getRandom) {
28
43
  let bytes = getRandom(step)
29
44
  let i = step
30
45
  while (i--) {
31
- id += alphabet[bytes[i] & mask] || ''
32
- if (id.length >= size) return id
46
+ if (bytes[i] < safeByteCutoff) {
47
+ id += alphabet[bytes[i] % alphabet.length]
48
+ if (id.length >= size) return id
49
+ }
33
50
  }
34
51
  }
35
52
  }
package/nanoid.js CHANGED
@@ -1 +1 @@
1
- let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";export let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e));for(let n=0;n<e;n++)t+=a[63&r[n]];return t};
1
+ let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";export let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+=a[63&r[e]];return t};
package/package.json CHANGED
@@ -1,26 +1,32 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "5.1.6",
3
+ "version": "5.1.8",
4
4
  "description": "A tiny (118 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
- "uuid",
7
- "random",
8
6
  "id",
9
- "url"
7
+ "random",
8
+ "url",
9
+ "uuid"
10
10
  ],
11
- "type": "module",
12
- "engines": {
13
- "node": "^18 || >=20"
14
- },
11
+ "license": "MIT",
12
+ "author": "Andrey Sitnik <andrey@sitnik.es>",
13
+ "repository": "ai/nanoid",
15
14
  "funding": [
16
15
  {
17
16
  "type": "github",
18
17
  "url": "https://github.com/sponsors/ai"
19
18
  }
20
19
  ],
21
- "author": "Andrey Sitnik <andrey@sitnik.ru>",
22
- "license": "MIT",
23
- "repository": "ai/nanoid",
20
+ "bin": "./bin/nanoid.js",
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "browser": {
24
+ "./index.js": "./index.browser.js"
25
+ },
26
+ "types": "./index.d.ts",
27
+ "react-native": {
28
+ "./index.js": "./index.browser.js"
29
+ },
24
30
  "exports": {
25
31
  ".": {
26
32
  "types": "./index.d.ts",
@@ -28,16 +34,13 @@
28
34
  "react-native": "./index.browser.js",
29
35
  "default": "./index.js"
30
36
  },
31
- "./non-secure": "./non-secure/index.js",
37
+ "./non-secure": {
38
+ "types": "./non-secure/index.d.ts",
39
+ "default": "./non-secure/index.js"
40
+ },
32
41
  "./package.json": "./package.json"
33
42
  },
34
- "browser": {
35
- "./index.js": "./index.browser.js"
36
- },
37
- "react-native": {
38
- "./index.js": "./index.browser.js"
39
- },
40
- "bin": "./bin/nanoid.js",
41
- "sideEffects": false,
42
- "types": "./index.d.ts"
43
+ "engines": {
44
+ "node": "^18 || >=20"
45
+ }
43
46
  }
@@ -1,2 +1,2 @@
1
- export const urlAlphabet =
1
+ export let urlAlphabet =
2
2
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'