elysiajs / elysia

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

Async generator yielding an object results in `[object Object]` output #741

Closed remorses closed 2 months ago

remorses commented 3 months ago

What version of Elysia.JS is running?

"elysia": "^1.1.3"

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

import { treaty } from '@elysiajs/eden'
import { sleep } from 'bun'
import { Elysia } from 'elysia'

const app = new Elysia()
    .get('/', async function* generator() {
        await sleep(100) // needed because of another bug
        yield { body: 'Hello Elysia' } // this will be [object Object]
    })
    .listen(3000, async () => {
        const app = treaty<App>('localhost:3000')

        {
            const { data: gen, error } = await app.index.get({})
            if (error) throw error

            for await (const res of gen) {
                console.log(res)
            }
        }
    })

export type App = typeof app

console.log(
    `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
)

What is the expected behavior?

Elysia should call JSON.stringify on every output of an async generator

What do you see instead?

Elysia encodes objects to [object Object]

Additional information

No response

SaltyAom commented 2 months ago

Should have been fixed with https://github.com/elysiajs/elysia/commit/0a4977acd6459c3357ad1dbe5d9231f7c7cd6278, published under 1.1.4

remorses commented 2 months ago

Can you check https://github.com/elysiajs/elysia/pull/743? The server should return valid text/event-stream content instead of concatenated JSON strings

remorses commented 2 months ago

For generators to make sense you need to have a separator between items, otherwise you can't distinguish each item. text/event-stream already has a format for this

remorses commented 2 months ago

Closing in favor of #742