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

lib: refactor validators #26809

Closed
wants to merge 9 commits into from
6 changes: 3 additions & 3 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const {
ERR_INVALID_PROTOCOL,
ERR_UNESCAPED_CHARACTERS
} = require('internal/errors').codes;
const { validateTimerDuration } = require('internal/timers');
const { getTimerDuration } = require('internal/timers');
const is_reused_symbol = require('internal/freelist').symbols.is_reused_symbol;
const {
DTRACE_HTTP_CLIENT_REQUEST,
Expand Down Expand Up @@ -146,7 +146,7 @@ function ClientRequest(input, options, cb) {
this.socketPath = options.socketPath;

if (options.timeout !== undefined)
this.timeout = validateTimerDuration(options.timeout, 'timeout');
this.timeout = getTimerDuration(options.timeout, 'timeout');

var method = options.method;
var methodIsString = (typeof method === 'string');
Expand Down Expand Up @@ -743,7 +743,7 @@ ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
}

listenSocketTimeout(this);
msecs = validateTimerDuration(msecs, 'msecs');
msecs = getTimerDuration(msecs, 'msecs');
if (callback) this.once('timeout', callback);

if (this.socket) {
Expand Down
4 changes: 2 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const {
const { validateString, isInt32 } = require('internal/validators');
const child_process = require('internal/child_process');
const {
_validateStdio,
getValidStdio,
setupChannel,
ChildProcess
} = child_process;
Expand Down Expand Up @@ -577,7 +577,7 @@ function spawnSync(file, args, options) {
// Validate and translate the kill signal, if present.
options.killSignal = sanitizeKillSignal(options.killSignal);

options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;
options.stdio = getValidStdio(options.stdio || 'pipe', true).stdio;

if (options.input) {
var stdin = options.stdio[0] = { ...options.stdio[0] };
Expand Down
20 changes: 10 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBuffer,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath
Expand All @@ -80,7 +79,8 @@ const {
} = require('internal/constants');
const {
isUint32,
validateMode,
parseMode,
validateBuffer,
validateInteger,
validateInt32,
validateUint32
Expand Down Expand Up @@ -426,7 +426,7 @@ function open(path, flags, mode, callback) {
}
const flagsNumber = stringToFlags(flags);
if (arguments.length >= 4) {
mode = validateMode(mode, 'mode', 0o666);
mode = parseMode(mode, 'mode', 0o666);
}
callback = makeCallback(callback);

Expand All @@ -444,7 +444,7 @@ function openSync(path, flags, mode) {
path = toPathIfFileURL(path);
validatePath(path);
const flagsNumber = stringToFlags(flags || 'r');
mode = validateMode(mode, 'mode', 0o666);
mode = parseMode(mode, 'mode', 0o666);

const ctx = { path };
const result = binding.open(pathModule.toNamespacedPath(path),
Expand Down Expand Up @@ -754,7 +754,7 @@ function mkdir(path, options, callback) {
const req = new FSReqCallback();
req.oncomplete = callback;
binding.mkdir(pathModule.toNamespacedPath(path),
validateMode(mode, 'mode', 0o777), recursive, req);
parseMode(mode, 'mode', 0o777), recursive, req);
}

function mkdirSync(path, options) {
Expand All @@ -773,7 +773,7 @@ function mkdirSync(path, options) {

const ctx = { path };
binding.mkdir(pathModule.toNamespacedPath(path),
validateMode(mode, 'mode', 0o777), recursive, undefined,
parseMode(mode, 'mode', 0o777), recursive, undefined,
ctx);
handleErrorFromBinding(ctx);
}
Expand Down Expand Up @@ -1010,7 +1010,7 @@ function unlinkSync(path) {

function fchmod(fd, mode, callback) {
validateInt32(fd, 'fd', 0);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');
callback = makeCallback(callback);

const req = new FSReqCallback();
Expand All @@ -1020,7 +1020,7 @@ function fchmod(fd, mode, callback) {

function fchmodSync(fd, mode) {
validateInt32(fd, 'fd', 0);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');
const ctx = {};
binding.fchmod(fd, mode, undefined, ctx);
handleErrorFromBinding(ctx);
Expand Down Expand Up @@ -1061,7 +1061,7 @@ function lchmodSync(path, mode) {
function chmod(path, mode, callback) {
path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');
callback = makeCallback(callback);

const req = new FSReqCallback();
Expand All @@ -1072,7 +1072,7 @@ function chmod(path, mode, callback) {
function chmodSync(path, mode) {
path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');

const ctx = { path };
binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx);
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ ChildProcess.prototype.spawn = function(options) {
// If no `stdio` option was given - use default
var stdio = options.stdio || 'pipe';

stdio = _validateStdio(stdio, false);
stdio = getValidStdio(stdio, false);

ipc = stdio.ipc;
ipcFd = stdio.ipcFd;
Expand Down Expand Up @@ -873,7 +873,7 @@ function isInternal(message) {

function nop() { }

function _validateStdio(stdio, sync) {
function getValidStdio(stdio, sync) {
var ipc;
var ipcFd;

Expand Down Expand Up @@ -1028,6 +1028,6 @@ function spawnSync(opts) {
module.exports = {
ChildProcess,
setupChannel,
_validateStdio,
getValidStdio,
spawnSync
};
11 changes: 4 additions & 7 deletions lib/internal/crypto/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const {
certExportPublicKey,
certVerifySpkac
} = internalBinding('crypto');
const {
validateBuffer
} = require('internal/validators');

const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const { isArrayBufferView } = require('internal/util/types');
Expand All @@ -14,13 +17,7 @@ const {
} = require('internal/crypto/util');

function verifySpkac(spkac) {
if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE(
'spkac',
['Buffer', 'TypedArray', 'DataView'],
spkac
);
}
validateBuffer(spkac, 'spkac');
return certVerifySpkac(spkac);
}

Expand Down
35 changes: 23 additions & 12 deletions lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,42 @@ function check(password, salt, keylen, options) {

password = validateArrayBufferView(password, 'password');
salt = validateArrayBufferView(salt, 'salt');
keylen = validateUint32(keylen, 'keylen');
validateUint32(keylen, 'keylen');

let { N, r, p, maxmem } = defaults;
if (options && options !== defaults) {
let has_N, has_r, has_p;
if (has_N = (options.N !== undefined))
N = validateUint32(options.N, 'N');
if (has_N = (options.N !== undefined)) {
validateUint32(options.N, 'N');
N = options.N;
}
if (options.cost !== undefined) {
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
N = validateUint32(options.cost, 'cost');
validateUint32(options.cost, 'cost');
N = options.cost;
}
if (has_r = (options.r !== undefined)) {
validateUint32(options.r, 'r');
r = options.r;
}
if (has_r = (options.r !== undefined))
r = validateUint32(options.r, 'r');
if (options.blockSize !== undefined) {
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
r = validateUint32(options.blockSize, 'blockSize');
validateUint32(options.blockSize, 'blockSize');
r = options.blockSize;
}
if (has_p = (options.p !== undefined)) {
validateUint32(options.p, 'p');
p = options.p;
}
if (has_p = (options.p !== undefined))
p = validateUint32(options.p, 'p');
if (options.parallelization !== undefined) {
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
p = validateUint32(options.parallelization, 'parallelization');
validateUint32(options.parallelization, 'parallelization');
p = options.parallelization;
}
if (options.maxmem !== undefined) {
validateUint32(options.maxmem, 'maxmem');
maxmem = options.maxmem;
}
if (options.maxmem !== undefined)
maxmem = validateUint32(options.maxmem, 'maxmem');
if (N === 0) N = defaults.N;
if (r === 0) r = defaults.r;
if (p === 0) p = defaults.p;
Expand Down
10 changes: 5 additions & 5 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ function getDefaultEncoding() {
// This is here because many functions accepted binary strings without
// any explicit encoding in older versions of node, and we don't want
// to break them unnecessarily.
function toBuf(str, encoding) {
if (typeof str === 'string') {
if (encoding === 'buffer' || !encoding)
function toBuf(val, encoding) {
if (typeof val === 'string') {
if (encoding === 'buffer')
encoding = 'utf8';
return Buffer.from(str, encoding);
return Buffer.from(val, encoding);
}
return str;
return val;
}

const getCiphers = cachedResult(() => filterDuplicateStrings(_getCiphers()));
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBuffer,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath
} = require('internal/fs/utils');
const {
validateMode,
parseMode,
validateBuffer,
validateInteger,
validateUint32
} = require('internal/validators');
Expand Down Expand Up @@ -198,7 +198,7 @@ async function open(path, flags, mode) {
validatePath(path);
if (arguments.length < 2) flags = 'r';
const flagsNumber = stringToFlags(flags);
mode = validateMode(mode, 'mode', 0o666);
mode = parseMode(mode, 'mode', 0o666);
return new FileHandle(
await binding.openFileHandle(pathModule.toNamespacedPath(path),
flagsNumber, mode, kUsePromises));
Expand Down Expand Up @@ -309,7 +309,7 @@ async function mkdir(path, options) {
throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive);

return binding.mkdir(pathModule.toNamespacedPath(path),
validateMode(mode, 'mode', 0o777), recursive,
parseMode(mode, 'mode', 0o777), recursive,
kUsePromises);
}

Expand Down Expand Up @@ -386,14 +386,14 @@ async function unlink(path) {

async function fchmod(handle, mode) {
validateFileHandle(handle);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');
return binding.fchmod(handle.fd, mode, kUsePromises);
}

async function chmod(path, mode) {
path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
mode = parseMode(mode, 'mode');
return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises);
}

Expand Down
10 changes: 0 additions & 10 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
} = require('internal/errors');
const {
isUint8Array,
isArrayBufferView,
isDate
} = require('internal/util/types');
const { once } = require('internal/util');
Expand Down Expand Up @@ -393,14 +392,6 @@ function toUnixTimestamp(time, name = 'time') {
throw new ERR_INVALID_ARG_TYPE(name, ['Date', 'Time in seconds'], time);
}

const validateBuffer = hideStackFrames((buffer) => {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'TypedArray', 'DataView'],
buffer);
}
});

const validateOffsetLengthRead = hideStackFrames(
(offset, length, bufferLength) => {
if (offset < 0 || offset >= bufferLength) {
Expand Down Expand Up @@ -453,7 +444,6 @@ module.exports = {
stringToSymlinkType,
Stats,
toUnixTimestamp,
validateBuffer,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath
Expand Down
Loading