@celljs/ta 4.12.0 → 4.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/dist/cli.mjs +1006 -164
- package/dist/gateway.mjs +182 -2
- package/dist/pi/CHANGELOG.md +15 -0
- package/dist/pi/package.json +1 -1
- package/dist/pi/packages/ta-agents/README.en.md +1 -1
- package/dist/pi/packages/ta-agents/README.md +1 -1
- package/dist/pi/packages/ta-agents/extension/index.mjs +75 -11
- package/dist/pi/packages/ta-agents/lib/acp-protocol.d.ts +2 -2
- package/dist/pi/packages/ta-agents/lib/acp-protocol.js +5 -2
- package/dist/pi/packages/ta-agents/lib/agent-prompt.d.ts +6 -4
- package/dist/pi/packages/ta-agents/lib/agent-prompt.js +7 -5
- package/dist/pi/packages/ta-agents/lib/agent-session.d.ts +2 -0
- package/dist/pi/packages/ta-agents/lib/agent-session.js +7 -0
- package/dist/pi/packages/ta-agents/lib/agent-tool.d.ts +2 -0
- package/dist/pi/packages/ta-agents/lib/agent-tool.js +44 -3
- package/dist/pi/packages/ta-agents/lib/agent-turn.d.ts +12 -2
- package/dist/pi/packages/ta-agents/lib/agent-turn.js +29 -4
- package/dist/pi/packages/ta-agents/package.json +1 -1
- package/dist/pi/packages/ta-bots/extension/index.mjs +72 -0
- package/dist/pi/packages/ta-bots/host/index.mjs +178 -0
- package/dist/pi/packages/ta-bots/package.json +7 -2
- package/dist/pi/packages/ta-changes/extension/index.mjs +72 -0
- package/dist/pi/packages/ta-changes/package.json +8 -2
- package/dist/pi/packages/ta-channels/README.en.md +3 -1
- package/dist/pi/packages/ta-channels/README.md +3 -1
- package/dist/pi/packages/ta-channels/extension/index.mjs +78 -4
- package/dist/pi/packages/ta-channels/host/index.mjs +289 -8
- package/dist/pi/packages/ta-channels/lib/channels-model.d.ts +25 -0
- package/dist/pi/packages/ta-channels/lib/channels-util.js +8 -2
- package/dist/pi/packages/ta-channels/lib/extension.js +2 -2
- package/dist/pi/packages/ta-channels/lib/i18n.d.ts +1 -0
- package/dist/pi/packages/ta-channels/lib/i18n.js +2 -0
- package/dist/pi/packages/ta-channels/lib/room.d.ts +0 -4
- package/dist/pi/packages/ta-channels/lib/room.js +31 -5
- package/dist/pi/packages/ta-channels/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-channels/lib/style.js +7 -13
- package/dist/pi/packages/ta-channels/lib/team-view.d.ts +19 -0
- package/dist/pi/packages/ta-channels/lib/team-view.js +45 -0
- package/dist/pi/packages/ta-channels/lib/team.d.ts +20 -4
- package/dist/pi/packages/ta-channels/lib/team.js +87 -6
- package/dist/pi/packages/ta-channels/package.json +26 -2
- package/dist/pi/packages/ta-channels/web/index.js +137 -27
- package/dist/pi/packages/ta-computer-use/package.json +1 -1
- package/dist/pi/packages/ta-cron/README.en.md +2 -2
- package/dist/pi/packages/ta-cron/README.md +2 -2
- package/dist/pi/packages/ta-cron/extension/index.mjs +113 -258
- package/dist/pi/packages/ta-cron/host/index.mjs +9 -0
- package/dist/pi/packages/ta-cron/lib/chat-task.d.ts +16 -15
- package/dist/pi/packages/ta-cron/lib/chat-task.js +45 -36
- package/dist/pi/packages/ta-cron/lib/extension.js +12 -8
- package/dist/pi/packages/ta-cron/lib/i18n.d.ts +5 -7
- package/dist/pi/packages/ta-cron/lib/i18n.js +10 -14
- package/dist/pi/packages/ta-cron/lib/index.js +0 -2
- package/dist/pi/packages/ta-cron/lib/page.d.ts +5 -0
- package/dist/pi/packages/ta-cron/lib/page.js +24 -0
- package/dist/pi/packages/ta-cron/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-cron/lib/style.js +8 -0
- package/dist/pi/packages/ta-cron/lib/task-change.d.ts +11 -0
- package/dist/pi/packages/ta-cron/lib/task-change.js +43 -0
- package/dist/pi/packages/ta-cron/lib/task-extension.d.ts +3 -0
- package/dist/pi/packages/ta-cron/lib/task-extension.js +2 -0
- package/dist/pi/packages/ta-cron/lib/task-util.d.ts +5 -0
- package/dist/pi/packages/ta-cron/lib/task-util.js +13 -0
- package/dist/pi/packages/ta-cron/lib/tasks-view.js +26 -2
- package/dist/pi/packages/ta-cron/package.json +10 -2
- package/dist/pi/packages/ta-cron/web/index.js +607 -784
- package/dist/pi/packages/ta-extensions/README.en.md +1 -2
- package/dist/pi/packages/ta-extensions/README.md +1 -2
- package/dist/pi/packages/ta-extensions/lib/i18n.d.ts +0 -4
- package/dist/pi/packages/ta-extensions/lib/i18n.js +2 -10
- package/dist/pi/packages/ta-extensions/lib/index.js +0 -2
- package/dist/pi/packages/ta-extensions/package.json +10 -2
- package/dist/pi/packages/ta-extensions/skills/ta-package/SKILL.md +15 -11
- package/dist/pi/packages/ta-extensions/web/index.js +197 -97
- package/dist/pi/packages/ta-games/package.json +8 -2
- package/dist/pi/packages/ta-glance/README.en.md +1 -0
- package/dist/pi/packages/ta-glance/README.md +1 -0
- package/dist/pi/packages/ta-glance/extension/index.mjs +446 -3
- package/dist/pi/packages/ta-glance/lib/card.js +38 -12
- package/dist/pi/packages/ta-glance/lib/extension.d.ts +3 -0
- package/dist/pi/packages/ta-glance/lib/extension.js +10 -0
- package/dist/pi/packages/ta-glance/lib/glance-ask.d.ts +15 -0
- package/dist/pi/packages/ta-glance/lib/glance-ask.js +97 -0
- package/dist/pi/packages/ta-glance/lib/glance-prompt.d.ts +1 -1
- package/dist/pi/packages/ta-glance/lib/glance-prompt.js +3 -3
- package/dist/pi/packages/ta-glance/lib/glance-protocol.d.ts +2 -0
- package/dist/pi/packages/ta-glance/lib/glance-protocol.js +3 -1
- package/dist/pi/packages/ta-glance/lib/glance-spec.d.ts +6 -1
- package/dist/pi/packages/ta-glance/lib/glance-spec.js +41 -7
- package/dist/pi/packages/ta-glance/lib/i18n.d.ts +5 -0
- package/dist/pi/packages/ta-glance/lib/i18n.js +9 -0
- package/dist/pi/packages/ta-glance/lib/index.js +4 -1
- package/dist/pi/packages/ta-glance/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-glance/lib/style.js +9 -1
- package/dist/pi/packages/ta-glance/package.json +1 -1
- package/dist/pi/packages/ta-glance/web/index.js +87 -19
- package/dist/pi/packages/ta-initiative/README.en.md +66 -0
- package/dist/pi/packages/ta-initiative/README.md +66 -0
- package/dist/pi/packages/ta-initiative/extension/index.mjs +741 -0
- package/dist/pi/packages/ta-initiative/host/index.mjs +1169 -0
- package/dist/pi/packages/ta-initiative/lib/chat-kinds.d.ts +4 -0
- package/dist/pi/packages/ta-initiative/lib/chat-kinds.js +17 -0
- package/dist/pi/packages/ta-initiative/lib/chat-proposals.d.ts +28 -0
- package/dist/pi/packages/ta-initiative/lib/chat-proposals.js +79 -0
- package/dist/pi/packages/ta-initiative/lib/conversation.d.ts +4 -0
- package/dist/pi/packages/ta-initiative/lib/conversation.js +27 -0
- package/dist/pi/packages/ta-initiative/lib/cron-proposals.d.ts +23 -0
- package/dist/pi/packages/ta-initiative/lib/cron-proposals.js +94 -0
- package/dist/pi/packages/ta-initiative/lib/event-proposals.d.ts +13 -0
- package/dist/pi/packages/ta-initiative/lib/event-proposals.js +76 -0
- package/dist/pi/packages/ta-initiative/lib/extension.d.ts +30 -0
- package/dist/pi/packages/ta-initiative/lib/extension.js +237 -0
- package/dist/pi/packages/ta-initiative/lib/host.d.ts +4 -0
- package/dist/pi/packages/ta-initiative/lib/host.js +11 -0
- package/dist/pi/packages/ta-initiative/lib/i18n.d.ts +79 -0
- package/dist/pi/packages/ta-initiative/lib/i18n.js +159 -0
- package/dist/pi/packages/ta-initiative/lib/inbox-util.d.ts +19 -0
- package/dist/pi/packages/ta-initiative/lib/inbox-util.js +45 -0
- package/dist/pi/packages/ta-initiative/lib/inbox.d.ts +23 -0
- package/dist/pi/packages/ta-initiative/lib/inbox.js +142 -0
- package/dist/pi/packages/ta-initiative/lib/index.d.ts +10 -0
- package/dist/pi/packages/ta-initiative/lib/index.js +121 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-protocol.d.ts +320 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-protocol.js +96 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-service.d.ts +37 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-service.js +104 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-store.d.ts +44 -0
- package/dist/pi/packages/ta-initiative/lib/initiative-store.js +112 -0
- package/dist/pi/packages/ta-initiative/lib/level-util.d.ts +22 -0
- package/dist/pi/packages/ta-initiative/lib/level-util.js +86 -0
- package/dist/pi/packages/ta-initiative/lib/material-util.d.ts +34 -0
- package/dist/pi/packages/ta-initiative/lib/material-util.js +155 -0
- package/dist/pi/packages/ta-initiative/lib/notices.d.ts +13 -0
- package/dist/pi/packages/ta-initiative/lib/notices.js +65 -0
- package/dist/pi/packages/ta-initiative/lib/notify-util.d.ts +25 -0
- package/dist/pi/packages/ta-initiative/lib/notify-util.js +60 -0
- package/dist/pi/packages/ta-initiative/lib/pace-util.d.ts +21 -0
- package/dist/pi/packages/ta-initiative/lib/pace-util.js +60 -0
- package/dist/pi/packages/ta-initiative/lib/page-initiative.d.ts +9 -0
- package/dist/pi/packages/ta-initiative/lib/page-initiative.js +40 -0
- package/dist/pi/packages/ta-initiative/lib/proposal-book.d.ts +61 -0
- package/dist/pi/packages/ta-initiative/lib/proposal-book.js +397 -0
- package/dist/pi/packages/ta-initiative/lib/react.d.ts +11 -0
- package/dist/pi/packages/ta-initiative/lib/react.js +16 -0
- package/dist/pi/packages/ta-initiative/lib/review-service.d.ts +32 -0
- package/dist/pi/packages/ta-initiative/lib/review-service.js +155 -0
- package/dist/pi/packages/ta-initiative/lib/server-texts.d.ts +7 -0
- package/dist/pi/packages/ta-initiative/lib/server-texts.js +17 -0
- package/dist/pi/packages/ta-initiative/lib/settings.d.ts +10 -0
- package/dist/pi/packages/ta-initiative/lib/settings.js +226 -0
- package/dist/pi/packages/ta-initiative/lib/style.d.ts +4 -0
- package/dist/pi/packages/ta-initiative/lib/style.js +56 -0
- package/dist/pi/packages/ta-initiative/lib/tool.d.ts +106 -0
- package/dist/pi/packages/ta-initiative/lib/tool.js +98 -0
- package/dist/pi/packages/ta-initiative/package.json +63 -0
- package/dist/pi/packages/ta-initiative/web/index.js +1226 -0
- package/dist/pi/packages/ta-launcher/README.en.md +2 -1
- package/dist/pi/packages/ta-launcher/README.md +2 -1
- package/dist/pi/packages/ta-launcher/lib/i18n.d.ts +2 -0
- package/dist/pi/packages/ta-launcher/lib/i18n.js +4 -0
- package/dist/pi/packages/ta-launcher/lib/launcher-view.js +32 -0
- package/dist/pi/packages/ta-launcher/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-launcher/lib/style.js +11 -0
- package/dist/pi/packages/ta-launcher/package.json +1 -1
- package/dist/pi/packages/ta-launcher/web/index.js +58 -1
- package/dist/pi/packages/ta-manual/lib/pictures-b.js +29 -0
- package/dist/pi/packages/ta-manual/lib/pictures-c.js +109 -2
- package/dist/pi/packages/ta-manual/lib/topics.d.ts +4 -0
- package/dist/pi/packages/ta-manual/lib/topics.js +123 -11
- package/dist/pi/packages/ta-manual/package.json +1 -1
- package/dist/pi/packages/ta-manual/skills/ta-manual/SKILL.md +45 -10
- package/dist/pi/packages/ta-manual/web/index.js +567 -12
- package/dist/pi/packages/ta-moments/README.en.md +29 -0
- package/dist/pi/packages/ta-moments/README.md +29 -0
- package/dist/pi/packages/ta-moments/extension/index.mjs +1787 -0
- package/dist/pi/packages/ta-moments/host/index.mjs +2415 -0
- package/dist/pi/packages/ta-moments/lib/brief-util.d.ts +30 -0
- package/dist/pi/packages/ta-moments/lib/brief-util.js +92 -0
- package/dist/pi/packages/ta-moments/lib/context.d.ts +18 -0
- package/dist/pi/packages/ta-moments/lib/context.js +35 -0
- package/dist/pi/packages/ta-moments/lib/draft-card.d.ts +6 -0
- package/dist/pi/packages/ta-moments/lib/draft-card.js +60 -0
- package/dist/pi/packages/ta-moments/lib/editor.d.ts +16 -0
- package/dist/pi/packages/ta-moments/lib/editor.js +126 -0
- package/dist/pi/packages/ta-moments/lib/extension.d.ts +24 -0
- package/dist/pi/packages/ta-moments/lib/extension.js +136 -0
- package/dist/pi/packages/ta-moments/lib/friends.d.ts +13 -0
- package/dist/pi/packages/ta-moments/lib/friends.js +27 -0
- package/dist/pi/packages/ta-moments/lib/host.d.ts +4 -0
- package/dist/pi/packages/ta-moments/lib/host.js +11 -0
- package/dist/pi/packages/ta-moments/lib/i18n.d.ts +92 -0
- package/dist/pi/packages/ta-moments/lib/i18n.js +201 -0
- package/dist/pi/packages/ta-moments/lib/image-util.d.ts +18 -0
- package/dist/pi/packages/ta-moments/lib/image-util.js +71 -0
- package/dist/pi/packages/ta-moments/lib/index.d.ts +8 -0
- package/dist/pi/packages/ta-moments/lib/index.js +50 -0
- package/dist/pi/packages/ta-moments/lib/moments-host.d.ts +133 -0
- package/dist/pi/packages/ta-moments/lib/moments-host.js +821 -0
- package/dist/pi/packages/ta-moments/lib/moments-protocol.d.ts +334 -0
- package/dist/pi/packages/ta-moments/lib/moments-protocol.js +99 -0
- package/dist/pi/packages/ta-moments/lib/page.d.ts +7 -0
- package/dist/pi/packages/ta-moments/lib/page.js +307 -0
- package/dist/pi/packages/ta-moments/lib/post-util.d.ts +31 -0
- package/dist/pi/packages/ta-moments/lib/post-util.js +205 -0
- package/dist/pi/packages/ta-moments/lib/server-texts.d.ts +29 -0
- package/dist/pi/packages/ta-moments/lib/server-texts.js +104 -0
- package/dist/pi/packages/ta-moments/lib/store.d.ts +33 -0
- package/dist/pi/packages/ta-moments/lib/store.js +108 -0
- package/dist/pi/packages/ta-moments/lib/style.d.ts +4 -0
- package/dist/pi/packages/ta-moments/lib/style.js +155 -0
- package/dist/pi/packages/ta-moments/lib/talk-host.d.ts +112 -0
- package/dist/pi/packages/ta-moments/lib/talk-host.js +582 -0
- package/dist/pi/packages/ta-moments/lib/talk-util.d.ts +26 -0
- package/dist/pi/packages/ta-moments/lib/talk-util.js +145 -0
- package/dist/pi/packages/ta-moments/lib/tool.d.ts +48 -0
- package/dist/pi/packages/ta-moments/lib/tool.js +56 -0
- package/dist/pi/packages/ta-moments/package.json +61 -0
- package/dist/pi/packages/ta-moments/web/index.js +2008 -0
- package/dist/pi/packages/ta-nearby/README.en.md +33 -0
- package/dist/pi/packages/ta-nearby/README.md +33 -0
- package/dist/pi/packages/ta-nearby/extension/index.mjs +410 -0
- package/dist/pi/packages/ta-nearby/host/index.mjs +1859 -0
- package/dist/pi/packages/ta-nearby/lib/extension.d.ts +27 -0
- package/dist/pi/packages/ta-nearby/lib/extension.js +190 -0
- package/dist/pi/packages/ta-nearby/lib/host.d.ts +7 -0
- package/dist/pi/packages/ta-nearby/lib/host.js +20 -0
- package/dist/pi/packages/ta-nearby/lib/i18n.d.ts +92 -0
- package/dist/pi/packages/ta-nearby/lib/i18n.js +183 -0
- package/dist/pi/packages/ta-nearby/lib/index.d.ts +8 -0
- package/dist/pi/packages/ta-nearby/lib/index.js +38 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-crypto.d.ts +31 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-crypto.js +59 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-meet.d.ts +35 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-meet.js +93 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-net.d.ts +59 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-net.js +253 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-protocol.d.ts +274 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-protocol.js +31 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-service.d.ts +199 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-service.js +1012 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-spot.d.ts +50 -0
- package/dist/pi/packages/ta-nearby/lib/nearby-spot.js +167 -0
- package/dist/pi/packages/ta-nearby/lib/page.d.ts +52 -0
- package/dist/pi/packages/ta-nearby/lib/page.js +588 -0
- package/dist/pi/packages/ta-nearby/lib/react.d.ts +8 -0
- package/dist/pi/packages/ta-nearby/lib/react.js +8 -0
- package/dist/pi/packages/ta-nearby/lib/server-texts.d.ts +45 -0
- package/dist/pi/packages/ta-nearby/lib/server-texts.js +148 -0
- package/dist/pi/packages/ta-nearby/lib/style.d.ts +4 -0
- package/dist/pi/packages/ta-nearby/lib/style.js +92 -0
- package/dist/pi/packages/ta-nearby/package.json +65 -0
- package/dist/pi/packages/ta-nearby/web/index.js +1055 -0
- package/dist/pi/packages/ta-notify/README.en.md +3 -1
- package/dist/pi/packages/ta-notify/README.md +3 -1
- package/dist/pi/packages/ta-notify/extension/index.mjs +85 -0
- package/dist/pi/packages/ta-notify/host/index.mjs +24 -1
- package/dist/pi/packages/ta-notify/lib/extension.d.ts +4 -1
- package/dist/pi/packages/ta-notify/lib/extension.js +20 -1
- package/dist/pi/packages/ta-notify/lib/notify-protocol.d.ts +23 -3
- package/dist/pi/packages/ta-notify/lib/notify-service.d.ts +4 -2
- package/dist/pi/packages/ta-notify/lib/notify-service.js +27 -2
- package/dist/pi/packages/ta-notify/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-notify/lib/style.js +1 -1
- package/dist/pi/packages/ta-notify/lib/watcher.js +2 -1
- package/dist/pi/packages/ta-notify/package.json +7 -2
- package/dist/pi/packages/ta-notify/web/index.js +3 -2
- package/dist/pi/packages/ta-recall/README.en.md +15 -6
- package/dist/pi/packages/ta-recall/README.md +15 -6
- package/dist/pi/packages/ta-recall/extension/index.mjs +311 -134
- package/dist/pi/packages/ta-recall/host/index.mjs +472 -116
- package/dist/pi/packages/ta-recall/lib/block-util.d.ts +11 -0
- package/dist/pi/packages/ta-recall/lib/block-util.js +43 -0
- package/dist/pi/packages/ta-recall/lib/brief-util.d.ts +25 -0
- package/dist/pi/packages/ta-recall/lib/brief-util.js +38 -1
- package/dist/pi/packages/ta-recall/lib/composer.d.ts +3 -2
- package/dist/pi/packages/ta-recall/lib/composer.js +9 -8
- package/dist/pi/packages/ta-recall/lib/extension-util.d.ts +21 -0
- package/dist/pi/packages/ta-recall/lib/extension-util.js +104 -0
- package/dist/pi/packages/ta-recall/lib/extension.d.ts +3 -3
- package/dist/pi/packages/ta-recall/lib/extension.js +31 -23
- package/dist/pi/packages/ta-recall/lib/i18n.d.ts +18 -10
- package/dist/pi/packages/ta-recall/lib/i18n.js +40 -24
- package/dist/pi/packages/ta-recall/lib/index.d.ts +2 -2
- package/dist/pi/packages/ta-recall/lib/index.js +5 -4
- package/dist/pi/packages/ta-recall/lib/keep-card.d.ts +10 -0
- package/dist/pi/packages/ta-recall/lib/keep-card.js +83 -0
- package/dist/pi/packages/ta-recall/lib/keep-util.d.ts +15 -0
- package/dist/pi/packages/ta-recall/lib/keep-util.js +55 -0
- package/dist/pi/packages/ta-recall/lib/recall-protocol.d.ts +92 -18
- package/dist/pi/packages/ta-recall/lib/recall-protocol.js +19 -9
- package/dist/pi/packages/ta-recall/lib/recall-service.d.ts +16 -6
- package/dist/pi/packages/ta-recall/lib/recall-service.js +101 -11
- package/dist/pi/packages/ta-recall/lib/requests-util.d.ts +6 -0
- package/dist/pi/packages/ta-recall/lib/requests-util.js +37 -0
- package/dist/pi/packages/ta-recall/lib/server-texts.d.ts +2 -0
- package/dist/pi/packages/ta-recall/lib/server-texts.js +5 -0
- package/dist/pi/packages/ta-recall/lib/settings.d.ts +1 -1
- package/dist/pi/packages/ta-recall/lib/settings.js +1 -1
- package/dist/pi/packages/ta-recall/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-recall/lib/style.js +2 -2
- package/dist/pi/packages/ta-recall/lib/tool-line.d.ts +2 -2
- package/dist/pi/packages/ta-recall/lib/tool-line.js +10 -5
- package/dist/pi/packages/ta-recall/lib/tool.d.ts +14 -5
- package/dist/pi/packages/ta-recall/lib/tool.js +29 -17
- package/dist/pi/packages/ta-recall/package.json +13 -5
- package/dist/pi/packages/ta-recall/web/index.js +414 -153
- package/dist/pi/packages/ta-rooms/README.en.md +6 -3
- package/dist/pi/packages/ta-rooms/README.md +6 -3
- package/dist/pi/packages/ta-rooms/extension/index.mjs +194 -9
- package/dist/pi/packages/ta-rooms/host/index.mjs +1023 -234
- package/dist/pi/packages/ta-rooms/lib/dialogs.js +5 -0
- package/dist/pi/packages/ta-rooms/lib/double-agents.d.ts +26 -0
- package/dist/pi/packages/ta-rooms/lib/double-agents.js +84 -0
- package/dist/pi/packages/ta-rooms/lib/double-grants.d.ts +22 -0
- package/dist/pi/packages/ta-rooms/lib/double-grants.js +140 -0
- package/dist/pi/packages/ta-rooms/lib/double-host.d.ts +43 -40
- package/dist/pi/packages/ta-rooms/lib/double-host.js +142 -311
- package/dist/pi/packages/ta-rooms/lib/double-i18n.d.ts +20 -0
- package/dist/pi/packages/ta-rooms/lib/double-i18n.js +40 -0
- package/dist/pi/packages/ta-rooms/lib/double-protocol.d.ts +96 -3
- package/dist/pi/packages/ta-rooms/lib/double-protocol.js +58 -3
- package/dist/pi/packages/ta-rooms/lib/double-tasks.d.ts +82 -0
- package/dist/pi/packages/ta-rooms/lib/double-tasks.js +527 -0
- package/dist/pi/packages/ta-rooms/lib/double-user.d.ts +19 -0
- package/dist/pi/packages/ta-rooms/lib/double-user.js +124 -0
- package/dist/pi/packages/ta-rooms/lib/double-util.d.ts +34 -2
- package/dist/pi/packages/ta-rooms/lib/double-util.js +60 -2
- package/dist/pi/packages/ta-rooms/lib/doubles-store.d.ts +4 -1
- package/dist/pi/packages/ta-rooms/lib/doubles-store.js +8 -2
- package/dist/pi/packages/ta-rooms/lib/doubles-view.js +7 -2
- package/dist/pi/packages/ta-rooms/lib/extension.d.ts +9 -0
- package/dist/pi/packages/ta-rooms/lib/extension.js +35 -8
- package/dist/pi/packages/ta-rooms/lib/friend-notices.d.ts +12 -0
- package/dist/pi/packages/ta-rooms/lib/friend-notices.js +47 -0
- package/dist/pi/packages/ta-rooms/lib/friends-view.d.ts +20 -0
- package/dist/pi/packages/ta-rooms/lib/friends-view.js +224 -0
- package/dist/pi/packages/ta-rooms/lib/host.d.ts +2 -1
- package/dist/pi/packages/ta-rooms/lib/host.js +9 -2
- package/dist/pi/packages/ta-rooms/lib/i18n.d.ts +23 -2
- package/dist/pi/packages/ta-rooms/lib/i18n.js +60 -18
- package/dist/pi/packages/ta-rooms/lib/index.js +18 -1
- package/dist/pi/packages/ta-rooms/lib/members-view.js +12 -1
- package/dist/pi/packages/ta-rooms/lib/page.d.ts +42 -4
- package/dist/pi/packages/ta-rooms/lib/page.js +108 -24
- package/dist/pi/packages/ta-rooms/lib/room-view.js +7 -3
- package/dist/pi/packages/ta-rooms/lib/rooms-page.js +6 -17
- package/dist/pi/packages/ta-rooms/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-rooms/lib/style.js +31 -6
- package/dist/pi/packages/ta-rooms/lib/tasks-view.d.ts +41 -0
- package/dist/pi/packages/ta-rooms/lib/tasks-view.js +228 -0
- package/dist/pi/packages/ta-rooms/package.json +11 -2
- package/dist/pi/packages/ta-rooms/web/index.js +1633 -751
- package/dist/pi/packages/ta-share/README.en.md +1 -1
- package/dist/pi/packages/ta-share/README.md +1 -1
- package/dist/pi/packages/ta-share/extension/index.mjs +72 -0
- package/dist/pi/packages/ta-share/lib/i18n.js +6 -6
- package/dist/pi/packages/ta-share/lib/share-dialog.js +1 -1
- package/dist/pi/packages/ta-share/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-share/lib/style.js +4 -6
- package/dist/pi/packages/ta-share/package.json +10 -2
- package/dist/pi/packages/ta-share/web/index.js +12 -14
- package/dist/pi/packages/ta-sync/README.en.md +55 -0
- package/dist/pi/packages/ta-sync/README.md +55 -0
- package/dist/pi/packages/ta-sync/host/index.mjs +3763 -0
- package/dist/pi/packages/ta-sync/lib/host.d.ts +10 -0
- package/dist/pi/packages/ta-sync/lib/host.js +94 -0
- package/dist/pi/packages/ta-sync/lib/i18n.d.ts +98 -0
- package/dist/pi/packages/ta-sync/lib/i18n.js +202 -0
- package/dist/pi/packages/ta-sync/lib/index.d.ts +7 -0
- package/dist/pi/packages/ta-sync/lib/index.js +115 -0
- package/dist/pi/packages/ta-sync/lib/migrate-archive.d.ts +88 -0
- package/dist/pi/packages/ta-sync/lib/migrate-archive.js +439 -0
- package/dist/pi/packages/ta-sync/lib/migrate-frames.d.ts +77 -0
- package/dist/pi/packages/ta-sync/lib/migrate-frames.js +210 -0
- package/dist/pi/packages/ta-sync/lib/migrate-i18n.d.ts +99 -0
- package/dist/pi/packages/ta-sync/lib/migrate-i18n.js +210 -0
- package/dist/pi/packages/ta-sync/lib/migrate-net.d.ts +51 -0
- package/dist/pi/packages/ta-sync/lib/migrate-net.js +170 -0
- package/dist/pi/packages/ta-sync/lib/migrate-protocol.d.ts +126 -0
- package/dist/pi/packages/ta-sync/lib/migrate-protocol.js +43 -0
- package/dist/pi/packages/ta-sync/lib/migrate-service.d.ts +86 -0
- package/dist/pi/packages/ta-sync/lib/migrate-service.js +521 -0
- package/dist/pi/packages/ta-sync/lib/move-panel.d.ts +16 -0
- package/dist/pi/packages/ta-sync/lib/move-panel.js +320 -0
- package/dist/pi/packages/ta-sync/lib/move-store.d.ts +15 -0
- package/dist/pi/packages/ta-sync/lib/move-store.js +42 -0
- package/dist/pi/packages/ta-sync/lib/panel.d.ts +28 -0
- package/dist/pi/packages/ta-sync/lib/panel.js +305 -0
- package/dist/pi/packages/ta-sync/lib/pictures.d.ts +22 -0
- package/dist/pi/packages/ta-sync/lib/pictures.js +92 -0
- package/dist/pi/packages/ta-sync/lib/react.d.ts +11 -0
- package/dist/pi/packages/ta-sync/lib/react.js +16 -0
- package/dist/pi/packages/ta-sync/lib/server-texts.d.ts +29 -0
- package/dist/pi/packages/ta-sync/lib/server-texts.js +111 -0
- package/dist/pi/packages/ta-sync/lib/style.d.ts +4 -0
- package/dist/pi/packages/ta-sync/lib/style.js +108 -0
- package/dist/pi/packages/ta-sync/lib/sync-codec.d.ts +16 -0
- package/dist/pi/packages/ta-sync/lib/sync-codec.js +61 -0
- package/dist/pi/packages/ta-sync/lib/sync-engine.d.ts +57 -0
- package/dist/pi/packages/ta-sync/lib/sync-engine.js +203 -0
- package/dist/pi/packages/ta-sync/lib/sync-folder.d.ts +156 -0
- package/dist/pi/packages/ta-sync/lib/sync-folder.js +385 -0
- package/dist/pi/packages/ta-sync/lib/sync-identity.d.ts +60 -0
- package/dist/pi/packages/ta-sync/lib/sync-identity.js +201 -0
- package/dist/pi/packages/ta-sync/lib/sync-items.d.ts +104 -0
- package/dist/pi/packages/ta-sync/lib/sync-items.js +426 -0
- package/dist/pi/packages/ta-sync/lib/sync-merge.d.ts +42 -0
- package/dist/pi/packages/ta-sync/lib/sync-merge.js +109 -0
- package/dist/pi/packages/ta-sync/lib/sync-protocol.d.ts +134 -0
- package/dist/pi/packages/ta-sync/lib/sync-protocol.js +72 -0
- package/dist/pi/packages/ta-sync/lib/sync-service.d.ts +128 -0
- package/dist/pi/packages/ta-sync/lib/sync-service.js +679 -0
- package/dist/pi/packages/ta-sync/lib/sync-store.d.ts +15 -0
- package/dist/pi/packages/ta-sync/lib/sync-store.js +41 -0
- package/dist/pi/packages/ta-sync/package.json +57 -0
- package/dist/pi/packages/ta-sync/web/index.js +2018 -0
- package/dist/pi/packages/ta-sync-cloud/README.en.md +31 -0
- package/dist/pi/packages/ta-sync-cloud/README.md +31 -0
- package/dist/pi/packages/ta-sync-cloud/host/index.mjs +810 -0
- package/dist/pi/packages/ta-sync-cloud/lib/git-store.d.ts +90 -0
- package/dist/pi/packages/ta-sync-cloud/lib/git-store.js +393 -0
- package/dist/pi/packages/ta-sync-cloud/lib/host.d.ts +13 -0
- package/dist/pi/packages/ta-sync-cloud/lib/host.js +112 -0
- package/dist/pi/packages/ta-sync-cloud/lib/s3-store.d.ts +72 -0
- package/dist/pi/packages/ta-sync-cloud/lib/s3-store.js +206 -0
- package/dist/pi/packages/ta-sync-cloud/lib/store-util.d.ts +17 -0
- package/dist/pi/packages/ta-sync-cloud/lib/store-util.js +69 -0
- package/dist/pi/packages/ta-sync-cloud/lib/webdav-store.d.ts +40 -0
- package/dist/pi/packages/ta-sync-cloud/lib/webdav-store.js +144 -0
- package/dist/pi/packages/ta-sync-cloud/package.json +63 -0
- package/dist/pi/packages/ta-team/README.en.md +28 -0
- package/dist/pi/packages/ta-team/README.md +28 -0
- package/dist/pi/packages/ta-team/extension/index.mjs +285 -0
- package/dist/pi/packages/ta-team/host/index.mjs +2121 -0
- package/dist/pi/packages/ta-team/lib/dialogs.d.ts +13 -0
- package/dist/pi/packages/ta-team/lib/dialogs.js +335 -0
- package/dist/pi/packages/ta-team/lib/extension.d.ts +21 -0
- package/dist/pi/packages/ta-team/lib/extension.js +123 -0
- package/dist/pi/packages/ta-team/lib/host.d.ts +13 -0
- package/dist/pi/packages/ta-team/lib/host.js +45 -0
- package/dist/pi/packages/ta-team/lib/i18n.d.ts +200 -0
- package/dist/pi/packages/ta-team/lib/i18n.js +401 -0
- package/dist/pi/packages/ta-team/lib/index.d.ts +8 -0
- package/dist/pi/packages/ta-team/lib/index.js +67 -0
- package/dist/pi/packages/ta-team/lib/join.d.ts +28 -0
- package/dist/pi/packages/ta-team/lib/join.js +48 -0
- package/dist/pi/packages/ta-team/lib/member-dialog.d.ts +2 -0
- package/dist/pi/packages/ta-team/lib/member-dialog.js +121 -0
- package/dist/pi/packages/ta-team/lib/members-tab.d.ts +11 -0
- package/dist/pi/packages/ta-team/lib/members-tab.js +133 -0
- package/dist/pi/packages/ta-team/lib/page.d.ts +42 -0
- package/dist/pi/packages/ta-team/lib/page.js +142 -0
- package/dist/pi/packages/ta-team/lib/parts.d.ts +39 -0
- package/dist/pi/packages/ta-team/lib/parts.js +47 -0
- package/dist/pi/packages/ta-team/lib/policy.d.ts +37 -0
- package/dist/pi/packages/ta-team/lib/policy.js +215 -0
- package/dist/pi/packages/ta-team/lib/protocol.d.ts +285 -0
- package/dist/pi/packages/ta-team/lib/protocol.js +119 -0
- package/dist/pi/packages/ta-team/lib/react.d.ts +8 -0
- package/dist/pi/packages/ta-team/lib/react.js +8 -0
- package/dist/pi/packages/ta-team/lib/record-tab.d.ts +8 -0
- package/dist/pi/packages/ta-team/lib/record-tab.js +90 -0
- package/dist/pi/packages/ta-team/lib/remote.d.ts +31 -0
- package/dist/pi/packages/ta-team/lib/remote.js +128 -0
- package/dist/pi/packages/ta-team/lib/roles-tab.d.ts +9 -0
- package/dist/pi/packages/ta-team/lib/roles-tab.js +76 -0
- package/dist/pi/packages/ta-team/lib/seal.d.ts +28 -0
- package/dist/pi/packages/ta-team/lib/seal.js +75 -0
- package/dist/pi/packages/ta-team/lib/shared-home.d.ts +23 -0
- package/dist/pi/packages/ta-team/lib/shared-home.js +123 -0
- package/dist/pi/packages/ta-team/lib/style.d.ts +4 -0
- package/dist/pi/packages/ta-team/lib/style.js +99 -0
- package/dist/pi/packages/ta-team/lib/team-admin.d.ts +31 -0
- package/dist/pi/packages/ta-team/lib/team-admin.js +140 -0
- package/dist/pi/packages/ta-team/lib/team-audit.d.ts +35 -0
- package/dist/pi/packages/ta-team/lib/team-audit.js +152 -0
- package/dist/pi/packages/ta-team/lib/team-core.d.ts +58 -0
- package/dist/pi/packages/ta-team/lib/team-core.js +115 -0
- package/dist/pi/packages/ta-team/lib/team-error.d.ts +4 -0
- package/dist/pi/packages/ta-team/lib/team-error.js +8 -0
- package/dist/pi/packages/ta-team/lib/team-expiry.d.ts +15 -0
- package/dist/pi/packages/ta-team/lib/team-expiry.js +67 -0
- package/dist/pi/packages/ta-team/lib/team-files.d.ts +21 -0
- package/dist/pi/packages/ta-team/lib/team-files.js +119 -0
- package/dist/pi/packages/ta-team/lib/team-inbox.d.ts +8 -0
- package/dist/pi/packages/ta-team/lib/team-inbox.js +32 -0
- package/dist/pi/packages/ta-team/lib/team-invites.d.ts +46 -0
- package/dist/pi/packages/ta-team/lib/team-invites.js +204 -0
- package/dist/pi/packages/ta-team/lib/team-page.d.ts +6 -0
- package/dist/pi/packages/ta-team/lib/team-page.js +41 -0
- package/dist/pi/packages/ta-team/lib/team-paths.d.ts +15 -0
- package/dist/pi/packages/ta-team/lib/team-paths.js +70 -0
- package/dist/pi/packages/ta-team/lib/team-people.d.ts +28 -0
- package/dist/pi/packages/ta-team/lib/team-people.js +106 -0
- package/dist/pi/packages/ta-team/lib/team-requests.d.ts +24 -0
- package/dist/pi/packages/ta-team/lib/team-requests.js +126 -0
- package/dist/pi/packages/ta-team/lib/team-runs.d.ts +38 -0
- package/dist/pi/packages/ta-team/lib/team-runs.js +136 -0
- package/dist/pi/packages/ta-team/lib/team-service.d.ts +32 -0
- package/dist/pi/packages/ta-team/lib/team-service.js +58 -0
- package/dist/pi/packages/ta-team/lib/team-shares.d.ts +52 -0
- package/dist/pi/packages/ta-team/lib/team-shares.js +145 -0
- package/dist/pi/packages/ta-team/lib/team-waits.d.ts +8 -0
- package/dist/pi/packages/ta-team/lib/team-waits.js +42 -0
- package/dist/pi/packages/ta-team/lib/team-watch.d.ts +28 -0
- package/dist/pi/packages/ta-team/lib/team-watch.js +136 -0
- package/dist/pi/packages/ta-team/package.json +68 -0
- package/dist/pi/packages/ta-team/web/index.js +2741 -0
- package/dist/pi/packages/ta-workspace/package.json +1 -1
- package/dist/pi/packages/ta-workspace/web/index.js +178 -0
- package/dist/public/assets/{abnfDiagram-S7VZAUFO-BnqtcbpZ.js → abnfDiagram-S7VZAUFO-C30pzFN3.js} +1 -1
- package/dist/public/assets/{architecture-WOLXFQ4H-BzPq1Ioe.js → architecture-WOLXFQ4H-wfYFQl2u.js} +1 -1
- package/dist/public/assets/{architectureDiagram-YGSQEFNW-BsquO3a7.js → architectureDiagram-YGSQEFNW-B05zgxyT.js} +1 -1
- package/dist/public/assets/{blockDiagram-APKKGZFW-BQNYOEP4.js → blockDiagram-APKKGZFW-BhN8Bsu0.js} +1 -1
- package/dist/public/assets/{browser-DS711n7Y.js → browser-DteLjv_-.js} +1 -1
- package/dist/public/assets/{c4Diagram-KJ2TOQU5-D6e79Ycq.js → c4Diagram-KJ2TOQU5-DvdF3qCV.js} +1 -1
- package/dist/public/assets/{chunk-6MUBBAK2-B-sgaPoU.js → chunk-6MUBBAK2-DNavkrnr.js} +1 -1
- package/dist/public/assets/{chunk-AXFCVDMN-D8pJKIg2.js → chunk-AXFCVDMN-DplVEqp_.js} +1 -1
- package/dist/public/assets/{chunk-BG4553SI-86TL-vCz.js → chunk-BG4553SI-BazOaHvw.js} +1 -1
- package/dist/public/assets/{chunk-DQASFODM-CfUVuGjc.js → chunk-DQASFODM-DmoQWa8b.js} +1 -1
- package/dist/public/assets/{chunk-HFQBQIA3-Drm0FEYk.js → chunk-HFQBQIA3-Cj1m9ccz.js} +1 -1
- package/dist/public/assets/{chunk-KQLB2F66-BVFRADlC.js → chunk-KQLB2F66-_jbilrLX.js} +1 -1
- package/dist/public/assets/{chunk-KS47KZWR-DfyALEmP.js → chunk-KS47KZWR-CWU16Lut.js} +1 -1
- package/dist/public/assets/{chunk-LEVMRMFU-DtJVzk2c.js → chunk-LEVMRMFU-wsbxWEZE.js} +1 -1
- package/dist/public/assets/{chunk-M57EVK23-DTy27PHQ.js → chunk-M57EVK23-BGvmwwTv.js} +1 -1
- package/dist/public/assets/{chunk-OY52VR5R-nSgn7jZg.js → chunk-OY52VR5R-Bp3hJ4kT.js} +1 -1
- package/dist/public/assets/{chunk-QB3YPZ4I-CkSxO_X6.js → chunk-QB3YPZ4I-C1rQVxsJ.js} +1 -1
- package/dist/public/assets/{chunk-R3KSYZAL-Ck6iAjAL.js → chunk-R3KSYZAL-CEy0dKWn.js} +1 -1
- package/dist/public/assets/{chunk-STOXU5M4-DQPHdSTh.js → chunk-STOXU5M4-akqvafwH.js} +1 -1
- package/dist/public/assets/{chunk-TU62E6Q5-Bc2AbV2Y.js → chunk-TU62E6Q5-CVXAS4DS.js} +1 -1
- package/dist/public/assets/{chunk-X4PVCEUW-Cgpm0IXm.js → chunk-X4PVCEUW-DT_bEMvQ.js} +1 -1
- package/dist/public/assets/{chunk-XXDRQBXY-CGE4eLpg.js → chunk-XXDRQBXY-Bgv_n5Qh.js} +1 -1
- package/dist/public/assets/{classDiagram-v2-L25IHNXF-4eQuDPEo.js → classDiagram-v2-L25IHNXF-BtjQChpS.js} +1 -1
- package/dist/public/assets/{cose-bilkent-JH36ORCC-tDLCWq07.js → cose-bilkent-JH36ORCC-DSrOY8Uk.js} +1 -1
- package/dist/public/assets/cynefin-EF2NZ3EQ-DYlwwGUb.js +1 -0
- package/dist/public/assets/{cynefinDiagram-L36ESFBY-ByqcZ8x9.js → cynefinDiagram-L36ESFBY-DTy7FNFS.js} +1 -1
- package/dist/public/assets/{d3-selection-Bgn1374-.js → d3-selection-ip6rDUzY.js} +1 -1
- package/dist/public/assets/{dagre-SPEB5RXH-mCgr71mV.js → dagre-SPEB5RXH-CHrks7Jb.js} +1 -1
- package/dist/public/assets/{diagram-3B7GL73J-DpYSwpQ0.js → diagram-3B7GL73J-eUJ7STAr.js} +1 -1
- package/dist/public/assets/{diagram-DNEMPZML-DMozUQzi.js → diagram-DNEMPZML-B_fptNkg.js} +1 -1
- package/dist/public/assets/{diagram-L3MUGCDN-Bk5Uf8PD.js → diagram-L3MUGCDN-C15owCSG.js} +1 -1
- package/dist/public/assets/{diagram-LA4NIPWU-DFt5cPVK.js → diagram-LA4NIPWU-Bfa1qEgY.js} +1 -1
- package/dist/public/assets/{diagram-MHCOC4E2-BkZZcKqK.js → diagram-MHCOC4E2-Bwxsih_Q.js} +1 -1
- package/dist/public/assets/{diagram-XPFHEONP-HEY6kyyN.js → diagram-XPFHEONP-DKZ_lwah.js} +1 -1
- package/dist/public/assets/{dist-Bj2CrKn7.js → dist-VHGBaby6.js} +1 -1
- package/dist/public/assets/{ebnfDiagram-UZSYDL2S-D4JvYyVb.js → ebnfDiagram-UZSYDL2S-CCkttvYi.js} +1 -1
- package/dist/public/assets/{editor.api-BpJFzYbl.js → editor.api-a2gC0dVD.js} +1 -1
- package/dist/public/assets/{elk-XV6XMXQE-CT6qLJkF.js → elk-XV6XMXQE-DcikW39e.js} +1 -1
- package/dist/public/assets/{erDiagram-EYBAPDEH-BGng1goR.js → erDiagram-EYBAPDEH-BE3TV5lU.js} +1 -1
- package/dist/public/assets/{eventmodeling-K75KTNOO-Tt8O-Q1Z.js → eventmodeling-K75KTNOO-C2s1kutf.js} +1 -1
- package/dist/public/assets/flowDiagram-MALGAFMO-Bufgn1OL.js +1 -0
- package/dist/public/assets/{freemarker2-Dd5ksoRn.js → freemarker2-2e8pZojb.js} +1 -1
- package/dist/public/assets/{ganttDiagram-23REXITY-Jl8VO_g2.js → ganttDiagram-23REXITY-rGnJptii.js} +1 -1
- package/dist/public/assets/gitGraph-VSP46ZUC-D_tzR9z9.js +1 -0
- package/dist/public/assets/{gitGraphDiagram-STJD7YCW-Be4qXALb.js → gitGraphDiagram-STJD7YCW-CNnsDjQA.js} +1 -1
- package/dist/public/assets/{handlebars-4xabgUi4.js → handlebars-CMKN6E37.js} +1 -1
- package/dist/public/assets/{html-Dk3kt2w_.js → html-0AjXQhkQ.js} +1 -1
- package/dist/public/assets/{index-Bx0c7eoh.js → index-TUtoc8CW.js} +2 -2
- package/dist/public/assets/info-OHQRW6UA-C5Pwv-6e.js +1 -0
- package/dist/public/assets/{infoDiagram-KBXO4MBG-Coc0tdwb.js → infoDiagram-KBXO4MBG-DMKs8cuE.js} +1 -1
- package/dist/public/assets/{ishikawaDiagram-M35NCXHK-CE_1VQCg.js → ishikawaDiagram-M35NCXHK-Dg_IbA0G.js} +1 -1
- package/dist/public/assets/{javascript-DKLGqy6v.js → javascript-CqYOqPjw.js} +1 -1
- package/dist/public/assets/{journeyDiagram-OPLOXDXO-CrolRZ_7.js → journeyDiagram-OPLOXDXO-ohAOfoEY.js} +1 -1
- package/dist/public/assets/{jsonMode-Chcs19_y.js → jsonMode-D-gI-S5H.js} +1 -1
- package/dist/public/assets/{kanban-definition-BZKZHSQY-CjJj47De.js → kanban-definition-BZKZHSQY-w-aKZS_U.js} +1 -1
- package/dist/public/assets/{liquid-jff3p-Ji.js → liquid-oBN7c8NZ.js} +1 -1
- package/dist/public/assets/{mdx-DW4uRDfJ.js → mdx-CnwhgGjX.js} +1 -1
- package/dist/public/assets/{mermaid-parser.core-XhQj6FvH.js → mermaid-parser.core-CfFkSR0t.js} +3 -3
- package/dist/public/assets/{mermaid.core-OD4Atimg.js → mermaid.core-apiVCSBY.js} +4 -4
- package/dist/public/assets/{mindmap-definition-I2QAS2VK-DpGelVY7.js → mindmap-definition-I2QAS2VK-E7vqVWB0.js} +1 -1
- package/dist/public/assets/module-C2jtFD0v.js +2365 -0
- package/dist/public/assets/module-xT4L5yYw.js +1 -0
- package/dist/public/assets/{monaco-BTVZP-tU.js → monaco-C0ByxW3l.js} +3 -3
- package/dist/public/assets/packet-JDAUHWVQ-xozI7a9T.js +1 -0
- package/dist/public/assets/{pegDiagram-FD4XPPHP-CqcBmPfB.js → pegDiagram-FD4XPPHP--RpwCE7x.js} +1 -1
- package/dist/public/assets/pie-XZMESJXO-BkHXIzDi.js +1 -0
- package/dist/public/assets/{pieDiagram-J4AKIIQI-CRir3beB.js → pieDiagram-J4AKIIQI-DxSbPaRZ.js} +1 -1
- package/dist/public/assets/{python-BwW_0207.js → python-vNPe_Fao.js} +1 -1
- package/dist/public/assets/{quadrantDiagram-XTYBLID2-DIY6gdcf.js → quadrantDiagram-XTYBLID2-CR_eeYXi.js} +1 -1
- package/dist/public/assets/radar-ABXABTNO-CaUh9zV3.js +1 -0
- package/dist/public/assets/railroad-I3PHUGI6-DckPvlDe.js +1 -0
- package/dist/public/assets/{railroad-abnf-LNEFI6M7-XWNOzunl.js → railroad-abnf-LNEFI6M7-D6I2HxIC.js} +1 -1
- package/dist/public/assets/{railroad-ebnf-DWYD2UWJ-CI0-j1Jz.js → railroad-ebnf-DWYD2UWJ-BuhDRGo3.js} +1 -1
- package/dist/public/assets/{railroad-peg-JR7BVNR7-BVB_IaIG.js → railroad-peg-JR7BVNR7-n15PB7Sb.js} +1 -1
- package/dist/public/assets/{railroadDiagram-MRQ6NCDL-CudwT6q2.js → railroadDiagram-MRQ6NCDL-Bbe5bNap.js} +1 -1
- package/dist/public/assets/{razor-DrjCyQqB.js → razor-CnKbF0Yz.js} +1 -1
- package/dist/public/assets/{requirementDiagram-Y7LWUY3S-CvRMeRqj.js → requirementDiagram-Y7LWUY3S-Beuygh9j.js} +1 -1
- package/dist/public/assets/{sankeyDiagram-WGJFYUXA-adxYCJKr.js → sankeyDiagram-WGJFYUXA-jwWPgnrS.js} +1 -1
- package/dist/public/assets/{sequenceDiagram-5NDRBG5M-lCRsoLUp.js → sequenceDiagram-5NDRBG5M-CmU-HYQI.js} +1 -1
- package/dist/public/assets/{stateDiagram-v2-FJ4FJNEA-DNst1BLg.js → stateDiagram-v2-FJ4FJNEA-Bo7BEi-m.js} +1 -1
- package/dist/public/assets/{swimlanes-UMQ2AFYF-S_omrdvA.js → swimlanes-UMQ2AFYF-PSKyZN_b.js} +1 -1
- package/dist/public/assets/swimlanesDiagram-OMPLPG4O-z_0ABNQq.js +8 -0
- package/dist/public/assets/{timeline-definition-SEUHKTQE-_pMGrTE-.js → timeline-definition-SEUHKTQE-BuRxLScZ.js} +1 -1
- package/dist/public/assets/treeView-D4JQ5SDB-BN38Jg9j.js +1 -0
- package/dist/public/assets/treemap-SAJKECNS-CKdz7u7W.js +1 -0
- package/dist/public/assets/{typescript-0-z_OYCU.js → typescript-C00ztMLg.js} +1 -1
- package/dist/public/assets/{usecaseDiagram-AFA6YXN4-B2oJKi5Q.js → usecaseDiagram-AFA6YXN4-Dlvyos8r.js} +1 -1
- package/dist/public/assets/{vennDiagram-NNMJR3RB-inPs2Kkr.js → vennDiagram-NNMJR3RB-CuLZubJH.js} +1 -1
- package/dist/public/assets/wardley-7MLQ67FV-B0cvM1zV.js +1 -0
- package/dist/public/assets/{wardleyDiagram-S7YFUSEH-DNhNHM7b.js → wardleyDiagram-S7YFUSEH-WOGGTs4y.js} +1 -1
- package/dist/public/assets/{xml-BXPxQzKF.js → xml-CHBFuf5n.js} +1 -1
- package/dist/public/assets/{xychartDiagram-GGNKY7IX-DJc93dNT.js → xychartDiagram-GGNKY7IX-DwW9eifP.js} +1 -1
- package/dist/public/assets/{yaml-CGmD9Rya.js → yaml-C4qkqzcL.js} +1 -1
- package/dist/public/index.html +1 -1
- package/dist/serve/index.mjs +7738 -3839
- package/package.json +37 -31
- package/dist/pi/packages/ta-cron/lib/composer.d.ts +0 -11
- package/dist/pi/packages/ta-cron/lib/composer.js +0 -58
- package/dist/pi/packages/ta-extensions/lib/composer.d.ts +0 -17
- package/dist/pi/packages/ta-extensions/lib/composer.js +0 -84
- package/dist/pi/packages/ta-recall/lib/skill-card.d.ts +0 -9
- package/dist/pi/packages/ta-recall/lib/skill-card.js +0 -51
- package/dist/pi/packages/ta-recall/lib/skill-util.d.ts +0 -19
- package/dist/pi/packages/ta-recall/lib/skill-util.js +0 -110
- package/dist/public/assets/cynefin-EF2NZ3EQ-Dd40W2Jh.js +0 -1
- package/dist/public/assets/flowDiagram-MALGAFMO-uFzenfA3.js +0 -1
- package/dist/public/assets/gitGraph-VSP46ZUC-D28I_jUc.js +0 -1
- package/dist/public/assets/info-OHQRW6UA-5vIGEIq7.js +0 -1
- package/dist/public/assets/module-CrD0OHQp.js +0 -1
- package/dist/public/assets/module-DEBMgdtf.js +0 -2273
- package/dist/public/assets/packet-JDAUHWVQ-CBxQ5Mzy.js +0 -1
- package/dist/public/assets/pie-XZMESJXO-DT5V3581.js +0 -1
- package/dist/public/assets/radar-ABXABTNO-BAOovYEa.js +0 -1
- package/dist/public/assets/railroad-I3PHUGI6-RZ2Rnwzh.js +0 -1
- package/dist/public/assets/swimlanesDiagram-OMPLPG4O-By5lnz4u.js +0 -8
- package/dist/public/assets/treeView-D4JQ5SDB-Cyb9N8KY.js +0 -1
- package/dist/public/assets/treemap-SAJKECNS-BkMo57pG.js +0 -1
- package/dist/public/assets/wardley-7MLQ67FV-CSt-uQ-Y.js +0 -1
|
@@ -0,0 +1,3763 @@
|
|
|
1
|
+
// web-src/host.ts
|
|
2
|
+
import { readFile as readFile5 } from "node:fs/promises";
|
|
3
|
+
import { join as join7 } from "node:path";
|
|
4
|
+
|
|
5
|
+
// web-src/migrate-protocol.ts
|
|
6
|
+
var MIGRATE_CATEGORIES = [
|
|
7
|
+
"conversations",
|
|
8
|
+
"settings",
|
|
9
|
+
"instructions",
|
|
10
|
+
"skills",
|
|
11
|
+
"extensions",
|
|
12
|
+
"data",
|
|
13
|
+
"models",
|
|
14
|
+
"changes",
|
|
15
|
+
"credentials",
|
|
16
|
+
"identity"
|
|
17
|
+
];
|
|
18
|
+
var SECRET_CATEGORIES = ["credentials", "identity"];
|
|
19
|
+
var MIGRATE_STATUS_KEY = "migrate";
|
|
20
|
+
var BACKUP_EXTENSION = ".tabackup";
|
|
21
|
+
|
|
22
|
+
// web-src/migrate-service.ts
|
|
23
|
+
import { randomBytes as randomBytes3, randomUUID } from "node:crypto";
|
|
24
|
+
import { createSocket } from "node:dgram";
|
|
25
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
26
|
+
import { mkdir as mkdir3, readdir as readdir3, rename as rename3, rm as rm2, stat as stat3 } from "node:fs/promises";
|
|
27
|
+
import { createConnection, createServer } from "node:net";
|
|
28
|
+
import { homedir as homedir2, hostname as hostname2 } from "node:os";
|
|
29
|
+
import { join as join3 } from "node:path";
|
|
30
|
+
|
|
31
|
+
// web-src/migrate-archive.ts
|
|
32
|
+
import { mkdir as mkdir2, readdir as readdir2, readFile as readFile2, rename as rename2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
|
|
33
|
+
import { homedir, hostname, platform } from "node:os";
|
|
34
|
+
import { basename, dirname as dirname2, join as join2, relative as relative2, sep as sep2 } from "node:path";
|
|
35
|
+
|
|
36
|
+
// web-src/migrate-frames.ts
|
|
37
|
+
import { createCipheriv, createDecipheriv, randomBytes, scryptSync } from "node:crypto";
|
|
38
|
+
import { deflateRawSync, inflateRawSync } from "node:zlib";
|
|
39
|
+
var encoder = new TextEncoder;
|
|
40
|
+
var decoder = new TextDecoder;
|
|
41
|
+
var FRAME_MAX = 4 * 1024 * 1024;
|
|
42
|
+
var PIECE = 512 * 1024;
|
|
43
|
+
var TAG = 16;
|
|
44
|
+
|
|
45
|
+
class FrameCipher {
|
|
46
|
+
key;
|
|
47
|
+
count = 0;
|
|
48
|
+
constructor(key) {
|
|
49
|
+
this.key = key;
|
|
50
|
+
}
|
|
51
|
+
seal(plain) {
|
|
52
|
+
const cipher = createCipheriv("aes-256-gcm", this.key, this.iv());
|
|
53
|
+
const sealed = Buffer.concat([cipher.update(plain), cipher.final(), cipher.getAuthTag()]);
|
|
54
|
+
return new Uint8Array(sealed);
|
|
55
|
+
}
|
|
56
|
+
open(sealed) {
|
|
57
|
+
if (sealed.length < TAG) {
|
|
58
|
+
throw new Error("A frame came cut short.");
|
|
59
|
+
}
|
|
60
|
+
const decipher = createDecipheriv("aes-256-gcm", this.key, this.iv());
|
|
61
|
+
decipher.setAuthTag(sealed.subarray(sealed.length - TAG));
|
|
62
|
+
try {
|
|
63
|
+
return new Uint8Array(Buffer.concat([decipher.update(sealed.subarray(0, sealed.length - TAG)), decipher.final()]));
|
|
64
|
+
} catch {
|
|
65
|
+
throw new Error("What came does not open: a wrong password, or it was changed on the way.");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
iv() {
|
|
69
|
+
const iv = Buffer.alloc(12);
|
|
70
|
+
iv.writeUIntBE(this.count++, 6, 6);
|
|
71
|
+
return iv;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
var Frames;
|
|
75
|
+
((Frames) => {
|
|
76
|
+
function encode(frame, cipher) {
|
|
77
|
+
const plain = new Uint8Array(1 + frame.body.length);
|
|
78
|
+
plain[0] = frame.type;
|
|
79
|
+
plain.set(frame.body, 1);
|
|
80
|
+
const payload = cipher ? cipher.seal(plain) : plain;
|
|
81
|
+
const out = new Uint8Array(4 + payload.length);
|
|
82
|
+
new DataView(out.buffer).setUint32(0, payload.length);
|
|
83
|
+
out.set(payload, 4);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
Frames.encode = encode;
|
|
87
|
+
function record(value) {
|
|
88
|
+
return { type: 1 /* Record */, body: encoder.encode(JSON.stringify(value)) };
|
|
89
|
+
}
|
|
90
|
+
Frames.record = record;
|
|
91
|
+
function bytes(piece2) {
|
|
92
|
+
const deflated = deflateRawSync(piece2, { level: 6 });
|
|
93
|
+
return deflated.length < piece2.length * 0.9 ? { type: 3 /* Deflated */, body: new Uint8Array(deflated) } : { type: 2 /* Bytes */, body: piece2 };
|
|
94
|
+
}
|
|
95
|
+
Frames.bytes = bytes;
|
|
96
|
+
function parseRecord(frame) {
|
|
97
|
+
if (frame.type !== 1 /* Record */) {
|
|
98
|
+
throw new Error("A record was expected.");
|
|
99
|
+
}
|
|
100
|
+
return JSON.parse(decoder.decode(frame.body));
|
|
101
|
+
}
|
|
102
|
+
Frames.parseRecord = parseRecord;
|
|
103
|
+
function piece(frame) {
|
|
104
|
+
if (frame.type === 2 /* Bytes */) {
|
|
105
|
+
return frame.body;
|
|
106
|
+
}
|
|
107
|
+
if (frame.type === 3 /* Deflated */) {
|
|
108
|
+
return new Uint8Array(inflateRawSync(frame.body, { maxOutputLength: FRAME_MAX }));
|
|
109
|
+
}
|
|
110
|
+
throw new Error("Bytes of a file were expected.");
|
|
111
|
+
}
|
|
112
|
+
Frames.piece = piece;
|
|
113
|
+
})(Frames ||= {});
|
|
114
|
+
|
|
115
|
+
class FrameReader {
|
|
116
|
+
cipher;
|
|
117
|
+
buffer = new Uint8Array(0);
|
|
118
|
+
chunks = [];
|
|
119
|
+
length = 0;
|
|
120
|
+
iterator;
|
|
121
|
+
ended = false;
|
|
122
|
+
constructor(source, cipher) {
|
|
123
|
+
this.cipher = cipher;
|
|
124
|
+
this.iterator = source[Symbol.asyncIterator]();
|
|
125
|
+
}
|
|
126
|
+
setCipher(cipher) {
|
|
127
|
+
this.cipher = cipher;
|
|
128
|
+
}
|
|
129
|
+
async next() {
|
|
130
|
+
const head = await this.take(4);
|
|
131
|
+
if (!head) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const length = new DataView(head.buffer, head.byteOffset, 4).getUint32(0);
|
|
135
|
+
if (length > FRAME_MAX + TAG + 1) {
|
|
136
|
+
throw new Error("A frame larger than it may be came.");
|
|
137
|
+
}
|
|
138
|
+
const payload = await this.take(length);
|
|
139
|
+
if (!payload) {
|
|
140
|
+
throw new Error("The stream ended inside a frame.");
|
|
141
|
+
}
|
|
142
|
+
const plain = this.cipher ? this.cipher.open(payload) : payload;
|
|
143
|
+
return { type: plain[0], body: plain.subarray(1) };
|
|
144
|
+
}
|
|
145
|
+
async take(count) {
|
|
146
|
+
while (this.buffer.length < count) {
|
|
147
|
+
if (this.ended) {
|
|
148
|
+
this.flatten();
|
|
149
|
+
if (this.buffer.length >= count) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const { value, done } = await this.iterator.next();
|
|
155
|
+
if (done) {
|
|
156
|
+
this.ended = true;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const chunk = value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
160
|
+
this.chunks.push(chunk);
|
|
161
|
+
this.length += chunk.length;
|
|
162
|
+
if (this.buffer.length + this.length >= count) {
|
|
163
|
+
this.flatten();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const out = this.buffer.slice(0, count);
|
|
167
|
+
this.buffer = this.buffer.subarray(count);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
flatten() {
|
|
171
|
+
const joined = new Uint8Array(this.buffer.length + this.length);
|
|
172
|
+
joined.set(this.buffer);
|
|
173
|
+
let at = this.buffer.length;
|
|
174
|
+
for (const chunk of this.chunks) {
|
|
175
|
+
joined.set(chunk, at);
|
|
176
|
+
at += chunk.length;
|
|
177
|
+
}
|
|
178
|
+
this.buffer = joined;
|
|
179
|
+
this.chunks = [];
|
|
180
|
+
this.length = 0;
|
|
181
|
+
}
|
|
182
|
+
async close() {
|
|
183
|
+
await this.iterator.return?.();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
var MAGIC = encoder.encode("TABACKUP");
|
|
187
|
+
var BackupFile;
|
|
188
|
+
((BackupFile) => {
|
|
189
|
+
function key(password, salt) {
|
|
190
|
+
return new Uint8Array(scryptSync(password.normalize("NFKC"), Buffer.from(salt, "base64url"), 32, { N: 2 ** 15, r: 8, p: 1, maxmem: 64 * 1024 * 1024 }));
|
|
191
|
+
}
|
|
192
|
+
BackupFile.key = key;
|
|
193
|
+
function header(name, password) {
|
|
194
|
+
const header2 = { v: 1, name, createdAt: Date.now(), ...password ? { salt: randomBytes(16).toString("base64url") } : {} };
|
|
195
|
+
const json = encoder.encode(JSON.stringify(header2));
|
|
196
|
+
const bytes = new Uint8Array(MAGIC.length + 4 + json.length);
|
|
197
|
+
bytes.set(MAGIC);
|
|
198
|
+
new DataView(bytes.buffer).setUint32(MAGIC.length, json.length);
|
|
199
|
+
bytes.set(json, MAGIC.length + 4);
|
|
200
|
+
return { bytes, header: header2, cipher: password && header2.salt ? new FrameCipher(key(password, header2.salt)) : undefined };
|
|
201
|
+
}
|
|
202
|
+
BackupFile.header = header;
|
|
203
|
+
async function readHeader(reader) {
|
|
204
|
+
const magic = await reader.take(MAGIC.length);
|
|
205
|
+
if (!magic || decoder.decode(magic) !== "TABACKUP") {
|
|
206
|
+
throw new Error("This is not a backup of Ta.");
|
|
207
|
+
}
|
|
208
|
+
const length = await reader.take(4);
|
|
209
|
+
const size = length ? new DataView(length.buffer, length.byteOffset, 4).getUint32(0) : 0;
|
|
210
|
+
const json = size > 0 && size < 64 * 1024 ? await reader.take(size) : undefined;
|
|
211
|
+
const header2 = json ? JSON.parse(decoder.decode(json)) : undefined;
|
|
212
|
+
if (header2?.v !== 1) {
|
|
213
|
+
throw new Error("This backup is of a newer Ta: update Ta first.");
|
|
214
|
+
}
|
|
215
|
+
return header2;
|
|
216
|
+
}
|
|
217
|
+
BackupFile.readHeader = readHeader;
|
|
218
|
+
})(BackupFile ||= {});
|
|
219
|
+
|
|
220
|
+
// web-src/sync-codec.ts
|
|
221
|
+
import { createHash } from "node:crypto";
|
|
222
|
+
import { gunzipSync, gzipSync } from "node:zlib";
|
|
223
|
+
|
|
224
|
+
// web-src/sync-merge.ts
|
|
225
|
+
var SyncMerge;
|
|
226
|
+
((SyncMerge) => {
|
|
227
|
+
function decide(local, remote, base, mergeable) {
|
|
228
|
+
const l = local?.h ?? "";
|
|
229
|
+
const r = remote && !remote.deleted ? remote.h : "";
|
|
230
|
+
if (l === r) {
|
|
231
|
+
return l === "" && !remote ? "forget" : "none";
|
|
232
|
+
}
|
|
233
|
+
if (base === undefined) {
|
|
234
|
+
if (l === "") {
|
|
235
|
+
return "pull";
|
|
236
|
+
}
|
|
237
|
+
if (r === "") {
|
|
238
|
+
return !remote || (local?.at ?? 0) > remote.at ? "push" : "pull";
|
|
239
|
+
}
|
|
240
|
+
return mergeable ? "merge" : "keepRemote";
|
|
241
|
+
}
|
|
242
|
+
if (l === base) {
|
|
243
|
+
return "pull";
|
|
244
|
+
}
|
|
245
|
+
if (r === base) {
|
|
246
|
+
return "push";
|
|
247
|
+
}
|
|
248
|
+
if (mergeable && l !== "" && r !== "") {
|
|
249
|
+
return "merge";
|
|
250
|
+
}
|
|
251
|
+
return (local?.at ?? 0) > (remote?.at ?? 0) ? "keepLocal" : "keepRemote";
|
|
252
|
+
}
|
|
253
|
+
SyncMerge.decide = decide;
|
|
254
|
+
function value(base, local, remote) {
|
|
255
|
+
if (isObject(local) && isObject(remote)) {
|
|
256
|
+
return object(isObject(base) ? base : undefined, local, remote);
|
|
257
|
+
}
|
|
258
|
+
if (same(local, base)) {
|
|
259
|
+
return remote;
|
|
260
|
+
}
|
|
261
|
+
return same(remote, base) ? local : remote;
|
|
262
|
+
}
|
|
263
|
+
SyncMerge.value = value;
|
|
264
|
+
function object(base, local, remote) {
|
|
265
|
+
const merged = {};
|
|
266
|
+
for (const key of new Set([...Object.keys(local), ...Object.keys(remote)])) {
|
|
267
|
+
const next = value(base?.[key], local[key], remote[key]);
|
|
268
|
+
if (next !== undefined) {
|
|
269
|
+
merged[key] = next;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return merged;
|
|
273
|
+
}
|
|
274
|
+
SyncMerge.object = object;
|
|
275
|
+
function set(base, local, remote) {
|
|
276
|
+
const had = new Set((base ?? []).map((entry) => canonical(entry)));
|
|
277
|
+
const here = new Set(local.map((entry) => canonical(entry)));
|
|
278
|
+
const there = new Set(remote.map((entry) => canonical(entry)));
|
|
279
|
+
const keeps = (key) => here.has(key) && there.has(key) || !had.has(key) && (here.has(key) || there.has(key));
|
|
280
|
+
const merged = [];
|
|
281
|
+
const seen = new Set;
|
|
282
|
+
for (const entry of [...local, ...remote]) {
|
|
283
|
+
const key = canonical(entry);
|
|
284
|
+
if (!seen.has(key) && keeps(key)) {
|
|
285
|
+
seen.add(key);
|
|
286
|
+
merged.push(entry);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return merged;
|
|
290
|
+
}
|
|
291
|
+
SyncMerge.set = set;
|
|
292
|
+
function canonical(value2) {
|
|
293
|
+
return JSON.stringify(sorted(value2)) ?? "null";
|
|
294
|
+
}
|
|
295
|
+
SyncMerge.canonical = canonical;
|
|
296
|
+
function same(a, b) {
|
|
297
|
+
return canonical(a) === canonical(b);
|
|
298
|
+
}
|
|
299
|
+
SyncMerge.same = same;
|
|
300
|
+
function sorted(value2) {
|
|
301
|
+
if (Array.isArray(value2)) {
|
|
302
|
+
return value2.map(sorted);
|
|
303
|
+
}
|
|
304
|
+
if (isObject(value2)) {
|
|
305
|
+
return Object.fromEntries(Object.keys(value2).sort().filter((key) => value2[key] !== undefined).map((key) => [key, sorted(value2[key])]));
|
|
306
|
+
}
|
|
307
|
+
return value2;
|
|
308
|
+
}
|
|
309
|
+
function isObject(value2) {
|
|
310
|
+
return !!value2 && typeof value2 === "object" && !Array.isArray(value2);
|
|
311
|
+
}
|
|
312
|
+
SyncMerge.isObject = isObject;
|
|
313
|
+
})(SyncMerge ||= {});
|
|
314
|
+
|
|
315
|
+
// web-src/sync-codec.ts
|
|
316
|
+
var encoder2 = new TextEncoder;
|
|
317
|
+
var decoder2 = new TextDecoder;
|
|
318
|
+
var SyncCodec;
|
|
319
|
+
((SyncCodec) => {
|
|
320
|
+
SyncCodec.PIECE = 48 * 1024;
|
|
321
|
+
function hash(bytes) {
|
|
322
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
323
|
+
}
|
|
324
|
+
SyncCodec.hash = hash;
|
|
325
|
+
function encode(bytes, piece = SyncCodec.PIECE) {
|
|
326
|
+
const text = Buffer.from(gzipSync(bytes)).toString("base64");
|
|
327
|
+
const pieces = [];
|
|
328
|
+
for (let start = 0;start < text.length; start += piece) {
|
|
329
|
+
pieces.push(text.slice(start, start + piece));
|
|
330
|
+
}
|
|
331
|
+
return pieces.length ? pieces : [""];
|
|
332
|
+
}
|
|
333
|
+
SyncCodec.encode = encode;
|
|
334
|
+
function decode(pieces) {
|
|
335
|
+
return new Uint8Array(gunzipSync(Buffer.from(pieces.join(""), "base64")));
|
|
336
|
+
}
|
|
337
|
+
SyncCodec.decode = decode;
|
|
338
|
+
function json(value) {
|
|
339
|
+
return encoder2.encode(SyncMerge.canonical(value));
|
|
340
|
+
}
|
|
341
|
+
SyncCodec.json = json;
|
|
342
|
+
function parse(bytes) {
|
|
343
|
+
return JSON.parse(decoder2.decode(bytes));
|
|
344
|
+
}
|
|
345
|
+
SyncCodec.parse = parse;
|
|
346
|
+
function pack(files) {
|
|
347
|
+
const entries = [...files].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
348
|
+
return json(Object.fromEntries(entries.map(([path, bytes]) => [path, Buffer.from(bytes).toString("base64")])));
|
|
349
|
+
}
|
|
350
|
+
SyncCodec.pack = pack;
|
|
351
|
+
function unpack(bytes) {
|
|
352
|
+
const data = parse(bytes);
|
|
353
|
+
const files = new Map;
|
|
354
|
+
if (SyncMerge.isObject(data)) {
|
|
355
|
+
for (const [path, text] of Object.entries(data)) {
|
|
356
|
+
if (typeof text === "string") {
|
|
357
|
+
files.set(path, new Uint8Array(Buffer.from(text, "base64")));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return files;
|
|
362
|
+
}
|
|
363
|
+
SyncCodec.unpack = unpack;
|
|
364
|
+
})(SyncCodec ||= {});
|
|
365
|
+
|
|
366
|
+
// web-src/sync-items.ts
|
|
367
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
368
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
369
|
+
var SETTINGS = "settings.json";
|
|
370
|
+
var SYNC_FILES = {
|
|
371
|
+
"keybindings.json": "settings",
|
|
372
|
+
"AGENTS.md": "instructions",
|
|
373
|
+
"AGENTS.override.md": "instructions",
|
|
374
|
+
"CLAUDE.md": "instructions",
|
|
375
|
+
"SYSTEM.md": "instructions",
|
|
376
|
+
"APPEND_SYSTEM.md": "instructions",
|
|
377
|
+
"models.json": "models",
|
|
378
|
+
"mcp.json": "models"
|
|
379
|
+
};
|
|
380
|
+
var IDENTITY_FILES = ["identity.json", "rooms.json", "friends.json"];
|
|
381
|
+
var SYNC_UNITS = { skills: "skills", prompts: "skills", themes: "skills", extensions: "extensions" };
|
|
382
|
+
var SKIPPED = new Set(["node_modules", ".git", ".DS_Store"]);
|
|
383
|
+
var MAX_FILE = 4 * 1024 * 1024;
|
|
384
|
+
var PORTABLE = /^(npm:|git:|github:|git@|https?:\/\/)/;
|
|
385
|
+
var SyncItems;
|
|
386
|
+
((SyncItems) => {
|
|
387
|
+
SyncItems.MACHINE_SETTINGS = new Set([
|
|
388
|
+
"lastChangelogVersion",
|
|
389
|
+
"sessionDir",
|
|
390
|
+
"shellPath",
|
|
391
|
+
"shellCommandPrefix",
|
|
392
|
+
"npmCommand",
|
|
393
|
+
"selfUpdateCommand",
|
|
394
|
+
"httpProxy",
|
|
395
|
+
"externalEditor",
|
|
396
|
+
"packages",
|
|
397
|
+
"extensions",
|
|
398
|
+
"skills",
|
|
399
|
+
"prompts",
|
|
400
|
+
"themes"
|
|
401
|
+
]);
|
|
402
|
+
function resourceOf(id) {
|
|
403
|
+
if (id === "settings") {
|
|
404
|
+
return "settings";
|
|
405
|
+
}
|
|
406
|
+
if (id === "packages") {
|
|
407
|
+
return "extensions";
|
|
408
|
+
}
|
|
409
|
+
if (id.startsWith("data:")) {
|
|
410
|
+
return "data";
|
|
411
|
+
}
|
|
412
|
+
if (id.startsWith("file:")) {
|
|
413
|
+
return SYNC_FILES[id.slice(5)];
|
|
414
|
+
}
|
|
415
|
+
return SYNC_UNITS[id.split("/")[0]];
|
|
416
|
+
}
|
|
417
|
+
SyncItems.resourceOf = resourceOf;
|
|
418
|
+
function portableSettings(settings) {
|
|
419
|
+
return Object.fromEntries(Object.entries(settings).filter(([key]) => !SyncItems.MACHINE_SETTINGS.has(key)));
|
|
420
|
+
}
|
|
421
|
+
SyncItems.portableSettings = portableSettings;
|
|
422
|
+
function withPortable(settings, portable) {
|
|
423
|
+
const machine = Object.entries(settings).filter(([key]) => SyncItems.MACHINE_SETTINGS.has(key));
|
|
424
|
+
return { ...Object.fromEntries(machine), ...portableSettings(portable) };
|
|
425
|
+
}
|
|
426
|
+
SyncItems.withPortable = withPortable;
|
|
427
|
+
function source(entry) {
|
|
428
|
+
const value = typeof entry === "string" ? entry : SyncMerge.isObject(entry) && typeof entry.source === "string" ? entry.source : undefined;
|
|
429
|
+
return value && PORTABLE.test(value) ? value : undefined;
|
|
430
|
+
}
|
|
431
|
+
SyncItems.source = source;
|
|
432
|
+
function portablePackages(settings) {
|
|
433
|
+
return Array.isArray(settings.packages) ? settings.packages.filter((entry) => source(entry) !== undefined) : [];
|
|
434
|
+
}
|
|
435
|
+
SyncItems.portablePackages = portablePackages;
|
|
436
|
+
function dataId(name, key) {
|
|
437
|
+
return `data:${name}:${key}`;
|
|
438
|
+
}
|
|
439
|
+
SyncItems.dataId = dataId;
|
|
440
|
+
function parseDataId(id) {
|
|
441
|
+
const match = /^data:([^:]+):(.+)$/.exec(id);
|
|
442
|
+
return match ? { name: match[1], key: match[2] } : undefined;
|
|
443
|
+
}
|
|
444
|
+
SyncItems.parseDataId = parseDataId;
|
|
445
|
+
function safePath(path) {
|
|
446
|
+
return path.length > 0 && !path.startsWith("/") && !path.includes("\\") && path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
|
|
447
|
+
}
|
|
448
|
+
SyncItems.safePath = safePath;
|
|
449
|
+
function item(id, type, bytes, at, value) {
|
|
450
|
+
return { id, type, h: SyncCodec.hash(bytes), at, bytes, ...value === undefined ? {} : { value } };
|
|
451
|
+
}
|
|
452
|
+
SyncItems.item = item;
|
|
453
|
+
})(SyncItems ||= {});
|
|
454
|
+
async function readText(file) {
|
|
455
|
+
try {
|
|
456
|
+
const info = await stat(file);
|
|
457
|
+
if (!info.isFile()) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
return { bytes: new Uint8Array(await readFile(file)), at: info.mtimeMs };
|
|
461
|
+
} catch {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async function walk(root) {
|
|
466
|
+
const files = new Map;
|
|
467
|
+
let at = 0;
|
|
468
|
+
const visit = async (dir) => {
|
|
469
|
+
for (const entry of await readdir(dir, { withFileTypes: true }).catch(() => [])) {
|
|
470
|
+
if (SKIPPED.has(entry.name)) {
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const path = join(dir, entry.name);
|
|
474
|
+
if (entry.isDirectory()) {
|
|
475
|
+
await visit(path);
|
|
476
|
+
} else if (entry.isFile()) {
|
|
477
|
+
const info = await stat(path).catch(() => {
|
|
478
|
+
return;
|
|
479
|
+
});
|
|
480
|
+
if (info && info.size <= MAX_FILE) {
|
|
481
|
+
files.set(relative(root, path).split(sep).join("/"), new Uint8Array(await readFile(path)));
|
|
482
|
+
at = Math.max(at, info.mtimeMs);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
await visit(root);
|
|
488
|
+
return { files, at };
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
class AgentDirPlace {
|
|
492
|
+
agentDir;
|
|
493
|
+
side;
|
|
494
|
+
now;
|
|
495
|
+
replacedDir;
|
|
496
|
+
stamp;
|
|
497
|
+
tracked = new Set;
|
|
498
|
+
constructor(agentDir, side, now = Date.now) {
|
|
499
|
+
this.agentDir = agentDir;
|
|
500
|
+
this.side = side;
|
|
501
|
+
this.now = now;
|
|
502
|
+
this.replacedDir = join(agentDir, "sync", "replaced");
|
|
503
|
+
}
|
|
504
|
+
begin() {
|
|
505
|
+
this.stamp = undefined;
|
|
506
|
+
}
|
|
507
|
+
async read(resources) {
|
|
508
|
+
const items = new Map;
|
|
509
|
+
const add = (item) => item && items.set(item.id, item);
|
|
510
|
+
if (resources.has("settings") || resources.has("extensions")) {
|
|
511
|
+
const file = await readText(join(this.agentDir, SETTINGS));
|
|
512
|
+
const parsed = file ? safeParse(file.bytes) : {};
|
|
513
|
+
const settings = SyncMerge.isObject(parsed) ? parsed : undefined;
|
|
514
|
+
if (settings && resources.has("settings")) {
|
|
515
|
+
const portable = SyncItems.portableSettings(settings);
|
|
516
|
+
add(SyncItems.item("settings", "json", SyncCodec.json(portable), file?.at ?? 0, portable));
|
|
517
|
+
}
|
|
518
|
+
if (settings && resources.has("extensions")) {
|
|
519
|
+
const packages = SyncItems.portablePackages(settings);
|
|
520
|
+
add(SyncItems.item("packages", "set", SyncCodec.json(packages), file?.at ?? 0, packages));
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
for (const [name, resource] of Object.entries(SYNC_FILES)) {
|
|
524
|
+
if (resources.has(resource)) {
|
|
525
|
+
const file = await readText(join(this.agentDir, name));
|
|
526
|
+
add(file && SyncItems.item(`file:${name}`, "bytes", file.bytes, file.at));
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
for (const [dir, resource] of Object.entries(SYNC_UNITS)) {
|
|
530
|
+
if (resources.has(resource)) {
|
|
531
|
+
for (const item of await this.units(dir)) {
|
|
532
|
+
add(item);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (resources.has("data")) {
|
|
537
|
+
const keys = await this.side.dataKeys();
|
|
538
|
+
this.tracked = new Set(keys.map(({ name, key }) => SyncItems.dataId(name, key)));
|
|
539
|
+
for (const { name, key } of keys) {
|
|
540
|
+
const value = await this.side.getData(name, key).catch(() => {
|
|
541
|
+
return;
|
|
542
|
+
});
|
|
543
|
+
if (value !== undefined && value !== null) {
|
|
544
|
+
add(SyncItems.item(SyncItems.dataId(name, key), "json", SyncCodec.json(value), 0, value));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return items;
|
|
549
|
+
}
|
|
550
|
+
async apply(id, type, content, aside) {
|
|
551
|
+
if (aside) {
|
|
552
|
+
await this.asideOf(id, type);
|
|
553
|
+
}
|
|
554
|
+
if (id === "settings") {
|
|
555
|
+
const portable = content ? SyncCodec.parse(content) : {};
|
|
556
|
+
await this.side.updateSettings((settings) => SyncItems.withPortable(settings, SyncMerge.isObject(portable) ? portable : {}));
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (id === "packages") {
|
|
560
|
+
await this.applyPackages(content ? SyncCodec.parse(content) : []);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const data = SyncItems.parseDataId(id);
|
|
564
|
+
if (data) {
|
|
565
|
+
await this.side.setData(data.name, data.key, content ? SyncCodec.parse(content) : null);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const path = this.pathOf(id);
|
|
569
|
+
if (!path) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (!content) {
|
|
573
|
+
if (!aside) {
|
|
574
|
+
await this.asideOf(id, type);
|
|
575
|
+
}
|
|
576
|
+
await rm(path, { recursive: true, force: true });
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (type === "tree") {
|
|
580
|
+
if ((await stat(path).catch(() => {
|
|
581
|
+
return;
|
|
582
|
+
}))?.isFile()) {
|
|
583
|
+
await rm(path, { force: true });
|
|
584
|
+
}
|
|
585
|
+
await this.writeTree(path, SyncCodec.unpack(content));
|
|
586
|
+
} else {
|
|
587
|
+
await rm(path, { recursive: true, force: true });
|
|
588
|
+
await this.writeFile(path, content);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
async keep(id, type, content, from) {
|
|
592
|
+
const target = join(this.folder(), `from-${from.replace(/[^\w.-]/g, "_")}`, this.asidePath(id));
|
|
593
|
+
if (type === "tree") {
|
|
594
|
+
await this.writeTree(target, SyncCodec.unpack(content));
|
|
595
|
+
} else {
|
|
596
|
+
await this.writeFile(target, content);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
tracks(name, key) {
|
|
600
|
+
return this.tracked.has(SyncItems.dataId(name, key));
|
|
601
|
+
}
|
|
602
|
+
async signature(resources) {
|
|
603
|
+
const parts = [];
|
|
604
|
+
const note = async (path, deep) => {
|
|
605
|
+
const info = await stat(path).catch(() => {
|
|
606
|
+
return;
|
|
607
|
+
});
|
|
608
|
+
if (!info) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
parts.push(`${relative(this.agentDir, path)}:${info.mtimeMs}:${info.size}`);
|
|
612
|
+
if (deep && info.isDirectory()) {
|
|
613
|
+
for (const entry of await readdir(path).catch(() => [])) {
|
|
614
|
+
if (!SKIPPED.has(entry)) {
|
|
615
|
+
await note(join(path, entry), true);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
if (resources.has("settings") || resources.has("extensions")) {
|
|
621
|
+
await note(join(this.agentDir, SETTINGS), false);
|
|
622
|
+
}
|
|
623
|
+
for (const [name, resource] of Object.entries(SYNC_FILES)) {
|
|
624
|
+
if (resources.has(resource)) {
|
|
625
|
+
await note(join(this.agentDir, name), false);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
for (const [dir, resource] of Object.entries(SYNC_UNITS)) {
|
|
629
|
+
if (resources.has(resource)) {
|
|
630
|
+
await note(join(this.agentDir, dir), true);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
if (resources.has("identity")) {
|
|
634
|
+
for (const name of IDENTITY_FILES) {
|
|
635
|
+
await note(join(this.agentDir, name), false);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return parts.join(`
|
|
639
|
+
`);
|
|
640
|
+
}
|
|
641
|
+
get usedFolder() {
|
|
642
|
+
return this.stamp ? join(this.replacedDir, this.stamp) : undefined;
|
|
643
|
+
}
|
|
644
|
+
async keepAside(path, bytes) {
|
|
645
|
+
await this.writeFile(join(this.folder(), ...path.split("/")), bytes);
|
|
646
|
+
}
|
|
647
|
+
async replacedCount() {
|
|
648
|
+
const folders = await readdir(this.replacedDir).catch(() => []);
|
|
649
|
+
return folders.length;
|
|
650
|
+
}
|
|
651
|
+
async units(dir) {
|
|
652
|
+
const root = join(this.agentDir, dir);
|
|
653
|
+
const items = [];
|
|
654
|
+
for (const entry of await readdir(root, { withFileTypes: true }).catch(() => [])) {
|
|
655
|
+
if (SKIPPED.has(entry.name) || entry.name.startsWith(".")) {
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
const id = `${dir}/${entry.name}`;
|
|
659
|
+
const path = join(root, entry.name);
|
|
660
|
+
if (entry.isDirectory()) {
|
|
661
|
+
const { files, at } = await walk(path);
|
|
662
|
+
if (files.size) {
|
|
663
|
+
items.push(SyncItems.item(id, "tree", SyncCodec.pack(files), at));
|
|
664
|
+
}
|
|
665
|
+
} else if (entry.isFile()) {
|
|
666
|
+
const file = await readText(path);
|
|
667
|
+
if (file && file.bytes.length <= MAX_FILE) {
|
|
668
|
+
items.push(SyncItems.item(id, "bytes", file.bytes, file.at));
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return items;
|
|
673
|
+
}
|
|
674
|
+
pathOf(id) {
|
|
675
|
+
const path = id.startsWith("file:") ? id.slice(5) : id;
|
|
676
|
+
if (!SyncItems.resourceOf(id) || !SyncItems.safePath(path)) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
return join(this.agentDir, ...path.split("/"));
|
|
680
|
+
}
|
|
681
|
+
asidePath(id) {
|
|
682
|
+
const data = SyncItems.parseDataId(id);
|
|
683
|
+
if (data) {
|
|
684
|
+
return join("data", ...data.name.split("/"), `${data.key.replace(/[^\w.-]/g, "_")}.json`);
|
|
685
|
+
}
|
|
686
|
+
if (id === "settings" || id === "packages") {
|
|
687
|
+
return `${id}.json`;
|
|
688
|
+
}
|
|
689
|
+
return (id.startsWith("file:") ? id.slice(5) : id).split("/").join(sep);
|
|
690
|
+
}
|
|
691
|
+
folder() {
|
|
692
|
+
if (!this.stamp) {
|
|
693
|
+
const date = new Date(this.now());
|
|
694
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
695
|
+
this.stamp = `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
|
696
|
+
}
|
|
697
|
+
return join(this.replacedDir, this.stamp);
|
|
698
|
+
}
|
|
699
|
+
async asideOf(id, type) {
|
|
700
|
+
const target = join(this.folder(), this.asidePath(id));
|
|
701
|
+
if (id === "settings" || id === "packages" || id.startsWith("data:")) {
|
|
702
|
+
const current = (await this.read(new Set([SyncItems.resourceOf(id)]))).get(id);
|
|
703
|
+
if (current) {
|
|
704
|
+
await this.writeFile(target, current.bytes);
|
|
705
|
+
}
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
const path = this.pathOf(id);
|
|
709
|
+
if (!path) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
if (type === "tree") {
|
|
713
|
+
const { files } = await walk(path);
|
|
714
|
+
if (files.size) {
|
|
715
|
+
await this.writeTree(target, files);
|
|
716
|
+
}
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const file = await readText(path);
|
|
720
|
+
if (file) {
|
|
721
|
+
await this.writeFile(target, file.bytes);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
async applyPackages(value) {
|
|
725
|
+
const wanted = Array.isArray(value) ? value.map((entry) => SyncItems.source(entry)).filter((entry) => !!entry) : [];
|
|
726
|
+
const file = await readText(join(this.agentDir, SETTINGS));
|
|
727
|
+
const settings = file ? safeParse(file.bytes) : {};
|
|
728
|
+
const have = SyncMerge.isObject(settings) ? SyncItems.portablePackages(settings).map((entry) => SyncItems.source(entry)).filter((entry) => !!entry) : [];
|
|
729
|
+
for (const source of wanted.filter((entry) => !have.includes(entry))) {
|
|
730
|
+
await this.side.installPackage(source);
|
|
731
|
+
}
|
|
732
|
+
for (const source of have.filter((entry) => !wanted.includes(entry))) {
|
|
733
|
+
await this.side.removePackage(source);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
async writeTree(root, files) {
|
|
737
|
+
const { files: before } = await walk(root);
|
|
738
|
+
for (const path of before.keys()) {
|
|
739
|
+
if (!files.has(path)) {
|
|
740
|
+
await rm(join(root, ...path.split("/")), { force: true });
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
for (const [path, bytes] of files) {
|
|
744
|
+
if (SyncItems.safePath(path)) {
|
|
745
|
+
await this.writeFile(join(root, ...path.split("/")), bytes);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
async writeFile(path, bytes) {
|
|
750
|
+
await mkdir(dirname(path), { recursive: true });
|
|
751
|
+
const temporary = `${path}.ta-sync-${process.pid}`;
|
|
752
|
+
await writeFile(temporary, bytes);
|
|
753
|
+
await rename(temporary, path);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
function safeParse(bytes) {
|
|
757
|
+
try {
|
|
758
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
759
|
+
} catch {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// web-src/migrate-archive.ts
|
|
765
|
+
var ITEMS = {
|
|
766
|
+
settings: "settings",
|
|
767
|
+
instructions: "instructions",
|
|
768
|
+
skills: "skills",
|
|
769
|
+
models: "models",
|
|
770
|
+
extensions: "extensions"
|
|
771
|
+
};
|
|
772
|
+
var ORDER = [
|
|
773
|
+
"conversations",
|
|
774
|
+
"changes",
|
|
775
|
+
"settings",
|
|
776
|
+
"instructions",
|
|
777
|
+
"skills",
|
|
778
|
+
"models",
|
|
779
|
+
"extensions",
|
|
780
|
+
"credentials",
|
|
781
|
+
"identity",
|
|
782
|
+
"data"
|
|
783
|
+
];
|
|
784
|
+
var SYNC_ROOM_KIND = "ta-sync";
|
|
785
|
+
var OWN = "@celljs/ta-sync";
|
|
786
|
+
var MAX_ENTRY = 512 * 1024 * 1024;
|
|
787
|
+
async function files(root, keep = () => true) {
|
|
788
|
+
const found = [];
|
|
789
|
+
const visit = async (dir) => {
|
|
790
|
+
for (const entry of await readdir2(dir, { withFileTypes: true }).catch(() => [])) {
|
|
791
|
+
const path = join2(dir, entry.name);
|
|
792
|
+
if (entry.isDirectory()) {
|
|
793
|
+
await visit(path);
|
|
794
|
+
} else if (entry.isFile()) {
|
|
795
|
+
const rel = relative2(root, path).split(sep2).join("/");
|
|
796
|
+
if (keep(rel)) {
|
|
797
|
+
found.push({ path: rel, size: (await stat2(path)).size });
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
await visit(root);
|
|
803
|
+
return found.sort((a, b) => a.path < b.path ? -1 : 1);
|
|
804
|
+
}
|
|
805
|
+
var MigrateUtil;
|
|
806
|
+
((MigrateUtil) => {
|
|
807
|
+
function sessionDir(cwd) {
|
|
808
|
+
return `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
|
809
|
+
}
|
|
810
|
+
MigrateUtil.sessionDir = sessionDir;
|
|
811
|
+
function remapPath(path, from, to) {
|
|
812
|
+
if (from.agentDir && (path === from.agentDir || path.startsWith(`${from.agentDir}/`))) {
|
|
813
|
+
return to.agentDir + path.slice(from.agentDir.length);
|
|
814
|
+
}
|
|
815
|
+
if (from.home && (path === from.home || path.startsWith(`${from.home}/`))) {
|
|
816
|
+
return to.home + path.slice(from.home.length);
|
|
817
|
+
}
|
|
818
|
+
return path;
|
|
819
|
+
}
|
|
820
|
+
MigrateUtil.remapPath = remapPath;
|
|
821
|
+
function remapText(text, pairs) {
|
|
822
|
+
let out = text;
|
|
823
|
+
for (const [before, after] of pairs) {
|
|
824
|
+
if (before && before !== after) {
|
|
825
|
+
out = out.split(before).join(after);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return out;
|
|
829
|
+
}
|
|
830
|
+
MigrateUtil.remapText = remapText;
|
|
831
|
+
})(MigrateUtil ||= {});
|
|
832
|
+
|
|
833
|
+
class MigrateSource {
|
|
834
|
+
agentDir;
|
|
835
|
+
place;
|
|
836
|
+
name;
|
|
837
|
+
side;
|
|
838
|
+
constructor(agentDir, place, name = hostname().replace(/\.local$/, ""), side) {
|
|
839
|
+
this.agentDir = agentDir;
|
|
840
|
+
this.place = place;
|
|
841
|
+
this.name = name;
|
|
842
|
+
this.side = side;
|
|
843
|
+
}
|
|
844
|
+
async manifest(categories, entries) {
|
|
845
|
+
const counts = {};
|
|
846
|
+
let bytes = 0;
|
|
847
|
+
for (const { head } of entries) {
|
|
848
|
+
const category = head.c;
|
|
849
|
+
counts[category] ??= { files: 0, bytes: 0 };
|
|
850
|
+
const count = counts[category];
|
|
851
|
+
count.files++;
|
|
852
|
+
count.bytes += head.size ?? 0;
|
|
853
|
+
bytes += head.size ?? 0;
|
|
854
|
+
}
|
|
855
|
+
return {
|
|
856
|
+
v: 1,
|
|
857
|
+
from: { name: this.name, platform: platform(), home: homedir(), agentDir: this.agentDir },
|
|
858
|
+
categories: [...categories],
|
|
859
|
+
counts,
|
|
860
|
+
bytes,
|
|
861
|
+
createdAt: Date.now()
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
async counts() {
|
|
865
|
+
const entries = await this.entries(ORDER);
|
|
866
|
+
const counts = Object.fromEntries(ORDER.map((category) => [category, { files: 0, bytes: 0 }]));
|
|
867
|
+
for (const { head } of entries) {
|
|
868
|
+
counts[head.c].files++;
|
|
869
|
+
counts[head.c].bytes += head.size ?? 0;
|
|
870
|
+
}
|
|
871
|
+
return counts;
|
|
872
|
+
}
|
|
873
|
+
async entries(categories) {
|
|
874
|
+
const wanted = new Set(categories);
|
|
875
|
+
const entries = [];
|
|
876
|
+
const file = (c, k, path, size) => entries.push({ head: { k, c, path, size }, read: async () => new Uint8Array(await readFile2(join2(this.agentDir, ...path.split("/")))) });
|
|
877
|
+
for (const category of ORDER.filter((each) => wanted.has(each))) {
|
|
878
|
+
if (category === "conversations") {
|
|
879
|
+
for (const found of await files(join2(this.agentDir, "sessions"), (path) => path.endsWith(".jsonl"))) {
|
|
880
|
+
file(category, "file", `sessions/${found.path}`, found.size);
|
|
881
|
+
}
|
|
882
|
+
} else if (category === "changes") {
|
|
883
|
+
for (const found of await files(join2(this.agentDir, "changes"))) {
|
|
884
|
+
file(category, "file", `changes/${found.path}`, found.size);
|
|
885
|
+
}
|
|
886
|
+
} else if (category === "credentials") {
|
|
887
|
+
await this.json(entries, category, "auth.json");
|
|
888
|
+
for (const found of await files(join2(this.agentDir, "bots"))) {
|
|
889
|
+
file(category, "file", `bots/${found.path}`, found.size);
|
|
890
|
+
}
|
|
891
|
+
} else if (category === "identity") {
|
|
892
|
+
const identity = this.side?.identity;
|
|
893
|
+
const bundle = identity ? await identity.exportIdentity().catch(() => {
|
|
894
|
+
return;
|
|
895
|
+
}) : undefined;
|
|
896
|
+
if (identity && bundle) {
|
|
897
|
+
const bytes = SyncCodec.json({ ...bundle, rooms: bundle.rooms.filter((room) => room.kind !== SYNC_ROOM_KIND) });
|
|
898
|
+
entries.push({
|
|
899
|
+
head: { k: "identity", c: category, size: bytes.length },
|
|
900
|
+
read: async () => {
|
|
901
|
+
await identity.setIdentityShared(true).catch(() => {
|
|
902
|
+
return;
|
|
903
|
+
});
|
|
904
|
+
return bytes;
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
} else if (category === "data") {
|
|
909
|
+
const root = join2(this.agentDir, "extension-data");
|
|
910
|
+
for (const found of await files(root, (path) => path.endsWith(".json") && !path.startsWith("directories/"))) {
|
|
911
|
+
const name = found.path.slice(0, -".json".length);
|
|
912
|
+
if (name !== OWN) {
|
|
913
|
+
entries.push({
|
|
914
|
+
head: { k: "data", c: category, name, size: found.size },
|
|
915
|
+
read: async () => new Uint8Array(await readFile2(join2(root, ...found.path.split("/"))))
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
} else {
|
|
920
|
+
if (category === "settings") {
|
|
921
|
+
await this.json(entries, category, "ta.json");
|
|
922
|
+
}
|
|
923
|
+
const resource = ITEMS[category];
|
|
924
|
+
for (const item of (await this.place.read(new Set([resource]))).values()) {
|
|
925
|
+
entries.push({
|
|
926
|
+
head: { k: "item", c: category, id: item.id, t: item.type, at: Math.round(item.at), size: item.bytes.length },
|
|
927
|
+
read: async () => item.bytes
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
return entries;
|
|
933
|
+
}
|
|
934
|
+
async write(entries, send, progress) {
|
|
935
|
+
let done = 0;
|
|
936
|
+
for (const entry of entries) {
|
|
937
|
+
const bytes = await entry.read();
|
|
938
|
+
await send(Frames.record({ ...entry.head, size: bytes.length }));
|
|
939
|
+
for (let at = 0;at < bytes.length; at += PIECE) {
|
|
940
|
+
const piece = bytes.subarray(at, at + PIECE);
|
|
941
|
+
await send(Frames.bytes(piece));
|
|
942
|
+
done += piece.length;
|
|
943
|
+
progress(done);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
await send(Frames.record({ k: "end" }));
|
|
947
|
+
}
|
|
948
|
+
async json(entries, c, path) {
|
|
949
|
+
const info = await stat2(join2(this.agentDir, path)).catch(() => {
|
|
950
|
+
return;
|
|
951
|
+
});
|
|
952
|
+
if (info?.isFile()) {
|
|
953
|
+
entries.push({ head: { k: "json", c, path, size: info.size }, read: async () => new Uint8Array(await readFile2(join2(this.agentDir, path))) });
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
class MigrateTarget {
|
|
959
|
+
agentDir;
|
|
960
|
+
place;
|
|
961
|
+
side;
|
|
962
|
+
manifest;
|
|
963
|
+
report = { conversations: 0, keptConversations: 0, items: 0, data: 0, extensions: 0, files: 0, skipped: [] };
|
|
964
|
+
to = { home: homedir(), agentDir: "" };
|
|
965
|
+
sessions = new Map;
|
|
966
|
+
known;
|
|
967
|
+
locals;
|
|
968
|
+
asideData = new Set;
|
|
969
|
+
constructor(agentDir, place, side, manifest) {
|
|
970
|
+
this.agentDir = agentDir;
|
|
971
|
+
this.place = place;
|
|
972
|
+
this.side = side;
|
|
973
|
+
this.manifest = manifest;
|
|
974
|
+
this.to.agentDir = agentDir;
|
|
975
|
+
}
|
|
976
|
+
static check(head) {
|
|
977
|
+
if ((head.size ?? 0) > MAX_ENTRY) {
|
|
978
|
+
throw new Error(`${head.path ?? head.id ?? head.name} is larger than 512 MB.`);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
async apply(head, bytes) {
|
|
982
|
+
const id = head.path ?? head.id ?? head.name ?? "?";
|
|
983
|
+
try {
|
|
984
|
+
if (head.k === "file" && head.c === "conversations") {
|
|
985
|
+
await this.conversation(head.path, bytes);
|
|
986
|
+
} else if (head.k === "file") {
|
|
987
|
+
await this.file(head, bytes);
|
|
988
|
+
} else if (head.k === "json") {
|
|
989
|
+
await this.json(head.path, bytes);
|
|
990
|
+
} else if (head.k === "item") {
|
|
991
|
+
await this.item(head, bytes);
|
|
992
|
+
} else if (head.k === "data") {
|
|
993
|
+
await this.data(head.name, bytes);
|
|
994
|
+
} else if (head.k === "identity") {
|
|
995
|
+
await this.identity(bytes);
|
|
996
|
+
}
|
|
997
|
+
} catch (error) {
|
|
998
|
+
this.report.skipped.push({ id, reason: error instanceof Error ? error.message : String(error) });
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
finish() {
|
|
1002
|
+
const replaced = this.place.usedFolder;
|
|
1003
|
+
return replaced ? { ...this.report, replaced } : this.report;
|
|
1004
|
+
}
|
|
1005
|
+
async conversation(path, bytes) {
|
|
1006
|
+
const name = basename(path);
|
|
1007
|
+
if (!SyncItems.safePath(path) || !name.endsWith(".jsonl")) {
|
|
1008
|
+
throw new Error("Not a conversation.");
|
|
1009
|
+
}
|
|
1010
|
+
const known = await this.knownSessions();
|
|
1011
|
+
const from = `${this.manifest.from.agentDir}/${path}`;
|
|
1012
|
+
if (known.has(name)) {
|
|
1013
|
+
this.report.keptConversations++;
|
|
1014
|
+
const here = known.get(name);
|
|
1015
|
+
if (here) {
|
|
1016
|
+
this.sessions.set(from, here);
|
|
1017
|
+
}
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
const text = new TextDecoder().decode(bytes);
|
|
1021
|
+
const end = text.indexOf(`
|
|
1022
|
+
`);
|
|
1023
|
+
const header = JSON.parse(end < 0 ? text : text.slice(0, end));
|
|
1024
|
+
if (header.type !== "session" || typeof header.cwd !== "string") {
|
|
1025
|
+
throw new Error("Not a conversation of pi.");
|
|
1026
|
+
}
|
|
1027
|
+
const cwd = MigrateUtil.remapPath(header.cwd, this.manifest.from, this.to);
|
|
1028
|
+
const rest = end < 0 ? "" : text.slice(end);
|
|
1029
|
+
const file = join2(this.agentDir, "sessions", MigrateUtil.sessionDir(cwd), name);
|
|
1030
|
+
await this.write(file, new TextEncoder().encode(JSON.stringify({ ...header, cwd }) + rest));
|
|
1031
|
+
known.set(name, file);
|
|
1032
|
+
this.sessions.set(from, file);
|
|
1033
|
+
this.report.conversations++;
|
|
1034
|
+
}
|
|
1035
|
+
async file(head, bytes) {
|
|
1036
|
+
const path = head.path;
|
|
1037
|
+
if (!SyncItems.safePath(path) || !/^(changes|bots)\//.test(path)) {
|
|
1038
|
+
throw new Error("Not a file Ta moves.");
|
|
1039
|
+
}
|
|
1040
|
+
const target = join2(this.agentDir, ...path.split("/"));
|
|
1041
|
+
const before = await readFile2(target).catch(() => {
|
|
1042
|
+
return;
|
|
1043
|
+
});
|
|
1044
|
+
if (before && Buffer.from(bytes).equals(before)) {
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
if (before) {
|
|
1048
|
+
if (head.c === "changes") {
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
await this.place.keepAside(path, new Uint8Array(before));
|
|
1052
|
+
}
|
|
1053
|
+
await this.write(target, bytes);
|
|
1054
|
+
this.report.files++;
|
|
1055
|
+
}
|
|
1056
|
+
async json(path, bytes) {
|
|
1057
|
+
if (path !== "auth.json" && path !== "ta.json") {
|
|
1058
|
+
throw new Error("Not a file Ta moves.");
|
|
1059
|
+
}
|
|
1060
|
+
const target = join2(this.agentDir, path);
|
|
1061
|
+
const before = await readFile2(target).catch(() => {
|
|
1062
|
+
return;
|
|
1063
|
+
});
|
|
1064
|
+
const remote = JSON.parse(path === "ta.json" ? this.remap(new TextDecoder().decode(bytes)) : new TextDecoder().decode(bytes));
|
|
1065
|
+
const local = before ? JSON.parse(before.toString("utf8")) : {};
|
|
1066
|
+
const merged = SyncMerge.value(undefined, local, remote);
|
|
1067
|
+
if (SyncMerge.same(merged, local)) {
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
if (before) {
|
|
1071
|
+
await this.place.keepAside(path, new Uint8Array(before));
|
|
1072
|
+
}
|
|
1073
|
+
await this.write(target, new TextEncoder().encode(`${JSON.stringify(merged, null, 2)}
|
|
1074
|
+
`), path === "auth.json" ? 384 : undefined);
|
|
1075
|
+
this.report.items++;
|
|
1076
|
+
}
|
|
1077
|
+
async item(head, bytes) {
|
|
1078
|
+
const id = head.id;
|
|
1079
|
+
const type = head.t;
|
|
1080
|
+
const resource = SyncItems.resourceOf(id);
|
|
1081
|
+
if (!resource || resource === "data") {
|
|
1082
|
+
throw new Error("Not an item Ta moves.");
|
|
1083
|
+
}
|
|
1084
|
+
this.locals ??= await this.place.read(new Set(["settings", "instructions", "skills", "models", "extensions"]));
|
|
1085
|
+
const local = this.locals.get(id);
|
|
1086
|
+
const hash = SyncCodec.hash(bytes);
|
|
1087
|
+
if (local?.h === hash) {
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
if (!local) {
|
|
1091
|
+
await this.place.apply(id, type, bytes, false);
|
|
1092
|
+
} else if (type === "json" || type === "set") {
|
|
1093
|
+
const remote = SyncCodec.parse(bytes);
|
|
1094
|
+
const merged = type === "set" ? SyncMerge.set(undefined, Array.isArray(local.value) ? local.value : [], Array.isArray(remote) ? remote : []) : SyncMerge.value(undefined, local.value, remote);
|
|
1095
|
+
if (SyncMerge.same(merged, local.value)) {
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
await this.place.apply(id, type, SyncCodec.json(merged), true);
|
|
1099
|
+
} else {
|
|
1100
|
+
await this.place.apply(id, type, bytes, true);
|
|
1101
|
+
}
|
|
1102
|
+
this.report.items++;
|
|
1103
|
+
if (resource === "extensions") {
|
|
1104
|
+
this.report.extensions++;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
async identity(bytes) {
|
|
1108
|
+
const identity = this.side.identity;
|
|
1109
|
+
if (!identity) {
|
|
1110
|
+
throw new Error("This Ta cannot take on an identity: update it.");
|
|
1111
|
+
}
|
|
1112
|
+
const bundle = SyncCodec.parse(bytes);
|
|
1113
|
+
const before = await identity.exportIdentity();
|
|
1114
|
+
if (bundle?.identity?.id !== before.identity.id) {
|
|
1115
|
+
await this.place.keepAside("identity.json", SyncCodec.json(before));
|
|
1116
|
+
}
|
|
1117
|
+
this.report.identity = await identity.importIdentity({ ...bundle, rooms: (Array.isArray(bundle.rooms) ? bundle.rooms : []).filter((room) => room?.kind !== SYNC_ROOM_KIND) }, { shared: true });
|
|
1118
|
+
}
|
|
1119
|
+
async data(name, bytes) {
|
|
1120
|
+
if (name === OWN) {
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
const data = JSON.parse(this.remap(new TextDecoder().decode(bytes)));
|
|
1124
|
+
if (!SyncMerge.isObject(data)) {
|
|
1125
|
+
throw new Error("Not the data of a package.");
|
|
1126
|
+
}
|
|
1127
|
+
let changed = false;
|
|
1128
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1129
|
+
const local = await this.side.getData(name, key);
|
|
1130
|
+
if (value === null || value === undefined || SyncMerge.same(local, value)) {
|
|
1131
|
+
continue;
|
|
1132
|
+
}
|
|
1133
|
+
if (local !== undefined && !this.asideData.has(name)) {
|
|
1134
|
+
this.asideData.add(name);
|
|
1135
|
+
const file = await readFile2(join2(this.agentDir, "extension-data", ...`${name}.json`.split("/"))).catch(() => {
|
|
1136
|
+
return;
|
|
1137
|
+
});
|
|
1138
|
+
if (file) {
|
|
1139
|
+
await this.place.keepAside(`extension-data/${name}.json`, new Uint8Array(file));
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
await this.side.setData(name, key, value);
|
|
1143
|
+
changed = true;
|
|
1144
|
+
}
|
|
1145
|
+
if (changed) {
|
|
1146
|
+
this.report.data++;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
remap(text) {
|
|
1150
|
+
const pairs = [...this.sessions];
|
|
1151
|
+
pairs.push([this.manifest.from.agentDir, this.to.agentDir], [`${this.manifest.from.home}/`, `${this.to.home}/`]);
|
|
1152
|
+
return MigrateUtil.remapText(text, pairs);
|
|
1153
|
+
}
|
|
1154
|
+
async knownSessions() {
|
|
1155
|
+
if (!this.known) {
|
|
1156
|
+
const known = new Map;
|
|
1157
|
+
const root = join2(this.agentDir, "sessions");
|
|
1158
|
+
for (const found of await files(root, (path) => path.endsWith(".jsonl"))) {
|
|
1159
|
+
known.set(basename(found.path), join2(root, ...found.path.split("/")));
|
|
1160
|
+
}
|
|
1161
|
+
this.known = known;
|
|
1162
|
+
}
|
|
1163
|
+
return this.known;
|
|
1164
|
+
}
|
|
1165
|
+
async write(file, bytes, mode) {
|
|
1166
|
+
await mkdir2(dirname2(file), { recursive: true });
|
|
1167
|
+
const temporary = `${file}.ta-move-${process.pid}`;
|
|
1168
|
+
await writeFile2(temporary, bytes, mode === undefined ? undefined : { mode });
|
|
1169
|
+
await rename2(temporary, file);
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// web-src/migrate-net.ts
|
|
1174
|
+
import { createECDH, hkdfSync, randomBytes as randomBytes2 } from "node:crypto";
|
|
1175
|
+
import { networkInterfaces } from "node:os";
|
|
1176
|
+
var MOVE_DISCOVERY_PORT = 47231;
|
|
1177
|
+
var MoveLinkUtil;
|
|
1178
|
+
((MoveLinkUtil) => {
|
|
1179
|
+
function link(move) {
|
|
1180
|
+
const query = new URLSearchParams({ a: move.addresses.join(","), n: move.name });
|
|
1181
|
+
return `ta-move://${move.addresses[0] ?? "127.0.0.1"}:${move.port}/?${query}${move.key ? `#k=${move.key}` : ""}`;
|
|
1182
|
+
}
|
|
1183
|
+
MoveLinkUtil.link = link;
|
|
1184
|
+
function is(text) {
|
|
1185
|
+
try {
|
|
1186
|
+
parse(text);
|
|
1187
|
+
return true;
|
|
1188
|
+
} catch {
|
|
1189
|
+
return false;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
MoveLinkUtil.is = is;
|
|
1193
|
+
function parse(text) {
|
|
1194
|
+
const url = new URL(text.trim());
|
|
1195
|
+
const port = Number(url.port);
|
|
1196
|
+
const key = new URLSearchParams(url.hash.slice(1)).get("k") ?? undefined;
|
|
1197
|
+
const addresses2 = (url.searchParams.get("a") ?? url.hostname).split(",").filter((address) => /^[\d.]+$|^[\da-f:]+$/i.test(address));
|
|
1198
|
+
if (url.protocol !== "ta-move:" || !(port > 0 && port < 65536) || !addresses2.length || key !== undefined && !/^[A-Za-z0-9_-]{43}$/.test(key)) {
|
|
1199
|
+
throw new Error("This is not a link of moving Ta.");
|
|
1200
|
+
}
|
|
1201
|
+
return { addresses: addresses2, port, key, name: url.searchParams.get("n") ?? addresses2[0] };
|
|
1202
|
+
}
|
|
1203
|
+
MoveLinkUtil.parse = parse;
|
|
1204
|
+
function addresses() {
|
|
1205
|
+
const found = Object.values(networkInterfaces()).flat().filter((each) => !!each && each.family === "IPv4" && !each.internal).map((each) => each.address);
|
|
1206
|
+
const private_ = (address) => /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/.test(address);
|
|
1207
|
+
return [...new Set(found)].sort((a, b) => Number(private_(b)) - Number(private_(a)));
|
|
1208
|
+
}
|
|
1209
|
+
MoveLinkUtil.addresses = addresses;
|
|
1210
|
+
function broadcasts() {
|
|
1211
|
+
const found = Object.values(networkInterfaces()).flat().filter((each) => !!each && each.family === "IPv4" && !each.internal).map((each) => {
|
|
1212
|
+
const address = each.address.split(".").map(Number);
|
|
1213
|
+
const mask = each.netmask.split(".").map(Number);
|
|
1214
|
+
return address.map((part, i) => (part | ~mask[i] & 255) >>> 0).join(".");
|
|
1215
|
+
});
|
|
1216
|
+
return [...new Set([...found, "255.255.255.255"])];
|
|
1217
|
+
}
|
|
1218
|
+
MoveLinkUtil.broadcasts = broadcasts;
|
|
1219
|
+
})(MoveLinkUtil ||= {});
|
|
1220
|
+
|
|
1221
|
+
class MoveChannel {
|
|
1222
|
+
socket;
|
|
1223
|
+
code;
|
|
1224
|
+
peer;
|
|
1225
|
+
reader;
|
|
1226
|
+
sealer;
|
|
1227
|
+
closed = false;
|
|
1228
|
+
constructor(socket, reader, keys, code, peer) {
|
|
1229
|
+
this.socket = socket;
|
|
1230
|
+
this.code = code;
|
|
1231
|
+
this.peer = peer;
|
|
1232
|
+
this.reader = reader;
|
|
1233
|
+
this.reader.setCipher(new FrameCipher(keys.receive));
|
|
1234
|
+
this.sealer = new FrameCipher(keys.send);
|
|
1235
|
+
socket.once("close", () => {
|
|
1236
|
+
this.closed = true;
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
async send(frame) {
|
|
1240
|
+
await write(this.socket, Frames.encode(frame, this.sealer));
|
|
1241
|
+
}
|
|
1242
|
+
async next() {
|
|
1243
|
+
const frame = await this.reader.next();
|
|
1244
|
+
if (!frame) {
|
|
1245
|
+
throw new Error("The other computer closed the connection.");
|
|
1246
|
+
}
|
|
1247
|
+
return Frames.parseRecord(frame);
|
|
1248
|
+
}
|
|
1249
|
+
close() {
|
|
1250
|
+
this.closed = true;
|
|
1251
|
+
this.socket.destroy();
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
async function write(socket, bytes) {
|
|
1255
|
+
if (socket.destroyed) {
|
|
1256
|
+
throw new Error("The connection closed.");
|
|
1257
|
+
}
|
|
1258
|
+
if (!socket.write(bytes)) {
|
|
1259
|
+
await new Promise((resolve, reject) => {
|
|
1260
|
+
const drained = () => {
|
|
1261
|
+
socket.off("close", closed);
|
|
1262
|
+
resolve();
|
|
1263
|
+
};
|
|
1264
|
+
const closed = () => {
|
|
1265
|
+
socket.off("drain", drained);
|
|
1266
|
+
reject(new Error("The connection closed."));
|
|
1267
|
+
};
|
|
1268
|
+
socket.once("drain", drained);
|
|
1269
|
+
socket.once("close", closed);
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
function derive(shared, key, receiver, sender) {
|
|
1274
|
+
const ikm = key ? Buffer.concat([shared, Buffer.from(key, "base64url")]) : shared;
|
|
1275
|
+
const salt = Buffer.concat([Buffer.from(receiver.nonce, "base64url"), Buffer.from(sender.nonce, "base64url")]);
|
|
1276
|
+
const info = Buffer.concat([Buffer.from("ta-move/1"), Buffer.from(receiver.pub, "base64url"), Buffer.from(sender.pub, "base64url")]);
|
|
1277
|
+
const okm = Buffer.from(hkdfSync("sha256", ikm, salt, info, 68));
|
|
1278
|
+
return { toSender: okm.subarray(0, 32), toReceiver: okm.subarray(32, 64), code: String(okm.readUInt32BE(64) % 1e6).padStart(6, "0") };
|
|
1279
|
+
}
|
|
1280
|
+
var MoveHandshake;
|
|
1281
|
+
((MoveHandshake) => {
|
|
1282
|
+
async function accept(socket, key, name) {
|
|
1283
|
+
const reader = new FrameReader(socket);
|
|
1284
|
+
const first = await reader.next();
|
|
1285
|
+
const hello = first ? Frames.parseRecord(first) : undefined;
|
|
1286
|
+
if (hello?.t !== "hello" || hello.v !== 1 || typeof hello.pub !== "string" || typeof hello.nonce !== "string") {
|
|
1287
|
+
throw new Error("Not a computer moving Ta.");
|
|
1288
|
+
}
|
|
1289
|
+
const ecdh = createECDH("prime256v1");
|
|
1290
|
+
const mine = { t: "hello", v: 1, pub: ecdh.generateKeys().toString("base64url"), nonce: randomBytes2(16).toString("base64url"), name };
|
|
1291
|
+
await write(socket, Frames.encode(Frames.record(mine)));
|
|
1292
|
+
const shared = ecdh.computeSecret(Buffer.from(hello.pub, "base64url"));
|
|
1293
|
+
const keys = derive(shared, hello.k === 1 ? key : undefined, hello, mine);
|
|
1294
|
+
const channel = new MoveChannel(socket, reader, { send: keys.toReceiver, receive: keys.toSender }, keys.code, "");
|
|
1295
|
+
const ask = await channel.next();
|
|
1296
|
+
if (ask.t !== "ask") {
|
|
1297
|
+
throw new Error("Not a computer moving Ta.");
|
|
1298
|
+
}
|
|
1299
|
+
channel.peer = String(ask.name ?? "").slice(0, 80) || socket.remoteAddress || "?";
|
|
1300
|
+
return channel;
|
|
1301
|
+
}
|
|
1302
|
+
MoveHandshake.accept = accept;
|
|
1303
|
+
async function connect(socket, key, name) {
|
|
1304
|
+
const reader = new FrameReader(socket);
|
|
1305
|
+
const ecdh = createECDH("prime256v1");
|
|
1306
|
+
const mine = { t: "hello", v: 1, pub: ecdh.generateKeys().toString("base64url"), nonce: randomBytes2(16).toString("base64url"), k: key ? 1 : 0 };
|
|
1307
|
+
await write(socket, Frames.encode(Frames.record(mine)));
|
|
1308
|
+
const first = await reader.next();
|
|
1309
|
+
const hello = first ? Frames.parseRecord(first) : undefined;
|
|
1310
|
+
if (hello?.t !== "hello" || hello.v !== 1 || typeof hello.pub !== "string" || typeof hello.nonce !== "string") {
|
|
1311
|
+
throw new Error("The other side is not a computer moving Ta.");
|
|
1312
|
+
}
|
|
1313
|
+
const shared = ecdh.computeSecret(Buffer.from(hello.pub, "base64url"));
|
|
1314
|
+
const keys = derive(shared, key, mine, hello);
|
|
1315
|
+
const channel = new MoveChannel(socket, reader, { send: keys.toSender, receive: keys.toReceiver }, keys.code, String(hello.name ?? "").slice(0, 80));
|
|
1316
|
+
await channel.send(Frames.record({ t: "ask", name }));
|
|
1317
|
+
return channel;
|
|
1318
|
+
}
|
|
1319
|
+
MoveHandshake.connect = connect;
|
|
1320
|
+
})(MoveHandshake ||= {});
|
|
1321
|
+
|
|
1322
|
+
// web-src/server-texts.ts
|
|
1323
|
+
var SyncServerTexts;
|
|
1324
|
+
((SyncServerTexts) => {
|
|
1325
|
+
function zh(env = process.env) {
|
|
1326
|
+
return /^zh/i.test(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || "");
|
|
1327
|
+
}
|
|
1328
|
+
SyncServerTexts.zh = zh;
|
|
1329
|
+
function title() {
|
|
1330
|
+
return zh() ? "我的设备" : "My devices";
|
|
1331
|
+
}
|
|
1332
|
+
SyncServerTexts.title = title;
|
|
1333
|
+
function notOn() {
|
|
1334
|
+
return zh() ? "同步还没有打开。" : "Sync is not on.";
|
|
1335
|
+
}
|
|
1336
|
+
SyncServerTexts.notOn = notOn;
|
|
1337
|
+
function notOwner(owner) {
|
|
1338
|
+
return zh() ? `只有开启同步的电脑(${owner})能添加设备,请在那台电脑上操作。` : `Only the computer that turned sync on (${owner}) adds devices: do it there.`;
|
|
1339
|
+
}
|
|
1340
|
+
SyncServerTexts.notOwner = notOwner;
|
|
1341
|
+
function folderMissing(path) {
|
|
1342
|
+
return zh() ? `${path} 里还没有这个同步:网盘可能还没把它同步过来,稍等再试;或者两台电脑用的不是同一个文件夹。` : `The sync is not in ${path} yet: the drive may still be bringing it, try again in a while; or the two computers use different folders.`;
|
|
1343
|
+
}
|
|
1344
|
+
SyncServerTexts.folderMissing = folderMissing;
|
|
1345
|
+
function folderKey() {
|
|
1346
|
+
return zh() ? "这个文件夹里的同步用的是另一把密钥,请重新在已同步的电脑上获取链接。" : "The sync in this folder has another key: get the link again from a computer that syncs.";
|
|
1347
|
+
}
|
|
1348
|
+
SyncServerTexts.folderKey = folderKey;
|
|
1349
|
+
function storeMissing() {
|
|
1350
|
+
return zh() ? "存储里还没有这个同步:另一台电脑用的可能不是同一个存储。" : "The sync is not in this storage: the other computer may use another one.";
|
|
1351
|
+
}
|
|
1352
|
+
SyncServerTexts.storeMissing = storeMissing;
|
|
1353
|
+
function storeUnavailable(type) {
|
|
1354
|
+
return zh() ? `这个同步放在 ${type || "存储"} 上,Ta 现在打不开它:在 设置 → 扩展 里打开“自己的存储同步”。` : `This sync is in ${type || "a storage"}, which Ta cannot open now: turn on Sync in your own storage in Settings → Extensions.`;
|
|
1355
|
+
}
|
|
1356
|
+
SyncServerTexts.storeUnavailable = storeUnavailable;
|
|
1357
|
+
function storeFailed(message) {
|
|
1358
|
+
return zh() ? `连不上存储:${message}` : `The storage cannot be reached: ${message}`;
|
|
1359
|
+
}
|
|
1360
|
+
SyncServerTexts.storeFailed = storeFailed;
|
|
1361
|
+
function folderNewer() {
|
|
1362
|
+
return zh() ? "这个文件夹里的同步来自更新版本的 Ta,请先升级。" : "The sync in this folder is of a newer Ta: update Ta first.";
|
|
1363
|
+
}
|
|
1364
|
+
SyncServerTexts.folderNewer = folderNewer;
|
|
1365
|
+
function official() {
|
|
1366
|
+
return zh() ? "官方中继" : "the official relay";
|
|
1367
|
+
}
|
|
1368
|
+
SyncServerTexts.official = official;
|
|
1369
|
+
function alreadyOn() {
|
|
1370
|
+
return zh() ? "这台电脑已经在同步,先停止同步再加入别的。" : "This computer already syncs: stop it first to join another.";
|
|
1371
|
+
}
|
|
1372
|
+
SyncServerTexts.alreadyOn = alreadyOn;
|
|
1373
|
+
})(SyncServerTexts ||= {});
|
|
1374
|
+
var MigrateServerTexts;
|
|
1375
|
+
((MigrateServerTexts) => {
|
|
1376
|
+
const zh = () => SyncServerTexts.zh();
|
|
1377
|
+
function busy() {
|
|
1378
|
+
return zh() ? "正在迁入,等这一次完成后再开始。" : "Moving in already: wait for it to end.";
|
|
1379
|
+
}
|
|
1380
|
+
MigrateServerTexts.busy = busy;
|
|
1381
|
+
function nothingChosen() {
|
|
1382
|
+
return zh() ? "至少选一类内容。" : "Choose at least one kind of content.";
|
|
1383
|
+
}
|
|
1384
|
+
MigrateServerTexts.nothingChosen = nothingChosen;
|
|
1385
|
+
function credentialsNeedPassword() {
|
|
1386
|
+
return zh() ? "备份里有登录凭据或身份时必须设密码。" : "A backup with sign-ins or the identity needs a password.";
|
|
1387
|
+
}
|
|
1388
|
+
MigrateServerTexts.credentialsNeedPassword = credentialsNeedPassword;
|
|
1389
|
+
function needsPassword() {
|
|
1390
|
+
return zh() ? "这个备份设了密码,请输入密码。" : "This backup has a password: enter it.";
|
|
1391
|
+
}
|
|
1392
|
+
MigrateServerTexts.needsPassword = needsPassword;
|
|
1393
|
+
function notBackup() {
|
|
1394
|
+
return zh() ? "这不是 Ta 的备份文件。" : "This is not a backup of Ta.";
|
|
1395
|
+
}
|
|
1396
|
+
MigrateServerTexts.notBackup = notBackup;
|
|
1397
|
+
function newer() {
|
|
1398
|
+
return zh() ? "对方的 Ta 更新,先升级这台电脑上的 Ta。" : "The other Ta is newer: update Ta here first.";
|
|
1399
|
+
}
|
|
1400
|
+
MigrateServerTexts.newer = newer;
|
|
1401
|
+
function cut() {
|
|
1402
|
+
return zh() ? "传输中断了,没有完成的部分可以重新迁移。" : "The move was cut off: move again for what did not come.";
|
|
1403
|
+
}
|
|
1404
|
+
MigrateServerTexts.cut = cut;
|
|
1405
|
+
function refused(name) {
|
|
1406
|
+
return zh() ? `${name || "对方"}没有同意。` : `${name || "The other computer"} did not allow it.`;
|
|
1407
|
+
}
|
|
1408
|
+
MigrateServerTexts.refused = refused;
|
|
1409
|
+
function noAnswer() {
|
|
1410
|
+
return zh() ? "对方没有回应。" : "The other computer did not answer.";
|
|
1411
|
+
}
|
|
1412
|
+
MigrateServerTexts.noAnswer = noAnswer;
|
|
1413
|
+
function unreachable() {
|
|
1414
|
+
return zh() ? "连不上对方:两台电脑要在同一个网络里,对方的“迁移到另一台电脑”页面要开着;防火墙可能拦住了,可以改用备份文件。" : "Cannot reach the other computer: both must be on one network, with Move to another computer open there; a firewall may stop it, a backup file works then.";
|
|
1415
|
+
}
|
|
1416
|
+
MigrateServerTexts.unreachable = unreachable;
|
|
1417
|
+
})(MigrateServerTexts ||= {});
|
|
1418
|
+
|
|
1419
|
+
// web-src/migrate-service.ts
|
|
1420
|
+
var CONNECT_MS = 4000;
|
|
1421
|
+
var ANSWER_MS = 10 * 60000;
|
|
1422
|
+
var DISCOVER_MS = 2 * 60000;
|
|
1423
|
+
var ANNOUNCE_MS = 2000;
|
|
1424
|
+
|
|
1425
|
+
class MigrateService {
|
|
1426
|
+
options;
|
|
1427
|
+
out;
|
|
1428
|
+
in;
|
|
1429
|
+
nearby = new Map;
|
|
1430
|
+
server;
|
|
1431
|
+
announcer;
|
|
1432
|
+
listener;
|
|
1433
|
+
timers = [];
|
|
1434
|
+
pending = new Map;
|
|
1435
|
+
publishing;
|
|
1436
|
+
name;
|
|
1437
|
+
constructor(options) {
|
|
1438
|
+
this.options = options;
|
|
1439
|
+
this.name = options.name ?? hostname2().replace(/\.local$/, "");
|
|
1440
|
+
}
|
|
1441
|
+
status() {
|
|
1442
|
+
const nearby = [...this.nearby.values()].map(({ at: _, ...each }) => each);
|
|
1443
|
+
return { ...this.out ? { out: this.out } : {}, ...this.in ? { in: this.in } : {}, ...this.listener ? { nearby } : {} };
|
|
1444
|
+
}
|
|
1445
|
+
async counts() {
|
|
1446
|
+
return this.source().counts();
|
|
1447
|
+
}
|
|
1448
|
+
async openLan(categories) {
|
|
1449
|
+
this.closeOut();
|
|
1450
|
+
const chosen = MigrateService.check(categories);
|
|
1451
|
+
const key = randomBytes3(32).toString("base64url");
|
|
1452
|
+
const server = createServer((socket) => void this.accepted(socket, key, chosen));
|
|
1453
|
+
await new Promise((resolve, reject) => {
|
|
1454
|
+
server.once("error", reject);
|
|
1455
|
+
server.listen(0, "0.0.0.0", () => resolve());
|
|
1456
|
+
});
|
|
1457
|
+
this.server = server;
|
|
1458
|
+
const port = server.address().port;
|
|
1459
|
+
const addresses = MoveLinkUtil.addresses();
|
|
1460
|
+
this.out = {
|
|
1461
|
+
kind: "lan",
|
|
1462
|
+
state: "open",
|
|
1463
|
+
categories: chosen,
|
|
1464
|
+
link: MoveLinkUtil.link({ addresses: addresses.length ? addresses : ["127.0.0.1"], port, key, name: this.name }),
|
|
1465
|
+
addresses,
|
|
1466
|
+
asks: []
|
|
1467
|
+
};
|
|
1468
|
+
this.announce(port, addresses);
|
|
1469
|
+
this.publish(true);
|
|
1470
|
+
return this.status();
|
|
1471
|
+
}
|
|
1472
|
+
answer(ask, allow) {
|
|
1473
|
+
const pending = this.pending.get(ask);
|
|
1474
|
+
if (pending) {
|
|
1475
|
+
this.pending.delete(ask);
|
|
1476
|
+
pending.resolve(allow);
|
|
1477
|
+
}
|
|
1478
|
+
return this.status();
|
|
1479
|
+
}
|
|
1480
|
+
closeOut() {
|
|
1481
|
+
for (const { resolve } of this.pending.values()) {
|
|
1482
|
+
resolve(false);
|
|
1483
|
+
}
|
|
1484
|
+
this.pending.clear();
|
|
1485
|
+
this.server?.close();
|
|
1486
|
+
this.server = undefined;
|
|
1487
|
+
this.announcer?.close();
|
|
1488
|
+
this.announcer = undefined;
|
|
1489
|
+
this.out = undefined;
|
|
1490
|
+
this.publish(true);
|
|
1491
|
+
return this.status();
|
|
1492
|
+
}
|
|
1493
|
+
closeIn() {
|
|
1494
|
+
if (this.in && (this.in.state === "done" || this.in.state === "failed")) {
|
|
1495
|
+
this.in = undefined;
|
|
1496
|
+
}
|
|
1497
|
+
this.publish(true);
|
|
1498
|
+
return this.status();
|
|
1499
|
+
}
|
|
1500
|
+
discover() {
|
|
1501
|
+
if (!this.listener) {
|
|
1502
|
+
const listener = createSocket({ type: "udp4", reuseAddr: true });
|
|
1503
|
+
listener.on("message", (message, from) => this.heard(message, from.address));
|
|
1504
|
+
listener.on("error", () => this.stopDiscovering());
|
|
1505
|
+
listener.bind(this.options.discoveryPort ?? MOVE_DISCOVERY_PORT);
|
|
1506
|
+
this.listener = listener;
|
|
1507
|
+
this.timers.push(setTimeout(() => this.stopDiscovering(), DISCOVER_MS));
|
|
1508
|
+
}
|
|
1509
|
+
this.publish(true);
|
|
1510
|
+
return this.status();
|
|
1511
|
+
}
|
|
1512
|
+
stopDiscovering() {
|
|
1513
|
+
this.listener?.close();
|
|
1514
|
+
this.listener = undefined;
|
|
1515
|
+
this.nearby.clear();
|
|
1516
|
+
this.publish(true);
|
|
1517
|
+
return this.status();
|
|
1518
|
+
}
|
|
1519
|
+
receive(link) {
|
|
1520
|
+
if (this.in && (this.in.state === "connecting" || this.in.state === "waiting" || this.in.state === "receiving")) {
|
|
1521
|
+
throw new Error(MigrateServerTexts.busy());
|
|
1522
|
+
}
|
|
1523
|
+
const move = MoveLinkUtil.parse(link);
|
|
1524
|
+
this.in = { kind: "lan", state: "connecting", from: move.name };
|
|
1525
|
+
this.publish(true);
|
|
1526
|
+
this.receiving(move.addresses, move.port, move.key);
|
|
1527
|
+
return this.status();
|
|
1528
|
+
}
|
|
1529
|
+
exportFile(categories, password, dir) {
|
|
1530
|
+
this.closeOut();
|
|
1531
|
+
const chosen = MigrateService.check(categories);
|
|
1532
|
+
if (chosen.some((category) => SECRET_CATEGORIES.includes(category)) && !password) {
|
|
1533
|
+
throw new Error(MigrateServerTexts.credentialsNeedPassword());
|
|
1534
|
+
}
|
|
1535
|
+
this.out = { kind: "file", state: "sending", categories: chosen, asks: [] };
|
|
1536
|
+
this.publish(true);
|
|
1537
|
+
this.exporting(chosen, password || undefined, dir);
|
|
1538
|
+
return this.status();
|
|
1539
|
+
}
|
|
1540
|
+
importFile(path, password) {
|
|
1541
|
+
if (this.in && (this.in.state === "connecting" || this.in.state === "waiting" || this.in.state === "receiving")) {
|
|
1542
|
+
throw new Error(MigrateServerTexts.busy());
|
|
1543
|
+
}
|
|
1544
|
+
this.in = { kind: "file", state: "connecting", from: path };
|
|
1545
|
+
this.publish(true);
|
|
1546
|
+
this.importing(path, password || undefined);
|
|
1547
|
+
return this.status();
|
|
1548
|
+
}
|
|
1549
|
+
async listBackups() {
|
|
1550
|
+
const found = [];
|
|
1551
|
+
for (const dir of this.backupDirs()) {
|
|
1552
|
+
for (const name of await readdir3(dir).catch(() => [])) {
|
|
1553
|
+
if (!name.endsWith(BACKUP_EXTENSION)) {
|
|
1554
|
+
continue;
|
|
1555
|
+
}
|
|
1556
|
+
const path = join3(dir, name);
|
|
1557
|
+
const info = await stat3(path).catch(() => {
|
|
1558
|
+
return;
|
|
1559
|
+
});
|
|
1560
|
+
if (info?.isFile()) {
|
|
1561
|
+
const header = await this.header(path).catch(() => {
|
|
1562
|
+
return;
|
|
1563
|
+
});
|
|
1564
|
+
if (header) {
|
|
1565
|
+
found.push({ path, name, size: info.size, modifiedAt: info.mtimeMs, encrypted: !!header.salt });
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return found.sort((a, b) => b.modifiedAt - a.modifiedAt);
|
|
1571
|
+
}
|
|
1572
|
+
dispose() {
|
|
1573
|
+
this.closeOut();
|
|
1574
|
+
this.stopDiscovering();
|
|
1575
|
+
for (const timer of this.timers) {
|
|
1576
|
+
clearTimeout(timer);
|
|
1577
|
+
}
|
|
1578
|
+
clearTimeout(this.publishing);
|
|
1579
|
+
}
|
|
1580
|
+
source() {
|
|
1581
|
+
return new MigrateSource(this.options.agentDir, this.options.place, this.name, this.options.side);
|
|
1582
|
+
}
|
|
1583
|
+
static check(categories) {
|
|
1584
|
+
const chosen = Array.isArray(categories) ? MIGRATE_CATEGORIES.filter((category) => categories.includes(category)) : [];
|
|
1585
|
+
if (!chosen.length) {
|
|
1586
|
+
throw new Error(MigrateServerTexts.nothingChosen());
|
|
1587
|
+
}
|
|
1588
|
+
return chosen;
|
|
1589
|
+
}
|
|
1590
|
+
backupDirs() {
|
|
1591
|
+
return this.options.backupDirs ?? [join3(homedir2(), "Downloads"), join3(homedir2(), "Desktop"), homedir2()];
|
|
1592
|
+
}
|
|
1593
|
+
async accepted(socket, key, categories) {
|
|
1594
|
+
const out = this.out;
|
|
1595
|
+
socket.setNoDelay(true);
|
|
1596
|
+
socket.on("error", () => {
|
|
1597
|
+
return;
|
|
1598
|
+
});
|
|
1599
|
+
let channel;
|
|
1600
|
+
try {
|
|
1601
|
+
channel = await withTimeout(MoveHandshake.accept(socket, key, this.name), CONNECT_MS * 3);
|
|
1602
|
+
} catch {
|
|
1603
|
+
socket.destroy();
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
if (!out || this.out !== out || out.state !== "open") {
|
|
1607
|
+
channel.close();
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1610
|
+
const ask = randomUUID();
|
|
1611
|
+
out.asks = [...out.asks, { ask, name: channel.peer, code: channel.code, at: Date.now() }];
|
|
1612
|
+
this.publish(true);
|
|
1613
|
+
const allow = await new Promise((resolve) => {
|
|
1614
|
+
this.pending.set(ask, { channel, resolve });
|
|
1615
|
+
const timer = setTimeout(() => resolve(false), ANSWER_MS);
|
|
1616
|
+
this.timers.push(timer);
|
|
1617
|
+
socket.once("close", () => resolve(false));
|
|
1618
|
+
});
|
|
1619
|
+
this.pending.delete(ask);
|
|
1620
|
+
out.asks = out.asks.filter((each) => each.ask !== ask);
|
|
1621
|
+
if (!allow || this.out !== out || out.state !== "open") {
|
|
1622
|
+
await channel.send(Frames.record({ t: "refused" })).catch(() => {
|
|
1623
|
+
return;
|
|
1624
|
+
});
|
|
1625
|
+
channel.close();
|
|
1626
|
+
this.publish(true);
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
for (const [other, pending] of this.pending) {
|
|
1630
|
+
this.pending.delete(other);
|
|
1631
|
+
pending.resolve(false);
|
|
1632
|
+
}
|
|
1633
|
+
this.announcer?.close();
|
|
1634
|
+
this.announcer = undefined;
|
|
1635
|
+
out.state = "sending";
|
|
1636
|
+
out.to = channel.peer;
|
|
1637
|
+
this.publish(true);
|
|
1638
|
+
try {
|
|
1639
|
+
await this.send(categories, (frame) => channel.send(frame), out);
|
|
1640
|
+
await channel.next().catch(() => {
|
|
1641
|
+
return;
|
|
1642
|
+
});
|
|
1643
|
+
out.state = "done";
|
|
1644
|
+
} catch (error) {
|
|
1645
|
+
out.state = "failed";
|
|
1646
|
+
out.error = message(error);
|
|
1647
|
+
} finally {
|
|
1648
|
+
channel.close();
|
|
1649
|
+
this.server?.close();
|
|
1650
|
+
this.server = undefined;
|
|
1651
|
+
this.publish(true);
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
async send(categories, send, out) {
|
|
1655
|
+
const source = this.source();
|
|
1656
|
+
const entries = await source.entries(categories);
|
|
1657
|
+
const manifest = await source.manifest(categories, entries);
|
|
1658
|
+
out.progress = { done: 0, total: manifest.bytes };
|
|
1659
|
+
this.publish(true);
|
|
1660
|
+
await send(Frames.record({ t: "manifest", manifest }));
|
|
1661
|
+
await source.write(entries, send, (done) => {
|
|
1662
|
+
out.progress = { done, total: manifest.bytes };
|
|
1663
|
+
this.publish();
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
async receiving(addresses, port, key) {
|
|
1667
|
+
const into = this.in;
|
|
1668
|
+
let channel;
|
|
1669
|
+
try {
|
|
1670
|
+
const socket = await connectFirst(addresses, port);
|
|
1671
|
+
channel = await withTimeout(MoveHandshake.connect(socket, key, this.name), CONNECT_MS * 3);
|
|
1672
|
+
into.state = "waiting";
|
|
1673
|
+
into.code = channel.code;
|
|
1674
|
+
into.from = channel.peer || into.from;
|
|
1675
|
+
this.publish(true);
|
|
1676
|
+
const first = await channel.next();
|
|
1677
|
+
if (first.t !== "manifest" || !first.manifest) {
|
|
1678
|
+
throw new Error(MigrateServerTexts.refused(into.from ?? ""));
|
|
1679
|
+
}
|
|
1680
|
+
const open = channel;
|
|
1681
|
+
await this.take(first.manifest, () => open.reader.next(), into);
|
|
1682
|
+
await channel.send(Frames.record({ t: "done" })).catch(() => {
|
|
1683
|
+
return;
|
|
1684
|
+
});
|
|
1685
|
+
} catch (error) {
|
|
1686
|
+
into.state = "failed";
|
|
1687
|
+
into.error = message(error);
|
|
1688
|
+
} finally {
|
|
1689
|
+
channel?.close();
|
|
1690
|
+
this.publish(true);
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
async take(manifest, next, into) {
|
|
1694
|
+
if (manifest?.v !== 1) {
|
|
1695
|
+
throw new Error(MigrateServerTexts.newer());
|
|
1696
|
+
}
|
|
1697
|
+
into.manifest = manifest;
|
|
1698
|
+
into.state = "receiving";
|
|
1699
|
+
into.progress = { done: 0, total: manifest.bytes };
|
|
1700
|
+
this.publish(true);
|
|
1701
|
+
this.options.place.begin();
|
|
1702
|
+
const target = new MigrateTarget(this.options.agentDir, this.options.place, this.options.side, manifest);
|
|
1703
|
+
let done = 0;
|
|
1704
|
+
for (;; ) {
|
|
1705
|
+
const frame = await next();
|
|
1706
|
+
if (!frame) {
|
|
1707
|
+
throw new Error(MigrateServerTexts.cut());
|
|
1708
|
+
}
|
|
1709
|
+
const head = Frames.parseRecord(frame);
|
|
1710
|
+
if (head.k === "end") {
|
|
1711
|
+
break;
|
|
1712
|
+
}
|
|
1713
|
+
MigrateTarget.check(head);
|
|
1714
|
+
const size = head.size ?? 0;
|
|
1715
|
+
const bytes = new Uint8Array(size);
|
|
1716
|
+
let at = 0;
|
|
1717
|
+
while (at < size) {
|
|
1718
|
+
const piece = await next();
|
|
1719
|
+
if (!piece) {
|
|
1720
|
+
throw new Error(MigrateServerTexts.cut());
|
|
1721
|
+
}
|
|
1722
|
+
const part = Frames.piece(piece);
|
|
1723
|
+
if (at + part.length > size) {
|
|
1724
|
+
throw new Error(MigrateServerTexts.cut());
|
|
1725
|
+
}
|
|
1726
|
+
bytes.set(part, at);
|
|
1727
|
+
at += part.length;
|
|
1728
|
+
done += part.length;
|
|
1729
|
+
into.progress = { done, total: manifest.bytes };
|
|
1730
|
+
this.publish();
|
|
1731
|
+
}
|
|
1732
|
+
await target.apply(head, bytes);
|
|
1733
|
+
}
|
|
1734
|
+
into.report = target.finish();
|
|
1735
|
+
into.state = "done";
|
|
1736
|
+
}
|
|
1737
|
+
async exporting(categories, password, dir) {
|
|
1738
|
+
const out = this.out;
|
|
1739
|
+
const folder = dir ?? this.backupDirs()[0];
|
|
1740
|
+
const date = new Date;
|
|
1741
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
1742
|
+
const stamp = `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(date.getHours())}${pad(date.getMinutes())}`;
|
|
1743
|
+
const path = join3(folder, `Ta-${this.name.replace(/[^\w.-]/g, "_")}-${stamp}${BACKUP_EXTENSION}`);
|
|
1744
|
+
let stream;
|
|
1745
|
+
try {
|
|
1746
|
+
await mkdir3(folder, { recursive: true });
|
|
1747
|
+
stream = createWriteStream(`${path}.part`, { mode: 384 });
|
|
1748
|
+
const { bytes, cipher } = BackupFile.header(this.name, password);
|
|
1749
|
+
await writeStream(stream, bytes);
|
|
1750
|
+
const file = stream;
|
|
1751
|
+
await this.send(categories, (frame) => writeStream(file, Frames.encode(frame, cipher)), out);
|
|
1752
|
+
await new Promise((resolve, reject) => file.end((error) => error ? reject(error) : resolve()));
|
|
1753
|
+
await rename3(`${path}.part`, path);
|
|
1754
|
+
out.state = "done";
|
|
1755
|
+
out.path = path;
|
|
1756
|
+
} catch (error) {
|
|
1757
|
+
stream?.destroy();
|
|
1758
|
+
await rm2(`${path}.part`, { force: true });
|
|
1759
|
+
out.state = "failed";
|
|
1760
|
+
out.error = message(error);
|
|
1761
|
+
} finally {
|
|
1762
|
+
this.publish(true);
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
async importing(path, password) {
|
|
1766
|
+
const into = this.in;
|
|
1767
|
+
const reader = new FrameReader(createReadStream(path, { highWaterMark: 1024 * 1024 }));
|
|
1768
|
+
try {
|
|
1769
|
+
const header = await BackupFile.readHeader(reader);
|
|
1770
|
+
into.from = header.name;
|
|
1771
|
+
if (header.salt) {
|
|
1772
|
+
if (!password) {
|
|
1773
|
+
throw new Error(MigrateServerTexts.needsPassword());
|
|
1774
|
+
}
|
|
1775
|
+
reader.setCipher(new FrameCipher(BackupFile.key(password, header.salt)));
|
|
1776
|
+
}
|
|
1777
|
+
const first = await reader.next();
|
|
1778
|
+
const record = first ? Frames.parseRecord(first) : undefined;
|
|
1779
|
+
if (record?.t !== "manifest" || !record.manifest) {
|
|
1780
|
+
throw new Error(MigrateServerTexts.notBackup());
|
|
1781
|
+
}
|
|
1782
|
+
await this.take(record.manifest, () => reader.next(), into);
|
|
1783
|
+
} catch (error) {
|
|
1784
|
+
into.state = "failed";
|
|
1785
|
+
into.error = message(error);
|
|
1786
|
+
} finally {
|
|
1787
|
+
await reader.close().catch(() => {
|
|
1788
|
+
return;
|
|
1789
|
+
});
|
|
1790
|
+
this.publish(true);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
async header(path) {
|
|
1794
|
+
const reader = new FrameReader(createReadStream(path, { start: 0, end: 64 * 1024 }));
|
|
1795
|
+
try {
|
|
1796
|
+
return await BackupFile.readHeader(reader);
|
|
1797
|
+
} finally {
|
|
1798
|
+
await reader.close().catch(() => {
|
|
1799
|
+
return;
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
announce(port, addresses) {
|
|
1804
|
+
if (!addresses.length) {
|
|
1805
|
+
return;
|
|
1806
|
+
}
|
|
1807
|
+
const socket = createSocket({ type: "udp4", reuseAddr: true });
|
|
1808
|
+
socket.on("error", () => {
|
|
1809
|
+
return;
|
|
1810
|
+
});
|
|
1811
|
+
socket.bind(() => {
|
|
1812
|
+
socket.setBroadcast(true);
|
|
1813
|
+
const message = Buffer.from(JSON.stringify({ t: "ta-move", v: 1, name: this.name, port, addresses }));
|
|
1814
|
+
const send = () => {
|
|
1815
|
+
if (this.announcer !== socket) {
|
|
1816
|
+
return;
|
|
1817
|
+
}
|
|
1818
|
+
for (const target of MoveLinkUtil.broadcasts()) {
|
|
1819
|
+
socket.send(message, this.options.discoveryPort ?? MOVE_DISCOVERY_PORT, target, () => {
|
|
1820
|
+
return;
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
this.timers.push(setTimeout(send, ANNOUNCE_MS));
|
|
1824
|
+
};
|
|
1825
|
+
send();
|
|
1826
|
+
});
|
|
1827
|
+
this.announcer = socket;
|
|
1828
|
+
}
|
|
1829
|
+
heard(message, address) {
|
|
1830
|
+
try {
|
|
1831
|
+
const data = JSON.parse(message.toString("utf8"));
|
|
1832
|
+
if (data.t !== "ta-move" || data.v !== 1 || typeof data.port !== "number") {
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
const name = String(data.name ?? address).slice(0, 80);
|
|
1836
|
+
const addresses = [address, ...Array.isArray(data.addresses) ? data.addresses.map(String) : []];
|
|
1837
|
+
const link = MoveLinkUtil.link({ addresses: [...new Set(addresses)], port: data.port, name });
|
|
1838
|
+
const known = `${address}:${data.port}`;
|
|
1839
|
+
const fresh = !this.nearby.has(known);
|
|
1840
|
+
this.nearby.set(known, { link, name, address, at: Date.now() });
|
|
1841
|
+
for (const [id, each] of this.nearby) {
|
|
1842
|
+
if (Date.now() - each.at > ANNOUNCE_MS * 5) {
|
|
1843
|
+
this.nearby.delete(id);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
if (fresh) {
|
|
1847
|
+
this.publish(true);
|
|
1848
|
+
}
|
|
1849
|
+
} catch {}
|
|
1850
|
+
}
|
|
1851
|
+
publish(now = false) {
|
|
1852
|
+
if (now) {
|
|
1853
|
+
clearTimeout(this.publishing);
|
|
1854
|
+
this.publishing = undefined;
|
|
1855
|
+
this.options.publish(this.status());
|
|
1856
|
+
return;
|
|
1857
|
+
}
|
|
1858
|
+
this.publishing ??= setTimeout(() => {
|
|
1859
|
+
this.publishing = undefined;
|
|
1860
|
+
this.options.publish(this.status());
|
|
1861
|
+
}, 400);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
function message(error) {
|
|
1865
|
+
return error instanceof Error ? error.message : String(error);
|
|
1866
|
+
}
|
|
1867
|
+
function withTimeout(promise, ms) {
|
|
1868
|
+
return new Promise((resolve, reject) => {
|
|
1869
|
+
const timer = setTimeout(() => reject(new Error(MigrateServerTexts.noAnswer())), ms);
|
|
1870
|
+
promise.then((value) => {
|
|
1871
|
+
clearTimeout(timer);
|
|
1872
|
+
resolve(value);
|
|
1873
|
+
}, (error) => {
|
|
1874
|
+
clearTimeout(timer);
|
|
1875
|
+
reject(error);
|
|
1876
|
+
});
|
|
1877
|
+
});
|
|
1878
|
+
}
|
|
1879
|
+
async function connectFirst(addresses, port) {
|
|
1880
|
+
for (const address of addresses) {
|
|
1881
|
+
const socket = await new Promise((resolve) => {
|
|
1882
|
+
const socket2 = createConnection({ host: address, port });
|
|
1883
|
+
const timer = setTimeout(() => {
|
|
1884
|
+
socket2.destroy();
|
|
1885
|
+
resolve(undefined);
|
|
1886
|
+
}, CONNECT_MS);
|
|
1887
|
+
socket2.once("connect", () => {
|
|
1888
|
+
clearTimeout(timer);
|
|
1889
|
+
resolve(socket2);
|
|
1890
|
+
});
|
|
1891
|
+
socket2.once("error", () => {
|
|
1892
|
+
clearTimeout(timer);
|
|
1893
|
+
resolve(undefined);
|
|
1894
|
+
});
|
|
1895
|
+
});
|
|
1896
|
+
if (socket) {
|
|
1897
|
+
socket.setNoDelay(true);
|
|
1898
|
+
socket.on("error", () => {
|
|
1899
|
+
return;
|
|
1900
|
+
});
|
|
1901
|
+
return socket;
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
throw new Error(MigrateServerTexts.unreachable());
|
|
1905
|
+
}
|
|
1906
|
+
function writeStream(stream, bytes) {
|
|
1907
|
+
return new Promise((resolve, reject) => {
|
|
1908
|
+
stream.write(bytes, (error) => error ? reject(error) : resolve());
|
|
1909
|
+
});
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
// web-src/sync-protocol.ts
|
|
1913
|
+
var SYNC_KIND = "ta-sync";
|
|
1914
|
+
var SYNC_PACKAGE = "@celljs/ta-sync";
|
|
1915
|
+
var SYNC_VIEW = `${SYNC_PACKAGE}/devices`;
|
|
1916
|
+
var SYNC_RESOURCES = ["settings", "instructions", "skills", "models", "extensions", "data", "identity"];
|
|
1917
|
+
var DEFAULT_RESOURCES = {
|
|
1918
|
+
settings: true,
|
|
1919
|
+
instructions: true,
|
|
1920
|
+
skills: true,
|
|
1921
|
+
models: false,
|
|
1922
|
+
extensions: true,
|
|
1923
|
+
data: true,
|
|
1924
|
+
identity: false
|
|
1925
|
+
};
|
|
1926
|
+
var SyncKeys;
|
|
1927
|
+
((SyncKeys) => {
|
|
1928
|
+
SyncKeys.RESOURCES = "resources";
|
|
1929
|
+
SyncKeys.REPORT = "report";
|
|
1930
|
+
SyncKeys.STATUS = "status";
|
|
1931
|
+
SyncKeys.JOINING = "joining";
|
|
1932
|
+
})(SyncKeys ||= {});
|
|
1933
|
+
var SyncStateKeys;
|
|
1934
|
+
((SyncStateKeys) => {
|
|
1935
|
+
SyncStateKeys.ITEM = "item:";
|
|
1936
|
+
SyncStateKeys.BLOB = "blob:";
|
|
1937
|
+
SyncStateKeys.DEVICE = "device:";
|
|
1938
|
+
function item(id) {
|
|
1939
|
+
return `${SyncStateKeys.ITEM}${id}`;
|
|
1940
|
+
}
|
|
1941
|
+
SyncStateKeys.item = item;
|
|
1942
|
+
function blob(hash, index) {
|
|
1943
|
+
return `${SyncStateKeys.BLOB}${hash}:${index}`;
|
|
1944
|
+
}
|
|
1945
|
+
SyncStateKeys.blob = blob;
|
|
1946
|
+
function device(member) {
|
|
1947
|
+
return `${SyncStateKeys.DEVICE}${member}`;
|
|
1948
|
+
}
|
|
1949
|
+
SyncStateKeys.device = device;
|
|
1950
|
+
})(SyncStateKeys ||= {});
|
|
1951
|
+
var SyncDeclarations;
|
|
1952
|
+
((SyncDeclarations) => {
|
|
1953
|
+
SyncDeclarations.SHELL = {
|
|
1954
|
+
"@celljs/ta-web": ["settings", "assistant.species", "assistant.gait"]
|
|
1955
|
+
};
|
|
1956
|
+
function keys(manifest) {
|
|
1957
|
+
const pi = manifest && typeof manifest === "object" ? manifest.pi : undefined;
|
|
1958
|
+
const sync = pi?.sync;
|
|
1959
|
+
return Array.isArray(sync) ? [...new Set(sync.filter((key) => typeof key === "string" && key.length > 0 && key.length <= 200))] : [];
|
|
1960
|
+
}
|
|
1961
|
+
SyncDeclarations.keys = keys;
|
|
1962
|
+
})(SyncDeclarations ||= {});
|
|
1963
|
+
|
|
1964
|
+
// web-src/sync-service.ts
|
|
1965
|
+
import { mkdir as mkdir5, readFile as readFile4, rename as rename5, rm as rm4, writeFile as writeFile4 } from "node:fs/promises";
|
|
1966
|
+
import { hostname as hostname3, platform as platform2 } from "node:os";
|
|
1967
|
+
import { join as join6 } from "node:path";
|
|
1968
|
+
|
|
1969
|
+
// ../ta-relay/esm/node/relays-file.js
|
|
1970
|
+
import { accessSync, chmodSync, constants, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
1971
|
+
import { homedir as homedir3 } from "node:os";
|
|
1972
|
+
import { dirname as dirname3, isAbsolute, join as join4, resolve } from "node:path";
|
|
1973
|
+
|
|
1974
|
+
// ../ta-relay/esm/common/relay-protocol.js
|
|
1975
|
+
var RELAY_DEFAULT_URL = "https://relay.cellbang.com";
|
|
1976
|
+
var RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
1977
|
+
var RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
1978
|
+
var RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
1979
|
+
var RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
1980
|
+
var RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
1981
|
+
var RELAY_BLOB_MAX_DAYS = 30;
|
|
1982
|
+
var RelayCloseCode;
|
|
1983
|
+
(function(RelayCloseCode2) {
|
|
1984
|
+
RelayCloseCode2[RelayCloseCode2["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
|
|
1985
|
+
RelayCloseCode2[RelayCloseCode2["REFUSED"] = 4401] = "REFUSED";
|
|
1986
|
+
RelayCloseCode2[RelayCloseCode2["REPLACED"] = 4409] = "REPLACED";
|
|
1987
|
+
RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
|
|
1988
|
+
RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
|
|
1989
|
+
RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
1990
|
+
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
1991
|
+
var RELAY_HUB_MAX_MEMBERS = 101;
|
|
1992
|
+
var RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
1993
|
+
var RELAY_HUB_MAX_KEYS = 4096;
|
|
1994
|
+
var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
1995
|
+
var RELAY_HUB_IDLE_DAYS = 90;
|
|
1996
|
+
|
|
1997
|
+
// ../ta-relay/esm/common/relay-info.js
|
|
1998
|
+
var RELAY_INFO_PATH = "/v1/info";
|
|
1999
|
+
var RELAY_LEGACY_INFO = {
|
|
2000
|
+
v: 1,
|
|
2001
|
+
capabilities: ["tunnel", "hub", "store", "pages"],
|
|
2002
|
+
limits: {
|
|
2003
|
+
hubMembers: RELAY_HUB_MAX_MEMBERS,
|
|
2004
|
+
hubValueBytes: RELAY_HUB_MAX_VALUE,
|
|
2005
|
+
hubKeys: RELAY_HUB_MAX_KEYS,
|
|
2006
|
+
hubStateBytes: RELAY_HUB_MAX_STATE,
|
|
2007
|
+
hubIdleDays: RELAY_HUB_IDLE_DAYS,
|
|
2008
|
+
storeValueBytes: RELAY_CONTENT_MAX_BYTES,
|
|
2009
|
+
storeDays: RELAY_BLOB_MAX_DAYS
|
|
2010
|
+
},
|
|
2011
|
+
persistent: true
|
|
2012
|
+
};
|
|
2013
|
+
var RelayInfoUtil;
|
|
2014
|
+
(function(RelayInfoUtil2) {
|
|
2015
|
+
function of(persistent) {
|
|
2016
|
+
return { ...RELAY_LEGACY_INFO, persistent };
|
|
2017
|
+
}
|
|
2018
|
+
RelayInfoUtil2.of = of;
|
|
2019
|
+
function response(info) {
|
|
2020
|
+
return new Response(JSON.stringify(info), {
|
|
2021
|
+
headers: { "content-type": "application/json", "access-control-allow-origin": "*", "cache-control": "no-store" }
|
|
2022
|
+
});
|
|
2023
|
+
}
|
|
2024
|
+
RelayInfoUtil2.response = response;
|
|
2025
|
+
function normalize(url) {
|
|
2026
|
+
const text = String(url !== null && url !== undefined ? url : "").trim();
|
|
2027
|
+
const parsed = new URL(text);
|
|
2028
|
+
if (!/^https?:$/.test(parsed.protocol) || parsed.search || parsed.hash || parsed.username || parsed.password) {
|
|
2029
|
+
throw new Error(`${text} is not the address of a relay.`);
|
|
2030
|
+
}
|
|
2031
|
+
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, "");
|
|
2032
|
+
}
|
|
2033
|
+
RelayInfoUtil2.normalize = normalize;
|
|
2034
|
+
async function probe(url, timeoutMs = 6000) {
|
|
2035
|
+
const started = Date.now();
|
|
2036
|
+
const controller = new AbortController;
|
|
2037
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
2038
|
+
try {
|
|
2039
|
+
const response2 = await fetch(`${normalize(url)}${RELAY_INFO_PATH}`, { signal: controller.signal, redirect: "manual" });
|
|
2040
|
+
const ms = Date.now() - started;
|
|
2041
|
+
if (response2.ok) {
|
|
2042
|
+
const info = parse(await response2.json().catch(() => {
|
|
2043
|
+
return;
|
|
2044
|
+
}));
|
|
2045
|
+
return info ? { online: true, info, ms } : { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms };
|
|
2046
|
+
}
|
|
2047
|
+
if (response2.status === 404 || response2.status === 426) {
|
|
2048
|
+
const root = await fetch(`${normalize(url)}/`, { signal: controller.signal }).then((answer) => answer.text(), () => "");
|
|
2049
|
+
return root.startsWith("Ta relay") ? { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms } : { online: false, ms, error: `It answers, but is not a relay of Ta (${response2.status}).` };
|
|
2050
|
+
}
|
|
2051
|
+
return { online: false, ms, error: `It answered ${response2.status}.` };
|
|
2052
|
+
} catch (error) {
|
|
2053
|
+
const aborted = (error === null || error === undefined ? undefined : error.name) === "AbortError";
|
|
2054
|
+
return { online: false, error: aborted ? "No answer in time." : (error === null || error === undefined ? undefined : error.message) || String(error) };
|
|
2055
|
+
} finally {
|
|
2056
|
+
clearTimeout(timer);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
RelayInfoUtil2.probe = probe;
|
|
2060
|
+
function parse(value) {
|
|
2061
|
+
const info = value;
|
|
2062
|
+
if ((info === null || info === undefined ? undefined : info.v) !== 1 || !Array.isArray(info.capabilities) || !info.limits || typeof info.limits !== "object") {
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
const known = ["tunnel", "hub", "store", "pages"];
|
|
2066
|
+
const limits = { ...RELAY_LEGACY_INFO.limits };
|
|
2067
|
+
for (const key of Object.keys(limits)) {
|
|
2068
|
+
const given = info.limits[key];
|
|
2069
|
+
if (typeof given === "number" && given > 0) {
|
|
2070
|
+
limits[key] = given;
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
return {
|
|
2074
|
+
v: 1,
|
|
2075
|
+
capabilities: info.capabilities.filter((capability) => known.includes(capability)),
|
|
2076
|
+
limits,
|
|
2077
|
+
persistent: info.persistent !== false
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
RelayInfoUtil2.parse = parse;
|
|
2081
|
+
})(RelayInfoUtil || (RelayInfoUtil = {}));
|
|
2082
|
+
|
|
2083
|
+
// ../ta-relay/esm/node/relays-file.js
|
|
2084
|
+
var RELAY_FEATURES = ["devices", "share", "rooms", "sync"];
|
|
2085
|
+
var OFFICIAL_RELAY = "cellbang";
|
|
2086
|
+
var RELAY_TYPE_FEATURES = { "ta-relay": "all", folder: ["sync"] };
|
|
2087
|
+
var RelayTypes;
|
|
2088
|
+
(function(RelayTypes2) {
|
|
2089
|
+
const types = new Map;
|
|
2090
|
+
RelayTypes2.NAME = /^[a-z][a-z0-9-]{1,31}$/;
|
|
2091
|
+
function register(type, entry) {
|
|
2092
|
+
if (!RelayTypes2.NAME.test(type) || type === "ta-relay" || type === "folder" || type === "module") {
|
|
2093
|
+
throw new RelaysConfigError(`${type} cannot be a type of relays.json.`);
|
|
2094
|
+
}
|
|
2095
|
+
types.set(type, entry);
|
|
2096
|
+
return () => {
|
|
2097
|
+
if (types.get(type) === entry) {
|
|
2098
|
+
types.delete(type);
|
|
2099
|
+
}
|
|
2100
|
+
};
|
|
2101
|
+
}
|
|
2102
|
+
RelayTypes2.register = register;
|
|
2103
|
+
function get(type) {
|
|
2104
|
+
return types.get(type);
|
|
2105
|
+
}
|
|
2106
|
+
RelayTypes2.get = get;
|
|
2107
|
+
})(RelayTypes || (RelayTypes = {}));
|
|
2108
|
+
var RelayServiceUtil;
|
|
2109
|
+
(function(RelayServiceUtil2) {
|
|
2110
|
+
function expand(path) {
|
|
2111
|
+
const text = String(path !== null && path !== undefined ? path : "").trim();
|
|
2112
|
+
if (text === "~" || text.startsWith("~/")) {
|
|
2113
|
+
return join4(homedir3(), text.slice(1));
|
|
2114
|
+
}
|
|
2115
|
+
return isAbsolute(text) ? resolve(text) : resolve(homedir3(), text);
|
|
2116
|
+
}
|
|
2117
|
+
RelayServiceUtil2.expand = expand;
|
|
2118
|
+
function portable(path) {
|
|
2119
|
+
const home = homedir3();
|
|
2120
|
+
return path === home ? "~" : path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
2121
|
+
}
|
|
2122
|
+
RelayServiceUtil2.portable = portable;
|
|
2123
|
+
function serves(type, feature) {
|
|
2124
|
+
var _a;
|
|
2125
|
+
var _b, _c;
|
|
2126
|
+
const features = (_c = (_b = RELAY_TYPE_FEATURES[type]) !== null && _b !== undefined ? _b : (_a = RelayTypes.get(type)) === null || _a === undefined ? undefined : _a.features) !== null && _c !== undefined ? _c : [];
|
|
2127
|
+
return features === "all" || features.includes(feature);
|
|
2128
|
+
}
|
|
2129
|
+
RelayServiceUtil2.serves = serves;
|
|
2130
|
+
function isRelay(entry) {
|
|
2131
|
+
return entry.type === "ta-relay" && typeof entry.url === "string";
|
|
2132
|
+
}
|
|
2133
|
+
RelayServiceUtil2.isRelay = isRelay;
|
|
2134
|
+
function isFolder(entry) {
|
|
2135
|
+
return entry.type === "folder" && typeof entry.path === "string";
|
|
2136
|
+
}
|
|
2137
|
+
RelayServiceUtil2.isFolder = isFolder;
|
|
2138
|
+
function only(type) {
|
|
2139
|
+
var _a;
|
|
2140
|
+
var _b, _c;
|
|
2141
|
+
const features = (_c = (_b = RELAY_TYPE_FEATURES[type]) !== null && _b !== undefined ? _b : (_a = RelayTypes.get(type)) === null || _a === undefined ? undefined : _a.features) !== null && _c !== undefined ? _c : [];
|
|
2142
|
+
return features === "all" ? undefined : [...features];
|
|
2143
|
+
}
|
|
2144
|
+
RelayServiceUtil2.only = only;
|
|
2145
|
+
function drives() {
|
|
2146
|
+
const home = homedir3();
|
|
2147
|
+
const found = [];
|
|
2148
|
+
const add = (name, dir) => {
|
|
2149
|
+
try {
|
|
2150
|
+
if (statSync(dir).isDirectory()) {
|
|
2151
|
+
found.push({ name, path: portable(join4(dir, "Ta")) });
|
|
2152
|
+
}
|
|
2153
|
+
} catch (_a) {}
|
|
2154
|
+
};
|
|
2155
|
+
add("iCloud Drive", join4(home, "Library", "Mobile Documents", "com~apple~CloudDocs"));
|
|
2156
|
+
try {
|
|
2157
|
+
for (const entry of readdirSync(join4(home, "Library", "CloudStorage"))) {
|
|
2158
|
+
add(entry.replace(/-.*$/, ""), join4(home, "Library", "CloudStorage", entry));
|
|
2159
|
+
}
|
|
2160
|
+
} catch (_a) {}
|
|
2161
|
+
for (const name of ["Dropbox", "OneDrive", "Nutstore Files", "坚果云"]) {
|
|
2162
|
+
add(name, join4(home, name));
|
|
2163
|
+
}
|
|
2164
|
+
return found.filter((drive, index) => found.findIndex((other) => other.path === drive.path) === index);
|
|
2165
|
+
}
|
|
2166
|
+
RelayServiceUtil2.drives = drives;
|
|
2167
|
+
function checkFolder(path) {
|
|
2168
|
+
const dir = expand(path);
|
|
2169
|
+
try {
|
|
2170
|
+
if (!statSync(dir).isDirectory()) {
|
|
2171
|
+
return `${dir} is not a folder.`;
|
|
2172
|
+
}
|
|
2173
|
+
accessSync(dir, constants.W_OK);
|
|
2174
|
+
return;
|
|
2175
|
+
} catch (error) {
|
|
2176
|
+
const code = error.code;
|
|
2177
|
+
return code === "ENOENT" ? `There is no folder ${dir}.` : `Ta cannot write in ${dir}.`;
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
RelayServiceUtil2.checkFolder = checkFolder;
|
|
2181
|
+
})(RelayServiceUtil || (RelayServiceUtil = {}));
|
|
2182
|
+
|
|
2183
|
+
class RelaysConfigError extends Error {
|
|
2184
|
+
}
|
|
2185
|
+
var ID = /^[a-z0-9][a-z0-9_-]{0,39}$/;
|
|
2186
|
+
|
|
2187
|
+
class RelaysFile {
|
|
2188
|
+
constructor(agentDir) {
|
|
2189
|
+
this.agentDir = agentDir;
|
|
2190
|
+
}
|
|
2191
|
+
get file() {
|
|
2192
|
+
return join4(this.agentDir, RelaysFile.FILE);
|
|
2193
|
+
}
|
|
2194
|
+
read() {
|
|
2195
|
+
let text;
|
|
2196
|
+
try {
|
|
2197
|
+
text = readFileSync(this.file, "utf8");
|
|
2198
|
+
} catch (_a) {
|
|
2199
|
+
return {};
|
|
2200
|
+
}
|
|
2201
|
+
let data;
|
|
2202
|
+
try {
|
|
2203
|
+
data = JSON.parse(text);
|
|
2204
|
+
} catch (error) {
|
|
2205
|
+
throw new RelaysConfigError(`${this.file} is not valid JSON: ${error.message}`);
|
|
2206
|
+
}
|
|
2207
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) {
|
|
2208
|
+
throw new RelaysConfigError(`${this.file} does not hold a JSON object.`);
|
|
2209
|
+
}
|
|
2210
|
+
return data;
|
|
2211
|
+
}
|
|
2212
|
+
safeRead() {
|
|
2213
|
+
try {
|
|
2214
|
+
return { config: this.read() };
|
|
2215
|
+
} catch (error) {
|
|
2216
|
+
return { config: {}, error: error.message };
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
services(config = this.safeRead().config) {
|
|
2220
|
+
const entries = [];
|
|
2221
|
+
const own = config.relays && typeof config.relays === "object" ? config.relays : {};
|
|
2222
|
+
if (!own[OFFICIAL_RELAY]) {
|
|
2223
|
+
entries.push({ id: OFFICIAL_RELAY, type: "ta-relay", url: RELAY_DEFAULT_URL, name: "relay.cellbang.com", builtin: true });
|
|
2224
|
+
}
|
|
2225
|
+
for (const [id, service] of Object.entries(own)) {
|
|
2226
|
+
if (!ID.test(id) || !service || typeof service !== "object") {
|
|
2227
|
+
continue;
|
|
2228
|
+
}
|
|
2229
|
+
const name = service.name ? { name: String(service.name).slice(0, 80) } : {};
|
|
2230
|
+
if (service.type === "folder") {
|
|
2231
|
+
if (typeof service.path === "string" && service.path.trim()) {
|
|
2232
|
+
entries.push({ id, type: "folder", path: service.path.trim(), ...name, builtin: false });
|
|
2233
|
+
}
|
|
2234
|
+
continue;
|
|
2235
|
+
}
|
|
2236
|
+
if (typeof service.type === "string" && service.type !== "ta-relay" && RelayTypes.NAME.test(service.type)) {
|
|
2237
|
+
const { type, name: _name, ...options } = service;
|
|
2238
|
+
entries.push({ id, type, options, ...name, builtin: false });
|
|
2239
|
+
continue;
|
|
2240
|
+
}
|
|
2241
|
+
const url = RelaysFile.url(service);
|
|
2242
|
+
if (url) {
|
|
2243
|
+
entries.push({ id, type: "ta-relay", url, ...name, builtin: id === OFFICIAL_RELAY });
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
return entries;
|
|
2247
|
+
}
|
|
2248
|
+
serviceFor(feature, fallback) {
|
|
2249
|
+
var _a;
|
|
2250
|
+
const { config } = this.safeRead();
|
|
2251
|
+
const id = this.idFor(feature, config);
|
|
2252
|
+
const service = id ? this.services(config).find((entry) => entry.id === id) : undefined;
|
|
2253
|
+
if (service) {
|
|
2254
|
+
return service;
|
|
2255
|
+
}
|
|
2256
|
+
const url = fallback !== null && fallback !== undefined ? fallback : RELAY_DEFAULT_URL;
|
|
2257
|
+
return (_a = this.services(config).find((entry) => RelayServiceUtil.isRelay(entry) && entry.url === url)) !== null && _a !== undefined ? _a : { id: "", type: "ta-relay", url, builtin: false };
|
|
2258
|
+
}
|
|
2259
|
+
idFor(feature, config = this.safeRead().config) {
|
|
2260
|
+
var _a, _b, _c;
|
|
2261
|
+
const use = config.use && typeof config.use === "object" ? config.use : {};
|
|
2262
|
+
const services = this.services(config);
|
|
2263
|
+
const pick = (id) => typeof id === "string" && services.some((service) => service.id === id && RelayServiceUtil.serves(service.type, feature)) ? id : undefined;
|
|
2264
|
+
const own = RELAY_FEATURES.includes(feature) ? undefined : pick(use.rooms);
|
|
2265
|
+
return (_c = (_b = (_a = pick(use[feature])) !== null && _a !== undefined ? _a : own) !== null && _b !== undefined ? _b : feature === "sync" ? pick(use.rooms) : undefined) !== null && _c !== undefined ? _c : pick(config.default);
|
|
2266
|
+
}
|
|
2267
|
+
urlFor(feature, fallback) {
|
|
2268
|
+
const service = this.serviceFor(feature, fallback);
|
|
2269
|
+
return RelayServiceUtil.isRelay(service) ? service.url : fallback !== null && fallback !== undefined ? fallback : RELAY_DEFAULT_URL;
|
|
2270
|
+
}
|
|
2271
|
+
add(url, name) {
|
|
2272
|
+
var _a;
|
|
2273
|
+
const address = RelayInfoUtil.normalize(url);
|
|
2274
|
+
const config = this.read();
|
|
2275
|
+
const relays = { ...(_a = config.relays) !== null && _a !== undefined ? _a : {} };
|
|
2276
|
+
const before = this.services(config).find((service) => RelayServiceUtil.isRelay(service) && service.url === address);
|
|
2277
|
+
if (before) {
|
|
2278
|
+
return before.id;
|
|
2279
|
+
}
|
|
2280
|
+
const stem = new URL(address).hostname.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 32) || "relay";
|
|
2281
|
+
let id = stem;
|
|
2282
|
+
for (let n = 2;relays[id] || id === OFFICIAL_RELAY; n++) {
|
|
2283
|
+
id = `${stem}-${n}`;
|
|
2284
|
+
}
|
|
2285
|
+
relays[id] = { type: "ta-relay", url: address, ...(name === null || name === undefined ? undefined : name.trim()) ? { name: name.trim().slice(0, 80) } : {} };
|
|
2286
|
+
this.write({ ...config, relays });
|
|
2287
|
+
return id;
|
|
2288
|
+
}
|
|
2289
|
+
addFolder(path, name) {
|
|
2290
|
+
var _a, _b;
|
|
2291
|
+
const wanted = RelayServiceUtil.expand(path);
|
|
2292
|
+
if (RelayServiceUtil.drives().some((drive) => RelayServiceUtil.expand(drive.path) === wanted)) {
|
|
2293
|
+
mkdirSync(wanted, { recursive: true });
|
|
2294
|
+
}
|
|
2295
|
+
const problem = RelayServiceUtil.checkFolder(path);
|
|
2296
|
+
if (problem) {
|
|
2297
|
+
throw new RelaysConfigError(problem);
|
|
2298
|
+
}
|
|
2299
|
+
const kept = RelayServiceUtil.portable(RelayServiceUtil.expand(path));
|
|
2300
|
+
const config = this.read();
|
|
2301
|
+
const before = this.services(config).find((service) => RelayServiceUtil.isFolder(service) && RelayServiceUtil.expand(service.path) === RelayServiceUtil.expand(kept));
|
|
2302
|
+
if (before) {
|
|
2303
|
+
return before.id;
|
|
2304
|
+
}
|
|
2305
|
+
const relays = { ...(_a = config.relays) !== null && _a !== undefined ? _a : {} };
|
|
2306
|
+
const stem = ((_b = kept.split("/").filter(Boolean).pop()) !== null && _b !== undefined ? _b : "folder").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 28) || "folder";
|
|
2307
|
+
let id = `folder-${stem}`;
|
|
2308
|
+
for (let n = 2;relays[id]; n++) {
|
|
2309
|
+
id = `folder-${stem}-${n}`;
|
|
2310
|
+
}
|
|
2311
|
+
relays[id] = { type: "folder", path: kept, ...(name === null || name === undefined ? undefined : name.trim()) ? { name: name.trim().slice(0, 80) } : {} };
|
|
2312
|
+
this.write({ ...config, relays });
|
|
2313
|
+
return id;
|
|
2314
|
+
}
|
|
2315
|
+
addStore(type, options, name) {
|
|
2316
|
+
var _a;
|
|
2317
|
+
const entry = RelayTypes.get(type);
|
|
2318
|
+
if (!entry) {
|
|
2319
|
+
throw new RelaysConfigError(`There is no type ${type}: turn on the package that adds it.`);
|
|
2320
|
+
}
|
|
2321
|
+
const clean = Object.fromEntries(Object.entries(options !== null && options !== undefined ? options : {}).filter(([key2, value]) => key2 !== "type" && key2 !== "name" && value !== undefined && value !== ""));
|
|
2322
|
+
const key = entry.key(clean);
|
|
2323
|
+
const config = this.read();
|
|
2324
|
+
const relays = { ...(_a = config.relays) !== null && _a !== undefined ? _a : {} };
|
|
2325
|
+
const before = this.services(config).find((service) => service.type === type && ("options" in service) && entry.key(service.options) === key);
|
|
2326
|
+
let id = before === null || before === undefined ? undefined : before.id;
|
|
2327
|
+
if (!id) {
|
|
2328
|
+
const stem = key.replace(/^[a-z]+:\/\//i, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 28) || type;
|
|
2329
|
+
id = `${type}-${stem}`.slice(0, 40).replace(/-+$/, "");
|
|
2330
|
+
for (let n = 2;relays[id]; n++) {
|
|
2331
|
+
id = `${type}-${stem}`.slice(0, 36).replace(/-+$/, "") + `-${n}`;
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
const named = (name === null || name === undefined ? undefined : name.trim()) ? name.trim().slice(0, 80) : before === null || before === undefined ? undefined : before.name;
|
|
2335
|
+
relays[id] = { type, ...clean, ...named ? { name: named } : {} };
|
|
2336
|
+
this.write({ ...config, relays });
|
|
2337
|
+
return id;
|
|
2338
|
+
}
|
|
2339
|
+
remove(id) {
|
|
2340
|
+
var _a;
|
|
2341
|
+
var _b, _c;
|
|
2342
|
+
const config = this.read();
|
|
2343
|
+
if (id === OFFICIAL_RELAY && !((_a = config.relays) === null || _a === undefined ? undefined : _a[id])) {
|
|
2344
|
+
throw new RelaysConfigError("The official relay comes with Ta.");
|
|
2345
|
+
}
|
|
2346
|
+
const using = Object.entries((_b = config.use) !== null && _b !== undefined ? _b : {}).filter(([, used]) => used === id).map(([feature]) => feature);
|
|
2347
|
+
if (using.length || config.default === id) {
|
|
2348
|
+
throw new RelaysConfigError(`${id} is used by ${[...using, ...config.default === id ? ["default"] : []].join(", ")}: choose another relay for them first.`);
|
|
2349
|
+
}
|
|
2350
|
+
const relays = { ...(_c = config.relays) !== null && _c !== undefined ? _c : {} };
|
|
2351
|
+
delete relays[id];
|
|
2352
|
+
this.write({ ...config, relays });
|
|
2353
|
+
}
|
|
2354
|
+
use(feature, id) {
|
|
2355
|
+
var _a;
|
|
2356
|
+
const config = this.read();
|
|
2357
|
+
if (!/^[a-z][a-z0-9-]{0,39}$/.test(feature)) {
|
|
2358
|
+
throw new RelaysConfigError(`${feature} is not a feature of relays.json.`);
|
|
2359
|
+
}
|
|
2360
|
+
const service = id === undefined ? undefined : this.services(config).find((each) => each.id === id);
|
|
2361
|
+
if (id !== undefined && !service) {
|
|
2362
|
+
throw new RelaysConfigError(`There is no relay ${id} in relays.json.`);
|
|
2363
|
+
}
|
|
2364
|
+
if (service && !RelayServiceUtil.serves(service.type, feature)) {
|
|
2365
|
+
throw new RelaysConfigError(`${id} only keeps what sync stores, and ${feature} needs a relay of Ta.`);
|
|
2366
|
+
}
|
|
2367
|
+
const use = { ...(_a = config.use) !== null && _a !== undefined ? _a : {} };
|
|
2368
|
+
if (id === undefined) {
|
|
2369
|
+
delete use[feature];
|
|
2370
|
+
} else {
|
|
2371
|
+
use[feature] = id;
|
|
2372
|
+
}
|
|
2373
|
+
this.write({ ...config, use });
|
|
2374
|
+
}
|
|
2375
|
+
write(config) {
|
|
2376
|
+
mkdirSync(dirname3(this.file), { recursive: true });
|
|
2377
|
+
const temporary = `${this.file}.${process.pid}.tmp`;
|
|
2378
|
+
writeFileSync(temporary, `${JSON.stringify(config, null, 2)}
|
|
2379
|
+
`, { mode: 384 });
|
|
2380
|
+
chmodSync(temporary, 384);
|
|
2381
|
+
renameSync(temporary, this.file);
|
|
2382
|
+
}
|
|
2383
|
+
static url(service) {
|
|
2384
|
+
if (!service || typeof service !== "object" || service.type !== undefined && service.type !== "ta-relay" || !("url" in service)) {
|
|
2385
|
+
return;
|
|
2386
|
+
}
|
|
2387
|
+
try {
|
|
2388
|
+
return RelayInfoUtil.normalize(String(service.url));
|
|
2389
|
+
} catch (_a) {
|
|
2390
|
+
return;
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
RelaysFile.FILE = "relays.json";
|
|
2395
|
+
|
|
2396
|
+
// web-src/sync-engine.ts
|
|
2397
|
+
var ATTEMPTS = 3;
|
|
2398
|
+
var TOMBSTONE_MS = 90 * 24 * 60 * 60 * 1000;
|
|
2399
|
+
|
|
2400
|
+
class SyncEngine {
|
|
2401
|
+
room;
|
|
2402
|
+
place;
|
|
2403
|
+
device;
|
|
2404
|
+
now;
|
|
2405
|
+
constructor(room, place, device, now = Date.now) {
|
|
2406
|
+
this.room = room;
|
|
2407
|
+
this.place = place;
|
|
2408
|
+
this.device = device;
|
|
2409
|
+
this.now = now;
|
|
2410
|
+
}
|
|
2411
|
+
async round(base, resources) {
|
|
2412
|
+
const report = { at: this.now(), sent: 0, received: 0, skipped: [] };
|
|
2413
|
+
for (let attempt = 0;attempt < ATTEMPTS; attempt++) {
|
|
2414
|
+
const raced = await this.once(base, resources, report);
|
|
2415
|
+
if (!raced) {
|
|
2416
|
+
break;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
await this.dropOldMarks();
|
|
2420
|
+
return report;
|
|
2421
|
+
}
|
|
2422
|
+
async once(base, resources, report) {
|
|
2423
|
+
const remote = await this.remote();
|
|
2424
|
+
const local = await this.place.read(resources);
|
|
2425
|
+
const ids = new Set([...local.keys(), ...remote.keys(), ...Object.keys(base.items)]);
|
|
2426
|
+
let raced = false;
|
|
2427
|
+
for (const id of [...ids].sort()) {
|
|
2428
|
+
const resource = SyncItems.resourceOf(id);
|
|
2429
|
+
if (!resource || !resources.has(resource)) {
|
|
2430
|
+
continue;
|
|
2431
|
+
}
|
|
2432
|
+
try {
|
|
2433
|
+
raced = await this.item(id, local.get(id), remote.get(id), base, report) || raced;
|
|
2434
|
+
} catch (error) {
|
|
2435
|
+
report.skipped.push({ id, reason: error instanceof Error ? error.message : String(error) });
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
return raced;
|
|
2439
|
+
}
|
|
2440
|
+
async item(id, local, remote, base, report) {
|
|
2441
|
+
const type = local?.type ?? remote?.entry.t ?? "bytes";
|
|
2442
|
+
const mergeable = type === "json" || type === "set";
|
|
2443
|
+
const had = base.items[id];
|
|
2444
|
+
const action = SyncMerge.decide(local, remote?.entry, had?.h, mergeable && (local?.type ?? type) === (remote?.entry.t ?? type));
|
|
2445
|
+
switch (action) {
|
|
2446
|
+
case "none":
|
|
2447
|
+
base.items[id] = { h: local?.h ?? "", ...local?.value === undefined ? {} : { v: local.value } };
|
|
2448
|
+
return false;
|
|
2449
|
+
case "forget":
|
|
2450
|
+
delete base.items[id];
|
|
2451
|
+
return false;
|
|
2452
|
+
case "push":
|
|
2453
|
+
case "keepLocal": {
|
|
2454
|
+
if (action === "keepLocal" && remote) {
|
|
2455
|
+
const theirs = await this.content(remote.entry);
|
|
2456
|
+
if (theirs) {
|
|
2457
|
+
await this.place.keep(id, remote.entry.t, theirs, remote.entry.by);
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
const ok = await this.send(id, local, remote);
|
|
2461
|
+
if (ok) {
|
|
2462
|
+
base.items[id] = { h: local?.h ?? "", ...local?.value === undefined ? {} : { v: local.value } };
|
|
2463
|
+
report.sent++;
|
|
2464
|
+
}
|
|
2465
|
+
return !ok;
|
|
2466
|
+
}
|
|
2467
|
+
case "pull":
|
|
2468
|
+
case "keepRemote": {
|
|
2469
|
+
const entry = remote?.entry;
|
|
2470
|
+
const content = entry && !entry.deleted ? await this.content(entry) : undefined;
|
|
2471
|
+
if (entry && !entry.deleted && !content) {
|
|
2472
|
+
return false;
|
|
2473
|
+
}
|
|
2474
|
+
const aside = !!local && (action === "keepRemote" || had === undefined || local.h !== had.h);
|
|
2475
|
+
await this.place.apply(id, entry?.t ?? type, content, aside);
|
|
2476
|
+
base.items[id] = { h: content ? entry?.h ?? "" : "", ...content && mergeable ? { v: SyncCodec.parse(content) } : {} };
|
|
2477
|
+
if (local || content) {
|
|
2478
|
+
report.received++;
|
|
2479
|
+
}
|
|
2480
|
+
return false;
|
|
2481
|
+
}
|
|
2482
|
+
case "merge":
|
|
2483
|
+
return this.merge(id, local, remote, had, base, report);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
async merge(id, local, remote, had, base, report) {
|
|
2487
|
+
const content = await this.content(remote.entry);
|
|
2488
|
+
if (!content) {
|
|
2489
|
+
return false;
|
|
2490
|
+
}
|
|
2491
|
+
const theirs = SyncCodec.parse(content);
|
|
2492
|
+
const merged = local.type === "set" ? SyncMerge.set(Array.isArray(had?.v) ? had.v : undefined, Array.isArray(local.value) ? local.value : [], Array.isArray(theirs) ? theirs : []) : SyncMerge.value(had?.v, local.value, theirs);
|
|
2493
|
+
const item = SyncItems.item(id, local.type, SyncCodec.json(merged), this.now(), merged);
|
|
2494
|
+
if (item.h !== remote.entry.h && !await this.send(id, item, remote)) {
|
|
2495
|
+
return true;
|
|
2496
|
+
}
|
|
2497
|
+
if (item.h !== local.h) {
|
|
2498
|
+
await this.place.apply(id, local.type, item.bytes, false);
|
|
2499
|
+
report.received++;
|
|
2500
|
+
}
|
|
2501
|
+
if (item.h !== remote.entry.h) {
|
|
2502
|
+
report.sent++;
|
|
2503
|
+
}
|
|
2504
|
+
base.items[id] = { h: item.h, v: merged };
|
|
2505
|
+
return false;
|
|
2506
|
+
}
|
|
2507
|
+
async send(id, local, remote) {
|
|
2508
|
+
const entry = local ? { t: local.type, h: local.h, at: Math.round(local.at || this.now()), by: this.device, n: 0 } : { t: remote?.entry.t ?? "bytes", h: "", at: this.now(), by: this.device, n: 0, deleted: true };
|
|
2509
|
+
if (local) {
|
|
2510
|
+
const pieces = SyncCodec.encode(local.bytes, this.room.piece);
|
|
2511
|
+
entry.n = pieces.length;
|
|
2512
|
+
const there = await this.room.getState(`${SyncStateKeys.BLOB}${local.h}:`);
|
|
2513
|
+
for (const [index, piece] of pieces.entries()) {
|
|
2514
|
+
const key = SyncStateKeys.blob(local.h, index);
|
|
2515
|
+
if (there[key]?.value !== piece) {
|
|
2516
|
+
await this.room.setState(key, piece);
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
const result = await this.room.setState(SyncStateKeys.item(id), entry, remote?.version ?? 0);
|
|
2521
|
+
if (result.ok && remote && !remote.entry.deleted && remote.entry.h !== entry.h) {
|
|
2522
|
+
await this.dropBlob(remote.entry);
|
|
2523
|
+
}
|
|
2524
|
+
return result.ok;
|
|
2525
|
+
}
|
|
2526
|
+
async dropBlob(entry) {
|
|
2527
|
+
const remote = await this.remote();
|
|
2528
|
+
if ([...remote.values()].some(({ entry: other }) => !other.deleted && other.h === entry.h)) {
|
|
2529
|
+
return;
|
|
2530
|
+
}
|
|
2531
|
+
for (let index = 0;index < entry.n; index++) {
|
|
2532
|
+
await this.room.setState(SyncStateKeys.blob(entry.h, index), null);
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
async content(entry) {
|
|
2536
|
+
const state = await this.room.getState(`${SyncStateKeys.BLOB}${entry.h}:`);
|
|
2537
|
+
const pieces = [];
|
|
2538
|
+
for (let index = 0;index < entry.n; index++) {
|
|
2539
|
+
const piece = state[SyncStateKeys.blob(entry.h, index)]?.value;
|
|
2540
|
+
if (typeof piece !== "string") {
|
|
2541
|
+
return;
|
|
2542
|
+
}
|
|
2543
|
+
pieces.push(piece);
|
|
2544
|
+
}
|
|
2545
|
+
const bytes = SyncCodec.decode(pieces);
|
|
2546
|
+
return SyncCodec.hash(bytes) === entry.h ? bytes : undefined;
|
|
2547
|
+
}
|
|
2548
|
+
async remote() {
|
|
2549
|
+
const items = new Map;
|
|
2550
|
+
for (const [key, { value, version }] of Object.entries(await this.room.getState(SyncStateKeys.ITEM))) {
|
|
2551
|
+
if (isEntry(value)) {
|
|
2552
|
+
items.set(key.slice(SyncStateKeys.ITEM.length), { entry: value, version });
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
return items;
|
|
2556
|
+
}
|
|
2557
|
+
async dropOldMarks() {
|
|
2558
|
+
for (const [id, { entry, version }] of await this.remote()) {
|
|
2559
|
+
if (entry.deleted && this.now() - entry.at > TOMBSTONE_MS) {
|
|
2560
|
+
await this.room.setState(SyncStateKeys.item(id), null, version);
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
function isEntry(value) {
|
|
2566
|
+
const entry = value;
|
|
2567
|
+
return SyncMerge.isObject(value) && ["json", "set", "bytes", "tree"].includes(entry.t) && typeof entry.h === "string" && typeof entry.at === "number" && typeof entry.n === "number" && typeof entry.by === "string";
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2570
|
+
// web-src/sync-folder.ts
|
|
2571
|
+
import { createCipheriv as createCipheriv2, createDecipheriv as createDecipheriv2, createHmac, randomBytes as randomBytes4 } from "node:crypto";
|
|
2572
|
+
import { watch } from "node:fs";
|
|
2573
|
+
import { mkdir as mkdir4, readdir as readdir4, readFile as readFile3, rename as rename4, rm as rm3, stat as stat4, writeFile as writeFile3 } from "node:fs/promises";
|
|
2574
|
+
import { join as join5, sep as sep3 } from "node:path";
|
|
2575
|
+
var CHECK = "ta-sync folder v1";
|
|
2576
|
+
var DIR = "ta-sync";
|
|
2577
|
+
var FOLDER_PIECE = 4 * 1024 * 1024;
|
|
2578
|
+
var FolderSyncUtil;
|
|
2579
|
+
((FolderSyncUtil) => {
|
|
2580
|
+
function randomId() {
|
|
2581
|
+
return randomBytes4(16).toString("base64url");
|
|
2582
|
+
}
|
|
2583
|
+
FolderSyncUtil.randomId = randomId;
|
|
2584
|
+
function randomKey() {
|
|
2585
|
+
return randomBytes4(32).toString("base64url");
|
|
2586
|
+
}
|
|
2587
|
+
FolderSyncUtil.randomKey = randomKey;
|
|
2588
|
+
function link(record, name) {
|
|
2589
|
+
const fragment = new URLSearchParams({ v: "1", s: record.id, k: record.key, p: record.path, n: name });
|
|
2590
|
+
return `ta-sync://folder#${fragment}`;
|
|
2591
|
+
}
|
|
2592
|
+
FolderSyncUtil.link = link;
|
|
2593
|
+
function storeLink(record, config, name) {
|
|
2594
|
+
const options = Buffer.from(JSON.stringify(config.options)).toString("base64url");
|
|
2595
|
+
const fragment = new URLSearchParams({
|
|
2596
|
+
v: "1",
|
|
2597
|
+
s: record.id,
|
|
2598
|
+
k: record.key,
|
|
2599
|
+
t: config.type,
|
|
2600
|
+
o: options,
|
|
2601
|
+
...config.name ? { a: config.name } : {},
|
|
2602
|
+
n: name
|
|
2603
|
+
});
|
|
2604
|
+
return `ta-sync://store#${fragment}`;
|
|
2605
|
+
}
|
|
2606
|
+
FolderSyncUtil.storeLink = storeLink;
|
|
2607
|
+
function isStore(text) {
|
|
2608
|
+
return String(text ?? "").trim().startsWith("ta-sync://store#");
|
|
2609
|
+
}
|
|
2610
|
+
FolderSyncUtil.isStore = isStore;
|
|
2611
|
+
function parseStore(text) {
|
|
2612
|
+
const value = String(text ?? "").trim();
|
|
2613
|
+
const fragment = new URLSearchParams(value.slice(value.indexOf("#") + 1));
|
|
2614
|
+
const [id, key, type] = ["s", "k", "t"].map((name) => fragment.get(name) ?? "");
|
|
2615
|
+
let options;
|
|
2616
|
+
try {
|
|
2617
|
+
options = JSON.parse(Buffer.from(fragment.get("o") ?? "", "base64url").toString("utf8"));
|
|
2618
|
+
} catch {
|
|
2619
|
+
options = undefined;
|
|
2620
|
+
}
|
|
2621
|
+
if (!isStore(value) || fragment.get("v") !== "1" || !/^[A-Za-z0-9_-]{16,64}$/.test(id) || !/^[A-Za-z0-9_-]{43}$/.test(key) || !/^[a-z][a-z0-9-]{1,31}$/.test(type) || !options || typeof options !== "object" || Array.isArray(options)) {
|
|
2622
|
+
throw new Error("This is not a link of sync in a storage.");
|
|
2623
|
+
}
|
|
2624
|
+
return { id, key, type, options, service: fragment.get("a") ?? "", name: fragment.get("n") ?? "" };
|
|
2625
|
+
}
|
|
2626
|
+
FolderSyncUtil.parseStore = parseStore;
|
|
2627
|
+
function is(text) {
|
|
2628
|
+
return String(text ?? "").trim().startsWith("ta-sync://folder#");
|
|
2629
|
+
}
|
|
2630
|
+
FolderSyncUtil.is = is;
|
|
2631
|
+
function parse(text) {
|
|
2632
|
+
const value = String(text ?? "").trim();
|
|
2633
|
+
const fragment = new URLSearchParams(value.slice(value.indexOf("#") + 1));
|
|
2634
|
+
const [id, key, path] = ["s", "k", "p"].map((name) => fragment.get(name) ?? "");
|
|
2635
|
+
if (!is(value) || fragment.get("v") !== "1" || !/^[A-Za-z0-9_-]{16,64}$/.test(id) || !/^[A-Za-z0-9_-]{43}$/.test(key) || !path) {
|
|
2636
|
+
throw new Error("This is not a link of sync in a folder.");
|
|
2637
|
+
}
|
|
2638
|
+
return { id, key, path, name: fragment.get("n") ?? "" };
|
|
2639
|
+
}
|
|
2640
|
+
FolderSyncUtil.parse = parse;
|
|
2641
|
+
})(FolderSyncUtil ||= {});
|
|
2642
|
+
|
|
2643
|
+
class LocalFolderStore {
|
|
2644
|
+
root;
|
|
2645
|
+
constructor(root) {
|
|
2646
|
+
this.root = root;
|
|
2647
|
+
}
|
|
2648
|
+
async list(dir) {
|
|
2649
|
+
const found = [];
|
|
2650
|
+
const at = join5(this.root, ...dir.split("/").filter(Boolean));
|
|
2651
|
+
for (const entry of await readdir4(at, { withFileTypes: true }).catch(() => [])) {
|
|
2652
|
+
if (entry.name.startsWith(".")) {
|
|
2653
|
+
continue;
|
|
2654
|
+
}
|
|
2655
|
+
if (entry.isDirectory()) {
|
|
2656
|
+
found.push({ name: entry.name, tag: "", dir: true });
|
|
2657
|
+
} else if (entry.isFile()) {
|
|
2658
|
+
const info = await stat4(join5(at, entry.name)).catch(() => {
|
|
2659
|
+
return;
|
|
2660
|
+
});
|
|
2661
|
+
if (info) {
|
|
2662
|
+
found.push({ name: entry.name, tag: `${info.mtimeMs}:${info.size}` });
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
return found.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
2667
|
+
}
|
|
2668
|
+
async read(path) {
|
|
2669
|
+
try {
|
|
2670
|
+
return new Uint8Array(await readFile3(this.path(path)));
|
|
2671
|
+
} catch {
|
|
2672
|
+
return;
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2675
|
+
async write(path, data) {
|
|
2676
|
+
const file = this.path(path);
|
|
2677
|
+
const dir = join5(file, "..");
|
|
2678
|
+
await mkdir4(dir, { recursive: true });
|
|
2679
|
+
const temporary = join5(dir, `.${file.split(sep3).pop()}.${process.pid}.tmp`);
|
|
2680
|
+
await writeFile3(temporary, data);
|
|
2681
|
+
await rename4(temporary, file);
|
|
2682
|
+
}
|
|
2683
|
+
async remove(path) {
|
|
2684
|
+
await rm3(this.path(path), { force: true });
|
|
2685
|
+
}
|
|
2686
|
+
watch(listener) {
|
|
2687
|
+
let watcher;
|
|
2688
|
+
try {
|
|
2689
|
+
watcher = watch(this.root, { recursive: true }, (_event, file) => listener(String(file ?? "").split(sep3).join("/")));
|
|
2690
|
+
watcher.on("error", () => {
|
|
2691
|
+
return;
|
|
2692
|
+
});
|
|
2693
|
+
} catch {}
|
|
2694
|
+
return () => watcher?.close();
|
|
2695
|
+
}
|
|
2696
|
+
path(path) {
|
|
2697
|
+
return join5(this.root, ...path.split("/").filter(Boolean));
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
class StoreSpace {
|
|
2702
|
+
store;
|
|
2703
|
+
record;
|
|
2704
|
+
piece = FOLDER_PIECE;
|
|
2705
|
+
key;
|
|
2706
|
+
root;
|
|
2707
|
+
opened = new Map;
|
|
2708
|
+
constructor(store, record) {
|
|
2709
|
+
this.store = store;
|
|
2710
|
+
this.record = record;
|
|
2711
|
+
this.key = Buffer.from(record.key, "base64url");
|
|
2712
|
+
this.root = `${DIR}/${record.id}`;
|
|
2713
|
+
}
|
|
2714
|
+
async create() {
|
|
2715
|
+
await this.store.write(`${this.root}/space.json`, Buffer.from(JSON.stringify({ v: 1, check: this.seal("space", CHECK).toString("base64") })));
|
|
2716
|
+
}
|
|
2717
|
+
async check() {
|
|
2718
|
+
const bytes = await this.store.read(`${this.root}/space.json`);
|
|
2719
|
+
if (!bytes) {
|
|
2720
|
+
return "missing";
|
|
2721
|
+
}
|
|
2722
|
+
try {
|
|
2723
|
+
const space = JSON.parse(Buffer.from(bytes).toString("utf8"));
|
|
2724
|
+
if (space.v !== 1) {
|
|
2725
|
+
return "newer";
|
|
2726
|
+
}
|
|
2727
|
+
return this.open(Buffer.from(String(space.check), "base64"), "space") === CHECK ? undefined : "key";
|
|
2728
|
+
} catch {
|
|
2729
|
+
return "key";
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
async getState(prefix) {
|
|
2733
|
+
if (prefix.startsWith(SyncStateKeys.BLOB)) {
|
|
2734
|
+
return this.blobs(prefix);
|
|
2735
|
+
}
|
|
2736
|
+
if (prefix === SyncStateKeys.DEVICE) {
|
|
2737
|
+
const found2 = {};
|
|
2738
|
+
for (const device of await this.devices()) {
|
|
2739
|
+
found2[SyncStateKeys.device(device.device)] = { value: device, version: 1 };
|
|
2740
|
+
}
|
|
2741
|
+
return found2;
|
|
2742
|
+
}
|
|
2743
|
+
const found = {};
|
|
2744
|
+
for (const [key, entry] of await this.items()) {
|
|
2745
|
+
if (key.startsWith(prefix) && entry.value !== null) {
|
|
2746
|
+
found[key] = { value: entry.value, version: entry.v };
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
return found;
|
|
2750
|
+
}
|
|
2751
|
+
async setState(key, value, version) {
|
|
2752
|
+
if (key.startsWith(SyncStateKeys.BLOB)) {
|
|
2753
|
+
const path = `${this.root}/blobs/${this.blobName(key)}`;
|
|
2754
|
+
if (value === null) {
|
|
2755
|
+
await this.store.remove(path);
|
|
2756
|
+
} else {
|
|
2757
|
+
await this.store.write(path, this.seal(key, value));
|
|
2758
|
+
}
|
|
2759
|
+
return { ok: true, version: 1 };
|
|
2760
|
+
}
|
|
2761
|
+
if (key.startsWith(SyncStateKeys.DEVICE)) {
|
|
2762
|
+
await this.store.write(`${this.root}/devices/${this.record.device}/device`, this.seal("device", value));
|
|
2763
|
+
return { ok: true, version: 1 };
|
|
2764
|
+
}
|
|
2765
|
+
const current = (await this.items()).get(key);
|
|
2766
|
+
const seen = current && current.value !== null ? current.v : 0;
|
|
2767
|
+
if (version !== undefined && version !== seen) {
|
|
2768
|
+
return { ok: false, version: seen };
|
|
2769
|
+
}
|
|
2770
|
+
const v = Math.max(current?.v ?? 0, version ?? 0) + 1;
|
|
2771
|
+
const sealed = { key, value: value ?? null, v };
|
|
2772
|
+
await this.store.write(`${this.root}/devices/${this.record.device}/items/${this.name(key)}`, this.seal(this.name(key), sealed));
|
|
2773
|
+
return { ok: true, version: v };
|
|
2774
|
+
}
|
|
2775
|
+
async devices() {
|
|
2776
|
+
const devices = [];
|
|
2777
|
+
for (const device of await this.dirs(`${this.root}/devices`)) {
|
|
2778
|
+
const files2 = await this.store.list(`${this.root}/devices/${device}`);
|
|
2779
|
+
const file = files2.find((each) => each.name === "device" && !each.dir);
|
|
2780
|
+
const value = file && await this.cached(`${this.root}/devices/${device}/device`, file.tag, "device");
|
|
2781
|
+
if (value) {
|
|
2782
|
+
devices.push({ ...value, device });
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
return devices;
|
|
2786
|
+
}
|
|
2787
|
+
async signature() {
|
|
2788
|
+
const parts = [];
|
|
2789
|
+
for (const device of await this.dirs(`${this.root}/devices`)) {
|
|
2790
|
+
if (device === this.record.device) {
|
|
2791
|
+
continue;
|
|
2792
|
+
}
|
|
2793
|
+
for (const file of await this.store.list(`${this.root}/devices/${device}/items`)) {
|
|
2794
|
+
parts.push(`${device}/${file.name}:${file.tag}`);
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
return parts.join(`
|
|
2798
|
+
`);
|
|
2799
|
+
}
|
|
2800
|
+
watch(listener) {
|
|
2801
|
+
if (!this.store.watch) {
|
|
2802
|
+
return () => {
|
|
2803
|
+
return;
|
|
2804
|
+
};
|
|
2805
|
+
}
|
|
2806
|
+
return this.store.watch((path) => {
|
|
2807
|
+
if (path !== undefined && (!path.startsWith(`${this.root}/`) || path.includes(`devices/${this.record.device}/`) || path.split("/").pop()?.startsWith("."))) {
|
|
2808
|
+
return;
|
|
2809
|
+
}
|
|
2810
|
+
listener();
|
|
2811
|
+
});
|
|
2812
|
+
}
|
|
2813
|
+
async items() {
|
|
2814
|
+
const latest = new Map;
|
|
2815
|
+
for (const device of await this.dirs(`${this.root}/devices`)) {
|
|
2816
|
+
const dir = `${this.root}/devices/${device}/items`;
|
|
2817
|
+
for (const file of await this.store.list(dir)) {
|
|
2818
|
+
if (file.dir) {
|
|
2819
|
+
continue;
|
|
2820
|
+
}
|
|
2821
|
+
const sealed = await this.cached(`${dir}/${file.name}`, file.tag, file.name);
|
|
2822
|
+
if (!sealed || typeof sealed.key !== "string" || typeof sealed.v !== "number" || this.name(sealed.key) !== file.name) {
|
|
2823
|
+
continue;
|
|
2824
|
+
}
|
|
2825
|
+
const before = latest.get(sealed.key);
|
|
2826
|
+
const later = !before || sealed.v > before.v || sealed.v === before.v && device > before.device;
|
|
2827
|
+
if (later) {
|
|
2828
|
+
latest.set(sealed.key, { ...sealed, device });
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
return latest;
|
|
2833
|
+
}
|
|
2834
|
+
async blobs(prefix) {
|
|
2835
|
+
const hash = prefix.slice(SyncStateKeys.BLOB.length).replace(/:$/, "");
|
|
2836
|
+
const stem = `${this.name(`blob:${hash}`)}.`;
|
|
2837
|
+
const found = {};
|
|
2838
|
+
for (const file of await this.store.list(`${this.root}/blobs`)) {
|
|
2839
|
+
if (file.dir || !file.name.startsWith(stem)) {
|
|
2840
|
+
continue;
|
|
2841
|
+
}
|
|
2842
|
+
const index = Number(file.name.slice(stem.length));
|
|
2843
|
+
const key = SyncStateKeys.blob(hash, index);
|
|
2844
|
+
const value = Number.isInteger(index) ? await this.read(`${this.root}/blobs/${file.name}`, key) : undefined;
|
|
2845
|
+
if (value !== undefined) {
|
|
2846
|
+
found[key] = { value, version: 1 };
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
return found;
|
|
2850
|
+
}
|
|
2851
|
+
async dirs(dir) {
|
|
2852
|
+
return (await this.store.list(dir)).filter((entry) => entry.dir).map((entry) => entry.name);
|
|
2853
|
+
}
|
|
2854
|
+
blobName(key) {
|
|
2855
|
+
const [, hash, index] = key.split(":");
|
|
2856
|
+
return `${this.name(`blob:${hash}`)}.${Number(index)}`;
|
|
2857
|
+
}
|
|
2858
|
+
name(key) {
|
|
2859
|
+
return createHmac("sha256", this.key).update(`name
|
|
2860
|
+
${key}`).digest("base64url").slice(0, 32);
|
|
2861
|
+
}
|
|
2862
|
+
seal(aad, value) {
|
|
2863
|
+
const iv = randomBytes4(12);
|
|
2864
|
+
const cipher = createCipheriv2("aes-256-gcm", this.key, iv);
|
|
2865
|
+
cipher.setAAD(Buffer.from(aad));
|
|
2866
|
+
const body = Buffer.concat([cipher.update(JSON.stringify(value)), cipher.final()]);
|
|
2867
|
+
return Buffer.concat([iv, cipher.getAuthTag(), body]);
|
|
2868
|
+
}
|
|
2869
|
+
open(sealed, aad) {
|
|
2870
|
+
const decipher = createDecipheriv2("aes-256-gcm", this.key, sealed.subarray(0, 12));
|
|
2871
|
+
decipher.setAAD(Buffer.from(aad));
|
|
2872
|
+
decipher.setAuthTag(sealed.subarray(12, 28));
|
|
2873
|
+
return JSON.parse(Buffer.concat([decipher.update(sealed.subarray(28)), decipher.final()]).toString("utf8"));
|
|
2874
|
+
}
|
|
2875
|
+
async cached(path, tag, aad) {
|
|
2876
|
+
const known = this.opened.get(path);
|
|
2877
|
+
if (tag && known?.tag === tag) {
|
|
2878
|
+
return known.value;
|
|
2879
|
+
}
|
|
2880
|
+
const value = await this.read(path, aad);
|
|
2881
|
+
if (value !== undefined && tag) {
|
|
2882
|
+
this.opened.set(path, { tag, value });
|
|
2883
|
+
}
|
|
2884
|
+
return value;
|
|
2885
|
+
}
|
|
2886
|
+
async read(path, aad) {
|
|
2887
|
+
try {
|
|
2888
|
+
const bytes = await this.store.read(path);
|
|
2889
|
+
return bytes ? this.open(Buffer.from(bytes), aad) : undefined;
|
|
2890
|
+
} catch {
|
|
2891
|
+
return;
|
|
2892
|
+
}
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
class FolderSpace extends StoreSpace {
|
|
2897
|
+
folder;
|
|
2898
|
+
constructor(folder, record) {
|
|
2899
|
+
super(new LocalFolderStore(folder), record);
|
|
2900
|
+
this.folder = folder;
|
|
2901
|
+
}
|
|
2902
|
+
get dir() {
|
|
2903
|
+
return join5(this.folder, DIR, this.record.id);
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
|
|
2907
|
+
// web-src/sync-identity.ts
|
|
2908
|
+
var SyncIdentityKeys;
|
|
2909
|
+
((SyncIdentityKeys) => {
|
|
2910
|
+
SyncIdentityKeys.PREFIX = "id:";
|
|
2911
|
+
SyncIdentityKeys.IDENTITY = "id:identity";
|
|
2912
|
+
SyncIdentityKeys.ROOM = "id:room:";
|
|
2913
|
+
SyncIdentityKeys.FRIENDS = "id:friends";
|
|
2914
|
+
function room(member) {
|
|
2915
|
+
return `${SyncIdentityKeys.ROOM}${member}`;
|
|
2916
|
+
}
|
|
2917
|
+
SyncIdentityKeys.room = room;
|
|
2918
|
+
})(SyncIdentityKeys ||= {});
|
|
2919
|
+
var MAX_FRIENDS = 150;
|
|
2920
|
+
|
|
2921
|
+
class SyncIdentity {
|
|
2922
|
+
room;
|
|
2923
|
+
side;
|
|
2924
|
+
skip;
|
|
2925
|
+
constructor(room, side, skip) {
|
|
2926
|
+
this.room = room;
|
|
2927
|
+
this.side = side;
|
|
2928
|
+
this.skip = skip;
|
|
2929
|
+
}
|
|
2930
|
+
async round(base) {
|
|
2931
|
+
const report = { switched: false, sent: 0, received: 0 };
|
|
2932
|
+
const space = await this.room.getState(SyncIdentityKeys.PREFIX);
|
|
2933
|
+
let local = await this.side.exportIdentity();
|
|
2934
|
+
const there = space[SyncIdentityKeys.IDENTITY];
|
|
2935
|
+
const theirs = SyncIdentityUtil.isIdentity(there?.value) ? there.value : undefined;
|
|
2936
|
+
if (!theirs) {
|
|
2937
|
+
if (!(await this.room.setState(SyncIdentityKeys.IDENTITY, SyncIdentityUtil.portable(local.identity), there?.version ?? 0)).ok) {
|
|
2938
|
+
return report;
|
|
2939
|
+
}
|
|
2940
|
+
await this.side.setIdentityShared(true);
|
|
2941
|
+
base.name = local.identity.name;
|
|
2942
|
+
report.sent++;
|
|
2943
|
+
} else if (theirs.id !== local.identity.id) {
|
|
2944
|
+
await this.side.importIdentity({ identity: theirs, rooms: [], friends: [] }, { shared: true });
|
|
2945
|
+
report.switched = true;
|
|
2946
|
+
report.received++;
|
|
2947
|
+
base.name = theirs.name;
|
|
2948
|
+
base.rooms = [];
|
|
2949
|
+
base.friends = [];
|
|
2950
|
+
local = await this.side.exportIdentity();
|
|
2951
|
+
} else {
|
|
2952
|
+
if (!local.identity.shared) {
|
|
2953
|
+
await this.side.setIdentityShared(true);
|
|
2954
|
+
}
|
|
2955
|
+
if (local.identity.name !== theirs.name) {
|
|
2956
|
+
if (base.name === theirs.name) {
|
|
2957
|
+
const named = { ...theirs, name: local.identity.name };
|
|
2958
|
+
if ((await this.room.setState(SyncIdentityKeys.IDENTITY, named, there?.version)).ok) {
|
|
2959
|
+
report.sent++;
|
|
2960
|
+
}
|
|
2961
|
+
} else {
|
|
2962
|
+
await this.side.setName(theirs.name);
|
|
2963
|
+
report.received++;
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
base.name = (await this.side.exportIdentity()).identity.name;
|
|
2967
|
+
}
|
|
2968
|
+
await this.rooms(space, base, report);
|
|
2969
|
+
await this.friends(space, base, report);
|
|
2970
|
+
return report;
|
|
2971
|
+
}
|
|
2972
|
+
async rooms(space, base, report) {
|
|
2973
|
+
const remote = new Map;
|
|
2974
|
+
for (const [key, entry] of Object.entries(space)) {
|
|
2975
|
+
if (key.startsWith(SyncIdentityKeys.ROOM)) {
|
|
2976
|
+
remote.set(key.slice(SyncIdentityKeys.ROOM.length), {
|
|
2977
|
+
...SyncIdentityUtil.isRecord(entry.value) ? { value: entry.value } : {},
|
|
2978
|
+
version: entry.version
|
|
2979
|
+
});
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
const had = new Set(base.rooms);
|
|
2983
|
+
const before = await this.side.exportIdentity();
|
|
2984
|
+
const mine = new Map(before.rooms.filter((record) => !this.skip(record)).map((record) => [record.member, record]));
|
|
2985
|
+
const rooms = new Map(before.rooms.map((record) => [record.id, record]));
|
|
2986
|
+
for (const [member, { value, version }] of remote) {
|
|
2987
|
+
const local = mine.get(member);
|
|
2988
|
+
if (value && local) {
|
|
2989
|
+
if (!SyncMerge.same(value, local) && await this.side.upsertRoom(value)) {
|
|
2990
|
+
report.received++;
|
|
2991
|
+
}
|
|
2992
|
+
} else if (value && had.has(member)) {
|
|
2993
|
+
await this.room.setState(SyncIdentityKeys.room(member), null, version);
|
|
2994
|
+
report.sent++;
|
|
2995
|
+
} else if (value && !this.skip(value) && (rooms.get(value.id)?.member ?? member) === member) {
|
|
2996
|
+
if (await this.side.upsertRoom(value)) {
|
|
2997
|
+
report.received++;
|
|
2998
|
+
}
|
|
2999
|
+
} else if (!value && local && had.has(member)) {
|
|
3000
|
+
if (await this.side.forgetRoom(local.id, member)) {
|
|
3001
|
+
report.received++;
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
for (const member of had) {
|
|
3006
|
+
const local = mine.get(member);
|
|
3007
|
+
if (local && !remote.has(member) && await this.side.forgetRoom(local.id, member)) {
|
|
3008
|
+
report.received++;
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
const after = await this.side.exportIdentity();
|
|
3012
|
+
const kept = [];
|
|
3013
|
+
for (const record of after.rooms.filter((each) => !this.skip(each))) {
|
|
3014
|
+
const there = remote.get(record.member);
|
|
3015
|
+
if (there?.value && SyncMerge.same(there.value, record)) {
|
|
3016
|
+
kept.push(record.member);
|
|
3017
|
+
continue;
|
|
3018
|
+
}
|
|
3019
|
+
if (!there?.value && had.has(record.member)) {
|
|
3020
|
+
continue;
|
|
3021
|
+
}
|
|
3022
|
+
if ((await this.room.setState(SyncIdentityKeys.room(record.member), record, there?.version ?? 0)).ok) {
|
|
3023
|
+
kept.push(record.member);
|
|
3024
|
+
report.sent++;
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
base.rooms = kept;
|
|
3028
|
+
}
|
|
3029
|
+
async friends(space, base, report) {
|
|
3030
|
+
const there = space[SyncIdentityKeys.FRIENDS];
|
|
3031
|
+
const remote = Array.isArray(there?.value) ? there.value.filter(SyncIdentityUtil.isFriend) : [];
|
|
3032
|
+
const had = new Set(base.friends);
|
|
3033
|
+
const local = (await this.side.exportIdentity()).friends;
|
|
3034
|
+
const localIds = new Set(local.map((friend) => friend.id));
|
|
3035
|
+
const remoteIds = new Set(remote.map((friend) => friend.id));
|
|
3036
|
+
for (const friend of local) {
|
|
3037
|
+
if (had.has(friend.id) && !remoteIds.has(friend.id) && await this.side.removeFriend(friend.id)) {
|
|
3038
|
+
report.received++;
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
const taken = remote.filter((friend) => !(had.has(friend.id) && !localIds.has(friend.id)));
|
|
3042
|
+
if (taken.length && await this.side.mergeFriends(taken)) {
|
|
3043
|
+
report.received++;
|
|
3044
|
+
}
|
|
3045
|
+
const merged = (await this.side.exportIdentity()).friends.sort((a, b) => b.seenAt - a.seenAt).slice(0, MAX_FRIENDS);
|
|
3046
|
+
if (!SyncMerge.same(SyncIdentityUtil.byId(merged), SyncIdentityUtil.byId(remote))) {
|
|
3047
|
+
if ((await this.room.setState(SyncIdentityKeys.FRIENDS, merged, there?.version ?? 0)).ok) {
|
|
3048
|
+
report.sent++;
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
base.friends = merged.map((friend) => friend.id);
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
var SyncIdentityUtil;
|
|
3055
|
+
((SyncIdentityUtil) => {
|
|
3056
|
+
function isIdentity(value) {
|
|
3057
|
+
const identity = value;
|
|
3058
|
+
return SyncMerge.isObject(value) && typeof identity.id === "string" && typeof identity.name === "string" && SyncMerge.isObject(identity.signKey);
|
|
3059
|
+
}
|
|
3060
|
+
SyncIdentityUtil.isIdentity = isIdentity;
|
|
3061
|
+
function isRecord(value) {
|
|
3062
|
+
const record = value;
|
|
3063
|
+
return SyncMerge.isObject(value) && typeof record.id === "string" && typeof record.member === "string" && typeof record.status === "string";
|
|
3064
|
+
}
|
|
3065
|
+
SyncIdentityUtil.isRecord = isRecord;
|
|
3066
|
+
function isFriend(value) {
|
|
3067
|
+
const friend = value;
|
|
3068
|
+
return SyncMerge.isObject(value) && typeof friend.id === "string" && typeof friend.seenAt === "number";
|
|
3069
|
+
}
|
|
3070
|
+
SyncIdentityUtil.isFriend = isFriend;
|
|
3071
|
+
function portable(identity) {
|
|
3072
|
+
const { shared: _shared, ...rest } = identity;
|
|
3073
|
+
return rest;
|
|
3074
|
+
}
|
|
3075
|
+
SyncIdentityUtil.portable = portable;
|
|
3076
|
+
function byId(friends) {
|
|
3077
|
+
return [...friends].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
3078
|
+
}
|
|
3079
|
+
SyncIdentityUtil.byId = byId;
|
|
3080
|
+
})(SyncIdentityUtil ||= {});
|
|
3081
|
+
|
|
3082
|
+
// web-src/sync-service.ts
|
|
3083
|
+
class SyncService {
|
|
3084
|
+
options;
|
|
3085
|
+
device;
|
|
3086
|
+
baseFile;
|
|
3087
|
+
timer;
|
|
3088
|
+
look;
|
|
3089
|
+
running;
|
|
3090
|
+
again = false;
|
|
3091
|
+
stopped = false;
|
|
3092
|
+
signature = "";
|
|
3093
|
+
lastFull = 0;
|
|
3094
|
+
report;
|
|
3095
|
+
shown = "";
|
|
3096
|
+
state;
|
|
3097
|
+
stops = [];
|
|
3098
|
+
space;
|
|
3099
|
+
spaceSignature = "";
|
|
3100
|
+
folderFile;
|
|
3101
|
+
constructor(options) {
|
|
3102
|
+
this.options = options;
|
|
3103
|
+
this.device = options.device ?? SyncService.deviceName();
|
|
3104
|
+
this.baseFile = join6(options.agentDir, "sync", "base.json");
|
|
3105
|
+
this.folderFile = join6(options.agentDir, "sync", "folder.json");
|
|
3106
|
+
}
|
|
3107
|
+
static deviceName() {
|
|
3108
|
+
return hostname3().replace(/\.local$/, "") || platform2();
|
|
3109
|
+
}
|
|
3110
|
+
start() {
|
|
3111
|
+
this.stops.push(this.options.rooms.onEvent((event) => this.heard(event)));
|
|
3112
|
+
this.look = setInterval(() => void this.lookAround(), this.options.lookMs ?? 30000);
|
|
3113
|
+
this.options.storage.get(SyncKeys.REPORT).then((report) => {
|
|
3114
|
+
this.report ??= report;
|
|
3115
|
+
});
|
|
3116
|
+
this.schedule(2000);
|
|
3117
|
+
}
|
|
3118
|
+
dispose() {
|
|
3119
|
+
this.stopped = true;
|
|
3120
|
+
clearTimeout(this.timer);
|
|
3121
|
+
clearInterval(this.look);
|
|
3122
|
+
this.space?.stop();
|
|
3123
|
+
for (const stop of this.stops) {
|
|
3124
|
+
stop();
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
dataChanged(name, key) {
|
|
3128
|
+
if (name === SYNC_PACKAGE ? key === SyncKeys.RESOURCES : this.options.place.tracks(name, key)) {
|
|
3129
|
+
this.schedule();
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
async status() {
|
|
3133
|
+
const resources = await this.resources();
|
|
3134
|
+
const replaced = { dir: this.options.place.replacedDir, count: await this.options.place.replacedCount() };
|
|
3135
|
+
const common = { resources, syncing: !!this.running, last: this.report, replaced };
|
|
3136
|
+
const folder = await this.folderRecord();
|
|
3137
|
+
if (folder?.service) {
|
|
3138
|
+
const opened = await this.openSpace(folder);
|
|
3139
|
+
const problem = opened.problem ?? await this.problemOf(opened.space, folder);
|
|
3140
|
+
const service = (await this.options.relays?.list().catch(() => {
|
|
3141
|
+
return;
|
|
3142
|
+
}) ?? []).find((each) => each.id === folder.service);
|
|
3143
|
+
return {
|
|
3144
|
+
state: "on",
|
|
3145
|
+
where: "store",
|
|
3146
|
+
via: service?.name ?? service?.where ?? folder.type ?? "",
|
|
3147
|
+
store: { type: folder.type ?? service?.type ?? "", where: service?.where ?? "", ...problem ? { problem } : {} },
|
|
3148
|
+
owner: false,
|
|
3149
|
+
connected: !problem,
|
|
3150
|
+
devices: problem || !opened.space ? [] : await this.folderDevices(opened.space),
|
|
3151
|
+
asks: [],
|
|
3152
|
+
...common
|
|
3153
|
+
};
|
|
3154
|
+
}
|
|
3155
|
+
if (folder) {
|
|
3156
|
+
const opened = await this.openSpace(folder);
|
|
3157
|
+
const space = opened.space;
|
|
3158
|
+
const problem = opened.problem ?? await this.problemOf(space, folder);
|
|
3159
|
+
const target = await this.target();
|
|
3160
|
+
const named = target.type === "folder" && target.path && RelayServiceUtil.expand(target.path) === RelayServiceUtil.expand(folder.path) ? target.name : undefined;
|
|
3161
|
+
return {
|
|
3162
|
+
state: "on",
|
|
3163
|
+
where: "folder",
|
|
3164
|
+
via: named ?? SyncService.lastPart(folder.path),
|
|
3165
|
+
folder: { path: RelayServiceUtil.expand(folder.path), ...problem ? { problem } : {} },
|
|
3166
|
+
owner: false,
|
|
3167
|
+
connected: !problem,
|
|
3168
|
+
devices: problem ? [] : await this.folderDevices(space),
|
|
3169
|
+
asks: [],
|
|
3170
|
+
...common
|
|
3171
|
+
};
|
|
3172
|
+
}
|
|
3173
|
+
const room = await this.room();
|
|
3174
|
+
if (!room) {
|
|
3175
|
+
const target = await this.target();
|
|
3176
|
+
return {
|
|
3177
|
+
state: "off",
|
|
3178
|
+
where: target.type === "ta-relay" ? "relay" : target.type,
|
|
3179
|
+
via: target.name ?? (target.type === "folder" ? SyncService.lastPart(target.path ?? "") : target.type === "store" ? target.where : target.host),
|
|
3180
|
+
...target.type === "folder" ? { folder: { path: RelayServiceUtil.expand(target.path ?? "") } } : {},
|
|
3181
|
+
...target.type === "store" ? { store: { type: target.store ?? "", where: target.where ?? "" } } : {},
|
|
3182
|
+
owner: false,
|
|
3183
|
+
connected: false,
|
|
3184
|
+
devices: [],
|
|
3185
|
+
asks: [],
|
|
3186
|
+
...common
|
|
3187
|
+
};
|
|
3188
|
+
}
|
|
3189
|
+
const state = room.status === "member" ? "on" : room.status;
|
|
3190
|
+
return {
|
|
3191
|
+
state,
|
|
3192
|
+
where: "relay",
|
|
3193
|
+
...room.relay ? { via: await this.relayName(room.relay) } : {},
|
|
3194
|
+
owner: room.owner,
|
|
3195
|
+
ownerName: room.ownerName,
|
|
3196
|
+
connected: room.connected,
|
|
3197
|
+
devices: state === "on" ? await this.devices(room) : [],
|
|
3198
|
+
asks: room.owner ? (await this.options.rooms.listRequests().catch(() => [])).filter((request) => request.room === room.id && !request.guest).map(({ ask, name, at }) => ({ ask, name, at })) : [],
|
|
3199
|
+
...common
|
|
3200
|
+
};
|
|
3201
|
+
}
|
|
3202
|
+
async enable() {
|
|
3203
|
+
const target = await this.target();
|
|
3204
|
+
if (target.type === "folder" && target.path || target.type === "store" && target.id) {
|
|
3205
|
+
const before = await this.room();
|
|
3206
|
+
if (before) {
|
|
3207
|
+
await this.options.rooms.leave(before.id);
|
|
3208
|
+
}
|
|
3209
|
+
const current = await this.folderRecord();
|
|
3210
|
+
const same = !!current && (target.type === "store" ? current.service === target.id : !current.service && RelayServiceUtil.expand(current.path) === RelayServiceUtil.expand(target.path ?? ""));
|
|
3211
|
+
if (!same) {
|
|
3212
|
+
const record = {
|
|
3213
|
+
id: FolderSyncUtil.randomId(),
|
|
3214
|
+
key: FolderSyncUtil.randomKey(),
|
|
3215
|
+
path: target.type === "folder" ? target.path ?? "" : "",
|
|
3216
|
+
...target.type === "store" ? { service: target.id, type: target.store } : {},
|
|
3217
|
+
device: FolderSyncUtil.randomId(),
|
|
3218
|
+
at: Date.now()
|
|
3219
|
+
};
|
|
3220
|
+
const problem = target.type === "folder" ? RelayServiceUtil.checkFolder(record.path) : undefined;
|
|
3221
|
+
if (problem) {
|
|
3222
|
+
throw new Error(problem);
|
|
3223
|
+
}
|
|
3224
|
+
const opened = await this.openSpace(record);
|
|
3225
|
+
if (!opened.space) {
|
|
3226
|
+
throw new Error(opened.problem);
|
|
3227
|
+
}
|
|
3228
|
+
await opened.space.create();
|
|
3229
|
+
await this.saveFolder(record);
|
|
3230
|
+
}
|
|
3231
|
+
await this.publish();
|
|
3232
|
+
this.schedule(0);
|
|
3233
|
+
return this.status();
|
|
3234
|
+
}
|
|
3235
|
+
const room = await this.room();
|
|
3236
|
+
if (room?.status !== "member") {
|
|
3237
|
+
if (room) {
|
|
3238
|
+
await this.options.rooms.leave(room.id);
|
|
3239
|
+
}
|
|
3240
|
+
await this.options.rooms.createRoom({ kind: SYNC_KIND, title: SyncServerTexts.title(), view: SYNC_VIEW, unlisted: true, use: "sync" });
|
|
3241
|
+
}
|
|
3242
|
+
this.schedule(0);
|
|
3243
|
+
return this.status();
|
|
3244
|
+
}
|
|
3245
|
+
async invite(renew = false) {
|
|
3246
|
+
const folder = await this.folderRecord();
|
|
3247
|
+
if (folder?.service) {
|
|
3248
|
+
const config = await this.options.relays?.storeConfig(folder.service);
|
|
3249
|
+
if (!config) {
|
|
3250
|
+
throw new Error(SyncServerTexts.storeUnavailable(folder.type ?? ""));
|
|
3251
|
+
}
|
|
3252
|
+
return { link: FolderSyncUtil.storeLink(folder, config, this.device), expiresAt: 0 };
|
|
3253
|
+
}
|
|
3254
|
+
if (folder) {
|
|
3255
|
+
return { link: FolderSyncUtil.link(folder, this.device), expiresAt: 0 };
|
|
3256
|
+
}
|
|
3257
|
+
const room = await this.room();
|
|
3258
|
+
if (room?.status !== "member") {
|
|
3259
|
+
throw new Error(SyncServerTexts.notOn());
|
|
3260
|
+
}
|
|
3261
|
+
if (!room.owner) {
|
|
3262
|
+
throw new Error(SyncServerTexts.notOwner(room.ownerName));
|
|
3263
|
+
}
|
|
3264
|
+
return this.options.rooms.invite(room.id, { renew });
|
|
3265
|
+
}
|
|
3266
|
+
async join(link) {
|
|
3267
|
+
const room = await this.room();
|
|
3268
|
+
if (room?.status === "member" || await this.folderRecord()) {
|
|
3269
|
+
throw new Error(SyncServerTexts.alreadyOn());
|
|
3270
|
+
}
|
|
3271
|
+
if (FolderSyncUtil.is(link)) {
|
|
3272
|
+
return this.joinFolder(link);
|
|
3273
|
+
}
|
|
3274
|
+
if (FolderSyncUtil.isStore(link)) {
|
|
3275
|
+
return this.joinStore(link);
|
|
3276
|
+
}
|
|
3277
|
+
if (room) {
|
|
3278
|
+
await this.options.rooms.leave(room.id);
|
|
3279
|
+
}
|
|
3280
|
+
const joined = await this.options.rooms.join(link);
|
|
3281
|
+
await this.options.storage.set(SyncKeys.JOINING, joined.id);
|
|
3282
|
+
await this.publish();
|
|
3283
|
+
return this.status();
|
|
3284
|
+
}
|
|
3285
|
+
async leave() {
|
|
3286
|
+
const folder = await this.folderRecord();
|
|
3287
|
+
if (folder) {
|
|
3288
|
+
this.space?.stop();
|
|
3289
|
+
this.space = undefined;
|
|
3290
|
+
await rm4(this.folderFile, { force: true });
|
|
3291
|
+
}
|
|
3292
|
+
const room = await this.room();
|
|
3293
|
+
if (room) {
|
|
3294
|
+
await this.options.rooms.leave(room.id);
|
|
3295
|
+
}
|
|
3296
|
+
await this.options.storage.set(SyncKeys.JOINING, null);
|
|
3297
|
+
await this.saveBase({ room: "", items: {} });
|
|
3298
|
+
await this.publish();
|
|
3299
|
+
return this.status();
|
|
3300
|
+
}
|
|
3301
|
+
async answer(ask, accept) {
|
|
3302
|
+
const room = await this.room();
|
|
3303
|
+
if (room?.owner) {
|
|
3304
|
+
await this.options.rooms.answer(room.id, ask, accept);
|
|
3305
|
+
}
|
|
3306
|
+
await this.publish();
|
|
3307
|
+
return this.status();
|
|
3308
|
+
}
|
|
3309
|
+
async removeDevice(member) {
|
|
3310
|
+
const room = await this.room();
|
|
3311
|
+
if (room?.owner) {
|
|
3312
|
+
await this.options.rooms.removeMember(room.id, member);
|
|
3313
|
+
await this.options.rooms.setState(room.id, SyncStateKeys.device(member), null).catch(() => {
|
|
3314
|
+
return;
|
|
3315
|
+
});
|
|
3316
|
+
}
|
|
3317
|
+
return this.status();
|
|
3318
|
+
}
|
|
3319
|
+
async setResources(change) {
|
|
3320
|
+
const resources = { ...await this.resources() };
|
|
3321
|
+
for (const resource of SYNC_RESOURCES) {
|
|
3322
|
+
if (typeof change?.[resource] === "boolean") {
|
|
3323
|
+
resources[resource] = change[resource];
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
await this.options.storage.set(SyncKeys.RESOURCES, resources);
|
|
3327
|
+
this.schedule(0);
|
|
3328
|
+
return this.status();
|
|
3329
|
+
}
|
|
3330
|
+
async syncNow() {
|
|
3331
|
+
clearTimeout(this.timer);
|
|
3332
|
+
await this.run();
|
|
3333
|
+
return this.status();
|
|
3334
|
+
}
|
|
3335
|
+
heard(event) {
|
|
3336
|
+
if (event.type === "rooms" || event.type === "request") {
|
|
3337
|
+
this.publish();
|
|
3338
|
+
return;
|
|
3339
|
+
}
|
|
3340
|
+
if (event.type === "state" && (event.key.startsWith(SyncStateKeys.ITEM) || event.key.startsWith(SyncStateKeys.BLOB) || event.key.startsWith(SyncIdentityKeys.PREFIX))) {
|
|
3341
|
+
this.room().then((room) => {
|
|
3342
|
+
const self = room?.members.find((member) => member.self)?.member;
|
|
3343
|
+
if (room?.id === event.room && event.by !== self) {
|
|
3344
|
+
this.schedule();
|
|
3345
|
+
}
|
|
3346
|
+
});
|
|
3347
|
+
}
|
|
3348
|
+
}
|
|
3349
|
+
schedule(delay = this.options.delayMs ?? 1500) {
|
|
3350
|
+
if (this.stopped) {
|
|
3351
|
+
return;
|
|
3352
|
+
}
|
|
3353
|
+
clearTimeout(this.timer);
|
|
3354
|
+
this.timer = setTimeout(() => void this.run(), delay);
|
|
3355
|
+
}
|
|
3356
|
+
run() {
|
|
3357
|
+
if (this.running) {
|
|
3358
|
+
this.again = true;
|
|
3359
|
+
return this.running;
|
|
3360
|
+
}
|
|
3361
|
+
this.running = this.round().catch((error) => {
|
|
3362
|
+
this.report = { at: Date.now(), sent: 0, received: 0, skipped: [], error: error instanceof Error ? error.message : String(error) };
|
|
3363
|
+
}).finally(async () => {
|
|
3364
|
+
this.running = undefined;
|
|
3365
|
+
await this.options.storage.set(SyncKeys.REPORT, this.report).catch(() => {
|
|
3366
|
+
return;
|
|
3367
|
+
});
|
|
3368
|
+
await this.publish();
|
|
3369
|
+
if (this.again && !this.stopped) {
|
|
3370
|
+
this.again = false;
|
|
3371
|
+
this.schedule(0);
|
|
3372
|
+
}
|
|
3373
|
+
});
|
|
3374
|
+
this.publish();
|
|
3375
|
+
return this.running;
|
|
3376
|
+
}
|
|
3377
|
+
async round() {
|
|
3378
|
+
const folder = await this.folderRecord();
|
|
3379
|
+
const room = folder ? undefined : await this.room();
|
|
3380
|
+
if (!folder && room?.status !== "member") {
|
|
3381
|
+
return;
|
|
3382
|
+
}
|
|
3383
|
+
let target;
|
|
3384
|
+
if (folder) {
|
|
3385
|
+
const opened = await this.openSpace(folder);
|
|
3386
|
+
const problem = opened.problem ?? await this.problemOf(opened.space, folder);
|
|
3387
|
+
if (problem || !opened.space) {
|
|
3388
|
+
throw new Error(problem);
|
|
3389
|
+
}
|
|
3390
|
+
target = { id: `folder:${folder.id}`, room: opened.space, self: folder.device };
|
|
3391
|
+
} else {
|
|
3392
|
+
const joined = room;
|
|
3393
|
+
target = { id: joined.id, room: this.roomOf(joined.id), self: joined.members.find((member) => member.self)?.member };
|
|
3394
|
+
}
|
|
3395
|
+
const now = await this.resources();
|
|
3396
|
+
const resources = new Set(SYNC_RESOURCES.filter((resource) => now[resource]));
|
|
3397
|
+
const loaded = await this.loadBase();
|
|
3398
|
+
const base = loaded.room === target.id ? loaded : { room: target.id, items: {} };
|
|
3399
|
+
this.options.place.begin();
|
|
3400
|
+
const engine = new SyncEngine(target.room, this.options.place, this.device);
|
|
3401
|
+
const report = await engine.round(base, resources);
|
|
3402
|
+
const identity = this.options.identity;
|
|
3403
|
+
if (identity && resources.has("identity")) {
|
|
3404
|
+
base.identity ??= { rooms: [], friends: [] };
|
|
3405
|
+
const shared = await new SyncIdentity(target.room, identity, (record) => record.kind === SYNC_KIND).round(base.identity);
|
|
3406
|
+
report.sent += shared.sent;
|
|
3407
|
+
report.received += shared.received;
|
|
3408
|
+
}
|
|
3409
|
+
await this.saveBase(base);
|
|
3410
|
+
if (target.self) {
|
|
3411
|
+
const entry = { name: this.device, platform: platform2(), at: report.at };
|
|
3412
|
+
await target.room.setState(SyncStateKeys.device(target.self), entry).catch(() => {
|
|
3413
|
+
return;
|
|
3414
|
+
});
|
|
3415
|
+
}
|
|
3416
|
+
if (folder && this.space) {
|
|
3417
|
+
this.spaceSignature = await this.space.space.signature().catch(() => this.spaceSignature);
|
|
3418
|
+
}
|
|
3419
|
+
this.signature = await this.options.place.signature(resources);
|
|
3420
|
+
this.lastFull = Date.now();
|
|
3421
|
+
if (report.sent || report.received || report.skipped.length || !this.report) {
|
|
3422
|
+
this.report = report;
|
|
3423
|
+
} else {
|
|
3424
|
+
this.report = { ...this.report, at: report.at, error: undefined };
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
async lookAround() {
|
|
3428
|
+
if (this.running || this.stopped) {
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
const resources = new Set(SYNC_RESOURCES.filter((resource) => this.resourcesNow[resource]));
|
|
3432
|
+
const signature = await this.options.place.signature(resources).catch(() => this.signature);
|
|
3433
|
+
const space = this.space ? await this.space.space.signature().catch(() => this.spaceSignature) : this.spaceSignature;
|
|
3434
|
+
const every = this.options.everyMs ?? 10 * 60000;
|
|
3435
|
+
if (signature !== this.signature || space !== this.spaceSignature || Date.now() - this.lastFull > every) {
|
|
3436
|
+
this.schedule(0);
|
|
3437
|
+
}
|
|
3438
|
+
}
|
|
3439
|
+
async target() {
|
|
3440
|
+
const service = await this.options.relays?.serviceFor("sync").catch(() => {
|
|
3441
|
+
return;
|
|
3442
|
+
});
|
|
3443
|
+
const name = service?.builtin ? undefined : service?.name;
|
|
3444
|
+
if (service?.type === "folder") {
|
|
3445
|
+
return { type: "folder", path: service.path, id: service.id, ...name ? { name } : {} };
|
|
3446
|
+
}
|
|
3447
|
+
if (service && service.type !== "ta-relay") {
|
|
3448
|
+
return { type: "store", id: service.id, store: service.type, where: service.where ?? service.type, ...name ? { name } : {} };
|
|
3449
|
+
}
|
|
3450
|
+
return {
|
|
3451
|
+
type: "ta-relay",
|
|
3452
|
+
name: service?.builtin || !service ? SyncServerTexts.official() : name,
|
|
3453
|
+
host: SyncService.host(service?.url ?? "https://relay.cellbang.com")
|
|
3454
|
+
};
|
|
3455
|
+
}
|
|
3456
|
+
async relayName(url) {
|
|
3457
|
+
const relays = await this.options.relays?.list?.().catch(() => {
|
|
3458
|
+
return;
|
|
3459
|
+
}) ?? [];
|
|
3460
|
+
const named = relays.find((relay) => relay.url === url);
|
|
3461
|
+
return named?.builtin ? SyncServerTexts.official() : named?.name ?? SyncService.host(url);
|
|
3462
|
+
}
|
|
3463
|
+
static host(url) {
|
|
3464
|
+
try {
|
|
3465
|
+
return new URL(url).host;
|
|
3466
|
+
} catch {
|
|
3467
|
+
return url;
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
static lastPart(path) {
|
|
3471
|
+
return path.split("/").filter(Boolean).pop() ?? path;
|
|
3472
|
+
}
|
|
3473
|
+
async joinFolder(link) {
|
|
3474
|
+
const given = FolderSyncUtil.parse(link);
|
|
3475
|
+
const relays = this.options.relays;
|
|
3476
|
+
const target = await this.target();
|
|
3477
|
+
const path = target.type === "folder" && target.path ? target.path : given.path;
|
|
3478
|
+
const record = { id: given.id, key: given.key, path, device: FolderSyncUtil.randomId(), at: Date.now() };
|
|
3479
|
+
const space = new FolderSpace(RelayServiceUtil.expand(path), record);
|
|
3480
|
+
const problem = RelayServiceUtil.checkFolder(path) ?? await this.problemOf(space, record);
|
|
3481
|
+
if (problem) {
|
|
3482
|
+
throw new Error(problem);
|
|
3483
|
+
}
|
|
3484
|
+
if (relays && target.type !== "folder") {
|
|
3485
|
+
await relays.choose("sync", await relays.addFolder(path));
|
|
3486
|
+
}
|
|
3487
|
+
await this.saveFolder(record);
|
|
3488
|
+
await this.publish();
|
|
3489
|
+
this.schedule(0);
|
|
3490
|
+
return this.status();
|
|
3491
|
+
}
|
|
3492
|
+
async joinStore(link) {
|
|
3493
|
+
const given = FolderSyncUtil.parseStore(link);
|
|
3494
|
+
const relays = this.options.relays;
|
|
3495
|
+
if (!relays) {
|
|
3496
|
+
throw new Error(SyncServerTexts.storeUnavailable(given.type));
|
|
3497
|
+
}
|
|
3498
|
+
const service = await relays.addStore(given.type, given.options, given.service || undefined).catch((error) => {
|
|
3499
|
+
throw new Error(/no type/i.test(String(error?.message)) ? SyncServerTexts.storeUnavailable(given.type) : error instanceof Error ? error.message : String(error));
|
|
3500
|
+
});
|
|
3501
|
+
const record = {
|
|
3502
|
+
id: given.id,
|
|
3503
|
+
key: given.key,
|
|
3504
|
+
path: "",
|
|
3505
|
+
service,
|
|
3506
|
+
type: given.type,
|
|
3507
|
+
device: FolderSyncUtil.randomId(),
|
|
3508
|
+
at: Date.now()
|
|
3509
|
+
};
|
|
3510
|
+
const opened = await this.openSpace(record);
|
|
3511
|
+
const problem = opened.problem ?? await this.problemOf(opened.space, record);
|
|
3512
|
+
if (problem) {
|
|
3513
|
+
throw new Error(problem);
|
|
3514
|
+
}
|
|
3515
|
+
await relays.choose("sync", service);
|
|
3516
|
+
await this.saveFolder(record);
|
|
3517
|
+
await this.publish();
|
|
3518
|
+
this.schedule(0);
|
|
3519
|
+
return this.status();
|
|
3520
|
+
}
|
|
3521
|
+
async folderRecord() {
|
|
3522
|
+
try {
|
|
3523
|
+
const record = JSON.parse(await readFile4(this.folderFile, "utf8"));
|
|
3524
|
+
return record && typeof record.id === "string" && typeof record.key === "string" && typeof record.path === "string" && typeof record.device === "string" ? record : undefined;
|
|
3525
|
+
} catch {
|
|
3526
|
+
return;
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
async saveFolder(record) {
|
|
3530
|
+
await mkdir5(join6(this.options.agentDir, "sync"), { recursive: true });
|
|
3531
|
+
await writeFile4(`${this.folderFile}.tmp`, JSON.stringify(record), { mode: 384 });
|
|
3532
|
+
await rename5(`${this.folderFile}.tmp`, this.folderFile);
|
|
3533
|
+
}
|
|
3534
|
+
async openSpace(record) {
|
|
3535
|
+
const where = record.service ? `store:${record.service}` : `folder:${record.path}`;
|
|
3536
|
+
if (this.space?.record.id === record.id && this.space.where === where) {
|
|
3537
|
+
return { space: this.space.space };
|
|
3538
|
+
}
|
|
3539
|
+
let space;
|
|
3540
|
+
if (record.service) {
|
|
3541
|
+
try {
|
|
3542
|
+
const relays = this.options.relays;
|
|
3543
|
+
if (!relays) {
|
|
3544
|
+
throw new Error("No relays.json.");
|
|
3545
|
+
}
|
|
3546
|
+
space = new StoreSpace(await relays.openStore(record.service), record);
|
|
3547
|
+
} catch {
|
|
3548
|
+
return { problem: SyncServerTexts.storeUnavailable(record.type ?? "") };
|
|
3549
|
+
}
|
|
3550
|
+
} else {
|
|
3551
|
+
space = new FolderSpace(RelayServiceUtil.expand(record.path), record);
|
|
3552
|
+
}
|
|
3553
|
+
this.space?.stop();
|
|
3554
|
+
let timer;
|
|
3555
|
+
const stop = space.watch(() => {
|
|
3556
|
+
clearTimeout(timer);
|
|
3557
|
+
timer = setTimeout(() => this.schedule(), 2000);
|
|
3558
|
+
});
|
|
3559
|
+
this.space = {
|
|
3560
|
+
record,
|
|
3561
|
+
where,
|
|
3562
|
+
space,
|
|
3563
|
+
stop: () => {
|
|
3564
|
+
clearTimeout(timer);
|
|
3565
|
+
stop();
|
|
3566
|
+
}
|
|
3567
|
+
};
|
|
3568
|
+
return { space };
|
|
3569
|
+
}
|
|
3570
|
+
async problemOf(space, record) {
|
|
3571
|
+
const folder = record.service ? undefined : RelayServiceUtil.checkFolder(record.path);
|
|
3572
|
+
if (folder) {
|
|
3573
|
+
return folder;
|
|
3574
|
+
}
|
|
3575
|
+
let check;
|
|
3576
|
+
try {
|
|
3577
|
+
check = await space.check();
|
|
3578
|
+
} catch (error) {
|
|
3579
|
+
return SyncServerTexts.storeFailed(error instanceof Error ? error.message : String(error));
|
|
3580
|
+
}
|
|
3581
|
+
return check === "missing" ? record.service ? SyncServerTexts.storeMissing() : SyncServerTexts.folderMissing(RelayServiceUtil.expand(record.path)) : check === "key" ? SyncServerTexts.folderKey() : check === "newer" ? SyncServerTexts.folderNewer() : undefined;
|
|
3582
|
+
}
|
|
3583
|
+
async folderDevices(space) {
|
|
3584
|
+
const self = this.space?.record.device;
|
|
3585
|
+
const devices = await space.devices();
|
|
3586
|
+
const shown = devices.map((device) => ({
|
|
3587
|
+
member: device.device,
|
|
3588
|
+
name: device.device === self ? this.device : device.name,
|
|
3589
|
+
platform: device.platform,
|
|
3590
|
+
online: device.device === self,
|
|
3591
|
+
self: device.device === self,
|
|
3592
|
+
owner: false,
|
|
3593
|
+
at: device.at
|
|
3594
|
+
}));
|
|
3595
|
+
if (self && !shown.some((device) => device.self)) {
|
|
3596
|
+
shown.push({ member: self, name: this.device, platform: platform2(), online: true, self: true, owner: false });
|
|
3597
|
+
}
|
|
3598
|
+
return shown.sort((a, b) => Number(b.self) - Number(a.self) || (b.at ?? 0) - (a.at ?? 0));
|
|
3599
|
+
}
|
|
3600
|
+
roomOf(id) {
|
|
3601
|
+
const rooms = this.options.rooms;
|
|
3602
|
+
return {
|
|
3603
|
+
getState: (prefix) => rooms.getState(id, prefix),
|
|
3604
|
+
setState: (key, value, version) => rooms.setState(id, key, value, version)
|
|
3605
|
+
};
|
|
3606
|
+
}
|
|
3607
|
+
async room() {
|
|
3608
|
+
const joining = await this.options.storage.get(SyncKeys.JOINING).catch(() => {
|
|
3609
|
+
return;
|
|
3610
|
+
});
|
|
3611
|
+
const rooms = (await this.options.rooms.listRooms().catch(() => [])).filter((room) => room.kind === SYNC_KIND || room.id === joining && room.kind === "");
|
|
3612
|
+
const latest = (room) => room.joinedAt ?? room.createdAt;
|
|
3613
|
+
rooms.sort((a, b) => Number(b.status === "member") - Number(a.status === "member") || latest(b) - latest(a));
|
|
3614
|
+
return rooms[0];
|
|
3615
|
+
}
|
|
3616
|
+
async devices(room) {
|
|
3617
|
+
const state = await this.options.rooms.getState(room.id, SyncStateKeys.DEVICE).catch(() => ({}));
|
|
3618
|
+
return room.members.filter((member) => !member.guest).map((member) => {
|
|
3619
|
+
const entry = state[SyncStateKeys.device(member.member)]?.value;
|
|
3620
|
+
return {
|
|
3621
|
+
member: member.member,
|
|
3622
|
+
name: member.self ? this.device : entry?.name ?? member.name,
|
|
3623
|
+
platform: member.self ? platform2() : entry?.platform,
|
|
3624
|
+
online: member.online || member.self,
|
|
3625
|
+
self: member.self,
|
|
3626
|
+
owner: member.owner,
|
|
3627
|
+
...entry?.at ? { at: entry.at } : {}
|
|
3628
|
+
};
|
|
3629
|
+
}).sort((a, b) => Number(b.self) - Number(a.self) || Number(b.owner) - Number(a.owner) || a.name.localeCompare(b.name));
|
|
3630
|
+
}
|
|
3631
|
+
resourcesNow = { ...DEFAULT_RESOURCES };
|
|
3632
|
+
async resources() {
|
|
3633
|
+
const saved = await this.options.storage.get(SyncKeys.RESOURCES).catch(() => {
|
|
3634
|
+
return;
|
|
3635
|
+
});
|
|
3636
|
+
const resources = { ...DEFAULT_RESOURCES };
|
|
3637
|
+
if (SyncMerge.isObject(saved)) {
|
|
3638
|
+
for (const resource of SYNC_RESOURCES) {
|
|
3639
|
+
if (typeof saved[resource] === "boolean") {
|
|
3640
|
+
resources[resource] = saved[resource];
|
|
3641
|
+
}
|
|
3642
|
+
}
|
|
3643
|
+
}
|
|
3644
|
+
this.resourcesNow = resources;
|
|
3645
|
+
return resources;
|
|
3646
|
+
}
|
|
3647
|
+
async publish() {
|
|
3648
|
+
const status = await this.status().catch(() => {
|
|
3649
|
+
return;
|
|
3650
|
+
});
|
|
3651
|
+
const text = status ? SyncMerge.canonical(status) : "";
|
|
3652
|
+
if (status && text !== this.shown) {
|
|
3653
|
+
if (status.state === "on" && this.state !== "on") {
|
|
3654
|
+
this.schedule(0);
|
|
3655
|
+
}
|
|
3656
|
+
this.state = status.state;
|
|
3657
|
+
this.shown = text;
|
|
3658
|
+
await this.options.storage.set(SyncKeys.STATUS, status).catch(() => {
|
|
3659
|
+
return;
|
|
3660
|
+
});
|
|
3661
|
+
}
|
|
3662
|
+
}
|
|
3663
|
+
async loadBase() {
|
|
3664
|
+
try {
|
|
3665
|
+
const data = JSON.parse(await readFile4(this.baseFile, "utf8"));
|
|
3666
|
+
return SyncMerge.isObject(data) && typeof data.room === "string" && SyncMerge.isObject(data.items) ? data : { room: "", items: {} };
|
|
3667
|
+
} catch {
|
|
3668
|
+
return { room: "", items: {} };
|
|
3669
|
+
}
|
|
3670
|
+
}
|
|
3671
|
+
async saveBase(base) {
|
|
3672
|
+
await mkdir5(join6(this.options.agentDir, "sync"), { recursive: true });
|
|
3673
|
+
await writeFile4(`${this.baseFile}.tmp`, JSON.stringify(base));
|
|
3674
|
+
await rename5(`${this.baseFile}.tmp`, this.baseFile);
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3678
|
+
// web-src/host.ts
|
|
3679
|
+
function hostSide(host) {
|
|
3680
|
+
return {
|
|
3681
|
+
updateSettings: (update) => host.updateSettings(update),
|
|
3682
|
+
installPackage: (source) => host.installPackage(source, "user"),
|
|
3683
|
+
removePackage: async (source) => {
|
|
3684
|
+
const found = (await host.listPackages()).find((pkg) => pkg.scope === "user" && pkg.source === source);
|
|
3685
|
+
if (found) {
|
|
3686
|
+
await host.removePackage(found.id);
|
|
3687
|
+
}
|
|
3688
|
+
},
|
|
3689
|
+
getData: (name, key) => host.getExtensionData(name, key),
|
|
3690
|
+
setData: (name, key, value) => host.setExtensionData(name, key, value),
|
|
3691
|
+
...typeof host.rooms.exportIdentity === "function" ? { identity: host.rooms } : {},
|
|
3692
|
+
dataKeys: async () => {
|
|
3693
|
+
const keys = Object.entries(SyncDeclarations.SHELL).flatMap(([name, list]) => list.map((key) => ({ name, key })));
|
|
3694
|
+
for (const pkg of await host.listPackages().catch(() => [])) {
|
|
3695
|
+
if (!pkg.enabled) {
|
|
3696
|
+
continue;
|
|
3697
|
+
}
|
|
3698
|
+
const manifest = await readFile5(join7(pkg.root, "package.json"), "utf8").then((text) => JSON.parse(text), () => {
|
|
3699
|
+
return;
|
|
3700
|
+
});
|
|
3701
|
+
keys.push(...SyncDeclarations.keys(manifest).map((key) => ({ name: pkg.name, key })));
|
|
3702
|
+
}
|
|
3703
|
+
return keys;
|
|
3704
|
+
}
|
|
3705
|
+
};
|
|
3706
|
+
}
|
|
3707
|
+
function activate(host) {
|
|
3708
|
+
const side = hostSide(host);
|
|
3709
|
+
const place = new AgentDirPlace(host.agentDir, side);
|
|
3710
|
+
const migrate = new MigrateService({
|
|
3711
|
+
agentDir: host.agentDir,
|
|
3712
|
+
place: new AgentDirPlace(host.agentDir, side),
|
|
3713
|
+
side,
|
|
3714
|
+
publish: (status) => void host.setExtensionData(SYNC_PACKAGE, MIGRATE_STATUS_KEY, status).catch(() => {
|
|
3715
|
+
return;
|
|
3716
|
+
})
|
|
3717
|
+
});
|
|
3718
|
+
host.setExtensionData(SYNC_PACKAGE, MIGRATE_STATUS_KEY, null).catch(() => {
|
|
3719
|
+
return;
|
|
3720
|
+
});
|
|
3721
|
+
const service = new SyncService({
|
|
3722
|
+
agentDir: host.agentDir,
|
|
3723
|
+
rooms: host.rooms,
|
|
3724
|
+
relays: host.relays,
|
|
3725
|
+
place,
|
|
3726
|
+
...side.identity ? { identity: side.identity } : {},
|
|
3727
|
+
storage: { get: (key) => host.getExtensionData(SYNC_PACKAGE, key), set: (key, value) => host.setExtensionData(SYNC_PACKAGE, key, value) }
|
|
3728
|
+
});
|
|
3729
|
+
const stopData = host.onExtensionData((name, key, cwd) => cwd === undefined && service.dataChanged(name, key));
|
|
3730
|
+
service.start();
|
|
3731
|
+
return {
|
|
3732
|
+
status: () => service.status(),
|
|
3733
|
+
enable: () => service.enable(),
|
|
3734
|
+
invite: (renew) => service.invite(renew === true),
|
|
3735
|
+
join: (link) => service.join(String(link ?? "")),
|
|
3736
|
+
leave: () => service.leave(),
|
|
3737
|
+
answer: (ask, accept) => service.answer(String(ask ?? ""), accept === true),
|
|
3738
|
+
removeDevice: (member) => service.removeDevice(String(member ?? "")),
|
|
3739
|
+
setResources: (change) => service.setResources(change ?? {}),
|
|
3740
|
+
syncNow: () => service.syncNow(),
|
|
3741
|
+
migrateStatus: () => migrate.status(),
|
|
3742
|
+
migrateCounts: () => migrate.counts(),
|
|
3743
|
+
migrateOpen: (categories) => migrate.openLan(categories),
|
|
3744
|
+
migrateAnswer: (ask, allow) => migrate.answer(String(ask ?? ""), allow === true),
|
|
3745
|
+
migrateCloseOut: () => migrate.closeOut(),
|
|
3746
|
+
migrateCloseIn: () => migrate.closeIn(),
|
|
3747
|
+
migrateDiscover: () => migrate.discover(),
|
|
3748
|
+
migrateStopDiscovering: () => migrate.stopDiscovering(),
|
|
3749
|
+
migrateReceive: (link) => migrate.receive(String(link ?? "")),
|
|
3750
|
+
migrateExport: (categories, password, dir) => migrate.exportFile(categories, typeof password === "string" ? password : undefined, typeof dir === "string" && dir ? dir : undefined),
|
|
3751
|
+
migrateImport: (path, password) => migrate.importFile(String(path ?? ""), typeof password === "string" ? password : undefined),
|
|
3752
|
+
migrateBackups: () => migrate.listBackups(),
|
|
3753
|
+
dispose: () => {
|
|
3754
|
+
stopData();
|
|
3755
|
+
service.dispose();
|
|
3756
|
+
migrate.dispose();
|
|
3757
|
+
}
|
|
3758
|
+
};
|
|
3759
|
+
}
|
|
3760
|
+
export {
|
|
3761
|
+
hostSide,
|
|
3762
|
+
activate as default
|
|
3763
|
+
};
|