-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify and decouple node proxy
- Loading branch information
Showing
2 changed files
with
16 additions
and
18 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
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,27 +1,12 @@ | ||
// https://nodejs.org/api/crypto.html | ||
// https://github.com/unjs/uncrypto | ||
import type nodeCrypto from "node:crypto"; | ||
|
||
export const CryptoKey = | ||
globalThis.CryptoKey as unknown as typeof nodeCrypto.webcrypto.CryptoKey; | ||
|
||
function ensureBound(thisArg: any, name: string | symbol) { | ||
const fn = thisArg[name]; | ||
return typeof fn === "function" ? fn.bind(thisArg) : fn; | ||
} | ||
|
||
export const webcrypto: Crypto & typeof nodeCrypto.webcrypto = new Proxy<Crypto & typeof nodeCrypto.webcrypto>({} as any, { | ||
get(target, name) { | ||
return CryptoKey && (CryptoKey as any)[name] ? ensureBound(CryptoKey, name) : ensureBound(globalThis.crypto, name); | ||
}, | ||
}); | ||
|
||
export const subtle: SubtleCrypto = webcrypto.subtle; | ||
export const subtle: SubtleCrypto = globalThis.crypto?.subtle; | ||
|
||
export const randomUUID: Crypto["randomUUID"] = () => { | ||
return webcrypto.randomUUID(); | ||
return globalThis.crypto?.randomUUID(); | ||
}; | ||
|
||
export const getRandomValues: Crypto["getRandomValues"] = (array: any) => { | ||
return webcrypto.getRandomValues(array); | ||
return globalThis.crypto?.getRandomValues(array); | ||
}; |