Skip to content

Commit 61e5de1

Browse files
authored
fs: refactor maybeCallback function
PR-URL: #52129 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 14de082 commit 61e5de1

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

lib/fs.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,6 @@ function showTruncateDeprecation() {
179179
}
180180
}
181181

182-
function maybeCallback(cb) {
183-
validateFunction(cb, 'cb');
184-
185-
return cb;
186-
}
187-
188182
// Ensure that callbacks run in the global context. Only use this function
189183
// for callbacks that are passed to the binding layer, callbacks that are
190184
// invoked from JS already run in the proper scope.
@@ -258,7 +252,7 @@ function accessSync(path, mode) {
258252
* @returns {void}
259253
*/
260254
function exists(path, callback) {
261-
maybeCallback(callback);
255+
validateFunction(callback, 'cb');
262256

263257
function suppressedCallback(err) {
264258
callback(err ? false : true);
@@ -368,7 +362,8 @@ function checkAborted(signal, callback) {
368362
* @returns {void}
369363
*/
370364
function readFile(path, options, callback) {
371-
callback = maybeCallback(callback || options);
365+
callback ||= options;
366+
validateFunction(callback, 'cb');
372367
options = getOptions(options, { flag: 'r' });
373368
const ReadFileContext = require('internal/fs/read/context');
374369
const context = new ReadFileContext(callback, options.encoding);
@@ -650,7 +645,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
650645
}
651646

652647
validateBuffer(buffer);
653-
callback = maybeCallback(callback);
648+
validateFunction(callback, 'cb');
654649

655650
if (offset == null) {
656651
offset = 0;
@@ -773,7 +768,8 @@ function readv(fd, buffers, position, callback) {
773768

774769
fd = getValidatedFd(fd);
775770
validateBufferArray(buffers);
776-
callback = maybeCallback(callback || position);
771+
callback ||= position;
772+
validateFunction(callback, 'cb');
777773

778774
const req = new FSReqCallback();
779775
req.oncomplete = wrapper;
@@ -830,7 +826,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
830826

831827
let offset = offsetOrOptions;
832828
if (isArrayBufferView(buffer)) {
833-
callback = maybeCallback(callback || position || length || offset);
829+
callback ||= position || length || offset;
830+
validateFunction(callback, 'cb');
834831

835832
if (typeof offset === 'object') {
836833
({
@@ -871,7 +868,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
871868

872869
const str = buffer;
873870
validateEncoding(str, length);
874-
callback = maybeCallback(position);
871+
callback = position;
872+
validateFunction(callback, 'cb');
875873

876874
const req = new FSReqCallback();
877875
req.oncomplete = wrapper;
@@ -952,7 +950,8 @@ function writev(fd, buffers, position, callback) {
952950

953951
fd = getValidatedFd(fd);
954952
validateBufferArray(buffers);
955-
callback = maybeCallback(callback || position);
953+
callback ||= position;
954+
validateFunction(callback, 'cb');
956955

957956
if (buffers.length === 0) {
958957
process.nextTick(callback, null, 0, buffers);
@@ -1053,7 +1052,7 @@ function truncate(path, len, callback) {
10531052

10541053
validateInteger(len, 'len');
10551054
len = MathMax(0, len);
1056-
callback = maybeCallback(callback);
1055+
validateFunction(callback, 'cb');
10571056
fs.open(path, 'r+', (er, fd) => {
10581057
if (er) return callback(er);
10591058
const req = new FSReqCallback();
@@ -1587,7 +1586,7 @@ function statfs(path, options = { bigint: false }, callback) {
15871586
callback = options;
15881587
options = kEmptyObject;
15891588
}
1590-
callback = maybeCallback(callback);
1589+
validateFunction(callback, 'cb');
15911590
path = getValidatedPath(path);
15921591
const req = new FSReqCallback(options.bigint);
15931592
req.oncomplete = (err, stats) => {
@@ -1909,7 +1908,7 @@ function fchmodSync(fd, mode) {
19091908
* @returns {void}
19101909
*/
19111910
function lchmod(path, mode, callback) {
1912-
callback = maybeCallback(callback);
1911+
validateFunction(callback, 'cb');
19131912
mode = parseFileMode(mode, 'mode');
19141913
fs.open(path, O_WRONLY | O_SYMLINK, (err, fd) => {
19151914
if (err) {
@@ -2267,7 +2266,8 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback)
22672266
* @returns {void}
22682267
*/
22692268
function writeFile(path, data, options, callback) {
2270-
callback = maybeCallback(callback || options);
2269+
callback ||= options;
2270+
validateFunction(callback, 'cb');
22712271
options = getOptions(options, {
22722272
encoding: 'utf8',
22732273
mode: 0o666,
@@ -2383,7 +2383,8 @@ function writeFileSync(path, data, options) {
23832383
* @returns {void}
23842384
*/
23852385
function appendFile(path, data, options, callback) {
2386-
callback = maybeCallback(callback || options);
2386+
callback ||= options;
2387+
validateFunction(callback, 'cb');
23872388
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' });
23882389

23892390
// Don't make changes directly on options object
@@ -2778,7 +2779,11 @@ realpathSync.native = (path, options) => {
27782779
* @returns {void}
27792780
*/
27802781
function realpath(p, options, callback) {
2781-
callback = typeof options === 'function' ? options : maybeCallback(callback);
2782+
if (typeof options === 'function') {
2783+
callback = options;
2784+
} else {
2785+
validateFunction(callback, 'cb');
2786+
}
27822787
options = getOptions(options);
27832788
p = toPathIfFileURL(p);
27842789

0 commit comments

Comments
 (0)