edge-utils
Version:
Platform-agnostic utilities for edge computing and serverless environments
3 lines (2 loc) • 3.96 kB
JavaScript
class t{constructor(t={}){this.defaultContentType=t.defaultContentType||"application/json",this.defaultLanguage=t.defaultLanguage||"en",this.defaultEncoding=t.defaultEncoding||"identity",this.defaultCharset=t.defaultCharset||"utf-8",this.supportedTypes=t.supportedTypes||[],this.supportedLanguages=t.supportedLanguages||[],this.supportedEncodings=t.supportedEncodings||[],this.supportedCharsets=t.supportedCharsets||[]}negotiateContentType(t){if(!t)return this.defaultContentType;const e=this.parseAcceptHeader(t),s=this.supportedTypes.length>0?this.supportedTypes:[this.defaultContentType];return this.findBestMatch(e,s,this.defaultContentType)}negotiateLanguage(t){if(!t)return this.defaultLanguage;const e=this.parseAcceptHeader(t),s=this.supportedLanguages.length>0?this.supportedLanguages:[this.defaultLanguage];return this.findBestMatch(e,s,this.defaultLanguage)}negotiateEncoding(t){if(!t)return this.defaultEncoding;const e=this.parseAcceptHeader(t),s=this.supportedEncodings.length>0?this.supportedEncodings:[this.defaultEncoding];return this.findBestMatch(e,s,this.defaultEncoding)}negotiateCharset(t){if(!t)return this.defaultCharset;const e=this.parseAcceptHeader(t),s=this.supportedCharsets.length>0?this.supportedCharsets:[this.defaultCharset];return this.findBestMatch(e,s,this.defaultCharset)}parseAcceptHeader(t){if(!t||"string"!=typeof t)return[];const e=t.split(",").map(t=>t.trim()),s=[];for(const t of e){const e=t.split(";"),n=e[0].trim().toLowerCase();let i=1;for(let t=1;t<e.length;t++){const s=e[t].trim();if(s.startsWith("q=")){const t=parseFloat(s.substring(2));!isNaN(t)&&t>=0&&t<=1&&(i=t);break}}const a=this.calculateSpecificity(n);s.push({value:n,quality:i,specificity:a,original:t})}return s.sort((t,e)=>Math.abs(t.quality-e.quality)>.001?e.quality-t.quality:e.specificity-t.specificity)}calculateSpecificity(t){return"*/*"===t?1:t.endsWith("/*")?2:t.includes("/")?3:0}findBestMatch(t,e,s){const n=new Set(e.map(t=>t.toLowerCase())),i=new Map(e.map(t=>[t.toLowerCase(),t]));for(const s of t){const t=s.value;if(n.has(t))return i.get(t);if("*/*"!==t){if(t.endsWith("/*")){const s=t.slice(0,-2);for(const t of e)if(t.toLowerCase().startsWith(s+"/"))return t}if(t.includes("/")){const[s]=t.split("/");for(const t of e){const e=t.toLowerCase();if(e.startsWith(s+"/")&&e.endsWith("/*"))return t}}}}return s}negotiate(t={}){const e={};for(const[s,n]of Object.entries(t))e[s.toLowerCase()]=n;return{contentType:this.negotiateContentType(e.accept),language:this.negotiateLanguage(e["accept-language"]),encoding:this.negotiateEncoding(e["accept-encoding"]),charset:this.negotiateCharset(e["accept-charset"])}}setSupportedOptions(t={}){t.contentTypes&&(this.supportedTypes=t.contentTypes),t.languages&&(this.supportedLanguages=t.languages),t.encodings&&(this.supportedEncodings=t.encodings),t.charsets&&(this.supportedCharsets=t.charsets)}getSupportedOptions(){return{contentTypes:this.supportedTypes,languages:this.supportedLanguages,encodings:this.supportedEncodings,charsets:this.supportedCharsets}}}module.exports={ContentNegotiator:t,QualityValue:class{static parse(t){if(!t||"string"!=typeof t)return{value:"",quality:1};const e=t.split(";"),s=e[0].trim();let n=1;for(let t=1;t<e.length;t++){const s=e[t].trim();if(s.startsWith("q=")){const t=parseFloat(s.substring(2));!isNaN(t)&&t>=0&&t<=1&&(n=t);break}}return{value:s,quality:n}}static sortByQuality(t){return t.sort((t,e)=>{const s="number"==typeof t.quality?t.quality:1;return("number"==typeof e.quality?e.quality:1)-s})}},createContentNegotiationMiddleware:function(e={}){const s=new t(e);return function(t,e={}){const n=t.headers||{},i=s.negotiate(n);return e.contentNegotiation||(e.contentNegotiation={}),Object.assign(e.contentNegotiation,i),e.getPreferredContentType=()=>i.contentType,e.getPreferredLanguage=()=>i.language,e.getPreferredEncoding=()=>i.encoding,e.getPreferredCharset=()=>i.charset,{request:t,context:e}}}};
//# sourceMappingURL=content-negotiation.esm.js.map