Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit cfcd232

Browse files
committed
refactor: add inline comments
1 parent 77d6949 commit cfcd232

File tree

2 files changed

+143
-24
lines changed

2 files changed

+143
-24
lines changed

index.js

+76-24
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,120 @@
11
const Toxy = require('./lib/toxy')
2-
const rules = require('./lib/rules')
3-
const poisons = require('./lib/poisons')
42

53
/**
6-
* API factory
4+
* Expose `Toxy` API factory
5+
*
6+
* @exports {Function}
77
*/
88

99
module.exports = Toxy
1010

1111
/**
12-
* Expose internal modules as static members
12+
* Exposes admin HTTP server constructor.
13+
*
14+
* @property {Function} admin
15+
* @static
1316
*/
1417

1518
Toxy.admin = require('./lib/admin')
19+
20+
/**
21+
* Exposes Rule constructor.
22+
*
23+
* @property {Rule} Rule
24+
* @static
25+
*/
26+
1627
Toxy.Rule = require('./lib/rule')
28+
29+
/**
30+
* Exposes Base object.
31+
*
32+
* @property {Object} Base
33+
* @static
34+
*/
35+
1736
Toxy.Base = require('./lib/base')
37+
38+
/**
39+
* Exposes Poison constructor.
40+
*
41+
* @property {Poison} Poison
42+
* @static
43+
*/
44+
1845
Toxy.Poison = require('./lib/poison')
46+
47+
/**
48+
* Exposes Directive constructor.
49+
*
50+
* @property {Directive} Directive
51+
* @static
52+
*/
53+
1954
Toxy.Directive = require('./lib/directive')
55+
56+
/**
57+
* Exposes Rocky proxy constructor.
58+
*
59+
* @property {Rocky} Rocky
60+
* @static
61+
*/
62+
2063
Toxy.Rocky = require('rocky').Rocky
2164

2265
/**
23-
* Expose current version
66+
* Expose current version.
67+
*
68+
* @property {String} VERSION
69+
* @static
2470
*/
2571

2672
Toxy.VERSION = require('./package.json').version
2773

2874
/**
29-
* Expose built-in poisons
75+
* Expose built-in poisons.
76+
*
77+
* @property {Object} rules
78+
* @static
3079
*/
3180

32-
Toxy.poisons = Toxy.prototype.poisons = Object.create(null)
33-
34-
poisons.forEach(function (poison) {
35-
Toxy.poisons[poison.name] = function () {
36-
return poison.apply(null, arguments)
37-
}
38-
})
81+
Toxy.poisons = Toxy.prototype.poisons
3982

4083
/**
41-
* Expose built-in rules
84+
* Expose built-in rules.
85+
*
86+
* @property {Object} rules
87+
* @static
4288
*/
4389

44-
Toxy.rules = Toxy.prototype.rules = Object.create(null)
45-
46-
rules.forEach(function (rule) {
47-
Toxy.rules[rule.name] = function () {
48-
return rule.apply(null, arguments)
49-
}
50-
})
90+
Toxy.rules = Toxy.prototype.rules
5191

5292
/**
53-
* Extend built-in rules
93+
* Attaches a new rule.
94+
*
95+
* @param {Function} poison
96+
* @method addPoison
97+
* @static
5498
*/
5599

56100
Toxy.addRule = addDirective('rules')
57101

58102
/**
59-
* Extend built-in poisons
103+
* Attaches a new poison.
104+
*
105+
* @param {Function} poison
106+
* @method addPoison
107+
* @static
60108
*/
61109

62110
Toxy.addPoison = addDirective('poisons')
63111

64112
/**
65-
* Add directive helper
113+
* Add directive helper.
114+
*
115+
* @param {String}
116+
* @function addDirective
117+
* @private
66118
*/
67119

68120
function addDirective (type) {

lib/toxy.js

+67
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const midware = require('midware')
22
const Proxy = require('./proxy')
3+
const rules = require('./lib/rules')
4+
const poisons = require('./lib/poisons')
35
const randomId = require('./helpers').randomId
46

57
module.exports = Toxy
@@ -24,17 +26,41 @@ function Toxy (opts) {
2426
setupMiddleware(this)
2527
}
2628

29+
/**
30+
* Default TCP port to listen.
31+
*
32+
* @property {Number} PORT
33+
* @static
34+
*/
35+
2736
Toxy.PORT = +process.env.PORT || 3000
2837

2938
Toxy.prototype = Object.create(Proxy.prototype)
3039

40+
/**
41+
* Starts listening on the network in the given port and host.
42+
*
43+
* @param {Number} port
44+
* @param {String} host
45+
* @method listen
46+
*/
47+
3148
Toxy.prototype.listen = function (port, host) {
3249
this.host = host
3350
this.port = +port || Toxy.PORT
3451
Proxy.prototype.listen.call(this, this.port, host)
3552
return this
3653
}
3754

55+
/**
56+
* Registers a new route in the proxy server.
57+
*
58+
* @param {String} method
59+
* @param {String} path
60+
* @return {Route}
61+
* @method route
62+
*/
63+
3864
Toxy.prototype.route = function (method, path) {
3965
const route = Proxy.prototype.route.apply(this, arguments)
4066

@@ -59,6 +85,15 @@ Toxy.prototype.route = function (method, path) {
5985
return route
6086
}
6187

88+
/**
89+
* Finds and returns an already registered route in the router stack.
90+
*
91+
* @param {String} routeId
92+
* @param {String} method
93+
* @return {Route}
94+
* @method findRoute
95+
*/
96+
6297
Toxy.prototype.findRoute = function (routeId, method) {
6398
if (method) routeId = randomId(method, routeId)
6499

@@ -71,6 +106,38 @@ Toxy.prototype.findRoute = function (routeId, method) {
71106
return routes.shift()
72107
}
73108

109+
/**
110+
* Expose built-in poisons.
111+
*
112+
* @property {Object} poisons
113+
*/
114+
115+
Toxy.prototype.poisons = Object.create(null)
116+
117+
poisons.forEach(function (poison) {
118+
Toxy.prototype.poisons[poison.name] = function () {
119+
return poison.apply(null, arguments)
120+
}
121+
})
122+
123+
/**
124+
* Expose built-in rules.
125+
*
126+
* @property {Object} rules
127+
*/
128+
129+
Toxy.prototype.rules = Object.create(null)
130+
131+
rules.forEach(function (rule) {
132+
Toxy.prototype.rules[rule.name] = function () {
133+
return rule.apply(null, arguments)
134+
}
135+
})
136+
137+
/**
138+
* Private helpers
139+
*/
140+
74141
function finalHandler (route) {
75142
var isFinalHandler = false
76143
route.use(function (req, res, next) {

0 commit comments

Comments
 (0)