Skip to content

Pipe stream throws error: response has been emitted already #1803

@Borewit

Description

@Borewit

Describe the bug

Pipe stream causes error: "The response has been emitted already."

Blocks: sindresorhus/file-type#470

  • got version v11.8.2
  • Node.js version: 16.4.2
  • OS & version: Windows 10

Actual behavior

Throws an error

C:\Users\Maarten\code\github\got\dist\source\core\index.js:1476
            throw new Error('Failed to pipe. The response has been emitted already.');
                  ^

Error: Failed to pipe. The response has been emitted already.
    at Request.pipe (C:\Users\Borewit\code\github\got\dist\source\core\index.js:1476:19)
    at Request.<anonymous> (C:\Users\Borewit\code\github\examples\examples\file-type\got-error.js:9:14)
    at Object.onceWrapper (node:events:513:28)
    at Request.emit (node:events:394:28)
    at emitReadable_ (node:internal/streams/readable:571:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

Expected behavior

Be able to read the HTTP content using a stream.
I don't see what reading has to do with HTTP headers already sent (emitted).

Code to reproduce

const got = require('got');
const stream = require('stream');

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

const httpStream = got.stream(url);
httpStream.on('error', error => console.error(error));
httpStream.once('readable', async () => {
  httpStream.pipe(new stream.PassThrough());
});
...

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Workaround

See: dec028d

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis change will extend Got features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions