Skip to content

Commit

Permalink
fs: refactor maybeCallback function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
anonrig authored and marco-ippolito committed May 3, 2024
1 parent d5d6e04 commit c5fd193
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,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.
Expand Down Expand Up @@ -257,7 +251,7 @@ function accessSync(path, mode) {
* @returns {void}
*/
function exists(path, callback) {
maybeCallback(callback);
validateFunction(callback, 'cb');

function suppressedCallback(err) {
callback(err ? false : true);
Expand Down Expand Up @@ -367,7 +361,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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -771,7 +766,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;
Expand Down Expand Up @@ -828,7 +824,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') {
({
Expand Down Expand Up @@ -869,7 +866,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;
Expand Down Expand Up @@ -950,7 +948,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);
Expand Down Expand Up @@ -1051,7 +1050,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();
Expand Down Expand Up @@ -1585,7 +1584,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) => {
Expand Down Expand Up @@ -1907,7 +1906,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) {
Expand Down Expand Up @@ -2265,7 +2264,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,
Expand Down Expand Up @@ -2381,7 +2381,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
Expand Down Expand Up @@ -2776,7 +2777,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);

Expand Down

0 comments on commit c5fd193

Please sign in to comment.