all files / modules/headers/ AcceptEncoding.js

90.63% Statements 29/32
83.33% Branches 15/18
100% Functions 7/7
90.63% Lines 29/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                   15×                   15×             13×             22×   22× 22× 21× 12×                       22×           16×     10×          
const parseMediaValue = require("../utils/parseMediaValue");
const parseMediaValues = require("../utils/parseMediaValues");
const qualityFactorForMediaValue = require("../utils/qualityFactorForMediaValue");
const stringifyMediaValues = require("../utils/stringifyMediaValues");
const Header = require("../Header");
const R = require("ramda");
 
function byHighestPrecedence(a, b) {
  // "*" gets least precedence, all others are equal
    let precedence;
    Iif (a === "*") {
        precedence = -1;
    } else Iif (b === "*") {
        precedence = 1;
    } else {
        precedence = 0;
    }
    return precedence;
}
 
/**
 * Represents an HTTP Accept-Encoding header and provides several methods
 * for determining acceptable content encodings.
 *
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
 */
class AcceptEncoding extends Header {
 
    constructor(value) {
        super("Accept-Encoding", value);
    }
 
  /**
   * Returns the value of this header as a string.
   */
    get value() {
        return stringifyMediaValues(this._mediaValues) || "";
    }
 
    set value(value) {
        this._mediaValues = value ? parseMediaValues(value) : [];
    }
 
  /**
   * Returns true if the given encoding is acceptable.
   */
    accepts(encoding) {
        return this.qualityFactorForEncoding(encoding) !== 0;
    }
 
  /**
   * Returns the quality factor for the given encoding.
   */
    qualityFactorForEncoding(encoding) {
        const values = this._mediaValues;
 
        const givenValue = parseMediaValue(encoding);
        const matchingValues = values.filter(function (value) {
            if (value.type === "*") {
                return true;
            }
 
            return value.type === givenValue.type;
        }).sort(byHighestPrecedence);
 
    // From RFC 2616:
    // The "identity" content-coding is always acceptable, unless
    // specifically refused because the Accept-Encoding field includes
    // "identity;q=0", or because the field includes "*;q=0" and does
    // not explicitly include the "identity" content-coding. If the
    // Accept-Encoding field-value is empty, then only the "identity"
    // encoding is acceptable.
        if (givenValue.type === "identity") {
            Iif (matchingValues.length && matchingValues[0].type === "identity") {
                return qualityFactorForMediaValue(matchingValues[0]);
            }
 
            return 1;
        }
 
        if (R.isEmpty(matchingValues)) {
            return 0;
        }
 
        return qualityFactorForMediaValue(matchingValues[0]);
    }
 
}
 
module.exports = AcceptEncoding;