
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@leisurelink/domain-context
Advanced tools
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.
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();
});
});
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
});
})
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);
});
});
app.use(domainContext.express.middleware);
server.register(domainContext.hapi.plugin);
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
FAQs
attach context to your domain
We found that @leisurelink/domain-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 24 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.