/** * An in-memory Origin Private File System for the OPFS worker runtime: just * the `FileSystemDirectoryHandle` / `FileSystemFileHandle` surface it uses. * Moved out of `tests/unit/opfs-worker-runtime.test.ts` so the recipe tests * can run the same runtime (2026-11-03 05-04). */ export type MemoryNode = MemoryDirectory | MemoryFile; export class MemoryFile { readonly kind = "file"; bytes = new Uint8Array(); lastModified = 0; reads = 0; } export class MemoryDirectory { readonly kind = "directory"; readonly children = new Map(); async getDirectoryHandle( name: string, options: FileSystemGetDirectoryOptions = {}, ): Promise { const current = this.children.get(name); if (current instanceof MemoryDirectory) { return current as unknown as FileSystemDirectoryHandle; } if (current || !options.create) throw notFound(); const created = new MemoryDirectory(); this.children.set(name, created); return created as unknown as FileSystemDirectoryHandle; } async getFileHandle( name: string, options: FileSystemGetFileOptions = {}, ): Promise { const current = this.children.get(name); if (current instanceof MemoryFile) { return fileHandle(current); } if (current || !options.create) throw notFound(); const created = new MemoryFile(); this.children.set(name, created); return fileHandle(created); } async removeEntry( name: string, options: FileSystemRemoveOptions = {}, ): Promise { const current = this.children.get(name); if (!current) throw notFound(); if ( current instanceof MemoryDirectory && current.children.size > 0 && !options.recursive ) { throw new DOMException("Directory is not empty.", "InvalidModificationError"); } this.children.delete(name); } async *entries(): AsyncIterableIterator< [string, FileSystemDirectoryHandle | FileSystemFileHandle] > { for (const [name, node] of this.children) { yield [ name, node instanceof MemoryDirectory ? (node as unknown as FileSystemDirectoryHandle) : fileHandle(node), ]; } } has(path: readonly string[]): boolean { let current: MemoryNode = this; for (const segment of path) { if (!(current instanceof MemoryDirectory)) return false; const next = current.children.get(segment); if (!next) return false; current = next; } return true; } } function fileHandle(file: MemoryFile): FileSystemFileHandle { return { kind: "file", name: "memory", async getFile() { file.reads += 1; const blob = new Blob([Uint8Array.from(file.bytes)]); Object.defineProperty(blob, "lastModified", { value: file.lastModified, }); return blob as File; }, async createWritable() { let pending = Uint8Array.from(file.bytes); return { async write(data: FileSystemWriteChunkType) { if (!(data instanceof Uint8Array)) { throw new TypeError("The test writer accepts Uint8Array only."); } pending = Uint8Array.from(data); }, async close() { file.bytes = pending; file.lastModified = Date.now(); }, async abort() {}, } as unknown as FileSystemWritableFileStream; }, } as FileSystemFileHandle; } function notFound(): DOMException { return new DOMException("Entry was not found.", "NotFoundError"); }