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

vorarbeiter

v7.0.5

Published

A simple service container

Readme

A simple service container

Installation

npm install vorarbeiter

Basic usage

  1. Create some services:
interface Car {
  getDriverName(): string;
}

class CarImpl implements Car {
  constructor(private readonly driver: Driver) {}
  getDriverName() {
    return this.driver.getName();
  }
}

interface Driver {
  getName(): string;
}

class DriverImpl implements Driver {
  getName() {
    return "Michael Schumacher";
  }
}
  1. Explain to Service Container how to create services, use factories for this:
class CarFactory implements ServiceFactory {
  create(container: ServiceContainer): CarImpl {
    const driver = container.get("driver");
    return new CarImpl(driver);
  }
}
  1. Create Service Specification:
import { createServiceSpecBuilder } from "vorarbeiter";

const specBuilder = createServiceSpecBuilder();

specBuilder.set("car", new CarFactory());
specBuilder.set("driver", () => new DriverImpl());

const spec = specBuilder.getServiceSpec();

If creating a service is trivial, as for driver, we can simply pass a function as a factory. As for class based factory we can pass ServiceContainer as a function parameter.

  1. Create Service Container with this Service Specification:
import { createServiceContainer } from "vorarbeiter";

const serviceContainer = createServiceContainer(spec);
  1. Get some service and call its method:
const car: Car = serviceContainer.get("car");

console.log(car.getDriverName());
  1. Get string "Michael Schumacher".

Service scope

Shared

By default, services have global scope. It means that the same service will be shared across whole application.

Example:

let serviceInstance1;
let serviceInstance2;
// In some part of our application we get a service
serviceInstance1 = serviceContainer.get("myService");
// In some another part of our application we get a service again
serviceInstance2 = serviceContainer.get("myService");

console.log(serviceInstance1 === serviceInstance2); // true

Scoped

Sometimes we need to have service uniqueness within a specific scope, for example, within one user request. To do that we should specify the Context Resolver when configure Service Specification. Resolving result of the Context Resolver should be any object. To imitate situation when we have two different contexts we can use AsyncLocalStorage from "node:async_hooks" package.

Example:

const asyncLocalStorage = new AsyncLocalStorage<object>();
specBuilder
  .set("myScopedService", () => ({ serviceName: "Awesome service" }))
  .scoped(() => asyncLocalStorage.getStore());

const serviceContainer = createServiceContainer(specBuilder.getServiceSpec());

let scopedService1;
{
  let scopedService2;

  asyncLocalStorage.run({}, () => {
    scopedService1 = serviceContainer.get("myScopedService");
    scopedService2 = serviceContainer.get("myScopedService");
  });
  console.log(scopedService1 === scopedService2);
}

{
  let scopedService3;
  let scopedService4;
  asyncLocalStorage.run({}, () => {
    scopedService3 = serviceContainer.get("myScopedService");
    scopedService4 = serviceContainer.get("myScopedService");
  });
  console.log(scopedService1 === scopedService3);
  console.log(scopedService3 === scopedService4);
}

// Output:
// true
// false
// true

Transient

Sometimes we need get new instance of a service each time we get it. To do this, we should make the service as transient.

Example:

const specBuilder = createServiceSpecBuilder();
specBuilder.set("myService", () => ({ serviceName: "My service" })).transient();

const spec = specBuilder.getServiceSpec();

const serviceContainer = createServiceContainer(spec);

console.log(serviceContainer.get("myService") === serviceContainer.get("myService")); // false

Injection after service creation

The most common type of injection is constructor injection. This type of injection occurs when creating service. But sometimes we want to inject after the service has been created. For this we can specify Service Injector for the service:

specBuilder.set("injectorService", () => {
  return new class {
    car!: Car;
    driver!: Driver;
    setDriver(driver: Driver) {
      this.driver = driver;
    }
  };
}).withInjector((service, container) => {
  service.car = container.get("car");
  service.setDriver(container.get("driver"));
});

This way we can perform property and setter injection.