Skip to content

Commit 68a649e

Browse files
refactor: extract _writePrimingEvent helper method
1 parent e0656d1 commit 68a649e

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/server/streamableHttp.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ export class StreamableHTTPServerTransport implements Transport {
258258
}
259259
}
260260

261+
/**
262+
* Writes a priming event to establish resumption capability.
263+
* This primes the client's Last-Event-ID for reconnection.
264+
*/
265+
private async _writePrimingEvent(res: ServerResponse, streamId: string): Promise<void> {
266+
const primingEventId = this._eventStore
267+
? await this._eventStore.storeEvent(streamId, {} as JSONRPCMessage)
268+
: `priming-${Date.now()}`;
269+
270+
let primingEvent = `id: ${primingEventId}\ndata: \n\n`;
271+
if (this._retryInterval !== undefined) {
272+
primingEvent = `id: ${primingEventId}\nretry: ${this._retryInterval}\ndata: \n\n`;
273+
}
274+
res.write(primingEvent);
275+
}
276+
261277
/**
262278
* Handles GET requests for SSE stream
263279
*/
@@ -329,18 +345,7 @@ export class StreamableHTTPServerTransport implements Transport {
329345
// otherwise the client will just wait for the first message
330346
res.writeHead(200, headers).flushHeaders();
331347

332-
// Send priming event to establish resumption capability
333-
// This primes the client's Last-Event-ID for reconnection
334-
const primingEventId = this._eventStore
335-
? await this._eventStore.storeEvent(this._standaloneSseStreamId, {} as JSONRPCMessage)
336-
: `priming-${Date.now()}`;
337-
338-
// Build priming event - always include id for resumability, optionally add retry for polling
339-
let primingEvent = `id: ${primingEventId}\ndata: \n\n`;
340-
if (this._retryInterval !== undefined) {
341-
primingEvent = `id: ${primingEventId}\nretry: ${this._retryInterval}\ndata: \n\n`;
342-
}
343-
res.write(primingEvent);
348+
await this._writePrimingEvent(res, this._standaloneSseStreamId);
344349

345350
// Assign the response to the standalone SSE stream
346351
this._streamMapping.set(this._standaloneSseStreamId, res);
@@ -570,18 +575,7 @@ export class StreamableHTTPServerTransport implements Transport {
570575

571576
res.writeHead(200, headers);
572577

573-
// Send priming event to establish resumption capability
574-
// This primes the client's Last-Event-ID for reconnection
575-
const primingEventId = this._eventStore
576-
? await this._eventStore.storeEvent(streamId, {} as JSONRPCMessage)
577-
: `priming-${Date.now()}`;
578-
579-
// Build priming event - always include id for resumability, optionally add retry for polling
580-
let primingEvent = `id: ${primingEventId}\ndata: \n\n`;
581-
if (this._retryInterval !== undefined) {
582-
primingEvent = `id: ${primingEventId}\nretry: ${this._retryInterval}\ndata: \n\n`;
583-
}
584-
res.write(primingEvent);
578+
await this._writePrimingEvent(res, streamId);
585579
}
586580
// Store the response for this request to send messages back through this connection
587581
// We need to track by request ID to maintain the connection

0 commit comments

Comments
 (0)