Versions 42 to 318
new file, 46 lines
1+import { createHash } from "node:crypto"; 2+import { jsonValueChunks } from "../../src/engine/json-value-chunks.ts"; 4+/** Preserve canonicalJson identity bytes without retaining the complete encoded request. */ 5+export function requestIdentityDigest(value: unknown): string { 6+ const hash = createHash("sha256"); 7+ const active = new Set<object>(); 8+ function* chunks(input: unknown, absent?: string): Generator<string> { 9+ if (typeof input === "string") { 10+ yield* jsonValueChunks(input); 11+ } else if (input && typeof input === "object") { 12+ if (active.has(input)) throw new TypeError("cyclic request identity"); 15+ if (Array.isArray(input)) { 17+ const length = input.length; 18+ for (let i = 0; i < length; i++) { 20+ if (i in input) yield* chunks(input[i], ""); 26+ for (const key of Object.keys(input).sort()) { 27+ if (!first) yield ","; 29+ yield* jsonValueChunks(key); 31+ yield* chunks((input as Record<string, unknown>)[key], "undefined"); 36+ active.delete(input); 39+ const encoded = JSON.stringify(input); 40+ if (encoded === undefined && absent === undefined) throw new TypeError("unsupported request identity"); 41+ yield encoded ?? absent!; 44+ for (const chunk of chunks(value)) hash.update(chunk); 45+ return hash.digest("hex");