generic-filehandle2
Version:
uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data
38 lines • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const promises_1 = require("fs/promises");
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 (0, promises_1.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 (0, promises_1.readFile)(this.filename, options);
}
async stat() {
return (0, promises_1.stat)(this.filename);
}
async close() {
/* do nothing */
}
}
exports.default = LocalFile;
//# sourceMappingURL=localFile.js.map