Versions 97 to 318
new file, 258 lines
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. 6+function fsError(code: string, path: string): Error { 7+ return Object.assign(new Error(`${code}: ${path}`), { code }); 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 }; 15+const enc = new TextEncoder(); 17+/** Optional scratch storage for file contents; directory metadata remains in memory. */ 18+export 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. */ 30+ private kind: Entry["kind"]; 32+ constructor(entry: Entry) { 33+ this.kind = entry.kind; 35+ entry.kind === "file" 36+ ? (entry.byteLength ?? entry.data.byteLength) 37+ : entry.kind === "link" 38+ ? enc.encode(entry.target).byteLength 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; 46+ return this.kind === "file"; 50+ return this.kind === "dir"; 54+ return this.kind === "link"; 58+export class MemoryFS { 59+ private readonly contents?: FileContents; 60+ constructor(contents?: FileContents) { 61+ this.contents = contents; 63+ encoder = new TextEncoder(); 64+ decoder = new TextDecoder(); 65+ entries = new Map<string, Entry>([["/", { kind: "dir", children: new Set(), mtimeMs: Date.now() }]]); 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), 81+ normalize(input: string) { 82+ const segments: string[] = []; 84+ for (const part of input.split("/")) { 85+ if (!part || part === ".") { 97+ return `/${segments.join("/")}` || "/"; 100+ parent(path: string) { 101+ const normalized = this.normalize(path); 102+ if (normalized === "/") { 106+ const parts = normalized.split("/").filter(Boolean); 108+ return parts.length ? `/${parts.join("/")}` : "/"; 111+ basename(path: string) { 112+ return this.normalize(path).split("/").filter(Boolean).pop() ?? ""; 115+ getEntry(path: string) { 116+ return this.entries.get(this.normalize(path)); 119+ requireEntry(path: string) { 120+ const entry = this.getEntry(path); 122+ throw fsError("ENOENT", path); 128+ requireDir(path: string) { 129+ const entry = this.requireEntry(path); 130+ if (entry.kind !== "dir") { 131+ throw fsError("ENOTDIR", path); 137+ async mkdir(path: string, options?: { recursive?: boolean } | number) { 138+ const target = this.normalize(path); 139+ if (target === "/") { 143+ const recursive = typeof options === "object" && options !== null && options.recursive; 144+ const parent = this.parent(target); 146+ if (!this.entries.has(parent)) { 148+ throw fsError("ENOENT", parent); 151+ await this.mkdir(parent, { recursive: true }); 154+ if (this.entries.has(target)) { 158+ this.entries.set(target, { 160+ children: new Set(), 161+ mtimeMs: Date.now(), 164+ this.requireDir(parent).children.add(this.basename(target)); 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 }); 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, { 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(), 185+ this.requireDir(this.parent(target)).children.add(this.basename(target)); 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); 195+ entry.kind === "link" 196+ ? this.encoder.encode(entry.target) 198+ ? this.contents.read(this.normalize(path)) 200+ const encoding = typeof options === "string" ? options : options?.encoding; 201+ return encoding ? this.decoder.decode(data) : data; 204+ async readdir(path: string) { 205+ return [...this.requireDir(path).children].sort(); 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); 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)); 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); 227+ this.entries.delete(target); 228+ this.requireDir(this.parent(target)).children.delete(this.basename(target)); 231+ async stat(path: string) { 232+ return new MemoryStats(this.requireEntry(path)); 235+ /** Links aren't followed anywhere here, so lstat and stat agree. */ 236+ async lstat(path: string) { 237+ return this.stat(path); 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; 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)); 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;