Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onAfterHandle and mapResponse behavior strangely and different in aot #638

Open
paletteOvO opened this issue May 14, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@paletteOvO
Copy link

paletteOvO commented May 14, 2024

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants