Skip to content

Commit 442d77f

Browse files
committed
minor improvements
- fix jsdoc for getCurrentContext - clean up getCurrentContext code - clean up the new test
1 parent 244627a commit 442d77f

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

server/current-context.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ var LoopBackContext = module.exports;
1414
* Get the current context object. The context is preserved
1515
* across async calls, it behaves like a thread-local storage.
1616
*
17+
* @options {Object} [options]
18+
* @property {Boolean} bind Bind get/set/bind methods of the context to the
19+
* context that's current at the time getCurrentContext() is invoked. This
20+
* can be used to work around 3rd party code breaking CLS context propagation.
1721
* @returns {Namespace} The context object or null.
1822
*/
19-
LoopBackContext.getCurrentContext = function() {
23+
LoopBackContext.getCurrentContext = function(options) {
2024
// A placeholder method, see LoopBackContext.createContext() for the real version
2125
return null;
2226
};
@@ -91,10 +95,12 @@ LoopBackContext.createContext = function(scopeName) {
9195
* you may run into unexpected issues that are fixed only for get & set.
9296
*/
9397
var boundContext = Object.create(ns);
94-
// Call to Function.prototype.bind(), not ns.bind()
95-
boundContext.bind = ns.bind.bind(ns);
9698
boundContext.get = boundContext.bind(ns.get);
9799
boundContext.set = boundContext.bind(ns.set);
100+
101+
// Call to Function.prototype.bind(), not ns.bind()
102+
boundContext.bind = ns.bind.bind(ns);
103+
98104
return boundContext;
99105
};
100106
}

test/main.test.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,31 +212,37 @@ describe('LoopBack Context', function() {
212212
function runWithRequestId(pushedValue, bindNextCb) {
213213
return new Promise(function chainExecutor(outerResolve, reject) {
214214
LoopBackContext.runInContext(function concurrentChain() {
215-
var middleware1 = function middleware1(req, res, next) {
215+
function middlewareBreakingCls(req, res, next) {
216216
var ctx = LoopBackContext.getCurrentContext({bind: true});
217217
if (bindNextCb) {
218218
next = ctx.bind(next);
219219
}
220-
ctx.set('test-key', req.pushedValue);
220+
ctx.set('test-key', pushedValue);
221221
var whenPromise = whenV377().delay(timeout);
222222
whenPromise.then(next).catch(reject);
223223
};
224-
var middleware2 = function middleware2(req, res, next) {
224+
225+
function middlewareReadingContext(req, res, next) {
225226
var ctx = LoopBackContext.getCurrentContext({bind: true});
226227
var pulledValue = ctx && ctx.get('test-key');
227228
next(null, pulledValue);
228229
};
229-
// Run chain
230-
var req = {pushedValue: pushedValue};
230+
231+
// Run the chain
232+
var req = null;
231233
var res = null;
232-
var next2 = function resolveWithResult(error, result) {
233-
outerResolve({
234-
pulledValue: result,
235-
pushedValue: pushedValue,
234+
middlewareBreakingCls(req, res, function(err) {
235+
if (err) return reject(err);
236+
237+
middlewareReadingContext(req, res, function(err, result) {
238+
if (err) return reject(err);
239+
240+
outerResolve({
241+
pulledValue: result,
242+
pushedValue: pushedValue,
243+
});
236244
});
237-
};
238-
var next1 = middleware2.bind(null, req, res, next2);
239-
middleware1(req, res, next1);
245+
});
240246
});
241247
});
242248
}

0 commit comments

Comments
 (0)