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

@awsmag/power-msk

v2.2.2

Published

A package to connect and work with AWS Kafka service

Readme

power-msk v2

Resilience-first KafkaJS helpers for production-grade apps on KafkaJS.

👉 Focused on robust consumers & producers + simple Koa middleware.

👉 You bring your own Kafka config (brokers, SSL, SASL, etc).


✨ Features

Resilient Consumer

  • Auto-recreate on fatal errors (STOP / CRASH with restart=false)
  • Partition-level isolation via pause/resume
  • Full-jitter exponential backoff
  • Health & readiness probes

Resilient Producer

  • Auto-reconnect & recreate on send errors
  • Batching (lingerMs, maxBatchSize) for throughput
  • Backpressure (maxQueueBytes) to prevent OOM
  • Idempotence & transactions supported
  • Health & readiness probes

Koa Middleware

  • Exposes a shared resilient producer on ctx.kafkaClient
  • sendMessages(), isHealthy(), isReady() available in requests
  • Graceful shutdown hook (mw.shutdown()) for clean exits

📦 Install

npm install @awsmag/power-msk kafkajs
# or
yarn add @awsmag/power-msk kafkajs

🚀 Quickstart

1) Consumer (eachMessage)

import { Kafka } from "kafkajs";
import { ConsumerSupervisor } from "@awsmag/power-msk";

const kafka = new Kafka({
  clientId: "orders-app",
  brokers: ["b1:9092","b2:9092","b3:9092"],
  ssl: true,
});

const sup = new ConsumerSupervisor({
  kafka,
  groupId: "orders-g1",
  topics: ["orders"],
  eachMessage: async ({ topic, partition, message }) => {
    const payload = JSON.parse(message.value!.toString());
    console.log("Received", payload);
  },
});

await sup.startForever();

2) Consumer (eachBatch)

const sup = new ConsumerSupervisor({
  kafka,
  groupId: "orders-g1",
  topics: ["orders"],
  eachBatch: async ({ batch, commitOffsetsIfNecessary }) => {
    for (const m of batch.messages) {
      console.log("Received", m.offset, m.value?.toString());
    }
    await commitOffsetsIfNecessary();
  },
  runConfig: { autoCommit: false, eachBatchAutoResolve: false },
});

await sup.startForever();

3) Producer

import { ResilientProducer } from "@awsmag/power-msk";

const producer = new ResilientProducer({
  kafka,
  idempotent: true,
  acks: -1,
  lingerMs: 10,
  maxBatchSize: 500,
  maxQueueBytes: 5 * 1024 * 1024,
});

await producer.start();

await producer.sendOne("orders", {
  key: Buffer.from("o:123"),
  value: Buffer.from(JSON.stringify({ id: 123, status: "created" })),
});

4) Koa Middleware

import Koa from "koa";
import Router from "@koa/router";
import { getKafkaClientMw } from "@awsmag/power-msk";

const app = new Koa();
const router = new Router();

const kafkaMw = getKafkaClientMw({
  clientId: "my-app",
  brokers: ["b1:9092", "b2:9092"],
  ssl: true,
});

app.use(kafkaMw);

router.post("/broadcast", async (ctx) => {
  const events = [{ id: 1, msg: "hello" }];
  await ctx.kafkaClient!.sendMessages(events, "my-topic");
  ctx.status = 202;
});

router.get("/healthz", (ctx) => { ctx.status = ctx.kafkaClient!.isHealthy() ? 200 : 500; });
router.get("/readyz",  (ctx) => { ctx.status = ctx.kafkaClient!.isReady() ? 200 : 503; });

app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);

// optional: stop producer cleanly in tests or shutdown scripts
// await kafkaMw.shutdown();

🛠 API

ConsumerSupervisor

  • startForever(): run until stopped
  • stop(): graceful stop
  • isHealthy() / isReady()

ResilientProducer

  • start() / stop()
  • send(topic, messages) / sendOne(topic, message)
  • withTransaction(fn)
  • isHealthy() / isReady()

Koa Middleware

  • getKafkaClientMw(opts)

    • Attaches ctx.kafkaClient with:

      • sendMessages(events[], topic)
      • isHealthy()
      • isReady()
    • Provides .shutdown() for clean exits (esp. in tests)


📚 Detailed Scenarios

Want to understand how the ConsumerSupervisor and ResilientProducer behave in real-world cases (errors, crashes, rebalances, backpressure)?

👉 See SCENARIOS.md for sequence diagrams and lifecycle walkthroughs.


🔄 Migration (v1 → v2)

  • IAM/MSK helpers dropped. Bring your own ssl/sasl.
  • Producer/Consumer APIs are now resilience-focused.
  • Koa middleware switched from per-request producer → shared resilient producer with auto-recreate.

Maintained by AWSMAG C/O S25Digital.