import { open, readFile, stat } from 'fs/promises' import type { BufferEncoding, FilehandleOptions, GenericFilehandle, Stats, } from './filehandle.ts' export default class LocalFile implements GenericFilehandle { private filename: string public constructor(source: string) { this.filename = source } public async read(length: number, position = 0) { if (length === 0) { return new Uint8Array(0) } const arr = new Uint8Array(length) let fd try { fd = await open(this.filename, 'r') const res = await fd.read(arr, 0, length, position) return res.buffer.subarray(0, res.bytesRead) } finally { if (fd) { try { await fd.close() } catch { // Ignore EBADF errors - the file descriptor is already closed/invalid // This can happen on network filesystems like Samba } } } } public async readFile( options?: Omit, ): Promise> public async readFile( options: | BufferEncoding | (Omit & { encoding: BufferEncoding }), ): Promise public async readFile( options?: FilehandleOptions | BufferEncoding, ): Promise | string> { return readFile(this.filename, options) } public async stat(): Promise { return stat(this.filename) } public async close(): Promise { /* do nothing */ } }