pi-cloudflare 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -177
- package/package.json +7 -42
- package/plugin.json +3 -3
- package/skills/agents-sdk/SKILL.md +17 -36
- package/skills/agents-sdk/references/client-sdk.md +7 -106
- package/skills/agents-sdk/references/human-in-the-loop.md +6 -63
- package/skills/agents-sdk/references/mcp.md +9 -184
- package/skills/agents-sdk/references/queue-retries.md +12 -73
- package/skills/agents-sdk/references/server-driven-messages.md +4 -60
- package/skills/agents-sdk/references/state-scheduling.md +10 -166
- package/skills/agents-sdk/references/streaming-chat.md +11 -193
- package/skills/agents-sdk/references/webhooks-push.md +2 -2
- package/skills/agents-sdk/references/workflows.md +14 -125
- package/skills/cloudflare/SKILL.md +135 -248
- package/skills/cloudflare/references/ai-gateway/README.md +17 -165
- package/skills/cloudflare/references/ai-gateway/configuration.md +14 -107
- package/skills/cloudflare/references/ai-gateway/dynamic-routing.md +12 -79
- package/skills/cloudflare/references/ai-gateway/features.md +22 -96
- package/skills/cloudflare/references/ai-gateway/sdk-integration.md +17 -112
- package/skills/cloudflare/references/ai-gateway/troubleshooting.md +14 -86
- package/skills/cloudflare/references/ai-search/README.md +17 -130
- package/skills/cloudflare/references/ai-search/api.md +10 -84
- package/skills/cloudflare/references/ai-search/configuration.md +16 -88
- package/skills/cloudflare/references/ai-search/gotchas.md +13 -81
- package/skills/cloudflare/references/ai-search/patterns.md +12 -82
- package/skills/cloudflare/references/analytics-engine/api.md +1 -1
- package/skills/cloudflare/references/api/README.md +2 -2
- package/skills/cloudflare/references/api/configuration.md +1 -1
- package/skills/cloudflare/references/bindings/README.md +11 -117
- package/skills/cloudflare/references/bindings/api.md +17 -201
- package/skills/cloudflare/references/bindings/configuration.md +20 -188
- package/skills/cloudflare/references/bindings/gotchas.md +16 -206
- package/skills/cloudflare/references/bindings/patterns.md +15 -195
- package/skills/cloudflare/references/bot-management/README.md +1 -1
- package/skills/cloudflare/references/browser-rendering/README.md +13 -73
- package/skills/cloudflare/references/browser-rendering/api.md +9 -105
- package/skills/cloudflare/references/browser-rendering/configuration.md +9 -75
- package/skills/cloudflare/references/browser-rendering/gotchas.md +12 -85
- package/skills/cloudflare/references/browser-rendering/patterns.md +9 -88
- package/skills/cloudflare/references/c3/README.md +6 -5
- package/skills/cloudflare/references/c3/api.md +3 -4
- package/skills/cloudflare/references/c3/patterns.md +4 -6
- package/skills/cloudflare/references/cache-reserve/README.md +1 -1
- package/skills/cloudflare/references/containers/README.md +15 -77
- package/skills/cloudflare/references/containers/api.md +13 -187
- package/skills/cloudflare/references/containers/configuration.md +15 -188
- package/skills/cloudflare/references/containers/gotchas.md +16 -178
- package/skills/cloudflare/references/containers/patterns.md +14 -198
- package/skills/cloudflare/references/cron-triggers/README.md +13 -92
- package/skills/cloudflare/references/cron-triggers/api.md +11 -191
- package/skills/cloudflare/references/cron-triggers/configuration.md +11 -175
- package/skills/cloudflare/references/cron-triggers/gotchas.md +13 -195
- package/skills/cloudflare/references/cron-triggers/patterns.md +12 -184
- package/skills/cloudflare/references/d1/README.md +9 -127
- package/skills/cloudflare/references/d1/api.md +16 -194
- package/skills/cloudflare/references/d1/configuration.md +16 -189
- package/skills/cloudflare/references/d1/gotchas.md +16 -96
- package/skills/cloudflare/references/d1/patterns.md +14 -183
- package/skills/cloudflare/references/do-storage/README.md +11 -70
- package/skills/cloudflare/references/do-storage/api.md +11 -99
- package/skills/cloudflare/references/do-storage/configuration.md +11 -107
- package/skills/cloudflare/references/do-storage/gotchas.md +12 -147
- package/skills/cloudflare/references/do-storage/patterns.md +11 -190
- package/skills/cloudflare/references/do-storage/testing.md +8 -178
- package/skills/cloudflare/references/email-routing/README.md +14 -84
- package/skills/cloudflare/references/email-routing/api.md +10 -192
- package/skills/cloudflare/references/email-routing/configuration.md +13 -186
- package/skills/cloudflare/references/email-routing/gotchas.md +12 -193
- package/skills/cloudflare/references/email-routing/patterns.md +10 -226
- package/skills/cloudflare/references/email-workers/README.md +13 -146
- package/skills/cloudflare/references/email-workers/api.md +12 -234
- package/skills/cloudflare/references/email-workers/configuration.md +14 -110
- package/skills/cloudflare/references/email-workers/gotchas.md +15 -125
- package/skills/cloudflare/references/email-workers/patterns.md +14 -100
- package/skills/cloudflare/references/flagship/README.md +2 -2
- package/skills/cloudflare/references/hyperdrive/README.md +17 -72
- package/skills/cloudflare/references/hyperdrive/api.md +19 -136
- package/skills/cloudflare/references/hyperdrive/configuration.md +27 -159
- package/skills/cloudflare/references/hyperdrive/gotchas.md +15 -72
- package/skills/cloudflare/references/hyperdrive/patterns.md +12 -187
- package/skills/cloudflare/references/images/README.md +9 -58
- package/skills/cloudflare/references/images/api.md +13 -96
- package/skills/cloudflare/references/images/configuration.md +17 -211
- package/skills/cloudflare/references/images/gotchas.md +16 -99
- package/skills/cloudflare/references/images/patterns.md +16 -115
- package/skills/cloudflare/references/kv/README.md +9 -83
- package/skills/cloudflare/references/kv/api.md +12 -154
- package/skills/cloudflare/references/kv/configuration.md +11 -139
- package/skills/cloudflare/references/kv/gotchas.md +15 -129
- package/skills/cloudflare/references/kv/patterns.md +15 -190
- package/skills/cloudflare/references/miniflare/README.md +15 -98
- package/skills/cloudflare/references/miniflare/api.md +16 -187
- package/skills/cloudflare/references/miniflare/configuration.md +12 -169
- package/skills/cloudflare/references/miniflare/gotchas.md +15 -160
- package/skills/cloudflare/references/miniflare/patterns.md +15 -177
- package/skills/cloudflare/references/network-interconnect/README.md +1 -1
- package/skills/cloudflare/references/network-interconnect/gotchas.md +1 -1
- package/skills/cloudflare/references/network-interconnect/patterns.md +1 -1
- package/skills/cloudflare/references/observability/README.md +20 -81
- package/skills/cloudflare/references/observability/api.md +20 -164
- package/skills/cloudflare/references/observability/configuration.md +18 -164
- package/skills/cloudflare/references/observability/gotchas.md +19 -109
- package/skills/cloudflare/references/observability/patterns.md +12 -103
- package/skills/cloudflare/references/pages/README.md +13 -81
- package/skills/cloudflare/references/pages/api.md +10 -201
- package/skills/cloudflare/references/pages/configuration.md +16 -201
- package/skills/cloudflare/references/pages/gotchas.md +13 -197
- package/skills/cloudflare/references/pages/patterns.md +11 -200
- package/skills/cloudflare/references/pages-functions/README.md +13 -91
- package/skills/cloudflare/references/pages-functions/api.md +10 -140
- package/skills/cloudflare/references/pages-functions/configuration.md +11 -119
- package/skills/cloudflare/references/pages-functions/gotchas.md +17 -94
- package/skills/cloudflare/references/pages-functions/patterns.md +12 -133
- package/skills/cloudflare/references/pipelines/README.md +1 -1
- package/skills/cloudflare/references/pulumi/README.md +2 -2
- package/skills/cloudflare/references/queues/README.md +15 -87
- package/skills/cloudflare/references/queues/api.md +10 -200
- package/skills/cloudflare/references/queues/configuration.md +18 -142
- package/skills/cloudflare/references/queues/gotchas.md +16 -204
- package/skills/cloudflare/references/queues/patterns.md +19 -218
- package/skills/cloudflare/references/r2/README.md +17 -86
- package/skills/cloudflare/references/r2/api.md +14 -198
- package/skills/cloudflare/references/r2/configuration.md +17 -163
- package/skills/cloudflare/references/r2/gotchas.md +15 -188
- package/skills/cloudflare/references/r2/patterns.md +16 -191
- package/skills/cloudflare/references/r2-data-catalog/README.md +11 -70
- package/skills/cloudflare/references/r2-data-catalog/api.md +14 -89
- package/skills/cloudflare/references/r2-data-catalog/configuration.md +15 -96
- package/skills/cloudflare/references/r2-data-catalog/gotchas.md +14 -51
- package/skills/cloudflare/references/r2-data-catalog/patterns.md +11 -116
- package/skills/cloudflare/references/realtime-sfu/README.md +1 -1
- package/skills/cloudflare/references/realtimekit/README.md +1 -1
- package/skills/cloudflare/references/secrets-store/README.md +14 -66
- package/skills/cloudflare/references/secrets-store/api.md +14 -193
- package/skills/cloudflare/references/secrets-store/configuration.md +16 -176
- package/skills/cloudflare/references/secrets-store/gotchas.md +14 -92
- package/skills/cloudflare/references/secrets-store/patterns.md +14 -194
- package/skills/cloudflare/references/smart-placement/README.md +2 -2
- package/skills/cloudflare/references/static-assets/README.md +15 -59
- package/skills/cloudflare/references/static-assets/api.md +12 -195
- package/skills/cloudflare/references/static-assets/configuration.md +16 -186
- package/skills/cloudflare/references/static-assets/gotchas.md +18 -162
- package/skills/cloudflare/references/static-assets/patterns.md +13 -185
- package/skills/cloudflare/references/stream/README.md +15 -107
- package/skills/cloudflare/references/stream/api-live.md +16 -193
- package/skills/cloudflare/references/stream/api.md +19 -197
- package/skills/cloudflare/references/stream/configuration.md +16 -139
- package/skills/cloudflare/references/stream/gotchas.md +16 -128
- package/skills/cloudflare/references/stream/patterns.md +20 -182
- package/skills/cloudflare/references/tail-workers/README.md +14 -82
- package/skills/cloudflare/references/tail-workers/api.md +10 -197
- package/skills/cloudflare/references/tail-workers/configuration.md +13 -174
- package/skills/cloudflare/references/tail-workers/gotchas.md +12 -188
- package/skills/cloudflare/references/tail-workers/patterns.md +11 -176
- package/skills/cloudflare/references/terraform/README.md +2 -2
- package/skills/cloudflare/references/tunnel/README.md +13 -122
- package/skills/cloudflare/references/tunnel/api.md +11 -190
- package/skills/cloudflare/references/tunnel/configuration.md +12 -155
- package/skills/cloudflare/references/tunnel/gotchas.md +12 -144
- package/skills/cloudflare/references/tunnel/networking.md +12 -166
- package/skills/cloudflare/references/tunnel/patterns.md +12 -189
- package/skills/cloudflare/references/vectorize/README.md +15 -126
- package/skills/cloudflare/references/vectorize/api.md +14 -84
- package/skills/cloudflare/references/vectorize/configuration.md +14 -83
- package/skills/cloudflare/references/vectorize/gotchas.md +12 -73
- package/skills/cloudflare/references/vectorize/patterns.md +17 -83
- package/skills/cloudflare/references/waf/README.md +15 -107
- package/skills/cloudflare/references/waf/api.md +17 -202
- package/skills/cloudflare/references/waf/configuration.md +14 -199
- package/skills/cloudflare/references/waf/gotchas.md +15 -200
- package/skills/cloudflare/references/waf/patterns.md +13 -193
- package/skills/cloudflare/references/workerd/README.md +2 -2
- package/skills/cloudflare/references/workers-ai/README.md +14 -187
- package/skills/cloudflare/references/workers-ai/api.md +10 -109
- package/skills/cloudflare/references/workers-ai/configuration.md +11 -92
- package/skills/cloudflare/references/workers-ai/gotchas.md +12 -111
- package/skills/cloudflare/references/workers-ai/patterns.md +9 -115
- package/skills/cloudflare/references/workers-for-platforms/README.md +3 -3
- package/skills/cloudflare/references/workers-vpc/README.md +1 -1
- package/skills/cloudflare/references/workflows/README.md +15 -69
- package/skills/cloudflare/references/workflows/api.md +12 -213
- package/skills/cloudflare/references/workflows/configuration.md +16 -150
- package/skills/cloudflare/references/workflows/gotchas.md +17 -88
- package/skills/cloudflare/references/workflows/patterns.md +19 -164
- package/skills/cloudflare-api-token/SKILL.md +1 -1
- package/skills/cloudflare-cf-cli/SKILL.md +44 -13
- package/skills/cloudflare-email-service/SKILL.md +6 -36
- package/skills/cloudflare-email-service/references/rest-api.md +12 -179
- package/skills/cloudflare-email-service/references/sending.md +17 -250
- package/skills/cloudflare-one/SKILL.md +4 -2
- package/skills/cloudflare-one-migrations/SKILL.md +1 -1
- package/skills/cloudflare-token-roll/SKILL.md +2 -5
- package/skills/durable-objects/SKILL.md +10 -16
- package/skills/durable-objects/references/rules.md +17 -293
- package/skills/durable-objects/references/testing.md +16 -257
- package/skills/nextjs-on-cloudflare/SKILL.md +33 -0
- package/skills/sandbox-migrate-to-next/SKILL.md +2 -2
- package/skills/sandbox-next/SKILL.md +3 -2
- package/skills/sandbox-next/references/api-quick-ref.md +15 -90
- package/skills/sandbox-stable/SKILL.md +1 -1
- package/skills/turnstile-spin/SKILL.md +14 -8
- package/skills/workers-best-practices/SKILL.md +36 -103
- package/skills/workers-best-practices/references/configuration.md +139 -0
- package/skills/workers-best-practices/references/platform-apis.md +51 -0
- package/skills/workers-best-practices/references/{rules.md → runtime-patterns.md} +9 -134
- package/skills/wrangler/SKILL.md +48 -901
- package/bin/setup.mjs +0 -119
- package/dist/api-token.d.ts +0 -12
- package/dist/api-token.d.ts.map +0 -1
- package/dist/api-token.js +0 -23
- package/dist/api-token.js.map +0 -1
- package/dist/auth-flow.d.ts +0 -15
- package/dist/auth-flow.d.ts.map +0 -1
- package/dist/auth-flow.js +0 -90
- package/dist/auth-flow.js.map +0 -1
- package/dist/client.d.ts +0 -18
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -47
- package/dist/client.js.map +0 -1
- package/dist/config.d.ts +0 -20
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -10
- package/dist/config.js.map +0 -1
- package/dist/index.d.ts +0 -22
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -65
- package/dist/index.js.map +0 -1
- package/dist/mcp-server.d.ts +0 -2
- package/dist/mcp-server.d.ts.map +0 -1
- package/dist/mcp-server.js +0 -106
- package/dist/mcp-server.js.map +0 -1
- package/dist/oauth.d.ts +0 -44
- package/dist/oauth.d.ts.map +0 -1
- package/dist/oauth.js +0 -135
- package/dist/oauth.js.map +0 -1
- package/dist/proxy.d.ts +0 -30
- package/dist/proxy.d.ts.map +0 -1
- package/dist/proxy.js +0 -41
- package/dist/proxy.js.map +0 -1
- package/dist/result-caps.d.ts +0 -23
- package/dist/result-caps.d.ts.map +0 -1
- package/dist/result-caps.js +0 -75
- package/dist/result-caps.js.map +0 -1
- package/dist/runtime.d.ts +0 -72
- package/dist/runtime.d.ts.map +0 -1
- package/dist/runtime.js +0 -249
- package/dist/runtime.js.map +0 -1
- package/dist/servers.d.ts +0 -19
- package/dist/servers.d.ts.map +0 -1
- package/dist/servers.js +0 -40
- package/dist/servers.js.map +0 -1
- package/dist/token-store.d.ts +0 -32
- package/dist/token-store.d.ts.map +0 -1
- package/dist/token-store.js +0 -61
- package/dist/token-store.js.map +0 -1
- package/dist/tool-cache.d.ts +0 -25
- package/dist/tool-cache.d.ts.map +0 -1
- package/dist/tool-cache.js +0 -43
- package/dist/tool-cache.js.map +0 -1
- package/mcp.json +0 -15
- package/skills/cloudflare/references/agents-sdk/README.md +0 -89
- package/skills/cloudflare/references/agents-sdk/api.md +0 -190
- package/skills/cloudflare/references/agents-sdk/configuration.md +0 -182
- package/skills/cloudflare/references/agents-sdk/gotchas.md +0 -158
- package/skills/cloudflare/references/agents-sdk/patterns.md +0 -192
- package/skills/cloudflare/references/durable-objects/README.md +0 -185
- package/skills/cloudflare/references/durable-objects/api.md +0 -187
- package/skills/cloudflare/references/durable-objects/configuration.md +0 -160
- package/skills/cloudflare/references/durable-objects/gotchas.md +0 -197
- package/skills/cloudflare/references/durable-objects/patterns.md +0 -201
- package/skills/cloudflare/references/sandbox/README.md +0 -96
- package/skills/cloudflare/references/sandbox/api.md +0 -198
- package/skills/cloudflare/references/sandbox/configuration.md +0 -143
- package/skills/cloudflare/references/sandbox/gotchas.md +0 -194
- package/skills/cloudflare/references/sandbox/patterns.md +0 -201
- package/skills/cloudflare/references/turnstile/README.md +0 -99
- package/skills/cloudflare/references/turnstile/api.md +0 -240
- package/skills/cloudflare/references/turnstile/configuration.md +0 -222
- package/skills/cloudflare/references/turnstile/gotchas.md +0 -218
- package/skills/cloudflare/references/turnstile/patterns.md +0 -193
- package/skills/cloudflare/references/workers/README.md +0 -108
- package/skills/cloudflare/references/workers/api.md +0 -195
- package/skills/cloudflare/references/workers/configuration.md +0 -185
- package/skills/cloudflare/references/workers/frameworks.md +0 -197
- package/skills/cloudflare/references/workers/gotchas.md +0 -137
- package/skills/cloudflare/references/workers/patterns.md +0 -198
- package/skills/cloudflare/references/wrangler/README.md +0 -135
- package/skills/cloudflare/references/wrangler/api.md +0 -188
- package/skills/cloudflare/references/wrangler/configuration.md +0 -197
- package/skills/cloudflare/references/wrangler/gotchas.md +0 -197
- package/skills/cloudflare/references/wrangler/patterns.md +0 -209
- package/skills/cloudflare-auth-setup/SKILL.md +0 -59
- package/skills/workers-best-practices/references/review.md +0 -174
|
@@ -1,295 +1,19 @@
|
|
|
1
1
|
# Durable Objects Rules & Best Practices
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
For
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
// Parent: GameServer tracks match references
|
|
23
|
-
// Children: GameMatch handles individual match state
|
|
24
|
-
async createMatch(name: string): Promise<string> {
|
|
25
|
-
const matchId = crypto.randomUUID();
|
|
26
|
-
this.ctx.storage.sql.exec(
|
|
27
|
-
"INSERT INTO matches (id, name) VALUES (?, ?)",
|
|
28
|
-
matchId, name
|
|
29
|
-
);
|
|
30
|
-
const child = this.env.GAME_MATCH.getByName(matchId);
|
|
31
|
-
await child.init(matchId, name);
|
|
32
|
-
return matchId;
|
|
33
|
-
}
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Location Hints
|
|
37
|
-
|
|
38
|
-
Influence DO creation location for latency-sensitive apps:
|
|
39
|
-
|
|
40
|
-
```typescript
|
|
41
|
-
const id = env.GAME.idFromName(gameId, { locationHint: "wnam" });
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Available hints: `wnam`, `enam`, `sam`, `weur`, `eeur`, `apac`, `oc`, `afr`, `me`.
|
|
45
|
-
|
|
46
|
-
## Storage
|
|
47
|
-
|
|
48
|
-
### SQLite (Recommended)
|
|
49
|
-
|
|
50
|
-
Configure in wrangler:
|
|
51
|
-
```jsonc
|
|
52
|
-
{ "migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyDO"] }] }
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
SQL API is synchronous:
|
|
56
|
-
```typescript
|
|
57
|
-
// Write
|
|
58
|
-
this.ctx.storage.sql.exec(
|
|
59
|
-
"INSERT INTO items (name, value) VALUES (?, ?)",
|
|
60
|
-
name, value
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
// Read
|
|
64
|
-
const rows = this.ctx.storage.sql.exec<{ id: number; name: string }>(
|
|
65
|
-
"SELECT * FROM items WHERE name = ?", name
|
|
66
|
-
).toArray();
|
|
67
|
-
|
|
68
|
-
// Single row
|
|
69
|
-
const row = this.ctx.storage.sql.exec<{ count: number }>(
|
|
70
|
-
"SELECT COUNT(*) as count FROM items"
|
|
71
|
-
).one();
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### Migrations
|
|
75
|
-
|
|
76
|
-
**Note:** `PRAGMA user_version` is **not supported** in Durable Objects SQLite storage. Use a `_sql_schema_migrations` table instead:
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
constructor(ctx: DurableObjectState, env: Env) {
|
|
80
|
-
super(ctx, env);
|
|
81
|
-
ctx.blockConcurrencyWhile(async () => this.migrate());
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private migrate() {
|
|
85
|
-
this.ctx.storage.sql.exec(`
|
|
86
|
-
CREATE TABLE IF NOT EXISTS _sql_schema_migrations (
|
|
87
|
-
id INTEGER PRIMARY KEY,
|
|
88
|
-
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
89
|
-
)
|
|
90
|
-
`);
|
|
91
|
-
|
|
92
|
-
const currentVersion = this.ctx.storage.sql
|
|
93
|
-
.exec<{ version: number }>("SELECT COALESCE(MAX(id), 0) as version FROM _sql_schema_migrations")
|
|
94
|
-
.one().version;
|
|
95
|
-
|
|
96
|
-
if (currentVersion < 1) {
|
|
97
|
-
this.ctx.storage.sql.exec(`
|
|
98
|
-
CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, data TEXT);
|
|
99
|
-
CREATE INDEX IF NOT EXISTS idx_items_data ON items(data);
|
|
100
|
-
INSERT INTO _sql_schema_migrations (id) VALUES (1);
|
|
101
|
-
`);
|
|
102
|
-
}
|
|
103
|
-
if (currentVersion < 2) {
|
|
104
|
-
this.ctx.storage.sql.exec(`
|
|
105
|
-
ALTER TABLE items ADD COLUMN created_at INTEGER;
|
|
106
|
-
INSERT INTO _sql_schema_migrations (id) VALUES (2);
|
|
107
|
-
`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
For production apps, consider [`durable-utils`](https://github.com/lambrospetrou/durable-utils#sqlite-schema-migrations) — provides a `SQLSchemaMigrations` class that tracks executed migrations both in memory and in storage. Also see [`@cloudflare/actors` storage utilities](https://github.com/cloudflare/actors/blob/main/packages/storage/src/sql-schema-migrations.ts) — a reference implementation of the same pattern used by the Cloudflare Actors framework.
|
|
113
|
-
|
|
114
|
-
### State Types
|
|
115
|
-
|
|
116
|
-
| Type | Speed | Persistence | Use Case |
|
|
117
|
-
|------|-------|-------------|----------|
|
|
118
|
-
| Class properties | Fastest | Lost on eviction | Caching, active connections |
|
|
119
|
-
| SQLite storage | Fast | Durable | Primary data |
|
|
120
|
-
| External (R2, D1) | Variable | Durable, cross-DO | Large files, shared data |
|
|
121
|
-
|
|
122
|
-
**Rule**: Always persist critical state to SQLite first, then update in-memory cache.
|
|
123
|
-
|
|
124
|
-
## Concurrency
|
|
125
|
-
|
|
126
|
-
### Input/Output Gates
|
|
127
|
-
|
|
128
|
-
Storage operations automatically block other requests (input gates). Responses wait for writes (output gates).
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
async increment(): Promise<number> {
|
|
132
|
-
// Safe: input gates block interleaving during storage ops
|
|
133
|
-
const val = (await this.ctx.storage.get<number>("count")) ?? 0;
|
|
134
|
-
await this.ctx.storage.put("count", val + 1);
|
|
135
|
-
return val + 1;
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Write Coalescing
|
|
140
|
-
|
|
141
|
-
Multiple writes without `await` between them are batched atomically:
|
|
142
|
-
|
|
143
|
-
```typescript
|
|
144
|
-
// ✅ Good: All three writes commit atomically
|
|
145
|
-
this.ctx.storage.sql.exec("UPDATE accounts SET balance = balance - ? WHERE id = ?", amount, fromId);
|
|
146
|
-
this.ctx.storage.sql.exec("UPDATE accounts SET balance = balance + ? WHERE id = ?", amount, toId);
|
|
147
|
-
this.ctx.storage.sql.exec("INSERT INTO transfers (from_id, to_id, amount) VALUES (?, ?, ?)", fromId, toId, amount);
|
|
148
|
-
|
|
149
|
-
// ❌ Bad: await breaks coalescing
|
|
150
|
-
await this.ctx.storage.put("key1", val1);
|
|
151
|
-
await this.ctx.storage.put("key2", val2); // Separate transaction!
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Race Conditions with External I/O
|
|
155
|
-
|
|
156
|
-
`fetch()` and other non-storage I/O allows interleaving:
|
|
157
|
-
|
|
158
|
-
```typescript
|
|
159
|
-
// ⚠️ Race condition possible
|
|
160
|
-
async processItem(id: string) {
|
|
161
|
-
const item = await this.ctx.storage.get<Item>(`item:${id}`);
|
|
162
|
-
if (item?.status === "pending") {
|
|
163
|
-
await fetch("https://api.example.com/process"); // Other requests can run here!
|
|
164
|
-
await this.ctx.storage.put(`item:${id}`, { status: "completed" });
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
**Solution**: Use optimistic locking (version numbers) or `transaction()`.
|
|
170
|
-
|
|
171
|
-
### blockConcurrencyWhile()
|
|
172
|
-
|
|
173
|
-
Blocks ALL concurrency. Use sparingly - only for initialization:
|
|
174
|
-
|
|
175
|
-
```typescript
|
|
176
|
-
// ✅ Good: One-time init
|
|
177
|
-
constructor(ctx: DurableObjectState, env: Env) {
|
|
178
|
-
super(ctx, env);
|
|
179
|
-
ctx.blockConcurrencyWhile(async () => this.migrate());
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// ❌ Bad: On every request (kills throughput)
|
|
183
|
-
async handleRequest() {
|
|
184
|
-
await this.ctx.blockConcurrencyWhile(async () => {
|
|
185
|
-
// ~5ms = max 200 req/sec
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
**Never** hold across external I/O (fetch, R2, KV).
|
|
191
|
-
|
|
192
|
-
## RPC Methods
|
|
193
|
-
|
|
194
|
-
Use RPC (compatibility date >= 2024-04-03) instead of fetch() handler:
|
|
195
|
-
|
|
196
|
-
```typescript
|
|
197
|
-
export class ChatRoom extends DurableObject<Env> {
|
|
198
|
-
async sendMessage(userId: string, content: string): Promise<Message> {
|
|
199
|
-
// Public methods are RPC endpoints
|
|
200
|
-
const result = this.ctx.storage.sql.exec<{ id: number }>(
|
|
201
|
-
"INSERT INTO messages (user_id, content) VALUES (?, ?) RETURNING id",
|
|
202
|
-
userId, content
|
|
203
|
-
);
|
|
204
|
-
return { id: result.one().id, userId, content };
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Caller
|
|
209
|
-
const stub = env.CHAT_ROOM.getByName(roomId);
|
|
210
|
-
const msg = await stub.sendMessage("user-123", "Hello!"); // Typed!
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### Explicit init() Method
|
|
214
|
-
|
|
215
|
-
DOs don't know their own ID. Pass identity explicitly:
|
|
216
|
-
|
|
217
|
-
```typescript
|
|
218
|
-
async init(entityId: string, metadata: Metadata): Promise<void> {
|
|
219
|
-
await this.ctx.storage.put("entityId", entityId);
|
|
220
|
-
await this.ctx.storage.put("metadata", metadata);
|
|
221
|
-
}
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
## Alarms
|
|
225
|
-
|
|
226
|
-
One alarm per DO. `setAlarm()` replaces existing.
|
|
227
|
-
|
|
228
|
-
```typescript
|
|
229
|
-
// Schedule
|
|
230
|
-
await this.ctx.storage.setAlarm(Date.now() + 60_000);
|
|
231
|
-
|
|
232
|
-
// Handler
|
|
233
|
-
async alarm(): Promise<void> {
|
|
234
|
-
const tasks = this.ctx.storage.sql.exec<Task>(
|
|
235
|
-
"SELECT * FROM tasks WHERE due_at <= ?", Date.now()
|
|
236
|
-
).toArray();
|
|
237
|
-
|
|
238
|
-
for (const task of tasks) {
|
|
239
|
-
await this.processTask(task);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Reschedule if more work
|
|
243
|
-
const next = this.ctx.storage.sql.exec<{ due_at: number }>(
|
|
244
|
-
"SELECT MIN(due_at) as due_at FROM tasks WHERE due_at > ?", Date.now()
|
|
245
|
-
).one();
|
|
246
|
-
if (next?.due_at) {
|
|
247
|
-
await this.ctx.storage.setAlarm(next.due_at);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Get/Delete
|
|
252
|
-
const alarm = await this.ctx.storage.getAlarm();
|
|
253
|
-
await this.ctx.storage.deleteAlarm();
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
**Retry**: Alarms auto-retry on failure. Use idempotent handlers.
|
|
257
|
-
|
|
258
|
-
## WebSockets (Hibernation API)
|
|
259
|
-
|
|
260
|
-
```typescript
|
|
261
|
-
async fetch(request: Request): Promise<Response> {
|
|
262
|
-
const pair = new WebSocketPair();
|
|
263
|
-
this.ctx.acceptWebSocket(pair[1]);
|
|
264
|
-
return new Response(null, { status: 101, webSocket: pair[0] });
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer) {
|
|
268
|
-
const data = JSON.parse(message as string);
|
|
269
|
-
// Handle message
|
|
270
|
-
ws.send(JSON.stringify({ type: "ack" }));
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async webSocketClose(ws: WebSocket, code: number, reason: string) {
|
|
274
|
-
// Cleanup
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Broadcast
|
|
278
|
-
getWebSockets().forEach(ws => ws.send(JSON.stringify(payload)));
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
## Error Handling
|
|
282
|
-
|
|
283
|
-
```typescript
|
|
284
|
-
async safeOperation(): Promise<Result> {
|
|
285
|
-
try {
|
|
286
|
-
return await this.riskyOperation();
|
|
287
|
-
} catch (error) {
|
|
288
|
-
console.error("Operation failed:", error);
|
|
289
|
-
// Log to external service if needed
|
|
290
|
-
throw error; // Re-throw to signal failure to caller
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
**Note**: Uncaught exceptions may terminate the DO instance. In-memory state is lost, but SQLite storage persists.
|
|
3
|
+
Choose one object per entity that needs coordinated state. Keep essential data in durable storage; in-memory state must be reconstructible. Prefer SQLite for new classes, and inspect the backend of existing classes before selecting APIs. For idle WebSocket servers, prefer hibernation and plan for state restoration.
|
|
4
|
+
|
|
5
|
+
Fetch the relevant current documentation before implementing or reviewing changes.
|
|
6
|
+
|
|
7
|
+
| Task | Documentation |
|
|
8
|
+
|------|---------------|
|
|
9
|
+
| Choose object boundaries, deterministic routing, parent-child relationships, or initialization | [Rules of Durable Objects](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/) |
|
|
10
|
+
| Choose SQLite or maintain an existing KV-backed class | [SQLite storage API](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/); [Legacy KV storage API](https://developers.cloudflare.com/durable-objects/api/legacy-kv-storage-api/) |
|
|
11
|
+
| Review storage gates, external I/O races, transactions, or schema initialization | [Rules of Durable Objects](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/); [SQLite storage API](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/); [Durable Object State](https://developers.cloudflare.com/durable-objects/api/state/) |
|
|
12
|
+
| Configure class lifecycle changes | [Class exports](https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/); [Legacy class migrations](https://developers.cloudflare.com/durable-objects/reference/durable-object-class-migrations-legacy/) |
|
|
13
|
+
| Set placement hints or jurisdiction constraints | [Data location](https://developers.cloudflare.com/durable-objects/reference/data-location/) |
|
|
14
|
+
| Create stubs, invoke RPC, or use HTTP handlers | [Invoke methods](https://developers.cloudflare.com/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/); [Namespace API](https://developers.cloudflare.com/durable-objects/api/namespace/) |
|
|
15
|
+
| Schedule per-object work and handle retries | [Alarms](https://developers.cloudflare.com/durable-objects/api/alarms/) |
|
|
16
|
+
| Restore WebSocket connection state after hibernation | [Use WebSockets](https://developers.cloudflare.com/durable-objects/best-practices/websockets/) |
|
|
17
|
+
| Handle exceptions, restarts, and shutdowns | [Error handling](https://developers.cloudflare.com/durable-objects/best-practices/error-handling/); [Object lifecycle](https://developers.cloudflare.com/durable-objects/concepts/durable-object-lifecycle/) |
|
|
18
|
+
|
|
19
|
+
For verification, use [Testing Durable Objects](testing.md). Keep API signatures, configuration, limits, and implementation examples in the linked docs.
|
|
@@ -1,264 +1,23 @@
|
|
|
1
1
|
# Testing Durable Objects
|
|
2
2
|
|
|
3
|
-
Use
|
|
3
|
+
Use Cloudflare’s Vitest integration to exercise Durable Objects in the Workers runtime. Before changing an existing suite, inspect its installed Vitest/Cloudflare packages, configuration, and test scripts. Follow the matching API or migration guide; adding a test does not by itself require migrating the suite.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Fetch the relevant current documentation before writing setup or test code:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
| Task | Documentation |
|
|
8
|
+
|------|---------------|
|
|
9
|
+
| Install compatible packages, configure Vitest and Wrangler, generate test types, run tests | [Write your first test](https://developers.cloudflare.com/workers/testing/vitest-integration/write-your-first-test/) |
|
|
10
|
+
| Migrate an existing pool-based suite | [Migrate to Vitest plugin](https://developers.cloudflare.com/workers/testing/vitest-integration/migration-guides/migrate-to-vitest-plugin/) |
|
|
11
|
+
| Configure bindings, runtime options, or multiple Workers | [Vitest configuration](https://developers.cloudflare.com/workers/testing/vitest-integration/configuration/) |
|
|
12
|
+
| Test RPC, Worker HTTP routes, instance separation, SQLite storage, and alarms | [Testing Durable Objects](https://developers.cloudflare.com/durable-objects/examples/testing-with-durable-objects/) |
|
|
13
|
+
| Inspect internals, enumerate instances, or trigger scheduled alarms with test helpers | [Test APIs](https://developers.cloudflare.com/workers/testing/vitest-integration/test-apis/) |
|
|
14
|
+
| Choose state cleanup and concurrency behavior | [Isolation and concurrency](https://developers.cloudflare.com/workers/testing/vitest-integration/isolation-and-concurrency/) |
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
npm i -D vitest@~3.2.0 @cloudflare/vitest-pool-workers
|
|
11
|
-
```
|
|
16
|
+
Choose tests around the behavior being changed:
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
- Use RPC tests for object behavior and HTTP integration tests for Worker routing and response contracts.
|
|
19
|
+
- Verify that one object retains state across calls and different object identities remain independent. Inspect SQLite state when persistence itself is the contract under test; repeated calls alone do not prove recovery after restart.
|
|
20
|
+
- For alarms, verify the scheduled work’s effects and any rescheduling or cancellation, using the documented helper to avoid waiting for wall-clock time.
|
|
21
|
+
- Check the installed integration’s isolation model before reusing object names. Use separate identities or explicit cleanup where state is shared between tests.
|
|
14
22
|
|
|
15
|
-
|
|
16
|
-
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
|
17
|
-
|
|
18
|
-
export default defineWorkersConfig({
|
|
19
|
-
test: {
|
|
20
|
-
poolOptions: {
|
|
21
|
-
workers: {
|
|
22
|
-
wrangler: { configPath: "./wrangler.toml" },
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### TypeScript Config (test/tsconfig.json)
|
|
30
|
-
|
|
31
|
-
```jsonc
|
|
32
|
-
{
|
|
33
|
-
"extends": "../tsconfig.json",
|
|
34
|
-
"compilerOptions": {
|
|
35
|
-
"moduleResolution": "bundler",
|
|
36
|
-
"types": ["@cloudflare/vitest-pool-workers"]
|
|
37
|
-
},
|
|
38
|
-
"include": ["./**/*.ts", "../src/worker-configuration.d.ts"]
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### Environment Types (env.d.ts)
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
declare module "cloudflare:test" {
|
|
46
|
-
interface ProvidedEnv extends Env {}
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Unit Tests (Direct DO Access)
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
import { env } from "cloudflare:test";
|
|
54
|
-
import { describe, it, expect } from "vitest";
|
|
55
|
-
|
|
56
|
-
describe("Counter DO", () => {
|
|
57
|
-
it("should increment", async () => {
|
|
58
|
-
const stub = env.COUNTER.getByName("test-counter");
|
|
59
|
-
|
|
60
|
-
expect(await stub.increment()).toBe(1);
|
|
61
|
-
expect(await stub.increment()).toBe(2);
|
|
62
|
-
expect(await stub.getCount()).toBe(2);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("isolates different instances", async () => {
|
|
66
|
-
const stub1 = env.COUNTER.getByName("counter-1");
|
|
67
|
-
const stub2 = env.COUNTER.getByName("counter-2");
|
|
68
|
-
|
|
69
|
-
await stub1.increment();
|
|
70
|
-
await stub1.increment();
|
|
71
|
-
await stub2.increment();
|
|
72
|
-
|
|
73
|
-
expect(await stub1.getCount()).toBe(2);
|
|
74
|
-
expect(await stub2.getCount()).toBe(1);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Integration Tests (HTTP via SELF)
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
import { SELF } from "cloudflare:test";
|
|
83
|
-
import { describe, it, expect } from "vitest";
|
|
84
|
-
|
|
85
|
-
describe("Worker HTTP", () => {
|
|
86
|
-
it("should increment via POST", async () => {
|
|
87
|
-
const res = await SELF.fetch("http://example.com?id=test", {
|
|
88
|
-
method: "POST",
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expect(res.status).toBe(200);
|
|
92
|
-
const data = await res.json<{ count: number }>();
|
|
93
|
-
expect(data.count).toBe(1);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it("should get count via GET", async () => {
|
|
97
|
-
await SELF.fetch("http://example.com?id=get-test", { method: "POST" });
|
|
98
|
-
await SELF.fetch("http://example.com?id=get-test", { method: "POST" });
|
|
99
|
-
|
|
100
|
-
const res = await SELF.fetch("http://example.com?id=get-test");
|
|
101
|
-
const data = await res.json<{ count: number }>();
|
|
102
|
-
expect(data.count).toBe(2);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Direct Internal Access
|
|
108
|
-
|
|
109
|
-
Use `runInDurableObject()` to access instance internals and storage:
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
import { env, runInDurableObject } from "cloudflare:test";
|
|
113
|
-
import { describe, it, expect } from "vitest";
|
|
114
|
-
import { Counter } from "../src";
|
|
115
|
-
|
|
116
|
-
describe("DO internals", () => {
|
|
117
|
-
it("can verify storage directly", async () => {
|
|
118
|
-
const stub = env.COUNTER.getByName("direct-test");
|
|
119
|
-
await stub.increment();
|
|
120
|
-
await stub.increment();
|
|
121
|
-
|
|
122
|
-
await runInDurableObject(stub, async (instance: Counter, state) => {
|
|
123
|
-
expect(instance).toBeInstanceOf(Counter);
|
|
124
|
-
|
|
125
|
-
const result = state.storage.sql
|
|
126
|
-
.exec<{ value: number }>(
|
|
127
|
-
"SELECT value FROM counters WHERE name = ?",
|
|
128
|
-
"default"
|
|
129
|
-
)
|
|
130
|
-
.one();
|
|
131
|
-
expect(result.value).toBe(2);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
## List DO IDs
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
import { env, listDurableObjectIds } from "cloudflare:test";
|
|
141
|
-
import { describe, it, expect } from "vitest";
|
|
142
|
-
|
|
143
|
-
describe("DO listing", () => {
|
|
144
|
-
it("can list all IDs in namespace", async () => {
|
|
145
|
-
const id1 = env.COUNTER.idFromName("list-1");
|
|
146
|
-
const id2 = env.COUNTER.idFromName("list-2");
|
|
147
|
-
|
|
148
|
-
await env.COUNTER.get(id1).increment();
|
|
149
|
-
await env.COUNTER.get(id2).increment();
|
|
150
|
-
|
|
151
|
-
const ids = await listDurableObjectIds(env.COUNTER);
|
|
152
|
-
expect(ids.length).toBe(2);
|
|
153
|
-
expect(ids.some(id => id.equals(id1))).toBe(true);
|
|
154
|
-
expect(ids.some(id => id.equals(id2))).toBe(true);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
## Testing Alarms
|
|
160
|
-
|
|
161
|
-
Use `runDurableObjectAlarm()` to trigger alarms immediately:
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
import { env, runInDurableObject, runDurableObjectAlarm } from "cloudflare:test";
|
|
165
|
-
import { describe, it, expect } from "vitest";
|
|
166
|
-
|
|
167
|
-
describe("DO alarms", () => {
|
|
168
|
-
it("can trigger alarms immediately", async () => {
|
|
169
|
-
const stub = env.COUNTER.getByName("alarm-test");
|
|
170
|
-
await stub.increment();
|
|
171
|
-
await stub.increment();
|
|
172
|
-
expect(await stub.getCount()).toBe(2);
|
|
173
|
-
|
|
174
|
-
// Schedule alarm
|
|
175
|
-
await runInDurableObject(stub, async (instance, state) => {
|
|
176
|
-
await state.storage.setAlarm(Date.now() + 60_000);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// Execute immediately without waiting
|
|
180
|
-
const ran = await runDurableObjectAlarm(stub);
|
|
181
|
-
expect(ran).toBe(true);
|
|
182
|
-
|
|
183
|
-
// Verify alarm handler ran (if it resets counter)
|
|
184
|
-
expect(await stub.getCount()).toBe(0);
|
|
185
|
-
|
|
186
|
-
// No alarm scheduled now
|
|
187
|
-
const ranAgain = await runDurableObjectAlarm(stub);
|
|
188
|
-
expect(ranAgain).toBe(false);
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Example alarm handler:
|
|
194
|
-
```typescript
|
|
195
|
-
async alarm(): Promise<void> {
|
|
196
|
-
this.ctx.storage.sql.exec("DELETE FROM counters");
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
## Test Isolation
|
|
201
|
-
|
|
202
|
-
Each test gets isolated storage automatically. DOs from one test don't affect others:
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
describe("Isolation", () => {
|
|
206
|
-
it("first test creates DO", async () => {
|
|
207
|
-
const stub = env.COUNTER.getByName("isolated");
|
|
208
|
-
await stub.increment();
|
|
209
|
-
expect(await stub.getCount()).toBe(1);
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it("second test has fresh state", async () => {
|
|
213
|
-
const ids = await listDurableObjectIds(env.COUNTER);
|
|
214
|
-
expect(ids.length).toBe(0); // Previous test's DO is gone
|
|
215
|
-
|
|
216
|
-
const stub = env.COUNTER.getByName("isolated");
|
|
217
|
-
expect(await stub.getCount()).toBe(0); // Fresh instance
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
## SQLite Storage Testing
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
describe("SQLite", () => {
|
|
226
|
-
it("can verify SQL storage", async () => {
|
|
227
|
-
const stub = env.COUNTER.getByName("sqlite-test");
|
|
228
|
-
await stub.increment("page-views");
|
|
229
|
-
await stub.increment("page-views");
|
|
230
|
-
await stub.increment("api-calls");
|
|
231
|
-
|
|
232
|
-
await runInDurableObject(stub, async (instance, state) => {
|
|
233
|
-
const rows = state.storage.sql
|
|
234
|
-
.exec<{ name: string; value: number }>(
|
|
235
|
-
"SELECT name, value FROM counters ORDER BY name"
|
|
236
|
-
)
|
|
237
|
-
.toArray();
|
|
238
|
-
|
|
239
|
-
expect(rows).toEqual([
|
|
240
|
-
{ name: "api-calls", value: 1 },
|
|
241
|
-
{ name: "page-views", value: 2 },
|
|
242
|
-
]);
|
|
243
|
-
|
|
244
|
-
expect(state.storage.sql.databaseSize).toBeGreaterThan(0);
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
## Running Tests
|
|
251
|
-
|
|
252
|
-
```bash
|
|
253
|
-
npx vitest # Watch mode
|
|
254
|
-
npx vitest run # Single run
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
package.json:
|
|
258
|
-
```json
|
|
259
|
-
{
|
|
260
|
-
"scripts": {
|
|
261
|
-
"test": "vitest"
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
```
|
|
23
|
+
Keep package versions, imports, configuration, helper signatures, and runnable examples in the linked documentation rather than copying them into this reference.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: nextjs-on-cloudflare
|
|
3
|
+
description: Build, migrate, and deploy Next.js apps on Cloudflare Workers with vinext. Use when starting a Next.js project on Cloudflare, moving an existing app to Workers, choosing between vinext and OpenNext, or setting up vinext for Workers. For setup, migration, or deployment, install vinext's upstream skills with `npx skills add cloudflare/vinext` if missing, then read and follow the applicable skill and docs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Next.js on Cloudflare
|
|
7
|
+
|
|
8
|
+
**Use vinext rather than OpenNext for new Next.js projects on Cloudflare Workers.** This is the default in the [Cloudflare Next.js guide](https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/). Preserve existing OpenNext setups during unrelated maintenance and respect explicit user choices.
|
|
9
|
+
|
|
10
|
+
## Why vinext
|
|
11
|
+
|
|
12
|
+
[vinext](https://github.com/cloudflare/vinext) reimplements the Next.js API surface on Vite:
|
|
13
|
+
|
|
14
|
+
- Familiar Next.js development: App Router, Pages Router, React Server Components, and supported `next/*` imports.
|
|
15
|
+
- Vite tooling: fast HMR, native ESM, and the Vite plugin ecosystem.
|
|
16
|
+
- Native Workers integration: local server execution in workerd, access to Cloudflare bindings, and a build-and-deploy workflow.
|
|
17
|
+
- Incremental migration: check compatibility and try vinext alongside an existing Next.js setup.
|
|
18
|
+
|
|
19
|
+
## Use the upstream workflow
|
|
20
|
+
|
|
21
|
+
Before setup, migration, or deployment, check whether the [skills maintained in vinext](https://github.com/cloudflare/vinext/tree/main/.agents/skills) are available. If missing, install them:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npx skills add cloudflare/vinext
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then read and follow the applicable upstream `SKILL.md` and its relevant references. Use the current [vinext docs](https://github.com/cloudflare/vinext#quick-start) for workflows the skills do not cover:
|
|
28
|
+
|
|
29
|
+
- **New project:** follow vinext's [new-project setup](https://github.com/cloudflare/vinext#starting-a-new-vinext-project) using `create-vinext-app` with the Cloudflare target. The upstream migration skill requires an existing Next.js project; do not apply it to an empty directory.
|
|
30
|
+
- **Existing Next.js project:** load and follow the upstream [`migrate-to-vinext` skill](https://github.com/cloudflare/vinext/blob/main/.agents/skills/migrate-to-vinext/SKILL.md), including its compatibility check and relevant references. Select Cloudflare as the deployment target.
|
|
31
|
+
- **Development and deployment:** follow the current [Workers integration docs](https://github.com/cloudflare/vinext#cloudflare-workers).
|
|
32
|
+
|
|
33
|
+
If installation is unavailable, read the linked upstream `SKILL.md` and relevant references directly. Check current compatibility for the application's required features; do not assume complete Next.js parity.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sandbox-migrate-to-next
|
|
3
|
-
description:
|
|
3
|
+
description: Migrate Cloudflare Sandbox apps from stable @cloudflare/sandbox to @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-next for apps already on the preview.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Migrate stable → Sandbox SDK 1.0 preview (`@next`)
|
|
@@ -151,7 +151,7 @@ Delete transport settings entirely. Remove session APIs. Isolate users with **se
|
|
|
151
151
|
|
|
152
152
|
### Deploy cutover
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
Staging/branch first. Production is **one** deploy of matching Worker + image:
|
|
155
155
|
|
|
156
156
|
```sh
|
|
157
157
|
npx wrangler deploy --containers-rollout=immediate
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sandbox-next
|
|
3
|
-
description:
|
|
3
|
+
description: Build or maintain Cloudflare Sandbox apps on @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-migrate-to-next when porting a stable app.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Sandbox SDK — `@next` (1.0 preview)
|
|
@@ -57,7 +57,8 @@ const result = await process.output({ encoding: "utf8" });
|
|
|
57
57
|
// result.stdout, result.exitCode
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
Task-specific API documentation: [references/api-quick-ref.md](references/api-quick-ref.md)
|
|
61
|
+
|
|
61
62
|
Examples index (`next` branch): [references/examples.md](references/examples.md)
|
|
62
63
|
|
|
63
64
|
## 3. Retrieve — open the doc for the task
|