From 022b53c9de7e54afca6a368c196b207031c0dd42 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 7 Jan 2017 20:12:08 -0800 Subject: [PATCH] lib: refactor crypto cipher/hash/curve getters * refactor internal util.filterDuplicateStrings() to eliminate unused code paths * `.indexOf()` -> `.includes()` in test * more concise arrow functions PR-URL: https://github.com/nodejs/node/pull/10682 Reviewed-By: James M Snell Reviewed-By: Michal Zasso --- lib/crypto.js | 18 +++++++++--------- lib/internal/util.js | 10 ++-------- test/parallel/test-crypto-authenticated.js | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index 6b2a04711a868a..c33728ac88c91f 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -637,17 +637,17 @@ exports.randomBytes = exports.pseudoRandomBytes = randomBytes; exports.rng = exports.prng = randomBytes; -exports.getCiphers = internalUtil.cachedResult(() => { - return internalUtil.filterDuplicateStrings(getCiphers()); -}); +exports.getCiphers = internalUtil.cachedResult( + () => internalUtil.filterDuplicateStrings(getCiphers()) +); -exports.getHashes = internalUtil.cachedResult(() => { - return internalUtil.filterDuplicateStrings(getHashes()); -}); +exports.getHashes = internalUtil.cachedResult( + () => internalUtil.filterDuplicateStrings(getHashes()) +); -exports.getCurves = internalUtil.cachedResult(() => { - return internalUtil.filterDuplicateStrings(getCurves()); -}); +exports.getCurves = internalUtil.cachedResult( + () => internalUtil.filterDuplicateStrings(getCurves()) +); Object.defineProperty(exports, 'fips', { get: getFipsCrypto, diff --git a/lib/internal/util.js b/lib/internal/util.js index 238eb65891365c..0c7f391eb1fad1 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -134,20 +134,14 @@ exports.normalizeEncoding = function normalizeEncoding(enc) { // Filters duplicate strings. Used to support functions in crypto and tls // modules. Implemented specifically to maintain existing behaviors in each. exports.filterDuplicateStrings = function filterDuplicateStrings(items, low) { - if (!Array.isArray(items)) - return []; - const len = items.length; - if (len <= 1) - return items; const map = new Map(); - for (var i = 0; i < len; i++) { + for (var i = 0; i < items.length; i++) { const item = items[i]; const key = item.toLowerCase(); if (low) { map.set(key, key); } else { - if (!map.has(key) || map.get(key) <= item) - map.set(key, item); + map.set(key, item); } } return Array.from(map.values()).sort(); diff --git a/test/parallel/test-crypto-authenticated.js b/test/parallel/test-crypto-authenticated.js index 95e3b1193b5490..245783fb1b4275 100644 --- a/test/parallel/test-crypto-authenticated.js +++ b/test/parallel/test-crypto-authenticated.js @@ -312,7 +312,7 @@ const ciphers = crypto.getCiphers(); for (const i in TEST_CASES) { const test = TEST_CASES[i]; - if (ciphers.indexOf(test.algo) === -1) { + if (!ciphers.includes(test.algo)) { common.skip('unsupported ' + test.algo + ' test'); continue; }