patch-id.ts
This file
Path
packages/git/src/patch-id.ts
29 lines · 1009 bytes · TypeScript
Last changed by
Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
History of this fileTake it
123456789101112131415161718192021222324252627282930import { PatchId } from "@repo/protocol/ids";import { Array, Effect, Order } from "effect";
import type { Change } from "./tree-diff.ts";
const hex = (bytes: ArrayBuffer) => [...new Uint8Array(bytes)] .map((byte) => byte.toString(16).padStart(2, "0")) .join("");
/** * An answer's patch id: SHA-256 over its change set as sorted * `path\0before\0after\n` lines, an absent blob as the empty string. It * ignores order and moves exactly when a blob the answer touches moves. It * is the platform's own id, not `git patch-id`, so a Worker computes it with * WebCrypto and no diff. */export const patchId = (changes: readonly Change[]): Effect.Effect<PatchId> => { const lines = Array.sort( changes.map( ({ after, before, path }) => `${path}\0${before ?? ""}\0${after ?? ""}\n` ), Order.String );
return Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(lines.join(""))) ).pipe(Effect.map((digest) => PatchId.make(hex(digest))));};