0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2018-08-06 01:17:25 +02:00
parent 85bfd71312
commit 8e1b6e7718
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 11 additions and 3 deletions

View File

@ -445,13 +445,14 @@ function openSync(path, flags, mode) {
function read(fd, buffer, offset, length, position, callback) {
validateUint32(fd, 'fd');
validateBuffer(buffer);
callback = maybeCallback(callback);
offset |= 0;
length |= 0;
if (length === 0) {
return process.nextTick(function tick() {
callback && callback(null, 0, buffer);
callback(null, 0, buffer);
});
}
@ -467,7 +468,7 @@ function read(fd, buffer, offset, length, position, callback) {
function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback && callback(err, bytesRead || 0, buffer);
callback(err, bytesRead || 0, buffer);
}
const req = new FSReqCallback();

View File

@ -58,7 +58,6 @@ test(new Uint8Array(expected.length),
// Reading beyond file length (3 in this case) should return no data.
// This is a test for a bug where reads > uint32 would return data
// from the current position in the file.
const fd = fs.openSync(filepath, 'r');
const pos = 0xffffffff + 1; // max-uint32 + 1
const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos);
assert.strictEqual(nRead, 0);
@ -68,3 +67,11 @@ test(new Uint8Array(expected.length),
assert.strictEqual(nRead, 0);
}));
}
assert.throws(
() => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
{
message: 'Callback must be a function',
code: 'ERR_INVALID_CALLBACK',
}
);