@jointly/cache-candidate 1.5.7 → 1.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var G=Object.create;var A=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),te=(e,t)=>{for(var n in t)A(e,n,{get:t[n],enumerable:!0})},S=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Y(t))!ee.call(e,a)&&a!==n&&A(e,a,{get:()=>t[a],enumerable:!(r=V(t,a))||r.enumerable});return e};var D=(e,t,n)=>(n=e!=null?G(Z(e)):{},S(t||!e||!e.__esModule?A(n,"default",{value:e,enumerable:!0}):n,e)),ne=e=>S(A({},"__esModule",{value:!0}),e);var O=F((
|
|
1
|
+
"use strict";var G=Object.create;var A=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),te=(e,t)=>{for(var n in t)A(e,n,{get:t[n],enumerable:!0})},S=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Y(t))!ee.call(e,a)&&a!==n&&A(e,a,{get:()=>t[a],enumerable:!(r=V(t,a))||r.enumerable});return e};var D=(e,t,n)=>(n=e!=null?G(Z(e)):{},S(t||!e||!e.__esModule?A(n,"default",{value:e,enumerable:!0}):n,e)),ne=e=>S(A({},"__esModule",{value:!0}),e);var O=F((Ne,j)=>{"use strict";var b=Object.defineProperty,ae=Object.getOwnPropertyDescriptor,re=Object.getOwnPropertyNames,oe=Object.prototype.hasOwnProperty,ie=(e,t)=>{for(var n in t)b(e,n,{get:t[n],enumerable:!0})},ce=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of re(t))!oe.call(e,a)&&a!==n&&b(e,a,{get:()=>t[a],enumerable:!(r=ae(t,a))||r.enumerable});return e},se=e=>ce(b({},"__esModule",{value:!0}),e),I={};ie(I,{Hooks:()=>M});j.exports=se(I);var M=(e=>(e.SETUP="SETUP",e.INIT="INIT",e.EXECUTION_PRE="EXECUTION_PRE",e.EXECUTION_POST="EXECUTION_POST",e.DATACACHE_RECORD_ADD_PRE="DATACACHE_RECORD_ADD_PRE",e.DATACACHE_RECORD_ADD_POST="DATACACHE_RECORD_ADD_POST",e.DATACACHE_RECORD_DELETE_PRE="DATACACHE_RECORD_DELETE_PRE",e.DATACACHE_RECORD_DELETE_POST="DATACACHE_RECORD_DELETE_POST",e.CACHE_HIT="CACHE_HIT",e))(M||{})});var q=F((Ie,Q)=>{"use strict";var v=Object.defineProperty,ue=Object.getOwnPropertyDescriptor,de=Object.getOwnPropertyNames,le=Object.prototype.hasOwnProperty,Ce=(e,t)=>{for(var n in t)v(e,n,{get:t[n],enumerable:!0})},he=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of de(t))!le.call(e,a)&&a!==n&&v(e,a,{get:()=>t[a],enumerable:!(r=ue(t,a))||r.enumerable});return e},fe=e=>he(v({},"__esModule",{value:!0}),e),U={};Ce(U,{Hook:()=>ge,hook:()=>ye});Q.exports=fe(U);var me={before:()=>{},after:()=>{}},pe={before:()=>{},after:()=>{}};function ge(e){return function(t,n,r){let a={},o=r.value,{before:i,after:c}={...me,...e};return r.value=function(...s){i({context:a,args:s,target:t,propertyKey:n,descriptor:r});let d=o.apply(this,s);return c({context:a,args:s,target:t,propertyKey:n,descriptor:r,result:d}),d},r}}function ye(e){return function(t){let n={},{before:r,after:a}={...pe,...e};return function(...o){r({context:n,args:o});let i=t(...o);return a({context:n,args:o,result:i}),i}}}});var we={};te(we,{CacheCandidate:()=>K,CacheCandidateOptionsDefault:()=>E,cacheCandidate:()=>B});module.exports=ne(we);var N=(e=new Map)=>({get:async t=>e.get(JSON.stringify(t)),has:async t=>e.has(JSON.stringify(t)),set:async(t,n)=>e.set(JSON.stringify(t),n),delete:async t=>e.delete(JSON.stringify(t))});var E={ttl:6e5,timeFrame:3e4,requestsThreshold:3,cache:N(),keepAlive:!1,plugins:[],events:{onCacheHit:e=>e,onCacheSet:e=>e,onCacheDelete:e=>e,onBeforeFunctionExecution:e=>e,onAfterFunctionExecution:e=>e}};var P=Symbol("DataCacheRecordNotFound"),x=Symbol("RunningQueryRecordNotFound");var k=D(O());var X=require("crypto");var J=D(O()),W=D(q());async function u(e,t,n){for(let r of t){let a=r.hooks.filter(i=>i.hook===e);if(a.length>1)throw new Error(`Only one hook instance per plugin is allowed. ${r.name} has ${a.length} instances of ${e}}`);if(a.length===0)continue;let o=a[0];if(Ee(o))await o.action(n,r.additionalParameters);else throw new Error(`Hook ${e} for plugin ${r.name} is not actionable.`)}}function Ee(e){return[e.action!==void 0,typeof e.action=="function"].every(t=>t===!0)}function $(e,t,n,r){return(0,W.hook)({before:()=>{u(e,n.plugins,r)},after:({result:a})=>{u(t,n.plugins,{...r,result:a})}})}function w({options:e}){e.plugins&&e.plugins.forEach(t=>{if(!t.hooks||!Array.isArray(t.hooks)||t.hooks.length===0)throw new Error(`Plugin ${t.name} has no hooks.`);t.hooks.forEach(n=>{if(!J.Hooks[n.hook])throw new Error(`Invalid hook ${n}`)})})}var C=D(O());function Te(e,t){return Date.now()<e+t.timeFrame}function Ae({key:e,timeframeCache:t,executionTime:n,executionEnd:r}){t.has(e)?t.get(e).push({executionTime:n,executionEnd:r}):t.set(e,[{executionEnd:r,executionTime:n}])}function De({options:e,key:t,timeframeCache:n}){if(n.has(t)){let r=n.get(t),a=r.filter(({executionEnd:o})=>Te(o,e));r.length!==a.length&&n.set(t,a)}}function Pe({key:e,runningQueryCache:t}){return t.has(e)?t.get(e):x}function y(...e){return(0,X.createHash)("sha256").update(e.join("|")).digest("hex")}function m({birthTime:e,options:t}){return Date.now()-e>=t.ttl}async function p({options:e,key:t,HookPayload:n}){if(await e.cache.has(t)){let{result:r,birthTime:a}=await e.cache.get(t);return m({birthTime:a,options:e})?(await f({options:e,key:t,HookPayload:n}),P):r}return P}async function g({options:e,key:t,result:n,HookPayload:r}){return await u(C.Hooks.DATACACHE_RECORD_ADD_PRE,e.plugins,r),e.cache.set(t,{result:n,birthTime:Date.now()},e.ttl).then(async()=>{await u(C.Hooks.DATACACHE_RECORD_ADD_POST,e.plugins,{...r,result:n}),e.events.onCacheSet({key:t})})}async function f({options:e,key:t,HookPayload:n}){$(C.Hooks.DATACACHE_RECORD_DELETE_PRE,C.Hooks.DATACACHE_RECORD_DELETE_POST,e,n)(e.cache.delete)(t).then(()=>{e.events.onCacheDelete({key:t})})}async function L({result:e,runningQueryCache:t,key:n,executionStart:r,options:a,timeframeCache:o,timeoutCache:i,args:c,HookPayload:s}){let d=Date.now(),h=d-r;a.events.onAfterFunctionExecution({key:n,executionTime:h}),Ae({key:n,timeframeCache:o,executionTime:h,executionEnd:d}),await T({options:a,key:n,timeframeCache:o,executionTime:h,args:c})>=a.requestsThreshold?g({options:a,key:n,result:e,HookPayload:s}).finally(()=>{t.delete(n),i.set(n,setTimeout(()=>{f({options:a,key:n,HookPayload:s}),i.delete(n)},a.ttl).unref())}):t.delete(n)}async function T({options:e,key:t,timeframeCache:n,executionTime:r,args:a}){let o=0,i=n.get(t);return e.candidateFunction?o=await Re(e,r,a,i):e.millisecondThreshold?o=Oe(r,e,i):o=i.length,o}function Oe(e,t,n){let r=0;return e>t.millisecondThreshold&&(r=n.filter(a=>a.executionTime>t.millisecondThreshold).length),r}async function Re(e,t,n,r){let a=0;return await e.candidateFunction({timeFrameCacheRecords:r,options:e,args:n})&&(a=e.requestsThreshold),a}function xe({timeoutCache:e,key:t}){e.get(t)?.refresh()}function be(e=10){return Math.random().toString(36).substring(2,e+2)}function ve(){let e={getDataCacheKey:y,getDataCacheRecord:p,addDataCacheRecord:g,deleteDataCacheRecord:f,isDataCacheRecordExpired:m,getExceedingAmount:T};return Object.defineProperty(e,"getDataCacheRecord",{value:p,writable:!1}),Object.defineProperty(e,"addDataCacheRecord",{value:g,writable:!1}),Object.defineProperty(e,"deleteDataCacheRecord",{value:f,writable:!1}),Object.defineProperty(e,"isDataCacheRecordExpired",{value:m,writable:!1}),e}async function _({options:e,key:t,timeoutCache:n,runningQueryCache:r,timeframeCache:a,args:o,originalMethod:i}){let c={options:e,key:t,timeoutCache:n,runningQueryCache:r,timeframeCache:a,fnArgs:o,internals:ve()};await u(C.Hooks.INIT,e.plugins,c);let s=await p({options:e,key:t,HookPayload:c});if(s!==P)return e.keepAlive&&xe({timeoutCache:n,key:t}),await u(C.Hooks.CACHE_HIT,e.plugins,{...c,result:s}),e.events.onCacheHit({key:t}),Promise.resolve(s);let d=Pe({key:t,runningQueryCache:r});if(d!==x)return await u(C.Hooks.CACHE_HIT,e.plugins,{...c,result:d}),d;De({options:e,key:t,timeframeCache:a}),await u(C.Hooks.EXECUTION_PRE,e.plugins,c),e.events.onBeforeFunctionExecution({key:t});let h=Date.now(),l=i(...o);return await u(C.Hooks.EXECUTION_POST,e.plugins,{...c,result:l}),l instanceof Promise?(r.set(t,l),l.then(R=>L({result:R,runningQueryCache:r,key:t,executionStart:h,options:e,timeframeCache:a,timeoutCache:n,args:o,HookPayload:c})),l):(L({result:l,runningQueryCache:r,key:t,executionStart:h,options:e,timeframeCache:a,timeoutCache:n,args:o,HookPayload:c}),l)}function H(e){let t=new Map,n=new Map,r=be(),a=new Map,o={...E,...e,events:{...E.events,...e.events||{}}};return{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:r,timeoutCache:a,options:o}}function K(e={}){let{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:r,timeoutCache:a,options:o}=H(e);return w({options:o}),u(k.Hooks.SETUP,o.plugins,{options:{...o,plugins:void 0},key:"",timeoutCache:a,runningQueryCache:n,timeframeCache:t,fnArgs:[],internals:{getDataCacheKey:y,getDataCacheRecord:p,addDataCacheRecord:g,deleteDataCacheRecord:f,isDataCacheRecordExpired:m,getExceedingAmount:T}}),function(c,s,d){let h=d.value;d.value=async function(...l){let R=c.constructor.name+JSON.stringify(this),z=y([s,r,R,JSON.stringify(l)]);return _({options:o,key:z,timeoutCache:a,runningQueryCache:n,timeframeCache:t,args:l,originalMethod:h})}}}function B(e,t={}){let{timeframeCache:n,runningQueryCache:r,uniqueIdentifier:a,timeoutCache:o,options:i}=H(t);return w({options:i}),u(k.Hooks.SETUP,i.plugins,{options:{...i,plugins:void 0},key:"",timeoutCache:o,runningQueryCache:r,timeframeCache:n,fnArgs:[],internals:{getDataCacheKey:y,getDataCacheRecord:p,addDataCacheRecord:g,deleteDataCacheRecord:f,isDataCacheRecordExpired:m,getExceedingAmount:T}}),function(...s){return _({options:i,key:y([a,JSON.stringify(s)]),timeoutCache:o,runningQueryCache:r,timeframeCache:n,args:s,originalMethod:e})}}0&&(module.exports={CacheCandidate,CacheCandidateOptionsDefault,cacheCandidate});
|