elysiajs / elysia

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

Setting headers inside generator functions doesn't work #729

Closed bogeychan closed 3 months ago

bogeychan commented 3 months ago

What version of Elysia.JS is running?

1.1.2

What platform is your computer?

WSL Ubuntu

What steps can reproduce the bug?

// test.test.ts

import { describe, it, expect } from 'bun:test'
import { req } from '../utils'

import { Elysia } from '../../src'

describe('Stream', () => {
    it('handle stream with headers', async () => {
        const app = new Elysia().get('/', async function* (ctx) {
            await Bun.sleep(1_000)
            ctx.set.headers['another'] = 'header'
            yield 'a'
        })

        const response = await app.handle(req('/'))

        expect(response.headers.get('another')).toBe('header')
    })
})

What is the expected behavior?

test pass

What do you see instead?

test fail

Additional information

related to https://github.com/elysiajs/elysia/issues/727


to fix this you have to await the first generator result but the handleStream function is not async...

https://github.com/elysiajs/elysia/blob/3b3fbcd5abae6778965cb1b10a3a7a8d804cacf2/src/handler.ts#L121

const { value } = await generator.next()

// set headers here

return new Response(..., set)
SaltyAom commented 3 months ago

Should have been fixed with https://github.com/elysiajs/elysia/commit/20431a290e1a1339a380253e3c10a068f4a131b1, published under 1.1.3

bogeychan commented 3 months ago

Thanks, works for me