@zabaca/zbc 0.24.0 → 0.25.0
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/CHANGELOG.md +28 -0
- package/package.json +3 -2
- package/src/commands/add.ts +78 -5
- package/src/commands/list.ts +6 -3
- package/src/commands/update.ts +42 -0
- package/src/engine/destroy.ts +23 -2
- package/src/engine/resolve.ts +5 -0
- package/src/utils/subtree.ts +37 -7
- package/src/utils/vendor-stamp.ts +92 -1
- package/src/utils/vendored-app.ts +353 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/walgit/worker/mcp.ts +1 -2
- package/templates/apps/warehouse/worker/router.ts +1 -2
- package/templates/apps/warehouse/worker/test-setup.ts +1 -2
- package/templates/infra/modules/cloudflare-access/index.ts +245 -28
- package/templates/infra/modules/cloudflare-access/registry.json +1 -1
- package/templates/infra/modules/d1/index.ts +133 -4
- package/templates/infra/modules/d1/registry.json +4 -1
- package/templates/infra/modules/queue/index.ts +313 -0
- package/templates/infra/modules/queue/registry.json +13 -0
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/src/define-module.ts +19 -2
- package/templates/infra/src/types.ts +24 -0
- package/templates/apps/inbox/package.json +0 -26
- package/templates/apps/inbox/public/index.html +0 -379
- package/templates/apps/inbox/registry.json +0 -9
- package/templates/apps/inbox/tsconfig.json +0 -19
- package/templates/apps/inbox/worker/api.ts +0 -197
- package/templates/apps/inbox/worker/commands.ts +0 -299
- package/templates/apps/inbox/worker/inbox-do.ts +0 -577
- package/templates/apps/inbox/worker/index.ts +0 -195
- package/templates/apps/inbox/worker/mcp.ts +0 -228
- package/templates/apps/inbox/worker/shared.ts +0 -117
- package/templates/apps/inbox/wrangler.jsonc +0 -48
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { defineModule } from '../../src/define-module'
|
|
3
|
+
import { type CfOptions, cf, cfRaw } from '../cloudflare-api'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* queue: a Cloudflare Queue and the Email Sending event subscriptions that fill
|
|
7
|
+
* it, via the REST API (r2-style: token through ctx.secret, idempotent
|
|
8
|
+
* list then create, console.log progress).
|
|
9
|
+
*
|
|
10
|
+
* What it is for: Cloudflare's Email Sending publishes an event for each
|
|
11
|
+
* recipient of each message a domain sends (delivered, bounced, complained, …)
|
|
12
|
+
* to a queue the account subscribes to, one subscription per sending domain. A
|
|
13
|
+
* Worker that wants its delivery reports consumes that queue.
|
|
14
|
+
*
|
|
15
|
+
* The queue has to EXIST before the Worker that consumes it is deployed:
|
|
16
|
+
* wrangler attaches a consumer by queue name (`queues.consumers` in the
|
|
17
|
+
* Worker's wrangler config) and does not create the queue. So the Worker's
|
|
18
|
+
* instance imports this one, which is what orders the apply.
|
|
19
|
+
*
|
|
20
|
+
* The calls are the ones wrangler's own `queues create` and `queues
|
|
21
|
+
* subscription create` make: `POST /accounts/{id}/queues` with `queue_name`,
|
|
22
|
+
* and `POST /accounts/{id}/event_subscriptions/subscriptions` with a name, a
|
|
23
|
+
* source (`email.sending`, a zone id and a domain), a destination (the queue)
|
|
24
|
+
* and the event names, written as wrangler's `--events` flag takes them
|
|
25
|
+
* (`message.bounced`; the queue message itself carries the full
|
|
26
|
+
* `cf.email.sending.message.bounced`). What the API answers when it LISTS a
|
|
27
|
+
* subscription is typed only by wrangler's client, so `planSubscription` reads
|
|
28
|
+
* the source and the destination in either spelling.
|
|
29
|
+
*
|
|
30
|
+
* ── WHAT IT NEVER DOES ───────────────────────────────────────────────────
|
|
31
|
+
* It deletes nothing it did not make: a queue that is not named here, or a
|
|
32
|
+
* subscription that is not listed, is left alone. `destroy` removes the
|
|
33
|
+
* subscriptions this instance names on its queue and LEAVES THE QUEUE, because
|
|
34
|
+
* messages may be waiting in it. A subscription that already exists under the
|
|
35
|
+
* same name is brought to the wanted events and nothing else, and one whose
|
|
36
|
+
* source reads back as a different domain or zone is a hard error rather than
|
|
37
|
+
* an overwrite.
|
|
38
|
+
*
|
|
39
|
+
* Token scope: CLOUDFLARE_API_TOKEN needs Account → Queues: Edit. Creating an
|
|
40
|
+
* event subscription may ask for more on some accounts; the refusal names the
|
|
41
|
+
* call that was refused.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/** Named per-code guidance for this module's calls; see `../cloudflare-api`. */
|
|
45
|
+
const OPTS: CfOptions = {
|
|
46
|
+
hints: {
|
|
47
|
+
10000:
|
|
48
|
+
`rejected the token (10000 Authentication error). ` +
|
|
49
|
+
`CLOUDFLARE_API_TOKEN is likely missing the Account → Queues: Edit scope. ` +
|
|
50
|
+
`Edit the token at https://dash.cloudflare.com/profile/api-tokens and re-run zbc apply.`,
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const subscriptionSchema = z.object({
|
|
55
|
+
/** The subscription's name, unique on the queue: this is how an apply finds the one it made. */
|
|
56
|
+
name: z.string(),
|
|
57
|
+
/** The zone the sending domain is in. */
|
|
58
|
+
zoneId: z.string(),
|
|
59
|
+
/** The sending domain: the zone's apex, or a verified sending subdomain. */
|
|
60
|
+
domain: z.string(),
|
|
61
|
+
/** The events, as wrangler's `--events` takes them (`message.bounced`). */
|
|
62
|
+
events: z.array(z.string()).min(1),
|
|
63
|
+
})
|
|
64
|
+
export type Subscription = z.infer<typeof subscriptionSchema>
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A subscription as Cloudflare lists it. Wrangler's client types the source and
|
|
68
|
+
* the destination in snake_case (`zone_id`, `queue_id`); both spellings are read,
|
|
69
|
+
* and a field that is in neither is simply unknown.
|
|
70
|
+
*/
|
|
71
|
+
export interface ListedSubscription {
|
|
72
|
+
id: string
|
|
73
|
+
name: string
|
|
74
|
+
enabled?: boolean
|
|
75
|
+
source?: { type?: string; zone_id?: string; zoneId?: string; domain?: string }
|
|
76
|
+
destination?: { type?: string; queue_id?: string; queueId?: string }
|
|
77
|
+
events?: string[]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type SubscriptionPlan =
|
|
81
|
+
| { action: 'create' }
|
|
82
|
+
| { action: 'leave'; id: string; warn?: string }
|
|
83
|
+
| { action: 'update-events'; id: string; warn?: string }
|
|
84
|
+
| { action: 'conflict'; id: string; why: string }
|
|
85
|
+
|
|
86
|
+
const sameSet = (a: readonly string[], b: readonly string[]): boolean =>
|
|
87
|
+
a.length === b.length && a.toSorted().join() === b.toSorted().join()
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The listed subscription that is this one: same NAME, on THIS QUEUE. One whose
|
|
91
|
+
* destination names another queue is somebody else's and is not looked at; one
|
|
92
|
+
* whose destination reads back in neither spelling is taken to be on this queue,
|
|
93
|
+
* since the listing was asked for this queue's subscriptions.
|
|
94
|
+
*/
|
|
95
|
+
function findMine(
|
|
96
|
+
existing: readonly ListedSubscription[],
|
|
97
|
+
name: string,
|
|
98
|
+
queueId: string,
|
|
99
|
+
): ListedSubscription | undefined {
|
|
100
|
+
return existing.find((s) => {
|
|
101
|
+
const to = s.destination?.queue_id ?? s.destination?.queueId
|
|
102
|
+
return s.name === name && (to === undefined || to === queueId)
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* What to do about one wanted subscription, given the ones listed. Pure, so the
|
|
108
|
+
* outcomes are a table test.
|
|
109
|
+
*
|
|
110
|
+
* A source that reads back as a DIFFERENT domain or zone, or as another source
|
|
111
|
+
* type, is a conflict and is never overwritten: someone changed it, or the name
|
|
112
|
+
* was reused, and which is right is not this module's to decide. A source that
|
|
113
|
+
* reads back in a shape this module cannot read (no domain, no zone) is not a
|
|
114
|
+
* conflict: the next apply must not be blocked by a subscription the first one
|
|
115
|
+
* made, over how Cloudflare spells it. It carries on as the same subscription
|
|
116
|
+
* and says so (`warn`).
|
|
117
|
+
*/
|
|
118
|
+
export function planSubscription(
|
|
119
|
+
wanted: Subscription,
|
|
120
|
+
existing: readonly ListedSubscription[],
|
|
121
|
+
queueId: string,
|
|
122
|
+
): SubscriptionPlan {
|
|
123
|
+
const found = findMine(existing, wanted.name, queueId)
|
|
124
|
+
if (!found) return { action: 'create' }
|
|
125
|
+
const zone = found.source?.zone_id ?? found.source?.zoneId
|
|
126
|
+
const domain = found.source?.domain
|
|
127
|
+
const type = found.source?.type
|
|
128
|
+
const readable = domain !== undefined && zone !== undefined
|
|
129
|
+
if (
|
|
130
|
+
(type !== undefined && type !== 'email.sending') ||
|
|
131
|
+
(readable && (zone !== wanted.zoneId || domain !== wanted.domain))
|
|
132
|
+
) {
|
|
133
|
+
return {
|
|
134
|
+
action: 'conflict',
|
|
135
|
+
id: found.id,
|
|
136
|
+
why: `a subscription named "${wanted.name}" exists on another source (${type ?? '?'} ${domain ?? '?'}); not changed.`,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const warn = readable
|
|
140
|
+
? undefined
|
|
141
|
+
: `its source reads back in a shape this module does not know (${JSON.stringify(found.source ?? null)}), so it is taken to be the one made here.`
|
|
142
|
+
const same = sameSet(found.events ?? [], wanted.events) && found.enabled !== false
|
|
143
|
+
return same
|
|
144
|
+
? { action: 'leave', id: found.id, ...(warn && { warn }) }
|
|
145
|
+
: { action: 'update-events', id: found.id, ...(warn && { warn }) }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface QueueRow {
|
|
149
|
+
queue_id: string
|
|
150
|
+
queue_name: string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Every row of a listing, page by page.
|
|
155
|
+
*
|
|
156
|
+
* Paginated rather than asked for in one oversized page, for the reason `d1`
|
|
157
|
+
* gives: a single page that "worked" on a small account is a silent wrong
|
|
158
|
+
* answer on a large one, and here that means a second queue created under a
|
|
159
|
+
* name that is already taken. A listing that reports no `result_info` is one
|
|
160
|
+
* page. Bounded, so a provider that never says "last page" fails rather than
|
|
161
|
+
* hangs.
|
|
162
|
+
*/
|
|
163
|
+
async function listAll<T>(token: string, path: string): Promise<T[]> {
|
|
164
|
+
const all: T[] = []
|
|
165
|
+
const sep = path.includes('?') ? '&' : '?'
|
|
166
|
+
for (let page = 1; page <= 100; page++) {
|
|
167
|
+
const envelope = await cfRaw<T[]>(
|
|
168
|
+
token,
|
|
169
|
+
'GET',
|
|
170
|
+
`${path}${sep}per_page=100&page=${page}`,
|
|
171
|
+
undefined,
|
|
172
|
+
OPTS,
|
|
173
|
+
)
|
|
174
|
+
all.push(...(envelope.result ?? []))
|
|
175
|
+
const info = envelope.result_info
|
|
176
|
+
if (!info || page >= (info.total_pages ?? 1)) return all
|
|
177
|
+
}
|
|
178
|
+
throw new Error(`Cloudflare listing ${path} did not terminate after 100 pages`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function findQueue(
|
|
182
|
+
token: string,
|
|
183
|
+
accountId: string,
|
|
184
|
+
queueName: string,
|
|
185
|
+
): Promise<QueueRow | undefined> {
|
|
186
|
+
const queues = await listAll<QueueRow>(token, `/accounts/${accountId}/queues`)
|
|
187
|
+
return queues.find((q) => q.queue_name === queueName)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function listSubscriptions(
|
|
191
|
+
token: string,
|
|
192
|
+
accountId: string,
|
|
193
|
+
queueId: string,
|
|
194
|
+
): Promise<ListedSubscription[]> {
|
|
195
|
+
return listAll<ListedSubscription>(
|
|
196
|
+
token,
|
|
197
|
+
`/accounts/${accountId}/event_subscriptions/subscriptions?queue_id=${encodeURIComponent(queueId)}`,
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export const queueModule = defineModule({
|
|
202
|
+
name: 'queue',
|
|
203
|
+
configSchema: z.object({
|
|
204
|
+
/** Cloudflare account id (not a secret: it is in the dashboard URL). */
|
|
205
|
+
accountId: z.string(),
|
|
206
|
+
/** Queue name: account-scoped, and what the consuming Worker's wrangler config names. */
|
|
207
|
+
queueName: z.string(),
|
|
208
|
+
/** Email Sending event subscriptions that fill the queue, one per sending domain. */
|
|
209
|
+
subscriptions: z.array(subscriptionSchema).default([]),
|
|
210
|
+
}),
|
|
211
|
+
outputs: z.object({
|
|
212
|
+
queueName: z.string(),
|
|
213
|
+
queueId: z.string(),
|
|
214
|
+
/** The id of each declared subscription, by its name. */
|
|
215
|
+
subscriptionIds: z.record(z.string()),
|
|
216
|
+
}),
|
|
217
|
+
async apply(config, ctx) {
|
|
218
|
+
const token = ctx.secret('CLOUDFLARE_API_TOKEN')
|
|
219
|
+
const base = `/accounts/${config.accountId}`
|
|
220
|
+
|
|
221
|
+
let queue = await findQueue(token, config.accountId, config.queueName)
|
|
222
|
+
if (queue) {
|
|
223
|
+
console.log(` Queue "${config.queueName}" already exists`)
|
|
224
|
+
} else {
|
|
225
|
+
queue = await cf<QueueRow>(
|
|
226
|
+
token,
|
|
227
|
+
'POST',
|
|
228
|
+
`${base}/queues`,
|
|
229
|
+
{ queue_name: config.queueName },
|
|
230
|
+
OPTS,
|
|
231
|
+
)
|
|
232
|
+
console.log(` Created queue "${config.queueName}"`)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const existing =
|
|
236
|
+
config.subscriptions.length > 0
|
|
237
|
+
? await listSubscriptions(token, config.accountId, queue.queue_id)
|
|
238
|
+
: []
|
|
239
|
+
const subscriptionIds: Record<string, string> = {}
|
|
240
|
+
for (const wanted of config.subscriptions) {
|
|
241
|
+
const plan = planSubscription(wanted, existing, queue.queue_id)
|
|
242
|
+
if (plan.action === 'conflict') throw new Error(`Subscription ${wanted.name}: ${plan.why}`)
|
|
243
|
+
if ('warn' in plan && plan.warn) console.log(` Subscription "${wanted.name}": ${plan.warn}`)
|
|
244
|
+
if (plan.action === 'leave') {
|
|
245
|
+
console.log(` Subscription "${wanted.name}" already there`)
|
|
246
|
+
subscriptionIds[wanted.name] = plan.id
|
|
247
|
+
} else if (plan.action === 'update-events') {
|
|
248
|
+
await cf(
|
|
249
|
+
token,
|
|
250
|
+
'PATCH',
|
|
251
|
+
`${base}/event_subscriptions/subscriptions/${plan.id}`,
|
|
252
|
+
{ enabled: true, events: wanted.events },
|
|
253
|
+
OPTS,
|
|
254
|
+
)
|
|
255
|
+
console.log(` Subscription "${wanted.name}": events set to ${wanted.events.join(', ')}`)
|
|
256
|
+
subscriptionIds[wanted.name] = plan.id
|
|
257
|
+
} else {
|
|
258
|
+
const made = await cf<{ id: string }>(
|
|
259
|
+
token,
|
|
260
|
+
'POST',
|
|
261
|
+
`${base}/event_subscriptions/subscriptions`,
|
|
262
|
+
{
|
|
263
|
+
name: wanted.name,
|
|
264
|
+
enabled: true,
|
|
265
|
+
source: { type: 'email.sending', zone_id: wanted.zoneId, domain: wanted.domain },
|
|
266
|
+
destination: { type: 'queues.queue', queue_id: queue.queue_id },
|
|
267
|
+
events: wanted.events,
|
|
268
|
+
},
|
|
269
|
+
OPTS,
|
|
270
|
+
)
|
|
271
|
+
console.log(
|
|
272
|
+
` Subscribed "${wanted.name}" (${wanted.domain}) to ${wanted.events.join(', ')}`,
|
|
273
|
+
)
|
|
274
|
+
subscriptionIds[wanted.name] = made.id
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return { queueName: config.queueName, queueId: queue.queue_id, subscriptionIds }
|
|
278
|
+
},
|
|
279
|
+
/**
|
|
280
|
+
* Remove the subscriptions this instance names, on its own queue, and leave
|
|
281
|
+
* the queue: messages may be waiting in it, and a queue is cheap to keep.
|
|
282
|
+
*
|
|
283
|
+
* A failed delete THROWS. It is not the bucket case `r2` reports and moves
|
|
284
|
+
* past (a non-empty bucket is the ordinary outcome there); a subscription
|
|
285
|
+
* left standing keeps publishing into a queue somebody believes was torn
|
|
286
|
+
* down, so a destroy that reported it as done would be the lie.
|
|
287
|
+
*/
|
|
288
|
+
async destroy(config, ctx) {
|
|
289
|
+
const token = ctx.secret('CLOUDFLARE_API_TOKEN')
|
|
290
|
+
const queue = await findQueue(token, config.accountId, config.queueName)
|
|
291
|
+
if (!queue) {
|
|
292
|
+
console.log(` Queue "${config.queueName}" is already gone`)
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
const existing =
|
|
296
|
+
config.subscriptions.length > 0
|
|
297
|
+
? await listSubscriptions(token, config.accountId, queue.queue_id)
|
|
298
|
+
: []
|
|
299
|
+
for (const wanted of config.subscriptions) {
|
|
300
|
+
const found = findMine(existing, wanted.name, queue.queue_id)
|
|
301
|
+
if (!found) continue
|
|
302
|
+
await cf(
|
|
303
|
+
token,
|
|
304
|
+
'DELETE',
|
|
305
|
+
`/accounts/${config.accountId}/event_subscriptions/subscriptions/${found.id}`,
|
|
306
|
+
undefined,
|
|
307
|
+
OPTS,
|
|
308
|
+
)
|
|
309
|
+
console.log(` Deleted subscription "${wanted.name}"`)
|
|
310
|
+
}
|
|
311
|
+
console.log(` Queue "${config.queueName}" left in place (it may hold messages)`)
|
|
312
|
+
},
|
|
313
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "queue",
|
|
3
|
+
"modules": ["cloudflare-api"],
|
|
4
|
+
"description": "Cloudflare Queue and the Email Sending event subscriptions that fill it, provisioned via the REST API. The Worker that consumes it names the queue in its wrangler config (queues.consumers).",
|
|
5
|
+
"files": [{ "path": "index.ts" }],
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"zod": "^3.24.0"
|
|
8
|
+
},
|
|
9
|
+
"secrets": ["CLOUDFLARE_API_TOKEN"],
|
|
10
|
+
"signupUrl": "https://dash.cloudflare.com/sign-up",
|
|
11
|
+
"tokenUrl": "https://dash.cloudflare.com/profile/api-tokens",
|
|
12
|
+
"instructions": "Reads the Cloudflare API through `../cloudflare-api`; CLOUDFLARE_API_TOKEN needs the Account → Queues: Edit scope.\n\nInstance example:\n\n // packages/infra/environments/production/delivery.ts\n import { queueModule } from '../../modules/queue'\n export default queueModule.instance({\n name: 'delivery',\n config: {\n accountId: '<account id>',\n queueName: 'myproject-delivery',\n subscriptions: [{ name: 'myproject-sending', zoneId: '<zone id>', domain: 'example.com', events: ['message.delivered', 'message.bounced'] }],\n },\n })\n\nThe consuming Worker declares the queue by name in its own wrangler config (`queues.consumers: [{ queue: 'myproject-delivery' }]`) and imports this instance, so the queue exists before wrangler attaches the consumer. `destroy` removes the subscriptions this instance names and leaves the queue."
|
|
13
|
+
}
|
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
"files": [{ "path": "index.ts" }],
|
|
6
6
|
"secrets": ["CLOUDFLARE_API_TOKEN"],
|
|
7
7
|
"tokenUrl": "https://dash.cloudflare.com/profile/api-tokens",
|
|
8
|
-
"instructions": "Reads the Cloudflare API through `../cloudflare-api`; CLOUDFLARE_API_TOKEN needs the Account → Workers R2 Storage: Edit scope.\n\nInstance example:\n\n // packages/infra/environments/production/inbox-raw.ts\n import { r2Module } from '../../modules/r2'\n export default r2Module.instance({\n name: 'inbox-raw',\n config: { accountId: '<account id>', bucketName: 'myproject-inbox-raw' },\n })\n\nWire it into a Worker via the cloudflare module:\n\n imports: [inboxRaw],\n config: { ..., r2Bindings: [{ binding: 'RAW', from: 'inbox-raw', output: 'bucketName' }] }"
|
|
8
|
+
"instructions": "Reads the Cloudflare API through `../cloudflare-api`; CLOUDFLARE_API_TOKEN needs the Account → Workers R2 Storage: Edit scope.\n\nInstance example:\n\n // packages/infra/environments/production/inbox-raw.ts\n import { r2Module } from '../../modules/r2'\n export default r2Module.instance({\n name: 'inbox-raw',\n config: { accountId: '<account id>', bucketName: 'myproject-inbox-raw' },\n })\n\nWire it into a Worker via the cloudflare module:\n\n imports: [inboxRaw],\n config: { ..., r2Bindings: [{ binding: 'RAW', from: 'inbox-raw', output: 'bucketName' }] }\n\n`destroy` deletes the bucket when it is empty. For one holding records no teardown may remove, set `retain: true` on the instance (beside `name`): `zbc destroy` then leaves it standing and refuses to target it."
|
|
9
9
|
}
|
|
@@ -98,14 +98,31 @@ export function defineModule<TConfig extends z.ZodType, TOutputs extends z.ZodTy
|
|
|
98
98
|
// present-but-empty key the engine would have to special-case.
|
|
99
99
|
...(opts.secretOutputs ? { secretOutputs: opts.secretOutputs } : {}),
|
|
100
100
|
instance(instanceOpts: InstanceOptions<TConfig>): ModuleInstance<TOutputs> {
|
|
101
|
+
const ephemeral =
|
|
102
|
+
instanceOpts.ephemeral ?? legacyConfigEphemeral(opts.name, instanceOpts.config)
|
|
103
|
+
const retain = instanceOpts.retain === true
|
|
104
|
+
if (retain && ephemeral) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Instance "${instanceOpts.name}" sets retain and ephemeral: ephemeral destroys it on ` +
|
|
107
|
+
`every apply, and retain says nothing may. Drop one.`,
|
|
108
|
+
)
|
|
109
|
+
}
|
|
101
110
|
return {
|
|
102
111
|
name: instanceOpts.name,
|
|
103
112
|
moduleName: opts.name,
|
|
104
113
|
config: instanceOpts.config,
|
|
105
114
|
imports: instanceOpts.imports ?? [],
|
|
106
|
-
ephemeral
|
|
115
|
+
ephemeral,
|
|
116
|
+
...(retain ? { retain } : {}),
|
|
107
117
|
_outputsSchema: opts.outputs,
|
|
108
|
-
|
|
118
|
+
// A retained instance carries NO destroy, rather than a flag the
|
|
119
|
+
// engine has to remember to read: an engine older than `retain` then
|
|
120
|
+
// finds nothing to call and skips it, as it skips any module without
|
|
121
|
+
// one. The newer engine reads `retain` only to say why, and to refuse
|
|
122
|
+
// a targeted destroy rather than skip it.
|
|
123
|
+
_definition: (retain
|
|
124
|
+
? { ...definition, destroy: undefined }
|
|
125
|
+
: definition) as ModuleDefinition<any, TOutputs>,
|
|
109
126
|
}
|
|
110
127
|
},
|
|
111
128
|
}
|
|
@@ -275,6 +275,24 @@ export interface InstanceOptions<TConfig extends z.ZodType> {
|
|
|
275
275
|
* with three different failure policies and the fourth with none at all.
|
|
276
276
|
*/
|
|
277
277
|
ephemeral?: boolean
|
|
278
|
+
/**
|
|
279
|
+
* Never destroy this resource: `zbc destroy <env>` leaves it standing and
|
|
280
|
+
* says so, and `zbc destroy <env> <this instance>` is refused. For a
|
|
281
|
+
* resource whose loss is not a teardown but a loss of records (a database,
|
|
282
|
+
* the bucket of their files), on a module whose `destroy` deletes.
|
|
283
|
+
*
|
|
284
|
+
* A property of the INSTANCE, like `ephemeral`, because whether a resource
|
|
285
|
+
* may be torn down is the environment's decision: the same `d1` module backs
|
|
286
|
+
* a scratch database a preview throws away and one nobody may delete.
|
|
287
|
+
*
|
|
288
|
+
* The instance carries no `destroy` at all (`_definition.destroy` is
|
|
289
|
+
* removed), so an engine that predates this key skips it too, as it skips
|
|
290
|
+
* any module without one. Refused together with `ephemeral`, which destroys
|
|
291
|
+
* on every apply. To delete a retained resource, remove `retain` in a
|
|
292
|
+
* reviewed change first, or delete it in the provider by hand. See
|
|
293
|
+
* docs/adr/0019-a-retained-instance-is-never-destroyed.md in zbc.
|
|
294
|
+
*/
|
|
295
|
+
retain?: boolean
|
|
278
296
|
}
|
|
279
297
|
|
|
280
298
|
export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
|
|
@@ -284,6 +302,12 @@ export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
|
|
|
284
302
|
imports: ModuleInstance[]
|
|
285
303
|
/** See `InstanceOptions.ephemeral`. Always set — the engine reads it directly. */
|
|
286
304
|
ephemeral: boolean
|
|
305
|
+
/**
|
|
306
|
+
* See `InstanceOptions.retain`. Optional, unlike `ephemeral`: an instance
|
|
307
|
+
* built by an older `define-module.ts` has none, and absent means not
|
|
308
|
+
* retained, which is what that instance always was.
|
|
309
|
+
*/
|
|
310
|
+
retain?: boolean
|
|
287
311
|
_outputsSchema: TOutputs
|
|
288
312
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
289
313
|
_definition: ModuleDefinition<any, TOutputs>
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zbc/inbox",
|
|
3
|
-
"license": "MIT",
|
|
4
|
-
"version": "0.0.1",
|
|
5
|
-
"private": true,
|
|
6
|
-
"type": "module",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"dev": "wrangler dev",
|
|
9
|
-
"deploy": "wrangler deploy",
|
|
10
|
-
"typecheck": "tsc --noEmit",
|
|
11
|
-
"test": "bun test worker"
|
|
12
|
-
},
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"@hono/mcp": "^0.3.1",
|
|
15
|
-
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
16
|
-
"hono": "^4.12.30",
|
|
17
|
-
"postal-mime": "^2.4.4",
|
|
18
|
-
"zod": "^4.4.3"
|
|
19
|
-
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@cloudflare/workers-types": "^4.20250101.0",
|
|
22
|
-
"@types/bun": "^1.3.14",
|
|
23
|
-
"typescript": "^7.0.2",
|
|
24
|
-
"wrangler": "^4.0.0"
|
|
25
|
-
}
|
|
26
|
-
}
|