Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Make pixel-plays run here #2

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/pixel-plays:torque/1-make-pixel-plays-run-here → main 1 commit fork

  • Merged. torque/1-make-pixel-plays-run-here was merged into main as cedb888.

Review

Sign in to review this change.
4 files changed +60 −6
README.md +3 −1
@@ -22,7 +22,9 @@ Requires [Node.js](https://nodejs.org/); nothing to install.
2222 node serve.mjs
2323 ```
2424
25 Then open http://127.0.0.1:5300/. Any static file server works, and the site deploys as plain static files.
25 Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and address; `bun serve.mjs` works the same. Any static file server works, and the site deploys as plain static files.
26
27 On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. The app keeps no state, so it does not need `FORGE_APP_DATA`. `bun test` checks that the server answers every game page and track.
2628
2729 ## Add a game
2830 Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js`. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`.
forge.json added +7
@@ -0,0 +1,7 @@
1 {
2 "app": {
3 "run": "bun serve.mjs",
4 "health": "/",
5 "test": "bun test"
6 }
7 }
serve.mjs +14 −5
@@ -1,20 +1,29 @@
11 import { createServer } from 'node:http';
2 import { readFile } from 'node:fs/promises';
2 import { readFile, stat } from 'node:fs/promises';
33 import { extname, join, normalize, sep } from 'node:path';
44 import { fileURLToPath } from 'node:url';
55
66 const root = fileURLToPath(new URL('.', import.meta.url));
77 const types = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.css': 'text/css', '.png': 'image/png', '.mp3': 'audio/mpeg' };
8 const port = process.env.PORT ? Number(process.env.PORT) : 5300;
9 const host = process.env.HOST || '127.0.0.1';
810
9 createServer(async (req, res) => {
10 const path = normalize(decodeURIComponent(new URL(req.url, 'http://x').pathname));
11 export const server = createServer(async (req, res) => {
12 let path;
13 try {
14 path = normalize(decodeURIComponent(new URL(req.url, 'http://x').pathname));
15 } catch {
16 return res.writeHead(400).end('bad request');
17 }
1118 const target = join(root, path.endsWith(sep) || path === sep ? join(path, 'index.html') : path);
1219 if (!target.startsWith(root)) return res.writeHead(403).end();
1320 try {
21 // a game folder without its trailing slash: redirect like vercel.json's trailingSlash, so relative assets resolve
22 if ((await stat(target)).isDirectory()) return res.writeHead(308, { location: path + '/' }).end();
1423 const body = await readFile(target);
15 res.writeHead(200, { 'content-type': types[extname(target)] || 'application/octet-stream', 'cache-control': 'no-store' });
24 res.writeHead(200, { 'content-type': types[extname(target)] || 'application/octet-stream', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer' });
1625 res.end(body);
1726 } catch {
1827 res.writeHead(404).end('not found');
1928 }
20 }).listen(5300, '127.0.0.1');
29 }).listen(port, host);
serve.test.mjs added +36
@@ -0,0 +1,36 @@
1 import { test, expect, afterAll } from 'bun:test';
2
3 process.env.PORT = '0';
4 const { server } = await import('./serve.mjs');
5 await new Promise(r => server.listening ? r() : server.once('listening', r));
6 const base = `http://127.0.0.1:${server.address().port}`;
7 afterAll(() => server.close());
8
9 test('/ answers with the redirect page to the first game', async () => {
10 const res = await fetch(base + '/');
11 expect(res.status).toBe(200);
12 expect(await res.text()).toContain('url=balote/');
13 });
14
15 test('every game page and its track is served', async () => {
16 for (const id of ['balote', 'stari-most', 'laundry', 'tie-break', 'mini-ramp']) {
17 const page = await fetch(`${base}/${id}/`);
18 expect(page.status).toBe(200);
19 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
20 const track = await fetch(`${base}/${id}/${id}.mp3`, { method: 'HEAD' });
21 expect(track.headers.get('content-type')).toBe('audio/mpeg');
22 }
23 });
24
25 test('a game folder without its slash redirects to it', async () => {
26 const res = await fetch(base + '/balote', { redirect: 'manual' });
27 expect(res.status).toBe(308);
28 expect(res.headers.get('location')).toBe('/balote/');
29 });
30
31 test('missing files 404, malformed paths 400, and traversal stays inside', async () => {
32 expect((await fetch(base + '/nope.js')).status).toBe(404);
33 expect((await fetch(base + '/%E0%A4%A')).status).toBe(400);
34 const res = await fetch(base + '/%2e%2e/%2e%2e/etc/passwd');
35 expect([403, 404]).toContain(res.status);
36 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs