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.
plume-ts-di
Advanced tools
Lightweight & simple dependency injection system for TypeScript based on code generation instead of annotation
Lightweight & simple dependency injection system for TypeScript based on code generation instead of annotation.
The features offered by this library are kept small. Having just a small set of features enables to:
package.json
file: npm install plume-ts-di
npm -D install ts-transformer-classname @wessberg/di-compiler
tsconfig.json
file:"plugins": [
{"transform": "./di-transformer-adapter.ts" },
{"transform": "ts-transformer-classname" }
]
Note that the di-compiler
might be difficult to configure, that's why it can be easier to create a di-transformer-adapter.ts
file in the project and use this file in the tsconfig.json
declaration:
import { di } from "@wessberg/di-compiler";
import * as ts from 'typescript';
export default function(program: ts.Program) {
return di({ program });
}
export default function installServicesModule(injector: Injector) {
// bindings, all classes in the DI system must be declared here
injector.registerSingleton(LocaleService);
injector.registerSingleton(IdlenessDetector);
injector.registerSingleton(SessionService);
injector.registerSingleton(ObservableNotificationEngine);
injector.registerSingleton(ObservableNotificationEngine, NotificationEngine);
injector.registerSingleton(Scheduler);
}
Injector
in your application entry point, generally index.ts
:const injector = new Injector();
installServicesModule(injector);
// you can now get instances of your singletons :
const instance = injector.getInstance(IdlenessDetector);
TS Interfaces are not compiled into JS and it poses problems with DI. Abstract classes must be used instead. Abstract classes can be implemented as interfaces in TS.
The provider pattern in DI can be used to have logic added in classes creation, or if you want to have instances of external libraries which do not offer Plume TS DI bindings.
Here is an exemple of creating NativeService
or BrowserService
(both implementing Service
) depending on the existence of a native JS function:
export default class ServiceProvider implements Provider<Service> {
private readonly service: Service;
constructor(private nativeService: NativeService, private browserService: BrowserService) {
if (typeof nativeFunction === 'function') {
this.service = nativeService;
} else {
this.service = browserService;
}
}
get(): Service {
return this.service;
}
}
This can then be registered in the Injector
: injector.registerSingletonProvider(ServiceProvider, Service);
And voilà, it is now possible to get the correct instance of Service
anywhere in the application: constructor(private readonly service: Service)
Sometimes and especially to integrate with React it is easier to use a global instance of the Injector in React components. To implement this pattern, two functions are provided:
configureGlobalInjector(Injector)
: Configure the global instance of the injector (should be called in index.ts
after the global Injector
has been fully configured)getGlobalInstance(ClassType)
: To get an instance of a type in the global Injector
For example in index.ts
:
const injector = new Injector();
installServicesModule(injector);
installComponentsModule(injector);
installApiModule(injector);
// to be called after the injector has been configured
configureGlobalInjector(injector);
And in a React component:
export default function Login() {
const sessionService = getGlobalInstance(SessionService);
const messageService = getGlobalInstance(MessageService);
// return ...
}
To integrate with React or Vue, data passed from the dependency injection system to the React/Vue components should rely on the Observable pattern:
The Observable pattern integrates way better than other alternatives like Redux.
Moreover, it is generally easier to integrate inside UI component using the global Injector instance.
Instances are created as they are needed. If you want to initialize all instances at startup (which is often a good thing to do), you need to call the method initializeSingletonInstances()
on the Injector
:
const injector = new Injector();
installServicesModule(injector);
// to be called after the injector has been configured
injector.initializeSingletonInstances();
Plume TS DI relies on:
npm login
npm run release
<= yarn must not be usedFAQs
Lightweight & simple dependency injection system for TypeScript based on code generation instead of annotation
The npm package plume-ts-di receives a total of 188 weekly downloads. As such, plume-ts-di popularity was classified as not popular.
We found that plume-ts-di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
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.