Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 76b1863

Browse filesBrowse files
branishacodebytere
authored andcommitted
fs: filehandle read now accepts object as argument
PR-URL: #34180 Fixes: #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>
1 parent 60874ba commit 76b1863
Copy full SHA for 76b1863

File tree

Expand file treeCollapse file tree

2 files changed

+34
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-10
lines changed
Open diff view settings
Collapse file

‎lib/internal/fs/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,19 @@ async function open(path, flags, mode) {
346346
flagsNumber, mode, kUsePromises));
347347
}
348348

349-
async function read(handle, buffer, offset, length, position) {
350-
validateBuffer(buffer);
349+
async function read(handle, bufferOrOptions, offset, length, position) {
350+
let buffer = bufferOrOptions;
351+
if (!isArrayBufferView(buffer)) {
352+
if (bufferOrOptions.buffer) {
353+
buffer = bufferOrOptions.buffer;
354+
validateBuffer(buffer);
355+
} else {
356+
buffer = Buffer.alloc(16384);
357+
}
358+
offset = bufferOrOptions.offset || 0;
359+
length = buffer.length;
360+
position = bufferOrOptions.position || null;
361+
}
351362

352363
if (offset == null) {
353364
offset = 0;
Collapse file

‎test/parallel/test-fs-promises-file-handle-read.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises-file-handle-read.js
+21-8Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const tmpdir = require('../common/tmpdir');
1313
const assert = require('assert');
1414
const tmpDir = tmpdir.path;
1515

16-
tmpdir.refresh();
16+
async function read(fileHandle, buffer, offset, length, position) {
17+
return useConf ?
18+
fileHandle.read({ buffer, offset, length, position }) :
19+
fileHandle.read(buffer, offset, length, position);
20+
}
1721

1822
async function validateRead() {
1923
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
@@ -23,7 +27,7 @@ async function validateRead() {
2327
const fd = fs.openSync(filePath, 'w+');
2428
fs.writeSync(fd, buffer, 0, buffer.length);
2529
fs.closeSync(fd);
26-
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
30+
const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
2731
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
2832
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
2933

@@ -38,7 +42,7 @@ async function validateEmptyRead() {
3842
const fd = fs.openSync(filePath, 'w+');
3943
fs.writeSync(fd, buffer, 0, buffer.length);
4044
fs.closeSync(fd);
41-
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
45+
const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
4246
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
4347

4448
await fileHandle.close();
@@ -51,12 +55,21 @@ async function validateLargeRead() {
5155
const filePath = fixtures.path('x.txt');
5256
const fileHandle = await open(filePath, 'r');
5357
const pos = 0xffffffff + 1; // max-uint32 + 1
54-
const readHandle = await fileHandle.read(Buffer.alloc(1), 0, 1, pos);
58+
const readHandle = await read(fileHandle, Buffer.alloc(1), 0, 1, pos);
5559

5660
assert.strictEqual(readHandle.bytesRead, 0);
5761
}
5862

59-
validateRead()
60-
.then(validateEmptyRead)
61-
.then(validateLargeRead)
62-
.then(common.mustCall());
63+
let useConf = false;
64+
65+
(async function() {
66+
for (const value of [false, true]) {
67+
tmpdir.refresh();
68+
useConf = value;
69+
70+
await validateRead()
71+
.then(validateEmptyRead)
72+
.then(validateLargeRead)
73+
.then(common.mustCall());
74+
}
75+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.