New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

poppins

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

poppins

ES6-optimized dependency injection

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
215
-36.76%
Maintainers
1
Weekly downloads
 
Created
Source

poppins

ES6-optimized dependency injection

How?

Create a dependency container

const Poppins = require('poppins')
const inject = Poppins()

Register a factory function

Here, our factory is named kite and requires paper and string as dependencies

inject('kite', ({paper, string}) => {
  if (paper && string) {
    return 'a kite!'
  } else {
    return 'no kite :('
  }
})

inject('paper', () => true)
inject('string', () => true)

Get your stuff, with dependencies injected

let {kite} = inject()
expect(kite).toEqual('a kite!')

Override dependencies with test doubles

let {kite} = inject({paper: false})
expect(kite).toEqual('no kite :(')

Caveats

The Module Cache

Each time you retrieve modules with let {foo, bar} = inject(), your factory functions are invoked to build the dependency tree. Caching is in place so each factory will be called at most once, even if multiple things depend on that module. However, a new cache is created for each time you call inject(). This allows you to have multiple instances of your app or library running in the same environment, while keeping their state isolated.

This also has benefits for test isolation, as you're guaranteed to get a brand-new object graph in each test if you access your modules using inject().

ES6 Proxies

Poppins uses ES6 proxies. It comes with a polyfill so you don't need a native Proxy implementation to use it, but you'll get better error messages (for example, if you try to inject a module that doesn't exist) if your environment does have native Proxies.

Keywords

dependency

FAQs

Package last updated on 14 Aug 2016

Did you know?

Socket

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.

Install

Related posts