Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: use validateBoolean() in rm/rmdir validation #35565

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 6 additions & 13 deletions lib/internal/fs/utils.js
Expand Up @@ -35,6 +35,7 @@ const {
const { once } = require('internal/util');
const { toPathIfFileURL } = require('internal/url');
const {
validateBoolean,
validateInt32,
validateUint32
} = require('internal/validators');
Expand Down Expand Up @@ -674,15 +675,11 @@ const defaultRmdirOptions = {
const validateRmOptions = hideStackFrames((path, options, callback) => {
try {
options = validateRmdirOptions(options, defaultRmOptions);
validateBoolean(options.force, 'options.force');
} catch (err) {
return callback(err);
}

if (typeof options.force !== 'boolean')
return callback(
new ERR_INVALID_ARG_TYPE('force', 'boolean', options.force)
);

lazyLoadFs().stat(path, (err, stats) => {
if (err) {
if (options.force && err.code === 'ENOENT') {
Expand All @@ -706,9 +703,7 @@ const validateRmOptions = hideStackFrames((path, options, callback) => {

const validateRmOptionsSync = hideStackFrames((path, options) => {
options = validateRmdirOptions(options, defaultRmOptions);

if (typeof options.force !== 'boolean')
throw new ERR_INVALID_ARG_TYPE('force', 'boolean', options.force);
validateBoolean(options.force, 'options.force');

try {
const stats = lazyLoadFs().statSync(path);
Expand Down Expand Up @@ -742,11 +737,9 @@ const validateRmdirOptions = hideStackFrames(

options = { ...defaults, ...options };

if (typeof options.recursive !== 'boolean')
throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', options.recursive);

validateInt32(options.retryDelay, 'retryDelay', 0);
validateUint32(options.maxRetries, 'maxRetries');
validateBoolean(options.recursive, 'options.recursive');
validateInt32(options.retryDelay, 'options.retryDelay', 0);
validateUint32(options.maxRetries, 'options.maxRetries');

return options;
});
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-fs-rm.js
Expand Up @@ -251,7 +251,7 @@ function removeAsync(dir) {
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "recursive" argument must be of type boolean\./
message: /^The "options\.recursive" property must be of type boolean\./
});
});

Expand All @@ -261,7 +261,7 @@ function removeAsync(dir) {
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "force" argument must be of type boolean\./
message: /^The "options\.force" property must be of type boolean\./
});
});

Expand All @@ -270,14 +270,14 @@ function removeAsync(dir) {
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "retryDelay" is out of range\./
message: /^The value of "options\.retryDelay" is out of range\./
});

assert.throws(() => {
validateRmOptionsSync(filePath, { maxRetries: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "maxRetries" is out of range\./
message: /^The value of "options\.maxRetries" is out of range\./
});
}
6 changes: 3 additions & 3 deletions test/parallel/test-fs-rmdir-recursive.js
Expand Up @@ -191,7 +191,7 @@ function removeAsync(dir) {
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "recursive" argument must be of type boolean\./
message: /^The "options\.recursive" property must be of type boolean\./
});
});

Expand All @@ -200,14 +200,14 @@ function removeAsync(dir) {
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "retryDelay" is out of range\./
message: /^The value of "options\.retryDelay" is out of range\./
});

assert.throws(() => {
validateRmdirOptions({ maxRetries: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "maxRetries" is out of range\./
message: /^The value of "options\.maxRetries" is out of range\./
});
}