index.ts
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { PageManifest } from '@single-pages/contracts';
import { PAGE_MANIFESTS } from '@single-pages/contracts';
export const APP_NAME = 'single-pages';
export const FRONT_PAGES: readonly PageManifest[] = PAGE_MANIFESTS;
export const DEFAULT_DEV_SERVER_PORT = 4173;
export const DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS = [
'https://dj.glsx.com.cn',
'https://frontend.test.glsx.com.cn',
'http://192.168.3.191:8543',
'http://192.168.1.146:8080',
] as const;
export const DEFAULT_PRELEARNING_ALLOWED_ORIGINS = [...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS];
export const DEFAULT_REMOTE_BACKEND_API_BASE_PATH = '/backend';
export interface BackendApiPathOptions {
hostname?: string;
envBasePath?: string;
}
export function parseSopPreviewAllowedOrigins(rawValue: string | undefined): string[] | '*' {
const text = rawValue?.trim();
if (!text) {
return [...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS];
}
if (text === '*') {
return '*';
}
return text
.split(',')
.map((item) => item.trim())
.filter((item) => item.length > 0)
.map((item) => {
try {
return new URL(item).origin;
} catch {
return item;
}
});
}
export function parsePrelearningAllowedOrigins(rawValue: string | undefined): string[] | '*' {
const text = rawValue?.trim();
if (!text) {
return [...DEFAULT_PRELEARNING_ALLOWED_ORIGINS];
}
if (text === '*') {
return '*';
}
return text
.split(',')
.map((item) => item.trim())
.filter((item) => item.length > 0)
.map((item) => {
try {
return new URL(item).origin;
} catch {
return item;
}
});
}
export function isLocalRuntimeHostname(hostname: string | undefined): boolean {
const normalized = hostname?.trim().toLowerCase();
if (!normalized) {
return true;
}
if (
normalized === 'localhost'
|| normalized === '0.0.0.0'
|| normalized === '::1'
|| normalized.startsWith('127.')
|| normalized.startsWith('192.168.')
|| normalized.startsWith('10.')
) {
return true;
}
const private172Match = /^172\.(\d+)\./.exec(normalized);
if (private172Match) {
const secondOctet = Number(private172Match[1]);
return secondOctet >= 16 && secondOctet <= 31;
}
return false;
}
export function normalizeBackendApiBasePath(basePath: string): string {
const trimmed = basePath.trim();
if (!trimmed || trimmed === '/') {
return '';
}
return `/${trimmed.replace(/^\/+|\/+$/g, '')}`;
}
export function resolveBackendApiBasePath(options: BackendApiPathOptions = {}): string {
if (options.envBasePath !== undefined) {
return normalizeBackendApiBasePath(options.envBasePath);
}
return isLocalRuntimeHostname(options.hostname) ? '' : DEFAULT_REMOTE_BACKEND_API_BASE_PATH;
}
export function buildBackendApiPath(pathname: string, options: BackendApiPathOptions = {}): string {
const basePath = resolveBackendApiBasePath(options);
const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;
return `${basePath}${normalizedPathname}`;
}