Skip to content

Commit 3a39e44

Browse files
committed
new error propagation
1 parent 07551c6 commit 3a39e44

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

Diff for: lib/caronte.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ proxy.createProxyServer = function createProxyServer(options) {
4848
options[key].agent = new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
4949
});
5050

51+
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
52+
5153
return {
52-
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }),
5354
web : caronte.createWebProxy(options),
5455
ws : caronte.createWsProxy(options),
5556
listen : function listen(port) {
@@ -62,6 +63,9 @@ proxy.createProxyServer = function createProxyServer(options) {
6263
server.listen(port);
6364

6465
return server;
66+
},
67+
emitter : function() {
68+
return options.ee;
6569
}
6670
};
6771
};

Diff for: lib/caronte/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ function createRightProxy(type) {
3333
return function(req, res) {
3434
var self = this,
3535
ev = 'caronte:' + type + ':';
36-
//self.emit(ev + 'begin', req, res);
36+
options.ee.emit(ev + 'begin', req, res);
3737

3838

3939
passes.some(function(pass) {
4040
var evnt = ev + pass.name.toLowerCase();
4141

42-
//self.emit(evnt + 'begin', req, res);
43-
var val = pass(req, res, options, self);
44-
//self.emit(evnt + 'end');
42+
options.ee.emit(evnt + 'begin', req, res);
43+
var val = pass(req, res, options);
44+
options.ee.emit(evnt + 'end');
45+
4546
return val;
4647
});
4748

48-
//self.emit(ev + 'end');
49+
options.ee.emit(ev + 'end');
4950
};
5051
};
5152
}

Diff for: lib/caronte/passes/web.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,17 @@ function XHeaders(req, res, options) {
7979
* @param {ClientRequest} Req Request object
8080
* @param {IncomingMessage} Res Response object
8181
* @param {Object} Options Config object passed to the proxy
82-
* @param {Object} Instance Proxy object that emits events
8382
*
8483
* @api private
8584
*/
8685

87-
function stream(req, res, options, instance) {
86+
function stream(req, res, options) {
8887
if(options.forward) {
89-
req.pipe(new ForwardStream(options, instance));
88+
req.pipe(new ForwardStream(options));
9089
}
9190

9291
if(options.target) {
93-
return req.pipe(new ProxyStream(options, res, instance)).pipe(res);
92+
return req.pipe(new ProxyStream(options, res)).pipe(res);
9493
}
9594

9695
res.end();
@@ -99,4 +98,4 @@ function stream(req, res, options, instance) {
9998
] // <--
10099
.forEach(function(func) {
101100
passes[func.name] = func;
102-
});
101+
});

Diff for: lib/caronte/streams/proxy.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ var Duplex = require('stream').Duplex,
33
http = require('http'),
44
https = require('https');
55

6-
function ProxyStream(options, res, instance) {
6+
function ProxyStream(options, res) {
77
Duplex.call(this);
88

99
this.options = options;
1010
this.res = res;
11-
this.instance = instance;
1211

1312
var self = this;
1413

@@ -86,7 +85,7 @@ ProxyStream.prototype.onResponse = function(proxyRes) {
8685
};
8786

8887
ProxyStream.prototype.onError = function(e) {
89-
if(this.instance.emit('proxyError', this.req, this.res, e)) return;
88+
if(this.options.ee.emit('proxyError', this.req, this.res, e)) return;
9089

9190
this.res.writeHead(500, { 'Content-Type': 'text/plain' });
9291
this.res.end('Internal Server Error');
@@ -102,4 +101,4 @@ ProxyStream.prototype._read = function(size) {
102101
this.push(chunk);
103102
};
104103

105-
module.exports = ProxyStream;
104+
module.exports = ProxyStream;

Diff for: lib/caronte/streams/websocket.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ WebsocketStream.prototype.onPipe = function(req) {
3636
};
3737

3838
WebsocketStream.prototye.onFinish = function() {
39-
39+
this.proxyReq.end();
4040
};
4141

4242
WebsocketStream.prototype.onResponse = function(proxyRes) {
43+
this.proxyRes = proxyRes;
44+
4345

4446
};
4547

0 commit comments

Comments
 (0)