Skip to content

Commit 8273cb6

Browse files
committed
[refactor] move to leaner architecture
1 parent 4d13156 commit 8273cb6

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
/*!
2+
*
3+
* Charon the demon, with the eyes of glede,
4+
* Beckoning to them, collects them all together,
5+
* Beats with his oar whoever lags behind
6+
* Dante - The Divine Comedy (Canto III)
7+
*
8+
*/
9+
110
module.exports = require('./lib/caronte');

lib/caronte.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ proxy.createProxyServer = function createProxyServer(options) {
3333
" xfwd : <true/false, adds x-forward headers> ",
3434
" } ",
3535
" ",
36-
"NOTE: `options.ws` and `options.ssl` are optional ",
37-
" either one or both `options.target` and ",
38-
" `options.forward` must exist "
36+
"NOTE: `options.ws` and `options.ssl` are optional. ",
37+
" `options.target and `options.forward` cannot be ",
38+
" both missing "
3939
].join("\n"));
4040
}
4141

lib/caronte/index.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
1-
var caronte = exports;
1+
var caronte = exports,
2+
web = require('./passes/web');
3+
ws = require('./passes/ws');
4+
5+
caronte.createWebProxy = createRightProxy('web');
6+
caronte.createWsProxy = createRightProxy('ws');
7+
8+
function createRightProxy(type) {
9+
passes = type === 'ws' ? ws : web;
10+
return function(options) {
11+
12+
passes = Object.keys(passes).map(function(pass) {
13+
return passes[pass];
14+
});
15+
16+
return function(req, res) {
17+
var self = this,
18+
ev = 'caronte:' + type + ':';
19+
20+
self.emit(ev + 'begin', req, res);
21+
22+
passes.forEach(function(pass) {
23+
var event = ev + pass.name.toLowerCase();
24+
25+
self.emit(event + 'begin', req, res);
26+
pass(req, res, options);
27+
self.emit(event + 'end');
28+
});
29+
30+
self.emit(ev + 'end');
31+
};
32+
};
33+
}
234

3-
caronte.createWebProxy = require('./web');
4-
caronte.createWsProxy = require('./ws');

lib/caronte/web/index.js renamed to lib/caronte/passes/web.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ var ForwardStream = require('../streams/forward'),
33
passes = exports;
44

55
/*!
6-
* List of passes.
6+
* Array of passes.
77
*
88
* A `pass` is just a function that is executed on `req, res, options`
99
* so that you can easily add new checks while still keeping the base
1010
* flexible.
11-
*
1211
*/
1312

14-
passes.XHeaders = XHeaders;
15-
passes.deleteLength = deleteLength;
16-
passes.timeout = timeout;
17-
passes.stream = stream;
13+
[ // <--
1814

1915
function deleteLength(req, res, options) {
2016
if(req.method === 'DELETE' && !req.headers['content-length']) {
@@ -53,4 +49,9 @@ function stream(req, res, options) {
5349
}
5450

5551
res.end();
56-
}
52+
}
53+
54+
] // <--
55+
.forEach(function(func) {
56+
passes[func.name] = func;
57+
});

lib/caronte/web.js

-25
This file was deleted.

0 commit comments

Comments
 (0)