http-proxy-middleware 3.0.1-beta.0 → 3.0.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/README.md CHANGED
@@ -26,48 +26,29 @@ Proxy `/api` requests to `http://www.example.org`
26
26
 
27
27
  :bulb: **Tip:** Set the option `changeOrigin` to `true` for [name-based virtual hosted sites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based).
28
28
 
29
- ```javascript
30
- // javascript
31
-
32
- const express = require('express');
33
- const { createProxyMiddleware } = require('http-proxy-middleware');
34
-
35
- const app = express();
36
-
37
- app.use(
38
- '/api',
39
- createProxyMiddleware({
40
- target: 'http://www.example.org/secret',
41
- changeOrigin: true,
42
- }),
43
- );
44
-
45
- app.listen(3000);
46
-
47
- // proxy and change the base path from "/api" to "/secret"
48
- // http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
49
- ```
50
-
51
29
  ```typescript
52
30
  // typescript
53
31
 
54
32
  import * as express from 'express';
55
- import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
33
+ import type { Request, Response, NextFunction } from 'express';
34
+
35
+ import { createProxyMiddleware } from 'http-proxy-middleware';
36
+ import type { Filter, Options, RequestHandler } from 'http-proxy-middleware';
56
37
 
57
38
  const app = express();
58
39
 
59
- app.use(
60
- '/api',
61
- createProxyMiddleware({
40
+ const proxyMiddleware = createProxyMiddleware<Request, Response>({
62
41
  target: 'http://www.example.org/api',
63
42
  changeOrigin: true,
64
43
  }),
65
- );
44
+
45
+ app.use('/api', proxyMiddleware);
66
46
 
67
47
  app.listen(3000);
68
48
 
69
49
  // proxy and keep the same base path "/api"
70
50
  // http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar
51
+
71
52
  ```
72
53
 
73
54
  _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).
@@ -664,4 +645,4 @@ $ yarn spellcheck
664
645
 
665
646
  The MIT License (MIT)
666
647
 
667
- Copyright (c) 2015-2022 Steven Chim
648
+ Copyright (c) 2015-2024 Steven Chim
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.verifyConfig = void 0;
3
+ exports.verifyConfig = verifyConfig;
4
4
  const errors_1 = require("./errors");
