0.2.2 • Published 4 months ago

@stdlib/process-read-stdin v0.2.2

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

stdin

NPM version Build Status Coverage Status

Read data from stdin.

Installation

npm install @stdlib/process-read-stdin

Usage

var stdin = require( '@stdlib/process-read-stdin' );

stdin( [encoding,] clbk )

Reads data from stdin.

function onRead( error, data ) {
    if ( error ) {
        return console.error( 'Error: %s', error.message );
    }
    console.log( data.toString() );
    // => '...'
}

stdin( onRead );

By default, returned data is a Buffer. To return a string of a specified encoding, provide an encoding parameter.

function onRead( error, data ) {
    if ( error ) {
        return console.error( 'Error: %s', error.message );
    }
    console.log( data );
    // => '...'
}

stdin( 'utf8', onRead );

When a file's calling Node.js process is running in a TTY context (i.e., no stdin), data will either be an empty Buffer (no encoding provided) or an empty string (encoding provided).

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

function onRead( error, data ) {
    if ( error ) {
        return console.error( 'Error: %s', error.message );
    }
    console.log( data );
    // => ''
}

stream.isTTY = true;

stdin( 'utf8', onRead );

Examples

var string2buffer = require( '@stdlib/buffer-from-string' );
var stream = require( '@stdlib/streams-node-stdin' );
var stdin = require( '@stdlib/process-read-stdin' );

function onRead( error, data ) {
    if ( error ) {
        console.error( 'Error: %s', error.message );
    } else {
        console.log( data.toString() );
        // => 'beep boop'
    }
}

// Fake not being in a terminal context:
stream.isTTY = false;

// Provide a callback to consume all data from `stdin`:
stdin( onRead );

// Push some data to `stdin`:
stream.push( string2buffer( 'beep' ) );
stream.push( string2buffer( ' ' ) );
stream.push( string2buffer( 'boop' ) );

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

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_p@everything-registry/sub-chunk-869@stdlib/string-code-point-at-cli@stdlib/utils-timeit-cli@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/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/fs-write-file-cli@stdlib/fs-append-file-cli@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
0.2.2

4 months ago

0.2.1

9 months ago

0.2.0

9 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

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago