Browse Source

add process.stdin

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
2e3e95ee89
  1. 17
      doc/api/process.markdown
  2. 4
      doc/api/repl.markdown
  3. 13
      doc/api/streams.markdown
  4. 16
      src/node.js

17
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');
});

4
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.

13
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

16
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;
};

Loading…
Cancel
Save