5
5
  function verifyConfig(options) {
6
6
  if (!options.target && !options.router) {
7
7
  throw new Error(errors_1.ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
8
8
  }
9
9
  }
10
- exports.verifyConfig = verifyConfig;
@@ -0,0 +1,3 @@
1
+ import type { Options, RequestHandler, NextFunction } from './types';
2
+ import type * as http from 'http';
3
+ export declare function createProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createProxyMiddleware = createProxyMiddleware;
4
+ const http_proxy_middleware_1 = require("./http-proxy-middleware");
5
+ function createProxyMiddleware(options) {
6
+ const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(options);
7
+ return middleware;
8
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPlugins = void 0;
3
+ exports.getPlugins = getPlugins;
4
4
  const default_1 = require("./plugins/default");
5
5
  function getPlugins(options) {
6
6
  // don't load default errorResponsePlugin if user has specified their own
@@ -11,4 +11,3 @@ function getPlugins(options) {
11
11
  const userPlugins = options.plugins ?? [];
12
12
  return [...defaultPlugins, ...userPlugins];
13
13
  }
14
- exports.getPlugins = getPlugins;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type * as http from 'http';
3
2
  export type BodyParserLikeRequest = http.IncomingMessage & {
4
3
  body: any;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fixRequestBody = void 0;
3
+ exports.fixRequestBody = fixRequestBody;
4
4
  const querystring = require("querystring");
5
5
  /**
6
6
  * Fix proxied body if bodyParser is involved.
@@ -16,11 +16,10 @@ function fixRequestBody(proxyReq, req) {
16
16
  proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
17
17
  proxyReq.write(bodyData);
18
18
  };
19
- if (contentType && contentType.includes('application/json')) {
19
+ if (contentType && (contentType.includes('application/json') || contentType.includes('+json'))) {
20
20
  writeBody(JSON.stringify(requestBody));
21
21
  }
22
22
  if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
23
23
  writeBody(querystring.stringify(requestBody));
24
24
  }
25
25
  }
26
- exports.fixRequestBody = fixRequestBody;
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import type * as http from 'http';
4
2
  type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buffer: Buffer, proxyRes: TReq, req: TReq, res: TRes) => Promise<Buffer | string>;
5
3
  /**
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.responseInterceptor = void 0;
3
+ exports.responseInterceptor = responseInterceptor;
4
4
  const zlib = require("zlib");
5
5
  const debug_1 = require("../debug");
6
6
  const function_1 = require("../utils/function");
@@ -39,7 +39,6 @@ function responseInterceptor(interceptor) {
39
39
  });
40
40
  };
41
41
  }
42
- exports.responseInterceptor = responseInterceptor;
43
42
  /**
44
43
  * Streaming decompression of proxy response
45
44
  * source: https://github.com/apache/superset/blob/9773aba522e957ed9423045ca153219638a85d2f/superset-frontend/webpack.proxy-config.js#L116
package/dist/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- /// <reference types="node" />
2
- import type { Options, RequestHandler, NextFunction } from './types';
3
- import type * as http from 'http';
4
- export declare function createProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
1
+ export * from './factory';
5
2
  export * from './handlers';
6
3
  export type { Filter, Options, RequestHandler } from './types';
7
4
  /**
package/dist/index.js CHANGED
@@ -14,13 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createProxyMiddleware = void 0;
18
- const http_proxy_middleware_1 = require("./http-proxy-middleware");
19
- function createProxyMiddleware(options) {
20
- const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(options);
21
- return middleware;
22
- }
23
- exports.createProxyMiddleware = createProxyMiddleware;
17
+ __exportStar(require("./factory"), exports);
24
18
  __exportStar(require("./handlers"), exports);
25
19
  /**
26
20
  * Default plugins
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Filter, RequestHandler } from '../types';
3
2
  import { LegacyOptions } from './types';
4
3
  import type * as http from 'http';
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.legacyCreateProxyMiddleware = void 0;
4
- const __1 = require("..");
3
+ exports.legacyCreateProxyMiddleware = legacyCreateProxyMiddleware;
4
+ const factory_1 = require("../factory");
5
5
  const debug_1 = require("../debug");
6
6
  const options_adapter_1 = require("./options-adapter");
7
7
  const debug = debug_1.Debug.extend('legacy-create-proxy-middleware');
8
8
  function legacyCreateProxyMiddleware(legacyContext, legacyOptions) {
9
9
  debug('init');
10
10
  const options = (0, options_adapter_1.legacyOptionsAdapter)(legacyContext, legacyOptions);
11
- const proxyMiddleware = (0, __1.createProxyMiddleware)(options);
11
+ const proxyMiddleware = (0, factory_1.createProxyMiddleware)(options);
12
12
  // https://github.com/chimurai/http-proxy-middleware/pull/731/files#diff-07e6ad10bda0df091b737caed42767657cd0bd74a01246a1a0b7ab59c0f6e977L118
13
13
  debug('add marker for patching req.url (old behavior)');
14
14
  proxyMiddleware.__LEGACY_HTTP_PROXY_MIDDLEWARE__ = true;
15
15
  return proxyMiddleware;
16
16
  }
17
- exports.legacyCreateProxyMiddleware = legacyCreateProxyMiddleware;
@@ -1,4 +1,4 @@
1
- import { Filter, Options } from '..';
1
+ import { Filter, Options } from '../types';
2
2
  import { LegacyOptions } from './types';
3
3
  /**
4
4
  * Convert {@link LegacyOptions legacy Options} to new {@link Options}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.legacyOptionsAdapter = void 0;
3
+ exports.legacyOptionsAdapter = legacyOptionsAdapter;
4
4
  const url = require("url");
5
5
  const debug_1 = require("../debug");
6
6
  const logger_1 = require("../logger");
@@ -88,7 +88,6 @@ function legacyOptionsAdapter(legacyContext, legacyOptions) {
88
88
  }
89
89
  return options;
90
90
  }
91
- exports.legacyOptionsAdapter = legacyOptionsAdapter;
92
91
  function getLegacyLogger(options) {
93
92
  const legacyLogger = options.logProvider && options.logProvider();
94
93
  if (legacyLogger) {
@@ -1,6 +1,5 @@
1
- /// <reference types="node" />
2
1
  import type * as http from 'http';
3
- import { Options } from '..';
2
+ import { Options } from '../types';
4
3
  /**
5
4
  * @deprecated
6
5
  *
package/dist/logger.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-empty-function */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getLogger = void 0;
4
+ exports.getLogger = getLogger;
5
5
  /**
6
6
  * Compatibility matrix
7
7
  *
@@ -23,4 +23,3 @@ const noopLogger = {
23
23
  function getLogger(options) {
24
24
  return options.logger || noopLogger;
25
25
  }
26
- exports.getLogger = getLogger;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { Filter } from './types';
3
2
  import type * as http from 'http';
4
3
  export declare function matchPathFilter<TReq = http.IncomingMessage>(pathFilter: Filter<TReq> | undefined, uri: string | undefined, req: http.IncomingMessage): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.matchPathFilter = void 0;
3
+ exports.matchPathFilter = matchPathFilter;
4
4
  const isGlob = require("is-glob");
5
5
  const micromatch = require("micromatch");
6
6
  const url = require("url");
@@ -31,7 +31,6 @@ function matchPathFilter(pathFilter = '/', uri, req) {
31
31
  }
32
32
  throw new Error(errors_1.ERRORS.ERR_CONTEXT_MATCHER_GENERIC);
33
33
  }
34
- exports.matchPathFilter = matchPathFilter;
35
34
  /**
36
35
  * @param {String} pathFilter '/api'
37
36
  * @param {String} uri 'http://example.org/api/b/c/d.html'
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createPathRewriter = void 0;
3
+ exports.createPathRewriter = createPathRewriter;
4
4
  const isPlainObj = require("is-plain-obj");
5
5
  const errors_1 = require("./errors");
6
6
  const debug_1 = require("./debug");
@@ -36,7 +36,6 @@ function createPathRewriter(rewriteConfig) {
36
36
  return result;
37
37
  }
38
38
  }
39
- exports.createPathRewriter = createPathRewriter;
40
39
  function isValidRewriteConfig(rewriteConfig) {
41
40
  if (typeof rewriteConfig === 'function') {
42
41
  return true;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loggerPlugin = void 0;
4
4
  const url_1 = require("url");
5
5
  const logger_1 = require("../../logger");
6
+ const logger_plugin_1 = require("../../utils/logger-plugin");
6
7
  const loggerPlugin = (proxyServer, options) => {
7
8
  const logger = (0, logger_1.getLogger)(options);
8
9
  proxyServer.on('error', (err, req, res, target) => {
@@ -25,8 +26,16 @@ const loggerPlugin = (proxyServer, options) => {
25
26
  // Next.js doesn't have req.baseUrl
26
27
  const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
27
28
  // construct targetUrl
28
- const target = new url_1.URL(options.target);
29
- target.pathname = proxyRes.req.path;
29
+ const port = (0, logger_plugin_1.getPort)(proxyRes.req?.agent?.sockets);
30
+ const obj = {
31
+ protocol: proxyRes.req.protocol,
32
+ host: proxyRes.req.host,
33
+ pathname: proxyRes.req.path,
34
+ };
35
+ const target = new url_1.URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
36
+ if (port) {
37
+ target.port = port;
38
+ }
30
39
  const targetUrl = target.toString();
31
40
  const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
32
41
  logger.info(exchange);
package/dist/router.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTarget = void 0;
3
+ exports.getTarget = getTarget;
4
4
  const isPlainObj = require("is-plain-obj");
5
5
  const debug_1 = require("./debug");
6
6
  const debug = debug_1.Debug.extend('router');
@@ -15,7 +15,6 @@ async function getTarget(req, config) {
15
15
  }
16
16
  return newTarget;
17
17
  }
18
- exports.getTarget = getTarget;
19
18
  function getTargetFromProxyTable(req, table) {
20
19
  let result;
21
20
  const host = req.headers.host;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStatusCode = void 0;
3
+ exports.getStatusCode = getStatusCode;
4
4
  function getStatusCode(errorCode) {
5
5
  let statusCode;
6
6
  if (/HPE_INVALID/.test(errorCode)) {
@@ -21,4 +21,3 @@ function getStatusCode(errorCode) {
21
21
  }
22
22
  return statusCode;
23
23
  }
24
- exports.getStatusCode = getStatusCode;
package/dist/types.d.ts CHANGED
@@ -2,10 +2,6 @@
2
2
  * Based on definition by DefinitelyTyped:
3
3
  * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6f529c6c67a447190f86bfbf894d1061e41e07b7/types/http-proxy-middleware/index.d.ts
4
4
  */
5
- /// <reference types="node" />
6
- /// <reference types="node" />
7
- /// <reference types="node" />
8
- /// <reference types="node" />
9
5
  import type * as http from 'http';
10
6
  import type * as httpProxy from 'http-proxy';
11
7
  import type * as net from 'net';
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/ban-types */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getFunctionName = void 0;
4
+ exports.getFunctionName = getFunctionName;
5
5
  function getFunctionName(fn) {
6
6
  return fn.name || '[anonymous Function]';
7
7
  }
8
- exports.getFunctionName = getFunctionName;
@@ -0,0 +1,7 @@
1
+ import type { Agent } from 'node:http';
2
+ export type Sockets = Pick<Agent, 'sockets'>;
3
+ /**
4
+ * Get port from target
5
+ * Using proxyRes.req.agent.sockets to determine the target port
6
+ */
7
+ export declare function getPort(sockets?: Sockets): string | undefined;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPort = getPort;
4
+ /**
5
+ * Get port from target
6
+ * Using proxyRes.req.agent.sockets to determine the target port
7
+ */
8
+ function getPort(sockets) {
9
+ return Object.keys(sockets || {})?.[0]?.split(':')[1];
10
+ }
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "3.0.1-beta.0",
3
+ "type": "commonjs",
4
+ "version": "3.0.1",
4
5
  "description": "The one-liner node.js proxy middleware for connect, express, next.js and more",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
@@ -9,6 +10,7 @@
9
10
  ],
10
11
  "scripts": {
11
12
  "clean": "rm -rf dist coverage tsconfig.tsbuildinfo .eslintcache",
13
+ "install:all": "yarn && (cd examples && yarn)",
12
14
  "lint": "yarn prettier && yarn eslint",
13
15
  "lint:fix": "yarn prettier:fix && yarn eslint:fix",
14
16
  "eslint": "eslint '{src,test}/**/*.ts' --cache",
@@ -27,7 +29,7 @@
27
29
  },
28
30
  "repository": {
29
31
  "type": "git",
30
- "url": "https://github.com/chimurai/http-proxy-middleware.git"
32
+ "url": "git+https://github.com/chimurai/http-proxy-middleware.git"
31
33
  },
32
34
  "keywords": [
33
35
  "reverse",
@@ -54,44 +56,42 @@
54
56
  },
55
57
  "homepage": "https://github.com/chimurai/http-proxy-middleware#readme",
56
58
  "devDependencies": {
57
- "@commitlint/cli": "19.2.1",
58
- "@commitlint/config-conventional": "19.1.0",
59
+ "@commitlint/cli": "19.4.1",
60
+ "@commitlint/config-conventional": "19.4.1",
59
61
  "@types/debug": "4.1.12",
60
62
  "@types/express": "4.17.21",
61
63
  "@types/is-glob": "4.0.4",
62
64
  "@types/jest": "29.5.12",
63
- "@types/micromatch": "4.0.6",
64
- "@types/node": "20.12.5",
65
+ "@types/micromatch": "4.0.9",
66
+ "@types/node": "22.5.1",
65
67
  "@types/supertest": "6.0.2",
66
- "@types/ws": "8.5.10",
67
- "@typescript-eslint/eslint-plugin": "7.6.0",
68
- "@typescript-eslint/parser": "7.6.0",
68
+ "@types/ws": "8.5.12",
69
+ "@typescript-eslint/eslint-plugin": "7.16.0",
70
+ "@typescript-eslint/parser": "7.16.0",
69
71
  "body-parser": "1.20.2",
70
- "browser-sync": "3.0.2",
71
- "connect": "3.7.0",
72
72
  "eslint": "8.57.0",
73
73
  "eslint-config-prettier": "9.1.0",
74
- "eslint-plugin-prettier": "5.1.3",
74
+ "eslint-plugin-prettier": "5.2.1",
75
75
  "express": "4.19.2",
76
76
  "get-port": "5.1.1",
77
- "husky": "9.0.11",
77
+ "husky": "9.1.5",
78
78
  "jest": "29.7.0",
79
- "lint-staged": "15.2.2",
80
- "mockttp": "3.10.2",
79
+ "lint-staged": "15.2.9",
80
+ "mockttp": "3.15.2",
81
81
  "open": "8.4.2",
82
- "prettier": "3.2.5",
83
- "supertest": "6.3.4",
84
- "ts-jest": "29.1.2",
85
- "typescript": "5.4.4",
86
- "ws": "8.16.0"
82
+ "prettier": "3.3.3",
83
+ "supertest": "7.0.0",
84
+ "ts-jest": "29.2.5",
85
+ "typescript": "5.5.4",
86
+ "ws": "8.18.0"
87
87
  },
88
88
  "dependencies": {
89
- "@types/http-proxy": "^1.17.10",
90
- "debug": "^4.3.4",
89
+ "@types/http-proxy": "^1.17.15",
90
+ "debug": "^4.3.6",
91
91
  "http-proxy": "^1.18.1",
92
- "is-glob": "^4.0.1",
92
+ "is-glob": "^4.0.3",
93
93
  "is-plain-obj": "^3.0.0",
94
- "micromatch": "^4.0.5"
94
+ "micromatch": "^4.0.8"
95
95
  },
96
96
  "engines": {
97
97
  "node": "^14.15.0 || ^16.10.0 || >=18.0.0"