From 178a740caf68144a8ec226fc1583ff24a789035d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 4 Sep 2020 22:23:56 -0700 Subject: [PATCH] crypto: simplify KeyObject constructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/35064 Reviewed-By: Tobias Nießen Reviewed-By: Michaël Zasso Reviewed-By: Denys Otrishko Reviewed-By: Anna Henningsen --- lib/internal/crypto/keys.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 889c3763b965df..3c976ca349e4fa 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -43,13 +43,6 @@ for (const m of [[kKeyEncodingPKCS1, 'pkcs1'], [kKeyEncodingPKCS8, 'pkcs8'], [kKeyEncodingSPKI, 'spki'], [kKeyEncodingSEC1, 'sec1']]) encodingNames[m[0]] = m[1]; -function checkKeyTypeAndHandle(type, handle) { - if (type !== 'secret' && type !== 'public' && type !== 'private') - throw new ERR_INVALID_ARG_VALUE('type', type); - if (typeof handle !== 'object' || !(handle instanceof KeyObjectHandle)) - throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle); -} - // Creating the KeyObject class is a little complicated due to inheritance // and that fact that KeyObjects should be transferrable between threads, // which requires the KeyObject base class to be implemented in C++. @@ -64,7 +57,12 @@ const [ // Publicly visible KeyObject class. class KeyObject extends NativeKeyObject { constructor(type, handle) { - super(checkKeyTypeAndHandle(type, handle) || handle); + if (type !== 'secret' && type !== 'public' && type !== 'private') + throw new ERR_INVALID_ARG_VALUE('type', type); + if (typeof handle !== 'object' || !(handle instanceof KeyObjectHandle)) + throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle); + + super(handle); this[kKeyType] = type;