pi-cloudflare 0.10.2 → 0.11.1
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 -180
- 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,150 +1,15 @@
|
|
|
1
|
-
# DO Storage
|
|
1
|
+
# DO Storage Troubleshooting
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Identify the backend and failing operation before applying concurrency or recovery guidance.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Fetch the relevant current documentation before implementing or reviewing changes.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Output Gates
|
|
20
|
-
Hold response until ALL writes from current request confirm:
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
// SAFE: Output gate waits for put() to confirm before returning response
|
|
24
|
-
async increment() {
|
|
25
|
-
const val = await this.ctx.storage.get("counter");
|
|
26
|
-
this.ctx.storage.put("counter", val + 1); // No await
|
|
27
|
-
return new Response(String(val)); // Response delayed until write confirms
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Write Coalescing
|
|
32
|
-
Multiple writes to same key = atomic (last write wins):
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
// SAFE: All three writes coalesce atomically
|
|
36
|
-
this.ctx.storage.put("key", 1);
|
|
37
|
-
this.ctx.storage.put("key", 2);
|
|
38
|
-
this.ctx.storage.put("key", 3); // Final value: 3
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### Breaking Gates (DANGER)
|
|
42
|
-
|
|
43
|
-
**fetch() breaks input/output gates** → allows request interleaving:
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
// UNSAFE: fetch() allows another request to interleave
|
|
47
|
-
async unsafe() {
|
|
48
|
-
const val = await this.ctx.storage.get("counter");
|
|
49
|
-
await fetch("https://api.example.com"); // Gate broken!
|
|
50
|
-
await this.ctx.storage.put("counter", val + 1); // Race condition possible
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
**Solution:** Use `blockConcurrencyWhile()` or `transaction()`:
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
// SAFE: Block concurrent requests explicitly
|
|
58
|
-
async safe() {
|
|
59
|
-
return await this.ctx.blockConcurrencyWhile(async () => {
|
|
60
|
-
const val = await this.ctx.storage.get("counter");
|
|
61
|
-
await fetch("https://api.example.com");
|
|
62
|
-
await this.ctx.storage.put("counter", val + 1);
|
|
63
|
-
return val;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### allowConcurrency Option
|
|
69
|
-
|
|
70
|
-
Opt out of input gate for reads that don't need protection:
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Allow concurrent reads (no consistency guarantee)
|
|
74
|
-
const val = await this.ctx.storage.get("metrics", { allowConcurrency: true });
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Common Errors
|
|
78
|
-
|
|
79
|
-
### "Race Condition in Concurrent Calls"
|
|
80
|
-
|
|
81
|
-
**Cause:** Multiple concurrent storage operations initiated from same event (e.g., `Promise.all()`) are not protected by input gate
|
|
82
|
-
**Solution:** Avoid concurrent storage operations within single event; input gate only serializes requests from different events, not operations within same event
|
|
83
|
-
|
|
84
|
-
### "Direct SQL Transaction Statements"
|
|
85
|
-
|
|
86
|
-
**Cause:** Using `BEGIN TRANSACTION` directly instead of transaction methods
|
|
87
|
-
**Solution:** Use `this.ctx.storage.transactionSync()` for sync operations or `this.ctx.storage.transaction()` for async operations
|
|
88
|
-
|
|
89
|
-
### "Async in transactionSync"
|
|
90
|
-
|
|
91
|
-
**Cause:** Using async operations inside `transactionSync()` callback
|
|
92
|
-
**Solution:** Use async `transaction()` method instead of `transactionSync()` when async operations needed
|
|
93
|
-
|
|
94
|
-
### "TypeScript Type Mismatch at Runtime"
|
|
95
|
-
|
|
96
|
-
**Cause:** Query doesn't return all fields specified in TypeScript type
|
|
97
|
-
**Solution:** Ensure SQL query selects all columns that match the TypeScript type definition
|
|
98
|
-
|
|
99
|
-
### "Silent Data Corruption with Large IDs"
|
|
100
|
-
|
|
101
|
-
**Cause:** JavaScript numbers have 53-bit precision; SQLite INTEGER is 64-bit
|
|
102
|
-
**Symptom:** IDs > 9007199254740991 (Number.MAX_SAFE_INTEGER) silently truncate/corrupt
|
|
103
|
-
**Solution:** Store large IDs as TEXT:
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
// BAD: Snowflake/Twitter IDs will corrupt
|
|
107
|
-
this.sql.exec("CREATE TABLE events(id INTEGER PRIMARY KEY)");
|
|
108
|
-
this.sql.exec("INSERT INTO events VALUES (?)", 1234567890123456789n); // Corrupts!
|
|
109
|
-
|
|
110
|
-
// GOOD: Store as TEXT
|
|
111
|
-
this.sql.exec("CREATE TABLE events(id TEXT PRIMARY KEY)");
|
|
112
|
-
this.sql.exec("INSERT INTO events VALUES (?)", "1234567890123456789");
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### "Alarm Not Deleted with deleteAll()"
|
|
116
|
-
|
|
117
|
-
**Cause:** `deleteAll()` doesn't delete alarms automatically
|
|
118
|
-
**Solution:** Call `deleteAlarm()` explicitly before `deleteAll()` to remove alarm
|
|
119
|
-
|
|
120
|
-
### "Slow Performance"
|
|
121
|
-
|
|
122
|
-
**Cause:** Using async KV API instead of sync API
|
|
123
|
-
**Solution:** Use sync KV API (`ctx.storage.kv`) for better performance with simple key-value operations
|
|
124
|
-
|
|
125
|
-
### "High Billing from Storage Operations"
|
|
126
|
-
|
|
127
|
-
**Cause:** Excessive `rowsRead`/`rowsWritten` or unused objects not cleaned up
|
|
128
|
-
**Solution:** Monitor `rowsRead`/`rowsWritten` metrics and ensure unused objects call `deleteAll()`
|
|
129
|
-
|
|
130
|
-
### "Durable Object Overloaded"
|
|
131
|
-
|
|
132
|
-
**Cause:** Single DO exceeding ~1K req/sec soft limit
|
|
133
|
-
**Solution:** Shard across multiple DOs with random IDs or other distribution strategy
|
|
134
|
-
|
|
135
|
-
## Limits
|
|
136
|
-
|
|
137
|
-
| Limit | Value | Notes |
|
|
138
|
-
|-------|-------|-------|
|
|
139
|
-
| Max columns per table | 100 | SQL limitation |
|
|
140
|
-
| Max string/BLOB per row | 2 MB | SQL limitation |
|
|
141
|
-
| Max row size | 2 MB | SQL limitation |
|
|
142
|
-
| Max SQL statement size | 100 KB | SQL limitation |
|
|
143
|
-
| Max SQL parameters | 100 | SQL limitation |
|
|
144
|
-
| Max LIKE/GLOB pattern | 50 B | SQL limitation |
|
|
145
|
-
| SQLite storage per object | 10 GB | SQLite-backed storage |
|
|
146
|
-
| SQLite key+value size | 2 MB | SQLite-backed storage |
|
|
147
|
-
| KV storage per object | Unlimited | KV-style storage |
|
|
148
|
-
| KV key size | 2 KiB | KV-style storage |
|
|
149
|
-
| KV value size | 128 KiB | KV-style storage |
|
|
150
|
-
| Request throughput | ~1K req/sec | Soft limit per DO |
|
|
7
|
+
| Task | Documentation |
|
|
8
|
+
|------|---------------|
|
|
9
|
+
| Input/output gates, write coalescing, external I/O races, or storage options | [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/); [Legacy KV storage API](https://developers.cloudflare.com/durable-objects/api/legacy-kv-storage-api/) |
|
|
10
|
+
| SQL transactions, synchronous callbacks, parameter types, or numeric precision | [SQLite storage API](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/) |
|
|
11
|
+
| Alarm cancellation and storage deletion | [Alarms](https://developers.cloudflare.com/durable-objects/api/alarms/); [SQLite storage API](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/) |
|
|
12
|
+
| Slow queries, indexing, caching, or initialization | [Rules of Durable Objects](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/); [Durable Object State](https://developers.cloudflare.com/durable-objects/api/state/) |
|
|
13
|
+
| Storage limits or CPU exhaustion | [Limits](https://developers.cloudflare.com/durable-objects/platform/limits/) |
|
|
14
|
+
| Storage charges and operation accounting | [Pricing](https://developers.cloudflare.com/durable-objects/platform/pricing/) |
|
|
15
|
+
| Overload, storage timeouts, or object resets | [Troubleshooting](https://developers.cloudflare.com/durable-objects/observability/troubleshooting/); [Error handling](https://developers.cloudflare.com/durable-objects/best-practices/error-handling/) |
|
|
@@ -1,194 +1,15 @@
|
|
|
1
|
-
# DO Storage Patterns
|
|
1
|
+
# DO Storage Patterns
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Persist essential state and treat memory as a reconstructible cache. Coordinate related updates within the storage and concurrency guarantees of the selected backend.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Fetch the relevant current documentation before implementing or reviewing changes.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
| Task | Documentation |
|
|
8
|
+
|------|---------------|
|
|
9
|
+
| Schema initialization, migrations, indexes, caching, or parent-child coordination | [Rules of Durable Objects](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/) |
|
|
10
|
+
| Counters, transactions, and atomic updates | [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/); [Counter example](https://developers.cloudflare.com/durable-objects/examples/build-a-counter/) |
|
|
11
|
+
| Batch processing or multiple scheduled events | [Alarms](https://developers.cloudflare.com/durable-objects/api/alarms/); [Batching example](https://developers.cloudflare.com/durable-objects/examples/alarms-api/) |
|
|
12
|
+
| Cleanup and expiration | [Time to Live example](https://developers.cloudflare.com/durable-objects/examples/durable-object-ttl/); [SQLite storage API](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/) |
|
|
13
|
+
| Design application-specific rate limiting | [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/) |
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
CREATE TABLE IF NOT EXISTS _sql_schema_migrations (
|
|
15
|
-
id INTEGER PRIMARY KEY,
|
|
16
|
-
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
17
|
-
)
|
|
18
|
-
`);
|
|
19
|
-
|
|
20
|
-
const ver = this.sql
|
|
21
|
-
.exec<{ version: number }>("SELECT COALESCE(MAX(id), 0) as version FROM _sql_schema_migrations")
|
|
22
|
-
.one().version;
|
|
23
|
-
|
|
24
|
-
if (ver < 1) {
|
|
25
|
-
this.sql.exec(`CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT)`);
|
|
26
|
-
this.sql.exec("INSERT INTO _sql_schema_migrations (id) VALUES (1)");
|
|
27
|
-
}
|
|
28
|
-
if (ver < 2) {
|
|
29
|
-
this.sql.exec(`ALTER TABLE users ADD COLUMN email TEXT`);
|
|
30
|
-
this.sql.exec("INSERT INTO _sql_schema_migrations (id) VALUES (2)");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
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.
|
|
37
|
-
|
|
38
|
-
## In-Memory Caching
|
|
39
|
-
|
|
40
|
-
```typescript
|
|
41
|
-
export class UserCache extends DurableObject {
|
|
42
|
-
cache = new Map<string, User>();
|
|
43
|
-
async getUser(id: string): Promise<User | undefined> {
|
|
44
|
-
if (this.cache.has(id)) {
|
|
45
|
-
const cached = this.cache.get(id);
|
|
46
|
-
if (cached) return cached;
|
|
47
|
-
}
|
|
48
|
-
const user = await this.ctx.storage.get<User>(`user:${id}`);
|
|
49
|
-
if (user) this.cache.set(id, user);
|
|
50
|
-
return user;
|
|
51
|
-
}
|
|
52
|
-
async updateUser(id: string, data: Partial<User>) {
|
|
53
|
-
const updated = { ...await this.getUser(id), ...data };
|
|
54
|
-
this.cache.set(id, updated);
|
|
55
|
-
await this.ctx.storage.put(`user:${id}`, updated);
|
|
56
|
-
return updated;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
## Rate Limiting
|
|
62
|
-
|
|
63
|
-
```typescript
|
|
64
|
-
export class RateLimiter extends DurableObject {
|
|
65
|
-
async checkLimit(key: string, limit: number, window: number): Promise<boolean> {
|
|
66
|
-
const now = Date.now();
|
|
67
|
-
this.sql.exec('DELETE FROM requests WHERE key = ? AND timestamp < ?', key, now - window);
|
|
68
|
-
const count = this.sql.exec('SELECT COUNT(*) as count FROM requests WHERE key = ?', key).one().count;
|
|
69
|
-
if (count >= limit) return false;
|
|
70
|
-
this.sql.exec('INSERT INTO requests (key, timestamp) VALUES (?, ?)', key, now);
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Batch Processing with Alarms
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
export class BatchProcessor extends DurableObject {
|
|
80
|
-
pending: string[] = [];
|
|
81
|
-
async addItem(item: string) {
|
|
82
|
-
this.pending.push(item);
|
|
83
|
-
if (!await this.ctx.storage.getAlarm()) await this.ctx.storage.setAlarm(Date.now() + 5000);
|
|
84
|
-
}
|
|
85
|
-
async alarm() {
|
|
86
|
-
const items = [...this.pending];
|
|
87
|
-
this.pending = [];
|
|
88
|
-
this.sql.exec(`INSERT INTO processed_items (item, timestamp) VALUES ${items.map(() => "(?, ?)").join(", ")}`, ...items.flatMap(item => [item, Date.now()]));
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
## Initialization Pattern
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
export class Counter extends DurableObject {
|
|
97
|
-
value: number;
|
|
98
|
-
constructor(ctx: DurableObjectState, env: Env) {
|
|
99
|
-
super(ctx, env);
|
|
100
|
-
ctx.blockConcurrencyWhile(async () => { this.value = (await ctx.storage.get("value")) || 0; });
|
|
101
|
-
}
|
|
102
|
-
async increment() {
|
|
103
|
-
this.value++;
|
|
104
|
-
this.ctx.storage.put("value", this.value); // Don't await (output gate protects)
|
|
105
|
-
return this.value;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
## Safe Counter / Optimized Write
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
// Input gate blocks other requests
|
|
114
|
-
async getUniqueNumber(): Promise<number> {
|
|
115
|
-
let val = await this.ctx.storage.get("counter");
|
|
116
|
-
await this.ctx.storage.put("counter", val + 1);
|
|
117
|
-
return val;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// No await on write - output gate delays response until write confirms
|
|
121
|
-
async increment(): Promise<Response> {
|
|
122
|
-
let val = await this.ctx.storage.get("counter");
|
|
123
|
-
this.ctx.storage.put("counter", val + 1);
|
|
124
|
-
return new Response(String(val));
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
## Parent-Child Coordination
|
|
129
|
-
|
|
130
|
-
Hierarchical DO pattern where parent manages child DOs:
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
// Parent DO coordinates children
|
|
134
|
-
export class Workspace extends DurableObject {
|
|
135
|
-
async createDocument(name: string): Promise<string> {
|
|
136
|
-
const docId = crypto.randomUUID();
|
|
137
|
-
const childId = this.env.DOCUMENT.idFromName(`${this.ctx.id.toString()}:${docId}`);
|
|
138
|
-
const childStub = this.env.DOCUMENT.get(childId);
|
|
139
|
-
await childStub.initialize(name);
|
|
140
|
-
|
|
141
|
-
// Track child in parent storage
|
|
142
|
-
this.sql.exec('INSERT INTO documents (id, name, created) VALUES (?, ?, ?)',
|
|
143
|
-
docId, name, Date.now());
|
|
144
|
-
return docId;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async listDocuments(): Promise<string[]> {
|
|
148
|
-
return this.sql.exec('SELECT id FROM documents').toArray().map(r => r.id);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Child DO
|
|
153
|
-
export class Document extends DurableObject {
|
|
154
|
-
async initialize(name: string) {
|
|
155
|
-
this.sql.exec('CREATE TABLE IF NOT EXISTS content(key TEXT PRIMARY KEY, value TEXT)');
|
|
156
|
-
this.sql.exec('INSERT INTO content VALUES (?, ?)', 'name', name);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
## Write Coalescing Pattern
|
|
162
|
-
|
|
163
|
-
Multiple writes to same key coalesce atomically (last write wins):
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
async updateMetrics(userId: string, actions: Action[]) {
|
|
167
|
-
// All writes coalesce - no await needed
|
|
168
|
-
for (const action of actions) {
|
|
169
|
-
this.ctx.storage.put(`user:${userId}:lastAction`, action.type);
|
|
170
|
-
this.ctx.storage.put(`user:${userId}:count`,
|
|
171
|
-
await this.ctx.storage.get(`user:${userId}:count`) + 1);
|
|
172
|
-
}
|
|
173
|
-
// Output gate ensures all writes confirm before response
|
|
174
|
-
return new Response("OK");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Atomic batch with SQL
|
|
178
|
-
async batchUpdate(items: Item[]) {
|
|
179
|
-
this.sql.exec('BEGIN');
|
|
180
|
-
for (const item of items) {
|
|
181
|
-
this.sql.exec('INSERT OR REPLACE INTO items VALUES (?, ?)', item.id, item.value);
|
|
182
|
-
}
|
|
183
|
-
this.sql.exec('COMMIT');
|
|
184
|
-
}
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
## Cleanup
|
|
188
|
-
|
|
189
|
-
```typescript
|
|
190
|
-
async cleanup() {
|
|
191
|
-
await this.ctx.storage.deleteAlarm(); // Separate from deleteAll
|
|
192
|
-
await this.ctx.storage.deleteAll();
|
|
193
|
-
}
|
|
194
|
-
```
|
|
15
|
+
Verify persistence, isolation, and rollback behavior with the [testing guidance](testing.md).
|
|
@@ -1,183 +1,13 @@
|
|
|
1
1
|
# DO Storage Testing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Choose tests around persistence, rollback, instance isolation, and scheduled-work behavior. Inspect installed test packages and configuration before changing the suite.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Fetch the relevant current documentation before implementing or reviewing changes.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
| Task | Documentation |
|
|
8
|
+
|------|---------------|
|
|
9
|
+
| Set up or migrate a test suite, choose helpers, and manage isolation | [Testing Durable Objects](../../../durable-objects/references/testing.md) |
|
|
10
|
+
| Exercise RPC, SQLite storage, and alarms | [Testing Durable Objects example](https://developers.cloudflare.com/durable-objects/examples/testing-with-durable-objects/) |
|
|
11
|
+
| Determine the storage or recovery contract to verify | [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/) |
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
test: {
|
|
13
|
-
poolOptions: {
|
|
14
|
-
workers: { wrangler: { configPath: "./wrangler.toml" } }
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
**package.json:** Add `@cloudflare/vitest-pool-workers` and `vitest` to devDependencies
|
|
21
|
-
|
|
22
|
-
## Basic Testing
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
import { env, runInDurableObject } from "cloudflare:test";
|
|
26
|
-
import { describe, it, expect } from "vitest";
|
|
27
|
-
|
|
28
|
-
describe("Counter DO", () => {
|
|
29
|
-
it("increments counter", async () => {
|
|
30
|
-
const id = env.COUNTER.idFromName("test");
|
|
31
|
-
const result = await runInDurableObject(env.COUNTER, id, async (instance, state) => {
|
|
32
|
-
const val1 = await instance.increment();
|
|
33
|
-
const val2 = await instance.increment();
|
|
34
|
-
return { val1, val2 };
|
|
35
|
-
});
|
|
36
|
-
expect(result.val1).toBe(1);
|
|
37
|
-
expect(result.val2).toBe(2);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Testing SQL Storage
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
it("creates and queries users", async () => {
|
|
46
|
-
const id = env.USER_MANAGER.idFromName("test");
|
|
47
|
-
await runInDurableObject(env.USER_MANAGER, id, async (instance, state) => {
|
|
48
|
-
await instance.createUser("alice@example.com", "Alice");
|
|
49
|
-
const user = await instance.getUser("alice@example.com");
|
|
50
|
-
expect(user).toEqual({ email: "alice@example.com", name: "Alice" });
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("handles schema migrations", async () => {
|
|
55
|
-
const id = env.USER_MANAGER.idFromName("migration-test");
|
|
56
|
-
await runInDurableObject(env.USER_MANAGER, id, async (instance, state) => {
|
|
57
|
-
const version = state.storage.sql.exec(
|
|
58
|
-
"SELECT value FROM _meta WHERE key = 'schema_version'"
|
|
59
|
-
).one()?.value;
|
|
60
|
-
expect(version).toBe("1");
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Testing Alarms
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
import { runDurableObjectAlarm } from "cloudflare:test";
|
|
69
|
-
|
|
70
|
-
it("processes batch on alarm", async () => {
|
|
71
|
-
const id = env.BATCH_PROCESSOR.idFromName("test");
|
|
72
|
-
|
|
73
|
-
// Add items
|
|
74
|
-
await runInDurableObject(env.BATCH_PROCESSOR, id, async (instance) => {
|
|
75
|
-
await instance.addItem("item1");
|
|
76
|
-
await instance.addItem("item2");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
// Trigger alarm
|
|
80
|
-
await runDurableObjectAlarm(env.BATCH_PROCESSOR, id);
|
|
81
|
-
|
|
82
|
-
// Verify processed
|
|
83
|
-
await runInDurableObject(env.BATCH_PROCESSOR, id, async (instance, state) => {
|
|
84
|
-
const count = state.storage.sql.exec(
|
|
85
|
-
"SELECT COUNT(*) as count FROM processed_items"
|
|
86
|
-
).one().count;
|
|
87
|
-
expect(count).toBe(2);
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Testing Concurrency
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
it("handles concurrent increments safely", async () => {
|
|
96
|
-
const id = env.COUNTER.idFromName("concurrent-test");
|
|
97
|
-
|
|
98
|
-
// Parallel increments
|
|
99
|
-
const results = await Promise.all([
|
|
100
|
-
runInDurableObject(env.COUNTER, id, (i) => i.increment()),
|
|
101
|
-
runInDurableObject(env.COUNTER, id, (i) => i.increment()),
|
|
102
|
-
runInDurableObject(env.COUNTER, id, (i) => i.increment())
|
|
103
|
-
]);
|
|
104
|
-
|
|
105
|
-
// All should get unique values
|
|
106
|
-
expect(new Set(results).size).toBe(3);
|
|
107
|
-
expect(Math.max(...results)).toBe(3);
|
|
108
|
-
});
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Test Isolation
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
// Per-test unique IDs
|
|
115
|
-
let testId: string;
|
|
116
|
-
beforeEach(() => { testId = crypto.randomUUID(); });
|
|
117
|
-
|
|
118
|
-
it("isolated test", async () => {
|
|
119
|
-
const id = env.MY_DO.idFromName(testId);
|
|
120
|
-
// Uses unique DO instance
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// Cleanup pattern
|
|
124
|
-
it("with cleanup", async () => {
|
|
125
|
-
const id = env.MY_DO.idFromName("cleanup-test");
|
|
126
|
-
try {
|
|
127
|
-
await runInDurableObject(env.MY_DO, id, async (instance) => {});
|
|
128
|
-
} finally {
|
|
129
|
-
await runInDurableObject(env.MY_DO, id, async (instance, state) => {
|
|
130
|
-
await state.storage.deleteAll();
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## Testing PITR
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
it("restores from bookmark", async () => {
|
|
140
|
-
const id = env.MY_DO.idFromName("pitr-test");
|
|
141
|
-
|
|
142
|
-
// Create checkpoint
|
|
143
|
-
const bookmark = await runInDurableObject(env.MY_DO, id, async (instance, state) => {
|
|
144
|
-
await state.storage.put("value", 1);
|
|
145
|
-
return await state.storage.getCurrentBookmark();
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// Modify and restore
|
|
149
|
-
await runInDurableObject(env.MY_DO, id, async (instance, state) => {
|
|
150
|
-
await state.storage.put("value", 2);
|
|
151
|
-
await state.storage.onNextSessionRestoreBookmark(bookmark);
|
|
152
|
-
state.abort();
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// Verify restored
|
|
156
|
-
await runInDurableObject(env.MY_DO, id, async (instance, state) => {
|
|
157
|
-
const value = await state.storage.get("value");
|
|
158
|
-
expect(value).toBe(1);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
## Testing Transactions
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
it("rolls back on error", async () => {
|
|
167
|
-
const id = env.BANK.idFromName("transaction-test");
|
|
168
|
-
|
|
169
|
-
await runInDurableObject(env.BANK, id, async (instance, state) => {
|
|
170
|
-
await state.storage.put("balance", 100);
|
|
171
|
-
|
|
172
|
-
await expect(
|
|
173
|
-
state.storage.transaction(async () => {
|
|
174
|
-
await state.storage.put("balance", 50);
|
|
175
|
-
throw new Error("Cancel");
|
|
176
|
-
})
|
|
177
|
-
).rejects.toThrow("Cancel");
|
|
178
|
-
|
|
179
|
-
const balance = await state.storage.get("balance");
|
|
180
|
-
expect(balance).toBe(100); // Rolled back
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
```
|
|
13
|
+
Use the current test documentation for helper signatures and runtime limitations. For point-in-time recovery tests, check both the storage API and the test runtime’s supported behavior before assuming a restart reproduces production recovery.
|
|
@@ -1,89 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Email Routing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Use routing rules for address-based forwarding; use an Email Worker when incoming mail needs custom processing. Fetch the linked docs before implementing APIs, DNS, configuration, or limits.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
| Task | Start here |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| Forward incoming mail to an existing mailbox | [Route emails](https://developers.cloudflare.com/email-service/get-started/route-emails/) |
|
|
8
|
+
| Manage addresses, verification, catch-all rules, or subaddressing | [Routing rules and addresses](https://developers.cloudflare.com/email-service/configuration/email-routing-addresses/) |
|
|
9
|
+
| Filter, parse, reply to, or store incoming mail | [Email Workers](../email-workers/README.md) |
|
|
10
|
+
| Send a new outbound message | [Send emails](https://developers.cloudflare.com/email-service/get-started/send-emails/) — Workers binding, REST API, or SMTP |
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
Forwarding requires verified destinations. Replying within an incoming email event and sending a new outbound message have different requirements; use the relevant API docs.
|
|
8
13
|
|
|
9
|
-
##
|
|
14
|
+
## Reference map
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// CRITICAL: Must consume stream before response
|
|
16
|
-
const parser = new PostalMime.default();
|
|
17
|
-
const email = await parser.parse(await message.raw.arrayBuffer());
|
|
18
|
-
|
|
19
|
-
// Process email
|
|
20
|
-
console.log(`From: ${message.from}, Subject: ${email.subject}`);
|
|
21
|
-
|
|
22
|
-
// Forward or reject
|
|
23
|
-
await message.forward("verified@destination.com");
|
|
24
|
-
}
|
|
25
|
-
} satisfies ExportedHandler<Env>;
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Reading Order
|
|
29
|
-
|
|
30
|
-
**Start here based on your goal:**
|
|
31
|
-
|
|
32
|
-
1. **New to Email Routing?** → [configuration.md](configuration.md) → [patterns.md](patterns.md)
|
|
33
|
-
2. **Adding Workers?** → [api.md](api.md) § Worker Runtime API → [patterns.md](patterns.md)
|
|
34
|
-
3. **Sending emails?** → [api.md](api.md) § SendEmail Binding
|
|
35
|
-
4. **Managing via API?** → [api.md](api.md) § REST API Operations
|
|
36
|
-
5. **Debugging issues?** → [gotchas.md](gotchas.md)
|
|
37
|
-
|
|
38
|
-
## Decision Tree
|
|
39
|
-
|
|
40
|
-
```
|
|
41
|
-
Need to receive emails?
|
|
42
|
-
├─ Simple forwarding only? → Dashboard rules (configuration.md)
|
|
43
|
-
├─ Complex logic/filtering? → Email Workers (api.md + patterns.md)
|
|
44
|
-
└─ Parse attachments/body? → postal-mime library (patterns.md § Parse Email)
|
|
45
|
-
|
|
46
|
-
Need to send emails?
|
|
47
|
-
├─ From Worker? → SendEmail binding (api.md § SendEmail)
|
|
48
|
-
└─ From external app? → Use external SMTP/API service
|
|
49
|
-
|
|
50
|
-
Having issues?
|
|
51
|
-
├─ Email not arriving? → gotchas.md § Mail Authentication
|
|
52
|
-
├─ Worker crashing? → gotchas.md § Stream Consumption
|
|
53
|
-
└─ Forward failing? → gotchas.md § Destination Verification
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Key Concepts
|
|
57
|
-
|
|
58
|
-
**Routing Rules**: Pattern-based forwarding configured via Dashboard/API. Simple but limited.
|
|
59
|
-
|
|
60
|
-
**Email Workers**: Custom TypeScript handlers with full email access. Handles complex logic, parsing, storage, rejection.
|
|
61
|
-
|
|
62
|
-
**SendEmail Binding**: Outbound email API for Workers. Transactional email only (no marketing/bulk).
|
|
63
|
-
|
|
64
|
-
**ForwardableEmailMessage**: Runtime interface for incoming emails. Provides headers, raw stream, forward/reject methods.
|
|
65
|
-
|
|
66
|
-
## In This Reference
|
|
67
|
-
|
|
68
|
-
- **[configuration.md](configuration.md)** - Setup, deployment, wrangler config
|
|
69
|
-
- **[api.md](api.md)** - REST API + Worker runtime API + types
|
|
70
|
-
- **[patterns.md](patterns.md)** - Common patterns with working examples
|
|
71
|
-
- **[gotchas.md](gotchas.md)** - Critical pitfalls, troubleshooting, limits
|
|
72
|
-
|
|
73
|
-
## Architecture
|
|
74
|
-
|
|
75
|
-
```
|
|
76
|
-
Internet → MX Records → Cloudflare Email Routing
|
|
77
|
-
├─ Routing Rules (dashboard)
|
|
78
|
-
└─ Email Worker (your code)
|
|
79
|
-
├─ Forward to destination
|
|
80
|
-
├─ Reject with reason
|
|
81
|
-
├─ Store in R2/KV/D1
|
|
82
|
-
└─ Send outbound (SendEmail)
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## See Also
|
|
86
|
-
|
|
87
|
-
- [Cloudflare Docs: Email Routing](https://developers.cloudflare.com/email-routing/)
|
|
88
|
-
- [Cloudflare Docs: Email Workers](https://developers.cloudflare.com/email-routing/email-workers/)
|
|
89
|
-
- [postal-mime npm package](https://www.npmjs.com/package/postal-mime)
|
|
16
|
+
- [Configuration](configuration.md): domains, rules, deployment, and local testing.
|
|
17
|
+
- [API](api.md): routing management and inbound/outbound operations.
|
|
18
|
+
- [Patterns](patterns.md): filtering, parsing, storage, and notifications.
|
|
19
|
+
- [Troubleshooting](gotchas.md): authentication, delivery, and current limits.
|