0.2.2 • Published 5 months ago

@stdlib/streams-node-stdin v0.2.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Standard Input

NPM version Build Status Coverage Status

Standard input.

Installation

npm install @stdlib/streams-node-stdin

Usage

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

stdin

Standard input as a Readable stream.

var string2buffer = require( '@stdlib/buffer-from-string' );
var Buffer = require( '@stdlib/buffer-ctor' );

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;
        }
        if ( typeof chunk === 'string' ) {
            chunk = string2buffer( chunk );
        }
        data.push( chunk );
        len += chunk.length;
    }
}

function onError( error ) {
    if ( error ) {
        console.error( error.message );
    }
}

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

Examples

var proc = require( 'process' );
var stdin = require( '@stdlib/streams-node-stdin' );
var stdout = require( '@stdlib/streams-node-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( proc.exit, 1000 );

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.

@infinitebrahmanuniverse/nolb-_stdlib_str@everything-registry/sub-chunk-870@stdlib/string-code-point-at-cli@stdlib/utils-timeit-cli@stdlib/streams-node@stdlib/assert-is-current-year-cli@stdlib/assert-is-digit-string-cli@stdlib/assert-is-domain-name-cli@stdlib/assert-is-kebabcase-cli@stdlib/assert-is-leap-year-cli@stdlib/assert-is-snakecase-cli@stdlib/assert-is-startcase-cli@stdlib/assert-is-node-builtin-cli@stdlib/assert-is-lowercase-cli@stdlib/assert-is-whitespace-cli@stdlib/assert-is-duration-string-cli@stdlib/assert-is-email-address-cli@stdlib/repl-server@stdlib/assert-is-absolute-http-uri-cli@stdlib/assert-is-absolute-path-cli@stdlib/assert-is-absolute-uri-cli@stdlib/assert-is-ascii-cli@stdlib/assert-is-constantcase-cli@stdlib/assert-is-alphagram-cli@stdlib/assert-is-alphanumeric-cli@stdlib/assert-is-anagram-cli@stdlib/assert-is-binary-string-cli@stdlib/assert-is-blank-string-cli@stdlib/assert-is-camelcase-cli@stdlib/assert-is-capitalized-cli@stdlib/assert-has-utf16-surrogate-pair-at-cli@stdlib/utils-convert-path-cli@stdlib/string-replace-cli@stdlib/string-reverse-cli@stdlib/string-right-pad-cli@stdlib/string-right-trim-cli@stdlib/string-right-trim-n-cli@stdlib/string-snakecase-cli@stdlib/string-startcase-cli@stdlib/string-starts-with-cli@stdlib/string-truncate-cli@stdlib/string-truncate-middle-cli@stdlib/string-uncapitalize-cli@stdlib/string-uppercase-cli@stdlib/string-substring-after-cli@stdlib/string-substring-after-last-cli@stdlib/string-substring-before-cli@stdlib/string-substring-before-last-cli@stdlib/string-trim-cli@stdlib/string-num-grapheme-clusters-cli@stdlib/string-num2words-cli@stdlib/string-pad-cli@stdlib/string-pascalcase-cli@stdlib/string-percent-encode-cli@stdlib/string-prev-grapheme-cluster-break-cli@stdlib/time-ms2duration-cli@stdlib/time-duration2ms-cli@stdlib/string-remove-first-cli@stdlib/string-remove-last-cli@stdlib/string-remove-punctuation-cli@stdlib/string-remove-utf8-bom-cli@stdlib/string-remove-words-cli@stdlib/string-repeat-cli@stdlib/string-replace-before-cli@stdlib/process-read-stdin@stdlib/assert-is-unc-path-cli@stdlib/assert-is-uppercase-cli@stdlib/assert-is-uri-cli@stdlib/assert-is-hex-string-cli@stdlib/assert-is-semver-cli@stdlib/assert-is-pascalcase-cli@stdlib/assert-is-regexp-string-cli@stdlib/assert-is-relative-path-cli@stdlib/assert-is-relative-uri-cli@stdlib/string-acronym-cli@stdlib/string-dotcase-cli@stdlib/string-ends-with-cli@stdlib/string-first-cli@stdlib/string-from-code-point-cli@stdlib/string-headercase-cli@stdlib/string-kebabcase-cli@stdlib/string-last-cli@stdlib/string-left-pad-cli@stdlib/string-left-trim-cli@stdlib/string-left-trim-n-cli@stdlib/string-lowercase-cli@stdlib/string-next-code-point-index-cli@stdlib/string-next-grapheme-cluster-break-cli@stdlib/string-camelcase-cli@stdlib/string-constantcase-cli@stdlib/string-capitalize-cli@npmtuanmap2024/accusamus-doloremque-quos@zalastax/nolb-_stdlib_str
0.2.2

5 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.0

1 year ago

0.1.1

1 year ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.1-alpha.2

4 years ago

0.0.1-alpha.1

4 years ago

0.0.1-alpha.0

4 years ago