Rename to loopback-context/LoopBackContext
This commit is contained in:
parent
9bd4afbe7e
commit
867a7c3616
|
@ -1,4 +1,4 @@
|
|||
# loopback-context-cls
|
||||
# loopback-context
|
||||
|
||||
Current context for LoopBack applications, based on
|
||||
node-continuation-local-storage.
|
||||
|
@ -11,7 +11,7 @@ node-continuation-local-storage.
|
|||
```json
|
||||
{
|
||||
"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:
|
||||
|
||||
```js
|
||||
var ClsContext = require('loopback-context-cls');
|
||||
var LoopBackContext = require('loopback-context');
|
||||
|
||||
// ...
|
||||
|
||||
MyModel.myMethod = function(cb) {
|
||||
var ctx = ClsContext.getCurrentContext();
|
||||
var ctx = LoopBackContext.getCurrentContext();
|
||||
ctx.get('key');
|
||||
ctx.set('key', { foo: 'bar' });
|
||||
});
|
||||
|
|
|
@ -5,13 +5,13 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var ClsContext = module.exports;
|
||||
var LoopBackContext = module.exports;
|
||||
|
||||
ClsContext.getCurrentContext = function() {
|
||||
LoopBackContext.getCurrentContext = function() {
|
||||
return null;
|
||||
};
|
||||
|
||||
ClsContext.runInContext =
|
||||
ClsContext.createContext = function() {
|
||||
LoopBackContext.runInContext =
|
||||
LoopBackContext.createContext = function() {
|
||||
throw new Error('Current context is not supported in the browser.');
|
||||
};
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
var cls = require('continuation-local-storage');
|
||||
var domain = require('domain');
|
||||
|
||||
var ClsContext = module.exports;
|
||||
var LoopBackContext = module.exports;
|
||||
|
||||
/**
|
||||
* Get the current context object. The context is preserved
|
||||
|
@ -16,14 +16,14 @@ var ClsContext = module.exports;
|
|||
*
|
||||
* @returns {Namespace} The context object or null.
|
||||
*/
|
||||
ClsContext.getCurrentContext = function() {
|
||||
// A placeholder method, see CurrentContext.createContext() for the real version
|
||||
LoopBackContext.getCurrentContext = function() {
|
||||
// A placeholder method, see LoopBackContext.createContext() for the real version
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Run the given function in such way that
|
||||
* `CurrentContext.getCurrentContext` returns the
|
||||
* `LoopBackContext.getCurrentContext` returns the
|
||||
* provided context object.
|
||||
*
|
||||
* **NOTE**
|
||||
|
@ -36,14 +36,14 @@ ClsContext.getCurrentContext = function() {
|
|||
* @param {Namespace} context An optional context object.
|
||||
* 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();
|
||||
currentDomain.oldBind = currentDomain.bind;
|
||||
currentDomain.bind = function(callback, context) {
|
||||
return currentDomain.oldBind(ns.bind(callback, context), context);
|
||||
};
|
||||
|
||||
var ns = context || ClsContext.createContext('loopback');
|
||||
var ns = context || LoopBackContext.createContext('loopback');
|
||||
|
||||
currentDomain.run(function() {
|
||||
ns.run(function executeInContext(context) {
|
||||
|
@ -54,11 +54,11 @@ ClsContext.runInContext = function(fn, context) {
|
|||
|
||||
/**
|
||||
* Create a new LoopBackContext instance that can be used
|
||||
* for `CurrentContext.runInContext`.
|
||||
* for `LoopBackContext.runInContext`.
|
||||
*
|
||||
* **NOTES**
|
||||
*
|
||||
* At the moment, `CurrentContext.getCurrentContext` supports
|
||||
* At the moment, `LoopBackContext.getCurrentContext` supports
|
||||
* a single global context instance only. If you call `createContext()`
|
||||
* multiple times, `getCurrentContext` will return the last context
|
||||
* created.
|
||||
|
@ -69,15 +69,15 @@ ClsContext.runInContext = function(fn, context) {
|
|||
* @param {String} scopeName An optional scope name.
|
||||
* @return {Namespace} The new context object.
|
||||
*/
|
||||
ClsContext.createContext = function(scopeName) {
|
||||
LoopBackContext.createContext = function(scopeName) {
|
||||
// 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);
|
||||
process.context[scopeName] = ns;
|
||||
// Set up CurrentContext.getCurrentContext()
|
||||
ClsContext.getCurrentContext = function() {
|
||||
// Set up LoopBackContext.getCurrentContext()
|
||||
LoopBackContext.getCurrentContext = function() {
|
||||
return ns && ns.active ? ns : null;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var ClsContext = require('../current-context');
|
||||
var LoopBackContext = require('../current-context');
|
||||
|
||||
module.exports = context;
|
||||
|
||||
|
@ -14,22 +14,23 @@ var name = 'loopback';
|
|||
/**
|
||||
* Context middleware.
|
||||
* ```js
|
||||
* var perRequestContext = require(
|
||||
* 'loopback-context/server/middleware/per-request-context.js');
|
||||
* var app = loopback();
|
||||
* app.use(loopback.context(options);
|
||||
* app.use(perRequestContext(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])
|
||||
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
|
||||
*/
|
||||
|
||||
function context(options) {
|
||||
options = options || {};
|
||||
var scope = options.name || name;
|
||||
var enableHttpContext = options.enableHttpContext || false;
|
||||
var ns = ClsContext.createContext(scope);
|
||||
var ns = LoopBackContext.createContext(scope);
|
||||
|
||||
// Return the middleware
|
||||
return function contextHandler(req, res, next) {
|
||||
|
@ -37,7 +38,7 @@ function context(options) {
|
|||
return next();
|
||||
}
|
||||
|
||||
ClsContext.runInContext(function processRequestInContext(ns, domain) {
|
||||
LoopBackContext.runInContext(function processRequestInContext(ns, domain) {
|
||||
req.loopbackContext = ns;
|
||||
|
||||
// Bind req/res event emitters to the given namespace
|
||||
|
|
|
@ -12,7 +12,7 @@ var expect = require('./helpers/expect');
|
|||
var loopback = require('loopback');
|
||||
var request = require('supertest');
|
||||
|
||||
describe('CLS Context', function() {
|
||||
describe('LoopBack Context', function() {
|
||||
var runInOtherDomain, runnerInterval;
|
||||
|
||||
before(function setupRunInOtherDomain() {
|
||||
|
@ -38,6 +38,7 @@ describe('CLS Context', function() {
|
|||
it('preserves callback domain', function(done) {
|
||||
var app = loopback({localRegistry: true, loadBuiltinModels: true});
|
||||
app.set('remoting', {context: false});
|
||||
app.set('legacyExplorer', false);
|
||||
app.use(require('../server/middleware/per-request-context')());
|
||||
app.use(loopback.rest());
|
||||
app.dataSource('db', {connector: 'memory'});
|
||||
|
|
Loading…
Reference in New Issue