Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/opfs.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469import type { FileHandle as NodeFileHandle } from "node:fs/promises";import type { FileBackendType, FileDriverType } from "./file.ts";import { defineFileDriver } from "./file.ts";import type { FileDriverCopyOptionsType, FileDriverDirectoryEntryType, FileDriverMoveOptionsType, FileDriverReadOptionsType, FileDriverSignalOptionsType, FileDriverStatType, FileDriverSyncFileType, FileDriverWritableFileType, FileDriverWriteOptionsType,} from "./file.ts";import { createLocalPath } from "./local.ts";import { FileSystemError, throwIfAborted, toFileSystemError } from "../error.ts";import type { PathType } from "../path.ts";
/** Node built-in filesystem module shape used through `process.getBuiltinModule()`. */export type NodeFsType = typeof import("node:fs");/** Node promise-based filesystem module shape used through `process.getBuiltinModule()`. */export type NodeFsPromisesType = typeof import("node:fs/promises");/** Node stream module shape used only to convert native streams to Web Streams. */export type NodeStreamType = typeof import("node:stream");
/** * Options for the Node filesystem driver. * * The configured `root` is the lexical host namespace represented by virtual * `/`. Virtual `..` escape is rejected, but native Node calls follow symbolic * links already present below that root. Treat the configured root as trusted * host storage rather than a process-level security isolation mechanism. */export interface NodeDriverOptionsType { /** Host directory exposed as virtual `/`. */ readonly root: string; /** Creates the host root during driver creation. Defaults to true. */ readonly createRoot?: boolean;}
/** Opens one update-mode file, creating it only when the path was absent. */export async function openUpdateFile( fs: NodeFsPromisesType, path: string, virtualPath: string,): Promise<NodeFileHandle> { try { return await fs.open(path, "r+"); } catch (error) { if (toFileSystemError(error, "write", virtualPath).code !== "not-found") throw error; return await fs.open(path, "w+"); }}
/** * Drains a Web byte stream into one Node file descriptor. * * The descriptor stays open for the full stream. Partial writes advance the * explicit cursor until every chunk is committed. If writing fails, the source * producer is cancelled before the file closes so upstream work does not keep * producing bytes for a terminal operation. */export async function writeStreamToFile( fs: NodeFsPromisesType, hostPath: string, virtualPath: string, source: ReadableStream<Uint8Array>, options: FileDriverWriteOptionsType,): Promise<void> { let file: NodeFileHandle | undefined; try { file = options.mode === "update" ? await openUpdateFile(fs, hostPath, virtualPath) : await fs.open(hostPath, options.mode === "replace" ? "w+" : "a+");
let position = options.mode === "replace" ? 0 : options.mode === "append" ? (await file.stat()).size : options.at ?? 0;
const reader = source.getReader(); try { while (true) { throwIfAborted(options.signal, "write", virtualPath); const next = await reader.read(); if (next.done) break;
let offset = 0; while (offset < next.value.byteLength) { const result = await file.write(next.value, offset, next.value.byteLength - offset, position); if (result.bytesWritten <= 0) throw new Error(`Node write made no progress for '${virtualPath}'.`); offset += result.bytesWritten; position += result.bytesWritten; } } } catch (error) { try { await reader.cancel(error); } catch { // The original write/cancellation failure is the useful terminal cause. } throw error; } finally { reader.releaseLock(); }
if (options.truncate) await file.truncate(position); } finally { await file?.close(); }}
/** * Long-lived Node positional file used by {@link NodeAdapter.openWritableFile}. * * The class keeps one descriptor open for rewrites and treats `#file === * undefined` as the only closed-state marker. `abort()` cannot roll back bytes * already written to a normal host file; it only releases the descriptor. */export class NodeWritableFile implements FileDriverWritableFileType { /** Canonical virtual path used in lifecycle diagnostics. */ readonly #path: PathType; /** Native file descriptor, cleared before terminal close/abort. */ #file: NodeFileHandle | undefined;
/** Takes ownership of the already-open Node file descriptor. */ constructor(path: PathType, file: NodeFileHandle) { this.#path = path; this.#file = file; }
/** Returns the live descriptor and rejects ordinary work after termination. */ #getFile(): NodeFileHandle { if (this.#file === undefined) throw new Error(`Writable file '${this.#path}' is closed.`); return this.#file; }
/** Writes every source byte at one explicit position, including partial native writes. */ async write(buffer: ArrayBufferView, options: { readonly at: number }): Promise<void> { const source = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); let offset = 0; while (offset < source.byteLength) { const result = await this.#getFile().write(source, offset, source.byteLength - offset, options.at + offset); if (result.bytesWritten <= 0) throw new Error(`Node positional write made no progress for '${this.#path}'.`); offset += result.bytesWritten; } }
/** Changes the current native file length without closing it. */ async truncate(size: number): Promise<void> { await this.#getFile().truncate(size); }
/** Requests `fsync` through Node's promise file handle. */ async flush(): Promise<void> { await this.#getFile().sync(); }
/** Closes once and clears the descriptor before awaiting native close. */ async close(): Promise<void> { const file = this.#file; if (file === undefined) return; this.#file = undefined; await file.close(); }
/** Releases the descriptor without claiming rollback of bytes already written. */ async abort(): Promise<void> { await this.close(); }}
/** * Synchronous random-access wrapper over one Node file descriptor. * * Cursor state is local to this wrapper. Passing `at` on a read/write performs * that operation at the explicit position and moves the wrapper cursor to the * end of the operation, matching the package sync-file contract. */export class NodeSyncFile implements FileDriverSyncFileType { /** Node sync API used for descriptor operations. */ readonly #fs: NodeFsType; /** Canonical virtual path used in lifecycle diagnostics. */ readonly #path: PathType; /** Native descriptor, cleared after close. */ #descriptor: number | undefined; /** Logical cursor used when an operation omits `at`. */ #cursor = 0;
/** Takes ownership of one already-open descriptor. */ constructor(fs: NodeFsType, path: PathType, descriptor: number) { this.#fs = fs; this.#path = path; this.#descriptor = descriptor; }
/** Returns the live descriptor and rejects access after close. */ #getDescriptor(): number { if (this.#descriptor === undefined) throw new Error(`Sync file '${this.#path}' is closed.`); return this.#descriptor; }
/** Reads synchronously into the caller buffer and advances the local cursor. */ read(buffer: ArrayBufferView, options: { readonly at?: number } = {}): number { const target = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); const position = options.at ?? this.#cursor; const count = this.#fs.readSync(this.#getDescriptor(), target, 0, target.byteLength, position); this.#cursor = position + count; return count; }
/** Writes synchronously and advances the local cursor by native progress. */ write(buffer: ArrayBufferView, options: { readonly at?: number } = {}): number { const source = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); const position = options.at ?? this.#cursor; const count = this.#fs.writeSync(this.#getDescriptor(), source, 0, source.byteLength, position); this.#cursor = position + count; return count; }
/** Returns the current native file size. */ getSize(): number { return this.#fs.fstatSync(this.#getDescriptor()).size; }
/** Truncates the file and clamps the local cursor to the new end. */ truncate(size: number): void { this.#fs.ftruncateSync(this.#getDescriptor(), size); if (this.#cursor > size) this.#cursor = size; }
/** Requests native filesystem durability for current descriptor writes. */ flush(): void { this.#fs.fsyncSync(this.#getDescriptor()); }
/** Closes the native descriptor exactly once. */ close(): void { const descriptor = this.#descriptor; if (descriptor === undefined) return; this.#descriptor = undefined; this.#fs.closeSync(descriptor); }}
/** * Node host-filesystem implementation of the portable file-driver contract. * * Runtime-specific modules are resolved through `process.getBuiltinModule()` in * the constructor. The package root and unrelated runtime subpaths therefore do not * load Node built-ins merely because this source exists in the package. */export class NodeBackend implements FileBackendType { /** Stable driver identity used in diagnostics. */ readonly name = "node"; /** Native Node filesystem operations exposed without facade emulation. */ readonly capabilities = { read: true, write: true, streamRead: true, streamWriteModes: ["replace", "append", "update"], rangeRead: true, copy: true, move: true, positionalWrite: true, syncAccess: true, } as const; /** Node synchronous filesystem module. */ readonly #fs: NodeFsType; /** Node promise-based filesystem module. */ readonly #fsp: NodeFsPromisesType; /** Node stream module used only for native-to-Web stream conversion. */ readonly #stream: NodeStreamType; /** Maps canonical virtual paths below the configured host root. */ readonly #hostPath: (path: string) => string;
/** Resolves Node built-ins and optionally creates the configured host root. */ constructor(options: NodeDriverOptionsType) { this.#fs = globalThis.process.getBuiltinModule("node:fs") as NodeFsType; this.#fsp = globalThis.process.getBuiltinModule("node:fs/promises") as NodeFsPromisesType; this.#stream = globalThis.process.getBuiltinModule("node:stream") as NodeStreamType; this.#hostPath = createLocalPath(options.root); if (options.createRoot ?? true) this.#fs.mkdirSync(this.#hostPath("/"), { recursive: true }); }
/** Returns host metadata or `null` when the virtual path is absent. */ async stat(path: PathType, options: FileDriverSignalOptionsType = {}): Promise<FileDriverStatType | null> { throwIfAborted(options.signal, "stat", path); try { const info = await this.#fsp.stat(this.#hostPath(path)); return info.isDirectory() ? { kind: "directory", lastModified: info.mtimeMs } : { kind: "file", size: info.size, lastModified: info.mtimeMs, mediaType: "" }; } catch (error) { const mapped = toFileSystemError(error, "stat", path); if (mapped.code === "not-found") return null; throw mapped; } }
/** Reads the complete file or performs positioned reads for one requested range. */ async readFile(path: PathType, options: FileDriverReadOptionsType = {}): Promise<Uint8Array> { throwIfAborted(options.signal, "read", path); if (options.at === undefined && options.length === undefined) { return new Uint8Array(await this.#fsp.readFile(this.#hostPath(path))); }
const file = await this.#fsp.open(this.#hostPath(path), "r"); try { const info = await file.stat(); if (info.isDirectory()) throw new FileSystemError("type-mismatch", "read", path, `'${path}' is a directory.`); const start = options.at ?? 0; const length = Math.max(0, Math.min(options.length ?? info.size - start, info.size - start)); const output = new Uint8Array(length); let offset = 0; while (offset < length) { const result = await file.read(output, offset, length - offset, start + offset); if (result.bytesRead === 0) break; offset += result.bytesRead; } return offset === output.byteLength ? output : output.slice(0, offset); } finally { await file.close(); } }
/** Opens a native Node read stream and projects it as a Web byte stream. */ async openReadStream(path: PathType, options: FileDriverReadOptionsType = {}): Promise<ReadableStream<Uint8Array>> { throwIfAborted(options.signal, "read", path); const target = this.#hostPath(path); if (options.length === 0) { // `createReadStream({ start, end: start })` yields one byte because Node's // `end` is inclusive. Stat once to preserve not-found/type failures, then // return the exact empty range requested by the portable contract. const info = await this.#fsp.stat(target); if (info.isDirectory()) throw new FileSystemError("type-mismatch", "read", path, `'${path}' is a directory.`); return new ReadableStream<Uint8Array>({ start(controller) { controller.close(); } }); } const start = options.at ?? 0; const end = options.length === undefined ? undefined : start + options.length - 1; const stream = this.#fs.createReadStream(target, { start, ...(end === undefined ? {} : { end }) }); return this.#stream.Readable.toWeb(stream) as unknown as ReadableStream<Uint8Array>; }
/** Preserves replace, append, and positioned update semantics with native Node APIs. */ async writeFile(path: PathType, data: Uint8Array, options: FileDriverWriteOptionsType): Promise<void> { throwIfAborted(options.signal, "write", path); const target = this.#hostPath(path); if (options.mode === "replace") { await this.#fsp.writeFile(target, data); return; } if (options.mode === "append") { await this.#fsp.appendFile(target, data); return; }
const file = await openUpdateFile(this.#fsp, target, path); try { const position = options.at ?? 0; let offset = 0; while (offset < data.byteLength) { const result = await file.write(data, offset, data.byteLength - offset, position + offset); if (result.bytesWritten <= 0) throw new Error(`Node write made no progress for '${path}'.`); offset += result.bytesWritten; } if (options.truncate) await file.truncate(position + data.byteLength); } finally { await file.close(); } }
/** Streams bytes directly to one native file without facade materialization. */ async writeStream( path: PathType, source: ReadableStream<Uint8Array>, options: FileDriverWriteOptionsType, ): Promise<void> { await writeStreamToFile(this.#fsp, this.#hostPath(path), path, source, options); }
/** Lazily yields native direct children that are files or directories. */ async *readDir( path: PathType, options: FileDriverSignalOptionsType = {}, ): AsyncIterableIterator<FileDriverDirectoryEntryType> { throwIfAborted(options.signal, "read-dir", path); for (const entry of await this.#fsp.readdir(this.#hostPath(path), { withFileTypes: true })) { throwIfAborted(options.signal, "read-dir", path); if (entry.isDirectory()) yield { name: entry.name, kind: "directory" }; else if (entry.isFile()) yield { name: entry.name, kind: "file" }; } }
/** Creates exactly one host directory. Parent creation belongs to the facade. */ async createDir(path: PathType, options: FileDriverSignalOptionsType = {}): Promise<void> { throwIfAborted(options.signal, "mkdir", path); await this.#fsp.mkdir(this.#hostPath(path)); }
/** * Removes exactly one host file, symbolic link, or empty directory. * * A plain host `rm(path)` is not a portable one-entry directory primitive: * supported Node/Bun runs can report `ERR_FS_EISDIR` for a directory. The * driver must nevertheless remove one already-empty directory because recursive policy * lives in the filesystem facade. `lstat()` also keeps a final symbolic link * distinct from the directory it may reference, so deletion removes the link * itself instead of following it. */ async remove(path: PathType, options: FileDriverSignalOptionsType = {}): Promise<void> { throwIfAborted(options.signal, "remove", path); const target = this.#hostPath(path); const info = await this.#fsp.lstat(target); throwIfAborted(options.signal, "remove", path); if (info.isDirectory()) await this.#fsp.rmdir(target); else await this.#fsp.unlink(target); }
/** Uses `copyFile()` so source bytes do not route through JavaScript buffers. */ async copy(source: PathType, destination: PathType, options: FileDriverCopyOptionsType): Promise<void> { throwIfAborted(options.signal, "copy", source); await this.#fsp.copyFile(this.#hostPath(source), this.#hostPath(destination)); }
/** Uses native rename for the driver's move capability. */ async move(source: PathType, destination: PathType, options: FileDriverMoveOptionsType): Promise<void> { throwIfAborted(options.signal, "move", source); await this.#fsp.rename(this.#hostPath(source), this.#hostPath(destination)); }
/** Opens one long-lived asynchronous positional file descriptor. */ async openWritableFile(path: PathType): Promise<FileDriverWritableFileType> { return new NodeWritableFile(path, await this.#fsp.open(this.#hostPath(path), "r+")); }
/** Opens one synchronous random-access descriptor and transfers ownership to the wrapper. */ async openSyncFile(path: PathType): Promise<FileDriverSyncFileType> { return new NodeSyncFile(this.#fs, path, this.#fs.openSync(this.#hostPath(path), "r+")); }}
/** * Creates a file driver over Node's native filesystem APIs. * * The driver maps virtual `/` to `root` and never exposes host paths through * the public facade. Importing the root OPFS package does not import this * driver; Node-specific behavior remains on the explicit `driver/node` * subpath. * * @example Use OPFS-shaped handles over a host directory. * ```ts * const driver = createNodeDriver({ root: "./data" }); * await fs.writeFile("/state.json", "{}", { parents: true }); * ``` */export function createNodeDriver(options: NodeDriverOptionsType): FileDriverType { const backend = new NodeBackend(options); return defineFileDriver(backend, { name: "node", requirements: [{ code: "node-filesystem", state: "available" }], limits: [], // Node already exposes the required file semantics directly, so the driver // does not need additional behavior-changing optimization toggles here. optimizations: [], });}