@graphql-box/cache-manager 5.4.11 → 5.4.13
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/LICENSE +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/production.analysis.txt +49 -49
- package/dist/types/cjs/debug/logCacheEntry.d.cts.map +1 -1
- package/dist/types/cjs/debug/logCacheQuery.d.cts.map +1 -1
- package/dist/types/cjs/debug/logPartialCompiled.d.cts.map +1 -1
- package/dist/types/cjs/helpers/isFirstResponseChunk.d.cts.map +1 -1
- package/dist/types/cjs/helpers/isLastResponseChunk.d.cts.map +1 -1
- package/dist/types/cjs/helpers/isNotLastResponseChunk.d.cts.map +1 -1
- package/dist/types/cjs/helpers/isNotResponseChunk.d.cts.map +1 -1
- package/dist/types/cjs/main.d.cts.map +1 -1
- package/dist/types/esm/debug/logCacheEntry.d.ts.map +1 -1
- package/dist/types/esm/debug/logCacheQuery.d.ts.map +1 -1
- package/dist/types/esm/debug/logPartialCompiled.d.ts.map +1 -1
- package/dist/types/esm/helpers/isFirstResponseChunk.d.ts.map +1 -1
- package/dist/types/esm/helpers/isLastResponseChunk.d.ts.map +1 -1
- package/dist/types/esm/helpers/isNotLastResponseChunk.d.ts.map +1 -1
- package/dist/types/esm/helpers/isNotResponseChunk.d.ts.map +1 -1
- package/dist/types/esm/main.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/__snapshots__/index.test.ts.snap +10 -10
- package/src/debug/logCacheEntry.ts +9 -12
- package/src/debug/logCacheQuery.ts +8 -11
- package/src/debug/logPartialCompiled.ts +2 -5
- package/src/helpers/filterQuery.ts +4 -4
- package/src/helpers/isFirstResponseChunk.ts +1 -1
- package/src/helpers/isLastResponseChunk.ts +1 -1
- package/src/helpers/isNotLastResponseChunk.ts +1 -1
- package/src/helpers/isNotResponseChunk.ts +1 -1
- package/src/helpers/normalizePatchResponseData.ts +1 -1
- package/src/index.test.ts +45 -20
- package/src/main.ts +25 -22
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import"core-js/modules/web.structured-clone.js";import"core-js/modules/es.array.push.js";import{CACHE_ENTRY_ADDED as e,CACHE_ENTRY_QUERIED as t,PARTIAL_QUERY_COMPILED as a,TYPE_NAME_KEY as s,DEFAULT_TYPE_ID_KEY as n,QUERY_RESPONSES as i,REQUEST_FIELD_PATHS as r,DATA_ENTITIES as c}from"@graphql-box/core";import{isPlainObject as h,isArray as o,mergeObjects as d,isObjectLike as l,resolveFragments as y,getName as u,buildFieldKeysAndPaths as p,deleteFragmentSpreads as f,getChildFields as _,deleteChildFields as g,getInlineFragments as m,deleteInlineFragments as C,hasChildFields as D,deleteFragmentDefinitions as F,getOperationDefinitions as q,ArgsError as P,GroupedError as b,getFragmentDefinitions as R,hashRequest as E,rehydrateCacheMetadata as N,iterateChildFields as w,dehydrateCacheMetadata as M}from"@graphql-box/helpers";import{Cacheability as T}from"cacheability";import{Kind as K,print as v,OperationTypeNode as I}from"graphql";import{isNumber as O,isString as k,isEmpty as x,keys as A,merge as j,set as Q,get as S,isUndefined as $,assign as z,unset as H,isEqual as U}from"lodash-es";const G="noCache",L=(e,t)=>o(e)?e.reduce(((e,a)=>!!e&&L(a,t)),!0):!!h(e)&&V(e,t),V=(e,t)=>{const a=h(e)?Object.keys(e):[...e.keys()];return!(1!==a.length||!h(e)||!e[t])||a.reduce(((a,s)=>!!a&&(O(s)&&o(e)||k(s)&&h(e)?L(e[s],t):a)),!0)},Y=({entity:e,queryResponse:t,requestPath:a})=>!e&&!t&&!a,B=e=>!!e.entity,J=({entity:e,requestPath:t})=>!!(e??t),W=e=>!!e.queryResponse,X=e=>!!e.requestPath,Z=(e,t,a)=>d(e,t,((e,t)=>h(t)&&t[a]?t[a]:void 0)),ee=(e,t,a)=>!e&&t?t:l(e)&&l(t)?Z(e,t,a):e,te=(e,t,a)=>`${t.isEntity?t.typeName:e.__typename}::${String(e[a.typeIDKey])}`,ae=(e,t,a)=>{if(!h(e)||!(a in e))return!1;const{isEntity:s=!1,possibleTypes:n=[]}=t??{};return!!s||0!==n.length&&n.some((t=>t.typeName===e.__typename))},se=(e,t,a,s)=>{if(!h(e))return e;const n=y(t.selectionSet?.selections,s.fragmentDefinitions);return A(e).reduce(((t,i)=>{const r=n.find((({fieldNode:e})=>u(e)===i));if(r){const t=p(r.fieldNode,a,s),n=s.fieldTypeMap.get(t.requestFieldPath);(ae(e[i],n,s.typeIDKey)||n?.hasArguments||n?.hasDirectives)&&delete e[i]}return t}),e)},ne=(e,t,a,s)=>{const n=y(t.selectionSet?.selections,s.fragmentDefinitions);return Object.keys(e).reduce(((t,i)=>{const r=n.find((({fieldNode:e})=>u(e)===i));if(r){const{requestFieldPath:t}=p(r.fieldNode,a,s),n=s.fieldTypeMap.get(t);(ae(e[i],n,s.typeIDKey)||n?.hasArguments)&&delete e[i]}return t}),e)},ie=(e,t)=>{if(!e||0===e.length)return{hasData:!1,typeUnused:!!t};const[a]=e;if(a){const{hasData:e,typeName:s}=a;return{hasData:e,typeUnused:s?s!==t:void 0}}return{hasData:e.some((({hasData:e,typeName:a})=>a===t&&e)),typeUnused:!e.every((({typeName:e})=>e===t))}},re=(e,t,a,n,i)=>{const{fragmentDefinitions:r,typeIDKey:c}=i,h=_(e,{fragmentDefinitions:r});if(!h)return!1;const o={};for(let d=h.length-1;d>=0;d-=1){const l=h[d];if(!l)continue;const{fieldNode:y,fragmentKind:f,fragmentName:m,typeName:C}=l;f===K.FRAGMENT_SPREAD&&m&&!o[m]&&(o[m]={hasData:0,total:r?.[m]?_(r[m],{fragmentDefinitions:r})?.length??0:0});const F=u(y);if(F===c||F===s)continue;const{requestFieldPath:q}=p(y,{requestFieldPath:n},i),{hasData:P,typeUnused:b}=ie(t.get(q),C);if(P||b)if(f===K.FRAGMENT_SPREAD&&m){const e=o[m];e&&(e.hasData+=1)}else D(y,{fragmentDefinitions:r})?re(y,t,a,q,i)&&g(e,y):g(e,y)}return((e,t,a,s)=>{if(!x(t))for(const[n,i]of Object.entries(t)){const t=a[n];if(!t)continue;t.paths.push(s);const{hasData:r,total:c}=i;r===c&&(f(e,n),t.deleted+=1)}})(e,o,a,n),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=m(e);let n=!1;for(const i of s){const s=_(i,{fragmentDefinitions:t});if(!s||0===s.length){C(e,i),n=!0;continue}const[r]=s;if(r){const{fieldNode:t}=r;u(t)===a&&(C(e,i),n=!0)}}})(e,i),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const n=_(e,{fragmentDefinitions:t});if(!n||n.length>3)return!1;const i=n.map((({fieldNode:e})=>e.name.value));if(2===i.length&&i.every((e=>e===a||e===s)))return g(e,n.map((({fieldNode:e})=>e))),!0;if(1===i.length&&i[0]===a||i[0]===s){const[t]=n;if(t){const{fieldNode:a}=t;return g(e,a),!0}}})(e,i),!D(e,{fragmentDefinitions:r})},ce=(e,{fieldPathChecklist:t},a)=>{const{ast:s}=e,n=q(s,a.operation)[0];if(!n)return s;const{fragmentDefinitions:i,operation:r}=a,c=_(n,{fragmentDefinitions:i});if(!c)return s;const h=(({request:e},{fragmentDefinitions:t})=>A(t??{}).reduce(((t,a)=>(t[a]={deleted:0,paths:[],total:(e.match(new RegExp(`\\.\\.\\.${a}`,"g"))??[]).length},t)),{}))(e,a);for(let e=c.length-1;e>=0;e-=1){const{fieldNode:s}=c[e],{requestFieldPath:i}=p(s,{requestFieldPath:r},a);re(s,t,h,i,a)&&g(n,s)}return a.queryFiltered=!0,((e,t,a,s)=>{const n=A(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:n,paths:i,total:r}=s;return 0===n&&1===r?[...e,{name:t,path:i[0]}]:e}),[]),{fragmentDefinitions:i={}}=s;for(const{name:e,path:r}of n){const n=i[e];n&&re(n,t,a,r,s)}const r=A(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:n,total:i}=s;return n>0&&n===i?[...e,t]:e}),[]);return 0===r.length?e:F(e,{include:r})})(s,t,h,a)},he=(e,t)=>o(e)&&O(t)||h(e)&&k(t)?e[t]:void 0,oe=e=>!!l(e)&&(s in e&&k(e[s]));function de(e,t,a,s){var n,i=arguments.length,r=i<3?t:null===s?s=Object.getOwnPropertyDescriptor(t,a):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,a,s);else for(var c=e.length-1;c>=0;c--)(n=e[c])&&(r=(i<3?n(r):i>3?n(t,a,r):n(t,a))||r);return i>3&&r&&Object.defineProperty(t,a,r),r}function le(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}class ye{static _countFieldPathChecklist(e){const t={missing:0,total:0};for(const[,a]of e){t.total+=a.length;const e=a.filter((({hasData:e})=>!e));t.missing+=e.length}return t}static _getFieldDataFromAncestor(e,t){const a=he(e,t);return l(a)?structuredClone(a):a}static _getOperationCacheControl(e,t){const a="no-cache";if(!e)return a;const s=e.get(t);return s?s.printCacheControl():a}static _isNodeEntity(e){if(!e)return!1;const{isEntity:t,possibleTypes:a}=e;return t||a.some((e=>e.isEntity))}static _isNodeRequestFieldPath(e){return!!e&&(this._isNodeEntity(e)||e.hasArguments||e.hasDirectives)}static _isValid(e){return!S(e,["metadata","cacheControl",G],!1)&&e.checkTTL()}static _mergeResponseCacheMetadata(e,t){return t?new Map([...t.cacheMetadata,...e]):e}static _setCachedData(e,{data:t},a){const s=t=>{(o(e)&&O(a)||h(e))&&(e[a]=t)};l(t)||$(t)?o(t)?s([]):h(t)&&s({}):s(t)}static _setCachedResponseSlice(e,{cacheMetadata:t,data:a,fieldPathChecklist:s},{propNameOrIndex:n,requestFieldPath:i},r,c,{operation:h}){ye._setCacheMetadata(t,e.cacheability,i,h),ye._setFieldPathChecklist(s,e,i,r),ye._setCachedData(a,e,n)}static _setCacheMetadata(e,t,a,s){if(!t)return;e.set(a,t);const n=e.get(s);(!n||n.metadata.ttl>t.metadata.ttl)&&e.set(s,t)}static _setFieldPathChecklist(e,{data:t},a,{dataTypename:s,fieldTypename:n,fragmentKind:i,fragmentName:r}){if($(n)||i===K.FRAGMENT_SPREAD){if(e.has(a))return;return void e.set(a,[{fragmentKind:i,fragmentName:r,hasData:!$(t)}])}if(s!==n)return;const c=e.get(a)??[];c.some((({typeName:e})=>e===s))||e.set(a,[...c,{fragmentKind:i,fragmentName:r,hasData:!$(t),typeName:s}])}constructor(e){this._cacheTiersEnabled={entity:!1,queryResponse:!0,requestPath:!1},this._partialQueryResponses=new Map,this._responseChunksAwaitingCaching=new Map;const t=[];if("cache"in e||t.push(new P("@graphql-box/cache-manager expected cache to be in options.")),e.typeCacheDirectives&&!h(e.typeCacheDirectives)){const e="@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";t.push(new P(e))}if(t.length>0)throw new b("@graphql-box/cache-manager argument validation errors.",t);"function"==typeof e.cache?e.cache().then((e=>{this._cache=e})).catch((e=>{throw e})):this._cache=e.cache,this._cacheTiersEnabled={...this._cacheTiersEnabled,...e.cacheTiersEnabled},this._cascadeCacheControl=e.cascadeCacheControl??!1,this._fallbackOperationCacheability=e.fallbackOperationCacheability??G,this._typeCacheDirectives=e.typeCacheDirectives??{},this._typeIDKey=e.typeIDKey??n}async analyzeQuery(e,t,a){if(!J(this._cacheTiersEnabled))return{updated:e};const{ast:s,hash:n}=e,i={...a,fragmentDefinitions:R(s),typeIDKey:this._typeIDKey},r=await this._retrieveCachedResponseData(e,t,i),{cacheMetadata:c,data:h,fieldCount:o}=r;if(o.missing===o.total||((e,t)=>V(e,t))(h,this._typeIDKey))return{updated:e};if(!o.missing&&W(this._cacheTiersEnabled)){const e=this._setQueryResponseCacheEntry(n,{cacheMetadata:c,data:h},t,i);return t.awaitDataCaching&&await e,{response:{cacheMetadata:c,data:h}}}const d=ce(e,r,i),l=v(d),{fragmentDefinitions:y,typeIDKey:u,...p}=i;return z(a,{...p,filteredRequest:l}),W(this._cacheTiersEnabled)&&this._setPartialQueryResponse(n,{cacheMetadata:c,data:h},t,a),{updated:{ast:d,hash:E(l),request:l}}}get cache(){return this._cache}async cacheQuery(e,t,a,s,n){const i={...n,fragmentDefinitions:R((t??e).ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,t,a,s,i)}async cacheResponse(e,t,a,s){const n={...s,fragmentDefinitions:R(e.ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,void 0,t,a,n)}get cacheTiersEnabled(){return this._cacheTiersEnabled}async checkCacheEntry(e,t,a,s){return!Y(this._cacheTiersEnabled)&&this._checkCacheEntry(e,t,a,s)}async checkQueryResponseCacheEntry(e,t,a){if(!W(this._cacheTiersEnabled))return!1;const s=await this._checkCacheEntry(i,e,t,a);if(!s)return!1;const{cacheMetadata:n,data:r}=s.entry;return{cacheMetadata:N(n),data:r}}deletePartialQueryResponse(e){W(this._cacheTiersEnabled)&&this._partialQueryResponses.delete(e)}async setQueryResponseCacheEntry(e,t,a,s){if(W(this._cacheTiersEnabled))return this._setQueryResponseCacheEntry(e.hash,t,a,s)}async _analyzeFieldNode(e,t,a,s,n){await(D(e,{fragmentDefinitions:n.fragmentDefinitions})?this._analyzeParentFieldNode(e,t,a,s,n):this._analyzeLeafFieldNode(e,t,a,s,n))}async _analyzeLeafFieldNode(e,t,a,s,n){const i=p(e,t,n),{hashedRequestFieldCacheKey:r,propNameOrIndex:c,requestFieldCacheKey:h,requestFieldPath:o}=i,d=n.fieldTypeMap.get(o),{entityData:l,fragmentKind:y,fragmentName:u,requestFieldPathData:f,typeName:_}=t,g={dataTypename:oe(l)?l.__typename:oe(f)?f.__typename:void 0,fieldTypename:_,fragmentKind:y,fragmentName:u};if(ye._isNodeRequestFieldPath(d)){if(X(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(r,h,s,n);ye._setCachedResponseSlice({cacheability:e,data:t},a,i,g,s,n)}}else{const e=ye._getFieldDataFromAncestor(l,c)??ye._getFieldDataFromAncestor(f,c);ye._setFieldPathChecklist(a.fieldPathChecklist,{data:e},o,g),ye._setCachedData(a.data,{data:e},c)}}async _analyzeParentFieldNode(e,t,a,n,i){const r=p(e,t,i),{propNameOrIndex:c,requestFieldCacheKey:h,requestFieldPath:o}=r,d=i.fieldTypeMap.get(o);if(!d)return;const{cacheability:y,data:u,entityData:f,requestFieldPathData:_}=await this._retrieveCachedParentNodeData(t,r,d,n,i),{fragmentKind:g,fragmentName:m,typeName:C}=t,D=S(u,s);if(ye._setCachedResponseSlice({cacheability:y,data:u},a,r,{dataTypename:D,fieldTypename:C,fragmentKind:g,fragmentName:m},n,i),!l(u))return;const F=[];w(e,u,i.fragmentDefinitions,((e,t,s,r,d)=>{F.push(this._analyzeFieldNode(e,{cacheability:y,entityData:f,fragmentKind:s,fragmentName:r,index:d,requestFieldCacheKey:h,requestFieldPath:o,requestFieldPathData:_,typeName:t},{...a,data:he(a.data,c)},n,i))})),await Promise.all(F)}_buildCacheMetadata({ast:e},{data:t,...a},s,n){const i=this._createCacheMetadata({data:t,...a},n),r=q(e,n.operation)[0];if(!r)return i;const c=_(r);if(!c)return i;for(const{fieldNode:e}of c)this._setFieldCacheability(e,{requestFieldPath:n.operation},{cacheMetadata:i,data:t},s,n);return i}async _cacheResponse(e,t,a,s,n){const i=((e,t)=>{if(!t.normalizePatchResponseData)return e;const{data:a,paths:s,...n}=e;return s?.length&&k(s[0])?{...n,data:Q({},s[0],a),paths:s}:e})(a,n);let r=i;((e,t)=>t.hasDeferOrStream&&e.hasNext)(a,n)&&(this._setResponseChunksAwaitingCaching(i,n),r=void 0),((e,t)=>t.hasDeferOrStream&&!e.hasNext&&e.paths)(a,n)&&(r=this._retrieveResponseDataForCaching(i,n));const c=[];if(r&&(h=this._cacheTiersEnabled,!Y(h))){const{data:a}=r,h=this._buildCacheMetadata(e,r,s,n);let o,d;if(J(this._cacheTiersEnabled)&&c.push(this._setEntityAndRequestFieldPathCacheEntries(e,{cacheMetadata:h,entityData:structuredClone(a),requestFieldPathData:structuredClone(a)},s,n)),n.operation===I.QUERY&&W(this._cacheTiersEnabled)){let i;n.queryFiltered&&t&&(c.push(this._setQueryResponseCacheEntry(t.hash,{cacheMetadata:h,data:a},s,n)),i=this._getPartialQueryResponse(e.hash)),o=ye._mergeResponseCacheMetadata(h,i),d=this._mergeResponseData(a,i),c.push(this._setQueryResponseCacheEntry(e.hash,{cacheMetadata:o,data:d},s,n))}if(s.awaitDataCaching&&await Promise.all(c),((e,t)=>!t.hasDeferOrStream&&!e.hasNext&&!e.paths)(i,n)&&o&&d)return{cacheMetadata:o,data:d}}var h;const{data:o,hasNext:d,paths:l}=i;return{cacheMetadata:this._buildCacheMetadata(e,i,s,n),data:o,hasNext:d,paths:l}}async _checkCacheEntry(e,t,a,s){try{const n=await this._hasCacheEntry(e,t);if(!n||!ye._isValid(n))return!1;const i=await this._getCacheEntry(e,t,a,s);return!$(i)&&{cacheability:n,entry:i}}catch{return!1}}_createCacheMetadata({_cacheMetadata:e,headers:t},{operation:a}){const s=new Map,n=(({_cacheMetadata:e,fallback:t,headers:a})=>{if(e&&!x(e)){const[t,...a]=Object.values(e);return new T({metadata:a.reduce(((e,t)=>e?t.ttl<e.ttl?t:e:t),t)})}return a?.has("cache-control")?new T({headers:a}):new T({cacheControl:t})})({_cacheMetadata:e,fallback:this._fallbackOperationCacheability,headers:t});return s.set(a,n),e&&N(e,s),s}async _getCacheEntry(e,t,a,s){return this._cache?.get(`${e}::${t}`)}_getPartialQueryResponse(e){const t=this._partialQueryResponses.get(e);return this._partialQueryResponses.delete(e),t}async _hasCacheEntry(e,t){try{return await(this._cache?.has(`${e}::${t}`))??!1}catch{return!1}}_mergeResponseData(e,t){return t?Z(t.data,e,this._typeIDKey):e}async _parseEntityAndRequestFieldPathCacheEntryData(e,t,{cacheMetadata:a,entityData:s,requestFieldPathData:n},i,h){const o=p(e,t,h),{hashedRequestFieldCacheKey:d,requestFieldCacheKey:y,requestFieldPath:u,responseDataPath:f}=o,_=S(n,f),g=h.fieldTypeMap.get(u),m=a.get(u);if(!l(_)&&!g?.hasDirectives)return;if(l(_)){const t=[];w(e,_,h.fragmentDefinitions,((e,r,c,o,d)=>{t.push(this._parseEntityAndRequestFieldPathCacheEntryData(e,{index:d,requestFieldCacheKey:y,requestFieldPath:u,responseDataPath:f},{cacheMetadata:a,entityData:s,requestFieldPathData:n},i,h))})),await Promise.all(t)}if($(_)||!g||!m)return;const C=ae(_,g,this._typeIDKey),F=g.hasArguments||g.hasDirectives;h.operation===I.QUERY&&(C||F)&&X(this._cacheTiersEnabled)&&(await this._setRequestFieldPathCacheEntry(o,{cacheability:m,fieldData:se(structuredClone(_),e,o,h),fieldTypeInfo:g},i,h),D(e,{fragmentDefinitions:h.fragmentDefinitions})&&(C?Q(n,f,{__cacheKey:`${r}::${d}`}):H(n,f))),C&&B(this._cacheTiersEnabled)&&(await this._setEntityCacheEntry({cacheability:m,fieldData:ne(structuredClone(S(s,f)),e,o,h),fieldTypeInfo:g},i,h),Q(s,f,{__cacheKey:`${c}::${te(_,g,h)}`}))}async _retrieveCachedEntityData(e,{possibleTypes:t,typeName:a},s,n){const i=[...t.map((e=>e.typeName)),a],r=(await Promise.all(i.map((t=>this._checkCacheEntry(c,`${t}::${String(e)}`,s,n))))).filter((e=>!!e));let h;if(1===r.length)h=r[0];else if(r.length>1){r.sort((({cacheability:e},{cacheability:t})=>e.metadata.ttl-t.metadata.ttl));const e=r[0];e&&(h={cacheability:e.cacheability,entry:r.reduce(((e,{entry:t})=>Z(e,t,this._typeIDKey)),{})})}return h??{}}async _retrieveCachedParentNodeData({entityData:e,requestFieldPathData:t},{hashedRequestFieldCacheKey:a,propNameOrIndex:s,requestFieldCacheKey:n},i,r,c){let o,d=ye._getFieldDataFromAncestor(e,s),l=ye._getFieldDataFromAncestor(t,s);if(ye._isNodeRequestFieldPath(i)&&X(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(a,n,r,c);l=ee(l,t,this._typeIDKey),e&&(o=e)}const y=((e,{typeIDValue:t},a)=>t||(h(e)?e[a]:void 0))(l,i,this._typeIDKey);if(ye._isNodeEntity(i)&&y&&B(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedEntityData(y,i,r,c);d=ee(d,t,this._typeIDKey),e&&(!o||e.metadata.ttl>o.metadata.ttl)&&(o=e)}return{cacheability:o,data:U(d,l)?d:ee(d,l,this._typeIDKey),entityData:d,requestFieldPathData:l}}async _retrieveCachedRequestFieldPathData(e,t,a,s){return await this._checkCacheEntry(r,e,a,{...s,requestFieldCacheKey:t})||{}}async _retrieveCachedResponseData({ast:e},t,a){const s={cacheMetadata:new Map,data:{},fieldCount:{missing:0,total:0},fieldPathChecklist:new Map},n=q(e,a.operation)[0];if(!n)return s;const i=_(n);return i?(await Promise.all(i.map((({fieldNode:e})=>this._analyzeFieldNode(e,{requestFieldPath:a.operation},s,t,a)))),s.fieldCount=ye._countFieldPathChecklist(s.fieldPathChecklist),s):s}_retrieveResponseDataForCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);return this._responseChunksAwaitingCaching.delete(t.requestID),(s=[...a??[],e]).reduce(((e,t,a)=>{const{_cacheMetadata:n,data:i,hasNext:r,headers:c,paths:h}=t;return n&&(e._cacheMetadata=e._cacheMetadata?{...e._cacheMetadata,...n}:n),e.data=j(e.data,i),0===a&&(e.headers=c),a===s.length-1&&(e.hasNext=r),h&&(e.paths??=[],e.paths.push(...h)),e}),{data:{}});var s}async _setCacheEntry(e,t,a,s,n,i){try{await(this._cache?.set(`${e}::${t}`,structuredClone(a),s))}catch{}}async _setEntityAndRequestFieldPathCacheEntries(e,t,a,s){const n=q(e.ast,s.operation)[0];if(!n)return;const i=_(n);i&&await Promise.all(i.map((({fieldNode:e})=>this._parseEntityAndRequestFieldPathCacheEntryData(e,{requestFieldPath:s.operation},t,a,s))))}async _setEntityCacheEntry({cacheability:e,fieldData:t,fieldTypeInfo:a},s,n){const i=`${a.isEntity?a.typeName:t.__typename}::${String(t[this._typeIDKey])}`,r=await this._checkCacheEntry(c,i,s,n);r&&(t=Z(r.entry,t,this._typeIDKey)),await this._setCacheEntry(c,i,t,{cacheHeaders:{cacheControl:e.printCacheControl()},tag:s.tag},s,n)}_setFieldCacheability(e,t,{cacheMetadata:a,data:s},n,i){const{requestFieldPath:r}=t,c=p(e,t,i),{requestFieldPath:h,responseDataPath:o}=c;if(!l(s))return;const d=S(s,o),y=i.fieldTypeMap.get(h);(l(d)||y?.hasDirectives)&&(this._setFieldTypeCacheDirective(a,{ancestorRequestFieldPath:r,requestFieldPath:h},i),l(d)&&w(e,d,i.fragmentDefinitions,((e,t,r,c,d)=>{this._setFieldCacheability(e,{index:d,requestFieldPath:h,responseDataPath:o},{cacheMetadata:a,data:s},n,i)})))}_setFieldTypeCacheDirective(e,{ancestorRequestFieldPath:t,requestFieldPath:a},{fieldTypeMap:s,operation:n}){if(e.has(a))return;const i=s.get(a);if(i&&this._typeCacheDirectives[i.typeName]){const t=new T({cacheControl:this._typeCacheDirectives[i.typeName]});ye._setCacheMetadata(e,t,a,n)}else this._cascadeCacheControl&&t&&ye._setCacheMetadata(e,e.get(t),a,n)}_setPartialQueryResponse(e,t,a,s){this._partialQueryResponses.set(e,t)}async _setQueryResponseCacheEntry(e,{cacheMetadata:t,data:a},s,n){const r=M(t),c=ye._getOperationCacheControl(t,n.operation);await this._setCacheEntry(i,e,{cacheMetadata:r,data:a},{cacheHeaders:{cacheControl:c},tag:s.tag},s,n)}async _setRequestFieldPathCacheEntry(e,{cacheability:t,fieldData:a},s,n){const{hashedRequestFieldCacheKey:i,requestFieldCacheKey:c}=e,h=await this._checkCacheEntry(r,i,s,{...n,requestFieldCacheKey:c});h&&l(h.entry)&&l(a)&&(a=Z(h.entry,a,this._typeIDKey)),this._setCacheEntry(r,i,a,{cacheHeaders:{cacheControl:t.printCacheControl()},tag:s.tag},s,{...n,requestFieldCacheKey:c})}_setResponseChunksAwaitingCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);a?this._responseChunksAwaitingCaching.set(t.requestID,[...a,e]):this._responseChunksAwaitingCaching.set(t.requestID,[e])}}de([(e,a,s)=>{const n=s.value;n&&(s.value=async function(...e){return new Promise((a=>{(async()=>{const{debugManager:s,requestFieldCacheKey:i,...r}=e[3];if(!s)return void a(await n.apply(this,e));const c=s.now(),h=await n.apply(this,e),o=s.now(),d=o-c;a(h);const l={cacheType:e[0],context:r,options:e[2],requestHash:e[1],result:h,stats:{duration:d,endTime:o,startTime:c},...i?{decryptedCacheKey:i}:{}};s.log(t,l)})()}))})},le("design:type",Function),le("design:paramtypes",["undefined"==typeof CacheTypes?Object:CacheTypes,String,"undefined"==typeof RequestOptions?Object:RequestOptions,Object]),le("design:returntype",Promise)],ye.prototype,"_getCacheEntry",null),de([(t,a,s)=>{const n=s.value;n&&(s.value=async function(...t){return new Promise((a=>{(async()=>{const{debugManager:s,requestFieldCacheKey:i,...r}=t[5];if(!s)return await n.apply(this,t),void a();const c=s.now();await n.apply(this,t);const h=s.now(),o=h-c;a();const d={cacheType:t[0],cachemapOptions:t[3],context:r,options:t[4],requestHash:t[1],stats:{duration:o,endTime:h,startTime:c},value:t[2],...i?{decryptedCacheKey:i}:{}};s.log(e,d)})()}))})},le("design:type",Function),le("design:paramtypes",["undefined"==typeof CacheTypes?Object:CacheTypes,String,Object,"undefined"==typeof CachemapOptions?Object:CachemapOptions,"undefined"==typeof RequestOptions?Object:RequestOptions,Object]),le("design:returntype",Promise)],ye.prototype,"_setCacheEntry",null),de([(e,t,s)=>{const n=s.value;n&&(s.value=function(...e){const{debugManager:t,...s}=e[3];if(!t)return void n.apply(this,e);const i=t.now();n.apply(this,e);const r=t.now(),c=r-i;t.log(a,{context:s,options:e[2],requestHash:e[0],result:e[1],stats:{duration:c,endTime:r,startTime:i}})})},le("design:type",Function),le("design:paramtypes",[String,"undefined"==typeof PartialQueryResponse?Object:PartialQueryResponse,"undefined"==typeof RequestOptions?Object:RequestOptions,"undefined"==typeof CacheManagerContext?Object:CacheManagerContext]),le("design:returntype",void 0)],ye.prototype,"_setPartialQueryResponse",null);export{ye as CacheManager};
|
|
1
|
+
import"core-js/modules/web.structured-clone.js";import"core-js/modules/es.array.push.js";import{CACHE_ENTRY_ADDED as e,CACHE_ENTRY_QUERIED as t,PARTIAL_QUERY_COMPILED as a,TYPE_NAME_KEY as s,DEFAULT_TYPE_ID_KEY as i,QUERY_RESPONSES as n,REQUEST_FIELD_PATHS as r,DATA_ENTITIES as c}from"@graphql-box/core";import{isPlainObject as h,isArray as o,mergeObjects as d,isObjectLike as l,resolveFragments as y,getName as p,buildFieldKeysAndPaths as u,deleteFragmentSpreads as f,getChildFields as _,deleteChildFields as g,getInlineFragments as m,deleteInlineFragments as C,hasChildFields as D,deleteFragmentDefinitions as F,getOperationDefinitions as P,ArgsError as b,GroupedError as q,getFragmentDefinitions as R,hashRequest as E,rehydrateCacheMetadata as N,iterateChildFields as w,dehydrateCacheMetadata as M}from"@graphql-box/helpers";import{Cacheability as T}from"cacheability";import{Kind as K,print as v,OperationTypeNode as I}from"graphql";import{isNumber as O,isString as k,isEmpty as A,keys as x,merge as j,set as Q,get as S,isUndefined as $,assign as z,unset as U,isEqual as H}from"lodash-es";const G="noCache",L=(e,t)=>o(e)?e.reduce(((e,a)=>!!e&&L(a,t)),!0):!!h(e)&&V(e,t),V=(e,t)=>{const a=h(e)?Object.keys(e):[...e.keys()];return!(1!==a.length||!h(e)||!e[t])||a.reduce(((a,s)=>!!a&&(O(s)&&o(e)||k(s)&&h(e)?L(e[s],t):a)),!0)},Y=({entity:e,queryResponse:t,requestPath:a})=>!e&&!t&&!a,B=e=>!!e.entity,J=({entity:e,requestPath:t})=>!!(e??t),W=e=>!!e.queryResponse,X=e=>!!e.requestPath,Z=(e,t,a)=>d(e,t,((e,t)=>h(t)&&t[a]?t[a]:void 0)),ee=(e,t,a)=>!e&&t?t:l(e)&&l(t)?Z(e,t,a):e,te=(e,t,a)=>`${t.isEntity?t.typeName:e.__typename}::${String(e[a.typeIDKey])}`,ae=(e,t,a)=>{if(!h(e)||!(a in e))return!1;const{isEntity:s=!1,possibleTypes:i=[]}=t??{};return!!s||0!==i.length&&i.some((t=>t.typeName===e.__typename))},se=(e,t,a,s)=>{if(!h(e))return e;const i=y(t.selectionSet?.selections,s.fragmentDefinitions);return x(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>p(e)===n));if(r){const t=u(r.fieldNode,a,s),i=s.fieldTypeMap.get(t.requestFieldPath);(ae(e[n],i,s.typeIDKey)||i?.hasArguments||i?.hasDirectives)&&delete e[n]}return t}),e)},ie=(e,t,a,s)=>{const i=y(t.selectionSet?.selections,s.fragmentDefinitions);return Object.keys(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>p(e)===n));if(r){const{requestFieldPath:t}=u(r.fieldNode,a,s),i=s.fieldTypeMap.get(t);(ae(e[n],i,s.typeIDKey)||i?.hasArguments)&&delete e[n]}return t}),e)},ne=(e,t)=>{if(!e||0===e.length)return{hasData:!1,typeUnused:!!t};const[a]=e;if(a){const{hasData:e,typeName:s}=a;return{hasData:e,typeUnused:s?s!==t:void 0}}return{hasData:e.some((({hasData:e,typeName:a})=>a===t&&e)),typeUnused:!e.every((({typeName:e})=>e===t))}},re=(e,t,a,i,n)=>{const{fragmentDefinitions:r,typeIDKey:c}=n,h=_(e,{fragmentDefinitions:r});if(!h)return!1;const o={};for(let d=h.length-1;d>=0;d-=1){const l=h[d];if(!l)continue;const{fieldNode:y,fragmentKind:f,fragmentName:m,typeName:C}=l;f===K.FRAGMENT_SPREAD&&m&&!o[m]&&(o[m]={hasData:0,total:r?.[m]?_(r[m],{fragmentDefinitions:r})?.length??0:0});const F=p(y);if(F===c||F===s)continue;const{requestFieldPath:P}=u(y,{requestFieldPath:i},n),{hasData:b,typeUnused:q}=ne(t.get(P),C);if(b||q)if(f===K.FRAGMENT_SPREAD&&m){const e=o[m];e&&(e.hasData+=1)}else D(y,{fragmentDefinitions:r})?re(y,t,a,P,n)&&g(e,y):g(e,y)}return((e,t,a,s)=>{if(!A(t))for(const[i,n]of Object.entries(t)){const t=a[i];if(!t)continue;t.paths.push(s);const{hasData:r,total:c}=n;r===c&&(f(e,i),t.deleted+=1)}})(e,o,a,i),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=m(e);let i=!1;for(const n of s){const s=_(n,{fragmentDefinitions:t});if(!s||0===s.length){C(e,n),i=!0;continue}const[r]=s;if(r){const{fieldNode:t}=r;p(t)===a&&(C(e,n),i=!0)}}})(e,n),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const i=_(e,{fragmentDefinitions:t});if(!i||i.length>3)return!1;const n=i.map((({fieldNode:e})=>e.name.value));if(2===n.length&&n.every((e=>e===a||e===s)))return g(e,i.map((({fieldNode:e})=>e))),!0;if(1===n.length&&n[0]===a||n[0]===s){const[t]=i;if(t){const{fieldNode:a}=t;return g(e,a),!0}}})(e,n),!D(e,{fragmentDefinitions:r})},ce=(e,{fieldPathChecklist:t},a)=>{const{ast:s}=e,i=P(s,a.data.operation)[0];if(!i)return s;const{data:n,fragmentDefinitions:r}=a,c=_(i,{fragmentDefinitions:r});if(!c)return s;const h=(({request:e},{fragmentDefinitions:t})=>x(t??{}).reduce(((t,a)=>(t[a]={deleted:0,paths:[],total:(e.match(new RegExp(`\\.\\.\\.${a}`,"g"))??[]).length},t)),{}))(e,a);for(let e=c.length-1;e>=0;e-=1){const{fieldNode:s}=c[e],{requestFieldPath:r}=u(s,{requestFieldPath:n.operation},a);re(s,t,h,r,a)&&g(i,s)}return a.data.queryFiltered=!0,((e,t,a,s)=>{const i=x(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,paths:n,total:r}=s;return 0===i&&1===r?[...e,{name:t,path:n[0]}]:e}),[]),{fragmentDefinitions:n={}}=s;for(const{name:e,path:r}of i){const i=n[e];i&&re(i,t,a,r,s)}const r=x(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,total:n}=s;return i>0&&i===n?[...e,t]:e}),[]);return 0===r.length?e:F(e,{include:r})})(s,t,h,a)},he=(e,t)=>o(e)&&O(t)||h(e)&&k(t)?e[t]:void 0,oe=e=>!!l(e)&&(s in e&&k(e[s]));function de(e,t,a,s){var i,n=arguments.length,r=n<3?t:null===s?s=Object.getOwnPropertyDescriptor(t,a):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,a,s);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(r=(n<3?i(r):n>3?i(t,a,r):i(t,a))||r);return n>3&&r&&Object.defineProperty(t,a,r),r}function le(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}class ye{static _countFieldPathChecklist(e){const t={missing:0,total:0};for(const[,a]of e){t.total+=a.length;const e=a.filter((({hasData:e})=>!e));t.missing+=e.length}return t}static _getFieldDataFromAncestor(e,t){const a=he(e,t);return l(a)?structuredClone(a):a}static _getOperationCacheControl(e,t){const a="no-cache";if(!e)return a;const s=e.get(t);return s?s.printCacheControl():a}static _isNodeEntity(e){if(!e)return!1;const{isEntity:t,possibleTypes:a}=e;return t||a.some((e=>e.isEntity))}static _isNodeRequestFieldPath(e){return!!e&&(this._isNodeEntity(e)||e.hasArguments||e.hasDirectives)}static _isValid(e){return!S(e,["metadata","cacheControl",G],!1)&&e.checkTTL()}static _mergeResponseCacheMetadata(e,t){return t?new Map([...t.cacheMetadata,...e]):e}static _setCachedData(e,{data:t},a){const s=t=>{(o(e)&&O(a)||h(e))&&(e[a]=t)};l(t)||$(t)?o(t)?s([]):h(t)&&s({}):s(t)}static _setCachedResponseSlice(e,{cacheMetadata:t,data:a,fieldPathChecklist:s},{propNameOrIndex:i,requestFieldPath:n},r,c,{data:h}){ye._setCacheMetadata(t,e.cacheability,n,h.operation),ye._setFieldPathChecklist(s,e,n,r),ye._setCachedData(a,e,i)}static _setCacheMetadata(e,t,a,s){if(!t)return;e.set(a,t);const i=e.get(s);(!i||i.metadata.ttl>t.metadata.ttl)&&e.set(s,t)}static _setFieldPathChecklist(e,{data:t},a,{dataTypename:s,fieldTypename:i,fragmentKind:n,fragmentName:r}){if($(i)||n===K.FRAGMENT_SPREAD){if(e.has(a))return;return void e.set(a,[{fragmentKind:n,fragmentName:r,hasData:!$(t)}])}if(s!==i)return;const c=e.get(a)??[];c.some((({typeName:e})=>e===s))||e.set(a,[...c,{fragmentKind:n,fragmentName:r,hasData:!$(t),typeName:s}])}constructor(e){this._cacheTiersEnabled={entity:!1,queryResponse:!0,requestPath:!1},this._partialQueryResponses=new Map,this._responseChunksAwaitingCaching=new Map;const t=[];if("cache"in e||t.push(new b("@graphql-box/cache-manager expected cache to be in options.")),e.typeCacheDirectives&&!h(e.typeCacheDirectives)){const e="@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";t.push(new b(e))}if(t.length>0)throw new q("@graphql-box/cache-manager argument validation errors.",t);"function"==typeof e.cache?e.cache().then((e=>{this._cache=e})).catch((e=>{throw e})):this._cache=e.cache,this._cacheTiersEnabled={...this._cacheTiersEnabled,...e.cacheTiersEnabled},this._cascadeCacheControl=e.cascadeCacheControl??!1,this._fallbackOperationCacheability=e.fallbackOperationCacheability??G,this._typeCacheDirectives=e.typeCacheDirectives??{},this._typeIDKey=e.typeIDKey??i}async analyzeQuery(e,t,a){if(!J(this._cacheTiersEnabled))return{updated:e};const{ast:s,hash:i}=e,n={...a,fragmentDefinitions:R(s),typeIDKey:this._typeIDKey},r=await this._retrieveCachedResponseData(e,t,n),{cacheMetadata:c,data:h,fieldCount:o}=r;if(o.missing===o.total||((e,t)=>V(e,t))(h,this._typeIDKey))return{updated:e};if(!o.missing&&W(this._cacheTiersEnabled)){const e=this._setQueryResponseCacheEntry(i,{cacheMetadata:c,data:h},t,n);return t.awaitDataCaching&&await e,{response:{cacheMetadata:c,data:h}}}const d=ce(e,r,n),l=v(d),{fragmentDefinitions:y,typeIDKey:p,...u}=n;return z(a,{...u,filteredRequest:l}),W(this._cacheTiersEnabled)&&this._setPartialQueryResponse(i,{cacheMetadata:c,data:h},t,a),{updated:{ast:d,hash:E(l),request:l}}}get cache(){return this._cache}async cacheQuery(e,t,a,s,i){const n={...i,fragmentDefinitions:R((t??e).ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,t,a,s,n)}async cacheResponse(e,t,a,s){const i={...s,fragmentDefinitions:R(e.ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,void 0,t,a,i)}get cacheTiersEnabled(){return this._cacheTiersEnabled}async checkCacheEntry(e,t,a,s){return!Y(this._cacheTiersEnabled)&&this._checkCacheEntry(e,t,a,s)}async checkQueryResponseCacheEntry(e,t,a){if(!W(this._cacheTiersEnabled))return!1;const s=await this._checkCacheEntry(n,e,t,a);if(!s)return!1;const{cacheMetadata:i,data:r}=s.entry;return{cacheMetadata:N(i),data:r}}deletePartialQueryResponse(e){W(this._cacheTiersEnabled)&&this._partialQueryResponses.delete(e)}async setQueryResponseCacheEntry(e,t,a,s){if(W(this._cacheTiersEnabled))return this._setQueryResponseCacheEntry(e.hash,t,a,s)}async _analyzeFieldNode(e,t,a,s,i){await(D(e,{fragmentDefinitions:i.fragmentDefinitions})?this._analyzeParentFieldNode(e,t,a,s,i):this._analyzeLeafFieldNode(e,t,a,s,i))}async _analyzeLeafFieldNode(e,t,a,s,i){const n=u(e,t,i),{hashedRequestFieldCacheKey:r,propNameOrIndex:c,requestFieldCacheKey:h,requestFieldPath:o}=n,d=i.fieldTypeMap.get(o),{entityData:l,fragmentKind:y,fragmentName:p,requestFieldPathData:f,typeName:_}=t,g={dataTypename:oe(l)?l.__typename:oe(f)?f.__typename:void 0,fieldTypename:_,fragmentKind:y,fragmentName:p};if(ye._isNodeRequestFieldPath(d)){if(X(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(r,h,s,i);ye._setCachedResponseSlice({cacheability:e,data:t},a,n,g,s,i)}}else{const e=ye._getFieldDataFromAncestor(l,c)??ye._getFieldDataFromAncestor(f,c);ye._setFieldPathChecklist(a.fieldPathChecklist,{data:e},o,g),ye._setCachedData(a.data,{data:e},c)}}async _analyzeParentFieldNode(e,t,a,i,n){const r=u(e,t,n),{propNameOrIndex:c,requestFieldCacheKey:h,requestFieldPath:o}=r,d=n.fieldTypeMap.get(o);if(!d)return;const{cacheability:y,data:p,entityData:f,requestFieldPathData:_}=await this._retrieveCachedParentNodeData(t,r,d,i,n),{fragmentKind:g,fragmentName:m,typeName:C}=t,D=S(p,s);if(ye._setCachedResponseSlice({cacheability:y,data:p},a,r,{dataTypename:D,fieldTypename:C,fragmentKind:g,fragmentName:m},i,n),!l(p))return;const F=[];w(e,p,n.fragmentDefinitions,((e,t,s,r,d)=>{F.push(this._analyzeFieldNode(e,{cacheability:y,entityData:f,fragmentKind:s,fragmentName:r,index:d,requestFieldCacheKey:h,requestFieldPath:o,requestFieldPathData:_,typeName:t},{...a,data:he(a.data,c)},i,n))})),await Promise.all(F)}_buildCacheMetadata({ast:e},{data:t,...a},s,i){const n=this._createCacheMetadata({data:t,...a},i),r=P(e,i.data.operation)[0];if(!r)return n;const c=_(r);if(!c)return n;for(const{fieldNode:e}of c)this._setFieldCacheability(e,{requestFieldPath:i.data.operation},{cacheMetadata:n,data:t},s,i);return n}async _cacheResponse(e,t,a,s,i){const n=((e,t)=>{if(!t.deprecated.normalizePatchResponseData)return e;const{data:a,paths:s,...i}=e;return s?.length&&k(s[0])?{...i,data:Q({},s[0],a),paths:s}:e})(a,i);let r=n;((e,t)=>t.deprecated.hasDeferOrStream&&e.hasNext)(a,i)&&(this._setResponseChunksAwaitingCaching(n,i),r=void 0),((e,t)=>t.deprecated.hasDeferOrStream&&!e.hasNext&&e.paths)(a,i)&&(r=this._retrieveResponseDataForCaching(n,i));const c=[];if(r&&(h=this._cacheTiersEnabled,!Y(h))){const{data:a}=r,h=this._buildCacheMetadata(e,r,s,i);let o,d;if(J(this._cacheTiersEnabled)&&c.push(this._setEntityAndRequestFieldPathCacheEntries(e,{cacheMetadata:h,entityData:structuredClone(a),requestFieldPathData:structuredClone(a)},s,i)),i.data.operation===I.QUERY&&W(this._cacheTiersEnabled)){let n;i.data.queryFiltered&&t&&(c.push(this._setQueryResponseCacheEntry(t.hash,{cacheMetadata:h,data:a},s,i)),n=this._getPartialQueryResponse(e.hash)),o=ye._mergeResponseCacheMetadata(h,n),d=this._mergeResponseData(a,n),c.push(this._setQueryResponseCacheEntry(e.hash,{cacheMetadata:o,data:d},s,i))}if(s.awaitDataCaching&&await Promise.all(c),((e,t)=>!t.deprecated.hasDeferOrStream&&!e.hasNext&&!e.paths)(n,i)&&o&&d)return{cacheMetadata:o,data:d}}var h;const{data:o,hasNext:d,paths:l}=n;return{cacheMetadata:this._buildCacheMetadata(e,n,s,i),data:o,hasNext:d,paths:l}}async _checkCacheEntry(e,t,a,s){try{const i=await this._hasCacheEntry(e,t);if(!i||!ye._isValid(i))return!1;const n=await this._getCacheEntry(e,t,a,s);return!$(n)&&{cacheability:i,entry:n}}catch{return!1}}_createCacheMetadata({_cacheMetadata:e,headers:t},{data:a}){const s=new Map,i=(({_cacheMetadata:e,fallback:t,headers:a})=>{if(e&&!A(e)){const[t,...a]=Object.values(e);return new T({metadata:a.reduce(((e,t)=>e?t.ttl<e.ttl?t:e:t),t)})}return a?.has("cache-control")?new T({headers:a}):new T({cacheControl:t})})({_cacheMetadata:e,fallback:this._fallbackOperationCacheability,headers:t});return s.set(a.operation,i),e&&N(e,s),s}async _getCacheEntry(e,t,a,s){return this._cache?.get(`${e}::${t}`)}_getPartialQueryResponse(e){const t=this._partialQueryResponses.get(e);return this._partialQueryResponses.delete(e),t}async _hasCacheEntry(e,t){try{return await(this._cache?.has(`${e}::${t}`))??!1}catch{return!1}}_mergeResponseData(e,t){return t?Z(t.data,e,this._typeIDKey):e}async _parseEntityAndRequestFieldPathCacheEntryData(e,t,{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,h){const o=u(e,t,h),{hashedRequestFieldCacheKey:d,requestFieldCacheKey:y,requestFieldPath:p,responseDataPath:f}=o,_=S(i,f),g=h.fieldTypeMap.get(p),m=a.get(p);if(!l(_)&&!g?.hasDirectives)return;if(l(_)){const t=[];w(e,_,h.fragmentDefinitions,((e,r,c,o,d)=>{t.push(this._parseEntityAndRequestFieldPathCacheEntryData(e,{index:d,requestFieldCacheKey:y,requestFieldPath:p,responseDataPath:f},{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,h))})),await Promise.all(t)}if($(_)||!g||!m)return;const C=ae(_,g,this._typeIDKey),F=g.hasArguments||g.hasDirectives;h.data.operation===I.QUERY&&(C||F)&&X(this._cacheTiersEnabled)&&(await this._setRequestFieldPathCacheEntry(o,{cacheability:m,fieldData:se(structuredClone(_),e,o,h),fieldTypeInfo:g},n,h),D(e,{fragmentDefinitions:h.fragmentDefinitions})&&(C?Q(i,f,{__cacheKey:`${r}::${d}`}):U(i,f))),C&&B(this._cacheTiersEnabled)&&(await this._setEntityCacheEntry({cacheability:m,fieldData:ie(structuredClone(S(s,f)),e,o,h),fieldTypeInfo:g},n,h),Q(s,f,{__cacheKey:`${c}::${te(_,g,h)}`}))}async _retrieveCachedEntityData(e,{possibleTypes:t,typeName:a},s,i){const n=[...t.map((e=>e.typeName)),a],r=(await Promise.all(n.map((t=>this._checkCacheEntry(c,`${t}::${String(e)}`,s,i))))).filter((e=>!!e));let h;if(1===r.length)h=r[0];else if(r.length>1){r.sort((({cacheability:e},{cacheability:t})=>e.metadata.ttl-t.metadata.ttl));const e=r[0];e&&(h={cacheability:e.cacheability,entry:r.reduce(((e,{entry:t})=>Z(e,t,this._typeIDKey)),{})})}return h??{}}async _retrieveCachedParentNodeData({entityData:e,requestFieldPathData:t},{hashedRequestFieldCacheKey:a,propNameOrIndex:s,requestFieldCacheKey:i},n,r,c){let o,d=ye._getFieldDataFromAncestor(e,s),l=ye._getFieldDataFromAncestor(t,s);if(ye._isNodeRequestFieldPath(n)&&X(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(a,i,r,c);l=ee(l,t,this._typeIDKey),e&&(o=e)}const y=((e,{typeIDValue:t},a)=>t||(h(e)?e[a]:void 0))(l,n,this._typeIDKey);if(ye._isNodeEntity(n)&&y&&B(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedEntityData(y,n,r,c);d=ee(d,t,this._typeIDKey),e&&(!o||e.metadata.ttl>o.metadata.ttl)&&(o=e)}return{cacheability:o,data:H(d,l)?d:ee(d,l,this._typeIDKey),entityData:d,requestFieldPathData:l}}async _retrieveCachedRequestFieldPathData(e,t,a,s){return await this._checkCacheEntry(r,e,a,{...s,requestFieldCacheKey:t})||{}}async _retrieveCachedResponseData({ast:e},t,a){const s={cacheMetadata:new Map,data:{},fieldCount:{missing:0,total:0},fieldPathChecklist:new Map},i=P(e,a.data.operation)[0];if(!i)return s;const n=_(i);return n?(await Promise.all(n.map((({fieldNode:e})=>this._analyzeFieldNode(e,{requestFieldPath:a.data.operation},s,t,a)))),s.fieldCount=ye._countFieldPathChecklist(s.fieldPathChecklist),s):s}_retrieveResponseDataForCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.data.requestID);return this._responseChunksAwaitingCaching.delete(t.data.requestID),(s=[...a??[],e]).reduce(((e,t,a)=>{const{_cacheMetadata:i,data:n,hasNext:r,headers:c,paths:h}=t;return i&&(e._cacheMetadata=e._cacheMetadata?{...e._cacheMetadata,...i}:i),e.data=j(e.data,n),0===a&&(e.headers=c),a===s.length-1&&(e.hasNext=r),h&&(e.paths??=[],e.paths.push(...h)),e}),{data:{}});var s}async _setCacheEntry(e,t,a,s,i,n){try{await(this._cache?.set(`${e}::${t}`,structuredClone(a),s))}catch{}}async _setEntityAndRequestFieldPathCacheEntries(e,t,a,s){const i=P(e.ast,s.data.operation)[0];if(!i)return;const n=_(i);n&&await Promise.all(n.map((({fieldNode:e})=>this._parseEntityAndRequestFieldPathCacheEntryData(e,{requestFieldPath:s.data.operation},t,a,s))))}async _setEntityCacheEntry({cacheability:e,fieldData:t,fieldTypeInfo:a},s,i){const n=`${a.isEntity?a.typeName:t.__typename}::${String(t[this._typeIDKey])}`,r=await this._checkCacheEntry(c,n,s,i);r&&(t=Z(r.entry,t,this._typeIDKey)),await this._setCacheEntry(c,n,t,{cacheHeaders:{cacheControl:e.printCacheControl()},tag:s.tag},s,i)}_setFieldCacheability(e,t,{cacheMetadata:a,data:s},i,n){const{requestFieldPath:r}=t,c=u(e,t,n),{requestFieldPath:h,responseDataPath:o}=c;if(!l(s))return;const d=S(s,o),y=n.fieldTypeMap.get(h);(l(d)||y?.hasDirectives)&&(this._setFieldTypeCacheDirective(a,{ancestorRequestFieldPath:r,requestFieldPath:h},n),l(d)&&w(e,d,n.fragmentDefinitions,((e,t,r,c,d)=>{this._setFieldCacheability(e,{index:d,requestFieldPath:h,responseDataPath:o},{cacheMetadata:a,data:s},i,n)})))}_setFieldTypeCacheDirective(e,{ancestorRequestFieldPath:t,requestFieldPath:a},{data:s,fieldTypeMap:i}){if(e.has(a))return;const n=i.get(a);if(n&&this._typeCacheDirectives[n.typeName]){const t=new T({cacheControl:this._typeCacheDirectives[n.typeName]});ye._setCacheMetadata(e,t,a,s.operation)}else this._cascadeCacheControl&&t&&ye._setCacheMetadata(e,e.get(t),a,s.operation)}_setPartialQueryResponse(e,t,a,s){this._partialQueryResponses.set(e,t)}async _setQueryResponseCacheEntry(e,{cacheMetadata:t,data:a},s,i){const r=M(t),c=ye._getOperationCacheControl(t,i.data.operation);await this._setCacheEntry(n,e,{cacheMetadata:r,data:a},{cacheHeaders:{cacheControl:c},tag:s.tag},s,i)}async _setRequestFieldPathCacheEntry(e,{cacheability:t,fieldData:a},s,i){const{hashedRequestFieldCacheKey:n,requestFieldCacheKey:c}=e,h=await this._checkCacheEntry(r,n,s,{...i,requestFieldCacheKey:c});h&&l(h.entry)&&l(a)&&(a=Z(h.entry,a,this._typeIDKey)),this._setCacheEntry(r,n,a,{cacheHeaders:{cacheControl:t.printCacheControl()},tag:s.tag},s,{...i,requestFieldCacheKey:c})}_setResponseChunksAwaitingCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.data.requestID);a?this._responseChunksAwaitingCaching.set(t.data.requestID,[...a,e]):this._responseChunksAwaitingCaching.set(t.data.requestID,[e])}}de([(e,a,s)=>{const i=s.value;i&&(s.value=async function(...e){return new Promise((a=>{(async()=>{const{data:s,debugManager:n,requestFieldCacheKey:r}=e[3];if(!n)return void a(await i.apply(this,e));const c=n.now(),h=await i.apply(this,e),o=n.now(),d=o-c;a(h),n.log(t,{data:{...s,cacheType:e[0],...r?{decryptedCacheKey:r}:void 0},stats:{duration:d,endTime:o,startTime:c}})})()}))})},le("design:type",Function),le("design:paramtypes",["undefined"==typeof CacheTypes?Object:CacheTypes,String,"undefined"==typeof RequestOptions?Object:RequestOptions,Object]),le("design:returntype",Promise)],ye.prototype,"_getCacheEntry",null),de([(t,a,s)=>{const i=s.value;i&&(s.value=async function(...t){return new Promise((a=>{(async()=>{const{data:s,debugManager:n,requestFieldCacheKey:r}=t[5];if(!n)return await i.apply(this,t),void a();const c=n.now();await i.apply(this,t);const h=n.now(),o=h-c;a(),n.log(e,{data:{...s,cacheHeaders:t[3].cacheHeaders,cacheType:t[0],...r?{decryptedCacheKey:r}:void 0},stats:{duration:o,endTime:h,startTime:c}})})()}))})},le("design:type",Function),le("design:paramtypes",["undefined"==typeof CacheTypes?Object:CacheTypes,String,Object,"undefined"==typeof CachemapOptions?Object:CachemapOptions,"undefined"==typeof RequestOptions?Object:RequestOptions,Object]),le("design:returntype",Promise)],ye.prototype,"_setCacheEntry",null),de([(e,t,s)=>{const i=s.value;i&&(s.value=function(...e){const{data:t,debugManager:s}=e[3];if(!s)return void i.apply(this,e);const n=s.now();i.apply(this,e);const r=s.now(),c=r-n;s.log(a,{data:t,stats:{duration:c,endTime:r,startTime:n}})})},le("design:type",Function),le("design:paramtypes",[String,"undefined"==typeof PartialQueryResponse?Object:PartialQueryResponse,"undefined"==typeof RequestOptions?Object:RequestOptions,"undefined"==typeof CacheManagerContext?Object:CacheManagerContext]),le("design:returntype",void 0)],ye.prototype,"_setPartialQueryResponse",null);export{ye as CacheManager};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|