Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: Adjust default length for fs.readSync and fsPromises/read #42128

Merged
merged 6 commits into from Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/fs.md
Expand Up @@ -5396,7 +5396,7 @@ changes:
* `buffer` {Buffer|TypedArray|DataView}
* `options` {Object}
* `offset` {integer} **Default:** `0`
* `length` {integer} **Default:** `buffer.byteLength`
* `length` {integer} **Default:** `buffer.byteLength - offset`
* `position` {integer|bigint} **Default:** `null`
* Returns: {number}

Expand Down
8 changes: 6 additions & 2 deletions lib/fs.js
Expand Up @@ -686,10 +686,14 @@ function readSync(fd, buffer, offset, length, position) {
validateBuffer(buffer);

if (arguments.length <= 3) {
// Assume fs.read(fd, buffer, options)
// Assume fs.readSync(fd, buffer, options)
const options = offset || {};

({ offset = 0, length = buffer.byteLength, position } = options);
({
offset = 0,
length = buffer.byteLength - offset,
position = null
} = options);
}

if (offset == null) {
Expand Down
21 changes: 9 additions & 12 deletions lib/internal/fs/promises.js
Expand Up @@ -510,18 +510,15 @@ async function open(path, flags, mode) {
async function read(handle, bufferOrOptions, offset, length, position) {
let buffer = bufferOrOptions;
if (!isArrayBufferView(buffer)) {
if (bufferOrOptions === undefined) {
bufferOrOptions = {};
}
if (bufferOrOptions.buffer) {
buffer = bufferOrOptions.buffer;
validateBuffer(buffer);
} else {
buffer = Buffer.alloc(16384);
}
offset = bufferOrOptions.offset || 0;
length = bufferOrOptions.length ?? buffer.byteLength;
position = bufferOrOptions.position ?? null;
bufferOrOptions ??= {};
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved
({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.byteLength - offset,
position = null
} = bufferOrOptions);
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved

validateBuffer(buffer);
}

if (offset == null) {
Expand Down