diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-20 15:15:48 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-27 19:05:19 +0300 |
commit | 7bddfcc61a5a7d04583a8c4fec462ca5ce45b677 (patch) | |
tree | 0ed6a369fdb71c84281ae830b24ab78da6bce3f7 /lib/internal/validators.js | |
parent | 751c92d9728da6f6f86e443783a61253791cfc2f (diff) |
lib: consolidate arrayBufferView validation
There are lots of places that validate for arrayBufferView and we
have multiple functions that do the same thing. Instead, move the
validation into `internal/validators` so all files can use that
instead.
There are more functions throughout the code that do the same but
it takes some more work to fully consolidate all of those.
PR-URL: https://github.com/nodejs/node/pull/26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/validators.js')
-rw-r--r-- | lib/internal/validators.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/internal/validators.js b/lib/internal/validators.js index e4d937bf19c..3cc9c2820a1 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, |