http-proxy-middleware 3.0.5 → 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 +42 -49
- 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 +3 -3
- 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.d.ts +1 -1
- package/dist/handlers/fix-request-body.js +21 -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.d.ts +3 -3
- package/dist/handlers/response-interceptor.js +6 -9
- package/dist/http-proxy-middleware.d.ts +4 -3
- package/dist/http-proxy-middleware.js +165 -140
- 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 +3 -3
- 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 +6 -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 +20 -12
- package/dist/router.js +5 -8
- package/dist/status-code.js +1 -4
- package/dist/types.d.ts +21 -21
- package/dist/types.js +1 -6
- package/dist/utils/function.js +1 -4
- package/dist/utils/logger-plugin.js +1 -4
- package/dist/utils/sanitize.d.ts +1 -0
- package/dist/utils/sanitize.js +3 -0
- package/package.json +44 -36
- 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,10 +27,8 @@ Proxy `/api` requests to `http://www.example.org`
|
|
|
28
27
|
|
|
29
28
|
```typescript
|
|
30
29
|
// typescript
|
|
31
|
-
|
|
32
|
-
import
|
|
33
|
-
import type { Request, Response, NextFunction } from 'express';
|
|
34
|
-
|
|
30
|
+
import express from 'express';
|
|
31
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
35
32
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
36
33
|
import type { Filter, Options, RequestHandler } from 'http-proxy-middleware';
|
|
37
34
|
|
|
@@ -50,7 +47,7 @@ app.listen(3000);
|
|
|
50
47
|
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar
|
|
51
48
|
```
|
|
52
49
|
|
|
53
|
-
_All_ `
|
|
50
|
+
_All_ `httpxy` [options](https://github.com/unjs/httpxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).
|
|
54
51
|
|
|
55
52
|
## Table of Contents <!-- omit in toc -->
|
|
56
53
|
|
|
@@ -67,8 +64,8 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
|
|
|
67
64
|
- [`plugins` (Array)](#plugins-array)
|
|
68
65
|
- [`ejectPlugins` (boolean) default: `false`](#ejectplugins-boolean-default-false)
|
|
69
66
|
- [`logger` (Object)](#logger-object)
|
|
70
|
-
- [`
|
|
71
|
-
- [`
|
|
67
|
+
- [`httpxy` events](#httpxy-events)
|
|
68
|
+
- [`httpxy` options](#httpxy-options)
|
|
72
69
|
- [WebSocket](#websocket)
|
|
73
70
|
- [External WebSocket upgrade](#external-websocket-upgrade)
|
|
74
71
|
- [Intercept and manipulate requests](#intercept-and-manipulate-requests)
|
|
@@ -95,7 +92,7 @@ npm install --save-dev http-proxy-middleware
|
|
|
95
92
|
Create and configure a proxy middleware with: `createProxyMiddleware(config)`.
|
|
96
93
|
|
|
97
94
|
```javascript
|
|
98
|
-
|
|
95
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
99
96
|
|
|
100
97
|
const apiProxy = createProxyMiddleware({
|
|
101
98
|
target: 'http://www.example.org',
|
|
@@ -116,13 +113,13 @@ An example with `express` server.
|
|
|
116
113
|
|
|
117
114
|
```javascript
|
|
118
115
|
// include dependencies
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
import express from 'express';
|
|
117
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
121
118
|
|
|
122
119
|
const app = express();
|
|
123
120
|
|
|
124
121
|
// create the proxy
|
|
125
|
-
/** @type {import('http-proxy-middleware
|
|
122
|
+
/** @type {import('http-proxy-middleware').RequestHandler<import('express').Request, import('express').Response>} */
|
|
126
123
|
const exampleProxy = createProxyMiddleware({
|
|
127
124
|
target: 'http://www.example.org/api', // target host with the same base path
|
|
128
125
|
changeOrigin: true, // needed for virtual hosted sites
|
|
@@ -163,18 +160,15 @@ http-proxy-middleware options:
|
|
|
163
160
|
Narrow down which requests should be proxied. The `path` used for filtering is the `request.url` pathname. In Express, this is the `path` relative to the mount-point of the proxy.
|
|
164
161
|
|
|
165
162
|
- **path matching**
|
|
166
|
-
|
|
167
163
|
- `createProxyMiddleware({...})` - matches any path, all requests will be proxied when `pathFilter` is not configured.
|
|
168
164
|
- `createProxyMiddleware({ pathFilter: '/api', ...})` - matches paths starting with `/api`
|
|
169
165
|
|
|
170
166
|
- **multiple path matching**
|
|
171
|
-
|
|
172
167
|
- `createProxyMiddleware({ pathFilter: ['/api', '/ajax', '/someotherpath'], ...})`
|
|
173
168
|
|
|
174
169
|
- **wildcard path matching**
|
|
175
170
|
|
|
176
171
|
For fine-grained control you can use wildcard matching. Glob pattern matching is done by _micromatch_. Visit [micromatch](https://www.npmjs.com/package/micromatch) or [glob](https://www.npmjs.com/package/glob) for more globbing examples.
|
|
177
|
-
|
|
178
172
|
- `createProxyMiddleware({ pathFilter: '**', ...})` matches any path, all requests will be proxied.
|
|
179
173
|
- `createProxyMiddleware({ pathFilter: '**/*.html', ...})` matches any path which ends with `.html`
|
|
180
174
|
- `createProxyMiddleware({ pathFilter: '/*.html', ...})` matches paths directly under path-absolute
|
|
@@ -286,13 +280,15 @@ NOTE: register your own error handlers to prevent server from crashing.
|
|
|
286
280
|
|
|
287
281
|
```js
|
|
288
282
|
// eject default plugins and manually add them back
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
|
294
290
|
proxyEventsPlugin, // implements the "on:" option
|
|
295
|
-
}
|
|
291
|
+
} from 'http-proxy-middleware';
|
|
296
292
|
|
|
297
293
|
createProxyMiddleware({
|
|
298
294
|
target: `http://example.org`,
|
|
@@ -318,9 +314,9 @@ createProxyMiddleware({
|
|
|
318
314
|
});
|
|
319
315
|
```
|
|
320
316
|
|
|
321
|
-
## `
|
|
317
|
+
## `httpxy` events
|
|
322
318
|
|
|
323
|
-
Subscribe to [
|
|
319
|
+
Subscribe to [httpxy events](https://github.com/unjs/httpxy#events) with the `on` option:
|
|
324
320
|
|
|
325
321
|
```js
|
|
326
322
|
createProxyMiddleware({
|
|
@@ -359,7 +355,7 @@ createProxyMiddleware({
|
|
|
359
355
|
}
|
|
360
356
|
```
|
|
361
357
|
|
|
362
|
-
- **option.on.proxyReq**: function, subscribe to
|
|
358
|
+
- **option.on.proxyReq**: function, subscribe to httpxy's `proxyReq` event.
|
|
363
359
|
|
|
364
360
|
```javascript
|
|
365
361
|
function onProxyReq(proxyReq, req, res) {
|
|
@@ -369,7 +365,7 @@ createProxyMiddleware({
|
|
|
369
365
|
}
|
|
370
366
|
```
|
|
371
367
|
|
|
372
|
-
- **option.on.proxyReqWs**: function, subscribe to
|
|
368
|
+
- **option.on.proxyReqWs**: function, subscribe to httpxy's `proxyReqWs` event.
|
|
373
369
|
|
|
374
370
|
```javascript
|
|
375
371
|
function onProxyReqWs(proxyReq, req, socket, options, head) {
|
|
@@ -378,7 +374,7 @@ createProxyMiddleware({
|
|
|
378
374
|
}
|
|
379
375
|
```
|
|
380
376
|
|
|
381
|
-
- **option.on.open**: function, subscribe to
|
|
377
|
+
- **option.on.open**: function, subscribe to httpxy's `open` event.
|
|
382
378
|
|
|
383
379
|
```javascript
|
|
384
380
|
function onOpen(proxySocket) {
|
|
@@ -387,7 +383,7 @@ createProxyMiddleware({
|
|
|
387
383
|
}
|
|
388
384
|
```
|
|
389
385
|
|
|
390
|
-
- **option.on.close**: function, subscribe to
|
|
386
|
+
- **option.on.close**: function, subscribe to httpxy's `close` event.
|
|
391
387
|
|
|
392
388
|
```javascript
|
|
393
389
|
function onClose(res, socket, head) {
|
|
@@ -396,9 +392,9 @@ createProxyMiddleware({
|
|
|
396
392
|
}
|
|
397
393
|
```
|
|
398
394
|
|
|
399
|
-
## `
|
|
395
|
+
## `httpxy` options
|
|
400
396
|
|
|
401
|
-
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.
|
|
402
398
|
|
|
403
399
|
- **option.target**: url string to be parsed with the url module
|
|
404
400
|
- **option.forward**: url string to be parsed with the url module
|
|
@@ -418,13 +414,12 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
418
414
|
- **option.autoRewrite**: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false.
|
|
419
415
|
- **option.protocolRewrite**: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.
|
|
420
416
|
- **option.cookieDomainRewrite**: rewrites domain of `set-cookie` headers. Possible values:
|
|
421
|
-
|
|
422
417
|
- `false` (default): disable cookie rewriting
|
|
423
418
|
- String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
|
|
424
419
|
- Object: mapping of domains to new domains, use `"*"` to match all domains.
|
|
425
420
|
For example keep one domain unchanged, rewrite one domain and remove other domains:
|
|
426
421
|
|
|
427
|
-
```
|
|
422
|
+
```jsonc
|
|
428
423
|
cookieDomainRewrite: {
|
|
429
424
|
"unchanged.domain": "unchanged.domain",
|
|
430
425
|
"old.domain": "new.domain",
|
|
@@ -433,13 +428,12 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
433
428
|
```
|
|
434
429
|
|
|
435
430
|
- **option.cookiePathRewrite**: rewrites path of `set-cookie` headers. Possible values:
|
|
436
|
-
|
|
437
431
|
- `false` (default): disable cookie rewriting
|
|
438
432
|
- String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
|
|
439
433
|
- Object: mapping of paths to new paths, use `"*"` to match all paths.
|
|
440
434
|
For example, to keep one path unchanged, rewrite one path and remove other paths:
|
|
441
435
|
|
|
442
|
-
```
|
|
436
|
+
```jsonc
|
|
443
437
|
cookiePathRewrite: {
|
|
444
438
|
"/unchanged.path/": "/unchanged.path/",
|
|
445
439
|
"/old.path/": "/new.path/",
|
|
@@ -455,13 +449,12 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
455
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:
|
|
456
450
|
|
|
457
451
|
```javascript
|
|
458
|
-
|
|
452
|
+
import { createProxyServer } from 'httpxy';
|
|
453
|
+
import streamify from 'stream-array';
|
|
459
454
|
|
|
460
|
-
const
|
|
461
|
-
const HttpProxy = require('http-proxy');
|
|
462
|
-
const proxy = new HttpProxy();
|
|
455
|
+
const proxy = createProxyServer();
|
|
463
456
|
|
|
464
|
-
|
|
457
|
+
export default function proxyWithBody(req, res, next) {
|
|
465
458
|
proxy.web(
|
|
466
459
|
req,
|
|
467
460
|
res,
|
|
@@ -471,7 +464,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
471
464
|
},
|
|
472
465
|
next,
|
|
473
466
|
);
|
|
474
|
-
}
|
|
467
|
+
}
|
|
475
468
|
```
|
|
476
469
|
|
|
477
470
|
## WebSocket
|
|
@@ -504,7 +497,7 @@ Currently the only pre-provided request interceptor is `fixRequestBody`, which i
|
|
|
504
497
|
Example:
|
|
505
498
|
|
|
506
499
|
```javascript
|
|
507
|
-
|
|
500
|
+
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
|
|
508
501
|
|
|
509
502
|
const proxy = createProxyMiddleware({
|
|
510
503
|
/**
|
|
@@ -529,7 +522,7 @@ NOTE: `responseInterceptor` disables streaming of target's response.
|
|
|
529
522
|
Example:
|
|
530
523
|
|
|
531
524
|
```javascript
|
|
532
|
-
|
|
525
|
+
import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware';
|
|
533
526
|
|
|
534
527
|
const proxy = createProxyMiddleware({
|
|
535
528
|
/**
|
|
@@ -630,7 +623,7 @@ $ yarn build
|
|
|
630
623
|
$ yarn test
|
|
631
624
|
|
|
632
625
|
# code coverage
|
|
633
|
-
$ yarn
|
|
626
|
+
$ yarn coverage
|
|
634
627
|
|
|
635
628
|
# check spelling mistakes
|
|
636
629
|
$ yarn spellcheck
|
|
@@ -644,4 +637,4 @@ $ yarn spellcheck
|
|
|
644
637
|
|
|
645
638
|
The MIT License (MIT)
|
|
646
639
|
|
|
647
|
-
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
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
export declare function createProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
|
|
1
|
+
import type * as http from 'node:http';
|
|
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("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"
|
|
@@ -33,6 +44,9 @@ function fixRequestBody(proxyReq, req) {
|
|
|
33
44
|
else if (contentType.includes('multipart/form-data')) {
|
|
34
45
|
writeBody(handlerFormDataBodyData(contentType, requestBody));
|
|
35
46
|
}
|
|
47
|
+
else if (contentType.includes('text/plain')) {
|
|
48
|
+
writeBody(requestBody);
|
|
49
|
+
}
|
|
36
50
|
}
|
|
37
51
|
/**
|
|
38
52
|
* format FormData data
|
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,5 +1,5 @@
|
|
|
1
|
-
import type * as http from 'http';
|
|
2
|
-
type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buffer: Buffer, proxyRes:
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buffer: Buffer, proxyRes: http.IncomingMessage, req: TReq, res: TRes) => Promise<Buffer | string>;
|
|
3
3
|
/**
|
|
4
4
|
* Intercept responses from upstream.
|
|
5
5
|
* Automatically decompress (deflate, gzip, brotli).
|
|
@@ -7,5 +7,5 @@ type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buf
|
|
|
7
7
|
*
|
|
8
8
|
* NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
|
|
9
9
|
*/
|
|
10
|
-
export declare function responseInterceptor<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse>(interceptor: Interceptor<TReq, TRes>): (proxyRes:
|
|
10
|
+
export declare function responseInterceptor<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse>(interceptor: Interceptor<TReq, TRes>): (proxyRes: http.IncomingMessage, req: TReq, res: TRes) => Promise<void>;
|
|
11
11
|
export {};
|
|
@@ -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;
|