Add ETags that won't work

This commit is contained in:
Ilya Ig. Petrov 2019-02-26 17:52:33 +00:00
parent 4965921c06
commit 272a858cc3
2 changed files with 62 additions and 44 deletions

View File

@ -11,30 +11,38 @@
ifModifiedSince( ifModifiedSince(
url, url,
lastModified, cacheImprints,
cb = mandatory() cb = mandatory(),
) { ) {
if (!cacheImprints.eTag && !cacheImprints.lastModifiedStr) {
throw new TypeError('No cacheImprints provided!');
}
if (url.startsWith('data:')) { if (url.startsWith('data:')) {
return cb(null, false); return cb(null, false);
} }
const wasModifiedIn1970 = new Date(0).toUTCString();
const notModifiedCode = 304; const notModifiedCode = 304;
fetch(url, { fetch(url, {
method: 'HEAD', method: 'HEAD',
headers: new Headers({ headers: new Headers(
'If-Modified-Since': lastModified, cacheImprints.eTag
}), ? {'If-None-Match': cacheImprints.eTag}
: {'If-Modified-Since': cacheImprints.lastModifiedStr},
),
}).then( }).then(
(res) => { (res) => {
const eTag = res.headers.get('ETag');
const lastModifiedStr = res.headers.get('Last-Modified');
const newCacheImprints = (eTag || lastModifiedStr) ? { eTag, lastModifiedStr } : undefined;
cb( cb(
null, null,
res.status === notModifiedCode ? res.status === notModifiedCode
false : ? undefined
(res.headers.get('Last-Modified') || wasModifiedIn1970) : newCacheImprints,
); );
}, },
errorsLib.clarifyThen(checkCon, (err) => cb(err, wasModifiedIn1970)) errorsLib.clarifyThen(checkCon, (err) => cb(err)),
); );
}, },
@ -42,6 +50,7 @@
get(url, cb = mandatory()) { get(url, cb = mandatory()) {
const start = Date.now(); const start = Date.now();
// 'no-store' disables cache completely, we handle caching manually instead.
fetch(url, {cache: 'no-store'}).then( fetch(url, {cache: 'no-store'}).then(
(res) => { (res) => {
@ -54,7 +63,7 @@
console.log('Calling CB...'); console.log('Calling CB...');
cb(err, text); cb(err, text);
}, },
cb cb,
); );
}; };
@ -64,8 +73,8 @@
return textCb( return textCb(
errorsLib.clarify( errorsLib.clarify(
res, res,
'Получен ответ с неудачным HTTP-кодом ' + status + '.' 'Получен ответ с неудачным HTTP-кодом ' + status + '.',
) ),
); );
} }
@ -73,7 +82,7 @@
textCb(); textCb();
}, },
errorsLib.clarifyThen(checkCon, cb) errorsLib.clarifyThen(checkCon, cb),
); );
}, },
@ -98,7 +107,7 @@
cb(); cb();
}, },
errorsLib.clarifyThen(checkCon, cb) errorsLib.clarifyThen(checkCon, cb),
); );
}, },

View File

