/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/file-server@2.2.1/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const fs=require("graceful-fs"),path=require("path"),hashr=require("hashr"),kgo=require("kgo"),StreamCatcher=require("stream-catcher"),chokidar=require("chokidar"),watchers={},cacheMaxSize=1024e3;function cacheLengthFunction(e){return e.length}function createReadStream(e,r,t){return function(a){const o=fs.createReadStream(a);o.on("error",e.errorCallback.bind(null,r,t)),e.cache.read(a,o)}}function FileServer(e,r){if(!e)throw new Error("Must supply an error callback to File Server");this.cache=new StreamCatcher({length:cacheLengthFunction,max:null==r?1024e3:r}),this.errorCallback=function(r,t,a){e(a,r,t)},this.watchers={}}function getStats(e,r,t,a){const o=`${r}.gz`;e?fs.stat(o,((e,n)=>{if(e)return fs.stat(r,((e,t)=>{a.call(null,e,t,r)}));t.setHeader("Content-Encoding","gzip"),a(null,n,o)})):fs.stat(r,((e,t)=>{a.call(null,e,t,r)}))}FileServer.prototype.getFile=function(e,r,t,a,o,n){const s=this;if(!e.isFile())return s.errorCallback(o,n,{code:404,message:`404: Not Found ${r}`});const i=hashr.hash(r+e.mtime.getTime());return n.setHeader("ETag",i),n.setHeader("Cache-Control",`private, max-age=${a}`),o.headers&&o.headers["if-none-match"]===i?(n.writeHead(304),n.end()):(n.setHeader("Content-Type",t),n.on("error",s.errorCallback.bind(null,o,n)),0===e.size?n.end():void s.cache.write(r,n,createReadStream(s,o,n)))},FileServer.prototype.serveFile=function(e,r="text/plain",t=0){const a=this;if(!watchers[e]){const r=chokidar.watch(e,{persistent:!0,ignoreInitial:!0});r.on("change",(()=>{a.cache.del(e)})),watchers[e]=r,this.watchers[e]=r}if(!e||"string"!=typeof e)throw new Error("Must provide a fileName to serveFile");return function(o,n){const s=o.headers&&o.headers["accept-encoding"]&&~o.headers["accept-encoding"].indexOf("gzip");kgo({acceptsGzip:s,fileName:e,mimeType:r,maxAge:t,request:o,response:n})("stats","finalFilename",["acceptsGzip","fileName","response"],getStats)(["stats","finalFilename","mimeType","maxAge","request","response"],a.getFile.bind(a))(["*"],(r=>r.message&&~r.message.indexOf("ENOENT")?a.errorCallback(o,n,{code:404,message:`404: Not Found ${e}`}):a.errorCallback(o,n,r)))}},FileServer.prototype.serveDirectory=function(e,r,t=0){const a=this;if(!e||"string"!=typeof e)throw new Error("Must provide a rootDirectory to serveDirectory");if(!r||"object"!=typeof r)throw new Error("Must provide a mimeTypes object to serveDirectory");const o=Object.keys(r);for(let e=0;e<o.length;e++)if("."!==o[e].charAt(0))throw new Error(`Extension found without a leading periond ("."): ${o[e]}`);return function(o,n,s){arguments.length<3&&(s=o.url.slice(1));const i=path.join(e,s),c=path.extname(i).toLowerCase();return r[c]?~path.relative(e,i).indexOf("..")?a.errorCallback(o,n,{code:404,message:`404: Not Found ${s}`}):void a.serveFile(i,r[c],t)(o,n):a.errorCallback(o,n,{code:404,message:`404: Not Found ${i}`})}},FileServer.prototype.close=function(e){const r=[];Object.keys(this.watchers).forEach((e=>{const t=this.watchers[e];r.push(t.close()),delete this.watchers[e]})),Promise.all(r).then((()=>{e()}))},process.on("exit",(()=>{Object.values(watchers).forEach((e=>e.close()))})),module.exports=FileServer;
//# sourceMappingURL=/sm/17674200c8bf32be3e5767be4a39c8089335063b48ac7c110211bf0e28ce0c66.map