Skip to content

Commit

Permalink
fix: random uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 6, 2023
1 parent f2f7bee commit 47e9bd6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export const generateUUID = (): string | undefined => {
let _crypto: any
import('node:crypto')
.then((c) => (_crypto = c))
// eslint-disable-next-line unicorn/prefer-top-level-await
.catch(() => null)

export function generateUUID(): string | undefined {
try {
if ((globalThis as any).crypto?.randomUUID) {
return globalThis.crypto.randomUUID()
}
const crypto: Crypto | undefined = (globalThis as any).crypto || _crypto
if (!crypto) return undefined
return crypto.randomUUID()
} catch {}
return undefined
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2019",
"lib": ["ES2019", "DOM"],
"module": "ES2015",
"target": "ES2022",
"lib": ["ES2022", "DOM"],
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"types": ["node"],
Expand Down
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const common = defineConfig({
platform: 'neutral',
target: 'es2022',
splitting: false,
external: ['node:crypto'],
})

export default defineConfig([
Expand Down

0 comments on commit 47e9bd6

Please sign in to comment.