diff options
author | Nikola Glavina <glavina.nikola5@gmail.com> | 2020-11-14 01:53:49 +0100 |
---|---|---|
committer | Beth Griggs <bgriggs@redhat.com> | 2020-12-15 20:15:23 +0000 |
commit | 8d84bdc46b65379a3546d7a70fefa46091c2ff91 (patch) | |
tree | d99c2b6f0efb43ccd861a581499aa9dcb623833b | |
parent | 304e26900105199aad97df1a0ad8c9333e5a180b (diff) | |
download | node-new-8d84bdc46b65379a3546d7a70fefa46091c2ff91.tar.gz |
fs: filehandle read now accepts object as argument
PR-URL: https://github.com/nodejs/node/pull/34180
Fixes: https://github.com/nodejs/node/issues/34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
-rw-r--r-- | lib/internal/fs/promises.js | 15 | ||||
-rw-r--r-- | test/parallel/test-fs-promises-file-handle-read.js | 29 |
2 files changed, 34 insertions, 10 deletions
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index bec0b5ebbc..1f81a6f47b 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -322,8 +322,19 @@ async function open(path, flags, mode) { flagsNumber, mode, kUsePromises)); } -async function read(handle, buffer, offset, length, position) { - validateBuffer(buffer); +async function read(handle, bufferOrOptions, offset, length, position) { + let buffer = bufferOrOptions; + if (!isArrayBufferView(buffer)) { + if (bufferOrOptions.buffer) { + buffer = bufferOrOptions.buffer; + validateBuffer(buffer); + } else { + buffer = Buffer.alloc(16384); + } + offset = bufferOrOptions.offset || 0; + length = buffer.length; + position = bufferOrOptions.position || null; + } if (offset == null) { offset = 0; diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index 5ba7fc63e3..44c35840f2 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -13,7 +13,11 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -tmpdir.refresh(); +async function read(fileHandle, buffer, offset, length, position) { + return useConf ? + fileHandle.read({ buffer, offset, length, position }) : + fileHandle.read(buffer, offset, length, position); +} async function validateRead() { const filePath = path.resolve(tmpDir, 'tmp-read-file.txt'); @@ -23,7 +27,7 @@ async function validateRead() { const fd = fs.openSync(filePath, 'w+'); fs.writeSync(fd, buffer, 0, buffer.length); fs.closeSync(fd); - const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0); + const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0); assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead); assert.deepStrictEqual(buffer, readAsyncHandle.buffer); @@ -38,7 +42,7 @@ async function validateEmptyRead() { const fd = fs.openSync(filePath, 'w+'); fs.writeSync(fd, buffer, 0, buffer.length); fs.closeSync(fd); - const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0); + const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0); assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead); await fileHandle.close(); @@ -51,12 +55,21 @@ async function validateLargeRead() { const filePath = fixtures.path('x.txt'); const fileHandle = await open(filePath, 'r'); const pos = 0xffffffff + 1; // max-uint32 + 1 - const readHandle = await fileHandle.read(Buffer.alloc(1), 0, 1, pos); + const readHandle = await read(fileHandle, Buffer.alloc(1), 0, 1, pos); assert.strictEqual(readHandle.bytesRead, 0); } -validateRead() - .then(validateEmptyRead) - .then(validateLargeRead) - .then(common.mustCall()); +let useConf = false; + +(async function() { + for (const value of [false, true]) { + tmpdir.refresh(); + useConf = value; + + await validateRead() + .then(validateEmptyRead) + .then(validateLargeRead) + .then(common.mustCall()); + } +}); |