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

The 'body' stream never ends for empty HTTP response bodies on Node 16 #1798

Open
ljwagerfield opened this issue Jan 5, 2024 · 1 comment
Labels

Comments

@ljwagerfield
Copy link

Reproduction

Steps to reproduce the behavior:

  1. Install Node 16 and node-fetch@3.3.2.
  2. Run the following code.
  3. Observe that 'Streaming body... done!' is never called for the empty file.
import fetch from 'node-fetch';

async function run() {
  await downloadUrl('https://upcdn.io/W142hJk/raw/tmp/non-empty.txt'); // Non-Empty (works)
  await downloadUrl('https://upcdn.io/W142hJk/raw/tmp/empty.txt'); // Empty (hangs)
}

async function downloadUrl(url) {
  const response = await fetch(url);
  const responseBody = response.body;

  console.log('Streaming body...');

  await new Promise((resolve, reject) => {
    responseBody.on('data', () => console.log('Received chunk.'));
    responseBody.on('end', () => resolve());
    responseBody.on('error', (err) => reject(err));
  });

  console.log('Streaming body... done!');
}

run().then(
  () => {},
  (e) => console.error('Error!', e)
);

Expected behavior

end event on response.body should fire for empty HTTP response bodies (but it's not on Node 16).

Your Environment

software version
node-fetch 3.3.2
node 16.20.2
npm 8.19.4
Operating System macOs 13.3.1 (a)

Additional context

Issue occurs on Node 16, but not on Node 18.

@jonseymour
Copy link

jonseymour commented Feb 7, 2024

as a datapoint, we are seeing similar behaviour with Node 16. it is only happening on some requests, which suggests that a length condition may be a necessary condition for the issue to present itself. In our case the body length is 17887 bytes.

I have found another case where it happens with a response body of 16800 bytes. I have yet to see it with a shorter response body.

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

No branches or pull requests

2 participants