Skip to content
English

readSse ​

CORE · function · 运行时值

Decode an SSE response body into events.

Framing contract:

  • A network chunk is not an event. Bytes are buffered until a blank line, because UTF-8 sequences, CRLF pairs and data: lines may all straddle chunk boundaries.
  • \r\n, \r and \n all terminate a line; a trailing \r at the end of a chunk is held back until the next chunk shows whether an \n follows.
  • Comment lines (leading :) are discarded; they are used as heartbeats.
  • The size limit applies per line and per event (1 MiB), not per network read, so many complete events coalesced into one chunk after backpressure remain valid.
  • Cancellation is checked between lines, so aborting while the consumer is paused stops emitting the remaining buffered events of the same chunk.

导入 ​

ts
import { readSse } from '@jangts/agent-core/sse';
js
import { readSse } from '@jangts/agent-core/sse';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/sse、@jangts/agent-core/support。

定义 ​

调用 ​

ts
readSse(body: ReadableStream<Uint8Array>, options?: SseReadOptions): AsyncGenerator<SseEvent>

Decode an SSE response body into events.

Framing contract:

  • A network chunk is not an event. Bytes are buffered until a blank line, because UTF-8 sequences, CRLF pairs and data: lines may all straddle chunk boundaries.
  • \r\n, \r and \n all terminate a line; a trailing \r at the end of a chunk is held back until the next chunk shows whether an \n follows.
  • Comment lines (leading :) are discarded; they are used as heartbeats.
  • The size limit applies per line and per event (1 MiB), not per network read, so many complete events coalesced into one chunk after backpressure remain valid.
  • Cancellation is checked between lines, so aborting while the consumer is paused stops emitting the remaining buffered events of the same chunk.
参数类型传递说明
bodyReadableStream<Uint8Array<ArrayBufferLike>>必需—
optionsSseReadOptions可省略—
options 的字段
字段类型必需说明
signalAbortSignal | undefined可省略Cancels the pending read and any later read from the same stream.
idleTimeoutMsnumber | undefined可省略Maximum silence between chunks in milliseconds; omitted or &lt;= 0 disables the timer.
waitForCancelboolean | undefined可省略Await reader.cancel() before returning. false lets browser adapters skip the wait.

返回: AsyncGenerator<SseEvent, any, any>。

使用示例 ​

在本地解析一帧 SSE ​

入门 · Node.js

ts
import { Support } from '@jangts/agent-core';
const frame = Support.encodeSseEvent({ event: 'status', data: JSON.stringify({ phase: 'thinking' }) });
const body = new Response(frame).body!;
for await (const event of Support.readSse(body)) console.log(event.event, event.data);
js
import { Support } from '@jangts/agent-core';
const frame = Support.encodeSseEvent({ event: 'status', data: JSON.stringify({ phase: 'thinking' }) });
const body = new Response(frame).body;
for await (const event of Support.readSse(body))
    console.log(event.event, event.data);

生产者等待每一次事件交付 ​

配置 · Node.js

ts
import { Support } from '@jangts/agent-core';
const signal = new AbortController().signal;
const stream = Support.streamEvents<{ phase: string }>(async emit => {
  await emit({ phase: 'preparing' });
  await emit({ phase: 'complete' });
}, { signal });
for await (const event of stream) console.log(event.phase);
js
import { Support } from '@jangts/agent-core';
const signal = new AbortController().signal;
const stream = Support.streamEvents(async (emit) => {
    await emit({ phase: 'preparing' });
    await emit({ phase: 'complete' });
}, { signal });
for await (const event of stream)
    console.log(event.phase);

显式限制 JSON 帧数量和外围内容 ​

扩展 · Node.js

ts
import { Support } from '@jangts/agent-core';
const text = '{"kind":"reply","answer":"服务正常"}';
console.log(Support.scanJsonObjectFrames(text));
console.log(Support.parseJsonFrames(text, { maxFrames: 1, maxExtraClosings: 0 }));
const sent: string[] = [];
await Support.writeSseFrame({
  writer: { write(frame) { sent.push(frame); return true; }, async waitForDrain() {} },
  frame: Support.encodeSseEvent({ event: 'answer', data: text }),
}, { signal: new AbortController().signal });
console.log(sent);
js
import { Support } from '@jangts/agent-core';
const text = '{"kind":"reply","answer":"服务正常"}';
console.log(Support.scanJsonObjectFrames(text));
console.log(Support.parseJsonFrames(text, { maxFrames: 1, maxExtraClosings: 0 }));
const sent = [];
await Support.writeSseFrame({
    writer: { write(frame) { sent.push(frame); return true; }, async waitForDrain() { } },
    frame: Support.encodeSseEvent({ event: 'answer', data: text }),
}, { signal: new AbortController().signal });
console.log(sent);

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。