Skip to content

Commit f98d441

Browse files
danbevtargos
authored andcommitted
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: #22947 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 7d21cc2 commit f98d441

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

lib/internal/crypto/cipher.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,19 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
8383
LazyTransform.call(this, options);
8484
}
8585

86+
function invalidArrayBufferView(name, value) {
87+
return new ERR_INVALID_ARG_TYPE(
88+
name,
89+
['string', 'Buffer', 'TypedArray', 'DataView'],
90+
value
91+
);
92+
}
93+
8694
function createCipher(cipher, password, options, decipher) {
8795
validateString(cipher, 'cipher');
8896
password = toBuf(password);
8997
if (!isArrayBufferView(password)) {
90-
throw new ERR_INVALID_ARG_TYPE(
91-
'password',
92-
['string', 'Buffer', 'TypedArray', 'DataView'],
93-
password
94-
);
98+
throw invalidArrayBufferView('password', password);
9599
}
96100

97101
createCipherBase.call(this, cipher, password, options, decipher);
@@ -101,20 +105,12 @@ function createCipherWithIV(cipher, key, options, decipher, iv) {
101105
validateString(cipher, 'cipher');
102106
key = toBuf(key);
103107
if (!isArrayBufferView(key)) {
104-
throw new ERR_INVALID_ARG_TYPE(
105-
'key',
106-
['string', 'Buffer', 'TypedArray', 'DataView'],
107-
key
108-
);
108+
throw invalidArrayBufferView('key', key);
109109
}
110110

111111
iv = toBuf(iv);
112112
if (iv !== null && !isArrayBufferView(iv)) {
113-
throw new ERR_INVALID_ARG_TYPE(
114-
'iv',
115-
['string', 'Buffer', 'TypedArray', 'DataView'],
116-
iv
117-
);
113+
throw invalidArrayBufferView('iv', iv);
118114
}
119115
createCipherBase.call(this, cipher, key, options, decipher, iv);
120116
}
@@ -149,11 +145,7 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
149145
outputEncoding = outputEncoding || encoding;
150146

151147
if (typeof data !== 'string' && !isArrayBufferView(data)) {
152-
throw new ERR_INVALID_ARG_TYPE(
153-
'data',
154-
['string', 'Buffer', 'TypedArray', 'DataView'],
155-
data
156-
);
148+
throw invalidArrayBufferView('data', data);
157149
}
158150

159151
const ret = this._handle.update(data, inputEncoding);

0 commit comments

Comments
 (0)