UNPKG

8.66 kBJavaScriptView Raw
1import*as e from"cache-parser";import*as t from"fast-defer";import*as a from"object-code";var r={d:(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},s={};r.d(s,{h4:()=>i,UN:()=>b,uu:()=>S,Kd:()=>I,ZF:()=>O,nv:()=>w,p:()=>l,E7:()=>d,NQ:()=>o,xK:()=>A,G6:()=>h,LN:()=>m,Bw:()=>y,Ad:()=>u,$k:()=>v,v8:()=>E,Jk:()=>g,tI:()=>p,iS:()=>f});const n=(e=>{var t={};return r.d(t,e),t})({parse:()=>e.parse}),i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),o=e=>{if(!e)return"not enough headers";const t=e[i.CacheControl];if(t){const{noCache:a,noStore:r,mustRevalidate:s,maxAge:o,immutable:c}=(0,n.parse)(String(t));if(a||r)return"dont cache";if(c)return 31536e6;if(s)return 0;if(void 0!==o){const t=e[i.Age];return t?1e3*(o-Number(t)):1e3*o}}const a=e[i.Expires];if(a){const e=Date.parse(String(a))-Date.now();return e>=0?e:"dont cache"}return"not enough headers"};const c=(e=>{var t={};return r.d(t,e),t})({deferred:()=>t.deferred});function d(e){return e?t=>e(t)||304===t:e=>e>=200&&e<300||304===e}function u(e="get",t=[]){return e=e.toLowerCase(),t.some((t=>t===e))}function f(e,t){var a;t.headers||(t.headers={});const{etag:r,modifiedSince:s}=t.cache;if(r){const s=!0===r?null===(a=e.data)||void 0===a?void 0:a.headers[i.ETag]:r;s&&(t.headers[i.IfNoneMatch]=s)}s&&(t.headers[i.IfModifiedSince]=!0===s?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():s.toUTCString())}function l(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=Object.assign(Object.assign({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function h(e){const t=async t=>{var a;const r=t.id=e.generateKey(t);if(!1===t.cache)return t;if(t.cache=Object.assign(Object.assign({},e.defaults.cache),t.cache),!u(t.method,t.cache.methods))return t;let s,n=await e.storage.get(r,t);e:if("empty"===n.state||"stale"===n.state){if(e.waiting[r]&&(n=await e.storage.get(r,t),"empty"!==n.state)){0;break e}return e.waiting[r]=(0,c.deferred)(),null===(a=e.waiting[r])||void 0===a||a.catch((()=>{})),await e.storage.set(r,{state:"loading",previous:n.state,data:n.data,createdAt:n.createdAt},t),"stale"===n.state&&f(n,t),t.validateStatus=d(t.validateStatus),t}if("loading"===n.state){const a=e.waiting[r];if(!a)return await e.storage.remove(r,t),t;0;try{s=await a}catch(e){return t}}else s=n.data;return t.adapter=()=>Promise.resolve({config:t,data:s.data,headers:s.headers,status:s.status,statusText:s.statusText,cached:!0,id:r}),t};return{onFulfilled:t,apply:()=>e.interceptors.request.use(t)}}async function g(e,t){var a;if("function"==typeof t)return t(e);const{statusCheck:r,responseMatch:s,containsHeaders:n}=t;if(r&&!await r(e.status)||s&&!await s(e))return!1;if(n)for(const[t,r]of Object.entries(n))if(!await r(null!==(a=e.headers[t.toLowerCase()])&&void 0!==a?a:e.headers[t]))return!1;return!0}async function p(e,t,a){for(const r in a){const s=a[r];if("delete"===s){await e.remove(r,t.config);continue}const n=await e.get(r,t.config);if("loading"===n.state)continue;const i=await s(n,t);"delete"!==i?"ignore"!==i&&await e.set(r,i,t.config):await e.remove(r,t.config)}}function m(e){const t=async(t,a)=>{var r;await e.storage.remove(t,a),null===(r=e.waiting[t])||void 0===r||r.reject(),delete e.waiting[t]},a=async a=>{var r,s,n;const o=a.id=null!==(r=(n=a.config).id)&&void 0!==r?r:n.id=e.generateKey(a.config);if(null!==(s=a.cached)&&void 0!==s||(a.cached=!1),a.cached)return a;const c=a.config.cache;if(!c)return Object.assign(Object.assign({},a),{cached:!1});const d=a.config,u=await e.storage.get(o,d);if("loading"!==u.state)return a;if(!u.data&&!await g(a,c.cachePredicate))return await t(o,d),a;for(const e in i)e.startsWith("XAxiosCache")&&delete a.headers[e];c.etag&&!0!==c.etag&&(a.headers[i.XAxiosCacheEtag]=c.etag),c.modifiedSince&&(a.headers[i.XAxiosCacheLastModified]=!0===c.modifiedSince?"use-cache-timestamp":c.modifiedSince.toUTCString());let f=c.ttl||-1;if(null==c?void 0:c.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(o,d),a;f="not enough headers"===r?f:r}const h=l(a,u.data);"function"==typeof f&&(f=await f(a)),c.staleIfError&&(a.headers[i.XAxiosCacheStaleIfError]=String(f)),(null==c?void 0:c.update)&&await p(e.storage,a,c.update);const m={state:"cached",ttl:f,createdAt:Date.now(),data:h},v=e.waiting[o];return v&&(v.resolve(m.data),delete e.waiting[o]),await e.storage.set(o,m,d),a},r=async a=>{var r;const s=a.config;if(!(null==s?void 0:s.cache)||!s.id)throw a;const n=await e.storage.get(s.id,s),i=s.cache;if("loading"!==n.state||"stale"!==n.previous)throw await t(s.id,s),a;if(null==i?void 0:i.staleIfError){const t="function"==typeof i.staleIfError?await i.staleIfError(a.response,n,a):i.staleIfError;if(!0===t||"number"==typeof t&&n.createdAt+t>Date.now())return null===(r=e.waiting[s.id])||void 0===r||r.resolve(n.data),delete e.waiting[s.id],await e.storage.set(s.id,{state:"stale",createdAt:Date.now(),data:n.data},s),{cached:!0,config:s,id:s.id,data:n.data.data,headers:n.data.headers,status:n.data.status,statusText:n.data.statusText}}throw a};return{onFulfilled:a,onRejected:r,apply:()=>e.interceptors.response.use(a,r)}}const v=e=>!!e&&!!e["is-storage"];function w(e){const t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function y(e){return e.createdAt+e.ttl<=Date.now()}function I({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,s)=>{const n=await t(r,s);if(!n)return{state:"empty"};if("cached"!==n.state||!y(n))return n;if(w(n)){const t={state:"stale",createdAt:n.createdAt,data:n.data};return await e(r,t,s),t}return await a(r,s),{state:"empty"}}}}function S(e=!1){const t=I({set:(e,a)=>{t.data[e]=a},remove:e=>{delete t.data[e]},find:a=>{const r=t.data[a];return e&&void 0!==r?"function"==typeof structuredClone?structuredClone(r):JSON.parse(JSON.stringify(r)):r}});return t.data=Object.create(null),t}const x=(e=>{var t={};return r.d(t,e),t})({hash:()=>a.hash}),C=/^\/|\/$/g;function b(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${(0,x.hash)(a)}`}}const A=b((({baseURL:e="",url:t="",method:a="get",params:r,data:s})=>(e&&(e=e.replace(C,"")),t&&(t=t.replace(C,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:s})));function E(e,t={}){var a,r,s,n,i;const c=e;if(c.storage=t.storage||S(),!v(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||A,c.headerInterpreter=t.headerInterpreter||o,c.requestInterceptor=t.requestInterceptor||h(c),c.responseInterceptor=t.responseInterceptor||m(c),c.debug=t.debug,c.defaults.cache={update:t.update||{},ttl:null!==(a=t.ttl)&&void 0!==a?a:3e5,methods:t.methods||["get"],cachePredicate:t.cachePredicate||{statusCheck:e=>e>=200&&e<400},etag:null===(r=t.etag)||void 0===r||r,modifiedSince:null!==(s=t.modifiedSince)&&void 0!==s?s:!1===t.etag,interpretHeader:null===(n=t.interpretHeader)||void 0===n||n,staleIfError:null===(i=t.staleIfError)||void 0===i||i},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function O(e,t=""){return I({find:a=>{const r=e.getItem(t+a);return r?JSON.parse(r):void 0},remove:a=>{e.removeItem(t+a)},set:(a,r)=>{const s=()=>e.setItem(t+a,JSON.stringify(r));try{return s()}catch(r){const n=Object.entries(e).filter((e=>e[0].startsWith(t))).map((e=>[e[0],JSON.parse(e[1])]));for(const t of n)"cached"===t[1].state&&y(t[1])&&!w(t[1])&&e.removeItem(t[0]);try{return s()}catch(t){const a=n.sort(((e,t)=>(e[1].createdAt||0)-(t[1].createdAt||0)));for(const t of a){e.removeItem(t[0]);try{return s()}catch(e){}}}e.removeItem(t+a)}}})}var j=s.h4,N=s.UN,T=s.uu,M=s.Kd,L=s.ZF,K=s.nv,X=s.p,k=s.E7,D=s.NQ,J=s.xK,P=s.G6,R=s.LN,U=s.Bw,q=s.Ad,H=s.$k,$=s.v8,F=s.Jk,G=s.tI,W=s.iS;export{j as Header,N as buildKeyGenerator,T as buildMemoryStorage,M as buildStorage,L as buildWebStorage,K as canStale,X as createCacheResponse,k as createValidateStatus,D as defaultHeaderInterpreter,J as defaultKeyGenerator,P as defaultRequestInterceptor,R as defaultResponseInterceptor,U as isExpired,q as isMethodIn,H as isStorage,$ as setupCache,F as testCachePredicate,G as updateCache,W as updateStaleRequest};
\No newline at end of file