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?version=167&format=json
235 linesspeed 100%Last change speed in version 150, 1 version changed it

You're reading version 167. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

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; exec: boolean; mtimeMs: number }
13 | { kind: "link"; target: string; mtimeMs: number };
15const enc = new TextEncoder();
17/** Plain fields, not getters: isomorphic-git writes to them when it normalizes stats. */
18class MemoryStats {
19 size: number;
20 mtimeMs: number;
21 ctimeMs: number;
22 mode: number;
23 private kind: Entry["kind"];
25 constructor(entry: Entry) {
26 this.kind = entry.kind;
27 this.size =
28 entry.kind === "file" ? entry.data.byteLength : entry.kind === "link" ? enc.encode(entry.target).byteLength : 0;
29 this.mtimeMs = entry.mtimeMs;
30 this.ctimeMs = entry.mtimeMs;
31 this.mode = entry.kind === "dir" ? 0o040000 : entry.kind === "link" ? 0o120000 : entry.exec ? 0o100755 : 0o100644;
32 }
34 isFile() {
35 return this.kind === "file";
36 }
38 isDirectory() {
39 return this.kind === "dir";
40 }
42 isSymbolicLink() {
43 return this.kind === "link";
44 }
47export class MemoryFS {
48 encoder = new TextEncoder();
49 decoder = new TextDecoder();
50 entries = new Map<string, Entry>([["/", { kind: "dir", children: new Set(), mtimeMs: Date.now() }]]);
52 promises = {
53 readFile: this.readFile.bind(this),
54 writeFile: this.writeFile.bind(this),
55 unlink: this.unlink.bind(this),
56 readdir: this.readdir.bind(this),
57 mkdir: this.mkdir.bind(this),
58 rmdir: this.rmdir.bind(this),
59 stat: this.stat.bind(this),
60 lstat: this.lstat.bind(this),
61 readlink: this.readlink.bind(this),
62 symlink: this.symlink.bind(this),
63 chmod: this.chmod.bind(this),
64 };
66 normalize(input: string) {
67 const segments: string[] = [];
69 for (const part of input.split("/")) {
70 if (!part || part === ".") {
71 continue;
72 }
74 if (part === "..") {
75 segments.pop();
76 continue;
77 }
79 segments.push(part);
80 }
82 return `/${segments.join("/")}` || "/";
83 }
85 parent(path: string) {
86 const normalized = this.normalize(path);
87 if (normalized === "/") {
88 return "/";
89 }
91 const parts = normalized.split("/").filter(Boolean);
92 parts.pop();
93 return parts.length ? `/${parts.join("/")}` : "/";
94 }
96 basename(path: string) {
97 return this.normalize(path).split("/").filter(Boolean).pop() ?? "";
98 }
100 getEntry(path: string) {
101 return this.entries.get(this.normalize(path));
102 }
104 requireEntry(path: string) {
105 const entry = this.getEntry(path);
106 if (!entry) {
107 throw fsError("ENOENT", path);
108 }
110 return entry;
111 }
113 requireDir(path: string) {
114 const entry = this.requireEntry(path);
115 if (entry.kind !== "dir") {
116 throw fsError("ENOTDIR", path);
117 }
119 return entry;
120 }
122 async mkdir(path: string, options?: { recursive?: boolean } | number) {
123 const target = this.normalize(path);
124 if (target === "/") {
125 return;
126 }
128 const recursive = typeof options === "object" && options !== null && options.recursive;
129 const parent = this.parent(target);
131 if (!this.entries.has(parent)) {
132 if (!recursive) {
133 throw fsError("ENOENT", parent);
134 }
136 await this.mkdir(parent, { recursive: true });
137 }
139 if (this.entries.has(target)) {
140 return;
141 }
143 this.entries.set(target, {
144 kind: "dir",
145 children: new Set(),
146 mtimeMs: Date.now(),
147 });
149 this.requireDir(parent).children.add(this.basename(target));
150 }
152 /** options.mode with any execute bit (isomorphic-git passes 0o777 for 100755) makes the file executable. */
153 async writeFile(path: string, data: string | Uint8Array | ArrayBuffer, options?: string | { mode?: number }) {
154 const target = this.normalize(path);
155 await this.mkdir(this.parent(target), { recursive: true });
157 const bytes =
158 typeof data === "string" ? this.encoder.encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);
159 const mode = typeof options === "object" && options !== null ? options.mode : undefined;
161 this.entries.set(target, {
162 kind: "file",
163 data: bytes,
164 exec: mode !== undefined && (mode & 0o111) !== 0,
165 mtimeMs: Date.now(),
166 });
168 this.requireDir(this.parent(target)).children.add(this.basename(target));
169 }
171 async readFile(path: string, options?: string | { encoding?: string }) {
172 const entry = this.requireEntry(path);
173 if (entry.kind === "dir") {
174 throw fsError("EISDIR", path);
175 }
177 const data = entry.kind === "link" ? this.encoder.encode(entry.target) : entry.data;
178 const encoding = typeof options === "string" ? options : options?.encoding;
179 return encoding ? this.decoder.decode(data) : data;
180 }
182 async readdir(path: string) {
183 return [...this.requireDir(path).children].sort();
184 }
186 async unlink(path: string) {
187 const target = this.normalize(path);
188 const entry = this.requireEntry(target);
189 if (entry.kind === "dir") {
190 throw fsError("EISDIR", path);
191 }
193 this.entries.delete(target);
194 this.requireDir(this.parent(target)).children.delete(this.basename(target));
195 }
197 async rmdir(path: string) {
198 const target = this.normalize(path);
199 const entry = this.requireDir(target);
200 if (entry.children.size > 0) {
201 throw fsError("ENOTEMPTY", path);
202 }
204 this.entries.delete(target);
205 this.requireDir(this.parent(target)).children.delete(this.basename(target));
206 }
208 async stat(path: string) {
209 return new MemoryStats(this.requireEntry(path));
210 }
212 /** Links aren't followed anywhere here, so lstat and stat agree. */
213 async lstat(path: string) {
214 return this.stat(path);
215 }
217 async readlink(path: string): Promise<string> {
218 const entry = this.requireEntry(path);
219 if (entry.kind !== "link") throw fsError("EINVAL", path);
220 return entry.target;
221 }
223 async symlink(target: string, path: string): Promise<void> {
224 const at = this.normalize(path);
225 if (this.entries.has(at)) throw fsError("EEXIST", path);
226 await this.mkdir(this.parent(at), { recursive: true });
227 this.entries.set(at, { kind: "link", target, mtimeMs: Date.now() });
228 this.requireDir(this.parent(at)).children.add(this.basename(at));
229 }
231 async chmod(path: string, mode: number): Promise<void> {
232 const entry = this.requireEntry(path);
233 if (entry.kind === "file") entry.exec = (mode & 0o111) !== 0;
234 }