Skip to content

Commit 47e9bd6

Browse files
committed
fix: random uuid
1 parent f2f7bee commit 47e9bd6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/utils/uuid.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
export const generateUUID = (): string | undefined => {
1+
let _crypto: any
2+
import('node:crypto')
3+
.then((c) => (_crypto = c))
4+
// eslint-disable-next-line unicorn/prefer-top-level-await
5+
.catch(() => null)
6+
7+
export function generateUUID(): string | undefined {
28
try {
3-
if ((globalThis as any).crypto?.randomUUID) {
4-
return globalThis.crypto.randomUUID()
5-
}
9+
const crypto: Crypto | undefined = (globalThis as any).crypto || _crypto
10+
if (!crypto) return undefined
11+
return crypto.randomUUID()
612
} catch {}
713
return undefined
814
}

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2019",
4-
"lib": ["ES2019", "DOM"],
5-
"module": "ES2015",
3+
"target": "ES2022",
4+
"lib": ["ES2022", "DOM"],
5+
"module": "ESNext",
66
"moduleResolution": "Node",
77
"resolveJsonModule": true,
88
"types": ["node"],

tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const common = defineConfig({
88
platform: 'neutral',
99
target: 'es2022',
1010
splitting: false,
11+
external: ['node:crypto'],
1112
})
1213

1314
export default defineConfig([

0 commit comments

Comments
 (0)