Skip to content

Commit db9387e

Browse files
authored
Merge pull request #11 from josieusa/feature/cls-hooked-3
[SEMVER-MAJOR] Replace continuation-local-storage with cls-hooked
2 parents 7cef481 + 996a49f commit db9387e

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "0.10"
5-
- "0.12"
64
- "4"
75
- "6"

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
# loopback-context
22

33
Current context for LoopBack applications, based on
4-
node-continuation-local-storage.
4+
cls-hooked.
5+
6+
## USAGE WARNING
7+
8+
**Only if you use this package, it's recommended to not run your app using `slc run` or `node .`**
9+
10+
Run using (recommended):
11+
12+
`node -r cls-hooked .`
13+
14+
This uses the `-r` option in order to require `cls-hooked` before your app (see warnings below for more info).
15+
16+
If you wish to use `strong-supervisor`, you would need to pass node options to `slc run`, which currently has issues, according to [strong-supervisor#56](https://github.com/strongloop/strong-supervisor/issues/56).
17+
18+
A less reliable, less recommended way, which instead should be compatible with `strong-supervisor`, would be to add this Javascript line at the first line of your app, and no later than that (**the order of require statements matters**):
19+
20+
`require('cls-hooked')`
21+
22+
Warning: to rely on the order of `require` statements is error-prone.
23+
24+
## INSTALL WARNING
25+
26+
**Only if you use this package, do NOT install your app using `npm install`.**
27+
28+
Install using:
29+
30+
```
31+
npm config set engine-strict true
32+
npm install
33+
```
34+
35+
This keeps you from using Node < v4.5.
536

637
## WARNING
738

@@ -13,8 +44,8 @@ As a result, loopback-context does not work in many situations, as can be
1344
seen from issues reported in LoopBack's
1445
[issue tracker](https://github.com/strongloop/loopback/issues?utf8=%E2%9C%93&q=is%3Aissue%20getCurrentcontext).
1546

16-
If you are running on Node v6, you can try the new alternative
17-
[cls-hooked](https://github.com/Jeff-Lewis/cls-hooked).
47+
The new alternative
48+
[cls-hooked](https://github.com/Jeff-Lewis/cls-hooked) is known to possibly inherit these problems if it's not imported before everything else, that's why you are required to follow the advice above if using this.
1849

1950
## Usage
2051

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "loopback-context",
33
"version": "3.0.0-alpha.1",
4-
"description": "Current context for LoopBack applications, based on node-continuation-local-storage",
4+
"description": "Current context for LoopBack applications, based on cls-hooked",
55
"engines": {
6-
"node": ">=4"
6+
"node": "^4.5 || ^5.10 || ^6.0"
77
},
88
"keywords": [
99
"StrongLoop",
@@ -23,9 +23,10 @@
2323
},
2424
"license": "MIT",
2525
"dependencies": {
26-
"continuation-local-storage": "^3.1.7"
26+
"cls-hooked": "^4.0.1"
2727
},
2828
"devDependencies": {
29+
"async": "1.5.2",
2930
"chai": "^3.5.0",
3031
"dirty-chai": "^1.2.2",
3132
"eslint": "^2.13.1",

server/current-context.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,9 @@
55

66
'use strict';
77

8+
var cls = require('cls-hooked');
89
var domain = require('domain');
910

10-
// Require CLS only when using the current context feature.
11-
// As soon as this require is done, all the instrumentation/patching
12-
// of async-listener is fired which is not ideal.
13-
//
14-
// Some users observed stack overflows due to promise instrumentation
15-
// and other people have seen similar things:
16-
// https://github.com/othiym23/async-listener/issues/57
17-
// It all goes away when instrumentation is disabled.
18-
var cls = function() {
19-
return require('continuation-local-storage');
20-
};
21-
2211
var LoopBackContext = module.exports;
2312

2413
/**
@@ -85,7 +74,7 @@ LoopBackContext.createContext = function(scopeName) {
8574
process.context = process.context || {};
8675
var ns = process.context[scopeName];
8776
if (!ns) {
88-
ns = cls().createNamespace(scopeName);
77+
ns = cls.createNamespace(scopeName);
8978
process.context[scopeName] = ns;
9079
// Set up LoopBackContext.getCurrentContext()
9180
LoopBackContext.getCurrentContext = function() {

test/main.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
'use strict';
77

8+
var async = require('async');
89
var LoopBackContext = require('..');
910
var Domain = require('domain');
1011
var EventEmitter = require('events').EventEmitter;
@@ -98,4 +99,34 @@ describe('LoopBack Context', function() {
9899
});
99100
});
100101
});
102+
103+
// Credits for the original idea for this test case to @marlonkjoseph
104+
// Original source of the POC gist of the idea:
105+
// https://gist.github.com/marlonkjoseph/f42f3c71f746896a0d4b7279a34ea753
106+
// Heavily edited by others
107+
it('keeps context when using waterfall() from async 1.5.2',
108+
function(done) {
109+
expect(require('async/package.json').version).to.equal('1.5.2');
110+
LoopBackContext.runInContext(function() {
111+
// Trigger async waterfall callbacks
112+
async.waterfall([
113+
function pushToContext(next) {
114+
var ctx = LoopBackContext.getCurrentContext();
115+
expect(ctx).is.an('object');
116+
ctx.set('test-key', 'test-value');
117+
next();
118+
},
119+
function pullFromContext(next) {
120+
var ctx = LoopBackContext.getCurrentContext();
121+
expect(ctx).is.an('object');
122+
var testValue = ctx && ctx.get('test-key', 'test-value');
123+
next(null, testValue);
124+
},
125+
function verify(testValue, next) {
126+
expect(testValue).to.equal('test-value');
127+
next();
128+
},
129+
], done);
130+
});
131+
});
101132
});

test/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-r cls-hooked

0 commit comments

Comments
 (0)