UNPKG

generic-filehandle2

Version:

uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data

35 lines 1.08 kB
import { open, readFile, stat } from 'fs/promises'; export default class LocalFile { constructor(source, _opts = {}) { this.filename = source; } async read(length, position = 0) { if (length === 0) { return new Uint8Array(0); } const arr = new Uint8Array(length); let fd; // Declare fd outside the try block so it's accessible in finally try { fd = await open(this.filename, 'r'); const res = await fd.read(arr, 0, length, position); return res.buffer.subarray(0, res.bytesRead); } finally { // This block will always execute, regardless of success or error if (fd) { // Only close if the fd was successfully opened await fd.close(); } } } async readFile(options) { return readFile(this.filename, options); } async stat() { return stat(this.filename); } async close() { /* do nothing */ } } //# sourceMappingURL=localFile.js.map