@korajs/cli 0.0.0-canary-20261008174129
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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/bin.cjs +9465 -0
- package/dist/bin.cjs.map +1 -0
- package/dist/bin.js +3150 -0
- package/dist/bin.js.map +1 -0
- package/dist/chunk-5NI2FEQL.js +92 -0
- package/dist/chunk-5NI2FEQL.js.map +1 -0
- package/dist/chunk-6C4BHSRA.js +82 -0
- package/dist/chunk-6C4BHSRA.js.map +1 -0
- package/dist/chunk-7Y4J2GXP.js +796 -0
- package/dist/chunk-7Y4J2GXP.js.map +1 -0
- package/dist/chunk-BURFQVMV.js +719 -0
- package/dist/chunk-BURFQVMV.js.map +1 -0
- package/dist/chunk-TVIRMDER.js +2569 -0
- package/dist/chunk-TVIRMDER.js.map +1 -0
- package/dist/create.cjs +1368 -0
- package/dist/create.cjs.map +1 -0
- package/dist/create.js +11 -0
- package/dist/create.js.map +1 -0
- package/dist/index.cjs +3489 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +772 -0
- package/dist/index.d.ts +772 -0
- package/dist/index.js +129 -0
- package/dist/index.js.map +1 -0
- package/dist/lab-manager-KUDII6BT.js +280 -0
- package/dist/lab-manager-KUDII6BT.js.map +1 -0
- package/dist/schema-loader-GGHECMTM.js +9 -0
- package/dist/schema-loader-GGHECMTM.js.map +1 -0
- package/dist/spectator-manager-FTH2NFSE.js +145 -0
- package/dist/spectator-manager-FTH2NFSE.js.map +1 -0
- package/dist/studio-server-XLGZNGG5.js +1747 -0
- package/dist/studio-server-XLGZNGG5.js.map +1 -0
- package/dist/vite.cjs +351 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +161 -0
- package/dist/vite.d.ts +161 -0
- package/dist/vite.js +321 -0
- package/dist/vite.js.map +1 -0
- package/package.json +79 -0
- package/templates/react-basic/.env.example +3 -0
- package/templates/react-basic/AGENTS.md +87 -0
- package/templates/react-basic/README.md.hbs +62 -0
- package/templates/react-basic/index.html.hbs +12 -0
- package/templates/react-basic/kora.config.ts +13 -0
- package/templates/react-basic/package.json.hbs +35 -0
- package/templates/react-basic/src/App.tsx +132 -0
- package/templates/react-basic/src/index.css +287 -0
- package/templates/react-basic/src/kora-worker.ts +9 -0
- package/templates/react-basic/src/main.tsx +24 -0
- package/templates/react-basic/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/react-basic/src/modules/todos/todo.queries.ts +5 -0
- package/templates/react-basic/src/modules/todos/todo.schema.ts +10 -0
- package/templates/react-basic/src/modules/todos/useTodos.ts +31 -0
- package/templates/react-basic/src/schema.ts +9 -0
- package/templates/react-basic/src/vite-env.d.ts +18 -0
- package/templates/react-basic/tsconfig.json.hbs +14 -0
- package/templates/react-basic/vite.config.ts +88 -0
- package/templates/react-sync/.env.example +25 -0
- package/templates/react-sync/AGENTS.md +87 -0
- package/templates/react-sync/README.md.hbs +106 -0
- package/templates/react-sync/index.html.hbs +12 -0
- package/templates/react-sync/kora.config.ts +17 -0
- package/templates/react-sync/package.json.hbs +44 -0
- package/templates/react-sync/server.ts +141 -0
- package/templates/react-sync/src/App.tsx +160 -0
- package/templates/react-sync/src/auth.ts +25 -0
- package/templates/react-sync/src/index.css +350 -0
- package/templates/react-sync/src/kora-worker.ts +9 -0
- package/templates/react-sync/src/main.tsx +52 -0
- package/templates/react-sync/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/react-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/react-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/react-sync/src/modules/todos/useTodos.ts +31 -0
- package/templates/react-sync/src/schema.ts +9 -0
- package/templates/react-sync/src/vite-env.d.ts +19 -0
- package/templates/react-sync/tsconfig.json.hbs +14 -0
- package/templates/react-sync/vite.config.ts +105 -0
- package/templates/react-tailwind/.env.example +3 -0
- package/templates/react-tailwind/AGENTS.md +87 -0
- package/templates/react-tailwind/README.md.hbs +62 -0
- package/templates/react-tailwind/index.html.hbs +12 -0
- package/templates/react-tailwind/kora.config.ts +13 -0
- package/templates/react-tailwind/package.json.hbs +38 -0
- package/templates/react-tailwind/src/App.tsx +201 -0
- package/templates/react-tailwind/src/index.css +7 -0
- package/templates/react-tailwind/src/kora-worker.ts +9 -0
- package/templates/react-tailwind/src/main.tsx +31 -0
- package/templates/react-tailwind/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/react-tailwind/src/modules/todos/todo.queries.ts +5 -0
- package/templates/react-tailwind/src/modules/todos/todo.schema.ts +10 -0
- package/templates/react-tailwind/src/modules/todos/useTodos.ts +31 -0
- package/templates/react-tailwind/src/schema.ts +9 -0
- package/templates/react-tailwind/src/vite-env.d.ts +18 -0
- package/templates/react-tailwind/tsconfig.json.hbs +14 -0
- package/templates/react-tailwind/vite.config.ts +95 -0
- package/templates/react-tailwind-sync/.env.example +25 -0
- package/templates/react-tailwind-sync/AGENTS.md +89 -0
- package/templates/react-tailwind-sync/README.md.hbs +106 -0
- package/templates/react-tailwind-sync/index.html.hbs +12 -0
- package/templates/react-tailwind-sync/kora.config.ts +17 -0
- package/templates/react-tailwind-sync/package.json.hbs +47 -0
- package/templates/react-tailwind-sync/server.ts +141 -0
- package/templates/react-tailwind-sync/src/App.tsx +293 -0
- package/templates/react-tailwind-sync/src/auth.ts +25 -0
- package/templates/react-tailwind-sync/src/index.css +7 -0
- package/templates/react-tailwind-sync/src/kora-worker.ts +9 -0
- package/templates/react-tailwind-sync/src/kora.ts +32 -0
- package/templates/react-tailwind-sync/src/main.tsx +44 -0
- package/templates/react-tailwind-sync/src/modules/todos/todo.mutations.ts +21 -0
- package/templates/react-tailwind-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/react-tailwind-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/react-tailwind-sync/src/modules/todos/useTodos.ts +31 -0
- package/templates/react-tailwind-sync/src/schema.ts +9 -0
- package/templates/react-tailwind-sync/src/vite-env.d.ts +19 -0
- package/templates/react-tailwind-sync/tsconfig.json.hbs +14 -0
- package/templates/react-tailwind-sync/vite.config.ts +112 -0
- package/templates/svelte-basic/.env.example +3 -0
- package/templates/svelte-basic/AGENTS.md +76 -0
- package/templates/svelte-basic/README.md.hbs +62 -0
- package/templates/svelte-basic/index.html.hbs +12 -0
- package/templates/svelte-basic/kora.config.ts +12 -0
- package/templates/svelte-basic/package.json.hbs +33 -0
- package/templates/svelte-basic/src/App.svelte +128 -0
- package/templates/svelte-basic/src/Root.svelte +14 -0
- package/templates/svelte-basic/src/index.css +287 -0
- package/templates/svelte-basic/src/kora-worker.ts +9 -0
- package/templates/svelte-basic/src/main.ts +16 -0
- package/templates/svelte-basic/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/svelte-basic/src/modules/todos/todo.queries.ts +5 -0
- package/templates/svelte-basic/src/modules/todos/todo.schema.ts +10 -0
- package/templates/svelte-basic/src/modules/todos/useTodos.ts +25 -0
- package/templates/svelte-basic/src/schema.ts +9 -0
- package/templates/svelte-basic/src/vite-env.d.ts +12 -0
- package/templates/svelte-basic/svelte.config.js +5 -0
- package/templates/svelte-basic/tsconfig.json.hbs +14 -0
- package/templates/svelte-basic/vite.config.ts +57 -0
- package/templates/svelte-sync/.env.example +25 -0
- package/templates/svelte-sync/AGENTS.md +76 -0
- package/templates/svelte-sync/README.md.hbs +106 -0
- package/templates/svelte-sync/index.html.hbs +12 -0
- package/templates/svelte-sync/kora.config.ts +17 -0
- package/templates/svelte-sync/package.json.hbs +43 -0
- package/templates/svelte-sync/server.ts +141 -0
- package/templates/svelte-sync/src/App.svelte +151 -0
- package/templates/svelte-sync/src/Root.svelte +22 -0
- package/templates/svelte-sync/src/auth.ts +25 -0
- package/templates/svelte-sync/src/index.css +339 -0
- package/templates/svelte-sync/src/kora-worker.ts +9 -0
- package/templates/svelte-sync/src/main.ts +32 -0
- package/templates/svelte-sync/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/svelte-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/svelte-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/svelte-sync/src/modules/todos/useTodos.ts +25 -0
- package/templates/svelte-sync/src/schema.ts +9 -0
- package/templates/svelte-sync/src/vite-env.d.ts +22 -0
- package/templates/svelte-sync/svelte.config.js +5 -0
- package/templates/svelte-sync/tsconfig.json.hbs +14 -0
- package/templates/svelte-sync/vite.config.ts +91 -0
- package/templates/svelte-tailwind/AGENTS.md +76 -0
- package/templates/svelte-tailwind/README.md.hbs +62 -0
- package/templates/svelte-tailwind/index.html.hbs +12 -0
- package/templates/svelte-tailwind/kora.config.ts +12 -0
- package/templates/svelte-tailwind/package.json.hbs +36 -0
- package/templates/svelte-tailwind/src/App.svelte +76 -0
- package/templates/svelte-tailwind/src/Root.svelte +14 -0
- package/templates/svelte-tailwind/src/index.css +7 -0
- package/templates/svelte-tailwind/src/kora-worker.ts +9 -0
- package/templates/svelte-tailwind/src/main.ts +16 -0
- package/templates/svelte-tailwind/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/svelte-tailwind/src/modules/todos/todo.queries.ts +5 -0
- package/templates/svelte-tailwind/src/modules/todos/todo.schema.ts +10 -0
- package/templates/svelte-tailwind/src/modules/todos/useTodos.ts +25 -0
- package/templates/svelte-tailwind/src/schema.ts +9 -0
- package/templates/svelte-tailwind/src/vite-env.d.ts +12 -0
- package/templates/svelte-tailwind/svelte.config.js +5 -0
- package/templates/svelte-tailwind/tsconfig.json.hbs +14 -0
- package/templates/svelte-tailwind/vite.config.ts +64 -0
- package/templates/svelte-tailwind-sync/.env.example +25 -0
- package/templates/svelte-tailwind-sync/AGENTS.md +76 -0
- package/templates/svelte-tailwind-sync/README.md.hbs +106 -0
- package/templates/svelte-tailwind-sync/index.html.hbs +12 -0
- package/templates/svelte-tailwind-sync/kora.config.ts +17 -0
- package/templates/svelte-tailwind-sync/package.json.hbs +46 -0
- package/templates/svelte-tailwind-sync/server.ts +141 -0
- package/templates/svelte-tailwind-sync/src/App.svelte +193 -0
- package/templates/svelte-tailwind-sync/src/Root.svelte +26 -0
- package/templates/svelte-tailwind-sync/src/auth.ts +25 -0
- package/templates/svelte-tailwind-sync/src/index.css +7 -0
- package/templates/svelte-tailwind-sync/src/kora-worker.ts +9 -0
- package/templates/svelte-tailwind-sync/src/main.ts +32 -0
- package/templates/svelte-tailwind-sync/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/svelte-tailwind-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/svelte-tailwind-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/svelte-tailwind-sync/src/modules/todos/useTodos.ts +25 -0
- package/templates/svelte-tailwind-sync/src/schema.ts +9 -0
- package/templates/svelte-tailwind-sync/src/vite-env.d.ts +22 -0
- package/templates/svelte-tailwind-sync/svelte.config.js +5 -0
- package/templates/svelte-tailwind-sync/tsconfig.json.hbs +14 -0
- package/templates/svelte-tailwind-sync/vite.config.ts +98 -0
- package/templates/tauri-react/.env.example +27 -0
- package/templates/tauri-react/.github/workflows/release-desktop.yml +107 -0
- package/templates/tauri-react/AGENTS.md +91 -0
- package/templates/tauri-react/README.md.hbs +183 -0
- package/templates/tauri-react/dev.ts +37 -0
- package/templates/tauri-react/index.html.hbs +16 -0
- package/templates/tauri-react/kora.config.ts +17 -0
- package/templates/tauri-react/package.json.hbs +46 -0
- package/templates/tauri-react/server.ts +148 -0
- package/templates/tauri-react/src/App.tsx +803 -0
- package/templates/tauri-react/src/AppShell.tsx +158 -0
- package/templates/tauri-react/src/SetupScreen.tsx +288 -0
- package/templates/tauri-react/src/auth.ts +45 -0
- package/templates/tauri-react/src/main.tsx +9 -0
- package/templates/tauri-react/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/tauri-react/src/modules/todos/todo.queries.ts +5 -0
- package/templates/tauri-react/src/modules/todos/todo.schema.ts +10 -0
- package/templates/tauri-react/src/modules/todos/useTodos.ts +31 -0
- package/templates/tauri-react/src/schema.ts +9 -0
- package/templates/tauri-react/src/sync-config.ts +103 -0
- package/templates/tauri-react/src/updater.ts +38 -0
- package/templates/tauri-react/src/vite-env.d.ts +19 -0
- package/templates/tauri-react/src-tauri/Cargo.toml +19 -0
- package/templates/tauri-react/src-tauri/build.rs +3 -0
- package/templates/tauri-react/src-tauri/capabilities/default.json +12 -0
- package/templates/tauri-react/src-tauri/icons/128x128.png +0 -0
- package/templates/tauri-react/src-tauri/icons/128x128@2x.png +0 -0
- package/templates/tauri-react/src-tauri/icons/32x32.png +0 -0
- package/templates/tauri-react/src-tauri/icons/icon.icns +0 -0
- package/templates/tauri-react/src-tauri/icons/icon.ico +0 -0
- package/templates/tauri-react/src-tauri/src/lib.rs +8 -0
- package/templates/tauri-react/src-tauri/src/main.rs +6 -0
- package/templates/tauri-react/src-tauri/tauri.conf.json +44 -0
- package/templates/tauri-react/tsconfig.json.hbs +14 -0
- package/templates/tauri-react/vite.config.ts +15 -0
- package/templates/vue-basic/AGENTS.md +84 -0
- package/templates/vue-basic/README.md.hbs +62 -0
- package/templates/vue-basic/index.html.hbs +12 -0
- package/templates/vue-basic/kora.config.ts +12 -0
- package/templates/vue-basic/package.json.hbs +32 -0
- package/templates/vue-basic/src/App.vue +117 -0
- package/templates/vue-basic/src/index.css +287 -0
- package/templates/vue-basic/src/kora-worker.ts +9 -0
- package/templates/vue-basic/src/main.ts +20 -0
- package/templates/vue-basic/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/vue-basic/src/modules/todos/todo.queries.ts +5 -0
- package/templates/vue-basic/src/modules/todos/todo.schema.ts +10 -0
- package/templates/vue-basic/src/modules/todos/useTodos.ts +32 -0
- package/templates/vue-basic/src/schema.ts +9 -0
- package/templates/vue-basic/src/vite-env.d.ts +17 -0
- package/templates/vue-basic/tsconfig.json.hbs +15 -0
- package/templates/vue-basic/vite.config.ts +74 -0
- package/templates/vue-sync/.env.example +25 -0
- package/templates/vue-sync/AGENTS.md +84 -0
- package/templates/vue-sync/README.md.hbs +106 -0
- package/templates/vue-sync/index.html.hbs +12 -0
- package/templates/vue-sync/kora.config.ts +17 -0
- package/templates/vue-sync/package.json.hbs +42 -0
- package/templates/vue-sync/server.ts +141 -0
- package/templates/vue-sync/src/App.vue +135 -0
- package/templates/vue-sync/src/auth.ts +25 -0
- package/templates/vue-sync/src/index.css +339 -0
- package/templates/vue-sync/src/kora-worker.ts +9 -0
- package/templates/vue-sync/src/main.ts +47 -0
- package/templates/vue-sync/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/vue-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/vue-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/vue-sync/src/modules/todos/useTodos.ts +32 -0
- package/templates/vue-sync/src/schema.ts +9 -0
- package/templates/vue-sync/src/vite-env.d.ts +27 -0
- package/templates/vue-sync/tsconfig.json.hbs +15 -0
- package/templates/vue-sync/vite.config.ts +91 -0
- package/templates/vue-tailwind/AGENTS.md +84 -0
- package/templates/vue-tailwind/README.md.hbs +62 -0
- package/templates/vue-tailwind/index.html.hbs +12 -0
- package/templates/vue-tailwind/kora.config.ts +12 -0
- package/templates/vue-tailwind/package.json.hbs +35 -0
- package/templates/vue-tailwind/src/App.vue +135 -0
- package/templates/vue-tailwind/src/index.css +7 -0
- package/templates/vue-tailwind/src/kora-worker.ts +9 -0
- package/templates/vue-tailwind/src/main.ts +31 -0
- package/templates/vue-tailwind/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/vue-tailwind/src/modules/todos/todo.queries.ts +5 -0
- package/templates/vue-tailwind/src/modules/todos/todo.schema.ts +10 -0
- package/templates/vue-tailwind/src/modules/todos/useTodos.ts +32 -0
- package/templates/vue-tailwind/src/schema.ts +9 -0
- package/templates/vue-tailwind/src/vite-env.d.ts +17 -0
- package/templates/vue-tailwind/tsconfig.json.hbs +15 -0
- package/templates/vue-tailwind/vite.config.ts +75 -0
- package/templates/vue-tailwind-sync/.env.example +25 -0
- package/templates/vue-tailwind-sync/AGENTS.md +84 -0
- package/templates/vue-tailwind-sync/README.md.hbs +106 -0
- package/templates/vue-tailwind-sync/index.html.hbs +12 -0
- package/templates/vue-tailwind-sync/kora.config.ts +17 -0
- package/templates/vue-tailwind-sync/package.json.hbs +45 -0
- package/templates/vue-tailwind-sync/server.ts +141 -0
- package/templates/vue-tailwind-sync/src/App.vue +245 -0
- package/templates/vue-tailwind-sync/src/auth.ts +25 -0
- package/templates/vue-tailwind-sync/src/index.css +7 -0
- package/templates/vue-tailwind-sync/src/kora-worker.ts +9 -0
- package/templates/vue-tailwind-sync/src/main.ts +56 -0
- package/templates/vue-tailwind-sync/src/modules/todos/todo.mutations.ts +25 -0
- package/templates/vue-tailwind-sync/src/modules/todos/todo.queries.ts +5 -0
- package/templates/vue-tailwind-sync/src/modules/todos/todo.schema.ts +10 -0
- package/templates/vue-tailwind-sync/src/modules/todos/useTodos.ts +32 -0
- package/templates/vue-tailwind-sync/src/schema.ts +9 -0
- package/templates/vue-tailwind-sync/src/vite-env.d.ts +27 -0
- package/templates/vue-tailwind-sync/tsconfig.json.hbs +15 -0
- package/templates/vue-tailwind-sync/vite.config.ts +92 -0
|
@@ -0,0 +1,1747 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/commands/studio/studio-server.ts
|
|
4
|
+
import { createServer } from "http";
|
|
5
|
+
|
|
6
|
+
// src/commands/studio/db-reader.ts
|
|
7
|
+
var KORA_TABLE_PREFIX = "_kora_";
|
|
8
|
+
var OPS_TABLE_PREFIX = "_kora_ops_";
|
|
9
|
+
var META_COLUMNS = /* @__PURE__ */ new Set([
|
|
10
|
+
"id",
|
|
11
|
+
"_created_at",
|
|
12
|
+
"_updated_at",
|
|
13
|
+
"_version",
|
|
14
|
+
"_field_versions",
|
|
15
|
+
"_deleted"
|
|
16
|
+
]);
|
|
17
|
+
function parseVersionStamp(raw) {
|
|
18
|
+
if (typeof raw !== "string" || raw.length === 0) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const parts = raw.split(":");
|
|
22
|
+
if (parts.length < 3) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const wallTime = Number.parseInt(parts[0] ?? "", 10);
|
|
26
|
+
const logical = Number.parseInt(parts[1] ?? "", 10);
|
|
27
|
+
if (Number.isNaN(wallTime) || Number.isNaN(logical)) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return { wallTime, logical, nodeId: parts.slice(2).join(":") };
|
|
31
|
+
}
|
|
32
|
+
function parseFieldVersionsColumn(raw) {
|
|
33
|
+
if (typeof raw !== "string" || raw.length === 0) {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(raw);
|
|
38
|
+
const result = {};
|
|
39
|
+
for (const [field, value] of Object.entries(parsed)) {
|
|
40
|
+
const version = parseVersionStamp(value);
|
|
41
|
+
if (version) {
|
|
42
|
+
result[field] = version;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
} catch {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function safeIdentifier(name) {
|
|
51
|
+
if (!/^[a-zA-Z0-9_]+$/.test(name)) {
|
|
52
|
+
throw new Error(`Unsafe SQL identifier: "${name}"`);
|
|
53
|
+
}
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
function parseJsonColumn(raw) {
|
|
57
|
+
if (typeof raw !== "string" || raw.length === 0) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(raw);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function stripOpMetadata(data) {
|
|
67
|
+
if (data === null) return null;
|
|
68
|
+
const fields = {};
|
|
69
|
+
for (const [key, value] of Object.entries(data)) {
|
|
70
|
+
if (key.startsWith("__kora_") && key.endsWith("__")) continue;
|
|
71
|
+
fields[key] = value;
|
|
72
|
+
}
|
|
73
|
+
return Object.keys(fields).length > 0 ? fields : null;
|
|
74
|
+
}
|
|
75
|
+
function parseJsonArrayColumn(raw) {
|
|
76
|
+
if (typeof raw !== "string" || raw.length === 0) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const parsed = JSON.parse(raw);
|
|
81
|
+
return Array.isArray(parsed) ? parsed.map(String) : [];
|
|
82
|
+
} catch {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
var StudioDbReader = class _StudioDbReader {
|
|
87
|
+
constructor(db, dbPath) {
|
|
88
|
+
this.db = db;
|
|
89
|
+
this.dbPath = dbPath;
|
|
90
|
+
}
|
|
91
|
+
db;
|
|
92
|
+
dbPath;
|
|
93
|
+
/**
|
|
94
|
+
* Open a Kora database strictly read-only.
|
|
95
|
+
* @throws when the file does not exist or is not a Kora database
|
|
96
|
+
*/
|
|
97
|
+
static async open(dbPath) {
|
|
98
|
+
let Database;
|
|
99
|
+
try {
|
|
100
|
+
Database = (await import("better-sqlite3")).default;
|
|
101
|
+
} catch {
|
|
102
|
+
throw new Error(
|
|
103
|
+
'Kora Studio needs the "better-sqlite3" package to open database files. Install it in your project: pnpm add -D better-sqlite3'
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const db = new Database(dbPath, { readonly: true, fileMustExist: true });
|
|
107
|
+
const reader = new _StudioDbReader(db, dbPath);
|
|
108
|
+
if (reader.listCollections().length === 0 && !reader.hasKoraTables()) {
|
|
109
|
+
db.close();
|
|
110
|
+
throw new Error(`"${dbPath}" does not look like a Kora database (no _kora_* tables found).`);
|
|
111
|
+
}
|
|
112
|
+
return reader;
|
|
113
|
+
}
|
|
114
|
+
close() {
|
|
115
|
+
this.db.close();
|
|
116
|
+
}
|
|
117
|
+
hasKoraTables() {
|
|
118
|
+
const row = this.db.prepare("SELECT COUNT(*) as count FROM sqlite_master WHERE type='table' AND name LIKE ?").get(`${KORA_TABLE_PREFIX}%`);
|
|
119
|
+
return row.count > 0;
|
|
120
|
+
}
|
|
121
|
+
/** Collections = tables that have a companion _kora_ops_<name> table. */
|
|
122
|
+
listCollections() {
|
|
123
|
+
const tables = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").all();
|
|
124
|
+
const names = new Set(tables.map((t) => t.name));
|
|
125
|
+
const collections = [];
|
|
126
|
+
for (const { name } of tables) {
|
|
127
|
+
if (name.startsWith(OPS_TABLE_PREFIX)) {
|
|
128
|
+
const collection = name.slice(OPS_TABLE_PREFIX.length);
|
|
129
|
+
if (names.has(collection)) {
|
|
130
|
+
collections.push(collection);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return collections.sort();
|
|
135
|
+
}
|
|
136
|
+
overview() {
|
|
137
|
+
const collections = this.listCollections().map((name) => {
|
|
138
|
+
const table = safeIdentifier(name);
|
|
139
|
+
const live = this.db.prepare(`SELECT COUNT(*) as count FROM ${table} WHERE _deleted = 0`).get();
|
|
140
|
+
const tombstones = this.db.prepare(`SELECT COUNT(*) as count FROM ${table} WHERE _deleted = 1`).get();
|
|
141
|
+
const operations = this.db.prepare(`SELECT COUNT(*) as count FROM ${OPS_TABLE_PREFIX}${table}`).get();
|
|
142
|
+
const columnRows = this.db.prepare(`PRAGMA table_info(${table})`).all();
|
|
143
|
+
return {
|
|
144
|
+
name,
|
|
145
|
+
liveRecords: live.count,
|
|
146
|
+
tombstones: tombstones.count,
|
|
147
|
+
operations: operations.count,
|
|
148
|
+
columns: columnRows.map((c) => c.name).filter((c) => !META_COLUMNS.has(c))
|
|
149
|
+
};
|
|
150
|
+
});
|
|
151
|
+
const versionVector = this.tableExists("_kora_version_vector") ? this.db.prepare(
|
|
152
|
+
"SELECT node_id as nodeId, sequence_number as sequenceNumber FROM _kora_version_vector ORDER BY node_id"
|
|
153
|
+
).all() : [];
|
|
154
|
+
const meta = this.tableExists("_kora_meta") ? this.db.prepare("SELECT key, value FROM _kora_meta ORDER BY key").all() : [];
|
|
155
|
+
const pendingSyncOps = this.tableExists("_kora_sync_queue") ? this.db.prepare("SELECT COUNT(*) as count FROM _kora_sync_queue").get().count : 0;
|
|
156
|
+
const auditTraces = this.tableExists("_kora_audit_traces") ? this.db.prepare("SELECT COUNT(*) as count FROM _kora_audit_traces").get().count : 0;
|
|
157
|
+
return { dbPath: this.dbPath, collections, versionVector, meta, pendingSyncOps, auditTraces };
|
|
158
|
+
}
|
|
159
|
+
records(collection, options = {}) {
|
|
160
|
+
const table = safeIdentifier(collection);
|
|
161
|
+
const limit = Math.min(Math.max(options.limit ?? 50, 1), 500);
|
|
162
|
+
const offset = Math.max(options.offset ?? 0, 0);
|
|
163
|
+
const clauses = [];
|
|
164
|
+
const params = [];
|
|
165
|
+
if (!options.includeDeleted) {
|
|
166
|
+
clauses.push("_deleted = 0");
|
|
167
|
+
}
|
|
168
|
+
if (options.search && options.search.trim().length > 0) {
|
|
169
|
+
const textColumns = this.db.prepare(`PRAGMA table_info(${table})`).all().filter((c) => c.type.toUpperCase().includes("TEXT") || c.name === "id");
|
|
170
|
+
const term = `%${options.search.trim()}%`;
|
|
171
|
+
const likes = textColumns.map((c) => {
|
|
172
|
+
params.push(term);
|
|
173
|
+
return `${safeIdentifier(c.name)} LIKE ?`;
|
|
174
|
+
});
|
|
175
|
+
if (likes.length > 0) {
|
|
176
|
+
clauses.push(`(${likes.join(" OR ")})`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const where = clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "";
|
|
180
|
+
const total = this.db.prepare(`SELECT COUNT(*) as count FROM ${table} ${where}`).get(...params).count;
|
|
181
|
+
const rows = this.db.prepare(`SELECT * FROM ${table} ${where} ORDER BY _updated_at DESC LIMIT ? OFFSET ?`).all(...params, limit, offset);
|
|
182
|
+
return { records: rows.map((row) => this.toStudioRecord(row)), total };
|
|
183
|
+
}
|
|
184
|
+
/** Full operation log for one collection (capped), for replay and the DAG. */
|
|
185
|
+
allOperations(collection, cap = 5e3) {
|
|
186
|
+
const table = safeIdentifier(collection);
|
|
187
|
+
const rows = this.db.prepare(`SELECT * FROM ${OPS_TABLE_PREFIX}${table} ORDER BY timestamp ASC LIMIT ?`).all(Math.min(Math.max(cap, 1), 2e4));
|
|
188
|
+
return rows.map((row) => this.toStudioOperation(row));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Cheap change fingerprint for live updates. SQLite's `data_version` PRAGMA
|
|
192
|
+
* increments whenever ANOTHER connection commits — exactly what a read-only
|
|
193
|
+
* watcher needs to know "something changed, refetch".
|
|
194
|
+
*/
|
|
195
|
+
fingerprint() {
|
|
196
|
+
const row = this.db.prepare("PRAGMA data_version").get();
|
|
197
|
+
return String(row.data_version);
|
|
198
|
+
}
|
|
199
|
+
/** Raw value of a single column on a record (for richtext preview decode). */
|
|
200
|
+
rawFieldValue(collection, recordId, field) {
|
|
201
|
+
const table = safeIdentifier(collection);
|
|
202
|
+
const column = safeIdentifier(field);
|
|
203
|
+
const row = this.db.prepare(`SELECT ${column} as value FROM ${table} WHERE id = ?`).get(recordId);
|
|
204
|
+
return row?.value;
|
|
205
|
+
}
|
|
206
|
+
record(collection, recordId) {
|
|
207
|
+
const table = safeIdentifier(collection);
|
|
208
|
+
const row = this.db.prepare(`SELECT * FROM ${table} WHERE id = ?`).get(recordId);
|
|
209
|
+
return row ? this.toStudioRecord(row) : null;
|
|
210
|
+
}
|
|
211
|
+
/** Full operation history for one record, newest first. */
|
|
212
|
+
recordOperations(collection, recordId) {
|
|
213
|
+
const table = safeIdentifier(collection);
|
|
214
|
+
const rows = this.db.prepare(
|
|
215
|
+
`SELECT * FROM ${OPS_TABLE_PREFIX}${table} WHERE record_id = ? ORDER BY timestamp DESC`
|
|
216
|
+
).all(recordId);
|
|
217
|
+
return rows.map((row) => this.toStudioOperation(row));
|
|
218
|
+
}
|
|
219
|
+
operations(collection, options = {}) {
|
|
220
|
+
const table = safeIdentifier(collection);
|
|
221
|
+
const limit = Math.min(Math.max(options.limit ?? 50, 1), 500);
|
|
222
|
+
const offset = Math.max(options.offset ?? 0, 0);
|
|
223
|
+
const total = this.db.prepare(`SELECT COUNT(*) as count FROM ${OPS_TABLE_PREFIX}${table}`).get().count;
|
|
224
|
+
const rows = this.db.prepare(`SELECT * FROM ${OPS_TABLE_PREFIX}${table} ORDER BY timestamp DESC LIMIT ? OFFSET ?`).all(limit, offset);
|
|
225
|
+
return { operations: rows.map((row) => this.toStudioOperation(row)), total };
|
|
226
|
+
}
|
|
227
|
+
auditTraces(limit = 100) {
|
|
228
|
+
if (!this.tableExists("_kora_audit_traces")) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const rows = this.db.prepare(
|
|
232
|
+
`SELECT id, recorded_at, event_type, collection, record_id, field, strategy, tier, constraint_name
|
|
233
|
+
FROM _kora_audit_traces ORDER BY recorded_at DESC LIMIT ?`
|
|
234
|
+
).all(Math.min(Math.max(limit, 1), 500));
|
|
235
|
+
return rows.map((row) => ({
|
|
236
|
+
id: String(row.id),
|
|
237
|
+
recordedAt: Number(row.recorded_at),
|
|
238
|
+
eventType: String(row.event_type),
|
|
239
|
+
collection: String(row.collection),
|
|
240
|
+
recordId: String(row.record_id),
|
|
241
|
+
field: String(row.field),
|
|
242
|
+
strategy: String(row.strategy),
|
|
243
|
+
tier: Number(row.tier),
|
|
244
|
+
constraintName: row.constraint_name === null ? null : String(row.constraint_name)
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
tableExists(name) {
|
|
248
|
+
const row = this.db.prepare("SELECT COUNT(*) as count FROM sqlite_master WHERE type='table' AND name = ?").get(name);
|
|
249
|
+
return row.count > 0;
|
|
250
|
+
}
|
|
251
|
+
toStudioRecord(row) {
|
|
252
|
+
const fields = {};
|
|
253
|
+
for (const [key, value] of Object.entries(row)) {
|
|
254
|
+
if (!META_COLUMNS.has(key)) {
|
|
255
|
+
fields[key] = value instanceof Uint8Array || Buffer.isBuffer(value) ? `<binary ${value.byteLength} bytes>` : value;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
id: String(row.id),
|
|
260
|
+
fields,
|
|
261
|
+
createdAt: Number(row._created_at),
|
|
262
|
+
updatedAt: Number(row._updated_at),
|
|
263
|
+
deleted: row._deleted === 1,
|
|
264
|
+
version: parseVersionStamp(row._version),
|
|
265
|
+
fieldVersions: parseFieldVersionsColumn(row._field_versions)
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
toStudioOperation(row) {
|
|
269
|
+
return {
|
|
270
|
+
id: String(row.id),
|
|
271
|
+
nodeId: String(row.node_id),
|
|
272
|
+
type: String(row.type),
|
|
273
|
+
recordId: String(row.record_id),
|
|
274
|
+
data: stripOpMetadata(parseJsonColumn(row.data)),
|
|
275
|
+
previousData: parseJsonColumn(row.previous_data),
|
|
276
|
+
timestamp: parseVersionStamp(row.timestamp),
|
|
277
|
+
sequenceNumber: Number(row.sequence_number),
|
|
278
|
+
causalDeps: parseJsonArrayColumn(row.causal_deps),
|
|
279
|
+
schemaVersion: Number(row.schema_version)
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/commands/studio/studio-replay.ts
|
|
285
|
+
function compareVersion(a, b) {
|
|
286
|
+
if (!a || !b) {
|
|
287
|
+
return a === b ? 0 : a ? 1 : -1;
|
|
288
|
+
}
|
|
289
|
+
if (a.wallTime !== b.wallTime) {
|
|
290
|
+
return a.wallTime - b.wallTime;
|
|
291
|
+
}
|
|
292
|
+
if (a.logical !== b.logical) {
|
|
293
|
+
return a.logical - b.logical;
|
|
294
|
+
}
|
|
295
|
+
return a.nodeId < b.nodeId ? -1 : a.nodeId > b.nodeId ? 1 : 0;
|
|
296
|
+
}
|
|
297
|
+
function sortByHlc(ops) {
|
|
298
|
+
return [...ops].sort((a, b) => compareVersion(a.timestamp, b.timestamp));
|
|
299
|
+
}
|
|
300
|
+
function replayToOperation(ops, upToOpId) {
|
|
301
|
+
const ordered = sortByHlc(ops);
|
|
302
|
+
let cutIndex = ordered.length - 1;
|
|
303
|
+
if (upToOpId !== null) {
|
|
304
|
+
const found = ordered.findIndex((o) => o.id === upToOpId);
|
|
305
|
+
if (found === -1) {
|
|
306
|
+
throw new Error(`Operation "${upToOpId}" not found in the log`);
|
|
307
|
+
}
|
|
308
|
+
cutIndex = found;
|
|
309
|
+
}
|
|
310
|
+
const records = /* @__PURE__ */ new Map();
|
|
311
|
+
for (let i = 0; i <= cutIndex; i++) {
|
|
312
|
+
const operation = ordered[i];
|
|
313
|
+
if (!operation) {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
const writer = operation.timestamp ? shortNode(operation.timestamp.nodeId) : "?";
|
|
317
|
+
if (operation.type === "insert" && operation.data) {
|
|
318
|
+
const writers = {};
|
|
319
|
+
for (const field of Object.keys(operation.data)) {
|
|
320
|
+
writers[field] = writer;
|
|
321
|
+
}
|
|
322
|
+
records.set(operation.recordId, {
|
|
323
|
+
fields: { ...operation.data },
|
|
324
|
+
deleted: false,
|
|
325
|
+
writers
|
|
326
|
+
});
|
|
327
|
+
} else if (operation.type === "update" && operation.data) {
|
|
328
|
+
const existing = records.get(operation.recordId);
|
|
329
|
+
if (existing) {
|
|
330
|
+
for (const [field, value] of Object.entries(operation.data)) {
|
|
331
|
+
existing.fields[field] = value;
|
|
332
|
+
existing.writers[field] = writer;
|
|
333
|
+
}
|
|
334
|
+
} else {
|
|
335
|
+
const writers = {};
|
|
336
|
+
for (const field of Object.keys(operation.data)) {
|
|
337
|
+
writers[field] = writer;
|
|
338
|
+
}
|
|
339
|
+
records.set(operation.recordId, {
|
|
340
|
+
fields: { ...operation.data },
|
|
341
|
+
deleted: false,
|
|
342
|
+
writers
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
} else if (operation.type === "delete") {
|
|
346
|
+
const existing = records.get(operation.recordId);
|
|
347
|
+
if (existing) {
|
|
348
|
+
existing.deleted = true;
|
|
349
|
+
} else {
|
|
350
|
+
records.set(operation.recordId, { fields: {}, deleted: true, writers: {} });
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
records: [...records.entries()].map(([id, r]) => ({
|
|
356
|
+
id,
|
|
357
|
+
fields: r.fields,
|
|
358
|
+
deleted: r.deleted,
|
|
359
|
+
lastWriterByField: r.writers
|
|
360
|
+
})),
|
|
361
|
+
appliedCount: cutIndex + 1,
|
|
362
|
+
totalCount: ordered.length,
|
|
363
|
+
cutOperation: ordered[cutIndex] ?? null
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function buildCausalDag(ops) {
|
|
367
|
+
const ordered = sortByHlc(ops);
|
|
368
|
+
const laneByNode = /* @__PURE__ */ new Map();
|
|
369
|
+
const lanes = [];
|
|
370
|
+
for (const operation of ordered) {
|
|
371
|
+
if (!laneByNode.has(operation.nodeId)) {
|
|
372
|
+
laneByNode.set(operation.nodeId, lanes.length);
|
|
373
|
+
lanes.push({ nodeId: operation.nodeId, shortNodeId: shortNode(operation.nodeId) });
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const idSet = new Set(ordered.map((o) => o.id));
|
|
377
|
+
const nodes = ordered.map((operation, index) => ({
|
|
378
|
+
id: operation.id,
|
|
379
|
+
type: operation.type,
|
|
380
|
+
recordId: operation.recordId,
|
|
381
|
+
nodeId: operation.nodeId,
|
|
382
|
+
shortNodeId: shortNode(operation.nodeId),
|
|
383
|
+
sequenceNumber: operation.sequenceNumber,
|
|
384
|
+
wallTime: operation.timestamp?.wallTime ?? 0,
|
|
385
|
+
x: index,
|
|
386
|
+
lane: laneByNode.get(operation.nodeId) ?? 0,
|
|
387
|
+
dataPreview: operation.data ? JSON.stringify(operation.data).slice(0, 80) : ""
|
|
388
|
+
}));
|
|
389
|
+
const edges = [];
|
|
390
|
+
for (const operation of ordered) {
|
|
391
|
+
for (const dep of operation.causalDeps) {
|
|
392
|
+
if (idSet.has(dep)) {
|
|
393
|
+
edges.push({ from: dep, to: operation.id });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return { nodes, edges, lanes };
|
|
398
|
+
}
|
|
399
|
+
function shortNode(nodeId) {
|
|
400
|
+
return nodeId.length > 8 ? nodeId.slice(-8) : nodeId;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// src/commands/studio/studio-ui.ts
|
|
404
|
+
var STUDIO_HTML = `<!DOCTYPE html>
|
|
405
|
+
<html lang="en">
|
|
406
|
+
<head>
|
|
407
|
+
<meta charset="utf-8" />
|
|
408
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
409
|
+
<title>Kora Studio</title>
|
|
410
|
+
<link rel="stylesheet" href="/style.css" />
|
|
411
|
+
</head>
|
|
412
|
+
<body>
|
|
413
|
+
<header>
|
|
414
|
+
<div class="logo">kora <span>studio</span></div>
|
|
415
|
+
<div class="modebadge" id="modebadge"></div>
|
|
416
|
+
<div class="dbpath mono" id="dbpath"></div>
|
|
417
|
+
<div class="live" id="live"><div class="dot"></div><span>live</span></div>
|
|
418
|
+
<button id="refresh">Refresh</button>
|
|
419
|
+
</header>
|
|
420
|
+
<div class="tabs" id="tabs"></div>
|
|
421
|
+
<div class="layout">
|
|
422
|
+
<nav class="sidebar" id="sidebar"></nav>
|
|
423
|
+
<main id="main"><div class="empty">Loading\u2026</div></main>
|
|
424
|
+
</div>
|
|
425
|
+
<div class="drawer" id="drawer"></div>
|
|
426
|
+
<div class="dag-tip" id="dagtip"></div>
|
|
427
|
+
<script src="/app.js"></script>
|
|
428
|
+
</body>
|
|
429
|
+
</html>
|
|
430
|
+
`;
|
|
431
|
+
|
|
432
|
+
// src/commands/studio/studio-ui-app.ts
|
|
433
|
+
var STUDIO_APP_JS = `
|
|
434
|
+
(function () {
|
|
435
|
+
'use strict';
|
|
436
|
+
|
|
437
|
+
// \u2500\u2500 State \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
438
|
+
var state = {
|
|
439
|
+
mode: 'file', // 'file' | 'lab'
|
|
440
|
+
tab: 'data', // data | ops | timetravel | merges | sync | lab
|
|
441
|
+
device: null, // selected lab device for data-ish tabs
|
|
442
|
+
devices: [],
|
|
443
|
+
collection: null,
|
|
444
|
+
page: 0,
|
|
445
|
+
includeDeleted: false,
|
|
446
|
+
search: '',
|
|
447
|
+
overview: null,
|
|
448
|
+
ttPosition: null, // time-travel slider position
|
|
449
|
+
ttPlaying: null,
|
|
450
|
+
labState: null,
|
|
451
|
+
feed: [],
|
|
452
|
+
deviceColors: {},
|
|
453
|
+
live: false
|
|
454
|
+
};
|
|
455
|
+
var PAGE = 50;
|
|
456
|
+
var refreshTimer = null;
|
|
457
|
+
|
|
458
|
+
// \u2500\u2500 Utilities \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
459
|
+
function qs(sel) { return document.querySelector(sel); }
|
|
460
|
+
function api(path) {
|
|
461
|
+
var sep = path.indexOf('?') === -1 ? '?' : '&';
|
|
462
|
+
var url = state.device ? path + sep + 'device=' + encodeURIComponent(state.device) : path;
|
|
463
|
+
return fetch(url).then(function (r) {
|
|
464
|
+
if (!r.ok) { return r.json().then(function (b) { throw new Error(b.error || r.statusText); }); }
|
|
465
|
+
return r.json();
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
function post(path, body) {
|
|
469
|
+
return fetch(path, {
|
|
470
|
+
method: 'POST',
|
|
471
|
+
headers: { 'content-type': 'application/json' },
|
|
472
|
+
body: JSON.stringify(body || {})
|
|
473
|
+
}).then(function (r) {
|
|
474
|
+
if (!r.ok) { return r.json().then(function (b) { throw new Error(b.error || r.statusText); }); }
|
|
475
|
+
return r.json();
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
function el(tag, attrs, children) {
|
|
479
|
+
var node = document.createElement(tag);
|
|
480
|
+
if (attrs) { Object.keys(attrs).forEach(function (k) {
|
|
481
|
+
if (k === 'class') node.className = attrs[k];
|
|
482
|
+
else if (k === 'text') node.textContent = attrs[k];
|
|
483
|
+
else if (k === 'html') node.innerHTML = attrs[k];
|
|
484
|
+
else if (k.slice(0, 2) === 'on') node.addEventListener(k.slice(2), attrs[k]);
|
|
485
|
+
else node.setAttribute(k, attrs[k]);
|
|
486
|
+
}); }
|
|
487
|
+
(children || []).forEach(function (c) { if (c) node.appendChild(c); });
|
|
488
|
+
return node;
|
|
489
|
+
}
|
|
490
|
+
function fmtTime(ms) {
|
|
491
|
+
if (!ms) return '\xB7';
|
|
492
|
+
var d = new Date(ms);
|
|
493
|
+
return d.toLocaleTimeString() + '.' + ('00' + d.getMilliseconds()).slice(-3);
|
|
494
|
+
}
|
|
495
|
+
function fmtDateTime(ms) {
|
|
496
|
+
if (!ms) return '\xB7';
|
|
497
|
+
var d = new Date(ms);
|
|
498
|
+
return d.toLocaleDateString() + ' ' + d.toLocaleTimeString();
|
|
499
|
+
}
|
|
500
|
+
function shortNode(id) { return id && id.length > 8 ? id.slice(-8) : (id || '\xB7'); }
|
|
501
|
+
function shortId(id) { return id && id.length > 13 ? id.slice(0, 13) + '\u2026' : (id || ''); }
|
|
502
|
+
function fmtVal(v) {
|
|
503
|
+
if (v === null || v === undefined) return 'null';
|
|
504
|
+
if (typeof v === 'object') return JSON.stringify(v);
|
|
505
|
+
return String(v);
|
|
506
|
+
}
|
|
507
|
+
function deviceColor(name) {
|
|
508
|
+
if (!(name in state.deviceColors)) {
|
|
509
|
+
state.deviceColors[name] = 'dev-c' + (Object.keys(state.deviceColors).length % 6);
|
|
510
|
+
}
|
|
511
|
+
return state.deviceColors[name];
|
|
512
|
+
}
|
|
513
|
+
function debounceRefresh() {
|
|
514
|
+
if (refreshTimer) clearTimeout(refreshTimer);
|
|
515
|
+
refreshTimer = setTimeout(function () { render(false); }, 250);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// \u2500\u2500 Boot + live events \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
519
|
+
fetch('/api/mode').then(function (r) { return r.json(); }).then(function (info) {
|
|
520
|
+
state.mode = info.mode;
|
|
521
|
+
state.devices = info.devices || [];
|
|
522
|
+
if (state.mode === 'lab') { state.tab = 'lab'; state.device = state.devices[0] || null; }
|
|
523
|
+
qs('#modebadge').textContent = state.mode;
|
|
524
|
+
if (info.spectator) {
|
|
525
|
+
qs('#dbpath').textContent = '\u21C4 ' + info.spectator.url +
|
|
526
|
+
(info.spectator.connected ? ' \xB7 connected' : ' \xB7 connecting\u2026') +
|
|
527
|
+
' \xB7 ' + info.spectator.operationsReceived + ' ops received';
|
|
528
|
+
}
|
|
529
|
+
connectEvents();
|
|
530
|
+
render(true);
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
function connectEvents() {
|
|
534
|
+
var source = new EventSource('/api/events');
|
|
535
|
+
source.addEventListener('hello', function () {
|
|
536
|
+
state.live = true; qs('#live').className = 'live on';
|
|
537
|
+
});
|
|
538
|
+
source.addEventListener('change', function () {
|
|
539
|
+
if (state.tab !== 'timetravel') debounceRefresh();
|
|
540
|
+
});
|
|
541
|
+
source.addEventListener('lab', function (event) {
|
|
542
|
+
var data = JSON.parse(event.data);
|
|
543
|
+
state.feed.push(data);
|
|
544
|
+
if (state.feed.length > 400) state.feed.shift();
|
|
545
|
+
appendFeedLine(data);
|
|
546
|
+
});
|
|
547
|
+
source.addEventListener('spectator', function (event) {
|
|
548
|
+
var data = JSON.parse(event.data);
|
|
549
|
+
data.device = 'server';
|
|
550
|
+
state.feed.push(data);
|
|
551
|
+
if (state.feed.length > 400) state.feed.shift();
|
|
552
|
+
appendFeedLine(data);
|
|
553
|
+
// Keep the header status fresh as ops stream in.
|
|
554
|
+
fetch('/api/spectator/status').then(function (r) { return r.json(); }).then(function (s) {
|
|
555
|
+
qs('#dbpath').textContent = '\u21C4 ' + s.url +
|
|
556
|
+
(s.connected ? ' \xB7 connected' : ' \xB7 reconnecting\u2026') +
|
|
557
|
+
' \xB7 ' + s.operationsReceived + ' ops received';
|
|
558
|
+
}).catch(function () {});
|
|
559
|
+
});
|
|
560
|
+
source.onerror = function () {
|
|
561
|
+
state.live = false; qs('#live').className = 'live';
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// \u2500\u2500 Tabs + chrome \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
566
|
+
function renderTabs() {
|
|
567
|
+
var tabs = qs('#tabs');
|
|
568
|
+
tabs.textContent = '';
|
|
569
|
+
var defs = [];
|
|
570
|
+
if (state.mode === 'lab') defs.push(['lab', 'Lab']);
|
|
571
|
+
defs.push(['data', 'Data'], ['ops', 'Operations'], ['timetravel', 'Time Travel'], ['merges', 'Merges'], ['sync', 'Sync']);
|
|
572
|
+
defs.forEach(function (d) {
|
|
573
|
+
tabs.appendChild(el('div', {
|
|
574
|
+
class: 'tab' + (state.tab === d[0] ? ' active' : ''),
|
|
575
|
+
text: d[1],
|
|
576
|
+
onclick: function () { state.tab = d[0]; state.page = 0; closeDrawer(); render(true); }
|
|
577
|
+
}));
|
|
578
|
+
});
|
|
579
|
+
tabs.appendChild(el('div', { class: 'spacer' }));
|
|
580
|
+
if (state.mode === 'lab' && state.tab !== 'lab') {
|
|
581
|
+
var pick = el('select', { onchange: function () { state.device = pick.value; state.page = 0; render(true); } },
|
|
582
|
+
state.devices.map(function (d) {
|
|
583
|
+
var o = el('option', { value: d, text: d });
|
|
584
|
+
if (d === state.device) o.selected = true;
|
|
585
|
+
return o;
|
|
586
|
+
}));
|
|
587
|
+
tabs.appendChild(el('div', { class: 'devicepick' }, [el('span', { text: 'device:' }), pick]));
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function renderSidebar() {
|
|
592
|
+
var nav = qs('#sidebar');
|
|
593
|
+
nav.textContent = '';
|
|
594
|
+
if (state.tab === 'lab' || state.tab === 'sync' || state.tab === 'merges') {
|
|
595
|
+
nav.style.display = 'none';
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
nav.style.display = '';
|
|
599
|
+
nav.appendChild(el('h3', { text: 'Collections' }));
|
|
600
|
+
(state.overview ? state.overview.collections : []).forEach(function (c) {
|
|
601
|
+
nav.appendChild(el('div', {
|
|
602
|
+
class: 'item' + (state.collection === c.name ? ' active' : ''),
|
|
603
|
+
onclick: function () { state.collection = c.name; state.page = 0; closeDrawer(); render(false); }
|
|
604
|
+
}, [
|
|
605
|
+
el('span', { text: c.name }),
|
|
606
|
+
el('span', { class: 'count', text: String(c.liveRecords) + (c.tombstones ? ' +' + c.tombstones + '\u2020' : '') })
|
|
607
|
+
]));
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// \u2500\u2500 Main render \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
612
|
+
function render(full) {
|
|
613
|
+
renderTabs();
|
|
614
|
+
if (state.tab === 'lab') { renderSidebar(); renderLab(full); return; }
|
|
615
|
+
api('/api/overview').then(function (overview) {
|
|
616
|
+
state.overview = overview;
|
|
617
|
+
qs('#dbpath').textContent = overview.dbPath || '';
|
|
618
|
+
if (!state.collection || !overview.collections.some(function (c) { return c.name === state.collection; })) {
|
|
619
|
+
state.collection = overview.collections.length ? overview.collections[0].name : null;
|
|
620
|
+
}
|
|
621
|
+
renderSidebar();
|
|
622
|
+
var main = qs('#main');
|
|
623
|
+
main.textContent = '';
|
|
624
|
+
if (state.tab === 'data') return renderData(main);
|
|
625
|
+
if (state.tab === 'ops') return renderOps(main);
|
|
626
|
+
if (state.tab === 'timetravel') return renderTimeTravel(main);
|
|
627
|
+
if (state.tab === 'merges') return renderMerges(main);
|
|
628
|
+
if (state.tab === 'sync') return renderSync(main);
|
|
629
|
+
}).catch(function (e) {
|
|
630
|
+
qs('#main').innerHTML = '<div class="error">' + e.message + '</div>';
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function toolbar(main, title, extra) {
|
|
635
|
+
main.appendChild(el('div', { class: 'toolbar' }, [el('h2', { text: title })].concat(extra || [])));
|
|
636
|
+
}
|
|
637
|
+
function pager(main, total) {
|
|
638
|
+
var pages = Math.max(1, Math.ceil(total / PAGE));
|
|
639
|
+
main.appendChild(el('div', { class: 'pager' }, [
|
|
640
|
+
el('button', { text: '\u2039 Prev', onclick: function () { if (state.page > 0) { state.page--; render(false); } } }),
|
|
641
|
+
el('span', { text: 'page ' + (state.page + 1) + ' / ' + pages + ' \xB7 ' + total + ' total' }),
|
|
642
|
+
el('button', { text: 'Next \u203A', onclick: function () { if (state.page < pages - 1) { state.page++; render(false); } } })
|
|
643
|
+
]));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// \u2500\u2500 Data tab \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
647
|
+
function renderData(main) {
|
|
648
|
+
if (!state.collection) { main.appendChild(el('div', { class: 'empty', text: 'No collections.' })); return; }
|
|
649
|
+
var searchBox = el('input', { type: 'text', placeholder: 'search\u2026', value: state.search });
|
|
650
|
+
searchBox.oninput = function () { state.search = searchBox.value; state.page = 0; debounceRefresh(); };
|
|
651
|
+
var deletedToggle = el('label', {}, [
|
|
652
|
+
(function () { var cb = el('input', { type: 'checkbox' }); cb.checked = state.includeDeleted;
|
|
653
|
+
cb.onchange = function () { state.includeDeleted = cb.checked; state.page = 0; render(false); }; return cb; })(),
|
|
654
|
+
el('span', { text: 'tombstones' })
|
|
655
|
+
]);
|
|
656
|
+
toolbar(main, state.collection, [searchBox, deletedToggle]);
|
|
657
|
+
|
|
658
|
+
var q = '/api/collections/' + state.collection + '/records?limit=' + PAGE +
|
|
659
|
+
'&offset=' + (state.page * PAGE) + '&includeDeleted=' + state.includeDeleted +
|
|
660
|
+
(state.search ? '&search=' + encodeURIComponent(state.search) : '');
|
|
661
|
+
api(q).then(function (data) {
|
|
662
|
+
var info = state.overview.collections.filter(function (c) { return c.name === state.collection; })[0];
|
|
663
|
+
var columns = info ? info.columns : [];
|
|
664
|
+
if (!data.records.length) { main.appendChild(el('div', { class: 'empty', text: 'No records.' })); return; }
|
|
665
|
+
var thead = el('tr', {}, [el('th', { text: 'id' })].concat(
|
|
666
|
+
columns.map(function (c) { return el('th', { text: c }); }),
|
|
667
|
+
[el('th', { text: 'updated' })]
|
|
668
|
+
));
|
|
669
|
+
var tbody = el('tbody', {}, data.records.map(function (r) {
|
|
670
|
+
return el('tr', { class: 'clickable' + (r.deleted ? ' tombstone' : ''), onclick: function () { openRecord(r.id); } },
|
|
671
|
+
[el('td', { class: 'mono', text: shortId(r.id) })].concat(
|
|
672
|
+
columns.map(function (c) { return el('td', { text: fmtVal(r.fields[c]).slice(0, 60) }); }),
|
|
673
|
+
[el('td', { class: 'mono', text: fmtDateTime(r.updatedAt) })]
|
|
674
|
+
));
|
|
675
|
+
}));
|
|
676
|
+
main.appendChild(el('table', { class: 'grid' }, [el('thead', {}, [thead]), tbody]));
|
|
677
|
+
pager(main, data.total);
|
|
678
|
+
}).catch(showError(main));
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// \u2500\u2500 Record drawer \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
682
|
+
function openRecord(id) {
|
|
683
|
+
api('/api/collections/' + state.collection + '/records/' + encodeURIComponent(id)).then(function (data) {
|
|
684
|
+
var drawer = qs('#drawer');
|
|
685
|
+
drawer.textContent = '';
|
|
686
|
+
var r = data.record;
|
|
687
|
+
drawer.appendChild(el('h3', {}, [
|
|
688
|
+
el('span', { class: 'mono', text: r.id }),
|
|
689
|
+
el('span', { class: 'close', text: '\u2715', onclick: closeDrawer })
|
|
690
|
+
]));
|
|
691
|
+
if (r.deleted) drawer.appendChild(el('span', { class: 'badge delete', text: 'tombstone' }));
|
|
692
|
+
|
|
693
|
+
drawer.appendChild(el('h4', { text: 'Fields \u2014 with last writer per field' }));
|
|
694
|
+
Object.keys(r.fields).forEach(function (f) {
|
|
695
|
+
var fv = r.fieldVersions[f];
|
|
696
|
+
var val = fmtVal(r.fields[f]);
|
|
697
|
+
var preview = data.richtextPreviews && data.richtextPreviews[f];
|
|
698
|
+
drawer.appendChild(el('div', { class: 'fieldrow' }, [
|
|
699
|
+
el('div', { class: 'fname mono', text: f }),
|
|
700
|
+
el('div', { class: 'fval', text: val }),
|
|
701
|
+
preview ? el('div', { class: 'preview', text: '\u201C' + preview + '\u201D' }) : null,
|
|
702
|
+
fv ? el('span', { class: 'chip' }, [
|
|
703
|
+
el('span', { text: 'last writer' }),
|
|
704
|
+
el('b', { class: 'mono', text: shortNode(fv.nodeId) }),
|
|
705
|
+
el('span', { class: 'mono', text: fmtDateTime(fv.wallTime) })
|
|
706
|
+
]) : el('span', { class: 'chip', text: 'no field version' })
|
|
707
|
+
]));
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
drawer.appendChild(el('h4', { text: 'Causal graph for this record' }));
|
|
711
|
+
var dagBox = el('div', { class: 'dagwrap' });
|
|
712
|
+
drawer.appendChild(dagBox);
|
|
713
|
+
api('/api/collections/' + state.collection + '/dag?record=' + encodeURIComponent(id)).then(function (dag) {
|
|
714
|
+
renderDag(dagBox, dag, 500);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
drawer.appendChild(el('h4', { text: 'Operation history (newest first)' }));
|
|
718
|
+
if (!data.operations.length) drawer.appendChild(el('div', { class: 'empty', text: 'No operations (compacted?).' }));
|
|
719
|
+
data.operations.forEach(function (o) {
|
|
720
|
+
var body = el('div', { class: 'op ' + o.type }, [
|
|
721
|
+
el('div', {}, [
|
|
722
|
+
el('span', { class: 'badge ' + o.type, text: o.type }),
|
|
723
|
+
el('span', { class: 'meta', text: ' ' + shortNode(o.nodeId) + ' \xB7 seq ' + o.sequenceNumber + ' \xB7 ' + (o.timestamp ? fmtDateTime(o.timestamp.wallTime) : '\xB7') })
|
|
724
|
+
])
|
|
725
|
+
]);
|
|
726
|
+
if (o.data) {
|
|
727
|
+
var pre = el('pre', { class: 'mono' });
|
|
728
|
+
pre.textContent = JSON.stringify(o.data, null, 1);
|
|
729
|
+
body.appendChild(pre);
|
|
730
|
+
}
|
|
731
|
+
drawer.appendChild(body);
|
|
732
|
+
});
|
|
733
|
+
drawer.className = 'drawer open';
|
|
734
|
+
}).catch(function (e) { alertError(e); });
|
|
735
|
+
}
|
|
736
|
+
function closeDrawer() { qs('#drawer').className = 'drawer'; }
|
|
737
|
+
|
|
738
|
+
// \u2500\u2500 Operations tab (DAG + table) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
739
|
+
function renderOps(main) {
|
|
740
|
+
if (!state.collection) { main.appendChild(el('div', { class: 'empty', text: 'No collections.' })); return; }
|
|
741
|
+
toolbar(main, state.collection + ' \u2014 operation log');
|
|
742
|
+
var dagBox = el('div', { class: 'dagwrap' });
|
|
743
|
+
main.appendChild(dagBox);
|
|
744
|
+
api('/api/collections/' + state.collection + '/dag?limit=120').then(function (dag) {
|
|
745
|
+
renderDag(dagBox, dag, null);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
api('/api/collections/' + state.collection + '/ops?limit=' + PAGE + '&offset=' + (state.page * PAGE)).then(function (data) {
|
|
749
|
+
if (!data.operations.length) { main.appendChild(el('div', { class: 'empty', text: 'Empty (compacted?).' })); return; }
|
|
750
|
+
var thead = el('tr', {}, ['type', 'record', 'node', 'seq', 'time', 'data'].map(function (h) { return el('th', { text: h }); }));
|
|
751
|
+
var tbody = el('tbody', {}, data.operations.map(function (o) {
|
|
752
|
+
return el('tr', { class: 'clickable', onclick: function () { openRecord(o.recordId); } }, [
|
|
753
|
+
el('td', {}, [el('span', { class: 'badge ' + o.type, text: o.type })]),
|
|
754
|
+
el('td', { class: 'mono', text: shortId(o.recordId) }),
|
|
755
|
+
el('td', { class: 'mono', text: shortNode(o.nodeId) }),
|
|
756
|
+
el('td', { class: 'mono', text: String(o.sequenceNumber) }),
|
|
757
|
+
el('td', { class: 'mono', text: o.timestamp ? fmtDateTime(o.timestamp.wallTime) : '\xB7' }),
|
|
758
|
+
el('td', { class: 'mono', text: o.data ? JSON.stringify(o.data).slice(0, 70) : '\xB7' })
|
|
759
|
+
]);
|
|
760
|
+
}));
|
|
761
|
+
main.appendChild(el('table', { class: 'grid' }, [el('thead', {}, [thead]), tbody]));
|
|
762
|
+
pager(main, data.total);
|
|
763
|
+
}).catch(showError(main));
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// \u2500\u2500 Causal DAG renderer (SVG) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
767
|
+
function renderDag(container, dag, maxWidth) {
|
|
768
|
+
container.textContent = '';
|
|
769
|
+
if (!dag.nodes.length) { container.appendChild(el('div', { class: 'empty', text: 'No operations to graph.' })); return; }
|
|
770
|
+
var XSTEP = 46, YSTEP = 44, PADX = 90, PADY = 26, R = 7;
|
|
771
|
+
var width = PADX + dag.nodes.length * XSTEP + 30;
|
|
772
|
+
var height = PADY + dag.lanes.length * YSTEP + 16;
|
|
773
|
+
var svgns = 'http://www.w3.org/2000/svg';
|
|
774
|
+
var svg = document.createElementNS(svgns, 'svg');
|
|
775
|
+
svg.setAttribute('width', String(width));
|
|
776
|
+
svg.setAttribute('height', String(height));
|
|
777
|
+
|
|
778
|
+
var defs = document.createElementNS(svgns, 'defs');
|
|
779
|
+
defs.innerHTML = '<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path d="M 0 0 L 10 5 L 0 10 z" fill="#3a4356"/></marker>';
|
|
780
|
+
svg.appendChild(defs);
|
|
781
|
+
|
|
782
|
+
var colors = { insert: '#48bb78', update: '#4299e1', delete: '#f56565' };
|
|
783
|
+
var pos = {};
|
|
784
|
+
dag.nodes.forEach(function (n) {
|
|
785
|
+
pos[n.id] = { x: PADX + n.x * XSTEP, y: PADY + n.lane * YSTEP + YSTEP / 2 };
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
dag.lanes.forEach(function (lane, i) {
|
|
789
|
+
var y = PADY + i * YSTEP + YSTEP / 2;
|
|
790
|
+
var line = document.createElementNS(svgns, 'line');
|
|
791
|
+
line.setAttribute('x1', '10'); line.setAttribute('x2', String(width - 10));
|
|
792
|
+
line.setAttribute('y1', String(y)); line.setAttribute('y2', String(y));
|
|
793
|
+
line.setAttribute('class', 'dag-lane-line');
|
|
794
|
+
svg.appendChild(line);
|
|
795
|
+
var label = document.createElementNS(svgns, 'text');
|
|
796
|
+
label.setAttribute('x', '10'); label.setAttribute('y', String(y - 8));
|
|
797
|
+
label.setAttribute('class', 'dag-lane-label');
|
|
798
|
+
label.textContent = lane.shortNodeId;
|
|
799
|
+
svg.appendChild(label);
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
dag.edges.forEach(function (e) {
|
|
803
|
+
var a = pos[e.from], b = pos[e.to];
|
|
804
|
+
if (!a || !b) return;
|
|
805
|
+
var path = document.createElementNS(svgns, 'path');
|
|
806
|
+
var midX = (a.x + b.x) / 2;
|
|
807
|
+
path.setAttribute('d', 'M ' + a.x + ' ' + a.y + ' C ' + midX + ' ' + a.y + ', ' + midX + ' ' + b.y + ', ' + (b.x - R - 2) + ' ' + b.y);
|
|
808
|
+
path.setAttribute('class', 'dag-edge');
|
|
809
|
+
svg.appendChild(path);
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
var tip = qs('#dagtip');
|
|
813
|
+
dag.nodes.forEach(function (n) {
|
|
814
|
+
var g = document.createElementNS(svgns, 'g');
|
|
815
|
+
g.setAttribute('class', 'dag-node');
|
|
816
|
+
var c = document.createElementNS(svgns, 'circle');
|
|
817
|
+
var p = pos[n.id];
|
|
818
|
+
c.setAttribute('cx', String(p.x)); c.setAttribute('cy', String(p.y)); c.setAttribute('r', String(R));
|
|
819
|
+
c.setAttribute('fill', '#161a22');
|
|
820
|
+
c.setAttribute('stroke', colors[n.type] || '#8b93a7');
|
|
821
|
+
g.appendChild(c);
|
|
822
|
+
g.addEventListener('mousemove', function (ev) {
|
|
823
|
+
tip.style.display = 'block';
|
|
824
|
+
tip.style.left = (ev.clientX + 14) + 'px';
|
|
825
|
+
tip.style.top = (ev.clientY + 14) + 'px';
|
|
826
|
+
tip.innerHTML = '<b>' + n.type + '</b> by ' + n.shortNodeId + ' \xB7 seq ' + n.sequenceNumber +
|
|
827
|
+
'<br/>record ' + shortId(n.recordId) +
|
|
828
|
+
(n.dataPreview ? '<br/><span class="mono">' + escapeHtml(n.dataPreview) + '</span>' : '');
|
|
829
|
+
});
|
|
830
|
+
g.addEventListener('mouseleave', function () { tip.style.display = 'none'; });
|
|
831
|
+
g.addEventListener('click', function () { openRecord(n.recordId); });
|
|
832
|
+
svg.appendChild(g);
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
if (maxWidth) container.style.maxWidth = maxWidth + 'px';
|
|
836
|
+
container.appendChild(svg);
|
|
837
|
+
container.scrollLeft = width;
|
|
838
|
+
}
|
|
839
|
+
function escapeHtml(s) {
|
|
840
|
+
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// \u2500\u2500 Time travel tab \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
844
|
+
function renderTimeTravel(main) {
|
|
845
|
+
if (!state.collection) { main.appendChild(el('div', { class: 'empty', text: 'No collections.' })); return; }
|
|
846
|
+
toolbar(main, state.collection + ' \u2014 time travel');
|
|
847
|
+
api('/api/collections/' + state.collection + '/replay').then(function (full) {
|
|
848
|
+
var total = full.totalCount;
|
|
849
|
+
if (!total) { main.appendChild(el('div', { class: 'empty', text: 'No operations to replay.' })); return; }
|
|
850
|
+
if (state.ttPosition === null || state.ttPosition > total) state.ttPosition = total;
|
|
851
|
+
|
|
852
|
+
var posLabel = el('span', { class: 'mono', text: '' });
|
|
853
|
+
var slider = el('input', { type: 'range', min: '1', max: String(total), value: String(state.ttPosition) });
|
|
854
|
+
var playBtn = el('button', { text: '\u25B6 replay' });
|
|
855
|
+
var cutBox = el('div', { class: 'tt-cut mono' });
|
|
856
|
+
var stateBox = el('div', {});
|
|
857
|
+
|
|
858
|
+
function show(position) {
|
|
859
|
+
state.ttPosition = position;
|
|
860
|
+
posLabel.textContent = position + ' / ' + total + ' ops';
|
|
861
|
+
api('/api/collections/' + state.collection + '/replay?upTo=' + encodeURIComponent(opIdAt(position))).then(function (cut) {
|
|
862
|
+
var o = cut.cutOperation;
|
|
863
|
+
cutBox.innerHTML = o
|
|
864
|
+
? 'cut at <b>' + o.type + '</b> by ' + shortNode(o.nodeId) + ' \xB7 ' + (o.timestamp ? fmtDateTime(o.timestamp.wallTime) : '') +
|
|
865
|
+
(o.data ? ' \xB7 ' + escapeHtml(JSON.stringify(o.data).slice(0, 90)) : '')
|
|
866
|
+
: '';
|
|
867
|
+
renderReplayState(stateBox, cut);
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
// The full replay result orders ops by HLC; fetch the ordered id list once.
|
|
871
|
+
var orderedIds = [];
|
|
872
|
+
api('/api/collections/' + state.collection + '/dag?limit=20000').then(function (dag) {
|
|
873
|
+
orderedIds = dag.nodes.map(function (n) { return n.id; });
|
|
874
|
+
show(Math.min(state.ttPosition, orderedIds.length));
|
|
875
|
+
});
|
|
876
|
+
function opIdAt(position) { return orderedIds[position - 1]; }
|
|
877
|
+
|
|
878
|
+
slider.oninput = function () { show(Number(slider.value)); };
|
|
879
|
+
playBtn.onclick = function () {
|
|
880
|
+
if (state.ttPlaying) { clearInterval(state.ttPlaying); state.ttPlaying = null; playBtn.textContent = '\u25B6 replay'; return; }
|
|
881
|
+
var pos = 1;
|
|
882
|
+
playBtn.textContent = '\u23F8 pause';
|
|
883
|
+
state.ttPlaying = setInterval(function () {
|
|
884
|
+
if (pos > orderedIds.length) { clearInterval(state.ttPlaying); state.ttPlaying = null; playBtn.textContent = '\u25B6 replay'; return; }
|
|
885
|
+
slider.value = String(pos);
|
|
886
|
+
show(pos);
|
|
887
|
+
pos++;
|
|
888
|
+
}, 350);
|
|
889
|
+
};
|
|
890
|
+
|
|
891
|
+
main.appendChild(el('div', { class: 'tt-controls' }, [playBtn, slider, posLabel]));
|
|
892
|
+
main.appendChild(cutBox);
|
|
893
|
+
main.appendChild(stateBox);
|
|
894
|
+
}).catch(showError(main));
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
function renderReplayState(box, cut) {
|
|
898
|
+
box.textContent = '';
|
|
899
|
+
if (!cut.records.length) { box.appendChild(el('div', { class: 'empty', text: 'No records at this point.' })); return; }
|
|
900
|
+
var fieldSet = {};
|
|
901
|
+
cut.records.forEach(function (r) { Object.keys(r.fields).forEach(function (f) { fieldSet[f] = true; }); });
|
|
902
|
+
var fields = Object.keys(fieldSet);
|
|
903
|
+
var thead = el('tr', {}, [el('th', { text: 'id' })].concat(fields.map(function (f) { return el('th', { text: f }); })));
|
|
904
|
+
var tbody = el('tbody', {}, cut.records.map(function (r) {
|
|
905
|
+
return el('tr', { class: r.deleted ? 'tombstone' : '' },
|
|
906
|
+
[el('td', { class: 'mono', text: shortId(r.id) })].concat(fields.map(function (f) {
|
|
907
|
+
return el('td', { text: r.fields[f] === undefined ? '' : fmtVal(r.fields[f]).slice(0, 40) });
|
|
908
|
+
})));
|
|
909
|
+
}));
|
|
910
|
+
box.appendChild(el('table', { class: 'grid' }, [el('thead', {}, [thead]), tbody]));
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// \u2500\u2500 Merges tab \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
914
|
+
function renderMerges(main) {
|
|
915
|
+
toolbar(main, 'Merge audit trail');
|
|
916
|
+
api('/api/audit?limit=200').then(function (data) {
|
|
917
|
+
if (!data.traces.length) { main.appendChild(el('div', { class: 'empty', text: 'No merge audit traces recorded.' })); return; }
|
|
918
|
+
var thead = el('tr', {}, ['when', 'event', 'collection', 'record', 'field', 'strategy', 'tier'].map(function (h) { return el('th', { text: h }); }));
|
|
919
|
+
var tbody = el('tbody', {}, data.traces.map(function (t) {
|
|
920
|
+
return el('tr', {}, [
|
|
921
|
+
el('td', { class: 'mono', text: fmtDateTime(t.recordedAt) }),
|
|
922
|
+
el('td', { text: t.eventType }),
|
|
923
|
+
el('td', { text: t.collection }),
|
|
924
|
+
el('td', { class: 'mono', text: shortId(t.recordId) }),
|
|
925
|
+
el('td', { text: t.field }),
|
|
926
|
+
el('td', { text: t.strategy }),
|
|
927
|
+
el('td', { text: String(t.tier) })
|
|
928
|
+
]);
|
|
929
|
+
}));
|
|
930
|
+
main.appendChild(el('table', { class: 'grid' }, [el('thead', {}, [thead]), tbody]));
|
|
931
|
+
}).catch(showError(main));
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// \u2500\u2500 Sync tab \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
935
|
+
function renderSync(main) {
|
|
936
|
+
toolbar(main, 'Sync state' + (state.device ? ' \u2014 ' + state.device : ''));
|
|
937
|
+
var o = state.overview;
|
|
938
|
+
main.appendChild(el('div', { class: 'panel' }, [
|
|
939
|
+
el('h3', { text: 'Version vector \u2014 what this store has seen from each node' }),
|
|
940
|
+
el('table', { class: 'grid' }, [
|
|
941
|
+
el('thead', {}, [el('tr', {}, [el('th', { text: 'node' }), el('th', { text: 'max sequence' })])]),
|
|
942
|
+
el('tbody', {}, o.versionVector.map(function (v) {
|
|
943
|
+
return el('tr', {}, [el('td', { class: 'mono', text: v.nodeId }), el('td', { class: 'mono', text: String(v.sequenceNumber) })]);
|
|
944
|
+
}))
|
|
945
|
+
])
|
|
946
|
+
]));
|
|
947
|
+
main.appendChild(el('div', { class: 'panel' }, [
|
|
948
|
+
el('h3', { text: 'Outbound queue' }),
|
|
949
|
+
el('div', { text: o.pendingSyncOps + ' operation(s) waiting to sync' })
|
|
950
|
+
]));
|
|
951
|
+
main.appendChild(el('div', { class: 'panel' }, [
|
|
952
|
+
el('h3', { text: 'Store meta' }),
|
|
953
|
+
el('table', { class: 'grid' }, [
|
|
954
|
+
el('tbody', {}, o.meta.map(function (m) {
|
|
955
|
+
return el('tr', {}, [el('td', { class: 'mono', text: m.key }), el('td', { class: 'mono', text: m.value })]);
|
|
956
|
+
}))
|
|
957
|
+
])
|
|
958
|
+
]));
|
|
959
|
+
if (state.mode === 'spectator') {
|
|
960
|
+
var feedBox = el('div', { class: 'lab-feed', style: 'width:100%;max-height:340px' }, [
|
|
961
|
+
el('h3', { text: 'Live events from the server' }),
|
|
962
|
+
el('div', { class: 'feed', id: 'feed' })
|
|
963
|
+
]);
|
|
964
|
+
main.appendChild(feedBox);
|
|
965
|
+
state.feed.slice(-200).forEach(appendFeedLine);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// \u2500\u2500 Lab tab \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
970
|
+
function renderLab(full) {
|
|
971
|
+
var main = qs('#main');
|
|
972
|
+
qs('#sidebar').style.display = 'none';
|
|
973
|
+
Promise.all([fetch('/api/lab/state').then(function (r) { return r.json(); }),
|
|
974
|
+
fetch('/api/lab/convergence').then(function (r) { return r.json(); })])
|
|
975
|
+
.then(function (results) {
|
|
976
|
+
state.labState = results[0];
|
|
977
|
+
state.devices = results[0].devices.map(function (d) { return d.name; });
|
|
978
|
+
var convergence = results[1];
|
|
979
|
+
main.textContent = '';
|
|
980
|
+
qs('#dbpath').textContent = 'sync laboratory \xB7 ' + results[0].serverOperations + ' ops on server';
|
|
981
|
+
|
|
982
|
+
var conv = el('div', { class: 'convergence ' + (convergence.converged ? 'ok' : 'bad') }, [
|
|
983
|
+
el('div', { text: convergence.converged
|
|
984
|
+
? '\u2713 ALL ' + convergence.deviceCount + ' DEVICES CONVERGED'
|
|
985
|
+
: '\u2717 DEVICES DIVERGED (' + convergence.differences.length + ' difference(s)) \u2014 sync to converge' })
|
|
986
|
+
]);
|
|
987
|
+
if (!convergence.converged) {
|
|
988
|
+
conv.appendChild(el('div', { class: 'diffs', text: convergence.differences.slice(0, 4).join(' \xB7 ') }));
|
|
989
|
+
}
|
|
990
|
+
var addBtn = el('button', { class: 'primary', text: '+ add device', onclick: function () {
|
|
991
|
+
post('/api/lab/devices', {}).then(function () { render(true); }).catch(alertError);
|
|
992
|
+
} });
|
|
993
|
+
var syncAllBtn = el('button', { text: 'sync all', onclick: function () {
|
|
994
|
+
var chain = Promise.resolve();
|
|
995
|
+
state.devices.forEach(function (d) { chain = chain.then(function () { return post('/api/lab/devices/' + d + '/sync', {}); }); });
|
|
996
|
+
chain.then(function () { return post('/api/lab/devices/' + state.devices[0] + '/sync', {}); })
|
|
997
|
+
.then(function () { render(true); }).catch(alertError);
|
|
998
|
+
} });
|
|
999
|
+
main.appendChild(el('div', { class: 'lab-top' }, [conv, addBtn, syncAllBtn]));
|
|
1000
|
+
|
|
1001
|
+
var devicesBox = el('div', { class: 'lab-devices' });
|
|
1002
|
+
results[0].devices.forEach(function (d) { devicesBox.appendChild(deviceCard(d, results[0].collections)); });
|
|
1003
|
+
|
|
1004
|
+
var feedBox = el('div', { class: 'lab-feed' }, [
|
|
1005
|
+
el('h3', { text: 'Live events' }),
|
|
1006
|
+
el('div', { class: 'feed', id: 'feed' })
|
|
1007
|
+
]);
|
|
1008
|
+
main.appendChild(el('div', { class: 'lab-grid' }, [devicesBox, feedBox]));
|
|
1009
|
+
state.feed.slice(-200).forEach(appendFeedLine);
|
|
1010
|
+
})
|
|
1011
|
+
.catch(showError(qs('#main')));
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
function deviceCard(d, collections) {
|
|
1015
|
+
var collection = collections[0];
|
|
1016
|
+
var card = el('div', { class: 'device-card' + (d.connected ? '' : ' offline') });
|
|
1017
|
+
card.appendChild(el('div', { class: 'device-head' }, [
|
|
1018
|
+
el('span', { class: 'name ' + deviceColor(d.name), text: d.name }),
|
|
1019
|
+
el('span', { class: 'node mono', text: shortNode(d.nodeId) }),
|
|
1020
|
+
el('span', { class: 'badge ' + (d.connected ? 'ok' : 'bad'), text: d.connected ? 'online' : 'offline' }),
|
|
1021
|
+
d.pendingOperations ? el('span', { class: 'badge neutral', text: d.pendingOperations + ' queued' }) : null,
|
|
1022
|
+
el('span', { class: 'spacer' }),
|
|
1023
|
+
el('button', { text: d.connected ? 'disconnect' : 'connect', onclick: function () {
|
|
1024
|
+
post('/api/lab/devices/' + d.name + '/' + (d.connected ? 'disconnect' : 'connect'), {})
|
|
1025
|
+
.then(function () { render(true); }).catch(alertError);
|
|
1026
|
+
} }),
|
|
1027
|
+
el('button', { text: 'sync', onclick: function () {
|
|
1028
|
+
post('/api/lab/devices/' + d.name + '/sync', {}).then(function () { render(true); }).catch(alertError);
|
|
1029
|
+
} })
|
|
1030
|
+
]));
|
|
1031
|
+
|
|
1032
|
+
var body = el('div', { class: 'device-body' });
|
|
1033
|
+
card.appendChild(body);
|
|
1034
|
+
|
|
1035
|
+
// Chaos controls
|
|
1036
|
+
body.appendChild(el('h5', { text: 'Network chaos (applies on next connect)' }));
|
|
1037
|
+
var chaosDefs = [['dropRate', 'drop'], ['duplicateRate', 'duplicate'], ['reorderRate', 'reorder']];
|
|
1038
|
+
chaosDefs.forEach(function (cd) {
|
|
1039
|
+
var key = cd[0];
|
|
1040
|
+
var valueLabel = el('span', { class: 'mono', text: String(d.chaos[key]) });
|
|
1041
|
+
var range = el('input', { type: 'range', min: '0', max: '0.5', step: '0.05', value: String(d.chaos[key]) });
|
|
1042
|
+
range.oninput = function () { valueLabel.textContent = range.value; };
|
|
1043
|
+
range.onchange = function () {
|
|
1044
|
+
var body2 = {}; body2[key] = Number(range.value);
|
|
1045
|
+
post('/api/lab/devices/' + d.name + '/chaos', body2).catch(alertError);
|
|
1046
|
+
};
|
|
1047
|
+
body.appendChild(el('div', { class: 'chaos-row' }, [el('span', { text: cd[1] }), range, valueLabel]));
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
// Records with inline editing
|
|
1051
|
+
body.appendChild(el('h5', { text: 'records (' + collection.name + ') \u2014 edits happen ON this device' }));
|
|
1052
|
+
var recBox = el('div', {});
|
|
1053
|
+
body.appendChild(recBox);
|
|
1054
|
+
api('/api/collections/' + collection.name + '/records?limit=20&device=' + encodeURIComponent(d.name)).then(function (data) {
|
|
1055
|
+
data.records.forEach(function (r) {
|
|
1056
|
+
var titleInput = el('input', { type: 'text', value: String(r.fields.title || '') });
|
|
1057
|
+
titleInput.onchange = function () {
|
|
1058
|
+
post('/api/lab/devices/' + d.name + '/update', { collection: collection.name, id: r.id, data: { title: titleInput.value } }).catch(alertError);
|
|
1059
|
+
};
|
|
1060
|
+
var doneCb = el('input', { type: 'checkbox', title: 'done' });
|
|
1061
|
+
doneCb.checked = !!r.fields.done;
|
|
1062
|
+
doneCb.onchange = function () {
|
|
1063
|
+
post('/api/lab/devices/' + d.name + '/update', { collection: collection.name, id: r.id, data: { done: doneCb.checked } }).catch(alertError);
|
|
1064
|
+
};
|
|
1065
|
+
var incBtn = el('button', { text: '+1', title: 'atomic increment \u2014 composes across devices', onclick: function () {
|
|
1066
|
+
post('/api/lab/devices/' + d.name + '/update', { collection: collection.name, id: r.id, data: {}, increments: { points: 1 } })
|
|
1067
|
+
.then(function () { debounceRefresh(); }).catch(alertError);
|
|
1068
|
+
} });
|
|
1069
|
+
var delBtn = el('button', { class: 'danger', text: '\u2715', title: 'delete', onclick: function () {
|
|
1070
|
+
post('/api/lab/devices/' + d.name + '/delete', { collection: collection.name, id: r.id })
|
|
1071
|
+
.then(function () { debounceRefresh(); }).catch(alertError);
|
|
1072
|
+
} });
|
|
1073
|
+
recBox.appendChild(el('div', { class: 'rec-line' }, [
|
|
1074
|
+
doneCb, titleInput,
|
|
1075
|
+
el('span', { class: 'pts mono', text: String(r.fields.points === undefined ? '' : r.fields.points) }),
|
|
1076
|
+
incBtn, delBtn,
|
|
1077
|
+
el('button', { text: '\u2315', title: 'inspect', onclick: function () { state.device = d.name; state.collection = collection.name; openRecord(r.id); } })
|
|
1078
|
+
]));
|
|
1079
|
+
});
|
|
1080
|
+
if (!data.records.length) recBox.appendChild(el('div', { class: 'empty', text: 'no records yet' }));
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
// Insert form
|
|
1084
|
+
var newTitle = el('input', { type: 'text', placeholder: 'new task title\u2026' });
|
|
1085
|
+
var insertBtn = el('button', { class: 'primary', text: 'insert', onclick: function () {
|
|
1086
|
+
if (!newTitle.value.trim()) return;
|
|
1087
|
+
post('/api/lab/devices/' + d.name + '/insert', { collection: collection.name, data: { title: newTitle.value.trim() } })
|
|
1088
|
+
.then(function () { newTitle.value = ''; debounceRefresh(); }).catch(alertError);
|
|
1089
|
+
} });
|
|
1090
|
+
newTitle.addEventListener('keydown', function (e) { if (e.key === 'Enter') insertBtn.click(); });
|
|
1091
|
+
body.appendChild(el('div', { class: 'insert-form' }, [newTitle, insertBtn]));
|
|
1092
|
+
return card;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
function appendFeedLine(event) {
|
|
1096
|
+
var feed = qs('#feed');
|
|
1097
|
+
if (!feed) return;
|
|
1098
|
+
var cls = 'feed-line';
|
|
1099
|
+
if (event.type === 'operation:created') cls += ' evt-created';
|
|
1100
|
+
if (event.type === 'operation:applied') cls += ' evt-applied';
|
|
1101
|
+
if (event.type === 'merge:conflict') cls += ' evt-conflict';
|
|
1102
|
+
if (event.type === 'sync:apply-failed') cls += ' evt-failed';
|
|
1103
|
+
feed.appendChild(el('div', { class: cls }, [
|
|
1104
|
+
el('span', { class: 'mono', text: fmtTime(event.at) }),
|
|
1105
|
+
el('span', { class: 'dev ' + deviceColor(event.device), text: event.device }),
|
|
1106
|
+
el('span', { class: 'what', text: event.type.replace('operation:', '').replace('sync:', '') + ' \xB7 ' + event.summary })
|
|
1107
|
+
]));
|
|
1108
|
+
feed.scrollTop = feed.scrollHeight;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// \u2500\u2500 Misc \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
1112
|
+
function showError(main) {
|
|
1113
|
+
return function (e) { main.appendChild(el('div', { class: 'error', text: e.message })); };
|
|
1114
|
+
}
|
|
1115
|
+
function alertError(e) { console.error(e); qs('#dbpath').textContent = 'error: ' + e.message; }
|
|
1116
|
+
|
|
1117
|
+
qs('#refresh').onclick = function () { render(true); };
|
|
1118
|
+
document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeDrawer(); });
|
|
1119
|
+
})();
|
|
1120
|
+
`;
|
|
1121
|
+
|
|
1122
|
+
// src/commands/studio/studio-ui-css.ts
|
|
1123
|
+
var STUDIO_CSS = `
|
|
1124
|
+
:root {
|
|
1125
|
+
--bg: #0e1015; --panel: #161a22; --panel-2: #1c212c; --panel-3: #232936;
|
|
1126
|
+
--border: #262c3a; --text: #e6e9f0; --muted: #8b93a7;
|
|
1127
|
+
--accent: #4fd1c5; --accent-2: #9f7aea;
|
|
1128
|
+
--insert: #48bb78; --update: #4299e1; --delete: #f56565; --warn: #ed8936;
|
|
1129
|
+
--ok-bg: rgba(72,187,120,.12); --bad-bg: rgba(245,101,101,.12);
|
|
1130
|
+
}
|
|
1131
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
1132
|
+
html, body { height: 100%; }
|
|
1133
|
+
body {
|
|
1134
|
+
background: var(--bg); color: var(--text); overflow: hidden;
|
|
1135
|
+
font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
1136
|
+
}
|
|
1137
|
+
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
|
|
1138
|
+
button {
|
|
1139
|
+
background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
|
|
1140
|
+
border-radius: 6px; padding: 5px 12px; cursor: pointer; font-size: 13px;
|
|
1141
|
+
}
|
|
1142
|
+
button:hover { border-color: var(--accent); }
|
|
1143
|
+
button.primary { background: rgba(79,209,197,.12); border-color: var(--accent); color: var(--accent); }
|
|
1144
|
+
button.danger { color: var(--delete); }
|
|
1145
|
+
button:disabled { opacity: .4; cursor: default; }
|
|
1146
|
+
input, select {
|
|
1147
|
+
background: var(--bg); color: var(--text); border: 1px solid var(--border);
|
|
1148
|
+
border-radius: 6px; padding: 4px 8px; font-size: 13px;
|
|
1149
|
+
}
|
|
1150
|
+
input:focus, select:focus { outline: none; border-color: var(--accent); }
|
|
1151
|
+
input[type="checkbox"] { width: auto; }
|
|
1152
|
+
input[type="range"] { padding: 0; }
|
|
1153
|
+
|
|
1154
|
+
header {
|
|
1155
|
+
display: flex; align-items: center; gap: 14px; padding: 10px 16px;
|
|
1156
|
+
background: var(--panel); border-bottom: 1px solid var(--border); height: 48px;
|
|
1157
|
+
}
|
|
1158
|
+
header .logo { font-weight: 700; letter-spacing: .3px; white-space: nowrap; }
|
|
1159
|
+
header .logo span { color: var(--accent); }
|
|
1160
|
+
header .modebadge {
|
|
1161
|
+
font-size: 10px; text-transform: uppercase; letter-spacing: 1px; padding: 2px 8px;
|
|
1162
|
+
border-radius: 10px; background: rgba(159,122,234,.15); color: var(--accent-2);
|
|
1163
|
+
}
|
|
1164
|
+
header .dbpath { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
|
|
1165
|
+
header .live { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
|
|
1166
|
+
header .live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
|
|
1167
|
+
header .live.on .dot { background: var(--insert); box-shadow: 0 0 8px rgba(72,187,120,.8); }
|
|
1168
|
+
|
|
1169
|
+
.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
|
|
1170
|
+
.tabs .tab {
|
|
1171
|
+
padding: 8px 16px; cursor: pointer; color: var(--muted); font-size: 13px;
|
|
1172
|
+
border-bottom: 2px solid transparent; user-select: none;
|
|
1173
|
+
}
|
|
1174
|
+
.tabs .tab:hover { color: var(--text); }
|
|
1175
|
+
.tabs .tab.active { color: var(--accent); border-bottom-color: var(--accent); }
|
|
1176
|
+
.tabs .spacer { flex: 1; }
|
|
1177
|
+
.tabs .devicepick { display: flex; align-items: center; gap: 6px; padding: 6px 0; color: var(--muted); font-size: 12px; }
|
|
1178
|
+
|
|
1179
|
+
.layout { display: flex; height: calc(100vh - 85px); }
|
|
1180
|
+
nav.sidebar {
|
|
1181
|
+
width: 220px; background: var(--panel); border-right: 1px solid var(--border);
|
|
1182
|
+
padding: 12px 0; overflow-y: auto; flex-shrink: 0;
|
|
1183
|
+
}
|
|
1184
|
+
nav.sidebar h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); padding: 8px 16px 4px; }
|
|
1185
|
+
nav.sidebar .item {
|
|
1186
|
+
display: flex; justify-content: space-between; align-items: center;
|
|
1187
|
+
padding: 6px 16px; cursor: pointer; border-left: 2px solid transparent; font-size: 13px;
|
|
1188
|
+
}
|
|
1189
|
+
nav.sidebar .item:hover { background: var(--panel-2); }
|
|
1190
|
+
nav.sidebar .item.active { border-left-color: var(--accent); background: var(--panel-2); }
|
|
1191
|
+
nav.sidebar .item .count { color: var(--muted); font-size: 11px; }
|
|
1192
|
+
main { flex: 1; overflow: auto; padding: 16px 20px; }
|
|
1193
|
+
|
|
1194
|
+
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
|
|
1195
|
+
.toolbar h2 { font-size: 16px; margin-right: auto; }
|
|
1196
|
+
.toolbar label { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 5px; cursor: pointer; }
|
|
1197
|
+
|
|
1198
|
+
table.grid { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
|
|
1199
|
+
table.grid th, table.grid td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
|
|
1200
|
+
table.grid th { background: var(--panel-2); color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; position: sticky; top: 0; z-index: 1; }
|
|
1201
|
+
table.grid tbody tr.clickable { cursor: pointer; }
|
|
1202
|
+
table.grid tbody tr.clickable:hover { background: var(--panel-2); }
|
|
1203
|
+
tr.tombstone td { color: var(--muted); text-decoration: line-through; }
|
|
1204
|
+
|
|
1205
|
+
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
|
|
1206
|
+
.badge.insert { background: rgba(72,187,120,.15); color: var(--insert); }
|
|
1207
|
+
.badge.update { background: rgba(66,153,225,.15); color: var(--update); }
|
|
1208
|
+
.badge.delete { background: rgba(245,101,101,.15); color: var(--delete); }
|
|
1209
|
+
.badge.neutral { background: var(--panel-3); color: var(--muted); }
|
|
1210
|
+
.badge.ok { background: var(--ok-bg); color: var(--insert); }
|
|
1211
|
+
.badge.bad { background: var(--bad-bg); color: var(--delete); }
|
|
1212
|
+
|
|
1213
|
+
.chip {
|
|
1214
|
+
display: inline-flex; gap: 6px; align-items: center; background: var(--panel-2);
|
|
1215
|
+
border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; font-size: 11px; color: var(--muted);
|
|
1216
|
+
}
|
|
1217
|
+
.chip b { color: var(--accent-2); font-weight: 600; }
|
|
1218
|
+
|
|
1219
|
+
.pager { display: flex; gap: 8px; align-items: center; margin-top: 10px; color: var(--muted); font-size: 12px; }
|
|
1220
|
+
.empty { color: var(--muted); padding: 40px; text-align: center; }
|
|
1221
|
+
.error { color: var(--delete); padding: 20px; }
|
|
1222
|
+
|
|
1223
|
+
.drawer {
|
|
1224
|
+
position: fixed; top: 85px; right: 0; bottom: 0; width: 560px; max-width: 92vw;
|
|
1225
|
+
background: var(--panel); border-left: 1px solid var(--border); overflow-y: auto;
|
|
1226
|
+
padding: 16px; box-shadow: -12px 0 30px rgba(0,0,0,.45); display: none; z-index: 20;
|
|
1227
|
+
}
|
|
1228
|
+
.drawer.open { display: block; }
|
|
1229
|
+
.drawer h3 { font-size: 14px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; }
|
|
1230
|
+
.drawer h4 { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin: 16px 0 8px; }
|
|
1231
|
+
.drawer .close { cursor: pointer; color: var(--muted); padding: 4px 8px; }
|
|
1232
|
+
.fieldrow { padding: 8px 10px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; }
|
|
1233
|
+
.fieldrow .fname { color: var(--accent); font-size: 12px; }
|
|
1234
|
+
.fieldrow .fval { word-break: break-word; margin: 2px 0 4px; }
|
|
1235
|
+
.fieldrow .preview { color: var(--muted); font-style: italic; }
|
|
1236
|
+
|
|
1237
|
+
.op { border-left: 2px solid var(--border); margin-left: 6px; padding: 6px 0 6px 14px; position: relative; }
|
|
1238
|
+
.op::before { content: ""; position: absolute; left: -5px; top: 13px; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
|
|
1239
|
+
.op.insert::before { background: var(--insert); } .op.update::before { background: var(--update); } .op.delete::before { background: var(--delete); }
|
|
1240
|
+
.op .meta { color: var(--muted); font-size: 11px; }
|
|
1241
|
+
.op pre { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; margin-top: 4px; overflow-x: auto; }
|
|
1242
|
+
|
|
1243
|
+
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; margin-bottom: 14px; }
|
|
1244
|
+
.panel h3 { font-size: 13px; margin-bottom: 8px; color: var(--accent); }
|
|
1245
|
+
|
|
1246
|
+
/* DAG */
|
|
1247
|
+
.dagwrap { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow-x: auto; margin-bottom: 14px; }
|
|
1248
|
+
.dagwrap svg { display: block; }
|
|
1249
|
+
.dag-node { cursor: pointer; }
|
|
1250
|
+
.dag-node circle { stroke-width: 2; }
|
|
1251
|
+
.dag-node text { fill: var(--muted); font-size: 10px; font-family: ui-monospace, Menlo, monospace; }
|
|
1252
|
+
.dag-edge { stroke: #3a4356; stroke-width: 1.4; fill: none; marker-end: url(#arrow); }
|
|
1253
|
+
.dag-lane-label { fill: var(--accent-2); font-size: 11px; font-family: ui-monospace, Menlo, monospace; }
|
|
1254
|
+
.dag-lane-line { stroke: #1d2330; stroke-width: 1; }
|
|
1255
|
+
.dag-tip {
|
|
1256
|
+
position: fixed; background: var(--panel-3); border: 1px solid var(--border); border-radius: 6px;
|
|
1257
|
+
padding: 6px 10px; font-size: 12px; pointer-events: none; z-index: 50; max-width: 380px; display: none;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/* Time travel */
|
|
1261
|
+
.tt-controls { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
|
|
1262
|
+
.tt-controls input[type="range"] { flex: 1; }
|
|
1263
|
+
.tt-cut { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 14px; font-size: 12px; }
|
|
1264
|
+
|
|
1265
|
+
/* Lab */
|
|
1266
|
+
.lab-top { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
|
|
1267
|
+
.convergence { padding: 8px 14px; border-radius: 8px; font-weight: 600; font-size: 13px; }
|
|
1268
|
+
.convergence.ok { background: var(--ok-bg); color: var(--insert); border: 1px solid rgba(72,187,120,.4); }
|
|
1269
|
+
.convergence.bad { background: var(--bad-bg); color: var(--delete); border: 1px solid rgba(245,101,101,.4); }
|
|
1270
|
+
.convergence .diffs { font-weight: 400; font-size: 11px; margin-top: 4px; color: var(--muted); }
|
|
1271
|
+
.lab-grid { display: flex; gap: 14px; align-items: flex-start; }
|
|
1272
|
+
.lab-devices { flex: 1; display: grid; grid-template-columns: repeat(auto-fill, minmax(390px, 1fr)); gap: 14px; }
|
|
1273
|
+
.device-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
|
|
1274
|
+
.device-card.offline { border-color: rgba(245,101,101,.35); }
|
|
1275
|
+
.device-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: var(--panel-2); }
|
|
1276
|
+
.device-head .name { font-weight: 700; }
|
|
1277
|
+
.device-head .node { color: var(--muted); font-size: 11px; }
|
|
1278
|
+
.device-head .spacer { flex: 1; }
|
|
1279
|
+
.device-body { padding: 10px 12px; }
|
|
1280
|
+
.device-body h5 { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin: 10px 0 6px; }
|
|
1281
|
+
.chaos-row { display: grid; grid-template-columns: 78px 1fr 44px; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
|
|
1282
|
+
.rec-line { display: flex; align-items: center; gap: 6px; padding: 5px 6px; border-bottom: 1px solid var(--border); font-size: 12px; flex-wrap: wrap; }
|
|
1283
|
+
.rec-line:hover { background: var(--panel-2); }
|
|
1284
|
+
.rec-line .title { flex: 1; min-width: 120px; }
|
|
1285
|
+
.rec-line input[type="text"] { font-size: 12px; padding: 2px 6px; width: 130px; }
|
|
1286
|
+
.rec-line .pts { color: var(--accent); min-width: 30px; text-align: right; }
|
|
1287
|
+
.insert-form { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
|
|
1288
|
+
.insert-form input { flex: 1; min-width: 120px; }
|
|
1289
|
+
.lab-feed { width: 360px; flex-shrink: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; display: flex; flex-direction: column; max-height: calc(100vh - 200px); }
|
|
1290
|
+
.lab-feed h3 { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
|
|
1291
|
+
.lab-feed .feed { overflow-y: auto; padding: 6px 0; flex: 1; }
|
|
1292
|
+
.feed-line { display: flex; gap: 8px; padding: 3px 12px; font-size: 11px; align-items: baseline; }
|
|
1293
|
+
.feed-line .dev { min-width: 66px; font-weight: 600; }
|
|
1294
|
+
.feed-line .what { color: var(--muted); }
|
|
1295
|
+
.feed-line.evt-created .what, .feed-line.evt-applied .what { color: var(--text); }
|
|
1296
|
+
.feed-line.evt-conflict .what { color: var(--warn); }
|
|
1297
|
+
.feed-line.evt-failed .what { color: var(--delete); font-weight: 700; }
|
|
1298
|
+
.dev-c0 { color: #4fd1c5; } .dev-c1 { color: #9f7aea; } .dev-c2 { color: #f6ad55; }
|
|
1299
|
+
.dev-c3 { color: #63b3ed; } .dev-c4 { color: #f687b3; } .dev-c5 { color: #68d391; }
|
|
1300
|
+
`;
|
|
1301
|
+
|
|
1302
|
+
// src/commands/studio/studio-server.ts
|
|
1303
|
+
var SSE_POLL_MS = 700;
|
|
1304
|
+
async function startStudioServer(options) {
|
|
1305
|
+
const host = options.host ?? "127.0.0.1";
|
|
1306
|
+
const dbPath = options.spectator ? options.spectator.dbPath : options.dbPath;
|
|
1307
|
+
const context = {
|
|
1308
|
+
mode: options.spectator ? "spectator" : options.lab ? "lab" : "file",
|
|
1309
|
+
lab: options.lab ?? null,
|
|
1310
|
+
spectator: options.spectator ?? null,
|
|
1311
|
+
mainReader: dbPath ? await StudioDbReader.open(dbPath) : null,
|
|
1312
|
+
deviceReaders: /* @__PURE__ */ new Map()
|
|
1313
|
+
};
|
|
1314
|
+
const sseClients = /* @__PURE__ */ new Set();
|
|
1315
|
+
const broadcast = (event, data) => {
|
|
1316
|
+
const payload = `event: ${event}
|
|
1317
|
+
data: ${JSON.stringify(data)}
|
|
1318
|
+
|
|
1319
|
+
`;
|
|
1320
|
+
for (const client of sseClients) {
|
|
1321
|
+
client.write(payload);
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
let unsubscribeSpectator = null;
|
|
1325
|
+
if (context.spectator) {
|
|
1326
|
+
unsubscribeSpectator = context.spectator.onEvent((event) => {
|
|
1327
|
+
broadcast("spectator", event);
|
|
1328
|
+
broadcast("change", { at: event.at });
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
let pollTimer = null;
|
|
1332
|
+
if (context.mode === "file" && context.mainReader) {
|
|
1333
|
+
let lastFingerprint = context.mainReader.fingerprint();
|
|
1334
|
+
pollTimer = setInterval(() => {
|
|
1335
|
+
try {
|
|
1336
|
+
const current = context.mainReader?.fingerprint();
|
|
1337
|
+
if (current !== void 0 && current !== lastFingerprint) {
|
|
1338
|
+
lastFingerprint = current;
|
|
1339
|
+
broadcast("change", { at: Date.now() });
|
|
1340
|
+
}
|
|
1341
|
+
} catch {
|
|
1342
|
+
}
|
|
1343
|
+
}, SSE_POLL_MS);
|
|
1344
|
+
}
|
|
1345
|
+
let unsubscribeLab = null;
|
|
1346
|
+
if (context.lab) {
|
|
1347
|
+
unsubscribeLab = context.lab.onEvent((event) => {
|
|
1348
|
+
broadcast("lab", event);
|
|
1349
|
+
broadcast("change", { at: event.at });
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
const server = createServer((req, res) => {
|
|
1353
|
+
handleRequest(context, sseClients, req, res).catch((error) => {
|
|
1354
|
+
const message = error instanceof Error ? error.message : "Internal error";
|
|
1355
|
+
if (!res.headersSent) {
|
|
1356
|
+
sendJson(res, 500, { error: message });
|
|
1357
|
+
} else {
|
|
1358
|
+
res.end();
|
|
1359
|
+
}
|
|
1360
|
+
});
|
|
1361
|
+
});
|
|
1362
|
+
await new Promise((resolve, reject) => {
|
|
1363
|
+
server.once("error", reject);
|
|
1364
|
+
server.listen(options.port, host, () => {
|
|
1365
|
+
server.removeListener("error", reject);
|
|
1366
|
+
resolve();
|
|
1367
|
+
});
|
|
1368
|
+
});
|
|
1369
|
+
const address = server.address();
|
|
1370
|
+
const port = typeof address === "object" && address !== null ? address.port : options.port;
|
|
1371
|
+
return {
|
|
1372
|
+
server,
|
|
1373
|
+
port,
|
|
1374
|
+
url: `http://${host}:${port}`,
|
|
1375
|
+
close: async () => {
|
|
1376
|
+
if (pollTimer) {
|
|
1377
|
+
clearInterval(pollTimer);
|
|
1378
|
+
}
|
|
1379
|
+
unsubscribeSpectator?.();
|
|
1380
|
+
unsubscribeLab?.();
|
|
1381
|
+
for (const client of sseClients) {
|
|
1382
|
+
client.end();
|
|
1383
|
+
}
|
|
1384
|
+
sseClients.clear();
|
|
1385
|
+
context.mainReader?.close();
|
|
1386
|
+
for (const [, reader] of context.deviceReaders) {
|
|
1387
|
+
reader.close();
|
|
1388
|
+
}
|
|
1389
|
+
context.deviceReaders.clear();
|
|
1390
|
+
await new Promise((resolve, reject) => {
|
|
1391
|
+
server.close((error) => error ? reject(error) : resolve());
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
async function resolveReader(context, url) {
|
|
1397
|
+
const device = url.searchParams.get("device");
|
|
1398
|
+
if (device && context.lab) {
|
|
1399
|
+
const cached = context.deviceReaders.get(device);
|
|
1400
|
+
if (cached) {
|
|
1401
|
+
return cached;
|
|
1402
|
+
}
|
|
1403
|
+
const state = context.lab.deviceState(device);
|
|
1404
|
+
const reader = await StudioDbReader.open(state.dbPath);
|
|
1405
|
+
context.deviceReaders.set(device, reader);
|
|
1406
|
+
return reader;
|
|
1407
|
+
}
|
|
1408
|
+
if (context.mainReader) {
|
|
1409
|
+
return context.mainReader;
|
|
1410
|
+
}
|
|
1411
|
+
if (context.lab) {
|
|
1412
|
+
const first = context.lab.listDevices()[0];
|
|
1413
|
+
if (first) {
|
|
1414
|
+
return resolveReader(context, new URL(`${url.origin}${url.pathname}?device=${first.name}`));
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
throw new Error("No database available");
|
|
1418
|
+
}
|
|
1419
|
+
async function handleRequest(context, sseClients, req, res) {
|
|
1420
|
+
const url = new URL(req.url ?? "/", "http://studio.local");
|
|
1421
|
+
const path = url.pathname;
|
|
1422
|
+
const method = req.method ?? "GET";
|
|
1423
|
+
if (method === "GET" && (path === "/" || path === "/index.html")) {
|
|
1424
|
+
res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
|
|
1425
|
+
res.end(STUDIO_HTML);
|
|
1426
|
+
return;
|
|
1427
|
+
}
|
|
1428
|
+
if (method === "GET" && path === "/app.js") {
|
|
1429
|
+
res.writeHead(200, { "content-type": "text/javascript; charset=utf-8" });
|
|
1430
|
+
res.end(STUDIO_APP_JS);
|
|
1431
|
+
return;
|
|
1432
|
+
}
|
|
1433
|
+
if (method === "GET" && path === "/style.css") {
|
|
1434
|
+
res.writeHead(200, { "content-type": "text/css; charset=utf-8" });
|
|
1435
|
+
res.end(STUDIO_CSS);
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
if (method === "GET" && path === "/api/events") {
|
|
1439
|
+
res.writeHead(200, {
|
|
1440
|
+
"content-type": "text/event-stream",
|
|
1441
|
+
"cache-control": "no-store",
|
|
1442
|
+
connection: "keep-alive"
|
|
1443
|
+
});
|
|
1444
|
+
res.write(`event: hello
|
|
1445
|
+
data: ${JSON.stringify({ mode: context.mode })}
|
|
1446
|
+
|
|
1447
|
+
`);
|
|
1448
|
+
if (context.lab) {
|
|
1449
|
+
for (const event of context.lab.recentEvents().slice(-100)) {
|
|
1450
|
+
res.write(`event: lab
|
|
1451
|
+
data: ${JSON.stringify(event)}
|
|
1452
|
+
|
|
1453
|
+
`);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
if (context.spectator) {
|
|
1457
|
+
for (const event of context.spectator.recentEvents().slice(-100)) {
|
|
1458
|
+
res.write(`event: spectator
|
|
1459
|
+
data: ${JSON.stringify(event)}
|
|
1460
|
+
|
|
1461
|
+
`);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
sseClients.add(res);
|
|
1465
|
+
req.on("close", () => {
|
|
1466
|
+
sseClients.delete(res);
|
|
1467
|
+
});
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1470
|
+
if (path.startsWith("/api/lab")) {
|
|
1471
|
+
if (!context.lab) {
|
|
1472
|
+
sendJson(res, 404, {
|
|
1473
|
+
error: "Studio is not running in lab mode. Start with: kora studio --lab"
|
|
1474
|
+
});
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
await handleLabRoute(context, context.lab, req, res, url);
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
if (method !== "GET") {
|
|
1481
|
+
sendJson(res, 405, { error: "Studio data routes are read-only: GET only." });
|
|
1482
|
+
return;
|
|
1483
|
+
}
|
|
1484
|
+
if (path === "/api/mode") {
|
|
1485
|
+
sendJson(res, 200, {
|
|
1486
|
+
mode: context.mode,
|
|
1487
|
+
devices: context.lab?.listDevices().map((d) => d.name) ?? [],
|
|
1488
|
+
...context.spectator ? { spectator: context.spectator.status() } : {}
|
|
1489
|
+
});
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
if (path === "/api/spectator/status") {
|
|
1493
|
+
if (!context.spectator) {
|
|
1494
|
+
sendJson(res, 404, { error: "Not in spectator mode" });
|
|
1495
|
+
return;
|
|
1496
|
+
}
|
|
1497
|
+
sendJson(res, 200, context.spectator.status());
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
if (path === "/api/overview") {
|
|
1501
|
+
const reader = await resolveReader(context, url);
|
|
1502
|
+
sendJson(res, 200, reader.overview());
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
if (path === "/api/audit") {
|
|
1506
|
+
const reader = await resolveReader(context, url);
|
|
1507
|
+
sendJson(res, 200, { traces: reader.auditTraces(intParam(url, "limit") ?? 100) });
|
|
1508
|
+
return;
|
|
1509
|
+
}
|
|
1510
|
+
const collectionMatch = path.match(
|
|
1511
|
+
/^\/api\/collections\/([a-zA-Z0-9_]+)\/(records|ops|replay|dag)(?:\/([^/]+))?(?:\/(ops))?$/
|
|
1512
|
+
);
|
|
1513
|
+
if (collectionMatch) {
|
|
1514
|
+
const [, collection, kind, recordId, sub] = collectionMatch;
|
|
1515
|
+
if (!collection || !kind) {
|
|
1516
|
+
sendJson(res, 400, { error: "Bad request" });
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
const reader = await resolveReader(context, url);
|
|
1520
|
+
if (!reader.listCollections().includes(collection)) {
|
|
1521
|
+
sendJson(res, 404, { error: `Unknown collection "${collection}"` });
|
|
1522
|
+
return;
|
|
1523
|
+
}
|
|
1524
|
+
if (kind === "replay") {
|
|
1525
|
+
const ops = reader.allOperations(collection);
|
|
1526
|
+
const upTo = url.searchParams.get("upTo");
|
|
1527
|
+
sendJson(res, 200, replayToOperation(ops, upTo));
|
|
1528
|
+
return;
|
|
1529
|
+
}
|
|
1530
|
+
if (kind === "dag") {
|
|
1531
|
+
const record = url.searchParams.get("record");
|
|
1532
|
+
const limit = intParam(url, "limit") ?? 200;
|
|
1533
|
+
let ops = reader.allOperations(collection);
|
|
1534
|
+
if (record) {
|
|
1535
|
+
ops = ops.filter((o) => o.recordId === record);
|
|
1536
|
+
} else {
|
|
1537
|
+
ops = ops.slice(-limit);
|
|
1538
|
+
}
|
|
1539
|
+
sendJson(res, 200, buildCausalDag(ops));
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
if (kind === "ops") {
|
|
1543
|
+
sendJson(
|
|
1544
|
+
res,
|
|
1545
|
+
200,
|
|
1546
|
+
reader.operations(collection, {
|
|
1547
|
+
limit: intParam(url, "limit"),
|
|
1548
|
+
offset: intParam(url, "offset")
|
|
1549
|
+
})
|
|
1550
|
+
);
|
|
1551
|
+
return;
|
|
1552
|
+
}
|
|
1553
|
+
if (recordId && sub === "ops") {
|
|
1554
|
+
sendJson(res, 200, { operations: reader.recordOperations(collection, recordId) });
|
|
1555
|
+
return;
|
|
1556
|
+
}
|
|
1557
|
+
if (recordId) {
|
|
1558
|
+
const record = reader.record(collection, recordId);
|
|
1559
|
+
if (!record) {
|
|
1560
|
+
sendJson(res, 404, { error: `Record "${recordId}" not found` });
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
const richtextPreviews = await decodeRichtextFields(
|
|
1564
|
+
reader,
|
|
1565
|
+
collection,
|
|
1566
|
+
recordId,
|
|
1567
|
+
record.fields
|
|
1568
|
+
);
|
|
1569
|
+
sendJson(res, 200, {
|
|
1570
|
+
record,
|
|
1571
|
+
richtextPreviews,
|
|
1572
|
+
operations: reader.recordOperations(collection, recordId)
|
|
1573
|
+
});
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
sendJson(
|
|
1577
|
+
res,
|
|
1578
|
+
200,
|
|
1579
|
+
reader.records(collection, {
|
|
1580
|
+
limit: intParam(url, "limit"),
|
|
1581
|
+
offset: intParam(url, "offset"),
|
|
1582
|
+
includeDeleted: url.searchParams.get("includeDeleted") === "true",
|
|
1583
|
+
search: url.searchParams.get("search") ?? void 0
|
|
1584
|
+
})
|
|
1585
|
+
);
|
|
1586
|
+
return;
|
|
1587
|
+
}
|
|
1588
|
+
sendJson(res, 404, { error: `No route for ${path}` });
|
|
1589
|
+
}
|
|
1590
|
+
async function handleLabRoute(context, lab, req, res, url) {
|
|
1591
|
+
const path = url.pathname;
|
|
1592
|
+
const method = req.method ?? "GET";
|
|
1593
|
+
if (method === "GET" && path === "/api/lab/state") {
|
|
1594
|
+
const schema = lab.getSchema();
|
|
1595
|
+
sendJson(res, 200, {
|
|
1596
|
+
devices: lab.listDevices(),
|
|
1597
|
+
serverOperations: lab.serverOperationCount(),
|
|
1598
|
+
collections: Object.entries(schema.collections).map(([name, def]) => ({
|
|
1599
|
+
name,
|
|
1600
|
+
fields: Object.entries(def.fields).map(([fieldName, descriptor]) => ({
|
|
1601
|
+
name: fieldName,
|
|
1602
|
+
kind: descriptor.kind,
|
|
1603
|
+
optional: !descriptor.required,
|
|
1604
|
+
enumValues: descriptor.enumValues ?? null,
|
|
1605
|
+
defaultValue: descriptor.defaultValue ?? null
|
|
1606
|
+
}))
|
|
1607
|
+
}))
|
|
1608
|
+
});
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
if (method === "GET" && path === "/api/lab/convergence") {
|
|
1612
|
+
sendJson(res, 200, await lab.convergence());
|
|
1613
|
+
return;
|
|
1614
|
+
}
|
|
1615
|
+
if (method !== "POST") {
|
|
1616
|
+
sendJson(res, 405, { error: "POST required" });
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
const body = await readJsonBody(req);
|
|
1620
|
+
if (path === "/api/lab/devices") {
|
|
1621
|
+
const state = await lab.addDevice(typeof body.name === "string" ? body.name : void 0);
|
|
1622
|
+
sendJson(res, 200, state);
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1625
|
+
const deviceMatch = path.match(
|
|
1626
|
+
/^\/api\/lab\/devices\/([a-zA-Z0-9_-]+)\/(connect|disconnect|sync|chaos|insert|update|delete)$/
|
|
1627
|
+
);
|
|
1628
|
+
if (!deviceMatch) {
|
|
1629
|
+
sendJson(res, 404, { error: `No lab route for ${path}` });
|
|
1630
|
+
return;
|
|
1631
|
+
}
|
|
1632
|
+
const [, device, action] = deviceMatch;
|
|
1633
|
+
if (!device || !action) {
|
|
1634
|
+
sendJson(res, 400, { error: "Bad request" });
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
switch (action) {
|
|
1638
|
+
case "connect":
|
|
1639
|
+
await lab.connect(device);
|
|
1640
|
+
break;
|
|
1641
|
+
case "disconnect": {
|
|
1642
|
+
await lab.disconnect(device);
|
|
1643
|
+
break;
|
|
1644
|
+
}
|
|
1645
|
+
case "sync":
|
|
1646
|
+
await lab.sync(device);
|
|
1647
|
+
break;
|
|
1648
|
+
case "chaos":
|
|
1649
|
+
sendJson(
|
|
1650
|
+
res,
|
|
1651
|
+
200,
|
|
1652
|
+
lab.setChaos(device, body)
|
|
1653
|
+
);
|
|
1654
|
+
return;
|
|
1655
|
+
case "insert": {
|
|
1656
|
+
const record = await lab.insert(
|
|
1657
|
+
device,
|
|
1658
|
+
String(body.collection ?? ""),
|
|
1659
|
+
body.data ?? {}
|
|
1660
|
+
);
|
|
1661
|
+
sendJson(res, 200, { record });
|
|
1662
|
+
return;
|
|
1663
|
+
}
|
|
1664
|
+
case "update": {
|
|
1665
|
+
const record = await lab.update(
|
|
1666
|
+
device,
|
|
1667
|
+
String(body.collection ?? ""),
|
|
1668
|
+
String(body.id ?? ""),
|
|
1669
|
+
body.data ?? {},
|
|
1670
|
+
body.increments ?? void 0
|
|
1671
|
+
);
|
|
1672
|
+
sendJson(res, 200, { record });
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
case "delete":
|
|
1676
|
+
await lab.delete(device, String(body.collection ?? ""), String(body.id ?? ""));
|
|
1677
|
+
break;
|
|
1678
|
+
}
|
|
1679
|
+
sendJson(res, 200, { ok: true, device: lab.deviceState(device) });
|
|
1680
|
+
}
|
|
1681
|
+
async function decodeRichtextFields(reader, collection, recordId, fields) {
|
|
1682
|
+
const previews = {};
|
|
1683
|
+
const binaryFields = Object.entries(fields).filter(
|
|
1684
|
+
([, value]) => typeof value === "string" && value.startsWith("<binary ")
|
|
1685
|
+
);
|
|
1686
|
+
if (binaryFields.length === 0) {
|
|
1687
|
+
return previews;
|
|
1688
|
+
}
|
|
1689
|
+
let toPlainText = null;
|
|
1690
|
+
try {
|
|
1691
|
+
const storePkg = await import("@korajs/store");
|
|
1692
|
+
toPlainText = storePkg.richtextToPlainText ?? null;
|
|
1693
|
+
} catch {
|
|
1694
|
+
return previews;
|
|
1695
|
+
}
|
|
1696
|
+
if (!toPlainText) {
|
|
1697
|
+
return previews;
|
|
1698
|
+
}
|
|
1699
|
+
for (const [field] of binaryFields) {
|
|
1700
|
+
try {
|
|
1701
|
+
const raw = reader.rawFieldValue(collection, recordId, field);
|
|
1702
|
+
if (raw instanceof Uint8Array || Buffer.isBuffer(raw)) {
|
|
1703
|
+
previews[field] = toPlainText(new Uint8Array(raw));
|
|
1704
|
+
}
|
|
1705
|
+
} catch {
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return previews;
|
|
1709
|
+
}
|
|
1710
|
+
async function readJsonBody(req) {
|
|
1711
|
+
const chunks = [];
|
|
1712
|
+
let size = 0;
|
|
1713
|
+
for await (const chunk of req) {
|
|
1714
|
+
size += chunk.length;
|
|
1715
|
+
if (size > 256 * 1024) {
|
|
1716
|
+
throw new Error("Request body too large");
|
|
1717
|
+
}
|
|
1718
|
+
chunks.push(chunk);
|
|
1719
|
+
}
|
|
1720
|
+
if (chunks.length === 0) {
|
|
1721
|
+
return {};
|
|
1722
|
+
}
|
|
1723
|
+
try {
|
|
1724
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
1725
|
+
} catch {
|
|
1726
|
+
throw new Error("Invalid JSON body");
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
function intParam(url, name) {
|
|
1730
|
+
const raw = url.searchParams.get(name);
|
|
1731
|
+
if (raw === null) {
|
|
1732
|
+
return void 0;
|
|
1733
|
+
}
|
|
1734
|
+
const parsed = Number.parseInt(raw, 10);
|
|
1735
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
1736
|
+
}
|
|
1737
|
+
function sendJson(res, status, body) {
|
|
1738
|
+
res.writeHead(status, {
|
|
1739
|
+
"content-type": "application/json; charset=utf-8",
|
|
1740
|
+
"cache-control": "no-store"
|
|
1741
|
+
});
|
|
1742
|
+
res.end(JSON.stringify(body));
|
|
1743
|
+
}
|
|
1744
|
+
export {
|
|
1745
|
+
startStudioServer
|
|
1746
|
+
};
|
|
1747
|
+
//# sourceMappingURL=studio-server-XLGZNGG5.js.map
|