#Did Middleware 'context' change between releases?

1 messages · Page 1 of 1 (latest)

foggy temple
#

I am logging the context and I don't see the request object in it from a middleware:

import { loggerInstance } from "@helpers/logger"

export function onRequest(context, next) {
  loggerInstance.info(context, "request")
  if (context.request.includes === "europages") {
    context.locals.brand = "ep"
  } else {
    context.locals.brand = "wlw"
  }

  return next()
}

#

I am also getting:

but I didnt find any documentation about this error. Anyone? @tacit hamlet ❤️

tacit hamlet
#

Do you have any endpoints?

foggy temple
#

We do. One.

#
export async function get({ params }) {
  const { path } = params
  if (path === "status/health") {
    return new Response("I'm ok", {
      status: 200,
      headers: {
        "Content-Type": "text/html",
      },
    })
  }
}

#

But what we're trying to do is irrelevant, we're checking the request url and we wanna set an Astro.local depending on the url ( url - based theming )

#

I think the context structure changed during the RFC/Experimental and the next release for middleware..

#

Would be good to see how/where/a PR at least

tacit hamlet
#

Sorry, I don't really know how middleware works, I haven't used them myself and wasn't part of the development process

#

@outer ocean might know more

outer ocean
#

From your code, you're only returning a response in the if condition. What happens when the path is different?

#

The request is still there. Maybe it's not enumerable on the context object and your logger is only logging enumerated props. That's a guess at what's happening.

foggy temple
#

posting the logger implementation just in case you see something I dont:

import tracer from "dd-trace"
import formats from "dd-trace/ext/formats"

class Logger {
  info(payload, message) {
    this.log("info", payload, message)
  }

  warn(payload, message) {
    this.log("warn", payload, message)
  }

  error(payload, message) {
    this.log("error", payload, message)
  }

  log(level, payload, message) {
    const span = this.getActiveSpan()
    const time = new Date().toISOString()
    const record = { time, level, ...payload, message }

    if (span) {
      tracer.inject(span.context(), formats.LOG, record)
    }
    console.log(`${JSON.stringify(record)}\n`)
  }

  getActiveSpan() {
    return tracer.scope().active()
  }
}

export const loggerInstance = new Logger()
#

I guess stringify doesnt work for some of the objects of the request as they're circular dependencies?

outer ocean
#

did you try logging context.request specifically?

foggy temple
#

so yeah, the problem was our JSON.Stringify , we managed to log using util from Node

#

util.inspect

outer ocean
#

Ok, we should probably make that be enumerable. My guess was that it was a mistake.

foggy temple
#

if this is something you can change, definitely 🙂

foggy temple
#

Hey @outer ocean is there like a Type definition on the request? is it something from the Web Apis? We're a bit in the dark and we have some headers that are added only when deploying on our nginx so its tricky to test.

foggy temple
#

How can one resolve this warning?

#

11:07:27 AM [middleware] Middleware doesn't work for endpoints that return a simple body. The middleware will be disabled for this page. (x386054)

outer ocean
#

If you have any API endpoints that do return { body } you'll get that warning. Instead return a Response to get rid of it.

outer ocean
foggy temple
#

@outer ocean

export async function get({ params }) {
  const { path } = params
  if (path === "status/health") {
    return new Response("I'm ok", {
      status: 200,
      headers: {
        "Content-Type": "text/html",
      },
    })
  }
}

would this possibly return this warning when path isn't status/health?

outer ocean
#

yes

#

you need to still return a response

#

probably a 404

foggy temple
#

fixing it like:

export async function get({ params }) {
  const { path } = params
  if (path === "status/health") {
    return new Response("I'm ok", {
      status: 200,
      headers: {
        "Content-Type": "text/html",
      },
    })
  }
  return new Response("Not found", {
    status: 404,
    headers: {
      "Content-Type": "text/html",
    },
  })
}
#

?

outer ocean
#

yep exactly

foggy temple
#

cool, thanks

#

any idea how to convert the Request to json so I can log it properly formatted? 🙂

foggy temple
#

sorry, follow up important question, is the middleware running on Server Side?

outer ocean
#

yep, that's right, it's the same

outer ocean
foggy temple
#

@outer ocean sorry, I am logging my headers but many are missing

#

is this Request a browser-specific thing or does it also exist on server side?

#

(node etc)

outer ocean
#

both

#

what stuff is missing? Cookies?

foggy temple
#

X-Forwarded-Host

outer ocean
#

It's likely that something is stripping that off. If you are behind a proxy server like Nginx that could be it.

foggy temple
#

our infra team claims this header should actually be added by nginx

#

@outer ocean would this be the correct way to log all headers from context.request ? :

loggerInstance.info(context.request.headers, "headers")
outer ocean
#

sure, however you wish

foggy temple
#

I am saying this because when I log headers I get all weird Symbol stuff

#

I am wondering if there's a getter or something

outer ocean
#

Object.fromEntries(context.request.headers) will give you a plain object if that's what you're wanting

foggy temple
#

would util.inspect do the same?

outer ocean
#

im not sure

foggy temple
#

this is my problem ^

foggy temple
#

Hi, this is solved, and the answer is something I found accidentally. :

  const forwardedHost = context?.request?.headers.get("x-forwarded-host")

you need a .get() to get the header value..

#

normaly bracket notation did not work.

outer ocean
#

Ah yeah, Headers is a special object

#

You could do this: #1116636486775869480 message and get a plain object and then use bracket notation

foggy temple
#

what a mess JS is , seriously! Douglas' was so right.. some objects you can access normally, others have a getter