🚀 DAY 3 OF LAUNCH WEEK:Announcing Bun and vlt Support in Socket.Learn more →
Socket
Book a DemoInstallSign in
Socket

@apogeelabs/hoppity-subscriptions

Package Overview
Dependencies
Maintainers
4
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apogeelabs/hoppity-subscriptions

Subscription management for hoppity

latest
npmnpm
Version
0.1.0
Version published
Maintainers
4
Created
Source

@apogeelabs/hoppity-subscriptions

Subscription management middleware for hoppity - a Rascal broker configuration library.

Overview

This plugin provides middleware for setting up subscription handlers in a hoppity pipeline. It validates subscription handlers against the broker topology and automatically wires up message listeners when the broker is created.

Features

  • âś… Topology Validation: Validates that handler keys match subscriptions in the topology
  • âś… Handler Validation: Ensures all handlers are functions and checks for duplicates
  • âś… Fail-Fast Pipeline: Throws errors for missing/invalid subscriptions to prevent dead code
  • âś… Automatic Setup: Wires up message, error, and invalid_content event listeners
  • âś… Async Support: Handles both synchronous and asynchronous handler functions
  • âś… Error Handling: Comprehensive error handling with detailed logging
  • âś… Broker Access: Provides broker instance as 4th parameter to handlers
  • âś… Context Integration: Stores validated subscriptions in middleware context for diagnostics

Installation

pnpm add @apogeelabs/hoppity-subscriptions

Usage

Basic Example

import hoppity from "@apogeelabs/hoppity";
import { withSubscriptions } from "@apogeelabs/hoppity-subscriptions";

// Define your topology with subscriptions
const topology = {
    vhosts: {
        "/": {
            subscriptions: {
                "user-events": {
                    queue: "user-events-queue",
                    // ... other subscription config
                },
                "order-events": {
                    queue: "order-events-queue",
                    // ... other subscription config
                },
            },
        },
    },
};

// Define your handlers
const handlers = {
    "user-events": async (message, content, ackOrNack, broker) => {
        console.log("Processing user event:", content);
        // Your handler logic here
        ackOrNack();
    },
    "order-events": (message, content, ackOrNack, broker) => {
        console.log("Processing order event:", content);
        // Your handler logic here
        ackOrNack();
    },
};

// Create broker with subscription middleware
const broker = await hoppity.withTopology(topology).use(withSubscriptions(handlers)).build();

Handler Function Signature

type SubscriptionHandler = (
    message: Message,
    content: any,
    ackOrNackFn: AckOrNack,
    broker: BrokerAsPromised
) => Promise<void> | void;

Validation

The middleware validates:

  • Subscription Existence: All handler keys must match subscription names in the topology
  • Handler Functions: All handlers must be functions
  • No Duplicates: No duplicate handler keys allowed

If validation fails, the pipeline fails with a descriptive error message including:

  • Missing subscription names
  • Invalid handlers
  • Duplicate keys
  • Available subscription names for reference

Error Handling

The middleware provides automatic error handling:

  • Message Handler Errors: Caught and logged, message is nacked with the error
  • Subscription Errors: Logged as warnings
  • Invalid Content: Logged as warnings
  • Setup Failures: Pipeline fails immediately with enhanced error context
  • Async Handler Errors: Promise rejections are caught and the message is nacked

Context Integration

The middleware stores validated subscription information in the middleware context:

// Access validated subscriptions from context
context.data.validatedSubscriptions; // Array of subscription names that passed validation

API Reference

withSubscriptions(handlers: SubscriptionHandlers): MiddlewareFunction

Creates a middleware function that sets up subscription handlers.

Parameters

  • handlers: Object mapping subscription names to handler functions

Returns

A middleware function that can be used in the hoppity pipeline.

validateSubscriptionHandlers(topology: BrokerConfig, handlers: SubscriptionHandlers): ValidationResult

Validates subscription handlers against the broker topology. This function is exported for standalone validation if needed.

Parameters

  • topology: The broker topology configuration
  • handlers: The subscription handlers object

Returns

A ValidationResult object with detailed validation information.

Types

SubscriptionHandler

Handler function signature for processing subscription messages.

SubscriptionHandlers

Type for the handlers object: Record<string, SubscriptionHandler>

ValidationResult

Result object from validation with detailed error information:

interface ValidationResult {
    isValid: boolean;
    missingSubscriptions: string[];
    availableSubscriptions: string[];
    invalidHandlers: string[];
    duplicateHandlers: string[];
    errorMessage?: string;
}

Integration with Hoppity

This plugin integrates seamlessly with the hoppity middleware pipeline:

  • Topology Phase: Validates subscriptions and handlers
  • Broker Creation: Creates broker with validated topology
  • Callback Phase: Sets up subscription listeners and handlers

The middleware follows hoppity's fail-fast pattern and provides comprehensive logging throughout the process.

Development

# Install dependencies
pnpm install

# Build the package
pnpm build

# Run tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Lint code
pnpm lint

# Format code
pnpm format

License

ISC

Keywords

hoppity

FAQs

Package last updated on 11 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts