{"draft":null,"from":116,"to":318,"files":[{"path":"server/src/memory-fs.ts","added":258,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/memory-fs.ts\n@@ -0,0 +1,258 @@\n+// From Cloudflare's isomorphic-git example, with fixes isomorphic-git 1.43\n+// needs: errors carry .code (isomorphic-git checks err.code === \"ENOENT\"),\n+// stats are plain fields it can normalize in place, and the executable bit\n+// and symbolic links are kept (#27), so a checkout of a repo with either works.\n+\n+function fsError(code: string, path: string): Error {\n+\treturn Object.assign(new Error(`${code}: ${path}`), { code });\n+}\n+\n+type Entry =\n+\t| { kind: \"dir\"; children: Set<string>; mtimeMs: number }\n+\t| { kind: \"file\"; data: Uint8Array; byteLength?: number; exec: boolean; mtimeMs: number }\n+\t| { kind: \"link\"; target: string; mtimeMs: number };\n+\n+const enc = new TextEncoder();\n+\n+/** Optional scratch storage for file contents; directory metadata remains in memory. */\n+export interface FileContents {\n+\twrite(path: string, bytes: Uint8Array): void;\n+\tread(path: string): Uint8Array;\n+\tremove(path: string): void;\n+}\n+\n+/** Plain fields, not getters: isomorphic-git writes to them when it normalizes stats. */\n+class MemoryStats {\n+\tsize: number;\n+\tmtimeMs: number;\n+\tctimeMs: number;\n+\tmode: number;\n+\tprivate kind: Entry[\"kind\"];\n+\n+\tconstructor(entry: Entry) {\n+\t\tthis.kind = entry.kind;\n+\t\tthis.size =\n+\t\t\tentry.kind === \"file\"\n+\t\t\t\t? (entry.byteLength ?? entry.data.byteLength)\n+\t\t\t\t: entry.kind === \"link\"\n+\t\t\t\t\t? enc.encode(entry.target).byteLength\n+\t\t\t\t\t: 0;\n+\t\tthis.mtimeMs = entry.mtimeMs;\n+\t\tthis.ctimeMs = entry.mtimeMs;\n+\t\tthis.mode = entry.kind === \"dir\" ? 0o040000 : entry.kind === \"link\" ? 0o120000 : entry.exec ? 0o100755 : 0o100644;\n+\t}\n+\n+\tisFile() {\n+\t\treturn this.kind === \"file\";\n+\t}\n+\n+\tisDirectory() {\n+\t\treturn this.kind === \"dir\";\n+\t}\n+\n+\tisSymbolicLink() {\n+\t\treturn this.kind === \"link\";\n+\t}\n+}\n+\n+export class MemoryFS {\n+\tprivate readonly contents?: FileContents;\n+\tconstructor(contents?: FileContents) {\n+\t\tthis.contents = contents;\n+\t}\n+\tencoder = new TextEncoder();\n+\tdecoder = new TextDecoder();\n+\tentries = new Map<string, Entry>([[\"/\", { kind: \"dir\", children: new Set(), mtimeMs: Date.now() }]]);\n+\n+\tpromises = {\n+\t\treadFile: this.readFile.bind(this),\n+\t\twriteFile: this.writeFile.bind(this),\n+\t\tunlink: this.unlink.bind(this),\n+\t\treaddir: this.readdir.bind(this),\n+\t\tmkdir: this.mkdir.bind(this),\n+\t\trmdir: this.rmdir.bind(this),\n+\t\tstat: this.stat.bind(this),\n+\t\tlstat: this.lstat.bind(this),\n+\t\treadlink: this.readlink.bind(this),\n+\t\tsymlink: this.symlink.bind(this),\n+\t\tchmod: this.chmod.bind(this),\n+\t};\n+\n+\tnormalize(input: string) {\n+\t\tconst segments: string[] = [];\n+\n+\t\tfor (const part of input.split(\"/\")) {\n+\t\t\tif (!part || part === \".\") {\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\n+\t\t\tif (part === \"..\") {\n+\t\t\t\tsegments.pop();\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\n+\t\t\tsegments.push(part);\n+\t\t}\n+\n+\t\treturn `/${segments.join(\"/\")}` || \"/\";\n+\t}\n+\n+\tparent(path: string) {\n+\t\tconst normalized = this.normalize(path);\n+\t\tif (normalized === \"/\") {\n+\t\t\treturn \"/\";\n+\t\t}\n+\n+\t\tconst parts = normalized.split(\"/\").filter(Boolean);\n+\t\tparts.pop();\n+\t\treturn parts.length ? `/${parts.join(\"/\")}` : \"/\";\n+\t}\n+\n+\tbasename(path: string) {\n+\t\treturn this.normalize(path).split(\"/\").filter(Boolean).pop() ?? \"\";\n+\t}\n+\n+\tgetEntry(path: string) {\n+\t\treturn this.entries.get(this.normalize(path));\n+\t}\n+\n+\trequireEntry(path: string) {\n+\t\tconst entry = this.getEntry(path);\n+\t\tif (!entry) {\n+\t\t\tthrow fsError(\"ENOENT\", path);\n+\t\t}\n+\n+\t\treturn entry;\n+\t}\n+\n+\trequireDir(path: string) {\n+\t\tconst entry = this.requireEntry(path);\n+\t\tif (entry.kind !== \"dir\") {\n+\t\t\tthrow fsError(\"ENOTDIR\", path);\n+\t\t}\n+\n+\t\treturn entry;\n+\t}\n+\n+\tasync mkdir(path: string, options?: { recursive?: boolean } | number) {\n+\t\tconst target = this.normalize(path);\n+\t\tif (target === \"/\") {\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tconst recursive = typeof options === \"object\" && options !== null && options.recursive;\n+\t\tconst parent = this.parent(target);\n+\n+\t\tif (!this.entries.has(parent)) {\n+\t\t\tif (!recursive) {\n+\t\t\t\tthrow fsError(\"ENOENT\", parent);\n+\t\t\t}\n+\n+\t\t\tawait this.mkdir(parent, { recursive: true });\n+\t\t}\n+\n+\t\tif (this.entries.has(target)) {\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tthis.entries.set(target, {\n+\t\t\tkind: \"dir\",\n+\t\t\tchildren: new Set(),\n+\t\t\tmtimeMs: Date.now(),\n+\t\t});\n+\n+\t\tthis.requireDir(parent).children.add(this.basename(target));\n+\t}\n+\n+\t/** options.mode with any execute bit (isomorphic-git passes 0o777 for 100755) makes the file executable. */\n+\tasync writeFile(path: string, data: string | Uint8Array | ArrayBuffer, options?: string | { mode?: number }) {\n+\t\tconst target = this.normalize(path);\n+\t\tawait this.mkdir(this.parent(target), { recursive: true });\n+\n+\t\tconst bytes =\n+\t\t\ttypeof data === \"string\" ? this.encoder.encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);\n+\t\tconst mode = typeof options === \"object\" && options !== null ? options.mode : undefined;\n+\t\tthis.contents?.write(target, bytes);\n+\n+\t\tthis.entries.set(target, {\n+\t\t\tkind: \"file\",\n+\t\t\tdata: this.contents ? new Uint8Array(0) : bytes,\n+\t\t\t...(this.contents ? { byteLength: bytes.byteLength } : {}),\n+\t\t\texec: mode !== undefined && (mode & 0o111) !== 0,\n+\t\t\tmtimeMs: Date.now(),\n+\t\t});\n+\n+\t\tthis.requireDir(this.parent(target)).children.add(this.basename(target));\n+\t}\n+\n+\tasync readFile(path: string, options?: string | { encoding?: string }) {\n+\t\tconst entry = this.requireEntry(path);\n+\t\tif (entry.kind === \"dir\") {\n+\t\t\tthrow fsError(\"EISDIR\", path);\n+\t\t}\n+\n+\t\tconst data =\n+\t\t\tentry.kind === \"link\"\n+\t\t\t\t? this.encoder.encode(entry.target)\n+\t\t\t\t: this.contents\n+\t\t\t\t\t? this.contents.read(this.normalize(path))\n+\t\t\t\t\t: entry.data;\n+\t\tconst encoding = typeof options === \"string\" ? options : options?.encoding;\n+\t\treturn encoding ? this.decoder.decode(data) : data;\n+\t}\n+\n+\tasync readdir(path: string) {\n+\t\treturn [...this.requireDir(path).children].sort();\n+\t}\n+\n+\tasync unlink(path: string) {\n+\t\tconst target = this.normalize(path);\n+\t\tconst entry = this.requireEntry(target);\n+\t\tif (entry.kind === \"dir\") {\n+\t\t\tthrow fsError(\"EISDIR\", path);\n+\t\t}\n+\n+\t\tif (entry.kind === \"file\") this.contents?.remove(target);\n+\t\tthis.entries.delete(target);\n+\t\tthis.requireDir(this.parent(target)).children.delete(this.basename(target));\n+\t}\n+\n+\tasync rmdir(path: string) {\n+\t\tconst target = this.normalize(path);\n+\t\tconst entry = this.requireDir(target);\n+\t\tif (entry.children.size > 0) {\n+\t\t\tthrow fsError(\"ENOTEMPTY\", path);\n+\t\t}\n+\n+\t\tthis.entries.delete(target);\n+\t\tthis.requireDir(this.parent(target)).children.delete(this.basename(target));\n+\t}\n+\n+\tasync stat(path: string) {\n+\t\treturn new MemoryStats(this.requireEntry(path));\n+\t}\n+\n+\t/** Links aren't followed anywhere here, so lstat and stat agree. */\n+\tasync lstat(path: string) {\n+\t\treturn this.stat(path);\n+\t}\n+\n+\tasync readlink(path: string): Promise<string> {\n+\t\tconst entry = this.requireEntry(path);\n+\t\tif (entry.kind !== \"link\") throw fsError(\"EINVAL\", path);\n+\t\treturn entry.target;\n+\t}\n+\n+\tasync symlink(target: string, path: string): Promise<void> {\n+\t\tconst at = this.normalize(path);\n+\t\tif (this.entries.has(at)) throw fsError(\"EEXIST\", path);\n+\t\tawait this.mkdir(this.parent(at), { recursive: true });\n+\t\tthis.entries.set(at, { kind: \"link\", target, mtimeMs: Date.now() });\n+\t\tthis.requireDir(this.parent(at)).children.add(this.basename(at));\n+\t}\n+\n+\tasync chmod(path: string, mode: number): Promise<void> {\n+\t\tconst entry = this.requireEntry(path);\n+\t\tif (entry.kind === \"file\") entry.exec = (mode & 0o111) !== 0;\n+\t}\n+}"}],"unmatched":[]}