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.
@exodus/dependency-injection
Advanced tools
A simple dependency injection and inversion of container
This is a basic inversion of control container that resolves a dependency graph and instantiates all nodes. You want to use this if you're stitching together an application from dozens of different modules and your imperatively defined dependency DAG is starting to become unmanagable.
There are lots of sophisticated inversion of control containers out there, e.g. inversify, awilix, and others. They're probably great. Someday we may switch to them, but at this point they're overkill and they raise the cognitive load higher than absolutely necessary. This library implements a simple container in a highly opinionated way with very little flexibility. Here is a sample node in a list of dependencies:
[
...,
{
id: 'harry',
dependencies: ['scar', 'dobby'],
factory: ({ scar, dobby }) => {
// Harry uses his scar to defeat dobby, or however that book goes
},
type: 'wizard', // optional
}
]
As you can see:
id
.dependencies
as string ids.factory
function which receives a single object as an argument, with named options mapping 1:1 to ids listed in dependenciestype
field. You can use this to query the container for nodes where type
is a certain value.The container, given a list of such nodes, will give you all the instances. That's all there is to it!
Declare nodes and edges, and let the container wire everything together.
import createContainer from '@exodus/dependency-injection'
const createLogger =
(namespace) =>
(...args) =>
console(namespace, ...args)
const container = createContainer({ logger })
container.registerMultiple([
{
id: 'storage',
factory: createStorage,
},
{
id: 'assetsModule',
// the container will pass `({ logger })` to the factory
factory: createAssetsModule,
dependencies: ['logger'],
},
{
id: 'walletAccountsAtom',
factory: () =>
createInMemoryAtom({
defaultValue: { [WalletAccount.DEFAULT_NAME]: WalletAccount.DEFAULT },
}),
},
{
id: 'enabledWalletAccountsAtom',
// the container will pass `({ walletAccountsAtom })` to the factory
factory: createEnabledWalletAccountsAtom,
dependencies: ['walletAccountsAtom'],
},
{
id: 'blockchainMetadata',
factory: createBlockchainMetadata,
// the container will pass `({ assetsModule, enabledWalletAccountsAtom })` to the factory
dependencies: ['assetsModule', 'enabledWalletAccountsAtom'],
},
])
The container will throw when it encounters a duplicate dependency id. To override a dependency for test purposes, use the override flag
container.register({
id: 'balances',
override: true,
factory: () => ({ load: jest.fn() }),
})
container.resolve()
const {
storage,
assetsModule,
walletAccountsAtom,
enabledWalletAccountsAtom,
blockchainMetadata,
balances,
} = container.getAll()
Optional dependencies can be requested by appending a trailing ?
:
container.register({
id: 'jediSurvivor',
factory: ({ lightsaber, theForce }) => {
if (lightsaber) {
// code has to make sure optionality is gracefully handled
return new InsaneLightsaberWiedlingJedi(lightsaber)
}
return new ThereIsOnlyTheForce(theForce)
},
dependencies: ['lightsaber?', 'theForce'],
})
By default dependencies are injected as named options, which is the recommended pattern. However, if your use case demands dependencies to be injected as positional arguments, e.g. if your factory function is a reselect
selector, specify injectDependenciesAsPositionalArguments: true
either at the container level via the constructor, or at the individual node level. See examples in tests.
id
:
'analytics'
for the analytics module.'analytics'
, which corresponds to an object with certain methods with certain signatures. This is declared by consumers of analytics in their dependencies
array of ids.constructor({ analytics })
In the future we might disambiguate these, but in the short term we've opted for simplicity.
See headless as a real-world e2e example that initializes an IOC and makes use of multiple preprocessors.
Allow me to simulate your internal dialogue.
You
: Holy crap! The afterlife is 1) more verbose, 2) has higher cognitive load and 3) breaks Intellisense by using ids instead of function pointers.
Container
: I hear you. I should improve those (help me help you). On the other hand, I have several advantages that grow as your list of nodes grows (e.g. the Exodus browser extension already has >100 nodes). I enable you to:
Check out @exodus/dependency-preprocessors, which enables the config
and logger
special cases described above, and more.
FAQs
A simple dependency injection and inversion of container
We found that @exodus/dependency-injection 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.