Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
simple-di-poc
Advanced tools
Simple DI is a Dependency Injection and Inversion of control POC module to manage your dependencies in node js. It is implented in typescript and has express-js integrations right out of the box
Simple DI is a Dependency Injection and Inversion of control POC module to manage your dependencies in node js. It is implented in typescript and has express-js integrations right out of the box
Lets say you have the following classes:
class DependencyA { ... }
class DependencyB {
constructor(private a: DependencyA) {}
doSomething() {
....
}
}
class SomeService {
constructor(private b: DependencyB) {}
doSomething() {
....
}
}
To get your hands on an instance of SomeService
first you need some how to get a instance of DependencyB
class which requires an instance of DependencyA
. Dependencies relations can scalate quickly even on small apps.
With simple DI getting an instance of SomeService is rather easy.
To Get started you need to import the Service
decorator and use it in the classes you want to declarate as a dependency.
import { container } from 'simple-di-poc';
@Service()
class SomeService() {
...
}
Later on wherever you need an instance of SomeService
you will need import the container instance.
The main purpose of this class is to resolve the dependencies you previously delcare as such.
This class is a singleton, this means an instance of it is exported and you don't have to worry abount creating the instance.
To get an instance of a dependency you simple use the resolve()
method and pass the class type you need the instance of.
const someService = container.resolve(SomeService)
someService.doSomething();
and that's it, the container will take care of injecting the dependencies and building the instance.
Additionally when registering a service you can pass its lifetime, to let the container know when you need a new instance and when you need cached one already created before. There are three lifetimes supported:
transient
: The container will return a new instance every time the resolve method is called. Theres no caching here.singleton
: The container will return the same instance every time the resolve method is called. It will create a new instance the first time resolve method is called and cache it forever.scoped
: The container will associate the instance to a given scope, and it will return a new instance everytime the scope changes. It will create an instance if a given scope hasn't one yet and associate it with the scope, and return the same one as long the same scope is given. i.e.: If the scope is a http request object, it will return the same instance for the request.Well easy, for every dependency you register just pass the lifeTime
option with one of the previous values.
container.register(SomeService, { lifeTime: 'scoped' })
The default value is transient
, but if scoped
value was used, you'll have to provide the scope when calling the resolve method.
Lets say we are ina middleware function where the container is available:
function middleware(request, response, next) => {
const scopedSomeService = container.resolve(SomeService, request)
scopedSomeService.doSomething();
next();
}
The instance returned by the container will be shared for all the other situation where the same request instance is provied.
If you are working with express you can forget everything explained before and take advantage of the express-js integration decorators provided for even a more simple integration in 3 easy steps.
Service
decorator to let the container know that's a dependency you want registeredController
decorator along with the Get
, Post
, Put
, Patch
and Delete
decorators, to let the integration know how your express-js router should be builded.expressDiConnector()
integration function to register the routers in the express-js appHere's how:
@Service({ lifeTime: 'scoped' })
export class SomeSerivce {
doSomething(): string {
...
}
}
@Controller('/api')
export class TestController {
constructor(
private someService: SomeService,
) {}
@Get({ path: '/' })
get(req: Request, res: Response) {
res.json({ result: this.someService.doSomething() });
}
@Get({ path: '/:id' })
getById(req: Request, res: Response) {
res.json({ result: this.someService.doSomething(), id: req.params.id });
}
}
const app: Application = express();
expressDiConnector(app, [TestController]);
....
app.listen(3001);
And that's it!
FAQs
Simple DI is a Dependency Injection and Inversion of control POC module to manage your dependencies in node js. It is implented in typescript and has express-js integrations right out of the box
We found that simple-di-poc 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.