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 <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
anonrig authored Mar 19, 2024
1 parent 14de082 commit 61e5de1
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 @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
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 @@ -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;
Expand Down Expand Up @@ -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') {
({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 61e5de1

Please sign in to comment.