From 61e5de12689dcaa168f5348f5529e0642663ceae Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 19 Mar 2024 14:32:16 -0400 Subject: [PATCH] fs: refactor maybeCallback function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/52129 Reviewed-By: Michaƫl Zasso Reviewed-By: Paolo Insogna Reviewed-By: Luigi Pinca --- lib/fs.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 815018a855cde8..83c074bd579709 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -179,12 +179,6 @@ function showTruncateDeprecation() { } } -function maybeCallback(cb) { - validateFunction(cb, 'cb'); - - return cb; -} - // Ensure that callbacks run in the global context. Only use this function // for callbacks that are passed to the binding layer, callbacks that are // invoked from JS already run in the proper scope. @@ -258,7 +252,7 @@ function accessSync(path, mode) { * @returns {void} */ function exists(path, callback) { - maybeCallback(callback); + validateFunction(callback, 'cb'); function suppressedCallback(err) { callback(err ? false : true); @@ -368,7 +362,8 @@ function checkAborted(signal, callback) { * @returns {void} */ function readFile(path, options, callback) { - callback = maybeCallback(callback || options); + callback ||= options; + validateFunction(callback, 'cb'); options = getOptions(options, { flag: 'r' }); const ReadFileContext = require('internal/fs/read/context'); const context = new ReadFileContext(callback, options.encoding); @@ -650,7 +645,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { } validateBuffer(buffer); - callback = maybeCallback(callback); + validateFunction(callback, 'cb'); if (offset == null) { offset = 0; @@ -773,7 +768,8 @@ function readv(fd, buffers, position, callback) { fd = getValidatedFd(fd); validateBufferArray(buffers); - callback = maybeCallback(callback || position); + callback ||= position; + validateFunction(callback, 'cb'); const req = new FSReqCallback(); req.oncomplete = wrapper; @@ -830,7 +826,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) { let offset = offsetOrOptions; if (isArrayBufferView(buffer)) { - callback = maybeCallback(callback || position || length || offset); + callback ||= position || length || offset; + validateFunction(callback, 'cb'); if (typeof offset === 'object') { ({ @@ -871,7 +868,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) { const str = buffer; validateEncoding(str, length); - callback = maybeCallback(position); + callback = position; + validateFunction(callback, 'cb'); const req = new FSReqCallback(); req.oncomplete = wrapper; @@ -952,7 +950,8 @@ function writev(fd, buffers, position, callback) { fd = getValidatedFd(fd); validateBufferArray(buffers); - callback = maybeCallback(callback || position); + callback ||= position; + validateFunction(callback, 'cb'); if (buffers.length === 0) { process.nextTick(callback, null, 0, buffers); @@ -1053,7 +1052,7 @@ function truncate(path, len, callback) { validateInteger(len, 'len'); len = MathMax(0, len); - callback = maybeCallback(callback); + validateFunction(callback, 'cb'); fs.open(path, 'r+', (er, fd) => { if (er) return callback(er); const req = new FSReqCallback(); @@ -1587,7 +1586,7 @@ function statfs(path, options = { bigint: false }, callback) { callback = options; options = kEmptyObject; } - callback = maybeCallback(callback); + validateFunction(callback, 'cb'); path = getValidatedPath(path); const req = new FSReqCallback(options.bigint); req.oncomplete = (err, stats) => { @@ -1909,7 +1908,7 @@ function fchmodSync(fd, mode) { * @returns {void} */ function lchmod(path, mode, callback) { - callback = maybeCallback(callback); + validateFunction(callback, 'cb'); mode = parseFileMode(mode, 'mode'); fs.open(path, O_WRONLY | O_SYMLINK, (err, fd) => { if (err) { @@ -2267,7 +2266,8 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback) * @returns {void} */ function writeFile(path, data, options, callback) { - callback = maybeCallback(callback || options); + callback ||= options; + validateFunction(callback, 'cb'); options = getOptions(options, { encoding: 'utf8', mode: 0o666, @@ -2383,7 +2383,8 @@ function writeFileSync(path, data, options) { * @returns {void} */ function appendFile(path, data, options, callback) { - callback = maybeCallback(callback || options); + callback ||= options; + validateFunction(callback, 'cb'); options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' }); // Don't make changes directly on options object @@ -2778,7 +2779,11 @@ realpathSync.native = (path, options) => { * @returns {void} */ function realpath(p, options, callback) { - callback = typeof options === 'function' ? options : maybeCallback(callback); + if (typeof options === 'function') { + callback = options; + } else { + validateFunction(callback, 'cb'); + } options = getOptions(options); p = toPathIfFileURL(p);