shadow-claw 1.23.4 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. package/README.md +42 -5
  2. package/bin/cli.mjs +166 -0
  3. package/bin/cli.test.mjs +60 -0
  4. package/bin/commands/backup.mjs +70 -0
  5. package/bin/commands/clients.mjs +41 -0
  6. package/bin/commands/peer-id.mjs +76 -0
  7. package/bin/commands/peer-id.test.mjs +104 -0
  8. package/bin/commands/send.mjs +51 -0
  9. package/bin/commands/tasks.mjs +59 -0
  10. package/bin/commands/webrtc-listen.mjs +254 -0
  11. package/bin/prerender-dsd-shell/prerender-dsd-shell.test.mjs +46 -0
  12. package/bin/rolldown.config.test.mjs +401 -8
  13. package/bin/utils/control-client.mjs +243 -0
  14. package/bin/utils/control-client.test.mjs +83 -0
  15. package/bin/utils/ulid.mjs +45 -0
  16. package/bin/utils/webrtc-control-client.mjs +699 -0
  17. package/bin/utils/webrtc-control-client.test.mjs +318 -0
  18. package/dist/electron/main.cjs +1 -1
  19. package/dist/public/AGENTS.md +5 -3
  20. package/dist/public/README.md +42 -5
  21. package/dist/public/agent.worker.js +1 -1
  22. package/dist/public/backup-controller-C_s9in2t.js +1 -0
  23. package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +1 -0
  24. package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +1 -0
  25. package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +1 -1
  26. package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
  27. package/dist/public/components/shadow-claw-settings/shadow-claw-settings.html +1 -1
  28. package/dist/public/config-CrHchneq.js +1 -0
  29. package/dist/public/{configurePeerJs-B4Ym9VMj.js → configurePeerJs-CxpZeDJJ.js} +1 -1
  30. package/dist/public/{connections-BEn6JYdu.js → connections-DvbgdkaB.js} +1 -1
  31. package/dist/public/{custom-element-security-DdYlUozq.js → custom-element-security-NCqQ5lmC.js} +1 -1
  32. package/dist/public/docs/README.md +3 -1
  33. package/dist/public/docs/architecture/overview.md +12 -12
  34. package/dist/public/docs/decisions/bundled-typescript-architecture.md +1 -1
  35. package/dist/public/docs/decisions/indexeddb-and-opfs-storage.md +2 -2
  36. package/dist/public/docs/decisions/native-web-components-and-signals.md +1 -1
  37. package/dist/public/docs/example/article/index.html +1 -1
  38. package/dist/public/docs/guides/configuring-messaging-channels.md +41 -6
  39. package/dist/public/docs/guides/server-development-configuration.md +24 -0
  40. package/dist/public/docs/publishing/index.html +1 -1
  41. package/dist/public/docs/skill-creator/index.html +1 -1
  42. package/dist/public/docs/subsystems/backup.md +56 -0
  43. package/dist/public/docs/subsystems/cli.md +124 -0
  44. package/dist/public/docs/subsystems/control-plane.md +158 -0
  45. package/dist/public/docs/subsystems/notifications.md +54 -2
  46. package/dist/public/docs/subsystems/reactive-ui.md +1 -0
  47. package/dist/public/downloadGroupFile-By0GR6eV.js +1 -0
  48. package/dist/public/e2e/README.md +5 -1
  49. package/dist/public/{e2e-bridge-BQ4KzUt5.js → e2e-bridge-IGWsRl4i.js} +1 -1
  50. package/dist/public/file-viewer-D0eflgt6.js +1 -0
  51. package/dist/public/getAllTasks-vpBlgGsc.js +1 -0
  52. package/dist/public/getGroupDir-P1h9wl6S.js +1 -0
  53. package/dist/public/{git-DEUvQTUA.js → git-7Vq_SJSp.js} +2 -2
  54. package/dist/public/{iframe-sanitizer-0NS4XLOd.js → iframe-sanitizer-y2Z1pPwY.js} +1 -1
  55. package/dist/public/index.html +1 -1
  56. package/dist/public/index.js +1 -1
  57. package/dist/public/{initChatSplitResize-llGkSJXg.js → initChatSplitResize-CzV2E7Ul.js} +1 -1
  58. package/dist/public/initControlPlane-C9jpnWev.js +1 -0
  59. package/dist/public/llms.txt +1 -1
  60. package/dist/public/main/index.html +1 -1
  61. package/dist/public/main/memory/index.html +1 -1
  62. package/dist/public/{markdown-CyYR4App.js → markdown-BnpKxSxl.js} +1 -1
  63. package/dist/public/{mcp-reconnect-CSx4aMwy.js → mcp-reconnect-9emsqGRe.js} +1 -1
  64. package/dist/public/{memoryStorage-Dwg8NhAZ.js → memoryStorage-C0KvLNUp.js} +1 -1
  65. package/dist/public/{orchestrator-DRfEudXk.js → orchestrator-3h9xxHUm.js} +676 -676
  66. package/dist/public/orchestrator-nJj59RqD.js +15 -0
  67. package/dist/public/{peerjs-CurBuKvk.js → peerjs-BP4laMXj.js} +1 -1
  68. package/dist/public/{push-client-Dii-vPEh.js → push-client-CrjEXK3U.js} +1 -1
  69. package/dist/public/service-worker/push-handler.js +1 -1
  70. package/dist/public/service-worker.js +1 -1
  71. package/dist/public/{shadow-claw-5IHaDDMJ.js → shadow-claw-CbwETBWf.js} +2 -2
  72. package/dist/public/{shadow-claw-accounts-CZvz4iQz.js → shadow-claw-accounts-B4sBTQEj.js} +1 -1
  73. package/dist/public/{shadow-claw-channels-BKXFyRS3.js → shadow-claw-channels-BDWG5Qa3.js} +1 -1
  74. package/dist/public/{shadow-claw-chat-CCLuhWdI.js → shadow-claw-chat-Crv0DTCY.js} +6 -6
  75. package/dist/public/shadow-claw-control-plane-CX1IGbh9.js +199 -0
  76. package/dist/public/{shadow-claw-conversations-DV-87UUh.js → shadow-claw-conversations-_9qAaRXk.js} +1 -1
  77. package/dist/public/shadow-claw-dialog-BrOwAOdk.js +1 -0
  78. package/dist/public/{shadow-claw-file-viewer-Dk2dbAUd.js → shadow-claw-file-viewer-Blf6yGVD.js} +1 -1
  79. package/dist/public/shadow-claw-files-CYySBDCl.js +840 -0
  80. package/dist/public/{shadow-claw-git-DRA7_qck.js → shadow-claw-git-v8S0JjuR.js} +2 -2
  81. package/dist/public/{shadow-claw-integrations-1Xu5snuV.js → shadow-claw-integrations-D7ufpwhX.js} +1 -1
  82. package/dist/public/{shadow-claw-llm-6Aci1BDP.js → shadow-claw-llm-GWgoVfhI.js} +2 -2
  83. package/dist/public/{shadow-claw-mcp-remote-BUjAeVXC.js → shadow-claw-mcp-remote-kY4LMFNN.js} +1 -1
  84. package/dist/public/{shadow-claw-networking-FQe4nCp3.js → shadow-claw-networking-qHbP95z9.js} +1 -1
  85. package/dist/public/{shadow-claw-notifications-DV2wibL3.js → shadow-claw-notifications-YyHOK2Fr.js} +1 -1
  86. package/dist/public/{shadow-claw-page-header-B63S4HTU.js → shadow-claw-page-header-DMKbkv3s.js} +1 -1
  87. package/dist/public/{shadow-claw-pages-DE3jWj5K.js → shadow-claw-pages-DFZHhzFZ.js} +1 -1
  88. package/dist/public/{shadow-claw-settings-S6DAKfYx.js → shadow-claw-settings-DhxrKIfA.js} +10 -3
  89. package/dist/public/{shadow-claw-storage-DaacA3_K.js → shadow-claw-storage-BFynbP0j.js} +2 -2
  90. package/dist/public/{shadow-claw-task-server-IjKX042p.js → shadow-claw-task-server-3BIeb5oZ.js} +25 -4
  91. package/dist/public/{shadow-claw-tasks-Cy0JYmGU.js → shadow-claw-tasks-Caxc9atL.js} +1 -1
  92. package/dist/public/{shadow-claw-terminal-znAt7HyP.js → shadow-claw-terminal-Beb5qF-U.js} +1 -1
  93. package/dist/public/{shadow-claw-tools-Cutv9Si5.js → shadow-claw-tools-BfJx3A9W.js} +1 -1
  94. package/dist/public/{shadow-claw-webvm-BH8mPDEu.js → shadow-claw-webvm-BCS8ykeP.js} +1 -1
  95. package/dist/public/{syncWebMcpRegistration-BqJxaMGR.js → syncWebMcpRegistration-B_M2fCjH.js} +1 -1
  96. package/dist/server.js +286 -189
  97. package/package.json +2 -1
  98. package/src/__mocks__/jszip.cjs +15 -5
  99. package/src/components/settings/shadow-claw-accounts/shadow-claw-accounts.test.ts +35 -0
  100. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +117 -0
  101. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +81 -0
  102. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.test.ts +127 -0
  103. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.ts +292 -0
  104. package/src/components/settings/shadow-claw-git/shadow-claw-git.test.ts +72 -0
  105. package/src/components/settings/shadow-claw-integrations/connection-test-auth.test.ts +20 -30
  106. package/src/components/settings/shadow-claw-integrations/shadow-claw-integrations.test.ts +116 -0
  107. package/src/components/settings/shadow-claw-llm/model-ranking.test.ts +143 -51
  108. package/src/components/settings/shadow-claw-llm/shadow-claw-llm.test.ts +307 -4
  109. package/src/components/settings/shadow-claw-llm/shadow-claw-llm.ts +8 -8
  110. package/src/components/settings/shadow-claw-mcp-remote/shadow-claw-mcp-remote.test.ts +31 -0
  111. package/src/components/settings/shadow-claw-peerjs/shadow-claw-peerjs.test.ts +186 -0
  112. package/src/components/settings/shadow-claw-storage/shadow-claw-storage.test.ts +76 -0
  113. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +23 -2
  114. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
  115. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.test.ts +19 -0
  116. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.ts +2 -1
  117. package/src/components/shadow-claw/utils/initChatSplitResize.test.ts +180 -13
  118. package/src/components/shadow-claw/utils/setupEffects.test.ts +39 -0
  119. package/src/components/shadow-claw-a2ui/catalog/basic/shared.test.ts +117 -0
  120. package/src/components/shadow-claw-a2ui/shadow-claw-a2ui.test.ts +1159 -1
  121. package/src/components/shadow-claw-chat/shadow-claw-chat.test.ts +1523 -19
  122. package/src/components/shadow-claw-chat/utils/escapeHtml.test.ts +13 -0
  123. package/src/components/shadow-claw-chat/utils/getPeerChatDisplayStatus.test.ts +20 -0
  124. package/src/components/shadow-claw-conversations/shadow-claw-conversations.test.ts +494 -0
  125. package/src/components/shadow-claw-dialog/shadow-claw-dialog.ts +13 -2
  126. package/src/components/shadow-claw-file-viewer/shadow-claw-file-viewer.test.ts +273 -0
  127. package/src/components/shadow-claw-files/shadow-claw-files.test.ts +408 -1
  128. package/src/components/shadow-claw-pdf-viewer/utils/installGetOrInsertComputedPolyfill.test.ts +65 -0
  129. package/src/components/shadow-claw-settings/shadow-claw-settings.html +7 -0
  130. package/src/components/shadow-claw-settings/shadow-claw-settings.test.ts +221 -0
  131. package/src/components/shadow-claw-settings/shadow-claw-settings.ts +2 -0
  132. package/src/components/shadow-claw-tasks/shadow-claw-tasks.test.ts +407 -0
  133. package/src/components/shadow-claw-terminal/utils/consumeEscapeSequence.test.ts +58 -0
  134. package/src/components/shadow-claw-tools/shadow-claw-tools.test.ts +80 -0
  135. package/src/config/config.ts +10 -0
  136. package/src/core/backup-controller.test.ts +101 -0
  137. package/src/core/backup-controller.ts +137 -0
  138. package/src/core/control-plane-client.test.ts +408 -0
  139. package/src/core/control-plane-client.ts +425 -0
  140. package/src/core/orchestrator/orchestrator.ts +4 -0
  141. package/src/core/orchestrator/utils/compactContext.test.ts +78 -0
  142. package/src/core/orchestrator/utils/operations/channel.test.ts +254 -9
  143. package/src/core/orchestrator/utils/operations/provider.test.ts +326 -34
  144. package/src/core/orchestrator/utils/operations/vm.test.ts +114 -2
  145. package/src/core/orchestrator/utils/syncProxyConfigToServiceWorker.test.ts +60 -0
  146. package/src/core/utils/initControlPlane.test.ts +276 -0
  147. package/src/core/utils/initControlPlane.ts +437 -0
  148. package/src/core/utils/initializeApp.ts +18 -0
  149. package/src/db/getSession.test.ts +13 -1
  150. package/src/server/app.test.ts +12 -0
  151. package/src/server/app.ts +18 -8
  152. package/src/server/client-registry.test.ts +250 -0
  153. package/src/server/client-registry.ts +477 -0
  154. package/src/server/config.ts +15 -0
  155. package/src/server/control-plane-types.ts +120 -0
  156. package/src/server/control-plane.test.ts +472 -0
  157. package/src/server/control-plane.ts +581 -0
  158. package/src/server/routes/backup.test.ts +263 -0
  159. package/src/server/routes/backup.ts +262 -0
  160. package/src/server/routes/integrations-email.test.ts +532 -0
  161. package/src/server/routes/llamafile.test.ts +172 -48
  162. package/src/server/routes/mesh-llm.test.ts +192 -0
  163. package/src/server/routes/ollama.test.ts +49 -0
  164. package/src/server/server-peer.test.ts +86 -0
  165. package/src/server/server-peer.ts +330 -0
  166. package/src/server/server.ts +35 -0
  167. package/src/server/services/llamafile-manager.test.ts +230 -6
  168. package/src/server/services/transformers-runtime.test.ts +186 -18
  169. package/src/server/webrtc.test.ts +21 -0
  170. package/src/server/webrtc.ts +72 -0
  171. package/src/service-worker/fetch-proxy.test.ts +64 -1
  172. package/src/service-worker/push-handler.ts +39 -0
  173. package/src/shell/vm.test.ts +744 -12
  174. package/src/storage/ensureMainGroupIndex.test.ts +153 -0
  175. package/src/storage/getGroupFile.test.ts +66 -0
  176. package/src/storage/memoryStorage.test.ts +82 -2
  177. package/src/storage/writeGroupFileBytes.test.ts +143 -0
  178. package/src/stores/chat-ui.test.ts +20 -0
  179. package/src/stores/orchestrator.test.ts +134 -0
  180. package/src/stores/toast.test.ts +50 -0
  181. package/src/stores/tools.test.ts +147 -10
  182. package/src/subsystems/channels/imessage.test.ts +59 -0
  183. package/src/subsystems/channels/peerjs.test.ts +500 -62
  184. package/src/subsystems/channels/peerjs.ts +71 -4
  185. package/src/subsystems/channels/telegram.test.ts +177 -0
  186. package/src/subsystems/email/catalog.test.ts +1 -0
  187. package/src/subsystems/email/connections.test.ts +39 -1
  188. package/src/subsystems/git/credentials.test.ts +160 -825
  189. package/src/subsystems/git/git.test.ts +463 -0
  190. package/src/subsystems/notifications/__mocks__/node-sqlite.cjs +12 -1
  191. package/src/subsystems/notifications/push-routes.test.ts +33 -0
  192. package/src/subsystems/notifications/push-routes.ts +23 -0
  193. package/src/subsystems/notifications/push-store.ts +1 -1
  194. package/src/subsystems/providers/builtin-ai-tasks.test.ts +74 -0
  195. package/src/subsystems/providers/litert-lm-provider.test.ts +212 -51
  196. package/src/subsystems/providers/model-registry.test.ts +115 -0
  197. package/src/subsystems/providers/providers.test.ts +94 -1
  198. package/src/subsystems/providers/transformers-js-provider.test.ts +279 -0
  199. package/src/subsystems/providers/utils/createModelCacheFetch.test.ts +2 -2
  200. package/src/subsystems/providers/utils/downloadModelToCache.test.ts +2 -2
  201. package/src/subsystems/providers/utils/loadModelStream.test.ts +2 -2
  202. package/src/subsystems/skills/activateSkill.test.ts +101 -0
  203. package/src/subsystems/skills/discoverSkills.test.ts +251 -0
  204. package/src/subsystems/tools/cron.test.ts +64 -0
  205. package/src/subsystems/tools/declarative.test.ts +255 -89
  206. package/src/testing/assets/v86.9pfs/libv86.mjs +63 -0
  207. package/src/testing/assets/v86.ext2/libv86.mjs +3 -0
  208. package/src/testing/assets_missing_v86/assets/v86.9pfs/libv86.mjs +1 -0
  209. package/src/testing/jest-setup.ts +328 -40
  210. package/src/testing/mock-v86.mjs +49 -0
  211. package/src/ui/a2ui/types.ts +4 -3
  212. package/src/ui/a2ui/utils/normaliseComponentsToMap.test.ts +102 -0
  213. package/src/ui/a2ui/utils/resolveChildIds.test.ts +63 -0
  214. package/src/ui/a2ui/utils/resolveDynamicNumber.test.ts +13 -1
  215. package/src/worker/tools/bash/utils/waitForVMReady.test.ts +40 -0
  216. package/src/worker/tools/email/email.test.ts +193 -8
  217. package/src/worker/tools/email/utils/asEmailAttachmentInputs.test.ts +47 -0
  218. package/src/worker/tools/git/git.test.ts +214 -3
  219. package/src/worker/tools/rooms/invite-to-room.test.ts +54 -0
  220. package/src/worker/tools/rooms/leave-room.test.ts +40 -0
  221. package/src/worker/tools/rooms/list-room-members.test.ts +75 -0
  222. package/src/worker/tools/tasks/tasks-utils.test.ts +22 -0
  223. package/src/worker/tools/ui/ask-user.test.ts +50 -0
  224. package/src/worker/tools/ui/clear-chat.test.ts +19 -0
  225. package/src/worker/tools/ui/get-current-time.test.ts +19 -0
  226. package/src/worker/tools/ui/javascript.test.ts +116 -0
  227. package/src/worker/tools/ui/list-components.test.ts +10 -0
  228. package/src/worker/tools/ui/list-tool-profiles.test.ts +42 -0
  229. package/src/worker/tools/ui/manage-tools.test.ts +34 -0
  230. package/src/worker/tools/ui/render-component.test.ts +346 -0
  231. package/src/worker/tools/ui/send-notification.test.ts +40 -0
  232. package/src/worker/tools/ui/utils/getAllowFullInternetAccess.test.ts +31 -0
  233. package/src/worker/tools/ui/utils/parseBooleanConfig.test.ts +25 -0
  234. package/src/worker/tools/workspace/attach-file.test.ts +84 -0
  235. package/src/worker/tools/workspace/copy-file.test.ts +69 -0
  236. package/src/worker/tools/workspace/create-directory.test.ts +40 -0
  237. package/src/worker/tools/workspace/delete-file.test.ts +73 -0
  238. package/src/worker/tools/workspace/diff-files.test.ts +94 -0
  239. package/src/worker/tools/workspace/list-files.test.ts +36 -0
  240. package/src/worker/tools/workspace/move-file.test.ts +66 -0
  241. package/src/worker/tools/workspace/open-file.test.ts +57 -0
  242. package/src/worker/tools/workspace/patch-file.test.ts +81 -0
  243. package/src/worker/tools/workspace/read-file.test.ts +116 -0
  244. package/src/worker/tools/workspace/send-file.test.ts +82 -0
  245. package/src/worker/tools/workspace/update-memory.test.ts +29 -0
  246. package/src/worker/tools/workspace/write-file.test.ts +28 -0
  247. package/src/worker/utils/rate-limit.test.ts +199 -0
  248. package/src/worker/utils/tool-state.test.ts +29 -0
  249. package/src/worker/utils/transformers-js-utils.test.ts +59 -0
  250. package/dist/public/config-BYAuxx3j.js +0 -1
  251. package/dist/public/downloadGroupFile-Drdvg1Ev.js +0 -1
  252. package/dist/public/file-viewer-CcZYw-PS.js +0 -1
  253. package/dist/public/orchestrator-o9Fj67BN.js +0 -15
  254. package/dist/public/shadow-claw-dialog-ByyHgxM9.js +0 -1
  255. package/dist/public/shadow-claw-files-BpfpJIuP.js +0 -840
