Skip to content

Commit a00ef9f

Browse files
committed
Support node 18's non-global export of the webcrypto api
1 parent 8c01996 commit a00ef9f

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

spec/jest.setup.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
import crypto from "crypto";
2-
3-
Object.defineProperty(globalThis, "crypto", {
4-
value: crypto.webcrypto,
5-
});

src/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const crypto = typeof window === "undefined" ? require("crypto") : window.crypto;
2+
13
export async function sha1(source: string) {
24
const sourceBytes = new TextEncoder().encode(source);
3-
const digest: ArrayBuffer = await (globalThis as any).crypto.subtle.digest("SHA-1", sourceBytes);
5+
const digest: ArrayBuffer = await crypto.subtle.digest("SHA-1", sourceBytes);
46
const resultBytes = [...new Uint8Array(digest)];
57
return resultBytes.map((x) => x.toString(16).padStart(2, "0")).join("");
68
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"lib": ["es2020"],
3+
"lib": ["es2020", "DOM"],
44
"baseUrl": "./",
55
"moduleResolution": "node",
66
"module": "commonjs",

0 commit comments

Comments
 (0)