elysiajs / elysia

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

Context object not correctly passed to handler #688

Open Coachonko opened 2 weeks ago

Coachonko commented 2 weeks ago

What version of Elysia.JS is running?

1.0.25

What platform is your computer?

Linux 5.14.0-427.18.1.el9_4.x86_64

What steps can reproduce the bug?

const testHandlerWrapper = async (ctx, handler) => {
  handler(ctx.cookie)
}

new Elysia()
  .get('/test-one', ctx => console.log(ctx.cookie)) // {}
  .get('/test-two', ctx => testHandlerWrapper(ctx, console.log)) // undefined

What is the expected behavior?

const testHandlerWrapper = async (ctx, handler) => {
  handler(ctx.cookie)
}

new Elysia()
  .get('/test-one', ctx => console.log(ctx.cookie)) // {}
  .get('/test-two', ctx => testHandlerWrapper(ctx, console.log)) // {}

What do you see instead?

undefined instead of {}

Additional information

Another test

const testHandlerWrapper = async (ctx, handler) => {
  if (typeof handler === 'undefined') console.log(c)
  handler(c)
}

new Elysia()
  .get('/test-one', async ctx => tempFn(ctx))
  .get('/test-two', async ctx => tempFn(ctx, console.log))

output:

{
  request: Request (0 KB) {
    method: "GET",
    url: "http://localhost:12001/test-one",
    headers: Headers {
      "host": "localhost:12001",
      "connection": "keep-alive",
      "pragma": "no-cache",
      "cache-control": "no-cache",
      "dnt": "1",
      "upgrade-insecure-requests": "1",
      "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
      "sec-fetch-mode": "navigate",
      "sec-fetch-dest": "document",
      "accept-encoding": "gzip, deflate, br, zstd",
      "accept-language": "en-US,en;q=0.9,it;q=0.8,nl;q=0.7,nb;q=0.6",
      "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Microsoft Edge\";v=\"126\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Linux\"",
      "sec-fetch-site": "none",
      "sec-fetch-user": "?1",
    }
  },
  store: {},
  qi: -1,
  path: "/test-one",
  url: "http://localhost:12001/test-one",
  redirect: [Function: redirect],
  set: {
    headers: {},
    status: 200,
    cookie: {},
  },
  error: [Function: error22],
  headers: {
    host: "localhost:12001",
    connection: "keep-alive",
    pragma: "no-cache",
    "cache-control": "no-cache",
    dnt: "1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0",
    accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "sec-fetch-mode": "navigate",
    "sec-fetch-dest": "document",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "en-US,en;q=0.9,it;q=0.8,nl;q=0.7,nb;q=0.6",
    "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Microsoft Edge\";v=\"126\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
  },
  cookie: {},
  query: {},
}
{
  request: Request (0 KB) {
    method: "GET",
    url: "http://localhost:12001/test-two",
    headers: Headers {
      "host": "localhost:12001",
      "connection": "keep-alive",
      "pragma": "no-cache",
      "cache-control": "no-cache",
      "dnt": "1",
      "upgrade-insecure-requests": "1",
      "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
      "sec-fetch-mode": "navigate",
      "sec-fetch-dest": "document",
      "accept-encoding": "gzip, deflate, br, zstd",
      "accept-language": "en-US,en;q=0.9,it;q=0.8,nl;q=0.7,nb;q=0.6",
      "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Microsoft Edge\";v=\"126\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Linux\"",
      "sec-fetch-site": "none",
      "sec-fetch-user": "?1",
    }
  },
  store: {},
  qi: -1,
  path: "/test-two",
  url: "http://localhost:12001/test-two",
  redirect: [Function: redirect],
  set: {
    headers: {},
    status: 200,
  },
  error: [Function: error22],
}
SaltyAom commented 1 day ago

Can you verify if this was fixed on the latest (1.0.27)?