Import Upstream version 0.2.0

parents
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2
* text=auto
*.js text eol=lf
node_modules
coverage
.nyc_output
language: node_js
node_js:
- '6'
- '5'
- '4'
- '0.12'
after_script:
- 'cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js'
'use strict';
var Observable = require('any-observable');
function or(option, alternate, required) {
var result = option === false ? false : option || alternate;
if ((required && !result) || (result && typeof result !== 'string')) {
throw new TypeError(alternate + 'Event must be a string.');
}
return result;
}
module.exports = function (stream, opts) {
opts = opts || {};
var complete = false;
var dataListeners = [];
var awaited = opts.await;
var dataEvent = or(opts.dataEvent, 'data', true);
var errorEvent = or(opts.errorEvent, 'error');
var endEvent = or(opts.endEvent, 'end');
function cleanup() {
complete = true;
dataListeners.forEach(function (listener) {
stream.removeListener(dataEvent, listener);
});
dataListeners = null;
}
var completion = new Promise(function (resolve, reject) {
function onEnd(result) {
if (awaited) {
awaited.then(resolve);
} else {
resolve(result);
}
}
if (endEvent) {
stream.once(endEvent, onEnd);
} else if (awaited) {
onEnd();
}
if (errorEvent) {
stream.once(errorEvent, reject);
}
if (awaited) {
awaited.catch(reject);
}
}).catch(function (err) {
cleanup();
throw err;
}).then(function (result) {
cleanup();
return result;
});
return new Observable(function (observer) {
completion
.then(observer.complete.bind(observer))
.catch(observer.error.bind(observer));
if (complete) {
return null;
}
var onData = function onData(data) {
observer.next(data);
};
stream.on(dataEvent, onData);
dataListeners.push(onData);
return function () {
stream.removeListener(dataEvent, onData);
if (complete) {
return;
}
var idx = dataListeners.indexOf(onData);
if (idx !== -1) {
dataListeners.splice(idx, 1);
}
};
});
};
The MIT License (MIT)
Copyright (c) James Talmage <james@talmage.io> (github.com/jamestalmage)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "stream-to-observable",
"version": "0.2.0",
"description": "Convert Node Streams into ECMAScript-Observables",
"license": "MIT",
"repository": "jamestalmage/stream-to-observable",
"author": {
"name": "James Talmage",
"email": "james@talmage.io",
"url": "github.com/jamestalmage"
},
"engines": {
"node": ">=0.12.0"
},
"scripts": {
"test": "xo && nyc --reporter=lcov --reporter=text npm run test_both",
"test_both": "ava --require=any-observable/register/rxjs && ava --require=any-observable/register/zen"
},
"files": [
"index.js",
"zen.js",
"rxjs.js",
"rxjs-all.js"
],
"keywords": [
"stream",
"observable",
"convert"
],
"dependencies": {
"any-observable": "^0.2.0"
},
"devDependencies": {
"array-to-events": "^1.0.0",
"ava": "^0.15.2",
"coveralls": "^2.11.9",
"delay": "^1.3.1",
"nyc": "^6.4.0",
"rxjs": "^5.0.0-beta.6",
"xo": "^0.16.0",
"zen-observable": "^0.3.0"
},
"xo": {
"overrides": [
{
"files": [
"test.js"
],
"esnext": true
}
]
}
}
# 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)
> Convert Node Streams into ECMAScript-Observables
[`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.
[Learn more about Observables](#learn-about-observables)
## Install
```
$ npm install --save stream-to-observable
```
`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:
```
$ npm install --save zen-observable
```
or
```
$ npm install --save rxjs
```
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).
## Usage
```js
const fs = require('fs');
const split = require('split');
const streamToObservable = require('stream-to-observable');
const readStream = fs
.createReadStream('./hello-world.txt', {encoding: 'utf8'})
.pipe(split());
streamToObservable(readStream)
.filter(chunk => /hello/i.test(chunk))
.map(chunk => chunk.toUpperCase())
.forEach(chunk => {
console.log(chunk); // only the lines containing "hello" - and they will be capitalized
});
```
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.
## API
### streamToObservable(stream, [options])
#### stream
Type: [`ReadableStream`](https://nodejs.org/api/stream.html#stream_class_stream_readable)
*Note:*
`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.
#### options
##### await
Type: `Promise`
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.
##### endEvent
Type: `String` or `false` <br>
Default: `"end"`
If you are using an `EventEmitter` or non-standard Stream, you can change which event signals that the Observable should be completed.
Setting this to `false` will avoid listening for any end events.
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).
##### errorEvent
Type: `String` or `false` <br>
Default: `"error"`
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.
Setting this to `false` will avoid listening for any error events.
##### dataEvent
Type: `String`<br>
Default: `"data"`
If you are using an `EventEmitter` or non-standard Stream, you can change which event causes data to be emitted to the Observable.
## Learn about Observables
- [Overview](https://github.com/zenparsing/es-observable)
- [Formal Spec](https://zenparsing.github.io/es-observable/)
- [egghead.io lesson](https://egghead.io/lessons/javascript-introducing-the-observable) - Video
- [`rxjs` observables](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) - Observables implementation
- [`zen-observables`](https://github.com/zenparsing/zen-observable) - Observables implementation
## Transform Streams
`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.
## Caveats
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.
## License
MIT © [James Talmage](https://github.com/jamestalmage)
import {EventEmitter} from 'events';
import Observable from 'any-observable';
import ZenObservable from 'zen-observable';
import test from 'ava';
import delay from 'delay';
import arrayToEvents from 'array-to-events';
import m from './';
const isZen = Observable === ZenObservable;
const prefix = isZen ? 'zen' : 'rxjs';
function emitSequence(emitter, sequence, cb) {
arrayToEvents(emitter, sequence, {delay: 'immediate', done: cb});
}
// avoid deprecation warnings
// TODO: https://github.com/jden/node-listenercount/pull/1
function listenerCount(emitter, eventName) {
if (emitter.listenerCount) {
return emitter.listenerCount(eventName);
}
return EventEmitter.listenerCount(emitter, eventName);
}
function deferred() {
let res;
let rej;
const p = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
p.resolve = res;
p.reject = rej;
return p;
}
function * expectations(...args) {
yield * args;
}
test(`${prefix}: emits data events`, t => {
t.plan(2);
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['end'],
['data', 'baz']
]);
const expected = expectations('foo', 'bar');
return m(ee)
.forEach(chunk => t.is(chunk, expected.next().value))
.then(delay(10));
});
// RxJs and Zen-Observable do not honor the same contract - RxJs resolves to null.
if (isZen) {
test(`${prefix}: forEach resolves with the value passed to the "end" event`, async t => {
t.plan(1);
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['end', 'fin']
]);
const result = await m(ee).forEach(() => {});
t.is(result, 'fin');
});
}
test(`${prefix}: forEach resolves after resolution of the awaited promise${isZen ? ', with promise value' : ''}`, async t => {
t.plan(3);
const ee = new EventEmitter();
const awaited = deferred();
const expected = expectations('a', 'b');
emitSequence(ee,
[
['data', 'a'],
['data', 'b']
],
() => {
awaited.resolve('resolution');
setImmediate(() => ee.emit('data', 'c'));
}
);
const result =
await m(ee, {endEvent: false, await: awaited})
.forEach(chunk => t.is(chunk, expected.next().value));
await delay(10);
t.is(result, isZen ? 'resolution' : undefined);
});
test(`${prefix}: rejects on error events`, t => {
t.plan(3);
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['error', new Error('bar')],
['data', 'baz'], // should be ignored
['alternate-error', new Error('baz')]
]);
const expected = expectations('foo', 'bar');
return t.throws(
m(ee).forEach(chunk => t.is(chunk, expected.next().value)),
'bar'
).then(delay(10));
});
test(`${prefix}: change the name of the error event`, t => {
t.plan(4);
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['error', new Error('bar')],
['data', 'baz'],
['alternate-error', new Error('baz')]
]);
// Emitter throws uncaught exceptions for `error` events that have no registered listener.
// We just want to prove the implementation ignores the `error` event when an alternate `errorEvent` name is given.
// You would likely never specify an alternate `errorEvent`, if `error` events are actually going to be emitted.
ee.on('error', () => {});
const expected = expectations('foo', 'bar', 'baz');
return t.throws(
m(ee, {errorEvent: 'alternate-error'})
.forEach(chunk => t.is(chunk, expected.next().value)),
'baz'
);
});
test(`${prefix}: endEvent:false, and await:undefined means the Observable will never be resolved`, t => {
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['end'],
['false']
]);
let completed = false;
m(ee, {endEvent: false})
.forEach(() => {})
.then(() => {
completed = true;
});
return delay(30).then(() => t.false(completed));
});
test(`${prefix}: errorEvent can be disabled`, () => {
const ee = new EventEmitter();
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['error', new Error('error')],
['false', new Error('bar')],
['end']
]);
ee.on('error', () => {});
return m(ee, {errorEvent: false});
});
test(`${prefix}: protects against improper arguments`, t => {
t.throws(() => m(new EventEmitter(), {errorEvent: 3}), /errorEvent/);
t.throws(() => m(new EventEmitter(), {endEvent: 3}), /endEvent/);
t.throws(() => m(new EventEmitter(), {dataEvent: false}), /dataEvent/);
});
test(`${prefix}: listeners are cleaned up on completion, and no further listeners will be added.`, t => {
t.plan(5);
const ee = new EventEmitter();
t.is(listenerCount(ee, 'data'), 0);
const observable = m(ee);
observable.forEach(() => {});
t.is(listenerCount(ee, 'data'), 1);
observable.forEach(() => {});
t.is(listenerCount(ee, 'data'), 2);
emitSequence(ee, [
['data', 'foo'],
['data', 'bar'],
['end']
]);
return observable
.forEach(() => {})
.then(() => {
t.is(listenerCount(ee, 'data'), 0);
ee.on = ee.once = function () {
t.fail('should not have added more listeners');
};
observable.forEach(() => {});
t.is(listenerCount(ee, 'data'), 0);
return observable.forEach(() => {});
});
});
test(`${prefix}: unsubscribing reduces the listener count`, async t => {
const ee = new EventEmitter();
t.is(listenerCount(ee, 'data'), 0);
const observable = m(ee);
const subscription = observable.subscribe({});
t.is(listenerCount(ee, 'data'), 1);
subscription.unsubscribe();
t.is(listenerCount(ee, 'data'), 0);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment