elysiajs / elysia

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

onAfterHandle and mapResponse behavior strangely and different in aot #638

Open paletteOvO opened 1 month ago

paletteOvO commented 1 month ago

What version of Elysia.JS is running?

Elysia 1.0.20

What platform is your computer?

Linux 6.8.9-273-tkg-eevdf-llvm x86_64 unknown

What steps can reproduce the bug?

import { Elysia } from "elysia";

const app = new Elysia()
    .onRequest(({ request }) => {
        console.log(
            `[${new Date().toLocaleDateString()}] ${request.method} ${
                request.url
            }`,
        );
    })
    .onAfterHandle(({ set, response }) => {
        set.headers["Access-Control-Allow-Origin"] = "*";
        return response;
    })
    .mapResponse(({ headers, response, set }) => {
        const text = response?.toString() ?? "";

        if (headers["accept-encoding"]?.includes("gzip") !== true) {
            return;
        }

        set.headers["content-encoding"] = "gzip";

        return new Response(Bun.gzipSync(text));
    })
    .get("/", () => {
        return "Hello world";
    })
    .listen(process.env.PORT ?? 3000);

console.log(
    `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);
curl -sv --compress 127.0.0.1:3000

*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000
> GET / HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/8.7.1
> Accept: */*
> Accept-Encoding: deflate, gzip, br, zstd
> 
* Request completely sent off
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Content-Encoding: gzip
< content-type: text/plain;charset=utf-8
< Date: Tue, 14 May 2024 08:26:29 GMT
< Content-Length: 11
< 
{ [11 bytes data]
* Error while processing content unencoding: incorrect header check
* Closing connection

What is the expected behavior?

curl -sv --compress 127.0.0.1:3000

*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000
> GET / HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/8.7.1
> Accept: */*
> Accept-Encoding: deflate, gzip, br, zstd
> 
* Request completely sent off
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< content-type: text/plain;charset=utf-8
< Date: Tue, 14 May 2024 08:28:58 GMT
< Content-Length: 11
< 
{ [11 bytes data]
* Connection #0 to host 127.0.0.1 left intact
Hello world

What do you see instead?

No response

Additional information

Everything is fined when aot is false.

Also, I conducted some tests by removing the "onAfterHandle" and "mapResponse" handlers separately. I found that if there is an "onAfterHandle" handler present, the return of the "mapResponse" handler is ignored. However, everything works fine if both handlers are "onAfterHandle" or if there is only the gzipSync "mapResponse" handler.

thejasonxie commented 1 month ago

Had this issue to after setting aot to false to make the bundled server work with headers. My workaround is to not use the mapResponse handler and instead, make a utility gzipping function that I call when return response in the controller/route.