All files / src crypto.ts

100% Statements 13/13
83.33% Branches 5/6
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 264x     4x     4x 4x   4x 4x   4x       22x     22x 22x 704x        
import * as nodeCrypto from "node:crypto";
// Node 20+ globally available Crypto Module -OR- Browser Crypto Module
const globalCrypto =
  <Crypto>(<any>globalThis.crypto)?.webcrypto || globalThis.crypto;
// Globally available crypto (Node or Browser) -OR- Node Web Crypto (We can get rid of this in April 2025 after Node 18 is EOL)
// biome-ignore lint/style/noRestrictedImports: This is where we re-export our version of the module
const crypto = globalCrypto || nodeCrypto.webcrypto;
const { subtle, getRandomValues, randomUUID } = crypto;
 
export { subtle, getRandomValues, randomUUID };
export default crypto;
 
export async function computeContentHash(
  data: string | Uint8Array,
): Promise<string> {
  const bytes: Uint8Array<ArrayBuffer> =
    typeof data === "string"
      ? new TextEncoder().encode(data)
      : new Uint8Array(data);
  const hashBuffer = await subtle.digest("SHA-256", bytes);
  return Array.from(new Uint8Array(hashBuffer))
    .map((b) => b.toString(16).padStart(2, "0"))
    .join("")
    .slice(0, 8);
}