diff --git a/internal/dev-entrypoint/cloud-web-runtime.mjs b/internal/dev-entrypoint/cloud-web-runtime.mjs index 6e9dc78b..d8bfa32a 100644 --- a/internal/dev-entrypoint/cloud-web-runtime.mjs +++ b/internal/dev-entrypoint/cloud-web-runtime.mjs @@ -23,6 +23,8 @@ const CLIENT_DISCONNECT_ERROR_CODES = new Set([ ]); const CLIENT_DISCONNECT_ERROR_PATTERN = /\b(?:socket hang up|client disconnected|premature close)\b/iu; const CLOUD_WEB_ERROR_HANDLER_INSTALLED = Symbol.for("hwlab.cloud-web.error-handler-installed"); +const CLIENT_ROUTE_FALLBACK_PATHS = new Set(["/gate", "/diagnostics/gate", "/help", "/skills", "/access"]); +const STATIC_ASSET_EXTENSION_PATTERN = /\.[A-Za-z0-9][A-Za-z0-9_-]*$/u; export async function serveCloudWeb(options) { installCloudWebProcessErrorHandlers({ serviceId: options.serviceId }); @@ -66,8 +68,8 @@ export function createCloudWebServer({ return; } - const routePath = url.pathname.replace(/\/+$/u, "") || "/"; - const relativePath = routePath === "/" || routePath === "/gate" || routePath === "/diagnostics/gate" || routePath === "/help" || routePath === "/skills" || routePath === "/access" + const routePath = normalizeCloudWebRoutePath(url.pathname); + const relativePath = routePath === "/" || CLIENT_ROUTE_FALLBACK_PATHS.has(routePath) || isCloudWebClientRouteRequest(request, routePath) ? "index.html" : url.pathname.slice(1); for (const root of roots) { @@ -114,6 +116,20 @@ export function createCloudWebServer({ }); } +function normalizeCloudWebRoutePath(pathname) { + return String(pathname || "/").replace(/\/+$/u, "") || "/"; +} + +function isCloudWebClientRouteRequest(request, routePath) { + if (request.method !== "GET" && request.method !== "HEAD") return false; + if (!routePath || routePath === "/") return true; + if (routePath.startsWith("/auth/") || routePath === "/auth") return false; + if (routePath.startsWith("/v1/") || routePath === "/v1" || routePath === "/json-rpc") return false; + if (STATIC_ASSET_EXTENSION_PATTERN.test(path.basename(routePath))) return false; + const accept = String(request.headers.accept || "").toLowerCase(); + return !accept || accept.includes("text/html") || accept.includes("*/*"); +} + export function installCloudWebProcessErrorHandlers({ serviceId } = {}) { if (process[CLOUD_WEB_ERROR_HANDLER_INSTALLED]) return; process[CLOUD_WEB_ERROR_HANDLER_INSTALLED] = true; diff --git a/internal/dev-entrypoint/cloud-web-runtime.test.mjs b/internal/dev-entrypoint/cloud-web-runtime.test.mjs index 72dcdef4..5d446655 100644 --- a/internal/dev-entrypoint/cloud-web-runtime.test.mjs +++ b/internal/dev-entrypoint/cloud-web-runtime.test.mjs @@ -301,9 +301,10 @@ test("cloud web remains healthy after a client disconnects during static respons } }); -test("cloud web serves access deep link through the React shell", async () => { +test("cloud web serves client deep links through the Vue shell", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "hwlab-cloud-web-runtime-")); await writeFile(path.join(root, "index.html"), "
\n", "utf8"); + await writeFile(path.join(root, "asset.txt"), "asset body\n", "utf8"); const cloudWeb = createCloudWebServer({ serviceId: "hwlab-cloud-web", roots: [root], @@ -321,10 +322,25 @@ test("cloud web serves access deep link through the React shell", async () => { await listen(cloudWeb); try { - const response = await fetch(`${serverUrl(cloudWeb)}/access`); - assert.equal(response.status, 200); - assert.equal(response.headers.get("content-type"), "text/html; charset=utf-8"); - assert.equal(await response.text(), "
\n"); + for (const route of ["/access", "/register", "/api-keys", "/admin/users"]) { + const response = await fetch(`${serverUrl(cloudWeb)}${route}`, { + headers: { accept: "text/html" } + }); + assert.equal(response.status, 200, route); + assert.equal(response.headers.get("content-type"), "text/html; charset=utf-8", route); + assert.equal(await response.text(), "
\n", route); + } + + const assetResponse = await fetch(`${serverUrl(cloudWeb)}/asset.txt`, { + headers: { accept: "text/plain" } + }); + assert.equal(assetResponse.status, 200); + assert.equal(await assetResponse.text(), "asset body\n"); + + const missingAssetResponse = await fetch(`${serverUrl(cloudWeb)}/missing.css`, { + headers: { accept: "text/css,*/*" } + }); + assert.equal(missingAssetResponse.status, 404); } finally { await close(cloudWeb); await rm(root, { recursive: true, force: true });