Skip to content

Commit

Permalink
Enable meta cache from Server to Client
Browse files Browse the repository at this point in the history
* * 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 OpenUserJS#432 and OpenUserJS#249. Possible addition to OpenUserJS#944 down the line. Regression **not** present, nor needed, from detection and revert in OpenUserJS#894 . Script meta should always be the same whether minified fork or original
  • Loading branch information
Martii committed Apr 4, 2017
1 parent 955c15e commit 54967e2
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '' : ' '));
}

Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 54967e2

Please sign in to comment.