diff --git a/README.md b/README.md index 9bd72ea..8d9b618 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ mise exec -- pnpm db:migrate mise exec -- just dev ``` -Open **http://localhost:5173/console/**. Vite+ serves the console and forwards other paths to the local Worker on port 8787, including mapped repository URLs. Wrangler emulates D1, R2, and Durable Objects locally. The example secrets are for local development only. +Open **http://localhost:5173/#/**. Vite+ serves the console page at `/` and forwards other paths to the local Worker on port 8787, including mapped repository URLs. Wrangler emulates D1, R2, and Durable Objects locally. The example secrets are for local development only. Initialize the local instance once: @@ -221,7 +221,7 @@ Mappings work in both instance modes. Each URL selects exactly one repository. T Relative URLs start with a single slash. Absolute URLs must use HTTPS, except HTTP on `localhost`, `127.0.0.1`, or `[::1]` for local development. Matching uses the exact origin and the longest matching path prefix at a slash boundary, so `/snapshots` takes precedence over a root mapping. A missing artifact in that repository returns 404 without falling back to another repository. Trailing slashes are optional; duplicate URLs are rejected. `/api`, `/maven`, `/console`, `/health`, `/invite`, `/favicon.ico`, `/robots.txt`, and `/.well-known` are reserved, including their descendants. Nested mappings also reserve their prefixes in the parent mapping; artifacts shadowed by these prefixes remain available at the canonical `/maven/...` URL. -The console lives at `/console` and `/` redirects there. `APP_URL` remains the console's origin, without `/console`; OAuth callback URLs stay under `/api/auth`. Existing invitation links under `/invite` redirect to `/console/invite`. Other old console bookmarks need the `/console` prefix. The console displays the first configured mapping for a repository in copyable URLs and dependency snippets, falling back to its canonical URL when no mapping exists. Browser download buttons use the console origin so session credentials work even when a mapping uses another hostname. +The console is served from `/` and routes with a URL hash, for example `/#/default/repositories`, so root mappings and console pages share the origin without ambiguity. Its assets stay under `/console/assets/`, and old `/console/...` bookmarks and `/invite` links redirect to the matching `/#/...` route. `APP_URL` is the console's origin; OAuth callback URLs stay under `/api/auth`. The console displays the first configured mapping for a repository in copyable URLs and dependency snippets, falling back to its canonical URL when no mapping exists. Browser download buttons use the console origin so session credentials work even when a mapping uses another hostname. ## Initial implementation boundaries diff --git a/apps/web/src/app.tsx b/apps/web/src/app.tsx index 139313d..53af47c 100644 --- a/apps/web/src/app.tsx +++ b/apps/web/src/app.tsx @@ -118,7 +118,8 @@ export function App() { small onClick={async () => { await authClient.signOut(); - location.assign("/console"); + history.replaceState(null, "", "/#/"); + location.reload(); }} > Sign out diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx index 38dc519..7676bb8 100644 --- a/apps/web/src/main.tsx +++ b/apps/web/src/main.tsx @@ -1,21 +1,29 @@ import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; -import { BrowserRouter } from "react-router"; +import { HashRouter } from "react-router"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { App } from "./app"; import "./global.css"; if (import.meta.env.DEV) void import("virtual:stylex:runtime"); +// Canonicalize "/" and "/#" to "/#/", and old "/console/..." bookmarks to their hash routes. +if (location.pathname !== "/" || !location.hash.startsWith("#/")) { + const route = location.hash.startsWith("#/") + ? location.hash.slice(1) + : (location.pathname.replace(/^\/console(?=\/|$)/, "") || "/") + location.search; + history.replaceState(null, "", "/#" + route); +} + const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 15_000, retry: 1 } }, }); createRoot(document.getElementById("root")!).render( - + - + , ); diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 673da91..79a76bd 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -18,7 +18,12 @@ export default defineConfig({ port: 5173, strictPort: true, proxy: { - "^/(?!console(?:/|$))": "http://127.0.0.1:8787", + // Serve the console page at / and forward every other non-console path to the Worker + // with the original host, so origin-relative repository mappings resolve locally. + "^/(?!console(?:/|$))": { + target: "http://127.0.0.1:8787", + bypass: (req) => (req.url === "/" ? "/console/" : undefined), + }, }, }, }); diff --git a/apps/worker/src/index.ts b/apps/worker/src/index.ts index fbe96ef..934dc2e 100644 --- a/apps/worker/src/index.ts +++ b/apps/worker/src/index.ts @@ -95,14 +95,25 @@ app.doc("/api/openapi.json", { }); app.all("/api/*", (c) => c.json({ error: "Not found" }, 404)); app.all("/maven/*", (c) => c.json({ error: "Publish through the local maven-r2 proxy" }, 405)); -app.get("/", (c) => c.redirect(new URL("/console", c.env.APP_URL).href)); -app.get("/invite/:secret", (c) => c.redirect(new URL("/console" + c.req.path, c.env.APP_URL).href)); -app.on(["GET", "HEAD"], ["/console", "/console/*"], async (c) => { +// The console is a hash-routed single page at /#/...; its hashed assets live under /console/assets/. +const consoleUrl = (env: Env, route: string) => new URL("/#" + route, env.APP_URL).href; +app.on(["GET", "HEAD"], "/", async (c) => { const url = new URL(c.req.url); - if (!url.pathname.startsWith("/console/assets/")) url.pathname = "/console/index.html"; + if (url.origin !== new URL(c.env.APP_URL).origin) return c.redirect(consoleUrl(c.env, "/")); + url.pathname = "/console/index.html"; const response = await c.env.ASSETS.fetch(new Request(url, c.req.raw)); return new Response(response.body, response); }); +app.get("/invite/:secret", (c) => c.redirect(consoleUrl(c.env, c.req.path))); +app.on(["GET", "HEAD"], ["/console", "/console/*"], async (c) => { + const url = new URL(c.req.url); + if (!url.pathname.startsWith("/console/assets/")) + return c.redirect( + consoleUrl(c.env, (url.pathname.slice("/console".length) || "/") + url.search), + ); + const response = await c.env.ASSETS.fetch(c.req.raw); + return new Response(response.body, response); +}); for (const probe of ["/favicon.ico", "/robots.txt", "/.well-known/*"]) app.all(probe, (c) => c.json({ error: "Not found" }, 404)); app.all("*", serveMappedRepository); diff --git a/apps/worker/src/management.ts b/apps/worker/src/management.ts index 88410bb..b4715dd 100644 --- a/apps/worker/src/management.ts +++ b/apps/worker/src/management.ts @@ -585,7 +585,7 @@ export function registerManagement(app: OpenAPIHono) { return c.json( { id, - url: new URL("/console/invite/" + secret, c.env.APP_URL).href, + url: new URL("/#/invite/" + secret, c.env.APP_URL).href, expiresAt: expires, }, 201, diff --git a/apps/worker/test/publication.test.ts b/apps/worker/test/publication.test.ts index 03eb12f..f20cb01 100644 --- a/apps/worker/test/publication.test.ts +++ b/apps/worker/test/publication.test.ts @@ -301,7 +301,7 @@ describe("repository URL mappings", () => { "artifact", ); expect((await app.request("https://other.test" + path, {}, mapped)).status).toBe(404); - expect((await app.request("https://repo.test/", {}, mapped)).status).toBe(302); + expect((await app.request("https://other.test/", {}, mapped)).status).toBe(302); expect((await app.request("https://repo.test/snapshots", {}, mapped)).status).toBe(404); expect( await json( @@ -438,25 +438,26 @@ describe("repository URL mappings", () => { } as Fetcher, }; expect((await app.request("https://other.test/", {}, mapped)).headers.get("location")).toBe( - "https://repo.test/console", + "https://repo.test/#/", + ); + expect(await (await app.request("https://repo.test/", {}, mapped)).text()).toBe( + "/console/index.html", ); - expect( - await ( - await app.request( - "https://repo.test/console/test/repositories/releases", - {}, - mapped, - ) - ).text(), - ).toBe("/console/index.html"); expect( await (await app.request("https://repo.test/console/assets/app.js", {}, mapped)).text(), ).toBe("/console/assets/app.js"); + const location = async (url: string) => + (await app.request(url, {}, mapped)).headers.get("location"); + expect(await location("https://repo.test/console")).toBe("https://repo.test/#/"); expect( - (await app.request("https://other.test/invite/example", {}, mapped)).headers.get( - "location", - ), - ).toBe("https://repo.test/console/invite/example"); + await location("https://repo.test/console/test/repositories/releases?prefix=com/acme"), + ).toBe("https://repo.test/#/test/repositories/releases?prefix=com/acme"); + expect(await location("https://repo.test/console/invite/example")).toBe( + "https://repo.test/#/invite/example", + ); + expect(await location("https://other.test/invite/example")).toBe( + "https://repo.test/#/invite/example", + ); const favicon = await app.request("https://repo.test/favicon.ico", {}, mapped); expect(favicon.status).toBe(404); expect(favicon.headers.has("www-authenticate")).toBe(false); @@ -652,6 +653,7 @@ describe("account management and lifecycle", () => { }), 201, ); + expect(invitation.url).toMatch(/^https:\/\/repo\.test\/#\/invite\/[^/]+$/); const secret = invitation.url.split("/").at(-1); expect((await owner("/api/invitations/accept", "POST", { secret })).status).toBe(404); await json(await guest("/api/invitations/accept", "POST", { secret })); diff --git a/scripts/interop.mjs b/scripts/interop.mjs index 16dbba7..3a9000d 100644 --- a/scripts/interop.mjs +++ b/scripts/interop.mjs @@ -236,15 +236,13 @@ try { const page = await context.newPage(), errors = []; page.on("pageerror", (error) => errors.push(error.message)); - await page.goto(origin + "/console/test"); + await page.goto(origin + "/#/test"); await expect(page.getByRole("heading", { name: "Repositories", exact: true })).toBeVisible(); await expect(page.getByRole("link", { name: "Releases", exact: true })).toBeVisible(); const screenshots = join(root, ".artifacts"); await mkdir(screenshots, { recursive: true }); await page.screenshot({ path: join(screenshots, "repositories.png"), fullPage: true }); - await page.goto( - origin + "/console/test/repositories/releases?prefix=com/example/mavenr2/core/1.0.0", - ); + await page.goto(origin + "/#/test/repositories/releases?prefix=com/example/mavenr2/core/1.0.0"); await expect(page.getByRole("button", { name: "core-1.0.0.pom", exact: true })).toBeVisible(); await expect(page.locator("pre").filter({ hasText: "repositories {" })).toContainText( 'password = providers.environmentVariable("MAVEN_R2_READ_TOKEN").get()', @@ -269,7 +267,7 @@ try { SELECT repository_id,'zz/qa/1.0/qa-1.0-' || n || '.pom',object_key,size,sha256,checksums,publication_id,updated_at FROM files CROSS JOIN fixture WHERE path='com/example/mavenr2/core/1.0.0/core-1.0.0.pom'; `); - await page.goto(origin + "/console/test/repositories/releases"); + await page.goto(origin + "/#/test/repositories/releases"); await page.getByRole("button", { name: "Next page", exact: true }).click(); await expect(page).toHaveURL(/after=/); await page.getByRole("searchbox", { name: "Search artifact paths" }).fill("core-1.0.0.pom"); @@ -279,8 +277,8 @@ try { exact: true, }), ).toBeVisible(); - assert.equal(new URL(page.url()).searchParams.has("after"), false); - await page.goto(origin + "/console/test/tokens"); + assert.ok(!page.url().includes("after=")); + await page.goto(origin + "/#/test/tokens"); await page.getByRole("button", { name: "Create token", exact: true }).click(); const dialog = page.getByRole("dialog"); await dialog.getByLabel("Token name").fill("Browser verification"); @@ -293,6 +291,7 @@ try { await expect(page.getByText("Revoked", { exact: true })).toBeVisible(); await page.setViewportSize({ width: 390, height: 844 }); await page.goto(origin + "/console/test"); + await expect(page).toHaveURL(origin + "/#/test"); await expect(page.getByRole("heading", { name: "Repositories", exact: true })).toBeVisible(); await page.screenshot({ path: join(screenshots, "mobile.png"), fullPage: true }); assert.ok(