Skip to content

Commit

Permalink
new error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Sep 5, 2013
1 parent 07551c6 commit 3a39e44
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/caronte.js
Expand Up @@ -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) {
Expand All @@ -62,6 +63,9 @@ proxy.createProxyServer = function createProxyServer(options) {
server.listen(port);

return server;
},
emitter : function() {
return options.ee;
}
};
};
Expand Down
11 changes: 6 additions & 5 deletions lib/caronte/index.js
Expand Up @@ -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');
};
};
}
Expand Down
9 changes: 4 additions & 5 deletions lib/caronte/passes/web.js
Expand Up @@ -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();
Expand All @@ -99,4 +98,4 @@ function stream(req, res, options, instance) {
] // <--
.forEach(function(func) {
passes[func.name] = func;
});
});
7 changes: 3 additions & 4 deletions lib/caronte/streams/proxy.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand All @@ -102,4 +101,4 @@ ProxyStream.prototype._read = function(size) {
this.push(chunk);
};

module.exports = ProxyStream;
module.exports = ProxyStream;
4 changes: 3 additions & 1 deletion lib/caronte/streams/websocket.js
Expand Up @@ -36,10 +36,12 @@ WebsocketStream.prototype.onPipe = function(req) {
};

WebsocketStream.prototye.onFinish = function() {

this.proxyReq.end();
};

WebsocketStream.prototype.onResponse = function(proxyRes) {
this.proxyRes = proxyRes;


};

Expand Down

0 comments on commit 3a39e44

Please sign in to comment.