Skip to content

Commit 797d9ee

Browse files
TrottMylesBorins
authored andcommitted
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: #10682 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michal Zasso <[email protected]>
1 parent ef3d889 commit 797d9ee

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

lib/crypto.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -633,17 +633,17 @@ exports.randomBytes = exports.pseudoRandomBytes = randomBytes;
633633

634634
exports.rng = exports.prng = randomBytes;
635635

636-
exports.getCiphers = internalUtil.cachedResult(() => {
637-
return internalUtil.filterDuplicateStrings(getCiphers());
638-
});
636+
exports.getCiphers = internalUtil.cachedResult(
637+
() => internalUtil.filterDuplicateStrings(getCiphers())
638+
);
639639

640-
exports.getHashes = internalUtil.cachedResult(() => {
641-
return internalUtil.filterDuplicateStrings(getHashes());
642-
});
640+
exports.getHashes = internalUtil.cachedResult(
641+
() => internalUtil.filterDuplicateStrings(getHashes())
642+
);
643643

644-
exports.getCurves = internalUtil.cachedResult(() => {
645-
return internalUtil.filterDuplicateStrings(getCurves());
646-
});
644+
exports.getCurves = internalUtil.cachedResult(
645+
() => internalUtil.filterDuplicateStrings(getCurves())
646+
);
647647

648648
Object.defineProperty(exports, 'fips', {
649649
get: getFipsCrypto,

lib/internal/util.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,14 @@ exports.assertCrypto = function(exports) {
109109
// Filters duplicate strings. Used to support functions in crypto and tls
110110
// modules. Implemented specifically to maintain existing behaviors in each.
111111
exports.filterDuplicateStrings = function filterDuplicateStrings(items, low) {
112-
if (!Array.isArray(items))
113-
return [];
114-
const len = items.length;
115-
if (len <= 1)
116-
return items;
117112
const map = new Map();
118-
for (var i = 0; i < len; i++) {
113+
for (var i = 0; i < items.length; i++) {
119114
const item = items[i];
120115
const key = item.toLowerCase();
121116
if (low) {
122117
map.set(key, key);
123118
} else {
124-
if (!map.has(key) || map.get(key) <= item)
125-
map.set(key, item);
119+
map.set(key, item);
126120
}
127121
}
128122
return Array.from(map.values()).sort();

test/parallel/test-crypto-authenticated.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ const ciphers = crypto.getCiphers();
312312
for (const i in TEST_CASES) {
313313
const test = TEST_CASES[i];
314314

315-
if (ciphers.indexOf(test.algo) === -1) {
315+
if (!ciphers.includes(test.algo)) {
316316
common.skip('unsupported ' + test.algo + ' test');
317317
continue;
318318
}

0 commit comments

Comments
 (0)