proxy-events.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.proxyEventsPlugin = void 0;
  4. const debug_1 = require("../../debug");
  5. const function_1 = require("../../utils/function");
  6. const debug = debug_1.Debug.extend('proxy-events-plugin');
  7. /**
  8. * Implements option.on object to subscribe to http-proxy events.
  9. *
  10. * @example
  11. * ```js
  12. * createProxyMiddleware({
  13. * on: {
  14. * error: (error, req, res, target) => {},
  15. * proxyReq: (proxyReq, req, res, options) => {},
  16. * proxyReqWs: (proxyReq, req, socket, options) => {},
  17. * proxyRes: (proxyRes, req, res) => {},
  18. * open: (proxySocket) => {},
  19. * close: (proxyRes, proxySocket, proxyHead) => {},
  20. * start: (req, res, target) => {},
  21. * end: (req, res, proxyRes) => {},
  22. * econnreset: (error, req, res, target) => {},
  23. * }
  24. * });
  25. * ```
  26. */
  27. const proxyEventsPlugin = (proxyServer, options) => {
  28. Object.entries(options.on || {}).forEach(([eventName, handler]) => {
  29. debug(`register event handler: "${eventName}" -> "${(0, function_1.getFunctionName)(handler)}"`);
  30. proxyServer.on(eventName, handler);
  31. });
  32. };
  33. exports.proxyEventsPlugin = proxyEventsPlugin;