onstdin
v1.0.0
Published
Calls back on chuncks read via the standard input (stdin). (Synchroneously but non blocking)
Downloads
107
Readme
onstdin
Calls back on chuncks read via the standard input (stdin). (Synchroneously but non blocking)
Installation
$ npm install onstdinUsage
Example : Create two scripts : myScript.mjs and myOtherScript.mjs
// myScript.mjs
const options = { param1:'hello', param2:'world' }
console.log(JSON.stringify(options,null,2))// myOtherScript.mjs
import { onstdin } from "onstdin";
onstdin((content) => {
if(content) {
const { param1, param2 } = JSON.parse(content ? content : "{}");
console.log(`${param1} ${param2} onstdin`);
} else {
console.log("BOOM !");
}
});Execute a pipline via a node commande
$ node ./myScript.mjs | ./myOtherScript.mjsOutput
> hello world onstdinComplience
When nothing is passed through the stdin, a null value is passed. but the pipe is not blocked.
Example
$ node ./myScript.mjs | ./myOtherScript.mjs
> hello world onstdinExecuting the script without the first step will pass null in to myOtherScript.mjs.
$ node ./myOtherScript.mjs
> BOOM !Force blocking
When the -F or --force argument are passed the waiting for a chunk to be read to the end of a stream is enforced.
$ node ./myOtherScript.mjs -F
> API
onstdin(cb:() => void, enc:string = 'utf8'): void
| Parameters | description | | ---------- | ------------------------------------------------------ | | cb | the callback function | |enc| the encoding (utf8 by default)|
