Skip to content
中文

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 SseWriter

Field structure ​

FieldTypeRequiredDescription
write(frame: string) => booleanRequiredWrites one encoded frame; returns false when the consumer needs a drain wait.
waitForDrain(signal?: AbortSignal) => Promise<unknown>RequiredResolves once the destination can accept more data, or rejects when signal aborts.

Member index ​

MemberPurpose
writeWrites one encoded frame; returns false when the consumer needs a drain wait.
waitForDrainResolves 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): boolean

Writes one encoded frame; returns false when the consumer needs a drain wait.

ParameterTypePassingDescription
framestringRequired—

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.

ParameterTypePassingDescription
signalAbortSignal | undefinedOptional—

Returns: Promise<unknown>.

Awaited value: unknown.

Examples ​

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);

API reference, usage examples and developer guides.