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

hub

v6.1.3

Published

Service locator

Downloads

357

Readme

Hub - A Node.js Service Locator Library

hub is a lightweight service locator library for Node.js. It allows you to register, retrieve, and manage services, making dependency injection easier and decoupling your modules. This library supports asynchronous service initialization, de-initialization, and lazy-loading of services using promises.

Table of Contents

Installation

Install via npm:

npm install hub

Usage

Creating Hub Instance

To initialize the service locator in your application, create a standalone instance of hub in the main entry point of your application:

const hub = require('hub').standalone();

In all other files across your application, you can simply include hub using:

const hub = require('hub');

This ensures that all parts of your application share the same hub instance.

Registering Services

You can register a service with the register() method. A service constructor should return a promise that resolves to the service object, and an optional destructor can be provided for clean-up. Here's an example using async/await:

const hub = require('hub');

// Register a service
hub.register('logger', async () => {
    return {
        log: (message) => console.log(message),
    };
});

Retrieving Services

Once a service is registered, you can retrieve it using the get() method. The service will be lazily initialized the first time it's accessed:

const logger = await hub.get('logger');
logger.log('Service Locator Initialized!');

Destroying Services

To properly clean up resources, you can destroy a service by calling destroy(). This will execute the optional destructor provided during registration:

await hub.destroy('logger');
console.log('Logger service destroyed');

Unregistering Services

If needed, services can be unregistered to remove them from the service locator. This will not destroy an already initiated service:

hub.unregister('logger');

Checking Service Status

You can check if a service is registered or initiated:

console.log(hub.isRegistered('logger'));  // true/false
console.log(hub.isInitiated('logger'));   // true/false

API

register(name, constructor, destructor)

Registers a service with a given name.

  • name: String - The name of the service.
  • constructor: Function - A function that returns a promise resolving to the service instance.
  • destructor (optional): Function - A function that returns a promise for cleaning up the service.

Returns true if successful.

get(name, standalone)

Retrieves the service instance by its name. If standalone is true, it will not cache the service and will always invoke the constructor.

  • name: String | Array - Name of the service (or array of names).
  • standalone: Boolean (optional) - If true, the service will not be cached.

Returns a Promise that resolves to the service instance.

destroy(name, service)

Destroys a service and calls the registered destructor (if provided).

  • name: String | Array - Name of the service or array of services.
  • service (optional): The service instance to destroy.

Returns a Promise.

unregister(name)

Unregisters a service by its name.

  • name: String - Name of the service.

Throws an error if the service is not registered. Returns true if successful.

isRegistered(name)

Checks if a service is registered.

  • name: String - Name of the service.

Returns true if the service is registered, otherwise false.

getRegistered()

Retrieves a list of all registered services.

Returns an array of service names.

isInitiated(name)

Checks if a service has been initiated.

  • name: String - Name of the service.

Returns true if the service is initiated, otherwise false.

getInitiated()

Retrieves a list of all initiated services.

Returns an array of initiated service names.

standalone()

Returns a new instance of the Hub that is not cached by the require system. Useful for creating isolated service locators.