Skip to content

Commit

Permalink
Calculate md5 hexdigest instead of Adler-32 checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
zeveshe committed Apr 24, 2017
1 parent 4edc2dc commit f9e5c69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 21 additions & 6 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var path = require( 'path' );
var ADLER32 = require( 'adler-32' );
var crypto = require( 'crypto' );

module.exports = {
createFromFile: function ( filePath ) {
Expand Down Expand Up @@ -37,6 +37,20 @@ module.exports = {
* @type {Object}
*/
cache: cache,

/**
* Given a buffer, calculate md5 hash of its content.
* @method getHash
* @param {Buffer} buffer buffer to calculate hash on
* @return {String} content hash digest
*/
getHash: function ( buffer ) {
return crypto
.createHash( 'md5' )
.update( buffer )
.digest( 'hex' );
},

/**
* Return whether or not a file has changed since last time reconcile was called.
* @method hasFileChanged
Expand Down Expand Up @@ -93,12 +107,12 @@ module.exports = {
return { key: file, notFound: true, err: ex };
}

var checksum = ADLER32.buf( contentBuffer );
var hash = this.getHash( contentBuffer );

if ( !meta ) {
meta = { checksum: checksum };
meta = { hash: hash };
} else {
var isDifferent = checksum !== meta.checksum;
var isDifferent = hash !== meta.hash;
}

var nEntry = normalizedEntries[ file ] = {
Expand Down Expand Up @@ -183,6 +197,7 @@ module.exports = {

var entries = normalizedEntries;
var keys = Object.keys( entries );
var me = this;

if ( keys.length === 0 ) {
return;
Expand All @@ -193,8 +208,8 @@ module.exports = {

try {
var contentBuffer = fs.readFileSync( cacheEntry.key );
var checksum = ADLER32.buf( contentBuffer );
var meta = assign( cacheEntry.meta, { checksum: checksum } );
var hash = me.getHash( contentBuffer );
var meta = assign( cacheEntry.meta, { hash: hash } );

cache.setKey( entryName, meta );
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"write": "^0.3.1"
},
"dependencies": {
"adler-32": "^1.0.0",
"flat-cache": "^1.2.1",
"object-assign": "^4.0.1"
}
Expand Down

0 comments on commit f9e5c69

Please sign in to comment.