crypto: extract throwInvalidArgType function

This commit extracts the throwing of ERR_INVALID_ARG_TYPE which is done
in identical ways in a few places in cipher.js.

The motivation for this is that I think it improves readability enough to
warrant a commit even though I'm aware that we should avoid commits with
only these sort of refactoring.

PR-URL: https://github.com/nodejs/node/pull/22947
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Daniel Bevenius 2018-09-19 14:04:34 +02:00
parent 7aeda94724
commit b25e6abc68

View File

@ -86,15 +86,19 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
LazyTransform.call(this, options); LazyTransform.call(this, options);
} }
function invalidArrayBufferView(name, value) {
return new ERR_INVALID_ARG_TYPE(
name,
['string', 'Buffer', 'TypedArray', 'DataView'],
value
);
}
function createCipher(cipher, password, options, decipher) { function createCipher(cipher, password, options, decipher) {
validateString(cipher, 'cipher'); validateString(cipher, 'cipher');
password = toBuf(password); password = toBuf(password);
if (!isArrayBufferView(password)) { if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE( throw invalidArrayBufferView('password', password);
'password',
['string', 'Buffer', 'TypedArray', 'DataView'],
password
);
} }
createCipherBase.call(this, cipher, password, options, decipher); createCipherBase.call(this, cipher, password, options, decipher);
@ -104,20 +108,12 @@ function createCipherWithIV(cipher, key, options, decipher, iv) {
validateString(cipher, 'cipher'); validateString(cipher, 'cipher');
key = toBuf(key); key = toBuf(key);
if (!isArrayBufferView(key)) { if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw invalidArrayBufferView('key', key);
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
} }
iv = toBuf(iv); iv = toBuf(iv);
if (iv !== null && !isArrayBufferView(iv)) { if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE( throw invalidArrayBufferView('iv', iv);
'iv',
['string', 'Buffer', 'TypedArray', 'DataView'],
iv
);
} }
createCipherBase.call(this, cipher, key, options, decipher, iv); createCipherBase.call(this, cipher, key, options, decipher, iv);
} }
@ -152,11 +148,7 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
outputEncoding = outputEncoding || encoding; outputEncoding = outputEncoding || encoding;
if (typeof data !== 'string' && !isArrayBufferView(data)) { if (typeof data !== 'string' && !isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE( throw invalidArrayBufferView('data', data);
'data',
['string', 'Buffer', 'TypedArray', 'DataView'],
data
);
} }
const ret = this[kHandle].update(data, inputEncoding); const ret = this[kHandle].update(data, inputEncoding);