Skip to content

Commit

Permalink
fix: support -1 in all read and write methods
Browse files Browse the repository at this point in the history
  • Loading branch information
uhyo committed Aug 21, 2023
1 parent a0ba657 commit 192e2ef
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,9 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
private readvBase(fd: number, buffers: ArrayBufferView[], position: number | null): number {
const file = this.getFileByFdOrThrow(fd);
let p = position ?? undefined;
if (p === -1) {
p = undefined;
}
let bytesRead = 0;
for (const buffer of buffers) {
const bytes = file.read(buffer, 0, buffer.byteLength, p);
Expand Down Expand Up @@ -912,7 +915,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {

private writeBase(fd: number, buf: Buffer, offset?: number, length?: number, position?: number | null): number {
const file = this.getFileByFdOrThrow(fd, 'write');
return file.write(buf, offset, length, position);
return file.write(buf, offset, length, position === -1 || typeof position !== 'number' ? undefined : position);
}

writeSync(
Expand Down Expand Up @@ -973,6 +976,9 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
private writevBase(fd: number, buffers: ArrayBufferView[], position: number | null): number {
const file = this.getFileByFdOrThrow(fd);
let p = position ?? undefined;
if (p === -1) {
p = undefined;
}
let bytesWritten = 0;
for (const buffer of buffers) {
const nodeBuf = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
Expand Down

0 comments on commit 192e2ef

Please sign in to comment.