Skip to content

Commit

Permalink
Update: replace MD5 hashing of cache files with MurmurHash (fixes esl…
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Mar 13, 2016
1 parent a05394a commit 7ef1d07
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
17 changes: 3 additions & 14 deletions lib/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var fs = require("fs"),
SourceCodeFixer = require("./util/source-code-fixer"),
validator = require("./config/config-validator"),
stringify = require("json-stable-stringify"),
hash = require("./util/hash"),

crypto = require( "crypto" ),
pkg = require("../package.json");


Expand Down Expand Up @@ -262,17 +262,6 @@ function isErrorMessage(message) {
return message.severity === 2;
}

/**
* create a md5Hash of a given string
* @param {string} str the string to calculate the hash for
* @returns {string} the calculated hash
*/
function md5Hash(str) {
return crypto
.createHash("md5")
.update(str, "utf8")
.digest("hex");
}

/**
* return the cacheFile to be used by eslint, based on whether the provided parameter is
Expand All @@ -298,7 +287,7 @@ function getCacheFile(cacheFile, cwd) {
* @returns {string} the resolved path to the cacheFile
*/
function getCacheFileForDirectory() {
return path.join(resolvedCacheFile, ".cache_" + md5Hash(cwd));
return path.join(resolvedCacheFile, ".cache_" + hash(cwd));
}

var fileStats;
Expand Down Expand Up @@ -531,7 +520,7 @@ CLIEngine.prototype = {

var eslintVersion = pkg.version;

prevConfig.hash = md5Hash(eslintVersion + "_" + stringify(config));
prevConfig.hash = hash(eslintVersion + "_" + stringify(config));
}

return prevConfig.hash;
Expand Down
37 changes: 37 additions & 0 deletions lib/util/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @fileoverview Defining the hashing function in one place.
* @author Michael Ficarra
* @copyright 2016 Michael Ficarra. All rights reserved.
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var murmur = require("imurmurhash");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------

/**
* hash the given string
* @param {string} str the string to hash
* @returns {string} the hash
*/
function hash(str) {
return murmur(str).result().toString(36);
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = hash;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"glob": "^6.0.4",
"globals": "^8.18.0",
"ignore": "^2.2.19",
"imurmurhash": "^0.1.4",
"inquirer": "^0.12.0",
"is-my-json-valid": "^2.10.0",
"is-resolvable": "^1.0.0",
Expand Down
28 changes: 3 additions & 25 deletions tests/lib/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var assert = require("chai").assert,
Plugins = require("../../lib/config/plugins"),
fs = require("fs"),
os = require("os"),
crypto = require("crypto");
hash = require("../../lib/util/hash");

require("shelljs/global");
proxyquire = proxyquire.noCallThru().noPreserveCache();
Expand Down Expand Up @@ -1309,17 +1309,6 @@ describe("CLIEngine", function() {
it("should create the cache file inside the provided directory", function() {
assert.isFalse(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist");

sandbox.stub(crypto, "createHash", function() {
return {
update: function() {
return this;
},
digest: function() {
return "hashOfCurrentWorkingDirectory";
}
};
});

engine = new CLIEngine({
useEslintrc: false,
// specifying cache true the cache will be created
Expand All @@ -1337,7 +1326,7 @@ describe("CLIEngine", function() {

engine.executeOnFiles([file]);

assert.isTrue(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint was created");
assert.isTrue(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_" + hash(process.cwd()))), "the cache for eslint was created");

sandbox.restore();
});
Expand All @@ -1346,17 +1335,6 @@ describe("CLIEngine", function() {
it("should create the cache file inside the provided directory using the cacheLocation option", function() {
assert.isFalse(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist");

sandbox.stub(crypto, "createHash", function() {
return {
update: function() {
return this;
},
digest: function() {
return "hashOfCurrentWorkingDirectory";
}
};
});

engine = new CLIEngine({
useEslintrc: false,
// specifying cache true the cache will be created
Expand All @@ -1374,7 +1352,7 @@ describe("CLIEngine", function() {

engine.executeOnFiles([file]);

assert.isTrue(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint was created");
assert.isTrue(fs.existsSync(path.resolve("./tmp/.cacheFileDir/.cache_" + hash(process.cwd()))), "the cache for eslint was created");

sandbox.restore();
});
Expand Down

0 comments on commit 7ef1d07

Please sign in to comment.