Skip to content

Commit

Permalink
Improve granularity of a portion of HTTP 1.1 caching
Browse files Browse the repository at this point in the history
Post OpenUserJS#1076 OpenUserJS#1826 and applies to OpenUserJS#432 OpenUserJS#249

NOTE:
* This increases the server load for more frequent accuracy
  • Loading branch information
Martii committed Jul 8, 2022
1 parent 1069e82 commit 79fa8ca
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,34 @@ exports.sendMeta = function (aReq, aRes, aNext) {
}

function render() {
aRes.end(JSON.stringify(meta, null, isPro ? '' : ' '));
var metaJSON = JSON.stringify(meta, null, isPro ? '' : ' ');
var hash = null;

// HTTP/1.1 Caching
aRes.set('Cache-Control', 'public, max-age=' + maxAge +
', no-cache, no-transform, must-revalidate');

// Use SRI of the recalculated sha512sum
hash = crypto.createHash('sha512').update(metaJSON).digest('hex');
eTag = '"' + 'sha512-' + Buffer.from(hash, 'hex').toString('base64') + ' .meta.json"';

// If already client-side... HTTP/1.1 Caching
if (aReq.get('if-none-match') === eTag) {
aRes.status(304).send(); // Not Modified
return;
}

// Okay to send .meta.json...
aRes.set('Content-Type', 'application/json; charset=UTF-8');

// HTTP/1.0 Caching
aRes.set('Expires', moment(moment() + maxAge * 1000).utc()
.format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT');

// HTTP/1.1 Caching
aRes.set('Etag', eTag);

aRes.end(metaJSON);
}

function asyncComplete(aErr) {
Expand All @@ -875,6 +902,9 @@ exports.sendMeta = function (aReq, aRes, aNext) {
var installNameBase = getInstallNameBase(aReq, { hasExtension: true });
var meta = null;

var eTag = null;
var maxAge = 7 * 60 * 60 * 24; // nth day(s) in seconds

Script.findOne({ installName: caseSensitive(installNameBase + '.user.js') },
function (aErr, aScript) {
// WARNING: No err handling
Expand All @@ -884,40 +914,16 @@ exports.sendMeta = function (aReq, aRes, aNext) {
var whitespace = '\u0020\u0020\u0020\u0020';
var tasks = [];

var eTag = null;
var maxAge = 7 * 60 * 60 * 24; // nth day(s) in seconds

if (!aScript) {
aNext();
return;
}

// HTTP/1.1 Caching
aRes.set('Cache-Control', 'public, max-age=' + maxAge +
', no-cache, no-transform, must-revalidate');

script = modelParser.parseScript(aScript);
meta = script.meta; // NOTE: Watchpoint

if (/\.json$/.test(aReq.params.scriptname)) {
// Use SRI of the stored sha512sum
eTag = '"' + script.hashSRI + ' .meta.json"';

// If already client-side... HTTP/1.1 Caching
if (aReq.get('if-none-match') === eTag) {
aRes.status(304).send(); // Not Modified
return;
}

// Okay to send .meta.json...
aRes.set('Content-Type', 'application/json; charset=UTF-8');

// HTTP/1.0 Caching
aRes.set('Expires', moment(moment() + maxAge * 1000).utc()
.format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT');

// HTTP/1.1 Caching
aRes.set('Etag', eTag);
// NOTE: Caching is managed on rendering for more live stats

// Check for existance of OUJS metadata block
if (!meta.OpenUserJS) {
Expand Down

0 comments on commit 79fa8ca

Please sign in to comment.