Skip to content

Commit ed72d83

Browse files
Trottaddaleax
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 090f869 commit ed72d83

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

‎lib/internal/crypto/keys.js‎

Lines changed: 6 additions & 8 deletions
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-
functioncheckKeyTypeAndHandle(type,handle){
47-
if(type!=='secret'&&type!=='public'&&type!=='private')
48-
thrownewERR_INVALID_ARG_VALUE('type',type);
49-
if(typeofhandle!=='object'||!(handleinstanceofKeyObjectHandle))
50-
thrownewERR_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
classKeyObjectextendsNativeKeyObject{
6659
constructor(type,handle){
67-
super(checkKeyTypeAndHandle(type,handle)||handle);
60+
if(type!=='secret'&&type!=='public'&&type!=='private')
61+
thrownewERR_INVALID_ARG_VALUE('type',type);
62+
if(typeofhandle!=='object'||!(handleinstanceofKeyObjectHandle))
63+
thrownewERR_INVALID_ARG_TYPE('handle','object',handle);
64+
65+
super(handle);
6866

6967
this[kKeyType]=type;
7068

0 commit comments

Comments
(0)