Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
kontainer-di
Advanced tools
This is a really simple dependency injection container for Javascript applications (both nodejs/iojs and browser environments if using CommonJS modules). It uses Array.prototype.forEach
so please polyfill as needed on your environment.
We wrote a blog post presenting this library and the motivations behind it:
How to structure node.js applications with dependency injection
From npm:
npm install kontainer-di
When you want any of the following:
require
calls at top of every file../../../lib
and still enjoy a nice folder structureIdeally you will have a single file containing all your module registrations. Probably you'll have one for each environment. Then, whenever you need a module (with all its dependencies nicely injected for you), you will get it from the container (API below).
The container will cache all instantiated modules like standard require
does, so dependencies all parsed only once and every subsequent use of a register module will return the same instance.
See the examples
folder for a basic example in ES5, ES2015, an async one and a full Express application using the container.
This container works in a very simple way: you register modules/services with dependencies, and then can retrieve/start/stop each of them, or all at once.
container.registerModule(name, [depName, depName, ...], implementation)
- Registers a module in the container. Alias: register
name
- the name of the module, will be used as dependency name in other modules, and for getting/starting/stopping it.[depName, depName,...]
- this modules dependencies (their names) or an empty Arrayimplementation
- this module implementation. If the module has dependencies, this must be a function accepting the dependencies in the same order. The container will throw an Error if the number of declared dependencies doesn't match the factory function arity. If the module has no dependencies, it can be a plain Object.container.getModule(name)
- Retrieves the given module from the container, with all its dependencies injected, if any. Alias: get
container.startModule(name[, options])
- Retrieves the given module from the container, automatically calling the module's start
function if it exists. If your module needs to do some async stuff, make this function return a Promise (a thenable to be specific) and pass in {async: true}
in options.container.stopModule(name)
- Stops the module name
. It will delete the current saved instance, and call the module's stop
function if it exists.container.clearModule(name)
- Removes a module from the container. Will throw if the module has already been instantiated.container.startAll()
- Like calling container.start
for every registered module. Returns nothing.container.stopAll()
- Like calling container.stop
for every registered module. Returns nothing.container.reset()
- Resets the container configuration. Useful for testscontainer.debug()
- Prints out the container current configuration, using console.log
.FAQs
A simple lightweight Dependency Injection container
The npm package kontainer-di receives a total of 819 weekly downloads. As such, kontainer-di popularity was classified as not popular.
We found that kontainer-di 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
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.