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

@alterior/runtime

v3.9.0

Published

Core runtime for Alterior apps

Downloads

549

Readme

@alterior/runtime

Version

Provides the core of Alterior: the ability to declare, configure, bootstrap and execute an application composed of executable modules and their dependencies.

Installation

npm install @alterior/runtime

Usage

An Alterior application consists of an "entry" module and its dependency imports. You "bootstrap" the entry module which causes dependencies to be resolved, an application to be constructed, and modules to be executed.

You should define your entry module in its own file (app.module.ts).

@Module({
    imports: [ LoggerModule ]
})
export class AppModule {
    constructor(
        private logger : Logger
    ) {
    }

    altOnInit() {
        this.logger.info('Hello, world!');
    }
}

A separate entrypoint file (main.ts) should handle bootstrapping the entry module.

import { Application } from '@alterior/runtime';
import { AppModule } from './app.module';

Application.bootstrap(AppModule);

Module Lifecycle Events

Each Alterior module is the coupling of a unit of execution (the Module class) and a set of services used by the unit and/or made available to other modules which depend on it.

Modules can optionally define lifecycle methods which are invoked by the runtime. When a module is first bootstrapped, altOnInit() is run. A simple one-shot execution module should implement its business logic in altOnInit().

altOnStart() is called once when the application starts, and altOnStop() is called before the application is terminated, so as to give the module an opportunity to gracefully shut down. If you want the ability to dynamically control the start/stop status of your module, you should use Roles.

Roles

Many execution modules represent a service which can be turned on and off. Alterior has support baked in for this with Roles. Such modules can register a Role which allows the status of the service to be controlled and queried programmatically by using the RolesService injectable service.

To register a role, use RolesService.registerRole(roleRegistration). This is usually done within the altOnInit() method of a class marked with @Module(). You will need to provide start() and stop() methods which will be executed when the roles service decides to start/stop your role. You will also need to specify an identifier which is used when referring to the role in configuration and the environment.

Configuring enabled roles

When an Alterior app is bootstrapped, several environment variables are inspected to determine which roles should be enabled by default. They are checked in the following order (the first one found wins).

  • ALT_ROLES_ONLY - Enable only the given roles
  • ALT_ROLES_ALL_EXCEPT - Enable all roles except those listed (including roles that are disabled by default)
  • ALT_ROLES_DEFAULT_EXCEPT - Enable all default-enabled roles except those listed

The variables are comma-delimited lists of role identifiers that should be started or ignored. By default all registered roles which are configured as enabled by default are started.

Alternatively you can specify roles via the command line when the application is started using one of the following options:

--roles-only,       -r  [role,...]  Enable only the specified roles
--roles-skip,       -x  [role,...]  Enable all default-enabled roles except those specified
--all-roles-except, -R  [role,...]  Enable all roles except the specified roles (regardless of default status)

For example, to enable only the web-server and tasks roles:

node dist/main.js -r web-server,tasks

Stopping the application

The application can be explicitly stopped by injecting Runtime and calling the shutdown() method. This causes the altOnStop() lifecycle event to be run for all loaded modules, and execution to be stopped with process.exit(). If you wish to stop all modules of the application without exiting the process, use Runtime.stop() instead.

Custom Lifecycle Events

You can programmatically trigger custom lifecycle events by calling Runtime.fireEvent(eventName).

eventName should be an UpperCamelCase string. The method executed on modules will be alt${eventName}, so if you specify DoSomething, then the method altDoSomething() will be executed on each module which implements it.

Self Test

Passing --self-test to your application will cause Alterior to stop after the application is bootstrapped and perform a successful exit. This can be used as a sanity check to make sure that your service starts correctly while building.