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

@sigitex/bind

v1.1.2

Published

A lightweight dependency injection container with lazy resolution and proxy-based injection.

Downloads

71

Readme

bind

A lightweight dependency injection container with lazy resolution and proxy-based injection.

bun add @sigitex/bind

Note: This package currently exports TypeScript sources directly. A TypeScript-compatible runtime or bundler (Bun, etc.) is required.

Quick Start

import { bind, factory, singleton, constructor } from "@sigitex/bind"

const container = bind({
  dbUrl: "postgres://localhost/mydb",
  db: factory(({ dbUrl }) => createPool(dbUrl)),
  userService: factory(({ db }) => new UserService(db)),
})

const userService = container.resolve<UserService>("userService")

API

bind(bindings)

Creates a new Container with the given bindings. This is a shorthand for new Container().bind(bindings).

const container = bind({
  port: 3000,
  host: "localhost",
})

Container

The core class. Manages bindings and resolves dependencies lazily (instances are created on first access and then cached).

container.bind(bindings)

Registers bindings. Plain values are wrapped in a ValueBinding automatically. Returns the container for chaining.

container.bind({
  logger: factory(({ config }) => new Logger(config)),
  config: { level: "debug" },
})

Re-binding a key clears its cached instance, so subsequent resolves will use the new binding.

container.resolve<T>(key)

Resolves a binding by key. Throws if the key is not found.

const logger = container.resolve<Logger>("logger")

The special key "container" always resolves to the container itself.

container.createInjector<T>()

Returns a proxy object that lazily resolves dependencies on property access. This is how dependencies are injected into factories and constructors.

type Deps = { db: Database; logger: Logger }
const deps = container.createInjector<Deps>()
// deps.db triggers resolve("db") on first access

container.call(fn)

Calls a function with an injector as its first argument.

container.call(({ db, logger }) => {
  logger.info("connected")
  return db.query("SELECT 1")
})

container.new(Constructor)

Instantiates a class, passing an injector to the constructor.

class UserService {
  constructor({ db, logger }: Deps) { /* ... */ }
}

const service = container.new(UserService)

container.clone()

Creates a new container with the same bindings (but fresh instance cache). Useful for per-request scoping.

async function handleRequest(request: Request) {
  const requestContainer = container.clone()
  requestContainer.bind({ identity: await authenticate(request) })
  // ...
}

Binding Types

Plain values

Any value that is not a Binding instance is treated as a value binding:

bind({ port: 3000, name: "my-app" })

value(v)

Explicit value binding (equivalent to passing a plain value):

import { value } from "@sigitex/bind"
bind({ port: value(3000) })

factory(fn)

Creates the instance by calling fn with an injector. A new instance is created each time the container resolves the key (though the container caches the result after the first resolve).

import { factory } from "@sigitex/bind"

bind({
  db: factory(({ connectionString }) => new Pool(connectionString)),
})

constructor(Class)

Like factory, but calls new Class(injector) instead.

import { constructor } from "@sigitex/bind"

class EmailService {
  constructor({ smtp, templates }: Deps) { /* ... */ }
}

bind({ emailService: constructor(EmailService) })

singleton(binding)

Wraps any other binding so that its instance persists across container clones and outlives any single container. Useful for expensive resources that should be shared globally (connection pools, etc.).

import { factory, singleton } from "@sigitex/bind"

bind({
  pool: singleton(factory(({ dbUrl }) => new Pool(dbUrl))),
})

Resolution Behavior

  • Dependencies are resolved lazily -- a factory/constructor is not called until its key is first accessed.
  • Once resolved, instances are cached within that container. Subsequent resolves return the same instance.
  • The injector proxy resolves each property access independently -- you only pay for dependencies you actually use.
  • singleton caches globally (across all containers), while regular bindings cache per-container instance.

Usage with Frameworks

The container is designed to be cloned per-request so each request gets its own scope while sharing the base bindings:

const baseContainer = bind({
  db: singleton(factory(() => createPool())),
  userRepo: factory(({ db }) => new UserRepo(db)),
})

// Per-request:
const container = baseContainer.clone()
container.bind({ identity: currentUser })