Skip to content

Commit

Permalink
fix: 🐛 allow readin into various kinds of buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 15, 2023
1 parent 3edcac1 commit e9c70e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,18 @@ export class Node extends EventEmitter {
}

// Returns the number of bytes read.
read(buf: Buffer | Uint8Array, off: number = 0, len: number = buf.byteLength, pos: number = 0): number {
read(buf: Buffer | ArrayBufferView | DataView, off: number = 0, len: number = buf.byteLength, pos: number = 0): number {
this.atime = new Date();
if (!this.buf) this.buf = bufferAllocUnsafe(0);

let actualLen = len;
if (actualLen > buf.byteLength) {
actualLen = buf.byteLength;
}
if (actualLen + pos > this.buf.length) {
actualLen = this.buf.length - pos;
}

this.buf.copy(buf as Buffer, off, pos, pos + actualLen);
const buf2 = buf instanceof Buffer ? buf : Buffer.from(buf.buffer);
this.buf.copy(buf2, off, pos, pos + actualLen);
return actualLen;
}

Expand Down Expand Up @@ -534,7 +533,7 @@ export class File {
return bytes;
}

read(buf: Buffer | Uint8Array, offset: number = 0, length: number = buf.byteLength, position?: number): number {
read(buf: Buffer | ArrayBufferView | DataView, offset: number = 0, length: number = buf.byteLength, position?: number): number {
if (typeof position !== 'number') position = this.position;
const bytes = this.node.read(buf, offset, length, position);
this.position = position + bytes;
Expand Down
8 changes: 4 additions & 4 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,23 +1099,23 @@ export class Volume {
}
}

private readBase(fd: number, buffer: Buffer | Uint8Array, offset: number, length: number, position: number): number {
private readBase(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number): number {
const file = this.getFileByFdOrThrow(fd);
return file.read(buffer, Number(offset), Number(length), position);
}

readSync(fd: number, buffer: Buffer | Uint8Array, offset: number, length: number, position: number): number {
readSync(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number): number {
validateFd(fd);
return this.readBase(fd, buffer, offset, length, position);
}

read(
fd: number,
buffer: Buffer | Uint8Array,
buffer: Buffer | ArrayBufferView | DataView,
offset: number,
length: number,
position: number,
callback: (err?: Error | null, bytesRead?: number, buffer?: Buffer | Uint8Array) => void,
callback: (err?: Error | null, bytesRead?: number, buffer?: Buffer | ArrayBufferView | DataView) => void,
) {
validateCallback(callback);

Expand Down

0 comments on commit e9c70e9

Please sign in to comment.