mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[refactor] move to leaner architecture
This commit is contained in:
parent
4d13156721
commit
8273cb6461
9
index.js
9
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');
|
||||
@ -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"));
|
||||
}
|
||||
|
||||
|
||||
@ -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');
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
] // <--
|
||||
.forEach(function(func) {
|
||||
passes[func.name] = func;
|
||||
});
|
||||
@ -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');
|
||||
};
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user