25 lines
867 B
TypeScript
25 lines
867 B
TypeScript
/**
|
|
* Vitest setup polyfills for @suaveplan/bitpacker-crypto.
|
|
*
|
|
* The package's primitives consume `globalThis.crypto.getRandomValues` for
|
|
* AEAD nonces and `globalThis.crypto.subtle` for sha256 digests. Modern Node
|
|
* (≥ 19) exposes these on `globalThis.crypto`, but the vitest forks pool can
|
|
* sometimes start in a context where the global is not yet attached. Pull it
|
|
* explicitly from `node:crypto.webcrypto` if missing.
|
|
*/
|
|
import { webcrypto } from "node:crypto";
|
|
|
|
if (typeof globalThis.crypto?.getRandomValues !== "function") {
|
|
Object.defineProperty(globalThis, "crypto", {
|
|
value: webcrypto,
|
|
configurable: true,
|
|
writable: true,
|
|
});
|
|
}
|
|
|
|
if (typeof globalThis.crypto?.getRandomValues !== "function") {
|
|
throw new Error(
|
|
"[bitpacker-crypto polyfills] failed to install globalThis.crypto.getRandomValues"
|
|
);
|
|
}
|