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

chimurai / http-proxy-middleware / 19394606062

15 Nov 2025 07:36PM UTC coverage: 97.15%. Remained the same
19394606062

push

github

web-flow
chore(package.json): bump dev deps (#1153)

170 of 183 branches covered (92.9%)

409 of 421 relevant lines covered (97.15%)

24.55 hits per line

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

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

4
import { getLogger } from '../../logger';
12✔
5
import { Plugin } from '../../types';
6
import { getPort } from '../../utils/logger-plugin';
12✔
7

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

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

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

21
export const loggerPlugin: Plugin = (proxyServer, options) => {
12✔
22
  const logger = getLogger(options);
75✔
23

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

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

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

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

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

50
    try {
43✔
51
      const port = getPort(proxyRes.req?.agent?.sockets);
43✔
52

53
      const obj = {
43✔
54
        protocol: proxyRes.req.protocol,
55
        host: proxyRes.req.host,
56
        pathname: proxyRes.req.path,
57
      } as URL;
58

59
      target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
43✔
60

61
      if (port) {
43!
62
        target.port = port;
43✔
63
      }
64
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
    } catch (err) {
66
      // nock issue (https://github.com/chimurai/http-proxy-middleware/issues/1035)
67
      // fallback to old implementation (less correct - without port)
68
      target = new URL(options.target as URL);
×
69
      target.pathname = proxyRes.req.path;
×
70
    }
71

72
    const targetUrl = target.toString();
43✔
73

74
    const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
43✔
75
    logger.info(exchange);
43✔
76
  });
77

78
  /**
79
   * When client opens WebSocket connection
80
   */
81
  proxyServer.on('open', (socket) => {
75✔
82
    logger.info('[HPM] Client connected: %o', socket.address());
3✔
83
  });
84

85
  /**
86
   * When client closes WebSocket connection
87
   */
88
  proxyServer.on('close', (req, proxySocket, proxyHead) => {
75✔
89
    logger.info('[HPM] Client disconnected: %o', proxySocket.address());
3✔
90
  });
91
};
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

© 2025 Coveralls, Inc