new error propagation

This commit is contained in:
yawnt 2013-09-05 17:44:23 +02:00
parent 07551c63e4
commit 3a39e444ff
5 changed files with 21 additions and 16 deletions

View File

@ -48,8 +48,9 @@ proxy.createProxyServer = function createProxyServer(options) {
options[key].agent = new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
});
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
return {
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }),
web : caronte.createWebProxy(options),
ws : caronte.createWsProxy(options),
listen : function listen(port) {
@ -62,6 +63,9 @@ proxy.createProxyServer = function createProxyServer(options) {
server.listen(port);
return server;
},
emitter : function() {
return options.ee;
}
};
};

View File

@ -33,19 +33,20 @@ function createRightProxy(type) {
return function(req, res) {
var self = this,
ev = 'caronte:' + type + ':';
//self.emit(ev + 'begin', req, res);
options.ee.emit(ev + 'begin', req, res);
passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase();
//self.emit(evnt + 'begin', req, res);
var val = pass(req, res, options, self);
//self.emit(evnt + 'end');
options.ee.emit(evnt + 'begin', req, res);
var val = pass(req, res, options);
options.ee.emit(evnt + 'end');
return val;
});
//self.emit(ev + 'end');
options.ee.emit(ev + 'end');
};
};
}

View File

@ -79,18 +79,17 @@ function XHeaders(req, res, options) {
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {Object} Options Config object passed to the proxy
* @param {Object} Instance Proxy object that emits events
*
* @api private
*/
function stream(req, res, options, instance) {
function stream(req, res, options) {
if(options.forward) {
req.pipe(new ForwardStream(options, instance));
req.pipe(new ForwardStream(options));
}
if(options.target) {
return req.pipe(new ProxyStream(options, res, instance)).pipe(res);
return req.pipe(new ProxyStream(options, res)).pipe(res);
}
res.end();

View File

@ -3,12 +3,11 @@ var Duplex = require('stream').Duplex,
http = require('http'),
https = require('https');
function ProxyStream(options, res, instance) {
function ProxyStream(options, res) {
Duplex.call(this);
this.options = options;
this.res = res;
this.instance = instance;
var self = this;
@ -86,7 +85,7 @@ ProxyStream.prototype.onResponse = function(proxyRes) {
};
ProxyStream.prototype.onError = function(e) {
if(this.instance.emit('proxyError', this.req, this.res, e)) return;
if(this.options.ee.emit('proxyError', this.req, this.res, e)) return;
this.res.writeHead(500, { 'Content-Type': 'text/plain' });
this.res.end('Internal Server Error');

View File

@ -36,10 +36,12 @@ WebsocketStream.prototype.onPipe = function(req) {
};
WebsocketStream.prototye.onFinish = function() {
this.proxyReq.end();
};
WebsocketStream.prototype.onResponse = function(proxyRes) {
this.proxyRes = proxyRes;
};