Plugins belong to the project, and they load before it: the project file gained the field plugins: ["demo-plugin.js"] (default empty, no formatVersion bump), the Plugins tab's switch writes into it as an ordinary undo step (SetPluginActiveCommand) instead of into a server-wide plugins.json, and the start order is turned around - startPlugins only fetches the list of files, then editor.boot() runs and ProjectStore.load(id, prepare) hands the raw JSON through before it is deserialized, where pluginsOf(raw) reads the list and PluginManager.syncTo() activates exactly those; that is the point of the issue, because what a plugin registers has to stand there before normalizeGraph would throw it away as unknown (#126, #127), a project switch first stops what the new project does not name and then starts what it does, a file the server does not have is passed over silently, and after signing out the manager is dead so a late event cannot start anything in a torn-down session; on the server plugins.json, PluginStore::setActive, isActive, the route PUT ?r=plugins/{file} and the 403 for inactive modules are gone and GET ?r=plugins only answers {file,bytes,updatedAt}, which makes every plugin file fetchable - the consequence accepted in #117, written down in the wiki for #115 and #116; publishPlugins(root) only copies files now, the seeder switches the demo plugin on in the fixture project, and the Plugins tab carries a puzzle piece as its icon while only a plugin's own tab still goes without one (the knob and the socket are cubic curves, not arcs, which Pixi's parser draws as a spike after an L); 3 new e2e checks in module 38 and 5 new unit tests cover the project as the owner of the switch, its undo, the order, pluginsOf against crude input and an unknown file name (#119)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c830c7a3aa
commit
5ba796a2b6
28 files changed
+425
-269
No files matched your search
+4
-18
@@ -35,9 +35,8 @@ declare(strict_types=1);
|
||||
* POST ?r=sounds&name=x.wav raw sound bytes → {file,name,bytes}
|
||||
* DELETE ?r=sounds/{file} → 204
|
||||
*
|
||||
* GET ?r=plugins → [{file,bytes,updatedAt,active}], sorted by file name
|
||||
* GET ?r=plugins/{file} → the module source as text/javascript | 403 while switched off
|
||||
* PUT ?r=plugins/{file} {active} → {file,bytes,updatedAt,active}
|
||||
* GET ?r=plugins → [{file,bytes,updatedAt}], sorted by file name
|
||||
* GET ?r=plugins/{file} → the module source as text/javascript
|
||||
*
|
||||
* `name` is the file's original name, kept beside it as `{file}.json` (see OriginalName) and
|
||||
* empty when none is known. It is a caption, never a path: the stored file is always `{hash}.{ext}`.
|
||||
@@ -299,17 +298,6 @@ function routePlugins(array $parts, string $method): never
|
||||
Response::json($store->list());
|
||||
}
|
||||
|
||||
if ($method === 'PUT') {
|
||||
$active = Response::body()['active'] ?? null;
|
||||
if (!is_bool($active)) {
|
||||
Response::error('active must be true or false', 422);
|
||||
}
|
||||
$entry = $store->setActive($parts[1], $active);
|
||||
if ($entry === null) {
|
||||
Response::error('Plugin not found', 404);
|
||||
}
|
||||
Response::json($entry);
|
||||
}
|
||||
if ($method !== 'GET') {
|
||||
Response::error('Method not allowed', 405);
|
||||
}
|
||||
@@ -318,10 +306,8 @@ function routePlugins(array $parts, string $method): never
|
||||
if ($source === null) {
|
||||
Response::error('Plugin not found', 404);
|
||||
}
|
||||
// Switched off means not run: the editor never asks for such a module, and nobody else may.
|
||||
if (!$store->isActive($parts[1])) {
|
||||
Response::error('Plugin not active', 403);
|
||||
}
|
||||
// Every plugin file is handed out: since #119 the project says which of them the editor runs,
|
||||
// and the server has no way of knowing which project the asking tab has open.
|
||||
header('Content-Type: text/javascript; charset=utf-8');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
// A replaced file has to be the one the next editor start runs.
|
||||
|
||||
+12
-93
@@ -2,16 +2,13 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* The editor plugins in `data/plugins/`: one ES module per `.js` file. Whether one is switched on
|
||||
* is kept beside them in `plugins.json` (`{"demo-plugin.js": {"active": true}}`), server-wide; a
|
||||
* file the state does not name is off, so a plugin put there runs only once someone switched it
|
||||
* on (#114). The folder is not served statically: a module is only ever handed out by its bare
|
||||
* file name, and only while it is active, so no request can reach past it.
|
||||
* The editor plugins in `data/plugins/`: one ES module per `.js` file. Whether one runs is written
|
||||
* in the project that is open, not here (#119) – the server hands out every plugin it has and knows
|
||||
* nothing about switches. The folder is still not served statically: a module is only ever handed
|
||||
* out by its bare file name, so no request can reach past it.
|
||||
*/
|
||||
final class PluginStore
|
||||
{
|
||||
public const STATE_FILE = 'plugins.json';
|
||||
|
||||
public function __construct(private readonly string $dir)
|
||||
{
|
||||
}
|
||||
@@ -23,30 +20,28 @@ final class PluginStore
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorted by file name, which is also the order the editor activates them in. Every plugin,
|
||||
* active or not – the editor's Plugins tab is where one is switched on.
|
||||
* Sorted by file name, which is also the order the editor activates them in.
|
||||
*
|
||||
* @return list<array{file: string, bytes: int, updatedAt: int, active: bool}>
|
||||
* @return list<array{file: string, bytes: int, updatedAt: int}>
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
$files = is_dir($this->dir) ? scandir($this->dir) : [];
|
||||
$state = $this->readState() ?? [];
|
||||
$out = [];
|
||||
foreach ($files === false ? [] : $files as $file) {
|
||||
if ($this->exists($file)) {
|
||||
$out[] = $this->entry($file, $state);
|
||||
$path = "{$this->dir}/{$file}";
|
||||
$out[] = [
|
||||
'file' => $file,
|
||||
'bytes' => (int) filesize($path),
|
||||
'updatedAt' => (int) filemtime($path),
|
||||
];
|
||||
}
|
||||
}
|
||||
usort($out, static fn (array $a, array $b): int => strcmp($a['file'], $b['file']));
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function isActive(string $file): bool
|
||||
{
|
||||
return $this->exists($file) && self::activeIn($this->readState() ?? [], $file);
|
||||
}
|
||||
|
||||
/** The module's source, or null when there is no such plugin. */
|
||||
public function read(string $file): ?string
|
||||
{
|
||||
@@ -57,84 +52,8 @@ final class PluginStore
|
||||
return $source === false ? null : $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches a plugin on or off and returns its entry as `list` has it; null when there is no
|
||||
* such plugin. The state file is rewritten whole under a lock, so two switches at once cannot
|
||||
* lose each other's change. A state file that is not readable JSON is not overwritten: that
|
||||
* would silently switch off everything it named.
|
||||
*
|
||||
* @return array{file: string, bytes: int, updatedAt: int, active: bool}|null
|
||||
*/
|
||||
public function setActive(string $file, bool $active): ?array
|
||||
{
|
||||
if (!$this->exists($file)) {
|
||||
return null;
|
||||
}
|
||||
$lock = fopen("{$this->dir}/." . self::STATE_FILE . '.lock', 'c');
|
||||
if ($lock === false || !flock($lock, LOCK_EX)) {
|
||||
throw new RuntimeException('Could not lock the plugin state');
|
||||
}
|
||||
try {
|
||||
$state = $this->readState();
|
||||
if ($state === null) {
|
||||
throw new RuntimeException(self::STATE_FILE . ' is not readable JSON – left as it is');
|
||||
}
|
||||
$state[$file] = ['active' => $active];
|
||||
ksort($state);
|
||||
$path = "{$this->dir}/" . self::STATE_FILE;
|
||||
$json = json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n";
|
||||
$tmp = $path . '.' . bin2hex(random_bytes(4)) . '.tmp';
|
||||
if (file_put_contents($tmp, $json) === false || !rename($tmp, $path)) {
|
||||
@unlink($tmp);
|
||||
throw new RuntimeException('Could not write ' . self::STATE_FILE);
|
||||
}
|
||||
return $this->entry($file, $state);
|
||||
} finally {
|
||||
flock($lock, LOCK_UN);
|
||||
fclose($lock);
|
||||
}
|
||||
}
|
||||
|
||||
private function exists(string $file): bool
|
||||
{
|
||||
return self::isValidName($file) && is_file("{$this->dir}/{$file}");
|
||||
}
|
||||
|
||||
/**
|
||||
* The state as stored; an empty one when there is no file yet, null when there is one that
|
||||
* cannot be read.
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function readState(): ?array
|
||||
{
|
||||
$path = "{$this->dir}/" . self::STATE_FILE;
|
||||
if (!is_file($path)) {
|
||||
return [];
|
||||
}
|
||||
$raw = file_get_contents($path);
|
||||
$state = $raw === false ? null : json_decode($raw, true);
|
||||
return is_array($state) ? $state : null;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $state */
|
||||
private static function activeIn(array $state, string $file): bool
|
||||
{
|
||||
return is_array($state[$file] ?? null) && ($state[$file]['active'] ?? false) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $state
|
||||
* @return array{file: string, bytes: int, updatedAt: int, active: bool}
|
||||
*/
|
||||
private function entry(string $file, array $state): array
|
||||
{
|
||||
$path = "{$this->dir}/{$file}";
|
||||
return [
|
||||
'file' => $file,
|
||||
'bytes' => (int) filesize($path),
|
||||
'updatedAt' => (int) filemtime($path),
|
||||
'active' => self::activeIn($state, $file),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user