mach
Version:
HTTP for JavaScript
106 lines (83 loc) • 4.22 kB
JavaScript
;
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 Header = require("../Header");
function byHighestPrecedence(a, b) {
// "*" gets least precedence, all others are equal
return a === "*" ? -1 : b === "*" ? 1 : 0;
}
/**
* 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
*/
var AcceptEncoding = (function (Header) {
function AcceptEncoding(value) {
_classCallCheck(this, AcceptEncoding);
_get(Object.getPrototypeOf(AcceptEncoding.prototype), "constructor", this).call(this, "Accept-Encoding", value);
}
_inherits(AcceptEncoding, Header);
_prototypeProperties(AcceptEncoding, 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 encoding is acceptable.
*/
value: function accepts(encoding) {
return this.qualityFactorForEncoding(encoding) !== 0;
},
writable: true,
configurable: true
},
qualityFactorForEncoding: {
/**
* Returns the quality factor for the given encoding.
*/
value: function qualityFactorForEncoding(encoding) {
var values = this._mediaValues;
var givenValue = parseMediaValue(encoding);
var 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") {
if (matchingValues.length && matchingValues[0].type === "identity") {
return qualityFactorForMediaValue(matchingValues[0]);
}return 1;
}
if (!matchingValues.length) {
return 0;
}return qualityFactorForMediaValue(matchingValues[0]);
},
writable: true,
configurable: true
}
});
return AcceptEncoding;
})(Header);
module.exports = AcceptEncoding;