Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
[Saft](https://github.com/surikaterna/saft) is a lightweight Dependency Injection (DI) Framework for TypeScript & JavaScript. It is based on ES2015/ES2017 features such as decorators.
Readme
Saft is a lightweight Dependency Injection (DI) Framework for TypeScript & JavaScript. It is based on ES2015/ES2017 features such as decorators.
Allow dynamic extensions to application both in server (node) code as well as client (browserify, webpack) code.
npm install --save saft
Create module classes that provides values to an Injectors DI context. Then access the bound values from the injector.
// DbModule.ts
import { DB } from './DB';
export class DbModule {
@Promises('db')
getDb() {
return new DB();
}
}
// UserModule.ts
import { DB } from '../db/DB';
import { User } from './User';
export interface GetUserById {
(user: User): Promise<User>;
}
export class UserModule {
@Provides('getUserById')
@Inject('db')
createGetUserById(db: DB) {
return (id: string) => db.findById(id);
}
}
// injector.ts
import { Injector } from 'saft';
import { DbModule } from './user/DbModule';
import { UserModule } from './user/UserModule';
class InjectorSingleton {
static instance?: Injector;
static isResolved = false;
static getInjector(): Injector {
if (InjectorSingleton.instance) {
return InjectorSingleton.instance;
}
const injector = new Injector(
new DbModule(),
new UserModule()
);
InjectorSingleton.instance = injector;
return injector;
}
}
export default InjectorSingleton.getInjector();
// bootstrap.ts
function bootstrap() {
/**
* Run any services
* Bound values will be accessible through the injector singleton
*
* const getUserById = injector.get<GetUserById>('getUserById');
* const user = await getUserById('123');
*/
}
export default bootstrap;
// index.ts
import bootstrap from './bootstrap';
import injector from './modules/injector';
// Run application when all modules are ready to be consumed
injector.once('ready', bootstrap);
Used with Module classes to create a binding for a function that returns a value
class DbModule {
@Provides('db')
getDb() {
return new DB();
}
}
// Injector know how to create instances of DB with the key 'db'
const injector = new Injector(new MyModule());
Used with Module classes to create a binding for a function that instead of the value returns a promise of the value. This is used when the value should be injected instead of the promise to the "consuming" function.
class DbModule {
@Promises('db')
getDb() {
return Promise.resolve(new DB());
}
}
// Injector will cache the resolved value and return the DB instance when 'db' is injected
const injector = new Injector(new DbModule());
TODO: Write description
TODO: Write description
Used to ensure single instance of the provided value.
class DbModule {
@Provides('db')
@Singleton()
getDb() {
return new DB();
}
}
/**
* Injector will only initialize DB once
* The same instance will be provided every time 'db' is injected
*/
const injector = new Injector(new DbModule());
TODO: Write description
Decorate a function to make the injected values into arguments.
class DbModule {
@Promises('db')
@Singleton
getDb() {
return Promise.resolve(new DB());
}
@Provides('validate')
getValidator() {
return (entity: Entity) => {
if (!entity.id) {
throw new Error('Data is invalid');
}
};
}
}
class UserModule {
@Provides('addUser')
// Inject multiple values
@Inject('db', 'validate')
getAddUser(db: DB, validate: Validate) {
return (user: User) => {
try {
this.validate(user);
this.db.insert(user);
} catch (error) {
// Handle validation error
}
}
}
}
type AddUserFunc = ReturnType<UserModule['getAddUser']>;
// Use injector.get to access bound values
injector.get<AddUserFunc>('addUser').then((addUser) => {
// Will validate data and inserting into the database if valid
addUser(userData);
});
FAQs
[Saft](https://github.com/surikaterna/saft) is a lightweight Dependency Injection (DI) Framework for TypeScript & JavaScript. It is based on ES2015/ES2017 features such as decorators.
The npm package saft receives a total of 2,057 weekly downloads. As such, saft popularity was classified as popular.
We found that saft 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.