Skip to content

Commit dd7231b

Browse files
authored
Refactor to replace imurmurhash with node:crypto (#9177)
1 parent 5b6b024 commit dd7231b

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

‎lib/utils/__tests__/hash.test.mjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import hash from '../hash.mjs';
2+
3+
describe('hash()', () => {
4+
it('returns the expected value', () => {
5+
expect(hash('foo')).toBe('rL0Y20zC-F');
6+
});
7+
8+
it('returns a 10-character string', () => {
9+
expect(hash('foo')).toHaveLength(10);
10+
expect(hash('')).toHaveLength(10);
11+
expect(hash('a very long string that exceeds normal length')).toHaveLength(10);
12+
});
13+
14+
it('returns consistent results for the same input', () => {
15+
expect(hash('foo')).toBe(hash('foo'));
16+
});
17+
18+
it('returns different results for different inputs', () => {
19+
expect(hash('foo')).not.toBe(hash('bar'));
20+
});
21+
});

‎lib/utils/hash.mjs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import MurmurHash3 from 'imurmurhash';
1+
import { createHash } from 'node:crypto';
22

33
/**
44
* hash the given string
55
* @param {string} str the string to hash
66
* @returns {string} the hash
77
*/
88
export default function hash(str) {
9-
return MurmurHash3(str).result().toString(36);
9+
// Why md5?
10+
// - Fastest performance
11+
// - Sufficient collision resistance for file paths and configurations
12+
// - Not need cryptographic security
13+
return createHash('md5').update(str).digest('base64url').slice(0, 10);
1014
}

‎package-lock.json‎

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
146146
"html-tags": "^5.1.0",
147147
"ignore": "^7.0.5",
148148
"import-meta-resolve": "^4.2.0",
149-
"imurmurhash": "^0.1.4",
150149
"is-plain-object": "^5.0.0",
151150
"mathml-tag-names": "^4.0.0",
152151
"meow": "^14.1.0",
@@ -173,7 +172,6 @@
173172
"@types/debug": "^4.1.12",
174173
"@types/global-modules": "^2.0.2",
175174
"@types/globjoin": "^0.1.2",
176-
"@types/imurmurhash": "^0.1.4",
177175
"@types/micromatch": "^4.0.10",
178176
"@types/normalize-path": "^3.0.2",
179177
"@types/postcss-less": "^4.0.7",

0 commit comments

Comments
 (0)