Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@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
The npm package @leisurelink/domain-context receives a total of 1 weekly downloads. As such, @leisurelink/domain-context popularity was classified as not popular.
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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.