Public: anyone can read this; the crew can change it.
f2f: allow letters from any language in world names #10
Check · Review · Land · Live — No review yet.
f2f-fixer/voxel:fixer/main → main 4 commits fork
A guest
This pull comes from a guest: its preview runs on empty data, without the app's variables or a forge token, until a person who can write here approves it.
This pull waits for a person who can write here to review it; it never lands on its own.
3 files
changed +134
−117
README.md +113 −113
| @@ -1,113 +1,113 @@ | ||
| 1 | # voxel | |
| 2 | ||
| 3 | A block world in the browser. Name a world, walk into it, dig it up and build | |
| 4 | in it; come back later and it is how you left it. Hand-written WebGL2, no | |
| 5 | dependencies — the noise, the matrices, the mesher, the physics and the | |
| 6 | textures are all in `src/`. | |
| 7 | ||
| 8 | sh | |
| 9 | PORT=8080 FORGE_APP_DATA=./data bun run src/server.ts # run it here | |
| 10 | bun test # the check every pull runs | |
| 11 | FORGE_APP_DATA=./data bun run src/tty.ts # the terminal interface | |
| 12 | ||
| 13 | ||
| 14 | ## Playing it | |
| 15 | ||
| 16 | `W A S D` walks, `space` jumps, `shift` runs, the mouse looks once you click | |
| 17 | the page. Left click breaks the block you are pointing at, right click places | |
| 18 | the one in your hand, and `1`–`9` or the scroll wheel choose it. On a phone the | |
| 19 | left half of the screen is a thumb stick, the right half looks around, a tap | |
| 20 | breaks, and the buttons jump and place. The select at the top right sets how | |
| 21 | far you can see, from 64 to 192 metres. | |
| 22 | ||
| 23 | The sun crosses the sky every twenty minutes; caves and the undersides of | |
| 24 | things stay dark through the day, and the stars come out at night. | |
| 25 | ||
| 26 | Sheep, pigs and chickens wander the grass, fish keep to the water, and | |
| 27 | fireflies come out once it is properly dark. Nothing in this world can be | |
| 28 | hurt or killed: walk into one and it bolts, swing at one and it bolts, and | |
| 29 | the block behind it breaks anyway. They are not stored — see below. | |
| 30 | ||
| 31 | ## How it is put together | |
| 32 | ||
| 33 | - `src/world/` is the model, and it is the same code on both sides of the | |
| 34 | wire: `noise.ts` (seeded gradient noise), `terrain.ts` (height, caves, sea, | |
| 35 | trees), `chunk.ts`, `store.ts` (chunks plus the blocks people changed), | |
| 36 | `mesher.ts` (faces, ambient occlusion, a daylight bit), `physics.ts`, | |
| 37 | `raycast.ts`, `daylight.ts`. | |
| 38 | `mobs.ts` is the table of creatures — the boxes each one is made of, how it | |
| 39 | walks, swims or hovers, and where it is willing to live — and `herd.ts` is | |
| 40 | the creatures a world is holding right now. | |
| 41 | - `src/client/` is the browser: `worker.ts` generates and meshes chunks off | |
| 42 | the main thread, `renderer.ts` draws them, `mobmesh.ts` packs the whole herd | |
| 43 | into one buffer for one draw call, `input.ts` reads a keyboard or a thumb, | |
| 44 | `main.ts` is the loop. | |
| 45 | - `src/server.ts` routes and holds the security floor — every response | |
| 46 | carries it, including the 404, the 403, the 413, the 429 and the 500, and | |
| 47 | the page's one stylesheet runs on a nonce minted per response so the policy | |
| 48 | needs no `unsafe-inline`. `src/db.ts` is the schema; `src/valid.ts` is the | |
| 49 | boundary; `src/bucket.ts` is the rate limit; `src/pages/` renders HTML. | |
| 50 | - `src/bundle.ts` builds the two client bundles at boot and serves them from | |
| 51 | memory at a name that is a hash of their contents, cached for a year. | |
| 52 | ||
| 53 | What is real: the terrain, the physics, the lighting, the creatures and the | |
| 54 | saving. An animal is a body with a gait, run by the same collision code the | |
| 55 | player is, so it falls, jumps a step, stops at a wall and turns back at a | |
| 56 | cliff rather than following a path laid out for it. Nothing is faked or | |
| 57 | pre-baked — a world is a seed and a list of changed blocks, so | |
| 58 | storage stays small no matter how far you walk. There is no multiplayer: two | |
| 59 | people in one world will not see each other, though they do change the same | |
| 60 | blocks. Chunks are 16 x 64 x 16 and the world is 64 blocks tall. | |
| 61 | ||
| 62 | ## What it keeps | |
| 63 | ||
| 64 | Not the creatures. A chunk hashes its own animals out of the seed when it | |
| 65 | arrives and forgets them when it goes, so the herd is bounded by what is | |
| 66 | loaded around you (at most 40 at once) and a world is still only a seed and a | |
| 67 | list of changed blocks however long anything walks around in it. Two people | |
| 68 | in one world see different sheep, the same way they do not see each other. | |
| 69 | ||
| 70 | A world is a name and a seed. Only the blocks you change are stored, in | |
| 71 | SQLite under `FORGE_APP_DATA`. Where you are standing is kept per browser | |
| 72 | against a session cookie (HttpOnly, SameSite=Lax) — two people in the same | |
| 73 | world each get their own spot, their own time of day and their own hotbar. | |
| 74 | Worlds are public to anyone who can reach the app: anyone can open one by | |
| 75 | name and build in it, and a world you walk into that other people have | |
| 76 | already touched says so before you click in. | |
| 77 | ||
| 78 | Deleting is the one thing that is not shared. The session that made a world | |
| 79 | deletes it with one button; anyone else has to type the world's name back | |
| 80 | first, since there is no undo and no record of who did it. A world made | |
| 81 | before this rule existed belongs to nobody, so it takes the typed name from | |
| 82 | everyone. The session that owns a world is minted when the world is made, | |
| 83 | not taken from the request, so a cookie someone else planted in your browser | |
| 84 | owns nothing; what that browser had already saved moves across with it. The | |
| 85 | terminal has no session at all, so `d` there always asks for the name. | |
| 86 | ||
| 87 | Two limits keep the disk from being the only one: a copy of the app holds | |
| 88 | 500 worlds (`WORLD_CAP`) and a world holds 300 000 changed blocks | |
| 89 | (`EDIT_LIMIT`). Making worlds and saving blocks each have a token bucket in | |
| 90 | front of them (`src/bucket.ts`) so a client stuck in a loop is slowed rather | |
| 91 | than served. Request bodies are capped at 512 KB, counted as the bytes | |
| 92 | arrive rather than trusted from `Content-Length`. | |
| 93 | ||
| 94 | `/w/<name>/state.json` still sends every changed block in the world in one | |
| 95 | answer, which is the biggest thing this app hands out. It carries an ETag | |
| 96 | made of when the world last changed and when you last moved, so a returning | |
| 97 | player revalidates and gets a 304 instead of the bytes. Serving edits by | |
| 98 | chunk column — the shape that would bound one request by the chunk instead | |
| 99 | of by the world — is the change this does not make yet. | |
| 100 | ||
| 101 | It also has a terminal: the exact command is in `FORGE_APP_SSH` when the app | |
| 102 | runs (`ssh -p 2222 <name>@<forge>` — the app is the login name, the forge is | |
| 103 | the host; `<name>--<pull>@<forge>` for a preview's). It opens `src/tty.ts` on | |
| 104 | a real pty over the same worlds: `j`/`k` and `enter` to open one, `n` to make | |
| 105 | one, `d` to delete one — typing its name to confirm — then the same ground | |
| 106 | seen from above, where the cursor moves with `h j k l`, `x` breaks the top | |
| 107 | block and `1`–`9` place one. The creatures walk across that map while you | |
| 108 | watch it, on the same twenty-minute clock, and the line under it says what is | |
| 109 | about. | |
| 110 | ||
| 111 | The forge sets `PORT`, `FORGE_APP_DATA`, `FORGE_APP_ENV` | |
| 112 | (`production` or `preview`), `FORGE_APP_URL` and `FORGE_APP_COMMIT` | |
| 113 | when it starts the app. `forge.json` says how. | |
| 1 | # voxel | |
| 2 | ||
| 3 | A block world in the browser. Name a world, walk into it, dig it up and build | |
| 4 | in it; come back later and it is how you left it. Hand-written WebGL2, no | |
| 5 | dependencies — the noise, the matrices, the mesher, the physics and the | |
| 6 | textures are all in `src/`. | |
| 7 | ||
| 8 | ```sh | |
| 9 | PORT=8080 FORGE_APP_DATA=./data bun run src/server.ts # run it here | |
| 10 | bun test # the check every pull runs | |
| 11 | FORGE_APP_DATA=./data bun run src/tty.ts # the terminal interface | |
| 12 | ``` | |
| 13 | ||
| 14 | ## Playing it | |
| 15 | f2f: desk edit tab1 | |
| 16 | `W A S D` walks, `space` jumps, `shift` runs, the mouse looks once you click | |
| 17 | the page. Left click breaks the block you are pointing at, right click places | |
| 18 | the one in your hand, and `1`–`9` or the scroll wheel choose it. On a phone the | |
| 19 | left half of the screen is a thumb stick, the right half looks around, a tap | |
| 20 | breaks, and the buttons jump and place. The select at the top right sets how | |
| 21 | far you can see, from 64 to 192 metres. | |
| 22 | ||
| 23 | The sun crosses the sky every twenty minutes; caves and the undersides of | |
| 24 | things stay dark through the day, and the stars come out at night. | |
| 25 | ||
| 26 | Sheep, pigs and chickens wander the grass, fish keep to the water, and | |
| 27 | fireflies come out once it is properly dark. Nothing in this world can be | |
| 28 | hurt or killed: walk into one and it bolts, swing at one and it bolts, and | |
| 29 | the block behind it breaks anyway. They are not stored — see below. | |
| 30 | ||
| 31 | ## How it is put together | |
| 32 | ||
| 33 | - `src/world/` is the model, and it is the same code on both sides of the | |
| 34 | wire: `noise.ts` (seeded gradient noise), `terrain.ts` (height, caves, sea, | |
| 35 | trees), `chunk.ts`, `store.ts` (chunks plus the blocks people changed), | |
| 36 | `mesher.ts` (faces, ambient occlusion, a daylight bit), `physics.ts`, | |
| 37 | `raycast.ts`, `daylight.ts`. | |
| 38 | `mobs.ts` is the table of creatures — the boxes each one is made of, how it | |
| 39 | walks, swims or hovers, and where it is willing to live — and `herd.ts` is | |
| 40 | the creatures a world is holding right now. | |
| 41 | - `src/client/` is the browser: `worker.ts` generates and meshes chunks off | |
| 42 | the main thread, `renderer.ts` draws them, `mobmesh.ts` packs the whole herd | |
| 43 | into one buffer for one draw call, `input.ts` reads a keyboard or a thumb, | |
| 44 | `main.ts` is the loop. | |
| 45 | - `src/server.ts` routes and holds the security floor — every response | |
| 46 | carries it, including the 404, the 403, the 413, the 429 and the 500, and | |
| 47 | the page's one stylesheet runs on a nonce minted per response so the policy | |
| 48 | needs no `unsafe-inline`. `src/db.ts` is the schema; `src/valid.ts` is the | |
| 49 | boundary; `src/bucket.ts` is the rate limit; `src/pages/` renders HTML. | |
| 50 | - `src/bundle.ts` builds the two client bundles at boot and serves them from | |
| 51 | memory at a name that is a hash of their contents, cached for a year. | |
| 52 | ||
| 53 | What is real: the terrain, the physics, the lighting, the creatures and the | |
| 54 | saving. An animal is a body with a gait, run by the same collision code the | |
| 55 | player is, so it falls, jumps a step, stops at a wall and turns back at a | |
| 56 | cliff rather than following a path laid out for it. Nothing is faked or | |
| 57 | pre-baked — a world is a seed and a list of changed blocks, so | |
| 58 | storage stays small no matter how far you walk. There is no multiplayer: two | |
| 59 | people in one world will not see each other, though they do change the same | |
| 60 | blocks. Chunks are 16 x 64 x 16 and the world is 64 blocks tall. | |
| 61 | ||
| 62 | ## What it keeps | |
| 63 | ||
| 64 | Not the creatures. A chunk hashes its own animals out of the seed when it | |
| 65 | arrives and forgets them when it goes, so the herd is bounded by what is | |
| 66 | loaded around you (at most 40 at once) and a world is still only a seed and a | |
| 67 | list of changed blocks however long anything walks around in it. Two people | |
| 68 | in one world see different sheep, the same way they do not see each other. | |
| 69 | ||
| 70 | A world is a name and a seed. Only the blocks you change are stored, in | |
| 71 | SQLite under `FORGE_APP_DATA`. Where you are standing is kept per browser | |
| 72 | against a session cookie (HttpOnly, SameSite=Lax) — two people in the same | |
| 73 | world each get their own spot, their own time of day and their own hotbar. | |
| 74 | Worlds are public to anyone who can reach the app: anyone can open one by | |
| 75 | name and build in it, and a world you walk into that other people have | |
| 76 | already touched says so before you click in. | |
| 77 | ||
| 78 | Deleting is the one thing that is not shared. The session that made a world | |
| 79 | deletes it with one button; anyone else has to type the world's name back | |
| 80 | first, since there is no undo and no record of who did it. A world made | |
| 81 | before this rule existed belongs to nobody, so it takes the typed name from | |
| 82 | everyone. The session that owns a world is minted when the world is made, | |
| 83 | not taken from the request, so a cookie someone else planted in your browser | |
| 84 | owns nothing; what that browser had already saved moves across with it. The | |
| 85 | terminal has no session at all, so `d` there always asks for the name. | |
| 86 | ||
| 87 | Two limits keep the disk from being the only one: a copy of the app holds | |
| 88 | 500 worlds (`WORLD_CAP`) and a world holds 300 000 changed blocks | |
| 89 | (`EDIT_LIMIT`). Making worlds and saving blocks each have a token bucket in | |
| 90 | front of them (`src/bucket.ts`) so a client stuck in a loop is slowed rather | |
| 91 | than served. Request bodies are capped at 512 KB, counted as the bytes | |
| 92 | arrive rather than trusted from `Content-Length`. | |
| 93 | ||
| 94 | `/w/<name>/state.json` still sends every changed block in the world in one | |
| 95 | answer, which is the biggest thing this app hands out. It carries an ETag | |
| 96 | made of when the world last changed and when you last moved, so a returning | |
| 97 | player revalidates and gets a 304 instead of the bytes. Serving edits by | |
| 98 | chunk column — the shape that would bound one request by the chunk instead | |
| 99 | of by the world — is the change this does not make yet. | |
| 100 | ||
| 101 | It also has a terminal: the exact command is in `FORGE_APP_SSH` when the app | |
| 102 | runs (`ssh -p 2222 <name>@<forge>` — the app is the login name, the forge is | |
| 103 | the host; `<name>--<pull>@<forge>` for a preview's). It opens `src/tty.ts` on | |
| 104 | a real pty over the same worlds: `j`/`k` and `enter` to open one, `n` to make | |
| 105 | one, `d` to delete one — typing its name to confirm — then the same ground | |
| 106 | seen from above, where the cursor moves with `h j k l`, `x` breaks the top | |
| 107 | block and `1`–`9` place one. The creatures walk across that map while you | |
| 108 | watch it, on the same twenty-minute clock, and the line under it says what is | |
| 109 | about. | |
| 110 | ||
| 111 | The forge sets `PORT`, `FORGE_APP_DATA`, `FORGE_APP_ENV` | |
| 112 | (`production` or `preview`), `FORGE_APP_URL` and `FORGE_APP_COMMIT` | |
| 113 | when it starts the app. `forge.json` says how. |
src/server.test.ts +13 −1
| @@ -3,7 +3,7 @@ import { mkdtempSync } from "node:fs"; | ||
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | 5 | import { Store, WORLD_CAP, open } from "./db.ts"; |
| 6 | import { editsFrom, playerFrom, seedFrom, worldName } from "./valid.ts"; | |
| 6 | import { NAME_PATTERN, editsFrom, playerFrom, seedFrom, worldName } from "./valid.ts"; | |
| 7 | 7 | |
| 8 | 8 | process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-")); |
| 9 | 9 | process.env.PORT = "0"; |
| @@ -335,6 +335,18 @@ test("names, seeds and edits are checked before anything else sees them", () => | ||
| 335 | 335 | expect(worldName("<script>")).toBeNull(); |
| 336 | 336 | expect(worldName("a".repeat(25))).toBeNull(); |
| 337 | 337 | expect(worldName(42)).toBeNull(); |
| 338 | expect(worldName("Café")).toBe("Café"); | |
| 339 | expect(worldName("Zürich 2")).toBe("Zürich 2"); | |
| 340 | expect(worldName("Cafe\u0301")).toBe("Café"); | |
| 341 | expect(worldName("東京_タワー")).toBe("東京_タワー"); | |
| 342 | expect(worldName("नमस्ते")).toBe("नमस्ते"); | |
| 343 | expect(worldName("ü".repeat(24))).toBe("ü".repeat(24)); | |
| 344 | expect(worldName("ü".repeat(25))).toBeNull(); | |
| 345 | expect(worldName("-Zürich")).toBeNull(); | |
| 346 | expect(worldName("Zürich!")).toBeNull(); | |
| 347 | expect(worldName("Zürich/../x")).toBeNull(); | |
| 348 | // The browser compiles the pattern attribute with the v flag; it must parse there too. | |
| 349 | expect(new RegExp(`^(?:${NAME_PATTERN})$`, "v").test("Zürich-1")).toBe(true); | |
| 338 | 350 | expect(seedFrom("1234")).toBe(1234); |
| 339 | 351 | expect(seedFrom("hello")).toBe(seedFrom("hello")); |
| 340 | 352 | expect(seedFrom("hello")).not.toBe(seedFrom("hellp")); |
src/valid.ts +8 −3
| @@ -9,15 +9,20 @@ 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}"; | |
| 13 | const NAME = new RegExp(`^${NAME_PATTERN}$`); | |
| 12 | /** | |
| 13 | * A letter or digit from any script, then up to 23 more of those, combining | |
| 14 | * marks, spaces, underscores and hyphens. The hyphen is escaped so the same | |
| 15 | * source works in a browser's `pattern` attribute, which compiles with `v`. | |
| 16 | */ | |
| 17 | export const NAME_PATTERN = String.raw`[\p{L}\p{N}][\p{L}\p{M}\p{N} _\-]{0,23}`; | |
| 18 | const NAME = new RegExp(`^${NAME_PATTERN}$`, "u"); | |
| 14 | 19 | /** Blocks can be placed this far from the origin, in either direction. */ |
| 15 | 20 | export const WORLD_LIMIT = 1_000_000; |
| 16 | 21 | export const MAX_EDITS_PER_REQUEST = 2048; |
| 17 | 22 | |
| 18 | 23 | export function worldName(input: unknown): string | null { |
| 19 | 24 | if (typeof input !== "string") return null; |
| 20 | const name = input.trim().replace(/\s+/g, " "); | |
| 25 | const name = input.normalize("NFC").trim().replace(/\s+/g, " "); | |
| 21 | 26 | return NAME.test(name) ? name : null; |
| 22 | 27 | } |
| 23 | 28 |
new above
offline