@jointly/cache-candidate 3.0.0 → 3.1.0
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/dist/internal.d.ts +2 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var G=Object.create;var y=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,te=Object.prototype.hasOwnProperty;var
|
|
1
|
+
"use strict";var G=Object.create;var y=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,te=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ne=(e,t)=>{for(var n in t)y(e,n,{get:t[n],enumerable:!0})},_=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Z(t))!te.call(e,a)&&a!==n&&y(e,a,{get:()=>t[a],enumerable:!(r=Y(t,a))||r.enumerable});return e};var T=(e,t,n)=>(n=e!=null?G(ee(e)):{},_(t||!e||!e.__esModule?y(n,"default",{value:e,enumerable:!0}):n,e)),ae=e=>_(y({},"__esModule",{value:!0}),e);var x=F((Ie,N)=>{"use strict";var w=Object.defineProperty,re=Object.getOwnPropertyDescriptor,oe=Object.getOwnPropertyNames,ie=Object.prototype.hasOwnProperty,ce=(e,t)=>{for(var n in t)w(e,n,{get:t[n],enumerable:!0})},se=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of oe(t))!ie.call(e,a)&&a!==n&&w(e,a,{get:()=>t[a],enumerable:!(r=re(t,a))||r.enumerable});return e},ue=e=>se(w({},"__esModule",{value:!0}),e),S={};ce(S,{Hooks:()=>I});N.exports=ue(S);var I=(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))(I||{})});var q=F((Ne,U)=>{"use strict";var b=Object.defineProperty,de=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyNames,he=Object.prototype.hasOwnProperty,Ce=(e,t)=>{for(var n in t)b(e,n,{get:t[n],enumerable:!0})},fe=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of le(t))!he.call(e,a)&&a!==n&&b(e,a,{get:()=>t[a],enumerable:!(r=de(t,a))||r.enumerable});return e},me=e=>fe(b({},"__esModule",{value:!0}),e),j={};Ce(j,{Hook:()=>ye,hook:()=>Te});U.exports=me(j);var pe={before:()=>{},after:()=>{}},ge={before:()=>{},after:()=>{}};function ye(e){return function(t,n,r){let a={},o=r.value,{before:i,after:l}={...pe,...e};return r.value=function(...c){i({context:a,args:c,target:t,propertyKey:n,descriptor:r});let s=o.apply(this,c);return l({context:a,args:c,target:t,propertyKey:n,descriptor:r,result:s}),s},r}}function Te(e){return function(t){let n={},{before:r,after:a}={...ge,...e};return function(...o){r({context:n,args:o});let i=t(...o);return a({context:n,args:o,result:i}),i}}}});var He={};ne(He,{CacheCandidateOptionsDefault:()=>m,cacheCandidate:()=>z});module.exports=ae(He);var M=(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 m={ttl:6e5,timeFrame:3e4,requestsThreshold:1,cache:M(),expirationMode:"default",keepAlive:!1,plugins:[],fetchingMode:"default",events:{onCacheHit:e=>e,onCacheSet:e=>e,onCacheDelete:e=>e,onBeforeFunctionExecution:e=>e,onAfterFunctionExecution:e=>e}};var E=Symbol("DataCacheRecordNotFound"),v=Symbol("RunningQueryRecordNotFound");var V=T(x());var X=require("crypto");var W=T(x()),K=T(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,K.hook)({before:()=>{u(e,n.plugins,r)},after:({result:a})=>{u(t,n.plugins,{...r,result:a})}})}function L({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(!W.Hooks[n.hook])throw new Error(`Invalid hook ${n}`)})})}var d=T(x());function xe(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 Re({options:e,key:t,timeframeCache:n}){if(n.has(t)){let r=n.get(t),a=r.filter(({executionEnd:o})=>xe(o,e));r.length!==a.length&&n.set(t,a)}}function Pe({key:e,runningQueryCache:t}){return t.has(e)?t.get(e):v}function A(e){return(0,X.createHash)("sha256").update(e.join("|")).digest("hex")}function R({birthTime:e,options:t}){return Date.now()-e>=t.ttl}async function P({options:e,key:t,HookPayload:n,staleMap:r}){if(await e.cache.has(t)){let{result:a,birthTime:o}=await e.cache.get(t);return R({birthTime:o,options:e})?(await p({options:e,key:t,HookPayload:n,result:a,staleMap:r}),E):a}return E}async function D({options:e,key:t,result:n,HookPayload:r}){return await u(d.Hooks.DATACACHE_RECORD_ADD_PRE,e.plugins,r),e.cache.set(t,{result:n,birthTime:Date.now()},e.ttl).then(async()=>{await u(d.Hooks.DATACACHE_RECORD_ADD_POST,e.plugins,{...r,result:n}),e.events.onCacheSet({key:t})})}async function p({options:e,key:t,HookPayload:n,result:r,staleMap:a,forceDeleteFn:o}){e.fetchingMode==="stale-while-revalidate"&&a.set(t,r);let i=(o??!1)||H(e.expirationMode,"call-delete")?e.cache.delete:()=>Promise.resolve();$(d.Hooks.DATACACHE_RECORD_DELETE_PRE,d.Hooks.DATACACHE_RECORD_DELETE_POST,e,n)(i)(t).then(()=>{e.events.onCacheDelete({key:t})})}function H(e,t){switch(t){case"set-timeout":return e==="default"||e==="timeout-only";case"call-delete":return e==="default"}}async function Q({result:e,runningQueryCache:t,key:n,executionStart:r,options:a,timeframeCache:o,timeoutCache:i,args:l,staleMap:c,HookPayload:s}){let C=Date.now(),f=C-r;a.events.onAfterFunctionExecution({key:n,executionTime:f}),Ae({key:n,timeframeCache:o,executionTime:f,executionEnd:C}),await O({options:a,key:n,timeframeCache:o,executionTime:f,args:l})>=a.requestsThreshold?D({options:a,key:n,result:e,HookPayload:s}).finally(()=>{t.delete(n),H(a.expirationMode,"set-timeout")&&i.set(n,setTimeout(()=>{p({options:a,key:n,HookPayload:s,result:e,staleMap:c}),i.delete(n)},a.ttl).unref())}):t.delete(n)}async function O({options:e,key:t,timeframeCache:n,executionTime:r,args:a}){let o=0,i=n.get(t);return e.candidateFunction?o=await Oe(e,r,a,i):e.millisecondThreshold?o=De(r,e,i):o=i.length,o}function De(e,t,n){let r=0;return e>t.millisecondThreshold&&(r=n.filter(a=>a.executionTime>t.millisecondThreshold).length),r}async function Oe(e,t,n,r){let a=0;return await e.candidateFunction({timeFrameCacheRecords:r,options:e,args:n})&&(a=e.requestsThreshold),a}function ve({timeoutCache:e,key:t}){e.get(t)?.refresh()}function we(e=10){return Math.random().toString(36).substring(2,e+2)}function J(e){if(e.expirationMode==="eject"&&e.keepAlive===!0)throw new Error(`The cache-candidate can't support the "eject" expiration mode with the "keepAlive" option.`)}function be(){return{getDataCacheKey:A,getDataCacheRecord:P,addDataCacheRecord:D,deleteDataCacheRecord:p,isDataCacheRecordExpired:R,getExceedingAmount:O}}async function k({options:e,key:t,timeoutCache:n,runningQueryCache:r,timeframeCache:a,args:o,staleMap:i,originalMethod:l}){let c={options:e,key:t,timeoutCache:n,runningQueryCache:r,timeframeCache:a,fnArgs:o,internals:be()};await u(d.Hooks.INIT,e.plugins,c);let s=await P({options:e,key:t,HookPayload:c,staleMap:i});if(s!==E)return e.keepAlive&&H(e.expirationMode,"set-timeout")&&ve({timeoutCache:n,key:t}),await u(d.Hooks.CACHE_HIT,e.plugins,{...c,result:s}),e.events.onCacheHit({key:t}),Promise.resolve(s);let C=Pe({key:t,runningQueryCache:r});if(C!==v)return await u(d.Hooks.CACHE_HIT,e.plugins,{...c,result:C}),e.events.onCacheHit({key:t}),C;if(e.fetchingMode==="stale-while-revalidate"&&i.has(t)){let g=i.get(t);return await u(d.Hooks.CACHE_HIT,e.plugins,{...c,result:g}),e.events.onCacheHit({key:t}),i.delete(t),k({options:e,key:t,timeoutCache:n,runningQueryCache:r,timeframeCache:a,args:o,staleMap:i,originalMethod:l}),Promise.resolve(g)}Re({options:e,key:t,timeframeCache:a}),await u(d.Hooks.EXECUTION_PRE,e.plugins,c),e.events.onBeforeFunctionExecution({key:t});let f=Date.now(),h=l(...o);return await u(d.Hooks.EXECUTION_POST,e.plugins,{...c,result:h}),h instanceof Promise?(r.set(t,h),h.then(g=>Q({result:g,runningQueryCache:r,key:t,executionStart:f,options:e,timeframeCache:a,timeoutCache:n,args:o,staleMap:i,HookPayload:c})).catch(()=>{r.delete(t)}),h):(Q({result:h,runningQueryCache:r,key:t,executionStart:f,options:e,timeframeCache:a,timeoutCache:n,args:o,staleMap:i,HookPayload:c}),h)}function B(e){let t=new Map,n=new Map,r=we(),a=new Map,o={...m,...e,events:{...m.events,...e.events||{}}};return{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:r,timeoutCache:a,options:o,staleMap:new Map}}function z(e,t={}){let{timeframeCache:n,runningQueryCache:r,uniqueIdentifier:a,timeoutCache:o,options:i,staleMap:l}=B(t);return L({options:i}),J(i),u(V.Hooks.SETUP,i.plugins,{options:{...i,plugins:void 0},key:"",timeoutCache:o,runningQueryCache:r,timeframeCache:n,fnArgs:[],internals:{getDataCacheKey:A,getDataCacheRecord:P,addDataCacheRecord:D,deleteDataCacheRecord:p,isDataCacheRecordExpired:R,getExceedingAmount:O}}),function(...s){return k({options:i,key:t.customKeyFunction?t.customKeyFunction(s):A([a,JSON.stringify(s)]),timeoutCache:o,runningQueryCache:r,timeframeCache:n,args:s,staleMap:l,originalMethod:e})}}0&&(module.exports={CacheCandidateOptionsDefault,cacheCandidate});
|
package/dist/internal.d.ts
CHANGED
|
@@ -17,12 +17,13 @@ export declare function addDataCacheRecord({ options, key, result, HookPayload }
|
|
|
17
17
|
result: unknown;
|
|
18
18
|
HookPayload: PluginPayload;
|
|
19
19
|
}): Promise<void>;
|
|
20
|
-
export declare function deleteDataCacheRecord({ options, key, HookPayload, result, staleMap }: {
|
|
20
|
+
export declare function deleteDataCacheRecord({ options, key, HookPayload, result, staleMap, forceDeleteFn }: {
|
|
21
21
|
options: CacheCandidateOptions;
|
|
22
22
|
key: string;
|
|
23
23
|
HookPayload: PluginPayload;
|
|
24
24
|
result: unknown;
|
|
25
25
|
staleMap: StaleMap;
|
|
26
|
+
forceDeleteFn?: boolean;
|
|
26
27
|
}): Promise<void>;
|
|
27
28
|
export declare function getExceedingAmount({ options, key, timeframeCache, executionTime, args }: {
|
|
28
29
|
options: any;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jointly/cache-candidate",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.1.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@commitlint/cli": "^19.7.1",
|
|
36
36
|
"@commitlint/config-conventional": "^19.7.1",
|
|
37
|
-
"@jointly/cache-candidate-plugin-base": "^2.
|
|
37
|
+
"@jointly/cache-candidate-plugin-base": "^2.1.1",
|
|
38
38
|
"@types/node": "^22.13.5",
|
|
39
39
|
"@types/redis": "^4.0.11",
|
|
40
40
|
"@typescript-eslint/eslint-plugin": "^8.24.1",
|