0

We can echo line by line on nodejs via stream pipe from stdin to stdout, i.e. from Readable to Writable stream:

process.stdin.pipe(process.stdout);

Confusingly, it also works same as before from stdout to stdin, i.e. from Writable to Readable stream below. Why?

process.stdout.pipe(process.stdin);

@EDIT

The similar test with io.Copy in golang has reproduced the aforementioned issue. The cross-validation seems to hint the builtin pipe implementation can't be wrong on both incumbent platforms.

 package main                                                                                                          

 import (                                                                                                              
     "io"                                                                                                              
     "os"                                                                                                              
 )                                                                                                                     

 func main() {                                                                                                         
     io.Copy(os.Stdout, os.Stdin)                                                                                      
     // io.Copy(os.Stdin, os.Stdout) /* Also works than expected. */                                                                
 }                                                                                                                     

@EDIT 2

For further investigation another test with Deno.copy on the third platform deno behaves as expected:

Deno.copy(Deno.stdout, Deno.stdin); /* Works as expected. */
// Deno.copy(Deno.stdin, Deno.stdout); /* Fails as expected. */
sof
  • 9,113
  • 16
  • 57
  • 83
  • The second one doesn't seem to work for me. I get an `ENOTCONN` error. – jfriend00 Apr 12 '20 at 20:22
  • I even tested with the similar function [io.Copy](https://golang.org/pkg/io/#Copy) in *golang* by exchanging *src dst* pair between *os.Stdin os.Stdout* - the effect doesn't vary. – sof Apr 12 '20 at 21:04
  • What does that comment mean and why are you talking about golang in a node.js question? I cannot reproduce what you say in your question in node.js. `process.stdout.pipe(process.stdin);` produces nothing but an error for me. – jfriend00 Apr 12 '20 at 21:11
  • It cross-validates that the builtin *pipe* implementation can't be wrong on both *nodejs* and *golang*, even I didn't get the error you did. – sof Apr 12 '20 at 21:24
  • What version of node.js are you running? I'm running v12.13.1. – jfriend00 Apr 12 '20 at 21:28
  • Current LTS v12.16.2 – sof Apr 12 '20 at 21:38

0 Answers0