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

hurp

v4.0.2

Published

Asynchronous modules for Node.js applications

Downloads

36

Readme

hurp

npm

A little class lets you design your application as a composition of asynchronous modules.

Concept

Basically, async module is a class that can be initialized and destroyed asynchronously, e.g. with async init() and async destroy() methods.

The best example is a http server. It requires to be asynchronously initialized to open a listening port, and to be asynchronously destroyed to close the port and wait for pending requests to be done.

Typical application consist of many modules like http server, database driver, message queue client, business logic models. Some of them requires async initialization or destroying. Some of them depends on the others. We need to start accepting requests after all other modules are ready, and to close a database driver only after all pending requests are done in order to support graceful shutdown.

Hurp is a way to deal with that things.

Installation

$ npm install hurp

Usage

You can design your application as a set of independent modules, wiring them together in a single composition root. Modules should depend only on interfaces, implementations gets injected manually in a root class constructor.

Package exports a Hurp class. It is a container for async modules. You can add child modules with a use() method. Next, when you call its init() method, it will sequentially call init() method on every child module in that order in which they were added. Its destroy() method behaves similarly, but in reverse order.

Because Hurp instance has an async init() and async destroy() method, it is an async module itself. This can be used to build a complex modules tree. But most likely plain root object will be good enough.

So, you can extend the Hurp class to make something like App class, which will be a composition root of your application. Next, create all the things in its constructor, wire them up and use() async modules.

import { Hurp } from 'hurp';
import { Foo } from './foo';
import { Bar } from './bar';

export class App extends Hurp {
  public foo: Foo;
  public bar: Bar;
  
  constructor() {
    super();
    
    const foo = new Foo();
    this.foo = this.use(foo);
    
    const bar = new Bar({ foo });
    this.bar = this.use(bar);
  }
}

Now application can be launched with something like this:

import { App } from './app';

async function main() {
  const app = new App();
  
  await app.init();
}

main().catch(err => console.error(`boot failed: ${err.message}`));

Take a look at hurp-launch package for a more convenient way to launch hurp-based application

API

Module

import { Module } from 'hurp';

A module public interface. Has a methods init() and destory(), described below.

Hurp

import { Hurp } from 'hurp';

A class, container for async modules. You may extend it with your composition root implementation.

use<M extends Module>(mod: M): M

Adds module mod to a child module list. Returns mod.

async init(): Promise<void>

Sequentially calls init() method on every child module in that order in which they were added.

async destroy(): Promise<void>

Sequentially calls destroy() method on each child module in reverse order to the one in which they were added.