Skip to content

Commit

Permalink
feat(ejectPlugins): skip loading default plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Apr 15, 2022
1 parent 4bc66b9 commit bbe5c53
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 14 deletions.
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`

Eject pre-configured plugins.

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

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

const {
debugProxyErrorsPlugin,
loggerPlugin,
errorResponsePlugin,
proxyEventsPlugin,
} = 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",
]
`);
});
});

0 comments on commit bbe5c53

Please sign in to comment.