autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
44 lines (43 loc) • 1.31 kB
TypeScript
/**
* SSE (Server-Sent Events) 会话工具模块
*
* 提供统一的 SSE 连接管理:headers 设置、心跳保活、安全写入、生命周期事件。
* 所有 SSE 端点共用此模块,确保协议一致性。
*
* @module lib/http/utils/sse
*/
import type { Request, Response } from 'express';
/**
* 创建 SSE 会话 — 统一设置 headers、心跳、安全写入
*
* @param scene 场景标识
* @returns }
*/
export declare function createSSESession(req: Request, res: Response, scene: string): {
/**
* 发送一个 SSE 事件
* @param event 必须包含 type 字段
*/
send(event: Record<string, unknown>): void;
/**
* 正常结束流 — 发送 stream:done 并关闭连接
* @param [donePayload={}] done 事件携带的额外数据
*/
end(donePayload?: Record<string, unknown>): void;
/** 发送错误并结束流 */
error(message: string, code: string): void;
/** 是否已断开 */
readonly isDisconnected: boolean;
/** 会话 ID */
sessionId: string;
/** 获取性能指标 */
readonly metrics: {
endTime: number;
duration: number;
ttft: number | null;
startTime: number;
eventCount: number;
totalBytes: number;
firstTextDeltaTime: number;
};
};