Skip to content

Commit 7b8c769

Browse files
committed
Fix top level await issue
1 parent 4efb670 commit 7b8c769

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/token.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function encodeToBase64(data: unknown): string {
1515
// TODO: Improve error handling?
1616
const compatCrypto =
1717
typeof crypto === "undefined"
18-
? (await import("node:crypto")).webcrypto
19-
: crypto;
18+
? import("node:crypto").then((v) => v.webcrypto)
19+
: Promise.resolve(crypto);
2020

2121
const textEncoder = new TextEncoder();
2222

@@ -33,15 +33,17 @@ async function sign(
3333
encodedHeader: string,
3434
encodedPayload: string,
3535
): Promise<string> {
36-
const cryptoKey = await compatCrypto.subtle.importKey(
36+
const crypto = await compatCrypto;
37+
38+
const cryptoKey = await crypto.subtle.importKey(
3739
"raw",
3840
textEncoder.encode(apiKey),
3941
{ name: "HMAC", hash: "SHA-256" },
4042
false,
4143
["sign"],
4244
);
4345

44-
const signature = await compatCrypto.subtle.sign(
46+
const signature = await crypto.subtle.sign(
4547
"HMAC",
4648
cryptoKey,
4749
textEncoder.encode(`${encodedHeader}.${encodedPayload}`),

0 commit comments

Comments
 (0)