Skip to content

Commit c887a75

Browse files
committed
[api] Updated request hashes to use a unique identifier
1 parent d15bba4 commit c887a75

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

lib/node-http-proxy.js

+38-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports.HttpProxy = function () {
3232
this.emitter = new(events.EventEmitter);
3333
this.events = {};
3434
this.listeners = {};
35+
this.collisions = {};
3536
};
3637

3738
exports.createServer = function () {
@@ -83,34 +84,55 @@ exports.HttpProxy.prototype = {
8384
watch: function (req, res) {
8485
var self = this;
8586

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();
8790

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] = {
89105
onData: function () {
90-
self.events[req].push(['data'].concat(self.toArray(arguments)));
106+
self.events[req.id].push(['data'].concat(self.toArray(arguments)));
91107
},
92108
onEnd: function () {
93-
self.events[req].push(['end'].concat(self.toArray(arguments)));
109+
self.events[req.id].push(['end'].concat(self.toArray(arguments)));
94110
}
95111
};
96112

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+
99116
},
100117

101118
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+
105122
// 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();
108125
req.emit.apply(req, args);
109126
}
110-
127+
111128
// 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+
}
114136
},
115137

116138
proxyRequest: function (port, server, req, res) {
@@ -135,11 +157,12 @@ exports.HttpProxy.prototype = {
135157
res.end();
136158
});
137159

160+
138161
// Add a listener for the reverse_proxy response event
139162
reverse_proxy.addListener('response', function (response) {
140163
// Set the response headers of the client response
141164
res.writeHead(response.statusCode, response.headers);
142-
165+
143166
// Add event handler for the proxied response in chunks
144167
response.addListener('data', function (chunk) {
145168
if(req.method !== 'HEAD') {

0 commit comments

Comments
 (0)