Fix issues found in review of host pairing

- Saves of a datastore run one at a time, and changes made while saving
  are saved by the next save. Overlapping saves from approving and
  revoking hosts could write an older snapshot after a newer one.
- Map subscribers fetch everything again when a new session starts, as
  the controller only sends values that exist. A host revoked while a
  browser was disconnected stayed in its list.
- The host token check accepts the same pre-rename tokens as the
  WebSocket, so upgrading a host with such a token does not make it ask
  to join again.
This commit is contained in:
Danielv123 committed 2026-10-30 21:49:53 +01:00
1 parent 2cf7c66187
commit 1ed46dff9c
8 files changed
+121 -30

No files matched your search

+16 -3
View File
@@ -166,6 +166,7 @@ export abstract class Datastore<
"update": [ updates: U[] ],
}> {
private dirty = false;
private saving: Promise<void> = Promise.resolve();
constructor(
private provider: DatastoreProvider<K, V> = new MemoryDatastoreProvider(),
@@ -181,10 +182,22 @@ export abstract class Datastore<
// Save the datastore to the provider
async save() {
if (this.dirty) {
await this.provider.save(this.data);
// Saves run one at a time so an older snapshot can't be written after a newer one.
const save = this.saving.then(async () => {
if (!this.dirty) {
return;
}
// Cleared before writing so changes made while saving are saved by the next save.
this.dirty = false;
}
try {
await this.provider.save(this.data);
} catch (err) {
this.dirty = true;
throw err;
}
});
this.saving = save.catch(() => {});
await save;
}
// Load the datastore from the provider
+10
View File
@@ -725,6 +725,16 @@ export class MapSubscriber<
/** Current value of the subscribed resource */
values = new Map<K, V>();
override handleConnectionEvent(event: "connect" | "drop" | "resume" | "close") {
if (event === "connect") {
// A new session is only sent values that exist, not ones removed while disconnected,
// so everything is fetched again.
this.values.clear();
this.lastUpdatedMs = -1;
}
super.handleConnectionEvent(event);
}
protected override getLastUpdatedTimeMs(event: E) {
let maxTime = this.lastUpdatedMs;
for (const update of event.updates) {