@naturalcycles/nodejs-lib 15.6.0 → 15.7.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.
package/dist/security/id.util.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
2
|
import { ALPHABET_ALPHANUMERIC, ALPHABET_ALPHANUMERIC_LOWERCASE, ALPHABET_NONAMBIGUOUS, nanoIdCustomAlphabet, } from './nanoid.js';
|
|
3
|
+
let mockedStringId;
|
|
4
|
+
export function mockStringId(id) {
|
|
5
|
+
mockedStringId = id;
|
|
6
|
+
}
|
|
7
|
+
export function unmockStringId() {
|
|
8
|
+
mockedStringId = undefined;
|
|
9
|
+
}
|
|
3
10
|
/**
|
|
4
11
|
* Generate cryptographically-secure string id.
|
|
5
12
|
* Powered by `nanoid`.
|
|
6
13
|
*/
|
|
7
14
|
export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE) {
|
|
8
|
-
return nanoIdCustomAlphabet(alphabet, length)();
|
|
15
|
+
return mockedStringId ?? nanoIdCustomAlphabet(alphabet, length)();
|
|
9
16
|
}
|
|
10
17
|
/**
|
|
11
18
|
* Generate a string id of Base62 alphabet (same as "alphanumeric": A-Za-z0-9)
|
package/package.json
CHANGED
package/src/security/id.util.ts
CHANGED
|
@@ -6,12 +6,21 @@ import {
|
|
|
6
6
|
nanoIdCustomAlphabet,
|
|
7
7
|
} from './nanoid.js'
|
|
8
8
|
|
|
9
|
+
let mockedStringId: string | undefined
|
|
10
|
+
|
|
11
|
+
export function mockStringId(id: string): void {
|
|
12
|
+
mockedStringId = id
|
|
13
|
+
}
|
|
14
|
+
export function unmockStringId(): void {
|
|
15
|
+
mockedStringId = undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
9
18
|
/**
|
|
10
19
|
* Generate cryptographically-secure string id.
|
|
11
20
|
* Powered by `nanoid`.
|
|
12
21
|
*/
|
|
13
22
|
export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
|
|
14
|
-
return nanoIdCustomAlphabet(alphabet, length)()
|
|
23
|
+
return mockedStringId ?? nanoIdCustomAlphabet(alphabet, length)()
|
|
15
24
|
}
|
|
16
25
|
|
|
17
26
|
/**
|