Skip to content

Commit c5fd193

Browse files
anonrigmarco-ippolito
authored andcommitted
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 d5d6e04 commit c5fd193

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
@@ -178,12 +178,6 @@ function showTruncateDeprecation() {
178178
}
179179
}
180180

181-
function maybeCallback(cb) {
182-
validateFunction(cb, 'cb');
183-
184-
return cb;
185-
}
186-
187181
// Ensure that callbacks run in the global context. Only use this function
188182
// for callbacks that are passed to the binding layer, callbacks that are
189183
// invoked from JS already run in the proper scope.
@@ -257,7 +251,7 @@ function accessSync(path, mode) {
257251
* @returns {void}
258252
*/
259253
function exists(path, callback) {
260-
maybeCallback(callback);
254+
validateFunction(callback, 'cb');
261255

262256
function suppressedCallback(err) {
263257
callback(err ? false : true);
@@ -367,7 +361,8 @@ function checkAborted(signal, callback) {
367361
* @returns {void}
368362
*/
369363
function readFile(path, options, callback) {
370-
callback = maybeCallback(callback || options);
364+
callback ||= options;
365+
validateFunction(callback, 'cb');
371366
options = getOptions(options, { flag: 'r' });
372367
const ReadFileContext = require('internal/fs/read/context');
373368
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;
@@ -771,7 +766,8 @@ function readv(fd, buffers, position, callback) {
771766

772767
fd = getValidatedFd(fd);
773768
validateBufferArray(buffers);
774-
callback = maybeCallback(callback || position);
769+
callback ||= position;
770+
validateFunction(callback, 'cb');
775771

776772
const req = new FSReqCallback();
777773
req.oncomplete = wrapper;
@@ -828,7 +824,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
828824

829825
let offset = offsetOrOptions;
830826
if (isArrayBufferView(buffer)) {
831-
callback = maybeCallback(callback || position || length || offset);
827+
callback ||= position || length || offset;
828+
validateFunction(callback, 'cb');
832829

833830
if (typeof offset === 'object') {
834831
({
@@ -869,7 +866,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
869866

870867
const str = buffer;
871868
validateEncoding(str, length);
872-
callback = maybeCallback(position);
869+
callback = position;
870+
validateFunction(callback, 'cb');
873871

874872
const req = new FSReqCallback();
875873
req.oncomplete = wrapper;
@@ -950,7 +948,8 @@ function writev(fd, buffers, position, callback) {
950948

951949
fd = getValidatedFd(fd);
952950
validateBufferArray(buffers);
953-
callback = maybeCallback(callback || position);
951+
callback ||= position;
952+
validateFunction(callback, 'cb');
954953

955954
if (buffers.length === 0) {
956955
process.nextTick(callback, null, 0, buffers);
@@ -1051,7 +1050,7 @@ function truncate(path, len, callback) {
10511050

10521051
validateInteger(len, 'len');
10531052
len = MathMax(0, len);
1054-
callback = maybeCallback(callback);
1053+
validateFunction(callback, 'cb');
10551054
fs.open(path, 'r+', (er, fd) => {
10561055
if (er) return callback(er);
10571056
const req = new FSReqCallback();
@@ -1585,7 +1584,7 @@ function statfs(path, options = { bigint: false }, callback) {
15851584
callback = options;
15861585
options = kEmptyObject;
15871586
}
1588-
callback = maybeCallback(callback);
1587+
validateFunction(callback, 'cb');
15891588
path = getValidatedPath(path);
15901589
const req = new FSReqCallback(options.bigint);
15911590
req.oncomplete = (err, stats) => {
@@ -1907,7 +1906,7 @@ function fchmodSync(fd, mode) {
19071906
* @returns {void}
19081907
*/
19091908
function lchmod(path, mode, callback) {
1910-
callback = maybeCallback(callback);
1909+
validateFunction(callback, 'cb');
19111910
mode = parseFileMode(mode, 'mode');
19121911
fs.open(path, O_WRONLY | O_SYMLINK, (err, fd) => {
19131912
if (err) {
@@ -2265,7 +2264,8 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback)
22652264
* @returns {void}
22662265
*/
22672266
function writeFile(path, data, options, callback) {
2268-
callback = maybeCallback(callback || options);
2267+
callback ||= options;
2268+
validateFunction(callback, 'cb');
22692269
options = getOptions(options, {
22702270
encoding: 'utf8',
22712271
mode: 0o666,
@@ -2381,7 +2381,8 @@ function writeFileSync(path, data, options) {
23812381
* @returns {void}
23822382
*/
23832383
function appendFile(path, data, options, callback) {
2384-
callback = maybeCallback(callback || options);
2384+
callback ||= options;
2385+
validateFunction(callback, 'cb');
23852386
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' });
23862387

23872388
// Don't make changes directly on options object
@@ -2776,7 +2777,11 @@ realpathSync.native = (path, options) => {
27762777
* @returns {void}
27772778
*/
27782779
function realpath(p, options, callback) {
2779-
callback = typeof options === 'function' ? options : maybeCallback(callback);
2780+
if (typeof options === 'function') {
2781+
callback = options;
2782+
} else {
2783+
validateFunction(callback, 'cb');
2784+
}
27802785
options = getOptions(options);
27812786
p = toPathIfFileURL(p);
27822787

0 commit comments

Comments
 (0)