diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index d58137d..428be2c 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -174,17 +174,18 @@ export function createApp(cfg: Config, store: Store) { const body = await readJson(req, 200 * (cfg.maxEventBytes * 1.4 + 512)); const events = body?.events; 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) { 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'); const size = Math.floor((e.ct.length * 3) / 4); 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(() => { let next = store.latestSeq(groupId); + let bytes = 0; const out: number[] = []; for (const e of events) { const existing = store.seqOf(groupId, e.id); @@ -194,6 +195,7 @@ export function createApp(cfg: Config, store: Store) { } next += 1; 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); } store.addBytes(groupId, bytes);