Very Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174/*! * Adapted directly from negotiator at https://github.com/jshttp/negotiator/ * which is licensed as follows: * * (The MIT License) * * Copyright (c) 2012-2014 Federico Romero * Copyright (c) 2012-2014 Isaac Z. Schlueter * Copyright (c) 2014-2015 Douglas Christopher Wilson * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
interface Specificity { encoding?: string; i: number; o?: number; q: number; s?: number;}
const simpleEncodingRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
function parseEncoding(str: string, i: number): Specificity | undefined { const match = simpleEncodingRegExp.exec(str); if (!match) { return undefined; }
const encoding = match[1]; let q = 1; if (match[2]) { const params = match[2].split(";"); for (const param of params) { const p = param.trim().split("="); if (p[0] === "q") { q = parseFloat(p[1]); break; } } }
return { encoding, q, i };}
function specify( encoding: string, spec: Specificity, i: number = -1): Specificity | undefined { if (!spec.encoding) { return; } let s = 0; if (spec.encoding.toLocaleLowerCase() === encoding.toLocaleLowerCase()) { s = 1; } else if (spec.encoding !== "*") { return; }
return { i, o: spec.i, q: spec.q, s };}
function parseAcceptEncoding(accept: string): Specificity[] { const accepts = accept.split(","); const parsedAccepts: Specificity[] = []; let hasIdentity = false; let minQuality = 1;
for (let i = 0; i < accepts.length; i++) { const encoding = parseEncoding(accepts[i].trim(), i);
if (encoding) { parsedAccepts.push(encoding); hasIdentity = hasIdentity || !!specify("identity", encoding); minQuality = Math.min(minQuality, encoding.q || 1); } }
if (!hasIdentity) { parsedAccepts.push({ encoding: "identity", q: minQuality, i: accepts.length - 1 }); }
return parsedAccepts;}
function compareSpecs(a: Specificity, b: Specificity) { return ( b.q - a.q || (b.s || 0) - (a.s || 0) || (a.o || 0) - (b.o || 0) || a.i - b.i || 0 );}
function isQuality(spec: Specificity) { return spec.q > 0;}
function getEncodingPriority( encoding: string, accepted: Specificity[], index: number): Specificity { let priority: Specificity = { o: -1, q: 0, s: 0, i: 0 };
for (const s of accepted) { const spec = specify(encoding, s, index);
if ( spec && (priority.s! - spec.s! || priority.q - spec.q || priority.o! - spec.o!) < 0 ) { priority = spec; } }
return priority;}
/** Given an `Accept-Encoding` string, parse out the encoding returning a * negotiated encoding based on the `provided` encodings otherwise just a * prioritized array of encodings. */export function preferredEncodings( accept: string, provided?: string[]): string[] { const accepts = parseAcceptEncoding(accept);
if (!provided) { return accepts .filter(isQuality) .sort(compareSpecs) .map(spec => spec.encoding!); }
const priorities = provided.map((type, index) => getEncodingPriority(type, accepts, index) );
return priorities .filter(isQuality) .sort(compareSpecs) .map(priority => provided[priorities.indexOf(priority)]);}