From 7faeada0f0662722e4a6636d279e38c2bf0bf375 Mon Sep 17 00:00:00 2001 From: Luke Petre Date: Tue, 6 Oct 2015 15:26:09 +0100 Subject: [PATCH 1/2] Handle callbacks within a domain --- src/lib/transport.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/transport.js b/src/lib/transport.js index 6e18112ec..c9a2a42d6 100644 --- a/src/lib/transport.js +++ b/src/lib/transport.js @@ -140,6 +140,10 @@ Transport.prototype.request = function (params, cb) { // determine the response based on the presense of a callback if (typeof cb === 'function') { + // handle callbacks within a domain + if (process.domain) { + cb = process.domain.bind(cb); + } ret = { abort: abortRequest }; From 284f61e2eee35331b8c3e240177735d90069a762 Mon Sep 17 00:00:00 2001 From: Luke Petre Date: Tue, 6 Oct 2015 17:02:51 +0100 Subject: [PATCH 2/2] Adding transport tests for domain callbacks --- test/unit/specs/transport.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/unit/specs/transport.js b/test/unit/specs/transport.js index e00b03207..e4ff46511 100644 --- a/test/unit/specs/transport.js +++ b/test/unit/specs/transport.js @@ -670,6 +670,35 @@ describe('Transport Class', function () { }); }); + describe('handles process.domain', function () { + if (process && process.hasOwnProperty('domain')) { + it('works without a domain', function () { + expect(process.domain).to.be(null); + var tran = new Transport(); + shortCircuitRequest(tran); + tran.request({}, function(err, result, status) { + expect(process.domain).to.be(null); + }); + }); + + it('binds the callback to the correct domain', function () { + expect(process.domain).to.be(null); + var domain = require('domain').create(); + domain.run(function() { + var tran = new Transport(); + shortCircuitRequest(tran); + expect(process.domain).not.to.be(null); + var startingDomain = process.domain + tran.request({}, function(err, result, status) { + expect(process.domain).not.to.be(null); + expect(process.domain).to.be(startingDomain); + process.domain.exit(); + }); + }); + }); + } + }); + describe('aborting', function () { it('prevents the request from starting if called in the same tick', function () { var tran = new Transport({