-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
145 lines (123 loc) · 4.05 KB
/
vite.config.js
File metadata and controls
145 lines (123 loc) · 4.05 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
import http from 'node:http'
import https from 'node:https'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
function sanitizeFileName(name = 'apod-media') {
return name
.replace(/[<>:"/\\|?*\x00-\x1F]/g, '')
.replace(/\s+/g, ' ')
.trim()
.slice(0, 140) || 'apod-media';
}
function getFileNameFromUrl(urlString) {
try {
const parsed = new URL(urlString);
const pathPart = decodeURIComponent(parsed.pathname.split('/').pop() || 'apod-media');
return sanitizeFileName(pathPart);
} catch {
return 'apod-media';
}
}
function requestUpstream(urlString, redirectCount = 0) {
const MAX_REDIRECTS = 5;
return new Promise((resolve, reject) => {
let target;
try {
target = new URL(urlString);
} catch {
reject(new Error('Invalid URL'));
return;
}
const client = target.protocol === 'https:' ? https : http;
const req = client.request(
target,
{
method: 'GET',
timeout: 15000,
family: 4,
headers: {
'User-Agent': 'AstroViews-Download-Proxy/1.0',
Accept: '*/*',
Connection: 'close',
},
},
(upstream) => {
const statusCode = upstream.statusCode || 500;
const location = upstream.headers.location;
if (statusCode >= 300 && statusCode < 400 && location && redirectCount < MAX_REDIRECTS) {
upstream.resume();
const nextUrl = new URL(location, target).toString();
requestUpstream(nextUrl, redirectCount + 1).then(resolve).catch(reject);
return;
}
resolve({ upstream, statusCode, targetUrl: target.toString() });
}
);
req.on('timeout', () => {
req.destroy(new Error('Upstream timeout'));
});
req.on('error', reject);
req.end();
});
}
function downloadProxyPlugin() {
return {
name: 'download-proxy',
configureServer(server) {
server.middlewares.use('/download-proxy', async (req, res) => {
try {
const host = req.headers.host || 'localhost';
const parsed = new URL(req.url || '', `http://${host}`);
const target = parsed.searchParams.get('url');
if (!target) {
res.statusCode = 400;
res.end('Missing "url" query parameter');
return;
}
let targetUrl;
try {
targetUrl = new URL(target);
} catch {
res.statusCode = 400;
res.end('Invalid target URL');
return;
}
if (!['http:', 'https:'].includes(targetUrl.protocol)) {
res.statusCode = 400;
res.end('Only http/https URLs are allowed');
return;
}
const { upstream, statusCode, targetUrl: finalUrl } = await requestUpstream(targetUrl.toString());
if (statusCode >= 400) {
res.statusCode = statusCode;
res.end(`Upstream error: ${statusCode}`);
upstream.resume();
return;
}
const contentType = upstream.headers['content-type'] || 'application/octet-stream';
const contentLength = upstream.headers['content-length'];
const upstreamDisposition = upstream.headers['content-disposition'];
const fallbackName = getFileNameFromUrl(finalUrl);
res.statusCode = 200;
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Content-Type', contentType);
if (contentLength) res.setHeader('Content-Length', contentLength);
if (upstreamDisposition) {
res.setHeader('Content-Disposition', upstreamDisposition);
} else {
res.setHeader('Content-Disposition', `attachment; filename="${fallbackName}"`);
}
upstream.pipe(res);
} catch {
res.statusCode = 500;
res.end('Proxy download failed');
}
});
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), downloadProxyPlugin()],
})