@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
123 lines • 4.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FsCacheStore = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const store_1 = require("./store");
const helpers_1 = require("../../../helpers");
const encrypt_1 = require("../../../utils/encrypt");
class FsCacheStore extends store_1.CacheStore {
constructor() {
super();
this.cachePath = path_1.default.join((0, helpers_1.app)().storeagePath, './cache');
}
getPath(key) {
const sha = (0, encrypt_1.encrypt)('sha1', key);
const chunks = [];
for (let i = 0; i < sha.length; i += 2) {
chunks.push(sha.substring(i, i + 2));
}
const parts = chunks.slice(0, 2);
return path_1.default.join(this.cachePath, parts.join('/'), sha);
}
getPayload(key) {
var _a;
const p = this.getPath(key);
if (!fs_1.default.existsSync(p))
return;
const contents = fs_1.default.readFileSync(p);
const json = JSON.parse(contents.toString());
const expiresAt = (_a = json.expiresAt) !== null && _a !== void 0 ? _a : 0;
if (expiresAt !== 0 && Date.now() > expiresAt) {
fs_1.default.unlinkSync(p);
return;
}
return json;
}
async get(key) {
const payload = this.getPayload(key);
if (!payload)
return;
return payload.value;
}
async set(key, value, seconds) {
const p = this.getPath(key);
if (!fs_1.default.existsSync(p)) {
fs_1.default.mkdirSync(path_1.default.dirname(p), { recursive: true });
}
const item = {
value,
expiresAt: seconds ? Date.now() + seconds * 1000 : 0
};
fs_1.default.writeFileSync(p, JSON.stringify(item));
return true;
}
async add(key, value, seconds) {
if (await this.get(key))
return false;
return this.set(key, value, seconds);
}
async increment(key, value = 1) {
const existing = this.getPayload(key);
if (existing) {
value = existing.value + 1;
await this.set(key, value, existing.expiresAt);
}
await this.forever(key, value);
return value;
}
async decrement(key, value = 1) {
return this.increment(key, value * -1);
}
async forever(key, value) {
return this.set(key, value, 0);
}
async remove(key) {
const p = this.getPath(key);
if (fs_1.default.existsSync(p)) {
fs_1.default.unlinkSync(p);
return true;
}
return false;
}
async flush() {
this.emptyDir(this.cachePath);
this.rmEmptyDir(this.cachePath);
return true;
}
emptyDir(filePath) {
const files = fs_1.default.readdirSync(filePath);
files.forEach((file) => {
const nextFilePath = `${filePath}/${file}`;
const states = fs_1.default.statSync(nextFilePath);
if (states.isDirectory()) {
this.emptyDir(nextFilePath);
}
else {
fs_1.default.unlinkSync(nextFilePath);
}
});
}
rmEmptyDir(filePath) {
const files = fs_1.default.readdirSync(filePath);
if (files.length === 0) {
fs_1.default.rmdirSync(filePath);
}
else {
let tempFiles = 0;
files.forEach((file) => {
tempFiles++;
const nextFilePath = `${filePath}/${file}`;
this.rmEmptyDir(nextFilePath);
});
if (tempFiles === files.length) {
fs_1.default.rmdirSync(filePath);
}
}
}
}
exports.FsCacheStore = FsCacheStore;
//# sourceMappingURL=fs.js.map