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

barrage

v2.0.4

Published

Extensions to streams (as a mixin)

Downloads

17,179

Readme

barrage

NodeJS Streams with strong types via typescript, and helper methods to make dealing with object streams pleasent.

Build Status Dependency Status NPM version

Installation

npm install barrage

API

import {Readable, Writable, Transform} from 'barrage';

These are native streams, but with a few extensions:

readable.syphon(stream, [options])

This is exactly like the built in source.pipe(destination, [options]) except that it also forwards any errors emitted by source to the destination. When your streams represent transformations, that is usually much more useful than the built in .pipe.

readable.buffer([encoding]): Promise

When the barrage is a readable stream, this method buffers the results and handles errors, resulting in a node.js style callback API. If there is no encoding parameter, the callback is called with an Array for the result. If encoding is 'buffer' then the callback is called with a single Buffer for the result. If any other string is passed as encoding, the encoding parameter is passed on to buffer.toString(encoding) and the result is therefore a String

writable.wait(): Promise

This works like barrage.buffer, except that it does not buffer the result. It will wait for an end or finish event and then call the callback. If an error event is fired, the callback is called with that error. The callback is only ever called once.

If the callback parameter is absent, a Promises/A+ promise is returned instead.

readable.map(transform, options) / new barrage.Map(transform, options)

This passes each chunk to transform and then pushes the result of calling transform to the output stream. You can either call this as a method on an existing barrage stream, or create a Transform stream by calling new barrage.Map

e.g.

function square() {
  return new barrage.Map(function (x) {
    return x * x
  })
}

It supports both being asynchronous, and parallel:

function load() {
  return new barrage.Map(function (stat, callback) {
    fs.readFile(stat.fullPath, callback)
  }, {parallel: 10})
}

When operating in parallel, the ordering of the resulting stream is always preserved.

It also supports promises

function load() {
  return new barrage.Map(function (stat) {
    return Promise.denodeify(fs.readFile)(stat.fullPath)
  }, {parallel: 10})
}

readable.filter(transform, options) / new barrage.Filter(transform, options)

This is exactly like barrage.map / new barrage.Map except that transform should return true or false and the chunks will be filtered based on that value.

readable.flatMap(transform, options) / new barrage.FlatMap(transform, options)

Take a function that maps an object onto an array or stream (or if for...of is supported by your version of node, any iterable), then return a stream for those individual items. e.g.

var source = new b.Readable({objectMode: true});
source._read = function () {
  this.push(1)
  this.push(2)
  this.push(3)
  this.push(null)
};
source.flatMap(function (x) {
  var source = new b.Readable({objectMode: true})
  source._read = function () {
    this.push(x * 1) // 1, 2, 3
    this.push(x * 2) // 2, 4, 6
    this.push(null)
  }
  return source
}).buffer().done(function (data) {
    assert.deepEqual(data, [1, 2, 2, 4, 3, 6])
    done()
  })

readable.bufferTransform(transform, encoding) / new barrage.BufferTransform(transform, encoding)

Takes a function that transforms a string and returns a Transform stream. e.g.

function coffeify(filename) {
  return new barrage.BufferTransform(function (src) {
    return compileCoffee(filename, src)
  }, 'utf8')
}
function compileCoffee(filename, src) {
  //do compilation and return a string
}
fs.createReadStream('src.coffee').pipe(coffeify('src.coffee')).pipe(fs.createWriteStream('src.js'))

This is mostly useful for processing files over stdio and creating browserify transforms.

The transform function may optionally take a callback argument (if it returns undefined) or return a promise (instead of a string).

License

MIT