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

Adding 'data' event handler breaks output #109

Closed
techniq opened this issue Sep 23, 2015 · 4 comments
Closed

Adding 'data' event handler breaks output #109

techniq opened this issue Sep 23, 2015 · 4 comments

Comments

@techniq
Copy link

techniq commented Sep 23, 2015

I'm using multistream to stream multi API pages through the same stream (as suggested in issue #108) but I've ran into an issue. The API returns an empty array for pages that exceed the dataset so I need to "peek" at the data in the stream to know if I've reached the end. Using the example under stephenplusplus/stream-faqs as reference, if I add a stream.on('data', function() { ... }), no output is piped to process.stdout when using got.stream(). The issue does not occur when using request.

var MultiStream = require('multistream')
var got = require('got')
//var request = require('request')

var count = 0;
function factory (cb) {
  if (count > 3) return cb(null, null)
  count++
  var stream = got.stream('https://www.random.org/integers/?num='+count+'&min=1&max=6&col=1&base=10&format=plain&rnd=new')
  //var stream = request('https://www.random.org/integers/?num='+count+'&min=1&max=6&col=1&base=10&format=plain&rnd=new')

  stream.on('data', function() {
  })

  setTimeout(function () {
    cb(null, stream)
  }, 100)
}

MultiStream(factory).pipe(process.stdout);
@techniq techniq changed the title Add 'data' event handler breaks output Adding 'data' event handler breaks output Sep 23, 2015
@floatdrop
Copy link
Contributor

Thats because you dump all data to on('data', function () { ... }), this is not an issue with got. Use through2 to peek into stream.

@techniq
Copy link
Author

techniq commented Sep 23, 2015

Hmm, I just tried to add a a through2 transform to my pipeline that just passes data through and I still go no output when using got.stream but works fine with request. Sorry, I'm just getting into using streams so it might be something simple.

var MultiStream = require('multistream')
var through2 = require('through2')
var got = require('got')
var request = require('request')

var count = 0;
function factory (cb) {
  if (count > 3) return cb(null, null)
  count++
  var stream = got.stream('https://www.random.org/integers/?num='+count+'&min=1&max=6&col=1&base=10&format=plain&rnd=new')
  // var stream = request('https://www.random.org/integers/?num='+count+'&min=1&max=6&col=1&base=10&format=plain&rnd=new')

  stream.pipe(through2(function (chunk, enc, callback) {
    this.push(chunk)
    callback()
  }))

  setTimeout(function () {
    cb(null, stream)
  }, 100)
}

MultiStream(factory).pipe(process.stdout);

@floatdrop
Copy link
Contributor

Try this:

stream = stream.pipe(through2(function (chunk, enc, callback) {
    this.push(chunk)
    callback()
  }))

@techniq
Copy link
Author

techniq commented Sep 23, 2015

@floatdrop Thanks, that worked. It's odd that request works without setting the stream instance back onto the same variable. I tested with fs.createReadStream and it behaves like got.stream. Thanks again for your help.

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

2 participants