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