@crvouga/mockingbird-service-cognito 3.2.0 → 3.4.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 +18 -4
- package/DISCOVERY.md +23 -0
- package/README.md +26 -4
- package/dist/chunk-K7FBL7SU.js +4750 -0
- package/dist/chunk-K7FBL7SU.js.map +7 -0
- package/dist/chunk-UAWSJCL6.js +5648 -0
- package/dist/chunk-UAWSJCL6.js.map +7 -0
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +354 -41
- package/dist/index.js +1 -1
- package/dist/server.d.ts +709 -82
- package/dist/server.js +2 -2
- package/package.json +3 -2
- package/dist/chunk-2NGQG7VL.js +0 -831
- package/dist/chunk-2NGQG7VL.js.map +0 -7
- package/dist/chunk-DR6TIMPN.js +0 -3753
- package/dist/chunk-DR6TIMPN.js.map +0 -7
|
@@ -0,0 +1,4750 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createClock,
|
|
3
|
+
createRuntime,
|
|
4
|
+
normalizeBootControls,
|
|
5
|
+
normalizeControls,
|
|
6
|
+
ownedTransport,
|
|
7
|
+
parseDuration,
|
|
8
|
+
parseFaultHeader,
|
|
9
|
+
resolveAdminPrefix,
|
|
10
|
+
restoreClock
|
|
11
|
+
} from "./chunk-UAWSJCL6.js";
|
|
12
|
+
|
|
13
|
+
// ../../adapters/node/dist/cli.js
|
|
14
|
+
import { readFile as readFile5 } from "node:fs/promises";
|
|
15
|
+
import { createRequire as createRequire3 } from "node:module";
|
|
16
|
+
import { resolve as resolve3 } from "node:path";
|
|
17
|
+
import { pathToFileURL } from "node:url";
|
|
18
|
+
import { parseArgs } from "node:util";
|
|
19
|
+
|
|
20
|
+
// ../../adapters/node/dist/fleet.js
|
|
21
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
|
|
22
|
+
import { createHash as createHash4, randomUUID as randomUUID4 } from "node:crypto";
|
|
23
|
+
import { chmod, readFile as readFile2, rename, rm, writeFile } from "node:fs/promises";
|
|
24
|
+
|
|
25
|
+
// ../../adapters/node/dist/fleet-management.js
|
|
26
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
27
|
+
import { createHash as createHash3, randomBytes, randomUUID as randomUUID3 } from "node:crypto";
|
|
28
|
+
|
|
29
|
+
// ../../adapters/node/dist/fleet-archive.js
|
|
30
|
+
import { createHash } from "node:crypto";
|
|
31
|
+
var canonical = (value) => {
|
|
32
|
+
if (Array.isArray(value))
|
|
33
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
34
|
+
if (value !== null && typeof value === "object")
|
|
35
|
+
return `{${Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
36
|
+
return JSON.stringify(value);
|
|
37
|
+
};
|
|
38
|
+
var digest = (value) => createHash("sha256").update(canonical(value)).digest("hex");
|
|
39
|
+
var sealArchive = (content) => ({
|
|
40
|
+
...content,
|
|
41
|
+
sha256: digest(content)
|
|
42
|
+
});
|
|
43
|
+
function validateArchive(value) {
|
|
44
|
+
if (!value || typeof value !== "object")
|
|
45
|
+
throw new Error("expected fleet archive");
|
|
46
|
+
const doc = value;
|
|
47
|
+
const { sha256, ...content } = doc;
|
|
48
|
+
if (doc.format !== "mockingbird.fleet/v1" || typeof doc.sourceId !== "string" || typeof doc.createdAt !== "string" || !doc.namespaces || typeof doc.namespaces !== "object" || Array.isArray(doc.namespaces) || sha256 !== digest(content))
|
|
49
|
+
throw new Error("unsupported or corrupt fleet archive");
|
|
50
|
+
for (const [namespace, entry] of Object.entries(doc.namespaces)) {
|
|
51
|
+
if (!/^[A-Za-z0-9_.-]{1,64}$/.test(namespace) || !entry || !Number.isFinite(entry.clock?.now) || typeof entry.clock?.frozen !== "boolean" || !entry.services || !Object.keys(entry.services).length)
|
|
52
|
+
throw new Error("invalid archive namespace");
|
|
53
|
+
for (const [name, child] of Object.entries(entry.services))
|
|
54
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) || !child || child.version !== 1 || !["http", "postgres", "redis", "external"].includes(child.protocol))
|
|
55
|
+
throw new Error("unsupported archive service/version");
|
|
56
|
+
}
|
|
57
|
+
return doc;
|
|
58
|
+
}
|
|
59
|
+
function inspectArchive(input) {
|
|
60
|
+
const archive = validateArchive(input);
|
|
61
|
+
return {
|
|
62
|
+
format: archive.format,
|
|
63
|
+
createdAt: archive.createdAt,
|
|
64
|
+
sha256: archive.sha256,
|
|
65
|
+
namespaces: Object.fromEntries(Object.entries(archive.namespaces).map(([name, ns]) => [
|
|
66
|
+
name,
|
|
67
|
+
{
|
|
68
|
+
clock: ns.clock,
|
|
69
|
+
services: Object.fromEntries(Object.entries(ns.services).map(([service, state]) => [
|
|
70
|
+
service,
|
|
71
|
+
{
|
|
72
|
+
protocol: state.protocol,
|
|
73
|
+
version: state.version,
|
|
74
|
+
bytes: Buffer.byteLength(canonical(state.state))
|
|
75
|
+
}
|
|
76
|
+
]))
|
|
77
|
+
}
|
|
78
|
+
]))
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function diffArchives(left, right, options = {}) {
|
|
82
|
+
const a = validateArchive(left), b = validateArchive(right);
|
|
83
|
+
const limit = Math.min(1e3, Math.max(1, options.limit ?? 100));
|
|
84
|
+
const changes = [];
|
|
85
|
+
let total = 0;
|
|
86
|
+
const walk = (namespace, service, path, before, after) => {
|
|
87
|
+
if (canonical(before) === canonical(after))
|
|
88
|
+
return;
|
|
89
|
+
if (before && after && typeof before === "object" && typeof after === "object" && !Array.isArray(before) && !Array.isArray(after)) {
|
|
90
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)]))
|
|
91
|
+
walk(namespace, service, `${path}/${key}`, before[key], after[key]);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
total++;
|
|
95
|
+
if (changes.length < limit)
|
|
96
|
+
changes.push({
|
|
97
|
+
namespace,
|
|
98
|
+
service,
|
|
99
|
+
path,
|
|
100
|
+
kind: before === void 0 ? "added" : after === void 0 ? "removed" : "changed",
|
|
101
|
+
...options.values ? { before, after } : {}
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
for (const ns of /* @__PURE__ */ new Set([...Object.keys(a.namespaces), ...Object.keys(b.namespaces)])) {
|
|
105
|
+
if (options.namespace !== void 0 && options.namespace !== ns)
|
|
106
|
+
continue;
|
|
107
|
+
const an = a.namespaces[ns], bn = b.namespaces[ns];
|
|
108
|
+
walk(ns, "$clock", "/clock", an?.clock, bn?.clock);
|
|
109
|
+
for (const service of /* @__PURE__ */ new Set([
|
|
110
|
+
...Object.keys(an?.services ?? {}),
|
|
111
|
+
...Object.keys(bn?.services ?? {})
|
|
112
|
+
])) {
|
|
113
|
+
if (options.service !== void 0 && options.service !== service)
|
|
114
|
+
continue;
|
|
115
|
+
const x = an?.services[service], y = bn?.services[service];
|
|
116
|
+
if (x && y && (x.protocol !== y.protocol || x.version !== y.version))
|
|
117
|
+
throw new Error("incompatible snapshot comparison");
|
|
118
|
+
const normalize = (state) => {
|
|
119
|
+
if (state && typeof state === "object") {
|
|
120
|
+
const obj = state;
|
|
121
|
+
if (obj.format === "mockingbird.postgres/v1" && obj.inventory) {
|
|
122
|
+
if (options.values)
|
|
123
|
+
throw new Error("raw PostgreSQL row diff is unsupported; use bounded namespace inspection");
|
|
124
|
+
const { databases: _databases, ...metadata } = obj;
|
|
125
|
+
return metadata;
|
|
126
|
+
}
|
|
127
|
+
if (obj.$redis === "map" && Array.isArray(obj.value))
|
|
128
|
+
return Object.fromEntries(obj.value.map((pair) => [
|
|
129
|
+
typeof pair[0] === "string" ? pair[0] : canonical(pair[0]),
|
|
130
|
+
normalize(pair[1])
|
|
131
|
+
]));
|
|
132
|
+
if (Array.isArray(state))
|
|
133
|
+
return Object.fromEntries(state.map((value, index) => [String(index), normalize(value)]));
|
|
134
|
+
}
|
|
135
|
+
if (state && typeof state === "object" && Array.isArray(state.records)) {
|
|
136
|
+
const { records, ...rest } = state;
|
|
137
|
+
return {
|
|
138
|
+
...rest,
|
|
139
|
+
records: Object.fromEntries(records.map((r) => [`${r.collection}/${r.id}`, r]))
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (state && typeof state === "object" && !Array.isArray(state))
|
|
143
|
+
return Object.fromEntries(Object.entries(state).map(([k, v]) => [k, normalize(v)]));
|
|
144
|
+
return state;
|
|
145
|
+
};
|
|
146
|
+
walk(ns, service, "/state", normalize(x?.state), normalize(y?.state));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { format: "mockingbird.fleet-diff/v1", total, truncated: total > changes.length, changes };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ../../adapters/node/dist/fleet-bindings.js
|
|
153
|
+
var object = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
154
|
+
async function planRestoreBindings(input, plan, resolveSecret) {
|
|
155
|
+
const source = validateArchive(input), copy = structuredClone(source);
|
|
156
|
+
if (!plan || !Array.isArray(plan.bindings) || !object(plan.endpoints))
|
|
157
|
+
throw new Error("expected explicit restore bindings and endpoint references");
|
|
158
|
+
const changes = [];
|
|
159
|
+
const seen = /* @__PURE__ */ new Set();
|
|
160
|
+
const endpoint = (reference) => {
|
|
161
|
+
const value = plan.endpoints[reference];
|
|
162
|
+
if (typeof value !== "string")
|
|
163
|
+
throw new Error(`unresolved endpoint reference ${reference}`);
|
|
164
|
+
const url = new URL(value);
|
|
165
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password)
|
|
166
|
+
throw new Error(`invalid endpoint reference ${reference}`);
|
|
167
|
+
return url.href;
|
|
168
|
+
};
|
|
169
|
+
for (const binding of plan.bindings) {
|
|
170
|
+
const identity = `${binding.namespace}/${binding.service}/${binding.endpointId}`;
|
|
171
|
+
if (seen.has(identity))
|
|
172
|
+
throw new Error(`ambiguous binding ${identity}`);
|
|
173
|
+
seen.add(identity);
|
|
174
|
+
const section = copy.namespaces[binding.namespace]?.services[binding.service];
|
|
175
|
+
if (section?.protocol !== "http" || !object(section.state) || !object(section.state.webhooks) || !Array.isArray(section.state.webhooks.endpoints))
|
|
176
|
+
throw new Error(`unsupported callback binding ${identity}`);
|
|
177
|
+
const hub = section.state.webhooks;
|
|
178
|
+
const matches = hub.endpoints.filter((e) => object(e) && e.id === binding.endpointId);
|
|
179
|
+
if (matches.length !== 1)
|
|
180
|
+
throw new Error(`unresolved or ambiguous endpoint ${identity}`);
|
|
181
|
+
const current = matches[0], old = String(current.url), destination2 = endpoint(binding.endpointRef);
|
|
182
|
+
if (current.signUrl !== void 0 && binding.signing === void 0)
|
|
183
|
+
throw new Error(`signing policy required for ${identity}`);
|
|
184
|
+
if (binding.signing !== void 0 && !["preserve", "replace"].includes(binding.signing))
|
|
185
|
+
throw new Error(`unsupported signing policy ${identity}`);
|
|
186
|
+
const signUrl = binding.signing === "replace" ? endpoint(binding.signingRef ?? binding.endpointRef) : current.signUrl;
|
|
187
|
+
let secret;
|
|
188
|
+
if (binding.secretRef !== void 0) {
|
|
189
|
+
if (!resolveSecret)
|
|
190
|
+
throw new Error(`unresolved secret reference ${binding.secretRef}`);
|
|
191
|
+
try {
|
|
192
|
+
secret = await resolveSecret(binding.secretRef);
|
|
193
|
+
if (typeof secret !== "string" || !secret)
|
|
194
|
+
throw new Error();
|
|
195
|
+
} catch {
|
|
196
|
+
throw new Error(`unresolved secret reference ${binding.secretRef}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const apply = (value) => {
|
|
200
|
+
value.url = destination2;
|
|
201
|
+
if (signUrl !== void 0)
|
|
202
|
+
value.signUrl = signUrl;
|
|
203
|
+
if (secret !== void 0)
|
|
204
|
+
value.secret = secret;
|
|
205
|
+
};
|
|
206
|
+
apply(current);
|
|
207
|
+
if (Array.isArray(hub.payloads)) {
|
|
208
|
+
for (const pair of hub.payloads)
|
|
209
|
+
if (Array.isArray(pair) && object(pair[1]) && object(pair[1].endpoint) && pair[1].endpoint.id === binding.endpointId)
|
|
210
|
+
apply(pair[1].endpoint);
|
|
211
|
+
}
|
|
212
|
+
if (Array.isArray(hub.deliveries)) {
|
|
213
|
+
for (const pair of hub.deliveries)
|
|
214
|
+
if (Array.isArray(pair) && object(pair[1]) && pair[1].endpointId === binding.endpointId)
|
|
215
|
+
pair[1].url = destination2;
|
|
216
|
+
}
|
|
217
|
+
if (Array.isArray(section.state.records)) {
|
|
218
|
+
for (const row of section.state.records)
|
|
219
|
+
if (object(row) && row.collection === "runtime_webhook_endpoints" && object(row.value) && row.value.id === binding.endpointId)
|
|
220
|
+
apply(row.value);
|
|
221
|
+
}
|
|
222
|
+
changes.push({
|
|
223
|
+
namespace: binding.namespace,
|
|
224
|
+
service: binding.service,
|
|
225
|
+
endpointId: binding.endpointId,
|
|
226
|
+
endpointRef: binding.endpointRef,
|
|
227
|
+
...binding.secretRef ? { secretRef: binding.secretRef } : {},
|
|
228
|
+
previousFingerprint: digest(old),
|
|
229
|
+
destinationFingerprint: digest(destination2),
|
|
230
|
+
signing: binding.signing ?? "not-configured"
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
const { sha256: _sha, ...content } = copy;
|
|
234
|
+
return {
|
|
235
|
+
report: {
|
|
236
|
+
version: 1,
|
|
237
|
+
source: source.sha256,
|
|
238
|
+
changes,
|
|
239
|
+
limitations: [
|
|
240
|
+
"opaque payloads, tokens, issuer material and application URLs are unchanged",
|
|
241
|
+
"protocol key rebinding requires a registered participant transformation"
|
|
242
|
+
]
|
|
243
|
+
},
|
|
244
|
+
archive: sealArchive(content)
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
var rebaseArchiveNamespace = (input, source, destination2) => {
|
|
248
|
+
const archive = validateArchive(input);
|
|
249
|
+
if (!/^[A-Za-z0-9_.-]{1,64}$/.test(destination2) || !archive.namespaces[source])
|
|
250
|
+
throw new Error("invalid namespace fork");
|
|
251
|
+
const { sha256: _sha, ...content } = archive;
|
|
252
|
+
return sealArchive({
|
|
253
|
+
...content,
|
|
254
|
+
namespaces: { [destination2]: structuredClone(archive.namespaces[source]) }
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// ../../adapters/node/dist/fleet-instrumentation.js
|
|
259
|
+
import { randomUUID } from "node:crypto";
|
|
260
|
+
var FleetInstrumentation = class {
|
|
261
|
+
options;
|
|
262
|
+
auditEntries = [];
|
|
263
|
+
started = /* @__PURE__ */ new Map();
|
|
264
|
+
terminal = /* @__PURE__ */ new Set();
|
|
265
|
+
metrics = {
|
|
266
|
+
started: 0,
|
|
267
|
+
succeeded: 0,
|
|
268
|
+
failed: 0,
|
|
269
|
+
cancelled: 0,
|
|
270
|
+
recoveryRequired: 0,
|
|
271
|
+
phaseEvents: 0,
|
|
272
|
+
auditFailures: 0,
|
|
273
|
+
rollbackAttempts: 0,
|
|
274
|
+
rollbackFailures: 0,
|
|
275
|
+
telemetryFailures: 0,
|
|
276
|
+
durationsMs: { count: 0, sum: 0, max: 0 }
|
|
277
|
+
};
|
|
278
|
+
constructor(options = {}) {
|
|
279
|
+
this.options = options;
|
|
280
|
+
}
|
|
281
|
+
async audit(input) {
|
|
282
|
+
const event = { ...input, id: randomUUID(), timestamp: (/* @__PURE__ */ new Date()).toISOString() };
|
|
283
|
+
let accepted = true;
|
|
284
|
+
try {
|
|
285
|
+
if (this.options.audit) {
|
|
286
|
+
await this.options.audit.write(event);
|
|
287
|
+
event.durable = true;
|
|
288
|
+
} else
|
|
289
|
+
event.durable = false;
|
|
290
|
+
} catch {
|
|
291
|
+
this.metrics.auditFailures++;
|
|
292
|
+
event.durable = false;
|
|
293
|
+
accepted = this.options.audit?.failurePolicy !== "closed";
|
|
294
|
+
}
|
|
295
|
+
this.auditEntries.push(event);
|
|
296
|
+
if (this.auditEntries.length > 200)
|
|
297
|
+
this.auditEntries.shift();
|
|
298
|
+
return accepted;
|
|
299
|
+
}
|
|
300
|
+
phase(operation, recoveryRequired) {
|
|
301
|
+
if (!this.started.has(operation.id)) {
|
|
302
|
+
this.started.set(operation.id, performance.now());
|
|
303
|
+
this.metrics.started++;
|
|
304
|
+
}
|
|
305
|
+
const elapsedMs = Math.max(0, performance.now() - this.started.get(operation.id));
|
|
306
|
+
this.metrics.phaseEvents++;
|
|
307
|
+
if (operation.phase === "rolling-back")
|
|
308
|
+
this.metrics.rollbackAttempts++;
|
|
309
|
+
if (operation.phase === "rollback-failed")
|
|
310
|
+
this.metrics.rollbackFailures++;
|
|
311
|
+
if (operation.status !== "running" && !this.terminal.has(operation.id)) {
|
|
312
|
+
this.terminal.add(operation.id);
|
|
313
|
+
if (operation.status === "succeeded")
|
|
314
|
+
this.metrics.succeeded++;
|
|
315
|
+
else if (operation.status === "cancelled")
|
|
316
|
+
this.metrics.cancelled++;
|
|
317
|
+
else
|
|
318
|
+
this.metrics.failed++;
|
|
319
|
+
if (recoveryRequired)
|
|
320
|
+
this.metrics.recoveryRequired++;
|
|
321
|
+
this.metrics.durationsMs.count++;
|
|
322
|
+
this.metrics.durationsMs.sum += elapsedMs;
|
|
323
|
+
this.metrics.durationsMs.max = Math.max(this.metrics.durationsMs.max, elapsedMs);
|
|
324
|
+
}
|
|
325
|
+
const action = operation.action.replace(/\/namespaces\/[^/]+/g, "/namespaces/{namespace}").replace(/\/snapshots\/[^/]+/g, "/snapshots/{id}").replace(/\/scenarios\/[^/]+/g, "/scenarios/{name}");
|
|
326
|
+
try {
|
|
327
|
+
const sent = this.options.telemetry?.({
|
|
328
|
+
...operation.participant ? { participant: operation.participant } : {},
|
|
329
|
+
operationId: operation.id,
|
|
330
|
+
phase: operation.phase,
|
|
331
|
+
action,
|
|
332
|
+
services: operation.services ?? [],
|
|
333
|
+
status: operation.status,
|
|
334
|
+
elapsedMs,
|
|
335
|
+
recoveryRequired
|
|
336
|
+
});
|
|
337
|
+
if (sent)
|
|
338
|
+
void sent.catch(() => {
|
|
339
|
+
this.metrics.telemetryFailures++;
|
|
340
|
+
});
|
|
341
|
+
} catch {
|
|
342
|
+
this.metrics.telemetryFailures++;
|
|
343
|
+
}
|
|
344
|
+
while (this.started.size > 200) {
|
|
345
|
+
const id = this.started.keys().next().value;
|
|
346
|
+
this.started.delete(id);
|
|
347
|
+
this.terminal.delete(id);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
snapshot() {
|
|
351
|
+
return {
|
|
352
|
+
version: 1,
|
|
353
|
+
metrics: structuredClone(this.metrics),
|
|
354
|
+
cardinality: "aggregate-no-namespace-labels"
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
auditTrail() {
|
|
358
|
+
return {
|
|
359
|
+
version: 1,
|
|
360
|
+
entries: structuredClone(this.auditEntries),
|
|
361
|
+
retention: 200,
|
|
362
|
+
durableSink: Boolean(this.options.audit),
|
|
363
|
+
failurePolicy: this.options.audit?.failurePolicy ?? "memory-only"
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// ../../adapters/node/dist/fleet-journal.js
|
|
369
|
+
import { mkdir, readdir } from "node:fs/promises";
|
|
370
|
+
import { join } from "node:path";
|
|
371
|
+
var validate = (record) => {
|
|
372
|
+
if (record?.version !== 1 || !Number.isSafeInteger(record.revision) || record.revision < 1 || !/^[a-f0-9-]{36}$/.test(record.operationId) || !Array.isArray(record.scope) || record.scope.length > 1e3 || record.scope.some((ns) => !/^[A-Za-z0-9_.-]{1,64}$/.test(ns)) || ![
|
|
373
|
+
"prepared",
|
|
374
|
+
"committing",
|
|
375
|
+
"uploading",
|
|
376
|
+
"publishing",
|
|
377
|
+
"succeeded",
|
|
378
|
+
"failed",
|
|
379
|
+
"recovery-required",
|
|
380
|
+
"resolved"
|
|
381
|
+
].includes(record.phase) || [record.before, record.after].some((id) => id !== void 0 && !/^[a-f0-9]{64}$/.test(id)) || Object.keys(record).some((key) => ![
|
|
382
|
+
"version",
|
|
383
|
+
"revision",
|
|
384
|
+
"operationId",
|
|
385
|
+
"controller",
|
|
386
|
+
"namespace",
|
|
387
|
+
"scope",
|
|
388
|
+
"action",
|
|
389
|
+
"phase",
|
|
390
|
+
"timestamp",
|
|
391
|
+
"requestFingerprint",
|
|
392
|
+
"before",
|
|
393
|
+
"after",
|
|
394
|
+
"writerGeneration",
|
|
395
|
+
"resolution"
|
|
396
|
+
].includes(key)))
|
|
397
|
+
throw new Error("invalid durable operation metadata");
|
|
398
|
+
};
|
|
399
|
+
async function reconcileOperation(record, repository) {
|
|
400
|
+
validate(record);
|
|
401
|
+
if (["succeeded", "failed", "resolved"].includes(record.phase))
|
|
402
|
+
return { status: "terminal", operationId: record.operationId };
|
|
403
|
+
if (record.phase === "prepared")
|
|
404
|
+
return { status: "abandoned-before-commit", operationId: record.operationId };
|
|
405
|
+
if (record.after) {
|
|
406
|
+
try {
|
|
407
|
+
const archive = await repository.get(record.after), publication = await repository.publicationMetadata?.();
|
|
408
|
+
if (archive.sha256 === record.after && publication?.id === record.after && record.writerGeneration !== void 0 && publication.generation === record.writerGeneration)
|
|
409
|
+
return {
|
|
410
|
+
status: "verified-publication",
|
|
411
|
+
operationId: record.operationId,
|
|
412
|
+
archive: record.after
|
|
413
|
+
};
|
|
414
|
+
} catch {
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (record.before)
|
|
418
|
+
try {
|
|
419
|
+
await repository.get(record.before);
|
|
420
|
+
} catch {
|
|
421
|
+
return {
|
|
422
|
+
status: "recovery-required",
|
|
423
|
+
operationId: record.operationId,
|
|
424
|
+
reason: "before-state archive unavailable",
|
|
425
|
+
scope: record.scope
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
status: "recovery-required",
|
|
430
|
+
operationId: record.operationId,
|
|
431
|
+
reason: "commit or rollback cannot be proven",
|
|
432
|
+
scope: record.scope
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// ../../adapters/node/dist/fleet-observations.js
|
|
437
|
+
import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
|
|
438
|
+
var hash = (text) => createHash2("sha256").update(text).digest("hex");
|
|
439
|
+
var boundedText = async (stream, max) => {
|
|
440
|
+
if (!stream)
|
|
441
|
+
return "";
|
|
442
|
+
const reader = stream.getReader(), chunks = [];
|
|
443
|
+
let bytes = 0, timedOut = false;
|
|
444
|
+
const timer = setTimeout(() => {
|
|
445
|
+
timedOut = true;
|
|
446
|
+
void reader.cancel().catch(() => {
|
|
447
|
+
});
|
|
448
|
+
}, 100);
|
|
449
|
+
try {
|
|
450
|
+
for (; ; ) {
|
|
451
|
+
const result = await reader.read();
|
|
452
|
+
if (result.done)
|
|
453
|
+
break;
|
|
454
|
+
bytes += result.value.length;
|
|
455
|
+
if (bytes > max)
|
|
456
|
+
throw new Error("capture byte limit");
|
|
457
|
+
chunks.push(result.value);
|
|
458
|
+
}
|
|
459
|
+
if (timedOut)
|
|
460
|
+
throw new Error("capture deadline");
|
|
461
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
462
|
+
} finally {
|
|
463
|
+
clearTimeout(timer);
|
|
464
|
+
void reader.cancel().catch(() => {
|
|
465
|
+
});
|
|
466
|
+
reader.releaseLock();
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
var FleetObservations = class {
|
|
470
|
+
processId;
|
|
471
|
+
limits;
|
|
472
|
+
sequence = 0;
|
|
473
|
+
entries = [];
|
|
474
|
+
baselines = /* @__PURE__ */ new Map();
|
|
475
|
+
traces = /* @__PURE__ */ new Map();
|
|
476
|
+
constructor(processId, limits = { events: 500, traceEvents: 200, traceBytes: 1024 * 1024 }) {
|
|
477
|
+
this.processId = processId;
|
|
478
|
+
this.limits = limits;
|
|
479
|
+
}
|
|
480
|
+
record(event) {
|
|
481
|
+
this.entries.push({ ...event, id: ++this.sequence });
|
|
482
|
+
if (this.entries.length > this.limits.events)
|
|
483
|
+
this.entries.shift();
|
|
484
|
+
}
|
|
485
|
+
baseline(namespace, services) {
|
|
486
|
+
const id = randomUUID2();
|
|
487
|
+
this.baselines.set(id, { namespace, services, sequence: this.sequence });
|
|
488
|
+
while (this.baselines.size > 200)
|
|
489
|
+
this.baselines.delete(this.baselines.keys().next().value);
|
|
490
|
+
return { id, namespace, services, sequence: this.sequence };
|
|
491
|
+
}
|
|
492
|
+
assertClean(id, namespace, diagnostics, options = {}) {
|
|
493
|
+
const baseline = this.baselines.get(id);
|
|
494
|
+
if (!baseline || baseline.namespace !== namespace)
|
|
495
|
+
throw new Error("assertion baseline not found in this namespace");
|
|
496
|
+
const allowances = options.allowances ?? [];
|
|
497
|
+
if (!Array.isArray(allowances) || allowances.some((a) => !["unmatched", "rejected", "fault"].includes(a.kind) || !a.service || !a.pathFingerprint && !a.operationId || !Number.isSafeInteger(a.maxCount) || a.maxCount < 1))
|
|
498
|
+
throw new Error("allowances must identify a service and exact operation or path fingerprint with a bounded count");
|
|
499
|
+
const consumed = /* @__PURE__ */ new Map(), failures = [];
|
|
500
|
+
if (this.entries.length && baseline.sequence < (this.entries[0]?.id ?? 0) - 1)
|
|
501
|
+
failures.push({ kind: "evidence_truncated" });
|
|
502
|
+
for (const entry of this.entries) {
|
|
503
|
+
if (entry.id <= baseline.sequence || entry.namespace !== namespace || !baseline.services.includes(entry.service))
|
|
504
|
+
continue;
|
|
505
|
+
const kinds = entry.kind === "webhook" ? entry.deliveryState === "failed" || entry.deliveryState === "dropped" ? ["failed_delivery"] : [] : entry.fault ? ["fault"] : entry.unmatched ? ["unmatched"] : (entry.status ?? 0) >= 400 ? ["rejected"] : [];
|
|
506
|
+
for (const kind of kinds) {
|
|
507
|
+
const allowed = allowances.find((a) => a.kind === kind && a.service === entry.service && (!a.pathFingerprint || a.pathFingerprint === entry.pathFingerprint) && (!a.operationId || a.operationId === entry.operationId) && (a.status === void 0 || a.status === entry.status) && (consumed.get(a) ?? 0) < a.maxCount);
|
|
508
|
+
if (allowed)
|
|
509
|
+
consumed.set(allowed, (consumed.get(allowed) ?? 0) + 1);
|
|
510
|
+
else
|
|
511
|
+
failures.push({
|
|
512
|
+
kind,
|
|
513
|
+
service: entry.service,
|
|
514
|
+
method: entry.method,
|
|
515
|
+
pathFingerprint: entry.pathFingerprint,
|
|
516
|
+
operationId: entry.operationId,
|
|
517
|
+
status: entry.status,
|
|
518
|
+
journalReference: entry.id
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
for (const service of baseline.services) {
|
|
523
|
+
const d = diagnostics[service];
|
|
524
|
+
if (!d)
|
|
525
|
+
throw new Error(`missing assertion diagnostics for ${service}`);
|
|
526
|
+
if (d.activeRequests + d.activeConnections + d.blockedCommands + d.pendingWebhooks + (d.activeJobs ?? 0) > 0 || !options.allowCheckpointablePending && d.pendingJobs > 0)
|
|
527
|
+
failures.push({ kind: "leftover_work", service, diagnostics: d });
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
format: "mockingbird.assertions/v1",
|
|
531
|
+
clean: failures.length === 0,
|
|
532
|
+
namespace,
|
|
533
|
+
baseline: id,
|
|
534
|
+
failures: failures.slice(0, 100),
|
|
535
|
+
truncated: failures.length > 100,
|
|
536
|
+
inspectedThrough: this.sequence
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
startTrace(namespace, input, includeBodies = false, limitations = []) {
|
|
540
|
+
const baseline = validateArchive(input);
|
|
541
|
+
if (!baseline.namespaces[namespace])
|
|
542
|
+
throw new Error("trace baseline lacks selected namespace");
|
|
543
|
+
if (Buffer.byteLength(JSON.stringify(baseline)) > 8 * 1024 * 1024)
|
|
544
|
+
throw new Error("trace baseline exceeds 8 MiB limit");
|
|
545
|
+
if (this.traces.size >= 4 && !this.traces.has(namespace))
|
|
546
|
+
throw new Error("trace capacity reached");
|
|
547
|
+
const artifact = {
|
|
548
|
+
format: "mockingbird.trace/v1",
|
|
549
|
+
sourceProcess: this.processId,
|
|
550
|
+
namespace,
|
|
551
|
+
baseline: structuredClone(baseline),
|
|
552
|
+
events: [],
|
|
553
|
+
limitations: [...limitations],
|
|
554
|
+
truncated: false,
|
|
555
|
+
active: true
|
|
556
|
+
};
|
|
557
|
+
this.traces.set(namespace, { artifact, includeBodies, bytes: 0, inFlight: 0, next: 0 });
|
|
558
|
+
return structuredClone(artifact);
|
|
559
|
+
}
|
|
560
|
+
limitation(artifact, limitation) {
|
|
561
|
+
if (!artifact.limitations.includes(limitation))
|
|
562
|
+
artifact.limitations.push(limitation);
|
|
563
|
+
}
|
|
564
|
+
beginHTTP(namespace, service, request, logicalPath) {
|
|
565
|
+
const trace = this.traces.get(namespace);
|
|
566
|
+
if (!trace?.artifact.active)
|
|
567
|
+
return void 0;
|
|
568
|
+
const url = new URL(request.url), omitted = [];
|
|
569
|
+
if (logicalPath)
|
|
570
|
+
url.pathname = logicalPath;
|
|
571
|
+
for (const key of [...url.searchParams.keys()])
|
|
572
|
+
if (/(token|key|password|secret|signature|credential)/i.test(key)) {
|
|
573
|
+
url.searchParams.set(key, "[redacted]");
|
|
574
|
+
omitted.push("sensitive query fields redacted");
|
|
575
|
+
}
|
|
576
|
+
if ([...request.headers.keys()].some((h) => /authorization|api-key|token|secret|cookie/i.test(h)))
|
|
577
|
+
omitted.push("credential headers require explicit replay bindings");
|
|
578
|
+
const event = {
|
|
579
|
+
sequence: ++trace.next,
|
|
580
|
+
kind: "http",
|
|
581
|
+
service,
|
|
582
|
+
method: request.method,
|
|
583
|
+
path: `${url.pathname}${url.search}`,
|
|
584
|
+
contentType: request.headers.get("content-type") ?? "",
|
|
585
|
+
omitted
|
|
586
|
+
};
|
|
587
|
+
if (trace.inFlight++)
|
|
588
|
+
this.limitation(trace.artifact, "concurrent interactions are not certified for serial replay");
|
|
589
|
+
let body = Promise.resolve({ omitted: [] });
|
|
590
|
+
if (request.body) {
|
|
591
|
+
if (!trace.includeBodies)
|
|
592
|
+
body = Promise.resolve({ omitted: ["request body omitted"] });
|
|
593
|
+
else {
|
|
594
|
+
body = (async () => {
|
|
595
|
+
let timeout;
|
|
596
|
+
try {
|
|
597
|
+
let text = await Promise.race([
|
|
598
|
+
boundedText(request.clone().body, 65536),
|
|
599
|
+
new Promise((_, reject) => {
|
|
600
|
+
timeout = setTimeout(() => reject(new Error("capture deadline")), 100);
|
|
601
|
+
})
|
|
602
|
+
]);
|
|
603
|
+
const removed = [];
|
|
604
|
+
if (event.contentType?.includes("json")) {
|
|
605
|
+
text = JSON.stringify(JSON.parse(text), (key, value) => {
|
|
606
|
+
if (/(token|password|secret|credential|authorization|api.?key)/i.test(key)) {
|
|
607
|
+
removed.push("sensitive JSON body fields redacted");
|
|
608
|
+
return "[redacted]";
|
|
609
|
+
}
|
|
610
|
+
return value;
|
|
611
|
+
});
|
|
612
|
+
} else if (event.contentType?.includes("x-www-form-urlencoded")) {
|
|
613
|
+
const form = new URLSearchParams(text);
|
|
614
|
+
for (const key of [...form.keys()])
|
|
615
|
+
if (/(token|password|secret|credential|api.?key)/i.test(key)) {
|
|
616
|
+
form.set(key, "[redacted]");
|
|
617
|
+
removed.push("sensitive form fields redacted");
|
|
618
|
+
}
|
|
619
|
+
text = form.toString();
|
|
620
|
+
}
|
|
621
|
+
return { text, omitted: removed };
|
|
622
|
+
} catch {
|
|
623
|
+
return { omitted: ["request body capture truncated or unavailable"] };
|
|
624
|
+
} finally {
|
|
625
|
+
clearTimeout(timeout);
|
|
626
|
+
}
|
|
627
|
+
})();
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return { artifact: trace.artifact, event, body };
|
|
631
|
+
}
|
|
632
|
+
async finishHTTP(token, response) {
|
|
633
|
+
if (!token)
|
|
634
|
+
return;
|
|
635
|
+
const trace = this.traces.get(token.artifact.namespace);
|
|
636
|
+
if (!trace || trace.artifact !== token.artifact)
|
|
637
|
+
return;
|
|
638
|
+
const captured = await token.body;
|
|
639
|
+
token.event.status = response.status;
|
|
640
|
+
token.event.omitted.push(...captured.omitted);
|
|
641
|
+
if (captured.text !== void 0)
|
|
642
|
+
token.event.body = captured.text;
|
|
643
|
+
if (response.headers.get("content-type")?.includes("text/event-stream"))
|
|
644
|
+
token.event.omitted.push("streaming responses are unsupported");
|
|
645
|
+
else {
|
|
646
|
+
try {
|
|
647
|
+
const text = await boundedText(response.clone().body, 65536);
|
|
648
|
+
token.event.responseHash = hash(text);
|
|
649
|
+
} catch {
|
|
650
|
+
token.event.omitted.push("response hash unavailable or truncated");
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
trace.inFlight--;
|
|
654
|
+
for (const omitted of token.event.omitted)
|
|
655
|
+
this.limitation(trace.artifact, omitted);
|
|
656
|
+
this.append(trace, token.event);
|
|
657
|
+
}
|
|
658
|
+
append(trace, event) {
|
|
659
|
+
const bytes = Buffer.byteLength(JSON.stringify(event));
|
|
660
|
+
if (trace.artifact.events.length >= this.limits.traceEvents || trace.bytes + bytes > this.limits.traceBytes) {
|
|
661
|
+
trace.artifact.truncated = true;
|
|
662
|
+
trace.artifact.active = false;
|
|
663
|
+
this.limitation(trace.artifact, "recording limits reached");
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
trace.bytes += bytes;
|
|
667
|
+
trace.artifact.events.push(event);
|
|
668
|
+
trace.artifact.events.sort((a, b) => a.sequence - b.sequence);
|
|
669
|
+
}
|
|
670
|
+
unsupportedControl(namespace, action) {
|
|
671
|
+
const trace = this.traces.get(namespace);
|
|
672
|
+
if (trace?.artifact.active)
|
|
673
|
+
this.limitation(trace.artifact, `control not replayable: ${action}`);
|
|
674
|
+
}
|
|
675
|
+
control(namespace, action, controls) {
|
|
676
|
+
const trace = this.traces.get(namespace);
|
|
677
|
+
if (trace?.artifact.active)
|
|
678
|
+
this.append(trace, {
|
|
679
|
+
sequence: ++trace.next,
|
|
680
|
+
kind: "control",
|
|
681
|
+
action,
|
|
682
|
+
controls: structuredClone(controls),
|
|
683
|
+
omitted: []
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
trace(namespace, stop = false) {
|
|
687
|
+
const trace = this.traces.get(namespace);
|
|
688
|
+
if (!trace)
|
|
689
|
+
throw new Error("trace not started");
|
|
690
|
+
if (stop)
|
|
691
|
+
trace.artifact.active = false;
|
|
692
|
+
if (trace.inFlight)
|
|
693
|
+
this.limitation(trace.artifact, "requests still in flight when inspected");
|
|
694
|
+
return structuredClone(trace.artifact);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
// ../../adapters/node/dist/fleet-proxy.js
|
|
699
|
+
var ingressSupport = {
|
|
700
|
+
stripe: {
|
|
701
|
+
publicOrigin: true,
|
|
702
|
+
pathPrefix: true,
|
|
703
|
+
namespaces: "path-or-header",
|
|
704
|
+
streaming: true,
|
|
705
|
+
signing: "vendor request signing is not used; webhook signatures remain payload based"
|
|
706
|
+
},
|
|
707
|
+
other: {
|
|
708
|
+
publicOrigin: false,
|
|
709
|
+
reason: "host/path-sensitive signing and hosted URL rewriting are not certified; use a direct endpoint"
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
function advertisedHttpBase(entry) {
|
|
713
|
+
if (entry.publicPath !== void 0 && !entry.publicOrigin)
|
|
714
|
+
throw new Error("publicPath requires publicOrigin");
|
|
715
|
+
if (!entry.publicOrigin)
|
|
716
|
+
return void 0;
|
|
717
|
+
const url = new URL(entry.publicOrigin);
|
|
718
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.pathname !== "/" || url.search || url.hash)
|
|
719
|
+
throw new Error("publicOrigin must be an HTTP(S) origin without credentials or a path");
|
|
720
|
+
const prefix = entry.publicPath ?? "";
|
|
721
|
+
if (prefix && (!/^\/(?:[A-Za-z0-9_-]+\/?)*$/.test(prefix) || prefix.endsWith("/")))
|
|
722
|
+
throw new Error("publicPath must be a canonical path prefix without a trailing slash");
|
|
723
|
+
return `${url.origin}${prefix}`;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// ../../adapters/node/dist/fleet-time.js
|
|
727
|
+
var FleetTimeController = class {
|
|
728
|
+
context;
|
|
729
|
+
constructor(context) {
|
|
730
|
+
this.context = context;
|
|
731
|
+
}
|
|
732
|
+
async run(namespace, selected, body, single) {
|
|
733
|
+
const condition = body.condition ?? "no-events", maxEvents = single ? 1 : body.maxEvents ?? 100, maxAdvanceMs = body.maxAdvanceMs ?? 864e5, timeoutMs = body.timeoutMs ?? 5e3;
|
|
734
|
+
if (!["no-events", "idle", "checkpoint-ready"].includes(String(condition)) || typeof maxEvents !== "number" || !Number.isSafeInteger(maxEvents) || maxEvents < 1 || maxEvents > 1e4 || typeof maxAdvanceMs !== "number" || !Number.isFinite(maxAdvanceMs) || maxAdvanceMs < 0 || typeof timeoutMs !== "number" || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 3e4)
|
|
735
|
+
throw new Error("invalid bounded time-control condition or budgets");
|
|
736
|
+
const unsupported = [...selected].filter(([, child]) => !child.nextEvent).map(([name]) => name);
|
|
737
|
+
if (unsupported.length)
|
|
738
|
+
throw new Error(`next-event inspection unsupported for ${unsupported.join(", ")}`);
|
|
739
|
+
const clocks = [...selected.values()].map((child) => child.clock(namespace).state());
|
|
740
|
+
if (clocks.some((clock) => !clock.frozen || clock.now !== clocks[0]?.now))
|
|
741
|
+
throw new Error("freeze and align selected participant clocks before event stepping");
|
|
742
|
+
const start2 = clocks[0]?.now ?? this.context.clock(namespace).now(), deadline = Date.now() + timeoutMs;
|
|
743
|
+
const steps = [];
|
|
744
|
+
const diagnostics = () => Object.fromEntries([...selected].map(([name, child]) => [name, child.diagnostics(namespace)]));
|
|
745
|
+
const bounded = async (promise) => {
|
|
746
|
+
let timer;
|
|
747
|
+
try {
|
|
748
|
+
return await Promise.race([
|
|
749
|
+
promise,
|
|
750
|
+
new Promise((_, reject) => {
|
|
751
|
+
timer = setTimeout(() => reject(Object.assign(new Error("time-control host budget exceeded; recovery may be required"), { code: "MOCKINGBIRD_WORKLOAD_UNCERTAIN" })), Math.max(1, deadline - Date.now()));
|
|
752
|
+
})
|
|
753
|
+
]);
|
|
754
|
+
} finally {
|
|
755
|
+
clearTimeout(timer);
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
for (; ; ) {
|
|
759
|
+
const due = await bounded(Promise.all([...selected].map(async ([name, child]) => [name, await child.nextEvent?.(namespace)])));
|
|
760
|
+
if (due.some(([, time]) => time !== void 0 && !Number.isFinite(time)))
|
|
761
|
+
throw new Error("participant returned an invalid event time");
|
|
762
|
+
const services = diagnostics(), states = Object.values(services);
|
|
763
|
+
const ready = condition === "checkpoint-ready" ? states.every((d) => d.activeRequests + d.blockedCommands + d.pendingWebhooks + (d.activeJobs ?? 0) === 0) : condition === "idle" ? states.every((d) => d.activeRequests + d.blockedCommands + d.pendingWebhooks + d.pendingJobs === 0) : due.every(([, time]) => time === void 0);
|
|
764
|
+
if (!single && ready)
|
|
765
|
+
return Response.json({ status: "settled", namespace, condition, steps, services });
|
|
766
|
+
const times = due.flatMap(([, time]) => time === void 0 ? [] : [time]);
|
|
767
|
+
if (!times.length)
|
|
768
|
+
return Response.json({
|
|
769
|
+
status: "no-events",
|
|
770
|
+
namespace,
|
|
771
|
+
condition,
|
|
772
|
+
settled: ready,
|
|
773
|
+
steps,
|
|
774
|
+
services
|
|
775
|
+
});
|
|
776
|
+
const current = [...selected.values()][0]?.clock(namespace).now() ?? start2, target = Math.max(current, Math.min(...times));
|
|
777
|
+
if (Date.now() >= deadline || steps.length >= maxEvents || target - start2 > maxAdvanceMs)
|
|
778
|
+
return Response.json({ status: "budget-exhausted", namespace, condition, steps, services }, { status: 409 });
|
|
779
|
+
const signal = AbortSignal.timeout(Math.max(1, deadline - Date.now()));
|
|
780
|
+
const response = await this.context.transaction(namespace, selected, async (_name, child) => {
|
|
781
|
+
child.clock(namespace).set(target);
|
|
782
|
+
if (child.settle)
|
|
783
|
+
await bounded(child.settle(namespace, signal));
|
|
784
|
+
});
|
|
785
|
+
if (!response.ok)
|
|
786
|
+
return response;
|
|
787
|
+
this.context.clock(namespace).freeze();
|
|
788
|
+
this.context.clock(namespace).set(target);
|
|
789
|
+
try {
|
|
790
|
+
if (this.context.synchronize)
|
|
791
|
+
await bounded(this.context.synchronize({
|
|
792
|
+
namespace,
|
|
793
|
+
services: [...selected.keys()],
|
|
794
|
+
now: target,
|
|
795
|
+
signal
|
|
796
|
+
}));
|
|
797
|
+
} catch (error) {
|
|
798
|
+
const fences = [];
|
|
799
|
+
for (const child of selected.values())
|
|
800
|
+
try {
|
|
801
|
+
fences.push(child.lock(namespace));
|
|
802
|
+
} catch {
|
|
803
|
+
}
|
|
804
|
+
this.context.recovery.set(namespace, fences);
|
|
805
|
+
throw error;
|
|
806
|
+
}
|
|
807
|
+
const processed = due.filter(([, time]) => time !== void 0 && time <= target).map(([name]) => name);
|
|
808
|
+
steps.push({ now: target, processed });
|
|
809
|
+
this.context.phase(namespace, "stepping", steps.length, steps.length);
|
|
810
|
+
if (single)
|
|
811
|
+
return Response.json({
|
|
812
|
+
status: "stepped",
|
|
813
|
+
namespace,
|
|
814
|
+
steps,
|
|
815
|
+
services: diagnostics(),
|
|
816
|
+
clientClockSynchronization: this.context.synchronize ? "hook-invoked" : "external worker clocks are unchanged"
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
// ../../adapters/node/dist/fleet-management.js
|
|
823
|
+
var object2 = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
824
|
+
var fleetError = (status, message, extra = {}) => Response.json({
|
|
825
|
+
...extra,
|
|
826
|
+
error: {
|
|
827
|
+
type: "mockingbird_admin",
|
|
828
|
+
code: {
|
|
829
|
+
400: "invalid_request",
|
|
830
|
+
401: "unauthorized",
|
|
831
|
+
403: "forbidden",
|
|
832
|
+
404: "not_found",
|
|
833
|
+
409: "conflict",
|
|
834
|
+
413: "too_large",
|
|
835
|
+
429: "quota_exceeded",
|
|
836
|
+
500: "operation_failed",
|
|
837
|
+
503: "unavailable"
|
|
838
|
+
}[status] ?? "request_failed",
|
|
839
|
+
message
|
|
840
|
+
}
|
|
841
|
+
}, { status });
|
|
842
|
+
var checked = (body, keys) => {
|
|
843
|
+
const unknown = Object.keys(body).filter((k) => !keys.includes(k));
|
|
844
|
+
if (unknown.length)
|
|
845
|
+
throw new Error(`unknown fields: ${unknown.join(", ")}`);
|
|
846
|
+
};
|
|
847
|
+
var namePattern = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
848
|
+
var FleetManagement = class {
|
|
849
|
+
ctx;
|
|
850
|
+
operationContext = new AsyncLocalStorage();
|
|
851
|
+
interrupted = /* @__PURE__ */ new Map();
|
|
852
|
+
durable = /* @__PURE__ */ new Map();
|
|
853
|
+
async recoverOperations() {
|
|
854
|
+
if (!this.ctx.options.journal)
|
|
855
|
+
return;
|
|
856
|
+
if (!this.ctx.options.repository)
|
|
857
|
+
throw new Error("durable journal requires a portable archive repository");
|
|
858
|
+
let records;
|
|
859
|
+
try {
|
|
860
|
+
records = await this.ctx.options.journal.load();
|
|
861
|
+
} catch {
|
|
862
|
+
throw new Error("durable operation journal unavailable");
|
|
863
|
+
}
|
|
864
|
+
for (const record of records) {
|
|
865
|
+
const decision = await reconcileOperation(record, this.ctx.options.repository);
|
|
866
|
+
if (decision.status === "terminal")
|
|
867
|
+
continue;
|
|
868
|
+
if (decision.status === "abandoned-before-commit" || decision.status === "verified-publication") {
|
|
869
|
+
if (decision.status === "verified-publication")
|
|
870
|
+
await this.restoreArchive(await this.ctx.options.repository.get(decision.archive));
|
|
871
|
+
await this.ctx.options.journal.write({
|
|
872
|
+
...record,
|
|
873
|
+
revision: record.revision + 1,
|
|
874
|
+
phase: "resolved",
|
|
875
|
+
resolution: decision.status,
|
|
876
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
877
|
+
});
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
this.interrupted.set(record.operationId, record);
|
|
881
|
+
for (const namespace of record.scope) {
|
|
882
|
+
if (!this.namespaces.has(namespace)) {
|
|
883
|
+
const result = await this.createNamespace(namespace);
|
|
884
|
+
if (!result.ok)
|
|
885
|
+
throw new Error("cannot prepare interrupted namespace for recovery");
|
|
886
|
+
}
|
|
887
|
+
if (this.ctx.recovery.has(namespace))
|
|
888
|
+
continue;
|
|
889
|
+
const releases = [];
|
|
890
|
+
for (const child of this.ctx.children.values())
|
|
891
|
+
try {
|
|
892
|
+
releases.push(child.lock(namespace));
|
|
893
|
+
} catch {
|
|
894
|
+
}
|
|
895
|
+
this.ctx.recovery.set(namespace, releases);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
fenceNamespaces(scope) {
|
|
900
|
+
for (const namespace of scope) {
|
|
901
|
+
const releases = this.ctx.recovery.get(namespace) ?? [];
|
|
902
|
+
for (const child of this.ctx.children.values())
|
|
903
|
+
try {
|
|
904
|
+
releases.push(child.lock(namespace));
|
|
905
|
+
} catch {
|
|
906
|
+
}
|
|
907
|
+
this.ctx.recovery.set(namespace, releases);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
async journalPhase(operationId, phase, extra = {}) {
|
|
911
|
+
const current = this.durable.get(operationId);
|
|
912
|
+
if (!current || !this.ctx.options.journal)
|
|
913
|
+
return;
|
|
914
|
+
const next = {
|
|
915
|
+
...current,
|
|
916
|
+
...extra,
|
|
917
|
+
revision: current.revision + 1,
|
|
918
|
+
phase,
|
|
919
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
920
|
+
};
|
|
921
|
+
await this.ctx.options.journal.write(next);
|
|
922
|
+
this.durable.set(operationId, next);
|
|
923
|
+
}
|
|
924
|
+
observations;
|
|
925
|
+
requestPath(service, request) {
|
|
926
|
+
const prefix = this.ctx.config.services[service]?.adminPrefix ?? this.ctx.config.adminPrefix ?? "/__admin";
|
|
927
|
+
let path = new URL(request.url).pathname;
|
|
928
|
+
if (path.startsWith(`${prefix}/ns/`)) {
|
|
929
|
+
const tail = path.slice(`${prefix}/ns/`.length), slash = tail.indexOf("/");
|
|
930
|
+
path = slash < 0 ? "/" : tail.slice(slash);
|
|
931
|
+
}
|
|
932
|
+
return { path, admin: path === prefix || path.startsWith(`${prefix}/`) };
|
|
933
|
+
}
|
|
934
|
+
beginRequest(service, namespace, request) {
|
|
935
|
+
const route = this.requestPath(service, request);
|
|
936
|
+
return route.admin ? void 0 : this.observations.beginHTTP(namespace, service, request, route.path);
|
|
937
|
+
}
|
|
938
|
+
async finishRequest(service, namespace, _request, response, log, token) {
|
|
939
|
+
if (this.requestPath(service, _request).admin)
|
|
940
|
+
return;
|
|
941
|
+
const pathFingerprint = createHash3("sha256").update(log.path).digest("hex");
|
|
942
|
+
this.observations.record({
|
|
943
|
+
namespace,
|
|
944
|
+
service,
|
|
945
|
+
kind: "http",
|
|
946
|
+
method: log.method,
|
|
947
|
+
pathFingerprint,
|
|
948
|
+
status: log.status,
|
|
949
|
+
unmatched: log.unmatched,
|
|
950
|
+
fault: log.faultId !== void 0,
|
|
951
|
+
...log.operationId ? { operationId: log.operationId } : {}
|
|
952
|
+
});
|
|
953
|
+
await this.observations.finishHTTP(token, response);
|
|
954
|
+
}
|
|
955
|
+
observeDelivery(service, event) {
|
|
956
|
+
this.observations.record({
|
|
957
|
+
namespace: event.namespace,
|
|
958
|
+
service,
|
|
959
|
+
kind: "webhook",
|
|
960
|
+
deliveryState: event.state
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
instrumentation;
|
|
964
|
+
initializationToken = randomBytes(32).toString("base64url");
|
|
965
|
+
environmentContext = new AsyncLocalStorage();
|
|
966
|
+
environment;
|
|
967
|
+
ownsEnvironment() {
|
|
968
|
+
return this.environment !== void 0 && this.environmentContext.getStore() === this.environment.id;
|
|
969
|
+
}
|
|
970
|
+
topologyBusy(namespace) {
|
|
971
|
+
return this.topology.has(namespace) || this.environment !== void 0 && !this.ownsEnvironment();
|
|
972
|
+
}
|
|
973
|
+
environmentFence(namespace, service) {
|
|
974
|
+
return this.ownsEnvironment() && this.environment?.fences.get(namespace)?.has(service) === true;
|
|
975
|
+
}
|
|
976
|
+
checkCancelled(namespace) {
|
|
977
|
+
const id = this.operationId(namespace);
|
|
978
|
+
if (id && this.aborts.get(id)?.signal.aborted)
|
|
979
|
+
throw new Error("operation cancelled before commit");
|
|
980
|
+
}
|
|
981
|
+
activeOperation(namespace) {
|
|
982
|
+
const current = this.operationContext.getStore();
|
|
983
|
+
return current?.status === "running" ? current : [...this.operations.values()].find((operation) => operation.namespace === namespace && operation.status === "running");
|
|
984
|
+
}
|
|
985
|
+
operationId(namespace) {
|
|
986
|
+
return this.activeOperation(namespace)?.id;
|
|
987
|
+
}
|
|
988
|
+
async withEnvironment(namespaces, selected, run) {
|
|
989
|
+
if (this.ownsEnvironment())
|
|
990
|
+
return run();
|
|
991
|
+
if (this.environment || this.ctx.controlling.size || this.topology.size)
|
|
992
|
+
throw new Error("environment operation conflicts with active namespace work");
|
|
993
|
+
const env = { id: randomUUID3(), fences: /* @__PURE__ */ new Map() };
|
|
994
|
+
this.environment = env;
|
|
995
|
+
return this.environmentContext.run(env.id, async () => {
|
|
996
|
+
try {
|
|
997
|
+
for (const ns of namespaces) {
|
|
998
|
+
if (!this.namespaces.has(ns))
|
|
999
|
+
continue;
|
|
1000
|
+
await this.hook("prepare", ns, selected);
|
|
1001
|
+
const fences = /* @__PURE__ */ new Map();
|
|
1002
|
+
env.fences.set(ns, fences);
|
|
1003
|
+
for (const [name, child] of selected) {
|
|
1004
|
+
if (child.protocol !== "http" && !Object.hasOwn(child.namespaces.endpoints ?? {}, ns))
|
|
1005
|
+
continue;
|
|
1006
|
+
if (this.ctx.recovery.has(ns))
|
|
1007
|
+
continue;
|
|
1008
|
+
fences.set(name, child.lock(ns));
|
|
1009
|
+
}
|
|
1010
|
+
if (this.busy(ns, selected).length)
|
|
1011
|
+
throw new Error(`namespace ${ns} is not quiescent`);
|
|
1012
|
+
}
|
|
1013
|
+
return await run();
|
|
1014
|
+
} finally {
|
|
1015
|
+
for (const [ns, fences] of env.fences) {
|
|
1016
|
+
if (this.ctx.recovery.has(ns))
|
|
1017
|
+
this.ctx.recovery.get(ns)?.push(...fences.values());
|
|
1018
|
+
else
|
|
1019
|
+
for (const unlock of [...fences.values()].reverse())
|
|
1020
|
+
unlock();
|
|
1021
|
+
}
|
|
1022
|
+
this.environment = void 0;
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
operations = /* @__PURE__ */ new Map();
|
|
1027
|
+
namespaces = /* @__PURE__ */ new Map();
|
|
1028
|
+
credentials = /* @__PURE__ */ new Map();
|
|
1029
|
+
retries = /* @__PURE__ */ new Map();
|
|
1030
|
+
events = [];
|
|
1031
|
+
subscribers = /* @__PURE__ */ new Set();
|
|
1032
|
+
aborts = /* @__PURE__ */ new Map();
|
|
1033
|
+
topology = /* @__PURE__ */ new Set();
|
|
1034
|
+
privateRequests = /* @__PURE__ */ new Map();
|
|
1035
|
+
privateRequest(namespace, request) {
|
|
1036
|
+
const token = request.headers.get("x-mockingbird-initializer");
|
|
1037
|
+
return token !== null && (this.privateRequests.get(namespace) === token || this.lifecycle === "initializing" && token === this.initializationToken);
|
|
1038
|
+
}
|
|
1039
|
+
sequence = 0;
|
|
1040
|
+
timer;
|
|
1041
|
+
expiryTimer;
|
|
1042
|
+
backupRunning = false;
|
|
1043
|
+
backupStatus = {};
|
|
1044
|
+
lifecycle = "starting";
|
|
1045
|
+
constructor(ctx) {
|
|
1046
|
+
this.ctx = ctx;
|
|
1047
|
+
this.instrumentation = new FleetInstrumentation(ctx.options);
|
|
1048
|
+
this.observations = new FleetObservations(ctx.id);
|
|
1049
|
+
this.remember("default");
|
|
1050
|
+
for (const child of ctx.children.values())
|
|
1051
|
+
for (const ns of Object.keys(child.namespaces.endpoints ?? {}))
|
|
1052
|
+
this.remember(ns);
|
|
1053
|
+
for (const value of Object.values(ctx.options.limits ?? {}))
|
|
1054
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
1055
|
+
throw new Error("limits must be nonnegative integers");
|
|
1056
|
+
for (const child of ctx.children.values())
|
|
1057
|
+
child.setAdmission?.((namespace) => {
|
|
1058
|
+
const entry = this.namespaces.get(namespace);
|
|
1059
|
+
if (entry)
|
|
1060
|
+
entry.activityAt = Date.now();
|
|
1061
|
+
if (this.lifecycle === "initializing" || this.lifecycle === "draining" || this.lifecycle === "failed" || this.environment || this.topology.has(namespace) || this.ctx.recovery.has(namespace))
|
|
1062
|
+
return false;
|
|
1063
|
+
const limit = this.ctx.options.limits?.connections;
|
|
1064
|
+
if (limit === void 0)
|
|
1065
|
+
return true;
|
|
1066
|
+
let connections = 0;
|
|
1067
|
+
for (const c of ctx.children.values()) {
|
|
1068
|
+
if (c.protocol !== "http" && !Object.hasOwn(c.namespaces.endpoints ?? {}, namespace))
|
|
1069
|
+
continue;
|
|
1070
|
+
const d = c.diagnostics(namespace);
|
|
1071
|
+
connections += d.activeConnections + (c.protocol === "http" ? d.activeRequests : 0);
|
|
1072
|
+
}
|
|
1073
|
+
return connections < limit;
|
|
1074
|
+
});
|
|
1075
|
+
const retention = ctx.options.retention;
|
|
1076
|
+
if (retention?.maxCount !== void 0 && (!Number.isSafeInteger(retention.maxCount) || retention.maxCount < 1))
|
|
1077
|
+
throw new Error("retention maxCount must be positive");
|
|
1078
|
+
if (retention?.ttlMs !== void 0 && (!Number.isFinite(retention.ttlMs) || retention.ttlMs < 1))
|
|
1079
|
+
throw new Error("retention ttlMs must be positive");
|
|
1080
|
+
}
|
|
1081
|
+
remember(namespace) {
|
|
1082
|
+
if (!this.namespaces.has(namespace))
|
|
1083
|
+
this.namespaces.set(namespace, {
|
|
1084
|
+
generation: randomUUID3(),
|
|
1085
|
+
createdAt: Date.now(),
|
|
1086
|
+
activityAt: Date.now(),
|
|
1087
|
+
pinned: false
|
|
1088
|
+
});
|
|
1089
|
+
return this.namespaces.get(namespace);
|
|
1090
|
+
}
|
|
1091
|
+
generation(namespace) {
|
|
1092
|
+
return this.remember(namespace).generation;
|
|
1093
|
+
}
|
|
1094
|
+
admit(namespace, request) {
|
|
1095
|
+
if (this.privateRequest(namespace, request))
|
|
1096
|
+
return void 0;
|
|
1097
|
+
if (this.topology.has(namespace) || this.environment)
|
|
1098
|
+
return fleetError(409, "namespace lifecycle operation in progress", { namespace });
|
|
1099
|
+
if (this.lifecycle === "initializing" && request.headers.get("x-mockingbird-initializer") === this.initializationToken)
|
|
1100
|
+
return void 0;
|
|
1101
|
+
if (this.lifecycle !== "ready" && this.lifecycle !== "starting")
|
|
1102
|
+
return fleetError(503, `fleet is ${this.lifecycle}`);
|
|
1103
|
+
if (!this.namespaces.has(namespace) && this.ctx.options.limits?.maxNamespaces !== void 0 && this.namespaces.size + this.topology.size >= this.ctx.options.limits.maxNamespaces)
|
|
1104
|
+
return fleetError(429, "maximum namespaces reached");
|
|
1105
|
+
const entry = this.remember(namespace);
|
|
1106
|
+
entry.activityAt = Date.now();
|
|
1107
|
+
const expected = request.headers.get("x-mockingbird-generation");
|
|
1108
|
+
if (!["GET", "HEAD", "OPTIONS"].includes(request.method) && expected !== null && expected !== entry.generation)
|
|
1109
|
+
return fleetError(409, "stale namespace generation", { generation: entry.generation });
|
|
1110
|
+
const limits = this.ctx.options.limits;
|
|
1111
|
+
if (limits)
|
|
1112
|
+
for (const child of this.ctx.children.values()) {
|
|
1113
|
+
if (child.protocol !== "http" && !Object.hasOwn(child.namespaces.endpoints ?? {}, namespace))
|
|
1114
|
+
continue;
|
|
1115
|
+
const usage = child.diagnostics(namespace);
|
|
1116
|
+
if (limits.connections !== void 0 && usage.activeConnections + usage.activeRequests >= limits.connections)
|
|
1117
|
+
return fleetError(429, "namespace admission quota reached", { namespace });
|
|
1118
|
+
}
|
|
1119
|
+
return void 0;
|
|
1120
|
+
}
|
|
1121
|
+
emit(operation) {
|
|
1122
|
+
this.instrumentation.phase(operation, (operation.scope ?? [operation.namespace]).some((namespace) => this.ctx.recovery.has(namespace)));
|
|
1123
|
+
const event = {
|
|
1124
|
+
id: `${this.ctx.id}:${++this.sequence}`,
|
|
1125
|
+
namespace: operation.namespace,
|
|
1126
|
+
operation: structuredClone(operation)
|
|
1127
|
+
};
|
|
1128
|
+
this.events.push(event);
|
|
1129
|
+
if (this.events.length > 500)
|
|
1130
|
+
this.events.shift();
|
|
1131
|
+
for (const sub of this.subscribers)
|
|
1132
|
+
if (sub.namespace === operation.namespace) {
|
|
1133
|
+
const data = sub.member ? {
|
|
1134
|
+
id: event.id,
|
|
1135
|
+
namespace: event.namespace,
|
|
1136
|
+
operation: {
|
|
1137
|
+
id: operation.id,
|
|
1138
|
+
phase: operation.phase,
|
|
1139
|
+
status: operation.status,
|
|
1140
|
+
completed: operation.completed,
|
|
1141
|
+
total: operation.total
|
|
1142
|
+
}
|
|
1143
|
+
} : event;
|
|
1144
|
+
sub.push(`id: ${event.id}
|
|
1145
|
+
event: lifecycle
|
|
1146
|
+
data: ${JSON.stringify(data)}
|
|
1147
|
+
|
|
1148
|
+
`);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
phase(namespace, phase, completed, total, participant) {
|
|
1152
|
+
const operation = this.activeOperation(namespace);
|
|
1153
|
+
if (operation) {
|
|
1154
|
+
operation.phase = phase;
|
|
1155
|
+
if (participant)
|
|
1156
|
+
operation.participant = participant;
|
|
1157
|
+
else
|
|
1158
|
+
delete operation.participant;
|
|
1159
|
+
if (completed !== void 0)
|
|
1160
|
+
operation.completed = completed;
|
|
1161
|
+
if (total !== void 0)
|
|
1162
|
+
operation.total = total;
|
|
1163
|
+
this.emit(operation);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
prune() {
|
|
1167
|
+
const { maxCount = 50, ttlMs = 24 * 36e5 } = this.ctx.options.retention ?? {};
|
|
1168
|
+
const entries = [...this.ctx.checkpoints];
|
|
1169
|
+
for (const [id, point] of entries)
|
|
1170
|
+
if (Date.now() - Date.parse(point.createdAt ?? (/* @__PURE__ */ new Date()).toISOString()) > ttlMs)
|
|
1171
|
+
this.ctx.checkpoints.delete(id);
|
|
1172
|
+
while (this.ctx.checkpoints.size > maxCount)
|
|
1173
|
+
this.ctx.checkpoints.delete(this.ctx.checkpoints.keys().next().value);
|
|
1174
|
+
}
|
|
1175
|
+
select(body) {
|
|
1176
|
+
if (body.services === void 0)
|
|
1177
|
+
return this.ctx.children;
|
|
1178
|
+
if (!Array.isArray(body.services) || !body.services.length || body.services.some((n) => typeof n !== "string" || !this.ctx.children.has(n)))
|
|
1179
|
+
throw new Error("services must be a nonempty list of configured service names");
|
|
1180
|
+
return new Map(body.services.map((n) => [n, this.ctx.children.get(n)]));
|
|
1181
|
+
}
|
|
1182
|
+
scope(namespace, selected) {
|
|
1183
|
+
for (const [name, child] of selected)
|
|
1184
|
+
if (child.protocol !== "http" && !Object.hasOwn(child.namespaces.endpoints ?? {}, namespace))
|
|
1185
|
+
throw new Error(`namespace ${namespace} is not configured for ${name}`);
|
|
1186
|
+
}
|
|
1187
|
+
capability() {
|
|
1188
|
+
return {
|
|
1189
|
+
version: 1,
|
|
1190
|
+
archiveFormat: "mockingbird.fleet/v1",
|
|
1191
|
+
archiveProtection: this.ctx.options.archiveProtection?.policy ?? { format: "none" },
|
|
1192
|
+
processId: this.ctx.id,
|
|
1193
|
+
authentication: ["admin-key", "scoped-credential"],
|
|
1194
|
+
scopes: ["namespace", "services", "environment"],
|
|
1195
|
+
routes: [
|
|
1196
|
+
["GET", ""],
|
|
1197
|
+
["GET", "/namespaces"],
|
|
1198
|
+
["POST", "/namespaces/{namespace}/fork"],
|
|
1199
|
+
["GET", "/audit"],
|
|
1200
|
+
["GET", "/telemetry"],
|
|
1201
|
+
["GET", "/recovery"],
|
|
1202
|
+
["POST", "/recovery/{id}/rollback"],
|
|
1203
|
+
["POST", "/time/step-next"],
|
|
1204
|
+
["POST", "/time/run-until"],
|
|
1205
|
+
["POST", "/assertions/baseline"],
|
|
1206
|
+
["POST", "/assertions/{id}/check"],
|
|
1207
|
+
["POST", "/trace/start"],
|
|
1208
|
+
["POST", "/trace/stop"],
|
|
1209
|
+
["GET", "/trace"],
|
|
1210
|
+
["POST", "/archives/bindings"],
|
|
1211
|
+
["POST", "/namespaces/{namespace}"],
|
|
1212
|
+
["DELETE", "/namespaces/{namespace}"],
|
|
1213
|
+
["GET", "/namespaces/{namespace}/snapshots"],
|
|
1214
|
+
["POST", "/namespaces/{namespace}/snapshots"],
|
|
1215
|
+
["GET", "/namespaces/{namespace}/snapshots/{id}"],
|
|
1216
|
+
["DELETE", "/namespaces/{namespace}/snapshots/{id}"],
|
|
1217
|
+
["POST", "/namespaces/{namespace}/snapshots/{id}/restore"],
|
|
1218
|
+
["GET", "/metrics"],
|
|
1219
|
+
["GET", "/clock"],
|
|
1220
|
+
["POST", "/clock"],
|
|
1221
|
+
["POST", "/can-checkpoint"],
|
|
1222
|
+
["POST", "/wait-until-idle"],
|
|
1223
|
+
["POST", "/archives/export"],
|
|
1224
|
+
["POST", "/archives/import"],
|
|
1225
|
+
["POST", "/archives/inspect"],
|
|
1226
|
+
["POST", "/archives/diff"],
|
|
1227
|
+
["GET", "/operations"],
|
|
1228
|
+
["GET", "/operations/{id}"],
|
|
1229
|
+
["POST", "/operations/{id}/cancel"],
|
|
1230
|
+
["GET", "/events"],
|
|
1231
|
+
["POST", "/profiles/apply"],
|
|
1232
|
+
["POST", "/profiles/baseline"],
|
|
1233
|
+
["GET", "/scenarios"],
|
|
1234
|
+
["POST", "/scenarios/{name}/apply"],
|
|
1235
|
+
["GET", "/inspect"],
|
|
1236
|
+
["GET", "/diagnostics"],
|
|
1237
|
+
["POST", "/credentials"],
|
|
1238
|
+
["DELETE", "/credentials/{id}"],
|
|
1239
|
+
["POST", "/backups"],
|
|
1240
|
+
["GET", "/backups"],
|
|
1241
|
+
["GET", "/cleanup"],
|
|
1242
|
+
["POST", "/cleanup"],
|
|
1243
|
+
["POST", "/namespaces/{namespace}/policy"]
|
|
1244
|
+
].map(([method, path]) => ({ method, path })),
|
|
1245
|
+
services: Object.fromEntries([...this.ctx.children].map(([name, c]) => [
|
|
1246
|
+
name,
|
|
1247
|
+
{
|
|
1248
|
+
protocol: c.protocol,
|
|
1249
|
+
coverage: c.coverage,
|
|
1250
|
+
ingress: c.targetName === "stripe" ? ingressSupport.stripe : ingressSupport.other,
|
|
1251
|
+
target: c.targetName,
|
|
1252
|
+
outboundPolicy: c.outboundPolicy ?? "unsupported",
|
|
1253
|
+
version: 1,
|
|
1254
|
+
packageVersion: c.packageVersion,
|
|
1255
|
+
nextEvent: Boolean(c.nextEvent),
|
|
1256
|
+
externalWorkerClock: "requires explicit harness synchronization",
|
|
1257
|
+
checkpoint: true,
|
|
1258
|
+
portable: Boolean(c.exportCheckpoint && c.importCheckpoint),
|
|
1259
|
+
provisioning: Boolean(c.createNamespace && c.deleteNamespace),
|
|
1260
|
+
inspect: Boolean(c.inspect),
|
|
1261
|
+
controls: c.protocol === "http" ? ["clock", "faults", "latency", "seed"] : ["clock"],
|
|
1262
|
+
stalePrecondition: c.protocol === "http"
|
|
1263
|
+
}
|
|
1264
|
+
])),
|
|
1265
|
+
limits: {
|
|
1266
|
+
usage: {
|
|
1267
|
+
namespaces: this.namespaces.size,
|
|
1268
|
+
listeners: [...this.ctx.children.values()].reduce((n, c) => n + (c.protocol === "http" ? 1 : Object.keys(c.namespaces.endpoints ?? {}).length), 1)
|
|
1269
|
+
},
|
|
1270
|
+
headroom: {
|
|
1271
|
+
namespaces: this.ctx.options.limits?.maxNamespaces === void 0 ? null : this.ctx.options.limits.maxNamespaces - this.namespaces.size
|
|
1272
|
+
},
|
|
1273
|
+
checkpoints: this.ctx.options.retention?.maxCount ?? 50,
|
|
1274
|
+
eventHistory: 500,
|
|
1275
|
+
operationHistory: 200,
|
|
1276
|
+
idempotencyMs: 36e5,
|
|
1277
|
+
supportedBudgets: ["maxNamespaces", "maxListeners", "connections"],
|
|
1278
|
+
unsupportedBudgets: ["objectBytes", "records", "pendingJobs"],
|
|
1279
|
+
...this.ctx.options.limits
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
action(path, method) {
|
|
1284
|
+
if (["GET", "HEAD"].includes(method))
|
|
1285
|
+
return "read";
|
|
1286
|
+
if (/\/credentials/.test(path))
|
|
1287
|
+
return "credentials";
|
|
1288
|
+
if (/\/reset$/.test(path))
|
|
1289
|
+
return "reset";
|
|
1290
|
+
if (/\/restore$/.test(path) || path.endsWith("/archives/import"))
|
|
1291
|
+
return "restore";
|
|
1292
|
+
if (/\/snapshots/.test(path) || path.endsWith("/archives/export"))
|
|
1293
|
+
return "snapshot";
|
|
1294
|
+
if (/\/namespaces\/[^/]+$/.test(path))
|
|
1295
|
+
return "namespace";
|
|
1296
|
+
return "control";
|
|
1297
|
+
}
|
|
1298
|
+
auth(request, namespace, action, path) {
|
|
1299
|
+
const key = request.headers.get("x-mockingbird-admin-key");
|
|
1300
|
+
const adminKey = this.ctx.config.adminKey ?? process.env.MOCKINGBIRD_ADMIN_KEY;
|
|
1301
|
+
if (key !== null && (adminKey === void 0 || key !== adminKey)) {
|
|
1302
|
+
const credential = this.credentials.get(digest(key));
|
|
1303
|
+
if (credential && credential.expiresAt > Date.now()) {
|
|
1304
|
+
if (!credential.namespaces.includes(namespace) || !credential.actions.includes(action) || /\/(credentials|cleanup|backups)(\/|$)/.test(path) || ["/fleet", "/fleet/namespaces"].includes(path) || path === "/fleet/archives/import" || path === "/fleet/archives/export")
|
|
1305
|
+
return fleetError(403, "credential does not permit this scope or action");
|
|
1306
|
+
return credential.id;
|
|
1307
|
+
}
|
|
1308
|
+
return fleetError(401, "invalid or expired credential");
|
|
1309
|
+
}
|
|
1310
|
+
if (adminKey !== void 0 && key !== adminKey)
|
|
1311
|
+
return fleetError(401, "admin key required");
|
|
1312
|
+
return "admin";
|
|
1313
|
+
}
|
|
1314
|
+
async dispatch(request, fallback) {
|
|
1315
|
+
const cors = this.ctx.options.cors;
|
|
1316
|
+
const url = new URL(request.url), prefix = this.ctx.config.adminPrefix ?? "/__admin";
|
|
1317
|
+
const controlled = url.pathname === `${prefix}/fleet` || url.pathname.startsWith(`${prefix}/fleet/`) || url.pathname === `${prefix}/ui`;
|
|
1318
|
+
const origin = request.headers.get("origin");
|
|
1319
|
+
const allowed = controlled && cors !== void 0 && origin !== null && cors.origins.includes(origin);
|
|
1320
|
+
const methods = cors?.methods ?? ["GET", "POST", "DELETE", "OPTIONS"];
|
|
1321
|
+
const headers = cors?.headers ?? [
|
|
1322
|
+
"content-type",
|
|
1323
|
+
"x-mockingbird-admin-key",
|
|
1324
|
+
"x-mockingbird-namespace",
|
|
1325
|
+
"idempotency-key",
|
|
1326
|
+
"last-event-id"
|
|
1327
|
+
];
|
|
1328
|
+
if (controlled && cors && request.method === "OPTIONS" && origin !== null) {
|
|
1329
|
+
const method = request.headers.get("access-control-request-method");
|
|
1330
|
+
const requested = (request.headers.get("access-control-request-headers") ?? "").split(",").map((h) => h.trim().toLowerCase()).filter(Boolean);
|
|
1331
|
+
if (!allowed || !method || !methods.includes(method) || requested.some((h) => !headers.map((v) => v.toLowerCase()).includes(h)))
|
|
1332
|
+
return fleetError(403, "CORS preflight denied");
|
|
1333
|
+
return new Response(null, {
|
|
1334
|
+
status: 204,
|
|
1335
|
+
headers: {
|
|
1336
|
+
"access-control-allow-origin": origin,
|
|
1337
|
+
"access-control-allow-methods": methods.join(", "),
|
|
1338
|
+
"access-control-allow-headers": headers.join(", "),
|
|
1339
|
+
vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
const response = await this.dispatchControl(request, fallback);
|
|
1344
|
+
if (controlled && cors) {
|
|
1345
|
+
response.headers.append("vary", "Origin");
|
|
1346
|
+
if (allowed && origin)
|
|
1347
|
+
response.headers.set("access-control-allow-origin", origin);
|
|
1348
|
+
}
|
|
1349
|
+
return response;
|
|
1350
|
+
}
|
|
1351
|
+
async dispatchControl(request, fallback) {
|
|
1352
|
+
const url = new URL(request.url), prefix = this.ctx.config.adminPrefix ?? "/__admin";
|
|
1353
|
+
if (!url.pathname.startsWith(`${prefix}/`) && url.pathname !== prefix)
|
|
1354
|
+
return fallback();
|
|
1355
|
+
const path = url.pathname.slice(prefix.length).replace(/\/$/, "");
|
|
1356
|
+
if (path === "/live" && request.method === "GET")
|
|
1357
|
+
return Response.json({ status: this.lifecycle === "stopped" ? "stopped" : "alive", id: this.ctx.id }, { status: this.lifecycle === "stopped" ? 503 : 200 });
|
|
1358
|
+
if (path === "/health" && (this.lifecycle !== "ready" || this.ctx.controlling.size || this.ctx.recovery.size || this.environment))
|
|
1359
|
+
return Response.json({
|
|
1360
|
+
status: this.ctx.recovery.size ? "failed" : this.ctx.controlling.size || this.environment ? "maintenance" : this.lifecycle,
|
|
1361
|
+
alive: this.lifecycle !== "stopped",
|
|
1362
|
+
operations: [...this.ctx.controlling].map(([namespace2, operationId]) => ({
|
|
1363
|
+
namespace: namespace2,
|
|
1364
|
+
operationId
|
|
1365
|
+
})),
|
|
1366
|
+
fenced: [...this.ctx.recovery.keys()]
|
|
1367
|
+
}, { status: 503 });
|
|
1368
|
+
if (!path.startsWith("/fleet") && path !== "/ui")
|
|
1369
|
+
return fallback();
|
|
1370
|
+
let namespace;
|
|
1371
|
+
try {
|
|
1372
|
+
namespace = /^\/fleet\/namespaces\/([^/]+)/.test(path) ? decodeURIComponent(path.split("/")[3]) : url.searchParams.get("namespace") ?? request.headers.get("x-mockingbird-namespace") ?? this.ctx.config.namespace ?? "default";
|
|
1373
|
+
} catch {
|
|
1374
|
+
return fleetError(400, "invalid namespace encoding");
|
|
1375
|
+
}
|
|
1376
|
+
if (!namePattern.test(namespace))
|
|
1377
|
+
return fleetError(400, "invalid namespace");
|
|
1378
|
+
const action = this.action(path, request.method), caller = this.auth(request, namespace, action, path);
|
|
1379
|
+
const member = path === "/fleet/events" && request.method === "GET" && this.ctx.options.memberStatus === true;
|
|
1380
|
+
if (caller instanceof Response && !member) {
|
|
1381
|
+
const audited = await this.instrumentation.audit({
|
|
1382
|
+
actor: "unauthorized",
|
|
1383
|
+
namespace,
|
|
1384
|
+
action: `${request.method} ${this.action(path, request.method)}`,
|
|
1385
|
+
services: [],
|
|
1386
|
+
stage: "denied"
|
|
1387
|
+
});
|
|
1388
|
+
if (!audited)
|
|
1389
|
+
return fleetError(503, "required audit sink unavailable");
|
|
1390
|
+
return caller;
|
|
1391
|
+
}
|
|
1392
|
+
let body = {};
|
|
1393
|
+
if (!["GET", "HEAD", "DELETE"].includes(request.method)) {
|
|
1394
|
+
try {
|
|
1395
|
+
const text = await request.clone().text();
|
|
1396
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
1397
|
+
if (!object2(parsed))
|
|
1398
|
+
throw new Error();
|
|
1399
|
+
body = parsed;
|
|
1400
|
+
} catch {
|
|
1401
|
+
return fleetError(400, "expected JSON object");
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
this.prune();
|
|
1405
|
+
const mutating = !["GET", "HEAD"].includes(request.method);
|
|
1406
|
+
const run = async () => {
|
|
1407
|
+
let operation;
|
|
1408
|
+
if (mutating && !path.includes("/operations/") && !path.endsWith("/can-checkpoint") && !path.endsWith("/wait-until-idle")) {
|
|
1409
|
+
operation = {
|
|
1410
|
+
id: randomUUID3(),
|
|
1411
|
+
scope: body.allNamespaces === true ? [...this.namespaces.keys()] : [namespace],
|
|
1412
|
+
actor: typeof caller === "string" ? caller : "member",
|
|
1413
|
+
services: Array.isArray(body.services) && body.services.every((s) => typeof s === "string" && this.ctx.children.has(s)) ? body.services : [...this.ctx.children.keys()],
|
|
1414
|
+
namespace,
|
|
1415
|
+
action: `${request.method} ${path}`,
|
|
1416
|
+
phase: "validating",
|
|
1417
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1418
|
+
status: "running",
|
|
1419
|
+
completed: 0,
|
|
1420
|
+
total: this.ctx.children.size
|
|
1421
|
+
};
|
|
1422
|
+
this.operations.set(operation.id, operation);
|
|
1423
|
+
this.aborts.set(operation.id, new AbortController());
|
|
1424
|
+
this.emit(operation);
|
|
1425
|
+
}
|
|
1426
|
+
const execute = async () => {
|
|
1427
|
+
try {
|
|
1428
|
+
if (operation && !await this.instrumentation.audit({
|
|
1429
|
+
actor: operation.actor ?? "admin",
|
|
1430
|
+
namespace,
|
|
1431
|
+
action,
|
|
1432
|
+
services: operation.services ?? [],
|
|
1433
|
+
operationId: operation.id,
|
|
1434
|
+
stage: "attempt"
|
|
1435
|
+
}))
|
|
1436
|
+
throw new Error("required audit sink unavailable before mutation");
|
|
1437
|
+
if (operation && this.ctx.options.journal && !path.startsWith("/fleet/recovery") && !path.includes("/assertions/") && !path.startsWith("/fleet/trace")) {
|
|
1438
|
+
if (!this.ctx.options.repository)
|
|
1439
|
+
throw new Error("durable journal requires a repository");
|
|
1440
|
+
let before;
|
|
1441
|
+
if (this.namespaces.has(namespace) && !this.ctx.recovery.has(namespace)) {
|
|
1442
|
+
const archive = await this.capture(body.allNamespaces === true ? [...this.namespaces.keys()] : [namespace], this.select(body));
|
|
1443
|
+
before = await this.ctx.options.repository.put(archive);
|
|
1444
|
+
await this.ctx.options.repository.get(before);
|
|
1445
|
+
}
|
|
1446
|
+
const record = {
|
|
1447
|
+
version: 1,
|
|
1448
|
+
revision: 1,
|
|
1449
|
+
operationId: operation.id,
|
|
1450
|
+
controller: this.ctx.id,
|
|
1451
|
+
namespace,
|
|
1452
|
+
scope: body.allNamespaces === true ? [...this.namespaces.keys()] : [namespace],
|
|
1453
|
+
action,
|
|
1454
|
+
phase: "prepared",
|
|
1455
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1456
|
+
requestFingerprint: digest({ path, body, method: request.method }),
|
|
1457
|
+
...before ? { before } : {}
|
|
1458
|
+
};
|
|
1459
|
+
await this.ctx.options.journal.write(record);
|
|
1460
|
+
this.durable.set(operation.id, record);
|
|
1461
|
+
await this.journalPhase(operation.id, "committing");
|
|
1462
|
+
}
|
|
1463
|
+
const result = await this.handle(request, path, namespace, body, caller === "admin" && (!member || request.headers.has("x-mockingbird-admin-key"))) ?? await fallback();
|
|
1464
|
+
if (operation) {
|
|
1465
|
+
operation.status = this.aborts.get(operation.id)?.signal.aborted ? "cancelled" : result.ok ? "succeeded" : "failed";
|
|
1466
|
+
operation.phase = operation.status;
|
|
1467
|
+
operation.finishedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1468
|
+
const payload = await result.clone().json().catch(() => void 0);
|
|
1469
|
+
operation.result = object2(payload) ? Object.fromEntries(Object.entries(payload).filter(([key2]) => [
|
|
1470
|
+
"status",
|
|
1471
|
+
"id",
|
|
1472
|
+
"namespace",
|
|
1473
|
+
"failedService",
|
|
1474
|
+
"rollback",
|
|
1475
|
+
"rolledBack",
|
|
1476
|
+
"error",
|
|
1477
|
+
"sha256"
|
|
1478
|
+
].includes(key2))) : { status: result.status };
|
|
1479
|
+
if (result.ok)
|
|
1480
|
+
operation.completed = operation.total;
|
|
1481
|
+
await this.journalPhase(operation.id, result.ok ? "succeeded" : this.ctx.recovery.has(namespace) ? "recovery-required" : "failed");
|
|
1482
|
+
if (result.ok && (action === "reset" || action === "restore")) {
|
|
1483
|
+
for (const record of this.interrupted.values())
|
|
1484
|
+
if (record.scope.includes(namespace)) {
|
|
1485
|
+
const scope2 = record.scope.filter((ns) => ns !== namespace);
|
|
1486
|
+
const resolved = {
|
|
1487
|
+
...record,
|
|
1488
|
+
scope: scope2,
|
|
1489
|
+
revision: record.revision + 1,
|
|
1490
|
+
phase: scope2.length ? "recovery-required" : "resolved",
|
|
1491
|
+
resolution: `explicit-${action}`,
|
|
1492
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1493
|
+
};
|
|
1494
|
+
try {
|
|
1495
|
+
await this.ctx.options.journal?.write(resolved);
|
|
1496
|
+
} catch (error) {
|
|
1497
|
+
this.fenceNamespaces(record.scope);
|
|
1498
|
+
throw error;
|
|
1499
|
+
}
|
|
1500
|
+
if (scope2.length)
|
|
1501
|
+
this.interrupted.set(record.operationId, resolved);
|
|
1502
|
+
else
|
|
1503
|
+
this.interrupted.delete(record.operationId);
|
|
1504
|
+
}
|
|
1505
|
+
await this.ctx.publish();
|
|
1506
|
+
}
|
|
1507
|
+
if (!await this.instrumentation.audit({
|
|
1508
|
+
actor: operation.actor ?? "admin",
|
|
1509
|
+
namespace,
|
|
1510
|
+
action,
|
|
1511
|
+
services: operation.services ?? [],
|
|
1512
|
+
operationId: operation.id,
|
|
1513
|
+
stage: "terminal",
|
|
1514
|
+
result: operation.status === "succeeded" ? "succeeded" : operation.status === "cancelled" ? "cancelled" : "failed"
|
|
1515
|
+
})) {
|
|
1516
|
+
const fences = [];
|
|
1517
|
+
for (const name of operation.services ?? [])
|
|
1518
|
+
try {
|
|
1519
|
+
const release = this.ctx.children.get(name)?.lock(namespace);
|
|
1520
|
+
if (release)
|
|
1521
|
+
fences.push(release);
|
|
1522
|
+
} catch {
|
|
1523
|
+
}
|
|
1524
|
+
this.ctx.recovery.set(namespace, fences);
|
|
1525
|
+
await this.journalPhase(operation.id, "recovery-required");
|
|
1526
|
+
throw new Error("required terminal audit unavailable; recovery required");
|
|
1527
|
+
}
|
|
1528
|
+
result.headers.set("x-mockingbird-operation-id", operation.id);
|
|
1529
|
+
this.emit(operation);
|
|
1530
|
+
if (result.ok && request.method !== "DELETE" && (action === "restore" || action === "reset" || action === "namespace"))
|
|
1531
|
+
this.remember(namespace).generation = randomUUID3();
|
|
1532
|
+
}
|
|
1533
|
+
if (result.ok && request.method === "POST") {
|
|
1534
|
+
if (path === "/fleet/clock")
|
|
1535
|
+
this.observations.control(namespace, "clock", body);
|
|
1536
|
+
else if (/^\/fleet\/namespaces\/[^/]+\/reset$/.test(path))
|
|
1537
|
+
this.observations.control(namespace, "reset", body);
|
|
1538
|
+
else if (!path.startsWith("/fleet/trace") && !path.includes("/assertions/"))
|
|
1539
|
+
this.observations.unsupportedControl(namespace, action);
|
|
1540
|
+
}
|
|
1541
|
+
for (const point of this.ctx.checkpoints.values())
|
|
1542
|
+
if (!point.createdAt) {
|
|
1543
|
+
point.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1544
|
+
if (body.label !== void 0)
|
|
1545
|
+
point.label = body.label;
|
|
1546
|
+
}
|
|
1547
|
+
this.prune();
|
|
1548
|
+
if (this.ctx.recovery.size)
|
|
1549
|
+
await this.ctx.publish();
|
|
1550
|
+
return result;
|
|
1551
|
+
} catch (error) {
|
|
1552
|
+
if (operation && ["committing", "uploading", "publishing"].includes(this.durable.get(operation.id)?.phase ?? "")) {
|
|
1553
|
+
const releases = this.ctx.recovery.get(namespace) ?? [];
|
|
1554
|
+
for (const child of this.ctx.children.values())
|
|
1555
|
+
try {
|
|
1556
|
+
releases.push(child.lock(namespace));
|
|
1557
|
+
} catch {
|
|
1558
|
+
}
|
|
1559
|
+
this.ctx.recovery.set(namespace, releases);
|
|
1560
|
+
}
|
|
1561
|
+
if (operation) {
|
|
1562
|
+
operation.status = "failed";
|
|
1563
|
+
operation.phase = "failed";
|
|
1564
|
+
operation.finishedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1565
|
+
operation.result = {
|
|
1566
|
+
message: error instanceof Error ? error.message : "operation failed"
|
|
1567
|
+
};
|
|
1568
|
+
this.emit(operation);
|
|
1569
|
+
}
|
|
1570
|
+
if (this.ctx.recovery.size)
|
|
1571
|
+
await this.ctx.publish().catch(() => {
|
|
1572
|
+
});
|
|
1573
|
+
return fleetError(400, error instanceof Error ? error.message : "invalid control");
|
|
1574
|
+
} finally {
|
|
1575
|
+
if (operation)
|
|
1576
|
+
this.aborts.delete(operation.id);
|
|
1577
|
+
while (this.operations.size > 200) {
|
|
1578
|
+
const entry = [...this.operations].find(([, o]) => o.status !== "running");
|
|
1579
|
+
if (!entry)
|
|
1580
|
+
break;
|
|
1581
|
+
this.operations.delete(entry[0]);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
};
|
|
1585
|
+
return operation ? this.operationContext.run(operation, execute) : execute();
|
|
1586
|
+
};
|
|
1587
|
+
const key = request.headers.get("idempotency-key");
|
|
1588
|
+
if (!key || !mutating)
|
|
1589
|
+
return run();
|
|
1590
|
+
if (key.length > 256)
|
|
1591
|
+
return fleetError(400, "idempotency key too long");
|
|
1592
|
+
for (const [id, kept] of this.retries)
|
|
1593
|
+
if (kept.expires < Date.now())
|
|
1594
|
+
this.retries.delete(id);
|
|
1595
|
+
const scope = `${typeof caller === "string" ? caller : "member"}:${namespace}:${key}`;
|
|
1596
|
+
const fingerprint = digest({ path, method: request.method, body });
|
|
1597
|
+
const existing = this.retries.get(scope);
|
|
1598
|
+
if (existing)
|
|
1599
|
+
return existing.fingerprint === fingerprint ? (await existing.response).clone() : fleetError(409, "idempotency key reused with different request");
|
|
1600
|
+
if (this.retries.size >= 1e3)
|
|
1601
|
+
return fleetError(429, "idempotency retention capacity reached");
|
|
1602
|
+
const response = Promise.resolve().then(run);
|
|
1603
|
+
this.retries.set(scope, { fingerprint, expires: Date.now() + 36e5, response });
|
|
1604
|
+
return (await response).clone();
|
|
1605
|
+
}
|
|
1606
|
+
async handle(request, path, namespace, body, admin) {
|
|
1607
|
+
const route = path.slice("/fleet".length), method = request.method;
|
|
1608
|
+
if (path === "/ui")
|
|
1609
|
+
return new Response(fleetConsole(this.ctx.config.adminPrefix ?? "/__admin"), {
|
|
1610
|
+
headers: { "content-type": "text/html; charset=utf-8" }
|
|
1611
|
+
});
|
|
1612
|
+
if (route === "" && method === "GET")
|
|
1613
|
+
return Response.json({
|
|
1614
|
+
...this.capability(),
|
|
1615
|
+
manifest: this.ctx.manifest(),
|
|
1616
|
+
namespaces: this.namespaceStatus()
|
|
1617
|
+
});
|
|
1618
|
+
if (route === "/recovery" && method === "GET")
|
|
1619
|
+
return admin ? Response.json({
|
|
1620
|
+
version: 1,
|
|
1621
|
+
interrupted: [...this.interrupted.values()],
|
|
1622
|
+
resumable: Boolean(this.ctx.options.journal)
|
|
1623
|
+
}) : fleetError(403, "recovery administration required");
|
|
1624
|
+
const recovery = /^\/recovery\/([^/]+)\/rollback$/.exec(route);
|
|
1625
|
+
if (recovery && method === "POST") {
|
|
1626
|
+
checked(body, []);
|
|
1627
|
+
if (!admin)
|
|
1628
|
+
return fleetError(403, "recovery administration required");
|
|
1629
|
+
const record = this.interrupted.get(recovery[1]);
|
|
1630
|
+
if (!record?.before || !this.ctx.options.repository || !this.ctx.options.journal)
|
|
1631
|
+
throw new Error("interrupted operation has no verified portable before-state; explicit namespace reset is required");
|
|
1632
|
+
const beforeArchive = await this.ctx.options.repository.get(record.before);
|
|
1633
|
+
const { sha256: _identity, ...content } = beforeArchive;
|
|
1634
|
+
await this.restoreArchive(sealArchive({
|
|
1635
|
+
...content,
|
|
1636
|
+
namespaces: Object.fromEntries(Object.entries(beforeArchive.namespaces).filter(([namespace2]) => record.scope.includes(namespace2)))
|
|
1637
|
+
}));
|
|
1638
|
+
try {
|
|
1639
|
+
await this.ctx.options.journal.write({
|
|
1640
|
+
...record,
|
|
1641
|
+
revision: record.revision + 1,
|
|
1642
|
+
phase: "resolved",
|
|
1643
|
+
resolution: "explicit-restore-before",
|
|
1644
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1645
|
+
});
|
|
1646
|
+
} catch (error) {
|
|
1647
|
+
this.fenceNamespaces(record.scope);
|
|
1648
|
+
throw error;
|
|
1649
|
+
}
|
|
1650
|
+
this.interrupted.delete(record.operationId);
|
|
1651
|
+
await this.ctx.publish();
|
|
1652
|
+
return Response.json({
|
|
1653
|
+
status: "resolved",
|
|
1654
|
+
operationId: record.operationId,
|
|
1655
|
+
resolution: "restored-before-state"
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
if (route === "/telemetry" && method === "GET")
|
|
1659
|
+
return admin ? Response.json(this.instrumentation.snapshot()) : fleetError(403, "operator telemetry requires administrator scope");
|
|
1660
|
+
if (route === "/audit" && method === "GET")
|
|
1661
|
+
return admin ? Response.json(this.instrumentation.auditTrail()) : fleetError(403, "audit trail requires administrator scope");
|
|
1662
|
+
if (route === "/namespaces" && method === "GET")
|
|
1663
|
+
return Response.json({ namespaces: this.namespaceStatus() });
|
|
1664
|
+
const fork = /^\/namespaces\/[^/]+\/fork$/.test(route);
|
|
1665
|
+
if (fork && method === "POST") {
|
|
1666
|
+
checked(body, ["destination", "bindings"]);
|
|
1667
|
+
if (!admin)
|
|
1668
|
+
return fleetError(403, "namespace fork requires administrator scope");
|
|
1669
|
+
if (typeof body.destination !== "string" || !namePattern.test(body.destination))
|
|
1670
|
+
throw new Error("destination namespace required");
|
|
1671
|
+
const destination2 = body.destination;
|
|
1672
|
+
if (this.namespaces.has(destination2))
|
|
1673
|
+
return fleetError(409, "destination namespace already exists");
|
|
1674
|
+
return this.withEnvironment([namespace, destination2], this.ctx.children, async () => {
|
|
1675
|
+
let archive = await this.capture([namespace]);
|
|
1676
|
+
let bindings = {
|
|
1677
|
+
changes: [],
|
|
1678
|
+
limitations: ["opaque namespace-bearing keys and application credentials are unchanged"]
|
|
1679
|
+
};
|
|
1680
|
+
if (body.bindings !== void 0) {
|
|
1681
|
+
const prepared = await planRestoreBindings(archive, body.bindings, this.ctx.options.resolveArchiveSecret);
|
|
1682
|
+
archive = prepared.archive;
|
|
1683
|
+
bindings = prepared.report;
|
|
1684
|
+
}
|
|
1685
|
+
await this.restoreArchive(rebaseArchiveNamespace(archive, namespace, destination2));
|
|
1686
|
+
return Response.json({
|
|
1687
|
+
namespace: destination2,
|
|
1688
|
+
generation: this.generation(destination2),
|
|
1689
|
+
manifest: this.ctx.manifest(),
|
|
1690
|
+
bindings,
|
|
1691
|
+
connections: Object.fromEntries([...this.ctx.children].map(([name, child]) => [
|
|
1692
|
+
name,
|
|
1693
|
+
child.connections?.[destination2] ?? `${child.url}${this.ctx.config.adminPrefix ?? "/__admin"}/ns/${destination2}`
|
|
1694
|
+
]))
|
|
1695
|
+
}, { status: 201 });
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
if (route === "/archives/bindings" && method === "POST") {
|
|
1699
|
+
if (!admin)
|
|
1700
|
+
return fleetError(403, "restore binding plans require administrator scope");
|
|
1701
|
+
checked(body, ["archive", "plan"]);
|
|
1702
|
+
const prepared = await planRestoreBindings(body.archive, body.plan, this.ctx.options.resolveArchiveSecret);
|
|
1703
|
+
return Response.json(prepared.report);
|
|
1704
|
+
}
|
|
1705
|
+
if (route === "/assertions/baseline" && method === "POST") {
|
|
1706
|
+
checked(body, ["services"]);
|
|
1707
|
+
const selected = this.select(body);
|
|
1708
|
+
this.scope(namespace, selected);
|
|
1709
|
+
return Response.json(this.observations.baseline(namespace, [...selected.keys()]));
|
|
1710
|
+
}
|
|
1711
|
+
const assertion = /^\/assertions\/([^/]+)\/check$/.exec(route);
|
|
1712
|
+
if (assertion && method === "POST") {
|
|
1713
|
+
checked(body, ["allowances", "allowCheckpointablePending"]);
|
|
1714
|
+
if (body.allowCheckpointablePending !== void 0 && typeof body.allowCheckpointablePending !== "boolean")
|
|
1715
|
+
throw new Error("allowCheckpointablePending must be boolean");
|
|
1716
|
+
const diagnostics = Object.fromEntries([...this.ctx.children].filter(([, child]) => child.protocol === "http" || Object.hasOwn(child.namespaces.endpoints ?? {}, namespace)).map(([name, child]) => [name, child.diagnostics(namespace)]));
|
|
1717
|
+
const report = this.observations.assertClean(assertion[1], namespace, diagnostics, body);
|
|
1718
|
+
return Response.json(report, { status: report.clean ? 200 : 409 });
|
|
1719
|
+
}
|
|
1720
|
+
if (route.startsWith("/trace")) {
|
|
1721
|
+
if (!admin)
|
|
1722
|
+
return fleetError(403, "trace capture requires administrator scope");
|
|
1723
|
+
if (!this.ctx.options.trace)
|
|
1724
|
+
return fleetError(400, "trace capture is not enabled");
|
|
1725
|
+
if (route === "/trace/start" && method === "POST") {
|
|
1726
|
+
checked(body, ["includeBodies"]);
|
|
1727
|
+
if (body.includeBodies !== void 0 && typeof body.includeBodies !== "boolean")
|
|
1728
|
+
throw new Error("includeBodies must be boolean");
|
|
1729
|
+
const baseline = await this.capture([namespace]);
|
|
1730
|
+
const limitations = [...this.ctx.children.values()].some((child) => child.protocol !== "http") ? ["protocol/external interactions are not captured"] : [];
|
|
1731
|
+
return Response.json(this.observations.startTrace(namespace, baseline, body.includeBodies === true, limitations));
|
|
1732
|
+
}
|
|
1733
|
+
if (route === "/trace" && method === "GET")
|
|
1734
|
+
return Response.json(this.observations.trace(namespace));
|
|
1735
|
+
if (route === "/trace/stop" && method === "POST") {
|
|
1736
|
+
checked(body, []);
|
|
1737
|
+
return Response.json(this.observations.trace(namespace, true));
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
const nsRoute = /^\/namespaces\/([^/]+)$/.exec(route);
|
|
1741
|
+
if (nsRoute && method === "POST") {
|
|
1742
|
+
checked(body, ["baseline"]);
|
|
1743
|
+
if (body.baseline !== void 0 && typeof body.baseline !== "string")
|
|
1744
|
+
throw new Error("baseline must be a checkpoint id");
|
|
1745
|
+
return this.createNamespace(namespace, body.baseline);
|
|
1746
|
+
}
|
|
1747
|
+
if (nsRoute && method === "DELETE")
|
|
1748
|
+
return this.deleteNamespace(namespace);
|
|
1749
|
+
if (/^\/namespaces\/[^/]+\/policy$/.test(route) && method === "POST") {
|
|
1750
|
+
checked(body, ["pinned", "expiresInMs"]);
|
|
1751
|
+
if (body.pinned !== void 0 && typeof body.pinned !== "boolean")
|
|
1752
|
+
throw new Error("pinned must be boolean");
|
|
1753
|
+
if (body.expiresInMs !== void 0 && (typeof body.expiresInMs !== "number" || !Number.isSafeInteger(body.expiresInMs) || body.expiresInMs < 1 || body.expiresInMs > 864e5))
|
|
1754
|
+
throw new Error("expiry must be within one host day");
|
|
1755
|
+
if (body.pinned === void 0 && body.expiresInMs === void 0)
|
|
1756
|
+
throw new Error("namespace policy required");
|
|
1757
|
+
if (typeof body.pinned === "boolean")
|
|
1758
|
+
this.remember(namespace).pinned = body.pinned;
|
|
1759
|
+
if (typeof body.expiresInMs === "number") {
|
|
1760
|
+
this.remember(namespace).expiresAt = Date.now() + body.expiresInMs;
|
|
1761
|
+
if (!this.expiryTimer) {
|
|
1762
|
+
this.expiryTimer = setInterval(() => {
|
|
1763
|
+
for (const entry of this.cleanupPreview())
|
|
1764
|
+
if (entry.eligible)
|
|
1765
|
+
void this.deleteNamespace(entry.namespace).catch(() => {
|
|
1766
|
+
});
|
|
1767
|
+
}, Math.max(10, Math.min(6e4, body.expiresInMs)));
|
|
1768
|
+
this.expiryTimer.unref?.();
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
return Response.json({ namespace, ...this.remember(namespace) });
|
|
1772
|
+
}
|
|
1773
|
+
const snapshot = /^\/namespaces\/[^/]+\/snapshots(?:\/([^/]+))?$/.exec(route);
|
|
1774
|
+
if (snapshot && (method === "GET" || method === "DELETE")) {
|
|
1775
|
+
const info = (id, p) => ({
|
|
1776
|
+
id,
|
|
1777
|
+
namespace: p.namespace,
|
|
1778
|
+
label: p.label,
|
|
1779
|
+
createdAt: p.createdAt,
|
|
1780
|
+
services: [...p.values.keys()],
|
|
1781
|
+
portable: [...p.values.keys()].every((s) => Boolean(this.ctx.children.get(s)?.exportCheckpoint))
|
|
1782
|
+
});
|
|
1783
|
+
if (!snapshot[1]) {
|
|
1784
|
+
if (method !== "GET")
|
|
1785
|
+
return fleetError(405, "select a checkpoint to delete");
|
|
1786
|
+
return Response.json({
|
|
1787
|
+
snapshots: [...this.ctx.checkpoints].filter(([, p]) => p.namespace === namespace).map(([id, p]) => info(id, p))
|
|
1788
|
+
});
|
|
1789
|
+
}
|
|
1790
|
+
const point = this.ctx.checkpoints.get(snapshot[1]);
|
|
1791
|
+
if (!point || point.namespace !== namespace)
|
|
1792
|
+
return fleetError(404, "no checkpoint in this namespace");
|
|
1793
|
+
if (method === "DELETE")
|
|
1794
|
+
this.ctx.checkpoints.delete(snapshot[1]);
|
|
1795
|
+
return Response.json(method === "DELETE" ? { status: "deleted", id: snapshot[1] } : info(snapshot[1], point));
|
|
1796
|
+
}
|
|
1797
|
+
if ((route === "/time/step-next" || route === "/time/run-until") && method === "POST") {
|
|
1798
|
+
checked(body, ["services", "condition", "maxEvents", "maxAdvanceMs", "timeoutMs"]);
|
|
1799
|
+
const selected = this.select(body);
|
|
1800
|
+
this.scope(namespace, selected);
|
|
1801
|
+
const controller = new FleetTimeController({
|
|
1802
|
+
clock: this.ctx.clock,
|
|
1803
|
+
transaction: this.ctx.transaction,
|
|
1804
|
+
phase: (ns, phase, done, total) => this.phase(ns, phase, done, total),
|
|
1805
|
+
recovery: this.ctx.recovery,
|
|
1806
|
+
...this.ctx.options.synchronizeClock ? { synchronize: this.ctx.options.synchronizeClock } : {}
|
|
1807
|
+
});
|
|
1808
|
+
return controller.run(namespace, selected, body, route === "/time/step-next");
|
|
1809
|
+
}
|
|
1810
|
+
if (route === "/can-checkpoint" && method === "POST") {
|
|
1811
|
+
checked(body, ["services", "timeoutMs"]);
|
|
1812
|
+
if (body.timeoutMs !== void 0 && (typeof body.timeoutMs !== "number" || body.timeoutMs < 0 || body.timeoutMs > 3e4))
|
|
1813
|
+
throw new Error("timeoutMs must be between 0 and 30000");
|
|
1814
|
+
const selected = this.select(body);
|
|
1815
|
+
this.scope(namespace, selected);
|
|
1816
|
+
const until = Date.now() + (body.timeoutMs ?? 0);
|
|
1817
|
+
for (; ; ) {
|
|
1818
|
+
const services = Object.fromEntries([...selected].map(([n, c]) => {
|
|
1819
|
+
const d = c.diagnostics(namespace);
|
|
1820
|
+
return [
|
|
1821
|
+
n,
|
|
1822
|
+
{
|
|
1823
|
+
...d,
|
|
1824
|
+
checkpointReady: d.activeRequests + d.blockedCommands + d.pendingWebhooks + (d.activeJobs ?? 0) === 0,
|
|
1825
|
+
idle: d.activeRequests + d.blockedCommands + d.pendingWebhooks + d.pendingJobs === 0
|
|
1826
|
+
}
|
|
1827
|
+
];
|
|
1828
|
+
}));
|
|
1829
|
+
const ready = !this.ctx.controlling.has(namespace) && !this.ctx.recovery.has(namespace) && Object.values(services).every((d) => d.checkpointReady);
|
|
1830
|
+
if (ready || Date.now() >= until)
|
|
1831
|
+
return Response.json({ namespace, checkpointReady: ready, services }, { status: ready ? 200 : 409 });
|
|
1832
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
if (route === "/operations" && method === "GET")
|
|
1836
|
+
return Response.json({
|
|
1837
|
+
operations: [...this.operations.values()].filter((o) => o.namespace === namespace)
|
|
1838
|
+
});
|
|
1839
|
+
const operationRoute = /^\/operations\/([^/]+)(\/cancel)?$/.exec(route);
|
|
1840
|
+
if (operationRoute) {
|
|
1841
|
+
const op = this.operations.get(operationRoute[1]);
|
|
1842
|
+
if (!op || op.namespace !== namespace)
|
|
1843
|
+
return fleetError(404, "operation not found");
|
|
1844
|
+
if (operationRoute[2] && method === "POST") {
|
|
1845
|
+
checked(body, []);
|
|
1846
|
+
if (op.status !== "running" || !["validating", "preparing", "checkpointing"].includes(op.phase))
|
|
1847
|
+
return fleetError(409, "operation has passed cancellable phase");
|
|
1848
|
+
this.aborts.get(op.id)?.abort();
|
|
1849
|
+
return Response.json({ id: op.id, cancellationRequested: true });
|
|
1850
|
+
}
|
|
1851
|
+
if (method === "GET")
|
|
1852
|
+
return Response.json(op);
|
|
1853
|
+
}
|
|
1854
|
+
if (route === "/events" && method === "GET") {
|
|
1855
|
+
const cursor = request.headers.get("last-event-id");
|
|
1856
|
+
const encode = new TextEncoder();
|
|
1857
|
+
let subscription;
|
|
1858
|
+
let heartbeat;
|
|
1859
|
+
const cleanup = () => {
|
|
1860
|
+
if (subscription)
|
|
1861
|
+
this.subscribers.delete(subscription);
|
|
1862
|
+
clearInterval(heartbeat);
|
|
1863
|
+
};
|
|
1864
|
+
const stream = new ReadableStream({
|
|
1865
|
+
start: (controller) => {
|
|
1866
|
+
subscription = {
|
|
1867
|
+
namespace,
|
|
1868
|
+
member: this.ctx.options.memberStatus === true && !admin,
|
|
1869
|
+
push: (event) => {
|
|
1870
|
+
if ((controller.desiredSize ?? 0) < -32) {
|
|
1871
|
+
cleanup();
|
|
1872
|
+
controller.close();
|
|
1873
|
+
} else
|
|
1874
|
+
controller.enqueue(encode.encode(event));
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
this.subscribers.add(subscription);
|
|
1878
|
+
const at = this.events.findIndex((e) => e.id === cursor);
|
|
1879
|
+
if (cursor === null || at < 0)
|
|
1880
|
+
subscription.push(`event: resync
|
|
1881
|
+
data: ${JSON.stringify({ processId: this.ctx.id, namespace, generation: this.generation(namespace), lifecycle: this.lifecycle })}
|
|
1882
|
+
|
|
1883
|
+
`);
|
|
1884
|
+
for (const event of this.events.slice(at < 0 ? 0 : at + 1))
|
|
1885
|
+
if (event.namespace === namespace) {
|
|
1886
|
+
const data = this.ctx.options.memberStatus && !admin ? {
|
|
1887
|
+
id: event.id,
|
|
1888
|
+
namespace,
|
|
1889
|
+
operation: {
|
|
1890
|
+
id: event.operation.id,
|
|
1891
|
+
phase: event.operation.phase,
|
|
1892
|
+
status: event.operation.status,
|
|
1893
|
+
completed: event.operation.completed,
|
|
1894
|
+
total: event.operation.total
|
|
1895
|
+
}
|
|
1896
|
+
} : event;
|
|
1897
|
+
subscription.push(`id: ${event.id}
|
|
1898
|
+
event: lifecycle
|
|
1899
|
+
data: ${JSON.stringify(data)}
|
|
1900
|
+
|
|
1901
|
+
`);
|
|
1902
|
+
}
|
|
1903
|
+
heartbeat = setInterval(() => subscription.push(": heartbeat\n\n"), 15e3);
|
|
1904
|
+
heartbeat.unref?.();
|
|
1905
|
+
request.signal.addEventListener("abort", () => {
|
|
1906
|
+
cleanup();
|
|
1907
|
+
try {
|
|
1908
|
+
controller.close();
|
|
1909
|
+
} catch {
|
|
1910
|
+
}
|
|
1911
|
+
}, { once: true });
|
|
1912
|
+
},
|
|
1913
|
+
cancel: cleanup
|
|
1914
|
+
});
|
|
1915
|
+
return new Response(stream, {
|
|
1916
|
+
headers: { "content-type": "text/event-stream", "cache-control": "no-cache" }
|
|
1917
|
+
});
|
|
1918
|
+
}
|
|
1919
|
+
if (route === "/archives/export" && method === "POST") {
|
|
1920
|
+
checked(body, ["services", "allNamespaces"]);
|
|
1921
|
+
if (body.allNamespaces !== void 0 && typeof body.allNamespaces !== "boolean")
|
|
1922
|
+
throw new Error("allNamespaces must be boolean");
|
|
1923
|
+
const archive = await this.capture(body.allNamespaces === true ? [...this.namespaces.keys()] : [namespace], this.select(body));
|
|
1924
|
+
return Response.json(this.ctx.options.archiveProtection ? await this.ctx.options.archiveProtection.seal(archive) : archive);
|
|
1925
|
+
}
|
|
1926
|
+
if (route === "/archives/import" && method === "POST") {
|
|
1927
|
+
checked(body, ["archive", "namespace", "bindings"]);
|
|
1928
|
+
let archive = this.ctx.options.archiveProtection ? await this.ctx.options.archiveProtection.open(body.archive) : validateArchive(body.archive);
|
|
1929
|
+
if (body.namespace !== void 0 && (typeof body.namespace !== "string" || !namePattern.test(body.namespace)))
|
|
1930
|
+
throw new Error("invalid selected archive namespace");
|
|
1931
|
+
if (body.bindings !== void 0)
|
|
1932
|
+
archive = (await planRestoreBindings(archive, body.bindings, this.ctx.options.resolveArchiveSecret)).archive;
|
|
1933
|
+
await this.restoreArchive(archive, body.namespace);
|
|
1934
|
+
return Response.json({
|
|
1935
|
+
status: "restored",
|
|
1936
|
+
sha256: archive.sha256,
|
|
1937
|
+
namespaces: body.namespace ? [body.namespace] : Object.keys(archive.namespaces)
|
|
1938
|
+
});
|
|
1939
|
+
}
|
|
1940
|
+
if (route === "/archives/inspect" && method === "POST") {
|
|
1941
|
+
checked(body, ["archive"]);
|
|
1942
|
+
return Response.json(inspectArchive(body.archive));
|
|
1943
|
+
}
|
|
1944
|
+
if (route === "/archives/diff" && method === "POST") {
|
|
1945
|
+
checked(body, ["left", "right", "namespace", "service", "values", "limit"]);
|
|
1946
|
+
if (body.values !== void 0 && typeof body.values !== "boolean" || body.limit !== void 0 && (typeof body.limit !== "number" || !Number.isInteger(body.limit) || body.limit < 1 || body.limit > 1e3))
|
|
1947
|
+
throw new Error("invalid diff options");
|
|
1948
|
+
return Response.json(diffArchives(body.left, body.right, body));
|
|
1949
|
+
}
|
|
1950
|
+
if (route === "/profiles/apply" && method === "POST") {
|
|
1951
|
+
checked(body, ["name", "profile"]);
|
|
1952
|
+
const profile = typeof body.name === "string" ? this.ctx.options.profiles?.[body.name] : body.profile;
|
|
1953
|
+
return this.applyProfile(namespace, profile);
|
|
1954
|
+
}
|
|
1955
|
+
if (route === "/profiles/baseline" && method === "POST") {
|
|
1956
|
+
checked(body, ["services"]);
|
|
1957
|
+
return this.applyProfile(namespace, Object.fromEntries([...this.select(body)].map(([name, c]) => [
|
|
1958
|
+
name,
|
|
1959
|
+
c.protocol === "http" ? { faults: [], latency: 0 } : {}
|
|
1960
|
+
])));
|
|
1961
|
+
}
|
|
1962
|
+
if (route === "/scenarios" && method === "GET")
|
|
1963
|
+
return Response.json({
|
|
1964
|
+
scenarios: Object.entries(this.ctx.options.scenarios ?? {}).map(([name, b]) => ({
|
|
1965
|
+
name,
|
|
1966
|
+
format: b.format,
|
|
1967
|
+
version: b.version,
|
|
1968
|
+
description: b.description,
|
|
1969
|
+
services: Object.keys(b.services)
|
|
1970
|
+
}))
|
|
1971
|
+
});
|
|
1972
|
+
const scenario = /^\/scenarios\/([^/]+)\/apply$/.exec(route);
|
|
1973
|
+
if (scenario && method === "POST") {
|
|
1974
|
+
checked(body, []);
|
|
1975
|
+
const bundle = this.ctx.options.scenarios?.[scenario[1]];
|
|
1976
|
+
if (bundle?.format !== "mockingbird.scenario/v1" || !Number.isSafeInteger(bundle.version) || bundle.version < 1)
|
|
1977
|
+
throw new Error("unknown or incompatible scenario bundle");
|
|
1978
|
+
for (const [name, entry] of Object.entries(bundle.services))
|
|
1979
|
+
if (this.ctx.children.get(name)?.protocol !== entry.protocol)
|
|
1980
|
+
throw new Error(`incompatible scenario service ${name}`);
|
|
1981
|
+
for (const [name, entry] of Object.entries(bundle.services))
|
|
1982
|
+
if (entry.fixtures !== void 0) {
|
|
1983
|
+
const child = this.ctx.children.get(name);
|
|
1984
|
+
if (entry.protocol !== "http" || !child?.importCheckpoint)
|
|
1985
|
+
throw new Error(`scenario fixtures unsupported for ${name}`);
|
|
1986
|
+
await child.importCheckpoint(namespace, {
|
|
1987
|
+
format: "mockingbird.snapshot/v1",
|
|
1988
|
+
service: name,
|
|
1989
|
+
records: entry.fixtures
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
const profile = Object.fromEntries(Object.entries(bundle.services).map(([name, entry]) => [
|
|
1993
|
+
name,
|
|
1994
|
+
{
|
|
1995
|
+
...entry.controls,
|
|
1996
|
+
...bundle.seed !== void 0 && entry.protocol === "http" ? { seed: bundle.seed } : {},
|
|
1997
|
+
...bundle.clock ? { clock: bundle.clock } : {}
|
|
1998
|
+
}
|
|
1999
|
+
]));
|
|
2000
|
+
const response = await this.applyProfile(namespace, profile, async (name, child) => {
|
|
2001
|
+
const fixtures = bundle.services[name]?.fixtures;
|
|
2002
|
+
if (fixtures !== void 0) {
|
|
2003
|
+
if (!child.configure)
|
|
2004
|
+
throw new Error(`fixtures unsupported for ${name}`);
|
|
2005
|
+
const document = { format: "mockingbird.snapshot/v1", service: name, records: fixtures };
|
|
2006
|
+
await child.configure(namespace, { snapshot: document });
|
|
2007
|
+
}
|
|
2008
|
+
}, bundle.seedApplication);
|
|
2009
|
+
return response;
|
|
2010
|
+
}
|
|
2011
|
+
if (route === "/inspect" && method === "GET") {
|
|
2012
|
+
const url = new URL(request.url), service = url.searchParams.get("service");
|
|
2013
|
+
const child = service ? this.ctx.children.get(service) : void 0;
|
|
2014
|
+
if (!child?.inspect)
|
|
2015
|
+
return fleetError(400, "service inspection is unsupported");
|
|
2016
|
+
const offset = Number(url.searchParams.get("offset") ?? 0), limit = Number(url.searchParams.get("limit") ?? 100);
|
|
2017
|
+
if (!Number.isInteger(offset) || offset < 0 || !Number.isInteger(limit) || limit < 1 || limit > 1e3)
|
|
2018
|
+
throw new Error("invalid pagination");
|
|
2019
|
+
this.scope(namespace, /* @__PURE__ */ new Map([[service, child]]));
|
|
2020
|
+
const collection = url.searchParams.get("collection");
|
|
2021
|
+
return Response.json({
|
|
2022
|
+
namespace,
|
|
2023
|
+
service,
|
|
2024
|
+
result: await child.inspect(namespace, {
|
|
2025
|
+
offset,
|
|
2026
|
+
limit,
|
|
2027
|
+
...collection !== null ? { collection } : {}
|
|
2028
|
+
})
|
|
2029
|
+
});
|
|
2030
|
+
}
|
|
2031
|
+
if (route === "/diagnostics" && method === "GET") {
|
|
2032
|
+
const selected = new URL(request.url).searchParams.getAll("service");
|
|
2033
|
+
const children = selected.length ? this.select({ services: selected }) : this.ctx.children;
|
|
2034
|
+
this.scope(namespace, children);
|
|
2035
|
+
return Response.json({
|
|
2036
|
+
format: "mockingbird.diagnostics/v1",
|
|
2037
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2038
|
+
processId: this.ctx.id,
|
|
2039
|
+
namespace,
|
|
2040
|
+
generation: this.generation(namespace),
|
|
2041
|
+
configurationFingerprint: digest(this.ctx.config),
|
|
2042
|
+
capabilities: this.capability(),
|
|
2043
|
+
services: Object.fromEntries([...children].map(([name, c]) => [
|
|
2044
|
+
name,
|
|
2045
|
+
{
|
|
2046
|
+
protocol: c.protocol,
|
|
2047
|
+
diagnostics: c.diagnostics(namespace),
|
|
2048
|
+
unmatched: c.unmatched?.(namespace, 50)
|
|
2049
|
+
}
|
|
2050
|
+
])),
|
|
2051
|
+
operations: [...this.operations.values()].filter((o) => o.namespace === namespace).slice(-50).map(({ id, phase, action, status, startedAt, finishedAt }) => ({
|
|
2052
|
+
id,
|
|
2053
|
+
phase,
|
|
2054
|
+
action,
|
|
2055
|
+
status,
|
|
2056
|
+
startedAt,
|
|
2057
|
+
finishedAt
|
|
2058
|
+
})),
|
|
2059
|
+
truncated: [...this.operations.values()].filter((o) => o.namespace === namespace).length > 50,
|
|
2060
|
+
excluded: ["credentials", "requestBodies", "records", "modelText", "operationPayloads"]
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
2063
|
+
if (route === "/credentials" && method === "POST") {
|
|
2064
|
+
checked(body, ["namespaces", "actions", "ttlMs"]);
|
|
2065
|
+
if (!admin)
|
|
2066
|
+
return fleetError(403, "admin credential required");
|
|
2067
|
+
if (!Array.isArray(body.namespaces) || !body.namespaces.length || body.namespaces.some((n) => typeof n !== "string" || !namePattern.test(n)) || !Array.isArray(body.actions) || !body.actions.length || body.actions.some((a) => !["read", "reset", "restore", "snapshot", "control"].includes(String(a))) || typeof body.ttlMs !== "number" || body.ttlMs <= 0 || body.ttlMs > 864e5)
|
|
2068
|
+
throw new Error("expected namespaces, actions and ttlMs (at most one day)");
|
|
2069
|
+
const credential2 = randomBytes(32).toString("base64url"), id = randomUUID3(), expiresAt = Date.now() + body.ttlMs;
|
|
2070
|
+
this.credentials.set(digest(credential2), {
|
|
2071
|
+
id,
|
|
2072
|
+
namespaces: body.namespaces,
|
|
2073
|
+
actions: body.actions,
|
|
2074
|
+
expiresAt
|
|
2075
|
+
});
|
|
2076
|
+
return Response.json({ id, credential: credential2, expiresAt }, { status: 201 });
|
|
2077
|
+
}
|
|
2078
|
+
const credential = /^\/credentials\/([^/]+)$/.exec(route);
|
|
2079
|
+
if (credential && method === "DELETE") {
|
|
2080
|
+
if (!admin)
|
|
2081
|
+
return fleetError(403, "admin credential required");
|
|
2082
|
+
for (const [key, c] of this.credentials)
|
|
2083
|
+
if (c.id === credential[1]) {
|
|
2084
|
+
this.credentials.delete(key);
|
|
2085
|
+
return Response.json({ status: "revoked", id: c.id });
|
|
2086
|
+
}
|
|
2087
|
+
return fleetError(404, "credential not found");
|
|
2088
|
+
}
|
|
2089
|
+
if (route === "/backups" && method === "GET")
|
|
2090
|
+
return Response.json({
|
|
2091
|
+
...this.backupStatus,
|
|
2092
|
+
running: this.backupRunning,
|
|
2093
|
+
archives: await this.ctx.options.repository?.list() ?? []
|
|
2094
|
+
});
|
|
2095
|
+
if (route === "/backups" && method === "POST") {
|
|
2096
|
+
checked(body, ["allNamespaces"]);
|
|
2097
|
+
return Response.json(await this.backup(body.allNamespaces === true ? [...this.namespaces.keys()] : [namespace], [...this.operations.values()].reverse().find((o) => o.status === "running" && o.namespace === namespace)?.id));
|
|
2098
|
+
}
|
|
2099
|
+
if (route === "/cleanup" && method === "GET")
|
|
2100
|
+
return Response.json({ candidates: this.cleanupPreview() });
|
|
2101
|
+
if (route === "/cleanup" && method === "POST") {
|
|
2102
|
+
checked(body, []);
|
|
2103
|
+
const results = {};
|
|
2104
|
+
for (const entry of this.cleanupPreview())
|
|
2105
|
+
if (entry.eligible) {
|
|
2106
|
+
try {
|
|
2107
|
+
if (this.ctx.options.expiry?.finalBackup)
|
|
2108
|
+
await this.backup([entry.namespace]);
|
|
2109
|
+
const response = await this.deleteNamespace(entry.namespace);
|
|
2110
|
+
results[entry.namespace] = { status: response.status, result: await response.json() };
|
|
2111
|
+
} catch (error) {
|
|
2112
|
+
results[entry.namespace] = {
|
|
2113
|
+
status: "failed",
|
|
2114
|
+
message: error instanceof Error ? error.message : "cleanup failed"
|
|
2115
|
+
};
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
return Response.json({ results });
|
|
2119
|
+
}
|
|
2120
|
+
return void 0;
|
|
2121
|
+
}
|
|
2122
|
+
namespaceStatus() {
|
|
2123
|
+
return [...this.namespaces].map(([namespace, state]) => ({
|
|
2124
|
+
namespace,
|
|
2125
|
+
...state,
|
|
2126
|
+
status: this.ctx.recovery.has(namespace) ? "failed" : this.ctx.controlling.has(namespace) || this.topology.has(namespace) ? "maintenance" : "ready",
|
|
2127
|
+
operationId: this.ctx.controlling.get(namespace),
|
|
2128
|
+
expiresAt: state.expiresAt ?? (this.ctx.options.expiry ? Math.max(state.activityAt, ...[...this.ctx.children.values()].map((c) => c.lastActivity?.(namespace) ?? 0)) + this.ctx.options.expiry.idleMs : void 0)
|
|
2129
|
+
}));
|
|
2130
|
+
}
|
|
2131
|
+
busy(namespace, selected = this.ctx.children) {
|
|
2132
|
+
return [...selected].filter(([, c]) => {
|
|
2133
|
+
const d = c.diagnostics(namespace);
|
|
2134
|
+
return d.activeRequests + d.blockedCommands + d.pendingWebhooks + (d.activeJobs ?? 0) > 0;
|
|
2135
|
+
}).map(([name]) => name);
|
|
2136
|
+
}
|
|
2137
|
+
async hook(phase, namespace, selected) {
|
|
2138
|
+
const hook = this.ctx.options.workload?.[phase];
|
|
2139
|
+
const op = this.activeOperation(namespace);
|
|
2140
|
+
this.phase(namespace, phase === "prepare" ? "preparing" : phase === "verify" ? "verifying" : "resuming");
|
|
2141
|
+
const controller = op ? this.aborts.get(op.id) : new AbortController();
|
|
2142
|
+
if (controller?.signal.aborted && phase !== "resume")
|
|
2143
|
+
throw new Error("operation cancelled before commit");
|
|
2144
|
+
if (!hook)
|
|
2145
|
+
return;
|
|
2146
|
+
const timeout = AbortSignal.timeout(this.ctx.options.hookTimeoutMs ?? 3e4);
|
|
2147
|
+
const signal = controller && !controller.signal.aborted ? AbortSignal.any([controller.signal, timeout]) : timeout;
|
|
2148
|
+
await Promise.race([
|
|
2149
|
+
hook({
|
|
2150
|
+
namespace,
|
|
2151
|
+
services: [...selected.keys()],
|
|
2152
|
+
signal,
|
|
2153
|
+
operationId: op?.id ?? randomUUID3()
|
|
2154
|
+
}),
|
|
2155
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error(`${phase} hook aborted or timed out`)), { once: true }))
|
|
2156
|
+
]);
|
|
2157
|
+
}
|
|
2158
|
+
async createNamespace(namespace, baseline) {
|
|
2159
|
+
if (this.environment && !this.ownsEnvironment())
|
|
2160
|
+
return fleetError(409, "environment operation in progress");
|
|
2161
|
+
if (this.namespaces.has(namespace) || this.topology.has(namespace))
|
|
2162
|
+
return fleetError(409, "namespace already exists or is provisioning");
|
|
2163
|
+
if (this.ctx.controlling.has(namespace))
|
|
2164
|
+
return fleetError(409, "namespace operation in progress", {
|
|
2165
|
+
operationId: this.ctx.controlling.get(namespace)
|
|
2166
|
+
});
|
|
2167
|
+
const limits = this.ctx.options.limits;
|
|
2168
|
+
if (limits?.maxNamespaces !== void 0 && this.namespaces.size + this.topology.size >= limits.maxNamespaces)
|
|
2169
|
+
return fleetError(429, "maximum namespaces reached");
|
|
2170
|
+
const protocolCount = [...this.ctx.children.values()].filter((c) => c.protocol !== "http").length;
|
|
2171
|
+
const listenerCount = [...this.ctx.children.values()].reduce((n, c) => n + (c.protocol === "external" ? 0 : c.protocol === "http" ? 1 : Object.keys(c.namespaces.endpoints ?? {}).length), 1);
|
|
2172
|
+
if (limits?.maxListeners !== void 0 && listenerCount + (this.topology.size + 1) * protocolCount > limits.maxListeners)
|
|
2173
|
+
return fleetError(429, "maximum listeners reached");
|
|
2174
|
+
for (const [name, c] of this.ctx.children)
|
|
2175
|
+
if (!c.createNamespace || !c.deleteNamespace)
|
|
2176
|
+
throw new Error(`namespace provisioning unsupported for ${name}`);
|
|
2177
|
+
const point = baseline ? this.ctx.checkpoints.get(baseline) : void 0;
|
|
2178
|
+
if (baseline && !point)
|
|
2179
|
+
throw new Error("baseline checkpoint not found");
|
|
2180
|
+
this.topology.add(namespace);
|
|
2181
|
+
const created = [];
|
|
2182
|
+
try {
|
|
2183
|
+
this.phase(namespace, "provisioning");
|
|
2184
|
+
const docs = /* @__PURE__ */ new Map();
|
|
2185
|
+
if (point)
|
|
2186
|
+
for (const [name, value] of point.values) {
|
|
2187
|
+
const child = this.ctx.children.get(name);
|
|
2188
|
+
if (!child?.exportCheckpoint || !child.importCheckpoint)
|
|
2189
|
+
throw new Error(`portable baseline unsupported for ${name}`);
|
|
2190
|
+
docs.set(name, await child.exportCheckpoint(point.namespace, value));
|
|
2191
|
+
}
|
|
2192
|
+
for (const [name, child] of this.ctx.children) {
|
|
2193
|
+
await child.createNamespace?.(namespace);
|
|
2194
|
+
created.push(child);
|
|
2195
|
+
if (docs.has(name))
|
|
2196
|
+
await child.restore(namespace, await child.importCheckpoint?.(namespace, docs.get(name)));
|
|
2197
|
+
}
|
|
2198
|
+
if (point)
|
|
2199
|
+
restoreClock(this.ctx.clock(namespace), point.clock);
|
|
2200
|
+
this.remember(namespace);
|
|
2201
|
+
await this.ctx.publish();
|
|
2202
|
+
return Response.json({
|
|
2203
|
+
namespace,
|
|
2204
|
+
generation: this.generation(namespace),
|
|
2205
|
+
manifest: this.ctx.manifest(),
|
|
2206
|
+
connections: Object.fromEntries([...this.ctx.children].map(([n, c]) => [
|
|
2207
|
+
n,
|
|
2208
|
+
c.connections?.[namespace] ?? `${c.url}${this.ctx.config.adminPrefix ?? "/__admin"}/ns/${encodeURIComponent(namespace)}`
|
|
2209
|
+
]))
|
|
2210
|
+
}, { status: 201 });
|
|
2211
|
+
} catch (error) {
|
|
2212
|
+
const results = await Promise.allSettled(created.reverse().map((c) => c.deleteNamespace?.(namespace)));
|
|
2213
|
+
this.namespaces.delete(namespace);
|
|
2214
|
+
if (results.some((r) => r.status === "rejected"))
|
|
2215
|
+
return fleetError(500, "namespace provisioning cleanup failed", {
|
|
2216
|
+
namespace,
|
|
2217
|
+
recoveryRequired: true
|
|
2218
|
+
});
|
|
2219
|
+
throw error;
|
|
2220
|
+
} finally {
|
|
2221
|
+
this.topology.delete(namespace);
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
async deleteNamespace(namespace) {
|
|
2225
|
+
if (this.environment && !this.ownsEnvironment())
|
|
2226
|
+
return fleetError(409, "environment operation in progress");
|
|
2227
|
+
if (namespace === "default")
|
|
2228
|
+
return fleetError(409, "default namespace cannot be deleted");
|
|
2229
|
+
if (!this.namespaces.has(namespace))
|
|
2230
|
+
return fleetError(404, "namespace not found");
|
|
2231
|
+
if (this.remember(namespace).pinned)
|
|
2232
|
+
return fleetError(409, "namespace is pinned");
|
|
2233
|
+
if (this.topology.has(namespace) || this.ctx.controlling.has(namespace) || this.ctx.recovery.has(namespace))
|
|
2234
|
+
return fleetError(409, "namespace is busy or requires recovery");
|
|
2235
|
+
this.scope(namespace, this.ctx.children);
|
|
2236
|
+
const busy = this.busy(namespace);
|
|
2237
|
+
for (const [name, c] of this.ctx.children)
|
|
2238
|
+
if (c.diagnostics(namespace).activeConnections)
|
|
2239
|
+
busy.push(name);
|
|
2240
|
+
if (busy.length)
|
|
2241
|
+
return fleetError(409, "namespace has active work or clients", {
|
|
2242
|
+
services: [...new Set(busy)]
|
|
2243
|
+
});
|
|
2244
|
+
const selected = this.ctx.children;
|
|
2245
|
+
for (const [name, child] of selected)
|
|
2246
|
+
if (!child.deleteNamespace || !child.createNamespace || !child.exportCheckpoint || !child.importCheckpoint)
|
|
2247
|
+
throw new Error(`transactional namespace deletion unsupported for ${name}`);
|
|
2248
|
+
this.topology.add(namespace);
|
|
2249
|
+
const release = [];
|
|
2250
|
+
const documents = /* @__PURE__ */ new Map();
|
|
2251
|
+
const deleted = /* @__PURE__ */ new Set();
|
|
2252
|
+
const original = { ...this.remember(namespace) };
|
|
2253
|
+
try {
|
|
2254
|
+
for (const child of selected.values())
|
|
2255
|
+
release.push(child.lock(namespace));
|
|
2256
|
+
if (this.busy(namespace).length)
|
|
2257
|
+
return fleetError(409, "namespace acquired active work before deletion");
|
|
2258
|
+
for (const [name, child] of selected)
|
|
2259
|
+
documents.set(name, await child.exportCheckpoint?.(namespace, await child.checkpoint(namespace)));
|
|
2260
|
+
for (const [name, child] of selected) {
|
|
2261
|
+
await child.deleteNamespace?.(namespace);
|
|
2262
|
+
deleted.add(name);
|
|
2263
|
+
}
|
|
2264
|
+
this.namespaces.delete(namespace);
|
|
2265
|
+
await this.ctx.publish();
|
|
2266
|
+
for (const [id, p] of this.ctx.checkpoints)
|
|
2267
|
+
if (p.namespace === namespace)
|
|
2268
|
+
this.ctx.checkpoints.delete(id);
|
|
2269
|
+
return Response.json({ status: "deleted", namespace });
|
|
2270
|
+
} catch (error) {
|
|
2271
|
+
const rollback = {};
|
|
2272
|
+
for (const [name, child] of selected)
|
|
2273
|
+
if (documents.has(name)) {
|
|
2274
|
+
try {
|
|
2275
|
+
if (deleted.has(name) || child.protocol !== "http" && !Object.hasOwn(child.namespaces.endpoints ?? {}, namespace))
|
|
2276
|
+
await child.createNamespace?.(namespace);
|
|
2277
|
+
await child.restore(namespace, await child.importCheckpoint?.(namespace, documents.get(name)));
|
|
2278
|
+
rollback[name] = "restored";
|
|
2279
|
+
} catch {
|
|
2280
|
+
rollback[name] = "failed";
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
this.namespaces.set(namespace, original);
|
|
2284
|
+
if (Object.values(rollback).includes("failed")) {
|
|
2285
|
+
const fences = [];
|
|
2286
|
+
for (const child of selected.values())
|
|
2287
|
+
try {
|
|
2288
|
+
fences.push(child.lock(namespace));
|
|
2289
|
+
} catch {
|
|
2290
|
+
}
|
|
2291
|
+
this.ctx.recovery.set(namespace, fences);
|
|
2292
|
+
}
|
|
2293
|
+
await this.ctx.publish().catch(() => {
|
|
2294
|
+
});
|
|
2295
|
+
return fleetError(500, error instanceof Error ? error.message : "namespace deletion failed", {
|
|
2296
|
+
namespace,
|
|
2297
|
+
rollback,
|
|
2298
|
+
rolledBack: Object.values(rollback).every((v) => v === "restored")
|
|
2299
|
+
});
|
|
2300
|
+
} finally {
|
|
2301
|
+
for (const unlock of release.reverse())
|
|
2302
|
+
unlock();
|
|
2303
|
+
this.topology.delete(namespace);
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
async capture(namespaces, selected = this.ctx.children) {
|
|
2307
|
+
if (namespaces.length > 1 && !this.ownsEnvironment())
|
|
2308
|
+
return this.withEnvironment(namespaces, selected, () => this.capture(namespaces, selected));
|
|
2309
|
+
const contents = {};
|
|
2310
|
+
for (const [name, child] of selected)
|
|
2311
|
+
if (!child.exportCheckpoint || !child.importCheckpoint)
|
|
2312
|
+
throw new Error(`portable snapshots unsupported for ${name}`);
|
|
2313
|
+
for (const namespace of namespaces) {
|
|
2314
|
+
this.scope(namespace, selected);
|
|
2315
|
+
const services = {};
|
|
2316
|
+
const response = await this.ctx.transaction(namespace, selected, async (name, child) => {
|
|
2317
|
+
const point = await child.checkpoint(namespace);
|
|
2318
|
+
services[name] = {
|
|
2319
|
+
protocol: child.protocol,
|
|
2320
|
+
target: child.targetName ?? name,
|
|
2321
|
+
version: 1,
|
|
2322
|
+
...child.packageVersion ? { packageVersion: child.packageVersion } : {},
|
|
2323
|
+
state: await child.exportCheckpoint?.(namespace, point)
|
|
2324
|
+
};
|
|
2325
|
+
});
|
|
2326
|
+
if (!response.ok)
|
|
2327
|
+
throw new Error(`capture ${namespace} refused: ${canonical(await response.json())}`);
|
|
2328
|
+
contents[namespace] = { clock: this.ctx.clock(namespace).state(), services };
|
|
2329
|
+
}
|
|
2330
|
+
return sealArchive({
|
|
2331
|
+
format: "mockingbird.fleet/v1",
|
|
2332
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2333
|
+
sourceId: this.ctx.id,
|
|
2334
|
+
namespaces: contents
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
async restoreArchive(input, selectedNamespace) {
|
|
2338
|
+
const archive = validateArchive(input);
|
|
2339
|
+
const entries = Object.entries(archive.namespaces).filter(([ns]) => selectedNamespace === void 0 || ns === selectedNamespace);
|
|
2340
|
+
if (!entries.length)
|
|
2341
|
+
throw new Error("selected namespace absent from archive");
|
|
2342
|
+
if (entries.length > 1 && !this.ownsEnvironment()) {
|
|
2343
|
+
const selected = new Map([...new Set(entries.flatMap(([, ns]) => Object.keys(ns.services)))].map((name) => [
|
|
2344
|
+
name,
|
|
2345
|
+
this.ctx.children.get(name)
|
|
2346
|
+
]));
|
|
2347
|
+
for (const [name, child] of selected)
|
|
2348
|
+
if (!child)
|
|
2349
|
+
throw new Error(`unsupported archive service ${name}`);
|
|
2350
|
+
return this.withEnvironment(entries.map(([ns]) => ns), selected, () => this.restoreArchive(archive, selectedNamespace));
|
|
2351
|
+
}
|
|
2352
|
+
for (const [namespace, ns] of entries)
|
|
2353
|
+
for (const [name, state] of Object.entries(ns.services)) {
|
|
2354
|
+
const child = this.ctx.children.get(name);
|
|
2355
|
+
if (!child || child.protocol !== state.protocol || !child.importCheckpoint || (state.target ?? name) !== (child.targetName ?? name))
|
|
2356
|
+
throw new Error(`unsupported archive service ${name}`);
|
|
2357
|
+
await child.importCheckpoint(namespace, state.state);
|
|
2358
|
+
}
|
|
2359
|
+
const rollback = [];
|
|
2360
|
+
const created = [];
|
|
2361
|
+
try {
|
|
2362
|
+
for (const [namespace, ns] of entries) {
|
|
2363
|
+
if (!this.namespaces.has(namespace)) {
|
|
2364
|
+
const response2 = await this.createNamespace(namespace);
|
|
2365
|
+
if (!response2.ok)
|
|
2366
|
+
throw new Error(`namespace creation refused: ${canonical(await response2.json())}`);
|
|
2367
|
+
created.push(namespace);
|
|
2368
|
+
}
|
|
2369
|
+
const selected = new Map(Object.keys(ns.services).map((name) => [name, this.ctx.children.get(name)]));
|
|
2370
|
+
const values = /* @__PURE__ */ new Map();
|
|
2371
|
+
const oldClock = this.ctx.clock(namespace).state();
|
|
2372
|
+
const response = await this.ctx.transaction(namespace, selected, async (name, child) => {
|
|
2373
|
+
values.set(name, await child.checkpoint(namespace));
|
|
2374
|
+
await child.restore(namespace, await child.importCheckpoint?.(namespace, ns.services[name]?.state));
|
|
2375
|
+
}, true);
|
|
2376
|
+
if (!response.ok)
|
|
2377
|
+
throw new Error(`restore ${namespace} failed: ${canonical(await response.json())}`);
|
|
2378
|
+
rollback.push({ namespace, selected, values, clock: oldClock });
|
|
2379
|
+
restoreClock(this.ctx.clock(namespace), ns.clock);
|
|
2380
|
+
this.remember(namespace).generation = randomUUID3();
|
|
2381
|
+
}
|
|
2382
|
+
} catch (error) {
|
|
2383
|
+
const failures = [];
|
|
2384
|
+
for (const point of rollback.reverse()) {
|
|
2385
|
+
const response = await this.ctx.transaction(point.namespace, point.selected, async (name, child) => child.restore(point.namespace, point.values.get(name)), true);
|
|
2386
|
+
if (!response.ok)
|
|
2387
|
+
failures.push(point.namespace);
|
|
2388
|
+
else
|
|
2389
|
+
restoreClock(this.ctx.clock(point.namespace), point.clock);
|
|
2390
|
+
}
|
|
2391
|
+
for (const namespace of created.reverse()) {
|
|
2392
|
+
const response = await this.deleteNamespace(namespace);
|
|
2393
|
+
if (!response.ok)
|
|
2394
|
+
failures.push(namespace);
|
|
2395
|
+
}
|
|
2396
|
+
if (failures.length)
|
|
2397
|
+
throw new Error(`archive rollback requires recovery for ${failures.join(", ")}`, {
|
|
2398
|
+
cause: error
|
|
2399
|
+
});
|
|
2400
|
+
throw error;
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
async applyProfile(namespace, input, extra, seedApplication) {
|
|
2404
|
+
if (!object2(input) || !Object.keys(input).length)
|
|
2405
|
+
throw new Error("expected nonempty service control profile");
|
|
2406
|
+
const selected = this.select({ services: Object.keys(input) });
|
|
2407
|
+
for (const [name, child] of selected) {
|
|
2408
|
+
const controls = input[name];
|
|
2409
|
+
if (!object2(controls))
|
|
2410
|
+
throw new Error(`expected controls for ${name}`);
|
|
2411
|
+
normalizeControls(controls, `profile.${name}`);
|
|
2412
|
+
if (child.protocol !== "http" && Object.keys(controls).some((key) => key !== "clock"))
|
|
2413
|
+
throw new Error(`unsupported protocol controls for ${name}`);
|
|
2414
|
+
}
|
|
2415
|
+
return this.ctx.transaction(namespace, selected, async (name, child) => {
|
|
2416
|
+
const controls = input[name];
|
|
2417
|
+
if (child.configure)
|
|
2418
|
+
await child.configure(namespace, controls);
|
|
2419
|
+
else {
|
|
2420
|
+
const plan = normalizeControls(controls);
|
|
2421
|
+
if (plan.clock?.frozen === true)
|
|
2422
|
+
child.clock(namespace).freeze();
|
|
2423
|
+
if (plan.clock?.now !== void 0)
|
|
2424
|
+
child.clock(namespace).set(plan.clock.now);
|
|
2425
|
+
if (plan.clock?.frozen === false)
|
|
2426
|
+
child.clock(namespace).unfreeze();
|
|
2427
|
+
}
|
|
2428
|
+
await extra?.(name, child);
|
|
2429
|
+
if (name === [...selected.keys()].at(-1) && seedApplication) {
|
|
2430
|
+
const operation = [...this.operations.values()].find((o) => o.namespace === namespace && o.status === "running");
|
|
2431
|
+
const token = randomBytes(32).toString("base64url");
|
|
2432
|
+
this.privateRequests.set(namespace, token);
|
|
2433
|
+
const access = [];
|
|
2434
|
+
const connections = {};
|
|
2435
|
+
try {
|
|
2436
|
+
for (const [service, target] of selected) {
|
|
2437
|
+
if (target.initializationAccess) {
|
|
2438
|
+
const seedAccess = await target.initializationAccess();
|
|
2439
|
+
access.push(seedAccess);
|
|
2440
|
+
const url = seedAccess.connections[namespace];
|
|
2441
|
+
if (url)
|
|
2442
|
+
connections[service] = { url, namespaces: { [namespace]: url } };
|
|
2443
|
+
} else
|
|
2444
|
+
connections[service] = { url: target.url, namespaces: {} };
|
|
2445
|
+
}
|
|
2446
|
+
const signal = AbortSignal.timeout(this.ctx.options.hookTimeoutMs ?? 3e4);
|
|
2447
|
+
await Promise.race([
|
|
2448
|
+
seedApplication({
|
|
2449
|
+
namespace,
|
|
2450
|
+
services: [...selected.keys()],
|
|
2451
|
+
operationId: operation?.id ?? randomUUID3(),
|
|
2452
|
+
signal,
|
|
2453
|
+
manifest: this.ctx.manifest(),
|
|
2454
|
+
connections,
|
|
2455
|
+
headers: { "x-mockingbird-initializer": token, "x-mockingbird-namespace": namespace }
|
|
2456
|
+
}),
|
|
2457
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error("application seed hook timed out")), { once: true }))
|
|
2458
|
+
]);
|
|
2459
|
+
} catch (error) {
|
|
2460
|
+
throw Object.assign(new Error("application seed hook failed; recovery required", { cause: error }), { code: "MOCKINGBIRD_WORKLOAD_UNCERTAIN" });
|
|
2461
|
+
} finally {
|
|
2462
|
+
this.privateRequests.delete(namespace);
|
|
2463
|
+
await Promise.all(access.map((a) => a.close()));
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
});
|
|
2467
|
+
}
|
|
2468
|
+
async backup(namespaces, operationId) {
|
|
2469
|
+
if (!this.ctx.options.repository)
|
|
2470
|
+
throw new Error("archive repository is not configured");
|
|
2471
|
+
if (this.backupRunning)
|
|
2472
|
+
throw new Error("backup is already running; overlapping run skipped");
|
|
2473
|
+
this.backupRunning = true;
|
|
2474
|
+
this.backupStatus.lastAttempt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2475
|
+
try {
|
|
2476
|
+
const archive = await this.capture(namespaces);
|
|
2477
|
+
for (const namespace of namespaces)
|
|
2478
|
+
this.phase(namespace, "uploading");
|
|
2479
|
+
if (operationId)
|
|
2480
|
+
await this.journalPhase(operationId, "uploading", { after: archive.sha256 });
|
|
2481
|
+
const id = await this.ctx.options.repository.put(archive);
|
|
2482
|
+
for (const namespace of namespaces)
|
|
2483
|
+
this.phase(namespace, "verifying-upload");
|
|
2484
|
+
const verified = validateArchive(await this.ctx.options.repository.get(id));
|
|
2485
|
+
if (verified.sha256 !== archive.sha256)
|
|
2486
|
+
throw new Error("backup verification failed");
|
|
2487
|
+
if (operationId)
|
|
2488
|
+
await this.journalPhase(operationId, "publishing", this.ctx.options.repository.writerGeneration !== void 0 ? { writerGeneration: this.ctx.options.repository.writerGeneration } : {});
|
|
2489
|
+
for (const namespace of namespaces)
|
|
2490
|
+
this.phase(namespace, "publishing");
|
|
2491
|
+
await this.ctx.options.repository.publishLatest(id);
|
|
2492
|
+
const publication = await this.ctx.options.repository.publicationMetadata?.();
|
|
2493
|
+
if (operationId && publication)
|
|
2494
|
+
await this.journalPhase(operationId, "publishing", {
|
|
2495
|
+
writerGeneration: publication.generation
|
|
2496
|
+
});
|
|
2497
|
+
this.backupStatus.lastSuccess = (/* @__PURE__ */ new Date()).toISOString();
|
|
2498
|
+
this.backupStatus.archiveId = id;
|
|
2499
|
+
delete this.backupStatus.lastFailure;
|
|
2500
|
+
const keep = this.ctx.options.backupPolicy?.keep;
|
|
2501
|
+
if (keep !== void 0) {
|
|
2502
|
+
const archives = await this.ctx.options.repository.list();
|
|
2503
|
+
for (const old of archives.slice(0, Math.max(0, archives.length - keep)))
|
|
2504
|
+
if (old !== id)
|
|
2505
|
+
await this.ctx.options.repository.delete(old);
|
|
2506
|
+
}
|
|
2507
|
+
return { id, sha256: archive.sha256, verified: true };
|
|
2508
|
+
} catch (error) {
|
|
2509
|
+
this.backupStatus.lastFailure = error instanceof Error ? error.message : "backup failed";
|
|
2510
|
+
throw error;
|
|
2511
|
+
} finally {
|
|
2512
|
+
this.backupRunning = false;
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
cleanupPreview() {
|
|
2516
|
+
return this.namespaceStatus().filter((entry) => entry.namespace !== "default").map((entry) => {
|
|
2517
|
+
let busy = true;
|
|
2518
|
+
try {
|
|
2519
|
+
busy = this.busy(entry.namespace).length > 0 || [...this.ctx.children.values()].some((c) => c.diagnostics(entry.namespace).activeConnections > 0);
|
|
2520
|
+
} catch {
|
|
2521
|
+
}
|
|
2522
|
+
return {
|
|
2523
|
+
namespace: entry.namespace,
|
|
2524
|
+
expiresAt: entry.expiresAt,
|
|
2525
|
+
pinned: entry.pinned,
|
|
2526
|
+
eligible: entry.expiresAt !== void 0 && !entry.pinned && !busy && entry.status === "ready" && !this.topology.has(entry.namespace) && Date.now() >= (entry.expiresAt ?? Infinity)
|
|
2527
|
+
};
|
|
2528
|
+
});
|
|
2529
|
+
}
|
|
2530
|
+
async startPolicies() {
|
|
2531
|
+
if (this.ctx.options.repository && this.ctx.options.backupPolicy) {
|
|
2532
|
+
try {
|
|
2533
|
+
const id = await this.ctx.options.repository.latest();
|
|
2534
|
+
if (id) {
|
|
2535
|
+
await this.ctx.options.repository.get(id);
|
|
2536
|
+
this.backupStatus.archiveId = id;
|
|
2537
|
+
this.backupStatus.lastSuccess = (/* @__PURE__ */ new Date()).toISOString();
|
|
2538
|
+
}
|
|
2539
|
+
} catch (error) {
|
|
2540
|
+
this.backupStatus.lastFailure = error instanceof Error ? error.message : "repository recovery failed";
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
this.lifecycle = "ready";
|
|
2544
|
+
const policy = this.ctx.options.backupPolicy;
|
|
2545
|
+
if (policy) {
|
|
2546
|
+
if (!Number.isFinite(policy.intervalMs) || policy.intervalMs < 10)
|
|
2547
|
+
throw new Error("backup intervalMs must be at least 10");
|
|
2548
|
+
const schedule = () => {
|
|
2549
|
+
this.backupStatus.nextRun = new Date(Date.now() + policy.intervalMs).toISOString();
|
|
2550
|
+
if (!this.backupRunning)
|
|
2551
|
+
void this.backup(policy.namespaces ?? [...this.namespaces.keys()]).catch(() => {
|
|
2552
|
+
});
|
|
2553
|
+
};
|
|
2554
|
+
this.backupStatus.nextRun = new Date(Date.now() + policy.intervalMs).toISOString();
|
|
2555
|
+
this.timer = setInterval(schedule, policy.intervalMs);
|
|
2556
|
+
this.timer.unref?.();
|
|
2557
|
+
}
|
|
2558
|
+
const expiry = this.ctx.options.expiry;
|
|
2559
|
+
if (expiry) {
|
|
2560
|
+
if (!Number.isFinite(expiry.idleMs) || expiry.idleMs < 1)
|
|
2561
|
+
throw new Error("expiry idleMs must be positive");
|
|
2562
|
+
this.expiryTimer = setInterval(() => {
|
|
2563
|
+
for (const entry of this.cleanupPreview())
|
|
2564
|
+
if (entry.eligible)
|
|
2565
|
+
void (async () => {
|
|
2566
|
+
if (expiry.finalBackup)
|
|
2567
|
+
await this.backup([entry.namespace]);
|
|
2568
|
+
await this.deleteNamespace(entry.namespace);
|
|
2569
|
+
})().catch(() => {
|
|
2570
|
+
});
|
|
2571
|
+
}, expiry.intervalMs ?? 6e4);
|
|
2572
|
+
this.expiryTimer.unref?.();
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
stopPolicies() {
|
|
2576
|
+
clearInterval(this.timer);
|
|
2577
|
+
clearInterval(this.expiryTimer);
|
|
2578
|
+
}
|
|
2579
|
+
};
|
|
2580
|
+
var fleetConsole = (prefix) => `<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Mockingbird Fleet</title><style>body{font:16px system-ui;max-width:1000px;margin:3rem auto;padding:1rem;background:#10151b;color:#e0e9f2}input,select,button{padding:.7rem;margin:.3rem;background:#233142;color:inherit;border:1px solid #60768b;border-radius:5px}pre{white-space:pre-wrap;overflow-wrap:anywhere}a{color:#9fdbff}</style><h1>Mockingbird Fleet</h1><label>Admin key <input id="key" type="password" autocomplete="off"></label><label>Namespace <input id="ns" value="default"></label><button id="refresh">Refresh</button><section id="children"></section><label>Action <select id="action"><option value="snapshots">Capture checkpoint</option><option value="reset">Reset namespace</option></select></label><button id="run">Run</button><h2>Lifecycle and operations</h2><pre id="result"></pre><script type="module">const base=${JSON.stringify(`${prefix}/fleet`)};const key=document.querySelector('#key'),ns=document.querySelector('#ns'),result=document.querySelector('#result');async function api(path,method='GET',body){const r=await fetch(base+path,{method,headers:{'content-type':'application/json','x-mockingbird-admin-key':key.value},...(body?{body:JSON.stringify(body)}:{})});const data=await r.json();result.textContent=JSON.stringify(data,null,2);return data}document.querySelector('#refresh').onclick=async()=>{const data=await api('');const host=document.querySelector('#children');host.replaceChildren();for(const [name,child]of Object.entries(data.manifest?.services??{})){const link=document.createElement('a');link.href=child.adminUrl;link.textContent=name+' ('+child.protocol+') ';host.append(link)}await api('/operations?namespace='+encodeURIComponent(ns.value))};document.querySelector('#run').onclick=()=>api('/namespaces/'+encodeURIComponent(ns.value)+'/'+document.querySelector('#action').value,'POST',{});</script></html>`;
|
|
2581
|
+
|
|
2582
|
+
// ../../adapters/node/dist/fleet-preflight.js
|
|
2583
|
+
import { readFile, realpath } from "node:fs/promises";
|
|
2584
|
+
import { createRequire } from "node:module";
|
|
2585
|
+
import { basename, dirname, resolve } from "node:path";
|
|
2586
|
+
|
|
2587
|
+
// ../../adapters/node/dist/fleet-config.schema.json
|
|
2588
|
+
var fleet_config_schema_default = {
|
|
2589
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
2590
|
+
$id: "https://mockingbird.chrisvouga.dev/schemas/fleet-v1.json",
|
|
2591
|
+
title: "Mockingbird fleet configuration v1",
|
|
2592
|
+
type: "object",
|
|
2593
|
+
required: ["services"],
|
|
2594
|
+
additionalProperties: false,
|
|
2595
|
+
properties: {
|
|
2596
|
+
$schema: {
|
|
2597
|
+
const: "https://mockingbird.chrisvouga.dev/schemas/fleet-v1.json"
|
|
2598
|
+
},
|
|
2599
|
+
schemaVersion: {
|
|
2600
|
+
const: 1
|
|
2601
|
+
},
|
|
2602
|
+
services: {
|
|
2603
|
+
type: "object",
|
|
2604
|
+
propertyNames: {
|
|
2605
|
+
pattern: "^[a-z0-9]+(?:-[a-z0-9]+)*$"
|
|
2606
|
+
},
|
|
2607
|
+
additionalProperties: {
|
|
2608
|
+
$ref: "#/$defs/service"
|
|
2609
|
+
}
|
|
2610
|
+
},
|
|
2611
|
+
log: {
|
|
2612
|
+
enum: ["pretty", "json", "off"]
|
|
2613
|
+
},
|
|
2614
|
+
adminPrefix: {
|
|
2615
|
+
type: "string"
|
|
2616
|
+
},
|
|
2617
|
+
adminKey: {
|
|
2618
|
+
type: "string"
|
|
2619
|
+
},
|
|
2620
|
+
adminControls: {
|
|
2621
|
+
$ref: "#/$defs/controls"
|
|
2622
|
+
},
|
|
2623
|
+
namespace: {
|
|
2624
|
+
type: "string",
|
|
2625
|
+
pattern: "^[A-Za-z0-9_.-]{1,64}$"
|
|
2626
|
+
},
|
|
2627
|
+
control: {
|
|
2628
|
+
type: "object",
|
|
2629
|
+
additionalProperties: false,
|
|
2630
|
+
properties: {
|
|
2631
|
+
port: {
|
|
2632
|
+
type: "integer",
|
|
2633
|
+
minimum: 0,
|
|
2634
|
+
maximum: 65535
|
|
2635
|
+
},
|
|
2636
|
+
host: {
|
|
2637
|
+
type: "string"
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
},
|
|
2641
|
+
cors: {
|
|
2642
|
+
type: "object",
|
|
2643
|
+
required: ["origins"],
|
|
2644
|
+
additionalProperties: false,
|
|
2645
|
+
properties: {
|
|
2646
|
+
origins: {
|
|
2647
|
+
type: "array",
|
|
2648
|
+
items: {
|
|
2649
|
+
type: "string"
|
|
2650
|
+
}
|
|
2651
|
+
},
|
|
2652
|
+
methods: {
|
|
2653
|
+
type: "array",
|
|
2654
|
+
items: {
|
|
2655
|
+
enum: ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"]
|
|
2656
|
+
}
|
|
2657
|
+
},
|
|
2658
|
+
headers: {
|
|
2659
|
+
type: "array",
|
|
2660
|
+
items: {
|
|
2661
|
+
type: "string"
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
},
|
|
2666
|
+
extensions: {
|
|
2667
|
+
type: "object",
|
|
2668
|
+
propertyNames: {
|
|
2669
|
+
pattern: "^x-[a-z0-9-]+$"
|
|
2670
|
+
},
|
|
2671
|
+
additionalProperties: false
|
|
2672
|
+
}
|
|
2673
|
+
},
|
|
2674
|
+
$defs: {
|
|
2675
|
+
service: {
|
|
2676
|
+
type: "object",
|
|
2677
|
+
additionalProperties: false,
|
|
2678
|
+
properties: {
|
|
2679
|
+
target: {
|
|
2680
|
+
type: "string",
|
|
2681
|
+
pattern: "^[a-z0-9]+(?:-[a-z0-9]+)*$"
|
|
2682
|
+
},
|
|
2683
|
+
dependsOn: {
|
|
2684
|
+
type: "array",
|
|
2685
|
+
items: {
|
|
2686
|
+
type: "string"
|
|
2687
|
+
}
|
|
2688
|
+
},
|
|
2689
|
+
protocol: {
|
|
2690
|
+
enum: ["http", "postgres", "redis"]
|
|
2691
|
+
},
|
|
2692
|
+
port: {
|
|
2693
|
+
type: "integer",
|
|
2694
|
+
minimum: 0,
|
|
2695
|
+
maximum: 65535
|
|
2696
|
+
},
|
|
2697
|
+
host: {
|
|
2698
|
+
type: "string"
|
|
2699
|
+
},
|
|
2700
|
+
adminPrefix: {
|
|
2701
|
+
type: "string"
|
|
2702
|
+
},
|
|
2703
|
+
adminKey: {
|
|
2704
|
+
type: "string"
|
|
2705
|
+
},
|
|
2706
|
+
seed: {
|
|
2707
|
+
type: "string"
|
|
2708
|
+
},
|
|
2709
|
+
adminControls: {
|
|
2710
|
+
$ref: "#/$defs/controls"
|
|
2711
|
+
},
|
|
2712
|
+
fixtures: {
|
|
2713
|
+
type: "object"
|
|
2714
|
+
},
|
|
2715
|
+
baseUrl: {
|
|
2716
|
+
type: "string"
|
|
2717
|
+
},
|
|
2718
|
+
password: {
|
|
2719
|
+
type: "string"
|
|
2720
|
+
},
|
|
2721
|
+
user: {
|
|
2722
|
+
type: "string"
|
|
2723
|
+
},
|
|
2724
|
+
database: {
|
|
2725
|
+
type: "string"
|
|
2726
|
+
},
|
|
2727
|
+
namespaces: {
|
|
2728
|
+
type: "object",
|
|
2729
|
+
propertyNames: {
|
|
2730
|
+
pattern: "^[A-Za-z0-9_.-]{1,64}$"
|
|
2731
|
+
},
|
|
2732
|
+
additionalProperties: {
|
|
2733
|
+
anyOf: [
|
|
2734
|
+
{
|
|
2735
|
+
type: "string"
|
|
2736
|
+
},
|
|
2737
|
+
{
|
|
2738
|
+
type: "integer",
|
|
2739
|
+
minimum: 0,
|
|
2740
|
+
maximum: 15
|
|
2741
|
+
}
|
|
2742
|
+
]
|
|
2743
|
+
}
|
|
2744
|
+
},
|
|
2745
|
+
options: {
|
|
2746
|
+
type: "object",
|
|
2747
|
+
additionalProperties: false,
|
|
2748
|
+
description: "Compose the selected target option schema before supplying service-specific flags."
|
|
2749
|
+
},
|
|
2750
|
+
publicOrigin: {
|
|
2751
|
+
type: "string"
|
|
2752
|
+
},
|
|
2753
|
+
publicPath: {
|
|
2754
|
+
type: "string"
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
},
|
|
2758
|
+
controls: {
|
|
2759
|
+
type: "object",
|
|
2760
|
+
additionalProperties: false,
|
|
2761
|
+
properties: {
|
|
2762
|
+
seed: {},
|
|
2763
|
+
clock: {
|
|
2764
|
+
type: "object"
|
|
2765
|
+
},
|
|
2766
|
+
latency: {},
|
|
2767
|
+
faults: {
|
|
2768
|
+
type: "array"
|
|
2769
|
+
},
|
|
2770
|
+
snapshot: {
|
|
2771
|
+
type: "object"
|
|
2772
|
+
},
|
|
2773
|
+
virtualTime: {
|
|
2774
|
+
type: "boolean"
|
|
2775
|
+
},
|
|
2776
|
+
credentials: {
|
|
2777
|
+
type: "object"
|
|
2778
|
+
},
|
|
2779
|
+
namespaces: {
|
|
2780
|
+
type: "object"
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
};
|
|
2786
|
+
|
|
2787
|
+
// ../../adapters/node/dist/fleet-schema.js
|
|
2788
|
+
var object3 = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
2789
|
+
function validateFleetConfig(value, targetOptions = {}, extensions = {}) {
|
|
2790
|
+
const composed = composeFleetSchema(value, targetOptions, extensions), issues = [];
|
|
2791
|
+
const walk = (input, rule, path, output) => {
|
|
2792
|
+
if (rule.$ref) {
|
|
2793
|
+
const resolved = composed.$defs?.[rule.$ref.slice("#/$defs/".length)];
|
|
2794
|
+
if (!resolved)
|
|
2795
|
+
output.push({ path, message: "unresolved configuration schema" });
|
|
2796
|
+
else
|
|
2797
|
+
walk(input, resolved, path, output);
|
|
2798
|
+
return;
|
|
2799
|
+
}
|
|
2800
|
+
if (rule.anyOf) {
|
|
2801
|
+
if (!rule.anyOf.some((branch) => {
|
|
2802
|
+
const found = [];
|
|
2803
|
+
walk(input, branch, path, found);
|
|
2804
|
+
return found.length === 0;
|
|
2805
|
+
}))
|
|
2806
|
+
output.push({ path, message: "value does not match an allowed shape" });
|
|
2807
|
+
return;
|
|
2808
|
+
}
|
|
2809
|
+
if ("const" in rule && input !== rule.const) {
|
|
2810
|
+
output.push({
|
|
2811
|
+
path,
|
|
2812
|
+
message: path === "$.schemaVersion" || path === "$.$schema" ? "unsupported fleet schema version; upgrade the consumer/schema together" : "unexpected fixed value"
|
|
2813
|
+
});
|
|
2814
|
+
return;
|
|
2815
|
+
}
|
|
2816
|
+
if (rule.enum && !rule.enum.includes(input))
|
|
2817
|
+
output.push({ path, message: "unsupported enumerated value" });
|
|
2818
|
+
if (rule.type) {
|
|
2819
|
+
const valid = rule.type === "object" ? object3(input) : rule.type === "array" ? Array.isArray(input) : rule.type === "integer" ? typeof input === "number" && Number.isInteger(input) : typeof input === rule.type;
|
|
2820
|
+
if (!valid) {
|
|
2821
|
+
output.push({ path, message: `expected ${rule.type}` });
|
|
2822
|
+
return;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
if (typeof input === "number" && (rule.minimum !== void 0 && input < rule.minimum || rule.maximum !== void 0 && input > rule.maximum))
|
|
2826
|
+
output.push({ path, message: "number outside supported bounds" });
|
|
2827
|
+
if (typeof input === "string" && rule.pattern && !new RegExp(rule.pattern).test(input))
|
|
2828
|
+
output.push({ path, message: "invalid identifier" });
|
|
2829
|
+
if (Array.isArray(input) && rule.items)
|
|
2830
|
+
input.forEach((entry, index) => {
|
|
2831
|
+
walk(entry, rule.items, `${path}[${index}]`, output);
|
|
2832
|
+
});
|
|
2833
|
+
if (object3(input)) {
|
|
2834
|
+
for (const name of rule.required ?? [])
|
|
2835
|
+
if (!Object.hasOwn(input, name))
|
|
2836
|
+
output.push({ path: `${path}.${name}`, message: "required field missing" });
|
|
2837
|
+
for (const [key, entry] of Object.entries(input)) {
|
|
2838
|
+
if (rule.propertyNames)
|
|
2839
|
+
walk(key, rule.propertyNames, `${path}.${key}`, output);
|
|
2840
|
+
const property = rule.properties?.[key];
|
|
2841
|
+
if (property)
|
|
2842
|
+
walk(entry, property, `${path}.${key}`, output);
|
|
2843
|
+
else if (rule.additionalProperties === false)
|
|
2844
|
+
output.push({ path: `${path}.${key}`, message: "unknown configuration field" });
|
|
2845
|
+
else if (object3(rule.additionalProperties))
|
|
2846
|
+
walk(entry, rule.additionalProperties, `${path}.${key}`, output);
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
};
|
|
2850
|
+
walk(value, composed, "$", issues);
|
|
2851
|
+
return issues;
|
|
2852
|
+
}
|
|
2853
|
+
function composeFleetSchema(value, targetOptions = {}, extensions = {}) {
|
|
2854
|
+
const composed = structuredClone(fleet_config_schema_default);
|
|
2855
|
+
const serviceRule = composed.$defs?.service;
|
|
2856
|
+
const servicesRule = composed.properties?.services;
|
|
2857
|
+
servicesRule.properties = {};
|
|
2858
|
+
if (object3(value) && object3(value.services))
|
|
2859
|
+
for (const [instance, entry] of Object.entries(value.services)) {
|
|
2860
|
+
if (!object3(entry))
|
|
2861
|
+
continue;
|
|
2862
|
+
const target = typeof entry.target === "string" ? entry.target : instance;
|
|
2863
|
+
const declared = targetOptions[target] ?? {};
|
|
2864
|
+
const configured = structuredClone(serviceRule);
|
|
2865
|
+
configured.properties = {
|
|
2866
|
+
...configured.properties,
|
|
2867
|
+
options: {
|
|
2868
|
+
type: "object",
|
|
2869
|
+
additionalProperties: false,
|
|
2870
|
+
properties: Object.fromEntries(Object.entries(declared).map(([key, option]) => [key, { type: option.type }]))
|
|
2871
|
+
}
|
|
2872
|
+
};
|
|
2873
|
+
servicesRule.properties[instance] = configured;
|
|
2874
|
+
}
|
|
2875
|
+
if (composed.properties?.extensions)
|
|
2876
|
+
composed.properties.extensions.properties = extensions;
|
|
2877
|
+
return composed;
|
|
2878
|
+
}
|
|
2879
|
+
function assertFleetConfig(config, targetOptions = {}) {
|
|
2880
|
+
const issues = validateFleetConfig(config, targetOptions);
|
|
2881
|
+
if (issues.length)
|
|
2882
|
+
throw new Error(issues.slice(0, 20).map((i) => `${i.path}: ${i.message}`).join("; "));
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
// ../../adapters/node/dist/fleet-preflight.js
|
|
2886
|
+
var destination = async (path) => {
|
|
2887
|
+
const absolute = resolve(path);
|
|
2888
|
+
try {
|
|
2889
|
+
return await realpath(absolute);
|
|
2890
|
+
} catch (error) {
|
|
2891
|
+
if (error.code !== "ENOENT")
|
|
2892
|
+
throw error;
|
|
2893
|
+
const parent = dirname(absolute);
|
|
2894
|
+
return parent === absolute ? absolute : resolve(await destination(parent), basename(absolute));
|
|
2895
|
+
}
|
|
2896
|
+
};
|
|
2897
|
+
async function preflightFleet(config, options) {
|
|
2898
|
+
if (options.readyFile && options.connectionsFile && await destination(options.readyFile) === await destination(options.connectionsFile))
|
|
2899
|
+
throw new Error("ready-file and connections-file destinations must differ");
|
|
2900
|
+
if (!config.services || typeof config.services !== "object" || !Object.keys(config.services).length && !Object.keys(options.participants ?? {}).length)
|
|
2901
|
+
throw new Error("config must contain at least one service");
|
|
2902
|
+
validateFleetPolicies(options);
|
|
2903
|
+
if (options.archiveProtection && options.repository && options.repository.protectionPolicy?.format !== options.archiveProtection.policy.format)
|
|
2904
|
+
throw new Error("protected archive policy requires a protected repository; configure its protector before startup");
|
|
2905
|
+
resolveAdminPrefix(config.adminPrefix);
|
|
2906
|
+
normalizeBootControls(config.adminControls);
|
|
2907
|
+
const targets = /* @__PURE__ */ new Map();
|
|
2908
|
+
const ports = /* @__PURE__ */ new Set();
|
|
2909
|
+
for (const [name, entry] of Object.entries(config.services)) {
|
|
2910
|
+
try {
|
|
2911
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name))
|
|
2912
|
+
throw new Error("invalid service name");
|
|
2913
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
2914
|
+
throw new Error("expected service object");
|
|
2915
|
+
resolveAdminPrefix(entry.adminPrefix ?? config.adminPrefix);
|
|
2916
|
+
normalizeBootControls({ ...config.adminControls, ...entry.adminControls }, `services.${name}.adminControls`);
|
|
2917
|
+
if (entry.port !== void 0 && (!Number.isInteger(entry.port) || entry.port < 0 || entry.port > 65535))
|
|
2918
|
+
throw new Error("invalid port");
|
|
2919
|
+
if (entry.target !== void 0 && !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(entry.target))
|
|
2920
|
+
throw new Error("target must name a supported service suffix");
|
|
2921
|
+
const service = entry.target ?? name;
|
|
2922
|
+
if (advertisedHttpBase(entry) && service !== "stripe")
|
|
2923
|
+
throw new Error(`public ingress unsupported for ${service}; use a direct endpoint to preserve signed targets`);
|
|
2924
|
+
const target = await options.load(service);
|
|
2925
|
+
const protocol = "protocol" in target ? target.protocol : "http";
|
|
2926
|
+
if (entry.protocol !== void 0 && entry.protocol !== protocol)
|
|
2927
|
+
throw new Error("configured protocol does not match target");
|
|
2928
|
+
if (protocol !== "http") {
|
|
2929
|
+
if (entry.seed !== void 0 && (!String(entry.seed).trim() || !Number.isFinite(Number(entry.seed))))
|
|
2930
|
+
throw new Error("protocol seed must be a finite number");
|
|
2931
|
+
for (const [namespace, selector] of Object.entries(entry.namespaces ?? {})) {
|
|
2932
|
+
if (!/^[A-Za-z0-9_.-]{1,64}$/.test(namespace))
|
|
2933
|
+
throw new Error("invalid namespace");
|
|
2934
|
+
if (protocol === "postgres" ? typeof selector !== "string" || !selector.length : typeof selector !== "number" || !Number.isInteger(selector) || selector < 0 || selector > 15)
|
|
2935
|
+
throw new Error("invalid protocol namespace selector");
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
const port = entry.port ?? target.defaultPort;
|
|
2939
|
+
if (port !== 0) {
|
|
2940
|
+
const key = `${entry.host ?? "127.0.0.1"}:${port}`;
|
|
2941
|
+
if (ports.has(key))
|
|
2942
|
+
throw new Error(`duplicate listener ${key}`);
|
|
2943
|
+
ports.add(key);
|
|
2944
|
+
}
|
|
2945
|
+
let packageVersion = target.packageVersion;
|
|
2946
|
+
if (!packageVersion) {
|
|
2947
|
+
for (const from of [
|
|
2948
|
+
createRequire(resolve(process.cwd(), "package.json")),
|
|
2949
|
+
createRequire(import.meta.url)
|
|
2950
|
+
]) {
|
|
2951
|
+
try {
|
|
2952
|
+
let directory = dirname(from.resolve(`@crvouga/mockingbird-service-${service}/server`));
|
|
2953
|
+
for (; ; ) {
|
|
2954
|
+
try {
|
|
2955
|
+
const manifest = JSON.parse(await readFile(resolve(directory, "package.json"), "utf8"));
|
|
2956
|
+
if (manifest.name === `@crvouga/mockingbird-service-${service}`) {
|
|
2957
|
+
packageVersion = manifest.version;
|
|
2958
|
+
break;
|
|
2959
|
+
}
|
|
2960
|
+
} catch {
|
|
2961
|
+
}
|
|
2962
|
+
const parent = dirname(directory);
|
|
2963
|
+
if (parent === directory)
|
|
2964
|
+
break;
|
|
2965
|
+
directory = parent;
|
|
2966
|
+
}
|
|
2967
|
+
} catch {
|
|
2968
|
+
}
|
|
2969
|
+
if (packageVersion)
|
|
2970
|
+
break;
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
targets.set(name, { ...target, packageVersion: packageVersion ?? "3.4.0" });
|
|
2974
|
+
} catch (error) {
|
|
2975
|
+
throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
2976
|
+
}
|
|
2977
|
+
}
|
|
2978
|
+
for (const [name, participant] of Object.entries(options.participants ?? {})) {
|
|
2979
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) || targets.has(name))
|
|
2980
|
+
throw new Error(`external participant identity collision ${name}`);
|
|
2981
|
+
if (participant.protocol !== "external")
|
|
2982
|
+
throw new Error(`registered participant ${name} must declare external protocol`);
|
|
2983
|
+
targets.set(name, {
|
|
2984
|
+
name,
|
|
2985
|
+
protocol: "external",
|
|
2986
|
+
defaultPort: 0,
|
|
2987
|
+
start: async () => participant
|
|
2988
|
+
});
|
|
2989
|
+
}
|
|
2990
|
+
assertFleetConfig(config, Object.fromEntries([...targets].map(([name, target]) => [
|
|
2991
|
+
config.services[name]?.target ?? name,
|
|
2992
|
+
"options" in target ? target.options ?? {} : {}
|
|
2993
|
+
])));
|
|
2994
|
+
const ordered = /* @__PURE__ */ new Map();
|
|
2995
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
2996
|
+
const visit = (name, chain) => {
|
|
2997
|
+
if (ordered.has(name))
|
|
2998
|
+
return;
|
|
2999
|
+
if (!targets.has(name))
|
|
3000
|
+
throw new Error(`unknown dependency ${[...chain, name].join(" -> ")}`);
|
|
3001
|
+
if (visiting.has(name))
|
|
3002
|
+
throw new Error(`dependency cycle ${[...chain, name].join(" -> ")}`);
|
|
3003
|
+
visiting.add(name);
|
|
3004
|
+
const dependencies = config.services[name]?.dependsOn ?? options.participants?.[name]?.dependencies ?? [];
|
|
3005
|
+
if (!Array.isArray(dependencies) || dependencies.some((d) => typeof d !== "string"))
|
|
3006
|
+
throw new Error(`services.${name}.dependsOn must be a list of instance names`);
|
|
3007
|
+
for (const dependency of dependencies)
|
|
3008
|
+
visit(dependency, [...chain, name]);
|
|
3009
|
+
visiting.delete(name);
|
|
3010
|
+
ordered.set(name, targets.get(name));
|
|
3011
|
+
};
|
|
3012
|
+
for (const name of targets.keys())
|
|
3013
|
+
visit(name, []);
|
|
3014
|
+
return ordered;
|
|
3015
|
+
}
|
|
3016
|
+
function validateFleetPolicies(options) {
|
|
3017
|
+
if (options.journal && !options.repository)
|
|
3018
|
+
throw new Error("durable operation journal requires an archive repository");
|
|
3019
|
+
if (options.cors) {
|
|
3020
|
+
if (!Array.isArray(options.cors.origins) || options.cors.origins.some((o) => {
|
|
3021
|
+
try {
|
|
3022
|
+
const u = new URL(o);
|
|
3023
|
+
return !["http:", "https:"].includes(u.protocol) || u.origin !== o;
|
|
3024
|
+
} catch {
|
|
3025
|
+
return true;
|
|
3026
|
+
}
|
|
3027
|
+
}))
|
|
3028
|
+
throw new Error("cors.origins must contain explicit HTTP origins");
|
|
3029
|
+
if (options.cors.methods?.some((m) => !["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"].includes(m)) || options.cors.headers?.some((h) => !/^[a-zA-Z0-9-]+$/.test(h)))
|
|
3030
|
+
throw new Error("invalid CORS methods/headers");
|
|
3031
|
+
}
|
|
3032
|
+
for (const [key, value] of Object.entries(options.limits ?? {})) {
|
|
3033
|
+
if (!["maxNamespaces", "maxListeners", "connections"].includes(key))
|
|
3034
|
+
throw new Error(`unsupported namespace budget ${key}`);
|
|
3035
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
3036
|
+
throw new Error(`invalid limit ${key}`);
|
|
3037
|
+
}
|
|
3038
|
+
for (const [key, value] of Object.entries(options.retention ?? {}))
|
|
3039
|
+
if (!["maxCount", "ttlMs"].includes(key) || !Number.isSafeInteger(value) || value < 1)
|
|
3040
|
+
throw new Error(`invalid retention ${key}`);
|
|
3041
|
+
if (options.backupPolicy && (!options.repository || !Number.isSafeInteger(options.backupPolicy.intervalMs) || options.backupPolicy.intervalMs < 10 || options.backupPolicy.keep !== void 0 && (!Number.isSafeInteger(options.backupPolicy.keep) || options.backupPolicy.keep < 1)))
|
|
3042
|
+
throw new Error("backup policy requires a repository, intervalMs >= 10 and positive retention");
|
|
3043
|
+
if (options.expiry && (!Number.isSafeInteger(options.expiry.idleMs) || options.expiry.idleMs < 1 || options.expiry.intervalMs !== void 0 && (!Number.isSafeInteger(options.expiry.intervalMs) || options.expiry.intervalMs < 10) || options.expiry.finalBackup && !options.repository))
|
|
3044
|
+
throw new Error("invalid expiry policy or missing final-backup repository");
|
|
3045
|
+
for (const duration of [
|
|
3046
|
+
options.hookTimeoutMs,
|
|
3047
|
+
options.initializationTimeoutMs,
|
|
3048
|
+
options.shutdownTimeoutMs
|
|
3049
|
+
])
|
|
3050
|
+
if (duration !== void 0 && (!Number.isSafeInteger(duration) || duration < 1))
|
|
3051
|
+
throw new Error("hook deadlines must be positive integer milliseconds");
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
// ../../adapters/node/dist/serve.js
|
|
3055
|
+
import { createServer } from "node:http";
|
|
3056
|
+
import { Readable } from "node:stream";
|
|
3057
|
+
var serve = async (api, options = {}) => {
|
|
3058
|
+
const maxBytes = options.maxBodyBytes ?? 16 * 1024 * 1024;
|
|
3059
|
+
const timeoutMs = options.requestTimeoutMs ?? 3e4;
|
|
3060
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1)
|
|
3061
|
+
throw new Error("invalid request limits");
|
|
3062
|
+
const server = createServer(async (req, res) => {
|
|
3063
|
+
const aborted = new AbortController();
|
|
3064
|
+
let reader;
|
|
3065
|
+
let timer;
|
|
3066
|
+
const abort = () => {
|
|
3067
|
+
aborted.abort();
|
|
3068
|
+
void reader?.cancel().catch(() => {
|
|
3069
|
+
});
|
|
3070
|
+
};
|
|
3071
|
+
const fail = (status, message) => {
|
|
3072
|
+
if (!res.headersSent && !res.destroyed) {
|
|
3073
|
+
res.writeHead(status, { "content-type": "application/json", connection: "close" });
|
|
3074
|
+
res.end(JSON.stringify({ error: { type: "mockingbird_transport", message } }));
|
|
3075
|
+
}
|
|
3076
|
+
abort();
|
|
3077
|
+
};
|
|
3078
|
+
req.once("aborted", abort);
|
|
3079
|
+
res.once("close", abort);
|
|
3080
|
+
res.once("error", abort);
|
|
3081
|
+
try {
|
|
3082
|
+
const address = server.address();
|
|
3083
|
+
const port = typeof address === "object" && address !== null ? address.port : 80;
|
|
3084
|
+
const url = new URL((req.url ?? "/").replace(/^\/+/, "/"), `http://${req.headers.host ?? `localhost:${port}`}`);
|
|
3085
|
+
const headers = new Headers();
|
|
3086
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
3087
|
+
if (Array.isArray(value))
|
|
3088
|
+
for (const item of value)
|
|
3089
|
+
headers.append(name, item);
|
|
3090
|
+
else if (value !== void 0)
|
|
3091
|
+
headers.set(name, value);
|
|
3092
|
+
}
|
|
3093
|
+
const init = {
|
|
3094
|
+
method: req.method ?? "GET",
|
|
3095
|
+
headers,
|
|
3096
|
+
signal: aborted.signal
|
|
3097
|
+
};
|
|
3098
|
+
if (init.method !== "GET" && init.method !== "HEAD") {
|
|
3099
|
+
if (Number(req.headers["content-length"]) > maxBytes) {
|
|
3100
|
+
fail(413, "request body exceeds byte limit");
|
|
3101
|
+
return;
|
|
3102
|
+
}
|
|
3103
|
+
let bytes = 0;
|
|
3104
|
+
const source = Readable.toWeb(req);
|
|
3105
|
+
init.body = source.pipeThrough(new TransformStream({
|
|
3106
|
+
transform(chunk, controller) {
|
|
3107
|
+
bytes += chunk.byteLength;
|
|
3108
|
+
if (bytes > maxBytes) {
|
|
3109
|
+
fail(413, "request body exceeds byte limit");
|
|
3110
|
+
controller.error(new Error("request body exceeds byte limit"));
|
|
3111
|
+
} else
|
|
3112
|
+
controller.enqueue(chunk);
|
|
3113
|
+
}
|
|
3114
|
+
}));
|
|
3115
|
+
init.duplex = "half";
|
|
3116
|
+
timer = setTimeout(() => fail(408, "request body timed out"), timeoutMs);
|
|
3117
|
+
req.once("end", () => clearTimeout(timer));
|
|
3118
|
+
}
|
|
3119
|
+
const response = await api.fetch(new Request(url, init));
|
|
3120
|
+
if (aborted.signal.aborted || res.destroyed) {
|
|
3121
|
+
await response.body?.cancel();
|
|
3122
|
+
return;
|
|
3123
|
+
}
|
|
3124
|
+
const output = Object.fromEntries(response.headers);
|
|
3125
|
+
const cookies = response.headers.getSetCookie();
|
|
3126
|
+
if (cookies.length)
|
|
3127
|
+
output["set-cookie"] = cookies;
|
|
3128
|
+
res.writeHead(response.status, output);
|
|
3129
|
+
if (!response.body) {
|
|
3130
|
+
res.end();
|
|
3131
|
+
return;
|
|
3132
|
+
}
|
|
3133
|
+
res.flushHeaders();
|
|
3134
|
+
reader = response.body.getReader();
|
|
3135
|
+
for (; ; ) {
|
|
3136
|
+
const { done, value } = await reader.read();
|
|
3137
|
+
if (done || aborted.signal.aborted)
|
|
3138
|
+
break;
|
|
3139
|
+
if (!res.write(value)) {
|
|
3140
|
+
await new Promise((resolve4) => {
|
|
3141
|
+
const finish = () => {
|
|
3142
|
+
res.off("drain", finish);
|
|
3143
|
+
res.off("close", finish);
|
|
3144
|
+
res.off("error", finish);
|
|
3145
|
+
resolve4();
|
|
3146
|
+
};
|
|
3147
|
+
res.once("drain", finish);
|
|
3148
|
+
res.once("close", finish);
|
|
3149
|
+
res.once("error", finish);
|
|
3150
|
+
if (res.destroyed || aborted.signal.aborted)
|
|
3151
|
+
finish();
|
|
3152
|
+
});
|
|
3153
|
+
}
|
|
3154
|
+
}
|
|
3155
|
+
if (!aborted.signal.aborted)
|
|
3156
|
+
res.end();
|
|
3157
|
+
} catch (error) {
|
|
3158
|
+
if (error.code === "MOCKINGBIRD_DROP")
|
|
3159
|
+
req.socket.destroy();
|
|
3160
|
+
else if (!aborted.signal.aborted && !res.destroyed) {
|
|
3161
|
+
if (res.headersSent)
|
|
3162
|
+
res.destroy();
|
|
3163
|
+
else {
|
|
3164
|
+
res.writeHead(500, { "content-type": "application/json" });
|
|
3165
|
+
res.end(JSON.stringify({
|
|
3166
|
+
error: {
|
|
3167
|
+
type: "mockingbird_internal",
|
|
3168
|
+
message: error instanceof Error ? error.message : String(error)
|
|
3169
|
+
}
|
|
3170
|
+
}));
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
} finally {
|
|
3174
|
+
clearTimeout(timer);
|
|
3175
|
+
reader?.releaseLock();
|
|
3176
|
+
req.off("aborted", abort);
|
|
3177
|
+
res.off("close", abort);
|
|
3178
|
+
res.off("error", abort);
|
|
3179
|
+
if (!req.complete)
|
|
3180
|
+
req.resume();
|
|
3181
|
+
}
|
|
3182
|
+
});
|
|
3183
|
+
server.requestTimeout = timeoutMs;
|
|
3184
|
+
await new Promise((resolve4, reject) => {
|
|
3185
|
+
server.once("error", reject);
|
|
3186
|
+
server.listen(options.port ?? 0, options.host, resolve4);
|
|
3187
|
+
});
|
|
3188
|
+
return server;
|
|
3189
|
+
};
|
|
3190
|
+
|
|
3191
|
+
// ../../adapters/node/dist/listen.js
|
|
3192
|
+
var listen = async (api, options = {}) => {
|
|
3193
|
+
const host = options.host ?? "127.0.0.1";
|
|
3194
|
+
const server = await serve(api, { port: options.port ?? 0, host });
|
|
3195
|
+
const address = server.address();
|
|
3196
|
+
const port = typeof address === "object" && address !== null ? address.port : options.port ?? 0;
|
|
3197
|
+
const shown = host.includes(":") ? `[${host}]` : host;
|
|
3198
|
+
return {
|
|
3199
|
+
url: `http://${shown}:${port}`,
|
|
3200
|
+
port,
|
|
3201
|
+
host,
|
|
3202
|
+
server,
|
|
3203
|
+
close: () => new Promise((resolve4, reject) => {
|
|
3204
|
+
server.close((error) => {
|
|
3205
|
+
if (error && error.code !== "ERR_SERVER_NOT_RUNNING")
|
|
3206
|
+
reject(error);
|
|
3207
|
+
else
|
|
3208
|
+
resolve4();
|
|
3209
|
+
});
|
|
3210
|
+
server.closeAllConnections?.();
|
|
3211
|
+
})
|
|
3212
|
+
};
|
|
3213
|
+
};
|
|
3214
|
+
|
|
3215
|
+
// ../../adapters/node/dist/fleet.js
|
|
3216
|
+
var EMPTY = {
|
|
3217
|
+
activeRequests: 0,
|
|
3218
|
+
activeConnections: 0,
|
|
3219
|
+
blockedCommands: 0,
|
|
3220
|
+
pendingJobs: 0,
|
|
3221
|
+
pendingWebhooks: 0,
|
|
3222
|
+
unmatchedRequests: 0
|
|
3223
|
+
};
|
|
3224
|
+
var NS = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
3225
|
+
var json = (status, value) => {
|
|
3226
|
+
if (status >= 400 && isObject(value)) {
|
|
3227
|
+
const message = typeof value.error === "string" ? value.error : typeof value.message === "string" ? value.message : value.status === "failed" ? "fleet operation failed" : "fleet request rejected";
|
|
3228
|
+
return Response.json({
|
|
3229
|
+
...value,
|
|
3230
|
+
error: {
|
|
3231
|
+
type: "mockingbird_admin",
|
|
3232
|
+
code: {
|
|
3233
|
+
400: "invalid_request",
|
|
3234
|
+
401: "unauthorized",
|
|
3235
|
+
404: "not_found",
|
|
3236
|
+
409: "conflict",
|
|
3237
|
+
500: "operation_failed"
|
|
3238
|
+
}[status] ?? "request_failed",
|
|
3239
|
+
message
|
|
3240
|
+
}
|
|
3241
|
+
}, { status });
|
|
3242
|
+
}
|
|
3243
|
+
return Response.json(value, { status });
|
|
3244
|
+
};
|
|
3245
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3246
|
+
var pending = (d) => d.activeRequests + d.blockedCommands + d.pendingJobs + d.pendingWebhooks;
|
|
3247
|
+
var publicUrl = (raw) => {
|
|
3248
|
+
const url = new URL(raw);
|
|
3249
|
+
url.password = "";
|
|
3250
|
+
return url.href;
|
|
3251
|
+
};
|
|
3252
|
+
var atomicWrite = async (path, value) => {
|
|
3253
|
+
const temp = `${path}.${randomUUID4()}.tmp`;
|
|
3254
|
+
try {
|
|
3255
|
+
await writeFile(temp, `${JSON.stringify(value, null, 2)}
|
|
3256
|
+
`, { mode: 384, flag: "wx" });
|
|
3257
|
+
await chmod(temp, 384);
|
|
3258
|
+
await rename(temp, path);
|
|
3259
|
+
} finally {
|
|
3260
|
+
await rm(temp, { force: true });
|
|
3261
|
+
}
|
|
3262
|
+
};
|
|
3263
|
+
var removeOwned = async (path, id) => {
|
|
3264
|
+
if (!path)
|
|
3265
|
+
return;
|
|
3266
|
+
try {
|
|
3267
|
+
const value = JSON.parse(await readFile2(path, "utf8"));
|
|
3268
|
+
if (isObject(value) && value.id === id)
|
|
3269
|
+
await rm(path, { force: true });
|
|
3270
|
+
} catch (error) {
|
|
3271
|
+
if (error.code !== "ENOENT")
|
|
3272
|
+
throw error;
|
|
3273
|
+
}
|
|
3274
|
+
};
|
|
3275
|
+
var httpChild = async (target, entry, options, admission, generation, privateAccess, observer) => {
|
|
3276
|
+
const requestLog = new AsyncLocalStorage2();
|
|
3277
|
+
const created = await target.create(entry.options ?? {}, {
|
|
3278
|
+
adminPrefix: resolveAdminPrefix(entry.adminPrefix),
|
|
3279
|
+
adminKey: entry.adminKey,
|
|
3280
|
+
seed: entry.seed,
|
|
3281
|
+
onLog: (entry2) => {
|
|
3282
|
+
const current = requestLog.getStore();
|
|
3283
|
+
if (current)
|
|
3284
|
+
current.entry = entry2;
|
|
3285
|
+
options.onLog?.(entry2);
|
|
3286
|
+
},
|
|
3287
|
+
...entry.adminControls !== void 0 ? { adminControls: entry.adminControls } : {},
|
|
3288
|
+
...entry.fixtures !== void 0 ? { fixtures: entry.fixtures } : {},
|
|
3289
|
+
...(advertisedHttpBase(entry) ?? entry.baseUrl) !== void 0 ? { baseUrl: advertisedHttpBase(entry) ?? entry.baseUrl } : {}
|
|
3290
|
+
});
|
|
3291
|
+
if (!isObject(created) || [
|
|
3292
|
+
"fetch",
|
|
3293
|
+
"fleetSnapshot",
|
|
3294
|
+
"fleetRestore",
|
|
3295
|
+
"fleetExport",
|
|
3296
|
+
"fleetImport",
|
|
3297
|
+
"namespaceOf",
|
|
3298
|
+
"instance",
|
|
3299
|
+
"namespaceClock",
|
|
3300
|
+
"reset",
|
|
3301
|
+
"configure",
|
|
3302
|
+
"dropNamespace"
|
|
3303
|
+
].some((method) => typeof created[method] !== "function"))
|
|
3304
|
+
throw new Error("HTTP target does not implement the fleet runtime protocol; build/install matching supported service versions");
|
|
3305
|
+
const runtime = created;
|
|
3306
|
+
const unobserve = runtime.webhooks?.observeDeliveries(observer.delivery);
|
|
3307
|
+
if (options.outbound && runtime.webhooks)
|
|
3308
|
+
runtime.webhooks.setTransportPolicy(ownedTransport(options.outbound));
|
|
3309
|
+
const adminPrefix = resolveAdminPrefix(entry.adminPrefix);
|
|
3310
|
+
const locked = /* @__PURE__ */ new Set();
|
|
3311
|
+
const active = /* @__PURE__ */ new Map();
|
|
3312
|
+
const activity = /* @__PURE__ */ new Map();
|
|
3313
|
+
const api = {
|
|
3314
|
+
async fetch(request) {
|
|
3315
|
+
const namespace = runtime.namespaceOf(request);
|
|
3316
|
+
if (locked.has(namespace) && !privateAccess(namespace, request))
|
|
3317
|
+
return json(409, { error: "fleet control in progress", namespace });
|
|
3318
|
+
if (!new URL(request.url).pathname.endsWith(`${adminPrefix}/health`)) {
|
|
3319
|
+
const refused = admission(namespace, request);
|
|
3320
|
+
if (refused)
|
|
3321
|
+
return refused;
|
|
3322
|
+
}
|
|
3323
|
+
activity.set(namespace, Date.now());
|
|
3324
|
+
active.set(namespace, (active.get(namespace) ?? 0) + 1);
|
|
3325
|
+
let complete = false;
|
|
3326
|
+
const finish = () => {
|
|
3327
|
+
if (complete)
|
|
3328
|
+
return;
|
|
3329
|
+
complete = true;
|
|
3330
|
+
active.set(namespace, (active.get(namespace) ?? 1) - 1);
|
|
3331
|
+
request.signal.removeEventListener("abort", finish);
|
|
3332
|
+
};
|
|
3333
|
+
request.signal.addEventListener("abort", finish, { once: true });
|
|
3334
|
+
try {
|
|
3335
|
+
const log = {}, traceToken = observer.begin(namespace, request), started = performance.now();
|
|
3336
|
+
const response = await requestLog.run(log, () => runtime.fetch(request));
|
|
3337
|
+
await observer.finish(namespace, request, response, log.entry ?? {
|
|
3338
|
+
service: target.name,
|
|
3339
|
+
namespace,
|
|
3340
|
+
operationId: void 0,
|
|
3341
|
+
method: request.method,
|
|
3342
|
+
path: new URL(request.url).pathname,
|
|
3343
|
+
status: response.status,
|
|
3344
|
+
durationMs: performance.now() - started,
|
|
3345
|
+
unmatched: response.status === 404
|
|
3346
|
+
}, traceToken);
|
|
3347
|
+
if (!response.body) {
|
|
3348
|
+
finish();
|
|
3349
|
+
return response;
|
|
3350
|
+
}
|
|
3351
|
+
const reader = response.body.getReader();
|
|
3352
|
+
const headers = new Headers(response.headers);
|
|
3353
|
+
const epoch = generation(namespace);
|
|
3354
|
+
if (epoch)
|
|
3355
|
+
headers.set("x-mockingbird-generation", epoch);
|
|
3356
|
+
return new Response(new ReadableStream({
|
|
3357
|
+
async pull(controller) {
|
|
3358
|
+
try {
|
|
3359
|
+
const next = await reader.read();
|
|
3360
|
+
if (next.done) {
|
|
3361
|
+
finish();
|
|
3362
|
+
controller.close();
|
|
3363
|
+
reader.releaseLock();
|
|
3364
|
+
} else
|
|
3365
|
+
controller.enqueue(next.value);
|
|
3366
|
+
} catch (error) {
|
|
3367
|
+
finish();
|
|
3368
|
+
controller.error(error);
|
|
3369
|
+
}
|
|
3370
|
+
},
|
|
3371
|
+
async cancel(reason) {
|
|
3372
|
+
try {
|
|
3373
|
+
await reader.cancel(reason);
|
|
3374
|
+
} finally {
|
|
3375
|
+
finish();
|
|
3376
|
+
reader.releaseLock();
|
|
3377
|
+
}
|
|
3378
|
+
}
|
|
3379
|
+
}), { status: response.status, statusText: response.statusText, headers });
|
|
3380
|
+
} catch (error) {
|
|
3381
|
+
finish();
|
|
3382
|
+
throw error;
|
|
3383
|
+
}
|
|
3384
|
+
}
|
|
3385
|
+
};
|
|
3386
|
+
let server;
|
|
3387
|
+
try {
|
|
3388
|
+
server = await listen(api, {
|
|
3389
|
+
port: entry.port ?? target.defaultPort,
|
|
3390
|
+
host: entry.host ?? "127.0.0.1"
|
|
3391
|
+
});
|
|
3392
|
+
target.listening?.(server);
|
|
3393
|
+
} catch (error) {
|
|
3394
|
+
unobserve?.();
|
|
3395
|
+
runtime.stop?.();
|
|
3396
|
+
await server?.close();
|
|
3397
|
+
runtime.webhooks?.clear();
|
|
3398
|
+
await runtime.webhooks?.idle();
|
|
3399
|
+
throw error;
|
|
3400
|
+
}
|
|
3401
|
+
return {
|
|
3402
|
+
protocol: "http",
|
|
3403
|
+
outboundPolicy: runtime.webhooks ? "webhook-hub" : "unsupported",
|
|
3404
|
+
url: server.url,
|
|
3405
|
+
healthUrl: `${server.url}${adminPrefix}/health`,
|
|
3406
|
+
adminUrl: `${server.url}${adminPrefix}`,
|
|
3407
|
+
namespaces: { header: "x-mockingbird-namespace", path: `${adminPrefix}/ns/{name}` },
|
|
3408
|
+
ready: async () => {
|
|
3409
|
+
try {
|
|
3410
|
+
return (await fetch(`${server.url}${adminPrefix}/health`, { signal: AbortSignal.timeout(2e3) })).ok;
|
|
3411
|
+
} catch {
|
|
3412
|
+
return false;
|
|
3413
|
+
}
|
|
3414
|
+
},
|
|
3415
|
+
async close() {
|
|
3416
|
+
unobserve?.();
|
|
3417
|
+
runtime.stop?.();
|
|
3418
|
+
runtime.webhooks?.clear();
|
|
3419
|
+
await server.close();
|
|
3420
|
+
await runtime.webhooks?.idle();
|
|
3421
|
+
},
|
|
3422
|
+
lock(namespace) {
|
|
3423
|
+
if (locked.has(namespace))
|
|
3424
|
+
throw new Error("namespace already locked");
|
|
3425
|
+
locked.add(namespace);
|
|
3426
|
+
return () => locked.delete(namespace);
|
|
3427
|
+
},
|
|
3428
|
+
diagnostics(namespace) {
|
|
3429
|
+
return {
|
|
3430
|
+
...EMPTY,
|
|
3431
|
+
activeRequests: active.get(namespace) ?? 0,
|
|
3432
|
+
pendingWebhooks: runtime.webhooks?.pending(namespace) ?? 0,
|
|
3433
|
+
unmatchedRequests: runtime.metrics.report(namespace).unmatched.reduce((n, r) => n + r.count, 0)
|
|
3434
|
+
};
|
|
3435
|
+
},
|
|
3436
|
+
async checkpoint(namespace) {
|
|
3437
|
+
return runtime.fleetSnapshot(namespace);
|
|
3438
|
+
},
|
|
3439
|
+
async restore(namespace, checkpoint) {
|
|
3440
|
+
runtime.fleetRestore(checkpoint, namespace);
|
|
3441
|
+
},
|
|
3442
|
+
reset: (namespace) => runtime.reset(namespace),
|
|
3443
|
+
clock: (namespace) => runtime.namespaceClock(namespace),
|
|
3444
|
+
unmatched(namespace, limit) {
|
|
3445
|
+
const entries = runtime.metrics.report(namespace).unmatched;
|
|
3446
|
+
return {
|
|
3447
|
+
entries: entries.slice(0, limit).map(({ method, path, count }) => ({
|
|
3448
|
+
method,
|
|
3449
|
+
pathFingerprint: createHash4("sha256").update(path).digest("hex"),
|
|
3450
|
+
count
|
|
3451
|
+
})),
|
|
3452
|
+
truncated: entries.length > limit
|
|
3453
|
+
};
|
|
3454
|
+
},
|
|
3455
|
+
lastActivity: (namespace) => activity.get(namespace) ?? 0,
|
|
3456
|
+
async exportCheckpoint(_namespace, point) {
|
|
3457
|
+
return runtime.fleetExport(point);
|
|
3458
|
+
},
|
|
3459
|
+
async importCheckpoint(namespace, document) {
|
|
3460
|
+
return runtime.fleetImport(namespace, document);
|
|
3461
|
+
},
|
|
3462
|
+
async createNamespace(namespace) {
|
|
3463
|
+
runtime.instance(namespace);
|
|
3464
|
+
},
|
|
3465
|
+
deleteNamespace: (namespace) => runtime.dropNamespace(namespace),
|
|
3466
|
+
async configure(namespace, controls) {
|
|
3467
|
+
return runtime.configure(controls, namespace);
|
|
3468
|
+
},
|
|
3469
|
+
async inspect(namespace, { offset, limit, collection }) {
|
|
3470
|
+
const doc = runtime.fleetExport(runtime.fleetSnapshot(namespace));
|
|
3471
|
+
const rows = collection ? doc.records.filter((row) => row.collection === collection) : doc.records;
|
|
3472
|
+
return {
|
|
3473
|
+
total: rows.length,
|
|
3474
|
+
offset,
|
|
3475
|
+
records: rows.slice(offset, offset + limit),
|
|
3476
|
+
collections: [...new Set(doc.records.map((row) => row.collection))]
|
|
3477
|
+
};
|
|
3478
|
+
},
|
|
3479
|
+
...typeof runtime.instance().nextEvent === "function" ? {
|
|
3480
|
+
nextEvent: (namespace) => runtime.instance(namespace).nextEvent()
|
|
3481
|
+
} : {},
|
|
3482
|
+
async settle(namespace) {
|
|
3483
|
+
const instance = runtime.instance(namespace);
|
|
3484
|
+
await instance.tick?.();
|
|
3485
|
+
await runtime.webhooks?.idle(namespace);
|
|
3486
|
+
}
|
|
3487
|
+
};
|
|
3488
|
+
};
|
|
3489
|
+
async function startFleet(config, options) {
|
|
3490
|
+
if (config.cors && options.cors === void 0)
|
|
3491
|
+
options = { ...options, cors: config.cors };
|
|
3492
|
+
const targets = await preflightFleet(config, options);
|
|
3493
|
+
const id = randomUUID4();
|
|
3494
|
+
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3495
|
+
const children = /* @__PURE__ */ new Map();
|
|
3496
|
+
const clocks = /* @__PURE__ */ new Map();
|
|
3497
|
+
const checkpoints = /* @__PURE__ */ new Map();
|
|
3498
|
+
let supervisor;
|
|
3499
|
+
let management;
|
|
3500
|
+
let manifest;
|
|
3501
|
+
const privateConnections = () => Object.fromEntries([...children].map(([name, child]) => [
|
|
3502
|
+
name,
|
|
3503
|
+
{ url: child.connection ?? child.url, namespaces: child.connections }
|
|
3504
|
+
]));
|
|
3505
|
+
const publish = async () => {
|
|
3506
|
+
if (!manifest || management && management.lifecycle !== "ready")
|
|
3507
|
+
return;
|
|
3508
|
+
manifest.state = recovery.size ? "recovery-required" : "ready";
|
|
3509
|
+
for (const [name, child] of children)
|
|
3510
|
+
if (manifest.services[name])
|
|
3511
|
+
manifest.services[name].namespaces = child.namespaces;
|
|
3512
|
+
if (options.connectionsFile)
|
|
3513
|
+
await atomicWrite(options.connectionsFile, {
|
|
3514
|
+
version: 1,
|
|
3515
|
+
id,
|
|
3516
|
+
pid: process.pid,
|
|
3517
|
+
services: privateConnections()
|
|
3518
|
+
});
|
|
3519
|
+
if (options.readyFile)
|
|
3520
|
+
await atomicWrite(options.readyFile, manifest);
|
|
3521
|
+
};
|
|
3522
|
+
let stopped = false;
|
|
3523
|
+
const controlling = /* @__PURE__ */ new Map();
|
|
3524
|
+
const recovery = /* @__PURE__ */ new Map();
|
|
3525
|
+
let closing;
|
|
3526
|
+
const close = () => {
|
|
3527
|
+
if (closing)
|
|
3528
|
+
return closing;
|
|
3529
|
+
closing = (async () => {
|
|
3530
|
+
management?.stopPolicies();
|
|
3531
|
+
let gateFailure;
|
|
3532
|
+
if (options.beforeClose && management && ["ready", "failed"].includes(management.lifecycle)) {
|
|
3533
|
+
management.lifecycle = "draining";
|
|
3534
|
+
const signal = AbortSignal.timeout(options.shutdownTimeoutMs ?? 3e4);
|
|
3535
|
+
try {
|
|
3536
|
+
await Promise.race([
|
|
3537
|
+
options.beforeClose({
|
|
3538
|
+
manifest,
|
|
3539
|
+
signal,
|
|
3540
|
+
capture: (namespaces) => management.capture(namespaces)
|
|
3541
|
+
}),
|
|
3542
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error("pre-shutdown hook timed out")), { once: true }))
|
|
3543
|
+
]);
|
|
3544
|
+
} catch (error) {
|
|
3545
|
+
management.lifecycle = "failed";
|
|
3546
|
+
if (options.shutdownFailure !== "close") {
|
|
3547
|
+
closing = void 0;
|
|
3548
|
+
throw error;
|
|
3549
|
+
}
|
|
3550
|
+
gateFailure = error;
|
|
3551
|
+
}
|
|
3552
|
+
}
|
|
3553
|
+
stopped = true;
|
|
3554
|
+
if (management)
|
|
3555
|
+
management.lifecycle = "stopped";
|
|
3556
|
+
const entries = [...children];
|
|
3557
|
+
const results = [];
|
|
3558
|
+
for (const [, child] of [...entries].reverse()) {
|
|
3559
|
+
try {
|
|
3560
|
+
await child.close();
|
|
3561
|
+
results.unshift({ status: "fulfilled", value: void 0 });
|
|
3562
|
+
} catch (reason) {
|
|
3563
|
+
results.unshift({ status: "rejected", reason });
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
const cleanup = await Promise.allSettled([
|
|
3567
|
+
supervisor?.close(),
|
|
3568
|
+
removeOwned(options.readyFile, id),
|
|
3569
|
+
removeOwned(options.connectionsFile, id)
|
|
3570
|
+
]);
|
|
3571
|
+
const failures = results.flatMap((r, i) => r.status === "rejected" ? [{ service: entries[i]?.[0], cause: r.reason }] : []);
|
|
3572
|
+
if (gateFailure)
|
|
3573
|
+
failures.push({ service: "pre-shutdown", cause: gateFailure });
|
|
3574
|
+
failures.push(...cleanup.flatMap((r) => r.status === "rejected" ? [{ service: "controller", cause: r.reason }] : []));
|
|
3575
|
+
if (failures.length)
|
|
3576
|
+
throw new AggregateError(failures.map((f) => f.cause), `fleet shutdown failed: ${failures.map((f) => f.service).join(", ")}`);
|
|
3577
|
+
})();
|
|
3578
|
+
return closing;
|
|
3579
|
+
};
|
|
3580
|
+
if (options.readyFile)
|
|
3581
|
+
await rm(options.readyFile, { force: true });
|
|
3582
|
+
if (options.connectionsFile)
|
|
3583
|
+
await rm(options.connectionsFile, { force: true });
|
|
3584
|
+
const clock = (namespace) => {
|
|
3585
|
+
let value = clocks.get(namespace);
|
|
3586
|
+
if (!value) {
|
|
3587
|
+
value = createClock();
|
|
3588
|
+
const plan = normalizeBootControls(config.adminControls);
|
|
3589
|
+
const boot = { ...plan.shared.clock, ...plan.namespaces[namespace]?.clock };
|
|
3590
|
+
if (boot.frozen)
|
|
3591
|
+
value.freeze();
|
|
3592
|
+
if (boot.now !== void 0)
|
|
3593
|
+
value.set(boot.now);
|
|
3594
|
+
clocks.set(namespace, value);
|
|
3595
|
+
}
|
|
3596
|
+
return value;
|
|
3597
|
+
};
|
|
3598
|
+
const select = (body) => {
|
|
3599
|
+
if (body.services === void 0)
|
|
3600
|
+
return children;
|
|
3601
|
+
if (!Array.isArray(body.services) || body.services.length === 0)
|
|
3602
|
+
throw new Error("services must be a nonempty list");
|
|
3603
|
+
const selected = /* @__PURE__ */ new Map();
|
|
3604
|
+
for (const name of body.services) {
|
|
3605
|
+
if (typeof name !== "string" || !children.has(name))
|
|
3606
|
+
throw new Error("unknown service in selection");
|
|
3607
|
+
selected.set(name, children.get(name));
|
|
3608
|
+
}
|
|
3609
|
+
return selected;
|
|
3610
|
+
};
|
|
3611
|
+
const diagnostics = (namespace, selected = children) => Object.fromEntries([...selected].map(([name, child]) => [name, child.diagnostics(namespace)]));
|
|
3612
|
+
const missingNamespace = (namespace, selected) => {
|
|
3613
|
+
const missing = [...selected].filter(([, c]) => c.protocol !== "http" && !Object.hasOwn(c.namespaces.endpoints ?? {}, namespace)).map(([name]) => name);
|
|
3614
|
+
return missing.length ? json(404, { error: "namespace is not configured", namespace, services: missing }) : void 0;
|
|
3615
|
+
};
|
|
3616
|
+
const transaction = async (namespace, selected, run, recover = false) => {
|
|
3617
|
+
const unavailable = missingNamespace(namespace, selected);
|
|
3618
|
+
if (unavailable)
|
|
3619
|
+
return unavailable;
|
|
3620
|
+
if (management?.topologyBusy(namespace))
|
|
3621
|
+
return json(409, { error: "namespace topology operation in progress" });
|
|
3622
|
+
if (controlling.has(namespace))
|
|
3623
|
+
return json(409, {
|
|
3624
|
+
error: "another fleet operation is in progress",
|
|
3625
|
+
operationId: controlling.get(namespace)
|
|
3626
|
+
});
|
|
3627
|
+
if (recovery.has(namespace) && !recover)
|
|
3628
|
+
return json(409, {
|
|
3629
|
+
error: "namespace requires explicit reset or restore recovery",
|
|
3630
|
+
namespace
|
|
3631
|
+
});
|
|
3632
|
+
const held = recovery.get(namespace);
|
|
3633
|
+
if (held) {
|
|
3634
|
+
for (const unlock of held.reverse())
|
|
3635
|
+
unlock();
|
|
3636
|
+
recovery.delete(namespace);
|
|
3637
|
+
}
|
|
3638
|
+
controlling.set(namespace, management?.operationId(namespace) ?? randomUUID4());
|
|
3639
|
+
const release = [];
|
|
3640
|
+
let keepFence = false;
|
|
3641
|
+
const before = /* @__PURE__ */ new Map();
|
|
3642
|
+
const results = {};
|
|
3643
|
+
let failure;
|
|
3644
|
+
try {
|
|
3645
|
+
if (![...selected.keys()].every((name) => management?.environmentFence(namespace, name)))
|
|
3646
|
+
await management?.hook("prepare", namespace, selected);
|
|
3647
|
+
management?.phase(namespace, "checkpointing");
|
|
3648
|
+
for (const [name, child] of selected) {
|
|
3649
|
+
failure = name;
|
|
3650
|
+
if (!management?.environmentFence(namespace, name))
|
|
3651
|
+
release.push(child.lock(namespace));
|
|
3652
|
+
}
|
|
3653
|
+
const busy = Object.fromEntries([...selected].filter(([, child]) => {
|
|
3654
|
+
const state = child.diagnostics(namespace);
|
|
3655
|
+
return state.activeRequests + state.blockedCommands + state.pendingWebhooks + (state.activeJobs ?? 0) > 0;
|
|
3656
|
+
}).map(([name, child]) => [name, child.diagnostics(namespace)]));
|
|
3657
|
+
if (Object.keys(busy).length)
|
|
3658
|
+
return json(409, { error: "namespace is not quiescent", services: busy });
|
|
3659
|
+
for (const [name, child] of selected) {
|
|
3660
|
+
failure = name;
|
|
3661
|
+
management?.phase(namespace, "checkpointing", before.size, selected.size, name);
|
|
3662
|
+
before.set(name, await child.checkpoint(namespace));
|
|
3663
|
+
}
|
|
3664
|
+
management?.checkCancelled(namespace);
|
|
3665
|
+
management?.phase(namespace, "committing");
|
|
3666
|
+
for (const [name, child] of selected) {
|
|
3667
|
+
failure = name;
|
|
3668
|
+
management?.phase(namespace, "committing", Object.keys(results).length, selected.size, name);
|
|
3669
|
+
results[name] = { status: "ok", result: await run(name, child) };
|
|
3670
|
+
management?.phase(namespace, "committing", Object.keys(results).length, selected.size);
|
|
3671
|
+
}
|
|
3672
|
+
await management?.hook("verify", namespace, selected);
|
|
3673
|
+
try {
|
|
3674
|
+
await management?.hook("resume", namespace, selected);
|
|
3675
|
+
} catch (error) {
|
|
3676
|
+
keepFence = true;
|
|
3677
|
+
throw error;
|
|
3678
|
+
}
|
|
3679
|
+
return json(200, { status: "ok", namespace, services: results });
|
|
3680
|
+
} catch (error) {
|
|
3681
|
+
if (error.code === "MOCKINGBIRD_WORKLOAD_UNCERTAIN")
|
|
3682
|
+
keepFence = true;
|
|
3683
|
+
const rollback = {};
|
|
3684
|
+
for (const [name, point] of before) {
|
|
3685
|
+
try {
|
|
3686
|
+
management?.phase(namespace, "rolling-back", void 0, void 0, name);
|
|
3687
|
+
await selected.get(name)?.restore(namespace, point);
|
|
3688
|
+
rollback[name] = "restored";
|
|
3689
|
+
} catch {
|
|
3690
|
+
rollback[name] = "failed";
|
|
3691
|
+
management?.phase(namespace, "rollback-failed", void 0, void 0, name);
|
|
3692
|
+
}
|
|
3693
|
+
}
|
|
3694
|
+
keepFence = keepFence || Object.values(rollback).includes("failed") || held !== void 0;
|
|
3695
|
+
if (!keepFence)
|
|
3696
|
+
try {
|
|
3697
|
+
await management?.hook("resume", namespace, selected);
|
|
3698
|
+
} catch {
|
|
3699
|
+
keepFence = true;
|
|
3700
|
+
}
|
|
3701
|
+
if (keepFence && release.length === 0)
|
|
3702
|
+
for (const child of selected.values())
|
|
3703
|
+
try {
|
|
3704
|
+
release.push(child.lock(namespace));
|
|
3705
|
+
} catch {
|
|
3706
|
+
}
|
|
3707
|
+
return json(500, {
|
|
3708
|
+
error: error instanceof Error ? error.message : "fleet operation failed",
|
|
3709
|
+
status: "failed",
|
|
3710
|
+
namespace,
|
|
3711
|
+
failedService: failure,
|
|
3712
|
+
services: results,
|
|
3713
|
+
rollback,
|
|
3714
|
+
rolledBack: before.size > 0 && Object.values(rollback).every((value) => value === "restored")
|
|
3715
|
+
});
|
|
3716
|
+
} finally {
|
|
3717
|
+
if (keepFence)
|
|
3718
|
+
recovery.set(namespace, release);
|
|
3719
|
+
else
|
|
3720
|
+
for (const unlock of release.reverse())
|
|
3721
|
+
unlock();
|
|
3722
|
+
controlling.delete(namespace);
|
|
3723
|
+
}
|
|
3724
|
+
};
|
|
3725
|
+
const coreApi = {
|
|
3726
|
+
async fetch(request) {
|
|
3727
|
+
const url = new URL(request.url);
|
|
3728
|
+
const adminPrefix = resolveAdminPrefix(config.adminPrefix);
|
|
3729
|
+
const path = url.pathname.startsWith(`${adminPrefix}/`) ? url.pathname.slice(adminPrefix.length) : "";
|
|
3730
|
+
if (path === "/health" && request.method === "GET") {
|
|
3731
|
+
const states = await Promise.all([...children].map(async ([name, child]) => {
|
|
3732
|
+
let protocolReady = false;
|
|
3733
|
+
try {
|
|
3734
|
+
protocolReady = await child.ready();
|
|
3735
|
+
} catch {
|
|
3736
|
+
}
|
|
3737
|
+
return [
|
|
3738
|
+
name,
|
|
3739
|
+
{
|
|
3740
|
+
processReady: !stopped,
|
|
3741
|
+
protocolReady,
|
|
3742
|
+
status: protocolReady ? "ready" : "failed",
|
|
3743
|
+
url: publicUrl(child.url)
|
|
3744
|
+
}
|
|
3745
|
+
];
|
|
3746
|
+
}));
|
|
3747
|
+
const ready = !stopped && states.every(([, state]) => state.protocolReady);
|
|
3748
|
+
return json(ready ? 200 : 503, {
|
|
3749
|
+
status: ready ? "ready" : "failed",
|
|
3750
|
+
id,
|
|
3751
|
+
services: Object.fromEntries(states)
|
|
3752
|
+
});
|
|
3753
|
+
}
|
|
3754
|
+
if (!path.startsWith("/fleet/"))
|
|
3755
|
+
return json(404, { error: "not found" });
|
|
3756
|
+
const route = /^\/fleet\/namespaces\/([^/]+)\/(reset|snapshots)(?:\/([^/]+)\/restore)?$/.exec(path);
|
|
3757
|
+
let namespace;
|
|
3758
|
+
try {
|
|
3759
|
+
namespace = route ? decodeURIComponent(route[1]) : url.searchParams.get("namespace") ?? request.headers.get("x-mockingbird-namespace") ?? config.namespace ?? "default";
|
|
3760
|
+
} catch {
|
|
3761
|
+
return json(400, { error: "invalid namespace encoding" });
|
|
3762
|
+
}
|
|
3763
|
+
if (!NS.test(namespace))
|
|
3764
|
+
return json(400, { error: "invalid namespace" });
|
|
3765
|
+
if (request.method === "GET") {
|
|
3766
|
+
const unavailable2 = missingNamespace(namespace, children);
|
|
3767
|
+
if (unavailable2)
|
|
3768
|
+
return unavailable2;
|
|
3769
|
+
}
|
|
3770
|
+
if (request.method === "GET" && path === "/fleet/metrics")
|
|
3771
|
+
return json(200, { namespace, services: diagnostics(namespace) });
|
|
3772
|
+
if (request.method === "GET" && path === "/fleet/clock")
|
|
3773
|
+
return json(200, {
|
|
3774
|
+
namespace,
|
|
3775
|
+
...clock(namespace).state(),
|
|
3776
|
+
services: Object.fromEntries([...children].map(([name, child]) => [name, child.clock(namespace).state()]))
|
|
3777
|
+
});
|
|
3778
|
+
if (request.method !== "POST")
|
|
3779
|
+
return json(405, { error: "method not allowed" });
|
|
3780
|
+
let body;
|
|
3781
|
+
try {
|
|
3782
|
+
const text = await request.text();
|
|
3783
|
+
const value = text ? JSON.parse(text) : {};
|
|
3784
|
+
if (!isObject(value))
|
|
3785
|
+
throw new Error("expected object");
|
|
3786
|
+
body = value;
|
|
3787
|
+
} catch {
|
|
3788
|
+
return json(400, { error: "expected JSON object" });
|
|
3789
|
+
}
|
|
3790
|
+
let selected;
|
|
3791
|
+
try {
|
|
3792
|
+
selected = select(body);
|
|
3793
|
+
} catch (error) {
|
|
3794
|
+
return json(400, { error: error instanceof Error ? error.message : "bad selection" });
|
|
3795
|
+
}
|
|
3796
|
+
const allowed = route?.[3] ? [] : path === "/fleet/clock" ? ["services", "set", "advance", "freeze"] : route?.[2] === "snapshots" ? ["services", "label"] : ["services"];
|
|
3797
|
+
if (Object.keys(body).some((key) => !allowed.includes(key)))
|
|
3798
|
+
return json(400, { error: "unknown control field" });
|
|
3799
|
+
if (body.label !== void 0 && (typeof body.label !== "string" || body.label.length > 256))
|
|
3800
|
+
return json(400, { error: "label must be a string of at most 256 characters" });
|
|
3801
|
+
const unavailable = missingNamespace(namespace, selected);
|
|
3802
|
+
if (unavailable)
|
|
3803
|
+
return unavailable;
|
|
3804
|
+
if (path === "/fleet/wait-until-idle") {
|
|
3805
|
+
const services = diagnostics(namespace, selected);
|
|
3806
|
+
const idle = Object.values(services).every((d) => pending(d) === 0);
|
|
3807
|
+
return json(idle ? 200 : 409, { status: idle ? "idle" : "busy", namespace, services });
|
|
3808
|
+
}
|
|
3809
|
+
if (path === "/fleet/clock") {
|
|
3810
|
+
const current = clock(namespace);
|
|
3811
|
+
if (body.set === void 0 && body.advance === void 0 && body.freeze === void 0)
|
|
3812
|
+
return json(200, {
|
|
3813
|
+
namespace,
|
|
3814
|
+
...current.state(),
|
|
3815
|
+
services: Object.fromEntries([...selected].map(([name, child]) => [name, child.clock(namespace).state()]))
|
|
3816
|
+
});
|
|
3817
|
+
let target = current.now();
|
|
3818
|
+
if (body.set !== void 0) {
|
|
3819
|
+
target = typeof body.set === "number" ? body.set : typeof body.set === "string" ? Date.parse(body.set) : NaN;
|
|
3820
|
+
if (!Number.isFinite(target))
|
|
3821
|
+
return json(400, { error: "set must be epoch ms or ISO-8601" });
|
|
3822
|
+
}
|
|
3823
|
+
if (body.advance !== void 0) {
|
|
3824
|
+
const delta = parseDuration(body.advance);
|
|
3825
|
+
if (delta === void 0)
|
|
3826
|
+
return json(400, { error: "invalid advance" });
|
|
3827
|
+
target += delta;
|
|
3828
|
+
}
|
|
3829
|
+
const frozen = body.freeze === void 0 ? current.state().frozen : body.freeze;
|
|
3830
|
+
if (typeof frozen !== "boolean")
|
|
3831
|
+
return json(400, { error: "freeze must be boolean" });
|
|
3832
|
+
const response2 = await transaction(namespace, selected, async (_name, child) => {
|
|
3833
|
+
const childClock = child.clock(namespace);
|
|
3834
|
+
restoreClock(childClock, {
|
|
3835
|
+
now: body.set !== void 0 ? target : childClock.now() + (body.advance !== void 0 ? parseDuration(body.advance) ?? 0 : 0),
|
|
3836
|
+
frozen: body.freeze === void 0 ? childClock.state().frozen : frozen
|
|
3837
|
+
});
|
|
3838
|
+
await child.settle?.(namespace);
|
|
3839
|
+
return childClock.state();
|
|
3840
|
+
});
|
|
3841
|
+
if (response2.ok)
|
|
3842
|
+
restoreClock(current, { now: target, frozen });
|
|
3843
|
+
return response2;
|
|
3844
|
+
}
|
|
3845
|
+
if (!route)
|
|
3846
|
+
return json(404, { error: "not found" });
|
|
3847
|
+
if (route[2] === "reset") {
|
|
3848
|
+
if (route[3])
|
|
3849
|
+
return json(404, { error: "not found" });
|
|
3850
|
+
const response2 = await transaction(namespace, selected, async (_name, child) => child.reset(namespace), true);
|
|
3851
|
+
if (response2.ok && selected.size === children.size) {
|
|
3852
|
+
clocks.delete(namespace);
|
|
3853
|
+
clock(namespace);
|
|
3854
|
+
}
|
|
3855
|
+
return response2;
|
|
3856
|
+
}
|
|
3857
|
+
if (route[3]) {
|
|
3858
|
+
const point = checkpoints.get(route[3]);
|
|
3859
|
+
if (!point || point.namespace !== namespace)
|
|
3860
|
+
return json(404, { error: "no snapshot in this namespace" });
|
|
3861
|
+
const restored = new Map([...point.values].map(([name]) => [name, children.get(name)]));
|
|
3862
|
+
const response2 = await transaction(namespace, restored, async (name, child) => child.restore(namespace, point.values.get(name)), true);
|
|
3863
|
+
if (response2.ok)
|
|
3864
|
+
restoreClock(clock(namespace), point.clock);
|
|
3865
|
+
return response2;
|
|
3866
|
+
}
|
|
3867
|
+
const snapshotId = randomUUID4();
|
|
3868
|
+
const values = /* @__PURE__ */ new Map();
|
|
3869
|
+
const response = await transaction(namespace, selected, async (name, child) => {
|
|
3870
|
+
values.set(name, await child.checkpoint(namespace));
|
|
3871
|
+
return { checkpoint: `${snapshotId}:${name}` };
|
|
3872
|
+
});
|
|
3873
|
+
if (!response.ok)
|
|
3874
|
+
return response;
|
|
3875
|
+
checkpoints.set(snapshotId, { namespace, values, clock: clock(namespace).state() });
|
|
3876
|
+
return json(201, {
|
|
3877
|
+
...await response.json(),
|
|
3878
|
+
id: snapshotId,
|
|
3879
|
+
label: body.label
|
|
3880
|
+
});
|
|
3881
|
+
}
|
|
3882
|
+
};
|
|
3883
|
+
try {
|
|
3884
|
+
const adminPrefix = resolveAdminPrefix(config.adminPrefix);
|
|
3885
|
+
if (!isObject(config.services) || targets.size === 0)
|
|
3886
|
+
throw new Error("config must contain at least one service");
|
|
3887
|
+
for (const [name, target] of targets) {
|
|
3888
|
+
const entry = config.services[name] ?? {};
|
|
3889
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name))
|
|
3890
|
+
throw new Error("invalid service name");
|
|
3891
|
+
if (config.adminControls !== void 0 || entry.adminControls !== void 0)
|
|
3892
|
+
normalizeBootControls({ ...config.adminControls, ...entry.adminControls }, `services.${name}.adminControls`);
|
|
3893
|
+
try {
|
|
3894
|
+
if (entry.protocol && entry.protocol !== ("protocol" in target ? target.protocol : "http"))
|
|
3895
|
+
throw new Error("configured protocol does not match target");
|
|
3896
|
+
const adminKey = entry.adminKey ?? config.adminKey ?? process.env.MOCKINGBIRD_ADMIN_KEY;
|
|
3897
|
+
const child = "start" in target ? await target.start({
|
|
3898
|
+
...entry,
|
|
3899
|
+
...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
|
|
3900
|
+
}) : await httpChild(target, {
|
|
3901
|
+
...entry,
|
|
3902
|
+
adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
|
|
3903
|
+
...adminKey !== void 0 ? { adminKey } : {},
|
|
3904
|
+
// Fleet-wide controls, with the service's own keys replacing them.
|
|
3905
|
+
...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
|
|
3906
|
+
}, options, (namespace, request) => management?.admit(namespace, request), (namespace) => management?.generation(namespace), (namespace, request) => management?.privateRequest(namespace, request) ?? false, {
|
|
3907
|
+
begin: (namespace, request) => management?.beginRequest(name, namespace, request),
|
|
3908
|
+
finish: async (namespace, request, response, log, token) => {
|
|
3909
|
+
await management?.finishRequest(name, namespace, request, response, log, token);
|
|
3910
|
+
},
|
|
3911
|
+
delivery: (event) => management?.observeDelivery(name, event)
|
|
3912
|
+
});
|
|
3913
|
+
child.targetName = entry.target ?? name;
|
|
3914
|
+
if (target.packageVersion)
|
|
3915
|
+
child.packageVersion = target.packageVersion;
|
|
3916
|
+
children.set(name, child);
|
|
3917
|
+
if (!await child.ready())
|
|
3918
|
+
throw new Error("protocol readiness failed");
|
|
3919
|
+
} catch (error) {
|
|
3920
|
+
throw new Error(`service ${name} failed to start; all started listeners were closed: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
3921
|
+
}
|
|
3922
|
+
}
|
|
3923
|
+
management = new FleetManagement({
|
|
3924
|
+
id,
|
|
3925
|
+
config: { ...config, adminPrefix },
|
|
3926
|
+
options,
|
|
3927
|
+
children,
|
|
3928
|
+
checkpoints,
|
|
3929
|
+
clock,
|
|
3930
|
+
transaction,
|
|
3931
|
+
manifest: () => manifest,
|
|
3932
|
+
publish,
|
|
3933
|
+
recovery,
|
|
3934
|
+
controlling
|
|
3935
|
+
});
|
|
3936
|
+
const api = {
|
|
3937
|
+
fetch: (request) => management.dispatch(request, () => coreApi.fetch(request))
|
|
3938
|
+
};
|
|
3939
|
+
supervisor = await listen(api, {
|
|
3940
|
+
port: config.control?.port ?? 0,
|
|
3941
|
+
host: config.control?.host ?? "127.0.0.1"
|
|
3942
|
+
});
|
|
3943
|
+
manifest = {
|
|
3944
|
+
version: 1,
|
|
3945
|
+
capabilityVersion: 1,
|
|
3946
|
+
state: "ready",
|
|
3947
|
+
pid: process.pid,
|
|
3948
|
+
id,
|
|
3949
|
+
startedAt,
|
|
3950
|
+
adminBase: `${supervisor.url}${adminPrefix}/fleet`,
|
|
3951
|
+
healthUrl: `${supervisor.url}${adminPrefix}/health`,
|
|
3952
|
+
services: Object.fromEntries([...children].map(([name, child]) => [
|
|
3953
|
+
name,
|
|
3954
|
+
{
|
|
3955
|
+
protocol: child.protocol,
|
|
3956
|
+
url: advertisedHttpBase(config.services[name] ?? {}) ?? publicUrl(child.url),
|
|
3957
|
+
healthUrl: child.healthUrl ?? `${supervisor?.url}${adminPrefix}/health`,
|
|
3958
|
+
adminUrl: child.adminUrl ?? `${supervisor?.url}${adminPrefix}/fleet`,
|
|
3959
|
+
namespaces: child.namespaces,
|
|
3960
|
+
processReady: true,
|
|
3961
|
+
protocolReady: true
|
|
3962
|
+
}
|
|
3963
|
+
]))
|
|
3964
|
+
};
|
|
3965
|
+
await management.recoverOperations();
|
|
3966
|
+
if (options.initialize) {
|
|
3967
|
+
management.lifecycle = "initializing";
|
|
3968
|
+
const access = [];
|
|
3969
|
+
const connections = privateConnections();
|
|
3970
|
+
try {
|
|
3971
|
+
for (const [name, child] of children)
|
|
3972
|
+
if (child.initializationAccess) {
|
|
3973
|
+
const privateAccess = await child.initializationAccess();
|
|
3974
|
+
access.push(privateAccess);
|
|
3975
|
+
connections[name] = {
|
|
3976
|
+
url: privateAccess.connections.default ?? child.connection ?? child.url,
|
|
3977
|
+
namespaces: privateAccess.connections
|
|
3978
|
+
};
|
|
3979
|
+
}
|
|
3980
|
+
const signal = AbortSignal.timeout(options.initializationTimeoutMs ?? 3e4);
|
|
3981
|
+
await Promise.race([
|
|
3982
|
+
options.initialize({
|
|
3983
|
+
manifest,
|
|
3984
|
+
connections,
|
|
3985
|
+
headers: { "x-mockingbird-initializer": management.initializationToken },
|
|
3986
|
+
signal
|
|
3987
|
+
}),
|
|
3988
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error("fleet initialization timed out")), { once: true }))
|
|
3989
|
+
]);
|
|
3990
|
+
} finally {
|
|
3991
|
+
await Promise.all(access.map((entry) => entry.close()));
|
|
3992
|
+
}
|
|
3993
|
+
}
|
|
3994
|
+
await management.startPolicies();
|
|
3995
|
+
await publish();
|
|
3996
|
+
if (manifest.state === "ready")
|
|
3997
|
+
options.onReady?.(manifest);
|
|
3998
|
+
return { manifest, children, fetch: api.fetch, close };
|
|
3999
|
+
} catch (error) {
|
|
4000
|
+
try {
|
|
4001
|
+
await close();
|
|
4002
|
+
} catch (cleanup) {
|
|
4003
|
+
throw new AggregateError([error, cleanup], `fleet startup and cleanup failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
4004
|
+
}
|
|
4005
|
+
throw error;
|
|
4006
|
+
}
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
// ../../adapters/node/dist/fleet-tools.js
|
|
4010
|
+
import { readFile as readFile4 } from "node:fs/promises";
|
|
4011
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
4012
|
+
import { resolve as resolve2 } from "node:path";
|
|
4013
|
+
|
|
4014
|
+
// ../../adapters/node/dist/fleet-client.js
|
|
4015
|
+
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
4016
|
+
import { chmod as chmod2, readFile as readFile3, rename as rename2, rm as rm2, writeFile as writeFile2 } from "node:fs/promises";
|
|
4017
|
+
var FleetClientError = class extends Error {
|
|
4018
|
+
status;
|
|
4019
|
+
details;
|
|
4020
|
+
constructor(status, details) {
|
|
4021
|
+
super(`fleet control HTTP ${status}: ${canonical(details)}`);
|
|
4022
|
+
this.status = status;
|
|
4023
|
+
this.details = details;
|
|
4024
|
+
}
|
|
4025
|
+
};
|
|
4026
|
+
var FleetClient = class {
|
|
4027
|
+
base;
|
|
4028
|
+
options;
|
|
4029
|
+
constructor(base, options = {}) {
|
|
4030
|
+
this.base = base;
|
|
4031
|
+
this.options = options;
|
|
4032
|
+
}
|
|
4033
|
+
async request(path, options = {}) {
|
|
4034
|
+
const url = new URL(`${this.base.replace(/\/$/, "")}/${path.replace(/^\//, "")}`);
|
|
4035
|
+
if (url.username || url.password)
|
|
4036
|
+
throw new Error("fleet client URL must not contain credentials");
|
|
4037
|
+
url.searchParams.set("namespace", options.namespace ?? this.options.namespace ?? "default");
|
|
4038
|
+
const response = await (this.options.fetch ?? fetch)(url, {
|
|
4039
|
+
method: options.method ?? (options.body === void 0 ? "GET" : "POST"),
|
|
4040
|
+
headers: {
|
|
4041
|
+
"content-type": "application/json",
|
|
4042
|
+
...this.options.adminKey ? { "x-mockingbird-admin-key": this.options.adminKey } : {},
|
|
4043
|
+
...options.idempotencyKey ? { "idempotency-key": options.idempotencyKey } : {}
|
|
4044
|
+
},
|
|
4045
|
+
...options.body !== void 0 ? { body: JSON.stringify(options.body) } : {},
|
|
4046
|
+
signal: options.signal ?? AbortSignal.timeout(this.options.timeoutMs ?? 3e4)
|
|
4047
|
+
});
|
|
4048
|
+
const data = await response.json();
|
|
4049
|
+
if (!response.ok)
|
|
4050
|
+
throw new FleetClientError(response.status, data);
|
|
4051
|
+
return data;
|
|
4052
|
+
}
|
|
4053
|
+
discover() {
|
|
4054
|
+
return this.request("");
|
|
4055
|
+
}
|
|
4056
|
+
namespaces() {
|
|
4057
|
+
return this.request("namespaces");
|
|
4058
|
+
}
|
|
4059
|
+
createNamespace(namespace, baseline) {
|
|
4060
|
+
return this.request(`namespaces/${encodeURIComponent(namespace)}`, {
|
|
4061
|
+
body: baseline ? { baseline } : {}
|
|
4062
|
+
});
|
|
4063
|
+
}
|
|
4064
|
+
deleteNamespace(namespace) {
|
|
4065
|
+
return this.request(`namespaces/${encodeURIComponent(namespace)}`, { method: "DELETE" });
|
|
4066
|
+
}
|
|
4067
|
+
reset(namespace, options = {}) {
|
|
4068
|
+
return this.request(`namespaces/${encodeURIComponent(namespace)}/reset`, {
|
|
4069
|
+
body: options.services ? { services: options.services } : {},
|
|
4070
|
+
...options.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : {}
|
|
4071
|
+
});
|
|
4072
|
+
}
|
|
4073
|
+
snapshot(namespace, options = {}) {
|
|
4074
|
+
const { idempotencyKey, ...body } = options;
|
|
4075
|
+
return this.request(`namespaces/${encodeURIComponent(namespace)}/snapshots`, {
|
|
4076
|
+
body,
|
|
4077
|
+
...idempotencyKey ? { idempotencyKey } : {}
|
|
4078
|
+
});
|
|
4079
|
+
}
|
|
4080
|
+
restore(namespace, id, idempotencyKey) {
|
|
4081
|
+
return this.request(`namespaces/${encodeURIComponent(namespace)}/snapshots/${encodeURIComponent(id)}/restore`, { body: {}, ...idempotencyKey ? { idempotencyKey } : {} });
|
|
4082
|
+
}
|
|
4083
|
+
exportArchive(namespace, allNamespaces = false) {
|
|
4084
|
+
return this.request("archives/export", {
|
|
4085
|
+
body: { allNamespaces },
|
|
4086
|
+
...namespace ? { namespace } : {}
|
|
4087
|
+
});
|
|
4088
|
+
}
|
|
4089
|
+
exportProtectedArchive(namespace, allNamespaces = false) {
|
|
4090
|
+
return this.request("archives/export", {
|
|
4091
|
+
body: { allNamespaces },
|
|
4092
|
+
...namespace ? { namespace } : {}
|
|
4093
|
+
});
|
|
4094
|
+
}
|
|
4095
|
+
importArchive(archive, namespace) {
|
|
4096
|
+
if (archive.format !== "mockingbird.jwe/v1")
|
|
4097
|
+
validateArchive(archive);
|
|
4098
|
+
return this.request("archives/import", {
|
|
4099
|
+
body: { archive, ...namespace ? { namespace } : {} }
|
|
4100
|
+
});
|
|
4101
|
+
}
|
|
4102
|
+
operation(id) {
|
|
4103
|
+
return this.request(`operations/${encodeURIComponent(id)}`);
|
|
4104
|
+
}
|
|
4105
|
+
};
|
|
4106
|
+
function renderConnections(manifest, privateManifest, namespace, mapping) {
|
|
4107
|
+
if (!/^[A-Za-z0-9_.-]{1,64}$/.test(namespace))
|
|
4108
|
+
throw new Error("invalid namespace");
|
|
4109
|
+
const result = {};
|
|
4110
|
+
for (const [variable, entry] of Object.entries(mapping)) {
|
|
4111
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(variable))
|
|
4112
|
+
throw new Error(`invalid variable name ${variable}`);
|
|
4113
|
+
const child = manifest.services[entry.service], secret = privateManifest.services[entry.service];
|
|
4114
|
+
if (!child || !secret)
|
|
4115
|
+
throw new Error(`missing service mapping ${entry.service}`);
|
|
4116
|
+
if (child.protocol === "http") {
|
|
4117
|
+
const template = child.namespaces.path;
|
|
4118
|
+
if (typeof template !== "string")
|
|
4119
|
+
throw new Error(`missing HTTP namespace mapping ${entry.service}`);
|
|
4120
|
+
result[variable] = new URL(template.replace("{name}", encodeURIComponent(namespace)), secret.url).href;
|
|
4121
|
+
} else {
|
|
4122
|
+
const endpoint = secret.namespaces?.[namespace];
|
|
4123
|
+
if (!endpoint)
|
|
4124
|
+
throw new Error(`missing namespace mapping ${namespace}/${entry.service}`);
|
|
4125
|
+
result[variable] = endpoint;
|
|
4126
|
+
}
|
|
4127
|
+
}
|
|
4128
|
+
return result;
|
|
4129
|
+
}
|
|
4130
|
+
async function writeConnections(path, values, format = "json") {
|
|
4131
|
+
const temp = `${path}.${randomUUID5()}.tmp`;
|
|
4132
|
+
const text = format === "json" ? `${JSON.stringify(values, null, 2)}
|
|
4133
|
+
` : `${Object.entries(values).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join("\n")}
|
|
4134
|
+
`;
|
|
4135
|
+
try {
|
|
4136
|
+
await writeFile2(temp, text, { mode: 384, flag: "wx" });
|
|
4137
|
+
await chmod2(temp, 384);
|
|
4138
|
+
await rename2(temp, path);
|
|
4139
|
+
} finally {
|
|
4140
|
+
await rm2(temp, { force: true });
|
|
4141
|
+
}
|
|
4142
|
+
}
|
|
4143
|
+
|
|
4144
|
+
// ../../adapters/node/dist/fleet-tools.js
|
|
4145
|
+
async function doctorFleet(config, options, probes = []) {
|
|
4146
|
+
const findings = [];
|
|
4147
|
+
const plan = [];
|
|
4148
|
+
try {
|
|
4149
|
+
const targets = await preflightFleet(config, options);
|
|
4150
|
+
const require2 = createRequire2(resolve2(process.cwd(), "package.json"));
|
|
4151
|
+
for (const [service, target] of targets) {
|
|
4152
|
+
let version = "development";
|
|
4153
|
+
try {
|
|
4154
|
+
version = require2(`@crvouga/mockingbird-service-${service}/package.json`).version;
|
|
4155
|
+
} catch {
|
|
4156
|
+
}
|
|
4157
|
+
plan.push({
|
|
4158
|
+
service,
|
|
4159
|
+
protocol: "protocol" in target ? target.protocol : "http",
|
|
4160
|
+
port: config.services[service]?.port ?? target.defaultPort,
|
|
4161
|
+
version
|
|
4162
|
+
});
|
|
4163
|
+
}
|
|
4164
|
+
findings.push({
|
|
4165
|
+
check: "configuration",
|
|
4166
|
+
ok: true,
|
|
4167
|
+
message: "all targets and control documents validated; no listeners opened"
|
|
4168
|
+
});
|
|
4169
|
+
} catch (error) {
|
|
4170
|
+
findings.push({
|
|
4171
|
+
check: "configuration",
|
|
4172
|
+
ok: false,
|
|
4173
|
+
message: error instanceof Error ? error.message : "configuration failed"
|
|
4174
|
+
});
|
|
4175
|
+
}
|
|
4176
|
+
for (const probe of probes) {
|
|
4177
|
+
try {
|
|
4178
|
+
const url = new URL(probe.url);
|
|
4179
|
+
if (url.username || url.password)
|
|
4180
|
+
throw new Error("probe URLs must not contain credentials");
|
|
4181
|
+
const response = await fetch(url, { signal: AbortSignal.timeout(probe.timeoutMs ?? 2e3) });
|
|
4182
|
+
await response.body?.cancel();
|
|
4183
|
+
findings.push({
|
|
4184
|
+
check: "reachability",
|
|
4185
|
+
ok: response.ok,
|
|
4186
|
+
message: `endpoint returned HTTP ${response.status}`
|
|
4187
|
+
});
|
|
4188
|
+
} catch {
|
|
4189
|
+
findings.push({
|
|
4190
|
+
check: "reachability",
|
|
4191
|
+
ok: false,
|
|
4192
|
+
message: "endpoint unreachable; verify the listener host, port and readiness"
|
|
4193
|
+
});
|
|
4194
|
+
}
|
|
4195
|
+
}
|
|
4196
|
+
return {
|
|
4197
|
+
version: 1,
|
|
4198
|
+
ok: findings.every((f) => f.ok),
|
|
4199
|
+
findings,
|
|
4200
|
+
plan,
|
|
4201
|
+
storageWritesPerformed: false
|
|
4202
|
+
};
|
|
4203
|
+
}
|
|
4204
|
+
async function rehearseFleet(config, options, input, probes = [], timeoutMs = 3e4) {
|
|
4205
|
+
const archive = validateArchive(input);
|
|
4206
|
+
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4207
|
+
const isolated = {
|
|
4208
|
+
...config,
|
|
4209
|
+
control: { host: "127.0.0.1", port: 0 },
|
|
4210
|
+
services: Object.fromEntries(Object.entries(config.services).map(([name, entry]) => [
|
|
4211
|
+
name,
|
|
4212
|
+
{ ...entry, host: "127.0.0.1", port: 0 }
|
|
4213
|
+
]))
|
|
4214
|
+
};
|
|
4215
|
+
let fleet;
|
|
4216
|
+
try {
|
|
4217
|
+
fleet = await startFleet(isolated, { load: options.load });
|
|
4218
|
+
const client = new FleetClient(fleet.manifest.adminBase, {
|
|
4219
|
+
...config.adminKey ? { adminKey: config.adminKey } : {}
|
|
4220
|
+
});
|
|
4221
|
+
await client.importArchive(archive);
|
|
4222
|
+
const signal = AbortSignal.timeout(timeoutMs);
|
|
4223
|
+
for (const probe of probes)
|
|
4224
|
+
await Promise.race([
|
|
4225
|
+
probe(fleet, signal),
|
|
4226
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error("rehearsal probe timed out")), {
|
|
4227
|
+
once: true
|
|
4228
|
+
}))
|
|
4229
|
+
]);
|
|
4230
|
+
return {
|
|
4231
|
+
version: 1,
|
|
4232
|
+
ok: true,
|
|
4233
|
+
startedAt,
|
|
4234
|
+
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4235
|
+
sourceArchive: archive.sha256,
|
|
4236
|
+
rehearsalProcess: fleet.manifest.id,
|
|
4237
|
+
probes: probes.length
|
|
4238
|
+
};
|
|
4239
|
+
} finally {
|
|
4240
|
+
await fleet?.close();
|
|
4241
|
+
}
|
|
4242
|
+
}
|
|
4243
|
+
function fleetCommands(load) {
|
|
4244
|
+
const parse = async (path) => {
|
|
4245
|
+
if (typeof path !== "string")
|
|
4246
|
+
throw new Error("file path required");
|
|
4247
|
+
return JSON.parse(await readFile4(path, "utf8"));
|
|
4248
|
+
};
|
|
4249
|
+
return {
|
|
4250
|
+
"fleet control": {
|
|
4251
|
+
summary: "Call authenticated fleet controls using discovered adminBase",
|
|
4252
|
+
options: {
|
|
4253
|
+
base: { type: "string", description: "Discovered adminBase URL" },
|
|
4254
|
+
action: { type: "string", description: "Relative control route" },
|
|
4255
|
+
method: { type: "string", description: "HTTP method" },
|
|
4256
|
+
body: { type: "string", description: "JSON request file" },
|
|
4257
|
+
namespace: { type: "string", description: "Selected namespace" },
|
|
4258
|
+
"key-env": {
|
|
4259
|
+
type: "string",
|
|
4260
|
+
description: "Admin-key environment variable name",
|
|
4261
|
+
default: "MOCKINGBIRD_ADMIN_KEY"
|
|
4262
|
+
},
|
|
4263
|
+
"idempotency-key": { type: "string", description: "Retry identity" }
|
|
4264
|
+
},
|
|
4265
|
+
async run(values) {
|
|
4266
|
+
try {
|
|
4267
|
+
if (typeof values.base !== "string" || typeof values.action !== "string")
|
|
4268
|
+
throw new Error("--base and --action required");
|
|
4269
|
+
const key = process.env[String(values["key-env"] ?? "MOCKINGBIRD_ADMIN_KEY")];
|
|
4270
|
+
const client = new FleetClient(values.base, {
|
|
4271
|
+
...key ? { adminKey: key } : {},
|
|
4272
|
+
...typeof values.namespace === "string" ? { namespace: values.namespace } : {}
|
|
4273
|
+
});
|
|
4274
|
+
console.log(JSON.stringify(await client.request(values.action, {
|
|
4275
|
+
...typeof values.method === "string" ? { method: values.method } : {},
|
|
4276
|
+
...values.body ? { body: await parse(values.body) } : {},
|
|
4277
|
+
...typeof values["idempotency-key"] === "string" ? { idempotencyKey: values["idempotency-key"] } : {}
|
|
4278
|
+
})));
|
|
4279
|
+
return 0;
|
|
4280
|
+
} catch (error) {
|
|
4281
|
+
console.error(error instanceof Error ? error.message : "fleet control failed");
|
|
4282
|
+
return 1;
|
|
4283
|
+
}
|
|
4284
|
+
}
|
|
4285
|
+
},
|
|
4286
|
+
"fleet doctor": {
|
|
4287
|
+
summary: "Validate fleet configuration without starting or resetting services",
|
|
4288
|
+
options: {
|
|
4289
|
+
config: { type: "string", description: "Fleet configuration file" },
|
|
4290
|
+
json: { type: "boolean", description: "Machine-readable report" },
|
|
4291
|
+
endpoint: { type: "string", description: "Optional read-only reachability probe" },
|
|
4292
|
+
"ready-file": { type: "string", description: "Validate readiness destination" },
|
|
4293
|
+
"connections-file": { type: "string", description: "Validate private destination" }
|
|
4294
|
+
},
|
|
4295
|
+
async run(values) {
|
|
4296
|
+
try {
|
|
4297
|
+
const report = await doctorFleet(await parse(values.config), {
|
|
4298
|
+
load,
|
|
4299
|
+
...typeof values["ready-file"] === "string" ? { readyFile: values["ready-file"] } : {},
|
|
4300
|
+
...typeof values["connections-file"] === "string" ? { connectionsFile: values["connections-file"] } : {}
|
|
4301
|
+
}, typeof values.endpoint === "string" ? [{ url: values.endpoint }] : []);
|
|
4302
|
+
console.log(values.json ? JSON.stringify(report) : report.findings.map((f) => `${f.ok ? "PASS" : "FAIL"} ${f.check}: ${f.message}`).join("\n"));
|
|
4303
|
+
return report.ok ? 0 : 1;
|
|
4304
|
+
} catch (error) {
|
|
4305
|
+
console.error(error instanceof Error ? error.message : "doctor failed");
|
|
4306
|
+
return 1;
|
|
4307
|
+
}
|
|
4308
|
+
}
|
|
4309
|
+
},
|
|
4310
|
+
"fleet render": {
|
|
4311
|
+
summary: "Render explicit application variables from private namespace discovery",
|
|
4312
|
+
options: {
|
|
4313
|
+
manifest: { type: "string", description: "Public ready manifest file" },
|
|
4314
|
+
connections: { type: "string", description: "Private connection manifest file" },
|
|
4315
|
+
mapping: { type: "string", description: "JSON variable-to-service mapping" },
|
|
4316
|
+
namespace: { type: "string", description: "Explicit namespace" },
|
|
4317
|
+
output: { type: "string", description: "Private destination file" },
|
|
4318
|
+
format: { type: "string", description: "json or dotenv", default: "json" }
|
|
4319
|
+
},
|
|
4320
|
+
async run(values) {
|
|
4321
|
+
try {
|
|
4322
|
+
if (typeof values.namespace !== "string" || typeof values.output !== "string" || !["json", "dotenv"].includes(String(values.format)))
|
|
4323
|
+
throw new Error("--namespace, --output and valid --format required");
|
|
4324
|
+
const manifest = await parse(values.manifest), connections = await parse(values.connections);
|
|
4325
|
+
const output = renderConnections(manifest, connections, values.namespace, await parse(values.mapping));
|
|
4326
|
+
await writeConnections(values.output, output, values.format);
|
|
4327
|
+
console.log(JSON.stringify({ written: values.output, variables: Object.keys(output) }));
|
|
4328
|
+
return 0;
|
|
4329
|
+
} catch (error) {
|
|
4330
|
+
console.error(error instanceof Error ? error.message : "render failed");
|
|
4331
|
+
return 1;
|
|
4332
|
+
}
|
|
4333
|
+
}
|
|
4334
|
+
},
|
|
4335
|
+
"fleet rehearse": {
|
|
4336
|
+
summary: "Restore an archive in an isolated ephemeral fleet, then close it",
|
|
4337
|
+
options: {
|
|
4338
|
+
config: { type: "string", description: "Fleet configuration file" },
|
|
4339
|
+
archive: { type: "string", description: "Saved fleet archive file" }
|
|
4340
|
+
},
|
|
4341
|
+
async run(values) {
|
|
4342
|
+
try {
|
|
4343
|
+
console.log(JSON.stringify(await rehearseFleet(await parse(values.config), { load }, await parse(values.archive))));
|
|
4344
|
+
return 0;
|
|
4345
|
+
} catch (error) {
|
|
4346
|
+
console.error(error instanceof Error ? error.message : "rehearsal failed");
|
|
4347
|
+
return 1;
|
|
4348
|
+
}
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
};
|
|
4352
|
+
}
|
|
4353
|
+
|
|
4354
|
+
// ../../adapters/node/dist/cli.js
|
|
4355
|
+
var optionHelp = (options) => Object.entries(options).map(([name, option]) => {
|
|
4356
|
+
const flag = `--${name}${option.type === "string" ? ` ${option.value ?? "<value>"}` : ""}`;
|
|
4357
|
+
const fallback = option.default !== void 0 ? ` (default: ${String(option.default)})` : "";
|
|
4358
|
+
return ` ${flag.padEnd(30)} ${option.description}${fallback}`;
|
|
4359
|
+
});
|
|
4360
|
+
var help = (spec) => [
|
|
4361
|
+
`${spec.bin} \u2014 ${spec.description}`,
|
|
4362
|
+
"",
|
|
4363
|
+
"Usage:",
|
|
4364
|
+
` ${spec.bin} <command> [options]`,
|
|
4365
|
+
"",
|
|
4366
|
+
"Commands:",
|
|
4367
|
+
...Object.entries(spec.commands).map(([name, c]) => ` ${name.padEnd(30)} ${c.summary}`),
|
|
4368
|
+
"",
|
|
4369
|
+
`Run \`${spec.bin} <command> --help\` for a command's options.`
|
|
4370
|
+
].join("\n");
|
|
4371
|
+
var commandHelp = (spec, name, command) => [
|
|
4372
|
+
`${spec.bin} ${name} \u2014 ${command.summary}`,
|
|
4373
|
+
"",
|
|
4374
|
+
"Usage:",
|
|
4375
|
+
` ${command.usage ?? `${spec.bin} ${name} [options]`}`,
|
|
4376
|
+
...command.options ? ["", "Options:", ...optionHelp(command.options)] : []
|
|
4377
|
+
].join("\n");
|
|
4378
|
+
var runCli = async (spec, argv) => {
|
|
4379
|
+
const own = spec.commands.serve?.fleetTarget;
|
|
4380
|
+
if (own)
|
|
4381
|
+
spec = {
|
|
4382
|
+
...spec,
|
|
4383
|
+
commands: { ...fleetCommands((name2) => loadTarget(name2, own)), ...spec.commands }
|
|
4384
|
+
};
|
|
4385
|
+
const pair = argv.length >= 2 ? `${argv[0]} ${argv[1]}` : void 0;
|
|
4386
|
+
const words = pair !== void 0 && spec.commands[pair] ? 2 : 1;
|
|
4387
|
+
const name = words === 2 ? pair : argv[0];
|
|
4388
|
+
const rest = argv.slice(words);
|
|
4389
|
+
if (name === void 0 || name === "--help" || name === "-h" || name === "help") {
|
|
4390
|
+
console.log(help(spec));
|
|
4391
|
+
return 0;
|
|
4392
|
+
}
|
|
4393
|
+
const command = spec.commands[name];
|
|
4394
|
+
if (!command) {
|
|
4395
|
+
console.error(`${spec.bin}: unknown command ${JSON.stringify(name)}
|
|
4396
|
+
|
|
4397
|
+
${help(spec)}`);
|
|
4398
|
+
return 2;
|
|
4399
|
+
}
|
|
4400
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
4401
|
+
console.log(commandHelp(spec, name, command));
|
|
4402
|
+
return 0;
|
|
4403
|
+
}
|
|
4404
|
+
let parsed;
|
|
4405
|
+
try {
|
|
4406
|
+
parsed = parseArgs({
|
|
4407
|
+
args: rest,
|
|
4408
|
+
allowPositionals: true,
|
|
4409
|
+
strict: true,
|
|
4410
|
+
options: Object.fromEntries(Object.entries(command.options ?? {}).map(([key, option]) => [
|
|
4411
|
+
key,
|
|
4412
|
+
{
|
|
4413
|
+
type: option.type,
|
|
4414
|
+
...option.default !== void 0 ? { default: option.default } : {}
|
|
4415
|
+
}
|
|
4416
|
+
]))
|
|
4417
|
+
});
|
|
4418
|
+
} catch (error) {
|
|
4419
|
+
console.error(`${spec.bin} ${name}: ${error instanceof Error ? error.message : String(error)}
|
|
4420
|
+
|
|
4421
|
+
${commandHelp(spec, name, command)}`);
|
|
4422
|
+
return 2;
|
|
4423
|
+
}
|
|
4424
|
+
return command.run(parsed.values, parsed.positionals);
|
|
4425
|
+
};
|
|
4426
|
+
var COMMON_SERVE_OPTIONS = {
|
|
4427
|
+
port: { type: "string", value: "<port>", description: "Port to listen on" },
|
|
4428
|
+
host: { type: "string", value: "<host>", description: "Interface to bind", default: "127.0.0.1" },
|
|
4429
|
+
"admin-prefix": {
|
|
4430
|
+
type: "string",
|
|
4431
|
+
value: "<path>",
|
|
4432
|
+
description: "Internal API prefix (default /__admin; env MOCKINGBIRD_ADMIN_PREFIX)"
|
|
4433
|
+
},
|
|
4434
|
+
"admin-key": {
|
|
4435
|
+
type: "string",
|
|
4436
|
+
value: "<key>",
|
|
4437
|
+
description: "Require x-mockingbird-admin-key on /__admin/* (env MOCKINGBIRD_ADMIN_KEY)"
|
|
4438
|
+
},
|
|
4439
|
+
seed: {
|
|
4440
|
+
type: "string",
|
|
4441
|
+
value: "<seed>",
|
|
4442
|
+
description: "Seed for every random choice (env MOCKINGBIRD_SEED)"
|
|
4443
|
+
},
|
|
4444
|
+
"admin-controls": {
|
|
4445
|
+
type: "string",
|
|
4446
|
+
value: "<json|file>",
|
|
4447
|
+
description: "Admin controls to boot with, as JSON or a JSON file: clock, latency, faults, snapshot, namespaces (env MOCKINGBIRD_ADMIN_CONTROLS)"
|
|
4448
|
+
},
|
|
4449
|
+
clock: {
|
|
4450
|
+
type: "string",
|
|
4451
|
+
value: "<instant>",
|
|
4452
|
+
description: "Start the clock at this instant (epoch ms or ISO-8601)"
|
|
4453
|
+
},
|
|
4454
|
+
freeze: { type: "boolean", description: "Freeze the clock: it moves only when told to" },
|
|
4455
|
+
"auto-advance": {
|
|
4456
|
+
type: "string",
|
|
4457
|
+
value: "<duration>",
|
|
4458
|
+
description: "Step the clock this much before every request (e.g. 1s)"
|
|
4459
|
+
},
|
|
4460
|
+
latency: {
|
|
4461
|
+
type: "string",
|
|
4462
|
+
value: "<duration>",
|
|
4463
|
+
description: "Baseline response time for every request (e.g. 250ms)"
|
|
4464
|
+
},
|
|
4465
|
+
jitter: {
|
|
4466
|
+
type: "string",
|
|
4467
|
+
value: "<duration>",
|
|
4468
|
+
description: "Seeded extra latency between 0 and this"
|
|
4469
|
+
},
|
|
4470
|
+
"virtual-time": {
|
|
4471
|
+
type: "boolean",
|
|
4472
|
+
description: "Spend latency, delays and timeouts on the mock clock instead of really waiting"
|
|
4473
|
+
},
|
|
4474
|
+
fault: {
|
|
4475
|
+
type: "string",
|
|
4476
|
+
value: "<fault>",
|
|
4477
|
+
description: 'A fault to boot with, as in x-mockingbird-fault: 503, rate_limited, "status=503; delay=2s"'
|
|
4478
|
+
},
|
|
4479
|
+
snapshot: {
|
|
4480
|
+
type: "string",
|
|
4481
|
+
value: "<file>",
|
|
4482
|
+
description: "Start from a snapshot document (GET /__admin/snapshots/:id)"
|
|
4483
|
+
},
|
|
4484
|
+
log: {
|
|
4485
|
+
type: "string",
|
|
4486
|
+
value: "<pretty|json|off>",
|
|
4487
|
+
description: "Request log format",
|
|
4488
|
+
default: "pretty"
|
|
4489
|
+
},
|
|
4490
|
+
"log-requests": {
|
|
4491
|
+
type: "boolean",
|
|
4492
|
+
description: "One JSON line per request: namespace, operationId, status, ids touched (never bodies). Same as --log json"
|
|
4493
|
+
},
|
|
4494
|
+
config: {
|
|
4495
|
+
type: "string",
|
|
4496
|
+
value: "<file>",
|
|
4497
|
+
description: "Serve every service in a mockingbird.json config instead"
|
|
4498
|
+
},
|
|
4499
|
+
"ready-file": {
|
|
4500
|
+
type: "string",
|
|
4501
|
+
description: "Atomically publish fleet endpoint JSON",
|
|
4502
|
+
value: "<file>"
|
|
4503
|
+
},
|
|
4504
|
+
"connections-file": {
|
|
4505
|
+
type: "string",
|
|
4506
|
+
description: "Write private connection URLs with mode 0600",
|
|
4507
|
+
value: "<file>"
|
|
4508
|
+
},
|
|
4509
|
+
"ready-json": {
|
|
4510
|
+
type: "boolean",
|
|
4511
|
+
description: "Emit one public JSON record after fleet readiness"
|
|
4512
|
+
}
|
|
4513
|
+
};
|
|
4514
|
+
var formatLog = (format) => {
|
|
4515
|
+
if (format === "off")
|
|
4516
|
+
return void 0;
|
|
4517
|
+
if (format === "json")
|
|
4518
|
+
return (entry) => console.log(JSON.stringify(entry));
|
|
4519
|
+
return (entry) => {
|
|
4520
|
+
const op = entry.operationId ?? (entry.unmatched ? "UNMATCHED" : "-");
|
|
4521
|
+
const ns = entry.namespace === "default" ? "" : ` [${entry.namespace}]`;
|
|
4522
|
+
const fault = entry.faultId ? ` fault=${entry.faultId}` : "";
|
|
4523
|
+
const adopted = entry.adopted ? " adopted" : "";
|
|
4524
|
+
const received = entry.request ? ` received=${entry.request.bodyBytes === null ? "?" : `${entry.request.bodyBytes}B`} ct=${entry.request.contentType ?? "-"}${entry.request.transferEncoding ? ` te=${entry.request.transferEncoding}` : ""}` : "";
|
|
4525
|
+
const issues = entry.issues?.length ? ` issues=${entry.issues.slice(0, 3).map((issue) => `${issue.path || "$"}: ${issue.message}`).join("; ")}${entry.issues.length > 3 ? ` (+${entry.issues.length - 3})` : ""}` : "";
|
|
4526
|
+
console.log(`${entry.service} ${entry.method} ${entry.path} ${entry.status} ${op} ${entry.durationMs}ms${ns}${fault}${adopted}${received}${issues}`);
|
|
4527
|
+
};
|
|
4528
|
+
};
|
|
4529
|
+
var asString = (value) => typeof value === "string" ? value : void 0;
|
|
4530
|
+
var readJsonFile = async (path, label) => {
|
|
4531
|
+
try {
|
|
4532
|
+
return JSON.parse(await readFile5(path, "utf8"));
|
|
4533
|
+
} catch (error) {
|
|
4534
|
+
throw new Error(`${label}: cannot read ${path}: ${error instanceof Error ? error.message : error}`);
|
|
4535
|
+
}
|
|
4536
|
+
};
|
|
4537
|
+
var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4538
|
+
var resolveServeControls = async (values, env = process.env, base) => {
|
|
4539
|
+
const source = asString(values["admin-controls"]) ?? env.MOCKINGBIRD_ADMIN_CONTROLS;
|
|
4540
|
+
let document = isObject2(base) ? { ...base } : {};
|
|
4541
|
+
if (source !== void 0 && source.trim() !== "") {
|
|
4542
|
+
let parsed;
|
|
4543
|
+
if (source.trim().startsWith("{")) {
|
|
4544
|
+
try {
|
|
4545
|
+
parsed = JSON.parse(source);
|
|
4546
|
+
} catch {
|
|
4547
|
+
throw new Error("--admin-controls: not valid JSON");
|
|
4548
|
+
}
|
|
4549
|
+
} else
|
|
4550
|
+
parsed = await readJsonFile(source, "--admin-controls");
|
|
4551
|
+
if (!isObject2(parsed))
|
|
4552
|
+
throw new Error("--admin-controls: expected a JSON object");
|
|
4553
|
+
document = { ...document, ...parsed };
|
|
4554
|
+
}
|
|
4555
|
+
const clock = isObject2(document.clock) ? { ...document.clock } : {};
|
|
4556
|
+
const now = asString(values.clock);
|
|
4557
|
+
if (now !== void 0)
|
|
4558
|
+
clock.now = now;
|
|
4559
|
+
if (values.freeze === true)
|
|
4560
|
+
clock.frozen = true;
|
|
4561
|
+
const autoAdvance = asString(values["auto-advance"]);
|
|
4562
|
+
if (autoAdvance !== void 0)
|
|
4563
|
+
clock.autoAdvance = autoAdvance;
|
|
4564
|
+
if (Object.keys(clock).length > 0)
|
|
4565
|
+
document.clock = clock;
|
|
4566
|
+
const delay = asString(values.latency);
|
|
4567
|
+
const jitter = asString(values.jitter);
|
|
4568
|
+
if (delay !== void 0 || jitter !== void 0) {
|
|
4569
|
+
const latency = isObject2(document.latency) ? { ...document.latency } : document.latency !== void 0 ? { delay: document.latency } : {};
|
|
4570
|
+
if (delay !== void 0)
|
|
4571
|
+
latency.delay = delay;
|
|
4572
|
+
if (jitter !== void 0)
|
|
4573
|
+
latency.jitter = jitter;
|
|
4574
|
+
document.latency = latency;
|
|
4575
|
+
}
|
|
4576
|
+
if (values["virtual-time"] === true)
|
|
4577
|
+
document.virtualTime = true;
|
|
4578
|
+
const fault = asString(values.fault);
|
|
4579
|
+
if (fault !== void 0)
|
|
4580
|
+
document.faults = [
|
|
4581
|
+
...Array.isArray(document.faults) ? document.faults : [],
|
|
4582
|
+
parseFaultHeader(fault, "--fault")
|
|
4583
|
+
];
|
|
4584
|
+
const snapshot = asString(values.snapshot);
|
|
4585
|
+
if (snapshot !== void 0)
|
|
4586
|
+
document.snapshot = await readJsonFile(snapshot, "--snapshot");
|
|
4587
|
+
if (Object.keys(document).length === 0)
|
|
4588
|
+
return void 0;
|
|
4589
|
+
normalizeBootControls(document, "adminControls");
|
|
4590
|
+
return document;
|
|
4591
|
+
};
|
|
4592
|
+
var loadTarget = async (name, own) => {
|
|
4593
|
+
if (name === own.name)
|
|
4594
|
+
return own;
|
|
4595
|
+
const specifier = `@crvouga/mockingbird-service-${name}/server`;
|
|
4596
|
+
try {
|
|
4597
|
+
let mod;
|
|
4598
|
+
try {
|
|
4599
|
+
mod = await import(specifier);
|
|
4600
|
+
} catch {
|
|
4601
|
+
const fromProject = createRequire3(resolve3(process.cwd(), "package.json"));
|
|
4602
|
+
mod = await import(pathToFileURL(fromProject.resolve(specifier)).href);
|
|
4603
|
+
}
|
|
4604
|
+
if (!mod.serveTarget)
|
|
4605
|
+
throw new Error(`${specifier} exports no serveTarget`);
|
|
4606
|
+
return mod.serveTarget;
|
|
4607
|
+
} catch (error) {
|
|
4608
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
4609
|
+
throw new Error(`cannot load service "${name}": ${reason}. Install @crvouga/mockingbird-service-${name}.`);
|
|
4610
|
+
}
|
|
4611
|
+
};
|
|
4612
|
+
var start = async (target, values, config) => {
|
|
4613
|
+
const adminPrefix = resolveAdminPrefix(config.adminPrefix);
|
|
4614
|
+
const runtime = await target.create(values, {
|
|
4615
|
+
adminPrefix,
|
|
4616
|
+
adminKey: config.adminKey,
|
|
4617
|
+
seed: config.seed,
|
|
4618
|
+
onLog: formatLog(config.log),
|
|
4619
|
+
...config.adminControls !== void 0 ? { adminControls: config.adminControls } : {}
|
|
4620
|
+
});
|
|
4621
|
+
const listening = await listen(runtime, { port: config.port, host: config.host });
|
|
4622
|
+
target.listening?.(listening);
|
|
4623
|
+
console.log(`${target.name} emulator listening on ${listening.url}`);
|
|
4624
|
+
console.log(`${target.name} health: GET ${listening.url}${adminPrefix}/health`);
|
|
4625
|
+
console.log(`${target.name} admin: ${listening.url}${adminPrefix} (${config.adminKey ? "x-mockingbird-admin-key required" : "open \u2014 pass --admin-key to lock"})`);
|
|
4626
|
+
console.log(`${target.name} admin ui: ${listening.url}${adminPrefix}/ui`);
|
|
4627
|
+
for (const line of target.banner?.(runtime) ?? [])
|
|
4628
|
+
console.log(`${target.name} ${line.replaceAll("/__admin", adminPrefix)}`);
|
|
4629
|
+
return listening;
|
|
4630
|
+
};
|
|
4631
|
+
var untilSignal = async (servers) => new Promise((resolve4) => {
|
|
4632
|
+
const stop = async () => {
|
|
4633
|
+
;
|
|
4634
|
+
process.removeListener("SIGINT", stop);
|
|
4635
|
+
process.removeListener("SIGTERM", stop);
|
|
4636
|
+
const results = await Promise.allSettled(servers.map((s) => s.close()));
|
|
4637
|
+
for (const result of results)
|
|
4638
|
+
if (result.status === "rejected")
|
|
4639
|
+
console.error(result.reason instanceof Error ? result.reason.message : String(result.reason));
|
|
4640
|
+
resolve4(results.some((r) => r.status === "rejected") ? 1 : 0);
|
|
4641
|
+
};
|
|
4642
|
+
process.once("SIGINT", stop);
|
|
4643
|
+
process.once("SIGTERM", stop);
|
|
4644
|
+
});
|
|
4645
|
+
var serveCommand = (target) => ({
|
|
4646
|
+
fleetTarget: target,
|
|
4647
|
+
summary: `Serve the ${target.name} emulator or a configured fleet`,
|
|
4648
|
+
options: { ...COMMON_SERVE_OPTIONS, ..."options" in target ? target.options : {} },
|
|
4649
|
+
async run(values) {
|
|
4650
|
+
const log = values["log-requests"] === true ? "json" : asString(values.log) ?? "pretty";
|
|
4651
|
+
if (!["pretty", "json", "off"].includes(log)) {
|
|
4652
|
+
console.error(`--log must be pretty, json or off (got ${log})`);
|
|
4653
|
+
return 2;
|
|
4654
|
+
}
|
|
4655
|
+
const configPath = asString(values.config);
|
|
4656
|
+
if (configPath !== void 0) {
|
|
4657
|
+
try {
|
|
4658
|
+
const config = JSON.parse(await readFile5(configPath, "utf8"));
|
|
4659
|
+
config.adminPrefix = resolveAdminPrefix(config.adminPrefix ?? asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX);
|
|
4660
|
+
const adminKey2 = config.adminKey ?? asString(values["admin-key"]) ?? process.env.MOCKINGBIRD_ADMIN_KEY;
|
|
4661
|
+
if (adminKey2 !== void 0)
|
|
4662
|
+
config.adminKey = adminKey2;
|
|
4663
|
+
const adminControls = await resolveServeControls(values, process.env, config.adminControls);
|
|
4664
|
+
if (adminControls !== void 0)
|
|
4665
|
+
config.adminControls = adminControls;
|
|
4666
|
+
const fleet = await startFleet(config, {
|
|
4667
|
+
load: (name) => loadTarget(name, target),
|
|
4668
|
+
onLog: formatLog(config.log ?? log),
|
|
4669
|
+
...asString(values["ready-file"]) ? { readyFile: asString(values["ready-file"]) } : {},
|
|
4670
|
+
...asString(values["connections-file"]) ? { connectionsFile: asString(values["connections-file"]) } : {},
|
|
4671
|
+
onReady: (manifest) => {
|
|
4672
|
+
if (values["ready-json"])
|
|
4673
|
+
console.log(JSON.stringify(manifest));
|
|
4674
|
+
else
|
|
4675
|
+
for (const [name, endpoint] of Object.entries(manifest.services))
|
|
4676
|
+
console.log(`${name} emulator listening on ${endpoint.url}`);
|
|
4677
|
+
}
|
|
4678
|
+
});
|
|
4679
|
+
return untilSignal([fleet]);
|
|
4680
|
+
} catch (error) {
|
|
4681
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
4682
|
+
return 1;
|
|
4683
|
+
}
|
|
4684
|
+
}
|
|
4685
|
+
if ("start" in target) {
|
|
4686
|
+
console.error("Use --config to supervise protocol services with this command");
|
|
4687
|
+
return 2;
|
|
4688
|
+
}
|
|
4689
|
+
const port = asString(values.port);
|
|
4690
|
+
const adminKey = asString(values["admin-key"]) ?? process.env.MOCKINGBIRD_ADMIN_KEY;
|
|
4691
|
+
const seed = asString(values.seed) ?? process.env.MOCKINGBIRD_SEED;
|
|
4692
|
+
let listening;
|
|
4693
|
+
try {
|
|
4694
|
+
const adminControls = await resolveServeControls(values);
|
|
4695
|
+
listening = await start(target, values, {
|
|
4696
|
+
...adminControls !== void 0 ? { adminControls } : {},
|
|
4697
|
+
adminPrefix: resolveAdminPrefix(asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX),
|
|
4698
|
+
port: port === void 0 ? target.defaultPort : Number.parseInt(port, 10),
|
|
4699
|
+
host: asString(values.host) ?? "127.0.0.1",
|
|
4700
|
+
...adminKey !== void 0 ? { adminKey } : {},
|
|
4701
|
+
...seed !== void 0 ? { seed } : {},
|
|
4702
|
+
log
|
|
4703
|
+
});
|
|
4704
|
+
} catch (error) {
|
|
4705
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
4706
|
+
return 1;
|
|
4707
|
+
}
|
|
4708
|
+
return untilSignal([listening]);
|
|
4709
|
+
}
|
|
4710
|
+
});
|
|
4711
|
+
|
|
4712
|
+
// src/server.ts
|
|
4713
|
+
var DEFAULT_PORT = 8811;
|
|
4714
|
+
var createServer2 = async (options = {}) => {
|
|
4715
|
+
const { port, host, ...rest } = options;
|
|
4716
|
+
const runtime = createRuntime(rest);
|
|
4717
|
+
return { ...await listen(runtime, { port: port ?? 0, ...host ? { host } : {} }), runtime };
|
|
4718
|
+
};
|
|
4719
|
+
var serveTarget = {
|
|
4720
|
+
name: "cognito",
|
|
4721
|
+
defaultPort: DEFAULT_PORT,
|
|
4722
|
+
options: {
|
|
4723
|
+
"pool-id": { type: "string", value: "<id>", description: "User pool id" },
|
|
4724
|
+
"client-id": { type: "string", value: "<id>", description: "App client id" },
|
|
4725
|
+
region: { type: "string", value: "<region>", description: "AWS region (default us-east-1)" }
|
|
4726
|
+
},
|
|
4727
|
+
create: (values, common) => createRuntime({
|
|
4728
|
+
...typeof values["pool-id"] === "string" ? { poolId: values["pool-id"] } : {},
|
|
4729
|
+
...typeof values["client-id"] === "string" ? { clientId: values["client-id"] } : {},
|
|
4730
|
+
...typeof values.region === "string" ? { region: values.region } : {},
|
|
4731
|
+
...common.adminPrefix !== void 0 ? { adminPrefix: common.adminPrefix } : {},
|
|
4732
|
+
...common.adminKey !== void 0 ? { adminKey: common.adminKey } : {},
|
|
4733
|
+
...common.adminControls !== void 0 ? { adminControls: common.adminControls } : {},
|
|
4734
|
+
...common.seed !== void 0 ? { seed: common.seed } : {},
|
|
4735
|
+
...common.onLog ? { onLog: common.onLog } : {}
|
|
4736
|
+
}),
|
|
4737
|
+
banner: () => [
|
|
4738
|
+
"AWS SDK endpoint: set COGNITO_ENDPOINT to this URL",
|
|
4739
|
+
"admin: POST /__admin/users; GET /__admin/codes?username=\u2026"
|
|
4740
|
+
]
|
|
4741
|
+
};
|
|
4742
|
+
|
|
4743
|
+
export {
|
|
4744
|
+
runCli,
|
|
4745
|
+
serveCommand,
|
|
4746
|
+
DEFAULT_PORT,
|
|
4747
|
+
createServer2 as createServer,
|
|
4748
|
+
serveTarget
|
|
4749
|
+
};
|
|
4750
|
+
//# sourceMappingURL=chunk-K7FBL7SU.js.map
|