evm-watcher
Version:
Fault tolerant event watcher for topics on the ethereum virtual machine.
105 lines (104 loc) • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Disk = void 0;
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const logger_1 = tslib_1.__importDefault(require("../util/logger"));
const filePath = tslib_1.__importStar(require("path"));
/**
*
*/
class Disk {
/**
*
* @param {string} basePath
*/
constructor({ basePath }) {
this.basePath = basePath;
}
/**
*
* @param {string} path
* @return {boolean}
*/
objExists(path) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield fs_1.promises.access(filePath.join(this.basePath, path));
return true;
}
catch (e) {
return false;
}
});
}
/**
*
* @param {string} path
* @return {boolean}
*/
getObject(path) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
return yield fs_1.promises.readFile(filePath.join(this.basePath, path), "utf8");
}
catch (e) {
logger_1.default.error(e.message);
throw new Error("Unable to getObject.");
}
});
}
/**
*
* @param {string} path
* @param {string} body
* @return {void}
*/
putObject(path, body) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield this.mkPath(path);
yield fs_1.promises.writeFile(filePath.join(this.basePath, path), body);
return;
}
catch (e) {
logger_1.default.error(e);
throw new Error("Unable to putObject.");
}
});
}
/**
*
* @param {string} path
* @param {string} body
* @return {void}
*/
appendObject(path, body) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield this.mkPath(path);
yield fs_1.promises.appendFile(filePath.join(this.basePath, path), body);
return;
}
catch (e) {
logger_1.default.error(e);
throw new Error("Unable to putObject.");
}
});
}
/**
*
* @param {string} path
*/
mkPath(path) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const x = path.split(filePath.sep);
x.pop();
const pathWithNoFile = x.join(filePath.sep);
yield fs_1.promises.mkdir(filePath.join(this.basePath, pathWithNoFile), {
recursive: true,
});
});
}
}
exports.Disk = Disk;