diff --git a/app.js b/app.js index b86e911aa..14e5b82c0 100755 --- a/app.js +++ b/app.js @@ -39,6 +39,8 @@ var sessionSecret = process.env.SESSION_SECRET || settings.secret; var db = mongoose.connection; var dbOptions = { server: { socketOptions: { keepAlive: 1 } } }; +var scriptStorage = require('./controllers/scriptStorage'); + app.set('port', process.env.PORT || 8080); // Connect to the database @@ -81,6 +83,37 @@ app.use(bodyParser.json({ app.use(compression()); app.use(methodOverride('X-HTTP-Method-Override')); +// Intercept script/library/metadata requests to prevent +// the creation of useless session data +app.use(function (aReq, aRes, aNext) { + var matches = null; + + if (aReq.method === 'GET' && + (matches = + /^\/(install|meta|src)(?:\/(scripts|libs))?\/([^\/]+)\/([^\/]+)/ + .exec(aReq.url))) { + + // Set route parameters to mimick express route middleware + aReq.params = {}; + if (matches[1] === 'src' && matches[2]) { + aReq.params.type = matches[2]; + } + aReq.params.username = matches[3]; + aReq.params.scriptname = matches[4]; + + switch (matches[1]) { + case 'meta': + scriptStorage.sendMeta(aReq, aRes, aNext); + break; + default: + scriptStorage.sendScript(aReq, aRes, aNext); + break; + } + } else { + aNext(); + } +}); + // Order is very important here (i.e mess with at your own risk) app.use(cookieParser()); app.use(session({