
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
javascript-dependency-injection
Advanced tools
Javascript Dependency Injection library
DI makes classes accessible by a contract. Instances are created when requested and dependencies are injected into the constructor, facilitating lazy initialization and loose coupling between classes.
As an example, consider a User and Persitance classes:
function WebSql(name, fieldList) {
this.persist = function (obj) {
console.log('WebSQL will persist:');
fieldList.forEach(function (field) {
console.log(' ' + field + ': ' + obj[field]);
});
}
}
function IndexDB(name, fieldList) {
this.persist = function (obj) {
console.log('IndexDB will persist:');
fieldList.forEach(function (field) {
console.log(' ' + field + ': ' + obj[field]);
});
}
}
function User(email, passwd, storage, role) { // the `storoge` parameter holds an instance
this.email = email;
this.passwd = passwd;
this.role = role;
this.save = function () {
storage.persist(this);
};
}
With these classes in our pocket its time to setup the relations between them. The function that does this has the following signature
function (<contract name>,
<class reference>,
[optional list of constructor arguments],
{optional configuration object} )
Or just in code:
var di = new DI();
di.register('user', User, [null, 'welcome', 'websql', 'nobody']);
di.register('websql', WebSql, ['userTable', ['email','passwd', 'role']], {singleton: true});
di.register('indexdb', IndexDB, ['userTable', ['email','passwd', 'role']], {singleton: true});
Note that the provied constructor arguments are default values or contract names. From now it is easy to create instances:
var user1 = di.getInstance('user', ['john@exampe.com']),
-> email: 'john@exampe.com', passwd: 'welcome', storage : WebSQL instance, role: 'nobody'
user2 = di.getInstance('user', ['john@exampe.com', 'newSecret']); // define a new password
-> email: 'john@exampe.com', passwd: 'newSecret', storage : WebSQL instance, role: 'nobody'
if (user1 instanceof User) { /* user1 is an instance of User!! */ }
But it is also possible to use IndexDB as the persistance class:
var user = di.getInstance('user', ['john@exampe.com', null, 'indexdb']), // The password is set to null too!
-> email: 'john@exampe.com', passwd: null, storage : IndexDB instance, role: 'nobody'
root = di.getInstance('user', ['john@exampe.com', undefined, 'indexdb', 'admin']);
-> email: 'john@exampe.com', passwd: 'welcome', storage : IndexDB instance, role: 'admin'
Checkout more detailed the documentation here
Install the dependencies as follows
$> npm install
To minify the library
$> gulp
To run the tests
$> gulp test
$> bower install javascript-dependency-injection
FAQs
Javscript dependency injection library
We found that javascript-dependency-injection 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.