http-proxy-middleware 4.0.0-beta.0 → 4.0.0-beta.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.
- package/README.md +39 -39
- package/dist/configuration.d.ts +3 -2
- package/dist/configuration.js +3 -6
- package/dist/debug.d.ts +1 -1
- package/dist/debug.js +2 -5
- package/dist/errors.js +2 -5
- package/dist/factory.d.ts +2 -2
- package/dist/factory.js +3 -6
- package/dist/get-plugins.d.ts +3 -2
- package/dist/get-plugins.js +4 -7
- package/dist/handlers/fix-request-body.js +18 -7
- package/dist/handlers/index.d.ts +1 -1
- package/dist/handlers/index.js +1 -17
- package/dist/handlers/public.d.ts +2 -2
- package/dist/handlers/public.js +2 -7
- package/dist/handlers/response-interceptor.js +6 -9
- package/dist/http-proxy-middleware.d.ts +4 -3
- package/dist/http-proxy-middleware.js +30 -41
- package/dist/index.d.ts +4 -8
- package/dist/index.js +3 -23
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +1 -4
- package/dist/path-filter.d.ts +2 -2
- package/dist/path-filter.js +7 -11
- package/dist/path-rewriter.js +8 -11
- package/dist/plugins/default/debug-proxy-errors-plugin.d.ts +1 -1
- package/dist/plugins/default/debug-proxy-errors-plugin.js +3 -7
- package/dist/plugins/default/error-response-plugin.d.ts +1 -1
- package/dist/plugins/default/error-response-plugin.js +5 -9
- package/dist/plugins/default/index.d.ts +4 -4
- package/dist/plugins/default/index.js +4 -20
- package/dist/plugins/default/logger-plugin.d.ts +1 -1
- package/dist/plugins/default/logger-plugin.js +8 -12
- package/dist/plugins/default/proxy-events.d.ts +1 -1
- package/dist/plugins/default/proxy-events.js +5 -9
- package/dist/router.js +5 -8
- package/dist/status-code.js +1 -4
- package/dist/types.d.ts +5 -5
- package/dist/types.js +1 -2
- package/dist/utils/function.js +1 -4
- package/dist/utils/logger-plugin.js +1 -4
- package/dist/utils/sanitize.js +1 -4
- package/package.json +19 -12
- package/dist/legacy/create-proxy-middleware.d.ts +0 -12
- package/dist/legacy/create-proxy-middleware.js +0 -16
- package/dist/legacy/index.d.ts +0 -1
- package/dist/legacy/index.js +0 -17
- package/dist/legacy/options-adapter.d.ts +0 -6
- package/dist/legacy/options-adapter.js +0 -97
- package/dist/legacy/public.d.ts +0 -2
- package/dist/legacy/public.js +0 -5
- package/dist/legacy/types.d.ts +0 -111
- package/dist/legacy/types.js +0 -2
package/README.md
CHANGED
|
@@ -7,18 +7,17 @@
|
|
|
7
7
|
|
|
8
8
|
Node.js proxying made simple. Configure proxy middleware with ease for [connect](https://github.com/senchalabs/connect), [express](https://github.com/expressjs/express), [next.js](https://github.com/vercel/next.js) and [many more](#compatible-servers).
|
|
9
9
|
|
|
10
|
-
Powered by
|
|
10
|
+
Powered by [`httpxy`](https://github.com/unjs/httpxy). A maintained version of [http-proxy](https://github.com/http-party/node-http-proxy).
|
|
11
11
|
|
|
12
12
|
## ⚠️ Note <!-- omit in toc -->
|
|
13
13
|
|
|
14
|
-
This page is showing documentation for version
|
|
14
|
+
This page is showing documentation for version **v4.x.x** ([release notes](https://github.com/chimurai/http-proxy-middleware/releases))
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
For older documentation:
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
- <https://github.com/chimurai/http-proxy-middleware/tree/v0.21.0#readme>
|
|
18
|
+
- [v3.0.5](https://github.com/chimurai/http-proxy-middleware/tree/v3.0.5#readme)
|
|
19
|
+
- [v2.0.4](https://github.com/chimurai/http-proxy-middleware/tree/v2.0.4#readme)
|
|
20
|
+
- [v0.21.0](https://github.com/chimurai/http-proxy-middleware/tree/v0.21.0#readme)
|
|
22
21
|
|
|
23
22
|
## TL;DR <!-- omit in toc -->
|
|
24
23
|
|
|
@@ -28,7 +27,7 @@ Proxy `/api` requests to `http://www.example.org`
|
|
|
28
27
|
|
|
29
28
|
```typescript
|
|
30
29
|
// typescript
|
|
31
|
-
import
|
|
30
|
+
import express from 'express';
|
|
32
31
|
import type { NextFunction, Request, Response } from 'express';
|
|
33
32
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
34
33
|
import type { Filter, Options, RequestHandler } from 'http-proxy-middleware';
|
|
@@ -48,7 +47,7 @@ app.listen(3000);
|
|
|
48
47
|
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar
|
|
49
48
|
```
|
|
50
49
|
|
|
51
|
-
_All_ `
|
|
50
|
+
_All_ `httpxy` [options](https://github.com/unjs/httpxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).
|
|
52
51
|
|
|
53
52
|
## Table of Contents <!-- omit in toc -->
|
|
54
53
|
|
|
@@ -65,8 +64,8 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
|
|
|
65
64
|
- [`plugins` (Array)](#plugins-array)
|
|
66
65
|
- [`ejectPlugins` (boolean) default: `false`](#ejectplugins-boolean-default-false)
|
|
67
66
|
- [`logger` (Object)](#logger-object)
|
|
68
|
-
- [`
|
|
69
|
-
- [`
|
|
67
|
+
- [`httpxy` events](#httpxy-events)
|
|
68
|
+
- [`httpxy` options](#httpxy-options)
|
|
70
69
|
- [WebSocket](#websocket)
|
|
71
70
|
- [External WebSocket upgrade](#external-websocket-upgrade)
|
|
72
71
|
- [Intercept and manipulate requests](#intercept-and-manipulate-requests)
|
|
@@ -93,7 +92,7 @@ npm install --save-dev http-proxy-middleware
|
|
|
93
92
|
Create and configure a proxy middleware with: `createProxyMiddleware(config)`.
|
|
94
93
|
|
|
95
94
|
```javascript
|
|
96
|
-
|
|
95
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
97
96
|
|
|
98
97
|
const apiProxy = createProxyMiddleware({
|
|
99
98
|
target: 'http://www.example.org',
|
|
@@ -114,13 +113,13 @@ An example with `express` server.
|
|
|
114
113
|
|
|
115
114
|
```javascript
|
|
116
115
|
// include dependencies
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
import express from 'express';
|
|
117
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
119
118
|
|
|
120
119
|
const app = express();
|
|
121
120
|
|
|
122
121
|
// create the proxy
|
|
123
|
-
/** @type {import('http-proxy-middleware
|
|
122
|
+
/** @type {import('http-proxy-middleware').RequestHandler<import('express').Request, import('express').Response>} */
|
|
124
123
|
const exampleProxy = createProxyMiddleware({
|
|
125
124
|
target: 'http://www.example.org/api', // target host with the same base path
|
|
126
125
|
changeOrigin: true, // needed for virtual hosted sites
|
|
@@ -281,13 +280,15 @@ NOTE: register your own error handlers to prevent server from crashing.
|
|
|
281
280
|
|
|
282
281
|
```js
|
|
283
282
|
// eject default plugins and manually add them back
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
283
|
+
import {
|
|
284
|
+
debugProxyErrorsPlugin,
|
|
285
|
+
// log proxy events to a logger (ie. console)
|
|
286
|
+
errorResponsePlugin,
|
|
287
|
+
// subscribe to proxy errors to prevent server from crashing
|
|
288
|
+
loggerPlugin,
|
|
289
|
+
// return 5xx response on proxy error
|
|
289
290
|
proxyEventsPlugin, // implements the "on:" option
|
|
290
|
-
}
|
|
291
|
+
} from 'http-proxy-middleware';
|
|
291
292
|
|
|
292
293
|
createProxyMiddleware({
|
|
293
294
|
target: `http://example.org`,
|
|
@@ -313,9 +314,9 @@ createProxyMiddleware({
|
|
|
313
314
|
});
|
|
314
315
|
```
|
|
315
316
|
|
|
316
|
-
## `
|
|
317
|
+
## `httpxy` events
|
|
317
318
|
|
|
318
|
-
Subscribe to [
|
|
319
|
+
Subscribe to [httpxy events](https://github.com/unjs/httpxy#events) with the `on` option:
|
|
319
320
|
|
|
320
321
|
```js
|
|
321
322
|
createProxyMiddleware({
|
|
@@ -354,7 +355,7 @@ createProxyMiddleware({
|
|
|
354
355
|
}
|
|
355
356
|
```
|
|
356
357
|
|
|
357
|
-
- **option.on.proxyReq**: function, subscribe to
|
|
358
|
+
- **option.on.proxyReq**: function, subscribe to httpxy's `proxyReq` event.
|
|
358
359
|
|
|
359
360
|
```javascript
|
|
360
361
|
function onProxyReq(proxyReq, req, res) {
|
|
@@ -364,7 +365,7 @@ createProxyMiddleware({
|
|
|
364
365
|
}
|
|
365
366
|
```
|
|
366
367
|
|
|
367
|
-
- **option.on.proxyReqWs**: function, subscribe to
|
|
368
|
+
- **option.on.proxyReqWs**: function, subscribe to httpxy's `proxyReqWs` event.
|
|
368
369
|
|
|
369
370
|
```javascript
|
|
370
371
|
function onProxyReqWs(proxyReq, req, socket, options, head) {
|
|
@@ -373,7 +374,7 @@ createProxyMiddleware({
|
|
|
373
374
|
}
|
|
374
375
|
```
|
|
375
376
|
|
|
376
|
-
- **option.on.open**: function, subscribe to
|
|
377
|
+
- **option.on.open**: function, subscribe to httpxy's `open` event.
|
|
377
378
|
|
|
378
379
|
```javascript
|
|
379
380
|
function onOpen(proxySocket) {
|
|
@@ -382,7 +383,7 @@ createProxyMiddleware({
|
|
|
382
383
|
}
|
|
383
384
|
```
|
|
384
385
|
|
|
385
|
-
- **option.on.close**: function, subscribe to
|
|
386
|
+
- **option.on.close**: function, subscribe to httpxy's `close` event.
|
|
386
387
|
|
|
387
388
|
```javascript
|
|
388
389
|
function onClose(res, socket, head) {
|
|
@@ -391,9 +392,9 @@ createProxyMiddleware({
|
|
|
391
392
|
}
|
|
392
393
|
```
|
|
393
394
|
|
|
394
|
-
## `
|
|
395
|
+
## `httpxy` options
|
|
395
396
|
|
|
396
|
-
The following options are provided by the underlying [
|
|
397
|
+
The following options are provided by the underlying [httpxy](https://github.com/unjs/httpxy#options) library.
|
|
397
398
|
|
|
398
399
|
- **option.target**: url string to be parsed with the url module
|
|
399
400
|
- **option.forward**: url string to be parsed with the url module
|
|
@@ -448,13 +449,12 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
448
449
|
- **option.buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
|
|
449
450
|
|
|
450
451
|
```javascript
|
|
451
|
-
|
|
452
|
+
import { createProxyServer } from 'httpxy';
|
|
453
|
+
import streamify from 'stream-array';
|
|
452
454
|
|
|
453
|
-
const
|
|
454
|
-
const HttpProxy = require('http-proxy');
|
|
455
|
-
const proxy = new HttpProxy();
|
|
455
|
+
const proxy = createProxyServer();
|
|
456
456
|
|
|
457
|
-
|
|
457
|
+
export default function proxyWithBody(req, res, next) {
|
|
458
458
|
proxy.web(
|
|
459
459
|
req,
|
|
460
460
|
res,
|
|
@@ -464,7 +464,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
464
464
|
},
|
|
465
465
|
next,
|
|
466
466
|
);
|
|
467
|
-
}
|
|
467
|
+
}
|
|
468
468
|
```
|
|
469
469
|
|
|
470
470
|
## WebSocket
|
|
@@ -497,7 +497,7 @@ Currently the only pre-provided request interceptor is `fixRequestBody`, which i
|
|
|
497
497
|
Example:
|
|
498
498
|
|
|
499
499
|
```javascript
|
|
500
|
-
|
|
500
|
+
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
|
|
501
501
|
|
|
502
502
|
const proxy = createProxyMiddleware({
|
|
503
503
|
/**
|
|
@@ -522,7 +522,7 @@ NOTE: `responseInterceptor` disables streaming of target's response.
|
|
|
522
522
|
Example:
|
|
523
523
|
|
|
524
524
|
```javascript
|
|
525
|
-
|
|
525
|
+
import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware';
|
|
526
526
|
|
|
527
527
|
const proxy = createProxyMiddleware({
|
|
528
528
|
/**
|
|
@@ -623,7 +623,7 @@ $ yarn build
|
|
|
623
623
|
$ yarn test
|
|
624
624
|
|
|
625
625
|
# code coverage
|
|
626
|
-
$ yarn
|
|
626
|
+
$ yarn coverage
|
|
627
627
|
|
|
628
628
|
# check spelling mistakes
|
|
629
629
|
$ yarn spellcheck
|
|
@@ -637,4 +637,4 @@ $ yarn spellcheck
|
|
|
637
637
|
|
|
638
638
|
The MIT License (MIT)
|
|
639
639
|
|
|
640
|
-
Copyright (c) 2015-
|
|
640
|
+
Copyright (c) 2015-2026 Steven Chim
|
package/dist/configuration.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
import { Options } from './types.js';
|
|
3
|
+
export declare function verifyConfig<TReq extends http.IncomingMessage, TRes extends http.ServerResponse>(options: Options<TReq, TRes>): void;
|
package/dist/configuration.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.verifyConfig = verifyConfig;
|
|
4
|
-
const errors_1 = require("./errors");
|
|
5
|
-
function verifyConfig(options) {
|
|
1
|
+
import { ERRORS } from './errors.js';
|
|
2
|
+
export function verifyConfig(options) {
|
|
6
3
|
if (!options.target && !options.router) {
|
|
7
|
-
throw new Error(
|
|
4
|
+
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
|
|
8
5
|
}
|
|
9
6
|
}
|
package/dist/debug.d.ts
CHANGED
package/dist/debug.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Debug = void 0;
|
|
4
|
-
const createDebug = require("debug");
|
|
1
|
+
import createDebug from 'debug';
|
|
5
2
|
/**
|
|
6
3
|
* Debug instance with the given namespace: http-proxy-middleware
|
|
7
4
|
*/
|
|
8
|
-
|
|
5
|
+
export const Debug = createDebug('http-proxy-middleware');
|
package/dist/errors.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ERRORS = void 0;
|
|
4
|
-
var ERRORS;
|
|
1
|
+
export var ERRORS;
|
|
5
2
|
(function (ERRORS) {
|
|
6
3
|
ERRORS["ERR_CONFIG_FACTORY_TARGET_MISSING"] = "[HPM] Missing \"target\" option. Example: {target: \"http://www.example.org\"}";
|
|
7
4
|
ERRORS["ERR_CONTEXT_MATCHER_GENERIC"] = "[HPM] Invalid pathFilter. Expecting something like: \"/api\" or [\"/api\", \"/ajax\"]";
|
|
8
5
|
ERRORS["ERR_CONTEXT_MATCHER_INVALID_ARRAY"] = "[HPM] Invalid pathFilter. Plain paths (e.g. \"/api\") can not be mixed with globs (e.g. \"/api/**\"). Expecting something like: [\"/api\", \"/ajax\"] or [\"/api/**\", \"!**.html\"].";
|
|
9
6
|
ERRORS["ERR_PATH_REWRITER_CONFIG"] = "[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function";
|
|
10
|
-
})(ERRORS || (
|
|
7
|
+
})(ERRORS || (ERRORS = {}));
|
package/dist/factory.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type * as http from 'node:http';
|
|
2
|
-
import type { NextFunction, Options, RequestHandler } from './types';
|
|
3
|
-
export declare function createProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
|
|
2
|
+
import type { NextFunction, Options, RequestHandler } from './types.js';
|
|
3
|
+
export declare function createProxyMiddleware<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
|
package/dist/factory.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const http_proxy_middleware_1 = require("./http-proxy-middleware");
|
|
5
|
-
function createProxyMiddleware(options) {
|
|
6
|
-
const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(options);
|
|
1
|
+
import { HttpProxyMiddleware } from './http-proxy-middleware.js';
|
|
2
|
+
export function createProxyMiddleware(options) {
|
|
3
|
+
const { middleware } = new HttpProxyMiddleware(options);
|
|
7
4
|
return middleware;
|
|
8
5
|
}
|
package/dist/get-plugins.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
import type { Options, Plugin } from './types.js';
|
|
3
|
+
export declare function getPlugins<TReq extends http.IncomingMessage, TRes extends http.ServerResponse>(options: Options<TReq, TRes>): Plugin<TReq, TRes>[];
|
package/dist/get-plugins.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.getPlugins = getPlugins;
|
|
4
|
-
const default_1 = require("./plugins/default");
|
|
5
|
-
function getPlugins(options) {
|
|
1
|
+
import { debugProxyErrorsPlugin, errorResponsePlugin, loggerPlugin, proxyEventsPlugin, } from './plugins/default/index.js';
|
|
2
|
+
export function getPlugins(options) {
|
|
6
3
|
// don't load default errorResponsePlugin if user has specified their own
|
|
7
|
-
const maybeErrorResponsePlugin = options.on?.error ? [] : [
|
|
4
|
+
const maybeErrorResponsePlugin = options.on?.error ? [] : [errorResponsePlugin];
|
|
8
5
|
const defaultPlugins = options.ejectPlugins
|
|
9
6
|
? [] // no default plugins when ejecting
|
|
10
|
-
: [
|
|
7
|
+
: [debugProxyErrorsPlugin, proxyEventsPlugin, loggerPlugin, ...maybeErrorResponsePlugin];
|
|
11
8
|
const userPlugins = options.plugins ?? [];
|
|
12
9
|
return [...defaultPlugins, ...userPlugins];
|
|
13
10
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.fixRequestBody = fixRequestBody;
|
|
4
|
-
const querystring = require("node:querystring");
|
|
1
|
+
import * as querystring from 'node:querystring';
|
|
2
|
+
import * as zlib from 'node:zlib';
|
|
5
3
|
/**
|
|
6
4
|
* Fix proxied body if bodyParser is involved.
|
|
7
5
|
*/
|
|
8
|
-
function fixRequestBody(proxyReq, req) {
|
|
6
|
+
export function fixRequestBody(proxyReq, req) {
|
|
9
7
|
// skip fixRequestBody() when req.readableLength not 0 (bodyParser failure)
|
|
10
8
|
if (req.readableLength !== 0) {
|
|
11
9
|
return;
|
|
@@ -19,8 +17,21 @@ function fixRequestBody(proxyReq, req) {
|
|
|
19
17
|
return;
|
|
20
18
|
}
|
|
21
19
|
const writeBody = (bodyData) => {
|
|
22
|
-
|
|
23
|
-
proxyReq.
|
|
20
|
+
let proxyData = bodyData;
|
|
21
|
+
const contentEncoding = String(proxyReq.getHeader('Content-Encoding')).toLowerCase();
|
|
22
|
+
switch (contentEncoding) {
|
|
23
|
+
case 'br':
|
|
24
|
+
proxyData = zlib.brotliCompressSync(proxyData);
|
|
25
|
+
break;
|
|
26
|
+
case 'deflate':
|
|
27
|
+
proxyData = zlib.deflateSync(proxyData);
|
|
28
|
+
break;
|
|
29
|
+
case 'gzip':
|
|
30
|
+
proxyData = zlib.gzipSync(proxyData);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
proxyReq.setHeader('Content-Length', Buffer.byteLength(proxyData));
|
|
34
|
+
proxyReq.write(proxyData);
|
|
24
35
|
};
|
|
25
36
|
// Use if-elseif to prevent multiple writeBody/setHeader calls:
|
|
26
37
|
// Error: "Cannot set headers after they are sent to the client"
|
package/dist/handlers/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './public';
|
|
1
|
+
export * from './public.js';
|
package/dist/handlers/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./public"), exports);
|
|
1
|
+
export * from './public.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { responseInterceptor } from './response-interceptor';
|
|
2
|
-
export { fixRequestBody } from './fix-request-body';
|
|
1
|
+
export { responseInterceptor } from './response-interceptor.js';
|
|
2
|
+
export { fixRequestBody } from './fix-request-body.js';
|
package/dist/handlers/public.js
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.fixRequestBody = exports.responseInterceptor = void 0;
|
|
4
|
-
var response_interceptor_1 = require("./response-interceptor");
|
|
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; } });
|
|
1
|
+
export { responseInterceptor } from './response-interceptor.js';
|
|
2
|
+
export { fixRequestBody } from './fix-request-body.js';
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const debug_1 = require("../debug");
|
|
6
|
-
const function_1 = require("../utils/function");
|
|
7
|
-
const debug = debug_1.Debug.extend('response-interceptor');
|
|
1
|
+
import * as zlib from 'node:zlib';
|
|
2
|
+
import { Debug } from '../debug.js';
|
|
3
|
+
import { getFunctionName } from '../utils/function.js';
|
|
4
|
+
const debug = Debug.extend('response-interceptor');
|
|
8
5
|
/**
|
|
9
6
|
* Intercept responses from upstream.
|
|
10
7
|
* Automatically decompress (deflate, gzip, brotli).
|
|
@@ -12,7 +9,7 @@ const debug = debug_1.Debug.extend('response-interceptor');
|
|
|
12
9
|
*
|
|
13
10
|
* NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
|
|
14
11
|
*/
|
|
15
|
-
function responseInterceptor(interceptor) {
|
|
12
|
+
export function responseInterceptor(interceptor) {
|
|
16
13
|
return async function proxyResResponseInterceptor(proxyRes, req, res) {
|
|
17
14
|
debug('intercept proxy response');
|
|
18
15
|
const originalProxyRes = proxyRes;
|
|
@@ -25,7 +22,7 @@ function responseInterceptor(interceptor) {
|
|
|
25
22
|
// copy original headers
|
|
26
23
|
copyHeaders(proxyRes, res);
|
|
27
24
|
// call interceptor with intercepted response (buffer)
|
|
28
|
-
debug('call interceptor function: %s',
|
|
25
|
+
debug('call interceptor function: %s', getFunctionName(interceptor));
|
|
29
26
|
const interceptedBuffer = Buffer.from(await interceptor(buffer, originalProxyRes, req, res));
|
|
30
27
|
// set correct content-length (with double byte character support)
|
|
31
28
|
debug('set content-length: %s', Buffer.byteLength(interceptedBuffer, 'utf8'));
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
import type { Options, RequestHandler } from './types.js';
|
|
3
|
+
export declare class HttpProxyMiddleware<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse> {
|
|
3
4
|
private wsInternalSubscribed;
|
|
4
5
|
private serverOnCloseSubscribed;
|
|
5
6
|
private proxyOptions;
|
|
@@ -7,7 +8,7 @@ export declare class HttpProxyMiddleware<TReq, TRes> {
|
|
|
7
8
|
private pathRewriter;
|
|
8
9
|
private logger;
|
|
9
10
|
constructor(options: Options<TReq, TRes>);
|
|
10
|
-
middleware: RequestHandler
|
|
11
|
+
middleware: RequestHandler<TReq, TRes>;
|
|
11
12
|
private registerPlugins;
|
|
12
13
|
private catchUpgradeRequest;
|
|
13
14
|
private handleUpgrade;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const Router = require("./router");
|
|
12
|
-
const function_1 = require("./utils/function");
|
|
13
|
-
class HttpProxyMiddleware {
|
|
1
|
+
import { createProxyServer } from 'httpxy';
|
|
2
|
+
import { verifyConfig } from './configuration.js';
|
|
3
|
+
import { Debug as debug } from './debug.js';
|
|
4
|
+
import { getPlugins } from './get-plugins.js';
|
|
5
|
+
import { getLogger } from './logger.js';
|
|
6
|
+
import { matchPathFilter } from './path-filter.js';
|
|
7
|
+
import * as PathRewriter from './path-rewriter.js';
|
|
8
|
+
import * as Router from './router.js';
|
|
9
|
+
import { getFunctionName } from './utils/function.js';
|
|
10
|
+
export class HttpProxyMiddleware {
|
|
14
11
|
wsInternalSubscribed = false;
|
|
15
12
|
serverOnCloseSubscribed = false;
|
|
16
13
|
proxyOptions;
|
|
@@ -18,11 +15,11 @@ class HttpProxyMiddleware {
|
|
|
18
15
|
pathRewriter;
|
|
19
16
|
logger;
|
|
20
17
|
constructor(options) {
|
|
21
|
-
|
|
18
|
+
verifyConfig(options);
|
|
22
19
|
this.proxyOptions = options;
|
|
23
|
-
this.logger =
|
|
24
|
-
(
|
|
25
|
-
this.proxy =
|
|
20
|
+
this.logger = getLogger(options);
|
|
21
|
+
debug(`create proxy server`);
|
|
22
|
+
this.proxy = createProxyServer({});
|
|
26
23
|
this.registerPlugins(this.proxy, this.proxyOptions);
|
|
27
24
|
this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
|
|
28
25
|
// https://github.com/chimurai/http-proxy-middleware/issues/19
|
|
@@ -54,7 +51,7 @@ class HttpProxyMiddleware {
|
|
|
54
51
|
}
|
|
55
52
|
try {
|
|
56
53
|
// Proxying Phase: Handle the actual web request.
|
|
57
|
-
(
|
|
54
|
+
debug(`proxy request to target: %O`, activeProxyOptions.target);
|
|
58
55
|
await this.proxy.web(req, res, activeProxyOptions);
|
|
59
56
|
}
|
|
60
57
|
catch (err) {
|
|
@@ -82,28 +79,28 @@ class HttpProxyMiddleware {
|
|
|
82
79
|
const server = req.socket?.server;
|
|
83
80
|
if (server && !this.serverOnCloseSubscribed) {
|
|
84
81
|
server.on('close', () => {
|
|
85
|
-
(
|
|
82
|
+
debug('server close signal received: closing proxy server');
|
|
86
83
|
this.proxy.close(() => {
|
|
87
|
-
(
|
|
84
|
+
debug('proxy server closed');
|
|
88
85
|
});
|
|
89
86
|
});
|
|
90
87
|
this.serverOnCloseSubscribed = true;
|
|
91
88
|
}
|
|
92
|
-
if (this.proxyOptions.ws === true) {
|
|
89
|
+
if (this.proxyOptions.ws === true && server) {
|
|
93
90
|
// use initial request to access the server object to subscribe to http upgrade event
|
|
94
91
|
this.catchUpgradeRequest(server);
|
|
95
92
|
}
|
|
96
93
|
});
|
|
97
94
|
registerPlugins(proxy, options) {
|
|
98
|
-
const plugins =
|
|
95
|
+
const plugins = getPlugins(options);
|
|
99
96
|
plugins.forEach((plugin) => {
|
|
100
|
-
(
|
|
97
|
+
debug(`register plugin: "${getFunctionName(plugin)}"`);
|
|
101
98
|
plugin(proxy, options);
|
|
102
99
|
});
|
|
103
100
|
}
|
|
104
101
|
catchUpgradeRequest = (server) => {
|
|
105
102
|
if (!this.wsInternalSubscribed) {
|
|
106
|
-
(
|
|
103
|
+
debug('subscribing to server upgrade event');
|
|
107
104
|
server.on('upgrade', this.handleUpgrade);
|
|
108
105
|
// prevent duplicate upgrade handling;
|
|
109
106
|
// in case external upgrade is also configured
|
|
@@ -113,9 +110,10 @@ class HttpProxyMiddleware {
|
|
|
113
110
|
handleUpgrade = async (req, socket, head) => {
|
|
114
111
|
try {
|
|
115
112
|
if (this.shouldProxy(this.proxyOptions.pathFilter, req)) {
|
|
116
|
-
const
|
|
117
|
-
await this.
|
|
118
|
-
|
|
113
|
+
const proxiedReq = req;
|
|
114
|
+
const activeProxyOptions = await this.prepareProxyRequest(proxiedReq);
|
|
115
|
+
await this.proxy.ws(proxiedReq, socket, activeProxyOptions, head);
|
|
116
|
+
debug('server upgrade event received. Proxying WebSocket');
|
|
119
117
|
}
|
|
120
118
|
}
|
|
121
119
|
catch (err) {
|
|
@@ -136,10 +134,10 @@ class HttpProxyMiddleware {
|
|
|
136
134
|
*/
|
|
137
135
|
shouldProxy = (pathFilter, req) => {
|
|
138
136
|
try {
|
|
139
|
-
return
|
|
137
|
+
return matchPathFilter(pathFilter, req.url, req);
|
|
140
138
|
}
|
|
141
139
|
catch (err) {
|
|
142
|
-
(
|
|
140
|
+
debug('Error: matchPathFilter() called with request url: ', `"${req.url}"`);
|
|
143
141
|
this.logger.error(err);
|
|
144
142
|
return false;
|
|
145
143
|
}
|
|
@@ -153,14 +151,6 @@ class HttpProxyMiddleware {
|
|
|
153
151
|
* @return {Object} proxy options
|
|
154
152
|
*/
|
|
155
153
|
prepareProxyRequest = async (req) => {
|
|
156
|
-
/**
|
|
157
|
-
* Incorrect usage confirmed: https://github.com/expressjs/express/issues/4854#issuecomment-1066171160
|
|
158
|
-
* Temporary restore req.url patch for {@link src/legacy/create-proxy-middleware.ts legacyCreateProxyMiddleware()}
|
|
159
|
-
* FIXME: remove this patch in future release
|
|
160
|
-
*/
|
|
161
|
-
if (this.middleware.__LEGACY_HTTP_PROXY_MIDDLEWARE__) {
|
|
162
|
-
req.url = req.originalUrl || req.url;
|
|
163
|
-
}
|
|
164
154
|
const newProxyOptions = Object.assign({}, this.proxyOptions);
|
|
165
155
|
// Apply in order:
|
|
166
156
|
// 1. option.router
|
|
@@ -175,7 +165,7 @@ class HttpProxyMiddleware {
|
|
|
175
165
|
if (options.router) {
|
|
176
166
|
newTarget = await Router.getTarget(req, options);
|
|
177
167
|
if (newTarget) {
|
|
178
|
-
(
|
|
168
|
+
debug('router new target: "%s"', newTarget);
|
|
179
169
|
options.target = newTarget;
|
|
180
170
|
}
|
|
181
171
|
}
|
|
@@ -185,13 +175,12 @@ class HttpProxyMiddleware {
|
|
|
185
175
|
if (pathRewriter) {
|
|
186
176
|
const path = await pathRewriter(req.url, req);
|
|
187
177
|
if (typeof path === 'string') {
|
|
188
|
-
(
|
|
178
|
+
debug('pathRewrite new path: %s', path);
|
|
189
179
|
req.url = path;
|
|
190
180
|
}
|
|
191
181
|
else {
|
|
192
|
-
(
|
|
182
|
+
debug('pathRewrite: no rewritten path found: %s', req.url);
|
|
193
183
|
}
|
|
194
184
|
}
|
|
195
185
|
};
|
|
196
186
|
}
|
|
197
|
-
exports.HttpProxyMiddleware = HttpProxyMiddleware;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
export * from './factory';
|
|
2
|
-
export * from './handlers';
|
|
3
|
-
export type { Plugin, Filter, Options, RequestHandler } from './types';
|
|
1
|
+
export * from './factory.js';
|
|
2
|
+
export * from './handlers/index.js';
|
|
3
|
+
export type { Plugin, Filter, Options, RequestHandler } from './types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Default plugins
|
|
6
6
|
*/
|
|
7
|
-
export * from './plugins/default';
|
|
8
|
-
/**
|
|
9
|
-
* Legacy exports
|
|
10
|
-
*/
|
|
11
|
-
export * from './legacy';
|
|
7
|
+
export * from './plugins/default/index.js';
|