Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ioc-service-container
Advanced tools
This is a lightweight zero-dependency library for a service container written in TypeScript.
In this StackBlitz-Demo you can see a demonstration of the
ioc-service-container
. In the App.tsx
you can verify that the UserService
is fully typed without importing the
class.
Install the dependency with npm install ioc-service-container
If you use the ioc-service-container
in a TypeScript project, define the types of your services in a ioc.d.ts
file
otherwise you can skip this step.
// Import your services
import { TestApi } from '../your-path/to/TestApi';
import { FooApi } from '../your-path/to/FooApi';
import { TestService } from '../your-path/to/TestService';
// Create the mapping between ServiceId and Service
type IoCTypes = {
TestApi: TestApi;
FooApi: FooApi;
TestService: TestService;
myString: string;
// ...
};
// Redeclare the scg function to get full Typscript support
declare module 'ioc-service-container' {
export function scg<T extends keyof IoCTypes, U extends IoCTypes[T]>(id: T): U;
}
According to this you have to pass a factory, a constructable or an entity to the ioc container. So at
the initial script of your application you call a function named e.g. setupService
:
import { ServiceContainer } from 'ioc-service-container';
function setupService() {
ServiceContainer.set('TestApi', CustomTestApi); // setup by class reference
ServiceContainer.set('FooApi', () => new CustomFooApi()); // setup by custom factory
const testService = new TestService();
ServiceContainer.set('TestService', testService); // pass the instance directly
ServiceContainer.set('myString', 'hello world'); // pass primitive values
}
The factory is only instantiated at need.
Now you have 2 options to inject the requested service.
scg()
FunctionThe first is the most common one: const testApi = scg('TestApi);
. (Shortcut for ServiceContainer.get()
. Because of
the type declaration you have full TypeScript support at this point and no dependency on the file/class TestApi
. (See
the Demo)
@inject
DecoratorThis requires
"experimentalDecorators": true
to be enabled in yourtsconfig.json
(See Typescript Docs)
export class CustomTestService implements TestService {
@inject
private readonly customApi!: Api; // Important is the name of the property, it's mapped to the service id
@inject('FooApi') // If you don't want to name your property like the service id, pass the id as parameter
private readonly nameThisHowYouWant!: Api;
private readonly fooApi = ServiceContainer.get<Api>('FooApi'); // Use this syntax if you don't want to use decorators
private readonly barApi = scg('BarApi'); // Shortcut for ServiceContainer.get()
}
For Testing or similar use cases you have the option to use ServiceContainer.isSet('anId')
,
ServiceContainer.override('anId', 123)
or ServiceContainer.reset()
.
Structuring your code and avoiding implizit dependencies is two of the most effective ways to avoiding bugs, especially when code gets extended. To goal of Dependency Injection (DI) is to prevent structures like this:
class CustomService {
constructor() {
this.api = new CustomApi();
}
}
The CustomService
has an implizit dependency to the CustomApi
.
The goal of DI is to encapsulate the dependencies of a class. The CustomService should work without knowing which api it is using.
FAQs
Lightweight ioc service container
The npm package ioc-service-container receives a total of 331 weekly downloads. As such, ioc-service-container popularity was classified as not popular.
We found that ioc-service-container demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.