Skip to content

Commit e8e535d

Browse files
committed
Attempt fix for missing global crypto in Node.js 18
1 parent 0b5bdf3 commit e8e535d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/token.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ async function sign(
1919
encodedHeader: string,
2020
encodedPayload: string
2121
): Promise<string> {
22+
// missing crypto global for Node.js 18
23+
const localCrypto = crypto ?? (await import('node:crypto')).webcrypto
24+
2225
const textEncoder = new TextEncoder()
2326

24-
const cryptoKey = await crypto.subtle.importKey(
27+
const cryptoKey = await localCrypto.subtle.importKey(
2528
'raw',
2629
textEncoder.encode(apiKey),
2730
{ name: 'HMAC', hash: 'SHA-256' },
2831
false,
2932
['sign']
3033
)
3134

32-
const signature = await crypto.subtle.sign(
35+
const signature = await localCrypto.subtle.sign(
3336
'HMAC',
3437
cryptoKey,
3538
textEncoder.encode(`${encodedHeader}.${encodedPayload}`)

0 commit comments

Comments
 (0)