add continuation-local-storage support

v1.18.x
akhoury 9 years ago
parent 73633523b2
commit 61c76e4aba

@ -25,6 +25,7 @@
"connect-mongo": "~1.1.0", "connect-mongo": "~1.1.0",
"connect-multiparty": "^2.0.0", "connect-multiparty": "^2.0.0",
"connect-redis": "~3.0.2", "connect-redis": "~3.0.2",
"continuation-local-storage": "^3.1.6",
"cookie-parser": "^1.3.3", "cookie-parser": "^1.3.3",
"cron": "^1.0.5", "cron": "^1.0.5",
"csurf": "^1.6.1", "csurf": "^1.6.1",

@ -0,0 +1,41 @@
var continuationLocalStorage = require('continuation-local-storage');
var NAMESPACE = 'nodebb';
var namespace = continuationLocalStorage.createNamespace(NAMESPACE);
var cls = function (req, res, next) {
namespace.run(function() {
var value = {req: req};
if (process.env.NODE_ENV == 'development') {
value.audit = {created: process.hrtime()};
}
namespace.set('route', {
req: req,
res: res
});
next();
});
};
cls.storage = function () {
return cls.getNamespace(NAMESPACE);
};
cls.get = function (key) {
return namespace.get(key);
};
cls.set = function (key, value) {
return namespace.set(key, value);
};
cls.setItem = cls.set;
cls.getItem = cls.set;
cls.getNamespace = cls.storage;
cls.namespace = namespace;
cls.continuationLocalStorage = continuationLocalStorage;
module.exports = cls;

@ -14,6 +14,7 @@ var meta = require('../meta'),
compression = require('compression'), compression = require('compression'),
favicon = require('serve-favicon'), favicon = require('serve-favicon'),
session = require('express-session'), session = require('express-session'),
cls = require('./cls'),
useragent = require('express-useragent'); useragent = require('express-useragent');
@ -73,6 +74,7 @@ module.exports = function(app) {
app.use(middleware.addHeaders); app.use(middleware.addHeaders);
app.use(middleware.processRender); app.use(middleware.processRender);
app.use(cls);
auth.initialize(app, middleware); auth.initialize(app, middleware);
return middleware; return middleware;

Loading…
Cancel
Save