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

How to stream response after knowing the Content-Type? #881

Closed
1 task done
smoliji opened this issue Sep 18, 2019 · 3 comments
Closed
1 task done

How to stream response after knowing the Content-Type? #881

smoliji opened this issue Sep 18, 2019 · 3 comments

Comments

@smoliji
Copy link

smoliji commented Sep 18, 2019

What would you like to discuss?

How to use got.stream to stream a file response to writable stream, after I have information about the file type - defined in Content-Type header from the response?

I short:

got.stream(imageUrl)
   .on('response', async response => {
        const ext = mime.getExtension(response.headers['content-type']!);
        const outFile = createWriteStream(`${fileName}.${ext}`);
        pipeline(
            response,
            outFile
        );
    })

I have prepared an MWE here, where I do this task with the request library as well as with got.

But when using got, the Node.js process just exists with code = 0 without any error or warnings, and the out file is just a preview and has only 33.7KB (compared to 12MB original image). Only way I've got this working was by adding on('data' handler for the got.stream, but I think this would break the backpressure for the pipeline.

Not sure whether this is a bug or intended.

  • Node.js version: v10.16.0
  • OS & version: Deepin 15.11

Checklist

  • I have read the documentation.
@szmarczak
Copy link
Collaborator

Duplicate of #223

@szmarczak szmarczak marked this as a duplicate of #223 Sep 18, 2019
@smoliji
Copy link
Author

smoliji commented Sep 20, 2019

Thanks, totally missed that one!

Just for completion, this seems like the correct way to do this and to fix the "stuck issue", adding resume() resolves it, as proposed in #223 (comment)

got.stream(imageUrl)
   .on('response', async response => {
        const ext = mime.getExtension(response.headers['content-type']!);
        const outFile = createWriteStream(`${fileName}.${ext}`);
        pipeline(
            response,
            outFile
        );
    })
    .resume()

@flaushi
Copy link

flaushi commented Jul 11, 2021

For others: in @smoliji's code, the third parameter, a callback, was missing. But the solution works! Thanks!

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

No branches or pull requests

3 participants