elysiajs / elysia

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

The generator's response is actually inconsistent #755

Open HK-SHAO opened 1 month ago

HK-SHAO commented 1 month ago

What version of Elysia.JS is running?

1.1.4

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

server.ts:

import Elysia from "elysia";

export const app = new Elysia().post("/stream", async function* () {
  yield {hello: "world"};
  yield {hello: "world"};
  yield {hello: "world"};
});

app.listen(2345);

client.ts:

import { treaty } from "@elysiajs/eden";
import type { app } from ".";

const client = treaty<typeof app>("http://localhost:2345");

const { data, error } = await client.stream.post();
if (!data) throw error;

for await (let item of data) {
  console.log(item, typeof item);
}

What is the expected behavior?

% bun run client.ts
{
  hello: "world",
} object
{
  hello: "world",
} object
{
  hello: "world",
} object

What do you see instead?

% bun run client.ts
{"hello":"world"}{"hello":"world"} string
{
  hello: "world",
} object

Additional information

Sometimes numeric responses are merged together into a single number, and sometimes json obejct responses are merged into a single string. The same goes for non-asynchronous.

remorses commented 1 month ago

will be fixed by #743 and https://github.com/elysiajs/eden/pull/114

HK-SHAO commented 1 month ago

will be fixed by #743 and elysiajs/eden#114

@remorses A similar problem occurs in response to new ReadableStream(), which I solved by adding an await delay(0) after each controller.enqueue()

HK-SHAO commented 6 days ago

@SaltyAom When I sleep before each yield, it prevents objects from sticking into strings, do you think this is a bug in Bun?

import Elysia from "elysia";

export const server = new Elysia().post("/stream", async function* () {
  yield { hello: "world" };
  await Bun.sleep(0);
  yield { hello: "world" };
  await Bun.sleep(0);
  yield { hello: "world" };
});