Debian Codegraph @

readme.md

raw
1 # stream-to-observable [![Build Status](https://travis-ci.org/jamestalmage/stream-to-observable.svg?branch=master)](https://travis-ci.org/jamestalmage/stream-to-observable) [![Coverage Status](https://coveralls.io/repos/github/jamestalmage/stream-to-observable/badge.svg?branch=master)](https://coveralls.io/github/jamestalmage/stream-to-observable?branch=master)
2
3 > Convert Node Streams into ECMAScript-Observables
4
5 [`Observables`](https://github.com/zenparsing/es-observable) are rapidly gaining popularity. They have much in common with Streams, in that they both represent data that arrives over time. Most Observable implementations provide expressive methods for filtering and mutating incoming data. Methods like `.map()`, `.filter()`, and `.forEach` behave very similarly to their Array counterparts, so using Observables can be very intuitive.
6
7 [Learn more about Observables](#learn-about-observables)
8
9 ## Install
10
11 ```
12 $ npm install --save stream-to-observable
13
14 ```
15
16 `stream-to-observable` relies on [`any-observable`](https://github.com/sindresorhus/any-observable), which will search for an available Observable implementation. You need to install one yourself:
17
18 ```
19 $ npm install --save zen-observable
20 ```
21
22 or
23
24 ```
25 $ npm install --save rxjs
26 ```
27
28 If your code relies on a specific Observable implementation, you should likely specify one using `any-observable`s [registration shortcuts](https://github.com/sindresorhus/any-observable#registration-shortcuts).
29
30 ## Usage
31
32 ```js
33 const fs = require('fs');
34 const split = require('split');
35
36 const streamToObservable = require('stream-to-observable');
37
38 const readStream = fs
39 .createReadStream('./hello-world.txt', {encoding: 'utf8'})
40 .pipe(split());
41
42 streamToObservable(readStream)
43 .filter(chunk => /hello/i.test(chunk))
44 .map(chunk => chunk.toUpperCase())
45 .forEach(chunk => {
46 console.log(chunk); // only the lines containing "hello" - and they will be capitalized
47 });
48 ```
49
50 The [`split`](https://github.com/dominictarr/split) module above will chunk the stream into individual lines. This is often very handy for text streams, as each observable event is guaranteed to be a line.
51
52 ## API
53
54 ### streamToObservable(stream, [options])
55
56 #### stream
57
58 Type: [`ReadableStream`](https://nodejs.org/api/stream.html#stream_class_stream_readable)
59
60 *Note:*
61 `stream` can technically be any [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) instance. By default, this module listens to the standard Stream events (`data`, `error`, and `end`), but those are configurable via the `options` parameter. If you are using this with a standard Stream, you likely won't need the `options` parameter.
62
63 #### options
64
65 ##### await
66
67 Type: `Promise`
68
69 If provided, the Observable will not "complete" until `await` is resolved. If `await` is rejected, the Observable will immediately emit an `error` event and disconnect from the stream. This is mostly useful when attaching to the `stdin` or `stdout` streams of a [`child_process`](https://nodejs.org/api/child_process.html#child_process_child_stdio). Those streams usually do not emit `error` events, even if the underlying process exits with an error. This provides a means to reject the Observable if the child process exits with an unexpected error code.
70
71 ##### endEvent
72
73 Type: `String` or `false` <br>
74 Default: `"end"`
75
76 If you are using an `EventEmitter` or non-standard Stream, you can change which event signals that the Observable should be completed.
77
78 Setting this to `false` will avoid listening for any end events.
79
80 Setting this to `false` and providing an `await` Promise will cause the Observable to resolve immediately with the `await` Promise (the Observable will remove all it's `data` event listeners from the stream once the Promise is resolved).
81
82 ##### errorEvent
83
84 Type: `String` or `false` <br>
85 Default: `"error"`
86
87 If you are using an `EventEmitter` or non-standard Stream, you can change which event signals that the Observable should be closed with an error.
88
89 Setting this to `false` will avoid listening for any error events.
90
91 ##### dataEvent
92
93 Type: `String`<br>
94 Default: `"data"`
95
96 If you are using an `EventEmitter` or non-standard Stream, you can change which event causes data to be emitted to the Observable.
97
98 ## Learn about Observables
99
100 - [Overview](https://github.com/zenparsing/es-observable)
101 - [Formal Spec](https://zenparsing.github.io/es-observable/)
102 - [egghead.io lesson](https://egghead.io/lessons/javascript-introducing-the-observable) - Video
103 - [`rxjs` observables](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) - Observables implementation
104 - [`zen-observables`](https://github.com/zenparsing/zen-observable) - Observables implementation
105
106 ## Transform Streams
107
108 `data` events on the stream will be emitted as events in the Observable. Since most native streams emit `chunks` of binary data, you will likely want to use a `TransformStream` to convert those chunks of binary data into an object stream. [`split`](https://github.com/dominictarr/split) is just one popular TransformStream that splits streams into individual lines of text.
109
110 ## Caveats
111
112 It's important to note that using this module disables back-pressure controls on the stream. As such, it should not be used where back-pressure throttling is required (i.e. high volume web servers). It still has value for larger projects, as it can make unit testing streams much cleaner.
113
114 ## License
115
116 MIT © [James Talmage](https://github.com/jamestalmage)