Skip to content

Commit 5dcdf2b

Browse files
committed
[doc] added some documentation to functions and comments to understand better the code
1 parent 69f126b commit 5dcdf2b

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

lib/caronte/common.js

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
3737
return outgoing;
3838
};
3939

40+
/**
41+
* Set the proper configuration for sockets,
42+
* set no delay and set keep alive, also set
43+
* the timeout to 0.
44+
*
45+
* Examples:
46+
*
47+
* common.setupSocket(socket)
48+
* // => Socket
49+
*
50+
* @param {Socket} Socket instance to setup
51+
52+
* @return {Socket} Return the configured socket.
53+
*
54+
* @api private
55+
*/
56+
4057
common.setupSocket = function(socket) {
4158
socket.setTimeout(0);
4259
socket.setNoDelay(true);

lib/caronte/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ function createRightProxy(type) {
6565

6666
passes.some(function(pass) {
6767
var evnt = ev + pass.name.toLowerCase() + ':';
68+
69+
/**
70+
* Call of passes functions
71+
* pass(req, res, options, head)
72+
*
73+
* In WebSockets case the `res` variable
74+
* refer to the connection socket
75+
* pass(req, socket, options, head)
76+
*/
6877

6978
options.ee.emit(evnt + 'begin', req, res);
7079
var val = pass(req, res, options, head);

lib/caronte/passes/web-incoming.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,27 @@ function XHeaders(req, res, options) {
9191

9292
function stream(req, res, options) {
9393
if(options.forward) {
94+
// If forward enable, so just pipe the request
9495
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
9596
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
9697
);
9798
req.pipe(forwardReq);
9899
return res.end();
99100
}
100101

102+
// Request initalization
101103
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
102104
common.setupOutgoing(options.ssl || {}, options, req)
103105
);
104106

107+
// Error Handler
105108
proxyReq.on('error', function(err){
106109
var ev = 'caronte:outgoing:web:';
110+
// If no error listeners, so throw the error.
107111
if (options.ee.listeners(ev + 'error').length == 0){
108112
throw err;
109113
}
114+
// Also emit the error events
110115
options.ee.emit(ev + 'error', err, req, res);
111116
});
112117

@@ -117,10 +122,14 @@ function stream(req, res, options) {
117122

118123
options.ee.emit(ev + 'begin', req, res);
119124

125+
// When the previous request respond, we apply the
126+
// outgoing passes to the response
120127
web_o.some(function(pass) {
121128
var evnt = ev + pass.name.toLowerCase() + ':';
122129

123-
options.ee.emit(evnt + 'begin', req, res);
130+
options.ee.emit(evnt + 'begin', req, res);
131+
// Call the pass with the proxy response
132+
// pass(ClientRequest, IncomingMessage, proxyResponse)
124133
var val = pass(req, res, proxyRes);
125134
options.ee.emit(evnt + 'end');
126135

lib/caronte/passes/web-outgoing.js

+38
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,31 @@ var passes = exports;
1010

1111
[ // <--
1212

13+
/**
14+
* If is a HTTP 1.0 request, remove chunk headers
15+
*
16+
* @param {ClientRequest} Req Request object
17+
* @param {IncomingMessage} Res Response object
18+
* @param {proxyResponse} Res Response object from the proxy request
19+
*
20+
* @api private
21+
*/
1322
function removeChunked(req, res, proxyRes) {
1423
if(req.httpVersion === '1.0') {
1524
delete proxyRes.headers['transfer-encoding'];
1625
}
1726
},
1827

28+
/**
29+
* If is a HTTP 1.0 request, set the correct connection header
30+
* or if connection header not present, then use `keep-alive`
31+
*
32+
* @param {ClientRequest} Req Request object
33+
* @param {IncomingMessage} Res Response object
34+
* @param {proxyResponse} Res Response object from the proxy request
35+
*
36+
* @api private
37+
*/
1938
function setConnection(req, res, proxyRes) {
2039
if (req.httpVersion === '1.0') {
2140
if (req.headers.connection) {
@@ -31,12 +50,31 @@ var passes = exports;
3150
}
3251
},
3352

53+
/**
54+
* Copy headers from proxyResponse to response
55+
* set each header in response object.
56+
*
57+
* @param {ClientRequest} Req Request object
58+
* @param {IncomingMessage} Res Response object
59+
* @param {proxyResponse} Res Response object from the proxy request
60+
*
61+
* @api private
62+
*/
3463
function writeHeaders(req, res, proxyRes) {
3564
Object.keys(proxyRes.headers).forEach(function(key) {
3665
res.setHeader(key, proxyRes.headers[key]);
3766
});
3867
},
3968

69+
/**
70+
* Set the statusCode from the proxyResponse
71+
*
72+
* @param {ClientRequest} Req Request object
73+
* @param {IncomingMessage} Res Response object
74+
* @param {proxyResponse} Res Response object from the proxy request
75+
*
76+
* @api private
77+
*/
4078
function writeStatusCode(req, res, proxyRes) {
4179
res.writeHead(proxyRes.statusCode);
4280
}

lib/caronte/passes/ws-incoming.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ var passes = exports;
2222
/**
2323
* WebSocket requests must have the `GET` method and
2424
* the `upgrade:websocket` header
25+
*
26+
* @param {ClientRequest} Req Request object
27+
* @param {Socket} Websocket
28+
*
29+
* @api private
2530
*/
2631

2732
function checkMethodAndHeader (req, socket) {
@@ -35,8 +40,14 @@ function checkMethodAndHeader (req, socket) {
3540
},
3641

3742
/**
38-
* Setup socket
43+
* Set the proper configuration for sockets,
44+
* set no delay and set keep alive, also set
45+
* the timeout to 0.
3946
*
47+
* @param {ClientRequest} Req Request object
48+
* @param {Socket} Websocket
49+
*
50+
* @api private
4051
*/
4152

4253
function setupSocket(req, socket) {
@@ -49,6 +60,11 @@ function setupSocket(req, socket) {
4960
/**
5061
* Sets `x-forwarded-*` headers if specified in config.
5162
*
63+
* @param {ClientRequest} Req Request object
64+
* @param {Socket} Websocket
65+
* @param {Object} Options Config object passed to the proxy
66+
*
67+
* @api private
5268
*/
5369

5470
function XHeaders(req, socket, options) {
@@ -69,8 +85,14 @@ function XHeaders(req, socket, options) {
6985
},
7086

7187
/**
88+
* Does the actual proxying. Make the request and upgrade it
89+
* send the Switching Protocols request and pipe the sockets.
7290
*
91+
* @param {ClientRequest} Req Request object
92+
* @param {Socket} Websocket
93+
* @param {Object} Options Config object passed to the proxy
7394
*
95+
* @api private
7496
*/
7597
function stream(req, socket, options, head) {
7698
common.setupSocket(socket);
@@ -81,11 +103,14 @@ function stream(req, socket, options, head) {
81103
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
82104
common.setupOutgoing(options.ssl || {}, options, req)
83105
);
106+
// Error Handler
84107
proxyReq.on('error', function(err){
85108
var ev = 'caronte:outgoing:ws:';
109+
// If no error listeners, so throw the error.
86110
if (options.ee.listeners(ev + 'error').length == 0){
87111
throw err;
88112
}
113+
// Also emit the error events
89114
options.ee.emit(ev + 'error', err, req, socket, head);
90115
});
91116

0 commit comments

Comments
 (0)