@altostra/core
Version:
Core library for shared types and logic
1 lines • 5.07 kB
JavaScript
"use strict";var _DataCache_cachePath,_DataCache_fetch,_DataCache_validate,_DataCache_validateCache,_DataCache_debug,_DataCache_saveOptions,_DataCache_ttl,__awaiter=this&&this.__awaiter||function(a,e,t,i){return new(t||(t=Promise))((function(s,c){function r(a){try{h(i.next(a))}catch(a){c(a)}}function _(a){try{h(i.throw(a))}catch(a){c(a)}}function h(a){var e;a.done?s(a.value):(e=a.value,e instanceof t?e:new t((function(a){a(e)}))).then(r,_)}h((i=i.apply(a,e||[])).next())}))},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(a,e,t,i,s){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?a!==e||!s:!e.has(a))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?s.call(a,t):s?s.value=t:e.set(a,t),t},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(a,e,t,i){if("a"===t&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?a!==e||!i:!e.has(a))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===t?i:"a"===t?i.call(a):i?i.value:e.get(a)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.DataCache=void 0;const fs_1=require("fs"),ISODate_1=require("../CustomTypes/ISODate"),AltoError_1=require("../Errors/AltoError"),Logging_1=require("../Logging"),FS_1=require("./FS"),type_validations_1=require("@altostra/type-validations"),RejectionReasons_1=require("@altostra/type-validations/lib/RejectionReasons"),MILLISECS_IN_A_SECOND=1e3;class DataCache{constructor({cachePath:a,fetch:e,validate:t,debugLogger:i,saveOptions:s,ttlInSeconds:c}){_DataCache_cachePath.set(this,void 0),_DataCache_fetch.set(this,void 0),_DataCache_validate.set(this,void 0),_DataCache_validateCache.set(this,void 0),_DataCache_debug.set(this,void 0),_DataCache_saveOptions.set(this,void 0),_DataCache_ttl.set(this,void 0),__classPrivateFieldSet(this,_DataCache_cachePath,a,"f"),__classPrivateFieldSet(this,_DataCache_fetch,e,"f"),__classPrivateFieldSet(this,_DataCache_validate,(0,RejectionReasons_1.asRejectingValidator)(t),"f"),__classPrivateFieldSet(this,_DataCache_validateCache,cachedDataOf(t),"f"),__classPrivateFieldSet(this,_DataCache_debug,null!=i?i:(0,Logging_1.nullLogger)(),"f"),__classPrivateFieldSet(this,_DataCache_ttl,c,"f"),__classPrivateFieldSet(this,_DataCache_saveOptions,Object.assign(Object.assign({},defaultSaveOptions),s),"f")}updateCache(){return __awaiter(this,void 0,void 0,(function*(){try{const a=yield __classPrivateFieldGet(this,_DataCache_fetch,"f").call(this);this._validateData(a,"Fetched cache data is invalid.");const e=Object.assign({data:a},__classPrivateFieldGet(this,_DataCache_ttl,"f")&&{expires:new Date(Date.now()+1e3*__classPrivateFieldGet(this,_DataCache_ttl,"f")).toISOString()});try{yield(0,FS_1.saveJsonFile)(__classPrivateFieldGet(this,_DataCache_cachePath,"f"),e,Object.assign(Object.assign({},__classPrivateFieldGet(this,_DataCache_saveOptions,"f")),{createDir:!0}))}catch(a){__classPrivateFieldGet(this,_DataCache_debug,"f").error("Failed to save data to cache.",a)}return a}catch(a){return void __classPrivateFieldGet(this,_DataCache_debug,"f").error("Failed to populate cache.",a)}}))}get(){var a;return __awaiter(this,void 0,void 0,(function*(){return null!==(a=yield this._tryGet())&&void 0!==a?a:yield this.updateCache()}))}getWithUpdates(){return __awaiter(this,void 0,void 0,(function*(){let a=!1,e=yield this._tryGet();return null==e&&(a=!0,e=yield this.updateCache()),{get cached(){return e},get isUpdated(){return a},getUpdated:()=>__awaiter(this,void 0,void 0,(function*(){return a||(a=!0,e=yield this.updateCache()),e}))}}))}clearLocal(){return __awaiter(this,void 0,void 0,(function*(){try{yield fs_1.promises.unlink(__classPrivateFieldGet(this,_DataCache_cachePath,"f"))}catch(a){__classPrivateFieldGet(this,_DataCache_debug,"f").verbose("Failed to delete cached data: ",a)}}))}_tryGet(){return __awaiter(this,void 0,void 0,(function*(){try{const a=yield(0,FS_1.loadJsonFile)(__classPrivateFieldGet(this,_DataCache_cachePath,"f"),__classPrivateFieldGet(this,_DataCache_validateCache,"f"));return isExpired(a.expires)?void 0:a.data}catch(a){return void __classPrivateFieldGet(this,_DataCache_debug,"f").verbose("Failed to load cache data.",a)}}))}_validateData(a,e){const t=[];if(!__classPrivateFieldGet(this,_DataCache_validate,"f").call(this,a,a=>t.push(a)))throw AltoError_1.AltoError.create(e,{data:{invalidData:a,rejections:t}})}}exports.DataCache=DataCache,_DataCache_cachePath=new WeakMap,_DataCache_fetch=new WeakMap,_DataCache_validate=new WeakMap,_DataCache_validateCache=new WeakMap,_DataCache_debug=new WeakMap,_DataCache_saveOptions=new WeakMap,_DataCache_ttl=new WeakMap;const defaultSaveOptions={indent:2};function cachedDataOf(a){return(0,type_validations_1.objectOf)({data:a,expires:(0,type_validations_1.maybe)(ISODate_1.isISODate)})}function isExpired(a){return!!a&&new Date(a)<new Date}