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

@afauthhq/worker

v0.5.3

Published

AFAuth Cloudflare Workers bindings — createWorker, DurableObjectNonceStore, KvNonceStore, D1AccountStore

Readme

@afauthhq/worker

Cloudflare Workers bindings for the AFAuth Protocol. Wraps @afauthhq/server in a Worker-native router and provides storage implementations backed by Durable Objects, KV, and D1.

Quickstart

import {
  AFAuthNonceDO,
  createNonceDurableObject,
  createWorker,
  DurableObjectNonceStore,
  KvRevocationList,
} from "@afauthhq/worker";
import {
  consoleEmailHandler,
  MemoryAccountStore,
  type DiscoveryDocument,
} from "@afauthhq/server";

// Re-export the nonce DO base class under whatever class_name your
// wrangler.toml binding declares (default: `AFAuthNonceDO`).
export class AFAuthNonceDO extends createNonceDurableObject() {}

interface Env {
  AFAUTH_NONCE_DO: DurableObjectNamespace;
  AFAUTH_REVOCATIONS: KVNamespace;
}

const discovery: DiscoveryDocument = { /* ... */ };
const accounts = new MemoryAccountStore(); // replace with durable impl

export default {
  fetch(req, env: Env, ctx) {
    const handler = createWorker({
      nonceStore: new DurableObjectNonceStore(env.AFAUTH_NONCE_DO),
      revocationList: new KvRevocationList(env.AFAUTH_REVOCATIONS),
      serviceDid: discovery.service_did,
      accounts,
      recipients: { email: consoleEmailHandler },
      discovery,
      baseUrl: "https://api.example.com",
      extractOwnerSession: async (req) => /* your session extraction */ null,
    });
    return handler.fetch!(req, env, ctx);
  },
};

wrangler.toml binding for the DO:

[[durable_objects.bindings]]
name       = "AFAUTH_NONCE_DO"
class_name = "AFAuthNonceDO"

[[migrations]]
tag         = "v1"
new_classes = ["AFAuthNonceDO"]

Spam-resistant defaults

createWorker mirrors new Server({...}) rather than the higher-level defineService factory — the Worker needs explicit options for its DO bindings, KV namespaces, and extractOwnerSession. To get the same spam-resistance, pass trustAttestor() and declare attested_only on the discovery doc:

import { trustAttestor } from "@afauthhq/server";

const discovery: DiscoveryDocument = {
  /* ... */
  billing: {
    unclaimed_mode: "attested_only",
    accepted_attestors: ["afauth-trust"],
  },
};

createWorker({
  /* ... */
  attestor: trustAttestor(),
  discovery,
  // §10.4.4 "same human, same bucket". Because `createWorker` mirrors
  // `new Server` (not `defineService`), per-principal uniqueness is NOT
  // defaulted — pass it explicitly. `D1SubHUniquenessStore` gives an
  // atomic, durable claim via its UNIQUE index:
  subHUniqueness: new D1SubHUniquenessStore(env.AFAUTH_DB),
});

Agents that haven't run afauth trust link will be rejected with 401 attestation_required; the afauth signup CLI guides them through the link flow on that error. A second agent for a human who already has an account here is rejected with 409 principal_already_registered. To free a human's slot when their unclaimed account expires, pass the same store to sweepExpiredAccounts (subHUniqueness).

Nonce store: pick DO, not KV

§5.6 requires the seen-nonce set be shared and atomic across verifier instances. Cloudflare KV is shared but offers no atomic check-and-set: a get-then-put window admits cross-isolate replay during the freshness window.

| Store | Atomic? | Shared? | When to use | |---|---|---|---| | DurableObjectNonceStore | yes | yes | recommended for production | | KvNonceStore | no | yes | dev only, or single-region low-value deployments where the trade-off is documented | | MemoryNonceStore (from @afauthhq/server) | yes | no | tests only |

DurableObjectNonceStore partitions by keyid so unrelated agents fan out across distinct actor instances; only requests from the same agent share an actor and serialize against each other.

Exports

  • createWorker(opts) — returns an ExportedHandler routing the five AFAuth endpoints to @afauthhq/server handlers. Routing is done with a small in-house router (ADR-0002).
  • createNonceDurableObject() — factory that returns a Durable-Object base class implementing the §5.6 atomic check-and-set protocol. Subclass it in your Worker module and register the subclass in wrangler.toml.
  • DurableObjectNonceStoreNonceStore that delegates to the DO above. Spec-compliant atomic insert; recommended for production.
  • KvNonceStoreNonceStore backed by Cloudflare KV. Has a known eventual-consistency replay window; see the JSDoc on the class. Suitable for dev/low-value deployments only.
  • KvRevocationListRevocationList backed by Cloudflare KV (§8.3). Durable; no TTL.
  • KvAttestedFreshnessStoreAttestedFreshnessStore backed by Cloudflare KV (§10.7). Stores each account's attestedUntil; the KV entry's TTL is set to the remaining window, so lapsed sessions self-evict. Pass to Server via attestedSession: { store }.
  • KvRateLimiterRateLimiter backed by Cloudflare KV (§11.3). Fixed-window counter per key; eventually-consistent reads mean racing isolates may over-count (fail-safe per §11.3), never under-count.
  • D1AccountStoreAccountStore backed by Cloudflare D1 (§6 + §7.3). Every ADR-0004 named atomic op uses D1.batch() for transactional grouping. The schema lives at migrations/0001_init.sql; apply via wrangler d1 migrations apply <db-name> before first use. Schema is portable to standard Postgres/MySQL with minor syntactic changes.
  • D1SubHUniquenessStoreSubHUniquenessStore backed by Cloudflare D1 (§10.4.4), in the same database as D1AccountStore. Enforces "at most one account per human": claim() is atomic via the composite PRIMARY KEY (iss, sub_h) (INSERT … ON CONFLICT DO NOTHING), which a KvNonceStore-style get-then-put could not give — so there is no KV variant. Schema lives at migrations/0002_subh_uniqueness.sql; apply after 0001. Pass via subHUniqueness (and to sweepExpiredAccounts to release slots on expiry).
  • WorkerOptions — extends ServerOptions with the required extractOwnerSession callback for the claim-completion route.

See also

  • AFAuthHQ/spec — protocol spec.
  • @afauthhq/server — the handlers createWorker dispatches to.
  • examples/worker/ — runnable reference Worker that prefers DO when its binding is configured and falls back to KV (with a warning) otherwise.