Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/crud/__tests__/matryoshka.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ onlyOnNode20('CRUD matryoshka', () => {
});

describe('crud(fs(fsa(memfs)))', () => {
test('can write at offset', async () => {
const { fs } = memfs();
await fs.promises.writeFile('/file1.txt', 'abc');
const handle1 = await fs.promises.open('/file1.txt', 'r+');
await handle1.write(Buffer.from('.'), 0, 1, 1);
expect(await fs.promises.readFile('/file1.txt', 'utf8')).toBe('a.c');

const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const file2 = await fsa.getFileHandle('file2.txt', { create: true });
const writable1 = await file2.createWritable({ keepExistingData: false });
await writable1.write('abc');
await writable1.close();
const writable2 = await file2.createWritable({ keepExistingData: true });
await writable2.write({
type: 'write',
data: Buffer.from('.'),
position: 1,
size: 1,
});
await writable2.close();
expect(await (await file2.getFile()).text()).toBe('a.c');

const fs2 = new FsaNodeFs(fsa);
await fs2.promises.writeFile('/file3.txt', 'abc');
const handle = await fs2.promises.open('/file3.txt', 'r+');
await handle.write(Buffer.from('.'), 0, 1, 1);
expect(await fs2.promises.readFile('/file3.txt', 'utf8')).toBe('a.c');
});

testCrudfs(() => {
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
Expand Down
4 changes: 3 additions & 1 deletion src/fsa-to-node/FsaNodeFsOpenFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export class FsaNodeFsOpenFile {

public async write(data: ArrayBufferView, seek: number | null): Promise<void> {
if (typeof seek !== 'number') seek = this.seek;
const writer = await this.file.createWritable({ keepExistingData: this.keepExistingData });
else this.keepExistingData = true;
const keepExistingData = this.keepExistingData;
const writer = await this.file.createWritable({ keepExistingData });
await writer.write({
type: 'write',
data,
Expand Down
5 changes: 3 additions & 2 deletions src/node/FileHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ export class FileHandle extends EventEmitter implements IFileHandle {
length?: number,
position?: number | null,
): Promise<TFileHandleWriteResult> {
const writePosition = position !== null && position !== undefined ? position : this.position;
const useInternalPosition = typeof position !== 'number';
const writePosition: number = useInternalPosition ? this.position : position;

const result = await promisify(this.fs, 'write', bytesWritten => ({ bytesWritten, buffer }))(
this.fd,
Expand All @@ -226,7 +227,7 @@ export class FileHandle extends EventEmitter implements IFileHandle {
);

// Update internal position only if position was null/undefined
if (position === null || position === undefined) {
if (useInternalPosition) {
this.position += result.bytesWritten;
}

Expand Down
Loading