Skip to content
Myr

patch-id.ts

This file

Path

packages/git/src/patch-id.ts

27 lines · 861 bytes · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 958650a point at it.

Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a

lines 1–11why, one step back
1import { PatchId } from "@repo/protocol/ids";
2import { Array, Effect, Order } from "effect";34import type { Change } from "./tree-diff.ts";56const hex = (bytes: ArrayBuffer) =>7 [...new Uint8Array(bytes)]8 .map((byte) => byte.toString(16).padStart(2, "0"))9 .join("");1011/**

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 12–14why, one step back
12 * SHA-256 over sorted `path\0before\0after\n` lines, an absent blob as the
13 * empty string. Not `git patch-id`, so a Worker computes it with WebCrypto and14 * no diff.

Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a

lines 15–27why, one step back
15 */
16export const patchId = (changes: readonly Change[]): Effect.Effect<PatchId> => {17 const lines = Array.sort(18 changes.map(19 ({ after, before, path }) => `${path}\0${before ?? ""}\0${after ?? ""}\n`20 ),21 Order.String22 );2324 return Effect.promise(() =>25 crypto.subtle.digest("SHA-256", new TextEncoder().encode(lines.join("")))26 ).pipe(Effect.map((digest) => PatchId.make(hex(digest))));27};