/icetea-skills:durable-objectsfrom icetea-skills
Reviews, creates, and modifies Cloudflare Durable Objects code. Covers DO class structure, storage APIs (SQL and KV), alarms, WebSockets, RPC methods, concurrency patterns, and wrangler configuration.
Ships in icetea-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Build stateful, coordinated applications on Cloudflare's edge using Durable Objects.
Prefer retrieval from official docs over pre-training for Durable Objects tasks.
Fetch the relevant doc page when implementing features.
runInDurableObject(), alarm testing| Need | Example |
|---|---|
| Coordination | Chat rooms, multiplayer games, collaborative docs |
| Strong consistency | Inventory, booking systems, turn-based games |
| Per-entity storage | Multi-tenant SaaS, per-user data |
| Persistent connections | WebSockets, real-time notifications |
| Scheduled work per entity | Subscription renewals, game timeouts |
Quick test: If your database queries consistently filter by WHERE resourceID = 'abc', DOs likely simplify your architecture.
| Layer | Type | Durability | Speed | Use Case |
|---|---|---|---|---|
| In-memory variables | Transient | Lost on eviction | Fastest | Static utilities, ephemeral caches |
ctx.storage.sql | Persistent | Survives eviction | Fast | Primary data, relationships |
ctx.storage KV | Persistent | Survives eviction | Fast | Simple key-value data |
ws.serializeAttachment() | Per-connection | Survives hibernation | Fast | WebSocket session metadata |
| External (R2, D1, KV) | Global | Globally durable | Slower | Shared state, large objects |
Rule: Read from storage on every access — don't cache in instance variables. Storage reads cost ~1/1000th of writes ($0.001/M vs $1/M) and are internally cached, so the performance penalty is negligible. Instance variables are lost on eviction/hibernation and create subtle bugs. Safe instance variable uses: statically-initialized utilities (regex, bound shortcuts), ephemeral caches where under-counting on loss is acceptable.
// wrangler.jsonc
{
"durable_objects": {
"bindings": [{ "name": "MY_DO", "class_name": "MyDurableObject" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyDurableObject"] }]
}
import { DurableObject } from "cloudflare:workers";
export interface Env {
MY_DO: DurableObjectNamespace<MyDurableObject>;
}
export class MyDurableObject extends DurableObject<Env> {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
ctx.blockConcurrencyWhile(async () => {
this.ctx.storage.sql.exec(`
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
data TEXT NOT NULL
)
`);
});
}
async addItem(data: string): Promise<number> {
const result = this.ctx.storage.sql.exec<{ id: number }>(
"INSERT INTO items (data) VALUES (?) RETURNING id",
data
);
return result.one().id;
}
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const stub = env.MY_DO.getByName("my-instance");
const id = await stub.addItem("hello");
return Response.json({ id });
},
};
getByName() for deterministic routing - Same input = same DO instance; see rule #8 for untrusted inputnew_sqlite_classes in migrationsblockConcurrencyWhile() for schema setup onlysetAlarm() replaces any existing alarmgetByName(untrustedInput) creates DOs for ANY string; validate input before creating stubsblockConcurrencyWhile() on every request (kills throughput)await between related storage writes (breaks atomicity)blockConcurrencyWhile() across fetch() or external I/O// Deterministic - preferred for most cases
const stub = env.MY_DO.getByName("room-123");
// WRONG: untrusted input creates orphan DOs
const stub = env.MY_DO.getByName(untrustedInput);
// RIGHT: validate input before creating stub
// See gotchas.md "Leaked DOs" for safe patterns
const entity = await db.findById(untrustedInput);
if (!entity) return notFound();
const stub = env.MY_DO.getByName(entity.id);
// From existing ID string
const id = env.MY_DO.idFromString(storedIdString);
const stub = env.MY_DO.get(id);
// New unique ID - store mapping externally
const id = env.MY_DO.newUniqueId();
const stub = env.MY_DO.get(id);
// SQL (synchronous, recommended)
this.ctx.storage.sql.exec("INSERT INTO t (c) VALUES (?)", value);
const rows = this.ctx.storage.sql.exec<Row>("SELECT * FROM t").toArray();
// KV (async)
await this.ctx.storage.put("key", value);
const val = await this.ctx.storage.get<Type>("key");
// Schedule (replaces existing)
await this.ctx.storage.setAlarm(Date.now() + 60_000);
// Handler
async alarm(): Promise<void> {
// Process scheduled work
// Optionally reschedule: await this.ctx.storage.setAlarm(...)
}
// Cancel
await this.ctx.storage.deleteAlarm();
// Simple: Single column addition (check if exists)
const cols = this.ctx.storage.sql.exec("PRAGMA table_info(items)").toArray();
if (!cols.some(c => c.name === 'status')) {
this.ctx.storage.sql.exec("ALTER TABLE items ADD COLUMN status TEXT");
}
// Complex: Multiple migrations (KV or SQL table tracking)
const version = this.ctx.storage.kv.get("__schema_version") ?? 0;
if (version < 1) { /* migration 1 */ }
if (version < 2) { /* migration 2 */ this.ctx.storage.kv.put("__schema_version", 2); }
Warning:
PRAGMA user_versionis not supported in Durable Objects SQLite storage. Use KV-based or SQL table tracking instead.
An alternative to KV tracking is a _sql_schema_migrations table for visible, queryable migration state — see patterns for the full example.
See Patterns: Schema Migrations for decision tree, both approaches, and trade-offs.
import { env } from "cloudflare:test";
import { describe, it, expect } from "vitest";
describe("MyDO", () => {
it("should work", async () => {
const stub = env.MY_DO.getByName("test");
const result = await stub.addItem("test");
expect(result).toBe(1);
});
});
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
8 skills in this plugin
/agents-sdkBuild AI agents on Cloudflare Workers using the Agents SDK. Load when creating stateful agents, durable workflows, real-time WebSocket apps, scheduled tasks, MCP servers, or chat applications. Covers Agent class, state management, callable RPC, Workflows integration, and React hooks./cloudflareUse when working with Cloudflare platform - Workers, Pages, Queues, KV, D1, R2, Workers AI, Vectorize, Hyperdrive, Containers, Workflows. Triggers on Cloudflare development, deployment, or configuration tasks./http-cachingUse when implementing or troubleshooting HTTP caching headers, ETags, Cache-Control directives, CDN caching strategies, API response caching, ISR, or CORS response caching. Triggers on cache optimization decisions, 304 responses, stale content issues, cache invalidation, Vary header, or stale-while-revalidate usage.Other plugins' skills for the same job
Generated from the plugin's indexed repository (icetea-ai/skills) — no native marketplace selected
npx claudepluginhub p/icetea-ai-icetea-skills