dsh-builtin-browser 1.0.3 → 1.0.4
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 +89 -0
- package/browser-extension/constants.mjs +20 -0
- package/browser-extension/controller.mjs +293 -0
- package/browser-extension/locales.mjs +15 -0
- package/browser-extension/manifest.json +12 -0
- package/browser-extension/options.html +14 -0
- package/browser-extension/options.mjs +9 -0
- package/browser-extension/permission-guard.mjs +13 -0
- package/browser-extension/worker.mjs +74 -0
- package/package.json +20 -2
- package/scripts/build-client.mjs +305 -0
- package/scripts/build.mjs +69 -0
- package/scripts/bundle-host.mjs +28 -0
- package/scripts/fix-exports.mjs +40 -0
- package/scripts/install-electron.mjs +101 -0
- package/scripts/pack.mjs +99 -0
- package/scripts/publish-manifest.mjs +56 -0
- package/scripts/verify-exports.mjs +58 -0
- package/scripts/verify-preview-sdk.mjs +93 -0
- package/skills/dsh-builtin-browser/SKILL.md +98 -0
- package/skills/dsh-builtin-browser/references/batch.md +68 -0
- package/skills/dsh-builtin-browser/references/deliver.md +31 -0
- package/skills/dsh-builtin-browser/references/locate.md +11 -0
- package/skills/dsh-builtin-browser/references/recover.md +11 -0
- package/skills/dsh-builtin-browser/references/verify.md +13 -0
- package/skills/dsh-debug-host/SKILL.md +56 -0
- package/skills/dsh-debug-self/SKILL.md +50 -0
- package/src/bridge-compatibility.ts +39 -0
- package/src/browser/anti-bot.ts +251 -0
- package/src/browser/deep-dom.ts +40 -0
- package/src/browser/handles.ts +89 -0
- package/src/browser/runtime.ts +578 -0
- package/src/browser/takeover-target.ts +45 -0
- package/src/browser/types.ts +870 -0
- package/src/browser-electron/authenticated-route.ts +109 -0
- package/src/browser-electron/automatic-maintenance.ts +736 -0
- package/src/browser-electron/bridge-auto.ts +159 -0
- package/src/browser-electron/bridge-connection.ts +386 -0
- package/src/browser-electron/bridge-endpoint.ts +129 -0
- package/src/browser-electron/bridge-install.ts +290 -0
- package/src/browser-electron/bridge-maintenance.ts +189 -0
- package/src/browser-electron/bridge-status.ts +386 -0
- package/src/browser-electron/desktop-bridge-host.ts +253 -0
- package/src/browser-electron/desktop-identity.ts +353 -0
- package/src/browser-electron/desktop-shell.ts +100 -0
- package/src/browser-electron/document-readiness.ts +45 -0
- package/src/browser-electron/electron-provision.ts +347 -0
- package/src/browser-electron/entry.ts +1378 -0
- package/src/browser-electron/ephemeral-profile.ts +189 -0
- package/src/browser-electron/extension-browser.ts +230 -0
- package/src/browser-electron/extension-setup.ts +67 -0
- package/src/browser-electron/file-write.ts +31 -0
- package/src/browser-electron/foreground-watch.ts +142 -0
- package/src/browser-electron/guest-cookie-jar.ts +177 -0
- package/src/browser-electron/guest-cookie-store.ts +59 -0
- package/src/browser-electron/guest-css-runtime.ts +76 -0
- package/src/browser-electron/guest-css.ts +68 -0
- package/src/browser-electron/guest-route.ts +716 -0
- package/src/browser-electron/guest-runtime.ts +408 -0
- package/src/browser-electron/guest-storage-runtime.ts +54 -0
- package/src/browser-electron/guest-storage.ts +120 -0
- package/src/browser-electron/history-store.ts +224 -0
- package/src/browser-electron/host-main.ts +1385 -0
- package/src/browser-electron/host-patch-maintenance.ts +121 -0
- package/src/browser-electron/http-body.ts +65 -0
- package/src/browser-electron/json-reply.ts +47 -0
- package/src/browser-electron/maintenance-broker.ts +166 -0
- package/src/browser-electron/maintenance-lifecycle.ts +91 -0
- package/src/browser-electron/maintenance-route.ts +68 -0
- package/src/browser-electron/missing-system-browser.ts +114 -0
- package/src/browser-electron/open-slot.ts +49 -0
- package/src/browser-electron/paths.ts +25 -0
- package/src/browser-electron/preferred-browser.ts +130 -0
- package/src/browser-electron/preview-route.ts +215 -0
- package/src/browser-electron/process-ownership.ts +174 -0
- package/src/browser-electron/process.ts +23 -0
- package/src/browser-electron/profile-checkpoint.ts +187 -0
- package/src/browser-electron/profile-command.ts +47 -0
- package/src/browser-electron/profile-extension.ts +129 -0
- package/src/browser-electron/profile-lease.ts +211 -0
- package/src/browser-electron/profile-residue.ts +49 -0
- package/src/browser-electron/provider-core.ts +1107 -0
- package/src/browser-electron/provider-interaction.ts +1034 -0
- package/src/browser-electron/provider-media.ts +589 -0
- package/src/browser-electron/provider-navigation.ts +323 -0
- package/src/browser-electron/provider-observation.ts +664 -0
- package/src/browser-electron/provider-tabs.ts +372 -0
- package/src/browser-electron/provider.ts +666 -0
- package/src/browser-electron/reference-route.ts +598 -0
- package/src/browser-electron/remote-host.ts +1538 -0
- package/src/browser-electron/rpc-input.ts +13 -0
- package/src/browser-electron/rpc-ops.ts +41 -0
- package/src/browser-electron/runtime-diagnostics.ts +38 -0
- package/src/browser-electron/runtime-route.ts +105 -0
- package/src/browser-electron/runtime-status.ts +309 -0
- package/src/browser-electron/settings-route.ts +64 -0
- package/src/browser-electron/settings-store.ts +593 -0
- package/src/browser-electron/shortcuts-route.ts +112 -0
- package/src/browser-electron/sidebar-only-host.ts +300 -0
- package/src/browser-electron/startpage-shortcuts.ts +213 -0
- package/src/browser-electron/system-browser.ts +1850 -0
- package/src/browser-electron/tauri-host.ts +194 -0
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/virtual-cursor.ts +162 -0
- package/src/browser-electron/windows-system.ts +24 -0
- package/src/browser-reference/delivery.ts +384 -0
- package/src/browser-reference/entry.ts +631 -0
- package/src/browser-reference/export.ts +339 -0
- package/src/browser-reference/import.ts +303 -0
- package/src/browser-reference/page-script.ts +1537 -0
- package/src/browser-reference/service.ts +1346 -0
- package/src/browser-reference/store.ts +172 -0
- package/src/browser-reference/text-quote.ts +219 -0
- package/src/browser-reference/types.ts +195 -0
- package/src/browser-reference/visible.ts +367 -0
- package/src/browser-reference/web-sources.ts +106 -0
- package/src/client/apply-guard.ts +22 -0
- package/src/client/browser/BrowserController.ts +693 -0
- package/src/client/browser/BrowserFrame.ts +78 -0
- package/src/client/browser/BrowserNavigation.ts +169 -0
- package/src/client/browser/BrowserPage.ts +23 -0
- package/src/client/browser/BrowserPersistence.ts +44 -0
- package/src/client/browser/IframeImpl.ts +182 -0
- package/src/client/browser/child-tabs.ts +14 -0
- package/src/client/browser/frame-history.ts +125 -0
- package/src/client/browser/frame-state.ts +27 -0
- package/src/client/browser/iframe-reference.ts +67 -0
- package/src/client/browser/initialization.ts +262 -0
- package/src/client/browser/omnibox.ts +243 -0
- package/src/client/browser/store.ts +38 -0
- package/src/client/browser/takeover-channel.ts +72 -0
- package/src/client/browser/url.ts +46 -0
- package/src/client/browser/web-reference-source.ts +56 -0
- package/src/client/css-modules.d.ts +4 -0
- package/src/client/definition.tsx +36 -0
- package/src/client/electron/ElectronWebViewImpl.ts +371 -0
- package/src/client/electron/ElectronWebviewPresentation.ts +140 -0
- package/src/client/electron/NativeChildPresentation.ts +53 -0
- package/src/client/electron/NativeViewPage.ts +265 -0
- package/src/client/electron/pages.ts +23 -0
- package/src/client/electron/reference-overlay.ts +53 -0
- package/src/client/electron/workspace.ts +33 -0
- package/src/client/index.ts +333 -0
- package/src/client/locales.ts +860 -0
- package/src/client/pages.ts +28 -0
- package/src/client/reference/client/BrowserRefLink.tsx +142 -0
- package/src/client/reference/client/draft-reference.ts +167 -0
- package/src/client/reference/client/index.ts +7 -0
- package/src/client/reference/client/ordered-submission.ts +108 -0
- package/src/client/reference/client/pending-delivery.ts +216 -0
- package/src/client/reference/client/plugin.ts +208 -0
- package/src/client/reference/client/production/ReferenceCapsules.tsx +345 -0
- package/src/client/reference/client/production/ReferenceDock.module.css +193 -0
- package/src/client/reference/client/production/ReferenceDock.tsx +6 -0
- package/src/client/reference/client/production/ReferenceFeedback.tsx +15 -0
- package/src/client/reference/client/production/ReferenceMath.tsx +44 -0
- package/src/client/reference/client/production/ReferenceText.tsx +36 -0
- package/src/client/reference/client/production/assembly.ts +447 -0
- package/src/client/reference/client/production/asset-store.ts +144 -0
- package/src/client/reference/client/production/availability.ts +72 -0
- package/src/client/reference/client/production/bytes.ts +36 -0
- package/src/client/reference/client/production/composer-attachments.ts +44 -0
- package/src/client/reference/client/production/delivery-client.ts +211 -0
- package/src/client/reference/client/production/draft-adapter.ts +512 -0
- package/src/client/reference/client/production/http-transport.ts +212 -0
- package/src/client/reference/client/production/production-submission.ts +70 -0
- package/src/client/reference/client/production/protocol.ts +234 -0
- package/src/client/reference/client/production/reference-card.ts +240 -0
- package/src/client/reference/client/production/reference-labels.ts +29 -0
- package/src/client/reference/client/production/reply-link-bridge.ts +222 -0
- package/src/client/reference/client/production/reveal.ts +215 -0
- package/src/client/reference/client/production/runtime.ts +1056 -0
- package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
- package/src/client/reference/client/production/source-marker.ts +99 -0
- package/src/client/reference/client/production/source-opener.ts +55 -0
- package/src/client/reference/client/production/styles.ts +104 -0
- package/src/client/reference/client/production/user-text-rule.ts +67 -0
- package/src/client/reference/client/reveal.ts +109 -0
- package/src/client/reference/client/scheme.ts +114 -0
- package/src/client/reference/client/session-schedule.ts +36 -0
- package/src/client/reference/client/types.ts +168 -0
- package/src/client/reference/css-modules.d.ts +4 -0
- package/src/client/reference/index.ts +11 -0
- package/src/client/reference-capture-owner.ts +162 -0
- package/src/client/reference-intents.ts +36 -0
- package/src/client/settings/index.ts +42 -0
- package/src/client/settings/mount-guard.ts +31 -0
- package/src/client/settings/panel.ts +2768 -0
- package/src/client/shortcut-open.ts +30 -0
- package/src/client/startpage/binding.ts +99 -0
- package/src/client/startpage/controller.ts +300 -0
- package/src/client/startpage/model.ts +279 -0
- package/src/client/view/Browser.module.css +751 -0
- package/src/client/view/BrowserBody.tsx +315 -0
- package/src/client/view/BrowserPresentation.ts +9 -0
- package/src/client/view/BrowserTitle.tsx +17 -0
- package/src/client/view/IframePresentation.ts +365 -0
- package/src/client/view/StartPage.tsx +598 -0
- package/src/client/view/TakeoverBanner.tsx +21 -0
- package/src/compat/host-features.ts +92 -0
- package/src/index.ts +110 -0
- package/src/reference-owner.ts +5 -0
- package/src/reference-page.ts +9 -0
- package/src/reference-source.ts +3 -0
- package/src/reference-text.ts +219 -0
- package/src/startpage-settings.ts +103 -0
- package/src/startpage.ts +71 -0
- package/src/takeover-protocol.ts +28 -0
- package/src/tool-browser/block-recovery.ts +85 -0
- package/src/tool-browser/code-tool.ts +555 -0
- package/src/tool-browser/code-worker.ts +725 -0
- package/src/tool-browser/element-fields.ts +167 -0
- package/src/tool-browser/errors.ts +40 -0
- package/src/tool-browser/index.ts +265 -0
- package/src/tool-browser/page-probe.ts +675 -0
- package/src/tool-browser/panel-page.ts +126 -0
- package/src/tool-browser/probe-client.ts +183 -0
- package/src/tool-browser/screenshot-annotations.ts +131 -0
- package/src/tool-browser/screenshot-image.ts +49 -0
- package/src/tool-browser/state.ts +375 -0
- package/src/tool-browser/takeover.ts +199 -0
- package/src/tool-browser/tools-debug.ts +806 -0
- package/src/tool-browser/tools-interaction.ts +956 -0
- package/src/tool-browser/tools-navigation.ts +786 -0
- package/src/tool-browser/tools-observation.ts +474 -0
- package/src/tool-browser/tools-reference.ts +294 -0
- package/src/types/electron-shim.d.ts +144 -0
- package/src/types.ts +85 -0
- package/src/web-guest.ts +35 -0
- package/tools/accept-desktop.mjs +148 -0
- package/dist/client.d.cts +0 -516
- package/dist/client.d.mts +0 -516
- package/dist/index.cjs +0 -92
- package/dist/index.d.mts +0 -2960
- package/dist/index.mjs +0 -24
- package/dsh-builtin-browser-1.0.3-publish.tgz +0 -0
- package/dsh-builtin-browser-1.0.3.tgz +0 -0
- package/dsh-host-patch/sources/main/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/manifest.json +0 -218
- package/dsh-host-patch/sources/rc3/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/series.json +0 -260
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { BROWSER_PROFILE_DIR } from '../../assets/browser-policy.mjs'
|
|
2
|
+
/**
|
|
3
|
+
* Compatibility for historical automatic-install settings and explicit bridge-maintenance intent.
|
|
4
|
+
* Runtime automatic installation is disabled. Bridge maintenance requires the user to close DSH
|
|
5
|
+
* Desktop before explicitly running the installer; loading a plugin never changes the installation.
|
|
6
|
+
* Historical marker readers remain available for diagnostics.
|
|
7
|
+
* @module dsh-browser/browser-electron/bridge-auto
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync } from 'node:fs'
|
|
10
|
+
import { dirname, join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
import type { BridgeStatus } from './bridge-status.ts'
|
|
13
|
+
import type { BrowserChannel } from './system-browser.ts'
|
|
14
|
+
import { writeFileAtomic } from './settings-store.ts'
|
|
15
|
+
|
|
16
|
+
/** What the last automatic attempt was, and for which desktop build. */
|
|
17
|
+
export interface AutoInstallMarker {
|
|
18
|
+
/** Desktop build the attempt belonged to (`version` from the identity check when known). */
|
|
19
|
+
readonly build: string
|
|
20
|
+
/** ISO timestamp of the attempt, recorded BEFORE running, so a crash still counts. */
|
|
21
|
+
readonly attemptedAt: string
|
|
22
|
+
/** Outcome recorded afterwards, for the panel and for support: `started` / `installed` / a reason. */
|
|
23
|
+
readonly outcome: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Everything the decision needs, so it can be tested without a filesystem or a shell. */
|
|
27
|
+
export interface AutoInstallInput {
|
|
28
|
+
/** The launcher's current profile name, rather than an installed Desktop found on disk. */
|
|
29
|
+
readonly profile: string | undefined
|
|
30
|
+
/** Only sidebar and automatic selection need the desktop bridge. */
|
|
31
|
+
readonly channel: BrowserChannel
|
|
32
|
+
/** Quiet mode preserves the current desktop session instead of restarting it implicitly. */
|
|
33
|
+
readonly quiet: boolean
|
|
34
|
+
/** The desktop install directory, or undefined when this is the web end. */
|
|
35
|
+
readonly installRoot: string | undefined
|
|
36
|
+
/** Whether the operator left automatic installation on. */
|
|
37
|
+
readonly enabled: boolean
|
|
38
|
+
/** The reading the panel would show. */
|
|
39
|
+
readonly status: BridgeStatus
|
|
40
|
+
/** The last attempt, when one is recorded. */
|
|
41
|
+
readonly marker: AutoInstallMarker | undefined
|
|
42
|
+
/** How long an attempt for the same build suppresses the next one. */
|
|
43
|
+
readonly cooldownMs: number
|
|
44
|
+
/** Current time, injected. */
|
|
45
|
+
readonly now: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Why the automatic install did or did not run, in words a log line can carry. */
|
|
49
|
+
export interface AutoInstallDecision {
|
|
50
|
+
/** Whether to start the installer now. */
|
|
51
|
+
readonly run: boolean
|
|
52
|
+
/** The reason, either way. */
|
|
53
|
+
readonly why: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Historical attempt-record interval, retained for compatibility with existing diagnostics. */
|
|
57
|
+
export const AUTO_INSTALL_COOLDOWN_MS = 24 * 60 * 60 * 1_000
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Report that runtime bridge installation is deferred to explicit maintenance.
|
|
61
|
+
*
|
|
62
|
+
* @param input - the readings described by {@link AutoInstallInput}.
|
|
63
|
+
* @returns whether to run, and why.
|
|
64
|
+
*/
|
|
65
|
+
export function autoInstallDecision(_input: AutoInstallInput): AutoInstallDecision {
|
|
66
|
+
return { run: false, why: 'runtime automatic bridge installation is disabled; close DSH Desktop and run bridge maintenance explicitly' }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read the profile identity supplied by DSH's launcher before plugins mount.
|
|
71
|
+
* A Desktop installation or Electron executable can also belong to a Web/CLI invocation,
|
|
72
|
+
* so neither is used to infer the current surface.
|
|
73
|
+
* @param ctx - plugin context; older or custom compositions may omit the profile service.
|
|
74
|
+
* @returns the current profile name, or undefined when the launcher did not provide it.
|
|
75
|
+
*/
|
|
76
|
+
export function readAutoInstallProfile(ctx: unknown): string | undefined {
|
|
77
|
+
try {
|
|
78
|
+
const context = ctx as { get?: (name: string) => unknown }
|
|
79
|
+
if (typeof context?.get !== 'function') return undefined
|
|
80
|
+
const profile = context.get('profileContext') as { name?: unknown } | undefined
|
|
81
|
+
return typeof profile?.name === 'string' ? profile.name : undefined
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A manual uninstall could not record the operator's automatic-install opt-out. */
|
|
88
|
+
export class BridgeAutoInstallPreferenceError extends Error {
|
|
89
|
+
constructor(cause: unknown) {
|
|
90
|
+
super(`could not save automatic bridge installation preference: ${String(cause)}`, { cause })
|
|
91
|
+
this.name = 'BridgeAutoInstallPreferenceError'
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Preserve an explicit uninstall as an automatic-install opt-out in historical settings.
|
|
97
|
+
* The preference is written before the installer starts, so a failed write cannot launch maintenance.
|
|
98
|
+
* A failed uninstall retains the opt-out; an explicit install leaves an existing opt-out unchanged.
|
|
99
|
+
* @param action - the authenticated user's selected action.
|
|
100
|
+
* @param update - persist the operator's bridge preference.
|
|
101
|
+
* @param run - perform the installer action after its preference is durable.
|
|
102
|
+
* @returns the installer's result.
|
|
103
|
+
*/
|
|
104
|
+
export async function runWithManualBridgePreference<T>(
|
|
105
|
+
action: 'install' | 'uninstall',
|
|
106
|
+
update: (patch: { readonly bridge: { readonly autoInstall: false } }) => unknown,
|
|
107
|
+
run: () => Promise<T>,
|
|
108
|
+
): Promise<T> {
|
|
109
|
+
if (action === 'uninstall') {
|
|
110
|
+
try { update({ bridge: { autoInstall: false } }) }
|
|
111
|
+
catch (error) { throw new BridgeAutoInstallPreferenceError(error) }
|
|
112
|
+
}
|
|
113
|
+
return await run()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Where the attempt record lives, beside the settings the plugin already owns. */
|
|
117
|
+
export function autoInstallMarkerPath(dshHome: string): string {
|
|
118
|
+
return join(dshHome, BROWSER_PROFILE_DIR, 'bridge-auto-install.json')
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Read the last attempt.
|
|
123
|
+
*
|
|
124
|
+
* A record that cannot be parsed is treated as absent: the guard exists to prevent loops, and a single
|
|
125
|
+
* extra attempt after a corrupt file is cheaper than refusing to ever install again.
|
|
126
|
+
* @param path - marker file path.
|
|
127
|
+
* @returns the marker, or undefined.
|
|
128
|
+
*/
|
|
129
|
+
export function readAutoInstallMarker(path: string): AutoInstallMarker | undefined {
|
|
130
|
+
if (!existsSync(path)) return undefined
|
|
131
|
+
try {
|
|
132
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8')) as Partial<AutoInstallMarker>
|
|
133
|
+
if (typeof parsed.build !== 'string' || typeof parsed.attemptedAt !== 'string') return undefined
|
|
134
|
+
return {
|
|
135
|
+
build: parsed.build,
|
|
136
|
+
attemptedAt: parsed.attemptedAt,
|
|
137
|
+
outcome: typeof parsed.outcome === 'string' ? parsed.outcome : 'unknown',
|
|
138
|
+
}
|
|
139
|
+
} catch {
|
|
140
|
+
return undefined
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Record an attempt. Written before the installer runs: if the shell is killed mid-install, the next
|
|
146
|
+
* launch must still see that an attempt happened rather than starting another restart.
|
|
147
|
+
* @param path - marker file path.
|
|
148
|
+
* @param marker - what to record.
|
|
149
|
+
* @returns whether the record was saved; callers skip automatic restarts without a durable guard.
|
|
150
|
+
*/
|
|
151
|
+
export function writeAutoInstallMarker(path: string, marker: AutoInstallMarker): boolean {
|
|
152
|
+
try {
|
|
153
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
154
|
+
writeFileAtomic(path, `${JSON.stringify(marker, null, 2)}\n`)
|
|
155
|
+
return true
|
|
156
|
+
} catch {
|
|
157
|
+
return false
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { LOOPBACK_IPV4 } from '../../assets/browser-policy.mjs'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
/**
|
|
4
|
+
* Transport for the desktop shell's browser bridge: one long-lived connection to a
|
|
5
|
+
* loopback TCP service.
|
|
6
|
+
*
|
|
7
|
+
* WHY THIS IS ITS OWN MODULE
|
|
8
|
+
* The bridge used to be reached with a fresh connection per request, which measured
|
|
9
|
+
* at ~25ms per call (TCP handshake + token exchange) against ~0.2ms when the socket
|
|
10
|
+
* is reused. A single browser action issues several CDP commands, so that cost was
|
|
11
|
+
* multiplied on every tool call — the difference between a snappy agent and a
|
|
12
|
+
* visibly sluggish one. Reuse is therefore a property of the transport, and keeping
|
|
13
|
+
* it here means the view host above does not have to think about sockets at all.
|
|
14
|
+
*
|
|
15
|
+
* PROTOCOL
|
|
16
|
+
* The first line on a connection carries the token; the service marks the socket
|
|
17
|
+
* authenticated and ignores that line as a command. Requests and answers are then
|
|
18
|
+
* newline-delimited JSON, one answer per request, in order.
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-browser/browser-electron/bridge-connection
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { connect, type Socket } from 'node:net'
|
|
24
|
+
import type { BrowserSessionWireId, BrowserTabWireId } from '../browser/handles.ts'
|
|
25
|
+
import { bridgePackageError } from '../bridge-compatibility.ts'
|
|
26
|
+
|
|
27
|
+
export type BridgeRequest =
|
|
28
|
+
| { readonly op: 'version' }
|
|
29
|
+
| { readonly op: 'list'; readonly handle?: BrowserSessionWireId }
|
|
30
|
+
| { readonly op: 'collapseSidebar' | 'releaseSession'; readonly handle: BrowserSessionWireId }
|
|
31
|
+
| { readonly op: 'ensureSidebar'; readonly handle: BrowserTabWireId; readonly deadline: number; readonly transactionId: string; readonly url?: string; readonly attachOnly?: boolean }
|
|
32
|
+
| { readonly op: 'cancelSidebar'; readonly handle: BrowserTabWireId; readonly transactionId: string }
|
|
33
|
+
| { readonly op: 'adoptPage' | 'closePage' | 'showTab' | 'releaseTab'; readonly handle: BrowserTabWireId }
|
|
34
|
+
| { readonly op: 'download'; readonly handle: BrowserTabWireId; readonly url: string; readonly savePath: string }
|
|
35
|
+
| { readonly op: 'cdp'; readonly handle: BrowserTabWireId; readonly method: string; readonly params?: Record<string, unknown> }
|
|
36
|
+
|
|
37
|
+
/** Where the shell publishes its bridge endpoint. */
|
|
38
|
+
export interface BridgeEndpoint {
|
|
39
|
+
readonly port: number
|
|
40
|
+
readonly token: string
|
|
41
|
+
readonly pid: number
|
|
42
|
+
readonly updatedAt?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** One queued request, waiting for its answer. */
|
|
46
|
+
interface PendingCall {
|
|
47
|
+
readonly resolve: (answer: Record<string, unknown>) => void
|
|
48
|
+
readonly reject: (error: Error) => void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Safety cap on one un-answered reply line. CDP answers from the sidebar (a screenshot's
|
|
53
|
+
* base64 is the big one) cross this socket, so the cap is generous; what it exists for is a
|
|
54
|
+
* peer that never sends a newline — `this.buffer += chunk` grew without bound, unlike the
|
|
55
|
+
* same buffer in remote-host.ts, which caps at 512 MiB.
|
|
56
|
+
*/
|
|
57
|
+
const MAX_BUFFER_BYTES = 128 * 1024 * 1024
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Field this client adds to every request so the shell can echo it back and never relies on
|
|
61
|
+
* byte order to pair an answer with its request. Loosely named, and additive on purpose: a
|
|
62
|
+
* bridge that does not know it simply ignores it (the current shipped shell does), and the
|
|
63
|
+
* client then falls back to arrival order, which is what the protocol guaranteed before.
|
|
64
|
+
*/
|
|
65
|
+
const REQUEST_ID_FIELD = 'bridgeRequestId'
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* How long the published endpoint may go un-refreshed before its shell is presumed gone.
|
|
69
|
+
*
|
|
70
|
+
* A live shell rewrites the file every fifteen seconds (its keep-alive), so four missed refreshes
|
|
71
|
+
* is not a slow shell — it is a shell that exited and left its last write behind. The distinction
|
|
72
|
+
* matters because the file is the ONLY thing that says where the bridge is: a tombstone names a
|
|
73
|
+
* port nobody owns, and a client that trusts it dials that port for the rest of its life.
|
|
74
|
+
*/
|
|
75
|
+
const ENDPOINT_TTL_MS = 60_000
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The same window, for the readiness report.
|
|
79
|
+
*
|
|
80
|
+
* Exported rather than re-stated: "the shell is gone" must mean one thing in this plugin. A second
|
|
81
|
+
* copy of the number would drift, and the drift would show up as a panel that says ready while every
|
|
82
|
+
* call times out — the exact failure this constant exists to prevent.
|
|
83
|
+
*/
|
|
84
|
+
export const ENDPOINT_STALE_AFTER_MS = ENDPOINT_TTL_MS
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A reusable connection to the bridge.
|
|
88
|
+
*
|
|
89
|
+
* Requests may be in flight CONCURRENTLY, so answers are correlated by id rather than by
|
|
90
|
+
* position: `call()` stamps a unique {@link REQUEST_ID_FIELD} on the request and `onData()`
|
|
91
|
+
* resolves the entry that carries the echoed id. A bridge that does not echo it is still
|
|
92
|
+
* supported — its answer is handed to the oldest outstanding request, which is the
|
|
93
|
+
* arrival-order rule the protocol states. Once such an answer has been seen, the connection
|
|
94
|
+
* stops keeping two requests in flight (a second one re-dials), so position can never be the
|
|
95
|
+
* only thing distinguishing two callers' answers. An answer that matches nothing is never
|
|
96
|
+
* handed to whichever request happens to be first: the socket is dropped instead.
|
|
97
|
+
*
|
|
98
|
+
* A broken socket is discarded so the next call dials again — a dead connection must never
|
|
99
|
+
* become a dead plugin.
|
|
100
|
+
*/
|
|
101
|
+
export class BridgeConnection {
|
|
102
|
+
private socket: Socket | undefined
|
|
103
|
+
private buffer = ''
|
|
104
|
+
/** Outstanding requests by id. */
|
|
105
|
+
private readonly pending = new Map<number, PendingCall>()
|
|
106
|
+
/** Ids of the requests sent on the current socket, oldest first. */
|
|
107
|
+
private readonly queue: number[] = []
|
|
108
|
+
private nextId = 1
|
|
109
|
+
/** True once this connection may only have one request in flight at a time. */
|
|
110
|
+
private serialisable = false
|
|
111
|
+
/** True once the shell behind this connection has been reported gone. */
|
|
112
|
+
private lostNotified = false
|
|
113
|
+
private endpoint: BridgeEndpoint
|
|
114
|
+
private readonly endpointPath?: string
|
|
115
|
+
private readonly onLost?: () => void
|
|
116
|
+
constructor(
|
|
117
|
+
endpoint: BridgeEndpoint,
|
|
118
|
+
endpointPath?: string,
|
|
119
|
+
onLost?: () => void,
|
|
120
|
+
) {
|
|
121
|
+
this.endpoint = endpoint
|
|
122
|
+
this.endpointPath = endpointPath
|
|
123
|
+
this.onLost = onLost
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Re-read the published endpoint, and adopt it if the shell has moved.
|
|
128
|
+
*
|
|
129
|
+
* The bridge binds an ephemeral port and picks a NEW one whenever its server is recreated. The
|
|
130
|
+
* endpoint file is rewritten every fifteen seconds, but a host that adopted the old one keeps
|
|
131
|
+
* dialling a dead port forever: measured here as `connect ECONNREFUSED 127.0.0.1:52276` while
|
|
132
|
+
* the live bridge sat on 52411. Discarding the socket is not enough — the comment below says a
|
|
133
|
+
* dead connection must never become a dead plugin, and a stale PORT makes exactly that happen.
|
|
134
|
+
*
|
|
135
|
+
* Cheap and safe: one small file read per call, no socket work.
|
|
136
|
+
*
|
|
137
|
+
* @returns true when a different endpoint was adopted.
|
|
138
|
+
*/
|
|
139
|
+
private refreshEndpoint(): boolean {
|
|
140
|
+
if (this.endpointPath === undefined) return false
|
|
141
|
+
try {
|
|
142
|
+
const raw = JSON.parse(readFileSync(this.endpointPath, 'utf8')) as Partial<BridgeEndpoint>
|
|
143
|
+
if (typeof raw.port !== 'number' || typeof raw.token !== 'string') return false
|
|
144
|
+
// A shell that exited stops refreshing the file, and what it left names a port nobody owns.
|
|
145
|
+
// Trusting it is how a host ends up dialling a dead port for the rest of the process's life
|
|
146
|
+
// — reported as "the browser stopped working and only restarting DSH brought it back".
|
|
147
|
+
if (endpointExpired(raw.updatedAt)) {
|
|
148
|
+
this.notifyLost()
|
|
149
|
+
return false
|
|
150
|
+
}
|
|
151
|
+
if (raw.port === this.endpoint.port && raw.token === this.endpoint.token) return false
|
|
152
|
+
this.endpoint = {
|
|
153
|
+
port: raw.port,
|
|
154
|
+
token: raw.token,
|
|
155
|
+
pid: typeof raw.pid === 'number' ? raw.pid : this.endpoint.pid,
|
|
156
|
+
}
|
|
157
|
+
// The old socket belongs to the old port.
|
|
158
|
+
this.reset(new Error('dsh-builtin-browser: the bridge moved; reconnecting to the new port'))
|
|
159
|
+
return true
|
|
160
|
+
} catch {
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Report once that the shell is gone, so the surface can fall back instead of dialling it. */
|
|
166
|
+
private notifyLost(): void {
|
|
167
|
+
if (this.lostNotified) return
|
|
168
|
+
this.lostNotified = true
|
|
169
|
+
try {
|
|
170
|
+
this.onLost?.()
|
|
171
|
+
} catch {
|
|
172
|
+
// A listener must never break the transport: the caller's error is the one that matters.
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Send one request, reusing the socket if it is still healthy.
|
|
178
|
+
*
|
|
179
|
+
* Requests may overlap: the request is written as soon as it is queued, so two concurrent
|
|
180
|
+
* callers have two requests in flight. Each carries a unique {@link REQUEST_ID_FIELD}, and
|
|
181
|
+
* `onData()` resolves the entry that carries the echoed id — which is what makes
|
|
182
|
+
* correlation independent of arrival order. A bridge that does not echo the field (the
|
|
183
|
+
* shipped shell today) still works by arrival order; see {@link onData}.
|
|
184
|
+
* @param request - the request body (the token and the request id are added here).
|
|
185
|
+
* @param timeoutMs - how long to wait for the answer before dropping the socket.
|
|
186
|
+
* @returns the bridge's answer.
|
|
187
|
+
*/
|
|
188
|
+
async call(request: BridgeRequest, timeoutMs = 20_000): Promise<Record<string, unknown>> {
|
|
189
|
+
// One re-dial, and only for the case this exists for: a socket that has already carried
|
|
190
|
+
// an id-less answer cannot have a second request in flight, because two outstanding
|
|
191
|
+
// requests would be matched by position — the swap this correlation prevents. It is a
|
|
192
|
+
// straight-line re-dial rather than a retry loop: an earlier revision re-entered call()
|
|
193
|
+
// from inside its own promise executor, which left an orphaned pending entry behind and,
|
|
194
|
+
// when the condition could not clear, spun until the process ran out of heap.
|
|
195
|
+
// Follow the shell if its bridge moved to a new port (see refreshEndpoint).
|
|
196
|
+
this.refreshEndpoint()
|
|
197
|
+
// The shell behind this connection is gone, and the endpoint file says so. Refusing here is
|
|
198
|
+
// what turns a permanent ECONNREFUSED into one named error the surface can act on — by
|
|
199
|
+
// falling back to the browser it hosts itself, instead of dialling a dead port until DSH is
|
|
200
|
+
// restarted.
|
|
201
|
+
if (this.lostNotified) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
'dsh-builtin-browser: the desktop shell that backed this browser has exited '
|
|
204
|
+
+ '(its published endpoint stopped being refreshed)',
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
let socket = this.ensureSocket()
|
|
208
|
+
if (this.serialisable && this.queue.length > 0) {
|
|
209
|
+
this.reset(new Error('dsh-builtin-browser: bridge connection reopened so replies stay attributable'))
|
|
210
|
+
socket = this.ensureSocket()
|
|
211
|
+
}
|
|
212
|
+
const id = this.nextId++
|
|
213
|
+
return await new Promise<Record<string, unknown>>((resolve, reject) => {
|
|
214
|
+
this.pending.set(id, {
|
|
215
|
+
resolve: answer => { clearTimeout(timer); resolve(answer) },
|
|
216
|
+
reject: error => { clearTimeout(timer); reject(error) },
|
|
217
|
+
})
|
|
218
|
+
this.queue.push(id)
|
|
219
|
+
const timer = setTimeout(() => {
|
|
220
|
+
const error = new Error(`dsh-builtin-browser: bridge timed out after ${timeoutMs}ms`)
|
|
221
|
+
// An unanswered request desynchronises the stream for an id-less bridge, and leaves one
|
|
222
|
+
// outstanding for one that echoes ids — either way the next call should start on a
|
|
223
|
+
// fresh socket. Only when THIS request's socket is still the live one: tearing down the
|
|
224
|
+
// REPLACEMENT because an abandoned request timed out would break whatever is using it.
|
|
225
|
+
//
|
|
226
|
+
// This test used to read `this.pending.has(id) && this.socket === socket`, with
|
|
227
|
+
// `forget(id)` on the line above it — and forget() is what removes the entry that `has`
|
|
228
|
+
// was looking for, so the condition was false every single time and the rebuild never
|
|
229
|
+
// ran. A timed-out call therefore left the desynchronised socket in place, and every
|
|
230
|
+
// later call inherited exactly the ambiguity this reset exists to clear. Deciding
|
|
231
|
+
// before forgetting is the whole fix; the `else` still drops the stale entry.
|
|
232
|
+
if (this.socket === socket) this.reset(error)
|
|
233
|
+
else this.forget(id)
|
|
234
|
+
// reset() already rejected this entry through its wrapper when it ran, so this is
|
|
235
|
+
// either the only rejection or a no-op that guarantees the caller settles.
|
|
236
|
+
reject(error)
|
|
237
|
+
}, timeoutMs)
|
|
238
|
+
try {
|
|
239
|
+
// The id is added LAST so a request body can never shadow it, and the token is
|
|
240
|
+
// added here rather than by the caller.
|
|
241
|
+
socket.write(JSON.stringify({ token: this.endpoint.token, ...request, [REQUEST_ID_FIELD]: id }) + '\n')
|
|
242
|
+
} catch (error) {
|
|
243
|
+
clearTimeout(timer)
|
|
244
|
+
this.forget(id)
|
|
245
|
+
const failure = error instanceof Error ? error : new Error(String(error))
|
|
246
|
+
if (this.socket === socket) this.reset(failure)
|
|
247
|
+
reject(failure)
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Close the socket and fail anything still waiting on it. */
|
|
253
|
+
close(): void {
|
|
254
|
+
this.reset(new Error('dsh-builtin-browser: bridge closed'))
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** The live socket, connecting and authenticating it on first use. */
|
|
258
|
+
private ensureSocket(): Socket {
|
|
259
|
+
if (this.socket !== undefined) return this.socket
|
|
260
|
+
const socket = connect({ host: LOOPBACK_IPV4, port: this.endpoint.port })
|
|
261
|
+
socket.setEncoding('utf8')
|
|
262
|
+
socket.on('data', chunk => this.onData(String(chunk)))
|
|
263
|
+
// Both handlers pass the socket they belong to, so a late event from a socket that was
|
|
264
|
+
// already replaced cannot tear down its successor: 'close' still fires for a socket that
|
|
265
|
+
// was removed mid-recycle, and acting on it there would kill a perfectly good connection.
|
|
266
|
+
socket.on('error', error => this.socketFailed(socket, error))
|
|
267
|
+
socket.on('close', () => this.socketFailed(socket, new Error('dsh-builtin-browser: bridge connection closed')))
|
|
268
|
+
this.buffer = ''
|
|
269
|
+
this.socket = socket
|
|
270
|
+
socket.write(JSON.stringify({ token: this.endpoint.token }) + '\n')
|
|
271
|
+
return socket
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Tear the connection down for an event raised by `source`, unless it is already gone. */
|
|
275
|
+
private socketFailed(source: Socket, error: Error): void {
|
|
276
|
+
if (this.socket !== source) return
|
|
277
|
+
this.reset(error)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Resolve queued requests by id, or in order when the bridge does not echo one. */
|
|
281
|
+
private onData(chunk: string): void {
|
|
282
|
+
this.buffer += chunk
|
|
283
|
+
// A peer that never sends a newline must not grow this buffer without bound. Failing
|
|
284
|
+
// the connection is the honest outcome: the caller gets an error naming the cap, while
|
|
285
|
+
// the next call dials a fresh socket.
|
|
286
|
+
if (this.buffer.length > MAX_BUFFER_BYTES) {
|
|
287
|
+
this.buffer = ''
|
|
288
|
+
this.reset(new Error(`dsh-builtin-browser: bridge reply exceeded ${String(MAX_BUFFER_BYTES)} bytes`))
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
let newline = this.buffer.indexOf('\n')
|
|
292
|
+
while (newline !== -1) {
|
|
293
|
+
const line = this.buffer.slice(0, newline).trim()
|
|
294
|
+
this.buffer = this.buffer.slice(newline + 1)
|
|
295
|
+
newline = this.buffer.indexOf('\n')
|
|
296
|
+
if (line === '') continue
|
|
297
|
+
const echoed = echoOf(line)
|
|
298
|
+
const id = echoed ?? this.queue[0]
|
|
299
|
+
if (id === undefined) {
|
|
300
|
+
// Nothing is waiting for this answer: the peer is answering something this socket
|
|
301
|
+
// never asked (or answered twice). Drop the socket rather than trust the stream.
|
|
302
|
+
this.reset(new Error('dsh-builtin-browser: bridge answered with no request outstanding'))
|
|
303
|
+
return
|
|
304
|
+
}
|
|
305
|
+
const index = this.queue.indexOf(id)
|
|
306
|
+
if (index !== -1) this.queue.splice(index, 1)
|
|
307
|
+
const entry = this.pending.get(id)
|
|
308
|
+
if (entry === undefined) continue
|
|
309
|
+
this.pending.delete(id)
|
|
310
|
+
// Id-less bridges pair by order, so a reply for the WRONG request cannot be told apart
|
|
311
|
+
// from the right one. Remember that this bridge does not echo ids, and serialise every
|
|
312
|
+
// later call so the ambiguity cannot arise again.
|
|
313
|
+
if (echoed === undefined) {
|
|
314
|
+
this.serialisable = true
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
const answer = JSON.parse(line) as Record<string, unknown>
|
|
318
|
+
const packageError = bridgePackageError(answer)
|
|
319
|
+
if (packageError !== undefined) {
|
|
320
|
+
entry.reject(packageError)
|
|
321
|
+
continue
|
|
322
|
+
}
|
|
323
|
+
// A rejected token means the shell restarted with a new one, so this socket
|
|
324
|
+
// can never succeed again: answer (the caller needs the error) and drop it,
|
|
325
|
+
// so the next call dials afresh instead of retrying a doomed credential.
|
|
326
|
+
if (answer.ok === false && answer.error === 'bad token') {
|
|
327
|
+
entry.resolve(answer)
|
|
328
|
+
this.reset(new Error('dsh-builtin-browser: the bridge rejected our token'))
|
|
329
|
+
continue
|
|
330
|
+
}
|
|
331
|
+
entry.resolve(answer)
|
|
332
|
+
} catch (error) {
|
|
333
|
+
entry.reject(new Error(`dsh-builtin-browser: malformed bridge answer (${String(error)})`))
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Forget one request (its answer can no longer be matched). */
|
|
339
|
+
private forget(id: number): void {
|
|
340
|
+
this.pending.delete(id)
|
|
341
|
+
const index = this.queue.indexOf(id)
|
|
342
|
+
if (index !== -1) this.queue.splice(index, 1)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Tear the socket down, failing pending work. */
|
|
346
|
+
private reset(error: Error): void {
|
|
347
|
+
if (this.socket !== undefined) {
|
|
348
|
+
const socket = this.socket
|
|
349
|
+
this.socket = undefined
|
|
350
|
+
socket.removeAllListeners()
|
|
351
|
+
socket.destroy()
|
|
352
|
+
}
|
|
353
|
+
this.buffer = ''
|
|
354
|
+
this.queue.length = 0
|
|
355
|
+
const pending = [...this.pending.values()]
|
|
356
|
+
this.pending.clear()
|
|
357
|
+
for (const entry of pending) entry.reject(error)
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Whether a published endpoint has gone un-refreshed long enough to be a tombstone.
|
|
363
|
+
*
|
|
364
|
+
* A missing or unparseable stamp is NOT expired: an older bridge that never wrote one must keep
|
|
365
|
+
* working, and guessing "stale" from absence would break every shell that predates the field.
|
|
366
|
+
* @param updatedAt - the file's own refresh stamp, when it carries one.
|
|
367
|
+
*/
|
|
368
|
+
function endpointExpired(updatedAt: unknown): boolean {
|
|
369
|
+
if (typeof updatedAt !== 'string') return false
|
|
370
|
+
const at = Date.parse(updatedAt)
|
|
371
|
+
if (!Number.isFinite(at)) return false
|
|
372
|
+
return Date.now() - at > ENDPOINT_TTL_MS
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** The request id a raw answer line echoes back, when it carries one. */
|
|
376
|
+
function echoOf(line: string): number | undefined {
|
|
377
|
+
try {
|
|
378
|
+
const answer = JSON.parse(line) as Record<string, unknown>
|
|
379
|
+
const value = answer[REQUEST_ID_FIELD]
|
|
380
|
+
return typeof value === 'number' && Number.isSafeInteger(value) ? value : undefined
|
|
381
|
+
} catch {
|
|
382
|
+
// A malformed line cannot be matched by id; the caller consumes it in order and
|
|
383
|
+
// reports the parse failure itself.
|
|
384
|
+
return undefined
|
|
385
|
+
}
|
|
386
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the desktop shell is RUNNING, as opposed to whether its bridge is installed.
|
|
3
|
+
*
|
|
4
|
+
* The two questions are constantly confused, and the confusion is the reason a user on
|
|
5
|
+
* `channel: sidebar` sees "another window opened instead" and cannot tell why:
|
|
6
|
+
*
|
|
7
|
+
* - `bridge-status.ts` reads the INSTALL TREE: does the shell's main entry carry our patch, and is
|
|
8
|
+
* the archive out of the way. It survives a reboot; it says nothing about a process.
|
|
9
|
+
* - This module reads the ENDPOINT the shell publishes while it runs: a port, a token, a pid and a
|
|
10
|
+
* refresh stamp. It says nothing about the install tree.
|
|
11
|
+
*
|
|
12
|
+
* "Installed" plus "not running" is the single most common broken state (the desktop app is simply
|
|
13
|
+
* closed), and a readiness report that stops at the install tree calls it ready — then every call
|
|
14
|
+
* dials a port nobody owns. So both readings are taken, and the sidebar carrier is only ready when
|
|
15
|
+
* BOTH are true.
|
|
16
|
+
*
|
|
17
|
+
* Two independent signals, because each alone has a false-positive:
|
|
18
|
+
* - the refresh stamp (the shell rewrites the file every ~15 s, so a stale stamp means a shell that
|
|
19
|
+
* exited and left its last write behind — a tombstone naming a dead port);
|
|
20
|
+
* - the pid, checked with signal 0 (kills nothing, and works across platforms).
|
|
21
|
+
* @module dsh-browser/browser-electron/bridge-endpoint
|
|
22
|
+
*/
|
|
23
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
24
|
+
|
|
25
|
+
import { ENDPOINT_STALE_AFTER_MS } from './bridge-connection.ts'
|
|
26
|
+
import { bridgeEndpointPath } from './desktop-bridge-host.ts'
|
|
27
|
+
import { pidAlive } from './process.ts'
|
|
28
|
+
|
|
29
|
+
/** What the panel needs to know about the running shell. */
|
|
30
|
+
export interface BridgeEndpointState {
|
|
31
|
+
/** Whether a published endpoint file exists at all. */
|
|
32
|
+
readonly present: boolean
|
|
33
|
+
/** Whether the shell behind it looks alive right now: a fresh stamp AND a live pid. */
|
|
34
|
+
readonly alive: boolean
|
|
35
|
+
/** One line, already user-facing. */
|
|
36
|
+
readonly detail: string
|
|
37
|
+
/** When the shell said it last refreshed the file, ISO, when the file carries a stamp. */
|
|
38
|
+
readonly updatedAt?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Read the endpoint the shell publishes.
|
|
43
|
+
*
|
|
44
|
+
* Never throws: a settings panel must get an answer even when the file is mid-write, unreadable, or
|
|
45
|
+
* belongs to a shell that exited an hour ago.
|
|
46
|
+
* @param path - the endpoint file; defaults to the one location the shell writes.
|
|
47
|
+
* @param now - the clock, injected so a test can age a stamp without sleeping.
|
|
48
|
+
* @returns what the panel shows.
|
|
49
|
+
*/
|
|
50
|
+
export function readBridgeEndpoint(
|
|
51
|
+
path: string = bridgeEndpointPath(),
|
|
52
|
+
now: number = Date.now(),
|
|
53
|
+
): BridgeEndpointState {
|
|
54
|
+
if (!existsSync(path)) {
|
|
55
|
+
return {
|
|
56
|
+
present: false,
|
|
57
|
+
alive: false,
|
|
58
|
+
detail: 'no desktop shell has published a bridge endpoint',
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
let raw: { port?: unknown; token?: unknown; pid?: unknown; updatedAt?: unknown }
|
|
62
|
+
try {
|
|
63
|
+
raw = JSON.parse(readFileSync(path, 'utf8')) as typeof raw
|
|
64
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)
|
|
65
|
+
|| !Number.isInteger(raw.port) || Number(raw.port) < 1 || Number(raw.port) > 65535
|
|
66
|
+
|| typeof raw.token !== 'string' || raw.token.trim() === ''
|
|
67
|
+
|| !Number.isSafeInteger(raw.pid) || Number(raw.pid) <= 0
|
|
68
|
+
|| (raw.updatedAt !== undefined && (typeof raw.updatedAt !== 'string' || !Number.isFinite(Date.parse(raw.updatedAt))))) {
|
|
69
|
+
throw new Error('expected a port, a nonempty token, a positive pid and an optional timestamp')
|
|
70
|
+
}
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
present: true,
|
|
74
|
+
alive: false,
|
|
75
|
+
detail: `the published endpoint is unreadable: ${String(error)}`,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const updatedAt = typeof raw.updatedAt === 'string' ? raw.updatedAt : undefined
|
|
79
|
+
const pid = typeof raw.pid === 'number' ? raw.pid : undefined
|
|
80
|
+
|
|
81
|
+
// A stamp older than the refresh window is a tombstone, not a slow shell (see
|
|
82
|
+
// ENDPOINT_STALE_AFTER_MS). An ABSENT stamp is not expired on purpose: an older bridge never wrote
|
|
83
|
+
// one, and that shell deserves to be checked by its pid instead of being declared gone.
|
|
84
|
+
if (updatedAt !== undefined) {
|
|
85
|
+
const at = Date.parse(updatedAt)
|
|
86
|
+
if (Number.isFinite(at)) {
|
|
87
|
+
const ageMs = now - at
|
|
88
|
+
if (ageMs > ENDPOINT_STALE_AFTER_MS) {
|
|
89
|
+
return {
|
|
90
|
+
present: true,
|
|
91
|
+
alive: false,
|
|
92
|
+
updatedAt,
|
|
93
|
+
detail: `the published endpoint has not been refreshed for ${Math.round(ageMs / 1000)}s, `
|
|
94
|
+
+ 'so the shell that wrote it has most likely exited',
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (pid !== undefined && !pidAlive(pid)) {
|
|
100
|
+
return {
|
|
101
|
+
present: true,
|
|
102
|
+
alive: false,
|
|
103
|
+
...updatedAt === undefined ? {} : { updatedAt },
|
|
104
|
+
detail: `the shell that published the endpoint (pid ${pid}) is not running`,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
present: true,
|
|
109
|
+
alive: true,
|
|
110
|
+
...updatedAt === undefined ? {} : { updatedAt },
|
|
111
|
+
detail: 'a desktop shell is running and answering on its published endpoint',
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The endpoint file's mtime, for diagnostics that want to say "written at".
|
|
117
|
+
*
|
|
118
|
+
* Separate and optional: the readiness report does not need it, and a caller that does can fail
|
|
119
|
+
* without disturbing anything else.
|
|
120
|
+
* @param path - the endpoint file.
|
|
121
|
+
* @returns the mtime as ISO, or undefined.
|
|
122
|
+
*/
|
|
123
|
+
export function bridgeEndpointWrittenAt(path: string = bridgeEndpointPath()): string | undefined {
|
|
124
|
+
try {
|
|
125
|
+
return statSync(path).mtime.toISOString()
|
|
126
|
+
} catch {
|
|
127
|
+
return undefined
|
|
128
|
+
}
|
|
129
|
+
}
|