elysiajs / elysia

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

Using both Static and HTML plugins segfaults on Bun >=1.0.27 #524

Open Chooks22 opened 7 months ago

Chooks22 commented 7 months ago

What version of Elysia.JS is running?

1.0.0-beta.14

What platform is your computer?

Linux 6.7.9-zen1-1-zen x86_64 unknown

What steps can reproduce the bug?

import { $ } from 'bun'

await $`mkdir repro`.quiet()

$.cwd('./repro')

await $`bun init -y`.quiet()
await $`mkdir public`.quiet()
await $`bun add elysia@next @elysiajs/{static,html}@next`

const code = `
import Elysia from 'elysia'
import { html } from '@elysiajs/html'
import { staticPlugin } from '@elysiajs/static'

new Elysia()
  .use(staticPlugin({ prefix: '/static' }))
  .use(html())
  .listen(3000, async () => {
    console.log('fetching data with flags:', process.execArgv)
    const res = await fetch('http://localhost:3000/static/test.txt')
    const data = await res.text()
    console.log('got data: %s', data)
    process.exit()
  })
`.trim()

await Bun.write('repro/public/test.txt', 'foo')
await Bun.write('repro/index.ts', code)

await $`bun run --watch index.ts`
await $`bun run index.ts`

What is the expected behavior?

fetching data with flags: [ "--watch" ]
got data: foo
fetching data with flags: []
got data: foo

What do you see instead?

fetching data with flags: [ "--watch" ]
got data: foo
fetching data with flags: []

Additional information

Same issue on v0.8.x of Elysia Adding --watch or --hot flags to bun run works around the issue

hambergerpls commented 7 months ago

I'm experiencing the same problem when using @elysiajs/html with @elysiajs/static. As a temporary workaround, I created an assets endpoint with a wildcard and use Bun.file to retrieve them.

//index.jsx

import { Elysia, t } from 'elysia';
import { html } from "@elysiajs/html";

new Elysia()
    .use(html())
    .get('/assets/*', ({ set, params }) => {
        const file = Bun.file(`${import.meta.dir}/assets/${params['*']}`);
        set.headers['Content-Type'] = file.type;
        return file.text();
    })
    .get('/', () => {
        return (
            <html lang='en'>
                <head>
                    <script src="assets/js/htmx.min.js" />
                </head>
                <body>
                    <form hx-post="/" hx-swap="afterend">
                        <input type="text" name="name" />
                        <button type='submit'>Click Me!</button>
                    </form>
                </body>
            </html>
        )
    })
    .post('/', ({ body }) => {
        return (<script>alert("{body.name}!")</script>);
    }, {
        body: t.Object({ name: t.String() })
    })
    .listen(9999, ({ port }) => {
        console.log(`Server is running on port ${port}`)
    });

But I think the segmentation fault is related to Bun.