SseWriter
SUPPORT · interface · 类型合同
Destination that reports backpressure instead of buffering without bound.
导入
ts
import type { SseWriter } from '@jangts/agent-core/sse';js
/** @typedef {import('@jangts/agent-core/sse').SseWriter} SseWriter */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/sse、@jangts/agent-core/support。
定义
ts
interface SseWriter字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
write | (frame: string) => boolean | 必需 | Writes one encoded frame; returns false when the consumer needs a drain wait. |
waitForDrain | (signal?: AbortSignal) => Promise<unknown> | 必需 | Resolves once the destination can accept more data, or rejects when signal aborts. |
成员目录
| 成员 | 用途 |
|---|---|
write | Writes one encoded frame; returns false when the consumer needs a drain wait. |
waitForDrain | Resolves once the destination can accept more data, or rejects when signal aborts. |
成员详情
write
Writes one encoded frame; returns false when the consumer needs a drain wait.
ts
SseWriter.write(frame: string): booleanWrites one encoded frame; returns false when the consumer needs a drain wait.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
frame | string | 必需 | — |
返回: boolean。
waitForDrain
Resolves once the destination can accept more data, or rejects when signal aborts.
ts
SseWriter.waitForDrain(signal?: AbortSignal): Promise<unknown>Resolves once the destination can accept more data, or rejects when signal aborts.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
signal | AbortSignal | undefined | 可省略 | — |
返回: Promise<unknown>。
等待后的值: unknown。
使用示例
- SSE、JSON 帧与有界事件流:复用传输机制,保持取消和背压;协议解析与业务校验仍各自负责。
在本地解析一帧 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);