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

safebox

v0.2.1

Published

Dependency Injection container similar with Angular's DI but with service failure detection

Downloads

15

Readme

container node.js module

DI container inspired by angular.js with one major twist: a service can become "out of service" and your entire application parts will know that they have to work in "fail safe" mode. If everything is working fine again all parts depending on the failing component will get notified and normal working will be magically enabled again.

Why?

We found that existing modules for DI doesn't handle fails. Externale services can go down or the network can go down and error recovery code should automaticaly go up as fast as possible. Without a solid foundation on how objects get wired it is quite dificult to prevent loosing important data when such failures happens.

We extened a bit idea of DI to go beyound object instantiation on knowning precisely when a specific feature is ready or not to be used in other zones of the application. Also, simple having an object instance is not enough, you usually want to known when an object is god to use, a connection is ready or an subsystem is properly initialised. Therefore the API offerd by this module is created to provide nice sollutions on the problems caused by inherent node.js's asyncronism in the context of DI.

Install

npm install container

/*now in your code you can get an instance of the container*/
var container = require("safebox").container;

#API

service

/* name: is the name of the service
  arr: array with dependencies (names) for this service
  callback: function called when all dependencies are ready
*/
container.service(name, arr, callback)

The callback given to service or declareDependecy functions will behave like in angular except that the first parameter will be always a boolean (outOfService flag) that will signal that the callback is called for invalidating the current service or for proper initialisation

declareDependency

/*  identical with  service but for better intuition.
A callback can instantiate multiple local/closure variables that are not exposed services.
*/
container.declareDependency(name, arr, callback)

resolve

/* Directly assign a value to the service. It can't be null!!!!
It will try to initialise other services depending on name.
*/
container.resolve(name,value)

outOfService

/*  Declare that a service or feature is not working properly. 
All the services depending on this will get notified
*/
container.outOfService(name)
/*
Now.. all the services depending on 'name' got notified and are working in fail recovery mode.
You have to call container.resolve(name, value) to enable normal working
*/

Example

container.service('node', ['node_base'], function(outOfService, node_base){
    if(!outOfService){
        return {type:"node"}
    } else {
       //... will be called anyway at the begining for initialisation
    }
})

var fakeRoot = {fakeRoot:true};
var root = fakeRoot;
container.service('root', ['node'], function(outOfService, node) {
    if (!outOfService) {
        assert.true(node1 != null);
        root = {type: "root", node: node};
        return root; //value used as value
    } else {
        root = fakeRoot; //code handling out of service request (caching request or something else)
    }
});

/* initialisation will be triggered by calling container.service('node_base', [],..) or: */
container.resolve('node_base', value)

Test cases

You can find other examples in the test folder

#Observation We do not try to treat circular dependencies. Circular services will never be initialised. Normally it should not be a problem because even a smoke test should easily catch such errors. Some automated tests should catch them also.