@waq666/pneuma 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +144 -2
- package/bin/dsc.js +91 -0
- package/bin/headless.cjs +40 -0
- package/lib/adapter/core-runtime.d.ts +11 -0
- package/lib/adapter/core-runtime.js +259 -0
- package/lib/adapter/host.d.ts +18 -0
- package/lib/adapter/host.js +14 -0
- package/lib/adapter/runtime.d.ts +15 -0
- package/lib/adapter/runtime.js +290 -0
- package/lib/adapter/transcript.d.ts +184 -0
- package/lib/adapter/transcript.js +605 -0
- package/lib/app/AgentsOverlay.d.ts +18 -0
- package/lib/app/AgentsOverlay.js +63 -0
- package/lib/app/App.d.ts +13 -0
- package/lib/app/App.js +1498 -0
- package/lib/app/ApprovalCard.d.ts +12 -0
- package/lib/app/ApprovalCard.js +76 -0
- package/lib/app/AskCard.d.ts +20 -0
- package/lib/app/AskCard.js +45 -0
- package/lib/app/ChatView.d.ts +32 -0
- package/lib/app/ChatView.js +198 -0
- package/lib/app/Composer.d.ts +50 -0
- package/lib/app/Composer.js +282 -0
- package/lib/app/DiffBlock.d.ts +6 -0
- package/lib/app/DiffBlock.js +37 -0
- package/lib/app/MarkdownView.d.ts +6 -0
- package/lib/app/MarkdownView.js +99 -0
- package/lib/app/ModelPicker.d.ts +7 -0
- package/lib/app/ModelPicker.js +18 -0
- package/lib/app/PlanReviewCard.d.ts +13 -0
- package/lib/app/PlanReviewCard.js +54 -0
- package/lib/app/PresetPicker.d.ts +11 -0
- package/lib/app/PresetPicker.js +19 -0
- package/lib/app/PreviewOverlay.d.ts +14 -0
- package/lib/app/PreviewOverlay.js +22 -0
- package/lib/app/SessionPicker.d.ts +43 -0
- package/lib/app/SessionPicker.js +56 -0
- package/lib/app/SettingsOverlay.d.ts +32 -0
- package/lib/app/SettingsOverlay.js +157 -0
- package/lib/app/SkillsPicker.d.ts +7 -0
- package/lib/app/SkillsPicker.js +19 -0
- package/lib/app/StatusBar.d.ts +22 -0
- package/lib/app/StatusBar.js +272 -0
- package/lib/app/TaskStrips.d.ts +13 -0
- package/lib/app/TaskStrips.js +40 -0
- package/lib/app/ToolCard.d.ts +5 -0
- package/lib/app/ToolCard.js +40 -0
- package/lib/app/TranscriptOverlay.d.ts +12 -0
- package/lib/app/TranscriptOverlay.js +17 -0
- package/lib/app/Welcome.d.ts +6 -0
- package/lib/app/Welcome.js +66 -0
- package/lib/app/attach.d.ts +16 -0
- package/lib/app/attach.js +58 -0
- package/lib/app/click.d.ts +27 -0
- package/lib/app/click.js +89 -0
- package/lib/app/clipboard-image.d.ts +11 -0
- package/lib/app/clipboard-image.js +66 -0
- package/lib/app/cursor.d.ts +4 -0
- package/lib/app/cursor.js +24 -0
- package/lib/app/hello.d.ts +11 -0
- package/lib/app/hello.js +8 -0
- package/lib/app/history-store.d.ts +4 -0
- package/lib/app/history-store.js +41 -0
- package/lib/app/image-blocks.d.ts +12 -0
- package/lib/app/image-blocks.js +125 -0
- package/lib/app/markdown.d.ts +54 -0
- package/lib/app/markdown.js +189 -0
- package/lib/app/settings-model.d.ts +69 -0
- package/lib/app/settings-model.js +114 -0
- package/lib/app/theme.d.ts +166 -0
- package/lib/app/theme.js +141 -0
- package/lib/boot.d.ts +1 -0
- package/lib/boot.js +34 -0
- package/lib/commands.d.ts +49 -0
- package/lib/commands.js +115 -0
- package/lib/contract.d.ts +1973 -0
- package/lib/contract.js +36 -0
- package/lib/core/agent-roles.d.ts +52 -0
- package/lib/core/agent-roles.js +274 -0
- package/lib/core/approval-floor.d.ts +183 -0
- package/lib/core/approval-floor.js +679 -0
- package/lib/core/approval.d.ts +42 -0
- package/lib/core/approval.js +8 -0
- package/lib/core/audit.d.ts +61 -0
- package/lib/core/audit.js +59 -0
- package/lib/core/boot-notices.d.ts +6 -0
- package/lib/core/boot-notices.js +61 -0
- package/lib/core/cdp/actions.d.ts +182 -0
- package/lib/core/cdp/actions.js +649 -0
- package/lib/core/cdp/launch.d.ts +192 -0
- package/lib/core/cdp/launch.js +491 -0
- package/lib/core/cdp/snapshot.d.ts +139 -0
- package/lib/core/cdp/snapshot.js +316 -0
- package/lib/core/cdp/transport.d.ts +160 -0
- package/lib/core/cdp/transport.js +359 -0
- package/lib/core/command-policy.d.ts +108 -0
- package/lib/core/command-policy.js +580 -0
- package/lib/core/commands-completion.d.ts +78 -0
- package/lib/core/commands-completion.js +167 -0
- package/lib/core/compact-anchors.d.ts +76 -0
- package/lib/core/compact-anchors.js +284 -0
- package/lib/core/compact.d.ts +110 -0
- package/lib/core/compact.js +210 -0
- package/lib/core/config-store.d.ts +38 -0
- package/lib/core/config-store.js +307 -0
- package/lib/core/config.d.ts +36 -0
- package/lib/core/config.js +124 -0
- package/lib/core/connection-test.d.ts +60 -0
- package/lib/core/connection-test.js +84 -0
- package/lib/core/desktop-control.d.ts +298 -0
- package/lib/core/desktop-control.js +1311 -0
- package/lib/core/diff-text.d.ts +74 -0
- package/lib/core/diff-text.js +257 -0
- package/lib/core/dock-browser.d.ts +137 -0
- package/lib/core/dock-browser.js +146 -0
- package/lib/core/dock-ops.d.ts +59 -0
- package/lib/core/dock-ops.js +104 -0
- package/lib/core/dock-session.d.ts +39 -0
- package/lib/core/dock-session.js +28 -0
- package/lib/core/dsh-compat/hook.d.ts +18 -0
- package/lib/core/dsh-compat/hook.js +52 -0
- package/lib/core/dsh-compat/resolve-hook.d.ts +5 -0
- package/lib/core/dsh-compat/resolve-hook.js +59 -0
- package/lib/core/dsh-compat/tools-facade.d.ts +47 -0
- package/lib/core/dsh-compat/tools-facade.js +108 -0
- package/lib/core/err-text.d.ts +13 -0
- package/lib/core/err-text.js +15 -0
- package/lib/core/events.d.ts +149 -0
- package/lib/core/events.js +1 -0
- package/lib/core/git-exec.d.ts +23 -0
- package/lib/core/git-exec.js +72 -0
- package/lib/core/git-info.d.ts +47 -0
- package/lib/core/git-info.js +157 -0
- package/lib/core/goal.d.ts +89 -0
- package/lib/core/goal.js +189 -0
- package/lib/core/hooks.d.ts +272 -0
- package/lib/core/hooks.js +818 -0
- package/lib/core/host-browser-link.d.ts +59 -0
- package/lib/core/host-browser-link.js +9 -0
- package/lib/core/host-methods.d.ts +25 -0
- package/lib/core/host-methods.js +103 -0
- package/lib/core/image-diet.d.ts +77 -0
- package/lib/core/image-diet.js +127 -0
- package/lib/core/learnings/ledger.d.ts +73 -0
- package/lib/core/learnings/ledger.js +273 -0
- package/lib/core/learnings/skill-write.d.ts +250 -0
- package/lib/core/learnings/skill-write.js +854 -0
- package/lib/core/learnings/store.d.ts +102 -0
- package/lib/core/learnings/store.js +266 -0
- package/lib/core/lifecycle-hooks.d.ts +189 -0
- package/lib/core/lifecycle-hooks.js +576 -0
- package/lib/core/llm.d.ts +272 -0
- package/lib/core/llm.js +548 -0
- package/lib/core/lockfile.d.ts +17 -0
- package/lib/core/lockfile.js +110 -0
- package/lib/core/loop.d.ts +185 -0
- package/lib/core/loop.js +587 -0
- package/lib/core/lsp/client.d.ts +189 -0
- package/lib/core/lsp/client.js +690 -0
- package/lib/core/lsp/framing.d.ts +84 -0
- package/lib/core/lsp/framing.js +181 -0
- package/lib/core/lsp/line-shift.d.ts +28 -0
- package/lib/core/lsp/line-shift.js +127 -0
- package/lib/core/lsp/manager.d.ts +93 -0
- package/lib/core/lsp/manager.js +418 -0
- package/lib/core/lsp/normalize.d.ts +32 -0
- package/lib/core/lsp/normalize.js +139 -0
- package/lib/core/lsp/servers.d.ts +106 -0
- package/lib/core/lsp/servers.js +352 -0
- package/lib/core/lsp/uri.d.ts +76 -0
- package/lib/core/lsp/uri.js +179 -0
- package/lib/core/market.d.ts +51 -0
- package/lib/core/market.js +358 -0
- package/lib/core/mcp.d.ts +201 -0
- package/lib/core/mcp.js +836 -0
- package/lib/core/memory-eval.d.ts +158 -0
- package/lib/core/memory-eval.js +215 -0
- package/lib/core/memory.d.ts +266 -0
- package/lib/core/memory.js +703 -0
- package/lib/core/mention.d.ts +57 -0
- package/lib/core/mention.js +129 -0
- package/lib/core/microcompact.d.ts +48 -0
- package/lib/core/microcompact.js +79 -0
- package/lib/core/migrate.d.ts +56 -0
- package/lib/core/migrate.js +134 -0
- package/lib/core/model-caps.d.ts +83 -0
- package/lib/core/model-caps.js +150 -0
- package/lib/core/model-discovery.d.ts +19 -0
- package/lib/core/model-discovery.js +69 -0
- package/lib/core/modes.d.ts +72 -0
- package/lib/core/modes.js +152 -0
- package/lib/core/native-bridge.d.ts +135 -0
- package/lib/core/native-bridge.js +563 -0
- package/lib/core/path-policy.d.ts +32 -0
- package/lib/core/path-policy.js +248 -0
- package/lib/core/permission-presets.d.ts +67 -0
- package/lib/core/permission-presets.js +75 -0
- package/lib/core/plugin-loader.d.ts +36 -0
- package/lib/core/plugin-loader.js +294 -0
- package/lib/core/plugin-registry.d.ts +113 -0
- package/lib/core/plugin-registry.js +256 -0
- package/lib/core/prefs.d.ts +82 -0
- package/lib/core/prefs.js +372 -0
- package/lib/core/presets.d.ts +80 -0
- package/lib/core/presets.js +373 -0
- package/lib/core/pricing.d.ts +90 -0
- package/lib/core/pricing.js +126 -0
- package/lib/core/prompt.d.ts +92 -0
- package/lib/core/prompt.js +339 -0
- package/lib/core/ptc.d.ts +66 -0
- package/lib/core/ptc.js +70 -0
- package/lib/core/remote/frames.d.ts +91 -0
- package/lib/core/remote/frames.js +174 -0
- package/lib/core/remote/notify.d.ts +54 -0
- package/lib/core/remote/notify.js +77 -0
- package/lib/core/remote/owner.d.ts +32 -0
- package/lib/core/remote/owner.js +75 -0
- package/lib/core/remote/pairing.d.ts +104 -0
- package/lib/core/remote/pairing.js +261 -0
- package/lib/core/remote/push.d.ts +80 -0
- package/lib/core/remote/push.js +222 -0
- package/lib/core/remote/store.d.ts +18 -0
- package/lib/core/remote/store.js +59 -0
- package/lib/core/remote/tickets.d.ts +20 -0
- package/lib/core/remote/tickets.js +49 -0
- package/lib/core/remote/uploads.d.ts +63 -0
- package/lib/core/remote/uploads.js +161 -0
- package/lib/core/request-attribution.d.ts +26 -0
- package/lib/core/request-attribution.js +86 -0
- package/lib/core/request-invariant.d.ts +61 -0
- package/lib/core/request-invariant.js +199 -0
- package/lib/core/sandbox/backends.d.ts +64 -0
- package/lib/core/sandbox/backends.js +194 -0
- package/lib/core/sandbox/execpolicy.d.ts +93 -0
- package/lib/core/sandbox/execpolicy.js +452 -0
- package/lib/core/sandbox/net-proxy.d.ts +31 -0
- package/lib/core/sandbox/net-proxy.js +162 -0
- package/lib/core/sandbox/policy.d.ts +211 -0
- package/lib/core/sandbox/policy.js +559 -0
- package/lib/core/sandbox/win/abi.d.ts +161 -0
- package/lib/core/sandbox/win/abi.js +169 -0
- package/lib/core/sandbox/win/account.d.ts +71 -0
- package/lib/core/sandbox/win/account.js +206 -0
- package/lib/core/sandbox/win/acl.d.ts +54 -0
- package/lib/core/sandbox/win/acl.js +339 -0
- package/lib/core/sandbox/win/backend.d.ts +61 -0
- package/lib/core/sandbox/win/backend.js +249 -0
- package/lib/core/sandbox/win/doctor.d.ts +24 -0
- package/lib/core/sandbox/win/doctor.js +272 -0
- package/lib/core/sandbox/win/ffi.d.ts +107 -0
- package/lib/core/sandbox/win/ffi.js +166 -0
- package/lib/core/sandbox/win/net-abi.d.ts +154 -0
- package/lib/core/sandbox/win/net-abi.js +316 -0
- package/lib/core/sandbox/win/runner-entry.d.ts +35 -0
- package/lib/core/sandbox/win/runner-entry.js +391 -0
- package/lib/core/sandbox/win/setup.d.ts +60 -0
- package/lib/core/sandbox/win/setup.js +986 -0
- package/lib/core/sandbox/win/token.d.ts +35 -0
- package/lib/core/sandbox/win/token.js +238 -0
- package/lib/core/sandbox/win/wfp.d.ts +41 -0
- package/lib/core/sandbox/win/wfp.js +200 -0
- package/lib/core/sandbox/win/workspace-sid.d.ts +9 -0
- package/lib/core/sandbox/win/workspace-sid.js +45 -0
- package/lib/core/schedule/rule.d.ts +184 -0
- package/lib/core/schedule/rule.js +745 -0
- package/lib/core/schedule/runner.d.ts +185 -0
- package/lib/core/schedule/runner.js +678 -0
- package/lib/core/schedule/store.d.ts +182 -0
- package/lib/core/schedule/store.js +455 -0
- package/lib/core/secrets.d.ts +41 -0
- package/lib/core/secrets.js +150 -0
- package/lib/core/session-export.d.ts +18 -0
- package/lib/core/session-export.js +73 -0
- package/lib/core/session-index.d.ts +159 -0
- package/lib/core/session-index.js +683 -0
- package/lib/core/session-meta.d.ts +41 -0
- package/lib/core/session-meta.js +97 -0
- package/lib/core/session.d.ts +356 -0
- package/lib/core/session.js +998 -0
- package/lib/core/skills.d.ts +60 -0
- package/lib/core/skills.js +171 -0
- package/lib/core/spill.d.ts +159 -0
- package/lib/core/spill.js +329 -0
- package/lib/core/surface-registry.d.ts +45 -0
- package/lib/core/surface-registry.js +67 -0
- package/lib/core/team-board.d.ts +95 -0
- package/lib/core/team-board.js +316 -0
- package/lib/core/throughput.d.ts +46 -0
- package/lib/core/throughput.js +40 -0
- package/lib/core/todo-reminder.d.ts +44 -0
- package/lib/core/todo-reminder.js +55 -0
- package/lib/core/todo.d.ts +81 -0
- package/lib/core/todo.js +222 -0
- package/lib/core/token-estimate.d.ts +55 -0
- package/lib/core/token-estimate.js +82 -0
- package/lib/core/tool-guards.d.ts +103 -0
- package/lib/core/tool-guards.js +77 -0
- package/lib/core/tool-search.d.ts +152 -0
- package/lib/core/tool-search.js +311 -0
- package/lib/core/tools/bash.d.ts +281 -0
- package/lib/core/tools/bash.js +871 -0
- package/lib/core/tools/command-runner.d.ts +84 -0
- package/lib/core/tools/command-runner.js +68 -0
- package/lib/core/tools/fs-tools.d.ts +23 -0
- package/lib/core/tools/fs-tools.js +247 -0
- package/lib/core/tools/index.d.ts +8 -0
- package/lib/core/tools/index.js +4 -0
- package/lib/core/tools/sandbox-args.d.ts +51 -0
- package/lib/core/tools/sandbox-args.js +90 -0
- package/lib/core/tools/search-tools.d.ts +3 -0
- package/lib/core/tools/search-tools.js +153 -0
- package/lib/core/tools.d.ts +96 -0
- package/lib/core/tools.js +53 -0
- package/lib/core/ui-boot-source.d.ts +71 -0
- package/lib/core/ui-boot-source.js +121 -0
- package/lib/core/ui-bundles.d.ts +249 -0
- package/lib/core/ui-bundles.js +830 -0
- package/lib/core/ui-combo.d.ts +49 -0
- package/lib/core/ui-combo.js +55 -0
- package/lib/core/ui-events.d.ts +81 -0
- package/lib/core/ui-events.js +364 -0
- package/lib/core/ui-hmr.d.ts +36 -0
- package/lib/core/ui-hmr.js +128 -0
- package/lib/core/ui-methods.d.ts +69 -0
- package/lib/core/ui-methods.js +341 -0
- package/lib/core/ui-route.d.ts +69 -0
- package/lib/core/ui-route.js +122 -0
- package/lib/core/ui-server.d.ts +75 -0
- package/lib/core/ui-server.js +369 -0
- package/lib/core/ui-trust.d.ts +47 -0
- package/lib/core/ui-trust.js +164 -0
- package/lib/core/untrusted.d.ts +30 -0
- package/lib/core/untrusted.js +48 -0
- package/lib/core/update-check.d.ts +81 -0
- package/lib/core/update-check.js +177 -0
- package/lib/core/usage-log.d.ts +82 -0
- package/lib/core/usage-log.js +306 -0
- package/lib/core/version.d.ts +1 -0
- package/lib/core/version.js +37 -0
- package/lib/core/web-http.d.ts +57 -0
- package/lib/core/web-http.js +137 -0
- package/lib/core/web-static.d.ts +31 -0
- package/lib/core/web-static.js +75 -0
- package/lib/headless.d.ts +1 -0
- package/lib/headless.js +31 -0
- package/lib/host/kernel.d.ts +33 -0
- package/lib/host/kernel.js +451 -0
- package/lib/index.d.ts +41 -0
- package/lib/index.js +35 -0
- package/lib/plugins/agent.d.ts +2 -0
- package/lib/plugins/agent.js +274 -0
- package/lib/plugins/approval-floor.d.ts +2 -0
- package/lib/plugins/approval-floor.js +223 -0
- package/lib/plugins/approval.d.ts +16 -0
- package/lib/plugins/approval.js +674 -0
- package/lib/plugins/ask.d.ts +2 -0
- package/lib/plugins/ask.js +245 -0
- package/lib/plugins/browser.d.ts +68 -0
- package/lib/plugins/browser.js +1341 -0
- package/lib/plugins/commands.d.ts +23 -0
- package/lib/plugins/commands.js +498 -0
- package/lib/plugins/compact.d.ts +21 -0
- package/lib/plugins/compact.js +274 -0
- package/lib/plugins/computer-use.d.ts +47 -0
- package/lib/plugins/computer-use.js +539 -0
- package/lib/plugins/desktop-dock.d.ts +2 -0
- package/lib/plugins/desktop-dock.js +626 -0
- package/lib/plugins/dock-browser.d.ts +34 -0
- package/lib/plugins/dock-browser.js +765 -0
- package/lib/plugins/dsh-compat.d.ts +25 -0
- package/lib/plugins/dsh-compat.js +37 -0
- package/lib/plugins/file-review.d.ts +108 -0
- package/lib/plugins/file-review.js +393 -0
- package/lib/plugins/git.d.ts +26 -0
- package/lib/plugins/git.js +132 -0
- package/lib/plugins/goal.d.ts +20 -0
- package/lib/plugins/goal.js +245 -0
- package/lib/plugins/guards.d.ts +14 -0
- package/lib/plugins/guards.js +18 -0
- package/lib/plugins/hooks.d.ts +19 -0
- package/lib/plugins/hooks.js +583 -0
- package/lib/plugins/host-stdio.d.ts +88 -0
- package/lib/plugins/host-stdio.js +317 -0
- package/lib/plugins/image-diet.d.ts +17 -0
- package/lib/plugins/image-diet.js +85 -0
- package/lib/plugins/lifecycle-hooks.d.ts +26 -0
- package/lib/plugins/lifecycle-hooks.js +338 -0
- package/lib/plugins/llm.d.ts +13 -0
- package/lib/plugins/llm.js +215 -0
- package/lib/plugins/lsp.d.ts +54 -0
- package/lib/plugins/lsp.js +608 -0
- package/lib/plugins/mcp.d.ts +16 -0
- package/lib/plugins/mcp.js +673 -0
- package/lib/plugins/memory.d.ts +17 -0
- package/lib/plugins/memory.js +858 -0
- package/lib/plugins/microcompact.d.ts +18 -0
- package/lib/plugins/microcompact.js +119 -0
- package/lib/plugins/mode.d.ts +18 -0
- package/lib/plugins/mode.js +115 -0
- package/lib/plugins/permission-presets.d.ts +18 -0
- package/lib/plugins/permission-presets.js +135 -0
- package/lib/plugins/plan.d.ts +2 -0
- package/lib/plugins/plan.js +192 -0
- package/lib/plugins/plugin-manager.d.ts +2 -0
- package/lib/plugins/plugin-manager.js +165 -0
- package/lib/plugins/presets.d.ts +2 -0
- package/lib/plugins/presets.js +182 -0
- package/lib/plugins/prompt.d.ts +2 -0
- package/lib/plugins/prompt.js +264 -0
- package/lib/plugins/ptc.d.ts +2 -0
- package/lib/plugins/ptc.js +272 -0
- package/lib/plugins/remote/http.d.ts +11 -0
- package/lib/plugins/remote/http.js +11 -0
- package/lib/plugins/remote/routes.d.ts +24 -0
- package/lib/plugins/remote/routes.js +384 -0
- package/lib/plugins/remote/types.d.ts +135 -0
- package/lib/plugins/remote/types.js +87 -0
- package/lib/plugins/remote.d.ts +17 -0
- package/lib/plugins/remote.js +1028 -0
- package/lib/plugins/runtime-api.d.ts +16 -0
- package/lib/plugins/runtime-api.js +99 -0
- package/lib/plugins/runtime.d.ts +10 -0
- package/lib/plugins/runtime.js +451 -0
- package/lib/plugins/sandbox.d.ts +2 -0
- package/lib/plugins/sandbox.js +761 -0
- package/lib/plugins/schedule.d.ts +26 -0
- package/lib/plugins/schedule.js +538 -0
- package/lib/plugins/self-improve.d.ts +61 -0
- package/lib/plugins/self-improve.js +953 -0
- package/lib/plugins/session-search.d.ts +16 -0
- package/lib/plugins/session-search.js +308 -0
- package/lib/plugins/session-title.d.ts +19 -0
- package/lib/plugins/session-title.js +109 -0
- package/lib/plugins/session.d.ts +7 -0
- package/lib/plugins/session.js +296 -0
- package/lib/plugins/settings.d.ts +3 -0
- package/lib/plugins/settings.js +705 -0
- package/lib/plugins/skills.d.ts +2 -0
- package/lib/plugins/skills.js +360 -0
- package/lib/plugins/spill.d.ts +16 -0
- package/lib/plugins/spill.js +200 -0
- package/lib/plugins/subagent.d.ts +2 -0
- package/lib/plugins/subagent.js +862 -0
- package/lib/plugins/surfaces.d.ts +11 -0
- package/lib/plugins/surfaces.js +20 -0
- package/lib/plugins/team.d.ts +16 -0
- package/lib/plugins/team.js +149 -0
- package/lib/plugins/todo.d.ts +19 -0
- package/lib/plugins/todo.js +188 -0
- package/lib/plugins/tool-search.d.ts +2 -0
- package/lib/plugins/tool-search.js +425 -0
- package/lib/plugins/tools-default.d.ts +12 -0
- package/lib/plugins/tools-default.js +275 -0
- package/lib/plugins/tools.d.ts +16 -0
- package/lib/plugins/tools.js +62 -0
- package/lib/plugins/transcript.d.ts +15 -0
- package/lib/plugins/transcript.js +311 -0
- package/lib/plugins/tui.d.ts +2 -0
- package/lib/plugins/tui.js +98 -0
- package/lib/plugins/waiting.d.ts +12 -0
- package/lib/plugins/waiting.js +34 -0
- package/lib/plugins/web-search.d.ts +14 -0
- package/lib/plugins/web-search.js +263 -0
- package/lib/plugins/webserver.d.ts +48 -0
- package/lib/plugins/webserver.js +228 -0
- package/lib/remote/assets/app-DAcUCOEn.js +14 -0
- package/lib/remote/assets/icon-192.png +0 -0
- package/lib/remote/assets/icon-512.png +0 -0
- package/lib/remote/assets/index.html +34 -0
- package/lib/remote/assets/manifest.json +27 -0
- package/lib/remote/assets/style-DIB6729x.css +1 -0
- package/lib/remote/assets/sw.js +66 -0
- package/lib/services/types.d.ts +1228 -0
- package/lib/services/types.js +1 -0
- package/lib/tools/config-cli.d.ts +1 -0
- package/lib/tools/config-cli.js +89 -0
- package/lib/ui/arrival.d.ts +72 -0
- package/lib/ui/arrival.js +105 -0
- package/lib/ui/assets/app-B70nVIKd.js +69 -0
- package/lib/ui/assets/chunk-2UxHyX5q.js +1 -0
- package/lib/ui/assets/chunk-B1dDrJ26.js +1 -0
- package/lib/ui/assets/chunk-B6aJPvgy.js +1 -0
- package/lib/ui/assets/chunk-BEwlwnbL.js +1 -0
- package/lib/ui/assets/chunk-BGcgwlCI.js +12 -0
- package/lib/ui/assets/chunk-BIGW1oBm.js +1 -0
- package/lib/ui/assets/chunk-BPQ3VLAy.js +1 -0
- package/lib/ui/assets/chunk-BRH0KZbk.js +141 -0
- package/lib/ui/assets/chunk-BX6H_ySx.js +1 -0
- package/lib/ui/assets/chunk-B_P3UbPa.js +1 -0
- package/lib/ui/assets/chunk-B_TYUkTI.js +1 -0
- package/lib/ui/assets/chunk-B_vNuMnf.js +1 -0
- package/lib/ui/assets/chunk-BajyYBss.js +10 -0
- package/lib/ui/assets/chunk-BdnUsdx6.js +1 -0
- package/lib/ui/assets/chunk-Buea-lGh.js +1 -0
- package/lib/ui/assets/chunk-C0PvZCCE.js +1 -0
- package/lib/ui/assets/chunk-C0m74zRD.js +1 -0
- package/lib/ui/assets/chunk-C27-OAKa.js +1 -0
- package/lib/ui/assets/chunk-C2oV4EkX.js +1 -0
- package/lib/ui/assets/chunk-C7zT0LnQ.js +1 -0
- package/lib/ui/assets/chunk-C9GSunVT.js +1 -0
- package/lib/ui/assets/chunk-C9tS-k6U.js +1 -0
- package/lib/ui/assets/chunk-CHLpvVh8.js +1 -0
- package/lib/ui/assets/chunk-CLxacb5B.js +1 -0
- package/lib/ui/assets/chunk-COt5Ahok.js +1 -0
- package/lib/ui/assets/chunk-CPTIWCL2.js +1 -0
- package/lib/ui/assets/chunk-CQLl-L-p.js +1 -0
- package/lib/ui/assets/chunk-CUWRgxuq.js +55 -0
- package/lib/ui/assets/chunk-CWanVSXC.js +1 -0
- package/lib/ui/assets/chunk-Cf5RLm7j.js +1 -0
- package/lib/ui/assets/chunk-CickPsom.js +1 -0
- package/lib/ui/assets/chunk-Cj5Yp3dK.js +1 -0
- package/lib/ui/assets/chunk-Cmh6b_Ma.js +1 -0
- package/lib/ui/assets/chunk-Cp-IABpG.js +1 -0
- package/lib/ui/assets/chunk-CqE71os6.js +1 -0
- package/lib/ui/assets/chunk-CtKP3W-j.js +1 -0
- package/lib/ui/assets/chunk-Cvjx9yec.js +1 -0
- package/lib/ui/assets/chunk-CxGP3dtY.js +1 -0
- package/lib/ui/assets/chunk-CylS5w8V.js +1 -0
- package/lib/ui/assets/chunk-D38fbIBN.js +1 -0
- package/lib/ui/assets/chunk-D97Zzqfu.js +1 -0
- package/lib/ui/assets/chunk-DAi9KRSo.js +1 -0
- package/lib/ui/assets/chunk-DHK4yRfg.js +1 -0
- package/lib/ui/assets/chunk-DJeabLx-.js +1 -0
- package/lib/ui/assets/chunk-DNRxmY5a.js +1 -0
- package/lib/ui/assets/chunk-DNSN1z5r.js +1 -0
- package/lib/ui/assets/chunk-DNasK_ZG.js +1 -0
- package/lib/ui/assets/chunk-DPvrY3IQ.js +1 -0
- package/lib/ui/assets/chunk-DXmwc3jG.js +1 -0
- package/lib/ui/assets/chunk-Dd0kTxKP.js +1 -0
- package/lib/ui/assets/chunk-De0Btta0.js +1 -0
- package/lib/ui/assets/chunk-Des-eS-w.js +1 -0
- package/lib/ui/assets/chunk-Df6bDoY_.js +1 -0
- package/lib/ui/assets/chunk-DijEV5ha.js +1 -0
- package/lib/ui/assets/chunk-DkvoVjww.js +1 -0
- package/lib/ui/assets/chunk-DlC-OlHO.js +1 -0
- package/lib/ui/assets/chunk-DnzrXTVv.js +1 -0
- package/lib/ui/assets/chunk-Dualf0xi.js +1 -0
- package/lib/ui/assets/chunk-EXT84bkm.js +1 -0
- package/lib/ui/assets/chunk-GwR3hkPK.js +1 -0
- package/lib/ui/assets/chunk-P80f7IUj.js +1 -0
- package/lib/ui/assets/chunk-VOosw3JB.js +1 -0
- package/lib/ui/assets/chunk-YQ4lDkt_.js +140 -0
- package/lib/ui/assets/chunk-Yzrsuije.js +1 -0
- package/lib/ui/assets/chunk-Z72CF80r.js +1 -0
- package/lib/ui/assets/chunk-g9-lgVsj.js +1 -0
- package/lib/ui/assets/chunk-vGWfd6FD.js +1 -0
- package/lib/ui/assets/chunk-wDzz0qaB.js +1 -0
- package/lib/ui/assets/chunk-ySF7HWmS.js +1 -0
- package/lib/ui/assets/icon.png +0 -0
- package/lib/ui/assets/index.html +14 -0
- package/lib/ui/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/lib/ui/assets/style-Bq01xH_b.css +1 -0
- package/lib/ui/frames.d.ts +67 -0
- package/lib/ui/frames.js +146 -0
- package/lib/ui/index.d.ts +59 -0
- package/lib/ui/index.js +50 -0
- package/lib/ui/manifest.d.ts +172 -0
- package/lib/ui/manifest.js +158 -0
- package/lib/ui/plugin.d.ts +161 -0
- package/lib/ui/plugin.js +7 -0
- package/lib/ui/plugins/browser/client.js +290 -0
- package/lib/ui/plugins/browser/icon.png +0 -0
- package/lib/ui/plugins/composer/client.js +1843 -0
- package/lib/ui/plugins/composer/icon.png +0 -0
- package/lib/ui/plugins/files/client.js +646 -0
- package/lib/ui/plugins/files/icon.png +0 -0
- package/lib/ui/plugins/manifest.json +107 -0
- package/lib/ui/plugins/plugins/client.js +682 -0
- package/lib/ui/plugins/plugins/icon.png +0 -0
- package/lib/ui/plugins/preview/client.js +197 -0
- package/lib/ui/plugins/preview/icon.png +0 -0
- package/lib/ui/plugins/prompt/client.js +653 -0
- package/lib/ui/plugins/prompt/icon.png +0 -0
- package/lib/ui/plugins/sessions/client.js +1577 -0
- package/lib/ui/plugins/sessions/icon.png +0 -0
- package/lib/ui/plugins/settings/client.js +5470 -0
- package/lib/ui/plugins/settings/icon.png +0 -0
- package/lib/ui/plugins/skills/client.js +485 -0
- package/lib/ui/plugins/skills/icon.png +0 -0
- package/lib/ui/plugins/status/client.js +1420 -0
- package/lib/ui/plugins/status/icon.png +0 -0
- package/lib/ui/plugins/statusbar/client.js +504 -0
- package/lib/ui/plugins/statusbar/icon.png +0 -0
- package/lib/ui/plugins/tasks/client.js +52 -0
- package/lib/ui/plugins/tasks/icon.png +0 -0
- package/lib/ui/plugins/team/client.js +527 -0
- package/lib/ui/plugins/team/icon.png +0 -0
- package/lib/ui/plugins/terminal/client.js +232 -0
- package/lib/ui/plugins/terminal/icon.png +0 -0
- package/lib/ui/plugins/trace/client.js +1376 -0
- package/lib/ui/plugins/trace/icon.png +0 -0
- package/lib/ui/plugins/transcript/client.js +2661 -0
- package/lib/ui/plugins/transcript/icon.png +0 -0
- package/lib/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/scoped-slots.js +21 -0
- package/lib/ui/sdk/@dsc/ui/index.d.ts +242 -0
- package/lib/ui/sdk/@dsc/ui/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/contract.d.ts +1973 -0
- package/lib/ui/sdk/@dsc/ui-core/core/diff-text.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-browser.d.ts +137 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-ops.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/core/model-caps.d.ts +83 -0
- package/lib/ui/sdk/@dsc/ui-core/core/native-bridge.d.ts +135 -0
- package/lib/ui/sdk/@dsc/ui-core/core/token-estimate.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-core/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/arrival.d.ts +72 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/frames.d.ts +67 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/index.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/manifest.d.ts +172 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/plugin.d.ts +161 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services-react.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services.d.ts +63 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots-react.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots.d.ts +481 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/store.d.ts +89 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-model.d.ts +238 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-state.d.ts +163 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/index.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/app.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-layout/appearance.d.ts +70 -0
- package/lib/ui/sdk/@dsc/ui-layout/host-channel.d.ts +112 -0
- package/lib/ui/sdk/@dsc/ui-layout/index.d.ts +34 -0
- package/lib/ui/sdk/@dsc/ui-layout/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/page-state.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-layout/review-pane.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-layout/runtime-context.d.ts +44 -0
- package/lib/ui/sdk/@dsc/ui-layout/thread-view.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-layout/update-button.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-file-icon-artwork.d.ts +53 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-view.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/compat.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-primitives/completion-sound.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/confirm.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/css.d.ts +47 -0
- package/lib/ui/sdk/@dsc/ui-primitives/diff-rows.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-icons.d.ts +38 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-classify.d.ts +66 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-icon.d.ts +57 -0
- package/lib/ui/sdk/@dsc/ui-primitives/highlight.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/hover-delay.d.ts +17 -0
- package/lib/ui/sdk/@dsc/ui-primitives/icons.d.ts +109 -0
- package/lib/ui/sdk/@dsc/ui-primitives/index.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-primitives/lang-table.d.ts +10 -0
- package/lib/ui/sdk/@dsc/ui-primitives/markdown.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/native.d.ts +41 -0
- package/lib/ui/sdk/@dsc/ui-primitives/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/panels.d.ts +86 -0
- package/lib/ui/sdk/@dsc/ui-primitives/paths.d.ts +11 -0
- package/lib/ui/sdk/@dsc/ui-primitives/pdf-view.d.ts +13 -0
- package/lib/ui/sdk/@dsc/ui-primitives/plugin-text.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/select.d.ts +35 -0
- package/lib/ui/sdk/@dsc/ui-primitives/table-view.d.ts +23 -0
- package/lib/ui/sdk/@dsc/ui-primitives/thread-resizer.d.ts +22 -0
- package/lib/ui/sdk/@dsc/ui-primitives/time.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/toast.d.ts +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tokens.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-args.d.ts +49 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-summary.d.ts +64 -0
- package/lib/ui/sdk/@dsc/ui-renderer/boot.d.ts +121 -0
- package/lib/ui/sdk/@dsc/ui-renderer/hmr.d.ts +43 -0
- package/lib/ui/sdk/@dsc/ui-renderer/index.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-renderer/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/index.d.ts +19 -0
- package/lib/ui/sdk/@dsc/ui-slots/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots-react.d.ts +48 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots.d.ts +102 -0
- package/lib/ui/services-react.d.ts +27 -0
- package/lib/ui/services-react.js +32 -0
- package/lib/ui/services.d.ts +63 -0
- package/lib/ui/services.js +121 -0
- package/lib/ui/slots-ink.d.ts +26 -0
- package/lib/ui/slots-ink.js +78 -0
- package/lib/ui/slots-react.d.ts +28 -0
- package/lib/ui/slots-react.js +88 -0
- package/lib/ui/slots.d.ts +481 -0
- package/lib/ui/slots.js +550 -0
- package/lib/ui/store.d.ts +89 -0
- package/lib/ui/store.js +62 -0
- package/lib/ui/tui-plugin-api.d.ts +23 -0
- package/lib/ui/tui-plugin-api.js +23 -0
- package/lib/ui/tui-plugins.d.ts +47 -0
- package/lib/ui/tui-plugins.js +220 -0
- package/package.json +58 -6
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配对:桌面端给一张 8 位码(半小时内有效),手机输码换一个设备 token,之后凭 token 换 WS 票据。
|
|
3
|
+
*
|
|
4
|
+
* 三条硬规矩:
|
|
5
|
+
* 1. **明文码永不落盘、不进日志**。落盘的是 `sha256(盐 + 码)` 与随机盐;界面上那张卡
|
|
6
|
+
* 是码唯一的出口(action 的返回值,只显示在电脑那块屏幕上)。所以 transcript /
|
|
7
|
+
* dsc/notice 里也绝不能出现码——那些会写进会话 jsonl。
|
|
8
|
+
* 2. **一次性**。配对成功那张码当场作废,同一个码再输进来不会又发一个 token。
|
|
9
|
+
* 3. **错码限速**。同一张码连错 5 次锁 1 小时;锁住期间连对的码也不放行。
|
|
10
|
+
* 没有这一条,8 位码(32 字符表)在局域网里是可以被暴力试出来的。
|
|
11
|
+
*
|
|
12
|
+
* 设备侧只存 token 的 sha256,不存 token 本身:拿到 devices.json 也连不上。
|
|
13
|
+
*
|
|
14
|
+
* @module dsc/core/remote/pairing
|
|
15
|
+
*/
|
|
16
|
+
import { createHash, randomBytes, randomInt, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
17
|
+
import { readJsonFile, remoteFiles, writeJsonFile } from './store.js';
|
|
18
|
+
/**
|
|
19
|
+
* 配对码字母表:32 个字符,去掉 O/0/I/1/l 这些「看截图分不清」的。
|
|
20
|
+
* 大写 A–Z 去掉 I、O 剩 24 个,数字 2–9 八个,正好 32——所以每位的熵是 5 bit。
|
|
21
|
+
*/
|
|
22
|
+
export const CODE_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
|
23
|
+
/** 码长度(与 remote-web 的输入框长度一致,改这里要两边一起改)。 */
|
|
24
|
+
export const CODE_LENGTH = 8;
|
|
25
|
+
/** 码有效期半小时(30 分钟)。 */
|
|
26
|
+
export const CODE_TTL_MS = 1_800_000;
|
|
27
|
+
/** 同一张码允许连错几次。 */
|
|
28
|
+
export const MAX_CODE_ATTEMPTS = 5;
|
|
29
|
+
/** 连错超限后锁多久(与码的有效期无关,锁就是 1 小时)。 */
|
|
30
|
+
export const CODE_LOCK_MS = 3_600_000;
|
|
31
|
+
/** pending.json 里最多留几条记录(过期的会被清掉,正常只有一张活的)。 */
|
|
32
|
+
export const MAX_PENDING_RECORDS = 5;
|
|
33
|
+
/** 发一张码:`sha256(盐 + 码)` 落盘,明文只经返回值回到设置页。 */
|
|
34
|
+
function hashCode(salt, code) {
|
|
35
|
+
return createHash('sha256').update(`${salt}:${code}`).digest('hex');
|
|
36
|
+
}
|
|
37
|
+
/** 用户输进来的码先洗一遍:去掉空格与连字符、转大写(界面也做了,这里兜底)。 */
|
|
38
|
+
export function normalizeCode(input) {
|
|
39
|
+
return input.replace(/[^0-9a-zA-Z]/g, '').toUpperCase();
|
|
40
|
+
}
|
|
41
|
+
function tokenHashOf(token) {
|
|
42
|
+
return createHash('sha256').update(token).digest('hex');
|
|
43
|
+
}
|
|
44
|
+
/** 定长十六进制摘要的比较:长度不等直接 false,相等时用 timingSafeEqual。 */
|
|
45
|
+
function digestEquals(left, right) {
|
|
46
|
+
const a = Buffer.from(left, 'hex');
|
|
47
|
+
const b = Buffer.from(right, 'hex');
|
|
48
|
+
return a.length === b.length && a.length > 0 && timingSafeEqual(a, b);
|
|
49
|
+
}
|
|
50
|
+
export class RemotePairing {
|
|
51
|
+
files;
|
|
52
|
+
now;
|
|
53
|
+
/** 上次给某台设备刷 lastSeenAt 的时刻(每台一分钟最多写一次盘,别为每次重连都写文件)。 */
|
|
54
|
+
lastSeenWritten = new Map();
|
|
55
|
+
constructor(options = {}) {
|
|
56
|
+
this.files = remoteFiles(options.dir);
|
|
57
|
+
this.now = options.now ?? (() => Date.now());
|
|
58
|
+
}
|
|
59
|
+
// ── 配对码 ────────────────────────────────────────────────────────────────
|
|
60
|
+
loadCodes() {
|
|
61
|
+
const doc = readJsonFile(this.files.pending);
|
|
62
|
+
const list = doc?.['codes'];
|
|
63
|
+
if (!Array.isArray(list))
|
|
64
|
+
return [];
|
|
65
|
+
const out = [];
|
|
66
|
+
for (const item of list) {
|
|
67
|
+
if (item === null || typeof item !== 'object')
|
|
68
|
+
continue;
|
|
69
|
+
const record = item;
|
|
70
|
+
if (typeof record['salt'] !== 'string' || typeof record['hash'] !== 'string')
|
|
71
|
+
continue;
|
|
72
|
+
if (typeof record['createdAt'] !== 'number' || typeof record['expiresAt'] !== 'number')
|
|
73
|
+
continue;
|
|
74
|
+
out.push({
|
|
75
|
+
salt: record['salt'],
|
|
76
|
+
hash: record['hash'],
|
|
77
|
+
createdAt: record['createdAt'],
|
|
78
|
+
expiresAt: record['expiresAt'],
|
|
79
|
+
attempts: typeof record['attempts'] === 'number' ? record['attempts'] : 0,
|
|
80
|
+
...(typeof record['lockedUntil'] === 'number' ? { lockedUntil: record['lockedUntil'] } : {}),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
saveCodes(codes) {
|
|
86
|
+
writeJsonFile(this.files.pending, { version: 1, codes: codes.slice(-MAX_PENDING_RECORDS) });
|
|
87
|
+
}
|
|
88
|
+
/** 还没过期、也没被锁的码有几张(设置页显示状态用)。 */
|
|
89
|
+
liveCodeCount() {
|
|
90
|
+
const now = this.now();
|
|
91
|
+
return this.loadCodes().filter((code) => code.expiresAt > now && (code.lockedUntil ?? 0) <= now).length;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 发一张新码。
|
|
95
|
+
*
|
|
96
|
+
* @param options.replace - true = 先作废还没用掉的活码再发新的(「连接手机」弹窗与它
|
|
97
|
+
* 里面的「重新生成」走这条:明文码不落盘,旧码也重画不出来,界面要一直看得见一张码,
|
|
98
|
+
* 就只能换一张新的)。不传时保持一次一张的老语义——已经有一张没用过且没被锁的码时
|
|
99
|
+
* 返回 null,同一个屏幕上同时飘着两张有效码只会让人输错。
|
|
100
|
+
* @returns 新码与过期时刻;不传 replace 且已有活码时返回 null。
|
|
101
|
+
* 被锁的码不算「没用过」:它已经废了,用户当然可以再要一张。
|
|
102
|
+
*/
|
|
103
|
+
issueCode(options) {
|
|
104
|
+
const now = this.now();
|
|
105
|
+
// 顺手清掉过期与锁上的记录(锁上的永远用不了了,留着只会让状态显示看不懂)
|
|
106
|
+
const kept = this.loadCodes().filter((code) => code.expiresAt > now && (code.lockedUntil ?? 0) <= now);
|
|
107
|
+
if (kept.length > 0 && options?.replace !== true)
|
|
108
|
+
return null;
|
|
109
|
+
// replace:活码一条不留(它们对应的明文只在那张已经关掉的弹窗上,留着也验证不了)
|
|
110
|
+
if (options?.replace === true)
|
|
111
|
+
kept.length = 0;
|
|
112
|
+
const code = Array.from({ length: CODE_LENGTH }, () => CODE_ALPHABET[randomInt(CODE_ALPHABET.length)] ?? 'A').join('');
|
|
113
|
+
const salt = randomBytes(16).toString('hex');
|
|
114
|
+
const expiresAt = now + CODE_TTL_MS;
|
|
115
|
+
kept.push({ salt, hash: hashCode(salt, code), createdAt: now, expiresAt, attempts: 0 });
|
|
116
|
+
this.saveCodes(kept);
|
|
117
|
+
return { code, expiresAt };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 用码换 token。失败按状态码分三类:
|
|
121
|
+
* 401 没有可用的码 / 码不对(不区分是哪张,免得探测出「这个码存在但错了」)
|
|
122
|
+
* 429 这张码被锁着(输错太多次)
|
|
123
|
+
*/
|
|
124
|
+
verifyCode(input, name) {
|
|
125
|
+
const now = this.now();
|
|
126
|
+
const code = normalizeCode(input);
|
|
127
|
+
const all = this.loadCodes();
|
|
128
|
+
const live = all.filter((record) => record.expiresAt > now);
|
|
129
|
+
if (live.length === 0) {
|
|
130
|
+
return { ok: false, status: 401, error: '没有可用的配对码,请在电脑端「设置 → 远程控制」里生成一张' };
|
|
131
|
+
}
|
|
132
|
+
const locked = live.find((record) => (record.lockedUntil ?? 0) > now);
|
|
133
|
+
if (locked !== undefined) {
|
|
134
|
+
return { ok: false, status: 429, error: '这张配对码输错次数太多,已锁 1 小时;请在电脑端重新生成一张' };
|
|
135
|
+
}
|
|
136
|
+
const hit = live.find((record) => digestEquals(record.hash, hashCode(record.salt, code)));
|
|
137
|
+
if (hit === undefined) {
|
|
138
|
+
// 记一次失败:活着的码一起记(正常只有一张)。够 5 次就锁 1 小时。
|
|
139
|
+
for (const record of all) {
|
|
140
|
+
if (record.expiresAt <= now)
|
|
141
|
+
continue;
|
|
142
|
+
record.attempts += 1;
|
|
143
|
+
if (record.attempts >= MAX_CODE_ATTEMPTS)
|
|
144
|
+
record.lockedUntil = now + CODE_LOCK_MS;
|
|
145
|
+
}
|
|
146
|
+
this.saveCodes(all);
|
|
147
|
+
return { ok: false, status: 401, error: '配对码不对' };
|
|
148
|
+
}
|
|
149
|
+
// 成功:这张码当场作废,发一个 32 字节的设备 token
|
|
150
|
+
const token = randomBytes(32).toString('base64url');
|
|
151
|
+
const device = {
|
|
152
|
+
deviceId: randomUUID(),
|
|
153
|
+
tokenHash: tokenHashOf(token),
|
|
154
|
+
name: (name.trim() === '' ? '未命名设备' : name.trim()).slice(0, 32),
|
|
155
|
+
createdAt: now,
|
|
156
|
+
lastSeenAt: now,
|
|
157
|
+
};
|
|
158
|
+
this.saveCodes(all.filter((record) => record !== hit));
|
|
159
|
+
const devices = this.loadDevices();
|
|
160
|
+
// 同名设备再配一次:旧的顶掉,免得设置页里堆一排「我的手机」
|
|
161
|
+
this.saveDevices([...devices.filter((entry) => entry.name !== device.name), device]);
|
|
162
|
+
return { ok: true, token, deviceId: device.deviceId, name: device.name };
|
|
163
|
+
}
|
|
164
|
+
// ── 设备 token ────────────────────────────────────────────────────────────
|
|
165
|
+
loadDevices() {
|
|
166
|
+
const doc = readJsonFile(this.files.devices);
|
|
167
|
+
const list = doc?.['devices'];
|
|
168
|
+
if (!Array.isArray(list))
|
|
169
|
+
return [];
|
|
170
|
+
const out = [];
|
|
171
|
+
for (const item of list) {
|
|
172
|
+
if (item === null || typeof item !== 'object')
|
|
173
|
+
continue;
|
|
174
|
+
const record = item;
|
|
175
|
+
if (typeof record['tokenHash'] !== 'string')
|
|
176
|
+
continue;
|
|
177
|
+
out.push({
|
|
178
|
+
deviceId: typeof record['deviceId'] === 'string' ? record['deviceId'] : randomUUID(),
|
|
179
|
+
tokenHash: record['tokenHash'],
|
|
180
|
+
name: typeof record['name'] === 'string' ? record['name'] : '未命名设备',
|
|
181
|
+
createdAt: typeof record['createdAt'] === 'number' ? record['createdAt'] : 0,
|
|
182
|
+
lastSeenAt: typeof record['lastSeenAt'] === 'number' ? record['lastSeenAt'] : 0,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
saveDevices(devices) {
|
|
188
|
+
writeJsonFile(this.files.devices, { version: 1, devices });
|
|
189
|
+
}
|
|
190
|
+
/** 验设备 token;认不出返回 null。认得出就顺手刷一次 lastSeenAt(每台一分钟最多写一次盘)。 */
|
|
191
|
+
verifyToken(token) {
|
|
192
|
+
if (token === '')
|
|
193
|
+
return null;
|
|
194
|
+
const hash = tokenHashOf(token);
|
|
195
|
+
const devices = this.loadDevices();
|
|
196
|
+
const index = devices.findIndex((device) => digestEquals(device.tokenHash, hash));
|
|
197
|
+
if (index < 0)
|
|
198
|
+
return null;
|
|
199
|
+
const device = devices[index];
|
|
200
|
+
if (device === undefined)
|
|
201
|
+
return null;
|
|
202
|
+
const now = this.now();
|
|
203
|
+
const written = this.lastSeenWritten.get(device.deviceId) ?? 0;
|
|
204
|
+
if (now - written > 60_000) {
|
|
205
|
+
devices[index] = { ...device, lastSeenAt: now };
|
|
206
|
+
this.saveDevices(devices);
|
|
207
|
+
this.lastSeenWritten.set(device.deviceId, now);
|
|
208
|
+
}
|
|
209
|
+
return { ...device, lastSeenAt: now };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* 吊销一个 token。
|
|
213
|
+
* @returns 被吊销的那台设备(调用方拿 deviceId 去断开它已经建好的连接);
|
|
214
|
+
* 本来就不认这个 token 时返回 null。
|
|
215
|
+
*/
|
|
216
|
+
revoke(token) {
|
|
217
|
+
const hash = tokenHashOf(token);
|
|
218
|
+
const devices = this.loadDevices();
|
|
219
|
+
const hit = devices.find((device) => digestEquals(device.tokenHash, hash));
|
|
220
|
+
if (hit === undefined)
|
|
221
|
+
return null;
|
|
222
|
+
this.saveDevices(devices.filter((device) => device !== hit));
|
|
223
|
+
this.lastSeenWritten.delete(hit.deviceId);
|
|
224
|
+
return hit;
|
|
225
|
+
}
|
|
226
|
+
/** 吊销全部设备,返回吊销了几台。 */
|
|
227
|
+
revokeAll() {
|
|
228
|
+
const devices = this.loadDevices();
|
|
229
|
+
this.saveDevices([]);
|
|
230
|
+
this.lastSeenWritten.clear();
|
|
231
|
+
return devices.length;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 按 deviceId 吊销一台设备(设置页里每台设备一个「吊销」按钮走这条)。
|
|
235
|
+
*
|
|
236
|
+
* 为什么不是只留 `revoke(token)`:设置页手上只有 deviceId(token 本体从不落盘,
|
|
237
|
+
* 界面上也看不到),所以「逐台吊销」必须能按 identity 定位,而不是按凭据定位。
|
|
238
|
+
*
|
|
239
|
+
* @returns 被吊销的那台设备;本来就不在清单里时 null
|
|
240
|
+
*/
|
|
241
|
+
revokeDevice(deviceId) {
|
|
242
|
+
if (deviceId === '')
|
|
243
|
+
return null;
|
|
244
|
+
const devices = this.loadDevices();
|
|
245
|
+
const hit = devices.find((device) => device.deviceId === deviceId);
|
|
246
|
+
if (hit === undefined)
|
|
247
|
+
return null;
|
|
248
|
+
this.saveDevices(devices.filter((device) => device !== hit));
|
|
249
|
+
this.lastSeenWritten.delete(hit.deviceId);
|
|
250
|
+
return hit;
|
|
251
|
+
}
|
|
252
|
+
/** 已配对设备清单(给设置分区显示;不含 token 哈希)。 */
|
|
253
|
+
devices() {
|
|
254
|
+
return this.loadDevices().map((device) => ({
|
|
255
|
+
deviceId: device.deviceId,
|
|
256
|
+
name: device.name,
|
|
257
|
+
createdAt: device.createdAt,
|
|
258
|
+
lastSeenAt: device.lastSeenAt,
|
|
259
|
+
}));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** VAPID 的 subject:推送服务只要求它是一个 mailto 或 https 地址,不校验可达性。 */
|
|
2
|
+
export declare const PUSH_SUBJECT = "mailto:pneuma@localhost";
|
|
3
|
+
/** 一次推送的超时(推送服务不响应时别把这一轮卡住)。 */
|
|
4
|
+
export declare const PUSH_SEND_TIMEOUT_MS = 5000;
|
|
5
|
+
/** VAPID 密钥对。 */
|
|
6
|
+
export interface PushKeys {
|
|
7
|
+
publicKey: string;
|
|
8
|
+
privateKey: string;
|
|
9
|
+
}
|
|
10
|
+
/** 一条浏览器订阅(浏览器 `PushSubscription.toJSON()` 的形状 + 我们自己的归属信息)。 */
|
|
11
|
+
export interface PushSubscriptionRecord {
|
|
12
|
+
endpoint: string;
|
|
13
|
+
keys: {
|
|
14
|
+
p256dh: string;
|
|
15
|
+
auth: string;
|
|
16
|
+
};
|
|
17
|
+
/** 订阅时用的那台设备(deviceId,见 pairing.ts)。 */
|
|
18
|
+
deviceId: string;
|
|
19
|
+
createdAt: number;
|
|
20
|
+
}
|
|
21
|
+
/** 真正把一条推送发出去的那一步(自检脚本注入假的,不真打推送服务)。 */
|
|
22
|
+
export type PushSender = (subscription: PushSubscriptionRecord, payload: string) => Promise<void>;
|
|
23
|
+
/** 一次群发的结果。 */
|
|
24
|
+
export interface PushSendResult {
|
|
25
|
+
/** 库里当时有几条订阅。 */
|
|
26
|
+
attempted: number;
|
|
27
|
+
sent: number;
|
|
28
|
+
/** 404/410 当场清掉的条数。 */
|
|
29
|
+
dropped: number;
|
|
30
|
+
/** 其它失败的一句话(最多留前几条)。 */
|
|
31
|
+
errors: string[];
|
|
32
|
+
}
|
|
33
|
+
export type PushSubscribeOutcome = {
|
|
34
|
+
ok: true;
|
|
35
|
+
added: boolean;
|
|
36
|
+
total: number;
|
|
37
|
+
} | {
|
|
38
|
+
ok: false;
|
|
39
|
+
error: string;
|
|
40
|
+
};
|
|
41
|
+
export interface PushStoreOptions {
|
|
42
|
+
/** 数据目录(默认 `~/.dsc/remote`;自检脚本换成临时目录)。 */
|
|
43
|
+
dir?: string;
|
|
44
|
+
now?: () => number;
|
|
45
|
+
sender?: PushSender;
|
|
46
|
+
}
|
|
47
|
+
export declare class RemotePush {
|
|
48
|
+
private readonly files;
|
|
49
|
+
private readonly now;
|
|
50
|
+
private readonly sender;
|
|
51
|
+
private keysCache;
|
|
52
|
+
constructor(options?: PushStoreOptions);
|
|
53
|
+
/**
|
|
54
|
+
* 读 VAPID 密钥;没有就生成一份并原子落盘。
|
|
55
|
+
* @returns 密钥对;生成/落盘失败时抛(调用方按「Web Push 不可用」处理)
|
|
56
|
+
*/
|
|
57
|
+
keys(): PushKeys;
|
|
58
|
+
/** 给浏览器的 VAPID 公钥;取不到(web-push 坏了、磁盘写不动)时 null。 */
|
|
59
|
+
publicKey(): string | null;
|
|
60
|
+
/** 订阅清单(读不动、内容不是数组一律按空处理)。 */
|
|
61
|
+
subscriptions(): PushSubscriptionRecord[];
|
|
62
|
+
/** 库里现在有几条订阅。 */
|
|
63
|
+
count(): number;
|
|
64
|
+
/**
|
|
65
|
+
* 入库一条浏览器订阅(按 endpoint 去重:同一条更新,不新增)。
|
|
66
|
+
*
|
|
67
|
+
* @param input - 浏览器 `PushSubscription.toJSON()` 的 JSON
|
|
68
|
+
* @param deviceId - 这条订阅属于哪台设备
|
|
69
|
+
*/
|
|
70
|
+
add(input: unknown, deviceId: string): PushSubscribeOutcome;
|
|
71
|
+
/** 按 endpoint 删一条订阅;删掉了返回 true。 */
|
|
72
|
+
remove(endpoint: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* 给库里所有订阅发同一条消息。
|
|
75
|
+
*
|
|
76
|
+
* 失败一律只记账:404/410 的订阅当场删掉(它已经永久失效了),其余错误收进 errors
|
|
77
|
+
* 交给调用方写 notice。这个方法自身不抛——推送发不出去不该影响宿主干活。
|
|
78
|
+
*/
|
|
79
|
+
send(message: Record<string, unknown>): Promise<PushSendResult>;
|
|
80
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器推送(Web Push):手机上那个 PWA 关掉之后,宿主靠这条把「等你审批」「一轮跑完」
|
|
3
|
+
* 推到手机的通知栏。
|
|
4
|
+
*
|
|
5
|
+
* 三份东西:
|
|
6
|
+
*
|
|
7
|
+
* push-keys.json VAPID 密钥对(**首次用到时才生成**,tmp+rename 原子落盘)。
|
|
8
|
+
* 公钥交给浏览器订阅,私钥只在本机;换了密钥旧订阅全部作废。
|
|
9
|
+
* push-subs.json 订阅清单 `[{endpoint, keys:{p256dh,auth}, deviceId, createdAt}]`,
|
|
10
|
+
* 按 endpoint 去重(同一个浏览器重复订阅只留最新那份)。
|
|
11
|
+
* 发送 web-push 的 sendNotification。404 / 410 表示这个订阅已经废了
|
|
12
|
+
* (用户清了站点数据、卸了 PWA),当场从库里删掉;其余失败只记账,
|
|
13
|
+
* 由调用方写一条 notice,这里绝不抛。
|
|
14
|
+
*
|
|
15
|
+
* 总开关在偏好里(`remote.push`,默认关):关着时公钥报 null、订阅端点回 403、
|
|
16
|
+
* 一条推送都不发。所以密钥不会因为「装了插件」就被生成出来。
|
|
17
|
+
*
|
|
18
|
+
* @module dsc/core/remote/push
|
|
19
|
+
*/
|
|
20
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
21
|
+
import webpush from 'web-push';
|
|
22
|
+
import { readJsonFile, remoteFiles, writeJsonFile } from './store.js';
|
|
23
|
+
/** VAPID 的 subject:推送服务只要求它是一个 mailto 或 https 地址,不校验可达性。 */
|
|
24
|
+
export const PUSH_SUBJECT = 'mailto:pneuma@localhost';
|
|
25
|
+
/** 一次推送的超时(推送服务不响应时别把这一轮卡住)。 */
|
|
26
|
+
export const PUSH_SEND_TIMEOUT_MS = 5_000;
|
|
27
|
+
function isRecord(value) {
|
|
28
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
29
|
+
}
|
|
30
|
+
function errorText(error) {
|
|
31
|
+
return error instanceof Error ? error.message : String(error);
|
|
32
|
+
}
|
|
33
|
+
/** 从 web-push 抛出来的错误里取 HTTP 状态码;取不到给 0。 */
|
|
34
|
+
function statusOf(error) {
|
|
35
|
+
if (isRecord(error) && typeof error['statusCode'] === 'number')
|
|
36
|
+
return error['statusCode'];
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
/** 默认发送器:就是 web-push 本身。 */
|
|
40
|
+
const defaultSender = async (subscription, payload) => {
|
|
41
|
+
await webpush.sendNotification({ endpoint: subscription.endpoint, keys: { p256dh: subscription.keys.p256dh, auth: subscription.keys.auth } }, payload, { timeout: PUSH_SEND_TIMEOUT_MS });
|
|
42
|
+
};
|
|
43
|
+
export class RemotePush {
|
|
44
|
+
files;
|
|
45
|
+
now;
|
|
46
|
+
sender;
|
|
47
|
+
keysCache = null;
|
|
48
|
+
constructor(options = {}) {
|
|
49
|
+
this.files = remoteFiles(options.dir);
|
|
50
|
+
this.now = options.now ?? (() => Date.now());
|
|
51
|
+
this.sender = options.sender ?? defaultSender;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 读 VAPID 密钥;没有就生成一份并原子落盘。
|
|
55
|
+
* @returns 密钥对;生成/落盘失败时抛(调用方按「Web Push 不可用」处理)
|
|
56
|
+
*/
|
|
57
|
+
keys() {
|
|
58
|
+
if (this.keysCache !== null)
|
|
59
|
+
return this.keysCache;
|
|
60
|
+
const doc = readJsonFile(this.files.pushKeys);
|
|
61
|
+
if (doc !== null &&
|
|
62
|
+
typeof doc['publicKey'] === 'string' &&
|
|
63
|
+
typeof doc['privateKey'] === 'string' &&
|
|
64
|
+
doc['publicKey'] !== '' &&
|
|
65
|
+
doc['privateKey'] !== '') {
|
|
66
|
+
this.keysCache = { publicKey: doc['publicKey'], privateKey: doc['privateKey'] };
|
|
67
|
+
return this.keysCache;
|
|
68
|
+
}
|
|
69
|
+
const generated = webpush.generateVAPIDKeys();
|
|
70
|
+
const keys = { publicKey: generated.publicKey, privateKey: generated.privateKey };
|
|
71
|
+
// tmp + rename:写一半崩掉时读到的是「没有密钥」,下次重新生成,不会是半份密钥
|
|
72
|
+
writeJsonFile(this.files.pushKeys, keys);
|
|
73
|
+
this.keysCache = keys;
|
|
74
|
+
return keys;
|
|
75
|
+
}
|
|
76
|
+
/** 给浏览器的 VAPID 公钥;取不到(web-push 坏了、磁盘写不动)时 null。 */
|
|
77
|
+
publicKey() {
|
|
78
|
+
try {
|
|
79
|
+
return this.keys().publicKey;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** 订阅清单(读不动、内容不是数组一律按空处理)。 */
|
|
86
|
+
subscriptions() {
|
|
87
|
+
const file = this.files.pushSubs;
|
|
88
|
+
if (!existsSync(file))
|
|
89
|
+
return [];
|
|
90
|
+
let parsed;
|
|
91
|
+
try {
|
|
92
|
+
parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
if (!Array.isArray(parsed))
|
|
98
|
+
return [];
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const item of parsed) {
|
|
101
|
+
if (!isRecord(item))
|
|
102
|
+
continue;
|
|
103
|
+
const endpoint = item['endpoint'];
|
|
104
|
+
const keys = item['keys'];
|
|
105
|
+
if (typeof endpoint !== 'string' || endpoint === '' || !isRecord(keys))
|
|
106
|
+
continue;
|
|
107
|
+
const p256dh = keys['p256dh'];
|
|
108
|
+
const auth = keys['auth'];
|
|
109
|
+
if (typeof p256dh !== 'string' || p256dh === '' || typeof auth !== 'string' || auth === '')
|
|
110
|
+
continue;
|
|
111
|
+
out.push({
|
|
112
|
+
endpoint,
|
|
113
|
+
keys: { p256dh, auth },
|
|
114
|
+
deviceId: typeof item['deviceId'] === 'string' ? item['deviceId'] : '',
|
|
115
|
+
createdAt: typeof item['createdAt'] === 'number' ? item['createdAt'] : 0,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/** 库里现在有几条订阅。 */
|
|
121
|
+
count() {
|
|
122
|
+
return this.subscriptions().length;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 入库一条浏览器订阅(按 endpoint 去重:同一条更新,不新增)。
|
|
126
|
+
*
|
|
127
|
+
* @param input - 浏览器 `PushSubscription.toJSON()` 的 JSON
|
|
128
|
+
* @param deviceId - 这条订阅属于哪台设备
|
|
129
|
+
*/
|
|
130
|
+
add(input, deviceId) {
|
|
131
|
+
if (!isRecord(input))
|
|
132
|
+
return { ok: false, error: '请求体不是 PushSubscription 对象' };
|
|
133
|
+
const endpoint = input['endpoint'];
|
|
134
|
+
const keys = input['keys'];
|
|
135
|
+
if (typeof endpoint !== 'string' || endpoint.trim() === '')
|
|
136
|
+
return { ok: false, error: '订阅里没有 endpoint' };
|
|
137
|
+
// 推送端点必须 https:标准推送服务(FCM / Mozilla autopush)都是 https,明文 http 只会
|
|
138
|
+
// 出现在「把宿主当跳板打内网」的构造请求里(web-push 库会照单全发)。
|
|
139
|
+
if (!/^https:\/\//i.test(endpoint.trim()))
|
|
140
|
+
return { ok: false, error: 'endpoint 必须 https' };
|
|
141
|
+
if (!isRecord(keys))
|
|
142
|
+
return { ok: false, error: '订阅里没有 keys' };
|
|
143
|
+
const p256dh = keys['p256dh'];
|
|
144
|
+
const auth = keys['auth'];
|
|
145
|
+
if (typeof p256dh !== 'string' || p256dh === '')
|
|
146
|
+
return { ok: false, error: '订阅里没有 keys.p256dh' };
|
|
147
|
+
if (typeof auth !== 'string' || auth === '')
|
|
148
|
+
return { ok: false, error: '订阅里没有 keys.auth' };
|
|
149
|
+
const record = {
|
|
150
|
+
endpoint,
|
|
151
|
+
keys: { p256dh, auth },
|
|
152
|
+
deviceId,
|
|
153
|
+
createdAt: this.now(),
|
|
154
|
+
};
|
|
155
|
+
const list = this.subscriptions();
|
|
156
|
+
const index = list.findIndex((item) => item.endpoint === endpoint);
|
|
157
|
+
if (index >= 0) {
|
|
158
|
+
list[index] = record;
|
|
159
|
+
writeJsonFile(this.files.pushSubs, list);
|
|
160
|
+
return { ok: true, added: false, total: list.length };
|
|
161
|
+
}
|
|
162
|
+
list.push(record);
|
|
163
|
+
writeJsonFile(this.files.pushSubs, list);
|
|
164
|
+
return { ok: true, added: true, total: list.length };
|
|
165
|
+
}
|
|
166
|
+
/** 按 endpoint 删一条订阅;删掉了返回 true。 */
|
|
167
|
+
remove(endpoint) {
|
|
168
|
+
const target = endpoint.trim();
|
|
169
|
+
if (target === '')
|
|
170
|
+
return false;
|
|
171
|
+
const list = this.subscriptions();
|
|
172
|
+
const kept = list.filter((item) => item.endpoint !== target);
|
|
173
|
+
if (kept.length === list.length)
|
|
174
|
+
return false;
|
|
175
|
+
writeJsonFile(this.files.pushSubs, kept);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 给库里所有订阅发同一条消息。
|
|
180
|
+
*
|
|
181
|
+
* 失败一律只记账:404/410 的订阅当场删掉(它已经永久失效了),其余错误收进 errors
|
|
182
|
+
* 交给调用方写 notice。这个方法自身不抛——推送发不出去不该影响宿主干活。
|
|
183
|
+
*/
|
|
184
|
+
async send(message) {
|
|
185
|
+
const result = { attempted: 0, sent: 0, dropped: 0, errors: [] };
|
|
186
|
+
const list = this.subscriptions();
|
|
187
|
+
if (list.length === 0)
|
|
188
|
+
return result;
|
|
189
|
+
let keys;
|
|
190
|
+
try {
|
|
191
|
+
keys = this.keys();
|
|
192
|
+
webpush.setVapidDetails(PUSH_SUBJECT, keys.publicKey, keys.privateKey);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
result.errors.push(`VAPID 密钥不可用:${errorText(error)}`);
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
const payload = JSON.stringify(message);
|
|
199
|
+
const stale = [];
|
|
200
|
+
for (const subscription of list) {
|
|
201
|
+
result.attempted += 1;
|
|
202
|
+
try {
|
|
203
|
+
await this.sender(subscription, payload);
|
|
204
|
+
result.sent += 1;
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
const status = statusOf(error);
|
|
208
|
+
if (status === 404 || status === 410) {
|
|
209
|
+
stale.push(subscription.endpoint);
|
|
210
|
+
result.dropped += 1;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
result.errors.push(status > 0 ? `HTTP ${String(status)}` : errorText(error));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (stale.length > 0) {
|
|
217
|
+
const alive = this.subscriptions().filter((item) => !stale.includes(item.endpoint));
|
|
218
|
+
writeJsonFile(this.files.pushSubs, alive);
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** 远程控制的数据目录。 */
|
|
2
|
+
export declare const REMOTE_DIR: string;
|
|
3
|
+
/** 几份文件的位置;`dir` 只在自检脚本里换(默认就是 `~/.dsc/remote`)。 */
|
|
4
|
+
export interface RemoteFiles {
|
|
5
|
+
dir: string;
|
|
6
|
+
pending: string;
|
|
7
|
+
devices: string;
|
|
8
|
+
owner: string;
|
|
9
|
+
pushKeys: string;
|
|
10
|
+
pushSubs: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function remoteFiles(dir?: string): RemoteFiles;
|
|
13
|
+
/** 读一份 JSON;文件不在、读不动、内容不是对象时一律给 null(调用方按「空」处理)。 */
|
|
14
|
+
export declare function readJsonFile(file: string): Record<string, unknown> | null;
|
|
15
|
+
/** 原子写一份 JSON(临时文件 + rename)。 */
|
|
16
|
+
export declare function writeJsonFile(file: string, value: unknown): void;
|
|
17
|
+
/** 删一份文件(不在了就当成功,删不掉也不抛)。 */
|
|
18
|
+
export declare function removeFile(file: string): void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 远程控制的数据文件:`~/.dsc/remote/` 下的几份 JSON。
|
|
3
|
+
*
|
|
4
|
+
* pending.json 还没被用掉的配对码(只存盐与哈希,明文码永不落盘)
|
|
5
|
+
* devices.json 已配对设备的 token 哈希、名字与时间
|
|
6
|
+
* .owner.json 主控位:哪个进程在伺服(pid + 启动指纹 + 端口)
|
|
7
|
+
* push-keys.json Web Push 的 VAPID 密钥对(公钥给浏览器订阅,私钥只在本机)
|
|
8
|
+
* push-subs.json 浏览器推送订阅(endpoint + 密钥 + 哪台设备)
|
|
9
|
+
*
|
|
10
|
+
* 写盘一律「先写同目录临时文件再 rename」:进程正好在写的那一刻挂掉时,
|
|
11
|
+
* 读到的只可能是旧文件,不会是半个 JSON。
|
|
12
|
+
*
|
|
13
|
+
* @module dsc/core/remote/store
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { dirname, join } from 'node:path';
|
|
17
|
+
import { dscPath } from '../path-policy.js';
|
|
18
|
+
/** 远程控制的数据目录。 */
|
|
19
|
+
export const REMOTE_DIR = dscPath('remote');
|
|
20
|
+
export function remoteFiles(dir = REMOTE_DIR) {
|
|
21
|
+
return {
|
|
22
|
+
dir,
|
|
23
|
+
pending: join(dir, 'pending.json'),
|
|
24
|
+
devices: join(dir, 'devices.json'),
|
|
25
|
+
owner: join(dir, '.owner.json'),
|
|
26
|
+
pushKeys: join(dir, 'push-keys.json'),
|
|
27
|
+
pushSubs: join(dir, 'push-subs.json'),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** 读一份 JSON;文件不在、读不动、内容不是对象时一律给 null(调用方按「空」处理)。 */
|
|
31
|
+
export function readJsonFile(file) {
|
|
32
|
+
if (!existsSync(file))
|
|
33
|
+
return null;
|
|
34
|
+
try {
|
|
35
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
36
|
+
return parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
37
|
+
? parsed
|
|
38
|
+
: null;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** 原子写一份 JSON(临时文件 + rename)。 */
|
|
45
|
+
export function writeJsonFile(file, value) {
|
|
46
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
47
|
+
const tmp = `${file}.${String(process.pid)}.tmp`;
|
|
48
|
+
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
49
|
+
renameSync(tmp, file);
|
|
50
|
+
}
|
|
51
|
+
/** 删一份文件(不在了就当成功,删不掉也不抛)。 */
|
|
52
|
+
export function removeFile(file) {
|
|
53
|
+
try {
|
|
54
|
+
unlinkSync(file);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// 已经被人删掉了 / 权限不对:下一个进程会按「主人已死」接管,不碍事
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** 票据有效期:够浏览器从 HTTP 响应走到 WebSocket 握手,不够被人捡去用。 */
|
|
2
|
+
export declare const TICKET_TTL_MS = 30000;
|
|
3
|
+
/** 一张票据换来的东西:这台设备是谁(升级 WS 时用它把连接和设备对上)。 */
|
|
4
|
+
export interface TicketGrant {
|
|
5
|
+
deviceId: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class TicketStore {
|
|
8
|
+
/** 票据 → 过期时刻与设备身份。 */
|
|
9
|
+
private readonly tickets;
|
|
10
|
+
private readonly ttlMs;
|
|
11
|
+
private readonly now;
|
|
12
|
+
constructor(ttlMs?: number, now?: () => number);
|
|
13
|
+
/** 发一张新票,挂上取票的那台设备。 */
|
|
14
|
+
issue(deviceId?: string): string;
|
|
15
|
+
/** 用票:**取走即焚**,同一张票第二次来必然 null;过期也 null。 */
|
|
16
|
+
redeem(ticket: string): TicketGrant | null;
|
|
17
|
+
/** 现在还剩几张没用的票(自检用)。 */
|
|
18
|
+
get size(): number;
|
|
19
|
+
private prune;
|
|
20
|
+
}
|