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

twired

v0.1.5

Published

Separate function definitions and runtime calls using a variety of runtime executors

Downloads

3

Readme

codecov NPM Version

Twired

  • Separate business logic and runtime concerns.
  • Tiny (< 5kb) library with 0 dependencies.
  • Write distributed code that look and feel like regular code.
  • Easy to migrate — just write an executor adapting your current job/queue/task system. Doable in an afternoon!
  • Swap out executors in different environments for different behaviors and guarantees.

Problem Statement

When a codebase evolves to make use of distributed computing, they often have to change how code is written and structured. What used to be a simple function call is turned into createTask, runTask, collectTask calls, often split up into different directories and utilities.

This makes your codebase hard to understand, slow to debug, and difficult to develop.

You probably even adopt different systems or services for different characteristics and guarantees, multiplying the surface area for developers to understand — hundreds of createTask, sendMessage, startJob, each probably using the same (hopefully?) underlying functions nested deep inside wrapped calls and workers.

This library is written as an answer to this problem.

Experience again writing simple function calls with semantic clarity and type safety.

Port your existing job systems, message queues, run times to an executor you can write in an afternoon. Re-use all of your existing infrastructure, and test them side-by-side!

Run the same functions with different guarantees and characteristics depending on the environment and your needs!

Usage

  1. npm install twired
  2. move your function into a Twired class / extend your class with an executor property
  3. decorate your function with @dispatch or @dispatchAwait
  4. use / write an executor to run the decorated functions
  5. until decorators are accepted into Ecmascript, you will need a transpiler, e.g. babel / typescript

Examples

Each of the directories in examples are executable.

Check out each of their readmes!

  1. Local Executor (this is a minimal example wrapping a regular local function call)
  2. Redis Executor (this is an example which support different dispatch and dispatchAwait calls, and separating calling and running functions)
  3. MQ Executor (an example showcasing only dispatch type calls, validating executor configuration, and resuming of jobs)
import { Twired, dispatch, dispatchAwait } from "twired";

class Greeter extends Twired {
  @dispatch
  async greet(recipient: string) {
    const person = await getPerson(recipient);
    const message = await this.generateGreeting(person.name);
    await this.sendGreeting(recipient, message);
  }

  @dispatchAwait
  async generateGreeting(name: string) {
    return `Hello ${name}`;
  }

  @dispatch
  async sendGreeting(recipient: string, message: string) {
    const messageId = await sendEmail(recipient, message);
    await this.saveMessage(messageId);
  }

  @dispatch
  async saveMessage(messageId: string) {
    await saveEvent(messageId);
  }
}

async function main() {
  const executor = new RedisExecutor();
  const greeter = new Greeter(executor);
  const greeting = await greeter.greet("[email protected]");
}

Concepts

Inspired by this paper

dispatch and dispatchAwait decorators

These decorators wrap functions and allow executors to execute them.

dispatch is meant for function calls where you do not expect a response, e.g. saving events without waiting for its response. The type signature enforces a void return value.

dispatchAwait is meant for function calls where you expect a response, e.g. an API server recevies a request, wants to offload processing to a different machine / thread, and receive a result to create a response.

You will notice the distinction is arbitrary. You can technically use dispatchAwait for all calls, even for those without a response. I find it useful to make this explicit to understand the decorated function's behavior at a glance.

Executors can also make use of this distinction to optimize wrapped calls, e.g. an executor using messages can make dispatch calls return when a message is acknowledged without waiting for the result; an "unsafe" executor can even immediately return dispatch calls without waiting for the queue to acknowledge the message.

Why decorators?

Decorators for this use case strike a good balance of transprency (it's obvious that this method is not a regular function call) and lack-of-clutter. You can easily follow function calls without having to jump through higher order functions or different runners/workers.

Executors

The Executor interface defines how an executor deals with dispatch and dispatchAwait decorated functions.

Executors are expected to implement at least the call method, which represents how it deals with a function call from the decorators.

Executors can implement register, which will be called on decorator initialization. At the moment, this is the method used by executors to do anything outside of the function call, such as validate options per function call (e.g. needing a specified queue per decorated function), subscribing to whatever run time job/message system, processing work, routing, etc.

Advanced executors could have fallbacks to local execution, or on-the-fly adjustment of where to execute code depending on resource usage or desired durability requirements.

Twired

The Twired base class simply ensures that there is an executor attached to the decorated class methods, and that register initialization works out of the box.

You can also implement the interface HasExecutor, or even avoid all of it if you can ensure you have the executor property on your classes on decorator initialization. This requires knowing a bit of decorator-specific knowledge (ref).

TODOs

  • [ ] Error types / cases
  • [ ] Thread/worker example
  • [x] Redis example
  • [x] MQ example
  • [x] Executor with options example
    • [x] MQ executor enforces each decorated method needs a distinct queue name