@zabaca/zbc 0.11.0 → 0.13.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 (74) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/package.json +2 -1
  3. package/templates/apps/inbox/package.json +1 -1
  4. package/templates/apps/walgit/CONTEXT.md +135 -0
  5. package/templates/apps/walgit/Dockerfile +40 -18
  6. package/templates/apps/walgit/README.md +518 -55
  7. package/templates/apps/walgit/e2e/README.md +100 -6
  8. package/templates/apps/walgit/e2e/harness.ts +262 -1
  9. package/templates/apps/walgit/e2e/live.ts +588 -0
  10. package/templates/apps/walgit/e2e/scenarios.ts +367 -5
  11. package/templates/apps/walgit/e2e/suite.ts +5 -5
  12. package/templates/apps/walgit/examples/watch.ts +209 -0
  13. package/templates/apps/walgit/package.json +12 -3
  14. package/templates/apps/walgit/registry.json +6 -11
  15. package/templates/apps/walgit/shared/capabilities.ts +189 -0
  16. package/templates/apps/walgit/shared/container-env.ts +114 -0
  17. package/templates/apps/walgit/shared/credentials.ts +81 -0
  18. package/templates/apps/walgit/shared/events.ts +277 -0
  19. package/templates/apps/walgit/shared/landing.ts +1241 -0
  20. package/templates/apps/walgit/shared/llms.ts +463 -0
  21. package/templates/apps/walgit/shared/outbox.ts +266 -0
  22. package/templates/apps/walgit/shared/policy.ts +59 -0
  23. package/templates/apps/walgit/shared/protocol.ts +197 -0
  24. package/templates/apps/walgit/shared/provenance.ts +154 -0
  25. package/templates/apps/walgit/shared/ref-cache.ts +181 -0
  26. package/templates/apps/walgit/shared/telemetry.ts +219 -0
  27. package/templates/apps/walgit/src/announce-main.ts +37 -0
  28. package/templates/apps/walgit/src/announce.ts +98 -0
  29. package/templates/apps/walgit/src/append-only.ts +112 -0
  30. package/templates/apps/walgit/src/cache.ts +172 -0
  31. package/templates/apps/walgit/src/cli.ts +493 -0
  32. package/templates/apps/walgit/src/compact.ts +25 -23
  33. package/templates/apps/walgit/src/delete-repo.ts +237 -0
  34. package/templates/apps/walgit/src/expire.ts +248 -0
  35. package/templates/apps/walgit/src/gc.ts +8 -16
  36. package/templates/apps/walgit/src/git-backend.ts +29 -0
  37. package/templates/apps/walgit/src/git.ts +78 -0
  38. package/templates/apps/walgit/src/hook-main.ts +259 -17
  39. package/templates/apps/walgit/src/hooks.ts +4 -3
  40. package/templates/apps/walgit/src/http.ts +237 -45
  41. package/templates/apps/walgit/src/instructions.ts +240 -0
  42. package/templates/apps/walgit/src/keys.ts +126 -0
  43. package/templates/apps/walgit/src/limits.ts +158 -0
  44. package/templates/apps/walgit/src/materialize.bench.ts +10 -10
  45. package/templates/apps/walgit/src/materialize.ts +48 -40
  46. package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
  47. package/templates/apps/walgit/src/orphans.ts +2 -5
  48. package/templates/apps/walgit/src/pending.ts +201 -0
  49. package/templates/apps/walgit/src/push-cert.ts +254 -0
  50. package/templates/apps/walgit/src/push.ts +233 -46
  51. package/templates/apps/walgit/src/reconcile.ts +5 -8
  52. package/templates/apps/walgit/src/repo.ts +22 -77
  53. package/templates/apps/walgit/src/server.ts +148 -26
  54. package/templates/apps/walgit/src/signers.ts +577 -0
  55. package/templates/apps/walgit/src/store-env.ts +5 -4
  56. package/templates/apps/walgit/src/store.ts +47 -24
  57. package/templates/apps/walgit/src/usage.ts +404 -0
  58. package/templates/apps/walgit/src/verify.ts +171 -0
  59. package/templates/apps/walgit/src/wal-index.ts +189 -11
  60. package/templates/apps/walgit/tsconfig.json +8 -2
  61. package/templates/apps/walgit/tsconfig.worker.json +24 -0
  62. package/templates/apps/walgit/worker/events-do.ts +259 -0
  63. package/templates/apps/walgit/worker/index.ts +572 -0
  64. package/templates/apps/walgit/wrangler.jsonc +95 -0
  65. package/templates/apps/warehouse/package.json +1 -1
  66. package/templates/infra/modules/cloudflare/index.ts +77 -6
  67. package/templates/infra/modules/cloudflare-access/index.ts +85 -14
  68. package/templates/infra/modules/fly/index.ts +150 -10
  69. package/templates/infra/modules/turso/index.ts +43 -3
  70. package/templates/infra/package.json +1 -1
  71. package/templates/apps/walgit/entrypoint.sh +0 -59
  72. package/templates/apps/walgit/fly.toml +0 -52
  73. package/templates/apps/walgit/src/ssh-shell.ts +0 -36
  74. package/templates/apps/walgit/sshd_config +0 -38
