Files
feuerwerk/api/lib/Store.php
T
adminandClaude Opus 5.5 14972d057f
game-maker / gate (push) Successful in 3m53s
game-maker / suite (push) Successful in 11m55s
Refactoring round 8: hide data files, safe saves, Esc as Cancel (#85)
Recurring cleanup round over the code since round 7 (bc688f3). The
issue now also asks to fix bugs and small inconsistencies found on the
way, so unlike the rounds before, this one changes behaviour in a few
places. Duplicated code in api went from 23 clones with 228 lines to
none (jscpd, at least 50 tokens). In src it stays at 22 clones with
271 lines, the same before and after.

Bugs:
- nginx served project and snapshot files as they were, so anyone who
  knew a project id could read it without signing in. Every file the
  API writes now goes through AtomicFile (temp file, mode, rename).
  Projects, snapshots, releases, accounts and sessions are 0640, and
  nginx answers 403 for them.
- Any signed-in account could delete a shared image, font or sound,
  which projects of other accounts may wear. Deleting one is now for
  administrators only. The editor never deleted a file, so the
  client's delete is gone.
- Two saves of one project could run at once. The lock file was
  removed after each save, so a waiting save and the next one locked
  different files. The lock file now stays, and deleting a project
  takes the lock as well.
- Bytes that are no picture got a 500 instead of a 422, because the
  error handler also threw for warnings silenced with @.
- A session file was rewritten in place. A request reading it at that
  moment found it empty and was signed out. It is replaced atomically.
- Delete did nothing for a shape picked in the vector art list. Delete
  now goes through removeSelection, one case per kind of selection,
  and a kind without a case fails the type check.

Inconsistencies:
- Escape on a question does what its Cancel does. Cancel on "Delete
  project?" and "Restore snapshot?" leads back to the picker or the
  list like the other questions did, and so does deleting a project.
- Editor texts use American spelling throughout, like the port names:
  48 British spellings in 45 lines (colour, centre, initialised).
- objectName and groupName replace eleven copies of "own name, else
  the asset's" and three of "Group". One copy fell back to the object
  id instead of "?".

Duplicates, dead code, comments:
- routeFiles serves images, fonts and sounds instead of three copies.
  HashedFileStore holds what the three stores shared: keeping a file
  under its hash with its name, listing and deleting. The stores keep
  only which bytes they take and what a record says.
- AtomicFile replaces nine copies of temp file plus rename.
- removeShapes is the one way the canvas, the inspector button and
  Delete take shapes out.
- GraphInterpreter.fire was called by tests only. The 54 calls use a
  helper in tests/helpers.ts now.
- FileStore.delete, HttpFileStore.delete and the PluginApi type were
  unused. A doc comment in actions.ts sat above the wrong function.

Checked and left as is:
- solidCells is used by tests only. It is the per-cell view before the
  merge into rectangles, the one place the largest-scale rule shows.
- An undo checker ran all 124 commands of the unit tests and compared
  the project before and after undo. The only differences are empty
  ports, groups and notes lists where there were none, which the file
  format drops anyway.

Docs:
- README: the check sum.
- Wiki, own commit: API (admin-only deletes, 422, file modes, lock
  file), Home (the "Not yet" list named three things that exist),
  Concepts, Keyboard, Vector art, Architecture and Development, and
  the 38 characters Gitea flagged as ambiguous on eight pages.
- .gitignore: the lock files, and pasted-*.png in any folder, which
  keeps the screenshot archive out.

Tests:
- ./check.sh: 195 files, 2817 tests, exit 0 (+2 for the name helpers).
- PHP: accounts 52 in the web container and 53 in the CI image with
  the race test (+2), snapshots 15 (+1), releases 20.
- Full e2e run: ALL PASSED, 1586 checks (+6 in 12, 47, 49 and 50).
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks. The example
  project files were restored byte for byte afterwards.
- The file stores of HEAD and of this commit give byte-identical
  output for the live folders and for store, list and delete in a
  throwaway folder.
- Counter-checks, all fail as expected: a 500 for bad bytes fails 12,
  no admin check fails 49, removing the lock file again loses saves
  in 3 of 3 runs (151 to 170 of 321), Delete ignoring shapes fails 47,
  Escape that only closes fails 50, and dropping the shape case fails
  the type check (TS2366).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 18:12:55 +02:00

232 lines
8.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/** Thrown by Store::save when the client's revision is older than the stored one. */
final class StaleRevisionException extends RuntimeException
{
public function __construct(public readonly int $currentRevision)
{
parent::__construct("Project was modified elsewhere (revision {$currentRevision})");
}
}
/**
* File-based project store: one JSON document per project in `data/projects/{id}.json`.
* Each file wraps the project with metadata: { id, name, owner, revision, updatedAt, project }.
* Writes are atomic (temp file + rename) and guarded by an integer revision so that a
* stale client (second tab, old session) cannot silently overwrite newer work.
*
* A store belongs to one signed-in user: it lists, reads and writes only the projects whose
* `owner` is that user. Someone else's project is answered as missing rather than as forbidden,
* because "this id exists" is already more than a stranger should learn.
*
* An administrator's store (`$seesAll`, #179) reads, writes and deletes every project as if it
* were their own, and a save keeps the owner it found. The list stays their own: the others are
* reached through the user management, one account at a time.
*/
final class Store
{
public function __construct(
private readonly string $dir,
private readonly string $owner,
private readonly bool $seesAll = false,
) {
if (!is_dir($this->dir) && !mkdir($this->dir, 0775, true) && !is_dir($this->dir)) {
throw new RuntimeException("Cannot create data directory {$this->dir}");
}
}
public static function isValidId(string $id): bool
{
return (bool) preg_match('/^[a-z0-9]{6,32}$/', $id);
}
public static function newId(): string
{
return substr(bin2hex(random_bytes(8)), 0, 12);
}
/** @return list<array{id:string,name:string,revision:int,updatedAt:string}> */
public function list(): array
{
return $this->listOf($this->owner);
}
/** @return list<array{id:string,name:string,revision:int,updatedAt:string}> */
public function listOf(string $owner): array
{
$items = [];
foreach (glob($this->dir . '/*.json') ?: [] as $file) {
$doc = $this->readFile($file);
if ($doc === null || $doc['owner'] !== $owner) {
continue;
}
$items[] = [
'id' => $doc['id'],
'name' => $doc['name'],
'revision' => $doc['revision'],
'updatedAt' => $doc['updatedAt'],
];
}
usort($items, static fn(array $a, array $b): int => strcmp($b['updatedAt'], $a['updatedAt']));
return $items;
}
/** @return array{id:string,name:string,owner:string,revision:int,updatedAt:string,project:array}|null */
public function load(string $id): ?array
{
return $this->own($this->readFile($this->path($id)));
}
/** @return array{id:string,name:string,owner:string,revision:int,updatedAt:string,project:array} */
public function create(string $name): array
{
do {
$id = self::newId();
} while (file_exists($this->path($id)));
$project = [
'formatVersion' => 1,
'id' => $id,
'name' => $name,
'assets' => [],
'activeSceneId' => '',
];
$doc = $this->document($id, $name, $this->owner, 1, $project);
$this->writeFile($this->path($id), $doc);
return $doc;
}
/**
* Saves a project when `$expectedRevision` matches the stored one.
* Returns the new document, null when the project does not exist, or throws
* StaleRevisionException (carrying the current revision) on a conflict.
*/
public function save(string $id, int $expectedRevision, array $project): ?array
{
$path = $this->path($id);
return $this->locked($path, function () use ($path, $id, $expectedRevision, $project): ?array {
$current = $this->own($this->readFile($path));
if ($current === null) {
return null;
}
if ($current['revision'] !== $expectedRevision) {
throw new StaleRevisionException($current['revision']);
}
$name = is_string($project['name'] ?? null) && $project['name'] !== '' ? $project['name'] : $current['name'];
$project['id'] = $id;
$doc = $this->document($id, $name, $current['owner'], $expectedRevision + 1, $project);
$this->writeFile($path, $doc);
return $doc;
});
}
public function delete(string $id): bool
{
$path = $this->path($id);
// Under the save lock, or a save running at the same time writes the project back.
$deleted = $this->locked($path, fn(): bool => $this->load($id) !== null && unlink($path));
if ($deleted) {
// Whoever still waits for the lock finds no project and writes nothing.
@unlink($path . '.lock');
}
return $deleted;
}
/**
* Hands every project of `$from` to `$to` – what renaming an account does, since the name is
* the owner. Revision and date stay: nothing in the project changed, and a tab that has one of
* them open must not be told it was modified elsewhere. Returns how many moved.
*/
public function transfer(string $from, string $to): int
{
$moved = 0;
foreach (glob($this->dir . '/*.json') ?: [] as $file) {
// Under the save lock, or a save running at the same time writes the old owner back.
$moved += $this->locked($file, function () use ($file, $from, $to): int {
$doc = $this->readFile($file);
if ($doc === null || $doc['owner'] !== $from) {
return 0;
}
$doc['owner'] = $to;
$this->writeFile($file, $doc);
return 1;
});
}
return $moved;
}
/**
* The lock file stays when the work is done. Removing it would let two saves in at once: one
* still waiting holds the removed file, while the next one creates a new file and locks that.
*
* @template T
* @param callable():T $work
* @return T
*/
private function locked(string $path, callable $work): mixed
{
$lock = fopen($path . '.lock', 'c');
if ($lock === false || !flock($lock, LOCK_EX)) {
throw new RuntimeException('Cannot acquire project lock');
}
try {
return $work();
} finally {
flock($lock, LOCK_UN);
fclose($lock);
}
}
/** The document, or null when it belongs to someone else and this store does not see everything. */
private function own(?array $doc): ?array
{
return $doc !== null && ($this->seesAll || $doc['owner'] === $this->owner) ? $doc : null;
}
private function path(string $id): string
{
return $this->dir . '/' . $id . '.json';
}
private function document(string $id, string $name, string $owner, int $revision, array $project): array
{
return [
'id' => $id,
'name' => $name,
'owner' => $owner,
'revision' => $revision,
'updatedAt' => gmdate('c'),
'project' => $project,
];
}
private function readFile(string $path): ?array
{
if (!is_file($path)) {
return null;
}
$raw = file_get_contents($path);
$doc = $raw === false ? null : json_decode($raw, true);
if (!is_array($doc) || !isset($doc['id'], $doc['revision'], $doc['project'])) {
return null;
}
$doc['name'] = is_string($doc['name'] ?? null) ? $doc['name'] : 'Untitled';
// A file without an owner belongs to nobody and is therefore invisible to everybody –
// the safe way round, and the one that shows up straight away if a migration is missed.
$doc['owner'] = is_string($doc['owner'] ?? null) ? $doc['owner'] : '';
$doc['updatedAt'] = is_string($doc['updatedAt'] ?? null) ? $doc['updatedAt'] : '';
$doc['revision'] = (int) $doc['revision'];
return $doc;
}
private function writeFile(string $path, array $doc): void
{
$json = json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($json === false) {
throw new RuntimeException('Cannot encode project');
}
AtomicFile::write($path, $json, AtomicFile::HIDDEN, 'project file');
}
}