Standard Input

Standard input.

Usage

var stdin = require( '@stdlib/streams/base/stdin' );

stdin

Standard input as a Readable stream.

var data = [];
var len = 0;

stdin.on( 'readable', onReadable );
stdin.on( 'error', onError );
stdin.on( 'end', onEnd );

function onReadable() {
    var chunk;
    while ( true ) {
        chunk = stdin.read();
        if ( chunk === null ) {
            break;
        }
        data.push( chunk );
        len += chunk.length;
    }
}

function onError( error ) {
    if ( error ) {
        throw error;
    }
}

function onEnd() {
    data = Buffer.concat( data, len );
    console.log( data.toString() );
    // returns '...'
}

Examples

var stdin = require( '@stdlib/streams/base/stdin' );
var stdout = require( '@stdlib/streams/base/stdout' );

// Set the encoding:
stdin.setEncoding( 'utf8' );

// Create an echo stream:
stdin.pipe( stdout );

// Push data to `stdin`:
stdin.push( 'beep' );
stdin.push( ' ' );
stdin.push( 'boop' );
stdin.push( '\n' );

// End the stream:
stdin.push( null );

// Ensure the process closes:
setTimeout( process.exit, 1000 );