unjs / unenv

🕊️ Convert javaScript code to be runtime agnostic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

All response headers are converted to string

pheekus opened this issue · comments

This seems to be intentional but it breaks compatibility with packages like node-oidc-provider that expect response.getHeader() to return the same data type as was passed to response.setHeader(), which is what the node docs say should happen.

For context, here's what happens in one of the projects I'm working on for a client. We're using nuxt3 deployed with the aws-lambda nitro preset. Several API routes pass ServerResponse instances from this framework to node-oidc-provider that sets multi-value set-cookie header in one module and reads it afterwards in another one. Since the original value is an array, it expects to receive an array back. Instead of that, it gets a comma-separated string and errors.

Relevant code is below. If this is the behaviour you're planning to keep, I'd appreciate a quick comment about that so I and everyone else who stumbles upon this issue can look into a workaround. Thanks :)

this._headers[name.toLowerCase()] = value + "";

commented

Hi, can you provide a small reproduction please ?
I would like to know if you're doing all of this in an event handler and which methods you're using exactly.

@Hebilicious sure thing, I'll get you a test project to look at by the end of this week

@Hebilicious here's the repo, it's a heavily simplified version of the project I'm working on. I've included some comments on what it is and how it works in the readme: unjs-unenv-issue-118-demo.

This is a bit off-topic/cross-project BUT if anyone else experiences similar issues with headers/cookies/base64 in a nuxt3 app built with the aws-labmda preset, a custom preset with serverless-http worked super well for me as a temporary solution:

preset/entry.ts:

import "#internal/nitro/virtual/polyfill";
import { toNodeListener } from "h3";
import serverless from "serverless-http";

const listener = toNodeListener(useNitroApp().h3App);
export const handler = serverless(listener, { provider: "aws" });

preset/nitro.config.ts:

import type { NitroPreset } from "nitropack";
import { fileURLToPath } from "node:url";

export default <NitroPreset>{
  extends: "node-server",
  entry: fileURLToPath(new URL("./entry.ts", import.meta.url)),
};

nuxt.config.ts:

nitro: {
  preset: "./preset",
},