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

@stratum-hq/hono

v1.3.2

Published

Hono multi-tenancy middleware: tenant resolution with AsyncLocalStorage context

Readme

@stratum-hq/hono

Hono middleware for Stratum. It extracts tenant identity from a request and sets up AsyncLocalStorage context for downstream handlers.

Read the documentation at docs.stratum-hq.org/packages/hono.

Installation

npm install @stratum-hq/hono @stratum-hq/sdk @stratum-hq/core hono

Quick Start

Read the tenant from a claim of a verified JWT:

import { Hono } from "hono";
import { jwt } from "hono/jwt";
import { stratumMiddleware } from "@stratum-hq/hono";
import { StratumClient, getTenantContext } from "@stratum-hq/sdk";

const app = new Hono();

// A client for the control plane, used by `resolve` below. A `read` key is
// enough to resolve tenant contexts.
const sdkClient = new StratumClient({
  controlPlaneUrl: process.env.STRATUM_URL ?? "http://localhost:3001",
  apiKey: process.env.STRATUM_API_KEY!,
});

const jwtSecret = process.env.JWT_SECRET;
if (!jwtSecret) {
  throw new Error("JWT_SECRET is required to verify bearer tokens.");
}

// hono/jwt rejects a missing or invalid bearer token with 401 and stores the
// verified claims as jwtPayload. Register it before stratumMiddleware.
app.use("*", jwt({ secret: jwtSecret, alg: "HS256" }));

app.use("*", stratumMiddleware({
  // Read the tenant from the verified `tenant_id` claim
  jwtClaim: "tenant_id",
  // Optional: fetch the full tenant context (ancestry, config, permissions)
  resolve: async (tenantId) => sdkClient.resolveTenant(tenantId),
}));

app.get("/users", (c) => {
  const tenantId = c.get("tenantId");        // raw ID set by the middleware
  const ctx = getTenantContext();             // full context via AsyncLocalStorage
  return c.json({ tenantId, config: ctx.resolved_config });
});

stratumMiddleware does not verify a token itself; jwtClaim reads the jwtPayload that Hono's JWT middleware sets.

Client-controlled sources

pathParam and header both take the tenant ID from the request as the client sent it, so any client can name another tenant. Use them only when something you control authorizes the caller for that tenant (for example a later middleware that compares it with a claim of the verified token). Path parameter mode therefore requires trustPathParam: true. Header mode requires trustTenantHeader: true, which is appropriate only when a gateway you control sets the header, removes any client copy, and is the only way to reach the server:

// Your application authorizes the caller for the tenant in the path.
app.use("/tenants/:tenantId/*", stratumMiddleware({ pathParam: "tenantId", trustPathParam: true }));

app.use("*", stratumMiddleware({ header: "x-tenant-id", trustTenantHeader: true }));

Options

stratumMiddleware(options) extracts the tenant ID from exactly one source, in this precedence:

| Option | Behavior | |--------|----------| | jwtClaim | Read the claim from Hono's jwtPayload context variable | | pathParam | Read a URL path parameter (c.req.param(name)). Requires trustPathParam: true. Client-controlled: authorize the caller for that tenant separately | | header | Read a request header (default: x-tenant-id). Requires trustTenantHeader: true | | trustPathParam | Allow path parameter mode. Without it, stratumMiddleware throws at construction when pathParam is set and jwtClaim is not (default: false) | | trustTenantHeader | Allow header mode. Without it, and without jwtClaim or pathParam, stratumMiddleware throws at construction (default: false) | | resolve | Optional callback (tenantId) => TenantContext to populate ancestry, config, and permissions |

If no tenant ID is found, the middleware responds with 400 { error: "Missing tenant ID" }. If resolve rejects with a tenant error from @stratum-hq/core, for example from StratumClient.resolveTenant, the middleware responds with 404 TENANT_NOT_FOUND, 403 TENANT_SUSPENDED, 410 TENANT_ARCHIVED, or 403 FORBIDDEN. A control plane timeout gets 504 CONTROL_PLANE_TIMEOUT. An UnauthorizedError for the SDK's own API key gets 500 CONTROL_PLANE_AUTH_FAILED and a console.error line. Other errors go to the Hono error handler. Without a resolve callback the context is a placeholder (empty config/permissions); provide resolve for real tenant data.

Features

  • Tenant extraction from a verified JWT claim, a path parameter, or a trusted-gateway header
  • Binds tenant context via runWithTenantContext so downstream handlers can call getTenantContext()
  • Lightweight: structural types only, no heavy dependencies

Links

  • Documentation: https://docs.stratum-hq.org/packages/hono/
  • GitHub: https://github.com/stratum-hq/Stratum

License

MIT © Christian Crank