fix: close delete resume and SSE replay races

This commit is contained in:
User
2026-07-15 02:08:11 +02:00
parent 4d36f4acf1
commit 0bcec1591b
5 changed files with 208 additions and 5 deletions
+7 -1
View File
@@ -52,9 +52,15 @@ export class SseHub {
const subscriber = { send, close: options.close ?? (() => undefined), closed: false };
this.subs.get(sessionId)!.add(subscriber);
const afterId = Number.isSafeInteger(options.afterId) && (options.afterId ?? 0) >= 0
const requestedAfterId = Number.isSafeInteger(options.afterId) && (options.afterId ?? 0) >= 0
? options.afterId ?? 0
: 0;
// Native EventSource persists Last-Event-ID across a backend process restart. A cursor newer
// than anything this hub generation has produced cannot identify an event in this process,
// so replay the fresh generation from its beginning instead of suppressing every low id.
const afterId = requestedAfterId > (this.lastIds.get(sessionId) ?? 0)
? 0
: requestedAfterId;
const buf = this.buffers.get(sessionId) ?? [];
for (const item of buf) {
if (item.id > afterId) send(item.event, item.data, item.id);