GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: filehandle read now accepts object as argument · nodejs/node@8d84bdc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/fs
    • promises.js
  • test/parallel
    • test-fs-promises-file-handle-read.js
Expand file treeCollapse file tree
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
@@ -322,8 +322,19 @@ async function open(path, flags, mode) {
322322
flagsNumber, mode, kUsePromises));
323323
}
324324

325-
async function read(handle, buffer, offset, length, position) {
326-
validateBuffer(buffer);
325+
async function read(handle, bufferOrOptions, offset, length, position) {
326+
let buffer = bufferOrOptions;
327+
if (!isArrayBufferView(buffer)) {
328+
if (bufferOrOptions.buffer) {
329+
buffer = bufferOrOptions.buffer;
330+
validateBuffer(buffer);
331+
} else {
332+
buffer = Buffer.alloc(16384);
333+
}
334+
offset = bufferOrOptions.offset || 0;
335+
length = buffer.length;
336+
position = bufferOrOptions.position || null;
337+
}
327338

328339
if (offset == null) {
329340
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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.