-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try other method to force exclusion of dynamic import as we have to s…
…upport node 18
- Loading branch information
1 parent
56ed7c2
commit 70d6ea0
Showing
1 changed file
with
10 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
export async function hash(inputStr: string) { | ||
if (!globalThis?.crypto) { | ||
let usedCrypto | ||
// @ts-ignore | ||
if (typeof crypto === "undefined" && typeof process !== "undefined" && process?.versions?.node) { | ||
const modName = "crypto" | ||
usedCrypto = await import(`node:${modName}`) | ||
} else if (typeof crypto !== "undefined") { | ||
usedCrypto = crypto | ||
} | ||
if (!usedCrypto) { | ||
throw new Error("Could not find crypto features in runtime") | ||
} | ||
|
||
const idDigest = await globalThis.crypto.subtle.digest( | ||
"SHA-256", | ||
new TextEncoder().encode(inputStr) | ||
) | ||
const idDigest = await usedCrypto.subtle.digest("SHA-256", new TextEncoder().encode(inputStr)) | ||
return [...new Uint8Array(idDigest)].map((b) => ("00" + b.toString(16)).slice(-2)).join("") | ||
} |