Skip to content

Commit b79bd29

Browse files
committed
[feature] start working on the new server
1 parent a51b062 commit b79bd29

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

lib/http-proxy.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var http = require('http'),
2121
*/
2222

2323
proxy.createProxyServer = proxy.createServer = function createProxyServer(options) {
24-
if(!options) {
24+
/* if(!options) {
2525
throw new Error([
2626
"`options` is needed and it must have the following layout:",
2727
" ",
@@ -38,25 +38,7 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
3838
" `options.target and `options.forward` cannot be ",
3939
" both missing "
4040
].join("\n"));
41-
}
41+
} */
4242

43-
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
44-
45-
return {
46-
ee : options.ee,
47-
web : httpProxy.createWebProxy(options),
48-
ws : httpProxy.createWsProxy(options),
49-
listen : function listen(port) {
50-
var server = options.ssl ? https.createServer(options.ssl, this.web) : http.createServer(this.web);
51-
52-
if(options.ws) {
53-
server.on('upgrade', this.ws);
54-
}
55-
56-
server.listen(port);
57-
58-
return server;
59-
}
60-
};
43+
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
6144
};
62-

lib/http-proxy/index.js

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
var httpProxy = exports,
22
extend = require('util')._extend,
33
parse_url = require('url').parse,
4+
EE3 = require('eventemitter3').EventEmitter,
45
web = require('./passes/web-incoming'),
56
ws = require('./passes/ws-incoming');
67

78
httpProxy.createWebProxy = createRightProxy('web');
89
httpProxy.createWsProxy = createRightProxy('ws');
10+
httpProxy.Server = ProxyServer;
911

1012
/**
1113
* Returns a function that creates the loader for
@@ -36,7 +38,6 @@ function createRightProxy(type) {
3638
var self = this,
3739
args = [].slice.call(arguments),
3840
cntr = args.length - 1,
39-
ev = 'http-proxy:' + type + ':incoming:',
4041
head;
4142

4243
if(
@@ -55,16 +56,13 @@ function createRightProxy(type) {
5556
head = args[cntr];
5657
}
5758

58-
options.ee.emit(ev + 'begin', req, res);
59-
6059
['target', 'forward'].forEach(function(e) {
6160
if (typeof options[e] === 'string')
6261
options[e] = parse_url(options[e]);
6362
});
6463

65-
passes.some(function(pass) {
66-
var evnt = ev + pass.name.toLowerCase() + ':', val;
6764

65+
for(var i=0; i < passes.length; i++) {
6866
/**
6967
* Call of passes functions
7068
* pass(req, res, options, head)
@@ -73,16 +71,38 @@ function createRightProxy(type) {
7371
* refer to the connection socket
7472
* pass(req, socket, options, head)
7573
*/
76-
77-
options.ee.emit(evnt + 'begin', req, res);
78-
val = pass(req, res, options, head);
79-
options.ee.emit(evnt + 'end');
80-
81-
return val;
82-
});
83-
84-
options.ee.emit(ev + 'end');
74+
if(passes[i](req, res, this, head)) { // passes can return a truthy value to halt the loop
75+
break;
76+
}
77+
}
8578
};
8679
};
8780
}
8881

82+
83+
function ProxyServer(options, web, ws) {
84+
this.web = web;
85+
this.ws = ws;
86+
this.options = options;
87+
}
88+
89+
ProxyServer.prototype.listen = function(port) {
90+
var self = this,
91+
closure = function(req, res) { self.web(req, res); },
92+
server = options.ssl ?
93+
https.createServer(this.options.ssl, closure) :
94+
http.createServer(closure);
95+
96+
if(options.ws) {
97+
server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
98+
}
99+
100+
server.listen(port);
101+
102+
return server;
103+
};
104+
105+
ProxyServer.prototype.before = function() {};
106+
ProxyServer.prototype.after = function() {};
107+
108+
require('util').inherits(ProxyServer, EE);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"main" : "index.js",
1212

1313
"dependencies" : {
14-
"eventemitter2" : "*"
14+
"eventemitter3" : "*"
1515
},
1616
"devDependencies": {
1717
"mocha" : "*",

0 commit comments

Comments
 (0)