-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.ts
More file actions
executable file
·46 lines (42 loc) · 1.36 KB
/
server.ts
File metadata and controls
executable file
·46 lines (42 loc) · 1.36 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
#!/usr/bin/env bun
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
const port = 10000;
Bun.serve({
port,
async fetch(req) {
switch (req.method) {
case "POST": {
const { project, entry = "main.typ" } = await req.json();
if (!project)
return new Response("Missing 'project' field", {
status: 400,
});
const dir = await mkdtemp("/tmp/typ2docx-");
await writeFile(`${dir}/project.zip`, Buffer.from(project, "base64"));
await Bun.$`cd ${dir} && unzip -o project.zip && rm project.zip`.nothrow();
const { stderr, exitCode } =
await Bun.$`cd ${dir} && typ2docx ${entry} -o output.docx -e pdfservices`
.env(process.env)
.nothrow();
if (exitCode) {
await rm(dir, { recursive: true, force: true });
return new Response(stderr, { status: 500 });
}
const output = await readFile(`${dir}/output.docx`);
await rm(dir, { recursive: true, force: true });
return new Response(output, {
headers: {
"Content-Type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"Content-Disposition": "attachment; filename=output.docx",
},
});
}
case "GET":
return new Response(Bun.file("index.html"));
default:
return new Response(null, { status: 405 });
}
},
});
console.log(`Server is running on http://localhost:${port}`);