[api] ensure before/after options are proper streams for doing arbitrary transformation on requests

This commit is contained in:
Jarrett Cruger 2015-07-23 20:49:48 -04:00
parent bc56c34430
commit 87375a856a

View File

@ -2,6 +2,8 @@ var http = require('http'),
https = require('https'),
web_o = require('./web-outgoing'),
common = require('../common'),
PassThrough = require('stream').PassThrough,
isDuplex = require('isstream').isDuplex,
passes = exports;
web_o = Object.keys(web_o).map(function(pass) {
@ -93,6 +95,15 @@ web_o = Object.keys(web_o).map(function(pass) {
function stream(req, res, options, _, server, clb) {
//
// Enable arbitary transform/duplex streams to be used on the byte streams
//
var before = isDuplex(options.before) ? options.before : new PassThrough();
var after = isDuplex(options.after) ? options.after : new PassThrough();
before.on('error', proxyError);
after.on('error', proxyError);
// And we begin!
server.emit('start', req, res, options.target)
if(options.forward) {
@ -100,7 +111,7 @@ web_o = Object.keys(web_o).map(function(pass) {
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
);
(options.buffer || req).pipe(forwardReq);
(options.buffer || req).pipe(before).pipe(forwardReq);
if(!options.target) { return res.end(); }
}
@ -141,7 +152,7 @@ web_o = Object.keys(web_o).map(function(pass) {
}
}
(options.buffer || req).pipe(proxyReq);
(options.buffer || req).pipe(before).pipe(proxyReq);
proxyReq.on('response', function(proxyRes) {
if(server) { server.emit('proxyRes', proxyRes, req, res); }
@ -154,7 +165,7 @@ web_o = Object.keys(web_o).map(function(pass) {
server.emit('end', req, res, proxyRes);
});
proxyRes.pipe(res);
proxyRes.pipe(after).pipe(res);
});
//proxyReq.end();