• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chimurai / http-proxy-middleware / 24956483470

26 Apr 2026 12:09PM UTC coverage: 94.07% (-0.7%) from 94.78%
24956483470

push

github

web-flow
fix(logger-plugin): support ipv6 host and handle undefined protocol/host (#1215)

* fix(logger-plugin): support ipv6 host and handle undefined protocol/host

* test: mock http target with msw and nock

* test(logger-plugin): assert console.info() in nock/msw

179 of 198 branches covered (90.4%)

10 of 11 new or added lines in 2 files covered. (90.91%)

2 existing lines in 1 file now uncovered.

349 of 371 relevant lines covered (94.07%)

38.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.0
/src/plugins/default/logger-plugin.ts
1
import type { ClientRequest, IncomingMessage } from 'node:http';
2
import { URL } from 'node:url';
3

4
import { getLogger } from '../../logger.js';
5
import type { Plugin } from '../../types.js';
6
import { createUrl } from '../../utils/create-url.js';
7
import { getPort } from '../../utils/logger-plugin.js';
8

9
type ExpressRequest = {
10
  /** Express req.baseUrl */
11
  baseUrl?: string;
12
};
13

14
type BrowserSyncRequest = {
15
  /** BrowserSync req.originalUrl */
16
  originalUrl?: string;
17
};
18

19
/** Request Types from different server libs */
20
type FrameworkRequest = IncomingMessage & ExpressRequest & BrowserSyncRequest;
21

22
export const loggerPlugin: Plugin = (proxyServer, options) => {
17✔
23
  const logger = getLogger(options);
108✔
24

25
  proxyServer.on('error', (err, req, res, target?) => {
108✔
26
    const hostname = req?.headers?.host;
7✔
27
    const requestHref = `${hostname}${req?.url}`;
7✔
28
    const targetHref = `${(target as unknown as any)?.href}`; // target is undefined when websocket errors
7✔
29

30
    const errorMessage = '[HPM] Error occurred while proxying request %s to %s [%s] (%s)';
7✔
31
    const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
7✔
32

33
    logger.error(errorMessage, requestHref, targetHref, (err as any).code || err, errReference);
7✔
34
  });
35

36
  /**
37
   * Log request and response
38
   * @example
39
   * ```shell
40
   * [HPM] GET /users/ -> http://jsonplaceholder.typicode.com/users/ [304]
41
   * ```
42
   */
43
  proxyServer.on('proxyRes', (proxyRes: any, req: FrameworkRequest, res) => {
108✔
44
    // BrowserSync uses req.originalUrl
45
    // Next.js doesn't have req.baseUrl
46
    const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
65✔
47

48
    // construct targetUrl
49
    let target: URL;
50

51
    try {
65✔
52
      const port = getPort(proxyRes.req?.agent?.sockets);
65✔
53
      const { protocol, host, path } = proxyRes.req as ClientRequest;
65✔
54

55
      target = createUrl({ protocol, host, port, path });
65✔
56
    } catch (err) {
57
      // should not error. keeping fallback just in case
NEW
58
      console.error('[HPM] Unexpected error while creating target URL', err);
×
59
      // fallback to old implementation (less correct - without port)
UNCOV
60
      target = new URL(options.target as URL);
×
UNCOV
61
      target.pathname = proxyRes.req.path;
×
62
    }
63

64
    const targetUrl = target.toString();
65✔
65

66
    const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
65✔
67
    logger.info(exchange);
65✔
68
  });
69

70
  /**
71
   * When client opens WebSocket connection
72
   */
73
  proxyServer.on('open', (socket) => {
108✔
74
    logger.info('[HPM] Client connected: %o', socket.address());
4✔
75
  });
76

77
  /**
78
   * When client closes WebSocket connection
79
   */
80
  proxyServer.on('close', (req, proxySocket, proxyHead) => {
108✔
81
    logger.info('[HPM] Client disconnected: %o', proxySocket.address());
4✔
82
  });
83
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc