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

Response built by express is broken if res and req don't extend node's http.IncomingMessage and http.ServerResponse #6039

Open
Tofandel opened this issue Oct 11, 2024 · 1 comment · May be fixed by #6047

Comments

@Tofandel
Copy link

Tofandel commented Oct 11, 2024

To reproduce

import createApp from 'express';

const app = createApp();
console.log(app.response);

console.log(app.response.getHeader('vary'));
import createApp from 'express';
import {IncomingMessage} from 'unenv/runtime/node/http/_request'
import {ServerResponse} from 'unenv/runtime/node/http/_response'

const req = new IncomingMessage();
const res = new ServerResponse(req);

// Taken from express init middleware implementation
const app = createApp();
Object.setPrototypeOf(res, app.response);

res.setHeader('vary', 'test'); // TypeError: Cannot set properties of undefined (setting 'vary')
node:_http_outgoing:747
  const entry = headers[name.toLowerCase()];
                       ^

TypeError: Cannot read properties of undefined (reading 'vary')
    at ServerResponse.getHeader (node:_http_outgoing:747:24)

Because the non native response methods are being overwritten with the native methods

Solution:

var request = require('./request');

/**
 * Response prototype.
 * @public
 */

var res = new http.ServerResponse(request)
@Tofandel Tofandel added the bug label Oct 11, 2024
Tofandel added a commit to Tofandel/express that referenced this issue Oct 11, 2024
@Tofandel
Copy link
Author

Tofandel commented Oct 11, 2024

See also #2548 and unjs/unenv#328

@Tofandel Tofandel changed the title Response built by express is broken Response built by express is broken if res and req don't extend node's http.IncomingMessage and http.ServerResponse Oct 13, 2024
@IamLizu IamLizu added enhancement semver-minor This change is a semver minor and removed require-triage semver-minor This change is a semver minor labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants