Files
suaveplan/packages/foundation/bitpacker/bitpacker-crypto/tests/setup/polyfills.ts
2026-05-27 23:47:53 +01:00

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"
);
}