UNPKG

@dooboostore/simple-boot-http-server

Version:
130 lines (129 loc) 4.67 kB
// src/filters/CacheFilter.ts import { FileUtils } from "@dooboostore/core-node/file/FileUtils"; import { ConvertUtils } from "@dooboostore/core-node/convert/ConvertUtils"; import { RandomUtils } from "@dooboostore/core/random/RandomUtils"; var CacheFilter = class _CacheFilter { constructor(config) { this.config = config; this.storage = /* @__PURE__ */ new Map(); if (config.config.cacheDir && config.config.startUpClean) { FileUtils.deleteSync(config.config.cacheDir, { options: { recursive: true } }); } } static { this.CARRIER_KEY_NAME = "_CARRIER_CACHE_FILTER_KEY_NAME"; } async onInit(app) { } async proceedBefore({ rr, app, carrier }) { const filter = this.config.filter?.(rr, app); if (!filter) { return true; } if (this.config.config.cacheDir) { FileUtils.mkdirSync(this.config.config.cacheDir, { recursive: true }); } if (!this.cacheConfig && this.config.config.cacheDir) { try { this.cacheConfig = JSON.parse(ConvertUtils.toString(FileUtils.readSync([this.config.config.cacheDir, "cacheConfig.json"], { option: { encoding: "utf-8" } }))); } catch (e) { } } const date = (/* @__PURE__ */ new Date()).toISOString(); this.cacheConfig ??= { createDate: date, updateDate: date, // accessDate: date, caches: {} }; this.cacheConfig.createDate ??= date; this.cacheConfig.updateDate ??= date; this.cacheConfig.caches ??= {}; const rkey = this.config.key(rr, app); const sessionData = { key: rkey.key, lifeTime: rkey.lifeTime ?? this.config.config.lifeTime, isUpdate: false, responseHeaders: rkey.responseHeaders }; const cache = this.cacheConfig.caches[sessionData.key]; if (cache) { const existes = this.config.config.cacheDir ? FileUtils.existsSync(cache.path) : this.storage.has(cache.path); const diffTime = Date.now() - cache.createTime; if (!existes || diffTime > cache.lifeTime) { delete this.cacheConfig[sessionData.key]; this.deleteChunk(cache.path); sessionData.isUpdate = true; } else { rr.resSetHeaders({ ...cache.responseHeader, cacheFilter: `true;remainTime=${cache.lifeTime - diffTime}` }); try { const chunk = this.getChunk(cache.path); await rr.resEnd(chunk); } catch (e) { console.log("--------e", e); } } } else { sessionData.isUpdate = true; } carrier.set(_CacheFilter.CARRIER_KEY_NAME, sessionData); return sessionData.isUpdate; } deleteChunk(path) { if (this.config.config.cacheDir && FileUtils.existsSync(path)) { FileUtils.deleteFileSync(path); } else if (this.storage.has(path)) { this.storage.delete(path); } } getChunk(path) { return this.config.config.cacheDir ? FileUtils.readSync(path) : this.storage.get(path); } async proceedAfter({ rr, app, carrier }) { const sessionData = carrier.get(_CacheFilter.CARRIER_KEY_NAME); if (sessionData && sessionData.isUpdate) { try { if (this.config.config.cacheDir) { FileUtils.mkdirSync([this.config.config.cacheDir, "caches"], { recursive: true }); } const resBody = rr.resBodyData(); if (!resBody) { return true; } let p = void 0; const destFileUUID = RandomUtils.uuid4(); p = this.writeChunk(destFileUUID, resBody); const updateDateNow = Date.now(); const updateDate = new Date(updateDateNow).toISOString(); this.cacheConfig.createDate ??= updateDate; this.cacheConfig.updateDate = updateDate; const responseHeaders = {}; sessionData.responseHeaders?.map((it) => it.toLowerCase()).forEach((it) => { responseHeaders[it] = rr.resHeader(it); }); this.cacheConfig.caches[sessionData.key] = { path: p, createTime: updateDateNow, lifeTime: sessionData.lifeTime, responseHeader: responseHeaders }; if (this.config.config.cacheDir) { FileUtils.write(JSON.stringify(this.cacheConfig), { path: [this.config.config.cacheDir, "cacheConfig.json"] }); } } catch (e) { console.log("---", e); } } return true; } writeChunk(destFileUUID, resBody) { if (this.config.config.cacheDir) { return FileUtils.write(resBody, { path: [this.config.config.cacheDir, "caches", destFileUUID] }); } else { this.storage.set(destFileUUID, resBody); return destFileUUID; } } }; export { CacheFilter }; //# sourceMappingURL=CacheFilter.js.map