import { addonsJs, bootGuardJs, i18nJs, localeEn, localeZh, localeEnJs, localeZhJs, tasksJs, calendarJs, i18nRenderersJs, browserAppJs, dataActionsJs, terminalAppJs, dbAdminJs, indexHtml, manifest, serviceWorker, icon } from "./assets.js"; import { cloudApi } from "./cloud-state.js"; import { dbAdminApi } from "./db-admin.js"; const BING_ARCHIVE = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"; async function bingMetadata() { const response = await fetch(BING_ARCHIVE, { headers: { accept: "application/json" } }); if (!response.ok) throw new Error(`Bing metadata request failed: ${response.status}`); const image = (await response.json()).images?.[0]; if (!image?.url) throw new Error("Bing did not return today's image"); return { title: image.copyright || "Bing Daily Image", url: new URL(image.url, "https://www.bing.com").toString() }; } function headers(type, cache = "public, max-age=3600") { return { "content-type": type, "cache-control": cache }; } function userId(request) { return request.headers.get("oai-authenticated-user-id"); } function loginPage(request) { const language = request.headers.get("accept-language")?.toLowerCase().startsWith("zh") ? localeZh : localeEn; const text = language.login; const escape = (value) => String(value).replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", "\"": """, "'": "'" }[char])); return `Life OS
LIFE OS

${text.title}

${escape(text.description)}

${escape(text.continue)}

${escape(text.footnote)}

`; } export default { async fetch(request, env) { const url = new URL(request.url); const id = userId(request); if (["/api/lifeos-state", "/api/lifeos-initialize", "/api/lifeos-sync"].includes(url.pathname)) return cloudApi(request, env, id, url.pathname); if (url.pathname.startsWith("/api/db-admin/")) return dbAdminApi(request, env, id, url.pathname); try { if (url.pathname === "/api/bing-wallpaper") return Response.json(await bingMetadata(), { headers: { "cache-control": "public, max-age=3600" } }); if (url.pathname === "/api/bing-wallpaper-image") { const image = await bingMetadata(); const response = await fetch(image.url); if (!response.ok) throw new Error(`Bing image request failed: ${response.status}`); return new Response(response.body, { headers: headers(response.headers.get("content-type") || "image/jpeg", "public, max-age=21600") }); } } catch (error) { return Response.json({ error: "Bing wallpaper is temporarily unavailable", detail: String(error) }, { status: 502 }); } if (url.pathname === "/addons.js") return new Response(addonsJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/service-worker.js") return new Response(serviceWorker, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/manifest.webmanifest") return new Response(manifest, { headers: headers("application/manifest+json; charset=utf-8", "no-cache") }); if (url.pathname === "/life-os-icon.svg") return new Response(icon, { headers: headers("image/svg+xml", "public, max-age=86400") }); if (url.pathname === "/boot-guard.js") return new Response(bootGuardJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/i18n.js") return new Response(i18nJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/locales/en.js") return new Response(localeEnJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/locales/zh-CN.js") return new Response(localeZhJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/tasks.js") return new Response(tasksJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/calendar.js") return new Response(calendarJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/data-actions.js") return new Response(dataActionsJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/terminal-app.js") return new Response(terminalAppJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/db-admin.js") return new Response(dbAdminJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/browser-app.js") return new Response(browserAppJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/i18n-renderers.js") return new Response(i18nRenderersJs, { headers: headers("application/javascript; charset=utf-8", "no-cache") }); if (url.pathname === "/" || url.pathname === "/index.html") { if (!id) return new Response(loginPage(request), { headers: headers("text/html; charset=utf-8", "no-cache") }); return new Response(indexHtml, { headers: headers("text/html; charset=utf-8", "no-cache") }); } return new Response("Not found", { status: 404 }); }, };