elysiajs / elysia

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

Specifying TLS on listen 'expected fetch() to be a function' #133

Closed thennothinghappened closed 1 year ago

thennothinghappened commented 1 year ago

Hello! I'm trying out Elysia and trying to get TLS working. I haven't seen this mentioned anywhere, aside from one mention on Bun's own Bun.serve. I noticed that listen({}) can take a tls prop as well, so put in as so:

import { Elysia } from 'elysia';

const app = new Elysia()
    .get('/', () => 'test')
    .listen({
        port: (Bun.env.port ?? 8080) as string,
        tls: {
            cert: Bun.file(Bun.env.tls_cert as string),
            key: Bun.file(Bun.env.tls_key as string)
        }
    });

This is the only file of the project right now. Running without tls specified works fine, but with it prints a huge mess of minified code followed by:

TypeError: Expected fetch() to be a function
 code: "ERR_INVALID_ARG_TYPE"

      at /Users/orca/Documents/GitHub/notetest/node_modules/elysia/dist/bun/index.js:272:34990
      at /Users/orca/Documents/GitHub/notetest/src/index.ts:3:12

I'm assuming Elysia itself uses Bun.serve as a base? With that, as a test I tried specifying a fetch(req, server) to see what'd happen, and the same error still prints, so must be something internal.

Apologies if this is already known or not within the scope of the project, its been difficult to find much information anything TLS related, and this project doesn't seem to mention it anywhere, which makes it unusable in my use case

thennothinghappened commented 1 year ago

I've just tested this in Elysia's examples for better feedback, got this log output:

3537 |          if (typeof Bun === 'undefined')
3538 |                  throw new Error(
3539 |                          '.listen() is designed to run on Bun only. If you are running Elysia in other environment please use a dedicated plugin or export the handler via Elysia.fetch'
3540 |                  )
3541 | 
3542 |          this.server = Bun?.serve(serve)
                    ^
TypeError: Expected fetch() to be a function
 code: "ERR_INVALID_ARG_TYPE"

      at /Users/orca/Documents/GitHub/elysia/src/index.ts:3542:16
      at /Users/orca/Documents/GitHub/elysia/example/http.ts:118:2
3537 |          if (typeof Bun === 'undefined')
3538 |                  throw new Error(
3539 |                          '.listen() is designed to run on Bun only. If you are running Elysia in other environment please use a dedicated plugin or export the handler via Elysia.fetch'
3540 |                  )
3541 | 
3542 |          this.server = Bun?.serve(serve)
                    ^
TypeError: Expected fetch() to be a function
 code: "ERR_INVALID_ARG_TYPE"

      at /Users/orca/Documents/GitHub/elysia/src/index.ts:3542:16
      at /Users/orca/Documents/GitHub/elysia/example/http.ts:118:2
thennothinghappened commented 1 year ago

Should've looked around just a little more before posting :)

Found oven-sh/bun/issues/4555, this isn't an issue with Elysia.