UNPKG

mach

Version:
107 lines (85 loc) 4.21 kB
"use strict"; var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; var parseMediaValue = require("../utils/parseMediaValue"); var parseMediaValues = require("../utils/parseMediaValues"); var qualityFactorForMediaValue = require("../utils/qualityFactorForMediaValue"); var stringifyMediaValues = require("../utils/stringifyMediaValues"); var stringifyMediaValueWithoutQualityFactor = require("../utils/stringifyMediaValueWithoutQualityFactor"); var Header = require("../Header"); function paramsMatchIgnoringQualityFactor(params, givenParams) { for (var 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 */ var Accept = (function (Header) { function Accept(value) { _classCallCheck(this, Accept); _get(Object.getPrototypeOf(Accept.prototype), "constructor", this).call(this, "Accept", value); } _inherits(Accept, Header); _prototypeProperties(Accept, null, { value: { /** * Returns the value of this header as a string. */ get: function () { return stringifyMediaValues(this._mediaValues) || "*/*"; }, set: function (value) { this._mediaValues = value ? parseMediaValues(value) : []; }, configurable: true }, accepts: { /** * Returns true if the given media type is acceptable. */ value: function accepts(mediaType) { return this.qualityFactorForMediaType(mediaType) !== 0; }, writable: true, configurable: true }, qualityFactorForMediaType: { /** * Returns the quality factor for the given media type. */ value: function qualityFactorForMediaType(mediaType) { var values = this._mediaValues; if (!values.length) { return 1; }var givenValue = parseMediaValue(mediaType); var 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 (!matchingValues.length) { return 0; }return qualityFactorForMediaValue(matchingValues[0]); }, writable: true, configurable: true } }); return Accept; })(Header); module.exports = Accept;