From 54967e20b5f4a7ddaf472a271d8b043aadf86557 Mon Sep 17 00:00:00 2001 From: Martii Date: Mon, 3 Apr 2017 21:39:13 -0600 Subject: [PATCH 1/2] Enable meta cache from Server to Client * * Caching works well but GM kills it with [`channel.LOAD_BYPASS_CACHE`](https://github.com/greasemonkey/greasemonkey/blob/696848acc40c6395904a53acdc15a653d5be502c/modules/remoteScript.js#L606) **NOTE** Keep this at one day, possibly for long term. Applies to #432 and #249. Possible addition to #944 down the line. Regression **not** present, nor needed, from detection and revert in #894 . Script meta should always be the same whether minified fork or original --- controllers/scriptStorage.js | 39 ++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/controllers/scriptStorage.js b/controllers/scriptStorage.js index b6a9dd56f..590191c70 100644 --- a/controllers/scriptStorage.js +++ b/controllers/scriptStorage.js @@ -608,8 +608,6 @@ exports.sendMeta = function (aReq, aRes, aNext) { } function render() { - aRes.set('Content-Type', 'application/json; charset=UTF-8'); - aRes.end(JSON.stringify(meta, null, isPro ? '' : ' ')); } @@ -629,6 +627,10 @@ exports.sendMeta = function (aReq, aRes, aNext) { var installNameBase = getInstallNameBase(aReq, { hasExtension: true }); var meta = null; + var eTag = null; + var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds + var lastModified = null; + Script.findOne({ installName: caseSensitive(installNameBase + '.user.js') }, function (aErr, aScript) { var script = null; @@ -641,10 +643,36 @@ exports.sendMeta = function (aReq, aRes, aNext) { return; } + lastModified = aScript.updated; + + // Create a base 36 representation of the hex sha512sum + eTag = '"' + parseInt('0x' + aScript.hash, 16).toString(36) + '"'; + script = modelParser.parseScript(aScript); meta = script.meta; // NOTE: Watchpoint + // NOTE: HTTP/1.1 Caching + aRes.set('Cache-Control', 'public, max-age=' + maxAge + + ', no-cache, no-transform, must-revalidate'); + + // NOTE: HTTP/1.0 Caching + aRes.set('Last-Modified', lastModified); + + // If already client-side... NOTE: HTTP/1.0 and/or HTTP/1.1 Caching + if (aReq.get('if-modified-since') === lastModified || aReq.get('if-none-match') === eTag) { + aRes.status(304).send({ status: 304, message: 'Not Modified' }); + return; + } + if (/\.json$/.test(aReq.params.scriptname)) { + aRes.set('Content-Type', 'application/json; charset=UTF-8'); + + // NOTE: HTTP/1.0 Caching + aRes.set('Expires', moment(moment() + maxAge * 1000).utc() + .format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT'); + + // NOTE: HTTP/1.1 Caching + aRes.set('Etag', eTag); // Check for existance of OUJS metadata block if (!meta.OpenUserJS) { @@ -667,6 +695,13 @@ exports.sendMeta = function (aReq, aRes, aNext) { } else { aRes.set('Content-Type', 'text/javascript; charset=UTF-8'); + // NOTE: HTTP/1.0 Caching + aRes.set('Expires', moment(moment() + maxAge * 1000).utc() + .format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT'); + + // NOTE: HTTP/1.1 Caching + aRes.set('Etag', eTag); + aRes.write('// ==UserScript==\n'); if (meta.UserScript.version) { From 1f3e5ecb06b5303c1548d8f3d873fc5e22feb553 Mon Sep 17 00:00:00 2001 From: Martii Date: Mon, 3 Apr 2017 21:43:27 -0600 Subject: [PATCH 2/2] Scoot identifier declaration down a little Applies to #432 and #249. Possible addition to #944 down the line. --- controllers/scriptStorage.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/scriptStorage.js b/controllers/scriptStorage.js index 590191c70..5240779e8 100644 --- a/controllers/scriptStorage.js +++ b/controllers/scriptStorage.js @@ -627,10 +627,6 @@ exports.sendMeta = function (aReq, aRes, aNext) { var installNameBase = getInstallNameBase(aReq, { hasExtension: true }); var meta = null; - var eTag = null; - var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds - var lastModified = null; - Script.findOne({ installName: caseSensitive(installNameBase + '.user.js') }, function (aErr, aScript) { var script = null; @@ -638,6 +634,10 @@ exports.sendMeta = function (aReq, aRes, aNext) { var whitespace = '\u0020\u0020\u0020\u0020'; var tasks = []; + var eTag = null; + var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds + var lastModified = null; + if (!aScript) { aNext(); return;