@crvouga/mockingbird-runtime 0.0.0-stage → 0.0.2
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/README.md +131 -2
- package/dist/adapter-node.d.ts +2521 -0
- package/dist/adapter-node.js +6920 -0
- package/dist/adapter-node.js.map +7 -0
- package/dist/admin-ui.js +25 -0
- package/dist/admin-ui.js.map +7 -0
- package/dist/chunk-2KDYLZXC.js +118 -0
- package/dist/chunk-2KDYLZXC.js.map +7 -0
- package/dist/chunk-2Y25NPCV.js +1289 -0
- package/dist/chunk-2Y25NPCV.js.map +7 -0
- package/dist/chunk-4SAK6MQT.js +246 -0
- package/dist/chunk-4SAK6MQT.js.map +7 -0
- package/dist/chunk-ANCCZFF2.js +778 -0
- package/dist/chunk-ANCCZFF2.js.map +7 -0
- package/dist/chunk-EBGYPQPA.js +198 -0
- package/dist/chunk-EBGYPQPA.js.map +7 -0
- package/dist/chunk-EQLY2YOA.js +274 -0
- package/dist/chunk-EQLY2YOA.js.map +7 -0
- package/dist/chunk-G62YDIIJ.js +7664 -0
- package/dist/chunk-G62YDIIJ.js.map +7 -0
- package/dist/chunk-HU6IOAFC.js +437 -0
- package/dist/chunk-HU6IOAFC.js.map +7 -0
- package/dist/chunk-RNAHPNUJ.js +97 -0
- package/dist/chunk-RNAHPNUJ.js.map +7 -0
- package/dist/chunk-UP4QOSFD.js +87 -0
- package/dist/chunk-UP4QOSFD.js.map +7 -0
- package/dist/chunk-XNS34BPF.js +2489 -0
- package/dist/chunk-XNS34BPF.js.map +7 -0
- package/dist/core.d.ts +356 -0
- package/dist/core.js +51 -0
- package/dist/core.js.map +7 -0
- package/dist/crypto-XS5XCMRJ.js +21 -0
- package/dist/crypto-XS5XCMRJ.js.map +7 -0
- package/dist/helpers-UD7CG4WQ.js +18 -0
- package/dist/helpers-UD7CG4WQ.js.map +7 -0
- package/dist/http-codec.d.ts +72 -0
- package/dist/http-codec.js +25 -0
- package/dist/http-codec.js.map +7 -0
- package/dist/http-provider.d.ts +3243 -0
- package/dist/http-provider.js +37920 -0
- package/dist/http-provider.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +7 -0
- package/dist/mqtt-broker/node.d.ts +632 -0
- package/dist/mqtt-broker/node.js +355 -0
- package/dist/mqtt-broker/node.js.map +7 -0
- package/dist/mqtt-broker.d.ts +990 -0
- package/dist/mqtt-broker.js +913 -0
- package/dist/mqtt-broker.js.map +7 -0
- package/dist/openapi-metadata.d.ts +290 -0
- package/dist/openapi-metadata.js +28 -0
- package/dist/openapi-metadata.js.map +7 -0
- package/dist/openapi.d.ts +252 -0
- package/dist/openapi.js +45 -0
- package/dist/openapi.js.map +7 -0
- package/dist/preview/artifacts.d.ts +19 -0
- package/dist/preview/artifacts.js +40 -0
- package/dist/preview/artifacts.js.map +7 -0
- package/dist/preview/bridge.d.ts +21 -0
- package/dist/preview/bridge.js +9 -0
- package/dist/preview/bridge.js.map +7 -0
- package/dist/preview/harness.d.ts +12 -0
- package/dist/preview/harness.js +65 -0
- package/dist/preview/harness.js.map +7 -0
- package/dist/preview/host.d.ts +30 -0
- package/dist/preview/host.js +70 -0
- package/dist/preview/host.js.map +7 -0
- package/dist/service/admin.d.ts +106 -0
- package/dist/service/admin.js +340 -0
- package/dist/service/admin.js.map +7 -0
- package/dist/service.d.ts +2665 -0
- package/dist/service.js +264 -0
- package/dist/service.js.map +7 -0
- package/dist/sqlite.d.ts +69 -0
- package/dist/sqlite.js +19 -0
- package/dist/sqlite.js.map +7 -0
- package/dist/ui.d.ts +37 -0
- package/dist/ui.js +702 -0
- package/dist/ui.js.map +7 -0
- package/package.json +135 -4
|
@@ -0,0 +1,2489 @@
|
|
|
1
|
+
import {
|
|
2
|
+
requestBaseUrl
|
|
3
|
+
} from "./chunk-2Y25NPCV.js";
|
|
4
|
+
|
|
5
|
+
// ../service/core/dist/journal.js
|
|
6
|
+
var DEFAULT_JOURNAL_SIZE = 1e3;
|
|
7
|
+
var createJournal = (size = DEFAULT_JOURNAL_SIZE) => {
|
|
8
|
+
const capacity = Math.max(0, Math.floor(size));
|
|
9
|
+
const rings = /* @__PURE__ */ new Map();
|
|
10
|
+
let sequence = 0;
|
|
11
|
+
const order = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
const inOrder = (ring) => ring.entries.length < capacity ? ring.entries : [...ring.entries.slice(ring.next), ...ring.entries.slice(0, ring.next)];
|
|
13
|
+
return {
|
|
14
|
+
size: capacity,
|
|
15
|
+
record(entry) {
|
|
16
|
+
if (capacity === 0)
|
|
17
|
+
return;
|
|
18
|
+
order.set(entry, sequence++);
|
|
19
|
+
let ring = rings.get(entry.namespace);
|
|
20
|
+
if (!ring) {
|
|
21
|
+
ring = { entries: [], next: 0 };
|
|
22
|
+
rings.set(entry.namespace, ring);
|
|
23
|
+
}
|
|
24
|
+
if (ring.entries.length < capacity)
|
|
25
|
+
ring.entries.push(entry);
|
|
26
|
+
else {
|
|
27
|
+
ring.entries[ring.next] = entry;
|
|
28
|
+
ring.next = (ring.next + 1) % capacity;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
list(query = {}) {
|
|
32
|
+
const source = query.namespace !== void 0 ? inOrder(rings.get(query.namespace) ?? { entries: [], next: 0 }) : [...rings.values()].flatMap(inOrder).sort((a, b) => (order.get(a) ?? 0) - (order.get(b) ?? 0));
|
|
33
|
+
const matched = source.filter((entry) => (query.operationId === void 0 || entry.operationId === query.operationId) && (query.method === void 0 || entry.method.toUpperCase() === query.method.toUpperCase()) && (query.path === void 0 || entry.path === query.path) && (query.faultId === void 0 || entry.faultId === query.faultId) && (query.status === void 0 || entry.status === query.status) && (query.since === void 0 || Date.parse(entry.at) >= query.since));
|
|
34
|
+
if (query.limit === void 0)
|
|
35
|
+
return matched;
|
|
36
|
+
return query.limit <= 0 ? [] : matched.slice(-query.limit);
|
|
37
|
+
},
|
|
38
|
+
clear(namespace) {
|
|
39
|
+
if (namespace === void 0)
|
|
40
|
+
rings.clear();
|
|
41
|
+
else
|
|
42
|
+
rings.delete(namespace);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
var notes = /* @__PURE__ */ new WeakMap();
|
|
47
|
+
var annotateResponse = (response, extra) => {
|
|
48
|
+
const existing = notes.get(response);
|
|
49
|
+
notes.set(response, {
|
|
50
|
+
...existing,
|
|
51
|
+
...extra,
|
|
52
|
+
...existing?.ids || extra.ids ? { ids: { ...existing?.ids, ...extra.ids } } : {}
|
|
53
|
+
});
|
|
54
|
+
return response;
|
|
55
|
+
};
|
|
56
|
+
var responseNotes = (response) => notes.get(response);
|
|
57
|
+
|
|
58
|
+
// ../service/core/dist/clock.js
|
|
59
|
+
var createClock = (source = Date.now) => {
|
|
60
|
+
let offsetMs = 0;
|
|
61
|
+
let frozenAt;
|
|
62
|
+
const now = () => frozenAt ?? source() + offsetMs;
|
|
63
|
+
return {
|
|
64
|
+
now,
|
|
65
|
+
set: (epochMs) => {
|
|
66
|
+
if (frozenAt !== void 0)
|
|
67
|
+
frozenAt = epochMs;
|
|
68
|
+
else
|
|
69
|
+
offsetMs = epochMs - source();
|
|
70
|
+
},
|
|
71
|
+
advance: (deltaMs) => {
|
|
72
|
+
if (frozenAt !== void 0)
|
|
73
|
+
frozenAt += deltaMs;
|
|
74
|
+
else
|
|
75
|
+
offsetMs += deltaMs;
|
|
76
|
+
},
|
|
77
|
+
freeze: () => {
|
|
78
|
+
frozenAt = now();
|
|
79
|
+
},
|
|
80
|
+
unfreeze: () => {
|
|
81
|
+
if (frozenAt === void 0)
|
|
82
|
+
return;
|
|
83
|
+
offsetMs = frozenAt - source();
|
|
84
|
+
frozenAt = void 0;
|
|
85
|
+
},
|
|
86
|
+
reset: () => {
|
|
87
|
+
offsetMs = 0;
|
|
88
|
+
frozenAt = void 0;
|
|
89
|
+
},
|
|
90
|
+
// A frozen clock does not follow its source, so it has no offset to report.
|
|
91
|
+
state: () => ({
|
|
92
|
+
now: now(),
|
|
93
|
+
frozen: frozenAt !== void 0,
|
|
94
|
+
offsetMs: frozenAt !== void 0 ? 0 : offsetMs
|
|
95
|
+
})
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
var deriveClock = (base, moved = () => {
|
|
99
|
+
}) => {
|
|
100
|
+
const own = createClock(() => base.now());
|
|
101
|
+
const moving = (change) => (...args) => {
|
|
102
|
+
change(...args);
|
|
103
|
+
moved();
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
...own,
|
|
107
|
+
set: moving(own.set),
|
|
108
|
+
advance: moving(own.advance),
|
|
109
|
+
reset: moving(own.reset),
|
|
110
|
+
state: () => {
|
|
111
|
+
const state = own.state();
|
|
112
|
+
const frozen = state.frozen || base.state().frozen;
|
|
113
|
+
return { now: state.now, frozen, offsetMs: frozen ? 0 : state.offsetMs };
|
|
114
|
+
},
|
|
115
|
+
pinned: () => own.state().frozen,
|
|
116
|
+
place: moving(({ now, pinned }) => {
|
|
117
|
+
own.reset();
|
|
118
|
+
if (pinned)
|
|
119
|
+
own.freeze();
|
|
120
|
+
own.set(now);
|
|
121
|
+
})
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
var restoreClock = (clock, state) => {
|
|
125
|
+
clock.freeze();
|
|
126
|
+
clock.set(state.now);
|
|
127
|
+
if (!state.frozen)
|
|
128
|
+
clock.unfreeze();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// ../service/core/dist/upstream.js
|
|
132
|
+
var upstreamState = (value) => {
|
|
133
|
+
if (typeof value !== "object" || value === null)
|
|
134
|
+
throw new Error("expected upstream settings");
|
|
135
|
+
const state = value;
|
|
136
|
+
if (state.mode !== "emulated" && state.mode !== "forward")
|
|
137
|
+
throw new Error("invalid upstream mode");
|
|
138
|
+
const url = new URL(state.baseUrl);
|
|
139
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash)
|
|
140
|
+
throw new Error("upstream baseUrl must be an HTTP URL without credentials, query or fragment");
|
|
141
|
+
return { mode: state.mode, baseUrl: url.toString() };
|
|
142
|
+
};
|
|
143
|
+
var upstreamRequest = (request, baseUrl) => {
|
|
144
|
+
const source = new URL(request.url);
|
|
145
|
+
const target = new URL(baseUrl);
|
|
146
|
+
target.pathname = `${target.pathname.replace(/\/$/, "")}${source.pathname}`;
|
|
147
|
+
target.search = source.search;
|
|
148
|
+
const headers = new Headers(request.headers);
|
|
149
|
+
for (const name2 of [...headers.keys()]) {
|
|
150
|
+
if (name2.toLowerCase().startsWith("x-mockingbird-") || name2.toLowerCase() === "host")
|
|
151
|
+
headers.delete(name2);
|
|
152
|
+
}
|
|
153
|
+
return new Request(target, {
|
|
154
|
+
method: request.method,
|
|
155
|
+
headers,
|
|
156
|
+
signal: request.signal,
|
|
157
|
+
...request.method !== "GET" && request.method !== "HEAD" ? { body: request.body, duplex: "half" } : {}
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// ../service/core/dist/controls.js
|
|
162
|
+
var ControlError = class extends RangeError {
|
|
163
|
+
constructor(message) {
|
|
164
|
+
super(message);
|
|
165
|
+
this.name = "ControlError";
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
var UNITS = {
|
|
169
|
+
ms: 1,
|
|
170
|
+
s: 1e3,
|
|
171
|
+
m: 6e4,
|
|
172
|
+
h: 36e5,
|
|
173
|
+
d: 864e5
|
|
174
|
+
};
|
|
175
|
+
var parseDuration = (value) => {
|
|
176
|
+
if (typeof value === "number")
|
|
177
|
+
return Number.isFinite(value) ? value : void 0;
|
|
178
|
+
if (typeof value !== "string")
|
|
179
|
+
return void 0;
|
|
180
|
+
const match = /^(-?\d+(?:\.\d+)?)\s*(ms|s|m|h|d)?$/.exec(value.trim());
|
|
181
|
+
if (!match)
|
|
182
|
+
return void 0;
|
|
183
|
+
return Number(match[1]) * UNITS[match[2] ?? "ms"];
|
|
184
|
+
};
|
|
185
|
+
var parseInstant = (value) => {
|
|
186
|
+
if (typeof value === "number")
|
|
187
|
+
return Number.isFinite(value) ? value : void 0;
|
|
188
|
+
if (typeof value !== "string")
|
|
189
|
+
return void 0;
|
|
190
|
+
const text = value.trim();
|
|
191
|
+
if (/^-?\d+$/.test(text))
|
|
192
|
+
return Number(text);
|
|
193
|
+
const parsed = Date.parse(text);
|
|
194
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
195
|
+
};
|
|
196
|
+
var durationMs = (value, label, negative = false) => {
|
|
197
|
+
const parsed = parseDuration(value);
|
|
198
|
+
if (parsed === void 0)
|
|
199
|
+
throw new ControlError(`${label}: expected milliseconds or a duration like "250ms" or "15m"`);
|
|
200
|
+
if (!negative && parsed < 0)
|
|
201
|
+
throw new ControlError(`${label}: must not be negative`);
|
|
202
|
+
return parsed;
|
|
203
|
+
};
|
|
204
|
+
var instantMs = (value, label) => {
|
|
205
|
+
const parsed = parseInstant(value);
|
|
206
|
+
if (parsed === void 0)
|
|
207
|
+
throw new ControlError(`${label}: expected epoch ms or ISO-8601`);
|
|
208
|
+
return parsed;
|
|
209
|
+
};
|
|
210
|
+
var normalizeSeed = (seed, label = "seed") => {
|
|
211
|
+
if (typeof seed === "number" && Number.isFinite(seed))
|
|
212
|
+
return seed;
|
|
213
|
+
if (typeof seed === "string" && seed !== "")
|
|
214
|
+
return /^\d+$/.test(seed) ? Number(seed) : seed;
|
|
215
|
+
throw new ControlError(`${label}: expected a number or a non-empty string`);
|
|
216
|
+
};
|
|
217
|
+
var SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
218
|
+
var NAMESPACE_KEYS = /* @__PURE__ */ new Set(["seed", "clock", "virtualTime", "latency", "faults", "snapshot"]);
|
|
219
|
+
var MOCK_KEYS = /* @__PURE__ */ new Set([...NAMESPACE_KEYS, "credentials", "namespaces"]);
|
|
220
|
+
var REPORTED_KEYS = /* @__PURE__ */ new Set(["namespace", "snapshots"]);
|
|
221
|
+
var REPORTED_CLOCK_KEYS = /* @__PURE__ */ new Set(["iso", "offsetMs"]);
|
|
222
|
+
var KEY_PATTERN = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
223
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
224
|
+
var unknownKeys = (value, known, label, reported) => {
|
|
225
|
+
for (const key of Object.keys(value)) {
|
|
226
|
+
if (!known.has(key) && !reported?.has(key))
|
|
227
|
+
throw new ControlError(`${label}: unknown control ${JSON.stringify(key)}; expected ${[...known].join(", ")}`);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
var latencyRule = (value, label) => {
|
|
231
|
+
if (!isRecord(value))
|
|
232
|
+
return { delayMs: durationMs(value, label), jitterMs: 0 };
|
|
233
|
+
unknownKeys(value, /* @__PURE__ */ new Set(["delay", "jitter", "delayMs", "jitterMs", "operations"]), label);
|
|
234
|
+
return {
|
|
235
|
+
delayMs: durationMs(value.delay ?? value.delayMs ?? 0, `${label}.delay`),
|
|
236
|
+
jitterMs: durationMs(value.jitter ?? value.jitterMs ?? 0, `${label}.jitter`)
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
var normalizeLatency = (value, label = "latency") => {
|
|
240
|
+
const base = latencyRule(value, label);
|
|
241
|
+
const operations = {};
|
|
242
|
+
if (isRecord(value) && value.operations !== void 0) {
|
|
243
|
+
if (!isRecord(value.operations))
|
|
244
|
+
throw new ControlError(`${label}.operations: expected an object keyed by operationId`);
|
|
245
|
+
for (const [operationId, rule] of Object.entries(value.operations)) {
|
|
246
|
+
if (isRecord(rule) && rule.operations !== void 0)
|
|
247
|
+
throw new ControlError(`${label}.operations.${operationId}: cannot nest operations`);
|
|
248
|
+
operations[operationId] = latencyRule(rule, `${label}.operations.${operationId}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { ...base, operations };
|
|
252
|
+
};
|
|
253
|
+
var normalizeClock = (value, label = "clock") => {
|
|
254
|
+
if (!isRecord(value))
|
|
255
|
+
throw new ControlError(`${label}: expected an object`);
|
|
256
|
+
unknownKeys(value, /* @__PURE__ */ new Set(["now", "set", "frozen", "freeze", "autoAdvance", "autoAdvanceMs"]), label, REPORTED_CLOCK_KEYS);
|
|
257
|
+
const plan = {};
|
|
258
|
+
const now = value.now ?? value.set;
|
|
259
|
+
if (now !== void 0)
|
|
260
|
+
plan.now = instantMs(now, `${label}.now`);
|
|
261
|
+
const frozen = value.frozen ?? value.freeze;
|
|
262
|
+
if (frozen !== void 0) {
|
|
263
|
+
if (typeof frozen !== "boolean")
|
|
264
|
+
throw new ControlError(`${label}.frozen: expected a boolean`);
|
|
265
|
+
plan.frozen = frozen;
|
|
266
|
+
}
|
|
267
|
+
const auto = value.autoAdvance ?? value.autoAdvanceMs;
|
|
268
|
+
if (auto !== void 0)
|
|
269
|
+
plan.autoAdvanceMs = durationMs(auto, `${label}.autoAdvance`);
|
|
270
|
+
return plan;
|
|
271
|
+
};
|
|
272
|
+
var assertSnapshotDocument = (value, label = "snapshot") => {
|
|
273
|
+
if (!isRecord(value))
|
|
274
|
+
throw new ControlError(`${label}: expected a snapshot document`);
|
|
275
|
+
if (value.format !== SNAPSHOT_FORMAT)
|
|
276
|
+
throw new ControlError(`${label}.format: expected ${JSON.stringify(SNAPSHOT_FORMAT)}`);
|
|
277
|
+
if (value.upstream !== void 0) {
|
|
278
|
+
try {
|
|
279
|
+
upstreamState(value.upstream);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
throw new ControlError(`${label}.upstream: ${error instanceof Error ? error.message : "invalid settings"}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (!Array.isArray(value.records))
|
|
285
|
+
throw new ControlError(`${label}.records: expected a list`);
|
|
286
|
+
value.records.forEach((row, index) => {
|
|
287
|
+
if (!isRecord(row) || typeof row.collection !== "string" || typeof row.id !== "string" || row.value === void 0 && typeof row.raw !== "string" || row.seq !== void 0 && !Number.isSafeInteger(row.seq))
|
|
288
|
+
throw new ControlError(`${label}.records[${index}]: expected {collection, id, value, seq?}`);
|
|
289
|
+
});
|
|
290
|
+
if (value.sequences !== void 0) {
|
|
291
|
+
if (!Array.isArray(value.sequences))
|
|
292
|
+
throw new ControlError(`${label}.sequences: expected a list`);
|
|
293
|
+
value.sequences.forEach((row, index) => {
|
|
294
|
+
if (!isRecord(row) || typeof row.name !== "string" || typeof row.kind !== "string" || !Number.isSafeInteger(row.value))
|
|
295
|
+
throw new ControlError(`${label}.sequences[${index}]: expected {name, kind, value}`);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
if (value.clock !== void 0) {
|
|
299
|
+
if (!isRecord(value.clock) || typeof value.clock.now !== "number" || !Number.isFinite(value.clock.now) || typeof value.clock.frozen !== "boolean")
|
|
300
|
+
throw new ControlError(`${label}.clock: expected {now, frozen}`);
|
|
301
|
+
}
|
|
302
|
+
return value;
|
|
303
|
+
};
|
|
304
|
+
var normalizeControls = (value, label = "controls") => {
|
|
305
|
+
if (!isRecord(value))
|
|
306
|
+
throw new ControlError(`${label}: expected an object`);
|
|
307
|
+
unknownKeys(value, NAMESPACE_KEYS, label, REPORTED_KEYS);
|
|
308
|
+
const plan = {};
|
|
309
|
+
if (value.seed !== void 0)
|
|
310
|
+
plan.seed = normalizeSeed(value.seed, `${label}.seed`);
|
|
311
|
+
if (value.clock !== void 0)
|
|
312
|
+
plan.clock = normalizeClock(value.clock, `${label}.clock`);
|
|
313
|
+
if (value.virtualTime !== void 0) {
|
|
314
|
+
if (typeof value.virtualTime !== "boolean")
|
|
315
|
+
throw new ControlError(`${label}.virtualTime: expected a boolean`);
|
|
316
|
+
plan.virtualTime = value.virtualTime;
|
|
317
|
+
}
|
|
318
|
+
if (value.latency !== void 0)
|
|
319
|
+
plan.latency = normalizeLatency(value.latency, `${label}.latency`);
|
|
320
|
+
if (value.faults !== void 0) {
|
|
321
|
+
if (!Array.isArray(value.faults) || !value.faults.every(isRecord))
|
|
322
|
+
throw new ControlError(`${label}.faults: expected a list of fault rules`);
|
|
323
|
+
plan.faults = value.faults;
|
|
324
|
+
}
|
|
325
|
+
if (value.snapshot !== void 0)
|
|
326
|
+
plan.snapshot = assertSnapshotDocument(value.snapshot, `${label}.snapshot`);
|
|
327
|
+
return plan;
|
|
328
|
+
};
|
|
329
|
+
var normalizeBootControls = (value, label = "controls") => {
|
|
330
|
+
if (value === void 0)
|
|
331
|
+
return { shared: {}, namespaces: {}, credentials: {} };
|
|
332
|
+
if (!isRecord(value))
|
|
333
|
+
throw new ControlError(`${label}: expected an object`);
|
|
334
|
+
unknownKeys(value, MOCK_KEYS, label);
|
|
335
|
+
const { credentials, namespaces, ...shared } = value;
|
|
336
|
+
const plan = {
|
|
337
|
+
shared: normalizeControls(shared, label),
|
|
338
|
+
namespaces: {},
|
|
339
|
+
credentials: {}
|
|
340
|
+
};
|
|
341
|
+
if (namespaces !== void 0) {
|
|
342
|
+
if (!isRecord(namespaces))
|
|
343
|
+
throw new ControlError(`${label}.namespaces: expected an object`);
|
|
344
|
+
for (const [name2, each] of Object.entries(namespaces)) {
|
|
345
|
+
if (!KEY_PATTERN.test(name2))
|
|
346
|
+
throw new ControlError(`${label}.namespaces: bad namespace ${JSON.stringify(name2)}`);
|
|
347
|
+
plan.namespaces[name2] = normalizeControls(each, `${label}.namespaces.${name2}`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (credentials !== void 0) {
|
|
351
|
+
if (!isRecord(credentials))
|
|
352
|
+
throw new ControlError(`${label}.credentials: expected an object`);
|
|
353
|
+
for (const [credential, namespace] of Object.entries(credentials)) {
|
|
354
|
+
if (typeof namespace !== "string" || !KEY_PATTERN.test(namespace))
|
|
355
|
+
throw new ControlError(`${label}.credentials: bad namespace for a credential`);
|
|
356
|
+
plan.credentials[credential] = namespace;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return plan;
|
|
360
|
+
};
|
|
361
|
+
var CONTROL_HEADERS = {
|
|
362
|
+
namespace: "x-mockingbird-namespace",
|
|
363
|
+
adminKey: "x-mockingbird-admin-key",
|
|
364
|
+
branch: "x-mockingbird-branch",
|
|
365
|
+
at: "x-mockingbird-at",
|
|
366
|
+
reset: "x-mockingbird-reset",
|
|
367
|
+
upstream: "x-mockingbird-upstream",
|
|
368
|
+
restore: "x-mockingbird-restore",
|
|
369
|
+
seed: "x-mockingbird-seed",
|
|
370
|
+
time: "x-mockingbird-time",
|
|
371
|
+
advance: "x-mockingbird-advance",
|
|
372
|
+
virtualTime: "x-mockingbird-virtual-time",
|
|
373
|
+
fault: "x-mockingbird-fault",
|
|
374
|
+
delay: "x-mockingbird-delay",
|
|
375
|
+
timeout: "x-mockingbird-timeout",
|
|
376
|
+
snapshot: "x-mockingbird-snapshot"
|
|
377
|
+
};
|
|
378
|
+
var CONTROL_HEADER_REFERENCE = [
|
|
379
|
+
{
|
|
380
|
+
header: CONTROL_HEADERS.upstream,
|
|
381
|
+
value: "emulated | forward",
|
|
382
|
+
scope: "request",
|
|
383
|
+
guarded: true,
|
|
384
|
+
summary: "Select the injected upstream or emulator for one request; requires configured upstream transport for forward."
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
header: CONTROL_HEADERS.namespace,
|
|
388
|
+
value: "<key>",
|
|
389
|
+
scope: "select",
|
|
390
|
+
guarded: false,
|
|
391
|
+
summary: "Isolation key. Each key has its own records, clock, random streams, faults, latency, journal and snapshots."
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
header: CONTROL_HEADERS.branch,
|
|
395
|
+
value: "<name>",
|
|
396
|
+
scope: "select",
|
|
397
|
+
guarded: false,
|
|
398
|
+
summary: "Read and write a named copy-on-write branch of the namespace's history."
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
header: CONTROL_HEADERS.at,
|
|
402
|
+
value: "<checkpoint>",
|
|
403
|
+
scope: "select",
|
|
404
|
+
guarded: false,
|
|
405
|
+
summary: "Read the namespace as it was at a checkpoint. With a branch, start the branch there."
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
header: CONTROL_HEADERS.reset,
|
|
409
|
+
value: "true",
|
|
410
|
+
scope: "namespace",
|
|
411
|
+
guarded: true,
|
|
412
|
+
summary: "Reset the namespace to its boot state before the request is handled."
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
header: CONTROL_HEADERS.restore,
|
|
416
|
+
value: "<snapshot>",
|
|
417
|
+
scope: "namespace",
|
|
418
|
+
guarded: true,
|
|
419
|
+
summary: "Restore a snapshot before the request is handled."
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
header: CONTROL_HEADERS.seed,
|
|
423
|
+
value: "<seed>",
|
|
424
|
+
scope: "namespace",
|
|
425
|
+
guarded: true,
|
|
426
|
+
summary: "Restart the namespace's random streams from this seed."
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
header: CONTROL_HEADERS.time,
|
|
430
|
+
value: "<epoch ms | ISO-8601>",
|
|
431
|
+
scope: "namespace",
|
|
432
|
+
guarded: true,
|
|
433
|
+
summary: "Freeze the namespace clock at this instant."
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
header: CONTROL_HEADERS.advance,
|
|
437
|
+
value: "<duration>",
|
|
438
|
+
scope: "namespace",
|
|
439
|
+
guarded: true,
|
|
440
|
+
summary: "Move the namespace clock by this much, forward or (negative) back."
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
header: CONTROL_HEADERS.virtualTime,
|
|
444
|
+
value: "true | false",
|
|
445
|
+
scope: "namespace",
|
|
446
|
+
guarded: true,
|
|
447
|
+
summary: "Spend injected waits on the mock clock (true) or the wall clock (false)."
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
header: CONTROL_HEADERS.delay,
|
|
451
|
+
value: "<duration>",
|
|
452
|
+
scope: "request",
|
|
453
|
+
guarded: true,
|
|
454
|
+
summary: "Hold this response back this long, on top of the namespace's latency."
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
header: CONTROL_HEADERS.timeout,
|
|
458
|
+
value: "<duration>",
|
|
459
|
+
scope: "request",
|
|
460
|
+
guarded: true,
|
|
461
|
+
summary: "Never answer this request: hold it this long, then drop the connection."
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
header: CONTROL_HEADERS.fault,
|
|
465
|
+
value: '<status> | drop | <preset> | key=value; \u2026 | {"status": 503, \u2026}',
|
|
466
|
+
scope: "request",
|
|
467
|
+
guarded: true,
|
|
468
|
+
summary: "Fault this request only. Stored fault rules are not consulted when it answers."
|
|
469
|
+
},
|
|
470
|
+
{
|
|
471
|
+
header: CONTROL_HEADERS.snapshot,
|
|
472
|
+
value: "<name>",
|
|
473
|
+
scope: "namespace",
|
|
474
|
+
guarded: true,
|
|
475
|
+
summary: "Snapshot the namespace after the request is handled. Echoed on the response."
|
|
476
|
+
}
|
|
477
|
+
];
|
|
478
|
+
var flag = (value, header) => {
|
|
479
|
+
const text = value.trim().toLowerCase();
|
|
480
|
+
if (text === "true" || text === "1")
|
|
481
|
+
return true;
|
|
482
|
+
if (text === "false" || text === "0")
|
|
483
|
+
return false;
|
|
484
|
+
throw new ControlError(`${header}: expected true or false`);
|
|
485
|
+
};
|
|
486
|
+
var name = (value, header) => {
|
|
487
|
+
const text = value.trim();
|
|
488
|
+
if (!KEY_PATTERN.test(text))
|
|
489
|
+
throw new ControlError(`${header}: must match ${KEY_PATTERN}`);
|
|
490
|
+
return text;
|
|
491
|
+
};
|
|
492
|
+
var FAULT_PAIRS = /* @__PURE__ */ new Set([
|
|
493
|
+
"status",
|
|
494
|
+
"delay",
|
|
495
|
+
"jitter",
|
|
496
|
+
"timeout",
|
|
497
|
+
"drop",
|
|
498
|
+
"truncate",
|
|
499
|
+
"effect",
|
|
500
|
+
"preset",
|
|
501
|
+
"body"
|
|
502
|
+
]);
|
|
503
|
+
var parseFaultHeader = (value, header = CONTROL_HEADERS.fault) => {
|
|
504
|
+
const text = value.trim();
|
|
505
|
+
if (text === "")
|
|
506
|
+
throw new ControlError(`${header}: expected a fault`);
|
|
507
|
+
if (text.startsWith("{")) {
|
|
508
|
+
let parsed;
|
|
509
|
+
try {
|
|
510
|
+
parsed = JSON.parse(text);
|
|
511
|
+
} catch {
|
|
512
|
+
throw new ControlError(`${header}: not valid JSON`);
|
|
513
|
+
}
|
|
514
|
+
if (!isRecord(parsed))
|
|
515
|
+
throw new ControlError(`${header}: expected a JSON object`);
|
|
516
|
+
return parsed;
|
|
517
|
+
}
|
|
518
|
+
const rule = {};
|
|
519
|
+
const headers = {};
|
|
520
|
+
for (const [index, raw] of text.split(";").entries()) {
|
|
521
|
+
const part = raw.trim();
|
|
522
|
+
if (part === "")
|
|
523
|
+
continue;
|
|
524
|
+
const equals = part.indexOf("=");
|
|
525
|
+
if (equals < 0) {
|
|
526
|
+
if (index > 0 && part !== "drop")
|
|
527
|
+
throw new ControlError(`${header}: expected key=value, got ${JSON.stringify(part)}`);
|
|
528
|
+
if (/^\d{3}$/.test(part))
|
|
529
|
+
rule.status = Number(part);
|
|
530
|
+
else if (part === "drop")
|
|
531
|
+
rule.drop = true;
|
|
532
|
+
else
|
|
533
|
+
rule.preset = name(part, header);
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const key = part.slice(0, equals).trim().toLowerCase();
|
|
537
|
+
const item = part.slice(equals + 1).trim();
|
|
538
|
+
if (key.startsWith("header."))
|
|
539
|
+
headers[key.slice("header.".length)] = item;
|
|
540
|
+
else if (!FAULT_PAIRS.has(key))
|
|
541
|
+
throw new ControlError(`${header}: unknown key ${JSON.stringify(key)}; expected ${[...FAULT_PAIRS].join(", ")} or header.<name>`);
|
|
542
|
+
else if (key === "status" || key === "truncate") {
|
|
543
|
+
if (!/^\d+$/.test(item))
|
|
544
|
+
throw new ControlError(`${header}: ${key} must be a number`);
|
|
545
|
+
rule[key] = Number(item);
|
|
546
|
+
} else if (key === "drop")
|
|
547
|
+
rule.drop = flag(item, `${header} drop`);
|
|
548
|
+
else
|
|
549
|
+
rule[key] = item;
|
|
550
|
+
}
|
|
551
|
+
if (Object.keys(headers).length > 0)
|
|
552
|
+
rule.headers = headers;
|
|
553
|
+
if (Object.keys(rule).length === 0)
|
|
554
|
+
throw new ControlError(`${header}: expected a fault`);
|
|
555
|
+
return rule;
|
|
556
|
+
};
|
|
557
|
+
var GUARDED = CONTROL_HEADER_REFERENCE.filter((each) => each.guarded).map((each) => each.header);
|
|
558
|
+
var hasGuardedControlHeader = (headers) => GUARDED.some((header) => headers.has(header));
|
|
559
|
+
var parseControlHeaders = (headers) => {
|
|
560
|
+
if (!hasGuardedControlHeader(headers))
|
|
561
|
+
return void 0;
|
|
562
|
+
const controls = {};
|
|
563
|
+
const read = (key) => headers.get(CONTROL_HEADERS[key]);
|
|
564
|
+
const upstream = read("upstream");
|
|
565
|
+
if (upstream !== null) {
|
|
566
|
+
if (upstream !== "emulated" && upstream !== "forward")
|
|
567
|
+
throw new ControlError("x-mockingbird-upstream: expected emulated or forward");
|
|
568
|
+
controls.upstream = upstream;
|
|
569
|
+
}
|
|
570
|
+
const reset = read("reset");
|
|
571
|
+
if (reset !== null && flag(reset, CONTROL_HEADERS.reset))
|
|
572
|
+
controls.reset = true;
|
|
573
|
+
const restore = read("restore");
|
|
574
|
+
if (restore !== null)
|
|
575
|
+
controls.restore = name(restore, CONTROL_HEADERS.restore);
|
|
576
|
+
const seed = read("seed");
|
|
577
|
+
if (seed !== null)
|
|
578
|
+
controls.seed = normalizeSeed(seed.trim(), CONTROL_HEADERS.seed);
|
|
579
|
+
const time = read("time");
|
|
580
|
+
if (time !== null)
|
|
581
|
+
controls.time = instantMs(time, CONTROL_HEADERS.time);
|
|
582
|
+
const advance = read("advance");
|
|
583
|
+
if (advance !== null)
|
|
584
|
+
controls.advance = durationMs(advance, CONTROL_HEADERS.advance, true);
|
|
585
|
+
const virtualTime = read("virtualTime");
|
|
586
|
+
if (virtualTime !== null)
|
|
587
|
+
controls.virtualTime = flag(virtualTime, CONTROL_HEADERS.virtualTime);
|
|
588
|
+
const delay = read("delay");
|
|
589
|
+
if (delay !== null)
|
|
590
|
+
controls.delay = durationMs(delay, CONTROL_HEADERS.delay);
|
|
591
|
+
const timeout = read("timeout");
|
|
592
|
+
if (timeout !== null)
|
|
593
|
+
controls.timeout = durationMs(timeout, CONTROL_HEADERS.timeout);
|
|
594
|
+
const fault = read("fault");
|
|
595
|
+
if (fault !== null)
|
|
596
|
+
controls.fault = parseFaultHeader(fault);
|
|
597
|
+
const snapshot = read("snapshot");
|
|
598
|
+
if (snapshot !== null)
|
|
599
|
+
controls.snapshot = name(snapshot, CONTROL_HEADERS.snapshot);
|
|
600
|
+
return controls;
|
|
601
|
+
};
|
|
602
|
+
var controlHeaders = (input) => {
|
|
603
|
+
const headers = {};
|
|
604
|
+
const set = (key, value) => {
|
|
605
|
+
headers[CONTROL_HEADERS[key]] = value;
|
|
606
|
+
};
|
|
607
|
+
if (input.upstream !== void 0) {
|
|
608
|
+
if (input.upstream !== "emulated" && input.upstream !== "forward")
|
|
609
|
+
throw new ControlError("upstream: expected emulated or forward");
|
|
610
|
+
set("upstream", input.upstream);
|
|
611
|
+
}
|
|
612
|
+
if (input.namespace !== void 0)
|
|
613
|
+
set("namespace", name(input.namespace, "namespace"));
|
|
614
|
+
if (input.adminKey !== void 0)
|
|
615
|
+
set("adminKey", input.adminKey);
|
|
616
|
+
if (input.branch !== void 0)
|
|
617
|
+
set("branch", name(input.branch, "branch"));
|
|
618
|
+
if (input.at !== void 0)
|
|
619
|
+
set("at", input.at);
|
|
620
|
+
if (input.reset === true)
|
|
621
|
+
set("reset", "true");
|
|
622
|
+
if (input.restore !== void 0)
|
|
623
|
+
set("restore", name(input.restore, "restore"));
|
|
624
|
+
if (input.seed !== void 0)
|
|
625
|
+
set("seed", String(normalizeSeed(input.seed)));
|
|
626
|
+
if (input.time !== void 0) {
|
|
627
|
+
const time = input.time instanceof Date ? input.time.getTime() : input.time;
|
|
628
|
+
set("time", new Date(instantMs(time, "time")).toISOString());
|
|
629
|
+
}
|
|
630
|
+
if (input.advance !== void 0)
|
|
631
|
+
set("advance", String(durationMs(input.advance, "advance", true)));
|
|
632
|
+
if (input.virtualTime !== void 0)
|
|
633
|
+
set("virtualTime", String(input.virtualTime));
|
|
634
|
+
if (input.delay !== void 0)
|
|
635
|
+
set("delay", String(durationMs(input.delay, "delay")));
|
|
636
|
+
if (input.timeout !== void 0)
|
|
637
|
+
set("timeout", String(durationMs(input.timeout, "timeout")));
|
|
638
|
+
if (input.fault !== void 0) {
|
|
639
|
+
const fault = typeof input.fault === "object" ? JSON.stringify(input.fault) : String(input.fault);
|
|
640
|
+
parseFaultHeader(fault, "fault");
|
|
641
|
+
set("fault", fault);
|
|
642
|
+
}
|
|
643
|
+
if (input.snapshot !== void 0)
|
|
644
|
+
set("snapshot", name(input.snapshot, "snapshot"));
|
|
645
|
+
return headers;
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
// ../service/core/dist/rng.js
|
|
649
|
+
var seedFrom = (value) => {
|
|
650
|
+
let hash = 2166136261;
|
|
651
|
+
for (let i = 0; i < value.length; i++) {
|
|
652
|
+
hash ^= value.charCodeAt(i);
|
|
653
|
+
hash = Math.imul(hash, 16777619);
|
|
654
|
+
}
|
|
655
|
+
return hash >>> 0;
|
|
656
|
+
};
|
|
657
|
+
var createRng = (seed = 0) => {
|
|
658
|
+
const toNumeric = (value) => typeof value === "string" ? seedFrom(value) : value >>> 0;
|
|
659
|
+
let numeric = toNumeric(seed);
|
|
660
|
+
let state = numeric;
|
|
661
|
+
const next = () => {
|
|
662
|
+
state = state + 1831565813 >>> 0;
|
|
663
|
+
let t = state;
|
|
664
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
665
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
666
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
667
|
+
};
|
|
668
|
+
return {
|
|
669
|
+
next,
|
|
670
|
+
int: (min, max) => min + Math.floor(next() * (max - min + 1)),
|
|
671
|
+
reset: () => {
|
|
672
|
+
state = numeric;
|
|
673
|
+
},
|
|
674
|
+
state: () => state,
|
|
675
|
+
setState: (next2) => {
|
|
676
|
+
if (!Number.isSafeInteger(next2) || next2 < 0 || next2 > 4294967295) {
|
|
677
|
+
throw new RangeError("rng state must be an unsigned 32-bit integer");
|
|
678
|
+
}
|
|
679
|
+
state = next2 >>> 0;
|
|
680
|
+
},
|
|
681
|
+
reseed: (next2) => {
|
|
682
|
+
numeric = toNumeric(next2);
|
|
683
|
+
state = numeric;
|
|
684
|
+
},
|
|
685
|
+
get seed() {
|
|
686
|
+
return numeric;
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
// ../service/core/dist/faults.js
|
|
692
|
+
var STANDARD_FAULT_PRESETS = {
|
|
693
|
+
server_error: {
|
|
694
|
+
description: "The vendor answers 500.",
|
|
695
|
+
rules: [{ status: 500 }]
|
|
696
|
+
},
|
|
697
|
+
bad_gateway: {
|
|
698
|
+
description: "A proxy in front of the vendor answers 502.",
|
|
699
|
+
rules: [{ status: 502 }]
|
|
700
|
+
},
|
|
701
|
+
unavailable: {
|
|
702
|
+
description: "The vendor is down: 503 with retry-after.",
|
|
703
|
+
rules: [{ status: 503, headers: { "retry-after": "1" } }]
|
|
704
|
+
},
|
|
705
|
+
gateway_timeout: {
|
|
706
|
+
description: "A proxy gives up on the vendor: 504.",
|
|
707
|
+
rules: [{ status: 504 }]
|
|
708
|
+
},
|
|
709
|
+
rate_limited: {
|
|
710
|
+
description: "Too many requests: 429 with retry-after.",
|
|
711
|
+
rules: [{ status: 429, headers: { "retry-after": "1" } }]
|
|
712
|
+
},
|
|
713
|
+
unauthorized: {
|
|
714
|
+
description: "The credential is rejected: 401.",
|
|
715
|
+
rules: [{ status: 401 }]
|
|
716
|
+
},
|
|
717
|
+
forbidden: {
|
|
718
|
+
description: "The credential lacks permission: 403.",
|
|
719
|
+
rules: [{ status: 403 }]
|
|
720
|
+
},
|
|
721
|
+
slow: {
|
|
722
|
+
description: "Every response takes two more seconds.",
|
|
723
|
+
rules: [{ delayMs: 2e3 }]
|
|
724
|
+
},
|
|
725
|
+
timeout: {
|
|
726
|
+
description: "The vendor never answers: the request hangs 30 seconds, then the connection drops.",
|
|
727
|
+
rules: [{ timeoutMs: 3e4 }]
|
|
728
|
+
},
|
|
729
|
+
connection_reset: {
|
|
730
|
+
description: "The connection dies before any response.",
|
|
731
|
+
rules: [{ drop: true }]
|
|
732
|
+
},
|
|
733
|
+
truncated_response: {
|
|
734
|
+
description: "The response starts, then the connection dies mid-body.",
|
|
735
|
+
rules: [{ truncate: 0 }]
|
|
736
|
+
},
|
|
737
|
+
flaky: {
|
|
738
|
+
description: "Half of the requests answer 503, chosen by the seed.",
|
|
739
|
+
rules: [{ status: 503, rate: 0.5 }]
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
var GLOBAL = "*";
|
|
743
|
+
var RULE_KEYS = /* @__PURE__ */ new Set([
|
|
744
|
+
"id",
|
|
745
|
+
"operationId",
|
|
746
|
+
"method",
|
|
747
|
+
"path",
|
|
748
|
+
"pathPrefix",
|
|
749
|
+
"pathPattern",
|
|
750
|
+
"requestHeaders",
|
|
751
|
+
"query",
|
|
752
|
+
"requestBody",
|
|
753
|
+
"namespace",
|
|
754
|
+
"after",
|
|
755
|
+
"every",
|
|
756
|
+
"count",
|
|
757
|
+
"rate",
|
|
758
|
+
"from",
|
|
759
|
+
"until",
|
|
760
|
+
"status",
|
|
761
|
+
"body",
|
|
762
|
+
"headers",
|
|
763
|
+
"delay",
|
|
764
|
+
"delayMs",
|
|
765
|
+
"latencyMs",
|
|
766
|
+
"jitter",
|
|
767
|
+
"jitterMs",
|
|
768
|
+
"timeout",
|
|
769
|
+
"timeoutMs",
|
|
770
|
+
"drop",
|
|
771
|
+
"truncate",
|
|
772
|
+
"effect",
|
|
773
|
+
"params",
|
|
774
|
+
"fromPreset"
|
|
775
|
+
]);
|
|
776
|
+
var LISTING_KEYS = /* @__PURE__ */ new Set(["remaining", "hits", "seen"]);
|
|
777
|
+
var stringMap = (value, label) => {
|
|
778
|
+
if (!isRecord(value) || !Object.values(value).every((each) => typeof each === "string"))
|
|
779
|
+
throw new ControlError(`${label}: expected an object of strings`);
|
|
780
|
+
return value;
|
|
781
|
+
};
|
|
782
|
+
var requestBodyMap = (value) => {
|
|
783
|
+
if (!isRecord(value))
|
|
784
|
+
throw new ControlError("fault.requestBody: expected an object");
|
|
785
|
+
for (const [path, expected] of Object.entries(value)) {
|
|
786
|
+
if (path === "" || path.split(".").includes(""))
|
|
787
|
+
throw new ControlError(`fault.requestBody: ${JSON.stringify(path)} is not a dotted path`);
|
|
788
|
+
const scalar = expected === null || typeof expected === "string" || typeof expected === "boolean" || typeof expected === "number" && Number.isFinite(expected);
|
|
789
|
+
if (!scalar)
|
|
790
|
+
throw new ControlError(`fault.requestBody.${path}: expected a string, number, boolean or null`);
|
|
791
|
+
}
|
|
792
|
+
return value;
|
|
793
|
+
};
|
|
794
|
+
var count = (value, label, min) => {
|
|
795
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < min)
|
|
796
|
+
throw new ControlError(`${label}: expected an integer of at least ${min}`);
|
|
797
|
+
return value;
|
|
798
|
+
};
|
|
799
|
+
var windowEdge = (value, label, now) => {
|
|
800
|
+
if (typeof value === "string" && /[a-z]\s*$/i.test(value) && !/[T:Z]/.test(value)) {
|
|
801
|
+
const relative = parseDuration(value);
|
|
802
|
+
if (relative !== void 0) {
|
|
803
|
+
if (now === void 0)
|
|
804
|
+
throw new ControlError(`${label}: a duration needs a clock`);
|
|
805
|
+
return now + relative;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
const instant = parseInstant(value);
|
|
809
|
+
if (instant === void 0)
|
|
810
|
+
throw new ControlError(`${label}: expected epoch ms, ISO-8601, or a duration like "5m"`);
|
|
811
|
+
return instant;
|
|
812
|
+
};
|
|
813
|
+
var faultHasAction = (rule) => rule.status !== void 0 || (rule.delayMs ?? 0) > 0 || (rule.jitterMs ?? 0) > 0 || rule.timeoutMs !== void 0 || rule.drop === true || rule.truncate !== void 0 || rule.effect !== void 0;
|
|
814
|
+
var normalizeFault = (input, context = {}) => {
|
|
815
|
+
if (!isRecord(input))
|
|
816
|
+
throw new ControlError("fault: expected an object");
|
|
817
|
+
if (context.strict) {
|
|
818
|
+
for (const key of Object.keys(input)) {
|
|
819
|
+
if (!RULE_KEYS.has(key) && !LISTING_KEYS.has(key))
|
|
820
|
+
throw new ControlError(`fault: unknown field ${JSON.stringify(key)}`);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const id = input.id ?? context.id;
|
|
824
|
+
if (typeof id !== "string" || id === "")
|
|
825
|
+
throw new ControlError("fault.id: expected a string");
|
|
826
|
+
const rule = { id };
|
|
827
|
+
for (const key of [
|
|
828
|
+
"operationId",
|
|
829
|
+
"method",
|
|
830
|
+
"path",
|
|
831
|
+
"pathPrefix",
|
|
832
|
+
"effect",
|
|
833
|
+
"fromPreset"
|
|
834
|
+
]) {
|
|
835
|
+
const value = input[key];
|
|
836
|
+
if (value === void 0)
|
|
837
|
+
continue;
|
|
838
|
+
if (typeof value !== "string")
|
|
839
|
+
throw new ControlError(`fault.${key}: expected a string`);
|
|
840
|
+
rule[key] = value;
|
|
841
|
+
}
|
|
842
|
+
if (input.pathPattern !== void 0) {
|
|
843
|
+
if (typeof input.pathPattern !== "string")
|
|
844
|
+
throw new ControlError("fault.pathPattern: expected a string");
|
|
845
|
+
try {
|
|
846
|
+
new RegExp(input.pathPattern);
|
|
847
|
+
} catch {
|
|
848
|
+
throw new ControlError("fault.pathPattern: not a valid regular expression");
|
|
849
|
+
}
|
|
850
|
+
rule.pathPattern = input.pathPattern;
|
|
851
|
+
}
|
|
852
|
+
if (input.requestHeaders !== void 0)
|
|
853
|
+
rule.requestHeaders = Object.fromEntries(Object.entries(stringMap(input.requestHeaders, "fault.requestHeaders")).map(([k, v]) => [
|
|
854
|
+
k.toLowerCase(),
|
|
855
|
+
v
|
|
856
|
+
]));
|
|
857
|
+
if (input.query !== void 0)
|
|
858
|
+
rule.query = stringMap(input.query, "fault.query");
|
|
859
|
+
if (input.requestBody !== void 0)
|
|
860
|
+
rule.requestBody = requestBodyMap(input.requestBody);
|
|
861
|
+
const namespace = input.namespace ?? context.namespace;
|
|
862
|
+
if (namespace !== void 0) {
|
|
863
|
+
if (typeof namespace !== "string" || namespace !== GLOBAL && !KEY_PATTERN.test(namespace))
|
|
864
|
+
throw new ControlError(`fault.namespace: expected "*" or a key matching ${KEY_PATTERN}`);
|
|
865
|
+
rule.namespace = namespace;
|
|
866
|
+
}
|
|
867
|
+
if (input.after !== void 0)
|
|
868
|
+
rule.after = count(input.after, "fault.after", 0);
|
|
869
|
+
if (input.every !== void 0)
|
|
870
|
+
rule.every = count(input.every, "fault.every", 1);
|
|
871
|
+
if (input.count !== void 0)
|
|
872
|
+
rule.count = count(input.count, "fault.count", 0);
|
|
873
|
+
if (input.rate !== void 0) {
|
|
874
|
+
if (typeof input.rate !== "number" || !(input.rate >= 0 && input.rate <= 1))
|
|
875
|
+
throw new ControlError("fault.rate: expected a number from 0 to 1");
|
|
876
|
+
rule.rate = input.rate;
|
|
877
|
+
}
|
|
878
|
+
if (input.from !== void 0)
|
|
879
|
+
rule.from = windowEdge(input.from, "fault.from", context.now);
|
|
880
|
+
if (input.until !== void 0)
|
|
881
|
+
rule.until = windowEdge(input.until, "fault.until", context.now);
|
|
882
|
+
if (input.status !== void 0) {
|
|
883
|
+
if (typeof input.status !== "number" || !Number.isInteger(input.status) || input.status < 200 || input.status > 599)
|
|
884
|
+
throw new ControlError("fault.status: expected an HTTP status from 200 to 599");
|
|
885
|
+
rule.status = input.status;
|
|
886
|
+
}
|
|
887
|
+
if (input.body !== void 0)
|
|
888
|
+
rule.body = input.body;
|
|
889
|
+
if (input.headers !== void 0)
|
|
890
|
+
rule.headers = stringMap(input.headers, "fault.headers");
|
|
891
|
+
const delay = input.delay ?? input.delayMs ?? input.latencyMs;
|
|
892
|
+
if (delay !== void 0)
|
|
893
|
+
rule.delayMs = durationMs(delay, "fault.delay");
|
|
894
|
+
const jitter = input.jitter ?? input.jitterMs;
|
|
895
|
+
if (jitter !== void 0)
|
|
896
|
+
rule.jitterMs = durationMs(jitter, "fault.jitter");
|
|
897
|
+
const timeout = input.timeout ?? input.timeoutMs;
|
|
898
|
+
if (timeout !== void 0)
|
|
899
|
+
rule.timeoutMs = durationMs(timeout, "fault.timeout");
|
|
900
|
+
if (input.drop !== void 0) {
|
|
901
|
+
if (typeof input.drop !== "boolean")
|
|
902
|
+
throw new ControlError("fault.drop: expected a boolean");
|
|
903
|
+
if (input.drop)
|
|
904
|
+
rule.drop = true;
|
|
905
|
+
}
|
|
906
|
+
if (input.truncate !== void 0)
|
|
907
|
+
rule.truncate = count(input.truncate, "fault.truncate", 0);
|
|
908
|
+
if (input.params !== void 0) {
|
|
909
|
+
if (!isRecord(input.params))
|
|
910
|
+
throw new ControlError("fault.params: expected an object");
|
|
911
|
+
rule.params = input.params;
|
|
912
|
+
}
|
|
913
|
+
return rule;
|
|
914
|
+
};
|
|
915
|
+
var scopeOf = (rule) => rule.namespace === void 0 || rule.namespace === GLOBAL ? GLOBAL : rule.namespace;
|
|
916
|
+
var matchesAll = (wanted, read) => {
|
|
917
|
+
if (!wanted)
|
|
918
|
+
return true;
|
|
919
|
+
for (const [key, value] of Object.entries(wanted)) {
|
|
920
|
+
const actual = read(key);
|
|
921
|
+
if (actual === null || actual === void 0)
|
|
922
|
+
return false;
|
|
923
|
+
if (value !== "*" && actual !== value)
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
return true;
|
|
927
|
+
};
|
|
928
|
+
var field = (body, path) => {
|
|
929
|
+
let at = body;
|
|
930
|
+
for (const step of path.split(".")) {
|
|
931
|
+
if (Array.isArray(at)) {
|
|
932
|
+
if (!/^(0|[1-9]\d*)$/.test(step))
|
|
933
|
+
return void 0;
|
|
934
|
+
at = at[Number(step)];
|
|
935
|
+
} else if (isRecord(at) && Object.hasOwn(at, step))
|
|
936
|
+
at = at[step];
|
|
937
|
+
else
|
|
938
|
+
return void 0;
|
|
939
|
+
}
|
|
940
|
+
return at;
|
|
941
|
+
};
|
|
942
|
+
var bodyMatches = (wanted, body) => {
|
|
943
|
+
if (!isRecord(body))
|
|
944
|
+
return false;
|
|
945
|
+
for (const [path, expected] of Object.entries(wanted)) {
|
|
946
|
+
const actual = field(body, path);
|
|
947
|
+
if (actual === void 0)
|
|
948
|
+
return false;
|
|
949
|
+
if (expected !== "*" && actual !== expected)
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
return true;
|
|
953
|
+
};
|
|
954
|
+
var matches = (entry, candidate, body) => {
|
|
955
|
+
const { rule } = entry;
|
|
956
|
+
if (entry.scope !== GLOBAL && entry.scope !== candidate.namespace)
|
|
957
|
+
return false;
|
|
958
|
+
if (rule.operationId !== void 0 && rule.operationId !== candidate.operationId)
|
|
959
|
+
return false;
|
|
960
|
+
if (rule.method !== void 0 && rule.method.toUpperCase() !== candidate.method.toUpperCase()) {
|
|
961
|
+
return false;
|
|
962
|
+
}
|
|
963
|
+
if (rule.path !== void 0 && rule.path !== candidate.path)
|
|
964
|
+
return false;
|
|
965
|
+
if (rule.pathPrefix !== void 0 && !candidate.path.startsWith(rule.pathPrefix))
|
|
966
|
+
return false;
|
|
967
|
+
if (entry.pattern && !entry.pattern.test(candidate.path))
|
|
968
|
+
return false;
|
|
969
|
+
if (!matchesAll(rule.requestHeaders, (name2) => candidate.headers?.get(name2)))
|
|
970
|
+
return false;
|
|
971
|
+
if (!matchesAll(rule.query, (name2) => candidate.query?.get(name2)))
|
|
972
|
+
return false;
|
|
973
|
+
if (rule.requestBody && !bodyMatches(rule.requestBody, body.value))
|
|
974
|
+
return false;
|
|
975
|
+
if (rule.from !== void 0 || rule.until !== void 0) {
|
|
976
|
+
if (candidate.now === void 0)
|
|
977
|
+
return false;
|
|
978
|
+
if (rule.from !== void 0 && candidate.now < rule.from)
|
|
979
|
+
return false;
|
|
980
|
+
if (rule.until !== void 0 && candidate.now >= rule.until)
|
|
981
|
+
return false;
|
|
982
|
+
}
|
|
983
|
+
return true;
|
|
984
|
+
};
|
|
985
|
+
var faultResponse = (rule) => {
|
|
986
|
+
const status = rule.status ?? 500;
|
|
987
|
+
const headers = { "content-type": "application/json", ...rule.headers };
|
|
988
|
+
if (typeof rule.body === "string")
|
|
989
|
+
return new Response(rule.body, { status, headers });
|
|
990
|
+
if (rule.body === null)
|
|
991
|
+
return new Response(null, { status, headers: rule.headers ?? {} });
|
|
992
|
+
const body = rule.body === void 0 ? { detail: "Injected by Mockingbird" } : rule.body;
|
|
993
|
+
return new Response(JSON.stringify(body), { status, headers });
|
|
994
|
+
};
|
|
995
|
+
var listing = (entry) => ({
|
|
996
|
+
...entry.rule,
|
|
997
|
+
remaining: entry.remaining,
|
|
998
|
+
hits: entry.hits,
|
|
999
|
+
seen: entry.seen
|
|
1000
|
+
});
|
|
1001
|
+
var createFaultRegistry = (random = createRng(0), sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))) => {
|
|
1002
|
+
const entries = [];
|
|
1003
|
+
const counters = /* @__PURE__ */ new Map();
|
|
1004
|
+
const own = /* @__PURE__ */ new Map();
|
|
1005
|
+
const stream = typeof random === "function" ? random : (namespace) => {
|
|
1006
|
+
if (namespace === "default")
|
|
1007
|
+
return random;
|
|
1008
|
+
let found = own.get(namespace);
|
|
1009
|
+
if (!found) {
|
|
1010
|
+
found = createRng(random.seed);
|
|
1011
|
+
own.set(namespace, found);
|
|
1012
|
+
}
|
|
1013
|
+
return found;
|
|
1014
|
+
};
|
|
1015
|
+
const toEntry = (input, previous) => {
|
|
1016
|
+
const rule = normalizeFault(input);
|
|
1017
|
+
return {
|
|
1018
|
+
rule,
|
|
1019
|
+
scope: scopeOf(rule),
|
|
1020
|
+
remaining: previous?.remaining !== void 0 ? previous.remaining : rule.count ?? null,
|
|
1021
|
+
hits: previous?.hits ?? 0,
|
|
1022
|
+
seen: previous?.seen ?? 0,
|
|
1023
|
+
pattern: rule.pathPattern !== void 0 ? new RegExp(rule.pathPattern) : void 0
|
|
1024
|
+
};
|
|
1025
|
+
};
|
|
1026
|
+
const find = (id, namespace) => {
|
|
1027
|
+
if (namespace === void 0)
|
|
1028
|
+
return entries.findIndex((e) => e.rule.id === id);
|
|
1029
|
+
const mine = entries.findIndex((e) => e.rule.id === id && e.scope === namespace);
|
|
1030
|
+
return mine >= 0 ? mine : entries.findIndex((e) => e.rule.id === id && e.scope === GLOBAL);
|
|
1031
|
+
};
|
|
1032
|
+
const drop = (namespace) => {
|
|
1033
|
+
for (let i = entries.length - 1; i >= 0; i--)
|
|
1034
|
+
if (entries[i]?.scope === namespace)
|
|
1035
|
+
entries.splice(i, 1);
|
|
1036
|
+
};
|
|
1037
|
+
return {
|
|
1038
|
+
snapshot(namespace) {
|
|
1039
|
+
const rules = [];
|
|
1040
|
+
let globals = 0;
|
|
1041
|
+
for (const entry of entries) {
|
|
1042
|
+
if (entry.scope === GLOBAL)
|
|
1043
|
+
globals++;
|
|
1044
|
+
else if (entry.scope === namespace)
|
|
1045
|
+
rules.push({ ...listing(entry), position: globals });
|
|
1046
|
+
}
|
|
1047
|
+
return { rules: structuredClone(rules), nextId: counters.get(namespace) ?? 0 };
|
|
1048
|
+
},
|
|
1049
|
+
add(input) {
|
|
1050
|
+
const entry = toEntry(input);
|
|
1051
|
+
const existing = entries.findIndex((e) => e.rule.id === entry.rule.id && e.scope === entry.scope);
|
|
1052
|
+
if (existing >= 0)
|
|
1053
|
+
entries[existing] = entry;
|
|
1054
|
+
else
|
|
1055
|
+
entries.push(entry);
|
|
1056
|
+
return entry.rule;
|
|
1057
|
+
},
|
|
1058
|
+
nextId(namespace) {
|
|
1059
|
+
let next = counters.get(namespace) ?? 0;
|
|
1060
|
+
let id;
|
|
1061
|
+
do {
|
|
1062
|
+
next++;
|
|
1063
|
+
id = `fault_${next}`;
|
|
1064
|
+
} while (entries.some((e) => e.rule.id === id && e.scope === namespace));
|
|
1065
|
+
counters.set(namespace, next);
|
|
1066
|
+
return id;
|
|
1067
|
+
},
|
|
1068
|
+
list: (namespace) => entries.filter((e) => namespace === void 0 || e.scope === namespace || e.scope === GLOBAL).map(listing),
|
|
1069
|
+
get(id, namespace) {
|
|
1070
|
+
const entry = entries[find(id, namespace)];
|
|
1071
|
+
return entry ? listing(entry) : void 0;
|
|
1072
|
+
},
|
|
1073
|
+
remove(id, namespace) {
|
|
1074
|
+
const index = find(id, namespace);
|
|
1075
|
+
if (index < 0)
|
|
1076
|
+
return false;
|
|
1077
|
+
entries.splice(index, 1);
|
|
1078
|
+
return true;
|
|
1079
|
+
},
|
|
1080
|
+
clear(namespace) {
|
|
1081
|
+
if (namespace === void 0) {
|
|
1082
|
+
entries.length = 0;
|
|
1083
|
+
counters.clear();
|
|
1084
|
+
if (typeof random !== "function")
|
|
1085
|
+
random.reset();
|
|
1086
|
+
own.clear();
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
drop(namespace);
|
|
1090
|
+
counters.delete(namespace);
|
|
1091
|
+
},
|
|
1092
|
+
restore(namespace, state) {
|
|
1093
|
+
drop(namespace);
|
|
1094
|
+
for (const { remaining, hits, seen, position, ...rule } of state.rules) {
|
|
1095
|
+
const entry = toEntry({ ...rule, namespace }, { remaining, hits, seen });
|
|
1096
|
+
let globals = 0;
|
|
1097
|
+
let index = entries.length;
|
|
1098
|
+
if (position !== void 0) {
|
|
1099
|
+
for (const [at, each] of entries.entries()) {
|
|
1100
|
+
if (each.scope !== GLOBAL)
|
|
1101
|
+
continue;
|
|
1102
|
+
if (globals === position) {
|
|
1103
|
+
index = at;
|
|
1104
|
+
break;
|
|
1105
|
+
}
|
|
1106
|
+
globals++;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
entries.splice(index, 0, entry);
|
|
1110
|
+
}
|
|
1111
|
+
counters.set(namespace, state.nextId ?? 0);
|
|
1112
|
+
},
|
|
1113
|
+
async take(candidate) {
|
|
1114
|
+
const hits = [];
|
|
1115
|
+
const wait = candidate.wait ?? sleep;
|
|
1116
|
+
const draw = stream(candidate.namespace);
|
|
1117
|
+
const body = { value: void 0 };
|
|
1118
|
+
let read = false;
|
|
1119
|
+
for (const entry of [...entries]) {
|
|
1120
|
+
if (entry.remaining === 0)
|
|
1121
|
+
continue;
|
|
1122
|
+
if (entry.rule.requestBody && !read) {
|
|
1123
|
+
read = true;
|
|
1124
|
+
body.value = await candidate.readJson?.().catch(() => void 0);
|
|
1125
|
+
}
|
|
1126
|
+
if (!matches(entry, candidate, body))
|
|
1127
|
+
continue;
|
|
1128
|
+
const { rule } = entry;
|
|
1129
|
+
entry.seen++;
|
|
1130
|
+
const position = entry.seen - (rule.after ?? 0);
|
|
1131
|
+
if (position <= 0)
|
|
1132
|
+
continue;
|
|
1133
|
+
if (rule.every !== void 0 && position % rule.every !== 0)
|
|
1134
|
+
continue;
|
|
1135
|
+
if (draw.next() >= (rule.rate ?? 1))
|
|
1136
|
+
continue;
|
|
1137
|
+
entry.hits++;
|
|
1138
|
+
if (entry.remaining !== null)
|
|
1139
|
+
entry.remaining--;
|
|
1140
|
+
const hit = await act(rule, draw, wait);
|
|
1141
|
+
hits.push(hit);
|
|
1142
|
+
if (hit.drop || hit.response)
|
|
1143
|
+
break;
|
|
1144
|
+
}
|
|
1145
|
+
return hits;
|
|
1146
|
+
},
|
|
1147
|
+
async fire(rules, candidate) {
|
|
1148
|
+
const hits = [];
|
|
1149
|
+
for (const rule of rules) {
|
|
1150
|
+
const hit = await act(rule, stream(candidate.namespace), candidate.wait ?? sleep);
|
|
1151
|
+
hits.push(hit);
|
|
1152
|
+
if (hit.drop || hit.response)
|
|
1153
|
+
break;
|
|
1154
|
+
}
|
|
1155
|
+
return hits;
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
};
|
|
1159
|
+
var act = async (rule, draw, wait) => {
|
|
1160
|
+
const jitter = (rule.jitterMs ?? 0) > 0 ? draw.int(0, Math.floor(rule.jitterMs ?? 0)) : 0;
|
|
1161
|
+
const delay = (rule.delayMs ?? 0) + jitter;
|
|
1162
|
+
if (delay > 0)
|
|
1163
|
+
await wait(delay);
|
|
1164
|
+
const hit = { id: rule.id };
|
|
1165
|
+
if (rule.effect !== void 0)
|
|
1166
|
+
hit.effect = { name: rule.effect, params: rule.params ?? {} };
|
|
1167
|
+
if (rule.truncate !== void 0)
|
|
1168
|
+
hit.truncate = rule.truncate;
|
|
1169
|
+
if (rule.timeoutMs !== void 0) {
|
|
1170
|
+
if (rule.timeoutMs > 0)
|
|
1171
|
+
await wait(rule.timeoutMs);
|
|
1172
|
+
hit.drop = true;
|
|
1173
|
+
hit.timeout = true;
|
|
1174
|
+
} else if (rule.drop === true)
|
|
1175
|
+
hit.drop = true;
|
|
1176
|
+
else if (rule.status !== void 0)
|
|
1177
|
+
hit.response = faultResponse(rule);
|
|
1178
|
+
return hit;
|
|
1179
|
+
};
|
|
1180
|
+
|
|
1181
|
+
// ../service/core/dist/latency.js
|
|
1182
|
+
var NONE = Object.freeze({ delayMs: 0, jitterMs: 0, operations: {} });
|
|
1183
|
+
var copy = (state) => ({
|
|
1184
|
+
delayMs: state.delayMs,
|
|
1185
|
+
jitterMs: state.jitterMs,
|
|
1186
|
+
operations: Object.fromEntries(Object.entries(state.operations).map(([id, rule]) => [id, { ...rule }]))
|
|
1187
|
+
});
|
|
1188
|
+
var createLatencyRegistry = (random) => {
|
|
1189
|
+
const states = /* @__PURE__ */ new Map();
|
|
1190
|
+
return {
|
|
1191
|
+
get: (namespace) => copy(states.get(namespace) ?? NONE),
|
|
1192
|
+
set(namespace, state) {
|
|
1193
|
+
states.set(namespace, copy(state));
|
|
1194
|
+
return copy(state);
|
|
1195
|
+
},
|
|
1196
|
+
clear(namespace) {
|
|
1197
|
+
if (namespace === void 0)
|
|
1198
|
+
states.clear();
|
|
1199
|
+
else
|
|
1200
|
+
states.delete(namespace);
|
|
1201
|
+
},
|
|
1202
|
+
take(namespace, operationId) {
|
|
1203
|
+
const state = states.get(namespace);
|
|
1204
|
+
if (!state)
|
|
1205
|
+
return 0;
|
|
1206
|
+
const rule = (operationId !== void 0 ? state.operations[operationId] : void 0) ?? state;
|
|
1207
|
+
const jitter = rule.jitterMs > 0 ? random(namespace).int(0, Math.floor(rule.jitterMs)) : 0;
|
|
1208
|
+
return rule.delayMs + jitter;
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
};
|
|
1212
|
+
|
|
1213
|
+
// ../service/core/dist/namespace-controls.js
|
|
1214
|
+
var NO_BOOT = { shared: {}, namespaces: {}, credentials: {} };
|
|
1215
|
+
var ACTION = [
|
|
1216
|
+
"status",
|
|
1217
|
+
"body",
|
|
1218
|
+
"headers",
|
|
1219
|
+
"delayMs",
|
|
1220
|
+
"jitterMs",
|
|
1221
|
+
"timeoutMs",
|
|
1222
|
+
"drop",
|
|
1223
|
+
"truncate",
|
|
1224
|
+
"effect",
|
|
1225
|
+
"params"
|
|
1226
|
+
];
|
|
1227
|
+
var CHOOSES_REQUESTS = [
|
|
1228
|
+
"id",
|
|
1229
|
+
"operationId",
|
|
1230
|
+
"method",
|
|
1231
|
+
"path",
|
|
1232
|
+
"pathPrefix",
|
|
1233
|
+
"pathPattern",
|
|
1234
|
+
"requestHeaders",
|
|
1235
|
+
"query",
|
|
1236
|
+
"requestBody",
|
|
1237
|
+
"namespace",
|
|
1238
|
+
"after",
|
|
1239
|
+
"every",
|
|
1240
|
+
"count",
|
|
1241
|
+
"rate",
|
|
1242
|
+
"from",
|
|
1243
|
+
"until"
|
|
1244
|
+
];
|
|
1245
|
+
var createNamespaceControls = (options) => {
|
|
1246
|
+
const boot = options.boot ?? NO_BOOT;
|
|
1247
|
+
const presets = { ...options.presets };
|
|
1248
|
+
for (const [name2, preset] of Object.entries(STANDARD_FAULT_PRESETS))
|
|
1249
|
+
presets[name2] ??= preset;
|
|
1250
|
+
const settings = /* @__PURE__ */ new Map();
|
|
1251
|
+
const streams = /* @__PURE__ */ new Map();
|
|
1252
|
+
const ensured = /* @__PURE__ */ new Set();
|
|
1253
|
+
const settingsOf = (namespace) => {
|
|
1254
|
+
let found = settings.get(namespace);
|
|
1255
|
+
if (!found) {
|
|
1256
|
+
found = { seed: options.seed, virtualTime: false, autoAdvanceMs: 0 };
|
|
1257
|
+
settings.set(namespace, found);
|
|
1258
|
+
}
|
|
1259
|
+
return found;
|
|
1260
|
+
};
|
|
1261
|
+
const streamsOf = (namespace) => {
|
|
1262
|
+
let found = streams.get(namespace);
|
|
1263
|
+
if (!found) {
|
|
1264
|
+
const { seed } = settingsOf(namespace);
|
|
1265
|
+
found = { faults: createRng(seed), latency: createRng(seed) };
|
|
1266
|
+
streams.set(namespace, found);
|
|
1267
|
+
}
|
|
1268
|
+
return found;
|
|
1269
|
+
};
|
|
1270
|
+
const faults = createFaultRegistry((namespace) => streamsOf(namespace).faults, options.sleep);
|
|
1271
|
+
const latency = createLatencyRegistry((namespace) => streamsOf(namespace).latency);
|
|
1272
|
+
const reseed = (namespace, seed) => {
|
|
1273
|
+
const next = normalizeSeed(seed);
|
|
1274
|
+
settingsOf(namespace).seed = next;
|
|
1275
|
+
const own = streamsOf(namespace);
|
|
1276
|
+
own.faults.reseed(next);
|
|
1277
|
+
own.latency.reseed(next);
|
|
1278
|
+
options.onReseed?.(namespace, next);
|
|
1279
|
+
};
|
|
1280
|
+
const changeClock = (namespace, change) => {
|
|
1281
|
+
const clock = options.clock(namespace);
|
|
1282
|
+
if (change.reset === true) {
|
|
1283
|
+
clock.reset();
|
|
1284
|
+
settingsOf(namespace).autoAdvanceMs = 0;
|
|
1285
|
+
}
|
|
1286
|
+
if (change.frozen === true)
|
|
1287
|
+
clock.freeze();
|
|
1288
|
+
if (change.now !== void 0)
|
|
1289
|
+
clock.set(change.now);
|
|
1290
|
+
if (change.advanceMs !== void 0)
|
|
1291
|
+
clock.advance(change.advanceMs);
|
|
1292
|
+
if (change.frozen === false)
|
|
1293
|
+
clock.unfreeze();
|
|
1294
|
+
if (change.autoAdvanceMs !== void 0)
|
|
1295
|
+
settingsOf(namespace).autoAdvanceMs = change.autoAdvanceMs;
|
|
1296
|
+
};
|
|
1297
|
+
const resolve = (namespace, input, now) => {
|
|
1298
|
+
if (!isRecord(input))
|
|
1299
|
+
throw new ControlError("fault: expected an object");
|
|
1300
|
+
if (typeof input.preset !== "string") {
|
|
1301
|
+
const rule = normalizeFault(input, {
|
|
1302
|
+
id: "unassigned",
|
|
1303
|
+
// Scoped to the caller's namespace unless it asks for every one, so one worker's
|
|
1304
|
+
// injected failure never lands on another's request.
|
|
1305
|
+
namespace,
|
|
1306
|
+
now,
|
|
1307
|
+
strict: true
|
|
1308
|
+
});
|
|
1309
|
+
if (!faultHasAction(rule))
|
|
1310
|
+
throw new ControlError("a fault needs a status, a delay, a timeout, drop: true, truncate, an effect, or a preset");
|
|
1311
|
+
return { rules: [{ rule, assign: typeof input.id !== "string" }] };
|
|
1312
|
+
}
|
|
1313
|
+
const { preset: name2, ...given } = input;
|
|
1314
|
+
const preset = presets[name2];
|
|
1315
|
+
if (!preset)
|
|
1316
|
+
throw new ControlError(`no fault preset ${JSON.stringify(name2)}; known: ${Object.keys(presets).join(", ")}`);
|
|
1317
|
+
const { id, ...overrides } = normalizeFault({ ...given, id: given.id ?? name2 }, { now, strict: true });
|
|
1318
|
+
const rules = preset.rules ?? [];
|
|
1319
|
+
return {
|
|
1320
|
+
preset: { preset, overrides },
|
|
1321
|
+
rules: rules.map((each, index) => {
|
|
1322
|
+
const { id: _id, ...base } = normalizeFault({ ...each, id: name2 }, { now });
|
|
1323
|
+
return {
|
|
1324
|
+
assign: false,
|
|
1325
|
+
rule: {
|
|
1326
|
+
namespace,
|
|
1327
|
+
...base,
|
|
1328
|
+
...overrides,
|
|
1329
|
+
fromPreset: name2,
|
|
1330
|
+
id: `${id}${rules.length > 1 ? `_${index + 1}` : ""}`
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
})
|
|
1334
|
+
};
|
|
1335
|
+
};
|
|
1336
|
+
const store = (namespace, resolved) => {
|
|
1337
|
+
const added = resolved.rules.map(({ rule, assign }) => (
|
|
1338
|
+
// Only a rule that is accepted takes the next id, so a rejected request leaves no gap.
|
|
1339
|
+
faults.add(assign ? { ...rule, id: faults.nextId(rule.namespace ?? namespace) } : rule)
|
|
1340
|
+
));
|
|
1341
|
+
if (resolved.preset)
|
|
1342
|
+
options.onPreset?.(resolved.preset.preset, namespace, resolved.preset.overrides);
|
|
1343
|
+
return added;
|
|
1344
|
+
};
|
|
1345
|
+
const check = (plan) => {
|
|
1346
|
+
for (const each of plan.faults ?? []) {
|
|
1347
|
+
if (isRecord(each) && each.namespace === "*")
|
|
1348
|
+
throw new ControlError('faults: a control document cannot hold a "*" rule; add it with POST /faults');
|
|
1349
|
+
resolve("default", each, 0);
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
const commit = (namespace, plan, replace) => {
|
|
1353
|
+
if (plan.seed !== void 0)
|
|
1354
|
+
reseed(namespace, plan.seed);
|
|
1355
|
+
if (plan.clock !== void 0)
|
|
1356
|
+
changeClock(namespace, plan.clock);
|
|
1357
|
+
if (plan.virtualTime !== void 0)
|
|
1358
|
+
settingsOf(namespace).virtualTime = plan.virtualTime;
|
|
1359
|
+
if (plan.latency !== void 0)
|
|
1360
|
+
latency.set(namespace, plan.latency);
|
|
1361
|
+
if (plan.faults !== void 0) {
|
|
1362
|
+
if (replace)
|
|
1363
|
+
faults.clear(namespace);
|
|
1364
|
+
const now = options.clock(namespace).now();
|
|
1365
|
+
for (const each of plan.faults)
|
|
1366
|
+
store(namespace, resolve(namespace, { ...each, namespace }, now));
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1369
|
+
const bootPlan = (namespace) => ({
|
|
1370
|
+
...boot.shared,
|
|
1371
|
+
...boot.namespaces[namespace]
|
|
1372
|
+
});
|
|
1373
|
+
const start = (namespace, scope) => {
|
|
1374
|
+
ensured.add(namespace);
|
|
1375
|
+
const { snapshot, ...plan } = bootPlan(namespace);
|
|
1376
|
+
const early = faults.snapshot(namespace).rules;
|
|
1377
|
+
if (early.length > 0)
|
|
1378
|
+
faults.clear(namespace);
|
|
1379
|
+
if (snapshot)
|
|
1380
|
+
options.restoreDocument?.(namespace, snapshot, scope);
|
|
1381
|
+
commit(namespace, plan, true);
|
|
1382
|
+
for (const { remaining: _r, hits: _h, seen: _s, position: _p, ...rule } of early)
|
|
1383
|
+
faults.add(rule);
|
|
1384
|
+
};
|
|
1385
|
+
const ensure = (namespace) => {
|
|
1386
|
+
if (!ensured.has(namespace))
|
|
1387
|
+
start(namespace, "all");
|
|
1388
|
+
};
|
|
1389
|
+
const clockOf = (namespace) => {
|
|
1390
|
+
ensure(namespace);
|
|
1391
|
+
const state = options.clock(namespace).state();
|
|
1392
|
+
return {
|
|
1393
|
+
...state,
|
|
1394
|
+
iso: new Date(state.now).toISOString(),
|
|
1395
|
+
autoAdvanceMs: settingsOf(namespace).autoAdvanceMs
|
|
1396
|
+
};
|
|
1397
|
+
};
|
|
1398
|
+
const wipe = (namespace) => {
|
|
1399
|
+
faults.clear(namespace);
|
|
1400
|
+
latency.clear(namespace);
|
|
1401
|
+
settings.delete(namespace);
|
|
1402
|
+
streams.delete(namespace);
|
|
1403
|
+
ensured.delete(namespace);
|
|
1404
|
+
};
|
|
1405
|
+
check(boot.shared);
|
|
1406
|
+
for (const plan of Object.values(boot.namespaces))
|
|
1407
|
+
check(plan);
|
|
1408
|
+
return {
|
|
1409
|
+
faults,
|
|
1410
|
+
latency,
|
|
1411
|
+
presets,
|
|
1412
|
+
ensure,
|
|
1413
|
+
settings(namespace) {
|
|
1414
|
+
ensure(namespace);
|
|
1415
|
+
return settingsOf(namespace);
|
|
1416
|
+
},
|
|
1417
|
+
clock: clockOf,
|
|
1418
|
+
describe(namespace) {
|
|
1419
|
+
ensure(namespace);
|
|
1420
|
+
const current = settingsOf(namespace);
|
|
1421
|
+
return {
|
|
1422
|
+
namespace,
|
|
1423
|
+
seed: current.seed,
|
|
1424
|
+
clock: clockOf(namespace),
|
|
1425
|
+
virtualTime: current.virtualTime,
|
|
1426
|
+
latency: latency.get(namespace),
|
|
1427
|
+
faults: faults.list(namespace)
|
|
1428
|
+
};
|
|
1429
|
+
},
|
|
1430
|
+
check,
|
|
1431
|
+
apply(namespace, plan) {
|
|
1432
|
+
check(plan);
|
|
1433
|
+
ensure(namespace);
|
|
1434
|
+
commit(namespace, plan, true);
|
|
1435
|
+
},
|
|
1436
|
+
changeClock(namespace, change) {
|
|
1437
|
+
ensure(namespace);
|
|
1438
|
+
changeClock(namespace, change);
|
|
1439
|
+
return clockOf(namespace);
|
|
1440
|
+
},
|
|
1441
|
+
addFault(namespace, input) {
|
|
1442
|
+
ensure(namespace);
|
|
1443
|
+
return store(namespace, resolve(namespace, input, options.clock(namespace).now()));
|
|
1444
|
+
},
|
|
1445
|
+
scriptFaults(namespace, request) {
|
|
1446
|
+
const rules = [];
|
|
1447
|
+
if (request.fault) {
|
|
1448
|
+
if (typeof request.fault.preset !== "string") {
|
|
1449
|
+
const chooses = CHOOSES_REQUESTS.find((key) => key in (request.fault ?? {}));
|
|
1450
|
+
if (chooses)
|
|
1451
|
+
throw new ControlError(`fault: ${chooses} has no meaning here; a fault in a header is for its own request`);
|
|
1452
|
+
}
|
|
1453
|
+
for (const { rule } of resolve(namespace, request.fault, 0).rules) {
|
|
1454
|
+
const action = { id: "request" };
|
|
1455
|
+
for (const key of ACTION)
|
|
1456
|
+
if (rule[key] !== void 0)
|
|
1457
|
+
Object.assign(action, { [key]: rule[key] });
|
|
1458
|
+
rules.push(action);
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
if (request.timeoutMs !== void 0)
|
|
1462
|
+
rules.push({ id: "request", timeoutMs: request.timeoutMs });
|
|
1463
|
+
return rules;
|
|
1464
|
+
},
|
|
1465
|
+
reseed(namespace, seed) {
|
|
1466
|
+
ensure(namespace);
|
|
1467
|
+
reseed(namespace, seed);
|
|
1468
|
+
},
|
|
1469
|
+
reboot(namespace, scope = "all") {
|
|
1470
|
+
wipe(namespace);
|
|
1471
|
+
options.clock(namespace).reset();
|
|
1472
|
+
options.onReseed?.(namespace, options.seed);
|
|
1473
|
+
start(namespace, scope);
|
|
1474
|
+
},
|
|
1475
|
+
rebootAll() {
|
|
1476
|
+
const known = /* @__PURE__ */ new Set([...ensured, ...settings.keys()]);
|
|
1477
|
+
faults.clear();
|
|
1478
|
+
latency.clear();
|
|
1479
|
+
settings.clear();
|
|
1480
|
+
streams.clear();
|
|
1481
|
+
ensured.clear();
|
|
1482
|
+
for (const namespace of known) {
|
|
1483
|
+
options.clock(namespace).reset();
|
|
1484
|
+
options.onReseed?.(namespace, options.seed);
|
|
1485
|
+
}
|
|
1486
|
+
},
|
|
1487
|
+
snapshot(namespace) {
|
|
1488
|
+
ensure(namespace);
|
|
1489
|
+
const own = streamsOf(namespace);
|
|
1490
|
+
return {
|
|
1491
|
+
settings: { ...settingsOf(namespace) },
|
|
1492
|
+
streams: { faults: own.faults.state(), latency: own.latency.state() },
|
|
1493
|
+
faults: faults.snapshot(namespace),
|
|
1494
|
+
latency: latency.get(namespace)
|
|
1495
|
+
};
|
|
1496
|
+
},
|
|
1497
|
+
restore(namespace, snapshot) {
|
|
1498
|
+
ensure(namespace);
|
|
1499
|
+
const { seed, ...rest } = snapshot.settings ?? {};
|
|
1500
|
+
Object.assign(settingsOf(namespace), rest);
|
|
1501
|
+
if (seed !== void 0)
|
|
1502
|
+
reseed(namespace, seed);
|
|
1503
|
+
const own = streamsOf(namespace);
|
|
1504
|
+
if (snapshot.streams?.faults !== void 0)
|
|
1505
|
+
own.faults.setState(snapshot.streams.faults);
|
|
1506
|
+
if (snapshot.streams?.latency !== void 0)
|
|
1507
|
+
own.latency.setState(snapshot.streams.latency);
|
|
1508
|
+
if (snapshot.faults)
|
|
1509
|
+
faults.restore(namespace, snapshot.faults);
|
|
1510
|
+
if (snapshot.latency)
|
|
1511
|
+
latency.set(namespace, snapshot.latency);
|
|
1512
|
+
},
|
|
1513
|
+
forget: wipe
|
|
1514
|
+
};
|
|
1515
|
+
};
|
|
1516
|
+
|
|
1517
|
+
// ../service/core/dist/signing.js
|
|
1518
|
+
var encoder = new TextEncoder();
|
|
1519
|
+
var toBase64 = (bytes) => {
|
|
1520
|
+
let binary = "";
|
|
1521
|
+
for (const byte of bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)) {
|
|
1522
|
+
binary += String.fromCharCode(byte);
|
|
1523
|
+
}
|
|
1524
|
+
return btoa(binary);
|
|
1525
|
+
};
|
|
1526
|
+
var fromBase64 = (value) => Uint8Array.from(atob(value), (char) => char.charCodeAt(0));
|
|
1527
|
+
var toHex = (bytes) => [...bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1528
|
+
var keyBytes = (key) => typeof key === "string" ? encoder.encode(key) : key;
|
|
1529
|
+
var hmac = async (algorithm, key, message, encoding = "hex") => {
|
|
1530
|
+
const imported = await crypto.subtle.importKey("raw", keyBytes(key), { name: "HMAC", hash: algorithm }, false, ["sign"]);
|
|
1531
|
+
const signed = await crypto.subtle.sign("HMAC", imported, typeof message === "string" ? encoder.encode(message) : message);
|
|
1532
|
+
return encoding === "hex" ? toHex(signed) : toBase64(signed);
|
|
1533
|
+
};
|
|
1534
|
+
var sha = async (algorithm, input) => toHex(await crypto.subtle.digest(algorithm, typeof input === "string" ? encoder.encode(input) : input));
|
|
1535
|
+
var svixSecretBytes = (secret) => {
|
|
1536
|
+
const raw = secret.replace(/^f?whsec_/, "");
|
|
1537
|
+
try {
|
|
1538
|
+
return fromBase64(raw);
|
|
1539
|
+
} catch {
|
|
1540
|
+
throw new TypeError("webhook secret must be whsec_<base64> (as Svix issues it)");
|
|
1541
|
+
}
|
|
1542
|
+
};
|
|
1543
|
+
var signSvix = async (secret, messageId, timestampSeconds, body) => `v1,${await hmac("SHA-256", svixSecretBytes(secret), `${messageId}.${timestampSeconds}.${body}`, "base64")}`;
|
|
1544
|
+
var signTimestamped = async (secret, timestampSeconds, body) => `t=${timestampSeconds},v1=${await hmac("SHA-256", secret, `${timestampSeconds}.${body}`, "hex")}`;
|
|
1545
|
+
var signTwilio = async (authToken, url, params) => {
|
|
1546
|
+
const payload = url + Object.keys(params).sort().map((key) => `${key}${params[key]}`).join("");
|
|
1547
|
+
return hmac("SHA-1", authToken, payload, "base64");
|
|
1548
|
+
};
|
|
1549
|
+
var timingSafeEqual = (a, b) => {
|
|
1550
|
+
if (a.length !== b.length)
|
|
1551
|
+
return false;
|
|
1552
|
+
let diff = 0;
|
|
1553
|
+
for (let i = 0; i < a.length; i++)
|
|
1554
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
1555
|
+
return diff === 0;
|
|
1556
|
+
};
|
|
1557
|
+
|
|
1558
|
+
// ../service/core/dist/surface.js
|
|
1559
|
+
var defineMock = (runtime) => runtime;
|
|
1560
|
+
var ADMIN_ROUTE_REFERENCE = {
|
|
1561
|
+
"GET /upstream": "Read this namespace\u2019s emulated or forward mode and upstream base URL.",
|
|
1562
|
+
"PUT /upstream": "Set upstream mode or base URL; forward requires an injected transport.",
|
|
1563
|
+
"GET /": "This index: every route and control header, with a one-line summary each.",
|
|
1564
|
+
"GET /health": "Readiness. Never needs the admin key or a vendor credential.",
|
|
1565
|
+
"GET /controls": "The namespace's controls in one document: seed, clock, virtual time, latency, faults, snapshots.",
|
|
1566
|
+
"PUT /controls": "Apply a control document. Each key given replaces that control; `snapshot` replaces the records.",
|
|
1567
|
+
"DELETE /controls": "Put the namespace's controls back to boot. Records are untouched.",
|
|
1568
|
+
"POST /reset": "Return the namespace to its boot state: records, controls, snapshots, journal, metrics. `?all=1` resets every namespace.",
|
|
1569
|
+
"GET /namespaces": "The isolation keys in use.",
|
|
1570
|
+
"DELETE /namespaces/:name": "Reset a namespace and forget it.",
|
|
1571
|
+
"GET /clock": "The namespace clock: `now`, `iso`, `frozen`, `offsetMs`, `autoAdvanceMs`.",
|
|
1572
|
+
"POST /clock": "Move time: `now` (or `set`), `advance`, `frozen` (or `freeze`), `autoAdvance`, `reset`.",
|
|
1573
|
+
"GET /latency": "The namespace's baseline response time.",
|
|
1574
|
+
"PUT /latency": "Set it: `delay`, seeded `jitter`, and per-operation `operations`.",
|
|
1575
|
+
"DELETE /latency": "Remove it.",
|
|
1576
|
+
"GET /faults": "Fault rules that can hit this namespace. `?all=1` lists every namespace's.",
|
|
1577
|
+
"POST /faults": 'Add a fault rule, or a preset with `{"preset": "<name>"}`.',
|
|
1578
|
+
"PUT /faults": 'Replace the namespace\'s fault rules with `{"faults": [...]}`.',
|
|
1579
|
+
"DELETE /faults": "Remove the namespace's fault rules. `?id=` removes one; `?all=1` every namespace's.",
|
|
1580
|
+
"GET /faults/presets": "Named faults this emulator answers to.",
|
|
1581
|
+
"POST /faults/presets/:name": "Switch a preset on. The body overrides its fields.",
|
|
1582
|
+
"GET /faults/:id": "One fault rule, with its `hits`, `seen` and `remaining` counters.",
|
|
1583
|
+
"DELETE /faults/:id": "Remove one fault rule.",
|
|
1584
|
+
"GET /snapshots": "The namespace's snapshots.",
|
|
1585
|
+
"POST /snapshots": 'Snapshot the namespace: records, clock, random streams, faults, latency. `{"name"}` names it.',
|
|
1586
|
+
"GET /snapshots/:id": "A snapshot as portable JSON.",
|
|
1587
|
+
"PUT /snapshots/:id": "Store a snapshot document under this id, without restoring it.",
|
|
1588
|
+
"POST /snapshots/:id/restore": "Replace the namespace with a snapshot.",
|
|
1589
|
+
"DELETE /snapshots/:id": "Delete a snapshot.",
|
|
1590
|
+
"GET /timeline": "The namespace's checkpoint history and branches.",
|
|
1591
|
+
"POST /checkpoints": "Checkpoint a branch now. Successful mutations do this automatically.",
|
|
1592
|
+
"POST /branches/:name": "Create a copy-on-write branch, optionally from a checkpoint (`at`).",
|
|
1593
|
+
"POST /branches/:name/checkout": "Move a branch to a checkpoint.",
|
|
1594
|
+
"GET /requests": "The request journal. Filter with `operationId`, `method`, `path`, `status`, `faultId`, `since`, `limit`; `?all=1` for every namespace.",
|
|
1595
|
+
"DELETE /requests": "Clear the journal.",
|
|
1596
|
+
"GET /metrics": "Request counts and unmatched paths, for every namespace or the one named.",
|
|
1597
|
+
"DELETE /metrics": "Clear them.",
|
|
1598
|
+
"GET /credentials": "Vendor credential \u2192 namespace mappings, masked.",
|
|
1599
|
+
"PUT /credentials": "Map vendor credentials to namespaces, for SDKs that cannot send a header.",
|
|
1600
|
+
"DELETE /credentials": "Remove one mapping (`?credential=`) or all.",
|
|
1601
|
+
"GET /state": "The namespace's collections: declared shape and stored rows.",
|
|
1602
|
+
"GET /state/:collection": "A page of rows.",
|
|
1603
|
+
"GET /state/:collection/:id": "One row.",
|
|
1604
|
+
"POST /state/:collection": "Create a row from `{id?, value}`.",
|
|
1605
|
+
"PUT /state/:collection/:id": "Replace a row.",
|
|
1606
|
+
"PATCH /state/:collection/:id": "Shallow-merge into a row.",
|
|
1607
|
+
"DELETE /state/:collection/:id": "Delete a row.",
|
|
1608
|
+
"GET /ui": "The admin UI.",
|
|
1609
|
+
"GET /ui/": "The admin UI.",
|
|
1610
|
+
"GET /ui/manifest": "The admin UI's panels and extensions."
|
|
1611
|
+
};
|
|
1612
|
+
var STANDARD_ADMIN_ROUTES = Object.keys(ADMIN_ROUTE_REFERENCE);
|
|
1613
|
+
var PANEL_ID = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/;
|
|
1614
|
+
var RESERVED_VIEWS = /* @__PURE__ */ new Set([
|
|
1615
|
+
"overview",
|
|
1616
|
+
"manual",
|
|
1617
|
+
"state",
|
|
1618
|
+
"clock",
|
|
1619
|
+
"latency",
|
|
1620
|
+
"faults",
|
|
1621
|
+
"snapshots",
|
|
1622
|
+
"journal",
|
|
1623
|
+
"routes",
|
|
1624
|
+
"checkpoints"
|
|
1625
|
+
]);
|
|
1626
|
+
var checkPanel = (panel, seen, label) => {
|
|
1627
|
+
if (!PANEL_ID.test(panel.id)) {
|
|
1628
|
+
throw new RangeError(`${label} id must match ${PANEL_ID}: ${JSON.stringify(panel.id)}`);
|
|
1629
|
+
}
|
|
1630
|
+
if (RESERVED_VIEWS.has(panel.id)) {
|
|
1631
|
+
throw new RangeError(`${label} id ${panel.id} is reserved by the admin shell`);
|
|
1632
|
+
}
|
|
1633
|
+
if (seen.has(panel.id))
|
|
1634
|
+
throw new RangeError(`duplicate admin view id ${panel.id}`);
|
|
1635
|
+
seen.add(panel.id);
|
|
1636
|
+
if (panel.title.trim() === "")
|
|
1637
|
+
throw new RangeError(`${label} ${panel.id} needs a title`);
|
|
1638
|
+
};
|
|
1639
|
+
var assertAdminUi = (ui) => {
|
|
1640
|
+
if (!ui)
|
|
1641
|
+
return;
|
|
1642
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1643
|
+
for (const panel of ui.panels ?? [])
|
|
1644
|
+
checkPanel(panel, seen, "admin panel");
|
|
1645
|
+
for (const extension of ui.extensions ?? []) {
|
|
1646
|
+
if (extension.kind === "panel" || extension.kind === "route") {
|
|
1647
|
+
checkPanel(extension, seen, "admin extension");
|
|
1648
|
+
if (extension.kind === "route" && !/^(GET|POST|PUT|PATCH|DELETE) \/\S*$/.test(extension.route))
|
|
1649
|
+
throw new RangeError(`admin extension ${extension.id} needs a method and route path`);
|
|
1650
|
+
continue;
|
|
1651
|
+
}
|
|
1652
|
+
const id = extension.id ?? "sql";
|
|
1653
|
+
checkPanel({ id, title: extension.title ?? "SQL" }, seen, "admin extension");
|
|
1654
|
+
}
|
|
1655
|
+
};
|
|
1656
|
+
|
|
1657
|
+
// ../service/core/dist/control.js
|
|
1658
|
+
var HEALTH_PATH = "/__admin/health";
|
|
1659
|
+
var ADMIN_PREFIX = "/__admin";
|
|
1660
|
+
var resolveAdminPrefix = (prefix = ADMIN_PREFIX) => {
|
|
1661
|
+
if (!/^\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/.test(prefix)) {
|
|
1662
|
+
throw new Error("adminPrefix must be an absolute non-root path of literal segments, without a trailing slash");
|
|
1663
|
+
}
|
|
1664
|
+
return prefix;
|
|
1665
|
+
};
|
|
1666
|
+
var isAdminPath = (path, prefix) => path === prefix || path.startsWith(`${prefix}/`);
|
|
1667
|
+
var matchNamespacePath = (path, prefix) => /^\/ns\/([^/]+)(\/.*)?$/.exec(isAdminPath(path, prefix) ? path.slice(prefix.length) : "");
|
|
1668
|
+
var assertAdminPrefixAvailable = (prefix, paths) => {
|
|
1669
|
+
const reserved = prefix.split("/").slice(1);
|
|
1670
|
+
for (const path of paths) {
|
|
1671
|
+
const segments = path.split("/").slice(1);
|
|
1672
|
+
if (segments.length < reserved.length || /^\{[^}]+\}$/.test(segments[0] ?? ""))
|
|
1673
|
+
continue;
|
|
1674
|
+
if (reserved.every((part, i) => segments[i] === part || /^\{[^}]+\}$/.test(segments[i] ?? ""))) {
|
|
1675
|
+
throw new Error(`vendor route ${path} collides with adminPrefix ${prefix}; choose a different adminPrefix`);
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
};
|
|
1679
|
+
var ADMIN_KEY_HEADER = CONTROL_HEADERS.adminKey;
|
|
1680
|
+
var ADMIN_BRANDS_PATH = "/ui/brands.json";
|
|
1681
|
+
var hasAdminKey = (headers, adminKey) => {
|
|
1682
|
+
const sent = headers.get(ADMIN_KEY_HEADER);
|
|
1683
|
+
return sent !== null && timingSafeEqual(sent, adminKey);
|
|
1684
|
+
};
|
|
1685
|
+
var NAMESPACE_HEADER = CONTROL_HEADERS.namespace;
|
|
1686
|
+
var ADMIN_DOCS_URL = "https://mockingbird.chrisvouga.dev/docs/admin-controls";
|
|
1687
|
+
var ADMIN_CONTROL_PAGES = [
|
|
1688
|
+
{
|
|
1689
|
+
page: "namespaces",
|
|
1690
|
+
title: "Namespaces",
|
|
1691
|
+
routes: ["/namespaces", "/credentials"],
|
|
1692
|
+
headers: [CONTROL_HEADERS.namespace]
|
|
1693
|
+
},
|
|
1694
|
+
{ page: "reset", title: "Reset", routes: ["/reset"], headers: [CONTROL_HEADERS.reset] },
|
|
1695
|
+
{
|
|
1696
|
+
page: "snapshot-and-restore",
|
|
1697
|
+
title: "Snapshot and restore",
|
|
1698
|
+
routes: ["/snapshots"],
|
|
1699
|
+
headers: [CONTROL_HEADERS.restore, CONTROL_HEADERS.snapshot]
|
|
1700
|
+
},
|
|
1701
|
+
{
|
|
1702
|
+
page: "clock",
|
|
1703
|
+
title: "Clock",
|
|
1704
|
+
routes: ["/clock"],
|
|
1705
|
+
headers: [CONTROL_HEADERS.time, CONTROL_HEADERS.advance]
|
|
1706
|
+
},
|
|
1707
|
+
{
|
|
1708
|
+
page: "checkpoints-and-branches",
|
|
1709
|
+
title: "Checkpoints and branches",
|
|
1710
|
+
routes: ["/timeline", "/checkpoints", "/branches"],
|
|
1711
|
+
headers: [CONTROL_HEADERS.branch, CONTROL_HEADERS.at]
|
|
1712
|
+
},
|
|
1713
|
+
{ page: "faults", title: "Faults", routes: ["/faults"], headers: [CONTROL_HEADERS.fault] },
|
|
1714
|
+
{
|
|
1715
|
+
page: "latency-and-timeouts",
|
|
1716
|
+
title: "Latency and timeouts",
|
|
1717
|
+
routes: ["/latency"],
|
|
1718
|
+
headers: [CONTROL_HEADERS.delay, CONTROL_HEADERS.timeout, CONTROL_HEADERS.virtualTime]
|
|
1719
|
+
},
|
|
1720
|
+
{
|
|
1721
|
+
page: "seed-and-determinism",
|
|
1722
|
+
title: "Seed and determinism",
|
|
1723
|
+
routes: [],
|
|
1724
|
+
headers: [CONTROL_HEADERS.seed]
|
|
1725
|
+
},
|
|
1726
|
+
{ page: "control-document", title: "The control document", routes: ["/controls"], headers: [] },
|
|
1727
|
+
{ page: "boot", title: "Boot", routes: [], headers: [] },
|
|
1728
|
+
{ page: "request-headers", title: "Request headers", routes: [], headers: [] },
|
|
1729
|
+
{
|
|
1730
|
+
page: "journal-and-state",
|
|
1731
|
+
title: "Journal, metrics and state",
|
|
1732
|
+
routes: ["/requests", "/metrics", "/state", "/ui"],
|
|
1733
|
+
headers: []
|
|
1734
|
+
},
|
|
1735
|
+
{
|
|
1736
|
+
page: "reference",
|
|
1737
|
+
title: "Reference",
|
|
1738
|
+
routes: ["/", "/health", "/upstream"],
|
|
1739
|
+
headers: [CONTROL_HEADERS.adminKey, CONTROL_HEADERS.upstream]
|
|
1740
|
+
}
|
|
1741
|
+
];
|
|
1742
|
+
var pageUrl = (page) => `${ADMIN_DOCS_URL}/${page}`;
|
|
1743
|
+
var routeDocs = (route) => {
|
|
1744
|
+
if (!Object.hasOwn(ADMIN_ROUTE_REFERENCE, route))
|
|
1745
|
+
return void 0;
|
|
1746
|
+
const path = route.slice(route.indexOf(" ") + 1);
|
|
1747
|
+
const segment = `/${path.split("/")[1] ?? ""}`;
|
|
1748
|
+
const found = ADMIN_CONTROL_PAGES.find((each) => each.routes.includes(segment));
|
|
1749
|
+
return found && pageUrl(found.page);
|
|
1750
|
+
};
|
|
1751
|
+
var headerDocs = (header) => {
|
|
1752
|
+
const found = ADMIN_CONTROL_PAGES.find((each) => each.headers.includes(header));
|
|
1753
|
+
return found && pageUrl(found.page);
|
|
1754
|
+
};
|
|
1755
|
+
var json = (status, body) => new Response(JSON.stringify(body), {
|
|
1756
|
+
status,
|
|
1757
|
+
headers: { "content-type": "application/json; charset=utf-8" }
|
|
1758
|
+
});
|
|
1759
|
+
var adminError = (status, message) => json(status, { error: { type: "mockingbird_admin", message } });
|
|
1760
|
+
var paramCount = (pattern) => (pattern.match(/\/:/g) ?? []).length;
|
|
1761
|
+
var matchRoute = (pattern, path) => {
|
|
1762
|
+
const want = pattern.split("/").filter(Boolean);
|
|
1763
|
+
const have = path.split("/").filter(Boolean);
|
|
1764
|
+
if (want.length !== have.length)
|
|
1765
|
+
return void 0;
|
|
1766
|
+
const params = {};
|
|
1767
|
+
for (let i = 0; i < want.length; i++) {
|
|
1768
|
+
const segment = want[i];
|
|
1769
|
+
const actual = have[i];
|
|
1770
|
+
if (segment.startsWith(":"))
|
|
1771
|
+
params[segment.slice(1)] = decodeURIComponent(actual);
|
|
1772
|
+
else if (segment !== actual)
|
|
1773
|
+
return void 0;
|
|
1774
|
+
}
|
|
1775
|
+
return params;
|
|
1776
|
+
};
|
|
1777
|
+
var readJson = async (request) => {
|
|
1778
|
+
const text = await request.text();
|
|
1779
|
+
if (text.trim() === "")
|
|
1780
|
+
return void 0;
|
|
1781
|
+
return JSON.parse(text);
|
|
1782
|
+
};
|
|
1783
|
+
var timelineSnapshots = (timeTravel) => {
|
|
1784
|
+
const stored = /* @__PURE__ */ new Map();
|
|
1785
|
+
const counters = /* @__PURE__ */ new Map();
|
|
1786
|
+
const of = (namespace) => {
|
|
1787
|
+
let found = stored.get(namespace);
|
|
1788
|
+
if (!found) {
|
|
1789
|
+
found = /* @__PURE__ */ new Map();
|
|
1790
|
+
stored.set(namespace, found);
|
|
1791
|
+
}
|
|
1792
|
+
return found;
|
|
1793
|
+
};
|
|
1794
|
+
const remove = (namespace, id) => {
|
|
1795
|
+
const found = stored.get(namespace)?.get(id);
|
|
1796
|
+
if (!found)
|
|
1797
|
+
return false;
|
|
1798
|
+
stored.get(namespace)?.delete(id);
|
|
1799
|
+
timeTravel.release(namespace, found.checkpoint);
|
|
1800
|
+
return true;
|
|
1801
|
+
};
|
|
1802
|
+
return {
|
|
1803
|
+
create(namespace, given) {
|
|
1804
|
+
const point = timeTravel.checkpoint(namespace, "main");
|
|
1805
|
+
timeTravel.retain(namespace, point.id);
|
|
1806
|
+
let id = given;
|
|
1807
|
+
if (id === void 0) {
|
|
1808
|
+
let next = counters.get(namespace) ?? 0;
|
|
1809
|
+
do
|
|
1810
|
+
id = `snap_${++next}`;
|
|
1811
|
+
while (of(namespace).has(id));
|
|
1812
|
+
counters.set(namespace, next);
|
|
1813
|
+
} else
|
|
1814
|
+
remove(namespace, id);
|
|
1815
|
+
const info = { id, namespace, records: point.records ?? 0, at: point.at, portable: false };
|
|
1816
|
+
of(namespace).set(id, { checkpoint: point.id, info });
|
|
1817
|
+
return info;
|
|
1818
|
+
},
|
|
1819
|
+
list: (namespace) => [...stored.get(namespace)?.values() ?? []].map((each) => each.info),
|
|
1820
|
+
get: (namespace, id) => stored.get(namespace)?.get(id)?.info,
|
|
1821
|
+
export: () => void 0,
|
|
1822
|
+
import() {
|
|
1823
|
+
throw new ControlError("this emulator's snapshots cannot be imported or exported");
|
|
1824
|
+
},
|
|
1825
|
+
restore(namespace, id) {
|
|
1826
|
+
const found = stored.get(namespace)?.get(id);
|
|
1827
|
+
if (!found)
|
|
1828
|
+
return false;
|
|
1829
|
+
timeTravel.checkout(found.checkpoint, { namespace, branch: "main" });
|
|
1830
|
+
return true;
|
|
1831
|
+
},
|
|
1832
|
+
remove,
|
|
1833
|
+
clear(namespace) {
|
|
1834
|
+
for (const [name2, snapshots] of stored) {
|
|
1835
|
+
if (namespace !== void 0 && name2 !== namespace)
|
|
1836
|
+
continue;
|
|
1837
|
+
for (const id of [...snapshots.keys()]) {
|
|
1838
|
+
try {
|
|
1839
|
+
remove(name2, id);
|
|
1840
|
+
} catch {
|
|
1841
|
+
snapshots.delete(id);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
stored.delete(name2);
|
|
1845
|
+
counters.delete(name2);
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
};
|
|
1849
|
+
};
|
|
1850
|
+
var createControlPlane = (context) => {
|
|
1851
|
+
const adminPrefix = resolveAdminPrefix(context.adminPrefix);
|
|
1852
|
+
const controls = context.controls ?? createNamespaceControls({
|
|
1853
|
+
seed: 0,
|
|
1854
|
+
clock: (namespace) => context.namespaceClock?.(namespace) ?? context.clock
|
|
1855
|
+
});
|
|
1856
|
+
const { faults } = controls;
|
|
1857
|
+
const snapshots = context.snapshots ?? timelineSnapshots(context.timeTravel);
|
|
1858
|
+
const forgetSnapshots = (namespace) => {
|
|
1859
|
+
if (!context.snapshots)
|
|
1860
|
+
snapshots.clear(namespace);
|
|
1861
|
+
};
|
|
1862
|
+
const applyControls = context.applyControls ?? ((namespace, plan) => {
|
|
1863
|
+
if (plan.snapshot !== void 0)
|
|
1864
|
+
throw new ControlError("this emulator cannot start from a snapshot document");
|
|
1865
|
+
controls.apply(namespace, plan);
|
|
1866
|
+
});
|
|
1867
|
+
const headerNamespace = (request) => request.headers.get(NAMESPACE_HEADER) ?? context.defaultNamespace;
|
|
1868
|
+
const adminNamespace = (request, url) => url.searchParams.get("namespace") ?? headerNamespace(request);
|
|
1869
|
+
const explicitNamespace = (request, url) => url.searchParams.has("namespace") || request.headers.has(NAMESPACE_HEADER);
|
|
1870
|
+
const health = () => {
|
|
1871
|
+
return json(200, {
|
|
1872
|
+
...context.describe(),
|
|
1873
|
+
status: "ok",
|
|
1874
|
+
service: context.name,
|
|
1875
|
+
uptimeMs: context.wallNow() - context.startedAt,
|
|
1876
|
+
clock: context.clock.state(),
|
|
1877
|
+
namespaces: context.namespaces().length,
|
|
1878
|
+
adminPrefix,
|
|
1879
|
+
adminUi: `${adminPrefix}/ui`
|
|
1880
|
+
});
|
|
1881
|
+
};
|
|
1882
|
+
const describe = (namespace) => ({
|
|
1883
|
+
...controls.describe(namespace),
|
|
1884
|
+
snapshots: snapshots.list(namespace)
|
|
1885
|
+
});
|
|
1886
|
+
const guard = (run) => {
|
|
1887
|
+
try {
|
|
1888
|
+
return run();
|
|
1889
|
+
} catch (error) {
|
|
1890
|
+
if (error instanceof ControlError)
|
|
1891
|
+
return adminError(400, error.message);
|
|
1892
|
+
throw error;
|
|
1893
|
+
}
|
|
1894
|
+
};
|
|
1895
|
+
const presetList = () => ({
|
|
1896
|
+
presets: Object.entries(controls.presets).map(([name2, preset]) => ({ name: name2, ...preset }))
|
|
1897
|
+
});
|
|
1898
|
+
const removeFault = (id, namespace, everyNamespace) => faults.remove(id, everyNamespace ? void 0 : namespace) ? json(200, { status: "ok" }) : adminError(404, `no fault ${id}`);
|
|
1899
|
+
const builtin = {
|
|
1900
|
+
"GET /health": health,
|
|
1901
|
+
"GET /": ({ namespace }) => {
|
|
1902
|
+
const routes2 = [...Object.keys(builtin), ...Object.keys(context.routes)].sort();
|
|
1903
|
+
const reference = ADMIN_ROUTE_REFERENCE;
|
|
1904
|
+
return json(200, {
|
|
1905
|
+
service: context.name,
|
|
1906
|
+
...context.version !== void 0 ? { version: context.version } : {},
|
|
1907
|
+
namespace,
|
|
1908
|
+
adminPrefix,
|
|
1909
|
+
docs: ADMIN_DOCS_URL,
|
|
1910
|
+
guide: ADMIN_CONTROL_PAGES.map((each) => ({ title: each.title, docs: pageUrl(each.page) })),
|
|
1911
|
+
routes: routes2,
|
|
1912
|
+
reference: routes2.map((route) => {
|
|
1913
|
+
const docs = routeDocs(route);
|
|
1914
|
+
return {
|
|
1915
|
+
route,
|
|
1916
|
+
summary: reference[route] ?? `Specific to ${context.name}.`,
|
|
1917
|
+
...docs !== void 0 ? { docs } : {}
|
|
1918
|
+
};
|
|
1919
|
+
}),
|
|
1920
|
+
headers: CONTROL_HEADER_REFERENCE.map((each) => ({
|
|
1921
|
+
...each,
|
|
1922
|
+
docs: headerDocs(each.header) ?? ADMIN_DOCS_URL
|
|
1923
|
+
}))
|
|
1924
|
+
});
|
|
1925
|
+
},
|
|
1926
|
+
// Non-HTTP adapters share the index but cannot relay vendor requests.
|
|
1927
|
+
"GET /upstream": context.upstream?.get ?? (() => json(200, { mode: "emulated" })),
|
|
1928
|
+
"PUT /upstream": context.upstream?.set ?? (() => json(501, {
|
|
1929
|
+
error: {
|
|
1930
|
+
type: "mockingbird_unsupported",
|
|
1931
|
+
message: "This adapter does not support HTTP forwarding"
|
|
1932
|
+
}
|
|
1933
|
+
})),
|
|
1934
|
+
"GET /controls": ({ namespace }) => json(200, describe(namespace)),
|
|
1935
|
+
"PUT /controls": ({ body, namespace }) => guard(() => {
|
|
1936
|
+
applyControls(namespace, normalizeControls(body ?? {}, "controls"));
|
|
1937
|
+
return json(200, describe(namespace));
|
|
1938
|
+
}),
|
|
1939
|
+
"DELETE /controls": ({ namespace }) => {
|
|
1940
|
+
controls.reboot(namespace, "controls");
|
|
1941
|
+
return json(200, describe(namespace));
|
|
1942
|
+
},
|
|
1943
|
+
"POST /reset": async ({ url, namespace }) => {
|
|
1944
|
+
const target = url.searchParams.get("all") === "1" ? "*" : namespace;
|
|
1945
|
+
await context.reset(target);
|
|
1946
|
+
forgetSnapshots(target === "*" ? void 0 : target);
|
|
1947
|
+
return json(200, { status: "ok", reset: target === "*" ? context.namespaces() : [target] });
|
|
1948
|
+
},
|
|
1949
|
+
"GET /namespaces": () => json(200, { default: context.defaultNamespace, namespaces: context.namespaces() }),
|
|
1950
|
+
"DELETE /namespaces/:name": async ({ params }) => {
|
|
1951
|
+
const name2 = params.name;
|
|
1952
|
+
if (!KEY_PATTERN.test(name2))
|
|
1953
|
+
return adminError(400, "invalid namespace");
|
|
1954
|
+
if (context.dropNamespace)
|
|
1955
|
+
await context.dropNamespace(name2);
|
|
1956
|
+
else
|
|
1957
|
+
await context.reset(name2);
|
|
1958
|
+
forgetSnapshots(name2);
|
|
1959
|
+
return json(200, { status: "ok", namespaces: context.namespaces() });
|
|
1960
|
+
},
|
|
1961
|
+
"GET /clock": ({ namespace }) => json(200, controls.clock(namespace)),
|
|
1962
|
+
"POST /clock": ({ body, namespace }) => guard(() => {
|
|
1963
|
+
if (!isRecord(body))
|
|
1964
|
+
return adminError(400, "expected a JSON object");
|
|
1965
|
+
const { advance, reset, ...rest } = body;
|
|
1966
|
+
if (reset !== void 0 && typeof reset !== "boolean")
|
|
1967
|
+
return adminError(400, "reset: expected a boolean");
|
|
1968
|
+
return json(200, controls.changeClock(namespace, {
|
|
1969
|
+
...normalizeClock(rest),
|
|
1970
|
+
...advance !== void 0 ? { advanceMs: durationMs(advance, "advance", true) } : {},
|
|
1971
|
+
...reset === true ? { reset: true } : {}
|
|
1972
|
+
}));
|
|
1973
|
+
}),
|
|
1974
|
+
"GET /latency": ({ namespace }) => {
|
|
1975
|
+
controls.ensure(namespace);
|
|
1976
|
+
return json(200, controls.latency.get(namespace));
|
|
1977
|
+
},
|
|
1978
|
+
"PUT /latency": ({ body, namespace }) => guard(() => {
|
|
1979
|
+
controls.ensure(namespace);
|
|
1980
|
+
return json(200, controls.latency.set(namespace, normalizeLatency(body ?? {})));
|
|
1981
|
+
}),
|
|
1982
|
+
"DELETE /latency": ({ namespace }) => {
|
|
1983
|
+
controls.ensure(namespace);
|
|
1984
|
+
controls.latency.clear(namespace);
|
|
1985
|
+
return json(200, controls.latency.get(namespace));
|
|
1986
|
+
},
|
|
1987
|
+
"GET /faults": ({ url, namespace }) => {
|
|
1988
|
+
controls.ensure(namespace);
|
|
1989
|
+
return json(200, {
|
|
1990
|
+
faults: faults.list(url.searchParams.get("all") === "1" ? void 0 : namespace)
|
|
1991
|
+
});
|
|
1992
|
+
},
|
|
1993
|
+
"POST /faults": ({ body, namespace }) => guard(() => {
|
|
1994
|
+
const preset = isRecord(body) && typeof body.preset === "string" ? body.preset : void 0;
|
|
1995
|
+
if (preset !== void 0 && !controls.presets[preset])
|
|
1996
|
+
return adminError(404, `no fault preset ${JSON.stringify(preset)}; GET ${adminPrefix}/faults/presets`);
|
|
1997
|
+
const added = controls.addFault(namespace, body);
|
|
1998
|
+
return preset !== void 0 ? json(201, { preset, rules: added }) : json(201, added[0]);
|
|
1999
|
+
}),
|
|
2000
|
+
"PUT /faults": ({ body, namespace }) => guard(() => {
|
|
2001
|
+
const list = isRecord(body) ? body.faults : void 0;
|
|
2002
|
+
if (!Array.isArray(list))
|
|
2003
|
+
return adminError(400, 'expected {"faults": [...]}');
|
|
2004
|
+
controls.apply(namespace, normalizeControls({ faults: list }));
|
|
2005
|
+
return json(200, { faults: faults.list(namespace) });
|
|
2006
|
+
}),
|
|
2007
|
+
"DELETE /faults": ({ url, namespace }) => {
|
|
2008
|
+
controls.ensure(namespace);
|
|
2009
|
+
const everyNamespace = url.searchParams.get("all") === "1";
|
|
2010
|
+
const id = url.searchParams.get("id");
|
|
2011
|
+
if (id !== null)
|
|
2012
|
+
return removeFault(id, namespace, everyNamespace);
|
|
2013
|
+
faults.clear(everyNamespace ? void 0 : namespace);
|
|
2014
|
+
return json(200, { status: "ok" });
|
|
2015
|
+
},
|
|
2016
|
+
"GET /faults/presets": () => json(200, presetList()),
|
|
2017
|
+
"POST /faults/presets/:name": ({ params, body, namespace }) => guard(() => {
|
|
2018
|
+
const name2 = params.name;
|
|
2019
|
+
if (!controls.presets[name2])
|
|
2020
|
+
return adminError(404, `no fault preset ${name2}; GET ${adminPrefix}/faults/presets`);
|
|
2021
|
+
return json(201, {
|
|
2022
|
+
preset: name2,
|
|
2023
|
+
rules: controls.addFault(namespace, { ...isRecord(body) ? body : {}, preset: name2 })
|
|
2024
|
+
});
|
|
2025
|
+
}),
|
|
2026
|
+
"GET /faults/:id": ({ params, namespace }) => {
|
|
2027
|
+
controls.ensure(namespace);
|
|
2028
|
+
const found = faults.get(params.id, namespace);
|
|
2029
|
+
return found ? json(200, found) : adminError(404, `no fault ${params.id}`);
|
|
2030
|
+
},
|
|
2031
|
+
"DELETE /faults/:id": ({ params, url, namespace }) => {
|
|
2032
|
+
controls.ensure(namespace);
|
|
2033
|
+
return removeFault(params.id, namespace, url.searchParams.get("all") === "1");
|
|
2034
|
+
},
|
|
2035
|
+
"GET /snapshots": ({ namespace }) => json(200, { snapshots: snapshots.list(namespace) }),
|
|
2036
|
+
"POST /snapshots": ({ body, namespace }) => guard(() => {
|
|
2037
|
+
const id = isRecord(body) ? body.name : void 0;
|
|
2038
|
+
if (id !== void 0 && (typeof id !== "string" || !KEY_PATTERN.test(id)))
|
|
2039
|
+
return adminError(400, `name: must match ${KEY_PATTERN}`);
|
|
2040
|
+
return json(201, snapshots.create(namespace, id));
|
|
2041
|
+
}),
|
|
2042
|
+
"GET /snapshots/:id": ({ params, namespace }) => {
|
|
2043
|
+
const id = params.id;
|
|
2044
|
+
const info = snapshots.get(namespace, id);
|
|
2045
|
+
if (!info)
|
|
2046
|
+
return adminError(404, `no snapshot ${id} in namespace ${namespace}`);
|
|
2047
|
+
return json(200, snapshots.export(namespace, id) ?? info);
|
|
2048
|
+
},
|
|
2049
|
+
"PUT /snapshots/:id": ({ params, body, namespace }) => guard(() => {
|
|
2050
|
+
const id = params.id;
|
|
2051
|
+
if (!KEY_PATTERN.test(id))
|
|
2052
|
+
return adminError(400, `id: must match ${KEY_PATTERN}`);
|
|
2053
|
+
return json(200, snapshots.import(namespace, id, assertSnapshotDocument(body)));
|
|
2054
|
+
}),
|
|
2055
|
+
"POST /snapshots/:id/restore": ({ params, namespace }) => guard(() => {
|
|
2056
|
+
const id = params.id;
|
|
2057
|
+
if (!snapshots.restore(namespace, id))
|
|
2058
|
+
return adminError(404, `no snapshot ${id} in namespace ${namespace}`);
|
|
2059
|
+
return json(200, { status: "ok", id, namespace });
|
|
2060
|
+
}),
|
|
2061
|
+
"DELETE /snapshots/:id": ({ params, namespace }) => {
|
|
2062
|
+
const id = params.id;
|
|
2063
|
+
return snapshots.remove(namespace, id) ? json(200, { status: "ok" }) : adminError(404, `no snapshot ${id} in namespace ${namespace}`);
|
|
2064
|
+
},
|
|
2065
|
+
"GET /timeline": ({ namespace }) => json(200, context.timeTravel.inspect(namespace)),
|
|
2066
|
+
"POST /checkpoints": ({ body, namespace }) => {
|
|
2067
|
+
const branch = isRecord(body) && typeof body.branch === "string" ? body.branch : "main";
|
|
2068
|
+
try {
|
|
2069
|
+
return json(201, context.timeTravel.checkpoint(namespace, branch));
|
|
2070
|
+
} catch (error) {
|
|
2071
|
+
return adminError(409, error instanceof Error ? error.message : String(error));
|
|
2072
|
+
}
|
|
2073
|
+
},
|
|
2074
|
+
"POST /branches/:name": ({ params, body, namespace }) => {
|
|
2075
|
+
const at = isRecord(body) && typeof body.at === "string" ? body.at : void 0;
|
|
2076
|
+
try {
|
|
2077
|
+
return json(201, context.timeTravel.branch(params.name, {
|
|
2078
|
+
namespace,
|
|
2079
|
+
...at !== void 0 ? { at } : {}
|
|
2080
|
+
}));
|
|
2081
|
+
} catch (error) {
|
|
2082
|
+
return adminError(409, error instanceof Error ? error.message : String(error));
|
|
2083
|
+
}
|
|
2084
|
+
},
|
|
2085
|
+
"POST /branches/:name/checkout": ({ params, body, namespace }) => {
|
|
2086
|
+
if (!isRecord(body) || typeof body.checkpoint !== "string") {
|
|
2087
|
+
return adminError(400, 'expected {"checkpoint":"cp_..."}');
|
|
2088
|
+
}
|
|
2089
|
+
try {
|
|
2090
|
+
context.timeTravel.checkout(body.checkpoint, {
|
|
2091
|
+
namespace,
|
|
2092
|
+
branch: params.name
|
|
2093
|
+
});
|
|
2094
|
+
return json(200, { status: "ok", branch: params.name, checkpoint: body.checkpoint });
|
|
2095
|
+
} catch (error) {
|
|
2096
|
+
return adminError(409, error instanceof Error ? error.message : String(error));
|
|
2097
|
+
}
|
|
2098
|
+
},
|
|
2099
|
+
"GET /requests": ({ url, namespace }) => {
|
|
2100
|
+
const status = url.searchParams.get("status");
|
|
2101
|
+
const since = url.searchParams.get("since");
|
|
2102
|
+
const limit = url.searchParams.get("limit");
|
|
2103
|
+
const sinceMs = since === null ? void 0 : parseInstant(since);
|
|
2104
|
+
if (since !== null && sinceMs === void 0) {
|
|
2105
|
+
return adminError(400, "since: expected epoch ms or ISO-8601");
|
|
2106
|
+
}
|
|
2107
|
+
if (status !== null && !/^(0|\d{3})$/.test(status))
|
|
2108
|
+
return adminError(400, "status: expected an HTTP status, or 0 for a dropped connection");
|
|
2109
|
+
if (limit !== null && !/^\d+$/.test(limit))
|
|
2110
|
+
return adminError(400, "limit: expected a count");
|
|
2111
|
+
const operationId = url.searchParams.get("operationId");
|
|
2112
|
+
const method = url.searchParams.get("method");
|
|
2113
|
+
const path = url.searchParams.get("path");
|
|
2114
|
+
const faultId = url.searchParams.get("faultId");
|
|
2115
|
+
const everyNamespace = url.searchParams.get("all") === "1";
|
|
2116
|
+
return json(200, {
|
|
2117
|
+
size: context.journal.size,
|
|
2118
|
+
requests: context.journal.list({
|
|
2119
|
+
...everyNamespace ? {} : { namespace },
|
|
2120
|
+
...operationId !== null ? { operationId } : {},
|
|
2121
|
+
...method !== null ? { method } : {},
|
|
2122
|
+
...path !== null ? { path } : {},
|
|
2123
|
+
...faultId !== null ? { faultId } : {},
|
|
2124
|
+
...status !== null ? { status: Number(status) } : {},
|
|
2125
|
+
...sinceMs !== void 0 ? { since: sinceMs } : {},
|
|
2126
|
+
...limit !== null ? { limit: Number(limit) } : {}
|
|
2127
|
+
})
|
|
2128
|
+
});
|
|
2129
|
+
},
|
|
2130
|
+
"DELETE /requests": ({ url, namespace }) => {
|
|
2131
|
+
context.journal.clear(url.searchParams.get("all") === "1" ? void 0 : namespace);
|
|
2132
|
+
return json(200, { status: "ok" });
|
|
2133
|
+
},
|
|
2134
|
+
// Totals across every namespace, unless the caller names one.
|
|
2135
|
+
"GET /metrics": ({ request, url, namespace }) => json(200, context.metrics.report(explicitNamespace(request, url) ? namespace : void 0)),
|
|
2136
|
+
"DELETE /metrics": ({ request, url, namespace }) => {
|
|
2137
|
+
context.metrics.reset(explicitNamespace(request, url) ? namespace : void 0);
|
|
2138
|
+
return json(200, { status: "ok" });
|
|
2139
|
+
}
|
|
2140
|
+
};
|
|
2141
|
+
for (const key of Object.keys(context.routes)) {
|
|
2142
|
+
if (Object.hasOwn(builtin, key))
|
|
2143
|
+
throw new Error(`admin route ${key} overrides a shared route`);
|
|
2144
|
+
const path = key.slice(key.indexOf(" ") + 1);
|
|
2145
|
+
if (isAdminPath(path, "/ns") || isAdminPath(path, "/blobs"))
|
|
2146
|
+
throw new Error(`admin route ${key} overlaps an internal transport`);
|
|
2147
|
+
}
|
|
2148
|
+
const routes = [...Object.entries(builtin), ...Object.entries(context.routes)].map(([key, handler]) => {
|
|
2149
|
+
const space = key.indexOf(" ");
|
|
2150
|
+
return { method: key.slice(0, space), pattern: key.slice(space + 1), handler };
|
|
2151
|
+
}).sort((a, b) => paramCount(a.pattern) - paramCount(b.pattern));
|
|
2152
|
+
return {
|
|
2153
|
+
namespaceOf: headerNamespace,
|
|
2154
|
+
controls,
|
|
2155
|
+
snapshots,
|
|
2156
|
+
async handle(request) {
|
|
2157
|
+
const url = new URL(request.url);
|
|
2158
|
+
if (url.pathname === `${adminPrefix}/health` && request.method === "GET") {
|
|
2159
|
+
return health();
|
|
2160
|
+
}
|
|
2161
|
+
if (!isAdminPath(url.pathname, adminPrefix)) {
|
|
2162
|
+
return void 0;
|
|
2163
|
+
}
|
|
2164
|
+
const path = url.pathname.slice(adminPrefix.length) || "/";
|
|
2165
|
+
const shell = request.method === "GET" && (path === "/ui" || path === "/ui/" || path === ADMIN_BRANDS_PATH);
|
|
2166
|
+
if (!shell && context.adminKey !== void 0 && !hasAdminKey(request.headers, context.adminKey)) {
|
|
2167
|
+
return adminError(401, `missing or wrong ${ADMIN_KEY_HEADER}`);
|
|
2168
|
+
}
|
|
2169
|
+
for (const route of routes) {
|
|
2170
|
+
if (route.method !== request.method)
|
|
2171
|
+
continue;
|
|
2172
|
+
let params;
|
|
2173
|
+
try {
|
|
2174
|
+
params = matchRoute(route.pattern, path);
|
|
2175
|
+
} catch {
|
|
2176
|
+
return adminError(400, "invalid path encoding");
|
|
2177
|
+
}
|
|
2178
|
+
if (!params)
|
|
2179
|
+
continue;
|
|
2180
|
+
let body;
|
|
2181
|
+
try {
|
|
2182
|
+
body = await readJson(request);
|
|
2183
|
+
} catch {
|
|
2184
|
+
return adminError(400, "request body is not valid JSON");
|
|
2185
|
+
}
|
|
2186
|
+
const namespace = adminNamespace(request, url);
|
|
2187
|
+
if (!KEY_PATTERN.test(namespace))
|
|
2188
|
+
return adminError(400, "invalid namespace");
|
|
2189
|
+
return route.handler({
|
|
2190
|
+
request,
|
|
2191
|
+
url,
|
|
2192
|
+
params,
|
|
2193
|
+
namespace,
|
|
2194
|
+
body
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
return adminError(404, `no admin route ${request.method} ${path}; GET ${adminPrefix} lists them`);
|
|
2198
|
+
}
|
|
2199
|
+
};
|
|
2200
|
+
};
|
|
2201
|
+
|
|
2202
|
+
// ../service/core/dist/credentials.js
|
|
2203
|
+
var bearerToken = (request) => {
|
|
2204
|
+
const header = request.headers.get("authorization");
|
|
2205
|
+
if (!header)
|
|
2206
|
+
return void 0;
|
|
2207
|
+
const match = /^Bearer\s+(.+)$/i.exec(header.trim());
|
|
2208
|
+
return match?.[1]?.trim() || void 0;
|
|
2209
|
+
};
|
|
2210
|
+
var basicAuth = (request) => {
|
|
2211
|
+
const header = request.headers.get("authorization");
|
|
2212
|
+
if (!header)
|
|
2213
|
+
return void 0;
|
|
2214
|
+
const match = /^Basic\s+(.+)$/i.exec(header.trim());
|
|
2215
|
+
if (!match?.[1])
|
|
2216
|
+
return void 0;
|
|
2217
|
+
let decoded;
|
|
2218
|
+
try {
|
|
2219
|
+
decoded = atob(match[1].trim());
|
|
2220
|
+
} catch {
|
|
2221
|
+
return void 0;
|
|
2222
|
+
}
|
|
2223
|
+
const colon = decoded.indexOf(":");
|
|
2224
|
+
if (colon < 0)
|
|
2225
|
+
return { username: decoded, password: "" };
|
|
2226
|
+
return { username: decoded.slice(0, colon), password: decoded.slice(colon + 1) };
|
|
2227
|
+
};
|
|
2228
|
+
var sigV4AccessKeyId = (request) => {
|
|
2229
|
+
const header = request.headers.get("authorization");
|
|
2230
|
+
const fromHeader = header ? /Credential=([^/,\s]+)\//.exec(header)?.[1] : void 0;
|
|
2231
|
+
if (fromHeader)
|
|
2232
|
+
return fromHeader;
|
|
2233
|
+
const query = new URL(request.url).searchParams.get("X-Amz-Credential");
|
|
2234
|
+
return query ? query.split("/")[0] ?? void 0 : void 0;
|
|
2235
|
+
};
|
|
2236
|
+
var anyCredential = (request) => bearerToken(request) ?? basicAuth(request)?.username ?? sigV4AccessKeyId(request) ?? request.headers.get("x-api-key") ?? void 0;
|
|
2237
|
+
var createCredentialRegistry = () => {
|
|
2238
|
+
const map = /* @__PURE__ */ new Map();
|
|
2239
|
+
return {
|
|
2240
|
+
set: (credential, namespace) => {
|
|
2241
|
+
map.set(credential, namespace);
|
|
2242
|
+
},
|
|
2243
|
+
get: (credential) => map.get(credential),
|
|
2244
|
+
remove: (credential) => map.delete(credential),
|
|
2245
|
+
clear: () => map.clear(),
|
|
2246
|
+
entries: () => [...map].map(([credential, namespace]) => ({ credential, namespace })).sort((a, b) => a.credential.localeCompare(b.credential))
|
|
2247
|
+
};
|
|
2248
|
+
};
|
|
2249
|
+
var maskCredential = (credential) => credential.length <= 8 ? `${credential.slice(0, 2)}\u2026` : `${credential.slice(0, 6)}\u2026${credential.slice(-2)}`;
|
|
2250
|
+
|
|
2251
|
+
// ../service/core/dist/metrics.js
|
|
2252
|
+
var createMetrics = (partition = true) => {
|
|
2253
|
+
const namespaces = /* @__PURE__ */ new Map();
|
|
2254
|
+
let requests = 0;
|
|
2255
|
+
let faults = 0;
|
|
2256
|
+
let totalDurationMs = 0;
|
|
2257
|
+
const byOperation = /* @__PURE__ */ new Map();
|
|
2258
|
+
const unmatched = /* @__PURE__ */ new Map();
|
|
2259
|
+
return {
|
|
2260
|
+
record(entry) {
|
|
2261
|
+
if (partition) {
|
|
2262
|
+
let scoped = namespaces.get(entry.namespace);
|
|
2263
|
+
if (!scoped) {
|
|
2264
|
+
scoped = createMetrics(false);
|
|
2265
|
+
namespaces.set(entry.namespace, scoped);
|
|
2266
|
+
}
|
|
2267
|
+
scoped.record(entry);
|
|
2268
|
+
}
|
|
2269
|
+
requests++;
|
|
2270
|
+
totalDurationMs += entry.durationMs;
|
|
2271
|
+
if (entry.faultId !== void 0)
|
|
2272
|
+
faults++;
|
|
2273
|
+
const key = `${entry.operationId ?? "(unmatched)"} ${entry.status}`;
|
|
2274
|
+
byOperation.set(key, (byOperation.get(key) ?? 0) + 1);
|
|
2275
|
+
if (entry.unmatched) {
|
|
2276
|
+
const route = `${entry.method} ${entry.path}`;
|
|
2277
|
+
unmatched.set(route, (unmatched.get(route) ?? 0) + 1);
|
|
2278
|
+
}
|
|
2279
|
+
},
|
|
2280
|
+
report: (namespace) => namespace !== void 0 ? (namespaces.get(namespace) ?? createMetrics(false)).report() : {
|
|
2281
|
+
requests,
|
|
2282
|
+
byOperation: Object.fromEntries([...byOperation].sort(([a], [b]) => a.localeCompare(b))),
|
|
2283
|
+
unmatched: [...unmatched].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([route, count2]) => {
|
|
2284
|
+
const space = route.indexOf(" ");
|
|
2285
|
+
return { method: route.slice(0, space), path: route.slice(space + 1), count: count2 };
|
|
2286
|
+
}),
|
|
2287
|
+
faults,
|
|
2288
|
+
totalDurationMs
|
|
2289
|
+
},
|
|
2290
|
+
reset(namespace) {
|
|
2291
|
+
if (namespace !== void 0) {
|
|
2292
|
+
namespaces.delete(namespace);
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
namespaces.clear();
|
|
2296
|
+
requests = 0;
|
|
2297
|
+
faults = 0;
|
|
2298
|
+
totalDurationMs = 0;
|
|
2299
|
+
byOperation.clear();
|
|
2300
|
+
unmatched.clear();
|
|
2301
|
+
}
|
|
2302
|
+
};
|
|
2303
|
+
};
|
|
2304
|
+
|
|
2305
|
+
// ../service/core/dist/admin-client.js
|
|
2306
|
+
var loading;
|
|
2307
|
+
var loadAdminClient = () => {
|
|
2308
|
+
loading ??= import("./admin-ui.js").catch((error) => {
|
|
2309
|
+
loading = void 0;
|
|
2310
|
+
throw error;
|
|
2311
|
+
});
|
|
2312
|
+
return loading;
|
|
2313
|
+
};
|
|
2314
|
+
|
|
2315
|
+
// ../service/core/dist/admin-ui.js
|
|
2316
|
+
var escapeHtml = (value) => value.replace(/[&<>"']/g, (char) => {
|
|
2317
|
+
switch (char) {
|
|
2318
|
+
case "&":
|
|
2319
|
+
return "&";
|
|
2320
|
+
case "<":
|
|
2321
|
+
return "<";
|
|
2322
|
+
case ">":
|
|
2323
|
+
return ">";
|
|
2324
|
+
case '"':
|
|
2325
|
+
return """;
|
|
2326
|
+
default:
|
|
2327
|
+
return "'";
|
|
2328
|
+
}
|
|
2329
|
+
});
|
|
2330
|
+
var EMBEDDED_BRAND = typeof __MOCKINGBIRD_ADMIN_BRAND__ === "object" ? __MOCKINGBIRD_ADMIN_BRAND__ : void 0;
|
|
2331
|
+
var adminBrandCatalog = (service, brand = EMBEDDED_BRAND) => brand ? { [service]: brand } : {};
|
|
2332
|
+
var adminDocument = (client, service, adminPrefix) => {
|
|
2333
|
+
const name2 = escapeHtml(service);
|
|
2334
|
+
const script = client.adminClientSource({
|
|
2335
|
+
standardRoutes: STANDARD_ADMIN_ROUTES,
|
|
2336
|
+
adminPrefix,
|
|
2337
|
+
adminKeyHeader: ADMIN_KEY_HEADER,
|
|
2338
|
+
brandsUrl: `${adminPrefix}${ADMIN_BRANDS_PATH}`,
|
|
2339
|
+
service
|
|
2340
|
+
});
|
|
2341
|
+
return `<!DOCTYPE html>
|
|
2342
|
+
<html lang="en">
|
|
2343
|
+
<head>
|
|
2344
|
+
<meta charset="utf-8">
|
|
2345
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
2346
|
+
<meta name="color-scheme" content="light dark">
|
|
2347
|
+
<title>${name2} admin</title>
|
|
2348
|
+
<style>html,body{margin:0;min-height:100%}#admin-root{min-height:100%}</style>
|
|
2349
|
+
</head>
|
|
2350
|
+
<body>
|
|
2351
|
+
<main id="admin-root" data-mockingbird-admin data-admin-ui-library="antd" data-service="${name2}">
|
|
2352
|
+
<noscript>Enable JavaScript to use administration.</noscript>
|
|
2353
|
+
</main>
|
|
2354
|
+
${script}
|
|
2355
|
+
</body>
|
|
2356
|
+
</html>`;
|
|
2357
|
+
};
|
|
2358
|
+
var htmlResponse = (body) => new Response(body, {
|
|
2359
|
+
status: 200,
|
|
2360
|
+
headers: {
|
|
2361
|
+
"content-type": "text/html; charset=utf-8",
|
|
2362
|
+
"cache-control": "no-store"
|
|
2363
|
+
}
|
|
2364
|
+
});
|
|
2365
|
+
var json2 = (body) => new Response(JSON.stringify(body), {
|
|
2366
|
+
status: 200,
|
|
2367
|
+
headers: { "content-type": "application/json; charset=utf-8" }
|
|
2368
|
+
});
|
|
2369
|
+
var describeExtension = (extension) => {
|
|
2370
|
+
if (extension.kind === "route")
|
|
2371
|
+
return { ...extension };
|
|
2372
|
+
if (extension.kind === "sql") {
|
|
2373
|
+
return {
|
|
2374
|
+
kind: "sql",
|
|
2375
|
+
id: extension.id ?? "sql",
|
|
2376
|
+
title: extension.title ?? "SQL",
|
|
2377
|
+
...extension.description !== void 0 ? { description: extension.description } : {}
|
|
2378
|
+
};
|
|
2379
|
+
}
|
|
2380
|
+
return {
|
|
2381
|
+
kind: "panel",
|
|
2382
|
+
id: extension.id,
|
|
2383
|
+
title: extension.title,
|
|
2384
|
+
...extension.description !== void 0 ? { description: extension.description } : {},
|
|
2385
|
+
html: extension.html,
|
|
2386
|
+
...extension.script !== void 0 ? { script: extension.script } : {}
|
|
2387
|
+
};
|
|
2388
|
+
};
|
|
2389
|
+
var adminUiRoutes = (service, ui, adminPrefix = ADMIN_PREFIX, brand = EMBEDDED_BRAND) => {
|
|
2390
|
+
const page = async ({ request }) => {
|
|
2391
|
+
let client;
|
|
2392
|
+
try {
|
|
2393
|
+
client = await loadAdminClient();
|
|
2394
|
+
} catch (error) {
|
|
2395
|
+
return new Response(`The admin page is not available: its browser client (admin-ui.js) could not be loaded. ${error instanceof Error ? error.message : String(error)}`, { status: 503, headers: { "content-type": "text/plain; charset=utf-8" } });
|
|
2396
|
+
}
|
|
2397
|
+
const mountPrefix = new URL(requestBaseUrl(request)).pathname.replace(/\/$/, "");
|
|
2398
|
+
const shell = () => adminDocument(client, service, `${mountPrefix}${adminPrefix}`);
|
|
2399
|
+
return htmlResponse(ui?.render ? ui.render({ service, defaultHtml: shell }) : shell());
|
|
2400
|
+
};
|
|
2401
|
+
return {
|
|
2402
|
+
"GET /ui": page,
|
|
2403
|
+
"GET /ui/": page,
|
|
2404
|
+
[`GET ${ADMIN_BRANDS_PATH}`]: () => json2(adminBrandCatalog(service, brand)),
|
|
2405
|
+
"GET /ui/manifest": () => json2({
|
|
2406
|
+
service,
|
|
2407
|
+
standardRoutes: STANDARD_ADMIN_ROUTES,
|
|
2408
|
+
panels: (ui?.panels ?? []).map((panel) => ({
|
|
2409
|
+
id: panel.id,
|
|
2410
|
+
title: panel.title,
|
|
2411
|
+
...panel.description !== void 0 ? { description: panel.description } : {},
|
|
2412
|
+
html: panel.html,
|
|
2413
|
+
...panel.script !== void 0 ? { script: panel.script } : {}
|
|
2414
|
+
})),
|
|
2415
|
+
extensions: (ui?.extensions ?? []).map(describeExtension)
|
|
2416
|
+
})
|
|
2417
|
+
};
|
|
2418
|
+
};
|
|
2419
|
+
|
|
2420
|
+
export {
|
|
2421
|
+
DEFAULT_JOURNAL_SIZE,
|
|
2422
|
+
createJournal,
|
|
2423
|
+
annotateResponse,
|
|
2424
|
+
responseNotes,
|
|
2425
|
+
createClock,
|
|
2426
|
+
deriveClock,
|
|
2427
|
+
restoreClock,
|
|
2428
|
+
upstreamState,
|
|
2429
|
+
upstreamRequest,
|
|
2430
|
+
ControlError,
|
|
2431
|
+
parseDuration,
|
|
2432
|
+
parseInstant,
|
|
2433
|
+
normalizeSeed,
|
|
2434
|
+
SNAPSHOT_FORMAT,
|
|
2435
|
+
KEY_PATTERN,
|
|
2436
|
+
isRecord,
|
|
2437
|
+
normalizeLatency,
|
|
2438
|
+
normalizeClock,
|
|
2439
|
+
assertSnapshotDocument,
|
|
2440
|
+
normalizeControls,
|
|
2441
|
+
normalizeBootControls,
|
|
2442
|
+
CONTROL_HEADERS,
|
|
2443
|
+
CONTROL_HEADER_REFERENCE,
|
|
2444
|
+
parseFaultHeader,
|
|
2445
|
+
parseControlHeaders,
|
|
2446
|
+
controlHeaders,
|
|
2447
|
+
seedFrom,
|
|
2448
|
+
createRng,
|
|
2449
|
+
STANDARD_FAULT_PRESETS,
|
|
2450
|
+
normalizeFault,
|
|
2451
|
+
createFaultRegistry,
|
|
2452
|
+
createLatencyRegistry,
|
|
2453
|
+
createNamespaceControls,
|
|
2454
|
+
toBase64,
|
|
2455
|
+
fromBase64,
|
|
2456
|
+
toHex,
|
|
2457
|
+
hmac,
|
|
2458
|
+
sha,
|
|
2459
|
+
svixSecretBytes,
|
|
2460
|
+
signSvix,
|
|
2461
|
+
signTimestamped,
|
|
2462
|
+
signTwilio,
|
|
2463
|
+
timingSafeEqual,
|
|
2464
|
+
defineMock,
|
|
2465
|
+
ADMIN_ROUTE_REFERENCE,
|
|
2466
|
+
STANDARD_ADMIN_ROUTES,
|
|
2467
|
+
assertAdminUi,
|
|
2468
|
+
HEALTH_PATH,
|
|
2469
|
+
ADMIN_PREFIX,
|
|
2470
|
+
resolveAdminPrefix,
|
|
2471
|
+
isAdminPath,
|
|
2472
|
+
matchNamespacePath,
|
|
2473
|
+
assertAdminPrefixAvailable,
|
|
2474
|
+
ADMIN_KEY_HEADER,
|
|
2475
|
+
hasAdminKey,
|
|
2476
|
+
NAMESPACE_HEADER,
|
|
2477
|
+
ADMIN_DOCS_URL,
|
|
2478
|
+
ADMIN_CONTROL_PAGES,
|
|
2479
|
+
createControlPlane,
|
|
2480
|
+
bearerToken,
|
|
2481
|
+
basicAuth,
|
|
2482
|
+
sigV4AccessKeyId,
|
|
2483
|
+
anyCredential,
|
|
2484
|
+
createCredentialRegistry,
|
|
2485
|
+
maskCredential,
|
|
2486
|
+
createMetrics,
|
|
2487
|
+
adminUiRoutes
|
|
2488
|
+
};
|
|
2489
|
+
//# sourceMappingURL=chunk-XNS34BPF.js.map
|