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

express-domaining

Package Overview
Dependencies
Maintainers
4
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-domaining - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

36

lib/domain.js

@@ -26,10 +26,6 @@ /**

*
* @param {Object} logger
* Optional logger to write errors in the domain. If not defined, it uses console as logger.
* @return {Function(req, res, next)} Express middleware.
*/
module.exports = function(logger) {
module.exports = function() {
var errorLogger = logger || console;
return function domainMiddleware(req, res, next) {

@@ -41,24 +37,24 @@ var requestDomain = domain.create();

requestDomain.add(res);
var cleanDomain;
res.on('finish', cleanDomain);
requestDomain.on('error', domainErrorHandler);
requestDomain.enter();
requestDomain.run(next);
var domainErrorHandler = function(err) {
errorLogger.error(err);
function domainErrorHandler(err) {
cleanDomain();
};
// Pass to the user defined error handlers in express.
// This way the request can be finished with the user payloads the client may expect
// and logged.
next(err);
// rethrow to allow cluster code or unhandledException to manage it
// Usually, as this error is unexpected, the process should be terminated and restarted
throw err;
}
var requestHandler = function() {
next();
};
cleanDomain = function() {
function cleanDomain() {
requestDomain.removeListener('error', domainErrorHandler);
res.removeListener('finish', cleanDomain);
requestDomain.exit();
};
res.on('finish', cleanDomain);
requestDomain.on('error', domainErrorHandler);
requestDomain.enter();
requestDomain.run(requestHandler);
}
};
};
{
"name": "express-domaining",
"description": "Express middleware to create and destroy a node domain",
"version": "1.0.2",
"version": "2.0.0",
"license": "Apache-2.0",

@@ -13,3 +13,4 @@ "author": {

"Guido García Bernardo <guido.garciabernardo@telefonica.com>",
"David Lozano Llanos <david.lozanollanos@telefonica.com>"
"David Lozano Llanos <david.lozanollanos@telefonica.com>",
"Javier Mendiara Cañardo <javier.mendiaracanardo@telefonica.com>"
],

@@ -16,0 +17,0 @@ "repository": {

@@ -5,2 +5,7 @@ # express-domaining

[READ THIS BEFORE USING THIS MODULE!](https://nodejs.org/api/domain.html#domain_domain)
Allows you to respond to requests that had and unexpected error using your express error handlers and
rethrows the exception to be captured by your own unhandledExcetion/cluster code
[![npm version](https://badge.fury.io/js/express-domaining.svg)](http://badge.fury.io/js/express-domaining)

@@ -28,15 +33,2 @@ [![Build Status](https://travis-ci.org/telefonica/node-express-domaining.svg)](https://travis-ci.org/telefonica/node-express-domaining)

By default, errors in domain are traced by `console.error`. However, you can use a custom logger:
```js
var express = require('express'),
expressDomain = require('express-domaining'),
logger = require('logops');
var app = express();
app.use(expressDomain(logger));
app.listen(3000);
```
## License

@@ -43,0 +35,0 @@

@@ -84,4 +84,11 @@ 'use strict';

});
var unhandlerError = new Error('test error');
// get mocha exception handler
var originalException = process.listeners('uncaughtException').pop()
// and remove it
process.removeListener('uncaughtException', originalException);
var domainMiddleware = new DomainMiddleware();
domainMiddleware(req, res, function onNext() {
domainMiddleware(req, res, function onNext(propagatedError) {
expect(domainSpy.enter.calledOnce).to.be.true;

@@ -91,8 +98,17 @@ expect(domainSpy.run.calledOnce).to.be.true;

expect(domainSpy.add.withArgs(res).calledOnce).to.be.true;
if (propagatedError) {
process.once('uncaughtException', function(err) {
// restore mocha error handler
process.listeners('uncaughtException').push(originalException)
expect(err).to.be.eql(unhandlerError)
expect(propagatedError).to.be.eql(unhandlerError)
expect(domainSpy.remove.withArgs(req).notCalled).to.be.true;
expect(domainSpy.remove.withArgs(res).notCalled).to.be.true;
expect(domainSpy.exit.calledOnce).to.be.true;
done();
});
}
// Trigger error
domain.emit('error', new Error('test error'));
expect(domainSpy.remove.withArgs(req).notCalled).to.be.true;
expect(domainSpy.remove.withArgs(res).notCalled).to.be.true;
expect(domainSpy.exit.calledOnce).to.be.true;
done();
domain.emit('error', unhandlerError);
});

@@ -99,0 +115,0 @@ });

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