├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | .nyc_output 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | - '5' 5 | - '4' 6 | 7 | after_script: 8 | - 'cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js' 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Observable = require('any-observable'); 3 | 4 | function or(option, alternate, required) { 5 | const result = option === false ? false : option || alternate; 6 | 7 | if ((required && !result) || (result && typeof result !== 'string')) { 8 | throw new TypeError(alternate + 'Event must be a string.'); 9 | } 10 | 11 | return result; 12 | } 13 | 14 | module.exports = (stream, opts) => { 15 | opts = opts || {}; 16 | 17 | let complete = false; 18 | let dataListeners = []; 19 | const awaited = opts.await; 20 | const dataEvent = or(opts.dataEvent, 'data', true); 21 | const errorEvent = or(opts.errorEvent, 'error'); 22 | const endEvent = or(opts.endEvent, 'end'); 23 | 24 | function cleanup() { 25 | complete = true; 26 | dataListeners.forEach(listener => { 27 | stream.removeListener(dataEvent, listener); 28 | }); 29 | dataListeners = null; 30 | } 31 | 32 | const completion = new Promise((resolve, reject) => { 33 | function onEnd(result) { 34 | if (awaited) { 35 | awaited.then(resolve); 36 | } else { 37 | resolve(result); 38 | } 39 | } 40 | 41 | if (endEvent) { 42 | stream.once(endEvent, onEnd); 43 | } else if (awaited) { 44 | onEnd(); 45 | } 46 | 47 | if (errorEvent) { 48 | stream.once(errorEvent, reject); 49 | } 50 | 51 | if (awaited) { 52 | awaited.catch(reject); 53 | } 54 | }).catch(err => { 55 | cleanup(); 56 | throw err; 57 | }).then(result => { 58 | cleanup(); 59 | return result; 60 | }); 61 | 62 | return new Observable(observer => { 63 | completion 64 | .then(observer.complete.bind(observer)) 65 | .catch(observer.error.bind(observer)); 66 | 67 | if (complete) { 68 | return null; 69 | } 70 | 71 | const onData = data => { 72 | observer.next(data); 73 | }; 74 | 75 | stream.on(dataEvent, onData); 76 | dataListeners.push(onData); 77 | 78 | return () => { 79 | stream.removeListener(dataEvent, onData); 80 | 81 | if (complete) { 82 | return; 83 | } 84 | 85 | const idx = dataListeners.indexOf(onData); 86 | 87 | if (idx !== -1) { 88 | dataListeners.splice(idx, 1); 89 | } 90 | }; 91 | }); 92 | }; 93 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) James Talmage (github.com/jamestalmage) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stream-to-observable", 3 | "version": "0.2.0", 4 | "description": "Convert Node Streams into ECMAScript-Observables", 5 | "license": "MIT", 6 | "repository": "jamestalmage/stream-to-observable", 7 | "author": { 8 | "name": "James Talmage", 9 | "email": "james@talmage.io", 10 | "url": "github.com/jamestalmage" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && nyc --reporter=lcov --reporter=text npm run test_both", 17 | "test_both": "ava --require=any-observable/register/rxjs && ava --require=any-observable/register/zen" 18 | }, 19 | "files": [ 20 | "index.js" 21 | ], 22 | "keywords": [ 23 | "stream", 24 | "observable", 25 | "convert" 26 | ], 27 | "dependencies": { 28 | "any-observable": "^0.2.0" 29 | }, 30 | "devDependencies": { 31 | "array-to-events": "^1.0.0", 32 | "ava": "^0.25.0", 33 | "coveralls": "^3.0.0", 34 | "delay": "^2.0.0", 35 | "nyc": "^11.7.1", 36 | "rxjs": "^5.5.10", 37 | "xo": "^0.20.3", 38 | "zen-observable": "^0.8.8" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 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`
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`
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`
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://github.com/tc39/proposal-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) 117 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {EventEmitter} from 'events'; 2 | import Observable from 'any-observable'; 3 | import ZenObservable from 'zen-observable'; 4 | import test from 'ava'; 5 | import delay from 'delay'; 6 | import arrayToEvents from 'array-to-events'; 7 | import m from '.'; 8 | 9 | const isZen = Observable === ZenObservable; 10 | const prefix = isZen ? 'zen' : 'rxjs'; 11 | 12 | function emitSequence(emitter, sequence, cb) { 13 | arrayToEvents(emitter, sequence, {delay: 'immediate', done: cb}); 14 | } 15 | 16 | function deferred() { 17 | let res; 18 | let rej; 19 | const p = new Promise((resolve, reject) => { 20 | res = resolve; 21 | rej = reject; 22 | }); 23 | p.resolve = res; 24 | p.reject = rej; 25 | return p; 26 | } 27 | 28 | function * expectations(...args) { 29 | yield * args; 30 | } 31 | 32 | test(`${prefix}: emits data events`, async t => { 33 | t.plan(2); 34 | const ee = new EventEmitter(); 35 | 36 | emitSequence(ee, [ 37 | ['data', 'foo'], 38 | ['data', 'bar'], 39 | ['end'], 40 | ['data', 'baz'] 41 | ]); 42 | 43 | const expected = expectations('foo', 'bar'); 44 | 45 | await m(ee).forEach(chunk => t.is(chunk, expected.next().value)); 46 | await delay(10); 47 | }); 48 | 49 | test(`${prefix}: forEach resolves after resolution of the awaited promise`, async t => { 50 | t.plan(3); 51 | const ee = new EventEmitter(); 52 | const awaited = deferred(); 53 | const expected = expectations('a', 'b'); 54 | 55 | emitSequence(ee, 56 | [ 57 | ['data', 'a'], 58 | ['data', 'b'] 59 | ], 60 | () => { 61 | awaited.resolve('resolution'); 62 | setImmediate(() => ee.emit('data', 'c')); 63 | } 64 | ); 65 | 66 | const result = await m(ee, {endEvent: false, await: awaited}).forEach(chunk => t.is(chunk, expected.next().value)); 67 | await delay(10); 68 | t.is(result, undefined); 69 | }); 70 | 71 | test(`${prefix}: rejects on error events`, async t => { 72 | t.plan(3); 73 | 74 | const ee = new EventEmitter(); 75 | 76 | emitSequence(ee, [ 77 | ['data', 'foo'], 78 | ['data', 'bar'], 79 | ['error', new Error('bar')], 80 | ['data', 'baz'], // Should be ignored 81 | ['alternate-error', new Error('baz')] 82 | ]); 83 | 84 | const expected = expectations('foo', 'bar'); 85 | 86 | await t.throws( 87 | m(ee).forEach(chunk => t.is(chunk, expected.next().value)), 88 | 'bar' 89 | ); 90 | await delay(10); 91 | }); 92 | 93 | test(`${prefix}: change the name of the error event`, async t => { 94 | t.plan(4); 95 | 96 | const ee = new EventEmitter(); 97 | 98 | emitSequence(ee, [ 99 | ['data', 'foo'], 100 | ['data', 'bar'], 101 | ['error', new Error('bar')], 102 | ['data', 'baz'], 103 | ['alternate-error', new Error('baz')] 104 | ]); 105 | 106 | // Emitter throws uncaught exceptions for `error` events that have no registered listener. 107 | // We just want to prove the implementation ignores the `error` event when an alternate `errorEvent` name is given. 108 | // You would likely never specify an alternate `errorEvent`, if `error` events are actually going to be emitted. 109 | ee.on('error', () => {}); 110 | 111 | const expected = expectations('foo', 'bar', 'baz'); 112 | 113 | await t.throws( 114 | m(ee, {errorEvent: 'alternate-error'}) 115 | .forEach(chunk => t.is(chunk, expected.next().value)), 116 | 'baz' 117 | ); 118 | }); 119 | 120 | test(`${prefix}: endEvent:false, and await:undefined means the Observable will never be resolved`, async t => { 121 | const ee = new EventEmitter(); 122 | 123 | emitSequence(ee, [ 124 | ['data', 'foo'], 125 | ['data', 'bar'], 126 | ['end'], 127 | ['false'] 128 | ]); 129 | 130 | let completed = false; 131 | 132 | m(ee, {endEvent: false}) 133 | .forEach(() => {}) 134 | .then(() => { 135 | completed = true; 136 | }); 137 | 138 | await delay(30); 139 | t.false(completed); 140 | }); 141 | 142 | test(`${prefix}: errorEvent can be disabled`, async t => { 143 | const ee = new EventEmitter(); 144 | 145 | emitSequence(ee, [ 146 | ['data', 'foo'], 147 | ['data', 'bar'], 148 | ['error', new Error('error')], 149 | ['false', new Error('bar')], 150 | ['end'] 151 | ]); 152 | 153 | ee.on('error', () => {}); 154 | 155 | await t.notThrows(m(ee, {errorEvent: false})); 156 | }); 157 | 158 | test(`${prefix}: protects against improper arguments`, t => { 159 | t.throws(() => m(new EventEmitter(), {errorEvent: 3}), /errorEvent/); 160 | t.throws(() => m(new EventEmitter(), {endEvent: 3}), /endEvent/); 161 | t.throws(() => m(new EventEmitter(), {dataEvent: false}), /dataEvent/); 162 | }); 163 | 164 | test(`${prefix}: listeners are cleaned up on completion, and no further listeners will be added.`, async t => { 165 | t.plan(5); 166 | 167 | const ee = new EventEmitter(); 168 | t.is(ee.listenerCount('data'), 0); 169 | 170 | const observable = m(ee); 171 | 172 | observable.forEach(() => {}); 173 | t.is(ee.listenerCount('data'), 1); 174 | 175 | observable.forEach(() => {}); 176 | t.is(ee.listenerCount('data'), 2); 177 | 178 | emitSequence(ee, [ 179 | ['data', 'foo'], 180 | ['data', 'bar'], 181 | ['end'] 182 | ]); 183 | 184 | await observable.forEach(() => {}); 185 | 186 | t.is(ee.listenerCount('data'), 0); 187 | 188 | const onEvent = () => { 189 | t.fail('should not have added more listeners'); 190 | }; 191 | 192 | ee.on = onEvent; 193 | ee.once = onEvent; 194 | 195 | observable.forEach(() => {}); 196 | t.is(ee.listenerCount('data'), 0); 197 | 198 | await observable.forEach(() => {}); 199 | }); 200 | 201 | test(`${prefix}: unsubscribing reduces the listener count`, t => { 202 | const ee = new EventEmitter(); 203 | t.is(ee.listenerCount('data'), 0); 204 | 205 | const observable = m(ee); 206 | 207 | const subscription = observable.subscribe({}); 208 | 209 | t.is(ee.listenerCount('data'), 1); 210 | 211 | subscription.unsubscribe(); 212 | 213 | t.is(ee.listenerCount('data'), 0); 214 | }); 215 | --------------------------------------------------------------------------------