| [9af201e] | 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.getHandlers = exports.init = void 0;
|
|---|
| 4 | const logger_1 = require("./logger");
|
|---|
| 5 | const logger = (0, logger_1.getInstance)();
|
|---|
| 6 | function init(proxy, option) {
|
|---|
| 7 | const handlers = getHandlers(option);
|
|---|
| 8 | for (const eventName of Object.keys(handlers)) {
|
|---|
| 9 | proxy.on(eventName, handlers[eventName]);
|
|---|
| 10 | }
|
|---|
| 11 | // https://github.com/webpack/webpack-dev-server/issues/1642
|
|---|
| 12 | proxy.on('econnreset', (error, req, res, target) => {
|
|---|
| 13 | logger.error(`[HPM] ECONNRESET: %O`, error);
|
|---|
| 14 | });
|
|---|
| 15 | // https://github.com/webpack/webpack-dev-server/issues/1642#issuecomment-1104325120
|
|---|
| 16 | proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
|
|---|
| 17 | socket.on('error', (error) => {
|
|---|
| 18 | logger.error(`[HPM] WebSocket error: %O`, error);
|
|---|
| 19 | });
|
|---|
| 20 | });
|
|---|
| 21 | logger.debug('[HPM] Subscribed to http-proxy events:', Object.keys(handlers));
|
|---|
| 22 | }
|
|---|
| 23 | exports.init = init;
|
|---|
| 24 | function getHandlers(options) {
|
|---|
| 25 | // https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
|
|---|
| 26 | const proxyEventsMap = {
|
|---|
| 27 | error: 'onError',
|
|---|
| 28 | proxyReq: 'onProxyReq',
|
|---|
| 29 | proxyReqWs: 'onProxyReqWs',
|
|---|
| 30 | proxyRes: 'onProxyRes',
|
|---|
| 31 | open: 'onOpen',
|
|---|
| 32 | close: 'onClose',
|
|---|
| 33 | };
|
|---|
| 34 | const handlers = {};
|
|---|
| 35 | for (const [eventName, onEventName] of Object.entries(proxyEventsMap)) {
|
|---|
| 36 | // all handlers for the http-proxy events are prefixed with 'on'.
|
|---|
| 37 | // loop through options and try to find these handlers
|
|---|
| 38 | // and add them to the handlers object for subscription in init().
|
|---|
| 39 | const fnHandler = options ? options[onEventName] : null;
|
|---|
| 40 | if (typeof fnHandler === 'function') {
|
|---|
| 41 | handlers[eventName] = fnHandler;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | // add default error handler in absence of error handler
|
|---|
| 45 | if (typeof handlers.error !== 'function') {
|
|---|
| 46 | handlers.error = defaultErrorHandler;
|
|---|
| 47 | }
|
|---|
| 48 | // add default close handler in absence of close handler
|
|---|
| 49 | if (typeof handlers.close !== 'function') {
|
|---|
| 50 | handlers.close = logClose;
|
|---|
| 51 | }
|
|---|
| 52 | return handlers;
|
|---|
| 53 | }
|
|---|
| 54 | exports.getHandlers = getHandlers;
|
|---|
| 55 | function defaultErrorHandler(err, req, res) {
|
|---|
| 56 | // Re-throw error. Not recoverable since req & res are empty.
|
|---|
| 57 | if (!req && !res) {
|
|---|
| 58 | throw err; // "Error: Must provide a proper URL as target"
|
|---|
| 59 | }
|
|---|
| 60 | const host = req.headers && req.headers.host;
|
|---|
| 61 | const code = err.code;
|
|---|
| 62 | if (res.writeHead && !res.headersSent) {
|
|---|
| 63 | if (/HPE_INVALID/.test(code)) {
|
|---|
| 64 | res.writeHead(502);
|
|---|
| 65 | }
|
|---|
| 66 | else {
|
|---|
| 67 | switch (code) {
|
|---|
| 68 | case 'ECONNRESET':
|
|---|
| 69 | case 'ENOTFOUND':
|
|---|
| 70 | case 'ECONNREFUSED':
|
|---|
| 71 | case 'ETIMEDOUT':
|
|---|
| 72 | res.writeHead(504);
|
|---|
| 73 | break;
|
|---|
| 74 | default:
|
|---|
| 75 | res.writeHead(500);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | res.end(`Error occurred while trying to proxy: ${host}${req.url}`);
|
|---|
| 80 | }
|
|---|
| 81 | function logClose(req, socket, head) {
|
|---|
| 82 | // view disconnected websocket connections
|
|---|
| 83 | logger.info('[HPM] Client disconnected');
|
|---|
| 84 | }
|
|---|