@zabaca/zbc 0.21.1 → 0.23.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.
Files changed (44) hide show
  1. package/package.json +6 -1
  2. package/templates/CLAUDE.md +5 -0
  3. package/templates/apps/walgit/CONTEXT.md +5 -1
  4. package/templates/apps/walgit/README.md +159 -24
  5. package/templates/apps/walgit/package.json +2 -2
  6. package/templates/apps/walgit/registry.json +1 -1
  7. package/templates/apps/walgit/shared/browse.ts +18 -4
  8. package/templates/apps/walgit/shared/capabilities.ts +28 -3
  9. package/templates/apps/walgit/shared/container-env.ts +41 -2
  10. package/templates/apps/walgit/shared/container-lifecycle.ts +239 -0
  11. package/templates/apps/walgit/shared/container-snapshot.ts +219 -0
  12. package/templates/apps/walgit/shared/edge-refs.ts +612 -0
  13. package/templates/apps/walgit/{src → shared}/instructions.ts +8 -4
  14. package/templates/apps/walgit/shared/keys.ts +17 -0
  15. package/templates/apps/walgit/shared/landing.ts +10 -3
  16. package/templates/apps/walgit/shared/llms.ts +14 -5
  17. package/templates/apps/walgit/shared/policy.ts +1 -1
  18. package/templates/apps/walgit/shared/protocol.ts +35 -0
  19. package/templates/apps/walgit/shared/sleep-after.ts +44 -0
  20. package/templates/apps/walgit/shared/wal-index.ts +22 -1
  21. package/templates/apps/walgit/src/boot-residue.ts +58 -0
  22. package/templates/apps/walgit/src/cache.ts +9 -0
  23. package/templates/apps/walgit/src/cli.ts +39 -4
  24. package/templates/apps/walgit/src/delete-repo.ts +58 -9
  25. package/templates/apps/walgit/src/edge-refs.ts +148 -0
  26. package/templates/apps/walgit/src/expire.ts +75 -7
  27. package/templates/apps/walgit/src/hook-main.ts +2 -0
  28. package/templates/apps/walgit/src/http.ts +17 -4
  29. package/templates/apps/walgit/src/materialize.ts +34 -15
  30. package/templates/apps/walgit/src/push.ts +34 -8
  31. package/templates/apps/walgit/src/reconcile.ts +6 -1
  32. package/templates/apps/walgit/src/server.ts +69 -8
  33. package/templates/apps/walgit/src/sync.ts +10 -2
  34. package/templates/apps/walgit/worker/container-binding.ts +104 -0
  35. package/templates/apps/walgit/worker/durable-container.ts +786 -0
  36. package/templates/apps/walgit/worker/events-do.ts +16 -12
  37. package/templates/apps/walgit/worker/index.ts +235 -17
  38. package/templates/apps/walgit/worker/mcp.ts +9 -4
  39. package/templates/apps/walgit/wrangler.jsonc +44 -33
  40. package/templates/claude/settings.json +8 -0
  41. package/templates/infra/modules/cloudflare/index.ts +9 -0
  42. package/templates/infra/modules/cloudflare-zone/index.ts +83 -9
  43. package/templates/infra/modules/d1/index.ts +30 -4
  44. package/templates/infra/modules/d1/registry.json +1 -1
@@ -138,6 +138,26 @@ const recordSchema = z.discriminatedUnion('type', [
138
138
  ttl: ttlField,
139
139
  })
140
140
  .strict(),
141
+ /**
142
+ * Declared as its four fields rather than as one `content` string, because
143
+ * no single string means the same thing on both sides: the dashboard shows
144
+ * `priority weight port target`, while the API's `content` is
145
+ * `weight port target` with priority in a field of its own. A copied
146
+ * dashboard value would shift every number one place. See `desiredRecord`.
147
+ */
148
+ z
149
+ .object({
150
+ type: z.literal('SRV'),
151
+ /** The `_service._proto` label, e.g. `_autodiscover._tcp`. */
152
+ name: z.string(),
153
+ priority: z.number().int().min(0).max(65535),
154
+ weight: z.number().int().min(0).max(65535),
155
+ port: z.number().int().min(0).max(65535),
156
+ /** The host the service lives on. */
157
+ target: z.string().min(1),
158
+ ttl: ttlField,
159
+ })
160
+ .strict(),
141
161
  ])
