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

async-transforms

v1.0.9

Published

Asynchronous stream transforms

Downloads

1,556

Readme

Node.JS CI

Asynchronous stream transforms for Node. Allows async handlers and parallel execution, useful for build systems like Gulp and friends.

Install

Install async-transforms with your favourite package manager.

Usage

Use transforms.map, .filter or .gate to generate a stream.Transform instance that calls the passed handler.

// for example

import * as transforms from 'async-transforms';
import 'stream';

stream.pipeline(
  stream.Readable.from([object1, object2]),  // used for demo
  transforms.map(async (object) => {
    await object.expensiveOperation;
    await object.someOtherThing;
  }),
  createOutputStream('./foo'),
  (err) => {
    // callback
  },
)

These transforms operate in parallel and don't guarantee the order of their output (whatever finishes first). You can set options to configure behavior:

const s = transforms.map(handler, {
  order: true,  // force the same output order
  tasks: 5,     // limits number of parallel tasks
});

It's also possible to set objectMode: false (it's true by default) but this is unlikely to be useful to you.

Gulp

This example uses async-transforms to parallelize rendering with Less. This is important if e.g., Less is loading further files from the filesystem.

const transforms = require('async-transforms');
const {src, dest} = require('gulp');
const less = require('less');

exports.default = () => {
  return src('*.less')
    .pipe(transforms.map(async (file) => {
      const result = await less.render(file.contents.toString('utf8'));
      file.contents = Buffer.from(result.css);
      file.extname = '.css';
    }))
    .pipe(dest('output'));
};

While Gulp plugins for Less already exist, this makes it easier to write general-purpose, modern plugins with async and await syntax.

Worker Pool

This includes a submodule which provides a worker pool. It's useful when combined with the above transforms handler. For example:

import {pool} from 'async-transforms/worker';

const asyncCompile = pool(path.resolve('./compile.js'), {tasks: 2});

// use directly
const result = await asyncCompile('input', 'all', 'args', 'are', 'passed');

// or as part of a transform
stream.Readable.from([object1, object2])
    .pipe(transforms.map(asyncCompile))
    .pipe(transforms.map(() => {
      // do something with the result
    }));

The pool invokes the default export (or module.exports for CJS) of the target file. By default, it creates a maximum number of workers equal to 75% of your local CPUs, but set tasks to control this—use a fraction from 0-1 to set a ratio, and higher integers for an absolute number.

You can also specify minTasks to always keep a number of hot workers around. This number can only be an integer, and defaults to 1.

Use this for CPU-bound tasks like JS minification.

This doesn't really belong in this module. This can hold your binary 'open': if you're using the pool, be sure to process.exit() when you're done.