Skip to content

secondsky/claude-skills / plugins/cloudflare-durable-objects/skills/cloudflare-durable-objects

cloudflare-durable-objects

Verified

Cloudflare Durable Objects for stateful coordination and real-time apps. Use for chat, multiplayer games, WebSocket hibernation, or encountering class export, migration, alarm errors.

FileKindSize
SKILL.mdskill md21.6 KB
references/advanced-sql-patterns.mdmarkdown15.8 KB
references/alarms-api.mdmarkdown6.5 KB
references/best-practices.mdmarkdown8.5 KB
references/common-patterns.mdmarkdown16.7 KB
references/data-modeling.mdmarkdown16.9 KB
references/error-codes.mdmarkdown7.7 KB
references/gradual-deployments.mdmarkdown13.9 KB
references/migration-cheatsheet.mdmarkdown3.2 KB
references/migrations-guide.mdmarkdown7.3 KB
references/monitoring-debugging.mdmarkdown18.2 KB
references/performance-optimization.mdmarkdown16.2 KB
references/rpc-metadata.mdmarkdown16.9 KB
references/rpc-patterns.mdmarkdown6.5 KB
references/security-best-practices.mdmarkdown15.9 KB
references/state-api-reference.mdmarkdown6.0 KB
references/stubs-routing.mdmarkdown10.6 KB
references/top-errors.mdmarkdown10.2 KB
references/typescript-config.mdmarkdown8.3 KB
references/vitest-testing.mdmarkdown18.1 KB
references/websocket-hibernation.mdmarkdown6.3 KB
references/wrangler-commands.mdmarkdown3.4 KB
scripts/check-versions.shexecutablescript593 B
scripts/migration-generator.shexecutablescript13.4 KB
scripts/setup-vitest-do.shexecutablescript10.2 KB
scripts/validate-do-config.shexecutablescript10.0 KB
templates/alarms-api-do.tsscript6.1 KB
templates/basic-do.tsscript2.9 KB
templates/gradual-deployment-config.jsoncother10.4 KB
templates/location-hints.tsscript6.8 KB
templates/multi-do-coordination.tsscript7.8 KB
templates/package.jsonmanifest384 B
templates/rpc-metadata-do.tsscript10.8 KB
templates/rpc-vs-fetch.tsscript6.0 KB
templates/state-api-patterns.tsscript5.8 KB
templates/ttl-cleanup-do.tsscript17.6 KB
templates/vitest-do-test.tsscript12.3 KB
templates/websocket-hibernation-do.tsscript6.0 KB
templates/wrangler-do-config.jsoncother2.0 KB

templates/rpc-vs-fetch.ts

View on GitHub
/**
 * RPC vs HTTP Fetch Patterns
 *
 * Demonstrates:
 * - RPC methods (recommended for compat_date >= 2024-04-03)
 * - HTTP fetch handler (for HTTP flows or legacy compatibility)
 * - When to use each pattern
 */

import { DurableObject, DurableObjectState } from 'cloudflare:workers';

interface Env {
  RPC_EXAMPLE: DurableObjectNamespace<RpcExample>;
  FETCH_EXAMPLE: DurableObjectNamespace<FetchExample>;
}

/**
 * Pattern 1: RPC Methods (Recommended)
 *
 * ✅ Use when:
 * - New project (compat_date >= 2024-04-03)
 * - Type safety is important
 * - Simple method calls (not HTTP-specific logic)
 * - Auto-serialization of structured data
 */
export class RpcExample extends DurableObject<Env> {
  count: number = 0;

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);

    ctx.blockConcurrencyWhile(async () => {
      this.count = await ctx.storage.get<number>('count') || 0;
    });
  }

  // Public RPC methods (automatically exposed)
  async increment(): Promise<number> {
    this.count += 1;
    await this.ctx.storage.put('count', this.count);
    return this.count;
  }

  async decrement(): Promise<number> {
    this.count -= 1;
    await this.ctx.storage.put('count', this.count);
    return this.count;
  }

  async get(): Promise<number> {
    return this.count;
  }

  async reset(): Promise<void> {
    this.count = 0;
    await this.ctx.storage.put('count', 0);
  }

  // Complex return types work seamlessly
  async getStats(): Promise<{ count: number; timestamp: number }> {
    return {
      count: this.count,
      timestamp: Date.now(),
    };
  }

  // Methods can accept complex parameters
  async addMultiple(numbers: number[]): Promise<number> {
    const sum = numbers.reduce((acc, n) => acc + n, 0);
    this.count += sum;
    await this.ctx.storage.put('count', this.count);
    return this.count;
  }
}

