diff --git a/lib/http-proxy.js b/lib/http-proxy.js index d84772a..9456443 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -2,7 +2,6 @@ var http = require('http'), https = require('https'), url = require('url'), httpProxy = require('./http-proxy/'), - events = require('eventemitter2'), proxy = exports; /** @@ -42,3 +41,4 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options)); }; + diff --git a/lib/http-proxy/index.js b/lib/http-proxy/index.js index 9a2798f..5c6e7e0 100644 --- a/lib/http-proxy/index.js +++ b/lib/http-proxy/index.js @@ -89,20 +89,20 @@ function ProxyServer(options, web, ws) { ProxyServer.prototype.listen = function(port) { var self = this, closure = function(req, res) { self.web(req, res); }, - server = options.ssl ? + this._server = options.ssl ? https.createServer(this.options.ssl, closure) : http.createServer(closure); if(options.ws) { - server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); }); + this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); }); } - server.listen(port); + this._server.listen(port); - return server; + return this; }; ProxyServer.prototype.before = function() {}; ProxyServer.prototype.after = function() {}; -require('util').inherits(ProxyServer, EE); +require('util').inherits(ProxyServer, EE3); diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 8f5afae..13d1c01 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -89,55 +89,32 @@ web_o = Object.keys(web_o).map(function(pass) { * @api private */ - function stream(req, res, options) { + function stream(req, res, server) { if(options.forward) { // If forward enable, so just pipe the request - var forwardReq = (options.forward.protocol === 'https:' ? https : http).request( - common.setupOutgoing(options.ssl || {}, options, req, 'forward') + var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request( + common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward') ); req.pipe(forwardReq); return res.end(); } // Request initalization - var proxyReq = (options.target.protocol === 'https:' ? https : http).request( - common.setupOutgoing(options.ssl || {}, options, req) + var proxyReq = (server.options.target.protocol === 'https:' ? https : http).request( + common.setupOutgoing(server.options.ssl || {}, server.options, req) ); // Error Handler proxyReq.on('error', function(err){ - var ev = 'http-proxy:outgoing:web:'; - // If no error listeners, so throw the error. - if (!options.ee.listeners(ev + 'error').length){ - throw err; - } - // Also emit the error events - options.ee.emit(ev + 'error', err, req, res); + server.emit('error', err); }); req.pipe(proxyReq); proxyReq.on('response', function(proxyRes) { - var ev = 'http-proxy:outgoing:web:'; - - options.ee.emit(ev + 'begin', req, res); - - // When the previous request respond, we apply the - // outgoing passes to the response - web_o.some(function(pass) { - var evnt = ev + pass.name.toLowerCase() + ':', val; - - options.ee.emit(evnt + 'begin', req, res); - // Call the pass with the proxy response - // pass(ClientRequest, IncomingMessage, proxyResponse) - val = pass(req, res, proxyRes); - options.ee.emit(evnt + 'end'); - - return val; - }); - - options.ee.emit(ev + 'end'); - + for(var i=0; i < web_o.length; i++) { + if(web_o[i](req, res, proxyRes)) { break; } + } proxyRes.pipe(res); }); diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 070cefb..47ed638 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -96,24 +96,18 @@ var passes = exports; * * @api private */ - function stream(req, socket, options, head) { + function stream(req, socket, server, head) { common.setupSocket(socket); if (head && head.length) socket.unshift(head); - var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request( - common.setupOutgoing(options.ssl || {}, options, req) + var proxyReq = (~['https:', 'wss:'].indexOf(server.options.target.protocol) ? https : http).request( + common.setupOutgoing(server.options.ssl || {}, server.options, req) ); // Error Handler proxyReq.on('error', function(err){ - var ev = 'http-proxy:outgoing:ws:'; - // If no error listeners, so throw the error. - if (!options.ee.listeners(ev + 'error').length){ - throw err; - } - // Also emit the error events - options.ee.emit(ev + 'error', err, req, socket, head); + server.emit('error', err); }); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {