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/server/src/memory-fs.ts?format=json
258 linesspeed 90% · codex-capacity-chair 10%Last change codex-capacity-chair in version 180, 2 versions changed it
1// From Cloudflare's isomorphic-git example, with fixes isomorphic-git 1.43
2// needs: errors carry .code (isomorphic-git checks err.code === "ENOENT"),
3// stats are plain fields it can normalize in place, and the executable bit
4// and symbolic links are kept (#27), so a checkout of a repo with either works.
5
6function fsError(code: string, path: string): Error {
7 return Object.assign(new Error(`${code}: ${path}`), { code });
8}
9
10type Entry =
11 | { kind: "dir"; children: Set<string>; mtimeMs: number }
12 | { kind: "file"; data: Uint8Array; byteLength?: number; exec: boolean; mtimeMs: number }
13 | { kind: "link"; target: string; mtimeMs: number };
15const enc = new TextEncoder();
17/** Optional scratch storage for file contents; directory metadata remains in memory. */
18export interface FileContents {
19 write(path: string, bytes: Uint8Array): void;
20 read(path: string): Uint8Array;
21 remove(path: string): void;
24/** Plain fields, not getters: isomorphic-git writes to them when it normalizes stats. */
25class MemoryStats {
26 size: number;
27 mtimeMs: number;
28 ctimeMs: number;
29 mode: number;
30 private kind: Entry["kind"];
32 constructor(entry: Entry) {
33 this.kind = entry.kind;
34 this.size =
35 entry.kind === "file"
36 ? (entry.byteLength ?? entry.data.byteLength)
37 : entry.kind === "link"
38 ? enc.encode(entry.target).byteLength
39 : 0;
40 this.mtimeMs = entry.mtimeMs;
41 this.ctimeMs = entry.mtimeMs;
42 this.mode = entry.kind === "dir" ? 0o040000 : entry.kind === "link" ? 0o120000 : entry.exec ? 0o100755 : 0o100644;
43 }
45 isFile() {
46 return this.kind === "file";
47 }
49 isDirectory() {
50 return this.kind === "dir";
51 }
53 isSymbolicLink() {
54 return this.kind === "link";
55 }
58export class MemoryFS {
59 private readonly contents?: FileContents;
60 constructor(contents?: FileContents) {
61 this.contents = contents;
62 }
63 encoder = new TextEncoder();
64 decoder = new TextDecoder();
65 entries = new Map<string, Entry>([["/", { kind: "dir", children: new Set(), mtimeMs: Date.now() }]]);
67 promises = {
68 readFile: this.readFile.bind(this),
69 writeFile: this.writeFile.bind(this),
70 unlink: this.unlink.bind(this),
71 readdir: this.readdir.bind(this),
72 mkdir: this.mkdir.bind(this),
73 rmdir: this.rmdir.bind(this),
74 stat: this.stat.bind(this),
75 lstat: this.lstat.bind(this),
76 readlink: this.readlink.bind(this),
77 symlink: this.symlink.bind(this),
78 chmod: this.chmod.bind(this),
79 };
81 normalize(input: string) {
82 const segments: string[] = [];
84 for (const part of input.split("/")) {
85 if (!part || part === ".") {
86 continue;
87 }
89 if (part === "..") {
90 segments.pop();
91 continue;
92 }
94 segments.push(part);
95 }
97 return `/${segments.join("/")}` || "/";
98 }
100 parent(path: string) {
101 const normalized = this.normalize(path);
102 if (normalized === "/") {
103 return "/";
104 }
106 const parts = normalized.split("/").filter(Boolean);
107 parts.pop();
108 return parts.length ? `/${parts.join("/")}` : "/";
109 }
111 basename(path: string) {
112 return this.normalize(path).split("/").filter(Boolean).pop() ?? "";
113 }
115 getEntry(path: string) {
116 return this.entries.get(this.normalize(path));
117 }
119 requireEntry(path: string) {
120 const entry = this.getEntry(path);
121 if (!entry) {
122 throw fsError("ENOENT", path);
123 }
125 return entry;
126 }
128 requireDir(path: string) {
129 const entry = this.requireEntry(path);
130 if (entry.kind !== "dir") {
131 throw fsError("ENOTDIR", path);
132 }
134 return entry;
135 }
137 async mkdir(path: string, options?: { recursive?: boolean } | number) {
138 const target = this.normalize(path);
139 if (target === "/") {
140 return;
141 }
143 const recursive = typeof options === "object" && options !== null && options.recursive;
144 const parent = this.parent(target);
146 if (!this.entries.has(parent)) {
147 if (!recursive) {
148 throw fsError("ENOENT", parent);
149 }
151 await this.mkdir(parent, { recursive: true });
152 }
154 if (this.entries.has(target)) {
155 return;
156 }
158 this.entries.set(target, {
159 kind: "dir",
160 children: new Set(),
161 mtimeMs: Date.now(),
162 });
164 this.requireDir(parent).children.add(this.basename(target));
165 }
167 /** options.mode with any execute bit (isomorphic-git passes 0o777 for 100755) makes the file executable. */
168 async writeFile(path: string, data: string | Uint8Array | ArrayBuffer, options?: string | { mode?: number }) {
169 const target = this.normalize(path);
170 await this.mkdir(this.parent(target), { recursive: true });
172 const bytes =
173 typeof data === "string" ? this.encoder.encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);
174 const mode = typeof options === "object" && options !== null ? options.mode : undefined;
175 this.contents?.write(target, bytes);
177 this.entries.set(target, {
178 kind: "file",
179 data: this.contents ? new Uint8Array(0) : bytes,
180 ...(this.contents ? { byteLength: bytes.byteLength } : {}),
181 exec: mode !== undefined && (mode & 0o111) !== 0,
182 mtimeMs: Date.now(),
183 });
185 this.requireDir(this.parent(target)).children.add(this.basename(target));
186 }
188 async readFile(path: string, options?: string | { encoding?: string }) {
189 const entry = this.requireEntry(path);
190 if (entry.kind === "dir") {
191 throw fsError("EISDIR", path);
192 }
194 const data =
195 entry.kind === "link"
196 ? this.encoder.encode(entry.target)
197 : this.contents
198 ? this.contents.read(this.normalize(path))
199 : entry.data;
200 const encoding = typeof options === "string" ? options : options?.encoding;
201 return encoding ? this.decoder.decode(data) : data;
202 }
204 async readdir(path: string) {
205 return [...this.requireDir(path).children].sort();
206 }
208 async unlink(path: string) {
209 const target = this.normalize(path);
210 const entry = this.requireEntry(target);
211 if (entry.kind === "dir") {
212 throw fsError("EISDIR", path);
213 }
215 if (entry.kind === "file") this.contents?.remove(target);
216 this.entries.delete(target);
217 this.requireDir(this.parent(target)).children.delete(this.basename(target));
218 }
220 async rmdir(path: string) {
221 const target = this.normalize(path);
222 const entry = this.requireDir(target);
223 if (entry.children.size > 0) {
224 throw fsError("ENOTEMPTY", path);
225 }
227 this.entries.delete(target);
228 this.requireDir(this.parent(target)).children.delete(this.basename(target));
229 }
231 async stat(path: string) {
232 return new MemoryStats(this.requireEntry(path));
233 }
235 /** Links aren't followed anywhere here, so lstat and stat agree. */
236 async lstat(path: string) {
237 return this.stat(path);
238 }
240 async readlink(path: string): Promise<string> {
241 const entry = this.requireEntry(path);
242 if (entry.kind !== "link") throw fsError("EINVAL", path);
243 return entry.target;
244 }
246 async symlink(target: string, path: string): Promise<void> {
247 const at = this.normalize(path);
248 if (this.entries.has(at)) throw fsError("EEXIST", path);
249 await this.mkdir(this.parent(at), { recursive: true });
250 this.entries.set(at, { kind: "link", target, mtimeMs: Date.now() });
251 this.requireDir(this.parent(at)).children.add(this.basename(at));
252 }
254 async chmod(path: string, mode: number): Promise<void> {
255 const entry = this.requireEntry(path);
256 if (entry.kind === "file") entry.exec = (mode & 0o111) !== 0;
257 }