Skip to content

Commit

Permalink
feat: create a getFileTag that computes a files eTag
Browse files Browse the repository at this point in the history
it uses the crypto module and runs a sha256 hashing of the files last modification time.
  • Loading branch information
teclone committed Jul 21, 2018
1 parent 87f4d77 commit d2c13b3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/modules/StaticFileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* static file server module
*/
import path from 'path';
import crypto from 'crypto';

export default class {
/**
Expand Down Expand Up @@ -58,4 +59,17 @@ export default class {

return false;
}

/**
* computes and returns a files eTag
*@param {number} fileMTime - the files last modification time
*@param {string} [length=16] - the hash tag length to generate
*@returns {string}
*/
getFileTag(fileMTime, length) {
let hash = crypto.createHash('sha256');
hash.update(fileMTime.toString());

return hash.digest('hex').substring(0, length || 16);
}
}

0 comments on commit d2c13b3

Please sign in to comment.