Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
squash: fix fs.read()
  • Loading branch information
LiviaMedeiros committed Apr 17, 2022
commit 71f03ce69ebe2b4bc9e98d449db120e8153af504
40 changes: 19 additions & 21 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const {
validateEncoding,
validateFunction,
validateInteger,
validateObject,
validateString,
} = require('internal/validators');

Expand Down Expand Up @@ -609,35 +610,32 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
fd = getValidatedFd(fd);

let offset = offsetOrOptions;
if (
arguments.length === 4 &&
(offsetOrOptions === undefined || typeof offsetOrOptions === 'object')
) {
// This is fs.read(fd, buffer, options, callback)
callback = length;
({
offset = 0,
length = buffer.byteLength - offset,
position = null
} = offsetOrOptions ?? ObjectCreate(null));
} else if (arguments.length <= 3) {
// Assume fs.read(fd[, params], callback)
let params = ObjectCreate(null);
if (arguments.length < 3) {
let params = null;
if (arguments.length <= 4) {
if (arguments.length === 4) {
// This is fs.read(fd, buffer, options, callback)
validateObject(offsetOrOptions, 'options', { nullable: true });
callback = length;
params = offsetOrOptions;
} else if (arguments.length === 3) {
// This is fs.read(fd, bufferOrParams, callback)
if (!isArrayBufferView(buffer)) {
// This is fs.read(fd, params, callback)
params = buffer;
({ buffer = Buffer.alloc(16384) } = params ?? ObjectCreate(null));
}
callback = offsetOrOptions;
} else {
// This is fs.read(fd, callback)
callback = buffer;
} else {
// This is fs.read(fd, params, callback)
params = buffer;
callback = offset;
buffer = Buffer.alloc(16384);
}

({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.byteLength - offset,
position = null
} = params);
} = params ?? ObjectCreate(null));
}

validateBuffer(buffer);
Expand Down