https://github.com/zenflow/stream-line-reader
Read through a stream of lines on-demand
https://github.com/zenflow/stream-line-reader
Last synced: about 1 year ago
JSON representation
Read through a stream of lines on-demand
- Host: GitHub
- URL: https://github.com/zenflow/stream-line-reader
- Owner: zenflow
- Created: 2020-11-13T19:06:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T20:10:25.000Z (over 5 years ago)
- Last Synced: 2024-04-24T06:03:10.328Z (about 2 years ago)
- Language: TypeScript
- Size: 75.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# stream-line-reader
Read through a stream of lines on-demand
[](http://npmjs.com/package/stream-line-reader)
[](https://github.com/zenflow/stream-line-reader/actions?query=branch%3Amaster)
[](https://david-dm.org/zenflow/stream-line-reader)
[](https://codeclimate.com/github/zenflow/stream-line-reader)
[](https://snyk.io/test/github/zenflow/stream-line-reader?targetFile=package.json)
[](https://opensource.org/licenses/MIT)
## Installation
```
$ npm install stream-line-reader
```
## Usage
```js
const { spawn } = require('child_process');
const { StreamLineReader } = require('stream-line-reader');
async function example () {
const child = spawn('my-calculator-program');
const lines = new StreamLineReader(child.stdout);
console.log(await lines.readUntil('Done startup'));
// -> ['Starting...', 'Done startup']
child.stdin.write('1+2\n');
console.log(await lines.readUntil(/^Answer:/));
// -> ['Question: 1+2', 'Calculating...', 'Answer: 3']
child.stdin.write('3+4\n');
console.log(await lines.readUntil(line => line.startsWith('Answer:')));
// -> ['Question: 3+4', 'Calculating...', 'Answer: 7']
child.stdin.end();
console.log(await lines.readRemaining());
// -> ['Bye!', '']
}
```
Instance can be created with a single stream, or an array of streams,
in which case they are merged (interleaved), to be read as one stream.
If during a call to `.readUntil()` the stream ends without emitting a matching line,
an error will be raised with details for debugging.
Instances also have a `.readBuffered()` method for when some time has passed
and you just want to read whatever lines have become available.
It takes no arguments and returns synchronously an array of lines.