-
Notifications
You must be signed in to change notification settings - Fork 869
/
Copy pathproxy-events.ts
32 lines (30 loc) · 1.04 KB
/
proxy-events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Debug } from '../../debug';
import { Plugin } from '../../types';
import { getFunctionName } from '../../utils/function';
const debug = Debug.extend('proxy-events-plugin');
/**
* Implements option.on object to subscribe to http-proxy events.
*
* @example
* ```js
* createProxyMiddleware({
* on: {
* error: (error, req, res, target) => {},
* proxyReq: (proxyReq, req, res, options) => {},
* proxyReqWs: (proxyReq, req, socket, options) => {},
* proxyRes: (proxyRes, req, res) => {},
* open: (proxySocket) => {},
* close: (proxyRes, proxySocket, proxyHead) => {},
* start: (req, res, target) => {},
* end: (req, res, proxyRes) => {},
* econnreset: (error, req, res, target) => {},
* }
* });
* ```
*/
export const proxyEventsPlugin: Plugin = (proxyServer, options) => {
Object.entries(options.on || {}).forEach(([eventName, handler]) => {
debug(`register event handler: "${eventName}" -> "${getFunctionName(handler)}"`);
proxyServer.on(eventName, handler as (...args: unknown[]) => void);
});
};