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

Some data not coming out through stdout when used as the last stream. #18

Open
wllmtrng opened this issue Jan 28, 2016 · 4 comments
Open

Comments

@wllmtrng
Copy link

Some data is lost during piping for this script:

var combine = require('stream-combiner');
var through = require('through2');

// Used for debug. This issue manifests itself when middle stream is omitted.
middle = through(function(buffer, encoding, next) {
  console.log("GOT " + buffer.toString());
  next();
})

combine(process.stdin,
        middle,
        process.stdout);

Output

TEST 1 // From stdin
GOT TEST 1

TEST 1
TEST 2 // From stdin
TEST 3 // From stdin
GOT TEST 3

TEST 3

Seems like every other input from stdin is dropped.

When I do this:

var combine = require('stream-combiner');
var through = require('through2');

middle = through(function(buffer, encoding, next) {
  console.log("GOT " + buffer.toString());
  this.push(buffer);
  next();
})

combine(process.stdin,
        middle);

Output:

Williams-MBP:3_stream_adventure wtruong$ node 14_crypt.js
TEST 1
GOT TEST 1

TEST 2
GOT TEST 2

TEST 3
GOT TEST 3

Everything works correctly.

Running on Mac OS X with node v0.12.7 and:

├─┬ stream-combiner@0.2.2
│ ├── duplexer@0.1.1
│ └── through@2.3.8

├─┬ through2@2.0.0
│ └─┬ readable-stream@2.0.5
│ ├── core-util-is@1.0.2
│ ├── isarray@0.0.1
│ ├── process-nextick-args@1.0.6
│ ├── string_decoder@0.10.31
│ └── util-deprecate@1.0.2

@wllmtrng
Copy link
Author

Problem still exists with node v5.2.0

@dominictarr
Copy link
Owner

What you are doing is quite weird because console.log also writes to standard out.
This isn't really the right way to use combine.
For this, just use stdin.pipe(middle).pipe(stdout)

It makes more sense if the combined streams are all through streams.

@wllmtrng
Copy link
Author

wllmtrng commented Feb 7, 2016

I was using console.log just for debug purposes.

Run the following code:

var combine = require('stream-combiner');
var through = require('through2');

middle = through(function(buffer, encoding, next) {
  this.push(buffer)
  next();
})

combine(process.stdin,
        middle,
        process.stdout);

Running this in the terminal and entering text into stdin will get you this:

$ node test.js
1 // user inputs 1
1 //  stdout prints 1
2 //  user inputs 2, but nothing gets printed out to stdout
3 //  user inputs 3
3 //  stdout prints 3
4 //  user inputs 4, but nothing comes out of stdout
5 //  user inputs 5
5 //  stdout prints 5

If piping in stdin and out to stdout should not be used, then stdin and stdout examples should be removed from readme.md, only recommending through2 streams.

@dominictarr
Copy link
Owner

Wow, that readme is really really old. I havn't written openStdin() in a long time. that must be from node 0.8 or earlier. pipeing to stdout writes to the same thing as console.log(). The difference is that console.log is sync... so I wouldn't be surprised if something weird happened. If you want to debug while writing to stdout, use console.error instead of .log that writes to stderr not stdout.

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