Skip to content

Commit c69abaa

Browse files
committed
fs: add read(buffer, options) signatures
This adds the following: `fs.read(fd, buffer[, options], callback)` `filehandle.read(buffer[, options])`
1 parent c3a581c commit c69abaa

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

lib/fs.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -605,21 +605,30 @@ function openSync(path, flags, mode) {
605605
* ) => any} callback
606606
* @returns {void}
607607
*/
608-
function read(fd, buffer, offset, length, position, callback) {
608+
function read(fd, buffer, offsetOrOptions, length, position, callback) {
609609
fd = getValidatedFd(fd);
610610

611-
if (arguments.length <= 3) {
612-
// Assume fs.read(fd, options, callback)
613-
let options = ObjectCreate(null);
611+
let offset = offsetOrOptions;
612+
if (arguments.length === 4 && typeof offsetOrOptions === 'object') {
613+
// This is fs.read(fd, buffer, options, callback)
614+
callback = length;
615+
({
616+
offset = 0,
617+
length = buffer.byteLength - offset,
618+
position = null
619+
} = offsetOrOptions ?? ObjectCreate(null));
620+
} else if (arguments.length <= 3) {
621+
// Assume fs.read(fd, params, callback)
622+
let params = ObjectCreate(null);
614623
if (arguments.length < 3) {
615624
// This is fs.read(fd, callback)
616625
// buffer will be the callback
617626
callback = buffer;
618627
} else {
619628
// This is fs.read(fd, {}, callback)
620-
// buffer will be the options object
629+
// buffer will be the params object
621630
// offset is the callback
622-
options = buffer;
631+
params = buffer;
623632
callback = offset;
624633
}
625634

@@ -628,7 +637,7 @@ function read(fd, buffer, offset, length, position, callback) {
628637
offset = 0,
629638
length = buffer.byteLength - offset,
630639
position = null
631-
} = options);
640+
} = params);
632641
}
633642

634643
validateBuffer(buffer);
@@ -686,23 +695,21 @@ ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
686695
* }} [offset]
687696
* @returns {number}
688697
*/
689-
function readSync(fd, buffer, offset, length, position) {
698+
function readSync(fd, buffer, offsetOrOptions, length, position) {
690699
fd = getValidatedFd(fd);
691700

692701
validateBuffer(buffer);
693702

694-
if (arguments.length <= 3) {
695-
// Assume fs.readSync(fd, buffer, options)
696-
const options = offset || ObjectCreate(null);
697-
703+
let offset = offsetOrOptions;
704+
if (typeof offset === 'object') {
698705
({
699706
offset = 0,
700707
length = buffer.byteLength - offset,
701708
position = null
702-
} = options);
709+
} = offsetOrOptions ?? ObjectCreate(null));
703710
}
704711

705-
if (offset == null) {
712+
if (offset === false || offset === undefined) {
706713
offset = 0;
707714
} else {
708715
validateInteger(offset, 'offset', 0);

lib/internal/fs/promises.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,21 +508,31 @@ async function open(path, flags, mode) {
508508
flagsNumber, mode, kUsePromises));
509509
}
510510

511-
async function read(handle, bufferOrOptions, offset, length, position) {
512-
let buffer = bufferOrOptions;
511+
async function read(handle, bufferOrParams, offset, length, position) {
512+
let buffer = bufferOrParams;
513513
if (!isArrayBufferView(buffer)) {
514-
bufferOrOptions ??= ObjectCreate(null);
514+
// This is fh.read(params)
515+
bufferOrParams ??= ObjectCreate(null);
515516
({
516517
buffer = Buffer.alloc(16384),
517518
offset = 0,
518519
length = buffer.byteLength - offset,
519520
position = null
520-
} = bufferOrOptions);
521+
} = bufferOrParams);
521522

522523
validateBuffer(buffer);
523524
}
524525

525-
if (offset == null) {
526+
if (typeof offset === 'object') {
527+
// This is fh.read(buffer, options)
528+
({
529+
offset = 0,
530+
length = buffer.byteLength - offset,
531+
position = null
532+
} = offset ?? ObjectCreate(null));
533+
}
534+
535+
if (offset === false || offset === undefined) {
526536
offset = 0;
527537
} else {
528538
validateInteger(offset, 'offset', 0);

test/parallel/test-fs-readSync-optional-params.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,16 @@ for (const options of [
3131
{ length: expected.length, position: 0 },
3232
{ offset: 0, length: expected.length, position: 0 },
3333

34-
{ offset: null },
34+
{ offset: false },
3535
{ position: null },
3636
{ position: -1 },
3737
{ position: 0n },
3838

3939
// Test default params
4040
{},
4141
null,
42-
undefined,
43-
44-
// Test if bad params are interpreted as default (not mandatory)
45-
false,
46-
true,
47-
Infinity,
48-
42n,
49-
Symbol(),
5042

5143
// Test even more malicious corner cases
52-
'4'.repeat(expected.length),
53-
new String('4444'),
5444
[4, 4, 4, 4],
5545
]) {
5646
runTest(Buffer.allocUnsafe(expected.length), options);

0 commit comments

Comments
 (0)