Skip to content

Commit 178a740

Browse files
Trottrichardlau
authored andcommitted
crypto: simplify KeyObject constructor
Inline a function that only gets called in the constructor. Make call to `super()` more straightforward in the process by removing conditional involving the function as it only ever returns `undefined` or else throws. That made the code a little hard to understand, as without looking at the function, one would likely expect it to return `true` on success rather than `undefined`. PR-URL: #35064 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent a12d92c commit 178a740

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/internal/crypto/keys.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ for (const m of [[kKeyEncodingPKCS1, 'pkcs1'], [kKeyEncodingPKCS8, 'pkcs8'],
4343
[kKeyEncodingSPKI, 'spki'], [kKeyEncodingSEC1, 'sec1']])
4444
encodingNames[m[0]] = m[1];
4545

46-
function checkKeyTypeAndHandle(type, handle) {
47-
if (type !== 'secret' && type !== 'public' && type !== 'private')
48-
throw new ERR_INVALID_ARG_VALUE('type', type);
49-
if (typeof handle !== 'object' || !(handle instanceof KeyObjectHandle))
50-
throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle);
51-
}
52-
5346
// Creating the KeyObject class is a little complicated due to inheritance
5447
// and that fact that KeyObjects should be transferrable between threads,
5548
// which requires the KeyObject base class to be implemented in C++.
@@ -64,7 +57,12 @@ const [
6457
// Publicly visible KeyObject class.
6558
class KeyObject extends NativeKeyObject {
6659
constructor(type, handle) {
67-
super(checkKeyTypeAndHandle(type, handle) || handle);
60+
if (type !== 'secret' && type !== 'public' && type !== 'private')
61+
throw new ERR_INVALID_ARG_VALUE('type', type);
62+
if (typeof handle !== 'object' || !(handle instanceof KeyObjectHandle))
63+
throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle);
64+
65+
super(handle);
6866

6967
this[kKeyType] = type;
7068

0 commit comments

Comments
 (0)