/**
 * Worker using RPC pattern
 */
const rpcWorker = {
  async fetch(request: Request, env: Env): Promise<Response> {
    // Get stub
    const stub = env.RPC_EXAMPLE.getByName('my-counter');

    // Call RPC methods directly (type-safe)
    const count = await stub.increment();
    const stats = await stub.getStats();

    return new Response(JSON.stringify({ count, stats }), {
      headers: { 'content-type': 'application/json' },
    });
  },
};

/**
 * Pattern 2: HTTP Fetch Handler (Legacy / HTTP-specific flows)
 *
 * ✅ Use when:
 * - Need HTTP request/response pattern
 * - Complex routing logic
 * - WebSocket upgrade (requires fetch)
 * - Legacy compatibility (pre-2024-04-03)
 */
export class FetchExample extends DurableObject<Env> {
  count: number = 0;

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);

    ctx.blockConcurrencyWhile(async () => {
      this.count = await ctx.storage.get<number>('count') || 0;
    });
  }

  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);

    // Route based on path
    if (url.pathname === '/increment' && request.method === 'POST') {
      this.count += 1;
      await this.ctx.storage.put('count', this.count);

      return new Response(JSON.stringify({ count: this.count }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    if (url.pathname === '/decrement' && request.method === 'POST') {
      this.count -= 1;
      await this.ctx.storage.put('count', this.count);

      return new Response(JSON.stringify({ count: this.count }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    if (url.pathname === '/get' && request.method === 'GET') {
      return new Response(JSON.stringify({ count: this.count }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    if (url.pathname === '/reset' && request.method === 'POST') {
      this.count = 0;
      await this.ctx.storage.put('count', 0);

      return new Response(JSON.stringify({ count: 0 }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    // Complex HTTP logic (headers, cookies, etc.)
    if (url.pathname === '/stats' && request.method === 'GET') {
      const authHeader = request.headers.get('Authorization');

      if (!authHeader) {
        return new Response('Unauthorized', { status: 401 });
      }

      return new Response(JSON.stringify({
        count: this.count,
        timestamp: Date.now(),
      }), {
        headers: {
          'content-type': 'application/json',
          'cache-control': 'no-cache',
        },
      });
    }

    return new Response('Not found', { status: 404 });
  }
}

/**
 * Worker using HTTP fetch pattern
 */
const fetchWorker = {
  async fetch(request: Request, env: Env): Promise<Response> {
    // Get stub
    const stub = env.FETCH_EXAMPLE.getByName('my-counter');

    // Call fetch method (HTTP-style)
    const response = await stub.fetch('https://fake-host/increment', {
      method: 'POST',
    });

    const data = await response.json();

    return new Response(JSON.stringify(data), {
      headers: { 'content-type': 'application/json' },
    });
  },
};

/**
 * Pattern 3: Hybrid (RPC + Fetch)
 *
 * Use both patterns in the same DO:
 * - RPC for simple method calls
 * - fetch() for WebSocket upgrades or HTTP-specific logic
 */
export class HybridExample extends DurableObject<Env> {
  // RPC method
  async getStatus(): Promise<{ active: boolean; connections: number }> {
    return {
      active: true,
      connections: this.ctx.getWebSockets().length,
    };
  }

  // HTTP fetch for WebSocket upgrade
  async fetch(request: Request): Promise<Response> {
    const upgradeHeader = request.headers.get('Upgrade');

    if (upgradeHeader === 'websocket') {
      // WebSocket upgrade logic
      const webSocketPair = new WebSocketPair();
      const [client, server] = Object.values(webSocketPair);

      this.ctx.acceptWebSocket(server);

      return new Response(null, {
        status: 101,
        webSocket: client,
      });
    }

    return new Response('Not found', { status: 404 });
  }
}

// CRITICAL: Export classes
export default RpcExample;
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall