Skip to content

Commit

Permalink
Rename to loopback-context/LoopBackContext
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Jul 29, 2016
1 parent 9bd4afb commit 867a7c3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# loopback-context-cls
# loopback-context

Current context for LoopBack applications, based on
node-continuation-local-storage.
Expand All @@ -11,7 +11,7 @@ node-continuation-local-storage.
```json
{
"initial": {
"loopback-context-cls#per-request-context": {
"loopback-context#per-request-context": {
}
}
}
Expand All @@ -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' });
});
Expand Down
8 changes: 4 additions & 4 deletions browser/current-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
};
22 changes: 11 additions & 11 deletions server/current-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
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
* across async calls, it behaves like a thread-local storage.
*
* @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**
Expand All @@ -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) {
Expand All @@ -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.
Expand All @@ -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;
};
}
Expand Down
13 changes: 7 additions & 6 deletions server/middleware/per-request-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

var ClsContext = require('../current-context');
var LoopBackContext = require('../current-context');

module.exports = context;

Expand All @@ -14,30 +14,31 @@ 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) {
if (req.loopbackContext) {
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
Expand Down
3 changes: 2 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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'});
Expand Down

0 comments on commit 867a7c3

Please sign in to comment.