@@ -2,6 +2,8 @@ var http = require('http'),
2
2
https = require ( 'https' ) ,
3
3
web_o = require ( './web-outgoing' ) ,
4
4
common = require ( '../common' ) ,
5
+ PassThrough = require ( 'stream' ) . PassThrough ,
6
+ isDuplex = require ( 'isstream' ) . isDuplex ,
5
7
passes = exports ;
6
8
7
9
web_o = Object . keys ( web_o ) . map ( function ( pass ) {
@@ -93,14 +95,23 @@ web_o = Object.keys(web_o).map(function(pass) {
93
95
94
96
function stream ( req , res , options , _ , server , clb ) {
95
97
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
+
96
107
// And we begin!
97
108
server . emit ( 'start' , req , res , options . target )
98
109
if ( options . forward ) {
99
110
// If forward enable, so just pipe the request
100
111
var forwardReq = ( options . forward . protocol === 'https:' ? https : http ) . request (
101
112
common . setupOutgoing ( options . ssl || { } , options , req , 'forward' )
102
113
) ;
103
- ( options . buffer || req ) . pipe ( forwardReq ) ;
114
+ ( options . buffer || req ) . pipe ( before ) . pipe ( forwardReq ) ;
104
115
if ( ! options . target ) { return res . end ( ) ; }
105
116
}
106
117
@@ -141,7 +152,7 @@ web_o = Object.keys(web_o).map(function(pass) {
141
152
}
142
153
}
143
154
144
- ( options . buffer || req ) . pipe ( proxyReq ) ;
155
+ ( options . buffer || req ) . pipe ( before ) . pipe ( proxyReq ) ;
145
156
146
157
proxyReq . on ( 'response' , function ( proxyRes ) {
147
158
if ( server ) { server . emit ( 'proxyRes' , proxyRes , req , res ) ; }
@@ -154,7 +165,7 @@ web_o = Object.keys(web_o).map(function(pass) {
154
165
server . emit ( 'end' , req , res , proxyRes ) ;
155
166
} ) ;
156
167
157
- proxyRes . pipe ( res ) ;
168
+ proxyRes . pipe ( after ) . pipe ( res ) ;
158
169
} ) ;
159
170
160
171
//proxyReq.end();
0 commit comments