MCPcopy
hub / github.com/simstudioai/sim / readSSELines

Function readSSELines

apps/sim/lib/core/utils/sse.ts:130–164  ·  view source on GitHub ↗
(source: SSESource, options: ReadSSELinesOptions)

Source from the content-addressed store, hash-verified

128 * @param options - The `onData` callback plus an optional `signal`.
129 */
130export async function readSSELines(source: SSESource, options: ReadSSELinesOptions): Promise<void> {
131 const { onData, signal } = options
132 const { reader, ownsLock } = toReader(source)
133 const decoder = new TextDecoder()
134 let buffer = ''
135
136 try {
137 while (true) {
138 if (signal?.aborted) break
139
140 const { done, value } = await reader.read()
141
142 buffer += done ? decoder.decode() : decoder.decode(value, { stream: true })
143 const lines = buffer.split('\n')
144 buffer = done ? '' : (lines.pop() ?? '')
145
146 for (const rawLine of lines) {
147 if (signal?.aborted) return
148
149 const line = stripCarriageReturn(rawLine)
150 if (!line.startsWith('data:')) continue
151
152 let data = line.slice(5)
153 if (data.startsWith(' ')) data = data.slice(1)
154 if (data === DONE_SENTINEL) continue
155
156 if ((await onData(data)) === true) return
157 }
158
159 if (done) break
160 }
161 } finally {
162 if (ownsLock) reader.releaseLock()
163 }
164}
165
166/**
167 * The JSON convenience layer over {@link readSSELines}: invokes `onEvent` once

Callers 3

sse.test.tsFile · 0.90
useChatFunction · 0.90
readSSEEventsFunction · 0.85

Calls 4

toReaderFunction · 0.85
stripCarriageReturnFunction · 0.85
onDataFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected