Skip to content

Commit 3d4db3b

Browse files
author
Raymond Feng
committed
Clean up the context setup
1 parent 5401be5 commit 3d4db3b

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

example/client-server/models.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CartItem.sum = function(cartId, callback) {
1919
}, 0);
2020

2121
var ns = loopback.getCurrentContext();
22-
console.log(ns.get('req').url);
22+
console.log(ns.get('http').req.url);
2323
callback(null, total);
2424
});
2525
}

lib/middleware/context.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,18 @@ function context(options) {
2121
return ns;
2222
};
2323

24-
if (typeof juggler.getCurrentContext === 'function') {
25-
var jugglerContext = new ChainedContext(juggler.getCurrentContext(), ns);
26-
juggler.getCurrentContext = function () {
27-
return jugglerContext;
28-
};
29-
} else {
30-
juggler.getCurrentContext = loopback.getCurrentContext;
31-
}
32-
33-
if (typeof remoting.getCurrentContext === 'function') {
34-
var remotingContext = new ChainedContext(remoting.getCurrentContext(), ns);
35-
remoting.getCurrentContext = function () {
36-
return remotingContext;
37-
};
38-
} else {
39-
remoting.getCurrentContext = loopback.getCurrentContext;
40-
}
24+
chain(juggler);
25+
chain(remoting);
4126

4227
// Return the middleware
43-
return function (req, res, next) {
28+
return function(req, res, next) {
4429
// Bind req/res event emitters to the given namespace
4530
ns.bindEmitter(req);
4631
ns.bindEmitter(res);
4732
// Create namespace for the request context
48-
ns.run(function (context) {
33+
ns.run(function(context) {
4934
// Run the code in the context of the namespace
50-
ns.set('req', req);
51-
ns.set('res', res);
35+
ns.set('http', {req: req, res: res}); // Set up the transport context
5236
next();
5337
});
5438
};
@@ -93,4 +77,16 @@ ChainedContext.prototype.reset = function (name, val) {
9377
} else {
9478
return this.parent && this.parent.reset(name, val);
9579
}
96-
};
80+
};
81+
82+
function chain(child) {
83+
if (typeof child.getCurrentContext === 'function') {
84+
var childContext = new ChainedContext(child.getCurrentContext(),
85+
loopback.getCurrentContext());
86+
child.getCurrentContext = function() {
87+
return childContext;
88+
};
89+
} else {
90+
child.getCurrentContext = loopback.getCurrentContext;
91+
}
92+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"uid2": "0.0.3",
4747
"underscore": "~1.7.0",
4848
"underscore.string": "~2.3.3",
49-
"continuation-local-storage": "~3.0.0"
49+
"continuation-local-storage": "~3.1.1"
5050
},
5151
"peerDependencies": {
5252
"loopback-datasource-juggler": "^2.8.0"

test/rest.middleware.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ describe('loopback.rest', function() {
9292
var User = givenUserModelWithAuth();
9393
User.getToken = function(cb) {
9494
var context = loopback.getCurrentContext();
95-
var req = context.get('req');
95+
var req = context.get('http').req;
9696
expect(req).to.have.property('accessToken');
9797

9898
var juggler = require('loopback-datasource-juggler');
99-
expect(juggler.getCurrentContext().get('req')).to.have.property('accessToken');
99+
expect(juggler.getCurrentContext().get('http').req)
100+
.to.have.property('accessToken');
100101

101102
var remoting = require('strong-remoting');
102-
expect(remoting.getCurrentContext().get('req')).to.have.property('accessToken');
103+
expect(remoting.getCurrentContext().get('http').req)
104+
.to.have.property('accessToken');
103105

104106
cb(null, req && req.accessToken ? req.accessToken.id : null);
105107
};

0 commit comments

Comments
 (0)