@@ -32,6 +32,7 @@ exports.HttpProxy = function () {
32
32
this . emitter = new ( events . EventEmitter ) ;
33
33
this . events = { } ;
34
34
this . listeners = { } ;
35
+ this . collisions = { } ;
35
36
} ;
36
37
37
38
exports . createServer = function ( ) {
@@ -83,34 +84,55 @@ exports.HttpProxy.prototype = {
83
84
watch : function ( req , res ) {
84
85
var self = this ;
85
86
86
- this . events [ req ] = [ ] ;
87
+ // Create a unique id for this request so
88
+ // we can reference it later.
89
+ var id = new Date ( ) . getTime ( ) . toString ( ) ;
87
90
88
- this . listeners [ req ] = {
91
+ // If we get a request in the same tick, we need to
92
+ // append to the id so it stays unique.
93
+ if ( typeof this . collisions [ id ] === 'undefined' ) {
94
+ this . collisions [ id ] = 0 ;
95
+ }
96
+ else {
97
+ this . collisions [ id ] ++ ;
98
+ id += this . collisions [ id ] ;
99
+ }
100
+
101
+ req . id = id ;
102
+ this . events [ req . id ] = [ ] ;
103
+
104
+ this . listeners [ req . id ] = {
89
105
onData : function ( ) {
90
- self . events [ req ] . push ( [ 'data' ] . concat ( self . toArray ( arguments ) ) ) ;
106
+ self . events [ req . id ] . push ( [ 'data' ] . concat ( self . toArray ( arguments ) ) ) ;
91
107
} ,
92
108
onEnd : function ( ) {
93
- self . events [ req ] . push ( [ 'end' ] . concat ( self . toArray ( arguments ) ) ) ;
109
+ self . events [ req . id ] . push ( [ 'end' ] . concat ( self . toArray ( arguments ) ) ) ;
94
110
}
95
111
} ;
96
112
97
- req . addListener ( 'data' , this . listeners [ req ] . onData ) ;
98
- req . addListener ( 'end' , this . listeners [ req ] . onEnd ) ;
113
+ req . addListener ( 'data' , this . listeners [ req . id ] . onData ) ;
114
+ req . addListener ( 'end' , this . listeners [ req . id ] . onEnd ) ;
115
+
99
116
} ,
100
117
101
118
unwatch : function ( req , res ) {
102
- req . removeListener ( 'data' , this . listeners [ req ] . onData ) ;
103
- req . removeListener ( 'end' , this . listeners [ req ] . onEnd ) ;
104
-
119
+ req . removeListener ( 'data' , this . listeners [ req . id ] . onData ) ;
120
+ req . removeListener ( 'end' , this . listeners [ req . id ] . onEnd ) ;
121
+
105
122
// Rebroadcast any events that have been buffered
106
- while ( this . events [ req ] . length > 0 ) {
107
- var args = this . events [ req ] . shift ( ) ;
123
+ while ( this . events [ req . id ] . length > 0 ) {
124
+ var args = this . events [ req . id ] . shift ( ) ;
108
125
req . emit . apply ( req , args ) ;
109
126
}
110
-
127
+
111
128
// Remove the data from the event and listeners hashes
112
- delete this . listeners [ req ] ;
113
- delete this . events [ req ] ;
129
+ delete this . listeners [ req . id ] ;
130
+ delete this . events [ req . id ] ;
131
+
132
+ // If this request id is a base time, delete it
133
+ if ( typeof this . collisions [ req . id ] !== 'undefined' ) {
134
+ delete this . collisions [ req . id ] ;
135
+ }
114
136
} ,
115
137
116
138
proxyRequest : function ( port , server , req , res ) {
@@ -135,11 +157,12 @@ exports.HttpProxy.prototype = {
135
157
res . end ( ) ;
136
158
} ) ;
137
159
160
+
138
161
// Add a listener for the reverse_proxy response event
139
162
reverse_proxy . addListener ( 'response' , function ( response ) {
140
163
// Set the response headers of the client response
141
164
res . writeHead ( response . statusCode , response . headers ) ;
142
-
165
+
143
166
// Add event handler for the proxied response in chunks
144
167
response . addListener ( 'data' , function ( chunk ) {
145
168
if ( req . method !== 'HEAD' ) {
0 commit comments