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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@med1802/scoped-observer-message-broker

v2.1.0

Published

A lightweight message broker that sits on top of `@med1802/scoped-observer`. It keeps the same scoped event model, but adds convenient publish/subscribe APIs plus per-event interceptors for validating, transforming, or blocking traffic.

Readme

@med1802/scoped-observer-message-broker

A lightweight message broker that sits on top of @med1802/scoped-observer.
It keeps the same scoped event model, but adds convenient publish/subscribe APIs plus per-event interceptors for validating, transforming, or blocking traffic.

Features

  • Scoped messaging – reuse any observer created with createScopedObserver.
  • Per-event interceptors – hook into publish or subscribe for a single event.
  • Type-safe payload flow – interceptors can rewrite eventName and payload.
  • Minimal footprint – uses @med1802/scoped-observer as a peer dependency (you control the version).

Installation

npm install @med1802/scoped-observer-message-broker

Note: @med1802/scoped-observer is a peer dependency, so you need to install it separately.

Quick Start

import { createScopedObserver } from "@med1802/scoped-observer";
import { createMessageBroker } from "@med1802/scoped-observer-message-broker";

// Create a scoped observer (typically once per app)
const scopedObserver = createScopedObserver();

// Create a message broker
const messageBroker = createMessageBroker(scopedObserver);

// Register an interceptor to transform payload on publish
messageBroker.interceptor({
  eventName: "increment",
  onPublish: ({ eventName, payload }) => {
    return {
      eventName,
      payload: {
        incrementBy: payload,
        timestamp: Date.now(),
        multiplier: 2,
        random: Math.random(),
      },
    };
  },
});

// Subscribe to events
messageBroker.subscribe({
  eventName: "increment",
  callback: (payload) => {
    console.log(JSON.stringify(payload, null, 2));
    // Output:
    // {
    //   "incrementBy": 1,
    //   "timestamp": 1234567890,
    //   "multiplier": 2,
    //   "random": 0.123456
    // }
  },
});

// Publish an event (payload will be transformed by interceptor)
messageBroker.publish({
  eventName: "increment",
  payload: 1,
});

API

createMessageBroker(observer)

  • observer: Instance returned by createScopedObserver.
  • Returns an object with:
    • publish({ scope?, eventName, payload }) - Publish an event (scope is optional)
    • subscribe({ scope?, eventName, callback }) - Subscribe to an event (scope is optional)
    • interceptor({ eventName, onPublish?, onSubscribe? }) - Register interceptors for an event

Interceptors

Interceptors allow you to transform or block events before they're published or subscribed:

messageBroker.interceptor({
  eventName: "increment",
  onPublish: ({ eventName, payload }) => {
    // Transform payload before publishing
    return {
      eventName,
      payload: {
        ...payload,
        timestamp: Date.now(),
      },
    };
    // Or block by returning null/false
    // return null;
  },
  onSubscribe: ({ eventName }) => {
    // Rewrite event name if needed
    return { eventName: `transformed:${eventName}` };
    // Or block subscription by returning null/false
    // return null;
  },
});

Interceptor behavior:

  • onPublish receives { eventName, payload } and can:
    • return { eventName, payload } to continue (optionally mutated)
    • return null or false to abort publishing
  • onSubscribe receives { eventName } and can:
    • return { eventName } to continue (optionally rewritten)
    • return null or false to abort subscription

License

MIT © med1802