@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
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Which container a request goes to — the one seam every caller of the
3
+ * container uses (the proxy, the browse and raw routes, the sweep, the MCP
4
+ * reads, the ref-event object).
5
+ *
6
+ * Two container applications exist while a deployment moves from the
7
+ * `default` scheduling policy to `durable_object`, each behind its own Durable
8
+ * Object binding, and `WALGIT_CONTAINER_POLICY` picks one
9
+ * (`containerPolicyFrom`, shared/container-lifecycle.ts). Unset is the old one.
10
+ * That default is the point: `wrangler deploy` puts a Worker live before it
11
+ * creates the container application the Worker's new binding needs, so the
12
+ * deploy that introduces `WalgitDurableContainer` must not also route to it.
13
+ * README.md, "Deployment", has the phases.
14
+ *
15
+ * Both are reached the same way — one named object, `fetch` — so a caller holds
16
+ * a `ContainerFetcher` and never learns which application answered.
17
+ */
18
+
19
+ import { type Container, getContainer } from '@cloudflare/containers'
20
+
21
+ import { containerPolicyFrom } from '../shared/container-lifecycle'
22
+ import { SERVED_HEADER } from '../shared/protocol'
23
+ import { containerHost, type WalgitDurableContainer } from './durable-container'
24
+
25
+ /** The bindings and the switch, as every caller's environment carries them. */
26
+ export interface ContainerBindings {
27
+ /**
28
+ * The `default`-policy application's object (`WalgitContainer`). The BASE
29
+ * class rather than the subclass, which is defined in index.ts — and index.ts
30
+ * imports this file. All any caller does with it is `fetch`.
31
+ */
32
+ WALGIT_CONTAINER?: DurableObjectNamespace<Container>
33
+ /**
34
+ * The `durable_object`-policy application's object. Optional so a Worker
35
+ * deployed from a config that predates it still typechecks against reality:
36
+ * without the binding, the switch below has nothing to switch to.
37
+ */
38
+ WALGIT_DURABLE_CONTAINER?: DurableObjectNamespace<WalgitDurableContainer>
39
+ /** `durable_object` to route to the new application. Edge-only. */
40
+ WALGIT_CONTAINER_POLICY?: string
41
+ }
42
+
43
+ export interface ContainerFetcher {
44
+ fetch(request: Request): Promise<Response>
45
+ }
46
+
47
+ /**
48
+ * The container this deployment routes to.
49
+ *
50
+ * A switch set to `durable_object` on a Worker with no such binding is a
51
+ * configuration that cannot be honoured, and the answer is the old container
52
+ * with an error in the log rather than a refusal of every git request: the
53
+ * old application is still the one that exists.
54
+ */
55
+ export function containerFor(env: ContainerBindings): ContainerFetcher {
56
+ return retryingReads(pickContainer(env))
57
+ }
58
+
59
+ /**
60
+ * Send a bodiless read once more when the container never answered it.
61
+ *
62
+ * A deploy resets the Durable Object in front of the container, and a request
63
+ * caught mid-start in that moment is answered `500 Failed to start container:
64
+ * Durable Object reset because its code was updated` — by the object, with no
65
+ * `SERVED_HEADER`, because the container never saw it. The object's own retry
66
+ * dies with it, so the one that works is out here, against the NEW object. Only
67
+ * GET/HEAD, which carry no body to have been consumed, and only once.
68
+ */
69
+ /** The runtime's words when a deploy resets a Durable Object mid-request. */
70
+ const RESET_BY_DEPLOY = 'reset because its code was updated'
71
+
72
+ function retryingReads(target: ContainerFetcher): ContainerFetcher {
73
+ return {
74
+ async fetch(request) {
75
+ const response = await target.fetch(request)
76
+ const read = request.method === 'GET' || request.method === 'HEAD'
77
+ if (!read || response.status !== 500 || response.headers.has(SERVED_HEADER)) return response
78
+ // Only the reset. A start that genuinely failed already took its budget,
79
+ // and a second attempt would double the wait it was capped to avoid.
80
+ const text = await response.text()
81
+ if (!text.includes(RESET_BY_DEPLOY)) {
82
+ return new Response(text, { status: response.status, headers: response.headers })
83
+ }
84
+ return target.fetch(request)
85
+ },
86
+ }
87
+ }
88
+
89
+ function pickContainer(env: ContainerBindings): ContainerFetcher {
90
+ if (containerPolicyFrom(env) === 'durable_object') {
91
+ if (env.WALGIT_DURABLE_CONTAINER) return containerHost(env.WALGIT_DURABLE_CONTAINER)
92
+ console.error(
93
+ 'walgit: WALGIT_CONTAINER_POLICY=durable_object but no WALGIT_DURABLE_CONTAINER binding; ' +
94
+ 'routing to the default-policy container',
95
+ )
96
+ }
97
+ // A config that binds only the new class has nothing else to route to, so
98
+ // the switch is moot: this is every deployment after phase 3.
99
+ if (!env.WALGIT_CONTAINER) {
100
+ if (env.WALGIT_DURABLE_CONTAINER) return containerHost(env.WALGIT_DURABLE_CONTAINER)
101
+ throw new Error('walgit: no container binding (WALGIT_DURABLE_CONTAINER) configured')
102
+ }
103
+ return getContainer(env.WALGIT_CONTAINER)
104
+ }