Skip to content

Commit

Permalink
[feature] started working on error propagation, kinda sucks, gotta th…
Browse files Browse the repository at this point in the history
…ink it over
  • Loading branch information
yawnt committed Aug 10, 2013
1 parent bd3df45 commit 9ab8749
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/caronte/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function createRightProxy(type) {
var event = ev + pass.name.toLowerCase();

self.emit(event + 'begin', req, res);
pass(req, res, options);
pass(req, res, options, self);
self.emit(event + 'end');
});

Expand Down
7 changes: 4 additions & 3 deletions lib/caronte/passes/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ 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) {
function stream(req, res, options, instance) {
if(options.forward) {
req.pipe(new ForwardStream(options.forward));
req.pipe(new ForwardStream(options, instance));
}

if(options.target) {
return req.pipe(new ProxyStream(res, options)).pipe(res);
return req.pipe(new ProxyStream(options, res, instance)).pipe(res);
}

res.end();
Expand Down
9 changes: 7 additions & 2 deletions lib/caronte/streams/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ module.exports = ForwardStream;
* @api private
*/

function ForwardStream() {
function ForwardStream(options) {
var self = this;

self.options = options;
self.res = res;

Writable.call(this);

this.once('pipe', function(pipe) { self.onPipe(pipe) });
Expand All @@ -48,10 +51,12 @@ ForwardStream.prototype.onPipe = function(request) {
this.forwardReq = (options.ssl ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, request)
);

this.forwardReq.on('error', function() {}); /** Fire and forget */
};

/**
* Closes forwarded request when `pipe` is finished
* Closes forwarded request when `pipe` is finished.
*
* Examples:
*
Expand Down
33 changes: 24 additions & 9 deletions lib/caronte/streams/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ var Duplex = require('stream').Duplex,
http = require('http'),
https = require('https');

function ProxyStream() {
function ProxyStream(options, res, instance) {
this.options = options;
this.res = res;
this.instance = instance;

var self = this;

Duplex.call(this);
Expand All @@ -14,26 +18,37 @@ function ProxyStream() {

require('util').inherits(ProxyStream, Duplex);

ProxyStream.prototype.onPipe = function(request) {
ProxyStream.prototype.onPipe = function(req) {
this.req = req;

var self = this;

this.proxyReq = (options.ssl ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, request)
common.setupOutgoing(options.ssl || {}, options, req)
);

this.proxyReq.once('response', function(response) {
self.onResponse(response);
this.proxyReq.once('response', function(proxyRes) {
self.onResponse(proxyRes);
});
this.proxyReq.on('error', function() {}); // XXX TODO: add error handling
}
this.proxyReq.on('error', function(e) {
self.onError(e);
});
};

ProxyStream.prototype.onFinish = function() {

}
};

ProxyStream.prototype.onResponse = function(proxyRes) {
this.proxyRes = proxyRes;
}
};

ProxyStream.prototype.onError = function(e) {
if(this.instance.emit('error', this.req, this.res, e)) return;

this.res.writeHead(500, { 'Content-Type': 'text/plain' });
this.res.end('Internal Server Error');
};

ProxyStream.prototype._write = function(chunk, encoding, callback) {
this.proxyReq.write(chunk, encoding, callback);
Expand Down

0 comments on commit 9ab8749

Please sign in to comment.