
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
@inversifyjs/core
Advanced tools
@inversifyjs/core is a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript applications. It helps in managing dependencies and promoting a clean architecture by using dependency injection.
Dependency Injection
This feature allows you to define and inject dependencies into your classes. The code sample demonstrates how to use @inversifyjs/core to inject a Warrior instance into a Samurai class, promoting loose coupling and easier testing.
const { Container, injectable, inject } = require('inversify');
@injectable()
class Warrior {
fight() {
return 'Fight!';
}
}
@injectable()
class Samurai {
constructor(@inject(Warrior) warrior) {
this.warrior = warrior;
}
battle() {
return this.warrior.fight();
}
}
const container = new Container();
container.bind(Warrior).toSelf();
container.bind(Samurai).toSelf();
const samurai = container.get(Samurai);
console.log(samurai.battle()); // Output: Fight!
Binding and Scoping
This feature allows you to define the lifecycle of your dependencies. The code sample shows how to bind a Ninja class in a singleton scope, ensuring that only one instance of Ninja is created and shared across the application.
const { Container, injectable } = require('inversify');
@injectable()
class Ninja {
fight() {
return 'Ninja fight!';
}
}
const container = new Container();
container.bind(Ninja).toSelf().inSingletonScope();
const ninja1 = container.get(Ninja);
const ninja2 = container.get(Ninja);
console.log(ninja1 === ninja2); // Output: true
Middleware
Middleware in @inversifyjs/core allows you to intercept and modify the behavior of dependency resolution. The code sample demonstrates how to apply a middleware that logs messages before and after the execution of a dependency resolution.
const { Container, injectable, interfaces } = require('inversify');
@injectable()
class LoggerMiddleware {
handler(next) {
return (args) => {
console.log('Before execution');
const result = next(args);
console.log('After execution');
return result;
};
}
}
const container = new Container();
container.applyMiddleware((planAndResolve) => {
return (args) => {
console.log('Middleware applied');
return planAndResolve(args);
};
});
Tsyringe is a lightweight dependency injection container for TypeScript and JavaScript. It offers similar functionality to @inversifyjs/core but is designed to be more lightweight and easier to use, with a focus on TypeScript decorators.
Awilix is a powerful dependency injection container for JavaScript and Node.js. It provides a similar feature set to @inversifyjs/core, including support for dependency injection and lifecycle management, but with a focus on simplicity and ease of use.
BottleJS is a simple and lightweight dependency injection micro-container. It offers basic dependency injection capabilities similar to @inversifyjs/core but is more minimalistic and easier to integrate into small projects.
FAQs
InversifyJs core package
The npm package @inversifyjs/core receives a total of 172,882 weekly downloads. As such, @inversifyjs/core popularity was classified as popular.
We found that @inversifyjs/core demonstrated a healthy version release cadence and project activity because the last version was released less than 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 MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.