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

cameri / nostream / 25529283840

08 May 2026 12:17AM UTC coverage: 64.439% (-0.5%) from 64.937%
25529283840

Pull #615

github

web-flow
Merge fb553e964 into 6c8e29a01
Pull Request #615: test: add unit tests for remaining app workers (#489)

1801 of 3166 branches covered (56.89%)

Branch coverage included in aggregate %.

4148 of 6066 relevant lines covered (68.38%)

8.66 hits per line

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

93.14
/src/utils/http.ts
1
import { IncomingMessage } from 'http'
2

3
import { createLogger } from '../factories/logger-factory'
1✔
4
import { Settings } from '../@types/settings'
5

6
const logger = createLogger('http-utils')
1✔
7

8
const normalizeIpAddress = (input: string): string => {
1✔
9
  if (input.startsWith('::ffff:')) {
20✔
10
    return input.slice(7)
1✔
11
  }
12

13
  return input
19✔
14
}
15

16
const isTrustedProxy = (ipAddress: string, settings: Settings): boolean => {
1✔
17
  const trustedProxies = settings.network?.trustedProxies
59✔
18

19
  if (!Array.isArray(trustedProxies) || trustedProxies.length === 0) {
59✔
20
    return false
49✔
21
  }
22

23
  const normalizedRemote = normalizeIpAddress(ipAddress)
10✔
24

25
  return trustedProxies.some((trustedProxy) => {
10✔
26
    return normalizeIpAddress(trustedProxy) === normalizedRemote
10✔
27
  })
28
}
29

30
export const getRemoteAddress = (request: IncomingMessage, settings: Settings): string => {
1✔
31
  let header: string | undefined
32
  // TODO: Remove deprecation warning
33
  if ('network' in settings && 'remote_ip_header' in settings.network) {
51✔
34
    logger.warn(`WARNING: Setting network.remote_ip_header is deprecated and will be removed in a future version.
1✔
35
        Use network.remoteIpHeader instead.`)
36
    header = settings.network['remote_ip_header'] as string
1✔
37
  } else {
38
    header = settings.network.remoteIpHeader as string
50✔
39
  }
40

41
  const trustedProxies = settings.network?.trustedProxies
51✔
42
  if (header && (!Array.isArray(trustedProxies) || trustedProxies.length === 0)) {
51✔
43
    logger.warn('WARNING: network.remoteIpHeader is set but network.trustedProxies is empty. Forwarded headers will be ignored. Add your proxy IP to network.trustedProxies.')
2✔
44
  }
45

46
  const rawHeaderAddress = header ? request.headers[header] : undefined
51✔
47
  const headerAddress = Array.isArray(rawHeaderAddress) ? rawHeaderAddress[0] : rawHeaderAddress
51✔
48
  const socketAddress = request.socket.remoteAddress
51✔
49

50
  const trustedProxy = typeof socketAddress === 'string'
51✔
51
    && isTrustedProxy(socketAddress, settings)
52

53
  const result = trustedProxy && typeof headerAddress === 'string'
51✔
54
    ? headerAddress
55
    : socketAddress
56

57
  return (result as string).split(',')[0].trim()
51✔
58
}
59

60
const normalizePathPrefix = (pathPrefix: string | undefined): string => {
1✔
61
  if (typeof pathPrefix !== 'string') {
47!
62
    return ''
×
63
  }
64

65
  const prefix = pathPrefix.split(',')[0].trim()
47✔
66

67
  if (!prefix.startsWith('/') || prefix.startsWith('//')) {
47✔
68
    return ''
2✔
69
  }
70

71
  try {
45✔
72
    const { pathname } = new URL(prefix, 'http://nostream.local')
45✔
73
    const normalized = pathname.replace(/\/+$/, '')
45✔
74

75
    return normalized === '/' ? '' : normalized
45!
76
  } catch {
77
    return ''
×
78
  }
79
}
80

81
const getRelayUrlPathPrefix = (relayUrl: string | undefined): string => {
1✔
82
  if (typeof relayUrl !== 'string') {
52✔
83
    return ''
9✔
84
  }
85

86
  try {
43✔
87
    return normalizePathPrefix(new URL(relayUrl).pathname)
43✔
88
  } catch {
89
    return ''
×
90
  }
91
}
92

93
const getTrustedForwardedPathPrefix = (request: IncomingMessage, settings: Settings): string => {
1✔
94
  const socketAddress = request.socket?.remoteAddress
54✔
95
  if (typeof socketAddress !== 'string' || !isTrustedProxy(socketAddress, settings)) {
54✔
96
    return ''
50✔
97
  }
98

99
  const rawHeader = request.headers?.['x-forwarded-prefix']
4✔
100
  const rawPrefix = Array.isArray(rawHeader) ? rawHeader[0] : rawHeader
4!
101

102
  return normalizePathPrefix(rawPrefix)
4✔
103
}
104

105
export const getPublicPathPrefix = (request: IncomingMessage, settings: Settings): string => {
1✔
106
  return getTrustedForwardedPathPrefix(request, settings) || getRelayUrlPathPrefix(settings.info?.relay_url)
54✔
107
}
108

109
export const joinPathPrefix = (prefix: string, path: string): string => {
1✔
110
  const normalizedPrefix = prefix.replace(/\/+$/, '')
12✔
111
  const normalizedPath = path.startsWith('/') ? path : `/${path}`
12!
112

113
  return `${normalizedPrefix}${normalizedPath}`
12✔
114
}
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