Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/blobs.ts?format=json
65 linesFirst import 94% · review 6%Last change review in version 28, 3 versions changed it
1// Binary files (#27): content-addressed blobs. The server keeps a binary
2// file's bytes by their SHA-256 (PUT and GET /p/:project/blobs/<sha256>), and
3// the file in the graph points at that hash. The client keeps the hash it
4// knows for each binary file in .avcs/blobs.json, as it keeps the known copy
5// of a text file, and uploads only a hash the server hasn't been told about.
6
7import { createHash } from "node:crypto";
8import { readFileSync, statSync } from "node:fs";
9import { join } from "node:path";
10import { writeFileAtomic } from "./folder.ts";
11import { stateDir } from "./state-dir.ts";
13/** The server's limit for one file's bytes. */
14export const MAX_BLOB_BYTES = 20 * 1024 * 1024;
16const BLOBS_FILE = "blobs.json";
18export function sha256(bytes: Uint8Array): string {
19 return createHash("sha256").update(bytes).digest("hex");
22/**
23 * Whether normalized text is the file the server keeps whole under `sha`
24 * (#46): a text file matching abso.json's "whole", or over 1 MB, is stored
25 * like a binary file, by the hash of its text with LF endings. The client
26 * keeps that hash in blobs.json, and the file on disk stays text.
27 */
28export function keptWholeAs(text: string, sha: string | undefined): boolean {
29 return sha !== undefined && sha256(Buffer.from(text, "utf8")) === sha;
32/** A file's bytes and their hash, or undefined when it is gone; `tooBig` when over the server's limit (bytes not read). */
33export function blobOf(
34 path: string,
35): { sha: string; bytes: Uint8Array; size: number } | { tooBig: true; size: number } | undefined {
36 try {
37 const size = statSync(path).size;
38 if (size > MAX_BLOB_BYTES) return { tooBig: true, size };
39 const bytes = readFileSync(path);
40 return { sha: sha256(bytes), bytes, size };
41 } catch {
42 return undefined;
43 }
46/** The binary files the server has, as far as the client knows: path -> sha256. */
47export function readKnownBlobs(root: string): Map<string, string> {
48 try {
49 return new Map(
50 Object.entries(JSON.parse(readFileSync(join(stateDir(root), BLOBS_FILE), "utf8")) as Record<string, string>),
51 );
52 } catch {
53 return new Map();
54 }
57export function writeKnownBlobs(root: string, blobs: ReadonlyMap<string, string>): void {
58 const sorted = Object.fromEntries([...blobs].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
59 writeFileAtomic(join(stateDir(root), BLOBS_FILE), `${JSON.stringify(sorted, null, 1)}\n`);
62/** Short form of a hash for people: the first 12 hex digits. */
63export function shortSha(sha: string | null | undefined): string {
64 return sha ? sha.slice(0, 12) : "(none)";