Skip to content

Commit

Permalink
[refactor] move to leaner architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Aug 3, 2013
1 parent 4d13156 commit 8273cb6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
/*!
*
* Charon the demon, with the eyes of glede,
* Beckoning to them, collects them all together,
* Beats with his oar whoever lags behind
* Dante - The Divine Comedy (Canto III)
*
*/

module.exports = require('./lib/caronte');
6 changes: 3 additions & 3 deletions lib/caronte.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ proxy.createProxyServer = function createProxyServer(options) {
" 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 "
"NOTE: `options.ws` and `options.ssl` are optional. ",
" `options.target and `options.forward` cannot be ",
" both missing "
].join("\n"));
}

Expand Down
36 changes: 33 additions & 3 deletions lib/caronte/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
var caronte = exports;
var caronte = exports,
web = require('./passes/web');
ws = require('./passes/ws');

caronte.createWebProxy = createRightProxy('web');
caronte.createWsProxy = createRightProxy('ws');

function createRightProxy(type) {
passes = type === 'ws' ? ws : web;
return function(options) {

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

return function(req, res) {
var self = this,
ev = 'caronte:' + type + ':';

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

passes.forEach(function(pass) {
var event = ev + pass.name.toLowerCase();

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

self.emit(ev + 'end');
};
};
}

caronte.createWebProxy = require('./web');
caronte.createWsProxy = require('./ws');
15 changes: 8 additions & 7 deletions lib/caronte/web/index.js → lib/caronte/passes/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ var ForwardStream = require('../streams/forward'),
passes = exports;

/*!
* List of passes.
* Array 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']) {
Expand Down Expand Up @@ -53,4 +49,9 @@ function stream(req, res, options) {
}

res.end();
}
}

] // <--
.forEach(function(func) {
passes[func.name] = func;
});
25 changes: 0 additions & 25 deletions lib/caronte/web.js

This file was deleted.

0 comments on commit 8273cb6

Please sign in to comment.