
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
A little class lets you design your application as a composition of asynchronous modules.
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.
$ npm install hurp
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
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.
FAQs
Asynchronous modules for Node.js applications
We found that hurp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.