Skip to content

Commit

Permalink
Merge pull request #894 from Martii/Issue-432inlineCaching
Browse files Browse the repository at this point in the history
Enable script cache from Server to Client

Auto-merge
  • Loading branch information
Martii committed Feb 9, 2016
2 parents b1a77ca + 0e51e20 commit 80f58ff
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var fs = require('fs');
var PEG = require('pegjs');
var AWS = require('aws-sdk');
var UglifyJS = require("uglify-js-harmony");
var crypto = require('crypto');
var moment = require('moment');

//--- Model inclusions
var Script = require('../models/script').Script;
Expand Down Expand Up @@ -217,26 +219,26 @@ exports.sendScript = function (aReq, aRes, aNext) {
return;
}

// Send the script
aRes.set('Content-Type', 'text/javascript; charset=UTF-8');
// Retrieve the script
aStream.setEncoding('utf8');

// Only minify for response that doesn't contain `.min.` extension
if (!/\.min(\.user)?\.js$/.test(aReq._parsedUrl.pathname)) {
aStream.pipe(aRes);
} else {
// Otherwise set some defaults per script request via *UglifyJS2*
// and try minifying output
aStream.on('data', function (aData) {
chunks.push(aData);
});

aStream.on('data', function (aData) {
chunks.push(aData);
});
aStream.on('end', function () {
var source = chunks.join(''); // NOTE: Watchpoint

aStream.on('end', function () {
var source = chunks.join(''); // NOTE: Watchpoint
var lastModified = null;
var eTag = null;

console.log('MINIFICATION REQUEST:', 'installName: ' + aScript.installName);
var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds

// Only minify for response that doesn't contain `.min.` extension...
// otherwise set some defaults per script request via *UglifyJS2*
// and try minifying output

if (/\.min(\.user)?\.js$/.test(aReq._parsedUrl.pathname)) {
try {
source = UglifyJS.minify(source, {
fromString: true,
Expand All @@ -257,26 +259,46 @@ exports.sendScript = function (aReq, aRes, aNext) {
' line: ' + aE.line + ' col: ' + aE.col + ' pos: ' + aE.pos

].join('\n'));

}
}

aRes.write(source);
aRes.end();
});
}
// Set up server to client caching
lastModified = moment(aScript.updated).utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT';

// Don't count installs on raw source route
if (aScript.isLib || aReq.params.type) {
return;
}
eTag = '"' + crypto.createHash('sha256').update(source).digest('hex') + '"';

// Update the install count
++aScript.installs;
++aScript.installsSinceUpdate;
aRes.set('Etag', eTag);
aRes.set('Last-Modified', lastModified);
aRes.set('Cache-Control', 'public, max-age=' + maxAge);

// Resave affected properties
aScript.save(function (aErr, aScript) {
// WARNING: No error handling at this stage
// If already client-side...
if (aReq.get('if-modified-since') === lastModified || aReq.get('if-none-match') === eTag) {
aRes.status(304).send({ status: 304, message: 'Not Modified' });
return;
}

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

aRes.set('Content-Type', 'text/javascript; charset=UTF-8');

aRes.write(source);
aRes.end();

// Don't count installs on raw source route
if (aScript.isLib || aReq.params.type) {
return;
}

// Update the install count
++aScript.installs;
++aScript.installsSinceUpdate;

// Resave affected properties
aScript.save(function (aErr, aScript) {
// WARNING: No error handling at this stage
});
});
});
};
Expand Down

0 comments on commit 80f58ff

Please sign in to comment.