Server: count only newly stored events toward the group quota

Re-uploading existing events (retries, backup restore) is idempotent and
must not eat into MAX_GROUP_BYTES.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
This commit is contained in:
agent 2026-10-09 19:59:16 +00:00
parent 8dd25b93b0
commit f67add3660

View file

@ -174,17 +174,18 @@ export function createApp(cfg: Config, store: Store) {
const body = await readJson(req, 200 * (cfg.maxEventBytes * 1.4 + 512)); const body = await readJson(req, 200 * (cfg.maxEventBytes * 1.4 + 512));
const events = body?.events; const events = body?.events;
if (!Array.isArray(events) || events.length === 0 || events.length > 200) throw new HttpError(400, 'events must be 1..200'); if (!Array.isArray(events) || events.length === 0 || events.length > 200) throw new HttpError(400, 'events must be 1..200');
let bytes = 0; let incoming = 0;
for (const e of events) { for (const e of events) {
if (!EVENT_ID_RE.test(e?.id) || !DEVICE_RE.test(e?.deviceId) || !B64U_RE.test(e?.iv) || !B64U_RE.test(e?.ct) || e.iv.length !== 16) if (!EVENT_ID_RE.test(e?.id) || !DEVICE_RE.test(e?.deviceId) || !B64U_RE.test(e?.iv) || !B64U_RE.test(e?.ct) || e.iv.length !== 16)
throw new HttpError(400, 'bad event'); throw new HttpError(400, 'bad event');
const size = Math.floor((e.ct.length * 3) / 4); const size = Math.floor((e.ct.length * 3) / 4);
if (size > cfg.maxEventBytes) throw new HttpError(413, 'event too large'); if (size > cfg.maxEventBytes) throw new HttpError(413, 'event too large');
bytes += size; incoming += size;
} }
if (g.bytes + bytes > cfg.maxGroupBytes) throw new HttpError(507, 'group storage full'); if (g.bytes + incoming > cfg.maxGroupBytes) throw new HttpError(507, 'group storage full');
const seqs = store.tx(() => { const seqs = store.tx(() => {
let next = store.latestSeq(groupId); let next = store.latestSeq(groupId);
let bytes = 0;
const out: number[] = []; const out: number[] = [];
for (const e of events) { for (const e of events) {
const existing = store.seqOf(groupId, e.id); const existing = store.seqOf(groupId, e.id);
@ -194,6 +195,7 @@ export function createApp(cfg: Config, store: Store) {
} }
next += 1; next += 1;
store.insertEvent(groupId, next, { id: e.id, deviceId: e.deviceId, iv: e.iv, ct: e.ct }); store.insertEvent(groupId, next, { id: e.id, deviceId: e.deviceId, iv: e.iv, ct: e.ct });
bytes += Math.floor((e.ct.length * 3) / 4);
out.push(next); out.push(next);
} }
store.addBytes(groupId, bytes); store.addBytes(groupId, bytes);