From 2e3e95ee89045966dbd6bf955c319ab448c1c85e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 1 Jan 2011 21:54:46 -0800 Subject: [PATCH] add process.stdin --- doc/api/process.markdown | 17 +++++++++-------- doc/api/repl.markdown | 4 ++-- doc/api/streams.markdown | 13 +++++++++---- src/node.js | 16 +++++++++++----- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/doc/api/process.markdown b/doc/api/process.markdown index 5fee9a16da..3383d59a9f 100644 --- a/doc/api/process.markdown +++ b/doc/api/process.markdown @@ -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'); }); diff --git a/doc/api/repl.markdown b/doc/api/repl.markdown index 8ae3623a48..1bdb4f2fcb 100644 --- a/doc/api/repl.markdown +++ b/doc/api/repl.markdown @@ -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. diff --git a/doc/api/streams.markdown b/doc/api/streams.markdown index 6392eec7bb..51540cc1c0 100644 --- a/doc/api/streams.markdown +++ b/doc/api/streams.markdown @@ -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 diff --git a/src/node.js b/src/node.js index 5bc5e46357..1f910a2a3b 100644 --- a/src/node.js +++ b/src/node.js @@ -464,7 +464,9 @@ }; - var stdout; + var stdout, stdin; + + process.__defineGetter__('stdout', function() { if (stdout) return stdout; @@ -487,8 +489,8 @@ return stdout; }); - var stdin; - process.openStdin = function() { + + process.__defineGetter__('stdin', function() { if (stdin) return stdin; var binding = process.binding('stdio'), @@ -503,9 +505,13 @@ stdin.readable = true; } - stdin.resume(); - return stdin; + }); + + + process.openStdin = function() { + process.stdin.resume(); + return process.stdin; };