'use strict'; const fs=require('fs'), mimeTypes=require(`${__dirname}/MimeTypes.js`), unsafeKeys=new Set(['__proto__','constructor','prototype']), nestedKeys=new Set(['server','https','domains','restrictedType']), pendingContentTypes=Symbol('pendingContentTypes'), unmaterializedContentTypes=Symbol('unmaterializedContentTypes'), routingState=Symbol('routingState'), lazyContentTypeDescriptor=Object.freeze({ configurable:true, enumerable:true, get:materializeContentTypes, set:replaceContentTypes }), trackedDomainsHandler=Object.freeze({ set:setTrackedDomain, deleteProperty:deleteTrackedDomain, defineProperty:defineTrackedDomain }); class Config{ constructor(userConfig){ assignDefaults(this); this.merge(userConfig); installRoutingTracking(this); } merge(userConfig){ if(userConfig!==undefined && userConfig!==null){ mergeConfig(this,userConfig); } return this; } contentTypeFor(extension){ if(isLazyContentTypes(this)){ const state=this[pendingContentTypes]; if(state.value!==unmaterializedContentTypes){ const contentTypes=state.value; if(!contentTypes || !Object.hasOwn(contentTypes,extension)){ return undefined; } return contentTypes[extension]; } const overlays=state.overlays; if(overlays && Object.hasOwn(overlays,extension)){ return overlays[extension]; } return Object.hasOwn(mimeTypes,extension) ? mimeTypes[extension] : undefined; } const contentTypes=this.contentType; if(!contentTypes || !Object.hasOwn(contentTypes,extension)){ return undefined; } return contentTypes[extension]; } routingVersion(){ const state=routingStateFor(this); if(!Object.is(state.root,this.root)){ state.root=this.root; incrementRoutingVersion(state); } if(!Object.is(state.domain,this.domain)){ state.domain=this.domain; incrementRoutingVersion(state); } if(state.domains!==this.domains){ trackDomains(this,state,true); incrementRoutingVersion(state); }else if(domainsAliasChanged(state)){ incrementRoutingVersion(state); } return state.version; } static get defaults(){ return clone(defaultConfigs); } static get mimeTypes(){ return clone(mimeTypes); } } const defaultConfigs=deepFreeze({ verbose : false, port : 8080, root : process.cwd(), host : '127.0.0.1', domain : '0.0.0.0', log : false, logBody : false, logFunction : serverLogging, domains : {}, server : { index : 'index.html', noCache : true, allowDotfiles : false, timeout : 30000, requestTimeout : 300000, headersTimeout : 60000, keepAliveTimeout : 5000, maxRequestBodyBytes : false, compression : false, compressionThreshold: 1024, brotliQuality : 4, spaFallback : false }, https : { ca : '', privateKey : '', certificate : '', passphrase : false, http2 : true, port : 443, only : false }, contentType : mimeTypes, restrictedType: {}, errors : { headers : { 'Content-Type' : 'text/plain; charset=utf-8', 'X-Content-Type-Options' : 'nosniff' }, 400 : '400 Bad Request', 403 : '403 Forbidden', 404 : '404 Not Found', 405 : '405 Method Not Allowed', 413 : '413 Payload Too Large', 415 : '415 Unsupported Media Type', 416 : '416 Range Not Satisfiable', 421 : '421 Misdirected Request', 500 : '500 Internal Server Error' } }); function assignDefaults(config){ for(const key of Object.keys(defaultConfigs)){ if(key==='contentType'){ installLazyContentTypes(config); continue; } config[key]=clone(defaultConfigs[key]); } } function installRoutingTracking(config){ const state=routingStateFor(config); state.root=config.root; state.domain=config.domain; if(state.domains!==config.domains){ trackDomains(config,state,false); } } function routingStateFor(config){ let state=config[routingState]; if(state && state.owner===config){ return state; } state={ owner:config, version:state && Number.isSafeInteger(state.version) ? state.version : 0, root:config.root, domain:config.domain, domains:null }; Object.defineProperty( config, routingState, { value:state, writable:true } ); return state; } function trackDomains(config,state,watchAlias){ const domains=config.domains; if(!isRecord(domains)){ state.domains=domains; clearDomainsAlias(state); return; } const handler=Object.create(trackedDomainsHandler); handler.state=state; const tracked=new Proxy(domains,handler); const descriptor=Object.getOwnPropertyDescriptor(config,'domains'); if(watchAlias){ state.domainsAlias=domains; state.domainsAliasSnapshot=snapshotDomains(domains); }else{ clearDomainsAlias(state); } if(descriptor && Object.hasOwn(descriptor,'value') && descriptor.writable){ config.domains=tracked; state.domains=tracked; return; } state.domains=domains; } function setTrackedDomain(target,key,value){ const changed=Reflect.set(target,key,value,target); if(changed && typeof key==='string'){ incrementRoutingVersion(this.state); refreshDomainsAliasSnapshot(this.state,target); } return changed; } function deleteTrackedDomain(target,key){ const present=Object.hasOwn(target,key), changed=Reflect.deleteProperty(target,key); if(changed && present && typeof key==='string'){ incrementRoutingVersion(this.state); refreshDomainsAliasSnapshot(this.state,target); } return changed; } function defineTrackedDomain(target,key,descriptor){ const changed=Reflect.defineProperty(target,key,descriptor); if(changed && typeof key==='string'){ incrementRoutingVersion(this.state); refreshDomainsAliasSnapshot(this.state,target); } return changed; } function clearDomainsAlias(state){ if(Object.hasOwn(state,'domainsAlias')){ delete state.domainsAlias; delete state.domainsAliasSnapshot; } } function refreshDomainsAliasSnapshot(state,target){ if(state.domainsAlias===target){ state.domainsAliasSnapshot=snapshotDomains(target); } } function domainsAliasChanged(state){ const domains=state.domainsAlias; if(!domains){ return false; } const keys=Object.keys(domains), snapshot=state.domainsAliasSnapshot; let changed=!snapshot || keys.length!==snapshot.keys.length; if(!changed){ for(let index=0;index