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

@farfarawaylabs/chat-sdk-adapter-kv

v0.2.0

Published

Cloudflare KV state adapter for the Vercel Chat SDK

Downloads

155

Readme

@farfarawaylabs/chat-sdk-adapter-kv

Cloudflare KV state adapter for the Vercel Chat SDK. A drop-in replacement for the Redis state adapter, designed for Cloudflare Workers.

Installation

npm install @farfarawaylabs/chat-sdk-adapter-kv

Usage

Pass your KV namespace binding to createCloudflareKVState():

import { Chat } from "chat";
import { createCloudflareKVState } from "@farfarawaylabs/chat-sdk-adapter-kv";

export default {
  async fetch(request: Request, env: Env) {
    const bot = new Chat({
      userName: "mybot",
      adapters: { /* ... */ },
      state: createCloudflareKVState({ kv: env.CHAT_KV }),
    });
  },
};

Wrangler configuration

Add a KV namespace binding in your wrangler.toml:

[[kv_namespaces]]
binding = "CHAT_KV"
id = "your-kv-namespace-id"

Key prefix

All keys are namespaced under a configurable prefix (default: "chat-sdk"):

const state = createCloudflareKVState({
  kv: env.CHAT_KV,
  keyPrefix: "my-bot",
});

Custom logger

const state = createCloudflareKVState({
  kv: env.CHAT_KV,
  logger: myLogger,
});

Configuration

| Option | Required | Description | | ----------- | -------- | ---------------------------------------------- | | kv | Yes | Cloudflare KV namespace binding | | keyPrefix | No | Prefix for all keys (default: "chat-sdk") | | logger | No | Logger instance (defaults to ConsoleLogger) |

Key structure

{keyPrefix}:sub:{threadId}    - Subscription marker for a thread
{keyPrefix}:lock:{threadId}   - Lock key with TTL
{keyPrefix}:cache:{key}       - Cached key-value entry
{keyPrefix}:list:{key}        - List stored as JSON array

Features

| Feature | Supported | | ------------------------ | --------- | | Persistence | Yes | | Subscriptions | Yes | | Key-value caching | Yes | | Key prefix namespacing | Yes | | Best-effort locking | Yes | | Atomic distributed locks | No |

Limitations

Cloudflare KV has different characteristics than Redis. Be aware of these trade-offs:

Eventual consistency

KV is eventually consistent. Reads may return stale data for up to 60 seconds after a write. This can affect isSubscribed checks and lock visibility across workers in different regions.

Non-atomic locking

Lock operations (acquireLock, releaseLock, extendLock) and setIfNotExists use read-then-write patterns that are not atomic. Under high concurrency, two workers could both acquire the same lock. If you need reliable distributed locking, use Cloudflare Durable Objects instead.

Minimum TTL

KV's minimum expiration TTL is 60 seconds. Shorter TTLs are enforced logically via stored expiration timestamps — the adapter will treat the lock as expired, but the KV key won't be physically deleted until at least 60 seconds have passed.

Concurrent list appends

appendToList reads the full list, modifies it in memory, and writes it back. Concurrent appends from different workers may overwrite each other. For high-write list workloads, consider using Durable Objects or D1 instead.

When to use this adapter

This adapter is a good fit when:

  • You are running on Cloudflare Workers and want a simple state backend
  • Your workload has low to moderate concurrency
  • Eventual consistency is acceptable for your use case
  • You don't need strict distributed locking guarantees

If you need strong consistency or atomic operations, consider Durable Objects or D1 as your state backend.

License

MIT