From e570ae79f5b1d74fed52d2aed7f014caaa0836dd Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Thu, 2 Aug 2018 18:51:02 -0400 Subject: lib: extract validateString validator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pulls out a common argument validator to `internal/validators` PR-URL: https://github.com/nodejs/node/pull/22101 Reviewed-By: Joyee Cheung Reviewed-By: Michaƫl Zasso Reviewed-By: Trivikram Kamat Reviewed-By: Colin Ihrig Reviewed-By: Anatoli Papirovski Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/crypto/hash.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/internal/crypto/hash.js') diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index d172a6a3c8..7127d4d7ef 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -18,6 +18,7 @@ const { ERR_CRYPTO_HASH_UPDATE_FAILED, ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; +const { validateString } = require('internal/validators'); const { inherits } = require('util'); const { normalizeEncoding } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); @@ -28,8 +29,7 @@ const kFinalized = Symbol('finalized'); function Hash(algorithm, options) { if (!(this instanceof Hash)) return new Hash(algorithm, options); - if (typeof algorithm !== 'string') - throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm); + validateString(algorithm, 'algorithm'); this._handle = new _Hash(algorithm); this[kState] = { [kFinalized]: false @@ -83,8 +83,7 @@ Hash.prototype.digest = function digest(outputEncoding) { function Hmac(hmac, key, options) { if (!(this instanceof Hmac)) return new Hmac(hmac, key, options); - if (typeof hmac !== 'string') - throw new ERR_INVALID_ARG_TYPE('hmac', 'string', hmac); + validateString(hmac, 'hmac'); if (typeof key !== 'string' && !isArrayBufferView(key)) { throw new ERR_INVALID_ARG_TYPE('key', ['string', 'TypedArray', 'DataView'], key); -- cgit v1.2.3