mach
Version:
HTTP for JavaScript
105 lines (82 loc) • 4.25 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-Charset header and provides several methods
* for determining acceptable content character sets.
*
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2
*/
var AcceptCharset = (function (Header) {
function AcceptCharset(value) {
_classCallCheck(this, AcceptCharset);
_get(Object.getPrototypeOf(AcceptCharset.prototype), "constructor", this).call(this, "Accept-Charset", value);
}
_inherits(AcceptCharset, Header);
_prototypeProperties(AcceptCharset, 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 charset is acceptable.
*/
value: function accepts(charset) {
return this.qualityFactorForCharset(charset) !== 0;
},
writable: true,
configurable: true
},
qualityFactorForCharset: {
/**
* Returns the quality factor for the given charset.
*/
value: function qualityFactorForCharset(charset) {
var values = this._mediaValues;
var givenValue = parseMediaValue(charset);
var matchingValues = values.filter(function (value) {
if (value.type === "*") return true;
return value.type === givenValue.type;
}).sort(byHighestPrecedence);
// From RFC 2616:
// The special value "*", if present in the Accept-Charset field, matches every character
// set (including ISO-8859-1) which is not mentioned elsewhere in the Accept-Charset field.
// If no "*" is present in an Accept-Charset field, then all character sets not explicitly
// mentioned get a quality value of 0, except for ISO-8859-1, which gets a quality value of
// 1 if not explicitly mentioned.
if (givenValue.type === "iso-8859-1") {
if (matchingValues.length && matchingValues[0].type === "iso-8859-1") {
return qualityFactorForMediaValue(matchingValues[0]);
}return 1;
}
if (!matchingValues.length) {
return 0;
}return qualityFactorForMediaValue(matchingValues[0]);
},
writable: true,
configurable: true
}
});
return AcceptCharset;
})(Header);
module.exports = AcceptCharset;