mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[api] Updated request hashes to use a unique identifier
This commit is contained in:
parent
d15bba4c1d
commit
c887a75762
@ -32,6 +32,7 @@ exports.HttpProxy = function () {
|
|||||||
this.emitter = new(events.EventEmitter);
|
this.emitter = new(events.EventEmitter);
|
||||||
this.events = {};
|
this.events = {};
|
||||||
this.listeners = {};
|
this.listeners = {};
|
||||||
|
this.collisions = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.createServer = function () {
|
exports.createServer = function () {
|
||||||
@ -83,34 +84,55 @@ exports.HttpProxy.prototype = {
|
|||||||
watch: function (req, res) {
|
watch: function (req, res) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.events[req] = [];
|
// Create a unique id for this request so
|
||||||
|
// we can reference it later.
|
||||||
|
var id = new Date().getTime().toString();
|
||||||
|
|
||||||
this.listeners[req] = {
|
// If we get a request in the same tick, we need to
|
||||||
|
// append to the id so it stays unique.
|
||||||
|
if(typeof this.collisions[id] === 'undefined') {
|
||||||
|
this.collisions[id] = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.collisions[id]++;
|
||||||
|
id += this.collisions[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
req.id = id;
|
||||||
|
this.events[req.id] = [];
|
||||||
|
|
||||||
|
this.listeners[req.id] = {
|
||||||
onData: function () {
|
onData: function () {
|
||||||
self.events[req].push(['data'].concat(self.toArray(arguments)));
|
self.events[req.id].push(['data'].concat(self.toArray(arguments)));
|
||||||
},
|
},
|
||||||
onEnd: function () {
|
onEnd: function () {
|
||||||
self.events[req].push(['end'].concat(self.toArray(arguments)));
|
self.events[req.id].push(['end'].concat(self.toArray(arguments)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
req.addListener('data', this.listeners[req].onData);
|
req.addListener('data', this.listeners[req.id].onData);
|
||||||
req.addListener('end', this.listeners[req].onEnd);
|
req.addListener('end', this.listeners[req.id].onEnd);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
unwatch: function (req, res) {
|
unwatch: function (req, res) {
|
||||||
req.removeListener('data', this.listeners[req].onData);
|
req.removeListener('data', this.listeners[req.id].onData);
|
||||||
req.removeListener('end', this.listeners[req].onEnd);
|
req.removeListener('end', this.listeners[req.id].onEnd);
|
||||||
|
|
||||||
// Rebroadcast any events that have been buffered
|
// Rebroadcast any events that have been buffered
|
||||||
while(this.events[req].length > 0) {
|
while(this.events[req.id].length > 0) {
|
||||||
var args = this.events[req].shift();
|
var args = this.events[req.id].shift();
|
||||||
req.emit.apply(req, args);
|
req.emit.apply(req, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the data from the event and listeners hashes
|
// Remove the data from the event and listeners hashes
|
||||||
delete this.listeners[req];
|
delete this.listeners[req.id];
|
||||||
delete this.events[req];
|
delete this.events[req.id];
|
||||||
|
|
||||||
|
// If this request id is a base time, delete it
|
||||||
|
if (typeof this.collisions[req.id] !== 'undefined') {
|
||||||
|
delete this.collisions[req.id];
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
proxyRequest: function (port, server, req, res) {
|
proxyRequest: function (port, server, req, res) {
|
||||||
@ -135,6 +157,7 @@ exports.HttpProxy.prototype = {
|
|||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Add a listener for the reverse_proxy response event
|
// Add a listener for the reverse_proxy response event
|
||||||
reverse_proxy.addListener('response', function (response) {
|
reverse_proxy.addListener('response', function (response) {
|
||||||
// Set the response headers of the client response
|
// Set the response headers of the client response
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user