loopback-context/server/current-context.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

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';
var cls = require('cls-hooked');
2016-07-28 13:40:19 +00:00
var domain = require('domain');
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.
*/
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
* `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.
*/
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);
};
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
* for `LoopBackContext.runInContext`.
2016-07-28 13:40:19 +00:00
*
* **NOTES**
*
* 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.
*/
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) {
ns = cls.createNamespace(scopeName);
2016-07-28 13:40:19 +00:00
process.context[scopeName] = ns;
// Set up LoopBackContext.getCurrentContext()
LoopBackContext.getCurrentContext = function() {
2016-07-28 13:40:19 +00:00
return ns && ns.active ? ns : null;
};
}
return ns;
};
/**
* Create middleware that sets up a new context for each incoming HTTP request.
*
* See perRequestContextFactory for more details.
*/
LoopBackContext.perRequest = require('./middleware/per-request');