Skip to content

Commit d05af4a

Browse files
committed
[refactor docs] add descriptions
1 parent 8273cb6 commit d05af4a

File tree

4 files changed

+170
-2
lines changed

4 files changed

+170
-2
lines changed

Diff for: lib/caronte/common.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var common = exports;
2+
3+
/**
4+
* Copies the right headers from `options` and `req` to
5+
* `outgoing` which is then used to fire the proxied
6+
* request.
7+
*
8+
* Examples:
9+
*
10+
* common.setupOutgoing(outgoing, options, req)
11+
* // => { host: ..., hostname: ...}
12+
*
13+
* @param {Object} Outgoing Base object to be filled with required properties
14+
* @param {Object} Options Config object passed to the proxy
15+
* @param {ClientRequest} Req Request Object
16+
17+
* @return {Object} Outgoing Object with all required properties set
18+
*
19+
* @api private
20+
*/
21+
22+
common.setupOutgoing = function(outgoing, options, req) {
23+
['host', 'hostname', 'port', 'socketPath', 'agent'].forEach(
24+
function(e) { outgoing[e] = options[e]; }
25+
);
26+
27+
['method', 'path', 'headers'].forEach(
28+
function(e) { outgoing[e] = req[e]; }
29+
);
30+
31+
return outgoing;
32+
};

Diff for: lib/caronte/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ var caronte = exports,
55
caronte.createWebProxy = createRightProxy('web');
66
caronte.createWsProxy = createRightProxy('ws');
77

8+
/**
9+
* Returns a function that creates the loader for
10+
* either `ws` or `web`'s passes.
11+
*
12+
* Examples:
13+
*
14+
* caronte.createRightProxy('ws')
15+
* // => [Function]
16+
*
17+
* @param {String} Type Either 'ws' or 'web'
18+
19+
* @return {Function} Loader Function that when called returns an iterator for the right passes
20+
*
21+
* @api private
22+
*/
23+
824
function createRightProxy(type) {
925
passes = type === 'ws' ? ws : web;
1026
return function(options) {

Diff for: lib/caronte/passes/web.js

+44
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,51 @@ var ForwardStream = require('../streams/forward'),
1212

1313
[ // <--
1414

15+
/**
16+
* Sets `content-length` to '0' if request is of DELETE type.
17+
*
18+
* @param {ClientRequest} Req Request object
19+
* @param {IncomingMessage} Res Response object
20+
* @param {Object} Options Config object passed to the proxy
21+
*
22+
* @api private
23+
*/
24+
1525
function deleteLength(req, res, options) {
1626
if(req.method === 'DELETE' && !req.headers['content-length']) {
1727
req.headers['content-length'] = '0';
1828
}
1929
}
2030

31+
/**
32+
* Sets timeout in request socket if it was specified in options.
33+
*
34+
* @param {ClientRequest} Req Request object
35+
* @param {IncomingMessage} Res Response object
36+
* @param {Object} Options Config object passed to the proxy
37+
*
38+
* @api private
39+
*/
40+
2141
function timeout(req, res, options) {
2242
if(options.timeout) {
2343
req.socket.setTimeout(options.timeout);
2444
}
2545
}
2646

47+
/**
48+
* Sets `x-forwarded-*` headers if specified in config.
49+
*
50+
* @param {ClientRequest} Req Request object
51+
* @param {IncomingMessage} Res Response object
52+
* @param {Object} Options Config object passed to the proxy
53+
*
54+
* @api private
55+
*/
56+
2757
function XHeaders(req, res, options) {
58+
if(!options.xfwd) return;
59+
2860
var values = {
2961
for : req.connection.remoteAddress || req.socket.remoteAddress,
3062
port : req.connection.remotePort || req.socket.remotePort,
@@ -39,6 +71,18 @@ function XHeaders(req, res, options) {
3971
});
4072
}
4173

74+
/**
75+
* Does the actual proxying. If `forward` is enabled fires up
76+
* a ForwardStream, same happens for ProxyStream. The request
77+
* just dies otherwise.
78+
*
79+
* @param {ClientRequest} Req Request object
80+
* @param {IncomingMessage} Res Response object
81+
* @param {Object} Options Config object passed to the proxy
82+
*
83+
* @api private
84+
*/
85+
4286
function stream(req, res, options) {
4387
if(options.forward) {
4488
req.pipe(new ForwardStream(options.forward));

Diff for: lib/caronte/streams/forward.js

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1+
var Writable = require('stream').Writable,
2+
common = require('../common'),
3+
http = require('http'),
4+
https = require('https');
5+
6+
module.exports = ForwardStream;
7+
8+
/**
9+
* Forwards the request to the external target specified in options
10+
*
11+
* Examples:
12+
*
13+
* new ForwardStream(options)
14+
* // => { ... }
15+
*
16+
* @param {Object} Options Config object passed to the proxy
17+
18+
* @return {ForwardStream} Stream A clone of ForwardStream
19+
*
20+
* @api private
21+
*/
22+
123
function ForwardStream() {
2-
3-
}
24+
Writable.call(this);
25+
26+
this.once('pipe', this.onPipe);
27+
this.once('finish', this.onFinish);
28+
}
29+
30+
/**
31+
* Fires up the request to the external target
32+
*
33+
* Examples:
34+
*
35+
* (new ForwardStream(options)).onPipe(req)
36+
* // => undefined
37+
*
38+
* @param {HttpRequest} Req Request object
39+
*
40+
* @api private
41+
*/
42+
43+
ForwardStream.prototype.onPipe = function(request) {
44+
this.forwardReq = (options.ssl ? https : http).request(
45+
common.setupOutgoing(options.ssl || {}, options, request);
46+
);
47+
};
48+
49+
/**
50+
* Closes forwarded request when `pipe` is finished
51+
*
52+
* Examples:
53+
*
54+
* (new ForwardStream(options)).onFinish()
55+
* // => undefined
56+
*
57+
* @api private
58+
*/
59+
60+
ForwardStream.prototype.onFinish = function() {
61+
this.forwardReq.end();
62+
};
63+
64+
/**
65+
* Implements `stream.Writable`, writes to the forwarded request
66+
*
67+
* Examples:
68+
*
69+
* (new ForwardStream(options))._write(chunk, encoding, clb)
70+
* // => undefined
71+
*
72+
* @api private
73+
*/
74+
75+
ForwardStream.prototype._write = function(chunk, encoding, clb) {
76+
this.forwardReq.write(chunk, encoding, clb);
77+
};
78+
79+
require('util').inherits(ForwardStream, Writable);

0 commit comments

Comments
 (0)