From 8273cb6461e4d33f36e583b0354d1bea038d0a56 Mon Sep 17 00:00:00 2001 From: yawnt Date: Sat, 3 Aug 2013 08:47:07 +0200 Subject: [PATCH] [refactor] move to leaner architecture --- index.js | 9 ++++++ lib/caronte.js | 6 ++-- lib/caronte/index.js | 36 +++++++++++++++++++-- lib/caronte/{web/index.js => passes/web.js} | 15 +++++---- lib/caronte/web.js | 25 -------------- 5 files changed, 53 insertions(+), 38 deletions(-) rename lib/caronte/{web/index.js => passes/web.js} (89%) delete mode 100644 lib/caronte/web.js diff --git a/index.js b/index.js index 7007beda1..5c73968f3 100644 --- a/index.js +++ b/index.js @@ -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'); \ No newline at end of file diff --git a/lib/caronte.js b/lib/caronte.js index 97e2411b6..a06306008 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -33,9 +33,9 @@ proxy.createProxyServer = function createProxyServer(options) { " xfwd : ", " } ", " ", - "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")); } diff --git a/lib/caronte/index.js b/lib/caronte/index.js index 25b4984c8..7304850d1 100644 --- a/lib/caronte/index.js +++ b/lib/caronte/index.js @@ -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'); \ No newline at end of file diff --git a/lib/caronte/web/index.js b/lib/caronte/passes/web.js similarity index 89% rename from lib/caronte/web/index.js rename to lib/caronte/passes/web.js index f0d9426ea..be6753ccc 100644 --- a/lib/caronte/web/index.js +++ b/lib/caronte/passes/web.js @@ -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']) { @@ -53,4 +49,9 @@ function stream(req, res, options) { } res.end(); -} \ No newline at end of file +} + +] // <-- + .forEach(function(func) { + passes[func.name] = func; + }); \ No newline at end of file diff --git a/lib/caronte/web.js b/lib/caronte/web.js deleted file mode 100644 index 3c76b3ab1..000000000 --- a/lib/caronte/web.js +++ /dev/null @@ -1,25 +0,0 @@ -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'); - }; -}; \ No newline at end of file