[feature] started working on error propagation, kinda sucks, gotta think it over

This commit is contained in:
yawnt 2013-08-10 20:41:25 +02:00
parent bd3df45010
commit 9ab8749a9b
4 changed files with 36 additions and 15 deletions

View File

@ -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');
});

View File

@ -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();

View File

@ -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) });
@ -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:
*

View File

@ -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);
@ -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);