elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.74k stars 207 forks source link

Requesting Clarity for new Streaming Feature #732

Open pg-wtatum opened 1 month ago

pg-wtatum commented 1 month ago

A few issues related to the new async generator feature for streaming responses, one of which applied to the legacy method as well.

pg-wtatum commented 1 month ago

To elaborate further on the object -> JSON question, if I implement my server side as a typed generator that produces something other than string the inferred client type in treaty is AsyncGenerator but in fact I just get the string [object Object] instead

pg-wtatum commented 1 month ago

Sorry for the drip feed of info but the behavior is actually weirder than I first thought -- if I manually call JSON.stringify on the server, the values produced by

  const res = await client.funky.get();
  if (!res.error) {
    for await (const d of res.data) {
      console.log(d);
    }
  }

are objects rather than strings. This causes some significant typescript issues as I need to type the generator as string (but it actually returns objects)

edit to add I see that 1.1.3 was released today to address other issues related to the stream feature. I have updated and this appears to still be the case.

directormac commented 1 month ago

Currently implementing it using the Generator function as described in the docs, using for await in both ends when data is string or a certain type, it's being inferred correctly but can't parse them well as sometimes it yields 2 or more at once. i think this needs to be moved at eden repo

pg-wtatum commented 1 month ago

Not sure it's strictly on the Eden side -- I've definitely seen cases where only one object is emitted and it still serializes onto the wire as [object Object], but if manually stringified with JSON.stringify it goes onto the wire as JSON (of course) and is parsed by Eden.

Given the content type is text/event-stream I would expect that the wire format is newline delimited and simultaneous emissions should not be an issue?