From 87375a856a7cd11a06a242cbe3da45406be1f11c Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Thu, 23 Jul 2015 20:49:48 -0400 Subject: [PATCH] [api] ensure before/after options are proper streams for doing arbitrary transformation on requests --- lib/http-proxy/passes/web-incoming.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 4070eb3..2f6194f 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -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();