Skip to content
Merged
Show file tree
Hide file tree
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: allow offset === null
  • Loading branch information
LiviaMedeiros committed Apr 21, 2022
commit 42b640c6ff185c7fb565eb613c31460c4e9661aa
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
validateBuffer(buffer);
callback = maybeCallback(callback);

if (offset === undefined) {
if (offset == null) {
offset = 0;
} else {
validateInteger(offset, 'offset', 0);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
validateBuffer(buffer);
}

if (typeof offset === 'object') {
if (offset !== null && typeof offset === 'object') {
// This is fh.read(buffer, options)
({
offset = 0,
Expand All @@ -531,7 +531,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
} = offset ?? ObjectCreate(null));
}

if (offset === undefined) {
if (offset == null) {
offset = 0;
} else {
validateInteger(offset, 'offset', 0);
Expand Down
35 changes: 19 additions & 16 deletions test/parallel/test-fs-read-offset-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,40 @@ const filepath = fixtures.path('x.txt');
const buf = Buffer.alloc(1);
// Reading only one character, hence buffer of one byte is enough.

// Test is done by making sure the first letter in buffer is
// same as first letter in file.
// 120 is the ascii code of letter x.

// Test for callback API.
fs.open(filepath, 'r', common.mustSucceed((fd) => {
assert.throws(
() => fs.read(fd, { offset: null, buffer: buf }, () => {}),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
assert.throws(
() => fs.read(fd, buf, { offset: null }, () => {}),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
fs.close(fd, common.mustSucceed(() => {}));
fs.read(fd, { offset: null, buffer: buf },
common.mustSucceed((bytesRead, buffer) => {
assert.strictEqual(buffer[0], 120);
fs.close(fd, common.mustSucceed(() => {}));
}));
}));

fs.open(filepath, 'r', common.mustSucceed((fd) => {
fs.read(fd, buf, { offset: null },
common.mustSucceed((bytesRead, buffer) => {
assert.strictEqual(buffer[0], 120);
fs.close(fd, common.mustSucceed(() => {}));
}));
}));

let filehandle = null;

// Test for promise api
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
assert.rejects(
async () => filehandle.read(buf, { offset: null }),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
const readObject = await filehandle.read(buf, { offset: null }, buf.length);
assert.strictEqual(readObject.buffer[0], 120);
})()
.then(common.mustCall())
.finally(() => filehandle?.close());

(async () => {
filehandle = await fsPromises.open(filepath, 'r');

// In this test, null is interpreted as default options, not null offset.
// 120 is the ascii code of letter x.
const readObject = await filehandle.read(buf, null);
assert.strictEqual(readObject.buffer[0], 120);
})()
Expand Down