file-state-monitor
Version:
Completely customizable file monitoring using states
123 lines (97 loc) • 2.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _crypto = _interopRequireDefault(require("crypto"));
var _fs = _interopRequireDefault(require("fs"));
var _BaseFileState = _interopRequireDefault(require("./BaseFileState"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* State that uses a smart detection consting of checks for last modified,
* file size and SHA-1 hash to detect file changes
*/
class SmartState extends _BaseFileState.default {
/**
* Constructs a new combined state
*
* @param {Object} fileInfo Object containing existing state data
*/
constructor(fileInfo) {
super(fileInfo);
if (Object.keys(fileInfo).length === 1) {
let stat = _fs.default.statSync(this.path);
this._lastModified = stat.mtime.getTime();
this._size = stat.size;
this._sha1 = null;
}
}
/**
* Gets the last modification date of the file
*
* @return {number} Last modified date in miliseconds
*/
get lastModified() {
return this._lastModified;
}
/**
* Gets the file size of the file
*
* @return {number} File size in bytes
*/
get size() {
return this._size;
}
/**
* Gets the SHA-1 hash of the file's content
*
* @return {string} SHA-1 content hash
*/
get sha1() {
if (this._sha1 === null) {
this._sha1 = this.computeSha1();
}
return this._sha1;
}
/**
* Computes the sha1 hash of the files content
*
* @return {string} Computed sha1 hash
*/
computeSha1() {
let hash = _crypto.default.createHash('sha1');
hash.update(_fs.default.readFileSync(this.path));
return hash.digest('hex');
}
/**
* Checks if this file state is different than another given file state
*
* We do the checks in order of fastest to slowest. First check the
* modification times, then for different file size and last one is SHA-1
* content hash.
*
* @param {FileState} otherState File state to check against
* @return {boolean} True if the two file states are different, false otherwise
*/
isDifferentThan(otherState) {
if (this.path !== otherState.path) {
throw new Error(`Can only compare files with the same path, but tried to compare ${this._path} with ${otherState.path}`);
}
if (this.lastModified === otherState.lastModified) {
return false;
}
return this.size !== otherState.size || this.sha1 !== otherState.sha1;
}
/**
* @inheritdoc
*/
toJson() {
return {
path: this.path,
lastModified: this.lastModified,
size: this.size,
sha1: this.sha1
};
}
}
exports.default = SmartState;