Skip to content

Commit 601dbcb

Browse files
committed
[fix] refactor error handling
1 parent b79bd29 commit 601dbcb

File tree

4 files changed

+19
-48
lines changed

4 files changed

+19
-48
lines changed

lib/http-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var http = require('http'),
22
https = require('https'),
33
url = require('url'),
44
httpProxy = require('./http-proxy/'),
5-
events = require('eventemitter2'),
65
proxy = exports;
76

87
/**
@@ -42,3 +41,4 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
4241

4342
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
4443
};
44+

lib/http-proxy/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ function ProxyServer(options, web, ws) {
8989
ProxyServer.prototype.listen = function(port) {
9090
var self = this,
9191
closure = function(req, res) { self.web(req, res); },
92-
server = options.ssl ?
92+
this._server = options.ssl ?
9393
https.createServer(this.options.ssl, closure) :
9494
http.createServer(closure);
9595

9696
if(options.ws) {
97-
server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
97+
this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
9898
}
9999

100-
server.listen(port);
100+
this._server.listen(port);
101101

102-
return server;
102+
return this;
103103
};
104104

105105
ProxyServer.prototype.before = function() {};
106106
ProxyServer.prototype.after = function() {};
107107

108-
require('util').inherits(ProxyServer, EE);
108+
require('util').inherits(ProxyServer, EE3);

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

+9-32
Original file line numberDiff line numberDiff line change
@@ -89,55 +89,32 @@ web_o = Object.keys(web_o).map(function(pass) {
8989
* @api private
9090
*/
9191

92-
function stream(req, res, options) {
92+
function stream(req, res, server) {
9393
if(options.forward) {
9494
// If forward enable, so just pipe the request
95-
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
96-
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
95+
var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request(
96+
common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward')
9797
);
9898
req.pipe(forwardReq);
9999
return res.end();
100100
}
101101

102102
// Request initalization
103-
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
104-
common.setupOutgoing(options.ssl || {}, options, req)
103+
var proxyReq = (server.options.target.protocol === 'https:' ? https : http).request(
104+
common.setupOutgoing(server.options.ssl || {}, server.options, req)
105105
);
106106

107107
// Error Handler
108108
proxyReq.on('error', function(err){
109-
var ev = 'http-proxy:outgoing:web:';
110-
// If no error listeners, so throw the error.
111-
if (!options.ee.listeners(ev + 'error').length){
112-
throw err;
113-
}
114-
// Also emit the error events
115-
options.ee.emit(ev + 'error', err, req, res);
109+
server.emit('error', err);
116110
});
117111

118112
req.pipe(proxyReq);
119113

120114
proxyReq.on('response', function(proxyRes) {
121-
var ev = 'http-proxy:outgoing:web:';
122-
123-
options.ee.emit(ev + 'begin', req, res);
124-
125-
// When the previous request respond, we apply the
126-
// outgoing passes to the response
127-
web_o.some(function(pass) {
128-
var evnt = ev + pass.name.toLowerCase() + ':', val;
129-
130-
options.ee.emit(evnt + 'begin', req, res);
131-
// Call the pass with the proxy response
132-
// pass(ClientRequest, IncomingMessage, proxyResponse)
133-
val = pass(req, res, proxyRes);
134-
options.ee.emit(evnt + 'end');
135-
136-
return val;
137-
});
138-
139-
options.ee.emit(ev + 'end');
140-
115+
for(var i=0; i < web_o.length; i++) {
116+
if(web_o[i](req, res, proxyRes)) { break; }
117+
}
141118

142119
proxyRes.pipe(res);
143120
});

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,18 @@ var passes = exports;
9696
*
9797
* @api private
9898
*/
99-
function stream(req, socket, options, head) {
99+
function stream(req, socket, server, head) {
100100
common.setupSocket(socket);
101101

102102
if (head && head.length) socket.unshift(head);
103103

104104

105-
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
106-
common.setupOutgoing(options.ssl || {}, options, req)
105+
var proxyReq = (~['https:', 'wss:'].indexOf(server.options.target.protocol) ? https : http).request(
106+
common.setupOutgoing(server.options.ssl || {}, server.options, req)
107107
);
108108
// Error Handler
109109
proxyReq.on('error', function(err){
110-
var ev = 'http-proxy:outgoing:ws:';
111-
// If no error listeners, so throw the error.
112-
if (!options.ee.listeners(ev + 'error').length){
113-
throw err;
114-
}
115-
// Also emit the error events
116-
options.ee.emit(ev + 'error', err, req, socket, head);
110+
server.emit('error', err);
117111
});
118112

119113
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {

0 commit comments

Comments
 (0)