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

fluido

v2.1.3

Published

A drop in replacement for stream with Promise support and more!

Downloads

194

Readme

fluido

npm version Dependencies Status ci JavaScript Style Guide

Fluido is a drop-in replacement for the native stream module. It adds some functions that aren't included in the standard module and adds Promise support to stream methods. It also enables concurrent jobs while writing or transforming.

Features

  • Promise: you can use async/await inside stream methods, and functions without a callback will return a Promise.
  • Concurrency: a concurrency option available for Writable and Transform streams.
  • ESM: support native ESM (import and export keywords).
  • CommonJS: support old Node.js runtimes (require).
  • TypeScript: types declaration are included.

Install

npm install --save fluido

Usage

Callback and Promise

The pipeline and finished functions now returns a Promise if a callback function is not provided as last argument.

The pipeline function supports mapping functions as argument. This makes not possibile to Fluido to understand when the last function passed inside the pipeline is a callback or a mapping function.

A special type of callback needs to be used in that case:

import { asCallback, isCallback, pipeline } from 'fluido'

const callback = asCallback(err => {
  if (err) {
    // handle error
  } else {
    // all done
  }
})

console.log(isCallback(callback)) // true
console.log(isCallback(() => {})) // false

pipeline(source, mapSource, callback)

Stream with async/await

Stream implementation methods _construct, _write, _writev, _final, _transform, _flush, and _destroy (and their option conunterpart) now support the async keyword and/or a Promise as return value.

import { Readable, Transform, Writable } from 'fluido'

const r = new Readable({
  async construct () {
    // construct async stuff
  },
  async destroy () {
    // destroy async stuff
  }
})

const t = new Transform({
  async construct () {
    // construct async stuff
  },
  async transform (chunk) {
    // transform async stuff
  },
  async flush () {
    // flush async stuff
  },
  async destroy () {
    // destroy async stuff
  }
})

const w = new Writable({
  async construct () {
    // construct async stuff
  },
  async write (chunk) {
    // write async stuff
  },
  async writev (items) {
    // write async stuff
  },
  async final () {
    // finalize async stuff
  },
  async destroy () {
    // destroy async stuff
  }
})

A Readable stream does not implement a callback for the _read method by default. Because of that, It's not possible for Fluido to automatically detect when the _read method needs to be async.

To support async reads, a new method is available: _asyncRead (along side with its asyncRead option).

import { Readable } from 'fluido'

const r = new Readable({
  async asyncRead (size) {
    // read async stuff
  }
})

Concurrency

Passing the concurrency option to the Writable (may be Duplex or Transform) constructor will cause _write (or _transform) calls to be concurrent.

const { Transform, Writable } = require('fluido')

const w = new Writable({
  concurrency: 8,
  async write (chunk) {
    // At most 8 concurrent writes
  }
})

const t = new Transform({
  concurrency: 8,
  async transform (chunck) {
    // At most 8 concurrent transforms
  }
})

isNodeStream(value)

Returns true if value is a Readable or a Writable stream.

  • value <*>
  • Returns: <Boolean>
import { Readable, Writable, isNodeStream } from 'fluido'

console.log(isNodeStream(new Readable())) // true
console.log(isNodeStream(new Writable())) // true

isReadableStream(value)

Returns true if value is a Readable stream.

  • value <*>
  • Returns: <Boolean>
import { Readable, Writable, isReadableStream } from 'fluido'

console.log(isReadableStream(new Readable())) // true
console.log(isReadableStream(new Writable())) // false

isWritableStream(value)

Returns true if value is a Writable stream.

  • value <*>
  • Returns: <Boolean>
import { Readable, Writable, isWritableStream } from 'fluido'

console.log(isWritableStream(new Readable())) // false
console.log(isWritableStream(new Writable())) // true

isDuplexStream(value)

Returns true if value is both a Readable and a Writable stream.

  • value <*>
  • Returns: <Boolean>
import { Duplex, Readable, Writable, isDuplexStream } from 'fluido'

console.log(isDuplexStream(new Readable())) // false
console.log(isDuplexStream(new Writable())) // false
console.log(isDuplexStream(new Duplex())) // true

merge(...streams)

Combines two or more streams into a Duplex stream that writes concurrently to all Writable streams and reads concurrently from all Readable streams.

  • streams <Stream[]>
  • Returns: <Duplex>