@@ -35,6 +35,24 @@ export async function initializeApp(
35
35
 
36
36
  await orchestratorStore.whenReady;
37
37
 
38
+ if (typeof window !== "undefined") {
39
+ try {
40
+ const {
41
+ createDefaultControlPlaneClient,
42
+ shouldConnectControlPlane,
43
+ isControlPlaneEnabledSync,
44
+ } = await import("./initControlPlane.js");
45
+
46
+ if (isControlPlaneEnabledSync() && shouldConnectControlPlane()) {
47
+ createDefaultControlPlaneClient({
48
+ orchestrator: uiElement.orchestrator,
49
+ });
50
+ }
51
+ } catch (err) {
52
+ console.warn("[ControlPlane] Failed to initialize:", err);
53
+ }
54
+ }
55
+
38
56
  return { orchestrator: uiElement.orchestrator, isInitializing };
39
57
  } catch (error) {
40
58
  console.error("❌ Failed to initialize ShadowClaw:", error);
@@ -11,7 +11,19 @@ describe("getSession", () => {
11
11
  it("reads session by group id", async () => {
12
12
  const session: any = { groupId: "g1", messages: [], updatedAt: 1 };
13
13
 
14
- (txPromise as any).mockResolvedValue(session);
14
+ (txPromise as any).mockImplementation(
15
+ async (
16
+ _db: any,
17
+ _storeName: any,
18
+ _mode: any,
19
+ fn: (store: any) => any,
20
+ ) => {
21
+ const mockStore = {
22
+ get: jest.fn().mockReturnValue(session),
23
+ };
24
+ return fn(mockStore);
25
+ },
26
+ );
15
27
 
16
28
  await expect(getSession({} as any, "g1")).resolves.toEqual(session);
17
29
 
@@ -43,6 +43,9 @@ describe("app", () => {
43
43
  jest.unstable_mockModule("./routes/oauth.js", () => ({
44
44
  registerOAuthRoutes: jest.fn(),
45
45
  }));
46
+ jest.unstable_mockModule("./routes/backup.js", () => ({
47
+ registerBackupRoutes: jest.fn(),
48
+ }));
46
49
  jest.unstable_mockModule("./routes/csp-report.js", () => ({
47
50
  registerCspReportRoutes: jest.fn(),
48
51
  }));
@@ -92,6 +95,9 @@ describe("app", () => {
92
95
  jest.unstable_mockModule("./middleware/csp.js", () => ({
93
96
  createCspReportOnlyMiddleware: jest.fn(() => () => {}),
94
97
  }));
98
+ jest.unstable_mockModule("./client-registry.js", () => ({
99
+ openClientStore: jest.fn(),
100
+ }));
95
101
  jest.unstable_mockModule("./middleware/static-files.js", () => ({
96
102
  registerStaticFilesMiddleware: jest.fn(),
97
103
  }));
@@ -101,9 +107,11 @@ describe("app", () => {
101
107
  const { createApp } = await import("./app.js");
102
108
  const { registerProxyRoutes } = await import("./proxy.js");
103
109
  const { registerOAuthRoutes } = await import("./routes/oauth.js");
110
+ const { registerBackupRoutes } = await import("./routes/backup.js");
104
111
  const { registerCspReportRoutes } = await import("./routes/csp-report.js");
105
112
  const { openPushStore } =
106
113
  await import("../subsystems/notifications/push-store.js");
114
+ const { openClientStore } = await import("./client-registry.js");
107
115
  const { createCspReportOnlyMiddleware } =
108
116
  await import("./middleware/csp.js");
109
117
  const { registerStaticFilesMiddleware } =
@@ -118,11 +126,15 @@ describe("app", () => {
118
126
  verbose: false,
119
127
  });
120
128
  expect(registerOAuthRoutes).toHaveBeenCalledWith(appMock);
129
+ expect(registerBackupRoutes).toHaveBeenCalledTimes(1);
121
130
  expect(registerCspReportRoutes).toHaveBeenCalledTimes(1);
122
131
  expect(createCspReportOnlyMiddleware).toHaveBeenCalledTimes(1);
123
132
  expect(openPushStore).toHaveBeenCalledWith(
124
133
  expect.stringContaining("push-subscriptions.db"),
125
134
  );
135
+ expect(openClientStore).toHaveBeenCalledWith(
136
+ expect.stringContaining("clients.db"),
137
+ );
126
138
  expect(registerStaticFilesMiddleware).toHaveBeenCalledWith(
127
139
  appMock,
128
140
  config.rootPath,
package/src/server/app.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  registerPushRoutes,
9
9
  } from "../subsystems/notifications/push-routes.js";
10
10
  import { openPushStore } from "../subsystems/notifications/push-store.js";
11
+ import { openClientStore } from "./client-registry.js";
11
12
 
12
13
  import { registerTaskScheduleRoutes } from "../subsystems/notifications/task-schedule-routes.js";
13
14
  import {
@@ -28,6 +29,7 @@ import { registerProxyRoutes } from "./proxy.js";
28
29
  import { registerActivityLogRoutes } from "./routes/activity-log.js";
29
30
  import { registerCspReportRoutes } from "./routes/csp-report.js";
30
31
  import { registerOAuthRoutes } from "./routes/oauth.js";
32
+ import { registerBackupRoutes } from "./routes/backup.js";
31
33
 
32
34
  import type { Express } from "express";
33
35
  import type { ServerConfig } from "./config.js";
@@ -67,6 +69,15 @@ export function createApp(config: ServerConfig): {
67
69
 
68
70
  app.use(express.json({ limit: "1000mb" }));
69
71
 
72
+ // ---------------- DATABASE & STORES ----------------
73
+ if (!fs.existsSync(config.databaseDir)) {
74
+ fs.mkdirSync(config.databaseDir, { recursive: true });
75
+ }
76
+
77
+ openPushStore(path.join(config.databaseDir, "push-subscriptions.db"));
78
+ openClientStore(path.join(config.databaseDir, "clients.db"));
79
+ openTaskScheduleStore(path.join(config.databaseDir, "scheduled-tasks.db"));
80
+
70
81
  // ---------------- PROXY ROUTES ----------------
71
82
  registerProxyRoutes(app, {
72
83
  verbose,
@@ -81,21 +92,20 @@ export function createApp(config: ServerConfig): {
81
92
  logsDir: path.resolve(config.databaseDir, "..", ".cache", "logs"),
82
93
  });
83
94
 
95
+ // ---------------- BACKUP ROUTES ----------------
96
+ registerBackupRoutes(app, {
97
+ backupsDir: path.resolve(config.databaseDir, "..", ".cache", "backups"),
98
+ token: config.controlToken,
99
+ });
100
+
84
101
  // ---------------- CSP REPORT ROUTES ----------------
85
102
  registerCspReportRoutes(app, {
86
103
  logsDir: path.resolve(config.databaseDir, "..", ".cache", "logs"),
87
104
  logger,
88
105
  });
89
106
 
90
- // ---------------- DATABASE & PUSH / TASKS ----------------
91
- if (!fs.existsSync(config.databaseDir)) {
92
- fs.mkdirSync(config.databaseDir, { recursive: true });
93
- }
94
-
95
- openPushStore(path.join(config.databaseDir, "push-subscriptions.db"));
107
+ // ---------------- PUSH & TASK ROUTES ----------------
96
108
  registerPushRoutes(app);
97
-
98
- openTaskScheduleStore(path.join(config.databaseDir, "scheduled-tasks.db"));
99
109
  registerTaskScheduleRoutes(app);
100
110
 
101
111
  const scheduler = new ServerTaskScheduler({
@@ -0,0 +1,250 @@
1
+ import {
2
+ openClientStore,
3
+ closeClientStore,
4
+ registerClient,
5
+ updateClientHeartbeat,
6
+ unregisterClient,
7
+ getClient,
8
+ getAllClients,
9
+ pruneStaleClients,
10
+ generateControlToken,
11
+ getOrCreateControlToken,
12
+ saveControlTokenFile,
13
+ readControlTokenFile,
14
+ getControlTokenFilePath,
15
+ } from "./client-registry.js";
16
+ import fs from "node:fs";
17
+ import path from "node:path";
18
+
19
+ beforeEach(() => {
20
+ openClientStore(":memory:");
21
+ });
22
+
23
+ afterEach(() => {
24
+ closeClientStore();
25
+ });
26
+
27
+ const MOCK_CLIENT_1 = {
28
+ clientId: "sc-client-alpha",
29
+ deviceLabel: "iPad Safari",
30
+ capabilities: ["opfs", "webmcp", "push"],
31
+ version: "1.23.4",
32
+ };
33
+
34
+ const MOCK_CLIENT_2 = {
35
+ clientId: "sc-client-beta",
36
+ deviceLabel: "Desktop Chrome",
37
+ capabilities: ["opfs", "webmcp", "peerjs"],
38
+ version: "1.23.4",
39
+ };
40
+
41
+ describe("client-registry", () => {
42
+ describe("openClientStore", () => {
43
+ it("creates tables on first open", () => {
44
+ const db = openClientStore(":memory:");
45
+ const clients = db.prepare("SELECT * FROM clients").all();
46
+ expect(clients).toEqual([]);
47
+ });
48
+
49
+ it("returns the existing database instance if already opened", () => {
50
+ const db1 = openClientStore(":memory:");
51
+ const db2 = openClientStore(":memory:");
52
+ expect(db1).toBe(db2);
53
+ });
54
+ });
55
+
56
+ describe("registerClient", () => {
57
+ it("registers a new client with timestamps", () => {
58
+ const client = registerClient(MOCK_CLIENT_1);
59
+ expect(client.clientId).toBe(MOCK_CLIENT_1.clientId);
60
+ expect(client.deviceLabel).toBe(MOCK_CLIENT_1.deviceLabel);
61
+ expect(client.capabilities).toEqual(MOCK_CLIENT_1.capabilities);
62
+ expect(client.version).toBe(MOCK_CLIENT_1.version);
63
+ expect(typeof client.connectedAt).toBe("number");
64
+ expect(typeof client.lastSeen).toBe("number");
65
+
66
+ const fetched = getClient(MOCK_CLIENT_1.clientId);
67
+ expect(fetched).toEqual(client);
68
+ });
69
+
70
+ it("updates existing client registration when re-registered", () => {
71
+ registerClient(MOCK_CLIENT_1);
72
+ const updated = registerClient({
73
+ ...MOCK_CLIENT_1,
74
+ deviceLabel: "iPad Pro Safari (Updated)",
75
+ capabilities: ["opfs", "webmcp", "push", "peerjs"],
76
+ });
77
+
78
+ expect(updated.deviceLabel).toBe("iPad Pro Safari (Updated)");
79
+ expect(updated.capabilities).toEqual([
80
+ "opfs",
81
+ "webmcp",
82
+ "push",
83
+ "peerjs",
84
+ ]);
85
+
86
+ const all = getAllClients();
87
+ expect(all).toHaveLength(1);
88
+ expect(all[0].deviceLabel).toBe("iPad Pro Safari (Updated)");
89
+ });
90
+ });
91
+
92
+ describe("updateClientHeartbeat", () => {
93
+ it("updates lastSeen timestamp for an existing client", () => {
94
+ registerClient(MOCK_CLIENT_1);
95
+ const before = getClient(MOCK_CLIENT_1.clientId)!;
96
+
97
+ const targetTime = before.lastSeen + 5000;
98
+ const updated = updateClientHeartbeat(MOCK_CLIENT_1.clientId, targetTime);
99
+ expect(updated).toBe(true);
100
+
101
+ const after = getClient(MOCK_CLIENT_1.clientId)!;
102
+ expect(after.lastSeen).toBe(targetTime);
103
+ expect(after.connectedAt).toBe(before.connectedAt);
104
+ });
105
+
106
+ it("returns false when updating a nonexistent client", () => {
107
+ const updated = updateClientHeartbeat("nonexistent-client");
108
+ expect(updated).toBe(false);
109
+ });
110
+ });
111
+
112
+ describe("unregisterClient", () => {
113
+ it("removes a registered client", () => {
114
+ registerClient(MOCK_CLIENT_1);
115
+ registerClient(MOCK_CLIENT_2);
116
+ expect(getAllClients()).toHaveLength(2);
117
+
118
+ const removed = unregisterClient(MOCK_CLIENT_1.clientId);
119
+ expect(removed).toBe(true);
120
+
121
+ expect(getClient(MOCK_CLIENT_1.clientId)).toBeNull();
122
+ expect(getAllClients()).toHaveLength(1);
123
+ expect(getAllClients()[0].clientId).toBe(MOCK_CLIENT_2.clientId);
124
+ });
125
+
126
+ it("returns false when unregistering a nonexistent client", () => {
127
+ const removed = unregisterClient("nonexistent-client");
128
+ expect(removed).toBe(false);
129
+ });
130
+ });
131
+
132
+ describe("getAllClients", () => {
133
+ it("returns all registered clients ordered by lastSeen descending", () => {
134
+ registerClient(MOCK_CLIENT_1);
135
+ registerClient(MOCK_CLIENT_2);
136
+
137
+ updateClientHeartbeat(MOCK_CLIENT_1.clientId, 1000);
138
+ updateClientHeartbeat(MOCK_CLIENT_2.clientId, 2000);
139
+
140
+ const clients = getAllClients();
141
+ expect(clients).toHaveLength(2);
142
+ expect(clients[0].clientId).toBe(MOCK_CLIENT_2.clientId);
143
+ expect(clients[1].clientId).toBe(MOCK_CLIENT_1.clientId);
144
+ });
145
+ });
146
+
147
+ describe("pruneStaleClients", () => {
148
+ it("prunes clients whose lastSeen is older than maxAgeMs", () => {
149
+ const now = 100_000;
150
+ registerClient(MOCK_CLIENT_1);
151
+ registerClient(MOCK_CLIENT_2);
152
+
153
+ // Client 1 was seen 50s ago, Client 2 was seen 10s ago
154
+ updateClientHeartbeat(MOCK_CLIENT_1.clientId, now - 50_000);
155
+ updateClientHeartbeat(MOCK_CLIENT_2.clientId, now - 10_000);
156
+
157
+ // Prune anything older than 30s
158
+ const prunedCount = pruneStaleClients(30_000, now);
159
+ expect(prunedCount).toBe(1);
160
+
161
+ const remaining = getAllClients();
162
+ expect(remaining).toHaveLength(1);
163
+ expect(remaining[0].clientId).toBe(MOCK_CLIENT_2.clientId);
164
+ });
165
+ });
166
+
167
+ describe("generateControlToken", () => {
168
+ it("generates a high-entropy random hex token (64 characters / 256-bit)", () => {
169
+ const token1 = generateControlToken();
170
+ const token2 = generateControlToken();
171
+ expect(typeof token1).toBe("string");
172
+ expect(token1).toHaveLength(64);
173
+ expect(token2).toHaveLength(64);
174
+ expect(token1).not.toBe(token2);
175
+ expect(/^[0-9a-f]{64}$/.test(token1)).toBe(true);
176
+ });
177
+
178
+ it("supports custom byte lengths", () => {
179
+ const token = generateControlToken(16);
180
+ expect(token).toHaveLength(32);
181
+ expect(/^[0-9a-f]{32}$/.test(token)).toBe(true);
182
+ });
183
+ });
184
+
185
+ describe("getOrCreateControlToken", () => {
186
+ let tempCacheDir: string;
187
+
188
+ beforeEach(() => {
189
+ tempCacheDir = path.join(
190
+ process.cwd(),
191
+ ".cache",
192
+ "test-token-unit-" +
193
+ Date.now() +
194
+ "-" +
195
+ Math.random().toString(36).slice(2),
196
+ );
197
+ });
198
+
199
+ afterEach(() => {
200
+ try {
201
+ fs.rmSync(tempCacheDir, { recursive: true, force: true });
202
+ } catch (_) {}
203
+ });
204
+
205
+ it("generates a new token if none exists", () => {
206
+ const token = getOrCreateControlToken(undefined, tempCacheDir);
207
+ expect(typeof token).toBe("string");
208
+ expect(token).toHaveLength(64);
209
+ });
210
+
211
+ it("returns the existing token on subsequent calls", () => {
212
+ const token1 = getOrCreateControlToken(undefined, tempCacheDir);
213
+ const token2 = getOrCreateControlToken(undefined, tempCacheDir);
214
+ expect(token1).toBe(token2);
215
+ });
216
+
217
+ it("persists a custom configured token if provided", () => {
218
+ closeClientStore();
219
+ openClientStore(":memory:");
220
+ const customToken = "custom-secret-token-123456";
221
+ const token = getOrCreateControlToken(customToken, tempCacheDir);
222
+ expect(token).toBe(customToken);
223
+
224
+ const fetched = getOrCreateControlToken(undefined, tempCacheDir);
225
+ expect(fetched).toBe(customToken);
226
+ });
227
+
228
+ it("saves and reads token to .cache/control-token.json with timestamps", () => {
229
+ const saved = saveControlTokenFile(
230
+ "my-secret-test-token",
231
+ tempCacheDir,
232
+ 1700000000000,
233
+ );
234
+
235
+ expect(saved.token).toBe("my-secret-test-token");
236
+ expect(saved.createdAt).toBe(1700000000000);
237
+ expect(saved.createdAtIso).toBe(new Date(1700000000000).toISOString());
238
+
239
+ const read = readControlTokenFile(tempCacheDir);
240
+ expect(read).toEqual(saved);
241
+
242
+ const filePath = getControlTokenFilePath(tempCacheDir);
243
+ expect(fs.existsSync(filePath)).toBe(true);
244
+
245
+ const rawJson = JSON.parse(fs.readFileSync(filePath, "utf8"));
246
+ expect(rawJson.token).toBe("my-secret-test-token");
247
+ expect(rawJson.createdAt).toBe(1700000000000);
248
+ });
249
+ });
250
+ });