diff --git a/example/client-server/models.js b/example/client-server/models.js index 765b984d0..d2fbaf10a 100644 --- a/example/client-server/models.js +++ b/example/client-server/models.js @@ -19,7 +19,7 @@ CartItem.sum = function(cartId, callback) { }, 0); var ns = loopback.getCurrentContext(); - console.log(ns.get('req').url); + console.log(ns.get('http').req.url); callback(null, total); }); } diff --git a/lib/middleware/context.js b/lib/middleware/context.js index 7f9a41ca9..41662a6bd 100644 --- a/lib/middleware/context.js +++ b/lib/middleware/context.js @@ -21,34 +21,18 @@ function context(options) { return ns; }; - if (typeof juggler.getCurrentContext === 'function') { - var jugglerContext = new ChainedContext(juggler.getCurrentContext(), ns); - juggler.getCurrentContext = function () { - return jugglerContext; - }; - } else { - juggler.getCurrentContext = loopback.getCurrentContext; - } - - if (typeof remoting.getCurrentContext === 'function') { - var remotingContext = new ChainedContext(remoting.getCurrentContext(), ns); - remoting.getCurrentContext = function () { - return remotingContext; - }; - } else { - remoting.getCurrentContext = loopback.getCurrentContext; - } + chain(juggler); + chain(remoting); // Return the middleware - return function (req, res, next) { + return function(req, res, next) { // Bind req/res event emitters to the given namespace ns.bindEmitter(req); ns.bindEmitter(res); // Create namespace for the request context - ns.run(function (context) { + ns.run(function(context) { // Run the code in the context of the namespace - ns.set('req', req); - ns.set('res', res); + ns.set('http', {req: req, res: res}); // Set up the transport context next(); }); }; @@ -93,4 +77,16 @@ ChainedContext.prototype.reset = function (name, val) { } else { return this.parent && this.parent.reset(name, val); } -}; \ No newline at end of file +}; + +function chain(child) { + if (typeof child.getCurrentContext === 'function') { + var childContext = new ChainedContext(child.getCurrentContext(), + loopback.getCurrentContext()); + child.getCurrentContext = function() { + return childContext; + }; + } else { + child.getCurrentContext = loopback.getCurrentContext; + } +} \ No newline at end of file diff --git a/package.json b/package.json index ebbcd9890..1e13a61e3 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "uid2": "0.0.3", "underscore": "~1.7.0", "underscore.string": "~2.3.3", - "continuation-local-storage": "~3.0.0" + "continuation-local-storage": "~3.1.1" }, "peerDependencies": { "loopback-datasource-juggler": "^2.8.0" diff --git a/test/rest.middleware.test.js b/test/rest.middleware.test.js index 54a69ba33..13f7c98e6 100644 --- a/test/rest.middleware.test.js +++ b/test/rest.middleware.test.js @@ -92,14 +92,16 @@ describe('loopback.rest', function() { var User = givenUserModelWithAuth(); User.getToken = function(cb) { var context = loopback.getCurrentContext(); - var req = context.get('req'); + var req = context.get('http').req; expect(req).to.have.property('accessToken'); var juggler = require('loopback-datasource-juggler'); - expect(juggler.getCurrentContext().get('req')).to.have.property('accessToken'); + expect(juggler.getCurrentContext().get('http').req) + .to.have.property('accessToken'); var remoting = require('strong-remoting'); - expect(remoting.getCurrentContext().get('req')).to.have.property('accessToken'); + expect(remoting.getCurrentContext().get('http').req) + .to.have.property('accessToken'); cb(null, req && req.accessToken ? req.accessToken.id : null); };