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

waido

v0.4.0

Published

Wide event context library for Express and standalone handlers

Downloads

381

Readme

waido

Yet another wide event library.

Inspired by the wide-event approach from loggingsucks.com.

Focussed on Express middleware or standalone use via withWideContext.

Install

npm install waido

Core idea

  • Build one mutable wide event during execution.
  • Access it anywhere with useLogger() via AsyncLocalStorage to avoid passing it down forever.
  • Emit once at the end (auto by wrappers), with sampler decisions and diagnostics.

Quick start

import { initWaido, useLogger, withWideContext } from "waido";

const waido = initWaido({
  service: "billing-api",
  emitTimeoutMs: 2_000,
  drains: [
    async (event) => {
      console.log(JSON.stringify(event));
    },
  ],
});

const run = await withWideContext({ name: "rebuild-cache" }, async () => {
  const log = useLogger();
  log.setFields({ tenantId: "acme" });
  log.setFields({ cache: { phase: "done" } });
});

if (run.isErr()) {
  console.error(run.error);
}

await waido.destroy();

Result-first wrappers (better-result)

  • withWideContext()
  • flushWideEvents()

New runtime features

1) Lifecycle hooks (enrich + drain)

initWaido({
  enrichers: [
    ({ event }) => {
      event.data.deploymentId = process.env.DEPLOYMENT_ID;
    },
  ],
  drains: [
    async (event) => {
      // send to sink
    },
  ],
});

2) Structured errors (why, fix, link)

import { createStructuredError } from "waido";

throw createStructuredError({
  message: "Payment failed",
  why: "Card declined by issuer",
  fix: "Retry with another card",
  link: "https://docs.example.com/payments",
});

5) Include/exclude filters

Express:

createExpressWideEventMiddleware({
  includePaths: ["/api/**"],
  excludePaths: ["/api/health"],
});

6) Bounded payload policy

initWaido({
  payloadPolicy: {
    maxBytes: 32_000,
    overflowStrategy: "truncate", // "truncate" | "drop" | "error"
  },
});

7) Flush semantics

Manual flush:

import { flushWideEvents } from "waido";

const flush = await flushWideEvents({ timeoutMs: 10_000 });
if (flush.isErr()) {
  // handle timeout
}

flushWideEvents() now waits for both:

  • in-flight wrapper or middleware scopes
  • pending async emit/drain work that has already started

Use it during shutdown after you stop accepting new work.

If you prefer a runtime-local shutdown API:

const waido = initWaido({
  drains: [async (event) => console.log(event)],
});

await waido.destroy({ timeoutMs: 10_000 });

Queue/worker example:

const result = await withWideContext(
  {
    name: `consume ${message.topic}`,
    kind: "queue",
    data: {
      messageId: message.id,
    },
  },
  async () => {
    await handleMessage(message);
  },
  {
    emitTimeoutMs: 2_000,
  },
);

if (result.isErr()) {
  if (result.error._tag === "EmitWideEventTimeoutError") {
    // decide whether to retry or fail the message
  }
}

Express shutdown example:

const waido = initWaido({
  drains: [async (event) => console.log(event)],
});

server.close(async () => {
  const destroy = await waido.destroy({ timeoutMs: 10_000 });

  if (destroy.isErr()) {
    console.error(destroy.error.message);
  }
});

8) Sampling observability

Sampler can return decision metadata:

initWaido({
  sampler: (event) => ({
    sampled: event.outcome === "error",
    reason: event.outcome === "error" ? "always_keep_errors" : "non_error_drop",
    rule: "error_only",
  }),
});

The emitted event includes:

  • sampled (boolean)
  • sampling.reason
  • sampling.rule

9) Trace context helpers

Built-ins:

import { extractTraceContextFromHeaders, parseTraceparent } from "waido";

Express adapter auto-parses traceparent / tracestate headers.

Adapters

Express

import express from "express";
import { createExpressWideEventMiddleware, initWaido, useLogger } from "waido";

const waido = initWaido({
  service: "payments-api",
  emitTimeoutMs: 2_000,
  drains: [async (event) => console.log(event)],
});

const app = express();
app.use(express.json());
app.use(createExpressWideEventMiddleware());

app.get("/users/:id", (req, res) => {
  const log = useLogger();
  log.setFields({ user: { id: req.params.id } });
  res.json({ ok: true });
});

const server = app.listen(3000);

process.on("SIGTERM", () => {
  server.close(async () => {
    await waido.destroy({ timeoutMs: 10_000 });
  });
});

The middleware emits automatically when the response finalizes. Internally it calls awaitWideEventEmit(...), which wraps logger.emit(...).

Redaction and allowlist (userland example)

Redaction/allowlist is intentionally not hardcoded in core. Use an enricher to apply policy in your app:

initWaido({
  enrichers: [
    ({ event }) => {
      // apply allowlist + redact before drains
      event.data = redactAndAllowlist(event.data);
    },
  ],
});

See: examples/redaction-allowlist-userland.ts.

OpenTelemetry emission example

See: examples/emit-to-opentelemetry.ts.

Sentry exception drain example

See: examples/sentry-exception-drain.ts. The example uses an explicit Sentry tag allowlist so high-cardinality fields stay in context, not tags.

Releasing

This project uses changesets for versioning and publishing.

  1. Add a changeset to your PR:

    pnpm changeset

    Select the bump type (patch/minor/major) and describe the change.

  2. Merge the PR into main. The CI will detect pending changesets and open a "Release new version" PR that bumps the version and updates the changelog.

  3. Merge the release PR. CI will publish the new version to npm automatically.