data endpoint for entity 90008 (aka. a website)
1import { readFile, stat } from 'node:fs/promises';
2import { join } from 'node:path';
3import { brotliCompress, gzip } from 'node:zlib';
4import { env } from '$env/dynamic/private';
5import { initConstellation } from '$lib/constellation';
6
7export const GET = async ({ request }: { request: Request }) => {
8 const filePath = join(env.WEBSITE_DATA_DIR, 'constellation', 'background.svg');
9
10 try {
11 await stat(filePath);
12 } catch (e) {
13 await initConstellation();
14 }
15
16 try {
17 const file = await readFile(filePath);
18 const acceptEncoding = request.headers.get('accept-encoding') || '';
19
20 let content: any = file;
21 let contentEncoding: string | null = null;
22
23 if (acceptEncoding.includes('br')) {
24 content = await new Promise((resolve, reject) =>
25 brotliCompress(file, (err, buf) => (err ? reject(err) : resolve(buf)))
26 );
27 contentEncoding = 'br';
28 } else if (acceptEncoding.includes('gzip')) {
29 content = await new Promise((resolve, reject) =>
30 gzip(file, (err, buf) => (err ? reject(err) : resolve(buf)))
31 );
32 contentEncoding = 'gzip';
33 }
34
35 const headers: Record<string, string> = {
36 'Content-Type': 'image/svg+xml',
37 'Cache-Control': 'public, max-age=31536000, immutable'
38 };
39
40 if (contentEncoding) {
41 headers['Content-Encoding'] = contentEncoding;
42 }
43
44 return new Response(content, { headers });
45 } catch (err) {
46 console.error(`error serving background: ${err}`);
47 return new Response('not found', { status: 404 });
48 }
49};