@zabaca/zbc 0.6.0 → 0.6.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/package.json
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
"targetDir": "packages/inbox",
|
|
6
6
|
"modules": ["cloudflare-email"],
|
|
7
7
|
"secrets": ["INBOX_TOKEN", "CLOUDFLARE_API_TOKEN"],
|
|
8
|
-
"instructions": "1) Edit packages/inbox/wrangler.jsonc: set DEFAULT_FROM to an address on your email domain (e.g. inbox@mail.example.com).\n2) Create two instance files in packages/infra/environments/<env>/:\n\n // inbox.ts\n import { cloudflareModule } from '../../modules/cloudflare'\n export default cloudflareModule.instance({\n name: 'inbox',\n config: { workdir: 'packages/inbox', accountId: '<account id>', workerSecrets: ['INBOX_TOKEN'] },\n })\n\n // email.ts\n import { cloudflareEmailModule } from '../../modules/cloudflare-email'\n import inbox from './inbox'\n export default cloudflareEmailModule.instance({\n name: 'email',\n imports: [inbox],\n config: {\n accountId: '<account id>', zoneId: '<zone id>', domain: 'mail.<your-domain>',\n enableSending: true, enableRouting: true,\n catchAll: { action: 'worker', workerName: '
|
|
8
|
+
"instructions": "1) Edit packages/inbox/wrangler.jsonc: set DEFAULT_FROM to an address on your email domain (e.g. inbox@mail.example.com).\n2) Create two instance files in packages/infra/environments/<env>/:\n\n // inbox.ts\n import { cloudflareModule } from '../../modules/cloudflare'\n export default cloudflareModule.instance({\n name: 'inbox',\n config: { workdir: 'packages/inbox', accountId: '<account id>', workerSecrets: ['INBOX_TOKEN'] },\n })\n\n // email.ts\n import { cloudflareEmailModule } from '../../modules/cloudflare-email'\n import inbox from './inbox'\n export default cloudflareEmailModule.instance({\n name: 'email',\n imports: [inbox],\n config: {\n accountId: '<account id>', zoneId: '<zone id>', domain: 'mail.<your-domain>',\n enableSending: true, enableRouting: true,\n catchAll: { action: 'worker', workerName: { from: 'inbox', output: 'workerName' } },\n },\n })\n\n3) Add INBOX_TOKEN (any long random string, e.g. `openssl rand -hex 32`) to secrets.yaml, and make sure CLOUDFLARE_API_TOKEN has the scopes the cloudflare-email module documents.\n4) `zbc apply <env>`. Create the R2 bucket if wrangler asks for it (`wrangler r2 bucket create <project>-inbox-raw`).\nNote: Cloudflare holds newly onboarded sending domains in a preview sandbox (verified recipients only) for a short period after DNS validates; arbitrary-recipient sending starts working once it drops."
|
|
9
9
|
}
|
|
@@ -193,6 +193,13 @@ export const cloudflareModule = defineModule({
|
|
|
193
193
|
}),
|
|
194
194
|
outputs: z.object({
|
|
195
195
|
deployUrl: z.string(),
|
|
196
|
+
/**
|
|
197
|
+
* The deployed Worker's script name, parsed from wrangler's "Deployed
|
|
198
|
+
* <name> triggers" confirmation. Lets dependents reference this worker
|
|
199
|
+
* without repeating the name from wrangler.jsonc (e.g. cloudflare-email's
|
|
200
|
+
* catchAll.workerName as `{ from, output: 'workerName' }`).
|
|
201
|
+
*/
|
|
202
|
+
workerName: z.string(),
|
|
196
203
|
}),
|
|
197
204
|
async apply(config, ctx) {
|
|
198
205
|
const apiToken = ctx.secrets['CLOUDFLARE_API_TOKEN']
|
|
@@ -243,12 +250,15 @@ export const cloudflareModule = defineModule({
|
|
|
243
250
|
)
|
|
244
251
|
const out = wrangler(workdir, deployArgs, env)
|
|
245
252
|
// Success theater guard: wrangler can exit 0 without actually deploying
|
|
246
|
-
// (the --bun incident above). Require the deploy confirmation line
|
|
247
|
-
|
|
253
|
+
// (the --bun incident above). Require the deploy confirmation line — and
|
|
254
|
+
// capture the deployed script name from it for the workerName output.
|
|
255
|
+
const deployedMatch = out.match(/Deployed\s+(\S+)\s+triggers/)
|
|
256
|
+
if (!deployedMatch) {
|
|
248
257
|
throw new Error(
|
|
249
258
|
`wrangler deploy exited 0 but printed no "Deployed ... triggers" confirmation:\n${out}`,
|
|
250
259
|
)
|
|
251
260
|
}
|
|
261
|
+
const workerName = deployedMatch[1]!
|
|
252
262
|
const urlMatch = out.match(/https:\/\/[^\s"',]+\.workers\.dev[^\s"',]*/)
|
|
253
263
|
const deployUrl = urlMatch?.[0] ?? ''
|
|
254
264
|
console.log(` Deployed: ${deployUrl || '(URL not parsed — see wrangler output)'}`)
|
|
@@ -265,7 +275,7 @@ export const cloudflareModule = defineModule({
|
|
|
265
275
|
console.log(` Set Worker secret: ${name}`)
|
|
266
276
|
}
|
|
267
277
|
|
|
268
|
-
return { deployUrl }
|
|
278
|
+
return { deployUrl, workerName }
|
|
269
279
|
},
|
|
270
280
|
async destroy(config, ctx) {
|
|
271
281
|
const apiToken = ctx.secrets['CLOUDFLARE_API_TOKEN']
|
|
@@ -98,6 +98,47 @@ async function cfFetch<T>(
|
|
|
98
98
|
return envelope.result
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A worker name: either a literal script name, or a `{ from, output }`
|
|
103
|
+
* reference into an imported instance's outputs (e.g. the cloudflare module
|
|
104
|
+
* emits `workerName`). References make the coupling refactor-safe — a rename
|
|
105
|
+
* in wrangler.jsonc flows through automatically, and a typo'd instance or
|
|
106
|
+
* output is a hard error at apply time instead of a dangling routing rule.
|
|
107
|
+
*/
|
|
108
|
+
const workerRefSchema = z.union([
|
|
109
|
+
z.string(),
|
|
110
|
+
z.object({
|
|
111
|
+
/** Instance name — must be listed in this instance's `imports`. */
|
|
112
|
+
from: z.string(),
|
|
113
|
+
/** Which output of that instance holds the worker name. */
|
|
114
|
+
output: z.string(),
|
|
115
|
+
}),
|
|
116
|
+
])
|
|
117
|
+
|
|
118
|
+
type WorkerRef = z.infer<typeof workerRefSchema>
|
|
119
|
+
|
|
120
|
+
/** Resolve a worker-name entry to its literal string. */
|
|
121
|
+
function resolveWorkerName(
|
|
122
|
+
ref: WorkerRef,
|
|
123
|
+
imports: Record<string, unknown>,
|
|
124
|
+
fieldName: string,
|
|
125
|
+
): string {
|
|
126
|
+
if (typeof ref === 'string') return ref
|
|
127
|
+
const instanceOutputs = imports[ref.from]
|
|
128
|
+
if (instanceOutputs === undefined) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`${fieldName} references instance "${ref.from}", which is not in this instance's imports`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
const value = (instanceOutputs as Record<string, unknown> | null)?.[ref.output]
|
|
134
|
+
if (typeof value !== 'string') {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`${fieldName} references output "${ref.output}" on instance "${ref.from}", which doesn't emit it`,
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
return value
|
|
140
|
+
}
|
|
141
|
+
|
|
101
142
|
/** A routing rule action target. */
|
|
102
143
|
const addressSchema = z.object({
|
|
103
144
|
/** Local part only — the domain comes from config.domain. */
|
|
@@ -105,13 +146,14 @@ const addressSchema = z.object({
|
|
|
105
146
|
action: z.enum(['forward', 'worker']),
|
|
106
147
|
/** forward: a verified destination address (email). */
|
|
107
148
|
destination: z.string().optional(),
|
|
108
|
-
/** worker: the
|
|
109
|
-
workerName:
|
|
149
|
+
/** worker: the script name, literal or `{ from, output }` import reference. */
|
|
150
|
+
workerName: workerRefSchema.optional(),
|
|
110
151
|
})
|
|
111
152
|
|
|
112
153
|
const catchAllSchema = z.object({
|
|
113
154
|
action: z.enum(['worker', 'forward', 'drop']),
|
|
114
|
-
|
|
155
|
+
/** worker: the script name, literal or `{ from, output }` import reference. */
|
|
156
|
+
workerName: workerRefSchema.optional(),
|
|
115
157
|
destination: z.string().optional(),
|
|
116
158
|
})
|
|
117
159
|
|
|
@@ -132,17 +174,18 @@ interface SendingSubdomain {
|
|
|
132
174
|
/** Managed-rule marker so destroy only removes rules this module created. */
|
|
133
175
|
const RULE_PREFIX = 'zbc:'
|
|
134
176
|
|
|
135
|
-
function ruleAction(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
177
|
+
function ruleAction(
|
|
178
|
+
entry: { action: string; destination?: string; workerName?: WorkerRef },
|
|
179
|
+
imports: Record<string, unknown>,
|
|
180
|
+
fieldName: string,
|
|
181
|
+
): { type: string; value?: string[] } {
|
|
139
182
|
if (entry.action === 'forward') {
|
|
140
|
-
if (!entry.destination) throw new Error(
|
|
183
|
+
if (!entry.destination) throw new Error(`${fieldName}: action "forward" requires a destination`)
|
|
141
184
|
return { type: 'forward', value: [entry.destination] }
|
|
142
185
|
}
|
|
143
186
|
if (entry.action === 'worker') {
|
|
144
|
-
if (!entry.workerName) throw new Error(
|
|
145
|
-
return { type: 'worker', value: [entry.workerName] }
|
|
187
|
+
if (!entry.workerName) throw new Error(`${fieldName}: action "worker" requires a workerName`)
|
|
188
|
+
return { type: 'worker', value: [resolveWorkerName(entry.workerName, imports, fieldName)] }
|
|
146
189
|
}
|
|
147
190
|
return { type: 'drop' }
|
|
148
191
|
}
|
|
@@ -343,7 +386,7 @@ export const cloudflareEmailModule = defineModule({
|
|
|
343
386
|
name: ruleName,
|
|
344
387
|
enabled: true,
|
|
345
388
|
matchers: [{ type: 'literal', field: 'to', value: email }],
|
|
346
|
-
actions: [ruleAction(entry)],
|
|
389
|
+
actions: [ruleAction(entry, ctx.imports, `addresses[${entry.localPart}]`)],
|
|
347
390
|
}
|
|
348
391
|
const existing = existingRules.find(
|
|
349
392
|
(r) =>
|
|
@@ -370,7 +413,7 @@ export const cloudflareEmailModule = defineModule({
|
|
|
370
413
|
name: `${RULE_PREFIX}catch-all`,
|
|
371
414
|
enabled: true,
|
|
372
415
|
matchers: [{ type: 'all' }],
|
|
373
|
-
actions: [ruleAction(config.catchAll)],
|
|
416
|
+
actions: [ruleAction(config.catchAll, ctx.imports, 'catchAll')],
|
|
374
417
|
},
|
|
375
418
|
})
|
|
376
419
|
console.log(` Routing: catch-all → ${config.catchAll.action}`)
|