Skip to content

Commit

Permalink
Merge pull request #605 from OpenUserJs/issue-604
Browse files Browse the repository at this point in the history
Temporary patch for #604.
  • Loading branch information
sizzlemctwizzle committed Mar 20, 2015
2 parents 46956e9 + 55ce07d commit 4d1dcc1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 4d1dcc1

Please sign in to comment.