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/basic-do.ts

View on GitHub
/**
 * Basic Durable Object Example: Counter
 *
 * Demonstrates:
 * - DurableObject class structure
 * - RPC methods (recommended pattern)
 * - Key-value storage API
 * - State persistence
 */

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

interface Env {
  COUNTER: DurableObjectNamespace<Counter>;
}

export class Counter extends DurableObject<Env> {
  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);

    // Optional: Initialize from storage
    // Use blockConcurrencyWhile to load before handling requests
    ctx.blockConcurrencyWhile(async () => {
      const value = await ctx.storage.get<number>('value');
      if (value === undefined) {
        // First time initialization
        await ctx.storage.put('value', 0);
      }
    });
  }

  // RPC method: increment counter
  async increment(): Promise<number> {
    let value = await this.ctx.storage.get<number>('value') || 0;
    value += 1;
    await this.ctx.storage.put('value', value);
    return value;
  }

  // RPC method: decrement counter
  async decrement(): Promise<number> {
    let value = await this.ctx.storage.get<number>('value') || 0;
    value -= 1;
    await this.ctx.storage.put('value', value);
    return value;
  }

  // RPC method: get current value
  async get(): Promise<number> {
    return await this.ctx.storage.get<number>('value') || 0;
  }

  // RPC method: reset counter
  async reset(): Promise<void> {
    await this.ctx.storage.put('value', 0);
  }
}

// CRITICAL: Export the class
export default Counter;

/**
 * Worker that uses the Counter DO
 */
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);

    // Get DO stub (using named DO for global counter)
    const id = env.COUNTER.idFromName('global-counter');
    const stub = env.COUNTER.get(id);

    // Or use shortcut for named DOs:
    // const stub = env.COUNTER.getByName('global-counter');

    // Route requests
    if (url.pathname === '/increment' && request.method === 'POST') {
      const count = await stub.increment();
      return new Response(JSON.stringify({ count }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    if (url.pathname === '/decrement' && request.method === 'POST') {
      const count = await stub.decrement();
      return new Response(JSON.stringify({ count }), {
        headers: { 'content-type': 'application/json' },
      });
    }

    if (url.pathname === '/reset' && request.method === 'POST') {
      await stub.reset();
      return new Response(JSON.stringify({ count: 0 }), {
        headers: { 'content-type': 'application/json' },
      });
    }

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

    return new Response('Not found', { status: 404 });
  },
};
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: 197kUpdated 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