2016-07-28 13:40:19 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-context-cls
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-09-09 08:32:57 +00:00
|
|
|
var cls = require('cls-hooked');
|
2016-07-28 13:40:19 +00:00
|
|
|
var domain = require('domain');
|
|
|
|
|
2016-07-29 07:29:10 +00:00
|
|
|
var LoopBackContext = module.exports;
|
2016-07-28 13:40:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current context object. The context is preserved
|
|
|
|
* across async calls, it behaves like a thread-local storage.
|
|
|
|
*
|
|
|
|
* @returns {Namespace} The context object or null.
|
|
|
|
*/
|
2016-07-29 07:29:10 +00:00
|
|
|
LoopBackContext.getCurrentContext = function() {
|
|
|
|
// A placeholder method, see LoopBackContext.createContext() for the real version
|
2016-07-28 13:40:19 +00:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the given function in such way that
|
2016-07-29 07:29:10 +00:00
|
|
|
* `LoopBackContext.getCurrentContext` returns the
|
2016-07-28 13:40:19 +00:00
|
|
|
* provided context object.
|
|
|
|
*
|
|
|
|
* **NOTE**
|
|
|
|
*
|
|
|
|
* The method is supported on the server only, it does not work
|
|
|
|
* in the browser at the moment.
|
|
|
|
*
|
|
|
|
* @param {Function} fn The function to run, it will receive arguments
|
|
|
|
* (currentContext, currentDomain).
|
|
|
|
* @param {Namespace} context An optional context object.
|
|
|
|
* When no value is provided, then the default global context is used.
|
|
|
|
*/
|
2016-07-29 07:29:10 +00:00
|
|
|
LoopBackContext.runInContext = function(fn, context) {
|
2016-07-28 13:40:19 +00:00
|
|
|
var currentDomain = domain.create();
|
|
|
|
currentDomain.oldBind = currentDomain.bind;
|
|
|
|
currentDomain.bind = function(callback, context) {
|
|
|
|
return currentDomain.oldBind(ns.bind(callback, context), context);
|
|
|
|
};
|
|
|
|
|
2016-07-29 07:29:10 +00:00
|
|
|
var ns = context || LoopBackContext.createContext('loopback');
|
2016-07-28 13:40:19 +00:00
|
|
|
|
|
|
|
currentDomain.run(function() {
|
|
|
|
ns.run(function executeInContext(context) {
|
|
|
|
fn(ns, currentDomain);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new LoopBackContext instance that can be used
|
2016-07-29 07:29:10 +00:00
|
|
|
* for `LoopBackContext.runInContext`.
|
2016-07-28 13:40:19 +00:00
|
|
|
*
|
|
|
|
* **NOTES**
|
|
|
|
*
|
2016-07-29 07:29:10 +00:00
|
|
|
* At the moment, `LoopBackContext.getCurrentContext` supports
|
2016-07-28 13:40:19 +00:00
|
|
|
* a single global context instance only. If you call `createContext()`
|
|
|
|
* multiple times, `getCurrentContext` will return the last context
|
|
|
|
* created.
|
|
|
|
*
|
|
|
|
* The method is supported on the server only, it does not work
|
|
|
|
* in the browser at the moment.
|
|
|
|
*
|
|
|
|
* @param {String} scopeName An optional scope name.
|
|
|
|
* @return {Namespace} The new context object.
|
|
|
|
*/
|
2016-07-29 07:29:10 +00:00
|
|
|
LoopBackContext.createContext = function(scopeName) {
|
2016-07-28 13:40:19 +00:00
|
|
|
// Make the namespace globally visible via the process.context property
|
|
|
|
process.context = process.context || {};
|
|
|
|
var ns = process.context[scopeName];
|
|
|
|
if (!ns) {
|
2016-09-09 08:32:57 +00:00
|
|
|
ns = cls.createNamespace(scopeName);
|
2016-07-28 13:40:19 +00:00
|
|
|
process.context[scopeName] = ns;
|
2016-07-29 07:29:10 +00:00
|
|
|
// Set up LoopBackContext.getCurrentContext()
|
|
|
|
LoopBackContext.getCurrentContext = function() {
|
2016-07-28 13:40:19 +00:00
|
|
|
return ns && ns.active ? ns : null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return ns;
|
|
|
|
};
|
2016-08-01 13:02:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create middleware that sets up a new context for each incoming HTTP request.
|
|
|
|
*
|
|
|
|
* See perRequestContextFactory for more details.
|
|
|
|
*/
|
|
|
|
LoopBackContext.perRequest = require('./middleware/per-request');
|