From 361812dc7d05b2c8ed1f52cb2181d0f71d9c2f4f Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Thu, 15 Jun 2023 02:10:45 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20allow=20readin=20into=20v?= =?UTF-8?q?arious=20kinds=20of=20buffers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node.ts | 9 ++++----- src/volume.ts | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/node.ts b/src/node.ts index f172a9b15..a36eee7c7 100644 --- a/src/node.ts +++ b/src/node.ts @@ -185,10 +185,9 @@ 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; @@ -196,8 +195,8 @@ export class Node extends EventEmitter { 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; } @@ -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; diff --git a/src/volume.ts b/src/volume.ts index 1c453da6a..cffa75b79 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -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);