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

chat-state-cloudflare-do

v0.2.0

Published

Cloudflare Durable Objects state adapter for Chat SDK

Readme

chat-state-cloudflare-do

CI npm version npm downloads License: MIT

Cloudflare Durable Objects state adapter for Chat SDK. Uses a SQLite-backed Durable Object for persistent subscriptions, distributed locking, and caching — with zero external dependencies beyond the Workers runtime.

Installation

npm install chat chat-state-cloudflare-do

Usage

import { Chat } from "chat";
import { createSlackAdapter } from "@chat-adapter/slack";
import { createCloudflareState, ChatStateDO } from "chat-state-cloudflare-do";

// Re-export the Durable Object class so Cloudflare can find it
export { ChatStateDO };

export default {
  async fetch(request: Request, env: Env) {
    const bot = new Chat({
      userName: "my-bot",
      adapters: { slack: createSlackAdapter() },
      state: createCloudflareState({ namespace: env.CHAT_STATE }),
    });
    return bot.webhooks.slack(request);
  },
};

Wrangler configuration

Add the Durable Object binding and migration to your wrangler.jsonc (recommended) or wrangler.toml:

wrangler.jsonc (recommended)

{
  "durable_objects": {
    "bindings": [
      { "name": "CHAT_STATE", "class_name": "ChatStateDO" }
    ]
  },
  "migrations": [
    { "tag": "v1", "new_sqlite_classes": ["ChatStateDO"] }
  ]
}

wrangler.toml

[durable_objects]
bindings = [
  { name = "CHAT_STATE", class_name = "ChatStateDO" }
]

[[migrations]]
tag = "v1"
new_sqlite_classes = ["ChatStateDO"]

Environment type

import type { ChatStateDO } from "chat-state-cloudflare-do";

interface Env {
  CHAT_STATE: DurableObjectNamespace<ChatStateDO>;
}

Configuration

| Option | Type | Required | Default | Description | |--------|------|----------|---------|-------------| | namespace | DurableObjectNamespace<ChatStateDO> | Yes | — | Durable Object namespace binding from wrangler config | | name | string | No | "default" | Name for the DO instance | | shardKey | (threadId: string) => string | No | — | Function to derive a shard name from a thread ID | | locationHint | DurableObjectLocationHint | No | — | Location hint for DO placement |

Sharding

A single Durable Object handles approximately 500-1,000 requests per second. For high-traffic bots, use shardKey to distribute load across multiple DO instances:

const state = createCloudflareState({
  namespace: env.CHAT_STATE,
  shardKey: (threadId) => threadId.split(":")[0], // One DO per platform
});

Locks and subscriptions are per-thread, so sharding by any prefix of the thread ID is safe. Cache operations (get/set/delete) always route to the default shard since their keys are not thread-scoped.

| Strategy | shardKey | DOs created | |----------|-----------|-------------| | No sharding (default) | — | 1 | | Per platform | (id) => id.split(":")[0] | 1 per platform | | Per channel | (id) => id.split(":").slice(0, 2).join(":") | 1 per channel |

Architecture

The adapter uses a single Durable Object class (ChatStateDO) with three SQLite tables:

  • subscriptions — thread IDs the bot is subscribed to
  • locks — distributed locks with token-based ownership and TTL
  • cache — key-value pairs with optional TTL

All operations are single-threaded within a DO instance, providing distributed locking via DO atomicity rather than Lua scripts. Expired entries are cleaned up automatically via the Alarms API.

Each method call creates a fresh DO stub. Stubs are cheap (just a JS object) and the Cloudflare docs recommend creating new stubs rather than reusing them after errors.

Features

  • Persistent subscriptions across deployments
  • Distributed locking via single-threaded DO atomicity
  • Key-value caching with TTL
  • Automatic TTL cleanup via Alarms
  • Optional sharding for high-traffic bots
  • Location hints for latency optimization
  • Zero external dependencies (no Redis, no database)

Production recommendations

  • Use Smart Placement to co-locate your Worker with the DO
  • Monitor DO metrics in the Cloudflare dashboard
  • Enable sharding if you expect >500 req/s to a single DO instance
  • Use locationHint to place the DO near your primary user base

Documentation

License

MIT