Apply Cloudflare architecture pattern to current project
Applies proven Cloudflare architecture patterns to optimize your project.
/plugin marketplace add littlebearapps/cloudflare-engineer/plugin install cloudflare-engineer@littlebearapps-cloudflare<pattern-name> [--analyze-only]Apply proven architecture patterns to your Cloudflare project.
Arguments: "$ARGUMENTS"
| Pattern | Description | Effort |
|---|---|---|
service-bindings | Decompose monolithic Worker with RPC | Medium |
d1-batching | Optimize D1 write costs with batch operations | Low |
circuit-breaker | Add resilience for external API dependencies | Medium |
/cf-pattern service-bindings # Apply pattern with guidance
/cf-pattern d1-batching --analyze-only # Analyze without modifying
/cf-pattern circuit-breaker # Apply circuit breaker pattern
/cf-pattern # Interactive pattern selection
Parse $ARGUMENTS for:
<pattern-name>: Which pattern to apply (or empty for interactive)--analyze-only: Just analyze, don't modify filesIf pattern name provided: Load pattern details from @skills/patterns/SKILL.md
If no pattern name: Analyze codebase and recommend applicable patterns:
Check for Service Bindings triggers:
fetch() to internal URLsCheck for D1 Batching triggers:
db.run() in forEach/map callbacksCheck for Circuit Breaker triggers:
fetch() calls without timeoutScan project for pattern-specific indicators:
For service-bindings:
# Find main Worker file size
wc -l src/index.ts
# Find internal fetch calls
grep -r "fetch.*localhost\|fetch.*127.0.0.1\|fetch.*internal" src/
# Check subrequest patterns
grep -r "fetch(" src/ | wc -l
For d1-batching:
# Find per-row inserts
grep -rn "for.*await.*db\." src/
grep -rn "forEach.*await.*INSERT" src/
grep -rn "\.map.*await.*\.run" src/
For circuit-breaker:
# Find unprotected external fetches
grep -rn "fetch\s*(" src/ | grep -v "localhost\|127.0.0.1"
# Check for timeout usage
grep -r "AbortSignal.timeout\|setTimeout" src/
# Pattern Analysis: [pattern-name]
## Applicability Score: [High | Medium | Low]
## Detected Triggers
| Trigger | Found | Location |
|---------|-------|----------|
| [trigger 1] | Yes/No | file:line |
| [trigger 2] | Yes/No | file:line |
## Current State
[Summary of current implementation]
## Recommended Changes
1. [Change 1]
- File: [path]
- Current: [code snippet]
- After: [code snippet]
2. [Change 2]
...
## Estimated Effort
- Files to modify: X
- New files: Y
- Complexity: [Low | Medium | High]
## Trade-offs
**Benefits:**
- [Benefit 1]
- [Benefit 2]
**Costs:**
- [Cost 1]
- [Cost 2]
If --analyze-only:
Stop after analysis report.
Otherwise: Apply pattern changes:
When to Apply:
fetch() to internal URLsWhat Gets Created:
What Gets Modified:
Reference: @skills/patterns/service-bindings.md
When to Apply:
What Gets Modified:
No New Files (in-place optimization)
Reference: @skills/patterns/d1-batching.md
When to Apply:
What Gets Created:
circuit-breaker.ts utilityWhat Gets Modified:
Reference: @skills/patterns/circuit-breaker.md
When run without pattern name:
# Pattern Recommendation
Based on analysis of your codebase:
## Recommended Patterns
1. **d1-batching** (High applicability)
- Found 5 per-row insert loops
- Estimated savings: $XX/month
2. **circuit-breaker** (Medium applicability)
- Found 3 unprotected external API calls
- Improves resilience
3. **service-bindings** (Low applicability)
- Worker is 200 lines (threshold: 500)
- Consider when Worker grows
## Quick Apply
Run: `/cf-pattern d1-batching` to apply top recommendation
# Pattern Analysis: d1-batching
## Applicability Score: High
## Detected Triggers
| Trigger | Found | Location |
|---------|-------|----------|
| Per-row INSERT in loop | Yes | src/api/users.ts:45 |
| Per-row INSERT in loop | Yes | src/cron/sync.ts:23 |
| D1 cost driver | Yes | (from /cf-costs analysis) |
## Current State
Found 2 locations with per-row inserts:
**src/api/users.ts:45**
```typescript
for (const user of users) {
await db.run('INSERT INTO users (email) VALUES (?)', [user.email]);
}
src/cron/sync.ts:23
items.forEach(async (item) => {
await db.run('INSERT INTO items (name) VALUES (?)', [item.name]);
});
Before:
for (const user of users) {
await db.run('INSERT INTO users (email) VALUES (?)', [user.email]);
}
After:
const BATCH_SIZE = 100;
for (let i = 0; i < users.length; i += BATCH_SIZE) {
const batch = users.slice(i, i + BATCH_SIZE);
const placeholders = batch.map(() => '(?)').join(', ');
const values = batch.map(u => u.email);
await db.run(`INSERT INTO users (email) VALUES ${placeholders}`, values);
}
[Similar transformation...]
Benefits:
Costs:
Proceed with changes? Use /cf-pattern d1-batching to apply.
## Tips
- Run `/cf-costs` first to identify which patterns have highest ROI
- Use `--analyze-only` to preview changes before applying
- Start with `d1-batching` (lowest risk, quick wins)
- `circuit-breaker` is recommended for any production external API usage
- `service-bindings` is a larger refactor - plan accordingly