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

@nvana-dharma/dedup-pubsub-publisher

v2.0.0

Published

Generic Pub/Sub publisher with deduplication

Readme

@nvana-dharma/dedup-pubsub-publisher

A Redis-backed publisher wrapper that prevents duplicate messages from being published to Google Cloud Pub/Sub, even with concurrent processes.

Features

  • Exactly-once delivery: Prevents duplicate publishes across multiple concurrent processes
  • Fast and simple: Binary state model (pending/published) with no complex timing logic
  • Automatic cleanup: Deletes Redis keys on publish failure to allow retries
  • Long TTL support: Works with hour-long TTLs for reliable deduplication

Installation

npm install @nvana-dharma/dedup-pubsub-publisher

Usage

import { DedupPubSubPublisher, RedisDedupCache } from "@nvana-dharma/dedup-pubsub-publisher"
import { PubSub } from "@google-cloud/pubsub"

// Create the cache
const cache = new RedisDedupCache({
  redisUrl: "redis://localhost:6379",
  ttlSeconds: 3600, // 1 hour
  keyPrefix: "dedup:",
  logger: logger,
})

// Create your PubSub publisher (implement IPublisher interface)
const pubsubPublisher = new PubSubPublisher({
  projectId: "my-project",
  topicName: "my-topic",
})

// Wrap it with deduplication
const publisher = new DedupPubSubPublisher(pubsubPublisher, cache, logger)

// Connect
await publisher.connect()

// Publish messages
try {
  await publisher.publish(Buffer.from("my message"))
  // Success - message published or was already published
} catch (error) {
  // Publish failed - safe to retry (Redis key was cleaned up)
  console.error("Publish failed:", error)
}

// Get statistics
const stats = publisher.getStats()
console.log(`Published: ${stats.published}, Cached: ${stats.cached}, Failed: ${stats.failed}`)

// Cleanup
await publisher.stop()

How It Works

When you call publish(), the library automatically executes a two-phase commit protocol:

Two-Phase Commit Protocol (Automatic)

  1. Claim: Atomically set Redis key to "pending" state
  2. Publish: Publish message to Pub/Sub
  3. Confirm: On success, set Redis key to "published" state
  4. Cleanup: On failure, delete Redis key and throw error

All of this happens internally - you just call publish() and the library handles the rest.

Deduplication Logic

  • Key doesn't exist → Publish it
  • Key = "published" → Skip (already done)
  • Key = "pending" → Skip (another process is handling it)

Concurrent Process Safety

Process A: Set "pending" → Publishing...
Process B: Tries to publish → Sees "pending" → Skips ✓
Process A: Publish succeeds → Set "published"
Process C: Tries to publish → Sees "published" → Skips ✓

Failure Handling

When publish fails (e.g., Pub/Sub API down):

  1. Redis key is deleted
  2. Error is thrown
  3. Caller can safely retry

API Reference

DedupPubSubPublisher

Constructor

new DedupPubSubPublisher(
  publisher: IPublisher,
  cache: IDedupCache,
  logger: ILogger
)

Methods

publish(data: Buffer): Promise<void>

Publishes a message with deduplication. Throws on failure.

connect(): Promise<void>

Connects to the cache.

stop(): Promise<void>

Gracefully stops the publisher and disconnects from cache.

getStats(): PublishStats

Returns statistics: { published, cached, failed }

resetStats(): void

Resets statistics counters to zero.

RedisDedupCache

Constructor

new RedisDedupCache({
  redisUrl: string       // Redis connection URL
  ttlSeconds: number     // TTL for cache entries (e.g., 3600 for 1 hour)
  keyPrefix: string      // Prefix for all Redis keys
  logger: ILogger        // Logger instance
})

Responsibilities

This Library Handles

✅ Preventing duplicate publishes from concurrent processes ✅ Atomic claim operations (via Redis SETNX) ✅ Cleaning up Redis keys on publish failures

Caller Handles

📋 Checkpoint/retry logic for process crashes 📋 Ensuring messages don't get lost due to failures 📋 Managing application-level state persistence

Key Points

  • TTL: Use a long TTL (e.g., 1 hour) to prevent duplicates across restarts
  • Cache keys: Based on SHA-256 hash of message content
  • Process crashes: Caller should use checkpoints to detect and retry stuck messages
  • Pub/Sub assumptions: If publish() doesn't throw, the message landed successfully

License

MIT