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

@giancosta86/worker-transform

v1.0.3

Published

Multithread mapping stream

Downloads

6

Readme

worker-transform

Multithread mapping stream

GitHub CI npm version MIT License

Overview

worker-transform provides a WorkerTransform object-oriented stream that transparently runs a given operation on multiple items, concurrently, by internally managing a pool of worker threads.

Like any other standard Transform, this stream can be plugged into a pipeline, as well as manually controlled; for maximum performance, the item order is not guaranteed.

Installation

npm install @giancosta86/worker-transform

or

yarn add @giancosta86/worker-transform

The public API entirely resides in the root package index, so you shouldn't reference specific modules.

Usage

  1. First of all, you need an operation module: a module exporting just the operation - a function that will be applied to every item flowing through the stream.

    The operation must be:

    • with an arbitrary name

    • accepting one parameter - of type ChunkInput<TInput> - where you need to replace TInput with the actual type of the items entering the stream

      ChunkInput<TInput> actually contains just two fields, provided by the underlying stream infrastructure:

      • value - the item entering the stream

      • encoding - the associated character encoding. Actually meaningful only when the item is a string

    • returning one of these two types:

      • ChunkOutput<TOutput> - if the function is synchronous (without interruptions). You need to replace TOutput with the type of the items produced by the stream.

        The ChunkOutput<TOutput> type contains the value to be produced by the function as well as the related encoding - but the latter is optional, because it is meaningful only if the output items are strings

        The result of the function must be a ChunkOutput<T> - but such T can be a nullable type, such as number | null! When the value field of the returned ChunkOutput is null, the stream will just skip it!

      • Promise<ChunkOutput<TOutput>> - if the function is asynchronous - i.e., if it could perform await on external conditions or, more generally, if it is designed to return a Promise. Again, you need to replace TOutput with the type of the items produced by the stream - which, again, can be nullable

    • throwing errors when needed: both errors and rejected promises simply make the stream ignore the related input element

    An operation module with a synchronous operation could be:

    import { ChunkInput, ChunkOutput } from "@giancosta86/worker-transform";
    
    function add200({ value }: ChunkInput<number>): ChunkOutput<number> {
      return { value: value + 200 };
    }
    
    export = add200;

    On the other hand, an operation module with an asynchronous operation could be:

    import delay from "delay";
    import { ChunkInput, ChunkOutput } from "@giancosta86/worker-transform";
    
    async function add500({
      value
    }: ChunkInput<number>): Promise<ChunkOutput<number>> {
      await delay(5);
      await delay(2);
      await delay(6);
    
      return Promise.resolve({ value: value + 500 });
    }
    
    export = add500;
  2. Create an instance of WorkerTransform - passing at least the path to the operation module as expected by resolve(), and maybe additional options (see the description below)

    For example:

    import { join } from "node:path";
    import { WorkerTransform } from "@giancosta86/worker-transform";
    
    const modulePath = join(__dirname, "add200");
    
    const transform = new WorkerTransform(modulePath);
  3. Use it like any other standard stream - for example, in a pipeline:

    await pipeline(Readable.from([90, 95, 98]), transform, someWritableStream);

Additional constructor options

The following values can be passed to the constructor as fields of an optional object, right after the operation module path:

  • agentCount: the number of worker threads in the pool. Default: the number of processors

  • logger: a Logger interface, as exported by unified-logging. Default: no logger

  • highWaterMark: if present, passed to the base constructor

  • signal: if present, passed to the base constructor

Further reference

For additional examples, please consult the test suites in the source code repository.