@ezdevlol/memfs
Version:
In-memory file-system with Node's fs API.
42 lines (41 loc) • 1.25 kB
JavaScript
/**
* Represents an open file. Stores additional metadata about the open file, such
* as the seek position.
*/
export class FsaNodeFsOpenFile {
fd;
createMode;
flags;
file;
filename;
seek = 0;
/**
* This influences the behavior of the next write operation. On the first
* write we want to overwrite the file or keep the existing data, depending
* with which flags the file was opened. On subsequent writes we want to
* append to the file.
*/
keepExistingData;
constructor(fd, createMode, flags, file, filename) {
this.fd = fd;
this.createMode = createMode;
this.flags = flags;
this.file = file;
this.filename = filename;
this.keepExistingData = !!(flags & 1024 /* FLAG.O_APPEND */);
}
async close() { }
async write(data, seek) {
if (typeof seek !== 'number')
seek = this.seek;
const writer = await this.file.createWritable({ keepExistingData: this.keepExistingData });
await writer.write({
type: 'write',
data,
position: seek,
});
await writer.close();
this.keepExistingData = true;
this.seek += data.byteLength;
}
}