better-cf 0.1.0 → 0.2.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 CHANGED
@@ -1,172 +1,148 @@
1
1
  # better-cf
2
2
 
3
- Type-safe Cloudflare Queue SDK + CLI.
4
-
5
- `better-cf` removes queue binding boilerplate by scanning `defineQueue` exports, generating `.better-cf/entry.ts`, and patching Wrangler queue config.
3
+ Opinionated Cloudflare Queue SDK + automation CLI designed for modern developer experience.
6
4
 
7
5
  ## Why better-cf
8
6
 
9
- Cloudflare Queues are great, but setup usually repeats:
10
-
11
- - Manual queue binding names.
12
- - Manual `wrangler.toml` or `wrangler.jsonc` queue blocks.
13
- - Manual worker queue consumer plumbing.
7
+ `better-cf` keeps Cloudflare Queue primitives but improves day-to-day ergonomics:
14
8
 
15
- `better-cf` keeps queue code in plain functions and auto-generates runtime wiring.
9
+ - typed queue contracts from one place
10
+ - less manual entry/config wiring
11
+ - structured automation for local dev and codegen
12
+ - predictable runtime and testing APIs
16
13
 
17
- ## Install
14
+ ## Quickstart (Existing Project)
18
15
 
19
16
  ```bash
20
17
  npm i better-cf zod
21
18
  npm i -D wrangler @cloudflare/workers-types typescript
22
- ```
23
-
24
- ## Quickstart
25
-
26
- ```bash
27
19
  npx better-cf init
20
+ npm run dev
28
21
  ```
29
22
 
30
- Create queue:
31
-
32
- ```ts
33
- import { z } from 'zod';
34
- import { defineQueue } from './better-cf.config';
35
-
36
- export const signupQueue = defineQueue({
37
- message: z.object({ email: z.string().email(), userId: z.string() }),
38
- process: async (ctx, msg) => {
39
- await fetch('https://example.com', {
40
- method: 'POST',
41
- body: JSON.stringify(msg)
42
- });
43
- },
44
- retry: 3
45
- });
46
- ```
47
-
48
- Run dev:
23
+ ## Quickstart (New Project)
49
24
 
50
25
  ```bash
26
+ npx better-cf create my-worker
27
+ cd my-worker
51
28
  npm run dev
52
29
  ```
53
30
 
54
- Deploy:
31
+ Use a specific package manager during create:
55
32
 
56
33
  ```bash
57
- npm run deploy
34
+ npx better-cf create my-worker --use-pnpm
35
+ npx better-cf create my-worker --use-bun
58
36
  ```
59
37
 
60
38
  ## Canonical Imports
61
39
 
62
- - Queue SDK: `better-cf/queue`
40
+ - Queue runtime: `better-cf/queue`
63
41
  - Testing helpers: `better-cf/testing`
42
+ - CLI binary: `better-cf`
64
43
 
65
- ## Type Safety Guarantees
44
+ ## Core Workflow
66
45
 
67
- `createSDK<Env>()` propagates `Env` types into `ctx.env` for queue and worker handlers.
46
+ `better-cf dev` continuously orchestrates:
68
47
 
69
- ```ts
70
- import { createSDK } from 'better-cf/queue';
48
+ 1. scan queue definitions
49
+ 2. validate config
50
+ 3. generate `.better-cf/entry.ts` and type files
51
+ 4. patch wrangler queue sections
52
+ 5. infer env types
53
+ 6. run/restart `wrangler dev`
54
+ 7. re-run on source/config changes
71
55
 
72
- type Env = {
73
- DB: D1Database;
74
- EMAIL_QUEUE: Queue;
75
- };
56
+ One-shot mode:
76
57
 
77
- export const { defineQueue } = createSDK<Env>();
58
+ ```bash
59
+ better-cf generate
78
60
  ```
79
61
 
80
- Payload types are inferred from Zod schemas for `send`, `sendBatch`, `process`, and multi-job queue APIs.
62
+ ## Example Patterns
81
63
 
82
- ## Hono Example
83
-
84
- `better-cf` supports Hono default exports.
64
+ ### Single queue with typed payload
85
65
 
86
66
  ```ts
87
- import { Hono } from 'hono';
88
-
89
- const app = new Hono();
90
- app.get('/', (ctx) => ctx.text('ok'));
67
+ import { createSDK } from 'better-cf/queue';
68
+ import { z } from 'zod';
91
69
 
92
- export default app;
93
- ```
70
+ type Env = { QUEUE_SIGNUP: Queue };
71
+ const { defineQueue, defineWorker } = createSDK<Env>();
94
72
 
95
- The generated `.better-cf/entry.ts` resolves fetch handlers for object/function/named exports.
73
+ export const signupQueue = defineQueue({
74
+ message: z.object({ email: z.string().email() }),
75
+ process: async (ctx, msg) => {
76
+ console.log(ctx.message.id, msg.email);
77
+ },
78
+ retry: 3,
79
+ retryDelay: '30s'
80
+ });
96
81
 
97
- ## Legacy Cloudflare Compatibility (Service Worker Adapter)
82
+ export default defineWorker({
83
+ async fetch() {
84
+ return new Response('ok');
85
+ }
86
+ });
87
+ ```
98
88
 
99
- Set compatibility mode in `better-cf.config.ts`:
89
+ ### Batch mode
100
90
 
101
91
  ```ts
102
- export const betterCfConfig = {
103
- legacyServiceWorker: true
104
- };
92
+ const auditQueue = defineQueue({
93
+ message: z.object({ action: z.string() }),
94
+ batch: { maxSize: 10, timeout: '30s', maxConcurrency: 2 },
95
+ processBatch: async (ctx, messages) => {
96
+ console.log(messages.length, ctx.batch.queue);
97
+ ctx.batch.ackAll();
98
+ }
99
+ });
105
100
  ```
106
101
 
107
- This mode is adapter-only and not feature-parity guaranteed with module worker patterns.
108
-
109
- ## CLI Commands
110
-
111
- - `better-cf init`: scaffold config, worker, scripts.
112
- - `better-cf generate`: scan + generate `.better-cf/*` + patch Wrangler config.
113
- - `better-cf dev`: generate + watch + run `wrangler dev` (local only for queues).
114
- - `better-cf deploy`: generate + run `wrangler deploy`.
115
-
116
- ## Queue Config Mapping
117
-
118
- | better-cf | Wrangler |
119
- |---|---|
120
- | `retry` | `max_retries` |
121
- | `retryDelay` | `retry_delay` |
122
- | `deadLetter` | `dead_letter_queue` |
123
- | `batch.maxSize` | `max_batch_size` |
124
- | `batch.timeout` | `max_batch_timeout` |
125
- | `batch.maxConcurrency` | `max_concurrency` |
126
-
127
- ## Local Dev Constraints
128
-
129
- Cloudflare queue local development does not support `wrangler dev --remote` for queue consumers. `better-cf dev --remote` is blocked intentionally.
130
-
131
- ## Testing Queues
132
-
133
- Use `better-cf/testing`:
102
+ ### Queue unit testing
134
103
 
135
104
  ```ts
136
105
  import { testQueue } from 'better-cf/testing';
137
106
 
138
- const result = await testQueue(queueHandle, {
139
- env: mockEnv,
140
- message: { id: '1' }
107
+ const result = await testQueue(signupQueue, {
108
+ env: {},
109
+ message: { email: 'dev@example.com' }
141
110
  });
111
+
112
+ expect(result.acked).toHaveLength(1);
142
113
  ```
143
114
 
144
- ## Compatibility Matrix
115
+ ## Comparison with Cloudflare Queue Workflows
116
+
117
+ | Concern | Cloudflare path | better-cf path |
118
+ |---|---|---|
119
+ | Queue contract shape | Convention/custom runtime checks | `defineQueue({ message: z.object(...) })` |
120
+ | Entry + config wiring | Manual exports + Wrangler maintenance | Generated entry + automated Wrangler patching |
121
+ | Local dev orchestration | Team-managed scripts | One `better-cf dev` loop |
122
+ | Queue test harness | Custom mocks/harnesses | `testQueue` helper |
123
+
124
+ Detailed comparison: `/apps/docs/src/content/docs/comparison/cloudflare-vs-better-cf.mdx`
145
125
 
146
- - Wrangler: `>=3.91.0 <5`
147
- - Node: `>=18.18`
148
- - Config formats: `wrangler.toml`, `wrangler.json`, `wrangler.jsonc`
149
- - Runtime styles: module worker (first-class), service-worker adapter (compatibility mode)
150
- - Frameworks: Workers + Hono
126
+ ## Limitations
151
127
 
152
- ## Not Supported (v1)
128
+ ### Not supported
153
129
 
154
- - Pull consumers / HTTP pull consumers.
155
- - Queue create/delete lifecycle outside deploy workflow.
156
- - Queue metrics dashboard/API abstraction.
157
- - Runtime dynamic queue registration.
158
- - Remote queue local-dev parity for unsupported Cloudflare modes.
130
+ - Pull-message runtime abstraction implementation
131
+ - Queue metrics/dashboard abstraction
132
+ - Dynamic runtime queue declaration
133
+ - Unsupported remote queue local-dev parity modes
159
134
 
160
- ## Known Gaps
135
+ ### Known gaps
161
136
 
162
- - Non-literal config expressions reduce static wrangler extraction quality.
163
- - Legacy adapter mode may not match every service-worker edge case.
164
- - Non-standard worker export patterns beyond documented variants are out of scope.
165
- - Monorepo autodetection is basic; explicit `workerEntry` may be needed.
137
+ - Non-literal config extraction can reduce static mapping fidelity
138
+ - Legacy service-worker adapter is compatibility-oriented
139
+ - Non-standard worker export patterns beyond documented variants are out of scope
166
140
 
167
- ## Roadmap
141
+ Use native Cloudflare APIs directly where the SDK intentionally does not abstract.
168
142
 
169
- Planned future namespaces:
143
+ ## Docs
170
144
 
171
- - `better-cf/workflow`
172
- - `better-cf/durable-object`
145
+ - Site source: `apps/docs`
146
+ - Getting started page: `apps/docs/src/content/docs/getting-started.md`
147
+ - File structure guide: `apps/docs/src/content/docs/guides/file-structure.md`
148
+ - Cookbook page: `apps/docs/src/content/docs/examples/cookbook.md`