fs: make fs.read params optional · nodejs/node@94f3eed · GitHub
Skip to content

Commit 94f3eed

Browse files
lholmquistMylesBorins
authored andcommitted
fs: make fs.read params optional
This makes all the parameters of the `fs.read` function, except for `fd` and the callback(when not using as a promise) optional. They will default to sensible defaults. Fixes: #31237 PR-URL: #31402 Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 35d0569 commit 94f3eed

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

doc/api/fs.md

Lines changed: 34 additions & 0 deletions

lib/fs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,34 @@ function openSync(path, flags, mode) {
463463
return result;
464464
}
465465

466+
// usage:
467+
// fs.read(fd, buffer, offset, length, position, callback);
468+
// OR
469+
// fs.read(fd, {}, callback)
466470
function read(fd, buffer, offset, length, position, callback) {
467471
validateInt32(fd, 'fd', 0);
472+
473+
if (arguments.length <= 3) {
474+
// Assume fs.read(fd, options, callback)
475+
let options = {};
476+
if (arguments.length < 3) {
477+
// This is fs.read(fd, callback)
478+
// buffer will be the callback
479+
callback = buffer;
480+
} else {
481+
// This is fs.read(fd, {}, callback)
482+
// buffer will be the options object
483+
// offset is the callback
484+
options = buffer;
485+
callback = offset;
486+
}
487+
488+
buffer = options.buffer || Buffer.alloc(16384);
489+
offset = options.offset || 0;
490+
length = options.length || buffer.length;
491+
position = options.position;
492+
}
493+
468494
validateBuffer(buffer);
469495
callback = maybeCallback(callback);
470496

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const fs = require('fs');
6+
const assert = require('assert');
7+
const filepath = fixtures.path('x.txt');
8+
const fd = fs.openSync(filepath, 'r');
9+
10+
const expected = Buffer.from('xyz\n');
11+
const defaultBufferAsync = Buffer.alloc(16384);
12+
const bufferAsOption = Buffer.allocUnsafe(expected.length);
13+
14+
// Test passing in an empty options object
15+
fs.read(fd, { position: 0 }, common.mustCall((err, bytesRead, buffer) => {
16+
assert.strictEqual(bytesRead, expected.length);
17+
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
18+
}));
19+
20+
// Test not passing in any options object
21+
fs.read(fd, common.mustCall((err, bytesRead, buffer) => {
22+
assert.strictEqual(bytesRead, expected.length);
23+
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
24+
}));
25+
26+
// Test passing in options
27+
fs.read(fd, {
28+
buffer: bufferAsOption,
29+
offset: 0,
30+
lenght: bufferAsOption.length,
31+
position: 0
32+
},
33+
common.mustCall((err, bytesRead, buffer) => {
34+
assert.strictEqual(bytesRead, expected.length);
35+
assert.deepStrictEqual(bufferAsOption.length, buffer.length);
36+
}));
Lines changed: 19 additions & 0 deletions

0 commit comments

Comments
 (0)