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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@monoceros/cluster

v1.0.9

Published

Monoceros IOC container

Readme

@monoceros/cluster

Inversion Of Control container


Table of contents

Install

npm

npm install @monoceros/cluster

Usage

import Cluster from '@monoceros/cluster'

const cluster = new Cluster()

Register

You can register entities to the Cluster by calling .register() with a name and the entity.

const add = (a, b) => a + b

cluster.register('add', add)


// cluster.resolve('add')(1, 2)    // 3

Register as type

You need to specifiy the entity type if you want something other than the default, by passing it through the options parameter of .register().

See types for a list of possibilities

const current = () => ({
  version: Math.random()
})

cluster.register('current', current, {type: Cluster.Singleton})


// const one = cluster.resolve('current')
// one.version    // 0.3345465634534234560654992

// const two = cluster.resolve('current')
// two.version    // 0.3345465634534234560654992

Register dependencies

Functions / Classes depending on others have to pass a list of their dependency names in the option parameter of .register().

Note: dependencies should be passed in the same order as their parameter order in the entity. Dependencies should always be the first parameters of the entity.

Note: entities can be registered in any order.

const add = (a, b) => a + b

const dependend = (d1, a, b, c) => d1(a, b) * c

cluster.register('dependend', dependend, {dependencies: ['add']})
cluster.register('add', add)


// cluster.resolve('dependend')(10, 1, 3)    // 33

Register with arguments

Every argument after the first three arguments applied to .register() will be applied to the entity on calling resolve.

const add = (a, b) => a + b

cluster.register('three', add, {}, 1, 2)


// cluster.resolve('three')()   // 3

Resolve

You can get registered entities from the Cluster by calling .resolve() with the name of the required entity.

// const add = (a, b) => a + b
// cluster.register('add', add)

const resolved = cluster.resolve('add')

resolved(1, 2) // 3

With arguments

You can pass arguments to .resolve. These will automatically be applied to the entity to resolve.

// const add = (a, b) => a + b
// cluster.register('add', add)

const resolved = cluster.resolve('add', 1, 2)

resolved() // 3

Working with Superclusters

You can create clusters within clusters that will resolve requested entities in itself first. If it can't find it in its own entities, it will try to resolve it from the parent cluster.

const parent = new Cluster()

const child = parent.createCluster()

const add = (a, b) => a + b

parent.register('add', add)

child.resolve('add')(1, 2)    // 3

API

import Cluster from '@monoceros/cluster'

Cluster

new Cluster()

Returns instance of Cluster

Register

(name, entity[, options[, ...arguments]])

cluster.register(name, entity, options || {}, ...args)

Register new entity with name.

  • name (required) - Registration name, used by .resolve()

  • entity (required) - Entity to register

  • options (optional) - Object of options.

    • .type (optional) - Register type entity will be resolved with. Accepted values: see types

    • .dependencies (optional) - Array of entity names the current entity depends upon

  • ...args (optional) - Any arguments you want a function / class entity to be called with upon calling resolve

Interchanging the options object

if only passing dependencies ór entity type, the options object can be interchanged for directly passing the dependency array or the entity type.

// only passing type
cluster.register(name1, entity1, Cluster.Singleton)

// only passing dependencies
cluster.register(name2, entity2, ['dependency'])

// passing both
cluster.register(name3, entity3, { type: Cluster.Instance, dependencies: ['dependency'] })

Resolve

(name, [, ...arguments])

cluster.resolve(name, ...args)

Resolve registered entities.

  • name (required) -- Name of registered entity to resolve

  • ...args (optional) - Arguments to apply to entity on caling resolve

Types

| Description | Type | Resolves | Accepted entities | | ----------- | ------------------- | --------------------------------------------------------------------------- | ---------------------------------------------------------------- | | default | Cluster.Body | as-is | string, number, object, function, constructor, class | | Singleton | Cluster.Singleton | singleton | function | | Instance | Cluster.Instance | class instance | constructor, class |


Acknowledgements

Cluster was inspired by this article on dependency injection by krasimir

License

MIT @ Folkert-Jan van der Pol