@crvouga/mockingbird-service-rxvortex 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -4
- package/dist/{chunk-DOSGPNKC.js → chunk-KQEUUCLU.js} +1481 -91
- package/dist/chunk-KQEUUCLU.js.map +7 -0
- package/dist/{chunk-6CZCXPHW.js → chunk-VHD526HH.js} +3 -2
- package/dist/{chunk-6CZCXPHW.js.map → chunk-VHD526HH.js.map} +2 -2
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +41 -3
- package/dist/index.js +1 -1
- package/dist/server.d.ts +73 -3
- package/dist/server.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-DOSGPNKC.js.map +0 -7
|
@@ -38,30 +38,30 @@ var createClock = (source = Date.now) => {
|
|
|
38
38
|
var Collection = class {
|
|
39
39
|
sqlite;
|
|
40
40
|
namespace;
|
|
41
|
-
|
|
42
|
-
constructor(sqlite, namespace,
|
|
41
|
+
collectionName;
|
|
42
|
+
constructor(sqlite, namespace, collectionName) {
|
|
43
43
|
this.sqlite = sqlite;
|
|
44
44
|
this.namespace = namespace;
|
|
45
|
-
this.
|
|
45
|
+
this.collectionName = collectionName;
|
|
46
46
|
}
|
|
47
47
|
bumpCollectionSeq() {
|
|
48
|
-
const row = this.sqlite.prepare("SELECT value FROM mockingbird_sequences WHERE namespace = ? AND name = ? AND kind = 'collection'").get(this.namespace, this.
|
|
48
|
+
const row = this.sqlite.prepare("SELECT value FROM mockingbird_sequences WHERE namespace = ? AND name = ? AND kind = 'collection'").get(this.namespace, this.collectionName);
|
|
49
49
|
const next = (row?.value ?? 0) + 1;
|
|
50
50
|
this.sqlite.prepare(`INSERT INTO mockingbird_sequences (namespace, name, kind, value) VALUES (?, ?, 'collection', ?)
|
|
51
|
-
ON CONFLICT(namespace, name, kind) DO UPDATE SET value = excluded.value`).run(this.namespace, this.
|
|
51
|
+
ON CONFLICT(namespace, name, kind) DO UPDATE SET value = excluded.value`).run(this.namespace, this.collectionName, next);
|
|
52
52
|
return next;
|
|
53
53
|
}
|
|
54
54
|
nextSequence() {
|
|
55
55
|
return this.sqlite.transaction(() => this.bumpCollectionSeq());
|
|
56
56
|
}
|
|
57
57
|
get(id) {
|
|
58
|
-
const row = this.sqlite.prepare("SELECT value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.
|
|
58
|
+
const row = this.sqlite.prepare("SELECT value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.collectionName, id);
|
|
59
59
|
if (!row)
|
|
60
60
|
return void 0;
|
|
61
61
|
return JSON.parse(row.value).value;
|
|
62
62
|
}
|
|
63
63
|
has(id) {
|
|
64
|
-
const row = this.sqlite.prepare("SELECT 1 AS ok FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.
|
|
64
|
+
const row = this.sqlite.prepare("SELECT 1 AS ok FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.collectionName, id);
|
|
65
65
|
return row !== void 0;
|
|
66
66
|
}
|
|
67
67
|
/** Insert a new record, assigning it the next sequence number. */
|
|
@@ -71,32 +71,32 @@ var Collection = class {
|
|
|
71
71
|
const stored = { seq, value };
|
|
72
72
|
this.sqlite.prepare(`INSERT INTO mockingbird_records (namespace, collection, id, seq, value)
|
|
73
73
|
VALUES (?, ?, ?, ?, ?)
|
|
74
|
-
ON CONFLICT(namespace, collection, id) DO UPDATE SET seq = excluded.seq, value = excluded.value`).run(this.namespace, this.
|
|
74
|
+
ON CONFLICT(namespace, collection, id) DO UPDATE SET seq = excluded.seq, value = excluded.value`).run(this.namespace, this.collectionName, id, seq, JSON.stringify(stored));
|
|
75
75
|
return stored;
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
/** Replace an existing record's value, keeping its position. */
|
|
79
79
|
update(id, value) {
|
|
80
80
|
return this.sqlite.transaction(() => {
|
|
81
|
-
const row = this.sqlite.prepare("SELECT seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.
|
|
81
|
+
const row = this.sqlite.prepare("SELECT seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.collectionName, id);
|
|
82
82
|
if (!row)
|
|
83
83
|
return void 0;
|
|
84
84
|
const stored = { seq: row.seq, value };
|
|
85
|
-
this.sqlite.prepare("UPDATE mockingbird_records SET value = ? WHERE namespace = ? AND collection = ? AND id = ?").run(JSON.stringify(stored), this.namespace, this.
|
|
85
|
+
this.sqlite.prepare("UPDATE mockingbird_records SET value = ? WHERE namespace = ? AND collection = ? AND id = ?").run(JSON.stringify(stored), this.namespace, this.collectionName, id);
|
|
86
86
|
return stored;
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
delete(id) {
|
|
90
|
-
const result = this.sqlite.prepare("DELETE FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").run(this.namespace, this.
|
|
90
|
+
const result = this.sqlite.prepare("DELETE FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").run(this.namespace, this.collectionName, id);
|
|
91
91
|
return result.changes > 0;
|
|
92
92
|
}
|
|
93
93
|
/** How many records the collection holds, without reading them. */
|
|
94
94
|
count() {
|
|
95
|
-
const row = this.sqlite.prepare("SELECT COUNT(*) AS n FROM mockingbird_records WHERE namespace = ? AND collection = ?").get(this.namespace, this.
|
|
95
|
+
const row = this.sqlite.prepare("SELECT COUNT(*) AS n FROM mockingbird_records WHERE namespace = ? AND collection = ?").get(this.namespace, this.collectionName);
|
|
96
96
|
return Number(row?.n ?? 0);
|
|
97
97
|
}
|
|
98
98
|
list(options = {}) {
|
|
99
|
-
const rows = this.sqlite.prepare("SELECT id, seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ?").all(this.namespace, this.
|
|
99
|
+
const rows = this.sqlite.prepare("SELECT id, seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ?").all(this.namespace, this.collectionName);
|
|
100
100
|
const out = [];
|
|
101
101
|
for (const row of rows) {
|
|
102
102
|
const stored = JSON.parse(row.value);
|
|
@@ -353,16 +353,18 @@ var createControlPlane = (context) => {
|
|
|
353
353
|
uptimeMs: context.wallNow() - context.startedAt,
|
|
354
354
|
clock: context.clock.state(),
|
|
355
355
|
namespaces: context.namespaces().length,
|
|
356
|
+
adminUi: `${ADMIN_PREFIX}/ui`,
|
|
356
357
|
...context.describe()
|
|
357
358
|
});
|
|
358
359
|
}
|
|
359
360
|
if (url.pathname !== ADMIN_PREFIX && !url.pathname.startsWith(`${ADMIN_PREFIX}/`)) {
|
|
360
361
|
return void 0;
|
|
361
362
|
}
|
|
362
|
-
|
|
363
|
+
const path = url.pathname.slice(ADMIN_PREFIX.length) || "/";
|
|
364
|
+
const shell = request.method === "GET" && (path === "/ui" || path === "/ui/");
|
|
365
|
+
if (!shell && context.adminKey !== void 0 && request.headers.get(ADMIN_KEY_HEADER) !== context.adminKey) {
|
|
363
366
|
return adminError(401, `missing or wrong ${ADMIN_KEY_HEADER}`);
|
|
364
367
|
}
|
|
365
|
-
const path = url.pathname.slice(ADMIN_PREFIX.length) || "/";
|
|
366
368
|
for (const route of routes) {
|
|
367
369
|
if (route.method !== request.method)
|
|
368
370
|
continue;
|
|
@@ -699,64 +701,64 @@ var graphemeLength = (value) => [...value].length;
|
|
|
699
701
|
var validateValue = (document2, schema, value, path = []) => {
|
|
700
702
|
const errors = [];
|
|
701
703
|
const s = resolveSchema(document2, schema);
|
|
702
|
-
const
|
|
704
|
+
const fail2 = (message) => errors.push({ path, message });
|
|
703
705
|
const actual = jsonTypeOf(value);
|
|
704
706
|
if (actual === "undefined") {
|
|
705
|
-
|
|
707
|
+
fail2("value is undefined");
|
|
706
708
|
return errors;
|
|
707
709
|
}
|
|
708
710
|
const types = schemaTypes(s);
|
|
709
711
|
if (types.length > 0) {
|
|
710
712
|
const ok = types.some((t) => t === actual || t === "number" && actual === "integer");
|
|
711
713
|
if (!ok) {
|
|
712
|
-
|
|
714
|
+
fail2(`expected type ${types.join("|")}, got ${actual}`);
|
|
713
715
|
return errors;
|
|
714
716
|
}
|
|
715
717
|
}
|
|
716
718
|
if (s.enum && !(value === null && types.includes("null")) && !s.enum.some((candidate) => deepEqual(candidate, value))) {
|
|
717
|
-
|
|
719
|
+
fail2("value not in enum");
|
|
718
720
|
}
|
|
719
721
|
if (s.const !== void 0 && !deepEqual(s.const, value))
|
|
720
|
-
|
|
722
|
+
fail2("value does not equal const");
|
|
721
723
|
if (typeof value === "string") {
|
|
722
724
|
const length = graphemeLength(value);
|
|
723
725
|
if (s.minLength !== void 0 && length < s.minLength)
|
|
724
|
-
|
|
726
|
+
fail2(`length ${length} < minLength ${s.minLength}`);
|
|
725
727
|
if (s.maxLength !== void 0 && length > s.maxLength)
|
|
726
|
-
|
|
728
|
+
fail2(`length ${length} > maxLength ${s.maxLength}`);
|
|
727
729
|
if (s.pattern !== void 0) {
|
|
728
730
|
try {
|
|
729
731
|
if (!new RegExp(s.pattern, "u").test(value))
|
|
730
|
-
|
|
732
|
+
fail2(`does not match pattern ${s.pattern}`);
|
|
731
733
|
} catch {
|
|
732
734
|
}
|
|
733
735
|
}
|
|
734
736
|
if (s.format !== void 0) {
|
|
735
737
|
const pattern = FORMAT_PATTERNS[s.format];
|
|
736
738
|
if (pattern && !pattern.test(value))
|
|
737
|
-
|
|
739
|
+
fail2(`does not match format ${s.format}`);
|
|
738
740
|
}
|
|
739
741
|
}
|
|
740
742
|
if (typeof value === "number") {
|
|
741
743
|
if (s.minimum !== void 0 && value < s.minimum)
|
|
742
|
-
|
|
744
|
+
fail2(`${value} < minimum ${s.minimum}`);
|
|
743
745
|
if (s.maximum !== void 0 && value > s.maximum)
|
|
744
|
-
|
|
746
|
+
fail2(`${value} > maximum ${s.maximum}`);
|
|
745
747
|
if (s.exclusiveMinimum !== void 0 && value <= s.exclusiveMinimum)
|
|
746
|
-
|
|
748
|
+
fail2(`${value} <= exclusiveMinimum ${s.exclusiveMinimum}`);
|
|
747
749
|
if (s.exclusiveMaximum !== void 0 && value >= s.exclusiveMaximum)
|
|
748
|
-
|
|
750
|
+
fail2(`${value} >= exclusiveMaximum ${s.exclusiveMaximum}`);
|
|
749
751
|
if (s.multipleOf !== void 0 && Math.abs(value / s.multipleOf - Math.round(value / s.multipleOf)) > 1e-9) {
|
|
750
|
-
|
|
752
|
+
fail2(`${value} is not a multiple of ${s.multipleOf}`);
|
|
751
753
|
}
|
|
752
754
|
}
|
|
753
755
|
if (Array.isArray(value)) {
|
|
754
756
|
if (s.minItems !== void 0 && value.length < s.minItems)
|
|
755
|
-
|
|
757
|
+
fail2(`${value.length} items < minItems ${s.minItems}`);
|
|
756
758
|
if (s.maxItems !== void 0 && value.length > s.maxItems)
|
|
757
|
-
|
|
759
|
+
fail2(`${value.length} items > maxItems ${s.maxItems}`);
|
|
758
760
|
if (s.uniqueItems && value.some((item, i) => value.slice(0, i).some((prev) => deepEqual(prev, item))))
|
|
759
|
-
|
|
761
|
+
fail2("items are not unique");
|
|
760
762
|
value.forEach((item, i) => {
|
|
761
763
|
const itemSchema = s.prefixItems?.[i] ?? s.items;
|
|
762
764
|
if (itemSchema)
|
|
@@ -768,11 +770,11 @@ var validateValue = (document2, schema, value, path = []) => {
|
|
|
768
770
|
const keys = Object.keys(record2);
|
|
769
771
|
for (const name of s.required ?? [])
|
|
770
772
|
if (!(name in record2))
|
|
771
|
-
|
|
773
|
+
fail2(`missing required property ${name}`);
|
|
772
774
|
if (s.minProperties !== void 0 && keys.length < s.minProperties)
|
|
773
|
-
|
|
775
|
+
fail2(`${keys.length} properties < minProperties ${s.minProperties}`);
|
|
774
776
|
if (s.maxProperties !== void 0 && keys.length > s.maxProperties)
|
|
775
|
-
|
|
777
|
+
fail2(`${keys.length} properties > maxProperties ${s.maxProperties}`);
|
|
776
778
|
for (const key of keys) {
|
|
777
779
|
const property = s.properties?.[key];
|
|
778
780
|
if (property) {
|
|
@@ -780,14 +782,14 @@ var validateValue = (document2, schema, value, path = []) => {
|
|
|
780
782
|
continue;
|
|
781
783
|
}
|
|
782
784
|
if (s.additionalProperties === false)
|
|
783
|
-
|
|
785
|
+
fail2(`unexpected property ${key}`);
|
|
784
786
|
else if (typeof s.additionalProperties === "object") {
|
|
785
787
|
errors.push(...validateValue(document2, s.additionalProperties, record2[key], [...path, key]));
|
|
786
788
|
}
|
|
787
789
|
if (s.propertyNames) {
|
|
788
790
|
const nameErrors = validateValue(document2, s.propertyNames, key, [...path, key]);
|
|
789
791
|
if (nameErrors.length > 0)
|
|
790
|
-
|
|
792
|
+
fail2(`property name ${key} is invalid: ${nameErrors[0]?.message}`);
|
|
791
793
|
}
|
|
792
794
|
}
|
|
793
795
|
}
|
|
@@ -795,14 +797,14 @@ var validateValue = (document2, schema, value, path = []) => {
|
|
|
795
797
|
for (const branch of s.allOf)
|
|
796
798
|
errors.push(...validateValue(document2, branch, value, path));
|
|
797
799
|
if (s.anyOf && !s.anyOf.some((branch) => validateValue(document2, branch, value).length === 0))
|
|
798
|
-
|
|
800
|
+
fail2("matches no anyOf branch");
|
|
799
801
|
if (s.oneOf) {
|
|
800
802
|
const matches2 = s.oneOf.filter((branch) => validateValue(document2, branch, value).length === 0).length;
|
|
801
803
|
if (matches2 !== 1)
|
|
802
|
-
|
|
804
|
+
fail2(`matches ${matches2} oneOf branches, expected exactly 1`);
|
|
803
805
|
}
|
|
804
806
|
if (s.not && validateValue(document2, s.not, value).length === 0)
|
|
805
|
-
|
|
807
|
+
fail2("matches forbidden `not` schema");
|
|
806
808
|
return errors;
|
|
807
809
|
};
|
|
808
810
|
|
|
@@ -1334,6 +1336,936 @@ var clearNamespace = (sqlite, namespace) => {
|
|
|
1334
1336
|
});
|
|
1335
1337
|
};
|
|
1336
1338
|
|
|
1339
|
+
// ../core/dist/surface.js
|
|
1340
|
+
var defineMock = (runtime) => runtime;
|
|
1341
|
+
var STANDARD_ADMIN_ROUTES = [
|
|
1342
|
+
"GET /",
|
|
1343
|
+
"POST /reset",
|
|
1344
|
+
"GET /namespaces",
|
|
1345
|
+
"GET /clock",
|
|
1346
|
+
"POST /clock",
|
|
1347
|
+
"GET /faults",
|
|
1348
|
+
"POST /faults",
|
|
1349
|
+
"DELETE /faults",
|
|
1350
|
+
"POST /snapshots",
|
|
1351
|
+
"POST /snapshots/:id/restore",
|
|
1352
|
+
"DELETE /snapshots/:id",
|
|
1353
|
+
"GET /timeline",
|
|
1354
|
+
"POST /checkpoints",
|
|
1355
|
+
"POST /branches/:name",
|
|
1356
|
+
"POST /branches/:name/checkout",
|
|
1357
|
+
"GET /requests",
|
|
1358
|
+
"DELETE /requests",
|
|
1359
|
+
"GET /metrics",
|
|
1360
|
+
"DELETE /metrics",
|
|
1361
|
+
"GET /credentials",
|
|
1362
|
+
"PUT /credentials",
|
|
1363
|
+
"DELETE /credentials",
|
|
1364
|
+
"GET /state",
|
|
1365
|
+
"GET /state/:collection",
|
|
1366
|
+
"GET /state/:collection/:id",
|
|
1367
|
+
"POST /state/:collection",
|
|
1368
|
+
"PUT /state/:collection/:id",
|
|
1369
|
+
"PATCH /state/:collection/:id",
|
|
1370
|
+
"DELETE /state/:collection/:id",
|
|
1371
|
+
"GET /ui",
|
|
1372
|
+
"GET /ui/",
|
|
1373
|
+
"GET /ui/manifest"
|
|
1374
|
+
];
|
|
1375
|
+
var PANEL_ID = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/;
|
|
1376
|
+
var assertAdminUi = (ui) => {
|
|
1377
|
+
if (!ui?.panels)
|
|
1378
|
+
return;
|
|
1379
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1380
|
+
for (const panel of ui.panels) {
|
|
1381
|
+
if (!PANEL_ID.test(panel.id)) {
|
|
1382
|
+
throw new RangeError(`admin panel id must match ${PANEL_ID}: ${JSON.stringify(panel.id)}`);
|
|
1383
|
+
}
|
|
1384
|
+
if (seen.has(panel.id))
|
|
1385
|
+
throw new RangeError(`duplicate admin panel id ${panel.id}`);
|
|
1386
|
+
seen.add(panel.id);
|
|
1387
|
+
if (panel.title.trim() === "")
|
|
1388
|
+
throw new RangeError(`admin panel ${panel.id} needs a title`);
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
// ../core/dist/admin-ui.js
|
|
1393
|
+
var escapeHtml = (value) => value.replace(/[&<>"']/g, (char) => {
|
|
1394
|
+
switch (char) {
|
|
1395
|
+
case "&":
|
|
1396
|
+
return "&";
|
|
1397
|
+
case "<":
|
|
1398
|
+
return "<";
|
|
1399
|
+
case ">":
|
|
1400
|
+
return ">";
|
|
1401
|
+
case '"':
|
|
1402
|
+
return """;
|
|
1403
|
+
default:
|
|
1404
|
+
return "'";
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
var renderAdminDocument = (service) => {
|
|
1408
|
+
const name = escapeHtml(service);
|
|
1409
|
+
const standard = JSON.stringify(STANDARD_ADMIN_ROUTES);
|
|
1410
|
+
return `<!DOCTYPE html>
|
|
1411
|
+
<html lang="en">
|
|
1412
|
+
<head>
|
|
1413
|
+
<meta charset="utf-8">
|
|
1414
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
1415
|
+
<meta name="color-scheme" content="light dark">
|
|
1416
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Crect width='16' height='16' rx='3' fill='%233d4f3a'/%3E%3C/svg%3E">
|
|
1417
|
+
<title>${name} admin</title>
|
|
1418
|
+
<style>
|
|
1419
|
+
:root {
|
|
1420
|
+
color-scheme: light;
|
|
1421
|
+
--bg: #f6f5f2;
|
|
1422
|
+
--bg-elev: #ffffff;
|
|
1423
|
+
--bg-muted: #eeece6;
|
|
1424
|
+
--bg-inset: #f3f1eb;
|
|
1425
|
+
--line: #e2dfd6;
|
|
1426
|
+
--line-strong: #d0ccc1;
|
|
1427
|
+
--ink: #1c1915;
|
|
1428
|
+
--muted: #5e584f;
|
|
1429
|
+
--faint: #8a8378;
|
|
1430
|
+
--accent: #3d4f3a;
|
|
1431
|
+
--accent-ink: #f7f6f2;
|
|
1432
|
+
--accent-soft: #e5ece2;
|
|
1433
|
+
--ok: #1f7a45;
|
|
1434
|
+
--ok-soft: #e5f4eb;
|
|
1435
|
+
--warn: #9a6700;
|
|
1436
|
+
--warn-soft: #fbf3df;
|
|
1437
|
+
--err: #a33b32;
|
|
1438
|
+
--err-soft: #fdecea;
|
|
1439
|
+
--shadow: 0 1px 2px rgb(28 25 21 / 0.05), 0 12px 32px rgb(28 25 21 / 0.06);
|
|
1440
|
+
--sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
1441
|
+
--mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
|
|
1442
|
+
--radius: 14px;
|
|
1443
|
+
--header: 60px;
|
|
1444
|
+
--nav: 220px;
|
|
1445
|
+
}
|
|
1446
|
+
:root[data-theme="dark"] {
|
|
1447
|
+
color-scheme: dark;
|
|
1448
|
+
--bg: #141311;
|
|
1449
|
+
--bg-elev: #1e1c19;
|
|
1450
|
+
--bg-muted: #26231f;
|
|
1451
|
+
--bg-inset: #181715;
|
|
1452
|
+
--line: #34302b;
|
|
1453
|
+
--line-strong: #4a453d;
|
|
1454
|
+
--ink: #f4f1ea;
|
|
1455
|
+
--muted: #c4bdb1;
|
|
1456
|
+
--faint: #8f877c;
|
|
1457
|
+
--accent: #c5d5b8;
|
|
1458
|
+
--accent-ink: #1a2118;
|
|
1459
|
+
--accent-soft: #2a3328;
|
|
1460
|
+
--ok: #8fd6a8;
|
|
1461
|
+
--ok-soft: #1a2e22;
|
|
1462
|
+
--warn: #f0cc78;
|
|
1463
|
+
--warn-soft: #332911;
|
|
1464
|
+
--err: #f0a8a2;
|
|
1465
|
+
--err-soft: #3a201e;
|
|
1466
|
+
--shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 16px 40px rgb(0 0 0 / 0.28);
|
|
1467
|
+
}
|
|
1468
|
+
@media (prefers-color-scheme: dark) {
|
|
1469
|
+
:root:not([data-theme="light"]) {
|
|
1470
|
+
color-scheme: dark;
|
|
1471
|
+
--bg: #141311;
|
|
1472
|
+
--bg-elev: #1e1c19;
|
|
1473
|
+
--bg-muted: #26231f;
|
|
1474
|
+
--bg-inset: #181715;
|
|
1475
|
+
--line: #34302b;
|
|
1476
|
+
--line-strong: #4a453d;
|
|
1477
|
+
--ink: #f4f1ea;
|
|
1478
|
+
--muted: #c4bdb1;
|
|
1479
|
+
--faint: #8f877c;
|
|
1480
|
+
--accent: #c5d5b8;
|
|
1481
|
+
--accent-ink: #1a2118;
|
|
1482
|
+
--accent-soft: #2a3328;
|
|
1483
|
+
--ok: #8fd6a8;
|
|
1484
|
+
--ok-soft: #1a2e22;
|
|
1485
|
+
--warn: #f0cc78;
|
|
1486
|
+
--warn-soft: #332911;
|
|
1487
|
+
--err: #f0a8a2;
|
|
1488
|
+
--err-soft: #3a201e;
|
|
1489
|
+
--shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 16px 40px rgb(0 0 0 / 0.28);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
* { box-sizing: border-box; }
|
|
1493
|
+
html, body { margin: 0; height: 100%; }
|
|
1494
|
+
body {
|
|
1495
|
+
font-family: var(--sans);
|
|
1496
|
+
background: var(--bg);
|
|
1497
|
+
color: var(--ink);
|
|
1498
|
+
line-height: 1.45;
|
|
1499
|
+
padding-left: env(safe-area-inset-left);
|
|
1500
|
+
padding-right: env(safe-area-inset-right);
|
|
1501
|
+
-webkit-text-size-adjust: 100%;
|
|
1502
|
+
}
|
|
1503
|
+
button, input, select, textarea { font: inherit; color: inherit; }
|
|
1504
|
+
button { cursor: pointer; }
|
|
1505
|
+
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
|
1506
|
+
@media (prefers-reduced-motion: reduce) {
|
|
1507
|
+
* { scroll-behavior: auto !important; transition: none !important; }
|
|
1508
|
+
}
|
|
1509
|
+
.app { min-height: 100%; display: flex; flex-direction: column; }
|
|
1510
|
+
header.top {
|
|
1511
|
+
position: sticky; top: 0; z-index: 5;
|
|
1512
|
+
display: flex; align-items: center; gap: 12px;
|
|
1513
|
+
min-height: var(--header); padding: 10px 16px;
|
|
1514
|
+
background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
|
|
1515
|
+
border-bottom: 1px solid var(--line);
|
|
1516
|
+
backdrop-filter: blur(10px);
|
|
1517
|
+
}
|
|
1518
|
+
.brand { display: flex; flex-direction: column; min-width: 0; margin-right: auto; }
|
|
1519
|
+
.brand strong { font-size: 15px; letter-spacing: -0.01em; }
|
|
1520
|
+
.brand span { color: var(--faint); font-size: 12px; }
|
|
1521
|
+
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
|
1522
|
+
label.field { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--faint); }
|
|
1523
|
+
label.field input, label.field select, .controls input[type="password"] {
|
|
1524
|
+
background: var(--bg-inset); border: 1px solid var(--line); border-radius: 8px;
|
|
1525
|
+
padding: 7px 8px; min-height: 36px;
|
|
1526
|
+
}
|
|
1527
|
+
.icon-btn, .btn, .btn-primary, .btn-quiet, .btn-danger {
|
|
1528
|
+
border-radius: 9px; min-height: 36px; padding: 0 12px; border: 1px solid var(--line);
|
|
1529
|
+
background: var(--bg-elev);
|
|
1530
|
+
}
|
|
1531
|
+
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
|
|
1532
|
+
.btn-quiet { background: transparent; }
|
|
1533
|
+
.btn-danger { color: var(--err); }
|
|
1534
|
+
/* minmax(0, 1fr): a 1fr column will not shrink below its content, so a long
|
|
1535
|
+
record or the full nav row would widen the page past the viewport. */
|
|
1536
|
+
.layout {
|
|
1537
|
+
display: grid; grid-template-columns: var(--nav) minmax(0, 1fr);
|
|
1538
|
+
flex: 1; min-height: 0; min-width: 0;
|
|
1539
|
+
}
|
|
1540
|
+
nav.side {
|
|
1541
|
+
border-right: 1px solid var(--line); padding: 12px;
|
|
1542
|
+
display: flex; flex-direction: column; gap: 4px;
|
|
1543
|
+
background: var(--bg-elev);
|
|
1544
|
+
}
|
|
1545
|
+
nav.side button {
|
|
1546
|
+
text-align: left; border: 0; background: transparent; border-radius: 9px;
|
|
1547
|
+
padding: 9px 10px; min-height: 40px; color: var(--muted);
|
|
1548
|
+
}
|
|
1549
|
+
nav.side button[aria-current="true"] { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
|
|
1550
|
+
main { padding: 20px; max-width: 1100px; width: 100%; min-width: 0; }
|
|
1551
|
+
.view h2 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.02em; }
|
|
1552
|
+
.lede { margin: 0 0 16px; color: var(--muted); }
|
|
1553
|
+
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
|
|
1554
|
+
.card {
|
|
1555
|
+
background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
|
|
1556
|
+
padding: 14px 16px; box-shadow: var(--shadow);
|
|
1557
|
+
}
|
|
1558
|
+
.card .k { color: var(--faint); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
|
|
1559
|
+
.card .v { display: flex; flex-direction: column; gap: 2px; font-size: 22px; margin-top: 4px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
|
|
1560
|
+
.card .v .clock-time { font-size: 14px; color: var(--muted); }
|
|
1561
|
+
.split { display: grid; grid-template-columns: minmax(0, 240px) minmax(0, 1fr); gap: 16px; align-items: start; }
|
|
1562
|
+
.collection {
|
|
1563
|
+
width: 100%; text-align: left; border: 1px solid transparent; background: transparent;
|
|
1564
|
+
border-radius: 10px; padding: 8px 10px; min-height: 44px;
|
|
1565
|
+
}
|
|
1566
|
+
.collection[aria-current="true"] { background: var(--bg-elev); border-color: var(--line); }
|
|
1567
|
+
.collection small { display: block; color: var(--faint); }
|
|
1568
|
+
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 14px; }
|
|
1569
|
+
.chip {
|
|
1570
|
+
font-family: var(--mono); font-size: 12px; background: var(--bg-muted);
|
|
1571
|
+
border-radius: 999px; padding: 4px 8px;
|
|
1572
|
+
}
|
|
1573
|
+
.chip i { color: var(--faint); font-style: normal; }
|
|
1574
|
+
table { width: 100%; border-collapse: collapse; font-size: 14px; }
|
|
1575
|
+
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
|
|
1576
|
+
th { color: var(--faint); font-weight: 600; font-size: 12px; }
|
|
1577
|
+
tr.clickable { cursor: pointer; }
|
|
1578
|
+
tr.clickable:hover { background: var(--bg-muted); }
|
|
1579
|
+
pre, code, .mono { font-family: var(--mono); overflow-wrap: anywhere; }
|
|
1580
|
+
pre {
|
|
1581
|
+
margin: 0; white-space: pre-wrap; word-break: break-word;
|
|
1582
|
+
background: var(--bg-inset); border-radius: 10px; padding: 12px; font-size: 12px;
|
|
1583
|
+
}
|
|
1584
|
+
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
|
|
1585
|
+
.banner {
|
|
1586
|
+
display: none; margin-bottom: 12px; padding: 10px 12px; border-radius: 10px;
|
|
1587
|
+
background: var(--err-soft); color: var(--err);
|
|
1588
|
+
}
|
|
1589
|
+
.banner.show { display: block; }
|
|
1590
|
+
.empty { color: var(--muted); padding: 24px 8px; }
|
|
1591
|
+
dialog {
|
|
1592
|
+
border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--bg-elev);
|
|
1593
|
+
color: var(--ink); width: min(640px, calc(100vw - 24px)); box-shadow: var(--shadow);
|
|
1594
|
+
}
|
|
1595
|
+
dialog::backdrop { background: rgb(20 19 17 / 0.45); }
|
|
1596
|
+
dialog form, .dialog-body { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
|
|
1597
|
+
textarea {
|
|
1598
|
+
width: 100%; min-height: 180px; resize: vertical; border-radius: 10px;
|
|
1599
|
+
border: 1px solid var(--line); background: var(--bg-inset); padding: 10px;
|
|
1600
|
+
font-family: var(--mono); font-size: 12px;
|
|
1601
|
+
}
|
|
1602
|
+
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
|
|
1603
|
+
.badge { font-size: 11px; border-radius: 999px; padding: 2px 7px; background: var(--bg-muted); color: var(--muted); }
|
|
1604
|
+
.badge.ok { background: var(--ok-soft); color: var(--ok); }
|
|
1605
|
+
.badge.warn { background: var(--warn-soft); color: var(--warn); }
|
|
1606
|
+
.status-2 { color: var(--ok); }
|
|
1607
|
+
.status-4, .status-5, .status-0 { color: var(--err); }
|
|
1608
|
+
.stack { display: flex; flex-direction: column; gap: 12px; }
|
|
1609
|
+
.route {
|
|
1610
|
+
display: grid; grid-template-columns: 88px 1fr auto; gap: 8px; align-items: center;
|
|
1611
|
+
padding: 8px 0; border-bottom: 1px solid var(--line);
|
|
1612
|
+
}
|
|
1613
|
+
.method { font-family: var(--mono); font-size: 12px; font-weight: 700; }
|
|
1614
|
+
@media (max-width: 800px) {
|
|
1615
|
+
header.top {
|
|
1616
|
+
align-items: stretch; flex-direction: column;
|
|
1617
|
+
padding-top: max(10px, env(safe-area-inset-top));
|
|
1618
|
+
}
|
|
1619
|
+
input, select, textarea { font-size: 16px; }
|
|
1620
|
+
.controls { width: 100%; }
|
|
1621
|
+
label.field, label.field select, label.field input { flex: 1; min-width: 0; }
|
|
1622
|
+
.layout { grid-template-columns: minmax(0, 1fr); }
|
|
1623
|
+
nav.side {
|
|
1624
|
+
flex-direction: row; align-items: center; overflow-x: auto; min-width: 0;
|
|
1625
|
+
border-right: 0; border-bottom: 1px solid var(--line);
|
|
1626
|
+
padding: 8px;
|
|
1627
|
+
}
|
|
1628
|
+
nav.side button { white-space: nowrap; flex: none; }
|
|
1629
|
+
.split { grid-template-columns: minmax(0, 1fr); }
|
|
1630
|
+
main { padding: 14px; }
|
|
1631
|
+
table, thead, tbody, tr, th, td { display: block; }
|
|
1632
|
+
thead { display: none; }
|
|
1633
|
+
tr {
|
|
1634
|
+
border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; padding: 6px;
|
|
1635
|
+
background: var(--bg-elev); min-width: 0;
|
|
1636
|
+
}
|
|
1637
|
+
td {
|
|
1638
|
+
border: 0; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px;
|
|
1639
|
+
min-width: 0; overflow-wrap: anywhere;
|
|
1640
|
+
}
|
|
1641
|
+
td::before { content: attr(data-label); color: var(--faint); font-size: 12px; flex: none; }
|
|
1642
|
+
.route { grid-template-columns: 72px 1fr; }
|
|
1643
|
+
.route button { grid-column: 1 / -1; }
|
|
1644
|
+
}
|
|
1645
|
+
</style>
|
|
1646
|
+
</head>
|
|
1647
|
+
<body>
|
|
1648
|
+
<div class="app" data-service="${name}" data-mockingbird-admin>
|
|
1649
|
+
<header class="top">
|
|
1650
|
+
<div class="brand">
|
|
1651
|
+
<strong>${name}</strong>
|
|
1652
|
+
<span>Mockingbird admin</span>
|
|
1653
|
+
</div>
|
|
1654
|
+
<div class="controls">
|
|
1655
|
+
<label class="field">Namespace
|
|
1656
|
+
<select id="namespace" aria-label="Namespace"></select>
|
|
1657
|
+
</label>
|
|
1658
|
+
<label class="field">Admin key
|
|
1659
|
+
<input id="admin-key" type="password" autocomplete="off" spellcheck="false" aria-label="Admin key">
|
|
1660
|
+
</label>
|
|
1661
|
+
<button class="icon-btn" id="theme" type="button" aria-label="Toggle color theme">Theme</button>
|
|
1662
|
+
<button class="btn" id="refresh" type="button">Refresh</button>
|
|
1663
|
+
</div>
|
|
1664
|
+
</header>
|
|
1665
|
+
<div class="layout">
|
|
1666
|
+
<nav class="side" id="nav" aria-label="Admin sections">
|
|
1667
|
+
<button type="button" data-view="overview" aria-current="true">Overview</button>
|
|
1668
|
+
<button type="button" data-view="state">State</button>
|
|
1669
|
+
<button type="button" data-view="clock">Clock</button>
|
|
1670
|
+
<button type="button" data-view="faults">Faults</button>
|
|
1671
|
+
<button type="button" data-view="journal">Journal</button>
|
|
1672
|
+
<button type="button" data-view="routes">Routes</button>
|
|
1673
|
+
</nav>
|
|
1674
|
+
<main id="views">
|
|
1675
|
+
<p class="banner" id="banner" role="alert"></p>
|
|
1676
|
+
<section class="view" id="view-overview">
|
|
1677
|
+
<h2>Overview</h2>
|
|
1678
|
+
<p class="lede">Health, clock, and the collections this namespace is holding.</p>
|
|
1679
|
+
<div class="grid" id="overview-cards"></div>
|
|
1680
|
+
<h3>Recent requests</h3>
|
|
1681
|
+
<div id="overview-requests"></div>
|
|
1682
|
+
</section>
|
|
1683
|
+
<section class="view" id="view-state" hidden>
|
|
1684
|
+
<h2>State</h2>
|
|
1685
|
+
<p class="lede">Every collection in this namespace. Declared fields stay visible before any row exists; stored rows add whatever else they contain.</p>
|
|
1686
|
+
<div class="split">
|
|
1687
|
+
<div id="collections"></div>
|
|
1688
|
+
<div>
|
|
1689
|
+
<div id="schema"></div>
|
|
1690
|
+
<div class="row-actions">
|
|
1691
|
+
<button class="btn-primary" id="new-record" type="button">New record</button>
|
|
1692
|
+
</div>
|
|
1693
|
+
<div id="records"></div>
|
|
1694
|
+
</div>
|
|
1695
|
+
</div>
|
|
1696
|
+
</section>
|
|
1697
|
+
<section class="view" id="view-clock" hidden>
|
|
1698
|
+
<h2>Clock</h2>
|
|
1699
|
+
<p class="lede">The clock every timestamp in this mock reads.</p>
|
|
1700
|
+
<div class="card" id="clock-readout"></div>
|
|
1701
|
+
<div class="row-actions">
|
|
1702
|
+
<button class="btn" type="button" data-advance="60000">+1m</button>
|
|
1703
|
+
<button class="btn" type="button" data-advance="900000">+15m</button>
|
|
1704
|
+
<button class="btn" type="button" data-advance="3600000">+1h</button>
|
|
1705
|
+
<button class="btn" type="button" data-advance="86400000">+1d</button>
|
|
1706
|
+
<button class="btn" type="button" data-advance="-3600000">\u22121h</button>
|
|
1707
|
+
<button class="btn" id="freeze" type="button">Freeze</button>
|
|
1708
|
+
<button class="btn-quiet" id="clock-reset" type="button">Reset</button>
|
|
1709
|
+
</div>
|
|
1710
|
+
<form id="clock-set" class="row-actions">
|
|
1711
|
+
<label class="field">Set instant
|
|
1712
|
+
<input id="clock-instant" type="text" placeholder="2026-01-01T00:00:00Z or epoch ms" size="32">
|
|
1713
|
+
</label>
|
|
1714
|
+
<button class="btn-primary" type="submit">Set</button>
|
|
1715
|
+
</form>
|
|
1716
|
+
</section>
|
|
1717
|
+
<section class="view" id="view-faults" hidden>
|
|
1718
|
+
<h2>Faults</h2>
|
|
1719
|
+
<p class="lede">Inject a response, a delay, or a named preset. Rules apply to this namespace.</p>
|
|
1720
|
+
<form id="fault-form" class="card stack">
|
|
1721
|
+
<label class="field">Preset
|
|
1722
|
+
<select id="fault-preset"><option value="">Custom rule</option></select>
|
|
1723
|
+
</label>
|
|
1724
|
+
<label class="field">Status
|
|
1725
|
+
<input id="fault-status" inputmode="numeric" placeholder="503">
|
|
1726
|
+
</label>
|
|
1727
|
+
<label class="field">Path prefix
|
|
1728
|
+
<input id="fault-path" placeholder="/v1">
|
|
1729
|
+
</label>
|
|
1730
|
+
<label class="field">Method
|
|
1731
|
+
<input id="fault-method" placeholder="GET">
|
|
1732
|
+
</label>
|
|
1733
|
+
<div class="dialog-actions">
|
|
1734
|
+
<button class="btn-primary" type="submit">Add fault</button>
|
|
1735
|
+
<button class="btn-danger" id="fault-clear" type="button">Clear all</button>
|
|
1736
|
+
</div>
|
|
1737
|
+
</form>
|
|
1738
|
+
<div id="fault-list"></div>
|
|
1739
|
+
</section>
|
|
1740
|
+
<section class="view" id="view-journal" hidden>
|
|
1741
|
+
<h2>Journal</h2>
|
|
1742
|
+
<p class="lede">Requests this namespace has seen. Bodies are not stored.</p>
|
|
1743
|
+
<div class="row-actions">
|
|
1744
|
+
<button class="btn-danger" id="journal-clear" type="button">Clear journal</button>
|
|
1745
|
+
</div>
|
|
1746
|
+
<div id="journal"></div>
|
|
1747
|
+
</section>
|
|
1748
|
+
<section class="view" id="view-routes" hidden>
|
|
1749
|
+
<h2>Routes</h2>
|
|
1750
|
+
<p class="lede">Shared controls, plus any routes this mock adds. Extra routes are how a bespoke workflow shows up beside the default UI.</p>
|
|
1751
|
+
<div id="route-list"></div>
|
|
1752
|
+
<form id="route-form" class="card stack">
|
|
1753
|
+
<strong id="route-title">Select a route</strong>
|
|
1754
|
+
<label class="field">JSON body
|
|
1755
|
+
<textarea id="route-body" placeholder="{ }"></textarea>
|
|
1756
|
+
</label>
|
|
1757
|
+
<div class="dialog-actions">
|
|
1758
|
+
<button class="btn-primary" type="submit">Send</button>
|
|
1759
|
+
</div>
|
|
1760
|
+
<pre id="route-result">Response will show here.</pre>
|
|
1761
|
+
</form>
|
|
1762
|
+
</section>
|
|
1763
|
+
</main>
|
|
1764
|
+
</div>
|
|
1765
|
+
</div>
|
|
1766
|
+
<dialog id="editor">
|
|
1767
|
+
<form method="dialog" id="editor-form">
|
|
1768
|
+
<strong id="editor-title">Record</strong>
|
|
1769
|
+
<label class="field">Id
|
|
1770
|
+
<input id="editor-id" required>
|
|
1771
|
+
</label>
|
|
1772
|
+
<label class="field">JSON value
|
|
1773
|
+
<textarea id="editor-value"></textarea>
|
|
1774
|
+
</label>
|
|
1775
|
+
<div class="dialog-actions">
|
|
1776
|
+
<button class="btn-danger" id="editor-delete" type="button" value="delete">Delete</button>
|
|
1777
|
+
<button class="btn-quiet" type="submit" value="cancel">Cancel</button>
|
|
1778
|
+
<button class="btn-primary" type="submit" value="save">Save</button>
|
|
1779
|
+
</div>
|
|
1780
|
+
</form>
|
|
1781
|
+
</dialog>
|
|
1782
|
+
<script>
|
|
1783
|
+
(() => {
|
|
1784
|
+
const STANDARD = new Set(${standard})
|
|
1785
|
+
const state = {
|
|
1786
|
+
namespace: "default",
|
|
1787
|
+
key: sessionStorage.getItem("mockingbird-admin-key") || "",
|
|
1788
|
+
view: "overview",
|
|
1789
|
+
collection: "",
|
|
1790
|
+
shape: null,
|
|
1791
|
+
route: null,
|
|
1792
|
+
editing: null
|
|
1793
|
+
}
|
|
1794
|
+
const $ = (id) => document.getElementById(id)
|
|
1795
|
+
const banner = $("banner")
|
|
1796
|
+
const keyInput = $("admin-key")
|
|
1797
|
+
keyInput.value = state.key
|
|
1798
|
+
const params = new URLSearchParams(location.search)
|
|
1799
|
+
if (params.get("key")) {
|
|
1800
|
+
state.key = params.get("key")
|
|
1801
|
+
keyInput.value = state.key
|
|
1802
|
+
sessionStorage.setItem("mockingbird-admin-key", state.key)
|
|
1803
|
+
params.delete("key")
|
|
1804
|
+
const next = params.toString()
|
|
1805
|
+
history.replaceState(null, "", location.pathname + (next ? "?" + next : ""))
|
|
1806
|
+
}
|
|
1807
|
+
if (params.get("namespace")) state.namespace = params.get("namespace")
|
|
1808
|
+
|
|
1809
|
+
const showError = (error) => {
|
|
1810
|
+
banner.textContent = error instanceof Error ? error.message : String(error)
|
|
1811
|
+
banner.classList.add("show")
|
|
1812
|
+
}
|
|
1813
|
+
const clearError = () => banner.classList.remove("show")
|
|
1814
|
+
|
|
1815
|
+
async function originGet(path) {
|
|
1816
|
+
const response = await fetch(path, { headers: { accept: "application/json" } })
|
|
1817
|
+
if (!response.ok) throw new Error(response.status + " " + path)
|
|
1818
|
+
return response.json()
|
|
1819
|
+
}
|
|
1820
|
+
async function api(method, path, body) {
|
|
1821
|
+
const headers = { accept: "application/json" }
|
|
1822
|
+
if (state.key) headers["${ADMIN_KEY_HEADER}"] = state.key
|
|
1823
|
+
if (state.namespace) headers["x-mockingbird-namespace"] = state.namespace
|
|
1824
|
+
if (body !== undefined) headers["content-type"] = "application/json"
|
|
1825
|
+
const response = await fetch("${ADMIN_PREFIX}" + path, {
|
|
1826
|
+
method,
|
|
1827
|
+
headers,
|
|
1828
|
+
body: body === undefined ? undefined : JSON.stringify(body)
|
|
1829
|
+
})
|
|
1830
|
+
const text = await response.text()
|
|
1831
|
+
let data = null
|
|
1832
|
+
if (text) {
|
|
1833
|
+
try { data = JSON.parse(text) } catch { data = { raw: text } }
|
|
1834
|
+
}
|
|
1835
|
+
if (response.status === 401) {
|
|
1836
|
+
keyInput.focus()
|
|
1837
|
+
throw new Error((data && data.error && data.error.message) || "Admin key required")
|
|
1838
|
+
}
|
|
1839
|
+
if (!response.ok) {
|
|
1840
|
+
throw new Error((data && data.error && data.error.message) || (response.status + " " + method + " " + path))
|
|
1841
|
+
}
|
|
1842
|
+
return data
|
|
1843
|
+
}
|
|
1844
|
+
const panelApi = {
|
|
1845
|
+
get: (path) => api("GET", path),
|
|
1846
|
+
send: (method, path, body) => api(method, path, body),
|
|
1847
|
+
namespace: () => state.namespace
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
function setTheme(theme) {
|
|
1851
|
+
if (theme) document.documentElement.dataset.theme = theme
|
|
1852
|
+
else delete document.documentElement.dataset.theme
|
|
1853
|
+
}
|
|
1854
|
+
const storedTheme = localStorage.getItem("mockingbird-admin-theme")
|
|
1855
|
+
if (storedTheme) setTheme(storedTheme)
|
|
1856
|
+
$("theme").addEventListener("click", () => {
|
|
1857
|
+
const current = document.documentElement.dataset.theme
|
|
1858
|
+
const dark = current ? current === "dark" : matchMedia("(prefers-color-scheme: dark)").matches
|
|
1859
|
+
const next = dark ? "light" : "dark"
|
|
1860
|
+
localStorage.setItem("mockingbird-admin-theme", next)
|
|
1861
|
+
setTheme(next)
|
|
1862
|
+
})
|
|
1863
|
+
|
|
1864
|
+
function show(view) {
|
|
1865
|
+
state.view = view
|
|
1866
|
+
for (const button of document.querySelectorAll("#nav button")) {
|
|
1867
|
+
button.setAttribute("aria-current", button.dataset.view === view ? "true" : "false")
|
|
1868
|
+
}
|
|
1869
|
+
for (const section of document.querySelectorAll("main .view")) {
|
|
1870
|
+
section.hidden = section.id !== "view-" + view
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
document.getElementById("nav").addEventListener("click", (event) => {
|
|
1874
|
+
const button = event.target.closest("button[data-view]")
|
|
1875
|
+
if (!button) return
|
|
1876
|
+
show(button.dataset.view)
|
|
1877
|
+
refresh().catch(showError)
|
|
1878
|
+
})
|
|
1879
|
+
|
|
1880
|
+
function cards(items) {
|
|
1881
|
+
return '<div class="grid">' + items.map((item) =>
|
|
1882
|
+
'<article class="card"><div class="k">' + item.k + '</div><div class="v">' + item.v + '</div></article>'
|
|
1883
|
+
).join("") + "</div>"
|
|
1884
|
+
}
|
|
1885
|
+
function table(columns, rows) {
|
|
1886
|
+
if (rows.length === 0) return '<p class="empty">Nothing here yet.</p>'
|
|
1887
|
+
const head = "<tr>" + columns.map((column) => "<th>" + column.label + "</th>").join("") + "</tr>"
|
|
1888
|
+
const body = rows.map((row) => "<tr" + (row.attrs || "") + ">" + columns.map((column) =>
|
|
1889
|
+
'<td data-label="' + column.label + '">' + column.cell(row) + "</td>"
|
|
1890
|
+
).join("") + "</tr>").join("")
|
|
1891
|
+
return "<table><thead>" + head + "</thead><tbody>" + body + "</tbody></table>"
|
|
1892
|
+
}
|
|
1893
|
+
const esc = (value) => String(value ?? "").replace(/[&<>"']/g, (char) => ({
|
|
1894
|
+
"&": "&", "<": "<", ">": ">", '"': """, "'": "'"
|
|
1895
|
+
}[char]))
|
|
1896
|
+
const preview = (value) => {
|
|
1897
|
+
const text = JSON.stringify(value)
|
|
1898
|
+
return esc(text.length > 180 ? text.slice(0, 180) + "\u2026" : text)
|
|
1899
|
+
}
|
|
1900
|
+
const when = (epoch) => {
|
|
1901
|
+
const date = new Date(epoch)
|
|
1902
|
+
return Number.isNaN(date.getTime()) ? String(epoch) : date.toISOString()
|
|
1903
|
+
}
|
|
1904
|
+
const clockCard = (epoch) => {
|
|
1905
|
+
const date = new Date(epoch)
|
|
1906
|
+
if (Number.isNaN(date.getTime())) return esc(String(epoch))
|
|
1907
|
+
const iso = date.toISOString()
|
|
1908
|
+
return esc(iso.slice(0, 10)) + '<span class="clock-time">' + esc(iso.slice(11, 19)) + " UTC</span>"
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
async function loadNamespaces() {
|
|
1912
|
+
const data = await api("GET", "/namespaces")
|
|
1913
|
+
const select = $("namespace")
|
|
1914
|
+
const names = data.namespaces && data.namespaces.length ? data.namespaces : [data.default || "default"]
|
|
1915
|
+
if (!names.includes(state.namespace)) names.unshift(state.namespace)
|
|
1916
|
+
select.innerHTML = names.map((name) =>
|
|
1917
|
+
'<option value="' + esc(name) + '">' + esc(name) + "</option>"
|
|
1918
|
+
).join("")
|
|
1919
|
+
select.value = state.namespace
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
async function renderOverview() {
|
|
1923
|
+
const [health, shape, clock, journal] = await Promise.all([
|
|
1924
|
+
originGet("/health"),
|
|
1925
|
+
api("GET", "/state"),
|
|
1926
|
+
api("GET", "/clock"),
|
|
1927
|
+
api("GET", "/requests?limit=8")
|
|
1928
|
+
])
|
|
1929
|
+
state.shape = shape
|
|
1930
|
+
$("overview-cards").innerHTML = cards([
|
|
1931
|
+
{ k: "Service", v: esc(health.service || "${name}") },
|
|
1932
|
+
{ k: "Status", v: esc(health.status || "ok") },
|
|
1933
|
+
{ k: "Namespace", v: esc(state.namespace) },
|
|
1934
|
+
{ k: "Collections", v: String(shape.collections.length) },
|
|
1935
|
+
{ k: "Records", v: String(shape.collections.reduce((sum, item) => sum + item.count, 0)) },
|
|
1936
|
+
{ k: "Clock", v: clockCard(clock.now) }
|
|
1937
|
+
])
|
|
1938
|
+
const requests = (journal && journal.requests) || []
|
|
1939
|
+
$("overview-requests").innerHTML = table(
|
|
1940
|
+
[
|
|
1941
|
+
{ label: "Method", cell: (row) => esc(row.method) },
|
|
1942
|
+
{ label: "Path", cell: (row) => esc(row.path) },
|
|
1943
|
+
{ label: "Status", cell: (row) => '<span class="status-' + String(row.status).charAt(0) + '">' + esc(row.status) + "</span>" },
|
|
1944
|
+
{ label: "Operation", cell: (row) => esc(row.operationId || "") }
|
|
1945
|
+
],
|
|
1946
|
+
requests
|
|
1947
|
+
)
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
function schemaChips(collection) {
|
|
1951
|
+
if (!collection || collection.fields.length === 0) {
|
|
1952
|
+
return '<p class="empty">No fields yet. Add a record or declare the collection on the service.</p>'
|
|
1953
|
+
}
|
|
1954
|
+
return '<div class="chips">' + collection.fields.map((field) =>
|
|
1955
|
+
'<span class="chip">' + esc(field.name) + ' <i>' + esc(field.kind) + (field.optional ? "?" : "") + "</i></span>"
|
|
1956
|
+
).join("") + "</div>"
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
async function renderState() {
|
|
1960
|
+
const shape = await api("GET", "/state")
|
|
1961
|
+
state.shape = shape
|
|
1962
|
+
const host = $("collections")
|
|
1963
|
+
if (shape.collections.length === 0) {
|
|
1964
|
+
host.innerHTML = '<p class="empty">This namespace has no collections yet.</p>'
|
|
1965
|
+
$("schema").innerHTML = ""
|
|
1966
|
+
$("records").innerHTML = ""
|
|
1967
|
+
return
|
|
1968
|
+
}
|
|
1969
|
+
if (!shape.collections.some((item) => item.name === state.collection)) {
|
|
1970
|
+
state.collection = shape.collections[0].name
|
|
1971
|
+
}
|
|
1972
|
+
host.innerHTML = shape.collections.map((item) =>
|
|
1973
|
+
'<button type="button" class="collection" data-collection="' + esc(item.name) + '" aria-current="' +
|
|
1974
|
+
(item.name === state.collection ? "true" : "false") + '"><strong>' + esc(item.label) +
|
|
1975
|
+
'</strong><small>' + esc(item.count) + " \xB7 " + esc(item.source) + "</small></button>"
|
|
1976
|
+
).join("")
|
|
1977
|
+
const selected = shape.collections.find((item) => item.name === state.collection)
|
|
1978
|
+
$("schema").innerHTML = "<h3>" + esc(selected.label) + "</h3>" +
|
|
1979
|
+
(selected.description ? "<p class=" + '"lede">' + esc(selected.description) + "</p>" : "") +
|
|
1980
|
+
schemaChips(selected)
|
|
1981
|
+
const page = await api("GET", "/state/" + encodeURIComponent(state.collection) + "?limit=50")
|
|
1982
|
+
const rows = page.records.map((record) => ({
|
|
1983
|
+
...record,
|
|
1984
|
+
attrs: ' class="clickable" data-id="' + esc(record.id) + '"'
|
|
1985
|
+
}))
|
|
1986
|
+
$("records").innerHTML = table(
|
|
1987
|
+
[
|
|
1988
|
+
{ label: "Id", cell: (row) => "<code>" + esc(row.id) + "</code>" },
|
|
1989
|
+
{ label: "Seq", cell: (row) => esc(row.seq) },
|
|
1990
|
+
{ label: "Value", cell: (row) => preview(row.value) }
|
|
1991
|
+
],
|
|
1992
|
+
rows
|
|
1993
|
+
)
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
async function renderClock() {
|
|
1997
|
+
const clock = await api("GET", "/clock")
|
|
1998
|
+
$("clock-readout").innerHTML = "<div class='k'>Now</div><div class='v'>" + esc(when(clock.now)) +
|
|
1999
|
+
"</div><p>" + esc(clock.now) + " ms \xB7 offset " + esc(clock.offsetMs) + " ms " +
|
|
2000
|
+
(clock.frozen ? "<span class='badge warn'>frozen</span>" : "<span class='badge ok'>live</span>") + "</p>"
|
|
2001
|
+
$("freeze").textContent = clock.frozen ? "Unfreeze" : "Freeze"
|
|
2002
|
+
$("freeze").dataset.frozen = clock.frozen ? "1" : "0"
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
async function renderFaults() {
|
|
2006
|
+
const [faults, presets] = await Promise.all([
|
|
2007
|
+
api("GET", "/faults"),
|
|
2008
|
+
api("GET", "/faults/presets").catch(() => ({ presets: [] }))
|
|
2009
|
+
])
|
|
2010
|
+
const select = $("fault-preset")
|
|
2011
|
+
const current = select.value
|
|
2012
|
+
const list = presets.presets || []
|
|
2013
|
+
select.innerHTML = '<option value="">Custom rule</option>' + list.map((preset) =>
|
|
2014
|
+
'<option value="' + esc(preset.name) + '">' + esc(preset.name) + "</option>"
|
|
2015
|
+
).join("")
|
|
2016
|
+
select.value = current
|
|
2017
|
+
const rules = faults.faults || []
|
|
2018
|
+
$("fault-list").innerHTML = table(
|
|
2019
|
+
[
|
|
2020
|
+
{ label: "Id", cell: (row) => esc(row.id) },
|
|
2021
|
+
{ label: "Match", cell: (row) => esc([row.method, row.pathPrefix, row.operationId].filter(Boolean).join(" ") || "all") },
|
|
2022
|
+
{ label: "Effect", cell: (row) => esc(row.status ?? row.effect ?? (row.drop ? "drop" : row.latencyMs || "")) }
|
|
2023
|
+
],
|
|
2024
|
+
rules
|
|
2025
|
+
)
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
async function renderJournal() {
|
|
2029
|
+
const journal = await api("GET", "/requests?limit=100")
|
|
2030
|
+
$("journal").innerHTML = table(
|
|
2031
|
+
[
|
|
2032
|
+
{ label: "When", cell: (row) => esc(row.at || "") },
|
|
2033
|
+
{ label: "Method", cell: (row) => esc(row.method) },
|
|
2034
|
+
{ label: "Path", cell: (row) => esc(row.path) },
|
|
2035
|
+
{ label: "Status", cell: (row) => esc(row.status) },
|
|
2036
|
+
{ label: "ms", cell: (row) => esc(row.durationMs) }
|
|
2037
|
+
],
|
|
2038
|
+
journal.requests || []
|
|
2039
|
+
)
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
async function renderRoutes() {
|
|
2043
|
+
const data = await api("GET", "/")
|
|
2044
|
+
const routes = data.routes || []
|
|
2045
|
+
$("route-list").innerHTML = routes.map((route) => {
|
|
2046
|
+
const extra = STANDARD.has(route) ? "" : " <span class='badge'>extension</span>"
|
|
2047
|
+
return '<div class="route"><span class="method">' + esc(route.split(" ")[0]) +
|
|
2048
|
+
'</span><span>' + esc(route.split(" ").slice(1).join(" ")) + extra +
|
|
2049
|
+
'</span><button class="btn" type="button" data-route="' + esc(route) + '">Use</button></div>'
|
|
2050
|
+
}).join("")
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
const renderers = {
|
|
2054
|
+
overview: renderOverview,
|
|
2055
|
+
state: renderState,
|
|
2056
|
+
clock: renderClock,
|
|
2057
|
+
faults: renderFaults,
|
|
2058
|
+
journal: renderJournal,
|
|
2059
|
+
routes: renderRoutes
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
async function refresh() {
|
|
2063
|
+
clearError()
|
|
2064
|
+
if (state.view in renderers) await renderers[state.view]()
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
$("namespace").addEventListener("change", () => {
|
|
2068
|
+
state.namespace = $("namespace").value
|
|
2069
|
+
refresh().catch(showError)
|
|
2070
|
+
})
|
|
2071
|
+
keyInput.addEventListener("change", () => {
|
|
2072
|
+
state.key = keyInput.value.trim()
|
|
2073
|
+
sessionStorage.setItem("mockingbird-admin-key", state.key)
|
|
2074
|
+
refresh().catch(showError)
|
|
2075
|
+
})
|
|
2076
|
+
$("refresh").addEventListener("click", () => refresh().catch(showError))
|
|
2077
|
+
|
|
2078
|
+
$("collections").addEventListener("click", (event) => {
|
|
2079
|
+
const button = event.target.closest("[data-collection]")
|
|
2080
|
+
if (!button) return
|
|
2081
|
+
state.collection = button.dataset.collection
|
|
2082
|
+
renderState().catch(showError)
|
|
2083
|
+
})
|
|
2084
|
+
$("records").addEventListener("click", (event) => {
|
|
2085
|
+
const row = event.target.closest("[data-id]")
|
|
2086
|
+
if (!row) return
|
|
2087
|
+
openEditor(row.dataset.id).catch(showError)
|
|
2088
|
+
})
|
|
2089
|
+
|
|
2090
|
+
const editor = $("editor")
|
|
2091
|
+
async function openEditor(id) {
|
|
2092
|
+
state.editing = id || null
|
|
2093
|
+
$("editor-title").textContent = id ? "Edit record" : "New record"
|
|
2094
|
+
$("editor-id").value = id || ""
|
|
2095
|
+
$("editor-id").readOnly = Boolean(id)
|
|
2096
|
+
$("editor-delete").hidden = !id
|
|
2097
|
+
if (id) {
|
|
2098
|
+
const record = await api("GET", "/state/" + encodeURIComponent(state.collection) + "/" + encodeURIComponent(id))
|
|
2099
|
+
$("editor-value").value = JSON.stringify(record.value, null, 2)
|
|
2100
|
+
} else {
|
|
2101
|
+
$("editor-value").value = "{\\n \\n}"
|
|
2102
|
+
}
|
|
2103
|
+
editor.showModal()
|
|
2104
|
+
}
|
|
2105
|
+
$("new-record").addEventListener("click", () => openEditor(null).catch(showError))
|
|
2106
|
+
$("editor-form").addEventListener("submit", (event) => {
|
|
2107
|
+
const submitter = event.submitter
|
|
2108
|
+
if (!submitter || submitter.value !== "save") return
|
|
2109
|
+
event.preventDefault()
|
|
2110
|
+
let value
|
|
2111
|
+
try { value = JSON.parse($("editor-value").value) }
|
|
2112
|
+
catch { showError(new Error("Value is not JSON")); return }
|
|
2113
|
+
const id = $("editor-id").value.trim()
|
|
2114
|
+
const path = "/state/" + encodeURIComponent(state.collection)
|
|
2115
|
+
const request = state.editing
|
|
2116
|
+
? api("PUT", path + "/" + encodeURIComponent(id), { value })
|
|
2117
|
+
: api("POST", path, { id: id || undefined, value })
|
|
2118
|
+
request.then(() => { editor.close(); return renderState() }).catch(showError)
|
|
2119
|
+
})
|
|
2120
|
+
$("editor-delete").addEventListener("click", () => {
|
|
2121
|
+
const id = state.editing
|
|
2122
|
+
if (!id) return
|
|
2123
|
+
api("DELETE", "/state/" + encodeURIComponent(state.collection) + "/" + encodeURIComponent(id))
|
|
2124
|
+
.then(() => { editor.close(); return renderState() })
|
|
2125
|
+
.catch(showError)
|
|
2126
|
+
})
|
|
2127
|
+
|
|
2128
|
+
document.querySelectorAll("[data-advance]").forEach((button) => {
|
|
2129
|
+
button.addEventListener("click", () => {
|
|
2130
|
+
api("POST", "/clock", { advance: Number(button.dataset.advance) }).then(renderClock).catch(showError)
|
|
2131
|
+
})
|
|
2132
|
+
})
|
|
2133
|
+
$("freeze").addEventListener("click", () => {
|
|
2134
|
+
const frozen = $("freeze").dataset.frozen === "1"
|
|
2135
|
+
api("POST", "/clock", { freeze: !frozen }).then(renderClock).catch(showError)
|
|
2136
|
+
})
|
|
2137
|
+
$("clock-reset").addEventListener("click", () => {
|
|
2138
|
+
api("POST", "/clock", { reset: true }).then(renderClock).catch(showError)
|
|
2139
|
+
})
|
|
2140
|
+
$("clock-set").addEventListener("submit", (event) => {
|
|
2141
|
+
event.preventDefault()
|
|
2142
|
+
const raw = $("clock-instant").value.trim()
|
|
2143
|
+
const set = /^-?\\d+$/.test(raw) ? Number(raw) : raw
|
|
2144
|
+
api("POST", "/clock", { set }).then(renderClock).catch(showError)
|
|
2145
|
+
})
|
|
2146
|
+
|
|
2147
|
+
$("fault-form").addEventListener("submit", (event) => {
|
|
2148
|
+
event.preventDefault()
|
|
2149
|
+
const preset = $("fault-preset").value
|
|
2150
|
+
const body = preset
|
|
2151
|
+
? { preset }
|
|
2152
|
+
: {
|
|
2153
|
+
status: Number($("fault-status").value),
|
|
2154
|
+
...($("fault-path").value ? { pathPrefix: $("fault-path").value } : {}),
|
|
2155
|
+
...($("fault-method").value ? { method: $("fault-method").value } : {})
|
|
2156
|
+
}
|
|
2157
|
+
if (!preset && !Number.isFinite(body.status)) {
|
|
2158
|
+
showError(new Error("Status must be a number, or choose a preset"))
|
|
2159
|
+
return
|
|
2160
|
+
}
|
|
2161
|
+
api("POST", "/faults", body).then(renderFaults).catch(showError)
|
|
2162
|
+
})
|
|
2163
|
+
$("fault-clear").addEventListener("click", () => api("DELETE", "/faults").then(renderFaults).catch(showError))
|
|
2164
|
+
$("journal-clear").addEventListener("click", () => api("DELETE", "/requests").then(renderJournal).catch(showError))
|
|
2165
|
+
|
|
2166
|
+
$("route-list").addEventListener("click", (event) => {
|
|
2167
|
+
const button = event.target.closest("[data-route]")
|
|
2168
|
+
if (!button) return
|
|
2169
|
+
state.route = button.dataset.route
|
|
2170
|
+
$("route-title").textContent = state.route
|
|
2171
|
+
const method = state.route.split(" ")[0]
|
|
2172
|
+
$("route-body").disabled = method === "GET" || method === "DELETE"
|
|
2173
|
+
})
|
|
2174
|
+
$("route-form").addEventListener("submit", async (event) => {
|
|
2175
|
+
event.preventDefault()
|
|
2176
|
+
if (!state.route) return showError(new Error("Choose a route first"))
|
|
2177
|
+
const [method, path] = [state.route.split(" ")[0], state.route.split(" ").slice(1).join(" ")]
|
|
2178
|
+
const concrete = path.replace(/:([A-Za-z0-9_]+)/g, (_, key) => prompt("Value for " + key) || "")
|
|
2179
|
+
let body
|
|
2180
|
+
if (method !== "GET" && method !== "DELETE" && $("route-body").value.trim()) {
|
|
2181
|
+
try { body = JSON.parse($("route-body").value) }
|
|
2182
|
+
catch { return showError(new Error("Body is not JSON")) }
|
|
2183
|
+
}
|
|
2184
|
+
try {
|
|
2185
|
+
const data = await api(method, concrete, body)
|
|
2186
|
+
$("route-result").textContent = JSON.stringify(data, null, 2)
|
|
2187
|
+
} catch (error) {
|
|
2188
|
+
showError(error)
|
|
2189
|
+
}
|
|
2190
|
+
})
|
|
2191
|
+
|
|
2192
|
+
function mountPanels(panels) {
|
|
2193
|
+
const nav = $("nav")
|
|
2194
|
+
const views = $("views")
|
|
2195
|
+
for (const panel of panels) {
|
|
2196
|
+
const button = document.createElement("button")
|
|
2197
|
+
button.type = "button"
|
|
2198
|
+
button.dataset.view = panel.id
|
|
2199
|
+
button.textContent = panel.title
|
|
2200
|
+
nav.append(button)
|
|
2201
|
+
const section = document.createElement("section")
|
|
2202
|
+
section.className = "view"
|
|
2203
|
+
section.id = "view-" + panel.id
|
|
2204
|
+
section.hidden = true
|
|
2205
|
+
const title = document.createElement("h2")
|
|
2206
|
+
title.textContent = panel.title
|
|
2207
|
+
const lede = document.createElement("p")
|
|
2208
|
+
lede.className = "lede"
|
|
2209
|
+
lede.textContent = panel.description || ""
|
|
2210
|
+
const body = document.createElement("div")
|
|
2211
|
+
body.className = "panel-body"
|
|
2212
|
+
body.id = "panel-" + panel.id
|
|
2213
|
+
body.innerHTML = panel.html || ""
|
|
2214
|
+
section.append(title, lede, body)
|
|
2215
|
+
views.append(section)
|
|
2216
|
+
if (panel.script) {
|
|
2217
|
+
try {
|
|
2218
|
+
const run = new Function("root", "api", panel.script)
|
|
2219
|
+
run(body, panelApi)
|
|
2220
|
+
} catch (error) {
|
|
2221
|
+
const note = document.createElement("p")
|
|
2222
|
+
note.className = "banner show"
|
|
2223
|
+
note.textContent = error instanceof Error ? error.message : String(error)
|
|
2224
|
+
body.append(note)
|
|
2225
|
+
}
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
loadNamespaces()
|
|
2231
|
+
.then(() => api("GET", "/ui/manifest"))
|
|
2232
|
+
.then((manifest) => { mountPanels((manifest && manifest.panels) || []); return refresh() })
|
|
2233
|
+
.catch(showError)
|
|
2234
|
+
})()
|
|
2235
|
+
</script>
|
|
2236
|
+
</body>
|
|
2237
|
+
</html>`;
|
|
2238
|
+
};
|
|
2239
|
+
var htmlResponse = (body) => new Response(body, {
|
|
2240
|
+
status: 200,
|
|
2241
|
+
headers: {
|
|
2242
|
+
"content-type": "text/html; charset=utf-8",
|
|
2243
|
+
"cache-control": "no-store"
|
|
2244
|
+
}
|
|
2245
|
+
});
|
|
2246
|
+
var json2 = (body) => new Response(JSON.stringify(body), {
|
|
2247
|
+
status: 200,
|
|
2248
|
+
headers: { "content-type": "application/json; charset=utf-8" }
|
|
2249
|
+
});
|
|
2250
|
+
var adminUiRoutes = (service, ui) => {
|
|
2251
|
+
const shell = () => renderAdminDocument(service);
|
|
2252
|
+
const document2 = () => ui?.render ? ui.render({ service, defaultHtml: shell }) : shell();
|
|
2253
|
+
return {
|
|
2254
|
+
"GET /ui": () => htmlResponse(document2()),
|
|
2255
|
+
"GET /ui/": () => htmlResponse(document2()),
|
|
2256
|
+
"GET /ui/manifest": () => json2({
|
|
2257
|
+
service,
|
|
2258
|
+
panels: (ui?.panels ?? []).map((panel) => ({
|
|
2259
|
+
id: panel.id,
|
|
2260
|
+
title: panel.title,
|
|
2261
|
+
...panel.description !== void 0 ? { description: panel.description } : {},
|
|
2262
|
+
html: panel.html,
|
|
2263
|
+
...panel.script !== void 0 ? { script: panel.script } : {}
|
|
2264
|
+
}))
|
|
2265
|
+
})
|
|
2266
|
+
};
|
|
2267
|
+
};
|
|
2268
|
+
|
|
1337
2269
|
// ../../openapi/metadata/dist/types.js
|
|
1338
2270
|
var EXTENSION_KEYS = {
|
|
1339
2271
|
operation: "x-mockingbird",
|
|
@@ -1581,8 +2513,442 @@ var restoreNamespace = (sqlite, namespace, snapshot) => {
|
|
|
1581
2513
|
});
|
|
1582
2514
|
};
|
|
1583
2515
|
|
|
2516
|
+
// ../core/dist/state-view.js
|
|
2517
|
+
var COLLECTION_NAME = /^[A-Za-z0-9_.@:-]{1,200}$/;
|
|
2518
|
+
var RECORD_ID = /^[^\s/]{1,256}$/;
|
|
2519
|
+
var SAMPLE = 25;
|
|
2520
|
+
var WALK_LIMIT = 1e4;
|
|
2521
|
+
var SKIP_CTORS = /* @__PURE__ */ new Set(["Hono", "HonoRequest", "Router"]);
|
|
2522
|
+
var fail = (status, message) => ({ ok: false, status, message });
|
|
2523
|
+
var isRecord3 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2524
|
+
var kindOf = (value) => {
|
|
2525
|
+
if (value === null)
|
|
2526
|
+
return "null";
|
|
2527
|
+
if (Array.isArray(value))
|
|
2528
|
+
return "array";
|
|
2529
|
+
switch (typeof value) {
|
|
2530
|
+
case "string":
|
|
2531
|
+
return "string";
|
|
2532
|
+
case "number":
|
|
2533
|
+
return Number.isFinite(value) ? "number" : "unknown";
|
|
2534
|
+
case "boolean":
|
|
2535
|
+
return "boolean";
|
|
2536
|
+
case "object":
|
|
2537
|
+
return "object";
|
|
2538
|
+
default:
|
|
2539
|
+
return "unknown";
|
|
2540
|
+
}
|
|
2541
|
+
};
|
|
2542
|
+
var humanize = (name) => {
|
|
2543
|
+
const words = name.replace(/[@:_-]+/g, " ").trim();
|
|
2544
|
+
if (words === "")
|
|
2545
|
+
return name;
|
|
2546
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
2547
|
+
};
|
|
2548
|
+
var jsonValue = (value) => {
|
|
2549
|
+
if (value === void 0)
|
|
2550
|
+
return fail(400, "value is required");
|
|
2551
|
+
try {
|
|
2552
|
+
return { ok: true, value: JSON.parse(JSON.stringify(value)) };
|
|
2553
|
+
} catch {
|
|
2554
|
+
return fail(400, "value must be JSON");
|
|
2555
|
+
}
|
|
2556
|
+
};
|
|
2557
|
+
var ctorName = (value) => {
|
|
2558
|
+
const ctor = value.constructor;
|
|
2559
|
+
return typeof ctor?.name === "string" ? ctor.name : "";
|
|
2560
|
+
};
|
|
2561
|
+
var isSqlite = (value) => "prepare" in value && typeof value.prepare === "function" && "transaction" in value;
|
|
2562
|
+
var discoverCollections = (root) => {
|
|
2563
|
+
const found = [];
|
|
2564
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2565
|
+
const visit = (value, depth) => {
|
|
2566
|
+
if (found.length + seen.size > WALK_LIMIT)
|
|
2567
|
+
return;
|
|
2568
|
+
if (value === null || typeof value !== "object")
|
|
2569
|
+
return;
|
|
2570
|
+
if (seen.has(value))
|
|
2571
|
+
return;
|
|
2572
|
+
seen.add(value);
|
|
2573
|
+
if (value instanceof Collection) {
|
|
2574
|
+
found.push(value);
|
|
2575
|
+
return;
|
|
2576
|
+
}
|
|
2577
|
+
if (value instanceof Map) {
|
|
2578
|
+
for (const child of value.values())
|
|
2579
|
+
visit(child, depth + 1);
|
|
2580
|
+
return;
|
|
2581
|
+
}
|
|
2582
|
+
if (value instanceof Set) {
|
|
2583
|
+
for (const child of value.values())
|
|
2584
|
+
visit(child, depth + 1);
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2587
|
+
if (depth > 6 || isSqlite(value) || SKIP_CTORS.has(ctorName(value)))
|
|
2588
|
+
return;
|
|
2589
|
+
if (Array.isArray(value)) {
|
|
2590
|
+
const cap = Math.min(value.length, 32);
|
|
2591
|
+
for (let i = 0; i < cap; i++)
|
|
2592
|
+
visit(value[i], depth + 1);
|
|
2593
|
+
return;
|
|
2594
|
+
}
|
|
2595
|
+
let children;
|
|
2596
|
+
try {
|
|
2597
|
+
children = Object.values(value);
|
|
2598
|
+
} catch {
|
|
2599
|
+
return;
|
|
2600
|
+
}
|
|
2601
|
+
for (const child of children)
|
|
2602
|
+
visit(child, depth + 1);
|
|
2603
|
+
};
|
|
2604
|
+
visit(root, 0);
|
|
2605
|
+
return found;
|
|
2606
|
+
};
|
|
2607
|
+
var storedNames = (sqlite, namespace) => {
|
|
2608
|
+
const records = sqlite.prepare("SELECT DISTINCT collection AS name FROM mockingbird_records WHERE namespace = ?").all(namespace);
|
|
2609
|
+
const sequences = sqlite.prepare("SELECT DISTINCT name FROM mockingbird_sequences WHERE namespace = ? AND kind = 'collection'").all(namespace);
|
|
2610
|
+
return [...records, ...sequences].map((row) => row.name);
|
|
2611
|
+
};
|
|
2612
|
+
var blankAccum = (name) => ({
|
|
2613
|
+
name,
|
|
2614
|
+
kinds: /* @__PURE__ */ new Set(),
|
|
2615
|
+
present: 0,
|
|
2616
|
+
nested: /* @__PURE__ */ new Map()
|
|
2617
|
+
});
|
|
2618
|
+
var absorb = (accum, value) => {
|
|
2619
|
+
const kind = kindOf(value);
|
|
2620
|
+
accum.kinds.add(kind);
|
|
2621
|
+
accum.present++;
|
|
2622
|
+
if (!isRecord3(value))
|
|
2623
|
+
return;
|
|
2624
|
+
for (const [key, child] of Object.entries(value)) {
|
|
2625
|
+
const nested = accum.nested.get(key) ?? blankAccum(key);
|
|
2626
|
+
absorb(nested, child);
|
|
2627
|
+
accum.nested.set(key, nested);
|
|
2628
|
+
}
|
|
2629
|
+
};
|
|
2630
|
+
var finishField = (accum, samples) => {
|
|
2631
|
+
const kinds = [...accum.kinds];
|
|
2632
|
+
const kind = kinds.length === 1 ? kinds[0] : "unknown";
|
|
2633
|
+
const field = {
|
|
2634
|
+
name: accum.name,
|
|
2635
|
+
kind,
|
|
2636
|
+
optional: accum.present < samples || kinds.includes("null")
|
|
2637
|
+
};
|
|
2638
|
+
if (accum.nested.size > 0) {
|
|
2639
|
+
field.fields = [...accum.nested.values()].sort((a, b) => a.name.localeCompare(b.name)).map((child) => finishField(child, accum.present));
|
|
2640
|
+
}
|
|
2641
|
+
return field;
|
|
2642
|
+
};
|
|
2643
|
+
var inferFields = (records) => {
|
|
2644
|
+
if (records.length === 0)
|
|
2645
|
+
return [];
|
|
2646
|
+
const sample = records.slice(0, SAMPLE);
|
|
2647
|
+
const objects = sample.filter((record2) => isRecord3(record2.value));
|
|
2648
|
+
if (objects.length === 0) {
|
|
2649
|
+
const kinds = new Set(sample.map((record2) => kindOf(record2.value)));
|
|
2650
|
+
const list = [...kinds];
|
|
2651
|
+
return [
|
|
2652
|
+
{
|
|
2653
|
+
name: "(value)",
|
|
2654
|
+
kind: list.length === 1 ? list[0] : "unknown",
|
|
2655
|
+
optional: false
|
|
2656
|
+
}
|
|
2657
|
+
];
|
|
2658
|
+
}
|
|
2659
|
+
const root = /* @__PURE__ */ new Map();
|
|
2660
|
+
for (const record2 of objects) {
|
|
2661
|
+
for (const [key, value] of Object.entries(record2.value)) {
|
|
2662
|
+
const accum = root.get(key) ?? blankAccum(key);
|
|
2663
|
+
absorb(accum, value);
|
|
2664
|
+
root.set(key, accum);
|
|
2665
|
+
}
|
|
2666
|
+
}
|
|
2667
|
+
return [...root.values()].sort((a, b) => a.name.localeCompare(b.name)).map((accum) => finishField(accum, objects.length));
|
|
2668
|
+
};
|
|
2669
|
+
var mergeFields = (declared, inferred) => {
|
|
2670
|
+
const byName = /* @__PURE__ */ new Map();
|
|
2671
|
+
for (const field of inferred)
|
|
2672
|
+
byName.set(field.name, field);
|
|
2673
|
+
const ordered = [];
|
|
2674
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2675
|
+
for (const field of declared ?? []) {
|
|
2676
|
+
const found = byName.get(field.name);
|
|
2677
|
+
seen.add(field.name);
|
|
2678
|
+
if (!found) {
|
|
2679
|
+
ordered.push({ ...field, optional: field.optional });
|
|
2680
|
+
continue;
|
|
2681
|
+
}
|
|
2682
|
+
ordered.push({
|
|
2683
|
+
...found,
|
|
2684
|
+
kind: field.kind === "unknown" ? found.kind : field.kind,
|
|
2685
|
+
optional: field.optional || found.optional,
|
|
2686
|
+
...field.description !== void 0 ? { description: field.description } : {},
|
|
2687
|
+
...field.meta !== void 0 ? { meta: field.meta } : {},
|
|
2688
|
+
...field.fields !== void 0 ? { fields: mergeFields(field.fields, found.fields ?? []) } : {}
|
|
2689
|
+
});
|
|
2690
|
+
}
|
|
2691
|
+
for (const field of inferred) {
|
|
2692
|
+
if (!seen.has(field.name))
|
|
2693
|
+
ordered.push(field);
|
|
2694
|
+
}
|
|
2695
|
+
return ordered;
|
|
2696
|
+
};
|
|
2697
|
+
var collectionOf = (sqlite, namespace, name) => new Collection(sqlite, namespace, name);
|
|
2698
|
+
var readRecord = (collection, id) => {
|
|
2699
|
+
const rows = collection.list({ where: (_value, _seq) => true, order: "oldest" });
|
|
2700
|
+
const row = rows.find((entry) => entry.id === id);
|
|
2701
|
+
return row ? { id: row.id, seq: row.seq, value: row.value } : void 0;
|
|
2702
|
+
};
|
|
2703
|
+
var inspectState = (scope) => {
|
|
2704
|
+
const declared = /* @__PURE__ */ new Map();
|
|
2705
|
+
for (const declaration of scope.declarations ?? []) {
|
|
2706
|
+
if (COLLECTION_NAME.test(declaration.name))
|
|
2707
|
+
declared.set(declaration.name, declaration);
|
|
2708
|
+
}
|
|
2709
|
+
const names = new Set(declared.keys());
|
|
2710
|
+
for (const name of storedNames(scope.sqlite, scope.storageNamespace))
|
|
2711
|
+
names.add(name);
|
|
2712
|
+
for (const collection of discoverCollections(scope.root)) {
|
|
2713
|
+
if (collection.namespace === scope.storageNamespace)
|
|
2714
|
+
names.add(collection.collectionName);
|
|
2715
|
+
}
|
|
2716
|
+
const collections = [];
|
|
2717
|
+
for (const name of names) {
|
|
2718
|
+
if (!COLLECTION_NAME.test(name))
|
|
2719
|
+
continue;
|
|
2720
|
+
const declaration = declared.get(name);
|
|
2721
|
+
const collection = collectionOf(scope.sqlite, scope.storageNamespace, name);
|
|
2722
|
+
const records = collection.list({ order: "newest" }).slice(0, SAMPLE).map((row) => ({ id: row.id, seq: row.seq, value: row.value }));
|
|
2723
|
+
const inferred = inferFields(records);
|
|
2724
|
+
const fields = mergeFields(declaration?.fields, inferred);
|
|
2725
|
+
const source = declaration && records.length > 0 ? "mixed" : declaration ? "declared" : "inferred";
|
|
2726
|
+
collections.push({
|
|
2727
|
+
name,
|
|
2728
|
+
label: declaration?.label ?? humanize(name),
|
|
2729
|
+
...declaration?.description !== void 0 ? { description: declaration.description } : {},
|
|
2730
|
+
count: collection.count(),
|
|
2731
|
+
source,
|
|
2732
|
+
fields,
|
|
2733
|
+
...declaration?.meta !== void 0 ? { meta: declaration.meta } : {}
|
|
2734
|
+
});
|
|
2735
|
+
}
|
|
2736
|
+
const order = /* @__PURE__ */ new Map();
|
|
2737
|
+
for (const [index, item] of (scope.declarations ?? []).entries())
|
|
2738
|
+
order.set(item.name, index);
|
|
2739
|
+
collections.sort((a, b) => {
|
|
2740
|
+
const left = order.get(a.name);
|
|
2741
|
+
const right = order.get(b.name);
|
|
2742
|
+
if (left !== void 0 && right !== void 0)
|
|
2743
|
+
return left - right;
|
|
2744
|
+
if (left !== void 0)
|
|
2745
|
+
return -1;
|
|
2746
|
+
if (right !== void 0)
|
|
2747
|
+
return 1;
|
|
2748
|
+
return a.name.localeCompare(b.name);
|
|
2749
|
+
});
|
|
2750
|
+
return {
|
|
2751
|
+
namespace: scope.namespace,
|
|
2752
|
+
storageNamespace: scope.storageNamespace,
|
|
2753
|
+
collections
|
|
2754
|
+
};
|
|
2755
|
+
};
|
|
2756
|
+
var pageOf = (sqlite, storageNamespace, name, query) => {
|
|
2757
|
+
if (!COLLECTION_NAME.test(name))
|
|
2758
|
+
return fail(400, "collection name is not allowed");
|
|
2759
|
+
const limit = query.limit ?? 50;
|
|
2760
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 200) {
|
|
2761
|
+
return fail(400, "limit must be an integer from 1 to 200");
|
|
2762
|
+
}
|
|
2763
|
+
const order = query.order ?? "newest";
|
|
2764
|
+
if (order !== "newest" && order !== "oldest")
|
|
2765
|
+
return fail(400, 'order must be "newest" or "oldest"');
|
|
2766
|
+
const collection = collectionOf(sqlite, storageNamespace, name);
|
|
2767
|
+
const rows = collection.list({ order }).filter((row) => {
|
|
2768
|
+
if (query.after === void 0)
|
|
2769
|
+
return true;
|
|
2770
|
+
return order === "newest" ? row.seq < query.after : row.seq > query.after;
|
|
2771
|
+
});
|
|
2772
|
+
const slice = rows.slice(0, limit);
|
|
2773
|
+
const last = slice.at(-1);
|
|
2774
|
+
return {
|
|
2775
|
+
ok: true,
|
|
2776
|
+
value: {
|
|
2777
|
+
collection: name,
|
|
2778
|
+
count: collection.count(),
|
|
2779
|
+
records: slice.map((row) => ({ id: row.id, seq: row.seq, value: row.value })),
|
|
2780
|
+
next: rows.length > limit && last ? last.seq : null
|
|
2781
|
+
}
|
|
2782
|
+
};
|
|
2783
|
+
};
|
|
2784
|
+
var readStatePage = (scope, name, query = {}) => pageOf(scope.sqlite, scope.storageNamespace, name, query);
|
|
2785
|
+
var readStateRecord = (scope, name, id) => {
|
|
2786
|
+
if (!COLLECTION_NAME.test(name))
|
|
2787
|
+
return fail(400, "collection name is not allowed");
|
|
2788
|
+
if (!RECORD_ID.test(id))
|
|
2789
|
+
return fail(400, "record id is not allowed");
|
|
2790
|
+
const record2 = readRecord(collectionOf(scope.sqlite, scope.storageNamespace, name), id);
|
|
2791
|
+
return record2 ? { ok: true, value: record2 } : fail(404, `no record ${id} in ${name}`);
|
|
2792
|
+
};
|
|
2793
|
+
var requireId = (id) => RECORD_ID.test(id) ? { ok: true, value: id } : fail(400, "id must be a single path segment");
|
|
2794
|
+
var createStateRecord = (scope, name, input) => {
|
|
2795
|
+
if (!COLLECTION_NAME.test(name))
|
|
2796
|
+
return fail(400, "collection name is not allowed");
|
|
2797
|
+
const parsed = jsonValue(input.value);
|
|
2798
|
+
if (!parsed.ok)
|
|
2799
|
+
return parsed;
|
|
2800
|
+
const collection = collectionOf(scope.sqlite, scope.storageNamespace, name);
|
|
2801
|
+
const id = input.id ?? `${name}_${collection.nextSequence()}`;
|
|
2802
|
+
const checked = requireId(id);
|
|
2803
|
+
if (!checked.ok)
|
|
2804
|
+
return checked;
|
|
2805
|
+
if (collection.has(checked.value))
|
|
2806
|
+
return fail(409, `record ${checked.value} already exists`);
|
|
2807
|
+
const stored = collection.insert(checked.value, parsed.value);
|
|
2808
|
+
return { ok: true, value: { id: checked.value, seq: stored.seq, value: stored.value } };
|
|
2809
|
+
};
|
|
2810
|
+
var replaceStateRecord = (scope, name, id, value) => {
|
|
2811
|
+
if (!COLLECTION_NAME.test(name))
|
|
2812
|
+
return fail(400, "collection name is not allowed");
|
|
2813
|
+
const checked = requireId(id);
|
|
2814
|
+
if (!checked.ok)
|
|
2815
|
+
return checked;
|
|
2816
|
+
const parsed = jsonValue(value);
|
|
2817
|
+
if (!parsed.ok)
|
|
2818
|
+
return parsed;
|
|
2819
|
+
const collection = collectionOf(scope.sqlite, scope.storageNamespace, name);
|
|
2820
|
+
if (!collection.has(checked.value))
|
|
2821
|
+
return fail(404, `no record ${checked.value} in ${name}`);
|
|
2822
|
+
const stored = collection.update(checked.value, parsed.value);
|
|
2823
|
+
if (!stored)
|
|
2824
|
+
return fail(404, `no record ${checked.value} in ${name}`);
|
|
2825
|
+
return { ok: true, value: { id: checked.value, seq: stored.seq, value: stored.value } };
|
|
2826
|
+
};
|
|
2827
|
+
var patchStateRecord = (scope, name, id, value) => {
|
|
2828
|
+
const current = readStateRecord(scope, name, id);
|
|
2829
|
+
if (!current.ok)
|
|
2830
|
+
return current;
|
|
2831
|
+
const parsed = jsonValue(value);
|
|
2832
|
+
if (!parsed.ok)
|
|
2833
|
+
return parsed;
|
|
2834
|
+
const next = isRecord3(current.value.value) && isRecord3(parsed.value) ? { ...current.value.value, ...parsed.value } : parsed.value;
|
|
2835
|
+
return replaceStateRecord(scope, name, id, next);
|
|
2836
|
+
};
|
|
2837
|
+
var deleteStateRecord = (scope, name, id) => {
|
|
2838
|
+
if (!COLLECTION_NAME.test(name))
|
|
2839
|
+
return fail(400, "collection name is not allowed");
|
|
2840
|
+
const checked = requireId(id);
|
|
2841
|
+
if (!checked.ok)
|
|
2842
|
+
return checked;
|
|
2843
|
+
const collection = collectionOf(scope.sqlite, scope.storageNamespace, name);
|
|
2844
|
+
return collection.delete(checked.value) ? { ok: true, value: { id: checked.value } } : fail(404, `no record ${checked.value} in ${name}`);
|
|
2845
|
+
};
|
|
2846
|
+
var json3 = (status, body) => new Response(JSON.stringify(body), {
|
|
2847
|
+
status,
|
|
2848
|
+
headers: { "content-type": "application/json; charset=utf-8" }
|
|
2849
|
+
});
|
|
2850
|
+
var adminError2 = (status, message) => json3(status, { error: { type: "mockingbird_admin", message } });
|
|
2851
|
+
var fromResult = (result, status) => result.ok ? json3(status, result.value) : adminError2(result.status, result.message);
|
|
2852
|
+
var integerParam = (value) => {
|
|
2853
|
+
if (value === null)
|
|
2854
|
+
return void 0;
|
|
2855
|
+
if (!/^-?\d+$/.test(value))
|
|
2856
|
+
return "bad";
|
|
2857
|
+
return Number(value);
|
|
2858
|
+
};
|
|
2859
|
+
var stateAdminRoutes = (routes) => {
|
|
2860
|
+
const open = (namespace) => {
|
|
2861
|
+
try {
|
|
2862
|
+
return { ok: true, value: routes.open(namespace) };
|
|
2863
|
+
} catch (error) {
|
|
2864
|
+
return fail(400, error instanceof Error ? error.message : String(error));
|
|
2865
|
+
}
|
|
2866
|
+
};
|
|
2867
|
+
const wrote = (namespace) => {
|
|
2868
|
+
routes.afterWrite?.(namespace);
|
|
2869
|
+
};
|
|
2870
|
+
return {
|
|
2871
|
+
"GET /state": ({ namespace }) => {
|
|
2872
|
+
const scope = open(namespace);
|
|
2873
|
+
return fromResult(scope.ok ? { ok: true, value: inspectState(scope.value) } : scope, 200);
|
|
2874
|
+
},
|
|
2875
|
+
"GET /state/:collection": ({ namespace, params, url }) => {
|
|
2876
|
+
const scope = open(namespace);
|
|
2877
|
+
if (!scope.ok)
|
|
2878
|
+
return fromResult(scope, 200);
|
|
2879
|
+
const limit = integerParam(url.searchParams.get("limit"));
|
|
2880
|
+
const after = integerParam(url.searchParams.get("after"));
|
|
2881
|
+
if (limit === "bad")
|
|
2882
|
+
return adminError2(400, "limit: expected an integer");
|
|
2883
|
+
if (after === "bad")
|
|
2884
|
+
return adminError2(400, "after: expected a sequence number");
|
|
2885
|
+
const order = url.searchParams.get("order");
|
|
2886
|
+
return fromResult(readStatePage(scope.value, params.collection ?? "", {
|
|
2887
|
+
...limit !== void 0 ? { limit } : {},
|
|
2888
|
+
...after !== void 0 ? { after } : {},
|
|
2889
|
+
...order === "newest" || order === "oldest" ? { order } : {}
|
|
2890
|
+
}), 200);
|
|
2891
|
+
},
|
|
2892
|
+
"GET /state/:collection/:id": ({ namespace, params }) => {
|
|
2893
|
+
const scope = open(namespace);
|
|
2894
|
+
if (!scope.ok)
|
|
2895
|
+
return fromResult(scope, 200);
|
|
2896
|
+
return fromResult(readStateRecord(scope.value, params.collection ?? "", params.id ?? ""), 200);
|
|
2897
|
+
},
|
|
2898
|
+
"POST /state/:collection": ({ namespace, params, body }) => {
|
|
2899
|
+
const scope = open(namespace);
|
|
2900
|
+
if (!scope.ok)
|
|
2901
|
+
return fromResult(scope, 201);
|
|
2902
|
+
if (!isRecord3(body) || !("value" in body)) {
|
|
2903
|
+
return adminError2(400, 'expected {"id?": "<id>", "value": <json>}');
|
|
2904
|
+
}
|
|
2905
|
+
const id = body.id;
|
|
2906
|
+
if (id !== void 0 && typeof id !== "string")
|
|
2907
|
+
return adminError2(400, "id must be a string");
|
|
2908
|
+
const created = createStateRecord(scope.value, params.collection ?? "", {
|
|
2909
|
+
...typeof id === "string" ? { id } : {},
|
|
2910
|
+
value: body.value
|
|
2911
|
+
});
|
|
2912
|
+
if (created.ok)
|
|
2913
|
+
wrote(namespace);
|
|
2914
|
+
return fromResult(created, 201);
|
|
2915
|
+
},
|
|
2916
|
+
"PUT /state/:collection/:id": ({ namespace, params, body }) => {
|
|
2917
|
+
const scope = open(namespace);
|
|
2918
|
+
if (!scope.ok)
|
|
2919
|
+
return fromResult(scope, 200);
|
|
2920
|
+
if (!isRecord3(body) || !("value" in body))
|
|
2921
|
+
return adminError2(400, 'expected {"value": <json>}');
|
|
2922
|
+
const replaced = replaceStateRecord(scope.value, params.collection ?? "", params.id ?? "", body.value);
|
|
2923
|
+
if (replaced.ok)
|
|
2924
|
+
wrote(namespace);
|
|
2925
|
+
return fromResult(replaced, 200);
|
|
2926
|
+
},
|
|
2927
|
+
"PATCH /state/:collection/:id": ({ namespace, params, body }) => {
|
|
2928
|
+
const scope = open(namespace);
|
|
2929
|
+
if (!scope.ok)
|
|
2930
|
+
return fromResult(scope, 200);
|
|
2931
|
+
if (!isRecord3(body) || !("value" in body))
|
|
2932
|
+
return adminError2(400, 'expected {"value": <json>}');
|
|
2933
|
+
const patched = patchStateRecord(scope.value, params.collection ?? "", params.id ?? "", body.value);
|
|
2934
|
+
if (patched.ok)
|
|
2935
|
+
wrote(namespace);
|
|
2936
|
+
return fromResult(patched, 200);
|
|
2937
|
+
},
|
|
2938
|
+
"DELETE /state/:collection/:id": ({ namespace, params }) => {
|
|
2939
|
+
const scope = open(namespace);
|
|
2940
|
+
if (!scope.ok)
|
|
2941
|
+
return fromResult(scope, 200);
|
|
2942
|
+
const removed = deleteStateRecord(scope.value, params.collection ?? "", params.id ?? "");
|
|
2943
|
+
if (removed.ok)
|
|
2944
|
+
wrote(namespace);
|
|
2945
|
+
return fromResult(removed, 200);
|
|
2946
|
+
}
|
|
2947
|
+
};
|
|
2948
|
+
};
|
|
2949
|
+
|
|
1584
2950
|
// ../core/dist/version.js
|
|
1585
|
-
var PACKAGE_VERSION = true ? "1.
|
|
2951
|
+
var PACKAGE_VERSION = true ? "1.5.0" : UNRELEASED_VERSION;
|
|
1586
2952
|
|
|
1587
2953
|
// ../core/dist/signing.js
|
|
1588
2954
|
var encoder = new TextEncoder();
|
|
@@ -1917,11 +3283,11 @@ var createWebhookHub = (options) => {
|
|
|
1917
3283
|
};
|
|
1918
3284
|
return hub;
|
|
1919
3285
|
};
|
|
1920
|
-
var
|
|
1921
|
-
var
|
|
1922
|
-
var
|
|
3286
|
+
var json4 = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
3287
|
+
var adminError3 = (status, message) => json4(status, { error: { type: "mockingbird_admin", message } });
|
|
3288
|
+
var isRecord4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1923
3289
|
var parseEndpoint = (value) => {
|
|
1924
|
-
if (!
|
|
3290
|
+
if (!isRecord4(value) || typeof value.url !== "string")
|
|
1925
3291
|
return "each endpoint needs a url";
|
|
1926
3292
|
try {
|
|
1927
3293
|
new URL(value.url);
|
|
@@ -1938,18 +3304,18 @@ var parseEndpoint = (value) => {
|
|
|
1938
3304
|
const events = value.events ?? value.enabledEvents;
|
|
1939
3305
|
if (Array.isArray(events))
|
|
1940
3306
|
endpoint.events = events.map(String);
|
|
1941
|
-
if (
|
|
3307
|
+
if (isRecord4(value.tags)) {
|
|
1942
3308
|
endpoint.tags = Object.fromEntries(Object.entries(value.tags).map(([k, v]) => [k, String(v)]));
|
|
1943
3309
|
}
|
|
1944
3310
|
if (typeof value.account === "string")
|
|
1945
3311
|
endpoint.tags = { ...endpoint.tags, account: value.account };
|
|
1946
|
-
if (
|
|
3312
|
+
if (isRecord4(value.headers)) {
|
|
1947
3313
|
endpoint.headers = Object.fromEntries(Object.entries(value.headers).map(([k, v]) => [k, String(v)]));
|
|
1948
3314
|
}
|
|
1949
3315
|
return endpoint;
|
|
1950
3316
|
};
|
|
1951
3317
|
var webhookAdminRoutes = (hub) => ({
|
|
1952
|
-
"GET /webhooks": ({ url, namespace }) =>
|
|
3318
|
+
"GET /webhooks": ({ url, namespace }) => json4(200, {
|
|
1953
3319
|
deliveries: hub.deliveries(url.searchParams.get("all") === "1" ? void 0 : namespace).filter((d) => {
|
|
1954
3320
|
const type = url.searchParams.get("type");
|
|
1955
3321
|
return type === null || d.type === type;
|
|
@@ -1957,51 +3323,51 @@ var webhookAdminRoutes = (hub) => ({
|
|
|
1957
3323
|
}),
|
|
1958
3324
|
"GET /webhooks/events": ({ url, namespace }) => {
|
|
1959
3325
|
const type = url.searchParams.get("type");
|
|
1960
|
-
return
|
|
3326
|
+
return json4(200, {
|
|
1961
3327
|
events: hub.messages(url.searchParams.get("all") === "1" ? void 0 : namespace).filter((m) => type === null || m.type === type).map((m) => ({ ...m, payload: parsePayload(m) }))
|
|
1962
3328
|
});
|
|
1963
3329
|
},
|
|
1964
3330
|
"POST /webhooks/:id/replay": async ({ params }) => {
|
|
1965
3331
|
const replayed = await hub.replay(params.id);
|
|
1966
|
-
return replayed ?
|
|
3332
|
+
return replayed ? json4(200, replayed) : adminError3(404, `no delivery ${params.id}`);
|
|
1967
3333
|
},
|
|
1968
3334
|
"POST /webhooks/flush": async () => {
|
|
1969
3335
|
await hub.flush();
|
|
1970
|
-
return
|
|
3336
|
+
return json4(200, { status: "ok" });
|
|
1971
3337
|
},
|
|
1972
3338
|
"POST /webhooks/faults": ({ body, namespace }) => {
|
|
1973
|
-
if (!
|
|
1974
|
-
return
|
|
3339
|
+
if (!isRecord4(body) || !["duplicate", "reorder", "drop"].includes(String(body.mode))) {
|
|
3340
|
+
return adminError3(400, "mode must be duplicate, reorder or drop");
|
|
1975
3341
|
}
|
|
1976
3342
|
const fault = { mode: body.mode };
|
|
1977
3343
|
if (typeof body.count === "number")
|
|
1978
3344
|
fault.count = body.count;
|
|
1979
3345
|
hub.fault(namespace, fault);
|
|
1980
|
-
return
|
|
3346
|
+
return json4(201, { namespace, ...fault });
|
|
1981
3347
|
},
|
|
1982
|
-
"GET /webhook-endpoints": ({ namespace }) =>
|
|
3348
|
+
"GET /webhook-endpoints": ({ namespace }) => json4(200, {
|
|
1983
3349
|
endpoints: hub.endpoints(namespace).map(({ secret, ...rest }) => ({
|
|
1984
3350
|
...rest,
|
|
1985
3351
|
secret: secret ? "(set)" : null
|
|
1986
3352
|
}))
|
|
1987
3353
|
}),
|
|
1988
3354
|
"PUT /webhook-endpoints": ({ body, namespace }) => {
|
|
1989
|
-
const list = Array.isArray(body) ? body :
|
|
3355
|
+
const list = Array.isArray(body) ? body : isRecord4(body) ? body.endpoints : void 0;
|
|
1990
3356
|
if (!Array.isArray(list))
|
|
1991
|
-
return
|
|
3357
|
+
return adminError3(400, "expected [{url, secret?, events?}]");
|
|
1992
3358
|
const parsed = [];
|
|
1993
3359
|
for (const each of list) {
|
|
1994
3360
|
const endpoint = parseEndpoint(each);
|
|
1995
3361
|
if (typeof endpoint === "string")
|
|
1996
|
-
return
|
|
3362
|
+
return adminError3(400, endpoint);
|
|
1997
3363
|
parsed.push(endpoint);
|
|
1998
3364
|
}
|
|
1999
3365
|
const set = hub.setEndpoints(namespace, parsed);
|
|
2000
|
-
return
|
|
3366
|
+
return json4(200, { endpoints: set.map((e) => ({ ...e, secret: e.secret ? "(set)" : null })) });
|
|
2001
3367
|
},
|
|
2002
3368
|
"DELETE /webhook-endpoints": ({ namespace }) => {
|
|
2003
3369
|
hub.setEndpoints(namespace, []);
|
|
2004
|
-
return
|
|
3370
|
+
return json4(200, { status: "ok" });
|
|
2005
3371
|
}
|
|
2006
3372
|
});
|
|
2007
3373
|
var parsePayload = (message) => {
|
|
@@ -2066,6 +3432,7 @@ var operationMatcher = (document2) => {
|
|
|
2066
3432
|
return (request, path) => matchers.find((m) => m.method === request.method && m.pattern.test(path))?.operationId;
|
|
2067
3433
|
};
|
|
2068
3434
|
var createRuntime = (options) => {
|
|
3435
|
+
assertAdminUi(options.adminUi);
|
|
2069
3436
|
const sqlite = bootSqlite(options.sqlite);
|
|
2070
3437
|
const clock = options.clock ?? createClock();
|
|
2071
3438
|
const rng = createRng(options.seed ?? 0);
|
|
@@ -2272,6 +3639,19 @@ var createRuntime = (options) => {
|
|
|
2272
3639
|
else
|
|
2273
3640
|
timeline(name);
|
|
2274
3641
|
};
|
|
3642
|
+
const declarationsFor = (namespace) => {
|
|
3643
|
+
const declared = options.state;
|
|
3644
|
+
if (!declared)
|
|
3645
|
+
return [];
|
|
3646
|
+
return typeof declared === "function" ? declared(instance(namespace), namespace) : declared;
|
|
3647
|
+
};
|
|
3648
|
+
const stateScope = (namespace) => ({
|
|
3649
|
+
sqlite,
|
|
3650
|
+
namespace,
|
|
3651
|
+
storageNamespace: storageNamespace(namespace),
|
|
3652
|
+
root: instance(namespace),
|
|
3653
|
+
declarations: declarationsFor(namespace)
|
|
3654
|
+
});
|
|
2275
3655
|
const runtime = {
|
|
2276
3656
|
name: options.name,
|
|
2277
3657
|
sqlite,
|
|
@@ -2310,6 +3690,7 @@ var createRuntime = (options) => {
|
|
|
2310
3690
|
branch,
|
|
2311
3691
|
checkout,
|
|
2312
3692
|
timeline,
|
|
3693
|
+
state: (namespace = DEFAULT_NAMESPACE) => inspectState(stateScope(namespace)),
|
|
2313
3694
|
fetch: async (incoming) => {
|
|
2314
3695
|
let request = incoming;
|
|
2315
3696
|
const prefixed = PATH_PREFIX.exec(new URL(request.url).pathname);
|
|
@@ -2503,11 +3884,20 @@ var createRuntime = (options) => {
|
|
|
2503
3884
|
...credentialRoutes(credentials),
|
|
2504
3885
|
...options.presets ? presetRoutes(options.presets, runtime) : {},
|
|
2505
3886
|
...options.webhooks ? webhookAdminRoutes(options.webhooks) : {},
|
|
2506
|
-
...options.admin?.(runtime) ?? {}
|
|
3887
|
+
...options.admin?.(runtime) ?? {},
|
|
3888
|
+
// Standard state and UI routes win a colliding key. A service adds its own keys
|
|
3889
|
+
// beside these; it does not replace `/state` or `/ui`.
|
|
3890
|
+
...stateAdminRoutes({
|
|
3891
|
+
open: stateScope,
|
|
3892
|
+
afterWrite: (namespace) => {
|
|
3893
|
+
checkpoint(namespace, "main");
|
|
3894
|
+
}
|
|
3895
|
+
}),
|
|
3896
|
+
...adminUiRoutes(options.name, options.adminUi)
|
|
2507
3897
|
},
|
|
2508
3898
|
adminKey: options.adminKey
|
|
2509
3899
|
});
|
|
2510
|
-
return runtime;
|
|
3900
|
+
return defineMock(runtime);
|
|
2511
3901
|
};
|
|
2512
3902
|
var mutableResponse = (response) => {
|
|
2513
3903
|
try {
|
|
@@ -2671,7 +4061,7 @@ var DEFAULT_CATALOG = [
|
|
|
2671
4061
|
status: "inactive"
|
|
2672
4062
|
}
|
|
2673
4063
|
];
|
|
2674
|
-
var
|
|
4064
|
+
var isRecord5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2675
4065
|
var optionalString = (row, key) => {
|
|
2676
4066
|
const value = row[key];
|
|
2677
4067
|
if (value === void 0 || value === null) return null;
|
|
@@ -2679,7 +4069,7 @@ var optionalString = (row, key) => {
|
|
|
2679
4069
|
return value;
|
|
2680
4070
|
};
|
|
2681
4071
|
var parseCatalogItem = (value) => {
|
|
2682
|
-
if (!
|
|
4072
|
+
if (!isRecord5(value)) throw new Error("a catalog row must be an object");
|
|
2683
4073
|
const { catalog_id, medication_name, quantity, states, status } = value;
|
|
2684
4074
|
if (typeof catalog_id !== "string" || catalog_id.length === 0) {
|
|
2685
4075
|
throw new Error("catalog_id must be a non-empty string");
|
|
@@ -2714,7 +4104,7 @@ var parseCatalogItem = (value) => {
|
|
|
2714
4104
|
}
|
|
2715
4105
|
};
|
|
2716
4106
|
var parseCatalog = (value) => {
|
|
2717
|
-
const rows = Array.isArray(value) ? value :
|
|
4107
|
+
const rows = Array.isArray(value) ? value : isRecord5(value) ? value.data : void 0;
|
|
2718
4108
|
if (!Array.isArray(rows)) throw new Error("a catalog must be an array or {data: [...]}");
|
|
2719
4109
|
return rows.map(parseCatalogItem);
|
|
2720
4110
|
};
|
|
@@ -2870,12 +4260,12 @@ var RXVORTEX_PRESETS = {
|
|
|
2870
4260
|
webhook: { mode: "drop" }
|
|
2871
4261
|
}
|
|
2872
4262
|
};
|
|
2873
|
-
var
|
|
2874
|
-
var
|
|
2875
|
-
var
|
|
4263
|
+
var json5 = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
4264
|
+
var adminError4 = (status, message) => json5(status, { error: { type: "mockingbird_admin", message } });
|
|
4265
|
+
var isRecord6 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2876
4266
|
var parseAutoAdvance = (value) => {
|
|
2877
4267
|
if (value === null) return null;
|
|
2878
|
-
if (!
|
|
4268
|
+
if (!isRecord6(value)) return "autoAdvance must be {afterMs, path} or null";
|
|
2879
4269
|
if (typeof value.afterMs !== "number" || value.afterMs < 0)
|
|
2880
4270
|
return "autoAdvance.afterMs must be ms";
|
|
2881
4271
|
if (!Array.isArray(value.path) || value.path.some((s) => typeof s !== "string")) {
|
|
@@ -2884,10 +4274,10 @@ var parseAutoAdvance = (value) => {
|
|
|
2884
4274
|
return { afterMs: value.afterMs, path: value.path };
|
|
2885
4275
|
};
|
|
2886
4276
|
var adminRoutes = (runtime) => ({
|
|
2887
|
-
"GET /orders": ({ namespace }) =>
|
|
4277
|
+
"GET /orders": ({ namespace }) => json5(200, { orders: runtime.instance(namespace).orders() }),
|
|
2888
4278
|
"POST /orders/:id/transition": ({ params, body, namespace }) => {
|
|
2889
|
-
if (!
|
|
2890
|
-
return
|
|
4279
|
+
if (!isRecord6(body) || typeof body.to !== "string") {
|
|
4280
|
+
return adminError4(
|
|
2891
4281
|
400,
|
|
2892
4282
|
'expected {"to": "<vendor status>", "trackingnumber"?, "shippingcarrier"?}'
|
|
2893
4283
|
);
|
|
@@ -2900,61 +4290,61 @@ var adminRoutes = (runtime) => ({
|
|
|
2900
4290
|
...optional("shippingservice"),
|
|
2901
4291
|
...optional("delivered_date")
|
|
2902
4292
|
});
|
|
2903
|
-
return order ?
|
|
4293
|
+
return order ? json5(200, order) : adminError4(404, `no order ${params.id}`);
|
|
2904
4294
|
},
|
|
2905
|
-
"GET /catalog": ({ namespace }) =>
|
|
4295
|
+
"GET /catalog": ({ namespace }) => json5(200, { data: runtime.instance(namespace).state.catalogRows() }),
|
|
2906
4296
|
"PUT /catalog": ({ body, namespace }) => {
|
|
2907
|
-
const rows =
|
|
2908
|
-
if (!
|
|
2909
|
-
return
|
|
4297
|
+
const rows = isRecord6(body) ? body.items ?? body.data : void 0;
|
|
4298
|
+
if (!isRecord6(body) || !Array.isArray(rows)) {
|
|
4299
|
+
return adminError4(400, 'expected {"items": [{catalog_id, medication_name, \u2026}], "mode"?}');
|
|
2910
4300
|
}
|
|
2911
4301
|
const mode = body.mode ?? "replace";
|
|
2912
4302
|
if (mode !== "replace" && mode !== "merge") {
|
|
2913
|
-
return
|
|
4303
|
+
return adminError4(400, 'mode must be "replace" or "merge"');
|
|
2914
4304
|
}
|
|
2915
4305
|
let items;
|
|
2916
4306
|
try {
|
|
2917
4307
|
items = rows.map(parseCatalogItem);
|
|
2918
4308
|
} catch (error) {
|
|
2919
|
-
return
|
|
4309
|
+
return adminError4(400, `items: ${error.message}`);
|
|
2920
4310
|
}
|
|
2921
|
-
return
|
|
4311
|
+
return json5(200, { count: runtime.instance(namespace).state.putCatalog(items, mode) });
|
|
2922
4312
|
},
|
|
2923
|
-
"GET /settings": ({ namespace }) =>
|
|
4313
|
+
"GET /settings": ({ namespace }) => json5(200, runtime.instance(namespace).state.current()),
|
|
2924
4314
|
"PUT /settings": ({ body, namespace }) => {
|
|
2925
|
-
if (!
|
|
4315
|
+
if (!isRecord6(body)) return adminError4(400, "expected a JSON object");
|
|
2926
4316
|
const patch = {};
|
|
2927
4317
|
if (body.tokenTtlSeconds !== void 0) {
|
|
2928
4318
|
if (typeof body.tokenTtlSeconds !== "number")
|
|
2929
|
-
return
|
|
4319
|
+
return adminError4(400, "tokenTtlSeconds: number");
|
|
2930
4320
|
patch.tokenTtlSeconds = body.tokenTtlSeconds;
|
|
2931
4321
|
}
|
|
2932
4322
|
if (body.staticTokens !== void 0) {
|
|
2933
|
-
if (!Array.isArray(body.staticTokens)) return
|
|
4323
|
+
if (!Array.isArray(body.staticTokens)) return adminError4(400, "staticTokens: string[]");
|
|
2934
4324
|
patch.staticTokens = body.staticTokens.map(String);
|
|
2935
4325
|
}
|
|
2936
4326
|
if (body.clients !== void 0) {
|
|
2937
4327
|
if (!Array.isArray(body.clients))
|
|
2938
|
-
return
|
|
2939
|
-
patch.clients = body.clients.filter(
|
|
4328
|
+
return adminError4(400, "clients: [{client_id, client_secret}]");
|
|
4329
|
+
patch.clients = body.clients.filter(isRecord6).map((c) => ({
|
|
2940
4330
|
client_id: String(c.client_id),
|
|
2941
4331
|
client_secret: String(c.client_secret)
|
|
2942
4332
|
}));
|
|
2943
4333
|
}
|
|
2944
4334
|
if (body.autoAdvance !== void 0) {
|
|
2945
4335
|
const parsed = parseAutoAdvance(body.autoAdvance);
|
|
2946
|
-
if (typeof parsed === "string") return
|
|
4336
|
+
if (typeof parsed === "string") return adminError4(400, parsed);
|
|
2947
4337
|
patch.autoAdvance = parsed;
|
|
2948
4338
|
}
|
|
2949
4339
|
if (body.unknownPresets !== void 0) {
|
|
2950
4340
|
if (body.unknownPresets !== "reject" && body.unknownPresets !== "accept") {
|
|
2951
|
-
return
|
|
4341
|
+
return adminError4(400, 'unknownPresets: "reject" | "accept"');
|
|
2952
4342
|
}
|
|
2953
4343
|
patch.unknownPresets = body.unknownPresets;
|
|
2954
4344
|
}
|
|
2955
|
-
return
|
|
4345
|
+
return json5(200, runtime.instance(namespace).state.update(patch));
|
|
2956
4346
|
},
|
|
2957
|
-
"POST /tick": ({ namespace }) =>
|
|
4347
|
+
"POST /tick": ({ namespace }) => json5(200, { applied: runtime.instance(namespace).tick() })
|
|
2958
4348
|
});
|
|
2959
4349
|
var createRuntime2 = (options = {}) => {
|
|
2960
4350
|
const { retryDelaysMs, fetch: send, ...endpoint } = options.webhooks ?? { url: "" };
|
|
@@ -3378,4 +4768,4 @@ export {
|
|
|
3378
4768
|
tokenCredential,
|
|
3379
4769
|
RxVortexAPI
|
|
3380
4770
|
};
|
|
3381
|
-
//# sourceMappingURL=chunk-
|
|
4771
|
+
//# sourceMappingURL=chunk-KQEUUCLU.js.map
|