elysiajs / elysia

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

cors() doesn't work with new generator function #727

Closed fahreziadh closed 3 months ago

fahreziadh commented 3 months ago

What version of Elysia.JS is running?

1.1

What platform is your computer?

Im on Fedora 40

What steps can reproduce the bug?

image image

What is the expected behavior?

I expect the headers Allow-origin will showed

What do you see instead?

I can't see any headers even though I set them manually

Additional information

No response

bogeychan commented 3 months ago

This works for me, can you try elysia version 1.1.2 please?

and make sure you have @elysiajs/cors@1.1.0 installed

fahreziadh commented 3 months ago

This works for me, can you try elysia version 1.1.2 please?

and make sure you have @elysiajs/cors@1.1.0 installed

yes, i already update all of them to the latest version image

bogeychan commented 3 months ago

Run bun pm ls in your project root or check the versions inside node_modules folder manually. sometimes bun uses cached versions.

do you have any hook in your code that could manipulate the response?


this works for me. can you give it a try?

// index.ts - http://localhost:8080

import { Elysia } from "elysia";
import { cors } from "@elysiajs/cors";

new Elysia()
  .use(
    cors({
      origin: ["*"],
    })
  )
  .get("", () => {
    return async function* () {
      for (let index = 0; index < 100; index++) {
        await new Promise((res) => setTimeout(res, 20));
        yield `${index}, `;
      }
    };
  })
  .listen(8080);
<!-- index.html http://localhost:5500 -->

<!DOCTYPE html>
<html>

<body>
    <script type="module">
        const response = await fetch("http://localhost:8080")

        console.log(response.status)
        console.log(await response.text())
    </script>

</body>

</html>
fahreziadh commented 3 months ago

bun pm ls

I just tried the code you provided, it works well (I've seen there are headers there), but the response is not streaming but plain text.

but with my previous example (I followed what was in the docs), the response is streaming but the headers/cors are not there.

I called it in the browser directly

bogeychan commented 3 months ago

oh, sorry, mb.

ye, it doesnt set response headers at all... when using generator function as handler

import { Elysia } from "elysia";

new Elysia()
  .get("", async function* (ctx) {
    ctx.set.headers["my"] = "header"; // not in response
    for (let index = 0; index < 100; index++) {
      await new Promise((res) => setTimeout(res, 20));
      yield `${index}, `;
    }
  })
  .listen(8080);
fahreziadh commented 3 months ago
  ctx.set.headers["my"] = "header";

so, its not possible to set any headers to response when using generator function as handler, right?

bogeychan commented 3 months ago

Yes, I'll open another issue for it as it differs

fahreziadh commented 3 months ago

Yes, I'll open another issue for it as it differs

ah, thanks @bogeychan

SaltyAom commented 3 months ago

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

fahreziadh commented 3 months ago

Should have been fixed with 20431a2, published under 1.1.3

Its working now!, thanks guys <3