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

stream does not work with simple got stream snippet #342

Closed
fiws opened this issue Apr 1, 2020 · 4 comments
Closed

stream does not work with simple got stream snippet #342

fiws opened this issue Apr 1, 2020 · 4 comments
Assignees
Labels

Comments

@fiws
Copy link

fiws commented Apr 1, 2020

I don't get why this simple code snippet does not work.

I'm sure this is some kind of nodejs stream thing, but reading the docs of file-type i just expected this to work:

const got = require('got');
const FileType = require('file-type');

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

(async () => {
  const source = got.stream(url);
  console.log(await FileType.stream(source));
  // ( ↑ should do something with the stream instead of logging it)
})();

Result

(node:31937) UnhandledPromiseRejectionWarning: Error: Failed to pipe. The response has been emitted already.
    at DuplexWrapper.proxy.pipe (/home/fiws/hydra/pot-api/node_modules/got/dist/source/as-stream.js:98:19)
    at pipe (internal/streams/pipeline.js:54:15)
    at Array.reduce (<anonymous>)
    at Function.pipeline (internal/streams/pipeline.js:94:18)
    at DuplexWrapper.<anonymous> (/home/fiws/hydra/pot-api/node_modules/file-type/core.js:1290:26)
    at Object.onceWrapper (events.js:427:28)
    at DuplexWrapper.emit (events.js:321:20)
    at emitReadable_ (_stream_readable.js:577:12)
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
(node:31937) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:31937) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@Borewit
Copy link
Collaborator

Borewit commented Apr 2, 2020

Can you have a look at this one @bencmbrook? It seems related to the stream() function.

@bencmbrook
Copy link
Contributor

bencmbrook commented Apr 2, 2020

It looks like got.stream closes the HTTP request if a pipe isn't immediately attached.

This will fix your code:

const got = require('got');
const FileType = require('file-type');
+ const { PassThrough } = require('stream');

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

(async () => {
- const source = got.stream(url);
+ const source = got.stream(url).pipe(new PassThrough());
  console.log(await FileType.stream(source));
  // ( ↑ should do something with the stream instead of logging it)
})();

While I wouldn't call this a bug in file-type, it does make for a feature request, which is to expose a Writable or TransformStream.

@fiws
Copy link
Author

fiws commented Apr 3, 2020

thank you a lot. this solved my problem.

i don't think the normal user can expect that. Maybe document it or workaround somehow. Just a suggestion though.

You can close this issue if you want.

@bencmbrook
Copy link
Contributor

Thanks, closing this with TransformStream request in #350

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

3 participants