@@ -0,0 +1,277 @@
1
+ /**
2
+ * The ref-event wire, and every decision it needs.
3
+ *
4
+ * A client that wants to know when a ref moves has no endpoint of its own — it
5
+ * runs in a sandbox with no ingress — so the connection is outbound and stays
6
+ * open, and walgit talks down it. That is the whole feature: one WebSocket, a
7
+ * list of what you care about, and the current sha of each, followed by the new
8
+ * sha every time one of them moves.
9
+ *
10
+ * Events are LATEST STATE, not a log. There is no cursor, no `since` and no
11
+ * sequence number anywhere in this file, deliberately: a subscriber that
12
+ * reconnects gets the current state in its handshake, which is the only answer
13
+ * a client acting on "is my main current?" can use. Replay would be a second
14
+ * source of truth beside `index.json`, and the Index already is one.
15
+ *
16
+ * Everything decidable lives here, pure: whether a credential is good, what a
17
+ * `watch` message means, and which sockets an announcement reaches.
18
+ * `worker/events-do.ts` is the shell that owns the sockets and makes none of
19
+ * those decisions — which is why this file lives in `shared/` and is tested
20
+ * from `src/` with the rest of the suite rather than behind a Workers runtime.
21
+ * The paths, the grammars and the zero oid are `shared/protocol.ts`'s, because
22
+ * the socket is not the only transport that validates a repository name.
23
+ */
24
+
25
+ import { authorizedBy } from './credentials'
26
+ import { REF_NAME, REPO_ID, ZERO_OID } from './protocol'
27
+
28
+ /** One repository, and optionally the refs within it that matter. */
29
+ export interface WatchEntry {
30
+ repo: string
31
+ /** Omitted means every ref in that repository. */
32
+ refs?: string[]
33
+ }
34
+
35
+ /** A ref at a moment. `sha: null` is a deletion — the ref is gone. */
36
+ export interface RefEvent {
37
+ repo: string
38
+ ref: string
39
+ sha: string | null
40
+ }
41
+
42
+ /** The answer to a `watch`: current state, before any event can fire. */
43
+ export interface Handshake {
44
+ ok: true
45
+ refs: RefEvent[]
46
+ }
47
+
48
+ export type Parsed<T> = { ok: true; value: T } | { ok: false; error: string }
49
+
50
+ /**
51
+ * How many watch entries and refs one message may carry.
52
+ *
53
+ * A cap rather than an unbounded list because one connection asking to watch
54
+ * every repository on the host is the operator's problem, not the client's: the
55
+ * handshake alone reads the Index once per repository named. Exported so the
56
+ * refusal, the tests and any future advertisement of the limit all read the
57
+ * same number.
58
+ *
59
+ * There is deliberately no wildcard beside these — no `repo: "*"`, no
60
+ * `watch: "all"`. On a public deployment that would be a firehose of strangers'
61
+ * pushes, and `REPO_ID` rejects `*` for the same reason a URL path does.
62
+ */
63
+ export const MAX_WATCH_ENTRIES = 64
64
+ export const MAX_REFS_PER_ENTRY = 256
65
+
66
+ /**
67
+ * May this request watch?
68
+ *
69
+ * Exactly the credential a read needs, and for the same reason: an event says
70
+ * that a ref moved and what it moved to, which is a strict subset of what a
71
+ * fetch of that repository hands over. A public deployment therefore has a
72
+ * public stream — anything else would be a lock on the announcement of data
73
+ * anyone may already clone.
74
+ */
75
+ export function authorizeSubscribe(input: {
76
+ authorization: string | null
77
+ tokens: string[]
78
+ isPublic: boolean
79
+ }): boolean {
80
+ if (input.isPublic) return true
81
+ return authorizedBy(input.authorization, input.tokens)
82
+ }
83
+
84
+ /**
85
+ * May this caller announce?
86
+ *
87
+ * A separate secret from the read tokens, because the two answer different
88
+ * questions: a read token says "you may see this repository", and this says
89
+ * "you are walgit's own push path". Handing a subscriber the power to fabricate
90
+ * events for a repository it can read would make the stream unusable as a
91
+ * trigger for anything.
92
+ */
93
+ export function authorizeAnnounce(authorization: string | null, secret: string): boolean {
94
+ return secret !== '' && authorizedBy(authorization, [secret])
95
+ }
96
+
97
+ /**
98
+ * What a client asked to watch.
99
+ *
100
+ * Rejected with a reason rather than ignored: a subscriber whose message was
101
+ * dropped silently waits forever for events about a repository it never
102
+ * successfully named, and looks — from the client side — exactly like a
103
+ * repository nobody is pushing to.
104
+ */
105
+ export function parseWatch(raw: string): Parsed<WatchEntry[]> {
106
+ let body: unknown
107
+ try {
108
+ body = JSON.parse(raw)
109
+ } catch {
110
+ return { ok: false, error: 'message is not JSON' }
111
+ }
112
+ if (typeof body !== 'object' || body === null)
113
+ return { ok: false, error: 'message is not an object' }
114
+
115
+ const watch = (body as { watch?: unknown }).watch
116
+ if (!Array.isArray(watch)) return { ok: false, error: 'expected a "watch" array' }
117
+ if (watch.length === 0) return { ok: false, error: '"watch" is empty' }
118
+ if (watch.length > MAX_WATCH_ENTRIES) {
119
+ // Both numbers, because a client over the cap has to know how far over it
120
+ // is to split its subscription — a bare limit makes it guess.
121
+ return {
122
+ ok: false,
123
+ error: `at most ${MAX_WATCH_ENTRIES} watch entries, asked for ${watch.length}`,
124
+ }
125
+ }
126
+
127
+ const entries: WatchEntry[] = []
128
+ for (const item of watch) {
129
+ if (typeof item !== 'object' || item === null)
130
+ return { ok: false, error: 'watch entry is not an object' }
131
+ const { repo, refs } = item as { repo?: unknown; refs?: unknown }
132
+ if (typeof repo !== 'string' || !REPO_ID.test(repo)) {
133
+ return {
134
+ ok: false,
135
+ error: `invalid repository name: ${JSON.stringify(repo ?? null)}`,
136
+ }
137
+ }
138
+ if (refs === undefined) {
139
+ entries.push({ repo })
140
+ continue
141
+ }
142
+ if (!Array.isArray(refs)) return { ok: false, error: `"refs" for ${repo} is not an array` }
143
+ if (refs.length > MAX_REFS_PER_ENTRY) {
144
+ return {
145
+ ok: false,
146
+ error: `at most ${MAX_REFS_PER_ENTRY} refs per repository, asked for ${refs.length} on ${repo}`,
147
+ }
148
+ }
149
+ for (const ref of refs) {
150
+ if (typeof ref !== 'string' || !REF_NAME.test(ref)) {
151
+ return {
152
+ ok: false,
153
+ error: `invalid ref: ${JSON.stringify(ref ?? null)}`,
154
+ }
155
+ }
156
+ }
157
+ entries.push({ repo, refs: [...(refs as string[])] })
158
+ }
159
+ return { ok: true, value: entries }
160
+ }
161
+
162
+ /** The announcement the push path publishes, validated at the door. */
163
+ export function parseAnnounce(body: unknown): Parsed<RefEvent[]> {
164
+ if (typeof body !== 'object' || body === null)
165
+ return { ok: false, error: 'body is not an object' }
166
+ const events = (body as { events?: unknown }).events
167
+ if (!Array.isArray(events)) return { ok: false, error: 'expected an "events" array' }
168
+
169
+ const parsed: RefEvent[] = []
170
+ for (const item of events) {
171
+ if (typeof item !== 'object' || item === null)
172
+ return { ok: false, error: 'event is not an object' }
173
+ const { repo, ref, sha } = item as {
174
+ repo?: unknown
175
+ ref?: unknown
176
+ sha?: unknown
177
+ }
178
+ if (typeof repo !== 'string' || !REPO_ID.test(repo)) {
179
+ return {
180
+ ok: false,
181
+ error: `invalid repository name: ${JSON.stringify(repo ?? null)}`,
182
+ }
183
+ }
184
+ if (typeof ref !== 'string' || !REF_NAME.test(ref)) {
185
+ return {
186
+ ok: false,
187
+ error: `invalid ref: ${JSON.stringify(ref ?? null)}`,
188
+ }
189
+ }
190
+ if (sha !== null && (typeof sha !== 'string' || !/^[0-9a-f]{40,64}$/.test(sha))) {
191
+ return { ok: false, error: `invalid sha for ${ref}` }
192
+ }
193
+ parsed.push({ repo, ref, sha })
194
+ }
195
+ return { ok: true, value: parsed }
196
+ }
197
+
198
+ /**
199
+ * Does this subscription cover this ref?
200
+ *
201
+ * An entry with no `refs` covers the whole repository. That is not a
202
+ * convenience: an agent watching a repository it did not create does not know
203
+ * which branches exist, and enumerating them to subscribe would be the fetch
204
+ * this feature exists to avoid.
205
+ */
206
+ export function watchCovers(
207
+ watch: readonly WatchEntry[],
208
+ event: { repo: string; ref: string },
209
+ ): boolean {
210
+ return watch.some(
211
+ (entry) =>
212
+ entry.repo === event.repo && (entry.refs === undefined || entry.refs.includes(event.ref)),
213
+ )
214
+ }
215
+
216
+ /** Every repository a subscription names — what the handshake has to look up. */
217
+ export function watchedRepos(watch: readonly WatchEntry[]): string[] {
218
+ return [...new Set(watch.map((entry) => entry.repo))]
219
+ }
220
+
221
+ /**
222
+ * The handshake: current state for everything watched.
223
+ *
224
+ * Connect and catch-up are one operation on purpose. A subscriber that had to
225
+ * fetch first to learn where it stands would pay exactly the cost this feature
226
+ * removes, and would have a window between the fetch and the subscription in
227
+ * which a push is missed by both.
228
+ *
229
+ * A watched ref that does not exist is reported as `sha: null` only when the
230
+ * client named it explicitly — the client asked about that ref, so "it is not
231
+ * there" is an answer. A whole-repository watch lists what exists and invents
232
+ * nothing.
233
+ */
234
+ export function handshake(
235
+ watch: readonly WatchEntry[],
236
+ refsByRepo: Readonly<Record<string, Readonly<Record<string, string>>>>,
237
+ ): Handshake {
238
+ const refs: RefEvent[] = []
239
+ for (const entry of watch) {
240
+ const known = refsByRepo[entry.repo] ?? {}
241
+ if (entry.refs === undefined) {
242
+ for (const ref of Object.keys(known).sort())
243
+ refs.push({ repo: entry.repo, ref, sha: known[ref]! })
244
+ continue
245
+ }
246
+ for (const ref of entry.refs) refs.push({ repo: entry.repo, ref, sha: known[ref] ?? null })
247
+ }
248
+ return { ok: true, refs }
249
+ }
250
+
251
+ /**
252
+ * The events a ref transaction publishes.
253
+ *
254
+ * git reports a deletion as an all-zeroes new oid; the wire says `null`,
255
+ * because a client comparing shas should never have to know that the zero oid
256
+ * is not a commit.
257
+ */
258
+ export function eventsFromChanges(
259
+ repo: string,
260
+ // `oldOid` is accepted and ignored: callers hand this whole `RefChange`s
261
+ // straight from the push path, and a parameter narrower than what they hold
262
+ // would make every call site build a second object to drop one field.
263
+ changes: readonly { ref: string; oldOid?: string; newOid: string }[],
264
+ ): RefEvent[] {
265
+ return changes
266
+ .filter((change) => REF_NAME.test(change.ref))
267
+ .map((change) => ({
268
+ repo,
269
+ ref: change.ref,
270
+ sha: change.newOid === ZERO_OID ? null : change.newOid,
271
+ }))
272
+ }
273
+
274
+ /** One message on the wire. Serialized in one place so its shape is one thing. */
275
+ export function encode(message: Handshake | RefEvent | { error: string }): string {
276
+ return JSON.stringify(message)
277
+ }