debug-proxy-errors-plugin.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.debugProxyErrorsPlugin = void 0;
  4. const debug_1 = require("../../debug");
  5. const debug = debug_1.Debug.extend('debug-proxy-errors-plugin');
  6. /**
  7. * Subscribe to {@link https://www.npmjs.com/package/http-proxy#listening-for-proxy-events http-proxy error events} to prevent server from crashing.
  8. * Errors are logged with {@link https://www.npmjs.com/package/debug debug} library.
  9. */
  10. const debugProxyErrorsPlugin = (proxyServer) => {
  11. /**
  12. * http-proxy doesn't handle any errors by default (https://github.com/http-party/node-http-proxy#listening-for-proxy-events)
  13. * Prevent server from crashing when http-proxy errors (uncaught errors)
  14. */
  15. proxyServer.on('error', (error, req, res, target) => {
  16. debug(`http-proxy error event: \n%O`, error);
  17. });
  18. proxyServer.on('proxyReq', (proxyReq, req, socket) => {
  19. socket.on('error', (error) => {
  20. debug('Socket error in proxyReq event: \n%O', error);
  21. });
  22. });
  23. /**
  24. * Fix SSE close events
  25. * @link https://github.com/chimurai/http-proxy-middleware/issues/678
  26. * @link https://github.com/http-party/node-http-proxy/issues/1520#issue-877626125
  27. */
  28. proxyServer.on('proxyRes', (proxyRes, req, res) => {
  29. res.on('close', () => {
  30. if (!res.writableEnded) {
  31. debug('Destroying proxyRes in proxyRes close event');
  32. proxyRes.destroy();
  33. }
  34. });
  35. });
  36. /**
  37. * Fix crash when target server restarts
  38. * https://github.com/chimurai/http-proxy-middleware/issues/476#issuecomment-746329030
  39. * https://github.com/webpack/webpack-dev-server/issues/1642#issuecomment-790602225
  40. */
  41. proxyServer.on('proxyReqWs', (proxyReq, req, socket) => {
  42. socket.on('error', (error) => {
  43. debug('Socket error in proxyReqWs event: \n%O', error);
  44. });
  45. });
  46. proxyServer.on('open', (proxySocket) => {
  47. proxySocket.on('error', (error) => {
  48. debug('Socket error in open event: \n%O', error);
  49. });
  50. });
  51. proxyServer.on('close', (req, socket, head) => {
  52. socket.on('error', (error) => {
  53. debug('Socket error in close event: \n%O', error);
  54. });
  55. });
  56. // https://github.com/webpack/webpack-dev-server/issues/1642#issuecomment-1103136590
  57. proxyServer.on('econnreset', (error, req, res, target) => {
  58. debug(`http-proxy econnreset event: \n%O`, error);
  59. });
  60. };
  61. exports.debugProxyErrorsPlugin = debugProxyErrorsPlugin;