Files for my website
bwc9876.dev
1import rawTypst from "@assets/resume.typ?raw";
2import { spawn } from "node:child_process";
3
4const compileTypst = (raw: string): Promise<Buffer> => {
5 const cmd = spawn("typst", ["compile", "-f", "pdf", "-", "-"], { stdio: "pipe" });
6
7 cmd.stdin.write(raw);
8 cmd.stdin.end();
9
10 return new Promise((resolve, reject) => {
11 cmd.on("error", reject);
12
13 let stderr = "";
14 cmd.stderr.setEncoding("utf8");
15 cmd.stderr.on("data", (data) => {
16 stderr += data;
17 });
18
19 let stdout: Buffer | null = null;
20 cmd.stdout.on("data", (data) => {
21 if (stdout === null) {
22 stdout = data;
23 } else {
24 stdout = Buffer.concat([stdout, data]);
25 }
26 });
27
28 cmd.on("exit", (code) => {
29 if (code === 0) {
30 if (stdout !== null) {
31 resolve(stdout as Buffer);
32 } else {
33 reject(new Error(`No stdout, Stderr: ${stderr}`));
34 }
35 } else {
36 reject(new Error(`Exited with code ${code} Stderr: ${stderr}`));
37 }
38 });
39 });
40};
41
42export async function GET() {
43 const data = await compileTypst(rawTypst);
44 // @ts-expect-error "idk why it thinks this is wrong? Astro supports responses from a Buffer"
45 return new Response(data);
46}