-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
114 lines (107 loc) · 3.35 KB
/
index.ts
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
import Fastify from "fastify";
import fastifyMiddie from "@fastify/middie";
import fastifyStatic from "@fastify/static";
import fastifyCompress from "@fastify/compress";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { exec } from "child_process";
import chalk from "chalk";
import { createServer } from "http";
import { Socket } from "net";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { server as wisp } from "@mercuryworkshop/wisp-js/server";
const port: number = Number(process.env.PORT) || 8080;
const host: string = process.env.HOST || "localhost";
const build = async () => {
if (!fs.existsSync("dist")) {
console.log(
chalk.yellow.bold("🚧 Cannot find dist folder, building lunar..."),
);
try {
const Process = exec("npm run build");
Process.stdout?.on("data", (data) => {
process.stdout.write(chalk.cyan(data));
});
Process.stderr?.on("data", (data) => {
process.stderr.write(chalk.red(data));
});
await new Promise((resolve, reject) => {
Process.on("close", (code) => {
if (code === 0) {
resolve(true);
} else {
reject(
new Error(
`⚠️ Lunar failed to build failed with exit code ${code}`,
),
);
}
});
});
console.log(chalk.green.bold("✅ Dist folder was successfully built."));
} catch (error) {
throw new Error(
`${chalk.red.bold("❌ Failed to build the dist folder:")} ${error instanceof Error ? error.message : error}`,
);
}
}
};
const app = Fastify({
logger: false,
serverFactory: (handler) =>
createServer(handler).on("upgrade", (req, socket: Socket, head) => {
if (req.url?.startsWith("/goo")) {
wisp.routeRequest(req, socket, head);
} else {
socket.destroy();
}
}),
});
try {
await build();
await app.register(fastifyMiddie);
await app.register(fastifyCompress, { encodings: ["deflate", "gzip", "br"] });
if (fs.existsSync("./dist/server/entry.mjs")) {
//@ts-ignore
const module = await import("./dist/server/entry.mjs");
app.use(module.handler);
}
app.register(fastifyStatic, {
root: fileURLToPath(new URL("./dist/client", import.meta.url)),
});
app.register(fastifyStatic, {
root: epoxyPath,
prefix: "/e/",
decorateReply: false,
});
app.register(fastifyStatic, {
root: libcurlPath,
prefix: "/l/",
decorateReply: false,
});
app.register(fastifyStatic, {
root: baremuxPath,
prefix: "/bm/",
decorateReply: false,
});
app.listen({ host, port }, (err, address) => {
if (err) {
console.error(chalk.red.bold(`❌ Failed to start lunar: ${err.message}`));
process.exit(1);
} else {
console.log(
chalk.green.bold(
`🌙 Lunar v${process.env.npm_package_version} is running on:`,
),
);
console.log(chalk.blue(`🌐 Local: http://${host}:${port}`));
console.log(chalk.blue(`🌍 Network: ${address}`));
}
});
} catch (error: unknown) {
throw new Error(
`${chalk.red.bold("❌ An error happend while trying to start lunar:")} ${error instanceof Error ? error.message : error}`,
);
}