Skip to content

Commit 8e1b6e7

Browse files
committed
fs: require callback in read
Currently the read operation did not require the callback and that was an oversight when callbacks became mandatory. PR-URL: #22146 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 85bfd71 commit 8e1b6e7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/fs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,14 @@ function openSync(path, flags, mode) {
445445
function read(fd, buffer, offset, length, position, callback) {
446446
validateUint32(fd, 'fd');
447447
validateBuffer(buffer);
448+
callback = maybeCallback(callback);
448449

449450
offset |= 0;
450451
length |= 0;
451452

452453
if (length === 0) {
453454
return process.nextTick(function tick() {
454-
callback && callback(null, 0, buffer);
455+
callback(null, 0, buffer);
455456
});
456457
}
457458

@@ -467,7 +468,7 @@ function read(fd, buffer, offset, length, position, callback) {
467468

468469
function wrapper(err, bytesRead) {
469470
// Retain a reference to buffer so that it can't be GC'ed too soon.
470-
callback && callback(err, bytesRead || 0, buffer);
471+
callback(err, bytesRead || 0, buffer);
471472
}
472473

473474
const req = new FSReqCallback();

test/parallel/test-fs-read.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ test(new Uint8Array(expected.length),
5858
// Reading beyond file length (3 in this case) should return no data.
5959
// This is a test for a bug where reads > uint32 would return data
6060
// from the current position in the file.
61-
const fd = fs.openSync(filepath, 'r');
6261
const pos = 0xffffffff + 1; // max-uint32 + 1
6362
const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos);
6463
assert.strictEqual(nRead, 0);
@@ -68,3 +67,11 @@ test(new Uint8Array(expected.length),
6867
assert.strictEqual(nRead, 0);
6968
}));
7069
}
70+
71+
assert.throws(
72+
() => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
73+
{
74+
message: 'Callback must be a function',
75+
code: 'ERR_INVALID_CALLBACK',
76+
}
77+
);

0 commit comments

Comments
 (0)