142
162
 
143
163
  export type RecordConfig = z.infer<typeof recordSchema>
@@ -234,6 +254,9 @@ export function fqdn(name: string, zone: string): string {
234
254
  * Comparable form of a record's content. Cloudflare round-trips TXT values
235
255
  * with surrounding quotes in some API versions and normalises hostname targets
236
256
  * to lowercase without a trailing dot; neither difference is a change.
257
+ *
258
+ * An SRV's content is `weight port target`, and only the target is a hostname:
259
+ * the numbers compare as written, the last token as any other hostname does.
237
260
  */
238
261
  export function normalizeContent(type: string, content: string): string {
239
262
  const value = content.trim()
@@ -245,9 +268,22 @@ export function normalizeContent(type: string, content: string): string {
245
268
  if (type === 'CNAME' || type === 'MX' || type === 'NS' || type === 'AAAA') {
246
269
  return value.replace(/\.$/, '').toLowerCase()
247
270
  }
271
+ if (type === 'SRV') {
272
+ const tokens = value.split(/\s+/)
273
+ const target = tokens.pop() ?? ''
274
+ return [...tokens, target.replace(/\.$/, '').toLowerCase()].join(' ')
275
+ }
248
276
  return value
249
277
  }
250
278
 
279
+ /** What Cloudflare's create and PATCH take for an SRV, in place of `content`. */
280
+ export interface SrvData {
281
+ priority: number
282
+ weight: number
283
+ port: number
284
+ target: string
285
+ }
286
+
251
287
  export interface DesiredRecord {
252
288
  type: string
253
289
  /** Always an FQDN by the time it gets here. */
@@ -256,6 +292,38 @@ export interface DesiredRecord {
256
292
  ttl: number
257
293
  proxied?: boolean
258
294
  priority?: number
295
+ /** SRV only: the fields `content` and `priority` were derived from. */
296
+ data?: SrvData
297
+ }
298
+
299
+ /**
300
+ * One declared record in the shape the live one is compared against. For an
301
+ * SRV that is the API's shape, not the dashboard's: `content` is
302
+ * `weight port target` and priority rides alongside, exactly as
303
+ * `GET /dns_records` returns it, so `sameValue` and `differs` need no SRV case.
304
+ * `data` is carried too, because it and not `content` is what the write sends.
305
+ */
306
+ export function desiredRecord(record: RecordConfig, zone: string): DesiredRecord {
307
+ const name = fqdn(record.name, zone)
308
+ if (record.type === 'SRV') {
309
+ const { priority, weight, port, target } = record
310
+ return {
311
+ type: record.type,
312
+ name,
313
+ content: `${weight} ${port} ${target}`,
314
+ ttl: record.ttl,
315
+ priority,
316
+ data: { priority, weight, port, target },
317
+ }
318
+ }
319
+ return {
320
+ type: record.type,
321
+ name,
322
+ content: record.content,
323
+ ttl: record.ttl,
324
+ ...('proxied' in record ? { proxied: record.proxied } : {}),
325
+ ...('priority' in record ? { priority: record.priority } : {}),
326
+ }
259
327
  }
260
328
 
261
329
  export interface ActualRecord extends DesiredRecord {
@@ -323,7 +391,15 @@ function differs(desired: DesiredRecord, actual: DesiredRecord): boolean {
323
391
  return false
324
392
  }
325
393
 
326
- export const describeRecord = (r: DesiredRecord) => `${r.type} ${r.name} -> ${r.content}`
394
+ /**
395
+ * An SRV is printed `priority weight port target`, the order the dashboard and
396
+ * a zone file use. Its `content` alone omits priority, and a drift line is what
397
+ * an operator copies into an instance, so it has to carry all four numbers.
398
+ */
399
+ export const describeRecord = (r: DesiredRecord) =>
400
+ r.type === 'SRV'
401
+ ? `${r.type} ${r.name} -> ${r.priority ?? 0} ${r.content}`
402
+ : `${r.type} ${r.name} -> ${r.content}`
327
403
 
328
404
  export interface ZonePlan {
329
405
  create: DesiredRecord[]
@@ -562,6 +638,11 @@ async function listRecords(token: string, zoneId: string): Promise<ActualRecord[
562
638
 
563
639
  /** The request body Cloudflare wants for one declared record. */
564
640
  function recordBody(record: DesiredRecord): Record<string, unknown> {
641
+ // An SRV is written as its fields; `content` is what Cloudflare derives from
642
+ // them, and priority lives only inside `data`.
643
+ if (record.data) {
644
+ return { type: record.type, name: record.name, ttl: record.ttl, data: record.data }
645
+ }
565
646
  const body: Record<string, unknown> = {
566
647
  type: record.type,
567
648
  name: record.name,
@@ -617,14 +698,7 @@ export const cloudflareZoneModule = defineModule({
617
698
  }
618
699
 
619
700
  // 2. Diff the whole zone before mutating any of it.
620
- const desired = config.records.map((record) => ({
621
- type: record.type,
622
- name: fqdn(record.name, config.zone),
623
- content: record.content,
624
- ttl: record.ttl,
625
- ...('proxied' in record ? { proxied: record.proxied } : {}),
626
- ...('priority' in record ? { priority: record.priority } : {}),
627
- }))
701
+ const desired = config.records.map((record) => desiredRecord(record, config.zone))
628
702
  const actual = await listRecords(token, zone.id)
629
703
  const plan = planZone({ desired, actual })
630
704
 
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod'
2
2
  import { defineModule } from '../../src/define-module'
3
+ import type { ApplyContext } from '../../src/types'
3
4
  import { CfError, type CfOptions, cf, cfRaw } from '../cloudflare-api'
4
5
 
5
6
  /**
@@ -16,7 +17,10 @@ import { CfError, type CfOptions, cf, cfRaw } from '../cloudflare-api'
16
17
  * 'database_id', from: '<this instance>', output: 'databaseId' }` and the
17
18
  * placeholder id in the checked-in wrangler config is never read.
18
19
  *
19
- * Token scope: CLOUDFLARE_API_TOKEN needs Account → D1: Edit.
20
+ * Token scope: CLOUDFLARE_API_TOKEN needs Account → D1: Edit. Or set
21
+ * `apiToken: { from, output }` to use an imported instance's output instead
22
+ * (e.g. a `cloudflare-token` instance's `tokenValue`), the same way the
23
+ * `cloudflare` module takes its deploy credential.
20
24
  */
21
25
 
22
26
  /** Named per-code guidance for this module's calls — see `../cloudflare-api`. */
@@ -118,11 +122,33 @@ function isDuplicateColumn(err: unknown): boolean {
118
122
  return err instanceof CfError && /duplicate column name/i.test(err.message)
119
123
  }
120
124
 
125
+ /**
126
+ * The credential every call below uses: an imported instance's output when
127
+ * `apiToken` is set, else CLOUDFLARE_API_TOKEN from secrets.yaml. One helper
128
+ * so apply, destroy and the readiness probe can never disagree on it.
129
+ */
130
+ function apiTokenFor(
131
+ config: { apiToken?: { from: string; output: string } },
132
+ ctx: ApplyContext,
133
+ ): string {
134
+ return config.apiToken
135
+ ? ctx.output(config.apiToken, 'apiToken')
136
+ : ctx.secret('CLOUDFLARE_API_TOKEN')
137
+ }
138
+
121
139
  export const d1Module = defineModule({
122
140
  name: 'd1',
123
141
  configSchema: z.object({
124
142
  /** Cloudflare account id (not a secret — it's in the dashboard URL). */
125
143
  accountId: z.string(),
144
+ /**
145
+ * Where the credential comes from. Omit to read CLOUDFLARE_API_TOKEN from
146
+ * this environment's secrets.yaml. Set `{ from, output }` to pull it from
147
+ * an imported instance's outputs instead — e.g. a minted `cloudflare-token`
148
+ * whose permissions include D1 Read and D1 Write — so it never sits at
149
+ * rest. Mirrors the `cloudflare` module's `apiToken`.
150
+ */
151
+ apiToken: z.object({ from: z.string(), output: z.string() }).optional(),
126
152
  /** Database name — account-scoped, so namespace it per project/env. */
127
153
  databaseName: z.string(),
128
154
  /**
@@ -154,7 +180,7 @@ export const d1Module = defineModule({
154
180
  databaseId: z.string(),
155
181
  }),
156
182
  async apply(config, ctx) {
157
- const apiToken = ctx.secret('CLOUDFLARE_API_TOKEN')
183
+ const apiToken = apiTokenFor(config, ctx)
158
184
  const { accountId, databaseName } = config
159
185
 
160
186
  const existing = (await listDatabases(apiToken, accountId)).find((d) => d.name === databaseName)
@@ -211,7 +237,7 @@ export const d1Module = defineModule({
211
237
  * strand every instance behind it in a `zbc destroy`.
212
238
  */
213
239
  async destroy(config, ctx) {
214
- const apiToken = ctx.secret('CLOUDFLARE_API_TOKEN')
240
+ const apiToken = apiTokenFor(config, ctx)
215
241
  const { accountId, databaseName } = config
216
242
  // The lookup is deliberately OUTSIDE the try: the forgiving case is
217
243
  // "already gone", and that is the `!existing` branch. A listing that failed
@@ -246,7 +272,7 @@ export const d1Module = defineModule({
246
272
  ready: {
247
273
  proves: 'the database answers a query',
248
274
  async probe(outputs, config, ctx) {
249
- const apiToken = ctx.secret('CLOUDFLARE_API_TOKEN')
275
+ const apiToken = apiTokenFor(config, ctx)
250
276
  await query(apiToken, config.accountId, outputs.databaseId, 'SELECT 1')
251
277
  },
252
278
  },
@@ -9,5 +9,5 @@
9
9
  "secrets": ["CLOUDFLARE_API_TOKEN"],
10
10
  "signupUrl": "https://dash.cloudflare.com/sign-up",
11
11
  "tokenUrl": "https://dash.cloudflare.com/profile/api-tokens",
12
- "instructions": "Reads the Cloudflare API through `../cloudflare-api`; CLOUDFLARE_API_TOKEN needs the Account → D1: Edit scope.\n\nInstance example:\n\n // packages/infra/environments/production/app-db.ts\n import { d1Module } from '../../modules/d1'\n export default d1Module.instance({\n name: 'app-db',\n config: {\n accountId: '<account id>',\n databaseName: 'myproject-production',\n statements: ['CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY)'],\n },\n })\n\nWire it into a Worker via the cloudflare module — the binding must ALREADY be declared in that package's wrangler.jsonc (placeholder database_id and all); zbc only supplies the id:\n\n imports: [appDb],\n config: { ..., bindings: [{ type: 'd1_databases', binding: 'DB', field: 'database_id', from: 'app-db', output: 'databaseId' }] }\n\n`statements` are replayed on every apply, so each must be idempotent (`CREATE TABLE IF NOT EXISTS`). Use `additiveColumns` for `ALTER TABLE … ADD COLUMN`, which SQLite cannot spell idempotently — an already-present column is treated as success."
12
+ "instructions": "Reads the Cloudflare API through `../cloudflare-api`; CLOUDFLARE_API_TOKEN needs the Account → D1: Edit scope.\n\nInstance example:\n\n // packages/infra/environments/production/app-db.ts\n import { d1Module } from '../../modules/d1'\n export default d1Module.instance({\n name: 'app-db',\n config: {\n accountId: '<account id>',\n databaseName: 'myproject-production',\n statements: ['CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY)'],\n },\n })\n\nWire it into a Worker via the cloudflare module — the binding must ALREADY be declared in that package's wrangler.jsonc (placeholder database_id and all); zbc only supplies the id:\n\n imports: [appDb],\n config: { ..., bindings: [{ type: 'd1_databases', binding: 'DB', field: 'database_id', from: 'app-db', output: 'databaseId' }] }\n\nTo use a minted token instead of secrets.yaml (a `cloudflare-token` instance with 'D1 Read' and 'D1 Write'), import it and set `apiToken: { from: '<token instance>', output: 'tokenValue' }`.\n\n`statements` are replayed on every apply, so each must be idempotent (`CREATE TABLE IF NOT EXISTS`). Use `additiveColumns` for `ALTER TABLE … ADD COLUMN`, which SQLite cannot spell idempotently — an already-present column is treated as success."
13
13
  }