diff --git a/lib/fs.js b/lib/fs.js index aa32d0db2810e8..8aeaffb8ec17c3 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -69,7 +69,6 @@ const { stringToFlags, stringToSymlinkType, toUnixTimestamp, - validateBuffer, validateOffsetLengthRead, validateOffsetLengthWrite, validatePath @@ -81,6 +80,7 @@ const { const { isUint32, parseMode, + validateBuffer, validateInteger, validateInt32, validateUint32 diff --git a/lib/internal/crypto/certificate.js b/lib/internal/crypto/certificate.js index 29372ca497c935..d828bef0f6ae2d 100644 --- a/lib/internal/crypto/certificate.js +++ b/lib/internal/crypto/certificate.js @@ -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'); @@ -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); } diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 4a0c43a3937a6d..93c8fc0f889d88 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -27,13 +27,13 @@ const { stringToFlags, stringToSymlinkType, toUnixTimestamp, - validateBuffer, validateOffsetLengthRead, validateOffsetLengthWrite, validatePath } = require('internal/fs/utils'); const { parseMode, + validateBuffer, validateInteger, validateUint32 } = require('internal/validators'); diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index eacc4606619445..99e820d2944b69 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -14,7 +14,6 @@ const { } = require('internal/errors'); const { isUint8Array, - isArrayBufferView, isDate } = require('internal/util/types'); const { once } = require('internal/util'); @@ -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) { @@ -453,7 +444,6 @@ module.exports = { stringToSymlinkType, Stats, toUnixTimestamp, - validateBuffer, validateOffsetLengthRead, validateOffsetLengthWrite, validatePath diff --git a/lib/internal/validators.js b/lib/internal/validators.js index e4d937bf19c45e..3cc9c2820a1b09 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -8,6 +8,9 @@ const { ERR_OUT_OF_RANGE } } = require('internal/errors'); +const { + isArrayBufferView +} = require('internal/util/types'); function isInt32(value) { return value === (value | 0); @@ -107,10 +110,22 @@ function validateNumber(value, name) { throw new ERR_INVALID_ARG_TYPE(name, 'number', value); } +// TODO(BridgeAR): We have multiple validation functions that call +// `require('internal/utils').toBuf()` before validating for array buffer views. +// Those should likely also be consolidated in here. +const validateBuffer = hideStackFrames((buffer, name = 'buffer') => { + if (!isArrayBufferView(buffer)) { + throw new ERR_INVALID_ARG_TYPE(name, + ['Buffer', 'TypedArray', 'DataView'], + buffer); + } +}); + module.exports = { isInt32, isUint32, parseMode, + validateBuffer, validateInteger, validateInt32, validateUint32,