Skip to content

Commit 801e413

Browse files
committed
fix: revert random uuid
1 parent 5ce92b9 commit 801e413

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/globals.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare global {
2+
const IS_NODE: boolean
3+
const randomUUID: () => string
4+
}
5+
6+
export {}

src/node-shim.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { randomUUID } from 'crypto'
12
import {
23
File,
34
FormData,
@@ -22,4 +23,8 @@ if (!globals.FormData) {
2223
globals.FormData = FormData
2324
}
2425

25-
export { fetch, Headers, Request, Response, FormData }
26+
// UUID
27+
if (!globals.crypto) globals.crypto = {}
28+
if (!globals.crypto.randomUUID) globals.crypto.randomUUID = randomUUID
29+
30+
export { fetch, Headers, Request, Response, FormData, randomUUID }

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export const generateUUID = (): string | undefined => {
22
try {
3-
return globalThis?.crypto?.randomUUID?.()
3+
if ((globalThis?.crypto as any)?.randomUUID) {
4+
return (globalThis.crypto as any).randomUUID()
5+
} else if (IS_NODE) {
6+
return randomUUID()
7+
}
48
// eslint-disable-next-line no-empty
59
} catch {}
610
return undefined

tsup.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const modern = (): Options => {
2222
target: 'es2019',
2323
format: ['esm'],
2424
splitting: false,
25+
define: {
26+
IS_NODE: 'false',
27+
},
2528
minifySyntax: true,
2629
esbuildOptions: (options) => {
2730
options.outExtension = {}
@@ -35,6 +38,9 @@ const node = (): Options => ({
3538
platform: 'node',
3639
format: ['esm'],
3740
clean: true,
41+
define: {
42+
IS_NODE: 'true',
43+
},
3844
minifySyntax: true,
3945
inject: [$r('src/node-shim.ts')],
4046
esbuildOptions: (options) => {

0 commit comments

Comments
 (0)