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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: Middleware with returned undici response ignores the content-type if the response body is async. #1426

Closed
2 tasks done
dominikzogg opened this issue Mar 28, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@dominikzogg
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 馃槸

I get Content-Type: text/html if i return a response in a middleware with a async body.

import { Response } from 'undici';

return new Response(bodyStream.iterator(), {
      status: 200,
      statusText: 'OK',
      headers: {'content-type': 'application/json'},
    });

If the bodyStream is not async it does work.

import { Response } from 'undici';

const streamToBuffer = async (stream: Stream): Promise<Buffer> => {
  return new Promise<Buffer>((resolve, reject) => {
    const list: Array<Uint8Array> = [];

    stream.on('data', (chunk) => list.push(chunk));
    stream.on('end', () => resolve(Buffer.concat(list)));
    stream.on('error', (error) => reject(error));
  });
};

return new Response(await streamToBuffer(bodyStream), {
      status: 200,
      statusText: 'OK',
      headers: {'content-type': 'application/json'},
    });

Expected behavior 馃

I expect that the content-type passed as a headers is maintained in async and sync bodies on response.

Steps to reproduce 馃暪

Steps:

  1. Create a Middleware
  2. Return a Response (undici) with a async bodyInit and a headersInit with a content-type for example 'application/json'
  3. Do a request which hits the middleware and check the content-type on the response.

Context 馃敠

No response

Your environment 馃寧

System:
  OS: Fedora 39
  CPU: x86_64
Binaries:
  Node: 20.11.1 (/run/user/1000/fnm_multishells/128003_1711479810299/bin/node)
  pnpm: 8.15.4 (/run/user/1000/fnm_multishells/128003_1711479810299/bin/pnpm)
npmPackages:
 all of solid start +
 "undici": "^6.10.2"
 "undici-types": "^6.10.1"
@dominikzogg
Copy link
Author

This is the bridging code https://github.com/chubbyts/chubbyts-http-undici-bridge

@ryansolid
Copy link
Member

Hmm.. do you know if this is specific to undici? With Nitro I believe stuff like Response gets patched globally. I'm just wondering if this is an issue with how we are handling Responses from middleware in general or if this is due to some weirdness in the adapter layer.

@dominikzogg
Copy link
Author

@ryansolid what i can tell is, that the content-type header is part of the response object, but not part of the headers I get when calling it via curl.

@ryansolid
Copy link
Member

Moved this to Vinxi since it is lower level than we'd probably look at: nksaraf/vinxi#292

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