From 3a39e444ff68a74f6b586f0736bbd3f8a2511ca5 Mon Sep 17 00:00:00 2001 From: yawnt Date: Thu, 5 Sep 2013 17:44:23 +0200 Subject: [PATCH] new error propagation --- lib/caronte.js | 6 +++++- lib/caronte/index.js | 11 ++++++----- lib/caronte/passes/web.js | 9 ++++----- lib/caronte/streams/proxy.js | 7 +++---- lib/caronte/streams/websocket.js | 4 +++- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/caronte.js b/lib/caronte.js index 3ccd251..ed5e425 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -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; } }; }; diff --git a/lib/caronte/index.js b/lib/caronte/index.js index 21da744..747a64b 100644 --- a/lib/caronte/index.js +++ b/lib/caronte/index.js @@ -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'); }; }; } diff --git a/lib/caronte/passes/web.js b/lib/caronte/passes/web.js index 97c69af..dc815ab 100644 --- a/lib/caronte/passes/web.js +++ b/lib/caronte/passes/web.js @@ -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(); @@ -99,4 +98,4 @@ function stream(req, res, options, instance) { ] // <-- .forEach(function(func) { passes[func.name] = func; - }); \ No newline at end of file + }); diff --git a/lib/caronte/streams/proxy.js b/lib/caronte/streams/proxy.js index 2903c07..2f93987 100644 --- a/lib/caronte/streams/proxy.js +++ b/lib/caronte/streams/proxy.js @@ -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'); @@ -102,4 +101,4 @@ ProxyStream.prototype._read = function(size) { this.push(chunk); }; -module.exports = ProxyStream; \ No newline at end of file +module.exports = ProxyStream; diff --git a/lib/caronte/streams/websocket.js b/lib/caronte/streams/websocket.js index aa9a8cf..2d2dc69 100644 --- a/lib/caronte/streams/websocket.js +++ b/lib/caronte/streams/websocket.js @@ -36,10 +36,12 @@ WebsocketStream.prototype.onPipe = function(req) { }; WebsocketStream.prototye.onFinish = function() { - + this.proxyReq.end(); }; WebsocketStream.prototype.onResponse = function(proxyRes) { + this.proxyRes = proxyRes; + };