http-proxy-middleware 1.3.1 → 2.0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## [v2.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v2.0.0)
4
+
5
+ - chore(package): drop node 10 [BREAKING CHANGE] ([#577](https://github.com/chimurai/http-proxy-middleware/pull/577))
6
+
3
7
  ## [v1.3.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.3.1)
4
8
 
5
9
  - fix(fix-request-body): make sure the content-type exists ([#578](https://github.com/chimurai/http-proxy-middleware/pull/578)) ([oufeng](https://github.com/oufeng))
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # http-proxy-middleware
2
2
 
3
- [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/chimurai/http-proxy-middleware/Test/master?style=flat-square)](https://github.com/chimurai/http-proxy-middleware/actions?query=branch%3Amaster)
3
+ [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/chimurai/http-proxy-middleware/CI/master?style=flat-square)](https://github.com/chimurai/http-proxy-middleware/actions?query=branch%3Amaster)
4
4
  [![Coveralls](https://img.shields.io/coveralls/chimurai/http-proxy-middleware.svg?style=flat-square)](https://coveralls.io/r/chimurai/http-proxy-middleware)
5
5
  [![dependency Status](https://img.shields.io/david/chimurai/http-proxy-middleware.svg?style=flat-square)](https://david-dm.org/chimurai/http-proxy-middleware#info=dependencies)
6
6
  [![dependency Status](https://snyk.io/test/npm/http-proxy-middleware/badge.svg?style=flat-square)](https://snyk.io/test/npm/http-proxy-middleware)
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.responseInterceptor = void 0;
13
4
  const zlib = require("zlib");
@@ -19,27 +10,25 @@ const zlib = require("zlib");
19
10
  * NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
20
11
  */
21
12
  function responseInterceptor(interceptor) {
22
- return function proxyRes(proxyRes, req, res) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- const originalProxyRes = proxyRes;
25
- let buffer = Buffer.from('', 'utf8');
26
- // decompress proxy response
27
- const _proxyRes = decompress(proxyRes, proxyRes.headers['content-encoding']);
28
- // concat data stream
29
- _proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
30
- _proxyRes.on('end', () => __awaiter(this, void 0, void 0, function* () {
31
- // copy original headers
32
- copyHeaders(proxyRes, res);
33
- // call interceptor with intercepted response (buffer)
34
- const interceptedBuffer = Buffer.from(yield interceptor(buffer, originalProxyRes, req, res));
35
- // set correct content-length (with double byte character support)
36
- res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
37
- res.write(interceptedBuffer);
38
- res.end();
39
- }));
40
- _proxyRes.on('error', (error) => {
41
- res.end(`Error fetching proxied request: ${error.message}`);
42
- });
13
+ return async function proxyRes(proxyRes, req, res) {
14
+ const originalProxyRes = proxyRes;
15
+ let buffer = Buffer.from('', 'utf8');
16
+ // decompress proxy response
17
+ const _proxyRes = decompress(proxyRes, proxyRes.headers['content-encoding']);
18
+ // concat data stream
19
+ _proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
20
+ _proxyRes.on('end', async () => {
21
+ // copy original headers
22
+ copyHeaders(proxyRes, res);
23
+ // call interceptor with intercepted response (buffer)
24
+ const interceptedBuffer = Buffer.from(await interceptor(buffer, originalProxyRes, req, res));
25
+ // set correct content-length (with double byte character support)
26
+ res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
27
+ res.write(interceptedBuffer);
28
+ res.end();
29
+ });
30
+ _proxyRes.on('error', (error) => {
31
+ res.end(`Error fetching proxied request: ${error.message}`);
43
32
  });
44
33
  };
45
34
  }
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.HttpProxyMiddleware = void 0;
13
4
  const httpProxy = require("http-proxy");
@@ -23,11 +14,11 @@ class HttpProxyMiddleware {
23
14
  this.wsInternalSubscribed = false;
24
15
  this.serverOnCloseSubscribed = false;
25
16
  // https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this
26
- this.middleware = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
17
+ this.middleware = async (req, res, next) => {
27
18
  var _a, _b;
28
19
  if (this.shouldProxy(this.config.context, req)) {
29
20
  try {
30
- const activeProxyOptions = yield this.prepareProxyRequest(req);
21
+ const activeProxyOptions = await this.prepareProxyRequest(req);
31
22
  this.proxy.web(req, res, activeProxyOptions);
32
23
  }
33
24
  catch (err) {
@@ -57,7 +48,7 @@ class HttpProxyMiddleware {
57
48
  // use initial request to access the server object to subscribe to http upgrade event
58
49
  this.catchUpgradeRequest(server);
59
50
  }
60
- });
51
+ };
61
52
  this.catchUpgradeRequest = (server) => {
62
53
  if (!this.wsInternalSubscribed) {
63
54
  server.on('upgrade', this.handleUpgrade);
@@ -66,13 +57,13 @@ class HttpProxyMiddleware {
66
57
  this.wsInternalSubscribed = true;
67
58
  }
68
59
  };
69
- this.handleUpgrade = (req, socket, head) => __awaiter(this, void 0, void 0, function* () {
60
+ this.handleUpgrade = async (req, socket, head) => {
70
61
  if (this.shouldProxy(this.config.context, req)) {
71
- const activeProxyOptions = yield this.prepareProxyRequest(req);
62
+ const activeProxyOptions = await this.prepareProxyRequest(req);
72
63
  this.proxy.ws(req, socket, head, activeProxyOptions);
73
64
  this.logger.info('[HPM] Upgrading to WebSocket');
74
65
  }
75
- });
66
+ };
76
67
  /**
77
68
  * Determine whether request should be proxied.
78
69
  *
@@ -93,7 +84,7 @@ class HttpProxyMiddleware {
93
84
  * @param {Object} req
94
85
  * @return {Object} proxy options
95
86
  */
96
- this.prepareProxyRequest = (req) => __awaiter(this, void 0, void 0, function* () {
87
+ this.prepareProxyRequest = async (req) => {
97
88
  // https://github.com/chimurai/http-proxy-middleware/issues/17
98
89
  // https://github.com/chimurai/http-proxy-middleware/issues/94
99
90
  req.url = req.originalUrl || req.url;
@@ -103,30 +94,30 @@ class HttpProxyMiddleware {
103
94
  // Apply in order:
104
95
  // 1. option.router
105
96
  // 2. option.pathRewrite
106
- yield this.applyRouter(req, newProxyOptions);
107
- yield this.applyPathRewrite(req, this.pathRewriter);
97
+ await this.applyRouter(req, newProxyOptions);
98
+ await this.applyPathRewrite(req, this.pathRewriter);
108
99
  // debug logging for both http(s) and websockets
109
100
  if (this.proxyOptions.logLevel === 'debug') {
110
101
  const arrow = logger_1.getArrow(originalPath, req.url, this.proxyOptions.target, newProxyOptions.target);
111
102
  this.logger.debug('[HPM] %s %s %s %s', req.method, originalPath, arrow, newProxyOptions.target);
112
103
  }
113
104
  return newProxyOptions;
114
- });
105
+ };
115
106
  // Modify option.target when router present.
116
- this.applyRouter = (req, options) => __awaiter(this, void 0, void 0, function* () {
107
+ this.applyRouter = async (req, options) => {
117
108
  let newTarget;
118
109
  if (options.router) {
119
- newTarget = yield Router.getTarget(req, options);
110
+ newTarget = await Router.getTarget(req, options);
120
111
  if (newTarget) {
121
112
  this.logger.debug('[HPM] Router new target: %s -> "%s"', options.target, newTarget);
122
113
  options.target = newTarget;
123
114
  }
124
115
  }
125
- });
116
+ };
126
117
  // rewrite path
127
- this.applyPathRewrite = (req, pathRewriter) => __awaiter(this, void 0, void 0, function* () {
118
+ this.applyPathRewrite = async (req, pathRewriter) => {
128
119
  if (pathRewriter) {
129
- const path = yield pathRewriter(req.url, req);
120
+ const path = await pathRewriter(req.url, req);
130
121
  if (typeof path === 'string') {
131
122
  req.url = path;
132
123
  }
@@ -134,7 +125,7 @@ class HttpProxyMiddleware {
134
125
  this.logger.info('[HPM] pathRewrite: No rewritten path found. (%s)', req.url);
135
126
  }
136
127
  }
137
- });
128
+ };
138
129
  this.logError = (err, req, res, target) => {
139
130
  var _a;
140
131
  const hostname = ((_a = req.headers) === null || _a === void 0 ? void 0 : _a.host) || req.hostname || req.host; // (websocket) || (node0.10 || node 4/5)
package/dist/router.js CHANGED
@@ -1,30 +1,19 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.getTarget = void 0;
13
4
  const isPlainObj = require("is-plain-obj");
14
5
  const logger_1 = require("./logger");
15
6
  const logger = logger_1.getInstance();
16
- function getTarget(req, config) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- let newTarget;
19
- const router = config.router;
20
- if (isPlainObj(router)) {
21
- newTarget = getTargetFromProxyTable(req, router);
22
- }
23
- else if (typeof router === 'function') {
24
- newTarget = yield router(req);
25
- }
26
- return newTarget;
27
- });
7
+ async function getTarget(req, config) {
8
+ let newTarget;
9
+ const router = config.router;
10
+ if (isPlainObj(router)) {
11
+ newTarget = getTargetFromProxyTable(req, router);
12
+ }
13
+ else if (typeof router === 'function') {
14
+ newTarget = await router(req);
15
+ }
16
+ return newTarget;
28
17
  }
29
18
  exports.getTarget = getTarget;
30
19
  function getTargetFromProxyTable(req, table) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "1.3.1",
3
+ "version": "2.0.0",
4
4
  "description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,6 +19,7 @@
19
19
  "build": "tsc",
20
20
  "pretest": "yarn build",
21
21
  "test": "jest",
22
+ "precoverage": "yarn build",
22
23
  "coverage": "jest --coverage --coverageReporters=lcov",
23
24
  "prepare": "yarn build && rm dist/tsconfig.tsbuildinfo"
24
25
  },
@@ -87,7 +88,7 @@
87
88
  "micromatch": "^4.0.2"
88
89
  },
89
90
  "engines": {
90
- "node": ">=8.0.0"
91
+ "node": ">=12.0.0"
91
92
  },
92
93
  "husky": {
93
94
  "hooks": {