JSPM

chat-adapter-state-durable-object

0.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 4
    • Score
      100M100P100Q36604F
    • License MIT

    Cloudflare Durable Object state adapter for the chat-sdk

    Package Exports

    • chat-adapter-state-durable-object

    Readme

    Chat SDK Durable Object State Adapater

    Cloudflare Durable Object state adapter for chat. Provides persistent subscriptions, distributed locking, and key-value cache via a Durable Object (RPC only, no fetch handler).

    Installation

    pnpm add chat-adapter-state-durable-object chat

    Setup

    1. Export the Durable Object from your Worker and bind it in wrangler.toml. The DO uses SQLite storage (one row per subscription, lock, and cache entry), so you must use a SQLite-backed Durable Object:
    // src/index.ts
    export { ChatStateDurableObject } from "chat-adapter-state-durable-object";
    # wrangler.toml
    [[durable_objects.bindings]]
    name = "EXPORTED_DO"
    class_name = "ChatStateDurableObject"
    
    # Required: enable SQLite for this DO class (run once, then optional to remove)
    [[migrations]]
    tag = "v1"
    new_sqlite_classes = ["ChatStateDurableObject"]
    1. Create the state adapter with a stub and pass it to Chat:
    import { Chat } from "chat";
    import { createDurableObjectState } from "chat-adapter-state-durable-object";
    
    export default {
      async fetch(req: Request, env: Env, ctx: ExecutionContext) {
        // Get a stub - ID could be per tenant, or just a single DO.
        const stub = env.EXPORTED_DO.getByName("chat-state");
    
        const chat = new Chat({
          state: createDurableObjectState(stub),
          // ...adapters, handlers
        });
    
        // ...
      },
    };

    Optional logger for errors:

    const state = createDurableObjectState(stub, { logger: myLogger });

    API

    • createDurableObjectState(stub, options?) – Returns a StateAdapter that forwards all operations to the Durable Object via RPC.
    • ChatStateDurableObject – Durable Object class to bind.

    Development

    Tests use Cloudflare’s Vitest Workers integration and run against a real Durable Object (no mocks). Ensure wrangler.toml and src/worker.ts are present, then:

    pnpm test

    License

    MIT