@leisurelink/domain-context

attach context to your domain which will implicitly follow your code (including async work and exceptions) without needing to pass references. useful for keeping a reference to the current user or a correlation id over wide swaths of code and to add those contexts to log/audit events.

Usage no npm install needed!

<script type="module">
  import leisurelinkDomainContext from 'https://cdn.skypack.dev/@leisurelink/domain-context';
</script>

README

Domain Context

attach context to your domain which will implicitly follow your code (including async work and exceptions) without needing to pass references. useful for keeping a reference to the current user or a correlation id over wide swaths of code and to add those contexts to log/audit events.

Usage

as Middleware

var domainContext = require('@leisurelink/domain-context');

app.use(domainContext.express.middleware);

app.use(function(req, res, next) {
    var context = domainContext();
    context.whoami = 'user';
    context.correlationId = '12345';
    next();
});
...

router.get('/', function(req, res, next) {
    process.nextTick(function() {
        // i follow async context!
        res.send('whoami: ', domainContext().whoami);
        next();
    });
});

with Domains

manually wiring up

var domain = require('domain');
var domainContext = require('domain-context');

var d1 = domain.create();
domainContext.create(d1);

d1.on('error', function(err) {
    // i even follow errors!
    console.log({
        error: err,
        who: domainContext().user,
        correlationId: domainContext().correlationId
    });
});

d1.run(function() {
    domainContext().user = 'frodo';
    process.nextTick(function() {
        console.log('whoami: ', domainContext().user);
        throw new Error(); // see on.error above
    });
});

or a shorter version

domainContext.run(function() {
    domain.active.on('error', function(err) {
        // i even follow errors!
        console.log({
            error: err,
            who: domainContext().user,
            correlationId: domainContext().correlationId
        });
    });
    domainContext().user = 'frodo';
    process.nextTick(function() {
        console.log('whoami: ', domainContext().user);
        throw new Error(); // see on.error above
    });
})

within Tests

if you have tests that require domain context, you can use the Domains method above, or the following convenience methods.

test suites like mocha do not restore Domain state after each test. calls to done() will carry into the Domain.

describe('tests', function() {
    afterEach(domainContext.exit);
    it('should test with domain context', function(done) {
        assert.ok(!domainContext());
        domainContext.run(function() {
            assert.ok(domainContext());
            done();
        });
    });
});

or if you have lots of tests that need context:

beforeEach(domainContext.run);
afterEach(domainContext.exit);

if your framework creates a context for you (using middleware/plugins), using a Promise does not require exiting.

it('should work', function() {
    return server.inject(...).then(res => { ... })
});

vs

describe('tests', function() {
    afterEach(domainContext.exit);
    it('should work', function(done) => {
        server.inject(...).then(res => done);
    });
});

Middlewares/Plugins

express

app.use(domainContext.express.middleware);

hapi

server.register(domainContext.hapi.plugin);

Known Issues

Promises are inherited from v8, but are not officially supported by nodejs. v8 native promises do not work properly with Domains. .then() loses context. if you use Promises, you should replace global.Promise with a 3rd party library.

see @leisurelink/pure-js-global-promise