Public: anyone can read this; the crew can change it.
f2f: world names up to 32 characters #9
f2f-maker/voxel:maker/main → main 2 commits fork
5 files
changed +14
−9
src/pages/home.ts +3 −3
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { escape, head, where, commit } from "./layout.ts"; |
| 8 | import { NAME_PATTERN } from "../valid.ts"; | |
| 8 | import { NAME_MAX, NAME_PATTERN } from "../valid.ts"; | |
| 9 | 9 | import type { World } from "../db.ts"; |
| 10 | 10 | |
| 11 | 11 | const STYLE = ` |
| @@ -57,7 +57,7 @@ function deleteForm(name: string, mine: boolean): string { | ||
| 57 | 57 | return `<details class="drop"><summary>delete</summary><div> |
| 58 | 58 | <p>You did not make ${escape(name)}. Type its name to delete it and everything in it.</p> |
| 59 | 59 | <form method="post" action="/worlds/delete">${hidden} |
| 60 | <input class="confirm" name="confirm" maxlength="24" placeholder="${escape(name)}" | |
| 60 | <input class="confirm" name="confirm" maxlength="${NAME_MAX}" placeholder="${escape(name)}" | |
| 61 | 61 | aria-label="Type the world's name to confirm" required> |
| 62 | 62 | <button class="quiet">delete</button> |
| 63 | 63 | </form> |
| @@ -87,7 +87,7 @@ export function homePage( | ||
| 87 | 87 | ${commit ? `<code>${escape(commit)}</code>` : ""}</p> |
| 88 | 88 | |
| 89 | 89 | <form class="new" method="post" action="/worlds"> |
| 90 | <input name="name" placeholder="World name" required maxlength="24" pattern="${NAME_PATTERN}" | |
| 90 | <input name="name" placeholder="World name" required maxlength="${NAME_MAX}" pattern="${NAME_PATTERN}" | |
| 91 | 91 | title="Letters, digits, spaces, dashes and underscores" autofocus> |
| 92 | 92 | <input name="seed" placeholder="Seed (optional)" maxlength="32"> |
| 93 | 93 | <button>Make a world</button> |
src/server.test.ts +4 −1
| @@ -49,6 +49,7 @@ test("the front page lists worlds and hands out a session", async () => { | ||
| 49 | 49 | const body = await response.text(); |
| 50 | 50 | expect(body).toContain("<h1>voxel</h1>"); |
| 51 | 51 | expect(body).toContain("No worlds yet"); |
| 52 | expect(body).toContain(`maxlength="32" pattern="[A-Za-z0-9][A-Za-z0-9 _-]{0,31}"`); | |
| 52 | 53 | }); |
| 53 | 54 | |
| 54 | 55 | test("every response stands on the security floor", async () => { |
| @@ -333,7 +334,9 @@ test("names, seeds and edits are checked before anything else sees them", () => | ||
| 333 | 334 | expect(worldName("hi/there")).toBeNull(); |
| 334 | 335 | expect(worldName("")).toBeNull(); |
| 335 | 336 | expect(worldName("<script>")).toBeNull(); |
| 336 | expect(worldName("a".repeat(25))).toBeNull(); | |
| 337 | expect(worldName("Saturday building session 01")).toBe("Saturday building session 01"); | |
| 338 | expect(worldName("a".repeat(32))).toBe("a".repeat(32)); | |
| 339 | expect(worldName("a".repeat(33))).toBeNull(); | |
| 337 | 340 | expect(worldName(42)).toBeNull(); |
| 338 | 341 | expect(seedFrom("1234")).toBe(1234); |
| 339 | 342 | expect(seedFrom("hello")).toBe(seedFrom("hello")); |
src/server.ts +2 −2
| @@ -11,7 +11,7 @@ import { Buckets } from "./bucket.ts"; | ||
| 11 | 11 | import { buildClient } from "./bundle.ts"; |
| 12 | 12 | import { gamePage } from "./pages/game.ts"; |
| 13 | 13 | import { homePage } from "./pages/home.ts"; |
| 14 | import { editsFrom, playerFrom, seedFrom, worldName } from "./valid.ts"; | |
| 14 | import { NAME_MAX, editsFrom, playerFrom, seedFrom, worldName } from "./valid.ts"; | |
| 15 | 15 | import { HOTBAR } from "./world/blocks.ts"; |
| 16 | 16 | import { findSpawn } from "./world/terrain.ts"; |
| 17 | 17 | |
| @@ -221,7 +221,7 @@ export const server = Bun.serve({ | ||
| 221 | 221 | if (form === OVER) return tooBig(cookie); |
| 222 | 222 | const name = form && worldName(form.get("name")); |
| 223 | 223 | if (!name) { |
| 224 | return page("no", `<p>A world's name is 1 to 24 letters, digits, spaces, dashes or underscores. | |
| 224 | return page("no", `<p>A world's name is 1 to ${NAME_MAX} letters, digits, spaces, dashes or underscores. | |
| 225 | 225 | <a href="/">Try again.</a></p>`, 400, cookie); |
| 226 | 226 | } |
| 227 | 227 | let world = store.world(name); |
src/tty.ts +2 −2
| @@ -19,7 +19,7 @@ import { findSpawn } from "./world/terrain.ts"; | ||
| 19 | 19 | import { DAY_SECONDS, daylight, timeName } from "./world/daylight.ts"; |
| 20 | 20 | import { Herd } from "./world/herd.ts"; |
| 21 | 21 | import { KINDS } from "./world/mobs.ts"; |
| 22 | import { seedFrom, worldName } from "./valid.ts"; | |
| 22 | import { NAME_MAX, seedFrom, worldName } from "./valid.ts"; | |
| 23 | 23 | |
| 24 | 24 | const store = new Store(open()); |
| 25 | 25 | const where = process.env.FORGE_APP_ENV === "preview" |
| @@ -224,7 +224,7 @@ process.stdin.on("data", (key: string) => { | ||
| 224 | 224 | prompt = undefined; |
| 225 | 225 | } else if (key === "\x1b") { prompt = undefined; says = ""; } |
| 226 | 226 | else if (key === "\x7f") prompt = { ...prompt, text: prompt.text.slice(0, -1) }; |
| 227 | else if (key >= " " && prompt.text.length < 24) prompt = { ...prompt, text: prompt.text + key }; | |
| 227 | else if (key >= " " && prompt.text.length < NAME_MAX) prompt = { ...prompt, text: prompt.text + key }; | |
| 228 | 228 | draw(); |
| 229 | 229 | return; |
| 230 | 230 | } |
src/valid.ts +3 −1
| @@ -9,7 +9,9 @@ import { isBlockId } from "./world/blocks.ts"; | ||
| 9 | 9 | import { hash3 } from "./world/noise.ts"; |
| 10 | 10 | import type { Edit, Player } from "./db.ts"; |
| 11 | 11 | |
| 12 | export const NAME_PATTERN = "[A-Za-z0-9][A-Za-z0-9 _-]{0,23}"; | |
| 12 | /** The longest a world's name may be, in characters. */ | |
| 13 | export const NAME_MAX = 32; | |
| 14 | export const NAME_PATTERN = `[A-Za-z0-9][A-Za-z0-9 _-]{0,${NAME_MAX - 1}}`; | |
| 13 | 15 | const NAME = new RegExp(`^${NAME_PATTERN}$`); |
| 14 | 16 | /** Blocks can be placed this far from the origin, in either direction. */ |
| 15 | 17 | export const WORLD_LIMIT = 1_000_000; |
new above
offline