From b8c952c4183f9c8734859f1996215268ef513448 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 12 Dec 2022 16:17:44 +0100 Subject: [PATCH] crypto: simplify lazy loading of internal modules The internal `require()` is actually just one map load (to see if the module is already loaded) + one property load (state check for circular dependencies) for modules that are already loaded. Refs: https://github.com/nodejs/node/pull/45659#discussion_r1033762328 PR-URL: https://github.com/nodejs/node/pull/45809 Reviewed-By: Daeyeon Jeong Reviewed-By: Luigi Pinca Reviewed-By: Minwoo Jung --- lib/internal/crypto/util.js | 10 ----- lib/internal/crypto/webcrypto.js | 65 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index a5446aee187382..d7e6356486221e 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -62,15 +62,6 @@ const { const kHandle = Symbol('kHandle'); const kKeyObject = Symbol('kKeyObject'); -const lazyRequireCache = {}; - -function lazyRequire(name) { - let ret = lazyRequireCache[name]; - if (ret === undefined) - ret = lazyRequireCache[name] = require(name); - return ret; -} - let defaultEncoding = 'buffer'; function setDefaultEncoding(val) { @@ -431,7 +422,6 @@ module.exports = { validateByteSource, validateKeyOps, jobPromise, - lazyRequire, validateMaxBufferLength, bigIntArrayToUnsignedBigInt, bigIntArrayToUnsignedInt, diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index f8af06b1adf14f..d02fa67a36fc97 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -53,7 +53,6 @@ const { getArrayBufferOrView, getBlockSize, hasAnyNotIn, - lazyRequire, normalizeAlgorithm, normalizeHashName, validateMaxBufferLength, @@ -99,7 +98,7 @@ async function generateKey( // Fall through case 'RSA-OAEP': resultType = 'CryptoKeyPair'; - result = await lazyRequire('internal/crypto/rsa') + result = await require('internal/crypto/rsa') .rsaKeyGenerate(algorithm, extractable, keyUsages); break; case 'Ed25519': @@ -110,19 +109,19 @@ async function generateKey( // Fall through case 'X448': resultType = 'CryptoKeyPair'; - result = await lazyRequire('internal/crypto/cfrg') + result = await require('internal/crypto/cfrg') .cfrgGenerateKey(algorithm, extractable, keyUsages); break; case 'ECDSA': // Fall through case 'ECDH': resultType = 'CryptoKeyPair'; - result = await lazyRequire('internal/crypto/ec') + result = await require('internal/crypto/ec') .ecGenerateKey(algorithm, extractable, keyUsages); break; case 'HMAC': resultType = 'CryptoKey'; - result = await lazyRequire('internal/crypto/mac') + result = await require('internal/crypto/mac') .hmacGenerateKey(algorithm, extractable, keyUsages); break; case 'AES-CTR': @@ -133,7 +132,7 @@ async function generateKey( // Fall through case 'AES-KW': resultType = 'CryptoKey'; - result = await lazyRequire('internal/crypto/aes') + result = await require('internal/crypto/aes') .aesGenerateKey(algorithm, extractable, keyUsages); break; default: @@ -172,13 +171,13 @@ async function deriveBits(algorithm, baseKey, length) { case 'X448': // Fall through case 'ECDH': - return lazyRequire('internal/crypto/diffiehellman') + return require('internal/crypto/diffiehellman') .ecdhDeriveBits(algorithm, baseKey, length); case 'HKDF': - return lazyRequire('internal/crypto/hkdf') + return require('internal/crypto/hkdf') .hkdfDeriveBits(algorithm, baseKey, length); case 'PBKDF2': - return lazyRequire('internal/crypto/pbkdf2') + return require('internal/crypto/pbkdf2') .pbkdf2DeriveBits(algorithm, baseKey, length); } throw lazyDOMException('Unrecognized name.'); @@ -242,15 +241,15 @@ async function deriveKey( case 'X448': // Fall through case 'ECDH': - bits = await lazyRequire('internal/crypto/diffiehellman') + bits = await require('internal/crypto/diffiehellman') .ecdhDeriveBits(algorithm, baseKey, length); break; case 'HKDF': - bits = await lazyRequire('internal/crypto/hkdf') + bits = await require('internal/crypto/hkdf') .hkdfDeriveBits(algorithm, baseKey, length); break; case 'PBKDF2': - bits = await lazyRequire('internal/crypto/pbkdf2') + bits = await require('internal/crypto/pbkdf2') .pbkdf2DeriveBits(algorithm, baseKey, length); break; default: @@ -272,7 +271,7 @@ async function exportKeySpki(key) { // Fall through case 'RSA-OAEP': if (key.type === 'public') { - return lazyRequire('internal/crypto/rsa') + return require('internal/crypto/rsa') .rsaExportKey(key, kWebCryptoKeyFormatSPKI); } break; @@ -280,7 +279,7 @@ async function exportKeySpki(key) { // Fall through case 'ECDH': if (key.type === 'public') { - return lazyRequire('internal/crypto/ec') + return require('internal/crypto/ec') .ecExportKey(key, kWebCryptoKeyFormatSPKI); } break; @@ -292,7 +291,7 @@ async function exportKeySpki(key) { // Fall through case 'X448': if (key.type === 'public') { - return lazyRequire('internal/crypto/cfrg') + return require('internal/crypto/cfrg') .cfrgExportKey(key, kWebCryptoKeyFormatSPKI); } break; @@ -311,7 +310,7 @@ async function exportKeyPkcs8(key) { // Fall through case 'RSA-OAEP': if (key.type === 'private') { - return lazyRequire('internal/crypto/rsa') + return require('internal/crypto/rsa') .rsaExportKey(key, kWebCryptoKeyFormatPKCS8); } break; @@ -319,7 +318,7 @@ async function exportKeyPkcs8(key) { // Fall through case 'ECDH': if (key.type === 'private') { - return lazyRequire('internal/crypto/ec') + return require('internal/crypto/ec') .ecExportKey(key, kWebCryptoKeyFormatPKCS8); } break; @@ -331,7 +330,7 @@ async function exportKeyPkcs8(key) { // Fall through case 'X448': if (key.type === 'private') { - return lazyRequire('internal/crypto/cfrg') + return require('internal/crypto/cfrg') .cfrgExportKey(key, kWebCryptoKeyFormatPKCS8); } break; @@ -348,7 +347,7 @@ async function exportKeyRaw(key) { // Fall through case 'ECDH': if (key.type === 'public') { - return lazyRequire('internal/crypto/ec') + return require('internal/crypto/ec') .ecExportKey(key, kWebCryptoKeyFormatRaw); } break; @@ -360,7 +359,7 @@ async function exportKeyRaw(key) { // Fall through case 'X448': if (key.type === 'public') { - return lazyRequire('internal/crypto/cfrg') + return require('internal/crypto/cfrg') .cfrgExportKey(key, kWebCryptoKeyFormatRaw); } break; @@ -425,7 +424,7 @@ async function exportKeyJWK(key) { case 'AES-GCM': // Fall through case 'AES-KW': - jwk.alg = lazyRequire('internal/crypto/aes') + jwk.alg = require('internal/crypto/aes') .getAlgorithmName(key.algorithm.name, key.algorithm.length); return jwk; case 'HMAC': @@ -524,12 +523,12 @@ async function importKey( case 'RSA-PSS': // Fall through case 'RSA-OAEP': - return lazyRequire('internal/crypto/rsa') + return require('internal/crypto/rsa') .rsaImportKey(format, keyData, algorithm, extractable, keyUsages); case 'ECDSA': // Fall through case 'ECDH': - return lazyRequire('internal/crypto/ec') + return require('internal/crypto/ec') .ecImportKey(format, keyData, algorithm, extractable, keyUsages); case 'Ed25519': // Fall through @@ -538,10 +537,10 @@ async function importKey( case 'X25519': // Fall through case 'X448': - return lazyRequire('internal/crypto/cfrg') + return require('internal/crypto/cfrg') .cfrgImportKey(format, keyData, algorithm, extractable, keyUsages); case 'HMAC': - return lazyRequire('internal/crypto/mac') + return require('internal/crypto/mac') .hmacImportKey(format, keyData, algorithm, extractable, keyUsages); case 'AES-CTR': // Fall through @@ -550,7 +549,7 @@ async function importKey( case 'AES-GCM': // Fall through case 'AES-KW': - return lazyRequire('internal/crypto/aes') + return require('internal/crypto/aes') .aesImportKey(algorithm, format, keyData, extractable, keyUsages); case 'HKDF': // Fall through @@ -650,19 +649,19 @@ function signVerify(algorithm, key, data, signature) { case 'RSA-PSS': // Fall through case 'RSASSA-PKCS1-v1_5': - return lazyRequire('internal/crypto/rsa') + return require('internal/crypto/rsa') .rsaSignVerify(key, data, algorithm, signature); case 'ECDSA': - return lazyRequire('internal/crypto/ec') + return require('internal/crypto/ec') .ecdsaSignVerify(key, data, algorithm, signature); case 'Ed25519': // Fall through case 'Ed448': // Fall through - return lazyRequire('internal/crypto/cfrg') + return require('internal/crypto/cfrg') .eddsaSignVerify(key, data, algorithm, signature); case 'HMAC': - return lazyRequire('internal/crypto/mac') + return require('internal/crypto/mac') .hmacSignVerify(key, data, algorithm, signature); } throw lazyDOMException('Unrecognized named.', 'NotSupportedError'); @@ -705,18 +704,18 @@ async function cipherOrWrap(mode, algorithm, key, data, op) { switch (algorithm.name) { case 'RSA-OAEP': - return lazyRequire('internal/crypto/rsa') + return require('internal/crypto/rsa') .rsaCipher(mode, key, data, algorithm); case 'AES-CTR': // Fall through case 'AES-CBC': // Fall through case 'AES-GCM': - return lazyRequire('internal/crypto/aes') + return require('internal/crypto/aes') .aesCipher(mode, key, data, algorithm); case 'AES-KW': if (op === 'wrapKey' || op === 'unwrapKey') { - return lazyRequire('internal/crypto/aes') + return require('internal/crypto/aes') .aesCipher(mode, key, data, algorithm); } }