From 7357c14289c53d5c8dc13f38c1c696f01245b9a0 Mon Sep 17 00:00:00 2001 From: streamich Date: Sat, 17 Jun 2023 21:23:58 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20implement=20read()=20met?= =?UTF-8?q?hod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fsa-to-node/FsaNodeFs.ts | 17 ++++++++++- src/fsa-to-node/__tests__/FsaNodeFs.test.ts | 31 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index 0680f33ff..1395dd28b 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -195,7 +195,22 @@ export class FsaNodeFs implements FsCallbackApi, FsCommonObjects { position: number, callback: (err?: Error | null, bytesRead?: number, buffer?: Buffer | ArrayBufferView | DataView) => void, ): void => { - throw new Error('Not implemented'); + validateCallback(callback); + // This `if` branch is from Node.js + if (length === 0) { + return process.nextTick(() => { + if (callback) callback(null, 0, buffer); + }); + } + (async () => { + const openFile = await this.getFileByFd(fd, 'read'); + const file = await openFile.file.getFile(); + const src = await file.arrayBuffer(); + const slice = new Uint8Array(src, Number(position), Number(length)); + const dest = new Uint8Array(buffer.buffer, buffer.byteOffset + offset, slice.length); + dest.set(slice, 0); + return slice.length; + })().then((bytesWritten) => callback(null, bytesWritten, buffer), error => callback(error)); }; public readonly readFile: FsCallbackApi['readFile'] = ( diff --git a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts index 906c3fb26..9f9da28e6 100644 --- a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts +++ b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts @@ -579,3 +579,34 @@ describe('.writeFile()', () => { }); }); }); + +describe('.read()', () => { + test('can read from a file at offset into Buffer', async () => { + const { fs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }); + const handle = await fs.promises.open('/folder/file', 'r'); + const [buffer, length] = await new Promise<[Buffer, number]>((resolve, reject) => { + const buffer = Buffer.alloc(4); + fs.read(handle.fd, buffer, 0, 2, 1, (error, bytesRead, buffer) => { + if (error) reject(error); + else resolve([buffer as Buffer, bytesRead!]); + }); + }); + expect(length).toBe(2); + expect(buffer.slice(0, 2).toString()).toBe('es'); + }); + + test('can read from a file at offset into Uint8Array', async () => { + const { fs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }); + const handle = await fs.promises.open('/folder/file', 'r'); + const [buffer, length] = await new Promise<[Buffer, number]>((resolve, reject) => { + const buffer = new Uint8Array(4); + fs.read(handle.fd, buffer, 0, 2, 1, (error, bytesRead, buffer) => { + if (error) reject(error); + else resolve([buffer as Buffer, bytesRead!]); + }); + }); + expect(length).toBe(2); + expect(buffer[0]).toBe(101); + expect(buffer[1]).toBe(115); + }); +});