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

typed-subjects

v1.0.19

Published

Making type-safe calls via NATS

Readme

This library provides high level abstractions over the NATS messaging system, allowing you to make type-safe synchronous and asynchronous calls.

Typical use case is to use it in a microservices architecture for inter-service communication.

Usage

import {connectSubjects, drainWorkerQueues} from "typed-subjects"
import {connect, NatsConnection} from "nats"

// Obtain connection to NATS
const natsConnection = await connect({})

// Define shape of used subjects (API between your components)
const subjects = {
    getPaymentStatus: new RemoteProcedure<{id: number}, {status: PaymentStatus}>("payments.getStatus")
    payments: new TypedSubject<PaymentUpdate>("payments.update"),
}

enum PaymentStatus = {OPEN, PAID, CANCELLED}
type PaymentUpdate = {id: number; status: PaymentStatus}

// Connect subjects to NATS
connectSubjects(subjects, natsConnection)

// Implement/subscribe subjects

subjects.payments.getPaymentStatus.implement(async ({id}) => {
    return {status: PaymentStatus.PAID}
}, {
  // concurrency: number; // Number of concurrent messages processed 
  // timeout?: number; // Max time to process the message
  // middleware: Middleware | Middleware[]; // Middleware to wrap the handler
  // queue?: string; // NATS queue name (used for horizontal scaling)
})

subjects.payments.payments.subscribe(async ({id, status}) => {
    console.log("Got payment update", {id, status})
})

// Call/publish
const {status} = await subjects.getPaymentStatus.request({id: 1})
subjects.payments.payments.publish({id: 1, status: PaymentStatus.PAID})

// At the end, call drainWorkerQueues to wait for all messages to be processed
await drainWorkerQueues()

Main components

TypedSubject. Most basic component. Implements publish/subscribe pattern. Used to to publish messages of certain type.

type PaymentUpdate = {id: number; status: PaymentStatus}
const paymentUpdate  = new TypedSubject<PaymentUpdate>("payments.update")

FilteringSubject. Subject that will allow filtering of data based on partial properties of transferred message. Filter is defined by subject template. Implemented using NATS wildcards. Typical usage is to create subject for particular data, ie listen to object updates by its ID.

type PaymentUpdate = {id: number; status: PaymentStatus}
const paymentUpdate  = new FilteringSubject<PaymentUpdate>("payments.update.$id")

RemoteProcedure. Synchronous remote procedure with single optional request object and single optional result object. Implements NATS request/reply pattern.

const getPaymentStatus  = new RemoteProcedure<{id: number}, {status: PaymentStatus}>("payments.getStatus")

Helper functions

drainWorkerQueues. Finish processing of all messages in the worker queues. This function is useful when you want to wait for all messages to be processed before shutting down the application.