fix(decoder): connect even when the browser storage is full
A big installation outgrew the localStorage quota; saveState() threw QuotaExceededError and, since connecting saves first, the decoder never came online. The local copy is only a cache of the server state, so a write that doesn't fit now drops it instead of throwing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c8a797fe6f
commit
2c6c37f6ca
9 files changed
+99
-4
No files matched your search
@@ -34,6 +34,31 @@ export async function runDecoderUrlSmoke({ evaluate, navigate, savedState, waitF
|
||||
} finally {
|
||||
await second.close();
|
||||
}
|
||||
|
||||
// A full browser storage (big installation: results, training ledger, logo) made every
|
||||
// saveState() throw QuotaExceededError - and Connect saves before it connects, so the
|
||||
// decoder never came online. The local copy is only a cache of the server state.
|
||||
const third = await startFakeDecoder();
|
||||
try {
|
||||
const thirdUrl = `ws://127.0.0.1:${third.port}`;
|
||||
await evaluate(`(() => {
|
||||
window.__realSetItem = Storage.prototype.setItem;
|
||||
Storage.prototype.setItem = function (key, value) {
|
||||
if (key === "rc_timing_control_v1") throw new DOMException("Setting the value of '" + key + "' exceeded the quota.", "QuotaExceededError");
|
||||
return window.__realSetItem.call(this, key, value);
|
||||
};
|
||||
const input = document.querySelector('#settingsForm [name="wsUrl"]');
|
||||
input.value = ${JSON.stringify(thirdUrl)};
|
||||
document.getElementById("settingsConnect").click();
|
||||
})()`);
|
||||
await waitFor(async () => third.totalConnections() > 0, "connects although the browser storage is full");
|
||||
await waitFor(async () => (await savedState()).settings.wsUrl === thirdUrl, "and still saves to the server");
|
||||
assert.equal(await evaluate(`localStorage.getItem("rc_timing_control_v1")`), null, "the stale local copy is dropped");
|
||||
console.log("PASS a full browser storage no longer stops the decoder from connecting or saving to the server");
|
||||
} finally {
|
||||
await evaluate(`window.__realSetItem && (Storage.prototype.setItem = window.__realSetItem)`);
|
||||
await third.close();
|
||||
}
|
||||
} finally {
|
||||
const current = await savedState();
|
||||
await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...current, settings: { ...current.settings, wsUrl: before.settings.wsUrl, autoReconnect: false } }) });
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { writeLocalCache } from "../src/local_cache.js";
|
||||
|
||||
function fakeStorage({ quota = Infinity } = {}) {
|
||||
const items = new Map();
|
||||
return {
|
||||
items,
|
||||
setItem(key, value) {
|
||||
if (String(value).length > quota) throw Object.assign(new Error("Setting the value exceeded the quota."), { name: "QuotaExceededError" });
|
||||
items.set(key, String(value));
|
||||
},
|
||||
removeItem(key) { items.delete(key); },
|
||||
};
|
||||
}
|
||||
|
||||
test("writes the state when it fits", () => {
|
||||
const storage = fakeStorage();
|
||||
assert.deepEqual(writeLocalCache(storage, "k", { a: 1 }), { ok: true });
|
||||
assert.equal(storage.items.get("k"), '{"a":1}');
|
||||
});
|
||||
|
||||
test("a full storage never throws and drops the stale cache instead of keeping an old copy", () => {
|
||||
const storage = fakeStorage({ quota: 20 });
|
||||
writeLocalCache(storage, "k", { a: 1 });
|
||||
const result = writeLocalCache(storage, "k", { results: "x".repeat(100) });
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.name, "QuotaExceededError");
|
||||
assert.equal(storage.items.has("k"), false, "the outdated copy is removed so a reload hydrates from the server");
|
||||
});
|
||||
|
||||
test("a storage that throws on remove too is still survived", () => {
|
||||
const storage = { setItem() { throw new Error("blocked"); }, removeItem() { throw new Error("blocked"); } };
|
||||
assert.equal(writeLocalCache(storage, "k", {}).ok, false);
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import "./auth.test.js";
|
||||
import "./local_cache.test.js";
|
||||
import "./prologue_logic.test.js";
|
||||
import "./suspect_laps_logic.test.js";
|
||||
import "./series_logic.test.js";
|
||||
|
||||
Reference in new issue
Block a user