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

@paullaffitte/pipelines.js

v1.0.3

Published

Typescript pipeline tree execution.

Downloads

8

Readme

pipelines.js

Typescript pipeline tree execution.

Goal

The goal of pipelines.js is to provide an easy to use framework to build execution trees and, of course, execute it.

Getting started

Installation

$ npm i @paullaffitte/pipelines.js
# or
$ yarn add @paullaffitte/pipelines.js

Principle, concepts and examples

Pipelines class

You start with the base class Pipelines. It's using the design pattern builder to help creating your execution tree. When instanciating it, you can provide a Executor and Hooks.

const Pipelines = require('@paullaffitte/pipelines.js');

// This executor will returns its calculated duration after a defined duration
const defaultExecutor = async config => {
  return new Promise(resolve => {
    const start = Date.now();
    setTimeout(() => resolve(Date.now() - start), config.duration);
  });
};

// A list of hooks to use in the execution tree
const hooks = [
  node => {
    const { config, output } = node;

    // You can treat your nodes differently dependings on what you node is to trigger different actions
    if (typeof config.index == 'number') {
      // Here we log the index of the node and its calculated duration (the index is not built-in, its from the node configuration, see the example below)
      console.log(`${config.index} done (${output}ms)`);
    }
  },
];

const pp = new Pipelines(defaultExecutor, hooks);

Some concepts

An Executor is something that given a ExecutionNode will produce a Result.

A Hook is something that will produce an action following an Executor execution.

An ExecutionNode is a part of the execution tree, it contains an Executor and a configuration, its configuration can be anything.

Some ExecutionNode will be leaves, some other will be non-leaves :

  • Leaves will be executed given the default executor or a custom one if any was given before execution using the function pp.with (see documentation).
  • Non-leaves will always use a custom executor and their confguration will always be a list of nodes to execute. It will handle how the nodes are executed. For instance, with the function pp.parallel, you can trigger a parallel execution of the given nodes. You can create your own non-leaves nodes types with the function pp.node. For example, you could need a execution where you execute your nodes serially until one fail (which is not what pp.serial is doing, it will execute all nodes serially whether some fail or not).

A Result can be anything that an Executor should return.

Creating and executing an execution tree

Once your Pipelines object is correctly configured, you can start to create your execution tree.

// Using the executor in the example above, we want to execute those nodes
const executionList = [
  pp.exec({ index: 1, duration: 500 }),
  pp.exec({ index: 2, duration: 300 }),
  pp.exec({ index: 3, duration: 100 }),
];

// Our main execution tree will be sequential using the built-in function .sequence
const executionTree = pp.sequence([
  // Change the default executor to log something a different points in the execution
  pp.with(console.log).exec('Parallel pipelines'),
  // We execute the previously defined execution tree in parallel
  pp.parallel(executionList),
  // Another logging
  pp.with(console.log).exec('Sequencial pipelines'),
  // We execute the previously defined execution tree serially
  pp.sequence(executionList),
]);

You can then execute the tree you just built. All values returned but the executors will be returned as a result tree.

executionTree.execute().then(resultTree => {
  console.log(JSON.stringify(resultTree, null, 2));
});

When executed, we expect this tree to have the following output.

Parallel pipelines
3 done (100ms)
2 done (300ms)
1 done (500ms)
Sequencial pipelines
1 done (500ms)
2 done (300ms)
3 done (100ms)
[
  null,
  [
  500,
  300,
  100
  ],
  null,
  [
  500,
  300,
  100
  ]
]

See how you can find the values returned by our default executor at the leaves of the result tree.

This example was taken from tests, I recommend you to read them and run them if you want to get a better understanding.

Documentation

In order to generate the documentation, please run the following commands.

$ git clone [email protected]:paullaffitte/pipelines.js.git
$ cd pipelines.js
$ npm install
$ npm run docs

And then open ./docs/index.html.