1 line
202 KiB
JavaScript
1 line
202 KiB
JavaScript
|
(()=>{var __webpack_modules__={7315:(e,t,r)=>{"use strict";e.exports={FileSystemCache:r(4669),MaxAge:r(6564),oneBehindFetch:r(8414),log:r(277),isAmp:r(3471)}},4669:(e,t,r)=>{"use strict";const i=r(7147).promises;const{existsSync:n,mkdirSync:s}=r(7147);const a=r(6113);const o=r(2037);const l=r(277);const c=r(7330);const u=r(1017);const d={log:l,maxItems:50};class FileSystemCache{static create(e={}){const t=Object.assign(d,e);try{if(!t.baseDir){t.baseDir=u.join(o.tmpdir(),"ampproject-toolbox-optimizer")}if(!n(t.baseDir)){s(t.baseDir)}}catch(e){l.debug("No filesystem access, falling back to in-memory cache only",e);return new c(t.maxItems)}return new FileSystemCache(t)}constructor(e){this.opts=e;this.cache=new c(e.maxItems)}async get(e,t=null){let r=this.cache.get(e);if(r){return r}const n=this.createCacheFileName(e);try{const t=await i.readFile(n,"utf-8");r=JSON.parse(t);this.cache.set(e,r)}catch(e){r=t}return r}async set(e,t){try{this.cache.set(e,t);const r=this.createCacheFileName(e);return i.writeFile(r,JSON.stringify(t,null,""),"utf-8")}catch(e){this.opts.log.error("Could not write cache file",e)}}async clear(){const e=this.opts.baseDir;if(!n(e)){return}let t=await i.readdir(e,{withFileTypes:true});await Promise.all(t.map((t=>{let r=u.join(e,t.name);return t.isFile()&&t.name.endsWith(".json")?i.unlink(r):Promise.resolve()})))}createCacheFileName(e){const t=a.createHash("md5").update(e).digest("hex");return u.join(this.opts.baseDir,t+".json")}async deleteDir_(e){let t=await i.readdir(e,{withFileTypes:true});await Promise.all(t.map((t=>{let r=u.join(e,t.name);return t.isDirectory()?this.deleteDir_(r):i.unlink(r)})));await i.rmdir(e)}}e.exports=FileSystemCache},3471:e=>{"use strict";const t="<html";e.exports=function isAmp(e){let r=e.indexOf(t);if(r===-1){return false}r+=t.length;const i=e.indexOf(">",r);if(i===-1){return false}let n=e.substring(r,i).split(/\s+/);n=n.map((e=>e.split("=")[0]));return n.includes("amp")||n.includes("⚡")}},277:e=>{"use strict";class Log{constructor(e="",t=false,r=console){this.tag_=e;this.verbose_=t;this.prefix_=e?this.inverse_(e):"";this.output_=r}debug(e,...t){if(!this.verbose_){return}this.log_(this.output_.log,this.dim_(e),t)}log(e,...t){this.info(e,...t)}info(e,...t){this.log_(this.output_.log,e,...t)}success(e,...t){this.log_(this.output_.log,this.green_("SUCCESS "+e),t)}warn(e,...t){this.log_(this.output_.warn,this.yellow_("WARNING "+e),t)}error(e,...t){this.output_.log("\n");this.log_(this.output_.error,this.red_("ERROR "+e),t);this.output_.log("\n")}verbose(e=true){this.verbose_=!!e}tag(e){if(this.tag_){e=this.tag_+" "+e}return new Log(e,this.verbose_,this.output_)}log_(e,t,r){if(this.prefix_){t=this.prefix_+" "+t}if(r){e(...[t].concat(r))}else{e(t)}}inverse_(e){return`[7m${e}[0m`}dim_(e){return`[36m${e}[0m`}green_(e){return`[32m${e}[0m`}yellow_(e){return`[33m${e}[0m`}red_(e){return`[31m${e}[0m`}}e.exports=new Log},6564:e=>{"use strict";const t={value:0,isExpired:()=>true};class MaxAge{static zero(){return t}static parse(e){if(!e){return MaxAge.zero()}const t=e.match(/max-age=([0-9]+)[^0-9]?/i);if(!t){return MaxAge.zero()}return MaxAge.create(t[1])}static create(e){if(!Number.isInteger(e)){e=parseInt(e,10)}return new MaxAge(Date.now(),e)}static fromObject(e,t){return new MaxAge(e,t)}constructor(e,t){this.timestampInMs_=e;this.value=t}isExpired(e=Date.now()){const t=this.value*1e3;return this.timestampInMs_+t<e}toObject(){return{timestampInMs:this.timestampInMs_,maxAge:this.maxAge}}}e.exports=MaxAge},8414:(e,t,r)=>{"use strict";let i=r(3114);const n=r(6564);const s=new Map;async function oneBehindFetch(e,t){let r=s.get(e);if(!r){r={maxAge:Promise.resolve(n.zero())};s.set(e,r)}const a=await r.maxAge;if(!a.isExpired()){const e=await r.responsePromise;return e.clone()}const o=r.responsePromise;const l=i(e,t);r={responsePromise:l,maxAge:l.then((e=>n.parse(e.headers.get("cache-control"))))};s.set(e,r);const c=o||l;const u=await c;return u.clone()}oneBehindFetch.clearCache=()=>s.clear();oneBehindFetch.setDelegate=e=>i=e;e.exports=oneBehindFetch},8483:(e,t,r)=>{"use strict";cons
|