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 statusCode in node:http; undefined in Deno, 200 in Node #23970

Closed
vrugtehagel opened this issue May 24, 2024 · 1 comment · Fixed by #23977
Closed

Response statusCode in node:http; undefined in Deno, 200 in Node #23970

vrugtehagel opened this issue May 24, 2024 · 1 comment · Fixed by #23977

Comments

@vrugtehagel
Copy link
Contributor

vrugtehagel commented May 24, 2024

> deno --version
deno 1.43.6 (release, aarch64-apple-darwin)
v8 12.4.254.13
typescript 5.4.5
> node --version
v21.7.3

This issue popped up when trying to use Eleventy with Deno (see 11ty/eleventy-dev-server#78). To reproduce a stripped-down version of the issue, consider the following script:

import { createServer } from 'node:http'

const server = createServer((request, response) => {
    console.log(response.statusCode);
    response.end()
});

server.listen(8080);
await fetch('http:https://localhost:8080/')
server.close()

When running this script in node (e.g. node script.js), this logs 200. However, when running it in Deno (e.g. deno run -A script.js), it logs undefined. This causes the Eleventy bug mentioned above.

After a little bit of digging, I did notice that Node seems to set statusCode to 200 by default. Deno seems to set it, but after the callback provided to createServer() has already run.

A potential solution could be to default statusCode to 200 in Deno as well, though I have not checked for potential side effects as of yet.

@vrugtehagel vrugtehagel changed the title Response statusCode in node:http module undefined in Deno, 200 in Node Response statusCode in node:http; undefined in Deno, 200 in Node May 24, 2024
@vrugtehagel
Copy link
Contributor Author

Here's a slightly simpler reproduction case:

import { ServerResponse, ClientRequest } from 'node:http'

const request = new ClientRequest()
const response = new ServerResponse(request)
console.log(response.statusCode)

request.on('error', () => {})

I did try and set up Deno locally to see the impact of setting it to 200 by default, but I didn't quite manage to get the unit_node tests to run properly (24 of them fail even without making any changes). But, at least setting it to 200 by default doesn't seem to change the number of failing tests 😉

marvinhagemeister added a commit that referenced this issue May 26, 2024
Node sets the default HTTP response status code to 200 on the
`ServerResponse`. We initialised it as `undefined` before which caused a
problem with 11ty's dev server.

Thanks to @vrugtehagel for reporting this issue and finding the correct
fix as well 🎉

Fixes #23970
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant