@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 32 lines 1.15 KB
Raw
1 import * as Effect from "effect/Effect";
2 import { stableValue } from "./stable.ts";
3
4 type Input = ArrayBuffer | Uint8Array | string;
5
6 export const sha256 = (input: Input) =>
7 Effect.promise(async () => {
8 const digest = await crypto.subtle.digest("SHA-256", toArrayBuffer(input));
9 const hashArray = Array.from(new Uint8Array(digest));
10 return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
11 });
12
13 export const sha256Object = (input: object) => sha256(JSON.stringify(stableValue(input)));
14
15 /**
16 * Stable sha256 hex digest of a resolved Task input. Unlike
17 * {@link sha256Object} this does not stabilize key ordering — it hashes the
18 * raw `JSON.stringify(input ?? null)` so identical inputs map to identical
19 * hashes for caching/equality checks.
20 */
21 export const hashInput = (input: unknown): Effect.Effect<string> =>
22 sha256(JSON.stringify(input ?? null));
23
24 const toArrayBuffer = (input: Input) => {
25 if (input instanceof ArrayBuffer) {
26 return input;
27 }
28 if (typeof input === "string") {
29 return new TextEncoder().encode(input);
30 }
31 return input.buffer.slice(input.byteOffset, input.byteOffset + input.byteLength) as ArrayBuffer;
32 };