Open History to see the last commit
1const DEFAULT_SSR_HOST = '127.0.0.1'
2const DEFAULT_SSR_PORT = 4174
3β
4function normalizeUrlHost(host) {
5 if (host.includes(':') && !host.startsWith('[') && !host.endsWith(']')) {
6 return `[${host}]`
7 }
8β
9 return host
10}
11β
12function resolvePort(name, fallback) {
13 const value = process.env[name]
14 if (!value) {
15 return fallback
16 }
17β
18 const parsedPort = Number.parseInt(value, 10)
19 const isValidPort =
20 Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535
21β
22 if (!isValidPort) {
23 throw new Error(
24 `Invalid ${name} "${value}". Expected an integer between 1 and 65535.`,
25 )
26 }
27β
28 return parsedPort
29}
30β
31export const SSR_PORT = resolvePort('SSR_PORT', DEFAULT_SSR_PORT)
32export const SSR_HOST = process.env.SSR_HOST ?? DEFAULT_SSR_HOST
33β
34function resolveConnectHost(host) {
35 if (host === '0.0.0.0') {
36 return '127.0.0.1'
37 }
38β
39 if (host === '::') {
40 return '[::1]'
41 }
42β
43 return normalizeUrlHost(host)
44}
45β
46function resolvePublicOrigin(host, port) {
47 const explicitOrigin = process.env.SSR_PUBLIC_ORIGIN
48 if (explicitOrigin) {
49 const url = new URL(explicitOrigin)
50 return url.origin
51 }
52β
53 const explicitPublicHost = process.env.SSR_PUBLIC_HOST
54 const publicHost = explicitPublicHost
55 ? normalizeUrlHost(explicitPublicHost)
56 : resolveConnectHost(host)
57β
58 return `http://${publicHost}:${port}`
59}
60β
61export const SSR_CONNECT_HOST = resolveConnectHost(SSR_HOST)
62export const SSR_BASE_URL = `http://${SSR_CONNECT_HOST}:${SSR_PORT}`
63export const SSR_PUBLIC_ORIGIN = resolvePublicOrigin(SSR_HOST, SSR_PORT)