-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathvite.config.ts
More file actions
260 lines (236 loc) · 8.2 KB
/
Copy pathvite.config.ts
File metadata and controls
260 lines (236 loc) · 8.2 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import path from 'path';
import { fileURLToPath } from 'url';
import { loadEnv, type ConfigEnv, type UserConfig, type ViteDevServer } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';
import { execSync } from 'child_process';
import fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const LYRIC_PROXY_CORS_HEADERS: Record<string, string> = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS,PATCH,DELETE,POST,PUT',
'Access-Control-Allow-Headers': [
'X-CSRF-Token',
'X-Requested-With',
'Accept',
'Accept-Version',
'Content-Length',
'Content-MD5',
'Content-Type',
'Date',
'X-Api-Version',
'KG-Rec',
'KG-RC',
'KG-CLIENTTIMEMS',
'mid',
'x-router',
].join(', '),
};
const LYRIC_PROXY_IGNORED_FORWARD_HEADERS = ['host', 'connection', 'content-length', 'origin', 'referer'];
function isAllowedLyricProxyHost(hostname: string): boolean {
return hostname === 'qq.com' || hostname.endsWith('.qq.com') ||
hostname === 'y.gtimg.cn' ||
hostname === 'kugou.com' || hostname.endsWith('.kugou.com') ||
hostname === 'kgimg.com' || hostname.endsWith('.kgimg.com') ||
hostname === 'amll-ttml-db.stevexmh.net';
}
function isAmllDbHost(hostname: string): boolean {
return hostname === 'amll-ttml-db.stevexmh.net';
}
function setLyricProxyCorsHeaders(res: import('http').ServerResponse): void {
Object.entries(LYRIC_PROXY_CORS_HEADERS).forEach(([key, value]) => {
res.setHeader(key, value);
});
}
function sendLyricProxyJson(res: import('http').ServerResponse, statusCode: number, body: unknown): void {
setLyricProxyCorsHeaders(res);
res.statusCode = statusCode;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(body));
}
function readDevRequestBody(req: import('http').IncomingMessage): Promise<Uint8Array<ArrayBuffer>> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
req.on('data', (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
req.on('end', () => resolve(new Uint8Array(Buffer.concat(chunks))));
req.on('error', reject);
});
}
function devLyricProxyPlugin() {
return {
name: 'folia-dev-lyric-proxy',
apply: 'serve' as const,
configureServer(server: ViteDevServer) {
server.middlewares.use(async (req, res, next) => {
const requestUrl = new URL(req.url ?? '/', 'http://localhost');
if (requestUrl.pathname !== '/api/lyric-proxy') {
next();
return;
}
if (req.method === 'OPTIONS') {
setLyricProxyCorsHeaders(res);
res.statusCode = 200;
res.end();
return;
}
const targetUrlStr = requestUrl.searchParams.get('url');
if (!targetUrlStr) {
sendLyricProxyJson(res, 400, { error: 'Missing url parameter' });
return;
}
try {
const targetUrl = new URL(targetUrlStr);
if (!isAllowedLyricProxyHost(targetUrl.hostname)) {
sendLyricProxyJson(res, 403, { error: 'Forbidden: Domain not allowed' });
return;
}
const headers = new Headers();
for (const [key, value] of Object.entries(req.headers)) {
if (!LYRIC_PROXY_IGNORED_FORWARD_HEADERS.includes(key.toLowerCase()) && value) {
headers.set(key, Array.isArray(value) ? value.join(', ') : value);
}
}
const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method ?? '');
const response = await fetch(targetUrl.toString(), {
method: req.method,
headers,
body: hasBody ? await readDevRequestBody(req) : undefined,
});
setLyricProxyCorsHeaders(res);
if (isAmllDbHost(targetUrl.hostname) && response.status === 404) {
res.statusCode = 204;
res.end();
return;
}
res.statusCode = response.status;
res.statusMessage = response.statusText;
const contentType = response.headers.get('content-type');
if (contentType) {
res.setHeader('Content-Type', contentType);
}
const buffer = Buffer.from(await response.arrayBuffer());
res.end(buffer);
} catch (error) {
console.error('Vite lyric proxy request failed:', error);
sendLyricProxyJson(res, 500, { error: 'Proxy request failed', details: String(error) });
}
});
},
};
}
export default async function viteConfig({ mode }: ConfigEnv): Promise<UserConfig> {
const env = loadEnv(mode, '.', '');
let commitHash = '';
if (process.env.VERCEL_GIT_COMMIT_SHA) {
commitHash = process.env.VERCEL_GIT_COMMIT_SHA.substring(0, 7);
} else {
try {
commitHash = execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
console.warn('Could not get commit hash:', e);
commitHash = 'unknown, probably dev version';
}
}
let gitBranch = '';
if (process.env.VERCEL_GIT_COMMIT_REF) {
gitBranch = process.env.VERCEL_GIT_COMMIT_REF;
} else {
try {
gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
} catch (e) {
console.warn('Could not get git branch:', e);
gitBranch = 'unknown';
}
}
let commitSuffix = '';
if (commitHash && commitHash !== 'unknown, probably dev version') {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 2000);
const res = await fetch(`https://namoe.izuna.top/api/namoe?hash=${commitHash}`, {
signal: controller.signal
});
clearTimeout(timeout);
if (res.ok) {
const data = await res.json() as { name?: string };
if (data?.name) {
commitSuffix = `/${data.name}`;
}
}
} catch (e) {
// Ignore errors during fetch to prevent build failure
}
}
const appVersionLabel = process.env.APP_VERSION_LABEL?.trim() || 'Realeco';
const appReleaseChannel = process.env.APP_RELEASE_CHANNEL?.trim().toLowerCase() || 'realeco';
return {
base: process.env.ELECTRON === 'true' ? './' : '/',
worker: {
format: 'es'
},
build: {
rollupOptions: {
input: {
main: 'index.html',
stageClient: 'stage-client.html',
},
output: {
// three.js is a large dependency used only by the diorama 3D visualizer. Split it into its
// own chunk so it doesn't bloat the main bundle past the PWA precache size limit (each file
// must stay under workbox.maximumFileSizeToCacheInBytes to be cached for offline use).
manualChunks(id) {
return id.includes('/node_modules/three/') ? 'three' : undefined;
},
},
},
},
server: {
port: 3000,
host: '0.0.0.0',
},
plugins: [
devLyricProxyPlugin(),
react(),
VitePWA({
registerType: 'autoUpdate',
includeAssets: ['icon.svg'],
devOptions: {
enabled: true
},
workbox: {
maximumFileSizeToCacheInBytes: 5000000
},
manifest: {
name: 'Folia Music',
short_name: 'Folia',
description: 'A beautiful AI-themed music player',
theme_color: '#09090b',
background_color: '#09090b',
display: 'standalone',
icons: [
{
src: 'icon.svg',
sizes: '512x512',
type: 'image/svg+xml',
purpose: 'any maskable'
}
]
}
})
],
define: {
'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY),
'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY),
'__COMMIT_HASH__': JSON.stringify(commitHash + commitSuffix),
'__GIT_BRANCH__': JSON.stringify(gitBranch),
'__APP_VERSION__': JSON.stringify(JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8')).version),
'__APP_VERSION_LABEL__': JSON.stringify(appVersionLabel),
'__APP_RELEASE_CHANNEL__': JSON.stringify(appReleaseChannel)
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
}
}
};
}