The editor gets a name of its own that fits beside Brandmauer, Glutspeicher and Feuerwehr. This is stage 1, 2 and 4 of #422: the name, the URL and the stored state in the browser. Repository, CI runners, containers and the GAME_MAKER_* variables follow in stage 3. - All paths come from SITE_PATH in src/core/site.ts: the API endpoint, the data folders, the play page and the base in vite.config.ts. A later move is one line instead of fifteen. - The window title, the sign-in screen, the playground and the README say Feuerwerk. The package, __feuerwerkPlay, the take worklet and the writing app in Ogg and Matroska follow. - The session cookie is now feuerwerk_session, so everyone signs in once more. - localStorage keys are now feuerwerk.*. moveOldStoredKeys copies the old game-maker.* keys over on the first start, so settings, panel sizes and save games survive. - tools/ci/serve.sh serves the build under /feuerwerk/. The variable GAME_MAKER_BASE_URL keeps its name until stage 3. Outside the repository, nockal.com/feuerwerk/ is a second symlink to the same tree. nginx sends the bare /game-maker/ folder there with a 301. Everything else under /game-maker/ is still served directly, so shared play links and the API keep working. Finding: when nginx redirects a path without a trailing slash or with a query, the Brandmauer answers with its own 307 to the same URL plus a slash, placed after the query. A play link would lose its token that way, which is why only the bare folder is redirected. Tests: - Gate on melektaus-atx: CHECK EXIT 0, 307 files, 4756 tests (4 new in site.test.ts). - Counter-check: when the migration misses the old prefix, 3 of the 4 new tests fail. - e2e 60 under /feuerwerk/: ALL PASSED, 20 checks. - From outside: /feuerwerk/ 200, /game-maker/ 301, an old play link 200. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.4 KiB
TypeScript
94 lines
3.4 KiB
TypeScript
import type { Project } from '../core/model/Project';
|
|
import { celFilesOf } from '../core/model/pixelart';
|
|
import { type CelPixels, celFileName, decodeCel, encodeCel, isEmptyCel } from '../core/model/pixelCel';
|
|
import { dataPath } from '../core/site';
|
|
import { FileLibrary } from './FileLibrary';
|
|
|
|
/** Where cels go on the server: raw bytes in, the stored name back. */
|
|
export interface CelUploader {
|
|
upload(bytes: ArrayBuffer, type: string): Promise<{ file: string }>;
|
|
}
|
|
|
|
/**
|
|
* The pixels of pixel art (#130) in memory, keyed by cel file. A cel painted here is named from its
|
|
* bytes and kept at once, so the model can point at it while nothing has left the browser yet; the
|
|
* cels the server does not hold are uploaded before the project that names them is saved (answer 1).
|
|
* A cel from the server is loaded on the first ask, like a texture.
|
|
*/
|
|
export class PixelCels extends FileLibrary<CelPixels> {
|
|
private readonly cels = new Map<string, CelPixels>();
|
|
/** Painted here and not on the server yet. */
|
|
private readonly local = new Set<string>();
|
|
|
|
constructor() {
|
|
super(dataPath('pixels'), 'pixels');
|
|
}
|
|
|
|
/**
|
|
* The pixels of a cel: null for the empty name and for a file that cannot be read, undefined while
|
|
* it loads. A broken file reads as empty rather than holding the picture back for good.
|
|
*/
|
|
read(file: string): CelPixels | null | undefined {
|
|
if (!file) return null;
|
|
const cel = this.cels.get(file);
|
|
if (cel) return cel;
|
|
if (this.hasFailed(file)) return null;
|
|
this.startLoading(file);
|
|
return undefined;
|
|
}
|
|
|
|
/** Keeps painted pixels and returns the name they go by; an empty cel is `''`, no file at all. */
|
|
put(cel: CelPixels): string {
|
|
if (isEmptyCel(cel)) return '';
|
|
const bytes = encodeCel(cel);
|
|
const file = celFileName(bytes);
|
|
if (!this.cels.has(file)) {
|
|
this.cels.set(file, { width: cel.width, height: cel.height, data: cel.data.slice() });
|
|
this.local.add(file);
|
|
}
|
|
return file;
|
|
}
|
|
|
|
/** The cels this project names that only this browser holds. */
|
|
pending(project: Project): string[] {
|
|
const files = new Set<string>();
|
|
for (const asset of project.assets)
|
|
if (asset.type === 'pixelart')
|
|
for (const file of celFilesOf(asset)) if (this.local.has(file)) files.add(file);
|
|
return [...files];
|
|
}
|
|
|
|
/**
|
|
* Sends every cel the project needs and the server lacks. A name the server works out differently
|
|
* is an error: the project would point at a file that is not there.
|
|
*/
|
|
async upload(project: Project, store: CelUploader): Promise<void> {
|
|
for (const file of this.pending(project)) {
|
|
const cel = this.cels.get(file);
|
|
if (!cel) continue;
|
|
const bytes = encodeCel(cel);
|
|
const stored = await store.upload(bytes.buffer as ArrayBuffer, 'application/octet-stream');
|
|
if (stored.file !== file) throw new Error(`The server stored pixels as ${stored.file}, not ${file}`);
|
|
this.local.delete(file);
|
|
}
|
|
}
|
|
|
|
protected async fetchFile(url: string): Promise<CelPixels> {
|
|
const response = await fetch(url);
|
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
const cel = decodeCel(new Uint8Array(await response.arrayBuffer()));
|
|
if (!cel) throw new Error('not a cel');
|
|
return cel;
|
|
}
|
|
|
|
protected keep(file: string, cel: CelPixels): void {
|
|
this.cels.set(file, cel);
|
|
}
|
|
|
|
protected holds(file: string): boolean {
|
|
return this.cels.has(file);
|
|
}
|
|
}
|
|
|
|
export const pixelCels = new PixelCels();
|