@zabaca/zbc 0.22.0 → 0.23.1
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/package.json +1 -1
- package/templates/CLAUDE.md +5 -0
- package/templates/apps/walgit/CONTEXT.md +5 -1
- package/templates/apps/walgit/README.md +164 -27
- package/templates/apps/walgit/package.json +2 -2
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +18 -4
- package/templates/apps/walgit/shared/capabilities.ts +28 -3
- package/templates/apps/walgit/shared/container-env.ts +41 -2
- package/templates/apps/walgit/shared/container-lifecycle.ts +239 -0
- package/templates/apps/walgit/shared/container-snapshot.ts +219 -0
- package/templates/apps/walgit/shared/edge-refs.ts +612 -0
- package/templates/apps/walgit/{src → shared}/instructions.ts +8 -4
- package/templates/apps/walgit/shared/keys.ts +17 -0
- package/templates/apps/walgit/shared/landing.ts +10 -3
- package/templates/apps/walgit/shared/llms.ts +24 -7
- package/templates/apps/walgit/shared/policy.ts +1 -1
- package/templates/apps/walgit/shared/protocol.ts +35 -0
- package/templates/apps/walgit/shared/sleep-after.ts +44 -0
- package/templates/apps/walgit/shared/wal-index.ts +22 -1
- package/templates/apps/walgit/src/boot-residue.ts +58 -0
- package/templates/apps/walgit/src/cache.ts +9 -0
- package/templates/apps/walgit/src/cli.ts +39 -4
- package/templates/apps/walgit/src/delete-repo.ts +58 -9
- package/templates/apps/walgit/src/edge-refs.ts +148 -0
- package/templates/apps/walgit/src/expire.ts +75 -7
- package/templates/apps/walgit/src/hook-main.ts +2 -0
- package/templates/apps/walgit/src/http.ts +17 -4
- package/templates/apps/walgit/src/materialize.ts +34 -15
- package/templates/apps/walgit/src/private.ts +6 -2
- package/templates/apps/walgit/src/push.ts +34 -8
- package/templates/apps/walgit/src/reconcile.ts +6 -1
- package/templates/apps/walgit/src/server.ts +69 -8
- package/templates/apps/walgit/src/sync.ts +10 -2
- package/templates/apps/walgit/worker/container-binding.ts +104 -0
- package/templates/apps/walgit/worker/durable-container.ts +786 -0
- package/templates/apps/walgit/worker/events-do.ts +16 -12
- package/templates/apps/walgit/worker/index.ts +235 -17
- package/templates/apps/walgit/worker/mcp.ts +9 -4
- package/templates/apps/walgit/wrangler.jsonc +44 -33
- package/templates/claude/settings.json +8 -0
- package/templates/infra/modules/cloudflare/index.ts +9 -0
- package/templates/infra/modules/d1/index.ts +30 -4
- package/templates/infra/modules/d1/registry.json +1 -1
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Durable Object that owns walgit's container, driving `ctx.container`
|
|
3
|
+
* directly under the `durable_object` scheduling policy.
|
|
4
|
+
*
|
|
5
|
+
* It replaces `WalgitContainer` (worker/index.ts), which extends
|
|
6
|
+
* `@cloudflare/containers`' `Container` class. That class cannot run under this
|
|
7
|
+
* policy and is maintained only through 2026-12-31, and the policy is worth
|
|
8
|
+
* moving for on its own: a container starts in well under a second rather than
|
|
9
|
+
* four, and its root filesystem can be snapshotted — so a wake no longer has to
|
|
10
|
+
* rebuild every repository it serves from the log onto an empty disk. On
|
|
11
|
+
* agentgit a cold start cost 12–22 s, and most of it was that rebuild.
|
|
12
|
+
*
|
|
13
|
+
* A NEW CLASS, not the old one with a new base. A container application's
|
|
14
|
+
* scheduling policy is fixed when it is created, one Durable Object namespace
|
|
15
|
+
* attaches to one application, and changing `scheduling_policy` in place fails
|
|
16
|
+
* the deploy AFTER the new Worker is live. So this is a new namespace with its
|
|
17
|
+
* own application, bound as `WALGIT_DURABLE_CONTAINER` and routed to only once
|
|
18
|
+
* `WALGIT_CONTAINER_POLICY` says so (worker/container-binding.ts says why that
|
|
19
|
+
* takes two deploys). The old class stays exported until its application is
|
|
20
|
+
* deleted (README.md, "Deployment", phase 3).
|
|
21
|
+
*
|
|
22
|
+
* Everything the library used to decide is decided here, and each rule is in
|
|
23
|
+
* shared/ where it can be tested without a runtime:
|
|
24
|
+
*
|
|
25
|
+
* - START: from the last snapshot when it is for this image and this log,
|
|
26
|
+
* from the image otherwise (shared/container-snapshot.ts), with the
|
|
27
|
+
* forwarded environment (shared/container-env.ts) at `CONTAINER_INSTANCE`.
|
|
28
|
+
* `start()` returns before the process listens, so readiness is asked of
|
|
29
|
+
* the port. A restore that does not come up falls back to a fresh start.
|
|
30
|
+
* - REPLACE: the environment AND the image a container was booted from are
|
|
31
|
+
* fingerprinted, and a running container is destroyed the first time this
|
|
32
|
+
* deploy would boot it differently. The policy has no rollouts, so this is
|
|
33
|
+
* now the whole of how a new image reaches a running container.
|
|
34
|
+
* - PROXY: HTTP and WebSocket to port 8080, a bodiless read retried once when
|
|
35
|
+
* it lands on a container that just exited, and the first response after a
|
|
36
|
+
* real start stamped cold for the Worker's telemetry.
|
|
37
|
+
* - STOP: an alarm `WALGIT_SLEEP_AFTER` after the last request FINISHED —
|
|
38
|
+
* never while one is in flight — that snapshots the disk, SIGTERMs the
|
|
39
|
+
* process (src/server.ts drains and exits) and kills it if it lingers.
|
|
40
|
+
*
|
|
41
|
+
* What it no longer does is keep itself awake. The library's alarm loop
|
|
42
|
+
* re-armed every few minutes for as long as the container ran, polling it; this
|
|
43
|
+
* object sleeps between requests, wakes once to stop the container, and with
|
|
44
|
+
* the container stopped holds no alarm, no timer and no pending promise.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { DurableObject } from 'cloudflare:workers'
|
|
48
|
+
|
|
49
|
+
import {
|
|
50
|
+
bootFingerprint,
|
|
51
|
+
containerEnv,
|
|
52
|
+
reconcileContainerEnv,
|
|
53
|
+
type ContainerEnvName,
|
|
54
|
+
} from '../shared/container-env'
|
|
55
|
+
import {
|
|
56
|
+
CONTAINER_INSTANCE,
|
|
57
|
+
CONTAINER_PORT,
|
|
58
|
+
READY_POLL_MS,
|
|
59
|
+
READY_PROBE_TIMEOUT_MS,
|
|
60
|
+
INFLIGHT_MAX_MS,
|
|
61
|
+
READY_TIMEOUT_MS,
|
|
62
|
+
RETRY_DELAY_MS,
|
|
63
|
+
STOP_GRACE_MS,
|
|
64
|
+
freshStartTimeoutMs,
|
|
65
|
+
idleVerdict,
|
|
66
|
+
inactivityTimeoutMs,
|
|
67
|
+
mayRetry,
|
|
68
|
+
rearmAt,
|
|
69
|
+
} from '../shared/container-lifecycle'
|
|
70
|
+
import {
|
|
71
|
+
type SnapshotRecord,
|
|
72
|
+
recordSnapshot,
|
|
73
|
+
refreshSnapshot,
|
|
74
|
+
restorePlan,
|
|
75
|
+
shouldSnapshot,
|
|
76
|
+
needsNewSnapshot,
|
|
77
|
+
DISK_CHANGED_COMMAND,
|
|
78
|
+
snapshotsEnabled,
|
|
79
|
+
storeIdentity,
|
|
80
|
+
} from '../shared/container-snapshot'
|
|
81
|
+
import { COLD_HEADER, HEALTH_PATH } from '../shared/protocol'
|
|
82
|
+
import { sleepAfterMsFrom } from '../shared/sleep-after'
|
|
83
|
+
|
|
84
|
+
/** Only what this object reads — the Worker's `Env` is a superset. */
|
|
85
|
+
export interface ContainerHostEnv extends Partial<Record<ContainerEnvName, string>> {
|
|
86
|
+
/** How long the container idles before it stops (`shared/sleep-after.ts`). */
|
|
87
|
+
WALGIT_SLEEP_AFTER?: string
|
|
88
|
+
/** `1` to snapshot on the idle stop and restore on the next start. Edge-only. */
|
|
89
|
+
WALGIT_SNAPSHOTS?: string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The key under `containers[].images` in wrangler.jsonc. Wrangler builds the
|
|
94
|
+
* Dockerfile it names and hands this object the digest-pinned reference as
|
|
95
|
+
* `ctx.container.images.walgit` — which changes exactly when the image does.
|
|
96
|
+
*/
|
|
97
|
+
export const IMAGE_NAME = 'walgit'
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The one object every request is routed to. `@cloudflare/containers`'
|
|
101
|
+
* `getContainer` default, kept: one container serves every repository, for
|
|
102
|
+
* cache locality rather than correctness (worker/index.ts says why), and this
|
|
103
|
+
* is the name that makes it one.
|
|
104
|
+
*/
|
|
105
|
+
export const CONTAINER_OBJECT_NAME = 'cf-singleton-container'
|
|
106
|
+
|
|
107
|
+
/** The stub for that one object. What every caller of the binding uses. */
|
|
108
|
+
export function containerHost(
|
|
109
|
+
namespace: DurableObjectNamespace<WalgitDurableContainer>,
|
|
110
|
+
): DurableObjectStub<WalgitDurableContainer> {
|
|
111
|
+
return namespace.get(namespace.idFromName(CONTAINER_OBJECT_NAME))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Where `reconcileEnv` remembers what the container was booted from. */
|
|
115
|
+
const ENV_FINGERPRINT_KEY = 'container-env-fingerprint'
|
|
116
|
+
/** The one snapshot this object knows about (`SnapshotRecord`). */
|
|
117
|
+
const SNAPSHOT_KEY = 'container-snapshot'
|
|
118
|
+
/**
|
|
119
|
+
* The image the running container booted from. In storage rather than memory
|
|
120
|
+
* because the object sleeps while its container runs, and the stop that reads
|
|
121
|
+
* this is an alarm that wakes a fresh instance. `inspect()` cannot answer it:
|
|
122
|
+
* it reports an empty image for a container restored from a snapshot.
|
|
123
|
+
*/
|
|
124
|
+
const BOOTED_IMAGE_KEY = 'container-booted-image'
|
|
125
|
+
/** How the running container started (`StartSource`), for `needsNewSnapshot`. */
|
|
126
|
+
const BOOTED_FROM_KEY = 'container-booted-from'
|
|
127
|
+
|
|
128
|
+
const SIGTERM = 15
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The detached compaction a push can leave running (src/hook-main.ts spawns
|
|
132
|
+
* it), as `pgrep -f` finds it inside the container.
|
|
133
|
+
*/
|
|
134
|
+
const COMPACTION_PROCESS = 'compact-main'
|
|
135
|
+
|
|
136
|
+
/** How the running container came to be, for the cold stamp and the logs. */
|
|
137
|
+
type StartSource = 'snapshot' | 'image'
|
|
138
|
+
|
|
139
|
+
export class WalgitDurableContainer extends DurableObject<ContainerHostEnv> {
|
|
140
|
+
/**
|
|
141
|
+
* The forwarded environment, read once per object: a deploy constructs a new
|
|
142
|
+
* one, so this is never staler than the Worker in front of it.
|
|
143
|
+
*/
|
|
144
|
+
private readonly envVars = containerEnv(this.env)
|
|
145
|
+
private readonly sleepAfterMs = sleepAfterMsFrom(this.env)
|
|
146
|
+
private readonly snapshots = snapshotsEnabled(this.env)
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Did THIS object start the container, and from what? Read and cleared by
|
|
150
|
+
* the next response, which is stamped cold. Null until a start: an object
|
|
151
|
+
* reconstructed in front of a container that was already running started
|
|
152
|
+
* nothing, and stamping its first response cold once counted ~10 phantom
|
|
153
|
+
* cold starts a day.
|
|
154
|
+
*/
|
|
155
|
+
private freshStart: StartSource | null = null
|
|
156
|
+
|
|
157
|
+
/** Single-flight, so concurrent requests await one check rather than racing. */
|
|
158
|
+
private reconciled: Promise<void> | null = null
|
|
159
|
+
/** Single-flight, so concurrent requests never call `start()` twice. */
|
|
160
|
+
private starting: Promise<void> | null = null
|
|
161
|
+
/** A stop under way; a request that arrives during one waits it out. */
|
|
162
|
+
private stopping: Promise<void> | null = null
|
|
163
|
+
|
|
164
|
+
/** Requests between arriving here and their last byte leaving. */
|
|
165
|
+
private inflight = 0
|
|
166
|
+
/** When the last of them finished, if this object has seen one. */
|
|
167
|
+
private lastActivity: number | null = null
|
|
168
|
+
/** What this object last armed the alarm for, if it knows. */
|
|
169
|
+
private alarmAt: number | null = null
|
|
170
|
+
/**
|
|
171
|
+
* The running container's exit, from ONE `monitor()` per start. A pending
|
|
172
|
+
* `monitor()` keeps this object from being evicted for up to 15 minutes, so
|
|
173
|
+
* it is asked for only when an exit is actually being waited on — a start
|
|
174
|
+
* that stopped before it was ready, or a stop — and then shared, rather than
|
|
175
|
+
* asked again on every readiness probe and left pending each time.
|
|
176
|
+
*/
|
|
177
|
+
private exit: Promise<string> | null = null
|
|
178
|
+
|
|
179
|
+
constructor(ctx: DurableObjectState, env: ContainerHostEnv) {
|
|
180
|
+
super(ctx, env)
|
|
181
|
+
// The platform's inactivity timeout belongs to an object INSTANCE, not to
|
|
182
|
+
// the container: an instance constructed in front of a running container
|
|
183
|
+
// (a deploy, an eviction) starts without one, and without one Cloudflare
|
|
184
|
+
// stops the container shortly after this object next goes idle — before
|
|
185
|
+
// the alarm can snapshot it. So it is set again here, as the docs require.
|
|
186
|
+
const container = ctx.container
|
|
187
|
+
if (container?.running) {
|
|
188
|
+
void ctx.blockConcurrencyWhile(() =>
|
|
189
|
+
container
|
|
190
|
+
.setInactivityTimeout(inactivityTimeoutMs(this.sleepAfterMs))
|
|
191
|
+
.catch((error: unknown) =>
|
|
192
|
+
console.error(`walgit container: inactivity timeout not set: ${messageOf(error)}`),
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async fetch(request: Request): Promise<Response> {
|
|
199
|
+
// Before anything is proxied, so a request never reaches a container whose
|
|
200
|
+
// environment or image this deploy already superseded. Memoized: the
|
|
201
|
+
// storage read happens once per object lifetime, not per request.
|
|
202
|
+
//
|
|
203
|
+
// Started only with NOTHING in flight. The first one always is — every
|
|
204
|
+
// request awaits it before it is counted — but a reconcile that failed is
|
|
205
|
+
// cleared to be retried, and by then other requests may be streaming a
|
|
206
|
+
// clone or uploading a push from the very container a retry would destroy.
|
|
207
|
+
// So the retry waits for a request that arrives to an idle object; until
|
|
208
|
+
// then requests are served by the container already running, which is the
|
|
209
|
+
// state the failed reconcile left anyway.
|
|
210
|
+
if (!this.reconciled && this.inflight === 0) {
|
|
211
|
+
this.reconciled = this.reconcileEnv().catch((error) => {
|
|
212
|
+
// Cleared so a later request retries. Never rethrown: a container
|
|
213
|
+
// serving a stale limit is a worse day than an outage only if it is
|
|
214
|
+
// ALSO the reason git stopped working, and it should not be.
|
|
215
|
+
this.reconciled = null
|
|
216
|
+
console.error(`walgit container env reconcile failed: ${messageOf(error)}`)
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
if (this.reconciled) await this.reconciled
|
|
220
|
+
|
|
221
|
+
// Counted from here, before the start, so an alarm that fires while this
|
|
222
|
+
// request waits for a container sees it and does not stop that container.
|
|
223
|
+
// Released when the response is over — or after `INFLIGHT_MAX_MS`, so a
|
|
224
|
+
// caller that never reads the body cannot pin the container up for good.
|
|
225
|
+
this.inflight += 1
|
|
226
|
+
let settled = false
|
|
227
|
+
const settle = () => {
|
|
228
|
+
if (settled) return
|
|
229
|
+
settled = true
|
|
230
|
+
clearTimeout(cap)
|
|
231
|
+
this.finished()
|
|
232
|
+
}
|
|
233
|
+
const cap = setTimeout(() => {
|
|
234
|
+
if (settled) return
|
|
235
|
+
const path = new URL(request.url).pathname
|
|
236
|
+
console.warn(`walgit container: ${path} open ${INFLIGHT_MAX_MS} ms; no longer in flight`)
|
|
237
|
+
settle()
|
|
238
|
+
}, INFLIGHT_MAX_MS)
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await this.ensureRunning()
|
|
242
|
+
} catch (error) {
|
|
243
|
+
settle()
|
|
244
|
+
// No `SERVED_HEADER`, so the Worker counts it as the edge refusal it is.
|
|
245
|
+
return new Response(`Failed to start container: ${messageOf(error)}`, { status: 500 })
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let response: Response
|
|
249
|
+
try {
|
|
250
|
+
response = await this.forward(request)
|
|
251
|
+
} catch (error) {
|
|
252
|
+
settle()
|
|
253
|
+
console.error(`walgit container: proxy failed: ${messageOf(error)}`)
|
|
254
|
+
return new Response(`Error proxying request to container: ${messageOf(error)}`, {
|
|
255
|
+
status: 500,
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (response.webSocket) response = this.proxySocket(response, settle)
|
|
260
|
+
else if (response.body) response = this.trackBody(response, settle)
|
|
261
|
+
else settle()
|
|
262
|
+
|
|
263
|
+
// Read AFTER the forward: the start happens inside it, so a flag read
|
|
264
|
+
// before was still null for the request that woke the container and
|
|
265
|
+
// stamped the next one cold instead.
|
|
266
|
+
const cold = this.freshStart
|
|
267
|
+
this.freshStart = null
|
|
268
|
+
if (!cold) return response
|
|
269
|
+
// Rebuilt rather than mutated (Response headers are immutable), passing the
|
|
270
|
+
// body by reference so a clone is not buffered to add one header. The value
|
|
271
|
+
// says which kind of cold; the Worker reads only its presence.
|
|
272
|
+
const stamped = new Response(response.body, response)
|
|
273
|
+
stamped.headers.set(COLD_HEADER, cold)
|
|
274
|
+
return stamped
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The idle stop, and nothing else: this alarm is armed only while a
|
|
279
|
+
* container runs, and a fire that finds none returns without re-arming —
|
|
280
|
+
* which is what lets the object sleep with its container stopped.
|
|
281
|
+
*/
|
|
282
|
+
async alarm(): Promise<void> {
|
|
283
|
+
this.alarmAt = null
|
|
284
|
+
if (!this.ctx.container?.running) return
|
|
285
|
+
const verdict = idleVerdict({
|
|
286
|
+
now: Date.now(),
|
|
287
|
+
inflight: this.inflight,
|
|
288
|
+
lastActivity: this.lastActivity,
|
|
289
|
+
sleepAfterMs: this.sleepAfterMs,
|
|
290
|
+
})
|
|
291
|
+
if (!verdict.stop) {
|
|
292
|
+
await this.arm(verdict.at)
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
await this.stopContainer()
|
|
297
|
+
} catch (error) {
|
|
298
|
+
// Not rethrown: a rethrown alarm is retried on the platform's backoff,
|
|
299
|
+
// which is the wrong schedule for "try the stop again". Re-armed for a
|
|
300
|
+
// minute instead, and the inactivity timeout is the backstop under both.
|
|
301
|
+
console.error(`walgit container: idle stop failed: ${messageOf(error)}`)
|
|
302
|
+
if (this.ctx.container?.running) await this.arm(Date.now() + 60_000)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private get container(): Container {
|
|
307
|
+
const container = this.ctx.container
|
|
308
|
+
if (!container) throw new Error('no container is configured for this Durable Object')
|
|
309
|
+
return container
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The image this Worker version would start, digest-pinned. */
|
|
313
|
+
private image(): string {
|
|
314
|
+
const image = this.container.images[IMAGE_NAME]
|
|
315
|
+
if (!image) {
|
|
316
|
+
throw new Error(
|
|
317
|
+
`no container image named "${IMAGE_NAME}" (wrangler.jsonc containers[].images)`,
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
return image
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Bind this object to `reconcileContainerEnv`'s rules.
|
|
325
|
+
*
|
|
326
|
+
* The decision — and the two orderings that matter, no-record-is-a-mismatch
|
|
327
|
+
* and record-only-after-a-successful-destroy — lives in
|
|
328
|
+
* `shared/container-env.ts`, where it can be tested without a Workers
|
|
329
|
+
* runtime. What is left here is reaching the container, reaching storage,
|
|
330
|
+
* and logging.
|
|
331
|
+
*/
|
|
332
|
+
private async reconcileEnv(): Promise<void> {
|
|
333
|
+
const current = bootFingerprint(this.envVars, this.image())
|
|
334
|
+
let booted: string | undefined
|
|
335
|
+
// Captured so the port's getter below reads this object's state rather
|
|
336
|
+
// than the object literal's.
|
|
337
|
+
const state = this.ctx
|
|
338
|
+
|
|
339
|
+
const outcome = await reconcileContainerEnv(
|
|
340
|
+
{
|
|
341
|
+
get running() {
|
|
342
|
+
return state.container?.running ?? false
|
|
343
|
+
},
|
|
344
|
+
read: async () => {
|
|
345
|
+
booted = await state.storage.get<string>(ENV_FINGERPRINT_KEY)
|
|
346
|
+
return booted
|
|
347
|
+
},
|
|
348
|
+
// Killed rather than stopped gracefully, and not snapshotted on the
|
|
349
|
+
// way: this runs before the first request of a new object is proxied,
|
|
350
|
+
// so nothing of this object's is in flight, and the container holds
|
|
351
|
+
// nothing worth draining — every durable effect of a push is in the log
|
|
352
|
+
// before it is acknowledged (docs/adr/0007). The snapshot the last idle
|
|
353
|
+
// stop took is still on record and still restorable when the image is
|
|
354
|
+
// unchanged, so the replacement starts from it and re-syncs.
|
|
355
|
+
destroy: () => this.container.destroy(),
|
|
356
|
+
write: (fingerprint) => state.storage.put(ENV_FINGERPRINT_KEY, fingerprint),
|
|
357
|
+
},
|
|
358
|
+
current,
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
if (outcome === 'replaced') {
|
|
362
|
+
console.log(
|
|
363
|
+
`walgit container env changed (${booted ?? 'unrecorded'} -> ${current}); container replaced`,
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A running container, started if there was none. Waits out a stop first. */
|
|
369
|
+
private async ensureRunning(): Promise<void> {
|
|
370
|
+
// A failed stop is the alarm's to report; here it only has to be over.
|
|
371
|
+
while (this.stopping) await this.stopping.catch(() => undefined)
|
|
372
|
+
if (this.starting) return this.starting
|
|
373
|
+
if (this.container.running) return
|
|
374
|
+
const starting = this.boot().finally(() => {
|
|
375
|
+
if (this.starting === starting) this.starting = null
|
|
376
|
+
})
|
|
377
|
+
this.starting = starting
|
|
378
|
+
return starting
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Start a container — from the snapshot when it qualifies, else from the image. */
|
|
382
|
+
private async boot(): Promise<void> {
|
|
383
|
+
const image = this.image()
|
|
384
|
+
const store = storeIdentity(this.env)
|
|
385
|
+
const record = (await this.ctx.storage.get<SnapshotRecord>(SNAPSHOT_KEY)) ?? null
|
|
386
|
+
const plan = restorePlan({ enabled: this.snapshots, record, image, store, now: Date.now() })
|
|
387
|
+
|
|
388
|
+
const begun = Date.now()
|
|
389
|
+
if (plan.from === 'snapshot' && record) {
|
|
390
|
+
// Recorded BEFORE the start, not after: a deploy can reset this object
|
|
391
|
+
// between the container coming up and the write that would follow, and a
|
|
392
|
+
// container nobody recorded is one the next stop declines to snapshot.
|
|
393
|
+
await this.ctx.storage.put({ [BOOTED_IMAGE_KEY]: image, [BOOTED_FROM_KEY]: 'snapshot' })
|
|
394
|
+
const failure = await this.startFrom({ snapshotId: plan.id }, READY_TIMEOUT_MS)
|
|
395
|
+
if (failure === null) {
|
|
396
|
+
await this.ctx.storage.put(SNAPSHOT_KEY, refreshSnapshot(record, Date.now()))
|
|
397
|
+
this.started('snapshot')
|
|
398
|
+
return
|
|
399
|
+
}
|
|
400
|
+
// A snapshot that does not come up is not tried twice: forgotten, so the
|
|
401
|
+
// next start goes straight to the image, and the next idle stop records
|
|
402
|
+
// a new one.
|
|
403
|
+
console.error(`walgit container: restore of snapshot ${plan.id} failed (${failure})`)
|
|
404
|
+
await this.ctx.storage.delete(SNAPSHOT_KEY)
|
|
405
|
+
} else if (plan.from === 'image' && record && plan.reason !== 'disabled') {
|
|
406
|
+
console.log(`walgit container: snapshot ${record.id} not restored (${plan.reason})`)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ONE fresh start per request, inside what is left of `BOOT_BUDGET_MS`.
|
|
410
|
+
// A failure fails this request (and every request waiting on the same
|
|
411
|
+
// start) in seconds, and the next request makes the next attempt — which
|
|
412
|
+
// is also the retry the docs ask for after a stop, without making any one
|
|
413
|
+
// client wait through several.
|
|
414
|
+
// Recorded before the start, for the reason the restore above is.
|
|
415
|
+
await this.ctx.storage.put({ [BOOTED_IMAGE_KEY]: image, [BOOTED_FROM_KEY]: 'image' })
|
|
416
|
+
const failure = await this.startFrom({ image }, freshStartTimeoutMs(Date.now() - begun))
|
|
417
|
+
if (failure !== null) {
|
|
418
|
+
console.error(`walgit container: start failed (${failure})`)
|
|
419
|
+
await this.ctx.storage.delete([BOOTED_IMAGE_KEY, BOOTED_FROM_KEY])
|
|
420
|
+
throw new Error(failure)
|
|
421
|
+
}
|
|
422
|
+
this.started('image')
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** A start succeeded: stamp the next response, and make sure a stop is armed. */
|
|
426
|
+
private started(source: StartSource): void {
|
|
427
|
+
this.freshStart = source
|
|
428
|
+
console.log(`walgit container: started from ${source} (${CONTAINER_INSTANCE})`)
|
|
429
|
+
// Armed here as well as when a request finishes, so a container never runs
|
|
430
|
+
// without an alarm that will stop it — even when the request that started
|
|
431
|
+
// it never finishes because this object was reset.
|
|
432
|
+
void this.arm(Date.now() + this.sleepAfterMs)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* One `start()` and the wait for its port. Null when the container is up;
|
|
437
|
+
* otherwise why not, with the container no longer running.
|
|
438
|
+
*/
|
|
439
|
+
private async startFrom(
|
|
440
|
+
source: { image: string } | { snapshotId: string },
|
|
441
|
+
timeoutMs: number,
|
|
442
|
+
): Promise<string | null> {
|
|
443
|
+
const container = this.container
|
|
444
|
+
const common = {
|
|
445
|
+
env: this.envVars,
|
|
446
|
+
// The push path writes to object storage and announces to the Worker's
|
|
447
|
+
// public origin, both over the Internet. The `Container` class allowed it
|
|
448
|
+
// by default; this API asks.
|
|
449
|
+
enableInternet: true,
|
|
450
|
+
instance: CONTAINER_INSTANCE,
|
|
451
|
+
} as const
|
|
452
|
+
try {
|
|
453
|
+
if ('image' in source) container.start({ ...common, image: source.image })
|
|
454
|
+
else container.start({ ...common, containerSnapshot: { id: source.snapshotId } })
|
|
455
|
+
} catch (error) {
|
|
456
|
+
return `start refused: ${messageOf(error)}`
|
|
457
|
+
}
|
|
458
|
+
// A new container, so a new exit to wait on — never the last one's.
|
|
459
|
+
this.exit = null
|
|
460
|
+
try {
|
|
461
|
+
await container.setInactivityTimeout(inactivityTimeoutMs(this.sleepAfterMs))
|
|
462
|
+
} catch (error) {
|
|
463
|
+
// Not fatal: the container runs, and only the backstop is missing — it
|
|
464
|
+
// will be stopped when this object idles rather than by the alarm.
|
|
465
|
+
console.error(`walgit container: inactivity timeout not set: ${messageOf(error)}`)
|
|
466
|
+
}
|
|
467
|
+
return this.waitReady(timeoutMs)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Wait for the process to answer on its port.
|
|
472
|
+
*
|
|
473
|
+
* Asked of `HEALTH_PATH`, the one route src/http.ts answers before any
|
|
474
|
+
* credential, policy or repository is looked at — so the probe costs nothing
|
|
475
|
+
* on the container and is never counted as a request for `/`. Any answer is
|
|
476
|
+
* ready. A probe that fails while the container is still running is a
|
|
477
|
+
* process still booting; one that fails after it stopped is a container that
|
|
478
|
+
* will not come up, and `monitor()` — which settles for a container that
|
|
479
|
+
* already stopped — says why.
|
|
480
|
+
*/
|
|
481
|
+
private async waitReady(timeoutMs: number): Promise<string | null> {
|
|
482
|
+
const container = this.container
|
|
483
|
+
const port = container.getTcpPort(CONTAINER_PORT)
|
|
484
|
+
const deadline = Date.now() + timeoutMs
|
|
485
|
+
let last = 'no answer'
|
|
486
|
+
while (Date.now() < deadline) {
|
|
487
|
+
try {
|
|
488
|
+
const res = await port.fetch(`http://container${HEALTH_PATH}`, {
|
|
489
|
+
signal: AbortSignal.timeout(READY_PROBE_TIMEOUT_MS),
|
|
490
|
+
})
|
|
491
|
+
await res.body?.cancel()
|
|
492
|
+
return null
|
|
493
|
+
} catch (error) {
|
|
494
|
+
last = messageOf(error)
|
|
495
|
+
}
|
|
496
|
+
if (!container.running) {
|
|
497
|
+
const why = await settledWithin(this.exitOf(container), 1_000)
|
|
498
|
+
// Not yet settled is a container still being placed, not a dead one.
|
|
499
|
+
if (why !== undefined) return `stopped before it was ready: ${why}`
|
|
500
|
+
}
|
|
501
|
+
await sleep(READY_POLL_MS)
|
|
502
|
+
}
|
|
503
|
+
await container.destroy().catch(() => undefined)
|
|
504
|
+
return `not ready after ${timeoutMs} ms: ${last}`
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** Send a request to the container, once more if it lands on one that just exited. */
|
|
508
|
+
private async forward(request: Request): Promise<Response> {
|
|
509
|
+
// The container speaks plain HTTP; the scheme is the only thing rewritten.
|
|
510
|
+
const url = request.url.replace('https:', 'http:')
|
|
511
|
+
for (let attempt = 0; ; attempt++) {
|
|
512
|
+
try {
|
|
513
|
+
return await this.container.getTcpPort(CONTAINER_PORT).fetch(url, request)
|
|
514
|
+
} catch (error) {
|
|
515
|
+
if (!mayRetry(request.method, attempt)) throw error
|
|
516
|
+
console.warn(`walgit container: ${request.method} failed (${messageOf(error)}); retrying`)
|
|
517
|
+
await sleep(RETRY_DELAY_MS)
|
|
518
|
+
await this.ensureRunning()
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Pass a response body through, settling the request when its LAST byte
|
|
525
|
+
* leaves — a clone is one request that streams for as long as it takes, and
|
|
526
|
+
* the idle clock must not start until it is over. No copy and no buffering:
|
|
527
|
+
* the identity stream forwards chunks as they arrive.
|
|
528
|
+
*/
|
|
529
|
+
private trackBody(response: Response, settle: () => void): Response {
|
|
530
|
+
const { readable, writable } = new IdentityTransformStream()
|
|
531
|
+
// Settled either way: an abandoned clone is over too, and its rejection is
|
|
532
|
+
// the client's, not something to log.
|
|
533
|
+
response.body!.pipeTo(writable).then(settle, settle)
|
|
534
|
+
return new Response(readable, response)
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Proxy a WebSocket by holding both ends, so the request stays in flight for
|
|
539
|
+
* as long as the socket is open. Returning the container's socket as-is would
|
|
540
|
+
* work and would leave the idle clock blind to it. Nothing walgit serves from
|
|
541
|
+
* the container upgrades today — the ref-event stream is answered at the edge
|
|
542
|
+
* (worker/events-do.ts) — but the `Container` class proxied sockets, and a
|
|
543
|
+
* route that one day does must not be cut off mid-conversation.
|
|
544
|
+
*/
|
|
545
|
+
private proxySocket(response: Response, settle: () => void): Response {
|
|
546
|
+
const upstream = response.webSocket!
|
|
547
|
+
const [client, server] = Object.values(new WebSocketPair())
|
|
548
|
+
upstream.accept()
|
|
549
|
+
server.accept()
|
|
550
|
+
// 1005 and 1006 are reserved and may not be sent in a close frame.
|
|
551
|
+
const code = (value: number) => (value === 1005 || value === 1006 ? 1000 : value)
|
|
552
|
+
const pipe = (from: WebSocket, to: WebSocket) => {
|
|
553
|
+
from.addEventListener('message', (event) => {
|
|
554
|
+
try {
|
|
555
|
+
to.send(event.data)
|
|
556
|
+
} catch {
|
|
557
|
+
from.close(1011, 'walgit: the other end of the socket is gone')
|
|
558
|
+
}
|
|
559
|
+
})
|
|
560
|
+
from.addEventListener('close', (event) => {
|
|
561
|
+
settle()
|
|
562
|
+
try {
|
|
563
|
+
to.close(code(event.code), event.reason)
|
|
564
|
+
} catch {
|
|
565
|
+
// Already closed from the other side.
|
|
566
|
+
}
|
|
567
|
+
})
|
|
568
|
+
from.addEventListener('error', () => {
|
|
569
|
+
settle()
|
|
570
|
+
try {
|
|
571
|
+
to.close(1011, 'walgit: socket error')
|
|
572
|
+
} catch {
|
|
573
|
+
// Already closed.
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
}
|
|
577
|
+
pipe(server, upstream)
|
|
578
|
+
pipe(upstream, server)
|
|
579
|
+
return new Response(null, {
|
|
580
|
+
status: response.status,
|
|
581
|
+
headers: response.headers,
|
|
582
|
+
webSocket: client,
|
|
583
|
+
})
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** One request is over. The last one out moves the idle deadline. */
|
|
587
|
+
private finished(): void {
|
|
588
|
+
this.inflight = Math.max(0, this.inflight - 1)
|
|
589
|
+
if (this.inflight > 0) return
|
|
590
|
+
const now = Date.now()
|
|
591
|
+
this.lastActivity = now
|
|
592
|
+
if (!this.ctx.container?.running) return
|
|
593
|
+
const at = rearmAt({ now, scheduled: this.alarmAt, sleepAfterMs: this.sleepAfterMs })
|
|
594
|
+
if (at !== null) void this.arm(at)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
private async arm(at: number): Promise<void> {
|
|
598
|
+
this.alarmAt = at
|
|
599
|
+
try {
|
|
600
|
+
await this.ctx.storage.setAlarm(at)
|
|
601
|
+
} catch (error) {
|
|
602
|
+
this.alarmAt = null
|
|
603
|
+
console.error(`walgit container: alarm not armed: ${messageOf(error)}`)
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** Single-flight: a request that arrives mid-stop waits for it in `ensureRunning`. */
|
|
608
|
+
private stopContainer(): Promise<void> {
|
|
609
|
+
if (this.stopping) return this.stopping
|
|
610
|
+
const stopping = this.stop().finally(() => {
|
|
611
|
+
if (this.stopping === stopping) this.stopping = null
|
|
612
|
+
})
|
|
613
|
+
this.stopping = stopping
|
|
614
|
+
return stopping
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Snapshot, then SIGTERM, then — only if it lingers — kill.
|
|
619
|
+
*
|
|
620
|
+
* The snapshot comes first because it needs a running container, and it is
|
|
621
|
+
* taken with nothing in flight and the window of idleness behind it, so the
|
|
622
|
+
* detached work a push can leave running (a compaction) has had that long to
|
|
623
|
+
* finish. A failed snapshot does not stop the stop: the record already held
|
|
624
|
+
* is for this same image or `shouldSnapshot` would have said no, so the next
|
|
625
|
+
* start restores that one and re-syncs a little more.
|
|
626
|
+
*/
|
|
627
|
+
private async stop(): Promise<void> {
|
|
628
|
+
const container = this.container
|
|
629
|
+
if (this.snapshots && container.running) {
|
|
630
|
+
try {
|
|
631
|
+
await this.snapshot(container)
|
|
632
|
+
} catch (error) {
|
|
633
|
+
console.error(`walgit container: snapshot failed: ${messageOf(error)}`)
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (container.running) container.signal(SIGTERM)
|
|
638
|
+
const exited = await settledWithin(this.exitOf(container), STOP_GRACE_MS)
|
|
639
|
+
if (exited === undefined && container.running) {
|
|
640
|
+
console.warn(`walgit container: still running ${STOP_GRACE_MS} ms after SIGTERM; killing`)
|
|
641
|
+
await container.destroy()
|
|
642
|
+
}
|
|
643
|
+
await this.ctx.storage.delete([BOOTED_IMAGE_KEY, BOOTED_FROM_KEY])
|
|
644
|
+
this.freshStart = null
|
|
645
|
+
console.log('walgit container: stopped for idleness')
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/** Snapshot the running container, if it booted from the image this deploy starts. */
|
|
649
|
+
private async snapshot(container: Container): Promise<void> {
|
|
650
|
+
const image = this.image()
|
|
651
|
+
const bootedImage = (await this.ctx.storage.get<string>(BOOTED_IMAGE_KEY)) ?? null
|
|
652
|
+
if (!shouldSnapshot({ enabled: this.snapshots, bootedImage, image })) return
|
|
653
|
+
if (await this.compacting(container)) {
|
|
654
|
+
console.log('walgit container: compaction running; not snapshotting this stop')
|
|
655
|
+
return
|
|
656
|
+
}
|
|
657
|
+
const bootedFrom = (await this.ctx.storage.get<StartSource>(BOOTED_FROM_KEY)) ?? null
|
|
658
|
+
if (bootedFrom === 'snapshot') {
|
|
659
|
+
const changed = await this.diskChanged(container)
|
|
660
|
+
if (!needsNewSnapshot({ bootedFrom, changed })) {
|
|
661
|
+
console.log('walgit container: cache unchanged since restore; keeping the snapshot')
|
|
662
|
+
return
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
await this.flushDisk(container)
|
|
666
|
+
const begun = Date.now()
|
|
667
|
+
const handle = await container.snapshotContainer({ name: `walgit-${begun}` })
|
|
668
|
+
await this.ctx.storage.put(
|
|
669
|
+
SNAPSHOT_KEY,
|
|
670
|
+
recordSnapshot(handle, { image, store: storeIdentity(this.env), now: Date.now() }),
|
|
671
|
+
)
|
|
672
|
+
console.log(
|
|
673
|
+
`walgit container: snapshot ${handle.id} (${handle.size} bytes) in ${Date.now() - begun} ms`,
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Is a detached compaction still running in the container?
|
|
679
|
+
*
|
|
680
|
+
* It is the one piece of work nothing counts in flight: `post-receive`
|
|
681
|
+
* spawns it disowned so the push can be acknowledged, and it repacks the
|
|
682
|
+
* repository's Cache in place. A snapshot taken mid-repack would still only
|
|
683
|
+
* cost a re-sync — every object stays present throughout `git repack -adf`,
|
|
684
|
+
* and reconcile rewrites refs either way — but packs not yet renamed to their
|
|
685
|
+
* WAL names are packs the next materialize re-downloads. So the stop goes
|
|
686
|
+
* ahead (a killed compaction loses nothing: its lease lapses and the next
|
|
687
|
+
* push re-triggers it) and the snapshot does not: the record already held is
|
|
688
|
+
* for this same image, and the next start restores that one instead.
|
|
689
|
+
*
|
|
690
|
+
* Answered with `pgrep` in the container, which the image's busybox carries.
|
|
691
|
+
* A failed check reads as "not compacting": the cost of being wrong is the
|
|
692
|
+
* re-download above, and the cost of the other answer is a stop that never
|
|
693
|
+
* snapshots.
|
|
694
|
+
*/
|
|
695
|
+
private async compacting(container: Container): Promise<boolean> {
|
|
696
|
+
try {
|
|
697
|
+
const check = await container.exec(['pgrep', '-f', COMPACTION_PROCESS], {
|
|
698
|
+
stdout: 'ignore',
|
|
699
|
+
stderr: 'ignore',
|
|
700
|
+
})
|
|
701
|
+
return (await settledWithin(check.exitCode, 5_000)) === 0
|
|
702
|
+
} catch {
|
|
703
|
+
return false
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Has the repo cache changed since this container booted?
|
|
709
|
+
*
|
|
710
|
+
* `null` when the check could not answer, which `needsNewSnapshot` reads as
|
|
711
|
+
* "snapshot anyway": the cost of a needless snapshot is seconds, and the cost
|
|
712
|
+
* of a missed one is a restore that re-syncs what this run already fetched.
|
|
713
|
+
*/
|
|
714
|
+
private async diskChanged(container: Container): Promise<boolean | null> {
|
|
715
|
+
try {
|
|
716
|
+
const run = await container.exec(DISK_CHANGED_COMMAND, { stdout: 'pipe', stderr: 'ignore' })
|
|
717
|
+
const out = await settledWithin(run.output(), 10_000)
|
|
718
|
+
if (out === undefined || out.exitCode !== 0) return null
|
|
719
|
+
return new TextDecoder().decode(out.stdout).trim() !== ''
|
|
720
|
+
} catch {
|
|
721
|
+
return null
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/** The running container's exit, asked of `monitor()` once per start. */
|
|
726
|
+
private exitOf(container: Container): Promise<string> {
|
|
727
|
+
if (!this.exit) this.exit = exitOf(container)
|
|
728
|
+
return this.exit
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Ask the kernel to write out what it is still holding, so the snapshot sees
|
|
733
|
+
* the disk the process thinks it wrote. Best-effort: the platform's own
|
|
734
|
+
* snapshot may already do this, and a disk short of its last write is a
|
|
735
|
+
* re-sync, never a wrong answer (shared/container-snapshot.ts).
|
|
736
|
+
*/
|
|
737
|
+
private async flushDisk(container: Container): Promise<void> {
|
|
738
|
+
try {
|
|
739
|
+
const sync = await container.exec(['sync'], { stdout: 'ignore', stderr: 'ignore' })
|
|
740
|
+
await settledWithin(sync.exitCode, 10_000)
|
|
741
|
+
} catch (error) {
|
|
742
|
+
console.warn(`walgit container: sync before snapshot failed: ${messageOf(error)}`)
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* The container's exit, as a promise that never rejects: its value says how it
|
|
749
|
+
* ended. `monitor()` throws outright on an object that has no container, which
|
|
750
|
+
* reads here as already exited.
|
|
751
|
+
*/
|
|
752
|
+
function exitOf(container: Container): Promise<string> {
|
|
753
|
+
try {
|
|
754
|
+
return container.monitor().then(
|
|
755
|
+
() => 'exited cleanly',
|
|
756
|
+
(error: unknown) => messageOf(error),
|
|
757
|
+
)
|
|
758
|
+
} catch (error) {
|
|
759
|
+
return Promise.resolve(messageOf(error))
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* The promise's value if it settles within `ms`, else undefined — with the
|
|
765
|
+
* timer cleared either way, so nothing is left pending to hold the object in
|
|
766
|
+
* memory after the caller has moved on.
|
|
767
|
+
*/
|
|
768
|
+
async function settledWithin<T>(promise: Promise<T>, ms: number): Promise<T | undefined> {
|
|
769
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
770
|
+
const timeout = new Promise<undefined>((resolve) => {
|
|
771
|
+
timer = setTimeout(() => resolve(undefined), ms)
|
|
772
|
+
})
|
|
773
|
+
try {
|
|
774
|
+
return await Promise.race([promise, timeout])
|
|
775
|
+
} finally {
|
|
776
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function sleep(ms: number): Promise<void> {
|
|
781
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function messageOf(error: unknown): string {
|
|
785
|
+
return error instanceof Error ? error.message : String(error)
|
|
786
|
+
}
|