http-proxy-middleware 1.2.0 → 1.3.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [v1.3.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.3.1)
4
+
5
+ - 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))
6
+
7
+ ## [v1.3.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.3.0)
8
+
9
+ - docs(response interceptor): align with nodejs default utf8 ([#567](https://github.com/chimurai/http-proxy-middleware/pull/567))
10
+ - feat: try to proxy body even after body-parser middleware ([#492](https://github.com/chimurai/http-proxy-middleware/pull/492)) ([midgleyc](https://github.com/midgleyc))
11
+
12
+ ## [v1.2.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.2.1)
13
+
14
+ - fix(response interceptor): proxy original response headers ([#563](https://github.com/chimurai/http-proxy-middleware/pull/563))
15
+
3
16
  ## [v1.2.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.2.0)
4
17
 
5
18
  - feat(handler): response interceptor ([#520](https://github.com/chimurai/http-proxy-middleware/pull/520))
package/README.md CHANGED
@@ -69,6 +69,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
69
69
  - [app.use\(path, proxy\)](#appusepath-proxy)
70
70
  - [WebSocket](#websocket)
71
71
  - [External WebSocket upgrade](#external-websocket-upgrade)
72
+ - [Intercept and manipulate requests](#intercept-and-manipulate-requests)
72
73
  - [Intercept and manipulate responses](#intercept-and-manipulate-responses)
73
74
  - [Working examples](#working-examples)
74
75
  - [Recipes](#recipes)
@@ -482,6 +483,25 @@ const server = app.listen(3000);
482
483
  server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
483
484
  ```
484
485
 
486
+ ## Intercept and manipulate requests
487
+
488
+ Intercept requests from downstream by defining `onProxyReq` in `createProxyMiddleware`.
489
+
490
+ Currently the only pre-provided request interceptor is `fixRequestBody`, which is used to fix proxied POST requests when `bodyParser` is applied before this middleware.
491
+
492
+ Example:
493
+
494
+ ```javascript
495
+ const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');
496
+
497
+ const proxy = createProxyMiddleware({
498
+ /**
499
+ * Fix bodyParser
500
+ **/
501
+ onProxyReq: fixRequestBody,
502
+ });
503
+ ```
504
+
485
505
  ## Intercept and manipulate responses
486
506
 
487
507
  Intercept responses from upstream with `responseInterceptor`. (Make sure to set `selfHandleResponse: true`)
@@ -507,7 +527,7 @@ const proxy = createProxyMiddleware({
507
527
  * Intercept response and replace 'Hello' with 'Goodbye'
508
528
  **/
509
529
  onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
510
- const response = responseBuffer.toString('utf-8'); // convert buffer to string
530
+ const response = responseBuffer.toString('utf8'); // convert buffer to string
511
531
  return response.replace('Hello', 'Goodbye'); // manipulate response and return the result
512
532
  }),
513
533
  });
@@ -0,0 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { ClientRequest } from 'http';
3
+ import type { Request } from '../types';
4
+ /**
5
+ * Fix proxied body if bodyParser is involved.
6
+ */
7
+ export declare function fixRequestBody(proxyReq: ClientRequest, req: Request): void;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fixRequestBody = void 0;
4
+ const querystring = require("querystring");
5
+ /**
6
+ * Fix proxied body if bodyParser is involved.
7
+ */
8
+ function fixRequestBody(proxyReq, req) {
9
+ if (!req.body || !Object.keys(req.body).length) {
10
+ return;
11
+ }
12
+ const contentType = proxyReq.getHeader('Content-Type');
13
+ const writeBody = (bodyData) => {
14
+ // deepcode ignore ContentLengthInCode: bodyParser fix
15
+ proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
16
+ proxyReq.write(bodyData);
17
+ };
18
+ if (contentType && contentType.includes('application/json')) {
19
+ writeBody(JSON.stringify(req.body));
20
+ }
21
+ if (contentType === 'application/x-www-form-urlencoded') {
22
+ writeBody(querystring.stringify(req.body));
23
+ }
24
+ }
25
+ exports.fixRequestBody = fixRequestBody;
@@ -1 +1,2 @@
1
1
  export { responseInterceptor } from './response-interceptor';
2
+ export { fixRequestBody } from './fix-request-body';
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.responseInterceptor = void 0;
3
+ exports.fixRequestBody = exports.responseInterceptor = void 0;
4
4
  var response_interceptor_1 = require("./response-interceptor");
5
5
  Object.defineProperty(exports, "responseInterceptor", { enumerable: true, get: function () { return response_interceptor_1.responseInterceptor; } });
6
+ var fix_request_body_1 = require("./fix-request-body");
7
+ Object.defineProperty(exports, "fixRequestBody", { enumerable: true, get: function () { return fix_request_body_1.fixRequestBody; } });
@@ -28,8 +28,8 @@ function responseInterceptor(interceptor) {
28
28
  // concat data stream
29
29
  _proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
30
30
  _proxyRes.on('end', () => __awaiter(this, void 0, void 0, function* () {
31
- // set original content type from upstream
32
- res.setHeader('content-type', originalProxyRes.headers['content-type'] || '');
31
+ // copy original headers
32
+ copyHeaders(proxyRes, res);
33
33
  // call interceptor with intercepted response (buffer)
34
34
  const interceptedBuffer = Buffer.from(yield interceptor(buffer, originalProxyRes, req, res));
35
35
  // set correct content-length (with double byte character support)
@@ -70,3 +70,28 @@ function decompress(proxyRes, contentEncoding) {
70
70
  }
71
71
  return _proxyRes;
72
72
  }
73
+ /**
74
+ * Copy original headers
75
+ * https://github.com/apache/superset/blob/9773aba522e957ed9423045ca153219638a85d2f/superset-frontend/webpack.proxy-config.js#L78
76
+ */
77
+ function copyHeaders(originalResponse, response) {
78
+ response.statusCode = originalResponse.statusCode;
79
+ response.statusMessage = originalResponse.statusMessage;
80
+ if (response.setHeader) {
81
+ let keys = Object.keys(originalResponse.headers);
82
+ // ignore chunked, brotli, gzip, deflate headers
83
+ keys = keys.filter((key) => !['content-encoding', 'transfer-encoding'].includes(key));
84
+ keys.forEach((key) => {
85
+ let value = originalResponse.headers[key];
86
+ if (key === 'set-cookie') {
87
+ // remove cookie domain
88
+ value = Array.isArray(value) ? value : [value];
89
+ value = value.map((x) => x.replace(/Domain=[^;]+?/i, ''));
90
+ }
91
+ response.setHeader(key, value);
92
+ });
93
+ }
94
+ else {
95
+ response.headers = originalResponse.headers;
96
+ }
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
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",
@@ -15,11 +15,12 @@
15
15
  "eslint:fix": "yarn eslint --fix",
16
16
  "prettier": "prettier --list-different \"**/*.{js,ts,md,yml,json,html}\"",
17
17
  "prettier:fix": "prettier --write \"**/*.{js,ts,md,yml,json,html}\"",
18
+ "prebuild": "yarn clean",
18
19
  "build": "tsc",
19
20
  "pretest": "yarn build",
20
21
  "test": "jest",
21
22
  "coverage": "jest --coverage --coverageReporters=lcov",
22
- "prepare": "yarn clean && yarn build && rm dist/tsconfig.tsbuildinfo"
23
+ "prepare": "yarn build && rm dist/tsconfig.tsbuildinfo"
23
24
  },
24
25
  "repository": {
25
26
  "type": "git",
@@ -60,6 +61,7 @@
60
61
  "@types/ws": "^7.4.0",
61
62
  "@typescript-eslint/eslint-plugin": "^4.19.0",
62
63
  "@typescript-eslint/parser": "^4.19.0",
64
+ "body-parser": "^1.19.0",
63
65
  "browser-sync": "^2.26.14",
64
66
  "connect": "^3.7.0",
65
67
  "eslint": "^7.23.0",