Skip to content

Commit b25e6ab

Browse files
committed
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 7aeda94 commit b25e6ab

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

lib/internal/crypto/cipher.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
8686
LazyTransform.call(this, options);
8787
}
8888

89+
function invalidArrayBufferView(name, value) {
90+
return new ERR_INVALID_ARG_TYPE(
91+
name,
92+
['string', 'Buffer', 'TypedArray', 'DataView'],
93+
value
94+
);
95+
}
96+
8997
function createCipher(cipher, password, options, decipher) {
9098
validateString(cipher, 'cipher');
9199
password = toBuf(password);
92100
if (!isArrayBufferView(password)) {
93-
throw new ERR_INVALID_ARG_TYPE(
94-
'password',
95-
['string', 'Buffer', 'TypedArray', 'DataView'],
96-
password
97-
);
101+
throw invalidArrayBufferView('password', password);
98102
}
99103

100104
createCipherBase.call(this, cipher, password, options, decipher);
@@ -104,20 +108,12 @@ function createCipherWithIV(cipher, key, options, decipher, iv) {
104108
validateString(cipher, 'cipher');
105109
key = toBuf(key);
106110
if (!isArrayBufferView(key)) {
107-
throw new ERR_INVALID_ARG_TYPE(
108-
'key',
109-
['string', 'Buffer', 'TypedArray', 'DataView'],
110-
key
111-
);
111+
throw invalidArrayBufferView('key', key);
112112
}
113113

114114
iv = toBuf(iv);
115115
if (iv !== null && !isArrayBufferView(iv)) {
116-
throw new ERR_INVALID_ARG_TYPE(
117-
'iv',
118-
['string', 'Buffer', 'TypedArray', 'DataView'],
119-
iv
120-
);
116+
throw invalidArrayBufferView('iv', iv);
121117
}
122118
createCipherBase.call(this, cipher, key, options, decipher, iv);
123119
}
@@ -152,11 +148,7 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
152148
outputEncoding = outputEncoding || encoding;
153149

154150
if (typeof data !== 'string' && !isArrayBufferView(data)) {
155-
throw new ERR_INVALID_ARG_TYPE(
156-
'data',
157-
['string', 'Buffer', 'TypedArray', 'DataView'],
158-
data
159-
);
151+
throw invalidArrayBufferView('data', data);
160152
}
161153

162154
const ret = this[kHandle].update(data, inputEncoding);

0 commit comments

Comments
 (0)