Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@drewjbartlett/tiny-ioc
Advanced tools
`tiny-ioc` is a lightweight (less than 1kB) dependency injection / IOC container for TypeScript.
tiny-ioc
is a lightweight (less than 1kB) dependency injection / IOC container for TypeScript.
npm i @drewjbartlett/tiny-ioc --save
Create a container.ts
file that creates, registers, and then exports the container.
// container.ts
import { createContainer, Scope } from '@drewjbartlett/tiny-ioc';
const container = createContainer();
container.bind(MyClass, () => new MyClass(), Scope.Singleton);
container.registerFactory(DataSource, () => new DataSource(container.get(HttpClient)));
container.registerSingleton(HttpClient, () => new HttpClient());
export { container }
// some-other-file.ts
import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';
export async function makeRequest() {
try {
const dataSource = container.get(DataSource);
return await dataSource.get('/foo/bar');
} catch (e) {
//
}
}
bind
- Bind a dependency with a given scope.
bindSingleton
- Bind a dependency to the container as a singleton.
bindFactory
- Bind a dependency to the container as a factory. Each time the dependency is resolved the container will call the factory function.
bindOnce
- Only bind the given value if there is not already a binding.
get
- Attempt to resolve a given binding. Will throw if there is no binding found.
resetSingleton
- Reset a singleton value. If a value has been previously resolved and is registered as a singleton, this will keep the binding but reset the singleton value until the next resolve.
bound
- Determine if a binding exists or not.
unbind
- Remove the given binding from the container entirely.
swap
- Swap the old binding's value with the new value. This is useful when testing.
// some-other-file.ts
import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';
export async function makeRequest() {
try {
const dataSource = container.get(DataSource);
return await dataSource.get('/foo/bar');
} catch (e) {
//
}
}
// unit-test.test.ts
import { container } from 'path/to/container';
import { HttpClient } from 'path/to/http-client';
import { makeRequest } from 'path/top/make-request';
class DummyHttpClient {
get(url: string) {
return dummyData;
}
}
it('should make the request', () => {
container.swap(HttpClient, () => new DummyHttpClient());
await myRequest(); // this calls .get() on DummyHttpClient
})
FAQs
`tiny-ioc` is a lightweight (less than 1kB) dependency injection / IOC container for TypeScript.
The npm package @drewjbartlett/tiny-ioc receives a total of 0 weekly downloads. As such, @drewjbartlett/tiny-ioc popularity was classified as not popular.
We found that @drewjbartlett/tiny-ioc 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.