Skip to content

Commit 5ca9ac1

Browse files
committed
fix: do not use globalThis.require in ensureSubtleCryptoExists
1 parent 6ec5c89 commit 5ca9ac1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/utilities/src/hmac.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ let subtleCrypto = globalThis.crypto?.subtle;
4141

4242
async function ensureSubtleCryptoExists() {
4343
if (!subtleCrypto) {
44-
if (globalThis.require) {
45-
subtleCrypto = globalThis.require('node:crypto')?.webcrypto?.subtle;
46-
} else {
47-
subtleCrypto = (await import('node:crypto'))?.webcrypto?.subtle as SubtleCrypto;
44+
try {
45+
// eslint-disable-next-line @typescript-eslint/no-require-imports, global-require -- Backward compatibility for Node.js versions < 19
46+
subtleCrypto = require('node:crypto')?.webcrypto?.subtle;
47+
if (subtleCrypto) return;
48+
} catch {
49+
// Ignore require error
50+
}
51+
52+
try {
53+
subtleCrypto = (await import('node:crypto')).webcrypto.subtle as SubtleCrypto;
54+
} catch {
55+
// Ignore import error
4856
}
4957

5058
if (!subtleCrypto) {

0 commit comments

Comments
 (0)