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

chimurai / http-proxy-middleware / 14581171176

21 Apr 2025 08:50PM CUT coverage: 97.129%. Remained the same
14581171176

Pull #1112

github

web-flow
Merge 1e8b517eb into d1fb42501
Pull Request #1112: build(codespaces): add devcontainer.json

137 of 143 branches covered (95.8%)

406 of 418 relevant lines covered (97.13%)

24.65 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

3
import { URL } from 'url';
12✔
4

5
import { getLogger } from '../../logger';
12✔
6
import { Plugin } from '../../types';
7
import { getPort } from '../../utils/logger-plugin';
12✔
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) => {
12✔
23
  const logger = getLogger(options);
75✔
24

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

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

33
    logger.error(errorMessage, requestHref, targetHref, (err as any).code || err, errReference);
6✔
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) => {
75✔
44
    // BrowserSync uses req.originalUrl
45
    // Next.js doesn't have req.baseUrl
46
    const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
43✔
47

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

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

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

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

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

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

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

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

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