Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ejectPlugins): skip registering default plugins #750

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
- [`pathRewrite` (object/function)](#pathrewrite-objectfunction)
- [`router` (object/function)](#router-objectfunction)
- [`plugins` (Array)](#plugins-array)
- [`ejectPlugins` (boolean) default: `false`](#ejectplugins-boolean-default-false)
- [`logger` (Object)](#logger-object)
- [`http-proxy` events](#http-proxy-events)
- [`http-proxy` options](#http-proxy-options)
Expand Down Expand Up @@ -286,6 +287,30 @@ const config = {
};
```

### `ejectPlugins` (boolean) default: `false`

If you're not satisfied with the pre-configured plugins, you can eject them by configuring `ejectPlugins: true`.

NOTE: register your own error handlers to prevent server from crashing.

```js
// eject default plugins and manually add them back

const {
debugProxyErrorsPlugin, // subscribe to proxy errors to prevent server from crashing
loggerPlugin, // log proxy events to a logger (ie. console)
errorResponsePlugin, // return 5xx response on proxy error
proxyEventsPlugin, // implements the "on:" option
} = require('http-proxy-middleware/plugins/default');

createProxyMiddleware({
target: `http://example.org`,
changeOrigin: true,
ejectPlugins: true,
plugins: [debugProxyErrorsPlugin, loggerPlugin, errorResponsePlugin, proxyEventsPlugin],
});
```

### `logger` (Object)

Configure a logger to output information from http-proxy-middleware: ie. `console`, `winston`, `pino`, `bunyan`, `log4js`, etc...
Expand Down
15 changes: 15 additions & 0 deletions src/get-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Options, Plugin } from './types';
import {
debugProxyErrorsPlugin,
loggerPlugin,
errorResponsePlugin,
proxyEventsPlugin,
} from './plugins/default';

export function getPlugins(options: Options): Plugin[] {
const defaultPlugins: Plugin[] = !!options.ejectPlugins
? [] // no default plugins when ejecting
: [debugProxyErrorsPlugin, proxyEventsPlugin, loggerPlugin, errorResponsePlugin];
const userPlugins: Plugin[] = options.plugins ?? [];
return [...defaultPlugins, ...userPlugins];
}
17 changes: 3 additions & 14 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import type * as https from 'https';
import type { Request, RequestHandler, Options, Filter, Logger } from './types';
import * as httpProxy from 'http-proxy';
import { verifyConfig } from './configuration';
import { getPlugins } from './get-plugins';
import { matchPathFilter } from './path-filter';
import { getLogger } from './logger';
import * as PathRewriter from './path-rewriter';
import * as Router from './router';
import {
debugProxyErrorsPlugin,
loggerPlugin,
errorResponsePlugin,
proxyEventsPlugin,
} from './plugins/default';

export class HttpProxyMiddleware {
private logger: Logger;
Expand Down Expand Up @@ -81,14 +76,8 @@ export class HttpProxyMiddleware {
};

private registerPlugins(proxy: httpProxy, options: Options) {
const defaultPlugins = [
debugProxyErrorsPlugin,
proxyEventsPlugin,
loggerPlugin,
errorResponsePlugin,
];
const plugins = options.plugins ?? [];
[...defaultPlugins, ...plugins].forEach((plugin) => plugin(proxy, options));
const plugins = getPlugins(options);
plugins.forEach((plugin) => plugin(proxy, options));
}

private catchUpgradeRequest = (server: https.Server) => {
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export interface Options extends httpProxy.ServerOptions {
* ```
*/
plugins?: Plugin[];
/**
* Eject pre-configured plugins.
* NOTE: register your own error handlers to prevent server from crashing.
*
* @since v3.0.0
*/
ejectPlugins?: boolean;
/**
* Listen to http-proxy events
* @see {@link OnProxyEvent} for available events
Expand Down
88 changes: 88 additions & 0 deletions test/unit/get-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Plugin } from '../../src/types';
import { getPlugins } from '../../src/get-plugins';
import {
debugProxyErrorsPlugin,
loggerPlugin,
errorResponsePlugin,
proxyEventsPlugin,
} from '../../src/plugins/default';

describe('getPlugins', () => {
let plugins: Plugin[];

it('should return default plugins when no user plugins are provided', () => {
plugins = getPlugins({});

expect(plugins).toHaveLength(4);
expect(plugins.map((plugin) => plugin.name)).toMatchInlineSnapshot(`
Array [
"debugProxyErrorsPlugin",
"proxyEventsPlugin",
"loggerPlugin",
"errorResponsePlugin",
]
`);
});

it('should return no plugins when ejectPlugins is configured in option', () => {
plugins = getPlugins({
ejectPlugins: true,
});

expect(plugins).toHaveLength(0);
});

it('should return user plugins with default plugins when user plugins are provided', () => {
const myPlugin: Plugin = () => {
/* noop */
};
plugins = getPlugins({
plugins: [myPlugin],
});

expect(plugins).toHaveLength(5);
expect(plugins.map((plugin) => plugin.name)).toMatchInlineSnapshot(`
Array [
"debugProxyErrorsPlugin",
"proxyEventsPlugin",
"loggerPlugin",
"errorResponsePlugin",
"myPlugin",
]
`);
});

it('should only return user plugins when user plugins are provided with ejectPlugins option', () => {
const myPlugin: Plugin = () => {
/* noop */
};
plugins = getPlugins({
ejectPlugins: true,
plugins: [myPlugin],
});

expect(plugins).toHaveLength(1);
expect(plugins.map((plugin) => plugin.name)).toMatchInlineSnapshot(`
Array [
"myPlugin",
]
`);
});

it('should return manually added default plugins in different order after using ejectPlugins', () => {
plugins = getPlugins({
ejectPlugins: true,
plugins: [debugProxyErrorsPlugin, errorResponsePlugin, loggerPlugin, proxyEventsPlugin], // alphabetical order
});

expect(plugins).toHaveLength(4);
expect(plugins.map((plugin) => plugin.name)).toMatchInlineSnapshot(`
Array [
"debugProxyErrorsPlugin",
"errorResponsePlugin",
"loggerPlugin",
"proxyEventsPlugin",
]
`);
});
});