Skip to content

Commit

Permalink
crypto: simplify lazy loading of internal modules
Browse files Browse the repository at this point in the history
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: #45659 (comment)
PR-URL: #45809
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Jan 5, 2023
1 parent 819f786 commit a2230ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
10 changes: 0 additions & 10 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -431,7 +422,6 @@ module.exports = {
validateByteSource,
validateKeyOps,
jobPromise,
lazyRequire,
validateMaxBufferLength,
bigIntArrayToUnsignedBigInt,
bigIntArrayToUnsignedInt,
Expand Down
65 changes: 32 additions & 33 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const {
getArrayBufferOrView,
getBlockSize,
hasAnyNotIn,
lazyRequire,
normalizeAlgorithm,
normalizeHashName,
validateMaxBufferLength,
Expand Down Expand Up @@ -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':
Expand All @@ -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':
Expand All @@ -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:
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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:
Expand All @@ -272,15 +271,15 @@ 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;
case 'ECDSA':
// Fall through
case 'ECDH':
if (key.type === 'public') {
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatSPKI);
}
break;
Expand All @@ -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;
Expand All @@ -311,15 +310,15 @@ 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;
case 'ECDSA':
// Fall through
case 'ECDH':
if (key.type === 'private') {
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatPKCS8);
}
break;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit a2230ad

Please sign in to comment.