http-proxy-middleware 0.17.4 → 0.19.2

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.
@@ -1,72 +1,79 @@
1
- var _ = require('lodash');
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
- create: createPathRewriter
6
- };
6
+ create: createPathRewriter
7
+ }
7
8
 
8
9
  /**
9
10
  * Create rewrite function, to cache parsed rewrite rules.
10
11
  *
11
- * @returns {function} Function to rewrite paths; This function should accept `path` (request.url) as parameter
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
- var rulesCache;
16
+ var rulesCache
15
17
 
16
- if (!isValidRewriteConfig(rewriteConfig)) {
17
- return;
18
- }
18
+ if (!isValidRewriteConfig(rewriteConfig)) {
19
+ return
20
+ }
19
21
 
20
- if (_.isFunction(rewriteConfig)) {
21
- var customRewriteFn = rewriteConfig;
22
- return customRewriteFn;
23
- } else {
24
- rulesCache = parsePathRewriteRules(rewriteConfig);
25
- return rewritePath;
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
- function rewritePath(path) {
29
- var result = path;
30
+ function rewritePath(path) {
31
+ var result = path
30
32
 
31
- _.forEach(rulesCache, function(rule) {
32
- if (rule.regex.test(path)) {
33
- result = result.replace(rule.regex, rule.value);
34
- logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result);
35
- return false;
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
- return result;
40
- }
41
+ return result
42
+ }
41
43
  }
42
44
 
43
45
  function isValidRewriteConfig(rewriteConfig) {
44
- if (_.isFunction(rewriteConfig)) {
45
- return true;
46
- } else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
47
- return true;
48
- } else if (_.isUndefined(rewriteConfig) ||
49
- _.isNull(rewriteConfig) ||
50
- _.isEqual(rewriteConfig, {})) {
51
- return false;
52
- } else {
53
- throw new Error('[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function');
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
- var rules = [];
62
+ var rules = []
59
63
 
60
- if (_.isPlainObject(rewriteConfig)) {
61
- _.forIn(rewriteConfig, function(value, key) {
62
- rules.push({
63
- regex: new RegExp(key),
64
- value: rewriteConfig[key]
65
- });
66
- logger.info('[HPM] Proxy rewrite rule created: "%s" ~> "%s"', key, rewriteConfig[key]);
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
- return rules;
78
+ return rules
71
79
  }
72
-
package/lib/router.js CHANGED
@@ -1,53 +1,51 @@
1
- var _ = require('lodash');
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
- getTarget: getTarget
6
- };
5
+ getTarget: getTarget
6
+ }
7
7
 
8
8
  function getTarget(req, config) {
9
- var newTarget;
10
- var router = config.router;
9
+ var newTarget
10
+ var router = config.router
11
11
 
12
- if (_.isPlainObject(router)) {
13
- newTarget = getTargetFromProxyTable(req, router);
14
- } else if (_.isFunction(router)) {
15
- newTarget = router(req);
16
- }
12
+ if (_.isPlainObject(router)) {
13
+ newTarget = getTargetFromProxyTable(req, router)
14
+ } else if (_.isFunction(router)) {
15
+ newTarget = router(req)
16
+ }
17
17
 
18
- return newTarget;
18
+ return newTarget
19
19
  }
20
20
 
21
21
  function getTargetFromProxyTable(req, table) {
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
-
31
- if (hostAndPath.indexOf(key) > -1) { // match 'localhost:3000/api'
32
- result = table[key];
33
- logger.debug('[HPM] Router table match: "%s"', key);
34
- return false;
35
- }
36
- } else {
37
-
38
- if (key === host) { // match 'localhost:3000'
39
- result = table[key];
40
- logger.debug('[HPM] Router table match: "%s"', host);
41
- return false;
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
- return result;
46
+ return result
49
47
  }
50
48
 
51
49
  function containsPath(v) {
52
- return v.indexOf('/') > -1;
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.17.4",
3
+ "version": "0.19.2",
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,6 +9,8 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "clean": "rm -rf coverage",
12
+ "lint": "prettier \"**/*.{js,md}\" --list-different",
13
+ "lint:fix": "prettier \"**/*.{js,md}\" --write",
12
14
  "test": "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"
@@ -39,22 +41,41 @@
39
41
  },
40
42
  "homepage": "https://github.com/chimurai/http-proxy-middleware",
41
43
  "devDependencies": {
42
- "browser-sync": "^2.18.2",
43
- "chai": "^3.5.0",
44
- "connect": "^3.5.0",
45
- "coveralls": "^2.11.15",
46
- "express": "^4.14.0",
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": "^3.2.0",
50
- "mocha-lcov-reporter": "1.2.0",
51
- "opn": "^4.0.2",
52
- "ws": "^1.1.1"
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.16.2",
56
- "is-glob": "^3.1.0",
57
- "lodash": "^4.17.2",
58
- "micromatch": "^2.3.11"
62
+ "http-proxy": "^1.18.1",
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
  }