
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
injectdeps-config
Advanced tools
Module that binds configurations provided by node-config into an injectdeps container
Allows the binding of configuration constants provided by the node-config library through the injectdeps IoC container.
See the node-config documentation for how the configuration
files need to be named and various other options like loading them from a yaml
file instead of a json.
For the simplest operation you can just call the config loader with an injectdeps container as parameter. This loads all the compatible configurations from the config file and binds them as constants. By default the keys used for the binding will be the json path to the configuration.
Assuming this is your config/default.json
{
"app": {
"db": {
"host": "localhost",
"port": 1234,
"seeds": ["8.8.8.8","8.8.4.4"]
}
},
"other": {
"foo":"bar"
}
}
You can define your injected module like this
module.exports = require('injectdeps')(['app.db.host', 'app.db.port', 'app.db.debug'], function(host, port, debug){
return `${host}:${port}:${debug}`;
});
When you initialise your container also import the default binder module and load it into the container:
const container = injector.getContainer();
const configLoader = require('injectdeps-config')(config, {});
const db = configLoader(container)
.bindName('db').toObject(defaultDatabase)
.newObject('db');
or shorter:
require('injectdeps-config')(config, {})()
.bindName('db').toObject(defaultDatabase)
.newObject('db');
In the above example we don't provide a container to the loader, which means a new one will be created.
Various configuration options for the binder are described below. For this you should instantiate an EagerBinder
instead of using defaultEagerBinderModule
const settings = {
log: true,
root: 'app',
prefix: 'cfg',
objects: true
};
require('injectdeps-config')(config, settings)();
Use the root
configuration parameter of the eager binder. This will only load children of this particular path. For our above example:
{
root: 'app'
};
only loads the app
breanch of the configuration. The keys necessary for injecting are also shortened.
require('injectdeps')(['db.host', 'db.port'], function(host, port){});
In order to avoid collisions you can add a prefix to the binding keys. For our above example:
{
root: 'app',
prefix: 'cfg'
};
This makes correct biding:
require('injectdeps')(['cfg.db.host', 'cfg.db.port'], function(host, port){});
Note that there is no cfg
key in the configuration json.
In addition to binding every leaf entry of the configuration, you can also bind the intermediary object by turning on objects
in the EagerBinder settings.
{
root: 'app',
objects: true
};
This will bind to constants db.host
, db.port
, db.seeds
but also db
as the constant object
{
host: "localhost",
port: 1234,
seeds: ["8.8.8.8","8.8.4.4"]
}
For debugging purposes, you can turn on binding logs
{
log: true
};
This allows you to get an array of logs from the settings object after the binding is done.
console.log( settings.logs.join("\n") );
Binding 'cfg.db.host' to string 'localhost'
Binding 'cfg.db.port' to number '1234'
Binding 'cfg.db.seeds' to string[] '8.8.8.8,8.8.4.4'
FAQs
Module that binds configurations provided by node-config into an injectdeps container
We found that injectdeps-config 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
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.