npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@teppeis/stream-to-observable

v0.3.1

Published

Convert Node Streams into ECMAScript-Observables

Downloads

941

Readme

This is a fork of SamVerschueren/stream-to-observable.

stream-to-observable Build Status Coverage Status

Convert Node Streams into ECMAScript-Observables

Observables 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

Note: This module was forked from stream-to-observable and released under a different name due to inactivity.

Install

$ npm install --save @teppeis/stream-to-observable

stream-to-observable relies on 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-observables registration shortcuts.

Usage

const fs = require('fs');
const split = require('split');

const streamToObservable = require('@teppeis/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 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

Note: stream can technically be any 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. 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 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 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 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

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 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