Rename to loopback-context/LoopBackContext

This commit is contained in:
Miroslav Bajtoš 2016-07-29 09:29:10 +02:00
parent 9bd4afbe7e
commit 867a7c3616
5 changed files with 28 additions and 26 deletions

View File

@ -1,4 +1,4 @@
# loopback-context-cls # loopback-context
Current context for LoopBack applications, based on Current context for LoopBack applications, based on
node-continuation-local-storage. node-continuation-local-storage.
@ -11,7 +11,7 @@ node-continuation-local-storage.
```json ```json
{ {
"initial": { "initial": {
"loopback-context-cls#per-request-context": { "loopback-context#per-request-context": {
} }
} }
} }
@ -20,12 +20,12 @@ node-continuation-local-storage.
2) Then you can access the context from your code: 2) Then you can access the context from your code:
```js ```js
var ClsContext = require('loopback-context-cls'); var LoopBackContext = require('loopback-context');
// ... // ...
MyModel.myMethod = function(cb) { MyModel.myMethod = function(cb) {
var ctx = ClsContext.getCurrentContext(); var ctx = LoopBackContext.getCurrentContext();
ctx.get('key'); ctx.get('key');
ctx.set('key', { foo: 'bar' }); ctx.set('key', { foo: 'bar' });
}); });

View File

@ -5,13 +5,13 @@
'use strict'; 'use strict';
var ClsContext = module.exports; var LoopBackContext = module.exports;
ClsContext.getCurrentContext = function() { LoopBackContext.getCurrentContext = function() {
return null; return null;
}; };
ClsContext.runInContext = LoopBackContext.runInContext =
ClsContext.createContext = function() { LoopBackContext.createContext = function() {
throw new Error('Current context is not supported in the browser.'); throw new Error('Current context is not supported in the browser.');
}; };

View File

@ -8,7 +8,7 @@
var cls = require('continuation-local-storage'); var cls = require('continuation-local-storage');
var domain = require('domain'); var domain = require('domain');
var ClsContext = module.exports; var LoopBackContext = module.exports;
/** /**
* Get the current context object. The context is preserved * Get the current context object. The context is preserved
@ -16,14 +16,14 @@ var ClsContext = module.exports;
* *
* @returns {Namespace} The context object or null. * @returns {Namespace} The context object or null.
*/ */
ClsContext.getCurrentContext = function() { LoopBackContext.getCurrentContext = function() {
// A placeholder method, see CurrentContext.createContext() for the real version // A placeholder method, see LoopBackContext.createContext() for the real version
return null; return null;
}; };
/** /**
* Run the given function in such way that * Run the given function in such way that
* `CurrentContext.getCurrentContext` returns the * `LoopBackContext.getCurrentContext` returns the
* provided context object. * provided context object.
* *
* **NOTE** * **NOTE**
@ -36,14 +36,14 @@ ClsContext.getCurrentContext = function() {
* @param {Namespace} context An optional context object. * @param {Namespace} context An optional context object.
* When no value is provided, then the default global context is used. * When no value is provided, then the default global context is used.
*/ */
ClsContext.runInContext = function(fn, context) { LoopBackContext.runInContext = function(fn, context) {
var currentDomain = domain.create(); var currentDomain = domain.create();
currentDomain.oldBind = currentDomain.bind; currentDomain.oldBind = currentDomain.bind;
currentDomain.bind = function(callback, context) { currentDomain.bind = function(callback, context) {
return currentDomain.oldBind(ns.bind(callback, context), context); return currentDomain.oldBind(ns.bind(callback, context), context);
}; };
var ns = context || ClsContext.createContext('loopback'); var ns = context || LoopBackContext.createContext('loopback');
currentDomain.run(function() { currentDomain.run(function() {
ns.run(function executeInContext(context) { ns.run(function executeInContext(context) {
@ -54,11 +54,11 @@ ClsContext.runInContext = function(fn, context) {
/** /**
* Create a new LoopBackContext instance that can be used * Create a new LoopBackContext instance that can be used
* for `CurrentContext.runInContext`. * for `LoopBackContext.runInContext`.
* *
* **NOTES** * **NOTES**
* *
* At the moment, `CurrentContext.getCurrentContext` supports * At the moment, `LoopBackContext.getCurrentContext` supports
* a single global context instance only. If you call `createContext()` * a single global context instance only. If you call `createContext()`
* multiple times, `getCurrentContext` will return the last context * multiple times, `getCurrentContext` will return the last context
* created. * created.
@ -69,15 +69,15 @@ ClsContext.runInContext = function(fn, context) {
* @param {String} scopeName An optional scope name. * @param {String} scopeName An optional scope name.
* @return {Namespace} The new context object. * @return {Namespace} The new context object.
*/ */
ClsContext.createContext = function(scopeName) { LoopBackContext.createContext = function(scopeName) {
// Make the namespace globally visible via the process.context property // Make the namespace globally visible via the process.context property
process.context = process.context || {}; process.context = process.context || {};
var ns = process.context[scopeName]; var ns = process.context[scopeName];
if (!ns) { if (!ns) {
ns = cls.createNamespace(scopeName); ns = cls.createNamespace(scopeName);
process.context[scopeName] = ns; process.context[scopeName] = ns;
// Set up CurrentContext.getCurrentContext() // Set up LoopBackContext.getCurrentContext()
ClsContext.getCurrentContext = function() { LoopBackContext.getCurrentContext = function() {
return ns && ns.active ? ns : null; return ns && ns.active ? ns : null;
}; };
} }

View File

@ -5,7 +5,7 @@
'use strict'; 'use strict';
var ClsContext = require('../current-context'); var LoopBackContext = require('../current-context');
module.exports = context; module.exports = context;
@ -14,22 +14,23 @@ var name = 'loopback';
/** /**
* Context middleware. * Context middleware.
* ```js * ```js
* var perRequestContext = require(
* 'loopback-context/server/middleware/per-request-context.js');
* var app = loopback(); * var app = loopback();
* app.use(loopback.context(options); * app.use(perRequestContext(options);
* app.use(loopback.rest()); * app.use(loopback.rest());
* app.listen(); * app.listen();
* ``` * ```
* @options {Object} [options] Options for context * @options {Object} [options] Options for context
* @property {String} name Context scope name. * @property {String} name Context scope name.
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false. * @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
* @header loopback.context([options])
*/ */
function context(options) { function context(options) {
options = options || {}; options = options || {};
var scope = options.name || name; var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false; var enableHttpContext = options.enableHttpContext || false;
var ns = ClsContext.createContext(scope); var ns = LoopBackContext.createContext(scope);
// Return the middleware // Return the middleware
return function contextHandler(req, res, next) { return function contextHandler(req, res, next) {
@ -37,7 +38,7 @@ function context(options) {
return next(); return next();
} }
ClsContext.runInContext(function processRequestInContext(ns, domain) { LoopBackContext.runInContext(function processRequestInContext(ns, domain) {
req.loopbackContext = ns; req.loopbackContext = ns;
// Bind req/res event emitters to the given namespace // Bind req/res event emitters to the given namespace

View File

@ -12,7 +12,7 @@ var expect = require('./helpers/expect');
var loopback = require('loopback'); var loopback = require('loopback');
var request = require('supertest'); var request = require('supertest');
describe('CLS Context', function() { describe('LoopBack Context', function() {
var runInOtherDomain, runnerInterval; var runInOtherDomain, runnerInterval;
before(function setupRunInOtherDomain() { before(function setupRunInOtherDomain() {
@ -38,6 +38,7 @@ describe('CLS Context', function() {
it('preserves callback domain', function(done) { it('preserves callback domain', function(done) {
var app = loopback({localRegistry: true, loadBuiltinModels: true}); var app = loopback({localRegistry: true, loadBuiltinModels: true});
app.set('remoting', {context: false}); app.set('remoting', {context: false});
app.set('legacyExplorer', false);
app.use(require('../server/middleware/per-request-context')()); app.use(require('../server/middleware/per-request-context')());
app.use(loopback.rest()); app.use(loopback.rest());
app.dataSource('db', {connector: 'memory'}); app.dataSource('db', {connector: 'memory'});