8000 `set.headers` ignored when Elysia is bundled · Issue #625 · elysiajs/elysia · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
set.headers ignored when Elysia is bundled #625
Closed
@einar-hjortdal

Description

@einar-hjortdal

What version of Elysia.JS is runni 82A1 ng?

1.0.15

What platform is your computer?

Linux 5.14.0-362.24.1.el9_3.x86_64 x86_64 x86_64

What steps can reproduce the bug?

import Elysia from 'elysia'

function myHandler (ctx) {
  try {
    const body = `<!DOCTYPE html><head></head><body>HTML</body>`
    ctx.set.headers['Content-Type'] = 'text/html; charset=utf-8'
    return body
  } catch (err) {
    console.error(err)
  }
}

export function tryFiles (ctx, publicDir, handler) {
  if (ctx.path === '/') {
    return handler(ctx)
  }

  const absolutePath = join(process.cwd(), publicDir, ctx.path)

  const file = Bun.file(absolutePath)
  if (file.size === 0) {
    return handler(ctx)
  }

  return file
}

new Elysia()
  .get('*', ctx => {
    if (process.env.BUN_ENV === 'production') {
      return myHandler(ctx)
    }
    return tryFiles(ctx, 'build/browser', myHandler)
  })
  .on('start', () => console.log(`Running on port ${process.env.PORT}`))
  .listen(process.env.PORT)

This code works as intended when run by Bun without bundling.
If this code is bundled bun build ./index.js --outdir ./build --target=bun --public-path /_statics/ then the browser receives Content-Type text/plain instead of text/html

{
  "name": "elysia-bugs",
  "module": "index.js",
  "type": "module",
  "devDependencies": {
    "@types/bun": "latest"
  },
  "scripts": {
    "build": "bun build ./index.js --outdir ./build --target=bun --public-path /_statics/",
    "dev": "bun run build && PORT=8080 BUN_ENV=production bun run build/index.js",
    "start": "bun run build && PORT=8080 bun run build/index.js"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "dependencies": {
    "elysia": "^1.0.15"
  }
}

I need the bundle to work, please help me

What is the expected behavior?

built and not-built app work the same

What do you see instead?

built app is broken

Additional information

This may be a problem with bun:
In my debugging journey, I have combined the functions 2 handlers above (tryFiles and myHandler) into one and this issue disappears. However, when using the --minify flag, the same issue appears again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpendingShould be resolved in next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0