Public: anyone can read this; the crew can change it.
The home page's forge-name field says "free" or "taken" as you type #11283
mason/forge:mason/11239-the-home-page-s-forge-name-field-says-fr → main 6 commits fork
11 files
changed +128
−34
docs/features/start-my-forge.md +4
| @@ -17,6 +17,10 @@ In the terminal, a guest's "Start my forge" command (`src/tui/screens/startforge | ||
| 17 | 17 | |
| 18 | 18 | The form carries `from=home`. If the door refuses a stranger's start (rate limit, name taken, bad or reserved name, no room), they get a 303 back to `/?start=<kind>&name=<name>` and the home page shows one fixed sentence (`src/web/start_refusal.ts`) above the field, with the name kept. Only a kind from a fixed list is read; the door's own message, codes and times are never shown. Tests: `test/home-start-forge.test.ts`. |
| 19 | 19 | |
| 20 | ## The name check | |
| 21 | ||
| 22 | Typing in the field asks `GET /forges/check?name=…` (a small script, `nameCheckJs` in `src/spawn/name_check.ts`) and writes one line under it: Free, Taken, or why the name is not allowed. It applies the door's rules: the name's shape, the reserved words (`src/core/reserved.ts`) and the forges that exist or are being made. It is counted per address at the `name` door in `src/http/strangers.ts` (120 a minute; 429 with `Retry-After` beyond). The line has a fixed height, so the page does not move. Without JavaScript the form posts as before; the door still decides. Tests: `test/forge-name-check.test.ts`. | |
| 23 | ||
| 20 | 24 | ## Three screens, no link to copy |
| 21 | 25 | |
| 22 | 26 | When the door accepts a start from the home page (`from=home`, signed out), the browser is not shown the claim link. If the new forge already answers, it gets a 303 straight to its `/claim#<code>` form; if it is still booting, a "Making your forge" page polls `GET /forges/<name>/ready` and goes on by itself (`walkOn` in `src/web/pages/new-forge.ts`). On the claim form the person picks a username and password and lands signed in as the forge's admin. Screens: `/`, the claim form, the forge. Test: `test/stranger-three-screens.test.ts` (in the gate, which runs every test file). |
src/actions/spawn.ts +5
| @@ -194,6 +194,11 @@ async function readLedger(app: App, name: string): Promise<Ledger | undefined> { | ||
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | /** Whether a forge by this name exists or is being made: what the door answers 409 for. */ | |
| 198 | export async function forgeNameTaken(app: App, name: string): Promise<boolean> { | |
| 199 | return inFlight.has(name) || (await readLedger(app, name)) !== undefined; | |
| 200 | } | |
| 201 | ||
| 197 | 202 | async function writeLedger(app: App, ledger: Ledger): Promise<void> { |
| 198 | 203 | await mkdir(join(app.config.dataDir, "spawn"), { recursive: true }); |
| 199 | 204 | const path = ledgerPath(app, ledger.name); |
src/http/server.ts +2
| @@ -279,6 +279,7 @@ import { forgeLinkRevoke, forgeLinkSubmit, forgeLinkUndo, forgeRemoveConfirm, fo | ||
| 279 | 279 | forgeOwnCodeSubmit, forgesPage } from "../web/pages/forges.ts"; |
| 280 | 280 | import { forgeItSubmit } from "../web/pages/forge-it.ts"; |
| 281 | 281 | import { funnelSubmit } from "./funnel.ts"; |
| 282 | import { forgeNameCheck } from "../spawn/name_check.ts"; | |
| 282 | 283 | import { startForgeSubmit } from "../web/pages/start-forge.ts"; |
| 283 | 284 | import { newForgePage, newForgeSubmit } from "../web/pages/new-forge.ts"; |
| 284 | 285 | import { askedPage } from "../web/studio/asked_page.ts"; |
| @@ -855,6 +856,7 @@ export function buildRouter(): Router<Handler> { | ||
| 855 | 856 | router.add("GET", "/forges/:name/remove", forgeRemoveConfirm); |
| 856 | 857 | router.add("POST", "/forges/:name/remove", forgeRemoveSubmit); |
| 857 | 858 | router.add("GET", "/forges/:name/ready", forgeReady); |
| 859 | router.add("GET", "/forges/check", forgeNameCheck); | |
| 858 | 860 | router.add("GET", "/forges/new", newForgePage); |
| 859 | 861 | router.add("POST", "/forges/new", newForgeSubmit); |
| 860 | 862 | router.add("POST", "/forges/start", startForgeSubmit); |
src/http/strangers.ts +3 −3
| @@ -23,7 +23,7 @@ import { transferGate } from "../git/env.ts"; | ||
| 23 | 23 | import { bucketOf, clientBucket } from "./peer.ts"; |
| 24 | 24 | |
| 25 | 25 | /** A door a stranger is counted at, each with its own minute. */ |
| 26 | export type Door = "api" | "git" | "code" | "plan"; | |
| 26 | export type Door = "api" | "git" | "code" | "plan" | "name"; | |
| 27 | 27 | |
| 28 | 28 | export interface StrangerLimits { |
| 29 | 29 | /** Requests per minute through each door, per client address. */ |
| @@ -59,8 +59,8 @@ export interface StrangerLimits { | ||
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | export const STRANGER_LIMITS: StrangerLimits = { |
| 62 | perAddress: { api: 600, git: 60, code: 10, plan: 10 }, | |
| 63 | forge: { api: 6000, git: 600, code: 60, plan: 60 }, | |
| 62 | perAddress: { api: 600, git: 60, code: 10, plan: 10, name: 120 }, | |
| 63 | forge: { api: 6000, git: 600, code: 60, plan: 60, name: 1200 }, | |
| 64 | 64 | transfersPerAddress: 4, |
| 65 | 65 | transferShare: 0.5, |
| 66 | 66 | transferTimeoutMs: 7_200_000, |
src/spawn/name_check.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | /** | |
| 2 | * "Is this forge name free?" for the home page's name field (#11239): the | |
| 3 | * same rules the door applies — the name's shape (`plan.ts`), the words | |
| 4 | * nobody new may take (`core/reserved.ts`), and the forges that exist or are | |
| 5 | * being made — answered as one fixed word and sentence. The answer never | |
| 6 | * echoes what was typed. Counted per address at the `name` door. | |
| 7 | */ | |
| 8 | ||
| 9 | import { forgeNameTaken } from "../actions/spawn.ts"; | |
| 10 | import type { App } from "../app.ts"; | |
| 11 | import { reservedName } from "../core/reserved.ts"; | |
| 12 | import { json, tryAgain } from "../http/respond.ts"; | |
| 13 | import { takeDoor } from "../http/strangers.ts"; | |
| 14 | import type { Ctx } from "../http/context.ts"; | |
| 15 | import { NAME } from "./plan.ts"; | |
| 16 | ||
| 17 | export type NameState = "free" | "taken" | "reserved" | "invalid"; | |
| 18 | ||
| 19 | export const NAME_WORDS: Readonly<Record<NameState, string>> = { | |
| 20 | free: "Free", | |
| 21 | taken: "Taken", | |
| 22 | reserved: "Not allowed: that name is kept for the forge itself", | |
| 23 | invalid: "Not allowed: use 1 to 39 lowercase letters, digits or dashes, not starting or ending with a dash", | |
| 24 | }; | |
| 25 | ||
| 26 | export async function nameState(app: App, name: string): Promise<NameState> { | |
| 27 | if (!NAME.test(name)) return "invalid"; | |
| 28 | if (reservedName(app.db, name) !== undefined) return "reserved"; | |
| 29 | return (await forgeNameTaken(app, name)) ? "taken" : "free"; | |
| 30 | } | |
| 31 | ||
| 32 | /** GET /forges/check?name=… — any visitor; reads only, so no origin check. */ | |
| 33 | export async function forgeNameCheck(ctx: Ctx): Promise<Response> { | |
| 34 | const verdict = takeDoor(ctx.app, "name", ctx.request); | |
| 35 | if (!verdict.allowed) return tryAgain("rate_limited", 429, "slow down", verdict.retryAfter, { "cache-control": "no-store" }); | |
| 36 | const name = (ctx.url.searchParams.get("name") ?? "").slice(0, 64); | |
| 37 | const state = await nameState(ctx.app, name); | |
| 38 | return json({ state, message: NAME_WORDS[state] }, 200, { "cache-control": "no-store" }); | |
| 39 | } | |
| 40 | ||
| 41 | /** The field's side: asks after a short pause and writes the answer into #start-name-says. Without it the form simply posts. */ | |
| 42 | export const nameCheckJs = `(function(d){ | |
| 43 | var i=d.getElementById("start-name"),o=d.getElementById("start-name-says"),t,n=0; | |
| 44 | if(!i||!o||!window.fetch)return; | |
| 45 | i.addEventListener("input",function(){ | |
| 46 | clearTimeout(t);var v=i.value.trim(); | |
| 47 | if(!v){o.textContent="";return} | |
| 48 | t=setTimeout(function(){var k=++n; | |
| 49 | fetch("/forges/check?name="+encodeURIComponent(v),{credentials:"same-origin"}).then(function(r){return r.ok?r.json():null}).then(function(j){if(j&&k==n)o.textContent=j.message}).catch(function(){})},250)}) | |
| 50 | })(document)`; |
src/spawn/plan.ts +1 −1
| @@ -34,7 +34,7 @@ export const MAX_BUDGET_USD = 1000; | ||
| 34 | 34 | |
| 35 | 35 | /** One line saying what a fresh app is. */ |
| 36 | 36 | export const MAX_FRESH_CHARS = 200; |
| 37 | const NAME = /^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$/; | |
| 37 | export const NAME = /^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$/; | |
| 38 | 38 | const PLAN_KEY = /^[a-z][a-z0-9-]{0,19}$/; |
| 39 | 39 | const APP_REF = /^[A-Za-z0-9_.-]{1,100}\/[A-Za-z0-9_.-]{1,100}$/; |
| 40 | 40 |
src/web/pages/start-forge.ts −13
| @@ -18,19 +18,6 @@ import { page, redirect } from "../layout.ts"; | ||
| 18 | 18 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| 19 | 19 | import { startRefusalHref, startRefusalOf } from "../start_refusal.ts"; |
| 20 | 20 | |
| 21 | /** The home's live name check: 404 from the ready probe means nothing by that name was made here. */ | |
| 22 | export const startNameJs = `(function(d){ | |
| 23 | var i=d.getElementById("start-name"),s=d.getElementById("start-name-says"),t,k=0; | |
| 24 | if(!i||!s||!window.fetch)return; | |
| 25 | function say(m){s.textContent=m} | |
| 26 | i.addEventListener("input",function(){ | |
| 27 | clearTimeout(t);var n=i.value.trim().toLowerCase(),q=++k; | |
| 28 | if(!n){say("");return} | |
| 29 | if(!/^[a-z0-9][a-z0-9-]*$/.test(n)){say("Use lowercase letters, digits and dashes.");return} | |
| 30 | t=setTimeout(function(){fetch("/forges/"+encodeURIComponent(n)+"/ready",{credentials:"omit"}).then(function(r){ | |
| 31 | if(q!==k)return;say(r.status===404?n+" is free.":r.status===200?n+" is taken. Try another.":"")}).catch(function(){})},300)}) | |
| 32 | })(document)`; | |
| 33 | ||
| 34 | 21 | /** Waits for the forge, then goes to its claim page; after a minute and a half the link is still there. */ |
| 35 | 22 | export const startWaitJs = `(function(d){ |
| 36 | 23 | var a=d.getElementById("start-go"),s=d.getElementById("start-wait"); |
src/web/studio/home.ts +2 −2
| @@ -25,10 +25,10 @@ import { accountsClosedIn } from "../../core/users.ts"; | ||
| 25 | 25 | import { markTold, untoldAnswer } from "../../core/requests.ts"; |
| 26 | 26 | import { requestsCard } from "../requests_card.ts"; |
| 27 | 27 | import { plansRow } from "../plans_row.ts"; |
| 28 | import { nameCheckJs } from "../../spawn/name_check.ts"; | |
| 28 | 29 | import { freePlanName, funnelJs, noteHomeVisit } from "../funnel_door.ts"; |
| 29 | 30 | import { publicPlans } from "../../settings/plans.ts"; |
| 30 | 31 | import { getDoorSettings } from "../../settings/door.ts"; |
| 31 | import { startNameJs } from "../pages/start-forge.ts"; | |
| 32 | 32 | import { GET_A_FORGE_PATH } from "../../billing/plan_words.ts"; |
| 33 | 33 | import { appsForRepos } from "../../core/apps.ts"; |
| 34 | 34 | import { WEIGHT_MAX, WEIGHT_NAMES } from "../../core/front.ts"; |
| @@ -928,7 +928,7 @@ bun run src/main.ts</code></pre> | ||
| 928 | 928 | title: "forge: software for what you're building, yours to keep", |
| 929 | 929 | description: "Your own forge, with a private AI crew that builds your apps, games and business tools. Open source, and yours to keep.", |
| 930 | 930 | body, |
| 931 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}\n${startNameJs}`, | |
| 931 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}\n${nameCheckJs}`, | |
| 932 | 932 | jsonLd: siteStructuredData(origin, running().version), |
| 933 | 933 | ...(visitor |
| 934 | 934 | ? { |
test/design-audit.test.ts +1 −1
| @@ -15,7 +15,7 @@ const doc = readFileSync("docs/design/audit.md", "utf8"); | ||
| 15 | 15 | |
| 16 | 16 | /** Routes that answer a file, a fragment, a redirect hop or a callback. */ |
| 17 | 17 | const NOT_PAGES = |
| 18 | /^\/(assets\/|\.well-known\/|icons\/|og\/|live\/|__forge\/|go\/|new\/issue$|events$|system\/stream$|talk\/stream$|settings\/teams\/stream$|sw\.js$|robots\.txt$|favicon\.ico$|apple-touch-icon(-precomposed)?\.png$|sitemap\.xml$|manifest\.webmanifest$|opensearch\.xml$|llms\.txt$|openapi\.json$|releases\.json$|changelog\.atom$|explore\.atom$|docs(\/[^/]*)?$|audit\.csv$|settings\/traffic\/scrapes\/export$|fleet\/graph\/live$|fleet\/graph\.csv$|settings\/origins\/callback$|home\/archived\/|settings\/backups\/live$|api\/v1(\/|$))/; | |
| 18 | /^\/(assets\/|\.well-known\/|icons\/|og\/|live\/|__forge\/|go\/|new\/issue$|forges\/check$|events$|system\/stream$|talk\/stream$|settings\/teams\/stream$|sw\.js$|robots\.txt$|favicon\.ico$|apple-touch-icon(-precomposed)?\.png$|sitemap\.xml$|manifest\.webmanifest$|opensearch\.xml$|llms\.txt$|openapi\.json$|releases\.json$|changelog\.atom$|explore\.atom$|docs(\/[^/]*)?$|audit\.csv$|settings\/traffic\/scrapes\/export$|fleet\/graph\/live$|fleet\/graph\.csv$|settings\/origins\/callback$|home\/archived\/|settings\/backups\/live$|api\/v1(\/|$))/; | |
| 19 | 19 | |
| 20 | 20 | const getRoutes = [ |
| 21 | 21 | ...server.matchAll(/router\.add\(\s*"GET",\s*"([^"]+)"/g), |
test/forge-name-check.test.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** The home page's name field says free / taken / why not (#11239). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | ||
| 9 | let dataDir: string; | |
| 10 | let app: App; | |
| 11 | let servers: Servers; | |
| 12 | beforeAll(async () => { | |
| 13 | dataDir = await mkdtemp(join(tmpdir(), "forge-name-")); | |
| 14 | app = createApp({ dataDir, port: 0 }); | |
| 15 | servers = await serve(app); | |
| 16 | await mkdir(join(dataDir, "spawn"), { recursive: true }); | |
| 17 | await writeFile(join(dataDir, "spawn", "acme.json"), JSON.stringify({ name: "acme", bodyId: "b1", done: true })); | |
| 18 | }); | |
| 19 | afterAll(async () => { | |
| 20 | await servers.stop?.(); | |
| 21 | app.close(); | |
| 22 | await rm(dataDir, { recursive: true, force: true }); | |
| 23 | }); | |
| 24 | ||
| 25 | const check = async (name: string) => (await fetch(`${servers.url}/forges/check?name=${encodeURIComponent(name)}`)).json() as Promise<{ state: string; message: string }>; | |
| 26 | ||
| 27 | test("the check answers free, taken, reserved and invalid", async () => { | |
| 28 | expect((await check("my-new-forge")).state).toBe("free"); | |
| 29 | expect((await check("acme")).state).toBe("taken"); | |
| 30 | expect((await check("admin")).state).toBe("reserved"); | |
| 31 | expect((await check("Bad Name!")).state).toBe("invalid"); | |
| 32 | expect((await check("")).state).toBe("invalid"); | |
| 33 | }); | |
| 34 | ||
| 35 | test("the answer never echoes the name and is not cached", async () => { | |
| 36 | const res = await fetch(`${servers.url}/forges/check?name=%3Cscript%3E`); | |
| 37 | expect(res.headers.get("cache-control")).toBe("no-store"); | |
| 38 | expect(await res.text()).not.toContain("script"); | |
| 39 | }); | |
| 40 | ||
| 41 | test("the home page has a fixed-height answer line and the form still posts without JavaScript", async () => { | |
| 42 | const body = await (await fetch(`${servers.url}/`)).text(); | |
| 43 | const form = body.match(/<form[^>]*id="start-forge"[\s\S]*?<\/form>/)?.[0] ?? ""; | |
| 44 | expect(form).toContain('id="start-name-says"'); | |
| 45 | expect(form).toContain('action="/forges/start"'); | |
| 46 | expect(form).toMatch(/method="post"/i); | |
| 47 | }); | |
| 48 | ||
| 49 | test("the check is rate-limited per address", async () => { | |
| 50 | let last = 200; | |
| 51 | for (let i = 0; i < 130 && last === 200; i++) last = (await fetch(`${servers.url}/forges/check?name=x${i}`)).status; | |
| 52 | expect(last).toBe(429); | |
| 53 | }); |
test/home-start-forge.test.ts +7 −14
| @@ -184,7 +184,7 @@ function runScript(js: string, els: Record<string, Fake>, answer: (url: string) | ||
| 184 | 184 | g["fetch"] = (url: string) => { |
| 185 | 185 | asked.push(url); |
| 186 | 186 | const a = answer(url); |
| 187 | return a === "fail" ? Promise.reject(new Error("down")) : Promise.resolve({ status: a.status, json: () => Promise.resolve(a.json) }); | |
| 187 | return a === "fail" ? Promise.reject(new Error("down")) : Promise.resolve({ status: a.status, ok: a.status < 400, json: () => Promise.resolve(a.json) }); | |
| 188 | 188 | }; |
| 189 | 189 | g["setTimeout"] = (fn: () => void) => queueMicrotask(fn); |
| 190 | 190 | const restore = () => Object.assign(g, saved); |
| @@ -195,27 +195,20 @@ const settle = async () => { | ||
| 195 | 195 | for (let i = 0; i < 400; i++) await Promise.resolve(); |
| 196 | 196 | }; |
| 197 | 197 | |
| 198 | test("the live check: 404 reads free, 200 reads taken, a bad name never asks", async () => { | |
| 199 | const { startNameJs } = await import("../src/web/pages/start-forge.ts"); | |
| 198 | test("the live check asks the door's own rules; a blank name never asks", async () => { | |
| 199 | const { nameCheckJs } = await import("../src/spawn/name_check.ts"); | |
| 200 | 200 | const input = fake({}, "Sunny-Day"); |
| 201 | 201 | const says = fake(); |
| 202 | const run = runScript(startNameJs, { "start-name": input, "start-name-says": says }, (u) => ({ status: u.includes("sunny-day") ? 404 : 200 })); | |
| 202 | const run = runScript(nameCheckJs, { "start-name": input, "start-name-says": says }, (u) => ({ status: 200, json: { message: u.includes("Sunny-Day") ? "Free." : "Taken." } })); | |
| 203 | 203 | try { |
| 204 | 204 | input.on["input"]!(); |
| 205 | 205 | await settle(); |
| 206 | expect(run.asked).toEqual(["/forges/sunny-day/ready"]); | |
| 207 | expect(says.textContent).toBe("sunny-day is free."); | |
| 208 | input.value = "taken"; | |
| 209 | input.on["input"]!(); | |
| 210 | await settle(); | |
| 211 | expect(says.textContent).toBe("taken is taken. Try another."); | |
| 212 | input.value = "No Good!"; | |
| 213 | input.on["input"]!(); | |
| 214 | expect(says.textContent).toBe("Use lowercase letters, digits and dashes."); | |
| 215 | expect(run.asked.length).toBe(2); | |
| 206 | expect(run.asked).toEqual(["/forges/check?name=Sunny-Day"]); | |
| 207 | expect(says.textContent).toBe("Free."); | |
| 216 | 208 | input.value = ""; |
| 217 | 209 | input.on["input"]!(); |
| 218 | 210 | expect(says.textContent).toBe(""); |
| 211 | expect(run.asked.length).toBe(1); | |
| 219 | 212 | } finally { |
| 220 | 213 | run.restore(); |
| 221 | 214 | } |
new above
offline