[doc] added some documentation to functions and comments to understand better the code

This commit is contained in:
cronopio 2013-09-20 19:28:53 -05:00
parent 69f126b34c
commit 5dcdf2b36c
5 changed files with 100 additions and 2 deletions

View File

@ -37,6 +37,23 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
return outgoing; return outgoing;
}; };
/**
* Set the proper configuration for sockets,
* set no delay and set keep alive, also set
* the timeout to 0.
*
* Examples:
*
* common.setupSocket(socket)
* // => Socket
*
* @param {Socket} Socket instance to setup
* 
* @return {Socket} Return the configured socket.
*
* @api private
*/
common.setupSocket = function(socket) { common.setupSocket = function(socket) {
socket.setTimeout(0); socket.setTimeout(0);
socket.setNoDelay(true); socket.setNoDelay(true);

View File

@ -65,6 +65,15 @@ function createRightProxy(type) {
passes.some(function(pass) { passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':'; var evnt = ev + pass.name.toLowerCase() + ':';
/**
* Call of passes functions
* pass(req, res, options, head)
*
* In WebSockets case the `res` variable
* refer to the connection socket
* pass(req, socket, options, head)
*/
options.ee.emit(evnt + 'begin', req, res); options.ee.emit(evnt + 'begin', req, res);
var val = pass(req, res, options, head); var val = pass(req, res, options, head);

View File

@ -91,6 +91,7 @@ function XHeaders(req, res, options) {
function stream(req, res, options) { function stream(req, res, options) {
if(options.forward) { if(options.forward) {
// If forward enable, so just pipe the request
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request( var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req, 'forward') common.setupOutgoing(options.ssl || {}, options, req, 'forward')
); );
@ -98,15 +99,19 @@ function stream(req, res, options) {
return res.end(); return res.end();
} }
// Request initalization
var proxyReq = (options.target.protocol === 'https:' ? https : http).request( var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req) common.setupOutgoing(options.ssl || {}, options, req)
); );
// Error Handler
proxyReq.on('error', function(err){ proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:web:'; var ev = 'caronte:outgoing:web:';
// If no error listeners, so throw the error.
if (options.ee.listeners(ev + 'error').length == 0){ if (options.ee.listeners(ev + 'error').length == 0){
throw err; throw err;
} }
// Also emit the error events
options.ee.emit(ev + 'error', err, req, res); options.ee.emit(ev + 'error', err, req, res);
}); });
@ -117,10 +122,14 @@ function stream(req, res, options) {
options.ee.emit(ev + 'begin', req, res); options.ee.emit(ev + 'begin', req, res);
// When the previous request respond, we apply the
// outgoing passes to the response
web_o.some(function(pass) { web_o.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':'; var evnt = ev + pass.name.toLowerCase() + ':';
options.ee.emit(evnt + 'begin', req, res); options.ee.emit(evnt + 'begin', req, res);
// Call the pass with the proxy response
// pass(ClientRequest, IncomingMessage, proxyResponse)
var val = pass(req, res, proxyRes); var val = pass(req, res, proxyRes);
options.ee.emit(evnt + 'end'); options.ee.emit(evnt + 'end');

View File

@ -10,12 +10,31 @@ var passes = exports;
[ // <-- [ // <--
/**
* If is a HTTP 1.0 request, remove chunk headers
*
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {proxyResponse} Res Response object from the proxy request
*
* @api private
*/
function removeChunked(req, res, proxyRes) { function removeChunked(req, res, proxyRes) {
if(req.httpVersion === '1.0') { if(req.httpVersion === '1.0') {
delete proxyRes.headers['transfer-encoding']; delete proxyRes.headers['transfer-encoding'];
} }
}, },
/**
* If is a HTTP 1.0 request, set the correct connection header
* or if connection header not present, then use `keep-alive`
*
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {proxyResponse} Res Response object from the proxy request
*
* @api private
*/
function setConnection(req, res, proxyRes) { function setConnection(req, res, proxyRes) {
if (req.httpVersion === '1.0') { if (req.httpVersion === '1.0') {
if (req.headers.connection) { if (req.headers.connection) {
@ -31,12 +50,31 @@ var passes = exports;
} }
}, },
/**
* Copy headers from proxyResponse to response
* set each header in response object.
*
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {proxyResponse} Res Response object from the proxy request
*
* @api private
*/
function writeHeaders(req, res, proxyRes) { function writeHeaders(req, res, proxyRes) {
Object.keys(proxyRes.headers).forEach(function(key) { Object.keys(proxyRes.headers).forEach(function(key) {
res.setHeader(key, proxyRes.headers[key]); res.setHeader(key, proxyRes.headers[key]);
}); });
}, },
/**
* Set the statusCode from the proxyResponse
*
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {proxyResponse} Res Response object from the proxy request
*
* @api private
*/
function writeStatusCode(req, res, proxyRes) { function writeStatusCode(req, res, proxyRes) {
res.writeHead(proxyRes.statusCode); res.writeHead(proxyRes.statusCode);
} }

View File

@ -22,6 +22,11 @@ var passes = exports;
/** /**
* WebSocket requests must have the `GET` method and * WebSocket requests must have the `GET` method and
* the `upgrade:websocket` header * the `upgrade:websocket` header
*
* @param {ClientRequest} Req Request object
* @param {Socket} Websocket
*
* @api private
*/ */
function checkMethodAndHeader (req, socket) { function checkMethodAndHeader (req, socket) {
@ -35,8 +40,14 @@ function checkMethodAndHeader (req, socket) {
}, },
/** /**
* Setup socket * Set the proper configuration for sockets,
* set no delay and set keep alive, also set
* the timeout to 0.
* *
* @param {ClientRequest} Req Request object
* @param {Socket} Websocket
*
* @api private
*/ */
function setupSocket(req, socket) { function setupSocket(req, socket) {
@ -49,6 +60,11 @@ function setupSocket(req, socket) {
/** /**
* Sets `x-forwarded-*` headers if specified in config. * Sets `x-forwarded-*` headers if specified in config.
* *
* @param {ClientRequest} Req Request object
* @param {Socket} Websocket
* @param {Object} Options Config object passed to the proxy
*
* @api private
*/ */
function XHeaders(req, socket, options) { function XHeaders(req, socket, options) {
@ -69,8 +85,14 @@ function XHeaders(req, socket, options) {
}, },
/** /**
* Does the actual proxying. Make the request and upgrade it
* send the Switching Protocols request and pipe the sockets.
* *
* @param {ClientRequest} Req Request object
* @param {Socket} Websocket
* @param {Object} Options Config object passed to the proxy
* *
* @api private
*/ */
function stream(req, socket, options, head) { function stream(req, socket, options, head) {
common.setupSocket(socket); common.setupSocket(socket);
@ -81,11 +103,14 @@ function stream(req, socket, options, head) {
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request( var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req) common.setupOutgoing(options.ssl || {}, options, req)
); );
// Error Handler
proxyReq.on('error', function(err){ proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:ws:'; var ev = 'caronte:outgoing:ws:';
// If no error listeners, so throw the error.
if (options.ee.listeners(ev + 'error').length == 0){ if (options.ee.listeners(ev + 'error').length == 0){
throw err; throw err;
} }
// Also emit the error events
options.ee.emit(ev + 'error', err, req, socket, head); options.ee.emit(ev + 'error', err, req, socket, head);
}); });