Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable script cache from Server to Client #894

Merged
merged 1 commit into from
Feb 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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