SseWriter
SUPPORT · interface · Type contract
Destination that reports backpressure instead of buffering without bound.
Import
ts
import type { SseWriter } from '@jangts/agent-core/sse';js
/** @typedef {import('@jangts/agent-core/sse').SseWriter} SseWriter */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/sse, @jangts/agent-core/support.
Definition
ts
interface SseWriterField structure
| Field | Type | Required | Description |
|---|---|---|---|
write | (frame: string) => boolean | Required | Writes one encoded frame; returns false when the consumer needs a drain wait. |
waitForDrain | (signal?: AbortSignal) => Promise<unknown> | Required | Resolves once the destination can accept more data, or rejects when signal aborts. |
Member index
| Member | Purpose |
|---|---|
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. |
Member details
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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
frame | string | Required | — |
Returns: 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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
signal | AbortSignal | undefined | Optional | — |
Returns: Promise<unknown>.
Awaited value: unknown.
Examples
- SSE, JSON frames, and bounded event streams: Reuse transport mechanics while preserving cancellation and backpressure; protocol parsing and business validation remain separate.
Parse one SSE frame locally
Beginner · 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);Producer waits for each event delivery
Configuration · 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);