Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@leisurelink/domain-context

Package Overview
Dependencies
Maintainers
24
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@leisurelink/domain-context

attach context to your domain

  • 1.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
24
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 23 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc