forked from jin60641/react-fullpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmos.bunserver.ts
118 lines (103 loc) · 2.82 KB
/
cosmos.bunserver.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
115
116
117
118
import { rm, stat } from "node:fs/promises";
import type { ServeOptions } from "bun";
import * as path from "path";
import cosmosConfig from "./cosmos.config.json";
const PROJECT_ROOT = import.meta.dir;
const BUILD_DIR = path.resolve(PROJECT_ROOT, "build");
const waitForCosmosImports = async () => {
const fpath = `${PROJECT_ROOT}/cosmos.imports.ts`;
try {
const cosmosImports = await stat(fpath);
if (!cosmosImports.isFile()) {
throw new Error(`
file doesnt exist yet
`);
}
} catch {
return new Promise((resolve) => {
setTimeout(() => resolve(waitForCosmosImports()), 1000);
});
}
};
const buildApp = async () =>
rm(BUILD_DIR, { force: true, recursive: true }).then(() =>
Bun.build({
entrypoints: ["./cosmos.entrypoint.tsx"],
target: "browser",
outdir: "build",
})
.then((output) => output)
.catch((e) => {
console.info("\n\n error in build", e);
})
);
await waitForCosmosImports();
await buildApp().then((output) => {
if (output.success)
console.info(
`app built: ${output.success}; ${output.outputs.length} files `
);
else {
for (const message of output.logs) {
// Bun will pretty print the message object
console.error(message);
}
throw new Error(`build failed`);
}
});
const returnIndex = () => {
const index = `
<!DOCTYPE html>
<html lang="en">
<body>
<script src="${BUILD_DIR}/cosmos.entrypoint.js" type="module">
</script>
</body>
</html>
`;
return new Response(index, {
headers: {
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "*",
},
});
};
async function serveFromDir(config: {
directory?: string;
path: string;
}): Promise<Response | null> {
const filepath = path.join(config.directory || "", config.path);
try {
const fd = await stat(filepath);
if (fd && fd.isFile()) {
return new Response(Bun.file(filepath), {
headers: { "Access-Control-Allow-Origin": "*" },
});
}
} catch (err) {}
return null;
}
export default {
port: cosmosConfig.rendererUrl.split(":").pop(),
hostname: "0.0.0.0",
async fetch(req) {
const reqPath = new URL(req.url).pathname;
console.log(req.method, reqPath);
if (reqPath === "/") return returnIndex();
else {
const filepath = req.url.replace(cosmosConfig.rendererUrl, "");
const exactResponse = await serveFromDir({ path: filepath });
if (exactResponse) return exactResponse;
const buildResponse = await serveFromDir({
directory: BUILD_DIR,
path: filepath,
});
if (buildResponse) return buildResponse;
return new Response("File not found", {
status: 404,
});
}
},
} satisfies ServeOptions;
// watch imports
await import("./cosmos.imports.ts").catch((e) => e);