Supra_App/src/routes/api/scrcpy-server/+server.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* GET /api/scrcpy-server
*
* Proxies the scrcpy-server binary from GitHub releases.
* Fixes CORS issues (GitHub doesn't send Access-Control-Allow-Origin for binaries).
* Server-side fetch has no CORS restrictions.
*
* Response is cached by the browser for 24 hours via Cache-Control header.
*/
import type { RequestHandler } from './$types';
const GITHUB_URL =
'https://github.com/Genymobile/scrcpy/releases/download/v2.3/scrcpy-server-v2.3';
export const GET: RequestHandler = async () => {
try {
const response = await fetch(GITHUB_URL);
if (!response.ok) {
console.error(`[scrcpy-server] GitHub returned ${response.status}`);
return new Response(`Failed to fetch scrcpy-server: ${response.status}`, {
status: 502
});
}
// Stream the response directly — don't buffer the entire binary in memory on the server
return new Response(response.body, {
headers: {
'Content-Type': 'application/octet-stream',
'Cache-Control': 'public, max-age=86400, immutable'
}
});
} catch (e) {
console.error('[scrcpy-server] Fetch failed:', e);
return new Response('Failed to fetch scrcpy-server from GitHub', { status: 500 });
}
};