@ -94,7 +94,7 @@
}; };
const updatePacProxyIps = function updatePacProxyIps( const updatePacProxyIps = function updatePacProxyIps(
cb = throwIfError cb = throwIfError,
) { ) {
cb = asyncLogGroup( cb = asyncLogGroup(
@ -106,7 +106,7 @@
}; };
const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync( const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync(
provider, lastModifiedStr = mandatory(), cb = throwIfError, provider, cacheImprints = mandatory(), cb = throwIfError,
) { ) {
const pacUrl = provider.pacUrls[0]; const pacUrl = provider.pacUrls[0];
@ -135,18 +135,19 @@
} }
httpLib.ifModifiedSince(pacUrl, lastModifiedStr, (err, newLastModifiedStr) => { httpLib.ifModifiedSince(pacUrl, cacheImprints, (err, newCacheImprints) => {
if (!newLastModifiedStr) { // Always empty or non-empty object:
const ifWasEverModified = lastModifiedStr !== new Date(0).toUTCString(); const cacheImprintsForCb = newCacheImprints || cacheImprints;
if (ifWasEverModified) { if (!err && !newCacheImprints) {
const { eTag, lastModifiedStr } = cacheImprints;
if (eTag || lastModifiedStr) {
addWarning( addWarning(
'Ваш PAC-скрипт не нуждается в обновлении. Его дата: ' + 'Ваш PAC-скрипт не нуждается в обновлении. Его ' +
lastModifiedStr, (lastModifiedStr ? 'дата (на сервере): ' + lastModifiedStr : 'ETag: ' + eTag),
); );
const res = {lastModified: lastModifiedStr}; return cb(null, cacheImprintsForCb);
return cb(null, res);
} }
} }
@ -169,9 +170,9 @@
setPacAsync( setPacAsync(
pacData, pacData,
(err, res) => cb( (err) => cb(
err, err,
Object.assign(res || {}, {lastModified: newLastModifiedStr}), cacheImprintsForCb,
), ),
); );
@ -263,20 +264,22 @@
}, },
_currentPacProviderLastModified: 0, _currentPacProviderLastModified: 0, // Milliseconds or as a string.
_currentPacProviderETag: '',
getLastModifiedForKey(key = mandatory()) { getCacheImprintsForKey(key = mandatory()) {
if (this._currentPacProviderKey === key) { if (this._currentPacProviderKey === key) {
return new Date(this._currentPacProviderLastModified).toUTCString(); return {
lastModifiedStr: new Date(this._currentPacProviderLastModified).toUTCString(),
eTag: this._currentPacProviderETag,
};
} }
return new Date(0).toUTCString(); // If it's not for the current provider then lie we have very old imprints. TODO: reconsider.
return {
}, lastModifiedStr: new Date(0).toUTCString(),
eTag: '',
setLastModified(newValue = mandatory()) { };
this._currentPacProviderLastModified = newValue;
}, },
@ -296,12 +299,16 @@
setCurrentPacProviderKey( setCurrentPacProviderKey(
newKey = mandatory(), newKey = mandatory(),
lastModified = new Date().toUTCString(), {
lastModifiedStr = new Date().toUTCString(),
eTag = '',
} = {},
) { ) {
this.mustBeKey(newKey); this.mustBeKey(newKey);
this._currentPacProviderKey = newKey; this._currentPacProviderKey = newKey;
this._currentPacProviderLastModified = lastModified; this._currentPacProviderLastModified = lastModifiedStr;
this._currentPacProviderETag = eTag;
}, },
@ -361,11 +368,11 @@
const pacSetPromise = new Promise( const pacSetPromise = new Promise(
(resolve, reject) => setPacScriptFromProviderAsync( (resolve, reject) => setPacScriptFromProviderAsync(
pacProvider, pacProvider,
this.getLastModifiedForKey(key), this.getCacheImprintsForKey(key),
(err, res, ...warns) => { (err, cacheImprints, ...warns) => {
if (!err) { if (!err) {
this.setCurrentPacProviderKey(key, res.lastModified); this.setCurrentPacProviderKey(key, cacheImprints);
this.lastPacUpdateStamp = Date.now(); this.lastPacUpdateStamp = Date.now();
this.ifFirstInstall = false; this.ifFirstInstall = false;
this.setAlarms(); this.setAlarms();
@ -535,6 +542,9 @@
antiCensorRu._currentPacProviderLastModified = antiCensorRu._currentPacProviderLastModified =
oldStorage._currentPacProviderLastModified oldStorage._currentPacProviderLastModified
|| antiCensorRu._currentPacProviderLastModified; || antiCensorRu._currentPacProviderLastModified;
antiCensorRu._currentPacProviderETag =
oldStorage._currentPacProviderETag
|| antiCensorRu._currentPacProviderETag;
console.log( console.log(
'Last PAC update was on', 'Last PAC update was on',
new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU'), new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU'),
@ -598,8 +608,7 @@
console.log('Proxies found:', pacMods.filteredCustomsString); console.log('Proxies found:', pacMods.filteredCustomsString);
return; // Own proxies or Tor are used. return; // Own proxies or Tor are used.
} }
antiCensorRu.setCurrentPacProviderKey('Антизапрет'); antiCensorRu.setCurrentPacProviderKey('Антизапрет', { lastModifiedStr: new Date(0).toUTCString() });
antiCensorRu.setLastModified(0);
await new Promise((resolveSwitch) => await new Promise((resolveSwitch) =>
antiCensorRu.syncWithPacProviderAsync((err, res, warns) => { antiCensorRu.syncWithPacProviderAsync((err, res, warns) => {