123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- var statusCodeCacheableByDefault = [200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501];
- var understoodStatuses = [200, 203, 204, 300, 301, 302, 303, 307, 308, 404, 405, 410, 414, 501];
- var hopByHopHeaders = { 'connection': true, 'keep-alive': true, 'proxy-authenticate': true, 'proxy-authorization': true, 'te': true, 'trailer': true, 'transfer-encoding': true, 'upgrade': true };
- var excludedFromRevalidationUpdate = {
-
- 'content-length': true, 'content-encoding': true, 'transfer-encoding': true,
- 'content-range': true
- };
- function parseCacheControl(header) {
- var cc = {};
- if (!header) return cc;
-
-
- var parts = header.trim().split(/\s*,\s*/);
- for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref = _i.value;
- }
- var part = _ref;
- var _part$split = part.split(/\s*=\s*/, 2),
- k = _part$split[0],
- v = _part$split[1];
- cc[k] = v === undefined ? true : v.replace(/^"|"$/g, '');
- }
- return cc;
- }
- function formatCacheControl(cc) {
- var parts = [];
- for (var k in cc) {
- var v = cc[k];
- parts.push(v === true ? k : k + '=' + v);
- }
- if (!parts.length) {
- return undefined;
- }
- return parts.join(', ');
- }
- module.exports = function () {
- function CachePolicy(req, res) {
- var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
- shared = _ref2.shared,
- cacheHeuristic = _ref2.cacheHeuristic,
- immutableMinTimeToLive = _ref2.immutableMinTimeToLive,
- ignoreCargoCult = _ref2.ignoreCargoCult,
- _fromObject = _ref2._fromObject;
- _classCallCheck(this, CachePolicy);
- if (_fromObject) {
- this._fromObject(_fromObject);
- return;
- }
- if (!res || !res.headers) {
- throw Error("Response headers missing");
- }
- this._assertRequestHasHeaders(req);
- this._responseTime = this.now();
- this._isShared = shared !== false;
- this._cacheHeuristic = undefined !== cacheHeuristic ? cacheHeuristic : 0.1;
- this._immutableMinTtl = undefined !== immutableMinTimeToLive ? immutableMinTimeToLive : 24 * 3600 * 1000;
- this._status = 'status' in res ? res.status : 200;
- this._resHeaders = res.headers;
- this._rescc = parseCacheControl(res.headers['cache-control']);
- this._method = 'method' in req ? req.method : 'GET';
- this._url = req.url;
- this._host = req.headers.host;
- this._noAuthorization = !req.headers.authorization;
- this._reqHeaders = res.headers.vary ? req.headers : null;
- this._reqcc = parseCacheControl(req.headers['cache-control']);
-
-
- if (ignoreCargoCult && "pre-check" in this._rescc && "post-check" in this._rescc) {
- delete this._rescc['pre-check'];
- delete this._rescc['post-check'];
- delete this._rescc['no-cache'];
- delete this._rescc['no-store'];
- delete this._rescc['must-revalidate'];
- this._resHeaders = Object.assign({}, this._resHeaders, { 'cache-control': formatCacheControl(this._rescc) });
- delete this._resHeaders.expires;
- delete this._resHeaders.pragma;
- }
-
-
- if (!res.headers['cache-control'] && /no-cache/.test(res.headers.pragma)) {
- this._rescc['no-cache'] = true;
- }
- }
- CachePolicy.prototype.now = function now() {
- return Date.now();
- };
- CachePolicy.prototype.storable = function storable() {
-
- return !!(!this._reqcc['no-store'] && (
-
-
- 'GET' === this._method || 'HEAD' === this._method || 'POST' === this._method && this._hasExplicitExpiration()) &&
-
- understoodStatuses.indexOf(this._status) !== -1 &&
-
- !this._rescc['no-store'] && (
-
- !this._isShared || !this._rescc.private) && (
-
- !this._isShared || this._noAuthorization || this._allowsStoringAuthenticated()) && (
-
-
- this._resHeaders.expires ||
-
-
-
- this._rescc.public || this._rescc['max-age'] || this._rescc['s-maxage'] ||
-
- statusCodeCacheableByDefault.indexOf(this._status) !== -1));
- };
- CachePolicy.prototype._hasExplicitExpiration = function _hasExplicitExpiration() {
-
- return this._isShared && this._rescc['s-maxage'] || this._rescc['max-age'] || this._resHeaders.expires;
- };
- CachePolicy.prototype._assertRequestHasHeaders = function _assertRequestHasHeaders(req) {
- if (!req || !req.headers) {
- throw Error("Request headers missing");
- }
- };
- CachePolicy.prototype.satisfiesWithoutRevalidation = function satisfiesWithoutRevalidation(req) {
- this._assertRequestHasHeaders(req);
-
-
-
- var requestCC = parseCacheControl(req.headers['cache-control']);
- if (requestCC['no-cache'] || /no-cache/.test(req.headers.pragma)) {
- return false;
- }
- if (requestCC['max-age'] && this.age() > requestCC['max-age']) {
- return false;
- }
- if (requestCC['min-fresh'] && this.timeToLive() < 1000 * requestCC['min-fresh']) {
- return false;
- }
-
-
- if (this.stale()) {
- var allowsStale = requestCC['max-stale'] && !this._rescc['must-revalidate'] && (true === requestCC['max-stale'] || requestCC['max-stale'] > this.age() - this.maxAge());
- if (!allowsStale) {
- return false;
- }
- }
- return this._requestMatches(req, false);
- };
- CachePolicy.prototype._requestMatches = function _requestMatches(req, allowHeadMethod) {
-
- return (!this._url || this._url === req.url) && this._host === req.headers.host && (
-
- !req.method || this._method === req.method || allowHeadMethod && 'HEAD' === req.method) &&
-
- this._varyMatches(req);
- };
- CachePolicy.prototype._allowsStoringAuthenticated = function _allowsStoringAuthenticated() {
-
- return this._rescc['must-revalidate'] || this._rescc.public || this._rescc['s-maxage'];
- };
- CachePolicy.prototype._varyMatches = function _varyMatches(req) {
- if (!this._resHeaders.vary) {
- return true;
- }
-
- if (this._resHeaders.vary === '*') {
- return false;
- }
- var fields = this._resHeaders.vary.trim().toLowerCase().split(/\s*,\s*/);
- for (var _iterator2 = fields, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
- var _ref3;
- if (_isArray2) {
- if (_i2 >= _iterator2.length) break;
- _ref3 = _iterator2[_i2++];
- } else {
- _i2 = _iterator2.next();
- if (_i2.done) break;
- _ref3 = _i2.value;
- }
- var name = _ref3;
- if (req.headers[name] !== this._reqHeaders[name]) return false;
- }
- return true;
- };
- CachePolicy.prototype._copyWithoutHopByHopHeaders = function _copyWithoutHopByHopHeaders(inHeaders) {
- var headers = {};
- for (var name in inHeaders) {
- if (hopByHopHeaders[name]) continue;
- headers[name] = inHeaders[name];
- }
-
- if (inHeaders.connection) {
- var tokens = inHeaders.connection.trim().split(/\s*,\s*/);
- for (var _iterator3 = tokens, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
- var _ref4;
- if (_isArray3) {
- if (_i3 >= _iterator3.length) break;
- _ref4 = _iterator3[_i3++];
- } else {
- _i3 = _iterator3.next();
- if (_i3.done) break;
- _ref4 = _i3.value;
- }
- var _name = _ref4;
- delete headers[_name];
- }
- }
- if (headers.warning) {
- var warnings = headers.warning.split(/,/).filter(function (warning) {
- return !/^\s*1[0-9][0-9]/.test(warning);
- });
- if (!warnings.length) {
- delete headers.warning;
- } else {
- headers.warning = warnings.join(',').trim();
- }
- }
- return headers;
- };
- CachePolicy.prototype.responseHeaders = function responseHeaders() {
- var headers = this._copyWithoutHopByHopHeaders(this._resHeaders);
- var age = this.age();
-
-
- if (age > 3600 * 24 && !this._hasExplicitExpiration() && this.maxAge() > 3600 * 24) {
- headers.warning = (headers.warning ? `${headers.warning}, ` : '') + '113 - "rfc7234 5.5.4"';
- }
- headers.age = `${Math.round(age)}`;
- return headers;
- };
-
- CachePolicy.prototype.date = function date() {
- var dateValue = Date.parse(this._resHeaders.date);
- var maxClockDrift = 8 * 3600 * 1000;
- if (Number.isNaN(dateValue) || dateValue < this._responseTime - maxClockDrift || dateValue > this._responseTime + maxClockDrift) {
- return this._responseTime;
- }
- return dateValue;
- };
-
- CachePolicy.prototype.age = function age() {
- var age = Math.max(0, (this._responseTime - this.date()) / 1000);
- if (this._resHeaders.age) {
- var ageValue = this._ageValue();
- if (ageValue > age) age = ageValue;
- }
- var residentTime = (this.now() - this._responseTime) / 1000;
- return age + residentTime;
- };
- CachePolicy.prototype._ageValue = function _ageValue() {
- var ageValue = parseInt(this._resHeaders.age);
- return isFinite(ageValue) ? ageValue : 0;
- };
-
- CachePolicy.prototype.maxAge = function maxAge() {
- if (!this.storable() || this._rescc['no-cache']) {
- return 0;
- }
-
-
- if (this._isShared && this._resHeaders['set-cookie'] && !this._rescc.public && !this._rescc.immutable) {
- return 0;
- }
- if (this._resHeaders.vary === '*') {
- return 0;
- }
- if (this._isShared) {
- if (this._rescc['proxy-revalidate']) {
- return 0;
- }
-
- if (this._rescc['s-maxage']) {
- return parseInt(this._rescc['s-maxage'], 10);
- }
- }
-
- if (this._rescc['max-age']) {
- return parseInt(this._rescc['max-age'], 10);
- }
- var defaultMinTtl = this._rescc.immutable ? this._immutableMinTtl : 0;
- var dateValue = this.date();
- if (this._resHeaders.expires) {
- var expires = Date.parse(this._resHeaders.expires);
-
- if (Number.isNaN(expires) || expires < dateValue) {
- return 0;
- }
- return Math.max(defaultMinTtl, (expires - dateValue) / 1000);
- }
- if (this._resHeaders['last-modified']) {
- var lastModified = Date.parse(this._resHeaders['last-modified']);
- if (isFinite(lastModified) && dateValue > lastModified) {
- return Math.max(defaultMinTtl, (dateValue - lastModified) / 1000 * this._cacheHeuristic);
- }
- }
- return defaultMinTtl;
- };
- CachePolicy.prototype.timeToLive = function timeToLive() {
- return Math.max(0, this.maxAge() - this.age()) * 1000;
- };
- CachePolicy.prototype.stale = function stale() {
- return this.maxAge() <= this.age();
- };
- CachePolicy.fromObject = function fromObject(obj) {
- return new this(undefined, undefined, { _fromObject: obj });
- };
- CachePolicy.prototype._fromObject = function _fromObject(obj) {
- if (this._responseTime) throw Error("Reinitialized");
- if (!obj || obj.v !== 1) throw Error("Invalid serialization");
- this._responseTime = obj.t;
- this._isShared = obj.sh;
- this._cacheHeuristic = obj.ch;
- this._immutableMinTtl = obj.imm !== undefined ? obj.imm : 24 * 3600 * 1000;
- this._status = obj.st;
- this._resHeaders = obj.resh;
- this._rescc = obj.rescc;
- this._method = obj.m;
- this._url = obj.u;
- this._host = obj.h;
- this._noAuthorization = obj.a;
- this._reqHeaders = obj.reqh;
- this._reqcc = obj.reqcc;
- };
- CachePolicy.prototype.toObject = function toObject() {
- return {
- v: 1,
- t: this._responseTime,
- sh: this._isShared,
- ch: this._cacheHeuristic,
- imm: this._immutableMinTtl,
- st: this._status,
- resh: this._resHeaders,
- rescc: this._rescc,
- m: this._method,
- u: this._url,
- h: this._host,
- a: this._noAuthorization,
- reqh: this._reqHeaders,
- reqcc: this._reqcc
- };
- };
-
- CachePolicy.prototype.revalidationHeaders = function revalidationHeaders(incomingReq) {
- this._assertRequestHasHeaders(incomingReq);
- var headers = this._copyWithoutHopByHopHeaders(incomingReq.headers);
-
- delete headers['if-range'];
- if (!this._requestMatches(incomingReq, true) || !this.storable()) {
-
-
- delete headers['if-none-match'];
- delete headers['if-modified-since'];
- return headers;
- }
-
- if (this._resHeaders.etag) {
- headers['if-none-match'] = headers['if-none-match'] ? `${headers['if-none-match']}, ${this._resHeaders.etag}` : this._resHeaders.etag;
- }
-
- var forbidsWeakValidators = headers['accept-ranges'] || headers['if-match'] || headers['if-unmodified-since'] || this._method && this._method != 'GET';
-
- if (forbidsWeakValidators) {
- delete headers['if-modified-since'];
- if (headers['if-none-match']) {
- var etags = headers['if-none-match'].split(/,/).filter(function (etag) {
- return !/^\s*W\//.test(etag);
- });
- if (!etags.length) {
- delete headers['if-none-match'];
- } else {
- headers['if-none-match'] = etags.join(',').trim();
- }
- }
- } else if (this._resHeaders['last-modified'] && !headers['if-modified-since']) {
- headers['if-modified-since'] = this._resHeaders['last-modified'];
- }
- return headers;
- };
-
- CachePolicy.prototype.revalidatedPolicy = function revalidatedPolicy(request, response) {
- this._assertRequestHasHeaders(request);
- if (!response || !response.headers) {
- throw Error("Response headers missing");
- }
-
-
- var matches = false;
- if (response.status !== undefined && response.status != 304) {
- matches = false;
- } else if (response.headers.etag && !/^\s*W\//.test(response.headers.etag)) {
-
-
-
- matches = this._resHeaders.etag && this._resHeaders.etag.replace(/^\s*W\//, '') === response.headers.etag;
- } else if (this._resHeaders.etag && response.headers.etag) {
-
-
-
- matches = this._resHeaders.etag.replace(/^\s*W\//, '') === response.headers.etag.replace(/^\s*W\//, '');
- } else if (this._resHeaders['last-modified']) {
- matches = this._resHeaders['last-modified'] === response.headers['last-modified'];
- } else {
-
-
-
-
- if (!this._resHeaders.etag && !this._resHeaders['last-modified'] && !response.headers.etag && !response.headers['last-modified']) {
- matches = true;
- }
- }
- if (!matches) {
- return {
- policy: new this.constructor(request, response),
- modified: true
- };
- }
-
-
- var headers = {};
- for (var k in this._resHeaders) {
- headers[k] = k in response.headers && !excludedFromRevalidationUpdate[k] ? response.headers[k] : this._resHeaders[k];
- }
- var newResponse = Object.assign({}, response, {
- status: this._status,
- method: this._method,
- headers
- });
- return {
- policy: new this.constructor(request, newResponse),
- modified: false
- };
- };
- return CachePolicy;
- }();
|