http-proxy-middleware 0.17.2-beta → 0.18.0

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,73 @@
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
- function createPathRewriter(rewriteConfig) {
14
- var rulesCache;
15
+ function createPathRewriter (rewriteConfig) {
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
- 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) ||
45
+ function isValidRewriteConfig (rewriteConfig) {
46
+ if (_.isFunction(rewriteConfig)) {
47
+ return true
48
+ } else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
49
+ return true
50
+ } else if (_.isUndefined(rewriteConfig) ||
49
51
  _.isNull(rewriteConfig) ||
50
52
  _.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
- }
53
+ return false
54
+ } else {
55
+ throw new Error(ERRORS.ERR_PATH_REWRITER_CONFIG)
56
+ }
55
57
  }
56
58
 
57
- function parsePathRewriteRules(rewriteConfig) {
58
- var rules = [];
59
+ function parsePathRewriteRules (rewriteConfig) {
60
+ var rules = []
59
61
 
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
- }
62
+ if (_.isPlainObject(rewriteConfig)) {
63
+ _.forIn(rewriteConfig, function (value, key) {
64
+ rules.push({
65
+ regex: new RegExp(key),
66
+ value: rewriteConfig[key]
67
+ })
68
+ logger.info('[HPM] Proxy rewrite rule created: "%s" ~> "%s"', key, rewriteConfig[key])
69
+ })
70
+ }
69
71
 
70
- return rules;
72
+ return rules
71
73
  }
72
-
package/lib/router.js CHANGED
@@ -1,53 +1,49 @@
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
- };
7
-
8
- function getTarget(req, config) {
9
- var newTarget;
10
- var router = config.router;
11
-
12
- if (_.isPlainObject(router)) {
13
- newTarget = getTargetFromProxyTable(req, router);
14
- } else if (_.isFunction(router)) {
15
- newTarget = router(req);
16
- }
17
-
18
- return newTarget;
5
+ getTarget: getTarget
19
6
  }
20
7
 
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;
8
+ function getTarget (req, config) {
9
+ var newTarget
10
+ var router = config.router
27
11
 
28
- _.forIn(table, function(value, key) {
29
- if (containsPath(key)) {
12
+ if (_.isPlainObject(router)) {
13
+ newTarget = getTargetFromProxyTable(req, router)
14
+ } else if (_.isFunction(router)) {
15
+ newTarget = router(req)
16
+ }
30
17
 
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
- }
18
+ return newTarget
19
+ }
45
20
 
46
- });
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
+ if (hostAndPath.indexOf(key) > -1) { // match 'localhost:3000/api'
31
+ result = table[key]
32
+ logger.debug('[HPM] Router table match: "%s"', key)
33
+ return false
34
+ }
35
+ } else {
36
+ if (key === host) { // match 'localhost:3000'
37
+ result = table[key]
38
+ logger.debug('[HPM] Router table match: "%s"', host)
39
+ return false
40
+ }
41
+ }
42
+ })
47
43
 
48
- return result;
44
+ return result
49
45
  }
50
46
 
51
- function containsPath(v) {
52
- return v.indexOf('/') > -1;
47
+ function containsPath (v) {
48
+ return v.indexOf('/') > -1
53
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "0.17.2-beta",
3
+ "version": "0.18.0",
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,9 +9,11 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "clean": "rm -rf coverage",
12
- "test": "mocha --recursive --colors --reporter spec",
12
+ "lint": "standard --verbose | snazzy --colors",
13
+ "test": "npm run lint && mocha --recursive --colors --reporter spec",
13
14
  "cover": "npm run clean && istanbul cover ./node_modules/mocha/bin/_mocha -- --recursive",
14
- "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- --recursive --reporter spec && istanbul-coveralls && npm run clean"
15
+ "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- --recursive --reporter spec && istanbul-coveralls && npm run clean",
16
+ "commitmsg": "commitlint -e $GIT_PARAMS"
15
17
  },
16
18
  "repository": {
17
19
  "type": "git",
@@ -39,22 +41,40 @@
39
41
  },
40
42
  "homepage": "https://github.com/chimurai/http-proxy-middleware",
41
43
  "devDependencies": {
42
- "browser-sync": "^2.14.0",
43
- "chai": "^3.5.0",
44
- "connect": "^3.4.1",
45
- "coveralls": "^2.11.12",
46
- "express": "^4.14.0",
47
- "istanbul": "^0.4.4",
44
+ "@commitlint/cli": "^6.1.3",
45
+ "@commitlint/config-conventional": "^6.1.3",
46
+ "browser-sync": "^2.23.6",
47
+ "chai": "^4.1.2",
48
+ "connect": "^3.6.6",
49
+ "coveralls": "^3.0.0",
50
+ "express": "^4.16.3",
51
+ "husky": "^0.14.3",
52
+ "istanbul": "^0.4.5",
48
53
  "istanbul-coveralls": "^1.0.3",
49
- "mocha": "^3.0.2",
50
- "mocha-lcov-reporter": "1.2.0",
51
- "opn": "^4.0.2",
52
- "ws": "^1.1.1"
54
+ "mocha": "^5.0.4",
55
+ "mocha-lcov-reporter": "1.3.0",
56
+ "opn": "^5.2.0",
57
+ "snazzy": "^7.1.1",
58
+ "standard": "^11.0.0",
59
+ "ws": "^5.0.0"
53
60
  },
54
61
  "dependencies": {
55
- "http-proxy": "^1.14.0",
56
- "is-glob": "^2.0.1",
57
- "lodash": "^4.14.2",
58
- "micromatch": "^2.3.11"
62
+ "http-proxy": "^1.16.2",
63
+ "is-glob": "^4.0.0",
64
+ "lodash": "^4.17.5",
65
+ "micromatch": "^3.1.9"
66
+ },
67
+ "engines": {
68
+ "node": ">=4.0.0"
69
+ },
70
+ "standard": {
71
+ "env": [
72
+ "mocha"
73
+ ]
74
+ },
75
+ "commitlint": {
76
+ "extends": [
77
+ "@commitlint/config-conventional"
78
+ ]
59
79
  }
60
80
  }