Streaming stdout and stderr #1

Open
indexzero opened this Issue Jan 14, 2012 · 2 comments

Comments

Projects
None yet
2 participants

I noticed in the docs that you expose a collected .data() function in procstream, but is it possible to stream the data as it comes through? Something like:

var $p = require('procstreams');

$p('mkdir foo')
  .and('cp file.txt foo/')
  .and('rm file.txt')
    .stdout.on('data', function (data) { /* inspect the output and perform some kind of expectation */ }) 
    .stderr.on('data', function (data) { /* inspect the output and perform some kind of expectation */ }) 
    .on('exit', function() {
      console.log('done');
    });

This would be really useful for an integration with something like nexpect.

Owner

polotek commented Jan 14, 2012

So this is interesting. I definitely wanted to support immediate streaming. But you can't do it immediately with every proc in the chain. Because each one after the first one may just be a promise that has to wait on the previous proc to see if it should execute.

$p('mv foo.txt bar.txt').and('cat bar.txt')

If the mv fails, the cat doesn't execute so it can't be a full fledged child proc. I thought about doing a heavy proc promise that would mimic the stdout and stderr streams and proxy them through when/if the proc ever started. But that was a huge pain and I didn't want it to hold up an initial release.

Instead there is a start event that fires when each part of the chain is executed. You can use that to get the real proc that can then be streamed.

$p('mv foo.txt bar.txt').and('cat bar.txt')
  .on('start', function() {
    .stdout.on('data', function (data) { /* inspect the output and perform some kind of expectation */ }) 
    .stderr.on('data', function (data) { /* inspect the output and perform some kind of expectation */ })
  })
  .on('exit', function() {
    console.log('done');
  })

Try this out. What do you think of it? Is it a pain? I'm still open to refining the api, (I don't think data() is the best name for that method). I could also do the full proxy of the streams if you think that would really improve the experience.

FYI, I didn't document the start method because I'm not yet sure if it's fully reliable. It's in a nextTick so there's a chance it could happen after the proc starts executing. I don't think so though. I just need to prove it.

The only reason I would want this feature is for using nexpect with procstreams, but the solution isn't turning out as elegant as I would like. Maybe the solution here is to add nexpect features to procstreams:

  $p('node')
    .expect('>')
    .sendline("console.log('testing')")
    .expect("testing")
    .sendline("process.exit()")
    .then('mv foo.txt bar.txt')
    .on('exit', function () {

    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment