npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

di-proxy

v2.0.3

Published

Dependency Injection UMD Module using the Built-in Proxy.

Readme

DI-Proxy

Dependency Injection UMD Module using the Built-in Proxy.

NPM Version Node Version Build Status Code Coverage devDependencies License Standard Github File Size

Installation

$ npm i --save di-proxy

How to include in...

ES6 import (with babel):

import { inject, wrap } from 'di-proxy'

CommonJS:

const { inject, wrap } = require('di-proxy')

Browser:

<script src="https://unpkg.com/di-proxy"></script>
<script>
  const { inject, wrap } = window.diProxy
</script>

inject(resolver, [memoize]) ⇒ Proxy

Creates an optionally memoized proxy that invokes the resolver from trapped property accesses.

Kind: global function
Returns: Proxy - proxy - A proxy with a get trap that invokes the resolver.

| Param | Type | Default | Description | | --- | --- | --- | --- | | resolver | resolver | | A function invoked within the get trap of returned proxy. | | [memoize] | boolean | true | Shadow properties with result from first invocation of resolver. |

wrap(resolver, callback) ⇒ injector

Kind: global function
Returns: injector - injector - A function that passes arguments after the proxy in callback when invoked.

| Param | Type | Description | | --- | --- | --- | | resolver | resolver | A function invoked within the get trap of first argument to callback. | | callback | callback | A function that accepts a proxy as the first argument. |

resolver ⇒ *

Kind: global typedef
Returns: * - result - Synchronously resolved object referenced by key.
See: inject()

| Param | Type | Description | | --- | --- | --- | | key | string | A reference to some sort of dependency or query. |

callback ⇒ *

Kind: global typedef
See: wrap()

| Param | Type | Description | | --- | --- | --- | | proxy | Proxy | Bound parameter which is the result of inject(resolver, true). | | ...rest | * | Arguments passed when invoking injector. |

injector ⇒ *

Kind: global typedef
Returns: * - result - Result of invoking callback.
See: wrap()

| Param | Type | Description | | --- | --- | --- | | ...args | * | Passed as rest parameter to callback. |

Usage

const { http, express, 'socket.io': sio } = inject(require)

const app = express()
const server = http.Server(app)
const io = sio(server)
// ...

Other examples

jQuery:

const {
  'input#file-input': $file,
  'ul#preview-names': $previewNames
} = diProxy.inject(jQuery)

$file.change(() => {
  $previewNames.empty()

  const files = $file.prop('files')

  for (const file of files) {
    $previewNames.append($('<li/>').text(file.name))
  }
})

document.querySelector:

const qs = diProxy.wrap(
  document.querySelector.bind(document),
  ({ input: { id, name, type, value }, pre }) => {
    const json = JSON.stringify({ id, name, type, value })

    pre.appendChild(document.createTextNode(json))
  }
)

qs()

sessionStorage.getItem:

const ssdi = diProxy.inject(sessionStorage.getItem.bind(sessionStorage))

sessionStorage.setItem('test', 'value')
sessionStorage.setItem('another', 'thing')

// get some session properties
let { test, another } = ssdi

const formattedText = `test: ${test}, another: ${another}`
document.getElementById('out').textContent = formattedText

Dependencies and Supported Environments

This assumes that the environment has a working implementation for Proxy.

All modern browsers including Microsoft Edge, and Node.js >=6.0.0 are supported, according to caniuse.com and kangax/compat-table.

Some Notes on Performance

Internally this dependency injection uses Proxy. However, any performance implications have been mitigated by optionally memoizing each property accessor by default:

const string = diProxy.inject((prop) => {
  console.log('get trap invoked!')
  return prop
})

const value = string.somePropertyName // 'get trap invoked!'
console.log(value) // 'somePropertyName'
string.somePropertyName // nothing will be printed here
delete string.somePropertyName
string.somePropertyName // 'get trap invoked!'

You should be careful when using the default behavior in coordination with a query function for live data (like jQuery, for example), as it will memoize the resolver with the result of the first call for each property name unless you delete it from the proxy object before each subsequent call, or set the optional memoize parameter to false when calling inject().

This library allows you to re-use a created proxy or injector without taking a performance hit from the overhead of a naive proxy implementation.

License

Available under the MIT License (c) 2017 Patrick Roberts