Skip to content

Commit e870e81

Browse files
committed
refactor(proxy events): refactor to proxy-events plugin [BREAKING CHANGE]
1 parent 495eb88 commit e870e81

13 files changed

+193
-244
lines changed

README.md

+26-9
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ router: async function(req) {
274274
### `plugins` (Array)
275275

276276
```js
277-
const simpleRequestLogger = (proxy, options) => {
278-
proxy.on('proxyReq', (proxyReq, req, res) => {
277+
const simpleRequestLogger = (proxyServer, options) => {
278+
proxyServer.on('proxyReq', (proxyReq, req, res) => {
279279
console.log(`[HPM] [${req.method}] ${req.url}`); // outputs: [HPM] GET /users
280280
});
281281
},
@@ -323,9 +323,26 @@ function logProvider(provider) {
323323

324324
## `http-proxy` events
325325

326-
Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events):
326+
Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events) with the `on` option:
327327

328-
- **option.onError**: function, subscribe to http-proxy's `error` event for custom error handling.
328+
```js
329+
createProxyMiddleware({
330+
target: 'http://www.example.org',
331+
on: {
332+
proxyReq: (proxyReq, req, res) => {
333+
/* handle proxyReq */
334+
},
335+
proxyRes: (proxyRes, req, res) => {
336+
/* handle proxyRes */
337+
},
338+
error: (err, req, res) => {
339+
/* handle error */
340+
},
341+
},
342+
});
343+
```
344+
345+
- **option.on.error**: function, subscribe to http-proxy's `error` event for custom error handling.
329346

330347
```javascript
331348
function onError(err, req, res, target) {
@@ -336,7 +353,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
336353
}
337354
```
338355

339-
- **option.onProxyRes**: function, subscribe to http-proxy's `proxyRes` event.
356+
- **option.on.proxyRes**: function, subscribe to http-proxy's `proxyRes` event.
340357

341358
```javascript
342359
function onProxyRes(proxyRes, req, res) {
@@ -345,7 +362,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
345362
}
346363
```
347364

348-
- **option.onProxyReq**: function, subscribe to http-proxy's `proxyReq` event.
365+
- **option.on.proxyReq**: function, subscribe to http-proxy's `proxyReq` event.
349366

350367
```javascript
351368
function onProxyReq(proxyReq, req, res) {
@@ -355,7 +372,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
355372
}
356373
```
357374

358-
- **option.onProxyReqWs**: function, subscribe to http-proxy's `proxyReqWs` event.
375+
- **option.on.proxyReqWs**: function, subscribe to http-proxy's `proxyReqWs` event.
359376

360377
```javascript
361378
function onProxyReqWs(proxyReq, req, socket, options, head) {
@@ -364,7 +381,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
364381
}
365382
```
366383

367-
- **option.onOpen**: function, subscribe to http-proxy's `open` event.
384+
- **option.on.open**: function, subscribe to http-proxy's `open` event.
368385

369386
```javascript
370387
function onOpen(proxySocket) {
@@ -373,7 +390,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
373390
}
374391
```
375392

376-
- **option.onClose**: function, subscribe to http-proxy's `close` event.
393+
- **option.on.close**: function, subscribe to http-proxy's `close` event.
377394

378395
```javascript
379396
function onClose(res, socket, head) {

examples/response-interceptor/index.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,25 @@ const jsonPlaceholderProxy = createProxyMiddleware({
4545
},
4646
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
4747
selfHandleResponse: true, // manually call res.end(); IMPORTANT: res.end() is called internally by responseInterceptor()
48-
onProxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
49-
// log original request and proxied request info
50-
const exchange = `[DEBUG] ${req.method} ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path} [${proxyRes.statusCode}]`;
51-
console.log(exchange);
48+
on: {
49+
proxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
50+
// log original request and proxied request info
51+
const exchange = `[DEBUG] ${req.method} ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path} [${proxyRes.statusCode}]`;
52+
console.log(exchange);
5253

53-
// log original response
54-
// console.log(`[DEBUG] original response:\n${buffer.toString('utf8')}`);
54+
// log original response
55+
// console.log(`[DEBUG] original response:\n${buffer.toString('utf8')}`);
5556

56-
// set response content-type
57-
res.setHeader('content-type', 'application/json; charset=utf-8');
57+
// set response content-type
58+
res.setHeader('content-type', 'application/json; charset=utf-8');
5859

59-
// set response status code
60-
res.statusCode = 418;
60+
// set response status code
61+
res.statusCode = 418;
6162

62-
// return a complete different response
63-
return JSON.stringify(favoriteFoods);
64-
}),
63+
// return a complete different response
64+
return JSON.stringify(favoriteFoods);
65+
}),
66+
},
6567
logLevel: 'debug',
6668
});
6769

