
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
A simple Inversion of Control library for JavaScript.
import { ServiceRegistry } from "tioc";
class Service {
// ...
}
ServiceRegistry.create()
// Adding a simple service
.add("singleton", "myService", () => new Service())
// Alternative way to write it!
.addSingleton("foo", () => new Service())
.addScoped("bar", () => new Service())
.addTransient("baz", () => new Service());
import { ServiceRegistry } from "tioc";
class Service {
// ....
}
// Register a new instance of Service to the key "foo"
const registry = ServiceRegistry.create()
.addSingleton("foo", () => new Service());
// Start a new scope. This will give you a ServiceProvider
const provider = registry.scope();
// Retreive the service by the key, "foo"
const service = provider.foo();
tioc allows services to depend on another.
Factory functions are given a provider instance that they can use to get previously registered services.
import { ServiceRegistry } from "tioc";
class Config {
// ...
}
class Database {
constructor(private config: Config) {}
// ...
}
// ✅ Register the service you want to depend on first, and then use it!
ServiceRegistry.create()
.addSingleton("config", () => new Config())
.addSingleton("database", (provider) => new Database(provider.config())); // `provider` is a ServiceProvider, like the one received from `ServiceRegistry.scope()`
// ❌ Doesn't work. Services can't depend on ones that arent registered yet. This prevents cyclic dependencies.
ServiceRegistry.create()
.addSingleton("database", (provider) => new Database(provider.config())) // Error: Property 'config' does not exist on type '{}'.
.addSingleton("config", () => new Config());
It isn't recommended to make your services async, but sometimes there is no way around it.
The ServiceRegistry and ServiceProvider are built to allow for async services.
import { ServiceRegistry } from "tioc";
class Exchange {}
class Queue {}
declare class MessageQueue {
makeExchange(): Promise<Exchange>;
makeQueue(exchange: Exchange): Promise<Queue>;
}
const registry = ServiceRegistry.create()
.addSingleton("mq", () => new MessageQueue())
// Simply return a promise
.addSingleton("exchange", (provider) => provider.mq().makeExchange())
// Factory functions are allowed to be async.
.addSingleton("queue", async (provider) => {
const exchange = await provider.exchange()
return await provider.mq().makeQueue(exchange);
});
const provider = registry.scope();
// Async trickles down into the provider.
// If your factory returns a Promise, your provider will.
const exchange = await provider.exchange();
const queue = await provider.queue();
FAQs
A simple inversion of control provider
We found that tioc 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.