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

co-stream

v0.3.2

Published

[Streams](http://nodejs.org/api/stream.html "Stream") are node's best and most misunderstood idea, and _<em>co-stream</em>_ is a toolkit to make creating and working with streams <em>easy</em>.

Downloads

370

Readme

#co-stream Streams are node's best and most misunderstood idea, and co-stream is a toolkit to make creating and working with streams easy.

This package is very similar to event-stream, the difference is this package is in co style, so we can write async stream processing code in a nice way.

Installation

npm install co-stream

wait(stream)

Wait for stream 'end'/'finish' event;

co(function *() {
  // blabla
  yield* cs.wait(your_stream);
});

through (write?, end?)

Re-emits data synchronously. Easy way to create synchronous through streams. Pass in optional write and end methods. They will be called in the context of the stream. Use this.pause() and this.resume() to manage flow. Check this.paused to see current flow state. (write always returns !this.paused)

this function is the basis for most of the synchronous streams in event-stream.

cs.through(function write(data) {
    this.emit('data', data)
    //this.pause() 
  },
  function end () { //optional
    this.emit('end')
  })

fromEmitter(eventEmitter, opt)

Create a stream from an event emitter.

var cs = require('co-stream');

var es = cs.fromEmitter(evt, {
            objectMode: true,
            data: 'message',     // event name for data, default 'data'
            end: 'finish',       // event name for end, default 'end'
            pause: evt.stop,     // method to pause event emitter, default evt.pause || function () {}
            resume: evt.start });// method to resume event emiiter, default evt.resume || function () {}

es.pipe(process.stdout);

// cs.object.fromEmitter (cs.fromEmitter with default opt { objectMode: true })
// cs.string.fromEmitter (cs.fromEmitter with default opt { encoding: 'utf8' })

fromIterable(iterable)

Create a stream from an iterable object.

var cs = require('co-stream');

cs.fromIterable([1, 2, 3, 4, 5, 6, 7, 8]).pipe(cs.object.each(console.log));

// Promise is also supported.
const promise = new Promise(resolve => setTimeout(() => resolve(['a', 'b', 'c']), 1000));
cs.fromIterable(promise).pipe(cs.object.each(console.log));

map

Create a map stream from a function(can be generator / async function).

var cs = require('co-stream')

var ms = cs.map(function *(data) {
  //transform data
  return transformed_data;
}, { objectMode: true, parallel: 3 });

your_source_stream.pipe(ms).pipe(your_dest_stream);

// parallel param will allow you to process data parallelly, but the sequence of data may be changed if you have async call in the processor.
// cs.object.map (cs.map with default opt { objectMode: true })
// cs.string.map (cs.map with default opt { encoding: 'utf8' })

filter

Create a filter stream from a function(can be generator / async function).

var cs = require('co-stream')

var fs = cs.filter(async (data) => {
  // async data processing.
  return processingResult;
}, { objectMode: true, parallel: 3 });

your_source_stream.pipe(fs).pipe(your_dest_stream);

// parallel param will allow you to process data parallelly, but the sequence of data may be changed if you have async call in the processor.
// cs.object.filter (cs.filter with default opt { objectMode: true })
// cs.string.filter (cs.filter with default opt { encoding: 'utf8' })

each

If you just want to process data without any data to output, use this.

var cs = require('co-stream')

var ms = cs.each(function *(data) {
  // process data
}, { objectMode: true, parallel: 3 });

your_source_stream.pipe(ms);
// NOTE: ms.pipe is invalid.

// parallel param will allow you to process data parallelly.
// cs.object.each (cs.each with default opt { objectMode: true })
// cs.string.each (cs.each with default opt { encoding: 'utf8' })

split (matcher)

Break up a stream and reassemble it so that each line is a chunk. matcher may be a String, or a RegExp

Example, read every line in a file ...

fs.createReadStream(file, {flags: 'r'})
  .pipe(cs.split())
  .pipe(cs.object.each(function *(line) {
    //do something with the line 
    console.log('line: ', line);
  }))

duplex (writeStream, readStream)

Takes a writable stream and a readable stream and makes them appear as a readable writable stream.

It is assumed that the two streams are connected to each other in some way.

(This is used by pipeline and child.)

  var grep = cp.exec('grep Stream')

  cs.duplex(grep.stdin, grep.stdout)

Legacy API

var cs = require('co-stream'),
    co = require('co'),
    fs = require('fs');

co(function *() {
  var input = fs.createReadStream('test.in'),
      output = fs.createWriteStream('test.out');
      
  var cin = new (cs.Reader)(input), // new (cs.LineReader)(input) will create a line reader.
      cout = new (cs.Writer)(output);
      
  var txt;
  while (txt = yield cin.read('utf8')) {
    yield cout.write(txt); // or: yield cout.writeLine(txt)
  }
})();

// Object mode can process object streams such as mongodb stream.
co(function *() {
    var objstream = collection.find({ age: { $gte: 10, $lt: 15 } }).stream(),
        reader = new (cs.Reader)(objstream, { objectMode: true });

    var obj;
    while (obj = (yield  reader.read())) {
        // blabla....
    }
})();