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/tar.ts?format=json
139 linesFirst import 100%Last change First import in version 0, 1 version changed it
1// A small tar writer (POSIX ustar, with pax headers for long paths), so
2// `abso run` can send the folder without a dependency. Gzipped with node:zlib.
3
4import { gunzipSync, gzipSync } from "node:zlib";
5
6const BLOCK = 512;
7const ENCODER = new TextEncoder();
8
9export interface TarEntry {
10 readonly path: string;
11 readonly data: Uint8Array;
12 /** Unix permission bits; 0o644 unless given. */
13 readonly mode?: number;
14 /** A symbolic link to this target (data is then empty). */
15 readonly link?: string;
18function octal(value: number, width: number): string {
19 // width includes the terminating NUL.
20 return `${value.toString(8).padStart(width - 1, "0")}\0`;
23function header(name: string, size: number, mode: number, type: string, prefix = "", linkname = ""): Uint8Array {
24 const h = new Uint8Array(BLOCK);
25 const put = (offset: number, text: string): void => {
26 h.set(ENCODER.encode(text), offset);
27 };
28 put(0, name);
29 put(100, octal(mode, 8));
30 put(108, octal(0, 8));
31 put(116, octal(0, 8));
32 put(124, octal(size, 12));
33 put(136, octal(0, 12));
34 put(148, " ");
35 put(156, type);
36 put(157, linkname);
37 put(257, "ustar\0");
38 put(263, "00");
39 put(345, prefix);
40 let sum = 0;
41 for (const b of h) sum += b;
42 put(148, `${sum.toString(8).padStart(6, "0")}\0 `);
43 return h;
46/** Split a path into ustar's prefix (155 bytes) and name (100 bytes), or undefined when it doesn't fit. */
47function splitPath(path: string): { name: string; prefix: string } | undefined {
48 if (ENCODER.encode(path).length <= 100) return { name: path, prefix: "" };
49 for (let i = path.indexOf("/"); i >= 0; i = path.indexOf("/", i + 1)) {
50 const prefix = path.slice(0, i);
51 const name = path.slice(i + 1);
52 if (ENCODER.encode(prefix).length <= 155 && ENCODER.encode(name).length <= 100) return { name, prefix };
53 }
54 return undefined;
57function padded(data: Uint8Array): Uint8Array[] {
58 const rest = data.length % BLOCK;
59 return rest === 0 ? [data] : [data, new Uint8Array(BLOCK - rest)];
62/** A pax record "<len> path=<path>\n", where len counts the whole record. */
63function paxRecord(key: string, value: string): string {
64 const body = ` ${key}=${value}\n`;
65 let len = ENCODER.encode(body).length + 1;
66 while (String(len).length + ENCODER.encode(body).length !== len) len++;
67 return `${len}${body}`;
70export function tar(entries: readonly TarEntry[]): Uint8Array {
71 const parts: Uint8Array[] = [];
72 for (const entry of entries) {
73 const link = entry.link;
74 const mode = entry.mode ?? (link === undefined ? 0o644 : 0o777);
75 const type = link === undefined ? "0" : "2";
76 const data = link === undefined ? entry.data : new Uint8Array(0);
77 const split = splitPath(entry.path);
78 const longLink = link !== undefined && ENCODER.encode(link).length > 100;
79 if (split === undefined || longLink) {
80 const pax = ENCODER.encode(
81 (split === undefined ? paxRecord("path", entry.path) : "") + (longLink ? paxRecord("linkpath", link) : ""),
82 );
83 parts.push(header("PaxHeader", pax.length, 0o644, "x"), ...padded(pax));
84 }
85 const name = split ? split.name : entry.path.slice(-100).replace(/^[^/]*\//, "");
86 parts.push(header(name, data.length, mode, type, split?.prefix ?? "", link !== undefined && !longLink ? link : ""));
87 parts.push(...padded(data));
88 }
89 parts.push(new Uint8Array(BLOCK * 2));
90 let total = 0;
91 for (const p of parts) total += p.length;
92 const out = new Uint8Array(total);
93 let at = 0;
94 for (const p of parts) {
95 out.set(p, at);
96 at += p.length;
97 }
98 return out;
101export function tarGz(entries: readonly TarEntry[]): Uint8Array<ArrayBuffer> {
102 return new Uint8Array(gzipSync(tar(entries)));
105/**
106 * The files and links in a tar.gz (abso fetch reads the box's files back).
107 * Folders and other kinds are passed over; pax headers give long paths and
108 * link targets.
109 */
110export function untarGz(gz: Uint8Array): TarEntry[] {
111 const data = gunzipSync(gz);
112 const text = (from: number, len: number): string =>
113 new TextDecoder().decode(data.subarray(from, from + len)).replace(/\0[\s\S]*$/, "");
114 const out: TarEntry[] = [];
115 let pax: Record<string, string> = {};
116 for (let at = 0; at + BLOCK <= data.length; ) {
117 if (data.subarray(at, at + BLOCK).every((b) => b === 0)) break;
118 const name = text(at, 100);
119 const mode = parseInt(text(at + 100, 8).trim() || "644", 8);
120 const size = parseInt(text(at + 124, 12).trim() || "0", 8);
121 const type = text(at + 156, 1);
122 const linkname = text(at + 157, 100);
123 const prefix = text(at + 345, 155);
124 const body = data.subarray(at + BLOCK, at + BLOCK + size);
125 at += BLOCK + Math.ceil(size / BLOCK) * BLOCK;
126 if (type === "x") {
127 // Records of "<len> key=value\n".
128 pax = {};
129 for (const m of new TextDecoder().decode(body).matchAll(/\d+ ([^=\n]+)=([^\n]*)\n/g)) pax[m[1]!] = m[2]!;
130 continue;
131 }
132 if (type === "g") continue;
133 const path = (pax.path ?? (prefix ? `${prefix}/${name}` : name)).replace(/^\.\//, "");
134 if (type === "0" || type === "" || type === "7") out.push({ path, data: new Uint8Array(body), mode: mode & 0o777 });
135 else if (type === "2") out.push({ path, data: new Uint8Array(0), link: pax.linkpath ?? linkname });
136 pax = {};
137 }
138 return out;