Skip to content
Closed
Closed
Bug: #968
@geogeim

Description

Reproduction
I can't seem to detect when the stream is closed if the server abruptly resets the connection. In fact i don't think node-fetch closes the stream if the connection is reset which makes the application hang. Is there a way to detect this when working with streams?

Your Environment

software version
node-fetch 2.6.0
node 14.9.0
npm
Operating System Windows 10, Linux

Additional context

const express = require('express');
const fetch = require('node-fetch');

const { Writable, Readable } = require('stream');

const nullStream = new Writable({
  write(chunk, enc, cb) {
    console.log('got chunk');
    cb();
  }
})

const zeroStream = new Readable({
  read(sz) {
    this.push(new Uint8Array(sz).fill(0));
    console.log('sent chunk');
  }
})

function awaitStream(stream) {
  return new Promise((res, rej) => {
    stream.on('end', res);
    stream.on('close', res);
    stream.on('finish', res);
    stream.on('error', rej);
    stream.on('unpipe', rej);
  });
}

async function main() {
  const app = express();
  app.use('/test', (req, res) => {
    res.status(200);
    zeroStream.pipe(res);
    setTimeout(() => res.socket.destroy(), 10);
  });

  app.listen(9999);

  const res = await fetch("http://localhost:9999/test");
  if (res.ok) {
    res.body.pipe(nullStream);
    setTimeout(() => {
      console.log(`stream destroyed? ${res.body.destroyed}`);
    }, 1000)
    await awaitStream(res.body);
  }
}

main();

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions