'use strict'; var g = require('path'); var fs = require('fs'); var crypto = require('crypto'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var g__default = /*#__PURE__*/_interopDefault(g); var F=Object.defineProperty;var A=(o,e,t)=>e in o?F(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var a=(o,e,t)=>(A(o,typeof e!="symbol"?e+"":e,t),t);var y=({fileName:o,key:e,hash:t="sha256",encoding:i="hex"})=>`${o?`${o} hash_`:"hash_"}${crypto.createHash(t).update(e).digest(i)}`;var I=()=>({set:0,get:0,invalidate:0,validateFile:0,flushByRegex:0,flushAll:0}),x={logs:[],count:{success:I(),error:I()}},n=JSON.parse(JSON.stringify(x)),u=class{get(){return n}set({sizeInBytes:e,storedFilesCount:t}){n.logs.length>=500&&(n.logs=[]),n.logs.push({id:new Date().getTime(),bytes:e,megabytes:e/(1024*1024),storedFilesCount:t,date:new Date,count:JSON.parse(JSON.stringify(n.count))});}reset(){n=JSON.parse(JSON.stringify(x));}};var m={after:25,count:0},b={after:50,count:0},d=class{constructor({basePath:e,defaultTTL:t,hash:i,debug:s,autoInvalidate:r,enableMonitoring:c}){a(this,"basePath");a(this,"defaultTTL");a(this,"hash");a(this,"debug");a(this,"autoInvalidate");a(this,"enableMonitoring");a(this,"monitoringInstance");this.basePath=e||"./.file-sys-cache",this.defaultTTL=t||60,this.hash=i||"sha256",this.debug=s||!1,this.autoInvalidate=r||!1,this.enableMonitoring=c||!1;}async set({fileName:e="",key:t,payload:i,ttl:s=this.defaultTTL}){let r=s,c=y({fileName:e,key:t,hash:this.hash});try{let l=g__default.default.resolve(this.basePath);await fs.promises.mkdir(l,{recursive:!0});let p=g__default.default.resolve(l,c),P={payload:Object(i),ttl:r,expiration:r?Date.now()+r*1e3:null};return await fs.promises.writeFile(p,JSON.stringify(P)),this.debug&&console.info(`Data stored successfully to ${this.basePath}/${c} | TTL: ${r}`),this.enableMonitoring&&(n.count.success.set++,this.setLogs()),p}catch(l){throw this.debug&&console.error("Error setting file:",l),this.enableMonitoring&&n.count.error.set++,l}}async get({fileName:e="",key:t}){this.autoInvalidate&&(m.count++,m.count>=m.after&&(this.invalidate().catch(()=>{}),m.count=0));let i=y({fileName:e,key:t,hash:this.hash});try{let s=g__default.default.resolve(this.basePath,`${i}`);await fs.promises.stat(s);let r=await this.readFileAndParse(s);return this.enableMonitoring&&(n.count.success.get++,this.setLogs()),r.payload}catch(s){throw this.debug&&console.error("Error during file retrieval:",s),this.enableMonitoring&&n.count.error.get++,s}}async files(){let e=[];try{let t=fs.readdirSync(this.basePath);for(let i of t){let s=fs.statSync(`${this.basePath}/${i}`);try{let{ttl:r,expiration:c,expiresIn:l}=await this.validateFile(i)||{ttl:0,expiration:0,expiresIn:0};e.push({name:i,size:{bytes:s.size,megabytes:s.size/(1024*1024)},ttl:r,expiration:c,expires_in:l});}catch{}}}catch{}return e}async invalidate(){try{let e=fs.readdirSync(this.basePath);for(let t of e)try{let{expiresIn:i}=await this.validateFile(t)||{expiresIn:0};(i||0)<=0&&fs.unlinkSync(`${this.basePath}/${t}`);}catch{}this.enableMonitoring&&(n.count.success.invalidate++,this.log());}catch{this.enableMonitoring&&n.count.error.invalidate++;}}async validateFile(e){try{let t=g__default.default.resolve(this.basePath,`${e}`);await fs.promises.stat(t);let i=await this.readFileAndParse(t),s=i.expiration?Math.max(0,(i.expiration-Date.now())/1e3):null;return this.enableMonitoring&&n.count.success.validateFile++,{ttl:i.ttl,expiration:i.expiration,expiresIn:s}}catch(t){this.debug&&console.error("Error validateFile:",t),this.enableMonitoring&&n.count.error.validateFile++;}}flushByRegex(...e){try{let t=fs.readdirSync(this.basePath);for(let i of t)e.every(s=>i.match(s))&&fs.unlinkSync(`${this.basePath}/${i}`);this.enableMonitoring&&n.count.success.flushByRegex++;}catch(t){this.debug&&console.error("Error flushByRegex:",t),this.enableMonitoring&&n.count.error.flushByRegex++;}}flushAll(){fs.rmSync(this.basePath,{recursive:!0,force:!0}),this.enableMonitoring&&n.count.success.flushAll++;}get monitoring(){return !this.monitoringInstance&&this.enableMonitoring&&(this.monitoringInstance=new u),this.monitoringInstance}setLogs(){b.count++,b.count>=b.after&&(this.log(),b.count=0);}log(){let e=0,t=0;try{let i=fs.readdirSync(this.basePath);for(let s of i){t++;let r=fs.statSync(`${this.basePath}/${s}`);e+=r.size;}}catch{}this.monitoring?.set({sizeInBytes:e,storedFilesCount:t});}async readFileAndParse(e){let t=await fs.promises.readFile(e,"utf-8");return JSON.parse(t)}};var w=(o,e="sha256",t="hex")=>{let i=JSON.stringify(o);return crypto.createHash(e).update(i).digest(t)}; exports.FileSysCache = d; exports.generateKey = w; //# sourceMappingURL=out.js.map //# sourceMappingURL=file-sys-cache.cjs.map