2014-11-12 11:36:16 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
2014-06-17 17:35:19 +00:00
|
|
|
var juggler = require('loopback-datasource-juggler');
|
|
|
|
var remoting = require('strong-remoting');
|
|
|
|
var cls = require('continuation-local-storage');
|
2014-12-01 10:36:34 +00:00
|
|
|
var domain = require('domain');
|
2014-06-17 17:35:19 +00:00
|
|
|
|
|
|
|
module.exports = context;
|
|
|
|
|
|
|
|
var name = 'loopback';
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
function createContext(scope) {
|
2014-06-17 17:35:19 +00:00
|
|
|
// Make the namespace globally visible via the process.context property
|
|
|
|
process.context = process.context || {};
|
2014-10-22 21:29:56 +00:00
|
|
|
var ns = process.context[scope];
|
|
|
|
if (!ns) {
|
|
|
|
ns = cls.createNamespace(scope);
|
|
|
|
process.context[scope] = ns;
|
|
|
|
// Set up loopback.getCurrentContext()
|
|
|
|
loopback.getCurrentContext = function() {
|
2014-11-10 18:12:00 +00:00
|
|
|
return ns && ns.active ? ns : null;
|
2014-10-22 21:29:56 +00:00
|
|
|
};
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
chain(juggler);
|
|
|
|
chain(remoting);
|
|
|
|
}
|
|
|
|
return ns;
|
|
|
|
}
|
2014-06-17 17:35:19 +00:00
|
|
|
|
2014-11-06 22:07:34 +00:00
|
|
|
/**
|
|
|
|
* Context middleware.
|
|
|
|
* ```js
|
|
|
|
* var app = loopback();
|
|
|
|
* app.use(loopback.context(options);
|
|
|
|
* app.use(loopback.rest());
|
|
|
|
* app.listen();
|
|
|
|
* ```
|
|
|
|
* @options {Object} [options] Options for context
|
|
|
|
* @property {String} name Context scope name.
|
|
|
|
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
|
|
|
|
* @header loopback.context([options])
|
|
|
|
*/
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
function context(options) {
|
|
|
|
options = options || {};
|
|
|
|
var scope = options.name || name;
|
|
|
|
var enableHttpContext = options.enableHttpContext || false;
|
|
|
|
var ns = createContext(scope);
|
2014-12-01 10:36:34 +00:00
|
|
|
|
|
|
|
var currentDomain = process.domain = domain.create();
|
|
|
|
currentDomain.oldBind = currentDomain.bind;
|
|
|
|
currentDomain.bind = function(callback, context) {
|
|
|
|
return currentDomain.oldBind(ns.bind(callback, context), context);
|
|
|
|
};
|
|
|
|
|
2014-06-17 17:35:19 +00:00
|
|
|
// Return the middleware
|
2014-10-22 21:29:56 +00:00
|
|
|
return function contextHandler(req, res, next) {
|
|
|
|
if (req.loopbackContext) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
req.loopbackContext = ns;
|
2014-06-17 17:35:19 +00:00
|
|
|
// Bind req/res event emitters to the given namespace
|
|
|
|
ns.bindEmitter(req);
|
|
|
|
ns.bindEmitter(res);
|
2014-12-01 10:36:34 +00:00
|
|
|
|
|
|
|
currentDomain.add(req);
|
|
|
|
currentDomain.add(res);
|
|
|
|
|
2014-06-17 17:35:19 +00:00
|
|
|
// Create namespace for the request context
|
2014-12-01 10:36:34 +00:00
|
|
|
currentDomain.run(function() {
|
|
|
|
ns.run(function processRequestInContext(context) {
|
|
|
|
// Run the code in the context of the namespace
|
|
|
|
if (enableHttpContext) {
|
|
|
|
ns.set('http', {req: req, res: res}); // Set up the transport context
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2014-06-17 17:35:19 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a chained context
|
|
|
|
* @param {Object} child The child context
|
|
|
|
* @param {Object} parent The parent context
|
|
|
|
* @private
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function ChainedContext(child, parent) {
|
|
|
|
this.child = child;
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:07:34 +00:00
|
|
|
/*!
|
2014-06-17 17:35:19 +00:00
|
|
|
* Get the value by name from the context. If it doesn't exist in the child
|
|
|
|
* context, try the parent one
|
|
|
|
* @param {String} name Name of the context property
|
|
|
|
* @returns {*} Value of the context property
|
|
|
|
*/
|
2014-10-22 21:29:56 +00:00
|
|
|
ChainedContext.prototype.get = function(name) {
|
2014-06-17 17:35:19 +00:00
|
|
|
var val = this.child && this.child.get(name);
|
|
|
|
if (val === undefined) {
|
|
|
|
return this.parent && this.parent.get(name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
ChainedContext.prototype.set = function(name, val) {
|
2014-06-17 17:35:19 +00:00
|
|
|
if (this.child) {
|
|
|
|
return this.child.set(name, val);
|
|
|
|
} else {
|
|
|
|
return this.parent && this.parent.set(name, val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
ChainedContext.prototype.reset = function(name, val) {
|
2014-06-17 17:35:19 +00:00
|
|
|
if (this.child) {
|
|
|
|
return this.child.reset(name, val);
|
|
|
|
} else {
|
|
|
|
return this.parent && this.parent.reset(name, val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function chain(child) {
|
|
|
|
if (typeof child.getCurrentContext === 'function') {
|
|
|
|
var childContext = new ChainedContext(child.getCurrentContext(),
|
|
|
|
loopback.getCurrentContext());
|
|
|
|
child.getCurrentContext = function() {
|
|
|
|
return childContext;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
child.getCurrentContext = loopback.getCurrentContext;
|
|
|
|
}
|
|
|
|
}
|