Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB - loopback.getCurrentContext() return null #885

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions server/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var loopback = require('../../lib/loopback');
var juggler = require('loopback-datasource-juggler');
var remoting = require('strong-remoting');
var cls = require('continuation-local-storage');
var domain = require('domain');

module.exports = context;

Expand Down Expand Up @@ -44,6 +45,13 @@ function context(options) {
var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false;
var ns = createContext(scope);

var currentDomain = process.domain = domain.create();
currentDomain.oldBind = currentDomain.bind;
currentDomain.bind = function(callback, context) {
return currentDomain.oldBind(ns.bind(callback, context), context);
};

// Return the middleware
return function contextHandler(req, res, next) {
if (req.loopbackContext) {
Expand All @@ -53,13 +61,19 @@ function context(options) {
// Bind req/res event emitters to the given namespace
ns.bindEmitter(req);
ns.bindEmitter(res);

currentDomain.add(req);
currentDomain.add(res);

// Create namespace for the request context
ns.run(function processRequestInContext(context) {
// Run the code in the context of the namespace
if (enableHttpContext) {
ns.set('http', {req: req, res: res}); // Set up the transport context
}
next();
currentDomain.run(function() {
ns.run(function processRequestInContext(context) {
// Run the code in the context of the namespace
if (enableHttpContext) {
ns.set('http', {req: req, res: res}); // Set up the transport context
}
next();
});
});
};
}
Expand Down Expand Up @@ -115,4 +129,4 @@ function chain(child) {
} else {
child.getCurrentContext = loopback.getCurrentContext;
}
}
}
70 changes: 70 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var it = require('./util/it');
var describe = require('./util/describe');
var Domain = require('domain');
var EventEmitter = require('events').EventEmitter;

describe('loopback', function() {
var nameCounter = 0;
Expand Down Expand Up @@ -388,4 +391,71 @@ describe('loopback', function() {
});
});
});

describe.onServer('loopback.getCurrentContext', function() {
var app = loopback();
var runInOtherDomain;
var runnerInterval;

before(function() {

app.use(loopback.rest());
app.dataSource('db', { connector: 'memory' });

var TestModel = loopback.createModel({ name: 'TestModel' });
app.model(TestModel, {dataSource: 'db', public: true});

var emitterInOtherDomain = new EventEmitter();
Domain.create().add(emitterInOtherDomain);

runInOtherDomain = function(fn) {
emitterInOtherDomain.once('run', fn);
};

runnerInterval = setInterval(
function() {
emitterInOtherDomain.emit('run');
}, 10);

// function for remote method
TestModel.test = function(inst, cb) {
var tmpCtx = loopback.getCurrentContext();
if (tmpCtx) tmpCtx.set('data', 'test');
if (process.domain) cb = process.domain.bind(cb); // IMPORTANT
runInOtherDomain(cb);
};

// remote method
TestModel.remoteMethod('test', {
accepts: {arg: 'inst', type: uniqueModelName},
returns: {root: true},
http: {path: '/test', verb: 'get'}
});

// after remote hook
TestModel.afterRemote('**', function(ctxx, inst, next) {
var tmpCtx = loopback.getCurrentContext();
if (tmpCtx) {
ctxx.result.data = tmpCtx.get('data');
}else {
ctxx.result.data = '';
}
next();
});
});

after(function tearDownRunInOtherDomain() {
clearInterval(runnerInterval);
});

it('passed if the patch is applied, otherwise failed', function(done) {
request(app)
.get('/TestModels/test')
.end(function(err, res) {
if (err) return done(err);
assert.equal(res.body.data, 'test');
done();
});
});
});
});