Skip to content

Commit

Permalink
lib: refactor crypto cipher/hash/curve getters
Browse files Browse the repository at this point in the history
* refactor internal util.filterDuplicateStrings() to eliminate unused
  code paths
* `.indexOf()` -> `.includes()` in test
* more concise arrow functions

PR-URL: nodejs#10682
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michal Zasso <targos@protonmail.com>
  • Loading branch information
Trott authored and italoacasas committed Jan 18, 2017
1 parent ed35784 commit 578e542
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
18 changes: 9 additions & 9 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 578e542

Please sign in to comment.