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

Accessing ctx.query directly breaks the object #630

Open
SEAPUNK opened this issue May 9, 2024 · 2 comments
Open

Accessing ctx.query directly breaks the object #630

SEAPUNK opened this issue May 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@SEAPUNK
Copy link

SEAPUNK commented May 9, 2024

What version of Elysia.JS is running?

elysia@1.0.15, bun 1.1.7

What platform is your computer?

Linux 6.4.9-arch1-1 x86_64 unknown

What steps can reproduce the bug?

import { Elysia } from "elysia";
import assert from "node:assert";

const hostname = "127.0.0.1";
const port = 8089;
const app = new Elysia();

// using this request:
// http://localhost:8089/foo/bar?foo=bar&bar=baz
app.get("/foo/bar", async (ctx) => {
  console.log(ctx.query);
  // request with just the console.log above (commenting out the console.logs below)
/*
Listening on 127.0.0.1:8089
{
  query: undefined,
}
*/
  
  // when you add the following three console.log s:
  // console.log(JSON.stringify(ctx.query));
  // console.log(ctx.query.foo);
  // console.log(ctx.query.bar);
/*
Listening on 127.0.0.1:8089
{
  query: undefined,
  "query)": undefined,
  foo: "bar",
  bar: "baz",
}
{"foo":"bar","bar":"baz"}
bar
baz
*/
});

app.listen({
  hostname,
  port,
});
assert.ok(app.server);

console.log(`Listening on ${app.server.hostname}:${app.server.port}`);

What is the expected behavior?

For ctx.query to actually be a correct object containing all of the query parameters in the request.

What do you see instead?

See repro comments.

Additional information

Taking a glance at the codebase, it appears to be because elysia tries to be "smart" with figuring out what the route handler wants to access, and codegens around it. Why? Why is this a thing??? I understand that it may be for "performance reasons" but in place of it you open up a GIGANTIC can of worms causing issues such as above because people can't expect their code to work as it normally does within normal javascript execution. I am very much okay taking a perf hit if it means that the library I use doesn't try to be fancy with metaprogramming like this, potentially exposing my code to critical error paths.

@SEAPUNK SEAPUNK added the bug Something isn't working label May 9, 2024
@bogeychan
Copy link
Contributor

You can disable aot (the codegen) as a workaround:

new Elysia({ aot: false })

@SEAPUNK
Copy link
Author

SEAPUNK commented May 15, 2024

Thanks, I wasn't aware of that option. It's still a pretty nasty surprise learning that this was a thing Elysia did, especially given how this appears to not be the only bug relating to it. I would feel a lot more confident in Elysia if it had it disabled by default, instead letting the user opt into it in the docs, after warning about its potential volatility.

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