add process.stdin
authorRyan Dahl <ry@tinyclouds.org>
Sun, 2 Jan 2011 05:54:46 +0000 (21:54 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Sun, 2 Jan 2011 05:54:46 +0000 (21:54 -0800)
doc/api/process.markdown
doc/api/repl.markdown
doc/api/streams.markdown
src/node.js

index 5fee9a1..3383d59 100644 (file)
@@ -59,7 +59,8 @@ standard POSIX signal names such as SIGINT, SIGUSR1, etc.
 
 Example of listening for `SIGINT`:
 
-    var stdin = process.openStdin();
+    // Start reading from stdin so we don't exit.
+    process.stdin.resume();
 
     process.on('SIGINT', function () {
       console.log('Got SIGINT.  Press Control-D to exit.');
@@ -80,21 +81,21 @@ Example: the definition of `console.log`
     };
 
 
-### process.openStdin()
+### process.stdin
 
-Opens the standard input stream, returns a `Readable Stream`.
+A `Readable Stream` for stdin. The stdin stream is paused by default, so one
+must call `process.stdin.resume()` to read from it.
 
 Example of opening standard input and listening for both events:
 
-    var stdin = process.openStdin();
+    process.stdin.resume();
+    process.stdin.setEncoding('utf8');
 
-    stdin.setEncoding('utf8');
-
-    stdin.on('data', function (chunk) {
+    process.stdin.on('data', function (chunk) {
       process.stdout.write('data: ' + chunk);
     });
 
-    stdin.on('end', function () {
+    process.stdin.on('end', function () {
       process.stdout.write('end');
     });
 
index 8ae3623..1bdb4f2 100644 (file)
@@ -27,11 +27,11 @@ For example, you could add this to your bashrc file:
     alias node="env NODE_NO_READLINE=1 rlwrap node"
 
 
-### repl.start(prompt='> ', stream=process.openStdin())
+### repl.start(prompt='> ', stream=process.stdin)
 
 Starts a REPL with `prompt` as the prompt and `stream` for all I/O.  `prompt`
 is optional and defaults to `> `.  `stream` is optional and defaults to
-`process.openStdin()`.
+`process.stdin`.
 
 Multiple REPLs may be started against the same running instance of node.  Each
 will share the same global object but will have unique I/O.
index 6392eec..51540cc 100644 (file)
@@ -75,7 +75,8 @@ streams are kept in sync by pausing and resuming as necessary.
 
 Emulating the Unix `cat` command:
 
-    process.openStdin().pipe(process.stdout);
+    process.stdin.resume();
+    process.stdin.pipe(process.stdout);
 
 
 By default `end()` is called on the destination when the source stream emits
@@ -84,9 +85,13 @@ By default `end()` is called on the destination when the source stream emits
 
 This keeps `process.stdout` open so that "Goodbye" can be written at the end.
 
-    var stdin = process.openStdin();
-    stdin.pipe(process.stdout, { end: false });
-    stdin.on("end", function() { process.stdout.write("Goodbye\n"); });
+    process.stdin.resume();
+
+    process.stdin.pipe(process.stdout, { end: false });
+
+    process.stdin.on("end", function() {
+      process.stdout.write("Goodbye\n");
+    });
 
 NOTE: If the source stream does not support `pause()` and `resume()`, this function
 adds simple definitions which simply emit `'pause'` and `'resume'` events on
index 5bc5e46..1f910a2 100644 (file)
   };
 
 
-  var stdout;
+  var stdout, stdin;
+
+
   process.__defineGetter__('stdout', function() {
     if (stdout) return stdout;
 
     return stdout;
   });
 
-  var stdin;
-  process.openStdin = function() {
+
+  process.__defineGetter__('stdin', function() {
     if (stdin) return stdin;
 
     var binding = process.binding('stdio'),
       stdin.readable = true;
     }
 
-    stdin.resume();
-
     return stdin;
+  });
+
+
+  process.openStdin = function() {
+    process.stdin.resume();
+    return process.stdin;
   };