elysiajs / elysia

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

headers(header: Context['set']['headers'] | undefined) not work when no aot #711

Open OXeu opened 1 month ago

OXeu commented 1 month ago

What version of Elysia.JS is running?

1.0.27

What platform is your computer?

Linux 6.5.0-35-generic x86_64 x86_64

What steps can reproduce the bug?

bun create elysia app

Write the following code in src/index.ts

import { Elysia } from "elysia";

const app = new Elysia({ aot: false })
  .headers({
    "X-Powered-By": "Elysia",
  })
  .get("/", () => "Hello").listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
# No X-Powered-By Header
$ curl -v http://localhost:3000
* Host localhost:3000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:3000...
* Connected to localhost (::1) port 3000
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: text/plain;charset=utf-8
< Date: Wed, 10 Jul 2024 12:32:27 GMT
< Content-Length: 5
<
* Connection #0 to host localhost left intact
Hello

When set aot: true:

import { Elysia } from "elysia";

const app = new Elysia({ aot: true })
  .headers({
    "X-Powered-By": "Elysia",
  })
  .get("/", () => "Hello").listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
# Has X-Powered-By Header
$ curl -v http://localhost:3000
* Host localhost:3000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:3000...
* Connected to localhost (::1) port 3000
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Elysia
< content-type: text/plain;charset=utf-8
< Date: Wed, 10 Jul 2024 12:34:06 GMT
< Content-Length: 5
<
* Connection #0 to host localhost left intact
Hello

What is the expected behavior?

When aot: false, it still works

What do you see instead?

No response

Additional information

Actually it was found in @elysiajs/cors 1.0.4, when aot: false (Which is necessary for Cloudflare Worker), the OPTION method did not response properly, but in 1.0.2, it works well.

To reproduce:

bun create elysia app
cd app/
bun i @elysiajs/cors@1.0.4
// src/index.ts
import cors from "@elysiajs/cors";
import { Elysia } from "elysia";

const app = new Elysia({ aot: false })
  .use(cors({
    aot: false,
    allowedHeaders: [
      'authorization',
      'content-type'
    ],
  }))
  .get("/", () => "Hello Elysia").listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
# missing some headers created by `app.headers(defaultHeaders);`
# such as `Access-Control-Allow-Headers`
$ curl -v http://localhost:3000
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Vary: *
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET
< Access-Control-Expose-Headers: host, user-agent, accept
< content-type: text/plain;charset=utf-8
< Date: Wed, 10 Jul 2024 12:40:00 GMT
< Content-Length: 12
<
* Connection #0 to host localhost left intact
Hello Elysia%

But when downgradle to @elysiajs/cors@1.0.2 or just enable aot:

bun i @elysiajs/cors@1.0.2

it works well.

# Downgradle to 1.0.2
$ curl -v http://localhost:3000                                                                              [20:47:47]
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Vary: *
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: *
< Access-Control-Allow-Headers: authorization, content-type
< Access-Control-Expose-Headers: *
< content-type: text/plain;charset=utf-8
< Date: Wed, 10 Jul 2024 12:48:01 GMT
< Content-Length: 12
<
* Connection #0 to host localhost left intact
Hello Elysia
# set aot = true
$ curl -v http://localhost:3000                                                                              [20:48:01]
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Access-Control-Allow-Headers: authorization, content-type
< Access-Control-Allow-Credentials: true
< Vary: *
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: *
< Access-Control-Expose-Headers: *
< Access-Control-Exposed-Headers: *
< content-type: text/plain;charset=utf-8
< Date: Wed, 10 Jul 2024 12:48:39 GMT
< Content-Length: 12
<
* Connection #0 to host localhost left intact
Hello Elysia