Skip to content

Commit 237f25e

Browse files
author
Meeseeks
committed
Refine local storage bounds
Co-authored-by: Meeseeks <2e96988f190ed1bd3c568760103aa4cadb2bc6195b832e252c984392c89039bd@buzz.block.builderlab.xyz> Signed-off-by: Meeseeks <2e96988f190ed1bd3c568760103aa4cadb2bc6195b832e252c984392c89039bd@buzz.block.builderlab.xyz>
1 parent 88a55ae commit 237f25e

6 files changed

Lines changed: 90 additions & 14 deletions

File tree

‎desktop/src/features/communities/communityIconCache.test.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import test from "node:test";
33

44
import {
55
boundCommunityIconCache,
6+
loadCachedCommunityIcon,
67
MAX_CACHED_COMMUNITY_ICON_LENGTH,
78
MAX_CACHED_COMMUNITY_ICONS,
9+
saveCachedCommunityIcon,
810
} from "./communityIconCache.ts";
911

1012
test("community icon cache caps entries and rejects oversized icons", () => {
@@ -23,3 +25,18 @@ test("community icon cache caps entries and rejects oversized icons", () => {
2325
assert.equal(bounded.oversized, undefined);
2426
assert.equal(bounded[`relay-${MAX_CACHED_COMMUNITY_ICONS}`], "icon-32");
2527
});
28+
29+
test("community icon cache accepts relay-sized icons above 64 KiB", () => {
30+
const values = new Map([
31+
["buzz-community-icons", JSON.stringify({ relay: "prior-icon" })],
32+
]);
33+
globalThis.localStorage = {
34+
getItem: (key) => values.get(key) ?? null,
35+
setItem: (key, value) => values.set(key, String(value)),
36+
};
37+
const acceptedIcon = "x".repeat(80 * 1024);
38+
39+
saveCachedCommunityIcon("relay", acceptedIcon);
40+
41+
assert.equal(loadCachedCommunityIcon("relay"), acceptedIcon);
42+
});

‎desktop/src/features/communities/communityIconCache.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
const ICON_CACHE_KEY = "buzz-community-icons";
88
export const MAX_CACHED_COMMUNITY_ICONS = 32;
9-
export const MAX_CACHED_COMMUNITY_ICON_LENGTH = 64 * 1024;
9+
// Keep aligned with MAX_WORKSPACE_ICON_DATA_URL_LEN in
10+
// crates/buzz-relay/src/handlers/relay_admin.rs.
11+
export const MAX_CACHED_COMMUNITY_ICON_LENGTH = 98_304;
1012

1113
export function boundCommunityIconCache(
1214
cache: Record<string, string>,

‎desktop/src/features/profile/lib/selfProfileStorage.test.mjs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ test("storageKey: different pubkeys produce different keys", () => {
5151
assert.notEqual(a, b);
5252
});
5353

54-
function installStorage() {
54+
function installStorage(onGetItem = () => {}) {
5555
const values = new Map();
5656
globalThis.window = {
5757
dispatchEvent: () => true,
5858
localStorage: {
5959
get length() {
6060
return values.size;
6161
},
62-
getItem: (key) => values.get(key) ?? null,
62+
getItem: (key) => {
63+
onGetItem(key);
64+
return values.get(key) ?? null;
65+
},
6366
key: (index) => [...values.keys()][index] ?? null,
6467
removeItem: (key) => values.delete(key),
6568
setItem: (key, value) => values.set(key, String(value)),
@@ -99,6 +102,24 @@ test("writeSelfProfileCache caps each relay and the global cache by updatedAt",
99102
assert.equal(values.has(storageKey(relayA, "pubkey-1")), false);
100103
});
101104

105+
test("writeSelfProfileCache does not read existing payloads below both caps", () => {
106+
const readKeys = [];
107+
const values = installStorage((key) => readKeys.push(key));
108+
const relay = "wss://relay.example";
109+
const existingKey = storageKey(relay, "existing");
110+
const writtenKey = storageKey(relay, "written");
111+
values.set(existingKey, JSON.stringify(makeCache({ updatedAt: 1 })));
112+
113+
assert.equal(
114+
writeSelfProfileCache(relay, "written", makeCache({ updatedAt: 2 })),
115+
true,
116+
);
117+
118+
assert.deepEqual(readKeys, [writtenKey]);
119+
assert.equal(values.has(existingKey), true);
120+
assert.equal(values.has(writtenKey), true);
121+
});
122+
102123
test("parseSelfProfileCache: valid v1 payload round-trips", () => {
103124
const payload = {
104125
version: 1,

‎desktop/src/features/profile/lib/selfProfileStorage.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ export function readSelfProfileCache(
131131

132132
function trimSelfProfileCaches(relayUrl: string, preservedKey: string): void {
133133
const relayPrefix = `${STORAGE_KEY_PREFIX}:${normalizeRelayUrl(relayUrl)}:`;
134+
let totalEntryCount = 0;
135+
let relayEntryCount = 0;
136+
for (let i = 0; i < window.localStorage.length; i++) {
137+
const key = window.localStorage.key(i);
138+
if (!key?.startsWith(`${STORAGE_KEY_PREFIX}:`)) continue;
139+
totalEntryCount += 1;
140+
if (key.startsWith(relayPrefix)) relayEntryCount += 1;
141+
}
142+
if (
143+
totalEntryCount <= MAX_SELF_PROFILE_CACHES &&
144+
relayEntryCount <= MAX_SELF_PROFILE_CACHES_PER_RELAY
145+
) {
146+
return;
147+
}
148+
134149
const entries: Array<{ key: string; updatedAt: number }> = [];
135150
for (let i = 0; i < window.localStorage.length; i++) {
136151
const key = window.localStorage.key(i);

‎desktop/src/shared/features/store.test.mjs‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { getOverrides, OVERRIDES_KEY } from "./store.ts";
4+
import { getOverrides, OVERRIDES_KEY, setOverride } from "./store.ts";
55

66
function installStorage(value) {
77
const values = new Map([[OVERRIDES_KEY, JSON.stringify(value)]]);
8+
const writes = [];
89
globalThis.window = {
910
localStorage: {
1011
getItem: (key) => values.get(key) ?? null,
11-
setItem: (key, next) => values.set(key, String(next)),
12+
setItem: (key, next) => {
13+
writes.push([key, String(next)]);
14+
values.set(key, String(next));
15+
},
1216
},
1317
};
14-
return values;
18+
return { values, writes };
1519
}
1620

17-
test("getOverrides drops unknown feature IDs and compacts persisted storage", () => {
18-
const values = installStorage({ workflows: true, removedFeature: false });
21+
test("getOverrides drops unknown feature IDs without writing to storage", () => {
22+
const { values, writes } = installStorage({
23+
workflows: true,
24+
removedFeature: false,
25+
});
1926

2027
assert.deepEqual(getOverrides(), { workflows: true });
21-
assert.equal(values.get(OVERRIDES_KEY), JSON.stringify({ workflows: true }));
28+
assert.deepEqual(writes, []);
29+
assert.equal(
30+
values.get(OVERRIDES_KEY),
31+
JSON.stringify({ workflows: true, removedFeature: false }),
32+
);
33+
});
34+
35+
test("setOverride persists filtered overrides", () => {
36+
const { values } = installStorage({
37+
workflows: true,
38+
removedFeature: false,
39+
});
40+
41+
setOverride("projects", true);
42+
43+
assert.equal(
44+
values.get(OVERRIDES_KEY),
45+
JSON.stringify({ workflows: true, projects: true }),
46+
);
2247
});
2348

2449
test("getOverrides drops non-boolean values", () => {

‎desktop/src/shared/features/store.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ export function getOverrides(): FeatureOverrides {
2222
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
2323
return {};
2424
const featureIds = new Set(manifest.features.map((feature) => feature.id));
25-
const overrides = Object.fromEntries(
25+
return Object.fromEntries(
2626
Object.entries(parsed).filter(
2727
(entry): entry is [string, boolean] =>
2828
featureIds.has(entry[0]) && typeof entry[1] === "boolean",
2929
),
3030
);
31-
const serialized = JSON.stringify(overrides);
32-
if (serialized !== raw)
33-
window.localStorage.setItem(OVERRIDES_KEY, serialized);
34-
return overrides;
3531
} catch {
3632
return {};
3733
}

0 commit comments

Comments
 (0)