Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@ukab/container
Advanced tools
Container is a IoC packge for NodeJS. This package supports both ESM and CommonJs modules.
npm i @ukab/container
Note: You should install and import reflect-metadata before using this package.
1. using default container
import 'reflect-metadata';
import { Container, Injectable } from '@ukab/container';
class Router {
test() {
console.log('router test called')
}
}
// register router
Container.register(Router);
@Injectable()
class Test {
constructor(public router: Router) { }
test() {
this.router.test()
}
}
// register and get resolved instance
const instance = Container.resolve<Test>(Test);
instance.test();
2. creating and using container
import 'reflect-metadata';
import { Container, Injectable } from '@ukab/container';
const myContainer = Container.of('myContainer');
class Router {
test() {
console.log('router test called')
}
}
// register router
myContainer.register(Router);
@Injectable()
class Test {
constructor(public router: Router) { }
test() {
this.router.test()
}
}
// register and get resolved instance
const instance = myContainer.resolve<Test>(Test);
instance.test();
3. using provider with custom identity
import 'reflect-metadata';
import { Inject, Injectable, Container } from '@ukab/container';
class Router {
test() {
console.log('router test called')
}
}
// register router with custom identity
Container.register(Router, 'IRouter');
interface IRouter {
test(): void
}
@Injectable()
class Test {
constructor(@Inject('IRouter') public router: IRouter) {
}
test() {
this.router.test();
}
}
// register and get resolved instance
const instance = Container.resolve<Test>(Test);
instance.test();
4. injecting custom dependency in method
import 'reflect-metadata';
import { Inject, Injectable, Container } from '@ukab/container';
class Router {
test() {
console.log('router test called')
}
}
// register router with custom identity
Container.register(Router, 'IRouter');
interface IRouter {
test(): void
}
class User {
test() {
console.log('user test called')
}
}
// register user service
Container.register(User);
@Injectable()
class Test {
@Inject()
user: User
constructor(@Inject('IRouter') public router: IRouter) {
}
test(@Inject('p1') p1: number) {
this.router.test();
this.user.test();
console.log(p1);
}
}
Container.value<number>('p1', 1);
// register and call method with param injection
Container.register<Test>(Test);
Container.call(Test, 'test');
5. using context container
import 'reflect-metadata';
import { Inject, Injectable, Container, IocContext, Scope } from '.';
const container = Container.of('test');
@Injectable({ scope: Scope.Contextual })
class Test {
constructor(@Inject('p1') public p1: number) {
}
test() {
console.log('p1 value', this.p1);
}
}
container.register<Test>(Test);
// this context will use the given container
const context1 = new IocContext('random context id', container);
context1.set<string, number>('p1', 2);
// this context will use the given container
const context2 = new IocContext('random context id', container);
context2.set<string, number>('p1', 3);
const testFromContext2 = context2.get<Test>(Test);
testFromContext2.test() // p1 value 3
const testFromContext1 = context1.get<Test>(Test);
testFromContext1.test() // p1 value 2
FAQs
Ukab Ecosystem ioc container library
The npm package @ukab/container receives a total of 0 weekly downloads. As such, @ukab/container popularity was classified as not popular.
We found that @ukab/container 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.