@zabaca/zbc 0.24.0 → 0.26.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 +45 -0
- package/package.json +3 -2
- package/src/commands/add.ts +78 -5
- package/src/commands/init.ts +105 -10
- package/src/commands/list.ts +6 -3
- package/src/commands/update.ts +75 -6
- package/src/engine/destroy.ts +23 -2
- package/src/engine/resolve.ts +5 -0
- package/src/utils/detect-mode.ts +1 -1
- package/src/utils/host-workspaces.ts +57 -0
- package/src/utils/subtree.ts +78 -9
- package/src/utils/vendor-audit.ts +12 -8
- package/src/utils/vendor-stamp.ts +102 -1
- package/src/utils/vendored-app.ts +356 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/walgit/CONTEXT.md +45 -4
- package/templates/apps/walgit/README.md +394 -11
- package/templates/apps/walgit/shared/browse.ts +61 -0
- package/templates/apps/walgit/shared/capabilities.ts +26 -0
- package/templates/apps/walgit/shared/container-env.ts +12 -0
- package/templates/apps/walgit/shared/container-lifecycle.ts +1 -1
- package/templates/apps/walgit/shared/credentials.ts +4 -0
- package/templates/apps/walgit/shared/events.ts +35 -1
- package/templates/apps/walgit/shared/instructions.ts +4 -1
- package/templates/apps/walgit/shared/landing.ts +2 -1
- package/templates/apps/walgit/shared/llms.ts +73 -5
- package/templates/apps/walgit/shared/mcp.ts +19 -3
- package/templates/apps/walgit/shared/protocol.ts +40 -3
- package/templates/apps/walgit/shared/repo-list.ts +46 -6
- package/templates/apps/walgit/shared/scoped-token.ts +500 -0
- package/templates/apps/walgit/shared/token-revocation.ts +134 -0
- package/templates/apps/walgit/shared/wal-index.ts +19 -0
- package/templates/apps/walgit/shared/webhook.ts +281 -0
- package/templates/apps/walgit/src/admin.ts +510 -0
- package/templates/apps/walgit/src/announce-main.ts +17 -9
- package/templates/apps/walgit/src/announce.ts +86 -0
- package/templates/apps/walgit/src/browse.ts +255 -2
- package/templates/apps/walgit/src/cli.ts +140 -0
- package/templates/apps/walgit/src/expire.ts +5 -0
- package/templates/apps/walgit/src/git.ts +103 -0
- package/templates/apps/walgit/src/hook-main.ts +6 -3
- package/templates/apps/walgit/src/http.ts +563 -3
- package/templates/apps/walgit/src/limits.ts +3 -0
- package/templates/apps/walgit/src/merge.ts +525 -0
- package/templates/apps/walgit/src/rate-limit.ts +51 -14
- package/templates/apps/walgit/src/server.ts +65 -1
- package/templates/apps/walgit/worker/events-do.ts +218 -3
- package/templates/apps/walgit/worker/index.ts +134 -9
- package/templates/apps/walgit/worker/mcp.ts +18 -4
- 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,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The push webhook: one signed HTTP POST per moved ref, to the one URL the
|
|
3
|
+
* operator configured.
|
|
4
|
+
*
|
|
5
|
+
* It exists for a receiver that cannot hold the ref-event WebSocket open — a
|
|
6
|
+
* serverless function is the case it was asked for — and it is the same
|
|
7
|
+
* announcement, not a second source of truth. It is produced where the stream's
|
|
8
|
+
* announcement is (`publish` in `src/announce.ts`, run by the detached
|
|
9
|
+
* announcer `post-receive` spawns), from the same ref changes, so anything that
|
|
10
|
+
* moves a ref through that path fires both and there is no way to move one
|
|
11
|
+
* without the other.
|
|
12
|
+
*
|
|
13
|
+
* Unlike the stream, this side owes a delivery: a receiver behind a URL has no
|
|
14
|
+
* handshake to catch up through. So it is AT LEAST ONCE, with a bounded retry,
|
|
15
|
+
* and every event carries a stable `id` — generated once, sent identically on
|
|
16
|
+
* every attempt — for the receiver to de-duplicate on. It is still not a log
|
|
17
|
+
* (docs/adr/0009): an event whose attempts all fail is dropped — its line goes
|
|
18
|
+
* to the announcer's stderr, which `post-receive` detaches from the client — and
|
|
19
|
+
* nothing replays it; `index.json` holds the refs regardless.
|
|
20
|
+
*
|
|
21
|
+
* Signing is HMAC-SHA256 over `<timestamp>.<body>` with the shared secret. The
|
|
22
|
+
* timestamp is signed so a receiver can refuse a replay of a captured request
|
|
23
|
+
* by its age, and it is re-stamped on each attempt while the body stays the
|
|
24
|
+
* same, so a late retry is still fresh and still the same event.
|
|
25
|
+
*
|
|
26
|
+
* Runtime-neutral (WebCrypto and `fetch` only) so the Worker could deliver the
|
|
27
|
+
* same thing if a ref ever moves from the edge; the clock, the sleep and the
|
|
28
|
+
* fetch are all injected so the retry policy is tested without waiting on it.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { eventsFromChanges } from './events'
|
|
32
|
+
import { ZERO_OID } from './protocol'
|
|
33
|
+
import { ulid } from './ulid'
|
|
34
|
+
|
|
35
|
+
export interface WebhookConfig {
|
|
36
|
+
/** Where every event is POSTed (`WALGIT_WEBHOOK_URL`). */
|
|
37
|
+
url: string
|
|
38
|
+
/** The HMAC key (`WALGIT_WEBHOOK_SECRET`). Never logged, never sent. */
|
|
39
|
+
secret: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What one POST carries. Field names are the ref-event stream's, plus `oldSha`. */
|
|
43
|
+
export interface WebhookEvent {
|
|
44
|
+
/** Stable across retries — de-duplicate on it. A ULID, so it also sorts by time. */
|
|
45
|
+
id: string
|
|
46
|
+
repo: string
|
|
47
|
+
ref: string
|
|
48
|
+
/** Where the ref was before this push. `null` when the push created it. */
|
|
49
|
+
oldSha: string | null
|
|
50
|
+
/** Where it is now. `null` when the push deleted it — as on the stream. */
|
|
51
|
+
sha: string | null
|
|
52
|
+
/** The Proposals this move merged (docs/adr/0018); absent when none, as on the stream. */
|
|
53
|
+
merged?: string[]
|
|
54
|
+
/** When the ref moved, ISO 8601. */
|
|
55
|
+
timestamp: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const SIGNATURE_HEADER = 'X-Walgit-Signature'
|
|
59
|
+
export const TIMESTAMP_HEADER = 'X-Walgit-Timestamp'
|
|
60
|
+
export const EVENT_ID_HEADER = 'X-Walgit-Event'
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How old a signed timestamp a receiver should accept, in seconds — the
|
|
64
|
+
* default `verifyWebhook` applies. Generous enough for clock skew and one
|
|
65
|
+
* slow attempt; every retry is re-stamped, so backoff never ages a request.
|
|
66
|
+
*/
|
|
67
|
+
export const DEFAULT_TOLERANCE_SECONDS = 300
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The retry policy. Five attempts, backing off 1, 2, 4 and 8 seconds, each
|
|
71
|
+
* attempt bounded: an unreachable receiver costs the detached announcer well
|
|
72
|
+
* under a minute and the push nothing at all.
|
|
73
|
+
*/
|
|
74
|
+
export const WEBHOOK_ATTEMPTS = 5
|
|
75
|
+
export const WEBHOOK_BACKOFF_MS = 1_000
|
|
76
|
+
export const WEBHOOK_TIMEOUT_MS = 5_000
|
|
77
|
+
/**
|
|
78
|
+
* At most this many events of one push in flight at once. A mirror push moves
|
|
79
|
+
* hundreds of refs, and a receiver should not meet all of them at once.
|
|
80
|
+
*/
|
|
81
|
+
export const WEBHOOK_CONCURRENCY = 4
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The webhook's configuration, or null when this deployment has none.
|
|
85
|
+
*
|
|
86
|
+
* Both halves, as with the stream: an unsigned webhook is one a receiver cannot
|
|
87
|
+
* tell from a stranger's POST, so a URL without a secret is not a webhook.
|
|
88
|
+
*/
|
|
89
|
+
export function webhookConfigFromEnv(
|
|
90
|
+
env: Record<string, string | undefined>,
|
|
91
|
+
): WebhookConfig | null {
|
|
92
|
+
const url = (env.WALGIT_WEBHOOK_URL ?? '').trim()
|
|
93
|
+
const secret = env.WALGIT_WEBHOOK_SECRET ?? ''
|
|
94
|
+
// Blank reads as unset, but a real secret is the HMAC key VERBATIM: the
|
|
95
|
+
// receiver holds its own copy, and one trimmed here would never verify there.
|
|
96
|
+
if (!url || !secret.trim()) return null
|
|
97
|
+
return { url, secret }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The events one push's ref changes become. The same refs the stream
|
|
102
|
+
* announces (`eventsFromChanges`), each given its previous sha and an id.
|
|
103
|
+
*/
|
|
104
|
+
export function webhookEventsFromChanges(
|
|
105
|
+
repo: string,
|
|
106
|
+
changes: readonly { ref: string; oldOid: string; newOid: string }[],
|
|
107
|
+
merged: Readonly<Record<string, string[]>> = {},
|
|
108
|
+
now: number = Date.now(),
|
|
109
|
+
): WebhookEvent[] {
|
|
110
|
+
const before = new Map(changes.map((change) => [change.ref, change.oldOid]))
|
|
111
|
+
const timestamp = new Date(now).toISOString()
|
|
112
|
+
return eventsFromChanges(repo, changes, merged).map((event) => {
|
|
113
|
+
const old = before.get(event.ref)
|
|
114
|
+
return {
|
|
115
|
+
id: ulid(now),
|
|
116
|
+
repo: event.repo,
|
|
117
|
+
ref: event.ref,
|
|
118
|
+
oldSha: !old || old === ZERO_OID ? null : old,
|
|
119
|
+
sha: event.sha,
|
|
120
|
+
...(event.merged ? { merged: event.merged } : {}),
|
|
121
|
+
timestamp,
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const encoder = new TextEncoder()
|
|
127
|
+
|
|
128
|
+
async function hmacHex(secret: string, message: string): Promise<string> {
|
|
129
|
+
const key = await crypto.subtle.importKey(
|
|
130
|
+
'raw',
|
|
131
|
+
encoder.encode(secret),
|
|
132
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
133
|
+
false,
|
|
134
|
+
['sign'],
|
|
135
|
+
)
|
|
136
|
+
const mac = new Uint8Array(await crypto.subtle.sign('HMAC', key, encoder.encode(message)))
|
|
137
|
+
let hex = ''
|
|
138
|
+
for (const byte of mac) hex += byte.toString(16).padStart(2, '0')
|
|
139
|
+
return hex
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** `sha256=<hex>` over `<timestamp>.<body>`, the value of `X-Walgit-Signature`. */
|
|
143
|
+
export async function signWebhook(
|
|
144
|
+
secret: string,
|
|
145
|
+
timestamp: string,
|
|
146
|
+
body: string,
|
|
147
|
+
): Promise<string> {
|
|
148
|
+
return `sha256=${await hmacHex(secret, `${timestamp}.${body}`)}`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Compared in full whatever the first difference, so the time taken says nothing. */
|
|
152
|
+
function constantTimeEqual(a: string, b: string): boolean {
|
|
153
|
+
if (a.length !== b.length) return false
|
|
154
|
+
let diff = 0
|
|
155
|
+
for (let i = 0; i < a.length; i += 1) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
|
156
|
+
return diff === 0
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The receiver's check: is this request walgit's, and recent?
|
|
161
|
+
*
|
|
162
|
+
* `body` must be the RAW request body, byte for byte — a body parsed and
|
|
163
|
+
* re-serialized is a different string and will not verify. False on any
|
|
164
|
+
* missing header, a timestamp outside `toleranceSeconds` of `now`, or a
|
|
165
|
+
* signature that does not match.
|
|
166
|
+
*/
|
|
167
|
+
export async function verifyWebhook(input: {
|
|
168
|
+
secret: string
|
|
169
|
+
body: string
|
|
170
|
+
signature: string | null
|
|
171
|
+
timestamp: string | null
|
|
172
|
+
now?: number
|
|
173
|
+
toleranceSeconds?: number
|
|
174
|
+
}): Promise<boolean> {
|
|
175
|
+
const { secret, body, signature, timestamp } = input
|
|
176
|
+
if (!signature || !timestamp || !/^\d+$/.test(timestamp)) return false
|
|
177
|
+
const now = Math.floor((input.now ?? Date.now()) / 1000)
|
|
178
|
+
const tolerance = input.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS
|
|
179
|
+
if (Math.abs(now - Number(timestamp)) > tolerance) return false
|
|
180
|
+
return constantTimeEqual(signature, await signWebhook(secret, timestamp, body))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Worth trying again: the receiver is down or overloaded, not refusing. */
|
|
184
|
+
function retryable(status: number): boolean {
|
|
185
|
+
return status >= 500 || status === 429 || status === 408
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface DeliverOptions {
|
|
189
|
+
fetchImpl?: typeof fetch
|
|
190
|
+
sleep?: (ms: number) => Promise<void>
|
|
191
|
+
now?: () => number
|
|
192
|
+
attempts?: number
|
|
193
|
+
backoffMs?: number
|
|
194
|
+
timeoutMs?: number
|
|
195
|
+
log?: (line: string) => void
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Deliver every event of one push, at most `WEBHOOK_CONCURRENCY` at a time.
|
|
200
|
+
* Never throws; resolves once every event is delivered or given up on.
|
|
201
|
+
*/
|
|
202
|
+
export async function deliverWebhooks(
|
|
203
|
+
config: WebhookConfig,
|
|
204
|
+
events: readonly WebhookEvent[],
|
|
205
|
+
options: DeliverOptions = {},
|
|
206
|
+
concurrency: number = WEBHOOK_CONCURRENCY,
|
|
207
|
+
): Promise<void> {
|
|
208
|
+
let next = 0
|
|
209
|
+
const lane = async () => {
|
|
210
|
+
while (next < events.length) {
|
|
211
|
+
const event = events[next]!
|
|
212
|
+
next += 1
|
|
213
|
+
await deliverWebhook(config, event, options)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, events.length) }, lane))
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* POST one event until a 2xx, a refusal that is not worth repeating, or the
|
|
221
|
+
* attempts run out. Never throws; true when the receiver accepted it.
|
|
222
|
+
*
|
|
223
|
+
* What is logged names the event and the status, never the secret, the
|
|
224
|
+
* signature or the URL's query (which is where a receiver's own token tends
|
|
225
|
+
* to live).
|
|
226
|
+
*/
|
|
227
|
+
export async function deliverWebhook(
|
|
228
|
+
config: WebhookConfig,
|
|
229
|
+
event: WebhookEvent,
|
|
230
|
+
options: DeliverOptions = {},
|
|
231
|
+
): Promise<boolean> {
|
|
232
|
+
const fetchImpl = options.fetchImpl ?? fetch
|
|
233
|
+
const sleep = options.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)))
|
|
234
|
+
const now = options.now ?? Date.now
|
|
235
|
+
const attempts = options.attempts ?? WEBHOOK_ATTEMPTS
|
|
236
|
+
const backoffMs = options.backoffMs ?? WEBHOOK_BACKOFF_MS
|
|
237
|
+
const timeoutMs = options.timeoutMs ?? WEBHOOK_TIMEOUT_MS
|
|
238
|
+
const log = options.log ?? ((line: string) => console.error(line))
|
|
239
|
+
const body = JSON.stringify(event)
|
|
240
|
+
|
|
241
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
242
|
+
let outcome: string
|
|
243
|
+
try {
|
|
244
|
+
const timestamp = String(Math.floor(now() / 1000))
|
|
245
|
+
const response = await fetchImpl(config.url, {
|
|
246
|
+
method: 'POST',
|
|
247
|
+
headers: {
|
|
248
|
+
'content-type': 'application/json',
|
|
249
|
+
'user-agent': 'walgit-webhook',
|
|
250
|
+
[EVENT_ID_HEADER]: event.id,
|
|
251
|
+
[TIMESTAMP_HEADER]: timestamp,
|
|
252
|
+
[SIGNATURE_HEADER]: await signWebhook(config.secret, timestamp, body),
|
|
253
|
+
},
|
|
254
|
+
body,
|
|
255
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
256
|
+
// Never followed: a 307 would re-send the signed body to whatever host
|
|
257
|
+
// the receiver names, and a 301 would turn it into a GET that "succeeds".
|
|
258
|
+
redirect: 'manual',
|
|
259
|
+
})
|
|
260
|
+
// Drained so a kept-alive connection is free for the next attempt.
|
|
261
|
+
await response.body?.cancel().catch(() => {})
|
|
262
|
+
if (response.ok) return true
|
|
263
|
+
outcome = `refused (${response.status})`
|
|
264
|
+
if (!retryable(response.status)) {
|
|
265
|
+
log(`walgit: webhook ${event.id} ${outcome}; not retried`)
|
|
266
|
+
return false
|
|
267
|
+
}
|
|
268
|
+
} catch (err) {
|
|
269
|
+
outcome = `failed: ${(err as Error).name}`
|
|
270
|
+
}
|
|
271
|
+
if (attempt === attempts) {
|
|
272
|
+
log(`walgit: webhook ${event.id} ${outcome}; giving up after ${attempts} attempts`)
|
|
273
|
+
return false
|
|
274
|
+
}
|
|
275
|
+
// Jittered, so the events of one large push that all met the same outage
|
|
276
|
+
// do not all come back at the same instant.
|
|
277
|
+
const base = backoffMs * 2 ** (attempt - 1)
|
|
278
|
+
await sleep(Math.round(base * (0.75 + Math.random() * 0.5)))
|
|
279
|
+
}
|
|
280
|
+
return false
|
|
281
|
+
}
|