Skip to content

Commit

Permalink
[dist] first
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Aug 1, 2013
0 parents commit 4d13156
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.swp
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/caronte');
61 changes: 61 additions & 0 deletions lib/caronte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var http = require('http'),
https = require('https'),
url = require('url'),
caronte = require('./caronte'),
events = require('eventemitter2'),
proxy = exports;

/**
* Creates the proxy server.
*
* Examples:
*
* caronte.createProxyServer({ .. }, 8000)
* // => '{ web: [Function], ws: [Function] ... }'
*
* @param {Object} Options Config object passed to the proxy
*
* @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests
*
* @api public
*/

proxy.createProxyServer = function createProxyServer(options) {
if(!options) {
throw new Error([
"`options` is needed and it must have the following layout:",
" ",
" { ",
" target : <url string to be parsed with the url module> ",
" forward: <url string to be parsed with the url module> ",
" ssl : <object to be passed to https.createServer()> ",
" ws : <true/false, if you want to proxy websockets> ",
" xfwd : <true/false, adds x-forward headers> ",
" } ",
" ",
"NOTE: `options.ws` and `options.ssl` are optional ",
" either one or both `options.target` and ",
" `options.forward` must exist "
].join("\n"));
}

['target', 'forward'].forEach(function(key) {
options[key] = url.parse(options[key]);
});

return {
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }),
web : caronte.createWebProxy(options),
ws : caronte.createWsProxy(options),
listen : function listen(port) {
var server = options.ssl ? http.createServer(this.web) : https.createServer(options.ssl, this.web);

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

return server;
}
};
};

4 changes: 4 additions & 0 deletions lib/caronte/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var caronte = exports;

caronte.createWebProxy = require('./web');
caronte.createWsProxy = require('./ws');
3 changes: 3 additions & 0 deletions lib/caronte/streams/forward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function ForwardStream() {

}
3 changes: 3 additions & 0 deletions lib/caronte/streams/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function ProxyStream() {

}
25 changes: 25 additions & 0 deletions lib/caronte/web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var passes = require('./web/');

module.exports = createWebProxy;

function createWebProxy(options) {
passes = Object.keys(passes).map(function(pass) {
return passes[pass];
});

return function(req, res) {
var self = this;

self.emit('caronte:web:begin', req, res);

passes.forEach(function(pass) {
var event = 'caronte:web:' + pass.name.toLowerCase();

self.emit(event + ':begin', req, res);
pass(req, res, options);
self.emit(event + ':end');
});

self.emit('caronte:web:end');
};
};
56 changes: 56 additions & 0 deletions lib/caronte/web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var ForwardStream = require('../streams/forward'),
ProxyStream = require('../streams/proxy'),
passes = exports;

/*!
* List of passes.
*
* A `pass` is just a function that is executed on `req, res, options`
* so that you can easily add new checks while still keeping the base
* flexible.
*
*/

passes.XHeaders = XHeaders;
passes.deleteLength = deleteLength;
passes.timeout = timeout;
passes.stream = stream;

function deleteLength(req, res, options) {
if(req.method === 'DELETE' && !req.headers['content-length']) {
req.headers['content-length'] = '0';
}
}

function timeout(req, res, options) {
if(options.timeout) {
req.socket.setTimeout(options.timeout);
}
}

function XHeaders(req, res, options) {
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http')
};

['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header]
});
}

function stream(req, res, options) {
if(options.forward) {
req.pipe(new ForwardStream(options.forward));
}

if(options.target) {
return req.pipe(new ProxyStream(res, options)).pipe(res);
}

res.end();
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name" : "caronte",
"version" : "0.0.0",
"description" : "HTTP proxying for the masses",
"author" : "yawnt <yawn.localhost@gmail.com>",

"main" : "index.js",

"dependencies" : {
"eventemitter2": "*"
},
"devDependencies": {
"mocha" : "*",
"expect.js": "*",
"dox" : "*"
},
"scripts" : {
"test" : "mocha -t 20000 -R spec -r expect test/*-test.js"
},

"license" : "MIT"
}

0 comments on commit 4d13156

Please sign in to comment.