standalone: fixes a bunch of small issues

This commit is contained in:
Tiago
2025-06-16 10:36:43 +01:00
parent 1f83180f9d
commit 8a69cd7c98
3 changed files with 25 additions and 10 deletions
+2
View File
@@ -130,3 +130,5 @@ And by setting `window.CAP_CUSTOM_WASM_URL` to the path of the `cap_wasm_bg.wasm
```js
window.CAP_CUSTOM_WASM_URL = "https://<server url>/assets/cap_wasm_bg.wasm";
```
By default, these fetch from `process.env.CACHE_HOST`. You can change this by setting the `CACHE_HOST` environment variable when running the server. If you don't set it, it defaults to `https://cdn.jsdelivr.net`. You can also set it to `disable` to disable the assets server entirely.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "cap-standalone",
"version": "1.0.15",
"version": "1.0.16",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.js",
+22 -9
View File
@@ -60,20 +60,24 @@ const updateCache = async () => {
if (!(currentTime - lastUpdate > updateInterval)) return;
const CACHE_HOST = process.env.CACHE_HOST || "https://cdn.jsdelivr.net";
if (CACHE_HOST === "disable") return;
try {
const [widgetSource, floatingSource, wasmSource, wasmLoaderSource] =
await Promise.all([
fetch("https://cdn.jsdelivr.net/npm/@cap.js/widget@latest").then((r) =>
fetch(`${CACHE_HOST}/npm/@cap.js/widget@latest`).then((r) =>
r.text()
),
fetch(
"https://cdn.jsdelivr.net/npm/@cap.js/widget/cap-floating.min.js"
`${CACHE_HOST}/npm/@cap.js/widget/cap-floating.min.js`
).then((r) => r.text()),
fetch(
"https://cdn.jsdelivr.net/npm/@cap.js/wasm/browser/cap_wasm_bg.wasm"
`${CACHE_HOST}/npm/@cap.js/wasm/browser/cap_wasm_bg.wasm`
).then((r) => r.arrayBuffer()),
fetch(
"https://cdn.jsdelivr.net/npm/@cap.js/wasm/browser/cap_wasm.min.js"
`${CACHE_HOST}/npm/@cap.js/wasm/browser/cap_wasm.min.js`
).then((r) => r.text()),
]);
@@ -161,11 +165,16 @@ const auth = new Elysia({
})
)
.post("/", async ({ body, cookie, set }) => {
if (
!body?.password ||
!timingSafeEqual(Buffer.from(body.password), Buffer.from(ADMIN_KEY))
) {
set.status = 401;
try {
if (
!body?.password ||
!timingSafeEqual(Buffer.from(body.password), Buffer.from(ADMIN_KEY))
) {
set.status = 401;
return { success: false };
}
} catch {
set.status = 500;
return { success: false };
}
@@ -398,8 +407,12 @@ const assetsServer = new Elysia({ prefix: "/assets" })
.use(
cors({
origin: process.env.CORS_ORIGIN || true,
methods: ["GET"],
})
)
.onBeforeHandle(({ set }) => {
set.headers["Cache-Control"] = "max-age=31536000, immutable";
})
.get("/widget.js", ({ set }) => {
set.headers["Content-Type"] = "text/javascript";
return file(path.join(dataDir, "assets-widget.js"));