Skip to content

Commit

Permalink
[feature] start working on the new server
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Oct 8, 2013
1 parent a51b062 commit b79bd29
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
24 changes: 3 additions & 21 deletions lib/http-proxy.js
Expand Up @@ -21,7 +21,7 @@ var http = require('http'),
*/

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

options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });

return {
ee : options.ee,
web : httpProxy.createWebProxy(options),
ws : httpProxy.createWsProxy(options),
listen : function listen(port) {
var server = options.ssl ? https.createServer(options.ssl, this.web) : http.createServer(this.web);

if(options.ws) {
server.on('upgrade', this.ws);
}

server.listen(port);

return server;
}
};
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
};

48 changes: 34 additions & 14 deletions lib/http-proxy/index.js
@@ -1,11 +1,13 @@
var httpProxy = exports,
extend = require('util')._extend,
parse_url = require('url').parse,
EE3 = require('eventemitter3').EventEmitter,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');

httpProxy.createWebProxy = createRightProxy('web');
httpProxy.createWsProxy = createRightProxy('ws');
httpProxy.Server = ProxyServer;

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

if(
Expand All @@ -55,16 +56,13 @@ function createRightProxy(type) {
head = args[cntr];
}

options.ee.emit(ev + 'begin', req, res);

['target', 'forward'].forEach(function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});

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

for(var i=0; i < passes.length; i++) {
/**
* Call of passes functions
* pass(req, res, options, head)
Expand All @@ -73,16 +71,38 @@ function createRightProxy(type) {
* refer to the connection socket
* pass(req, socket, options, head)
*/

options.ee.emit(evnt + 'begin', req, res);
val = pass(req, res, options, head);
options.ee.emit(evnt + 'end');

return val;
});

options.ee.emit(ev + 'end');
if(passes[i](req, res, this, head)) { // passes can return a truthy value to halt the loop
break;
}
}
};
};
}


function ProxyServer(options, web, ws) {
this.web = web;
this.ws = ws;
this.options = options;
}

ProxyServer.prototype.listen = function(port) {
var self = this,
closure = function(req, res) { self.web(req, res); },
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); });
}

server.listen(port);

return server;
};

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

require('util').inherits(ProxyServer, EE);
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"main" : "index.js",

"dependencies" : {
"eventemitter2" : "*"
"eventemitter3" : "*"
},
"devDependencies": {
"mocha" : "*",
Expand Down

0 comments on commit b79bd29

Please sign in to comment.