recipes/proxy-events.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Proxy Events
22

3-
Subscribe to [`http-proxy`](https://github.com/nodejitsu/node-http-proxy) [![GitHub stars](https://img.shields.io/github/stars/nodejitsu/node-http-proxy.svg?style=social&label=Star)](https://github.com/nodejitsu/node-http-proxy) events: `error`, `proxyReq`, `proxyReqWs`, `proxyRes`, `open`, `close`.
3+
Subscribe to [`http-proxy`](https://github.com/nodejitsu/node-http-proxy) [![GitHub stars](https://img.shields.io/github/stars/nodejitsu/node-http-proxy.svg?style=social&label=Star)](https://github.com/nodejitsu/node-http-proxy) events: `error`, `proxyReq`, `proxyReqWs`, `proxyRes`, `open`, `close`, `start`, `end`, `econnreset`.
44

5-
## onError
5+
## on.error
66

77
Subscribe to http-proxy's [error event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
88

@@ -14,12 +14,15 @@ const onError = function (err, req, res) {
1414
console.log('And we are reporting a custom error message.');
1515
};
1616

17-
const options = { target: 'http://localhost:3000', onError: onError };
17+
const options = {
18+
target: 'http://localhost:3000',
19+
on: { 'error', onError }
20+
};
1821

1922
const apiProxy = createProxyMiddleware(options);
2023
```
2124

22-
## onProxyReq
25+
## on.proxyReq
2326

2427
Subscribe to http-proxy's [proxyReq event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
2528

@@ -31,12 +34,15 @@ const onProxyReq = function (proxyReq, req, res) {
3134
proxyReq.setHeader('x-added', 'foobar');
3235
};
3336

34-
const options = { target: 'http://localhost:3000', onProxyReq: onProxyReq };
37+
const options = {
38+
target: 'http://localhost:3000',
39+
on: { 'proxyReq', onProxyReq }
40+
};
3541

3642
const apiProxy = createProxyMiddleware(options);
3743
```
3844

39-
## onProxyReqWs
45+
## on.proxyReqWs
4046

4147
Subscribe to http-proxy's [proxyReqWs event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
4248

@@ -48,12 +54,15 @@ const onProxyReqWs = function (proxyReq, req, socket, options, head) {
4854
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
4955
};
5056

51-
const options = { target: 'http://localhost:3000', onProxyReqWs: onProxyReqWs };
57+
const options = {
58+
target: 'http://localhost:3000',
59+
on: { 'proxyReqWs', onProxyReqWs }
60+
};
5261

5362
const apiProxy = createProxyMiddleware(options);
5463
```
5564

56-
## onProxyRes
65+
## on.proxyRes
5766

5867
Subscribe to http-proxy's [proxyRes event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
5968

@@ -68,7 +77,10 @@ const onProxyRes = function (proxyRes, req, res) {
6877
delete proxyRes.headers['x-removed'];
6978
};
7079

71-
const options = { target: 'http://localhost:3000', onProxyRes: onProxyRes };
80+
const options = {
81+
target: 'http://localhost:3000',
82+
on: { 'proxyRes', onProxyRes }
83+
};
7284

7385
const apiProxy = createProxyMiddleware(options);
7486
```

src/_handlers.ts

-66
This file was deleted.

src/http-proxy-middleware.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import type { Request, RequestHandler, Options, Filter } from './types';
33
import * as httpProxy from 'http-proxy';
44
import { verifyConfig } from './configuration';
55
import { matchPathFilter } from './path-filter';
6-
import * as handlers from './_handlers';
76
import { getArrow, getInstance } from './logger';
87
import * as PathRewriter from './path-rewriter';
98
import * as Router from './router';
10-
import { debugProxyErrorsPlugin, createLoggerPlugin, errorResponsePlugin } from './plugins/default';
9+
import {
10+
debugProxyErrorsPlugin,
11+
createLoggerPlugin,
12+
errorResponsePlugin,
13+
proxyEventsPlugin,
14+
} from './plugins/default';
1115

1216
export class HttpProxyMiddleware {
1317
private logger = getInstance();
@@ -29,9 +33,6 @@ export class HttpProxyMiddleware {
2933

3034
this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
3135

32-
// attach handler to http-proxy events
33-
handlers.init(this.proxy, this.proxyOptions);
34-
3536
// https://github.com/chimurai/http-proxy-middleware/issues/19
3637
// expose function to upgrade externally
3738
this.middleware.upgrade = (req, socket, head) => {
@@ -79,7 +80,12 @@ export class HttpProxyMiddleware {
7980
};
8081

8182
private registerPlugins(proxy: httpProxy, options: Options) {
82-
const defaultPlugins = [debugProxyErrorsPlugin, createLoggerPlugin(), errorResponsePlugin];
83+
const defaultPlugins = [
84+
debugProxyErrorsPlugin,
85+
proxyEventsPlugin,
86+
createLoggerPlugin(),
87+
errorResponsePlugin,
88+
];
8389
const plugins = options.plugins ?? [];
8490
[...defaultPlugins, ...plugins].forEach((plugin) => plugin(proxy, options));
8591
}

src/plugins/default/debug-proxy-errors-plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { debug } from '../../debug';
22
import { Plugin } from '../../types';
33

4-
const debugError = debug.extend('error-handlers-plugin');
4+
const debugError = debug.extend('debug-proxy-errors-plugin');
55

66
/**
77
* Subscribe to {@link https://www.npmjs.com/package/http-proxy#listening-for-proxy-events http-proxy error events} to prevent server from crashing.

src/plugins/default/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './debug-proxy-errors-plugin';
22
export * from './error-response-plugin';
33
export * from './logger-plugin';
4+
export * from './proxy-events';

src/plugins/default/proxy-events.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { debug } from '../../debug';
2+
import { Plugin } from '../../types';
3+
4+
const log = debug.extend('proxy-events-plugin');
5+
6+
/**
7+
* Implements option.on object to subscribe to http-proxy events.
8+
*
9+
* @example
10+
* ```js
11+
* createProxyMiddleware({
12+
* on: {
13+
* error: (error, req, res, target) => {},
14+
* proxyReq: (proxyReq, req, res, options) => {},
15+
* proxyReqWs: (proxyReq, req, socket, options) => {},
16+
* proxyRes: (proxyRes, req, res) => {},
17+
* open: (proxySocket) => {},
18+
* close: (proxyRes, proxySocket, proxyHead) => {},
19+
* start: (req, res, target) => {},
20+
* end: (req, res, proxyRes) => {},
21+
* econnreset: (error, req, res, target) => {},
22+
* }
23+
* });
24+
* ```
25+
*/
26+
export const proxyEventsPlugin: Plugin = (proxyServer, options) => {
27+
Object.entries(options.on || {}).forEach(([eventName, handler]) => {
28+
proxyServer.on(eventName, handler as (...args: unknown[]) => void);
29+
log(`registered event handler: "${eventName}"`);
30+
});
31+
};

0 commit comments

Comments
 (0)