all files / blackbird/modules/headers/ Accept.js

100% Statements 28/28
100% Branches 18/18
100% Functions 8/8
100% Lines 28/28
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   26× 24×       20×                     13×                                                                 14×   14×     12× 12× 36×         12×              
const parseMediaValue = require("../utils/parseMediaValue");
const parseMediaValues = require("../utils/parseMediaValues");
const qualityFactorForMediaValue = require("../utils/qualityFactorForMediaValue");
const stringifyMediaValues = require("../utils/stringifyMediaValues");
const stringifyMediaValueWithoutQualityFactor = require("../utils/stringifyMediaValueWithoutQualityFactor");
const Header = require("../Header");
const R = require("ramda");
 
function paramsMatchIgnoringQualityFactor(params, givenParams) {
    for (const paramName in params) {
        if (params.hasOwnProperty(paramName) && paramName !== "q" && givenParams[paramName] !== params[paramName]) {
            return false;
        }
    }
 
    return true;
}
 
function byHighestPrecedence(a, b) {
  //   Accept: text/*, text/html, text/html;level=1, */*
  //
  // have the following precedence:
  //
  //   1) text/html;level=1
  //   2) text/html
  //   3) text/*
  //   4) */*
    return stringifyMediaValueWithoutQualityFactor(b).length - stringifyMediaValueWithoutQualityFactor(a).length;
}
 
/**
 * Represents an HTTP Accept header and provides several methods for
 * determining acceptable media types.
 *
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
 */
class Accept extends Header {
 
    constructor(value) {
        super("Accept", 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 media type is acceptable.
   */
    accepts(mediaType) {
        return this.qualityFactorForMediaType(mediaType) !== 0;
    }
 
  /**
   * Returns the quality factor for the given media type.
   */
    qualityFactorForMediaType(mediaType) {
        const values = this._mediaValues;
 
        if (R.isEmpty(values)) {
            return 1;
        }
 
        const givenValue = parseMediaValue(mediaType);
        const matchingValues = values.filter(function (value) {
            return (value.type === "*" || value.type === givenValue.type) &&
             (value.subtype === "*" || value.subtype === givenValue.subtype) &&
             paramsMatchIgnoringQualityFactor(value.params, givenValue.params);
        }).sort(byHighestPrecedence);
 
        if (R.isEmpty(matchingValues)) {
            return 0;
        }
 
        return qualityFactorForMediaValue(matchingValues[0]);
    }
 
}
 
module.exports = Accept;