http-proxy-middleware 0.17.3 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -1
- package/README.md +284 -240
- package/index.js +3 -3
- package/lib/config-factory.js +74 -71
- package/lib/context-matcher.js +46 -46
- package/lib/errors.js +12 -0
- package/lib/handlers.js +59 -51
- package/lib/index.js +167 -132
- package/lib/logger.js +130 -116
- package/lib/path-rewriter.js +57 -50
- package/lib/router.js +37 -39
- package/package.json +36 -15
package/lib/path-rewriter.js
CHANGED
|
@@ -1,72 +1,79 @@
|
|
|
1
|
-
var _
|
|
2
|
-
var logger = require('./logger').getInstance()
|
|
1
|
+
var _ = require('lodash')
|
|
2
|
+
var logger = require('./logger').getInstance()
|
|
3
|
+
var ERRORS = require('./errors')
|
|
3
4
|
|
|
4
5
|
module.exports = {
|
|
5
|
-
|
|
6
|
-
}
|
|
6
|
+
create: createPathRewriter
|
|
7
|
+
}
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Create rewrite function, to cache parsed rewrite rules.
|
|
10
11
|
*
|
|
11
|
-
* @
|
|
12
|
+
* @param {Object} rewriteConfig
|
|
13
|
+
* @return {Function} Function to rewrite paths; This function should accept `path` (request.url) as parameter
|
|
12
14
|
*/
|
|
13
15
|
function createPathRewriter(rewriteConfig) {
|
|
14
|
-
|
|
16
|
+
var rulesCache
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
if (!isValidRewriteConfig(rewriteConfig)) {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
if (_.isFunction(rewriteConfig)) {
|
|
23
|
+
var customRewriteFn = rewriteConfig
|
|
24
|
+
return customRewriteFn
|
|
25
|
+
} else {
|
|
26
|
+
rulesCache = parsePathRewriteRules(rewriteConfig)
|
|
27
|
+
return rewritePath
|
|
28
|
+
}
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
function rewritePath(path) {
|
|
31
|
+
var result = path
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
_.forEach(rulesCache, function(rule) {
|
|
34
|
+
if (rule.regex.test(path)) {
|
|
35
|
+
result = result.replace(rule.regex, rule.value)
|
|
36
|
+
logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result)
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
})
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
return result
|
|
42
|
+
}
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
function isValidRewriteConfig(rewriteConfig) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
if (_.isFunction(rewriteConfig)) {
|
|
47
|
+
return true
|
|
48
|
+
} else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
|
|
49
|
+
return true
|
|
50
|
+
} else if (
|
|
51
|
+
_.isUndefined(rewriteConfig) ||
|
|
52
|
+
_.isNull(rewriteConfig) ||
|
|
53
|
+
_.isEqual(rewriteConfig, {})
|
|
54
|
+
) {
|
|
55
|
+
return false
|
|
56
|
+
} else {
|
|
57
|
+
throw new Error(ERRORS.ERR_PATH_REWRITER_CONFIG)
|
|
58
|
+
}
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
function parsePathRewriteRules(rewriteConfig) {
|
|
58
|
-
|
|
62
|
+
var rules = []
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
if (_.isPlainObject(rewriteConfig)) {
|
|
65
|
+
_.forIn(rewriteConfig, function(value, key) {
|
|
66
|
+
rules.push({
|
|
67
|
+
regex: new RegExp(key),
|
|
68
|
+
value: rewriteConfig[key]
|
|
69
|
+
})
|
|
70
|
+
logger.info(
|
|
71
|
+
'[HPM] Proxy rewrite rule created: "%s" ~> "%s"',
|
|
72
|
+
key,
|
|
73
|
+
rewriteConfig[key]
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
69
77
|
|
|
70
|
-
|
|
78
|
+
return rules
|
|
71
79
|
}
|
|
72
|
-
|
package/lib/router.js
CHANGED
|
@@ -1,53 +1,51 @@
|
|
|
1
|
-
var _
|
|
2
|
-
var logger = require('./logger.js').getInstance()
|
|
1
|
+
var _ = require('lodash')
|
|
2
|
+
var logger = require('./logger.js').getInstance()
|
|
3
3
|
|
|
4
4
|
module.exports = {
|
|
5
|
-
|
|
6
|
-
}
|
|
5
|
+
getTarget: getTarget
|
|
6
|
+
}
|
|
7
7
|
|
|
8
8
|
function getTarget(req, config) {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
var newTarget
|
|
10
|
+
var router = config.router
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
if (_.isPlainObject(router)) {
|
|
13
|
+
newTarget = getTargetFromProxyTable(req, router)
|
|
14
|
+
} else if (_.isFunction(router)) {
|
|
15
|
+
newTarget = router(req)
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
return newTarget
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
function getTargetFromProxyTable(req, table) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
});
|
|
22
|
+
var result
|
|
23
|
+
var host = req.headers.host
|
|
24
|
+
var path = req.url
|
|
25
|
+
|
|
26
|
+
var hostAndPath = host + path
|
|
27
|
+
|
|
28
|
+
_.forIn(table, function(value, key) {
|
|
29
|
+
if (containsPath(key)) {
|
|
30
|
+
if (hostAndPath.indexOf(key) > -1) {
|
|
31
|
+
// match 'localhost:3000/api'
|
|
32
|
+
result = table[key]
|
|
33
|
+
logger.debug('[HPM] Router table match: "%s"', key)
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
if (key === host) {
|
|
38
|
+
// match 'localhost:3000'
|
|
39
|
+
result = table[key]
|
|
40
|
+
logger.debug('[HPM] Router table match: "%s"', host)
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
})
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
return result
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
function containsPath(v) {
|
|
52
|
-
|
|
50
|
+
return v.indexOf('/') > -1
|
|
53
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "http-proxy-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.1",
|
|
4
4
|
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean": "rm -rf coverage",
|
|
12
|
-
"
|
|
12
|
+
"lint": "prettier \"**/*.{js,md}\" --list-different",
|
|
13
|
+
"lint:fix": "prettier \"**/*.{js,md}\" --write",
|
|
14
|
+
"test": "npm run lint && mocha --recursive --colors --reporter spec",
|
|
13
15
|
"cover": "npm run clean && istanbul cover ./node_modules/mocha/bin/_mocha -- --recursive",
|
|
14
16
|
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- --recursive --reporter spec && istanbul-coveralls && npm run clean"
|
|
15
17
|
},
|
|
@@ -39,22 +41,41 @@
|
|
|
39
41
|
},
|
|
40
42
|
"homepage": "https://github.com/chimurai/http-proxy-middleware",
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
44
|
+
"@commitlint/cli": "^7.2.1",
|
|
45
|
+
"@commitlint/config-conventional": "^7.1.2",
|
|
46
|
+
"browser-sync": "^2.26.3",
|
|
47
|
+
"chai": "^4.2.0",
|
|
48
|
+
"connect": "^3.6.6",
|
|
49
|
+
"coveralls": "^3.0.2",
|
|
50
|
+
"express": "^4.16.4",
|
|
51
|
+
"husky": "^1.2.0",
|
|
47
52
|
"istanbul": "^0.4.5",
|
|
48
53
|
"istanbul-coveralls": "^1.0.3",
|
|
49
|
-
"mocha": "^
|
|
50
|
-
"mocha-lcov-reporter": "1.
|
|
51
|
-
"opn": "^4.0
|
|
52
|
-
"
|
|
54
|
+
"mocha": "^5.2.0",
|
|
55
|
+
"mocha-lcov-reporter": "1.3.0",
|
|
56
|
+
"opn": "^5.4.0",
|
|
57
|
+
"precise-commits": "^1.0.2",
|
|
58
|
+
"prettier": "^1.15.2",
|
|
59
|
+
"ws": "^6.1.2"
|
|
53
60
|
},
|
|
54
61
|
"dependencies": {
|
|
55
|
-
"http-proxy": "^1.
|
|
56
|
-
"is-glob": "^
|
|
57
|
-
"lodash": "^4.17.
|
|
58
|
-
"micromatch": "^
|
|
62
|
+
"http-proxy": "^1.17.0",
|
|
63
|
+
"is-glob": "^4.0.0",
|
|
64
|
+
"lodash": "^4.17.11",
|
|
65
|
+
"micromatch": "^3.1.10"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=4.0.0"
|
|
69
|
+
},
|
|
70
|
+
"husky": {
|
|
71
|
+
"hooks": {
|
|
72
|
+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
|
73
|
+
"pre-commit": "precise-commits"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"commitlint": {
|
|
77
|
+
"extends": [
|
|
78
|
+
"@commitlint/config-conventional"
|
|
79
|
+
]
|
|
59
80
|
}
|
|
60
81
|
}
|