Skip to content

Commit 87375a8

Browse files
committed
[api] ensure before/after options are proper streams for doing arbitrary transformation on requests
1 parent bc56c34 commit 87375a8

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/http-proxy/passes/web-incoming.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var http = require('http'),
22
https = require('https'),
33
web_o = require('./web-outgoing'),
44
common = require('../common'),
5+
PassThrough = require('stream').PassThrough,
6+
isDuplex = require('isstream').isDuplex,
57
passes = exports;
68

79
web_o = Object.keys(web_o).map(function(pass) {
@@ -93,14 +95,23 @@ web_o = Object.keys(web_o).map(function(pass) {
9395

9496
function stream(req, res, options, _, server, clb) {
9597

98+
//
99+
// Enable arbitary transform/duplex streams to be used on the byte streams
100+
//
101+
var before = isDuplex(options.before) ? options.before : new PassThrough();
102+
var after = isDuplex(options.after) ? options.after : new PassThrough();
103+
104+
before.on('error', proxyError);
105+
after.on('error', proxyError);
106+
96107
// And we begin!
97108
server.emit('start', req, res, options.target)
98109
if(options.forward) {
99110
// If forward enable, so just pipe the request
100111
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
101112
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
102113
);
103-
(options.buffer || req).pipe(forwardReq);
114+
(options.buffer || req).pipe(before).pipe(forwardReq);
104115
if(!options.target) { return res.end(); }
105116
}
106117

@@ -141,7 +152,7 @@ web_o = Object.keys(web_o).map(function(pass) {
141152
}
142153
}
143154

144-
(options.buffer || req).pipe(proxyReq);
155+
(options.buffer || req).pipe(before).pipe(proxyReq);
145156

146157
proxyReq.on('response', function(proxyRes) {
147158
if(server) { server.emit('proxyRes', proxyRes, req, res); }
@@ -154,7 +165,7 @@ web_o = Object.keys(web_o).map(function(pass) {
154165
server.emit('end', req, res, proxyRes);
155166
});
156167

157-
proxyRes.pipe(res);
168+
proxyRes.pipe(after).pipe(res);
158169
});
159170

160171
//proxyReq.end();

0 commit comments

Comments
 (0)