@tachybase/plugin-auth-oidc 0.23.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/.turbo/turbo-build.log +12 -0
- package/README.md +11 -0
- package/README.zh-CN.md +38 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/OIDCButton.d.ts +9 -0
- package/dist/client/Options.d.ts +2 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +3 -0
- package/dist/client/locale/index.d.ts +3 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +34 -0
- package/dist/externalVersion.js +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +40 -0
- package/dist/locale/es-ES.json +25 -0
- package/dist/locale/fr-FR.json +21 -0
- package/dist/locale/ko_KR.json +28 -0
- package/dist/locale/pt-BR.json +21 -0
- package/dist/locale/zh-CN.json +28 -0
- package/dist/node_modules/nanoid/.devcontainer.json +23 -0
- package/dist/node_modules/nanoid/LICENSE +20 -0
- package/dist/node_modules/nanoid/async/index.browser.cjs +69 -0
- package/dist/node_modules/nanoid/async/index.browser.js +69 -0
- package/dist/node_modules/nanoid/async/index.cjs +71 -0
- package/dist/node_modules/nanoid/async/index.d.ts +56 -0
- package/dist/node_modules/nanoid/async/index.js +71 -0
- package/dist/node_modules/nanoid/async/index.native.js +57 -0
- package/dist/node_modules/nanoid/async/package.json +12 -0
- package/dist/node_modules/nanoid/bin/nanoid.cjs +55 -0
- package/dist/node_modules/nanoid/index.browser.cjs +72 -0
- package/dist/node_modules/nanoid/index.browser.js +72 -0
- package/dist/node_modules/nanoid/index.cjs +1 -0
- package/dist/node_modules/nanoid/index.d.cts +91 -0
- package/dist/node_modules/nanoid/index.d.ts +91 -0
- package/dist/node_modules/nanoid/index.js +85 -0
- package/dist/node_modules/nanoid/nanoid.js +1 -0
- package/dist/node_modules/nanoid/non-secure/index.cjs +34 -0
- package/dist/node_modules/nanoid/non-secure/index.d.ts +33 -0
- package/dist/node_modules/nanoid/non-secure/index.js +34 -0
- package/dist/node_modules/nanoid/non-secure/package.json +6 -0
- package/dist/node_modules/nanoid/package.json +1 -0
- package/dist/node_modules/nanoid/url-alphabet/index.cjs +7 -0
- package/dist/node_modules/nanoid/url-alphabet/index.js +7 -0
- package/dist/node_modules/nanoid/url-alphabet/package.json +6 -0
- package/dist/node_modules/openid-client/lib/client.js +1884 -0
- package/dist/node_modules/openid-client/lib/device_flow_handle.js +125 -0
- package/dist/node_modules/openid-client/lib/errors.js +55 -0
- package/dist/node_modules/openid-client/lib/helpers/assert.js +24 -0
- package/dist/node_modules/openid-client/lib/helpers/base64url.js +13 -0
- package/dist/node_modules/openid-client/lib/helpers/client.js +208 -0
- package/dist/node_modules/openid-client/lib/helpers/consts.js +7 -0
- package/dist/node_modules/openid-client/lib/helpers/decode_jwt.js +27 -0
- package/dist/node_modules/openid-client/lib/helpers/deep_clone.js +1 -0
- package/dist/node_modules/openid-client/lib/helpers/defaults.js +27 -0
- package/dist/node_modules/openid-client/lib/helpers/generators.js +14 -0
- package/dist/node_modules/openid-client/lib/helpers/is_key_object.js +4 -0
- package/dist/node_modules/openid-client/lib/helpers/is_plain_object.js +1 -0
- package/dist/node_modules/openid-client/lib/helpers/issuer.js +111 -0
- package/dist/node_modules/openid-client/lib/helpers/keystore.js +298 -0
- package/dist/node_modules/openid-client/lib/helpers/merge.js +24 -0
- package/dist/node_modules/openid-client/lib/helpers/pick.js +9 -0
- package/dist/node_modules/openid-client/lib/helpers/process_response.js +71 -0
- package/dist/node_modules/openid-client/lib/helpers/request.js +200 -0
- package/dist/node_modules/openid-client/lib/helpers/unix_timestamp.js +1 -0
- package/dist/node_modules/openid-client/lib/helpers/weak_cache.js +1 -0
- package/dist/node_modules/openid-client/lib/helpers/webfinger_normalize.js +71 -0
- package/dist/node_modules/openid-client/lib/helpers/www_authenticate_parser.js +14 -0
- package/dist/node_modules/openid-client/lib/index.js +1 -0
- package/dist/node_modules/openid-client/lib/issuer.js +192 -0
- package/dist/node_modules/openid-client/lib/issuer_registry.js +3 -0
- package/dist/node_modules/openid-client/lib/passport_strategy.js +205 -0
- package/dist/node_modules/openid-client/lib/token_set.js +35 -0
- package/dist/node_modules/openid-client/package.json +1 -0
- package/dist/node_modules/openid-client/types/index.d.ts +623 -0
- package/dist/server/actions/getAuthUrl.d.ts +2 -0
- package/dist/server/actions/getAuthUrl.js +47 -0
- package/dist/server/actions/redirect.d.ts +2 -0
- package/dist/server/actions/redirect.js +55 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +33 -0
- package/dist/server/migrations/20231007124508-update-autosignup.d.ts +6 -0
- package/dist/server/migrations/20231007124508-update-autosignup.js +52 -0
- package/dist/server/oidc-auth.d.ts +15 -0
- package/dist/server/oidc-auth.js +154 -0
- package/dist/server/plugin.d.ts +11 -0
- package/dist/server/plugin.js +83 -0
- package/dist/swagger/index.d.ts +143 -0
- package/dist/swagger/index.js +178 -0
- package/package.json +37 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
let crypto = require('crypto')
|
|
2
|
+
|
|
3
|
+
let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
|
4
|
+
|
|
5
|
+
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
|
6
|
+
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
|
7
|
+
let random = bytes =>
|
|
8
|
+
new Promise((resolve, reject) => {
|
|
9
|
+
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
|
10
|
+
// Memory flushing is unnecessary since the buffer allocation itself resets
|
|
11
|
+
// the memory with the new bytes.
|
|
12
|
+
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
13
|
+
if (err) {
|
|
14
|
+
reject(err)
|
|
15
|
+
} else {
|
|
16
|
+
resolve(buf)
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
22
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
23
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
24
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
25
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
26
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
27
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
28
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
29
|
+
// the random bytes redundancy has to be satisfied.
|
|
30
|
+
|
|
31
|
+
// Note: every hardware random generator call is performance expensive,
|
|
32
|
+
// because the system call for entropy collection takes a lot of time.
|
|
33
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
34
|
+
|
|
35
|
+
// Next, a step determines how many random bytes to generate.
|
|
36
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
37
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
38
|
+
// according to benchmarks).
|
|
39
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
40
|
+
|
|
41
|
+
let tick = (id, size = defaultSize) =>
|
|
42
|
+
random(step).then(bytes => {
|
|
43
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
44
|
+
let i = step
|
|
45
|
+
while (i--) {
|
|
46
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
47
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
48
|
+
if (id.length >= size) return id
|
|
49
|
+
}
|
|
50
|
+
return tick(id, size)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
return size => tick('', size)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let nanoid = (size = 21) =>
|
|
57
|
+
random((size |= 0)).then(bytes => {
|
|
58
|
+
let id = ''
|
|
59
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
60
|
+
while (size--) {
|
|
61
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
62
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
63
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
64
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
65
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
66
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
67
|
+
}
|
|
68
|
+
return id
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,56 @@
|
|
|
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>
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
2
|
+
|
|
3
|
+
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
4
|
+
|
|
5
|
+
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
|
6
|
+
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
|
7
|
+
let random = bytes =>
|
|
8
|
+
new Promise((resolve, reject) => {
|
|
9
|
+
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
|
10
|
+
// Memory flushing is unnecessary since the buffer allocation itself resets
|
|
11
|
+
// the memory with the new bytes.
|
|
12
|
+
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
13
|
+
if (err) {
|
|
14
|
+
reject(err)
|
|
15
|
+
} else {
|
|
16
|
+
resolve(buf)
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
22
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
23
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
24
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
25
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
26
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
27
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
28
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
29
|
+
// the random bytes redundancy has to be satisfied.
|
|
30
|
+
|
|
31
|
+
// Note: every hardware random generator call is performance expensive,
|
|
32
|
+
// because the system call for entropy collection takes a lot of time.
|
|
33
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
34
|
+
|
|
35
|
+
// Next, a step determines how many random bytes to generate.
|
|
36
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
37
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
38
|
+
// according to benchmarks).
|
|
39
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
40
|
+
|
|
41
|
+
let tick = (id, size = defaultSize) =>
|
|
42
|
+
random(step).then(bytes => {
|
|
43
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
44
|
+
let i = step
|
|
45
|
+
while (i--) {
|
|
46
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
47
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
48
|
+
if (id.length >= size) return id
|
|
49
|
+
}
|
|
50
|
+
return tick(id, size)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
return size => tick('', size)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let nanoid = (size = 21) =>
|
|
57
|
+
random((size |= 0)).then(bytes => {
|
|
58
|
+
let id = ''
|
|
59
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
60
|
+
while (size--) {
|
|
61
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
62
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
63
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
64
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
65
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
66
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
67
|
+
}
|
|
68
|
+
return id
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
export { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { getRandomBytesAsync } from 'expo-random'
|
|
2
|
+
|
|
3
|
+
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
4
|
+
|
|
5
|
+
let random = getRandomBytesAsync
|
|
6
|
+
|
|
7
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
8
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
9
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
10
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
11
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
12
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
13
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
14
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
15
|
+
// the random bytes redundancy has to be satisfied.
|
|
16
|
+
|
|
17
|
+
// Note: every hardware random generator call is performance expensive,
|
|
18
|
+
// because the system call for entropy collection takes a lot of time.
|
|
19
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
20
|
+
|
|
21
|
+
// Next, a step determines how many random bytes to generate.
|
|
22
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
23
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
24
|
+
// according to benchmarks).
|
|
25
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
26
|
+
|
|
27
|
+
let tick = (id, size = defaultSize) =>
|
|
28
|
+
random(step).then(bytes => {
|
|
29
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
30
|
+
let i = step
|
|
31
|
+
while (i--) {
|
|
32
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
33
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
34
|
+
if (id.length >= size) return id
|
|
35
|
+
}
|
|
36
|
+
return tick(id, size)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return size => tick('', size)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let nanoid = (size = 21) =>
|
|
43
|
+
random((size |= 0)).then(bytes => {
|
|
44
|
+
let id = ''
|
|
45
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
46
|
+
while (size--) {
|
|
47
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
48
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
49
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
50
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
51
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
52
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
53
|
+
}
|
|
54
|
+
return id
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
export { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
let { nanoid, customAlphabet } = require('..')
|
|
4
|
+
|
|
5
|
+
function print(msg) {
|
|
6
|
+
process.stdout.write(msg + '\n')
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function error(msg) {
|
|
10
|
+
process.stderr.write(msg + '\n')
|
|
11
|
+
process.exit(1)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
15
|
+
print(`
|
|
16
|
+
Usage
|
|
17
|
+
$ nanoid [options]
|
|
18
|
+
|
|
19
|
+
Options
|
|
20
|
+
-s, --size Generated ID size
|
|
21
|
+
-a, --alphabet Alphabet to use
|
|
22
|
+
-h, --help Show this help
|
|
23
|
+
|
|
24
|
+
Examples
|
|
25
|
+
$ nanoid --s 15
|
|
26
|
+
S9sBF77U6sDB8Yg
|
|
27
|
+
|
|
28
|
+
$ nanoid --size 10 --alphabet abc
|
|
29
|
+
bcabababca`)
|
|
30
|
+
process.exit()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let alphabet, size
|
|
34
|
+
for (let i = 2; i < process.argv.length; i++) {
|
|
35
|
+
let arg = process.argv[i]
|
|
36
|
+
if (arg === '--size' || arg === '-s') {
|
|
37
|
+
size = Number(process.argv[i + 1])
|
|
38
|
+
i += 1
|
|
39
|
+
if (Number.isNaN(size) || size <= 0) {
|
|
40
|
+
error('Size must be positive integer')
|
|
41
|
+
}
|
|
42
|
+
} else if (arg === '--alphabet' || arg === '-a') {
|
|
43
|
+
alphabet = process.argv[i + 1]
|
|
44
|
+
i += 1
|
|
45
|
+
} else {
|
|
46
|
+
error('Unknown argument ' + arg)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (alphabet) {
|
|
51
|
+
let customNanoid = customAlphabet(alphabet, size)
|
|
52
|
+
print(customNanoid())
|
|
53
|
+
} else {
|
|
54
|
+
print(nanoid(size))
|
|
55
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
|
2
|
+
// according to `browser` config in `package.json`.
|
|
3
|
+
|
|
4
|
+
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
|
5
|
+
|
|
6
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
7
|
+
|
|
8
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
9
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
10
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
11
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
12
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
13
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
|
14
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
15
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
16
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
17
|
+
// the random bytes redundancy has to be satisfied.
|
|
18
|
+
|
|
19
|
+
// Note: every hardware random generator call is performance expensive,
|
|
20
|
+
// because the system call for entropy collection takes a lot of time.
|
|
21
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
22
|
+
|
|
23
|
+
// Next, a step determines how many random bytes to generate.
|
|
24
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
25
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
26
|
+
// according to benchmarks).
|
|
27
|
+
|
|
28
|
+
// `-~f => Math.ceil(f)` if f is a float
|
|
29
|
+
// `-~i => i + 1` if i is an integer
|
|
30
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
31
|
+
|
|
32
|
+
return (size = defaultSize) => {
|
|
33
|
+
let id = ''
|
|
34
|
+
while (true) {
|
|
35
|
+
let bytes = getRandom(step)
|
|
36
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
37
|
+
let j = step | 0
|
|
38
|
+
while (j--) {
|
|
39
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
40
|
+
id += alphabet[bytes[j] & mask] || ''
|
|
41
|
+
if (id.length === size) return id
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let customAlphabet = (alphabet, size = 21) =>
|
|
48
|
+
customRandom(alphabet, size, random)
|
|
49
|
+
|
|
50
|
+
let nanoid = (size = 21) =>
|
|
51
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
52
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
53
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
54
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
55
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
56
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
57
|
+
byte &= 63
|
|
58
|
+
if (byte < 36) {
|
|
59
|
+
// `0-9a-z`
|
|
60
|
+
id += byte.toString(36)
|
|
61
|
+
} else if (byte < 62) {
|
|
62
|
+
// `A-Z`
|
|
63
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
64
|
+
} else if (byte > 62) {
|
|
65
|
+
id += '-'
|
|
66
|
+
} else {
|
|
67
|
+
id += '_'
|
|
68
|
+
}
|
|
69
|
+
return id
|
|
70
|
+
}, '')
|
|
71
|
+
|
|
72
|
+
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
|
2
|
+
// according to `browser` config in `package.json`.
|
|
3
|
+
|
|
4
|
+
import { urlAlphabet } from './url-alphabet/index.js'
|
|
5
|
+
|
|
6
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
7
|
+
|
|
8
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
9
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
10
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
11
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
12
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
13
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
|
14
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
15
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
16
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
17
|
+
// the random bytes redundancy has to be satisfied.
|
|
18
|
+
|
|
19
|
+
// Note: every hardware random generator call is performance expensive,
|
|
20
|
+
// because the system call for entropy collection takes a lot of time.
|
|
21
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
22
|
+
|
|
23
|
+
// Next, a step determines how many random bytes to generate.
|
|
24
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
25
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
26
|
+
// according to benchmarks).
|
|
27
|
+
|
|
28
|
+
// `-~f => Math.ceil(f)` if f is a float
|
|
29
|
+
// `-~i => i + 1` if i is an integer
|
|
30
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
31
|
+
|
|
32
|
+
return (size = defaultSize) => {
|
|
33
|
+
let id = ''
|
|
34
|
+
while (true) {
|
|
35
|
+
let bytes = getRandom(step)
|
|
36
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
37
|
+
let j = step | 0
|
|
38
|
+
while (j--) {
|
|
39
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
40
|
+
id += alphabet[bytes[j] & mask] || ''
|
|
41
|
+
if (id.length === size) return id
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let customAlphabet = (alphabet, size = 21) =>
|
|
48
|
+
customRandom(alphabet, size, random)
|
|
49
|
+
|
|
50
|
+
let nanoid = (size = 21) =>
|
|
51
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
52
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
53
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
54
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
55
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
56
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
57
|
+
byte &= 63
|
|
58
|
+
if (byte < 36) {
|
|
59
|
+
// `0-9a-z`
|
|
60
|
+
id += byte.toString(36)
|
|
61
|
+
} else if (byte < 62) {
|
|
62
|
+
// `A-Z`
|
|
63
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
64
|
+
} else if (byte > 62) {
|
|
65
|
+
id += '-'
|
|
66
|
+
} else {
|
|
67
|
+
id += '_'
|
|
68
|
+
}
|
|
69
|
+
return id
|
|
70
|
+
}, '')
|
|
71
|
+
|
|
72
|
+
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(){var e={982:function(e){"use strict";e.exports=require("crypto")},573:function(e,t,r){let l=r(982);let{urlAlphabet:n}=r(343);const a=128;let u,i;let fillPool=e=>{if(!u||u.length<e){u=Buffer.allocUnsafe(e*a);l.randomFillSync(u);i=0}else if(i+e>u.length){l.randomFillSync(u);i=0}i+=e};let random=e=>{fillPool(e|=0);return u.subarray(i-e,i)};let customRandom=(e,t,r)=>{let l=(2<<31-Math.clz32(e.length-1|1))-1;let n=Math.ceil(1.6*l*t/e.length);return(a=t)=>{let u="";while(true){let t=r(n);let i=n;while(i--){u+=e[t[i]&l]||"";if(u.length===a)return u}}}};let customAlphabet=(e,t=21)=>customRandom(e,t,random);let nanoid=(e=21)=>{fillPool(e|=0);let t="";for(let r=i-e;r<i;r++){t+=n[u[r]&63]}return t};e.exports={nanoid:nanoid,customAlphabet:customAlphabet,customRandom:customRandom,urlAlphabet:n,random:random}},343:function(e){let t="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";e.exports={urlAlphabet:t}}};var t={};function __nccwpck_require__(r){var l=t[r];if(l!==undefined){return l.exports}var n=t[r]={exports:{}};var a=true;try{e[r](n,n.exports,__nccwpck_require__);a=false}finally{if(a)delete t[r]}return n.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r=__nccwpck_require__(573);module.exports=r})();
|
|
@@ -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
|
|
@@ -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
|