MCPcopy Create free account
hub / github.com/Effect-TS/effect / sseEvents

Function sseEvents

packages/effect/src/graphql/GraphQLProtocol.ts:296–344  ·  view source on GitHub ↗
(
  response: HttpClientResponse.HttpClientResponse
)

Source from the content-addressed store, hash-verified

294// retryable `TransportError`. A `retry:` line is kept and reported as
295// `retryAfter` if the body fails or ends early.
296const sseEvents = (
297 response: HttpClientResponse.HttpClientResponse
298): Stream.Stream<{ readonly body: unknown; readonly headers: Headers.Headers }, TransportError> =>
299 Stream.suspend(() => {
300 const headers = response.headers
301 let retryAfter: Duration.Duration | undefined
302 let complete = false
303 let events: Array<Sse.Event> = []
304 const parser = Sse.makeParser((event) => {
305 if (event._tag === "Retry") {
306 retryAfter = event.duration
307 } else {
308 events.push(event)
309 }
310 }, { dispatchEmptyNamedEvents: true })
311 return response.stream.pipe(
312 Stream.decodeText(),
313 Stream.mapError((cause) =>
314 new TransportError({ description: `The graphql-sse stream failed: ${cause.message}`, retryAfter, cause })
315 ),
316 Stream.mapEffect((text) => {
317 const error = parser.feed(text)
318 if (error !== undefined) {
319 return Effect.fail(new TransportError({ description: error.message, cause: error }))
320 }
321 const batch = events
322 events = []
323 return Effect.succeed(batch)
324 }),
325 Stream.flattenIterable,
326 Stream.takeWhile((event) => {
327 if (event.event === "complete") complete = true
328 return !complete
329 }),
330 Stream.filter((event) => event.event === "next"),
331 Stream.mapEffect((event) =>
332 Effect.try({
333 try: () => ({ body: JSON.parse(event.data) as unknown, headers }),
334 catch: (cause) =>
335 new TransportError({ description: `Invalid graphql-sse next event: ${errorMessage(cause)}`, cause })
336 })
337 ),
338 Stream.concat(Stream.suspend(() =>
339 complete ? Stream.empty : Stream.fail(
340 new TransportError({ description: "The graphql-sse stream ended without a complete event", retryAfter })
341 )
342 ))
343 )
344 })
345
346interface HttpOptions<R = never> {
347 readonly url: Url

Callers 1

subscribeFunction · 0.85

Calls 9

errorMessageFunction · 0.85
decodeTextMethod · 0.80
pushMethod · 0.65
pipeMethod · 0.65
feedMethod · 0.65
failMethod · 0.65
succeedMethod · 0.45
filterMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected