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 +89 -113
- package/dist/cli/index.js +1057 -129
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +55 -9
- package/dist/index.js.map +1 -1
- package/dist/queue/index.d.ts +4 -4
- package/dist/queue/index.js +55 -9
- package/dist/queue/index.js.map +1 -1
- package/dist/queue/internal.d.ts +1 -1
- package/dist/{types-zED9gDCd.d.ts → types-BAZ_wtox.d.ts} +41 -8
- package/package.json +16 -8
- package/docs/api/queue.md +0 -24
- package/docs/api/testing.md +0 -17
- package/docs/getting-started.md +0 -40
- package/docs/guides/hono.md +0 -18
- package/docs/guides/legacy-cloudflare.md +0 -15
- package/docs/limitations.md +0 -16
- package/docs/reference/errors.md +0 -21
- package/docs/reference/wrangler-mapping.md +0 -18
package/README.md
CHANGED
|
@@ -1,172 +1,148 @@
|
|
|
1
1
|
# better-cf
|
|
2
2
|
|
|
3
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
|
|
31
|
+
Use a specific package manager during create:
|
|
55
32
|
|
|
56
33
|
```bash
|
|
57
|
-
|
|
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
|
|
40
|
+
- Queue runtime: `better-cf/queue`
|
|
63
41
|
- Testing helpers: `better-cf/testing`
|
|
42
|
+
- CLI binary: `better-cf`
|
|
64
43
|
|
|
65
|
-
##
|
|
44
|
+
## Core Workflow
|
|
66
45
|
|
|
67
|
-
`
|
|
46
|
+
`better-cf dev` continuously orchestrates:
|
|
68
47
|
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
73
|
-
DB: D1Database;
|
|
74
|
-
EMAIL_QUEUE: Queue;
|
|
75
|
-
};
|
|
56
|
+
One-shot mode:
|
|
76
57
|
|
|
77
|
-
|
|
58
|
+
```bash
|
|
59
|
+
better-cf generate
|
|
78
60
|
```
|
|
79
61
|
|
|
80
|
-
|
|
62
|
+
## Example Patterns
|
|
81
63
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
`better-cf` supports Hono default exports.
|
|
64
|
+
### Single queue with typed payload
|
|
85
65
|
|
|
86
66
|
```ts
|
|
87
|
-
import {
|
|
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
|
-
|
|
93
|
-
|
|
70
|
+
type Env = { QUEUE_SIGNUP: Queue };
|
|
71
|
+
const { defineQueue, defineWorker } = createSDK<Env>();
|
|
94
72
|
|
|
95
|
-
|
|
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
|
-
|
|
82
|
+
export default defineWorker({
|
|
83
|
+
async fetch() {
|
|
84
|
+
return new Response('ok');
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
```
|
|
98
88
|
|
|
99
|
-
|
|
89
|
+
### Batch mode
|
|
100
90
|
|
|
101
91
|
```ts
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
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(
|
|
139
|
-
env:
|
|
140
|
-
message: {
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
|
|
128
|
+
### Not supported
|
|
153
129
|
|
|
154
|
-
- Pull
|
|
155
|
-
- Queue
|
|
156
|
-
-
|
|
157
|
-
-
|
|
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
|
-
|
|
135
|
+
### Known gaps
|
|
161
136
|
|
|
162
|
-
- Non-literal config
|
|
163
|
-
- Legacy
|
|
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
|
-
|
|
141
|
+
Use native Cloudflare APIs directly where the SDK intentionally does not abstract.
|
|
168
142
|
|
|
169
|
-
|
|
143
|
+
## Docs
|
|
170
144
|
|
|
171
|
-
- `
|
|
172
|
-
- `
|
|
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`
|