Replace custom rolled stable string hashing with library

Reviewed By: cpojer

Differential Revision: D2937202

fb-gh-sync-id: dc08547c71da2bc35cfad108e63fd5e87f0ba734
shipit-source-id: dc08547c71da2bc35cfad108e63fd5e87f0ba734
This commit is contained in:
David Aurelio 2016-02-15 06:57:26 -08:00 committed by facebook-github-bot-7
parent 012caf0e22
commit 5357bd7d99
2 changed files with 5 additions and 17 deletions

View File

@ -11,6 +11,7 @@
const crypto = require('crypto'); const crypto = require('crypto');
const docblock = require('./DependencyGraph/docblock'); const docblock = require('./DependencyGraph/docblock');
const isAbsolutePath = require('absolute-path'); const isAbsolutePath = require('absolute-path');
const jsonStableStringify = require('json-stable-stringify');
const path = require('path'); const path = require('path');
const extractRequires = require('./lib/extractRequires'); const extractRequires = require('./lib/extractRequires');
@ -206,29 +207,15 @@ function whileInDocBlock(chunk, i, result) {
const knownHashes = new WeakMap(); const knownHashes = new WeakMap();
function stableObjectHash(object) { function stableObjectHash(object) {
let digest = knownHashes.get(object); let digest = knownHashes.get(object);
if (!digest) { if (!digest) {
const hash = crypto.createHash('md5'); digest = crypto.createHash('md5')
stableObjectHash.addTo(object, hash); .update(jsonStableStringify(object))
digest = hash.digest('base64'); .digest('base64');
knownHashes.set(object, digest); knownHashes.set(object, digest);
} }
return digest; return digest;
} }
stableObjectHash.addTo = function addTo(value, hash) {
if (value === null || typeof value !== 'object') {
hash.update(JSON.stringify(value));
} else {
Object.keys(value).sort().forEach(key => {
const valueForKey = value[key];
if (valueForKey !== undefined) {
hash.update(key);
addTo(valueForKey, hash);
}
});
}
};
function cacheKey(field, transformOptions) { function cacheKey(field, transformOptions) {
return transformOptions !== undefined return transformOptions !== undefined

View File

@ -10,6 +10,7 @@
jest jest
.dontMock('absolute-path') .dontMock('absolute-path')
.dontMock('json-stable-stringify')
.dontMock('../fastfs') .dontMock('../fastfs')
.dontMock('../lib/extractRequires') .dontMock('../lib/extractRequires')
.dontMock('../lib/replacePatterns') .dontMock('../lib/replacePatterns')