dsh-builtin-browser 0.5.8 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +410 -1
- package/README.en.md +16 -64
- package/README.md +33 -64
- package/assets/browser-carriers.svg +20 -0
- package/assets/browser-extension-identity.d.mts +2 -0
- package/assets/browser-extension-identity.mjs +2 -0
- package/assets/browser-messages.d.mts +5 -0
- package/assets/browser-messages.mjs +64 -0
- package/assets/browser-panel-locales.d.mts +2 -0
- package/assets/browser-panel-locales.mjs +380 -0
- package/assets/browser-policy.d.mts +37 -0
- package/assets/browser-policy.mjs +37 -0
- package/assets/browser-products.d.mts +6 -0
- package/assets/browser-products.mjs +76 -0
- package/assets/screenshots/1-main.png +0 -0
- package/assets/screenshots/2-form-fill.png +0 -0
- package/assets/screenshots/3-challenge.png +0 -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/cordis.patch.yml +23 -48
- package/desktop-bridge/desktop-discovery.d.mts +2 -0
- package/desktop-bridge/desktop-discovery.mjs +36 -0
- package/desktop-bridge/desktop-identity.json +17 -1
- package/desktop-bridge/desktop-processes.d.mts +1 -0
- package/desktop-bridge/desktop-processes.mjs +15 -0
- package/desktop-bridge/guest-proxy.mjs +109 -0
- package/desktop-bridge/install.mjs +197 -74
- package/desktop-bridge/main.mjs +11 -0
- package/desktop-bridge/maintenance-files.d.mts +8 -0
- package/desktop-bridge/maintenance-files.mjs +28 -0
- package/desktop-bridge/maintenance-progress.ps1 +2 -2
- package/desktop-bridge/native-guests.mjs +41 -0
- package/desktop-bridge/plugin-browser-bridge.js +618 -370
- package/desktop-bridge/profile-loader.mjs +26 -0
- package/dist/client.cjs +43167 -0
- package/dist/client.d.cts +516 -0
- package/dist/client.d.mts +516 -0
- package/dist/index.cjs +92 -0
- package/dist/index.d.mts +2960 -0
- package/dist/index.mjs +24 -0
- package/docs/HANDOVER-2026-11-15.md +178 -0
- package/docs/README.md +10 -19
- package/docs/architecture.md +17 -145
- package/docs/child-tabs.md +13 -0
- package/docs/desktop-acceptance.md +9 -0
- package/docs/development.md +88 -18
- package/docs/extension-browser.md +44 -0
- package/docs/hardcoding-audit.md +64 -0
- package/docs/limitations.md +23 -73
- package/docs/pr64-extension-status.md +46 -0
- package/docs/pr64-maintenance.md +25 -0
- package/docs/pr64-native-children.md +52 -0
- package/docs/pr64-native-views.md +60 -0
- package/docs/tool-reference.md +87 -11
- package/docs/user-guide.md +47 -255
- package/docs/why-browser.md +6 -39
- package/dsh-host-patch/README.md +60 -0
- package/dsh-host-patch/apply.mjs +194 -0
- package/dsh-host-patch/legacy-bridge.mjs +53 -0
- package/dsh-host-patch/rc3-revision1.json +90 -0
- package/dsh-host-patch/rc3-revision2.json +112 -0
- package/dsh-host-patch/rc3-revision3.json.gz +0 -0
- package/dsh-host-patch/rc3-revision4.json.gz +0 -0
- package/dsh-host-patch/rc3-revision5.json.gz +0 -0
- package/dsh-host-patch/rc3-revision6.json.gz +0 -0
- package/dsh-host-patch/rc3-revision7.json.gz +0 -0
- package/dsh-host-patch/rc3-revision8.json.gz +0 -0
- package/dsh-host-patch/rc3.json +148 -0
- package/dsh-host-patch/rc5-revision1.json +90 -0
- package/dsh-host-patch/rc5-revision2.json +112 -0
- package/dsh-host-patch/rc5-revision3.json.gz +0 -0
- package/dsh-host-patch/rc5-revision4.json.gz +0 -0
- package/dsh-host-patch/rc5-revision5.json.gz +0 -0
- package/dsh-host-patch/rc5-revision6.json.gz +0 -0
- package/dsh-host-patch/rc5-revision7.json.gz +0 -0
- package/dsh-host-patch/rc5-revision8.json.gz +0 -0
- package/dsh-host-patch/rc5.json +148 -0
- package/lib/bridge-compatibility.js +38 -0
- package/lib/browser/handles.js +78 -0
- package/lib/browser/runtime.js +155 -43
- package/lib/browser/takeover-target.js +24 -0
- package/lib/browser-electron/authenticated-route.js +23 -0
- package/lib/browser-electron/automatic-maintenance.js +394 -51
- package/lib/browser-electron/bridge-auto.js +2 -1
- package/lib/browser-electron/bridge-connection.js +8 -1
- package/lib/browser-electron/bridge-install.js +27 -2
- package/lib/browser-electron/bridge-maintenance.js +35 -10
- package/lib/browser-electron/bridge-status.js +98 -27
- package/lib/browser-electron/desktop-bridge-host.js +193 -560
- package/lib/browser-electron/desktop-identity.js +40 -2
- package/lib/browser-electron/document-readiness.js +34 -0
- package/lib/browser-electron/electron-provision.js +14 -44
- package/lib/browser-electron/entry.js +659 -252
- package/lib/browser-electron/extension-browser.js +234 -0
- package/lib/browser-electron/extension-setup.js +69 -0
- package/lib/browser-electron/guest-cookie-jar.LICENSE.txt +32 -0
- package/lib/browser-electron/guest-cookie-jar.js +10559 -0
- package/lib/browser-electron/guest-cookie-store.js +74 -0
- package/lib/browser-electron/guest-css-runtime.js +75 -0
- package/lib/browser-electron/guest-css.js +106 -0
- package/lib/browser-electron/guest-route.js +603 -0
- package/lib/browser-electron/guest-runtime.js +356 -0
- package/lib/browser-electron/guest-storage-runtime.js +52 -0
- package/lib/browser-electron/guest-storage.js +163 -0
- package/lib/browser-electron/history-store.js +2 -1
- package/lib/browser-electron/host-main.js +162 -7
- package/lib/browser-electron/host-patch-maintenance.js +141 -0
- package/lib/browser-electron/http-body.js +22 -0
- package/lib/browser-electron/maintenance-broker.js +35 -5
- package/lib/browser-electron/maintenance-lifecycle.js +7 -5
- package/lib/browser-electron/maintenance-route.js +9 -6
- package/lib/browser-electron/open-slot.js +47 -0
- package/lib/browser-electron/paths.js +4 -1
- package/lib/browser-electron/preferred-browser.js +144 -0
- package/lib/browser-electron/preview-route.js +11 -8
- package/lib/browser-electron/process-ownership.js +198 -0
- package/lib/browser-electron/profile-checkpoint.js +222 -0
- package/lib/browser-electron/profile-command.js +60 -0
- package/lib/browser-electron/profile-extension.js +126 -0
- package/lib/browser-electron/profile-lease.js +157 -0
- package/lib/browser-electron/profile-residue.js +61 -0
- package/lib/browser-electron/provider.js +719 -262
- package/lib/browser-electron/reference-route.js +635 -0
- package/lib/browser-electron/remote-host.js +12 -13
- package/lib/browser-electron/runtime-route.js +6 -5
- package/lib/browser-electron/runtime-status.js +30 -29
- package/lib/browser-electron/settings-store.js +10 -5
- package/lib/browser-electron/shortcuts-route.js +108 -0
- package/lib/browser-electron/sidebar-only-host.js +119 -18
- package/lib/browser-electron/startpage-shortcuts.js +172 -0
- package/lib/browser-electron/system-browser.js +352 -371
- package/lib/browser-electron/windows-system.js +2 -1
- package/lib/browser-reference/delivery.js +326 -0
- package/lib/browser-reference/entry.js +597 -0
- package/lib/browser-reference/export.js +298 -0
- package/lib/browser-reference/import.js +295 -0
- package/lib/browser-reference/page-script.js +1406 -0
- package/lib/browser-reference/service.js +1062 -0
- package/lib/browser-reference/store.js +177 -0
- package/lib/browser-reference/submission-receipts.js +139 -0
- package/lib/browser-reference/text-quote.js +167 -0
- package/lib/browser-reference/types.js +11 -0
- package/lib/browser-reference/visible.js +311 -0
- package/lib/browser-reference/web-sources.js +97 -0
- package/lib/index.js +10 -3
- package/lib/reference-owner.js +5 -0
- package/lib/reference-page.js +12 -0
- package/lib/reference-source.js +2 -0
- package/lib/reference-text.js +188 -0
- package/lib/startpage-settings.js +63 -0
- package/lib/startpage.js +44 -0
- package/lib/takeover-protocol.js +2 -0
- package/lib/tool-browser/code-tool.js +416 -0
- package/lib/tool-browser/code-worker.js +872 -0
- package/lib/tool-browser/index.js +940 -76
- package/lib/tool-browser/page-probe.js +3 -3
- package/lib/tool-browser/panel-page.js +95 -0
- package/lib/tool-browser/screenshot-image.js +51 -0
- package/lib/tool-browser/takeover.js +205 -0
- package/lib/types.js +1 -0
- package/lib/web-guest.js +26 -0
- package/native-host/client.d.mts +17 -0
- package/native-host/client.mjs +100 -0
- package/native-host/host.mjs +150 -0
- package/native-host/identity.d.mts +2 -0
- package/native-host/identity.mjs +1 -0
- package/native-host/install.mjs +168 -0
- package/native-host/protocol.mjs +75 -0
- package/package.json +80 -92
- package/scripts/build.mjs +66 -0
- package/scripts/bundle-host.mjs +28 -0
- package/scripts/fix-exports.mjs +50 -0
- package/scripts/install-electron.mjs +11 -26
- package/scripts/verify-exports.mjs +47 -0
- package/scripts/verify-preview-sdk.mjs +93 -0
- package/skills/dsh-builtin-browser/SKILL.md +56 -47
- package/skills/dsh-builtin-browser/references/batch.md +31 -1
- 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/handles.ts +89 -0
- package/src/browser/runtime.ts +146 -50
- package/src/browser/takeover-target.ts +36 -0
- package/src/browser/types.ts +19 -7
- package/src/browser-electron/authenticated-route.ts +29 -1
- package/src/browser-electron/automatic-maintenance.ts +368 -56
- package/src/browser-electron/bridge-auto.ts +5 -4
- package/src/browser-electron/bridge-connection.ts +20 -2
- package/src/browser-electron/bridge-endpoint.ts +3 -3
- package/src/browser-electron/bridge-install.ts +40 -4
- package/src/browser-electron/bridge-maintenance.ts +42 -14
- package/src/browser-electron/bridge-status.ts +94 -23
- package/src/browser-electron/desktop-bridge-host.ts +185 -585
- package/src/browser-electron/desktop-identity.ts +53 -2
- package/src/browser-electron/document-readiness.ts +45 -0
- package/src/browser-electron/electron-provision.ts +12 -45
- package/src/browser-electron/entry.ts +1299 -882
- package/src/browser-electron/ephemeral-profile.ts +1 -1
- package/src/browser-electron/extension-browser.ts +223 -0
- package/src/browser-electron/extension-setup.ts +67 -0
- package/src/browser-electron/guest-cookie-jar.ts +176 -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 +568 -0
- package/src/browser-electron/guest-runtime.ts +357 -0
- package/src/browser-electron/guest-storage-runtime.ts +54 -0
- package/src/browser-electron/guest-storage.ts +117 -0
- package/src/browser-electron/history-store.ts +5 -4
- package/src/browser-electron/host-main.ts +156 -11
- package/src/browser-electron/host-patch-maintenance.ts +112 -0
- package/src/browser-electron/http-body.ts +23 -0
- package/src/browser-electron/maintenance-broker.ts +29 -5
- package/src/browser-electron/maintenance-lifecycle.ts +14 -9
- package/src/browser-electron/maintenance-route.ts +17 -14
- package/src/browser-electron/missing-system-browser.ts +2 -2
- package/src/browser-electron/open-slot.ts +49 -0
- package/src/browser-electron/paths.ts +5 -1
- package/src/browser-electron/preferred-browser.ts +120 -0
- package/src/browser-electron/preview-route.ts +18 -11
- package/src/browser-electron/process-ownership.ts +168 -0
- package/src/browser-electron/profile-checkpoint.ts +187 -0
- package/src/browser-electron/profile-command.ts +46 -0
- package/src/browser-electron/profile-extension.ts +124 -0
- package/src/browser-electron/profile-lease.ts +115 -0
- package/src/browser-electron/profile-residue.ts +49 -0
- package/src/browser-electron/provider.ts +771 -289
- package/src/browser-electron/reference-route.ts +608 -0
- package/src/browser-electron/remote-host.ts +15 -15
- package/src/browser-electron/runtime-route.ts +11 -10
- package/src/browser-electron/runtime-status.ts +51 -32
- package/src/browser-electron/settings-store.ts +27 -7
- package/src/browser-electron/shortcuts-route.ts +112 -0
- package/src/browser-electron/sidebar-only-host.ts +118 -19
- package/src/browser-electron/startpage-shortcuts.ts +213 -0
- package/src/browser-electron/system-browser.ts +309 -338
- package/src/browser-electron/windows-system.ts +2 -1
- 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 +1315 -0
- package/src/browser-reference/store.ts +172 -0
- package/src/browser-reference/submission-receipts.ts +122 -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 +103 -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 +176 -0
- package/src/client/browser/child-tabs.ts +14 -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 +52 -0
- package/src/client/css-modules.d.ts +4 -0
- package/src/client/definition.tsx +30 -0
- package/src/client/electron/ElectronWebViewImpl.ts +364 -0
- package/src/client/electron/ElectronWebviewPresentation.ts +138 -0
- package/src/client/electron/NativeChildPresentation.ts +45 -0
- package/src/client/electron/NativeViewPage.ts +251 -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 +293 -0
- package/src/client/locales.ts +857 -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 +189 -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 +471 -0
- package/src/client/reference/client/production/asset-store.ts +144 -0
- package/src/client/reference/client/production/availability.ts +74 -0
- package/src/client/reference/client/production/bytes.ts +36 -0
- package/src/client/reference/client/production/delivery-client.ts +211 -0
- package/src/client/reference/client/production/draft-adapter.ts +508 -0
- package/src/client/reference/client/production/http-transport.ts +217 -0
- package/src/client/reference/client/production/production-submission.ts +310 -0
- package/src/client/reference/client/production/protocol.ts +231 -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 +1101 -0
- package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
- package/src/client/reference/client/production/source-marker.ts +63 -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 +58 -0
- package/src/client/reference/client/reveal.ts +107 -0
- package/src/client/reference/client/scheme.ts +114 -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 +34 -0
- package/{client.js → src/client/settings/panel.ts} +2657 -2231
- package/src/client/shortcut-open.ts +30 -0
- package/src/client/startpage/binding.ts +99 -0
- package/src/client/startpage/controller.ts +303 -0
- package/src/client/startpage/model.ts +279 -0
- package/src/client/view/Browser.module.css +751 -0
- package/src/client/view/BrowserBody.tsx +314 -0
- package/src/client/view/BrowserPresentation.ts +9 -0
- package/src/client/view/BrowserTitle.tsx +17 -0
- package/src/client/view/IframePresentation.ts +344 -0
- package/src/client/view/StartPage.tsx +597 -0
- package/src/client/view/TakeoverBanner.tsx +21 -0
- package/src/index.ts +110 -73
- 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 +197 -0
- package/src/startpage-settings.ts +103 -0
- package/src/startpage.ts +71 -0
- package/src/takeover-protocol.ts +19 -0
- package/src/tool-browser/code-tool.ts +401 -0
- package/src/tool-browser/code-worker.ts +639 -0
- package/src/tool-browser/index.ts +908 -77
- package/src/tool-browser/page-probe.ts +4 -4
- package/src/tool-browser/panel-page.ts +124 -0
- package/src/tool-browser/screenshot-image.ts +49 -0
- package/src/tool-browser/takeover.ts +198 -0
- package/src/types/electron-shim.d.ts +20 -0
- package/src/types.ts +85 -0
- package/src/web-guest.ts +34 -0
- package/tools/accept-desktop.mjs +148 -0
- package/lib/browser/runtime.d.ts +0 -175
- package/lib/browser/types.d.ts +0 -797
- package/lib/browser-electron/authenticated-route.d.ts +0 -27
- package/lib/browser-electron/automatic-maintenance.d.ts +0 -41
- package/lib/browser-electron/bridge-auto.d.ts +0 -93
- package/lib/browser-electron/bridge-connection.d.ts +0 -93
- package/lib/browser-electron/bridge-endpoint.d.ts +0 -30
- package/lib/browser-electron/bridge-install.d.ts +0 -85
- package/lib/browser-electron/bridge-maintenance.d.ts +0 -24
- package/lib/browser-electron/bridge-status.d.ts +0 -50
- package/lib/browser-electron/desktop-bridge-host.d.ts +0 -248
- package/lib/browser-electron/desktop-identity.d.ts +0 -58
- package/lib/browser-electron/electron-provision.d.ts +0 -92
- package/lib/browser-electron/entry.d.ts +0 -64
- package/lib/browser-electron/ephemeral-profile.d.ts +0 -37
- package/lib/browser-electron/file-write.d.ts +0 -2
- package/lib/browser-electron/history-store.d.ts +0 -98
- package/lib/browser-electron/host-main.d.ts +0 -26
- package/lib/browser-electron/http-body.d.ts +0 -24
- package/lib/browser-electron/json-reply.d.ts +0 -33
- package/lib/browser-electron/maintenance-broker.d.ts +0 -35
- package/lib/browser-electron/maintenance-lifecycle.d.ts +0 -32
- package/lib/browser-electron/maintenance-route.d.ts +0 -18
- package/lib/browser-electron/missing-system-browser.d.ts +0 -54
- package/lib/browser-electron/paths.d.ts +0 -4
- package/lib/browser-electron/preview-route.d.ts +0 -89
- package/lib/browser-electron/process.d.ts +0 -14
- package/lib/browser-electron/provider.d.ts +0 -959
- package/lib/browser-electron/remote-host.d.ts +0 -256
- package/lib/browser-electron/rpc-input.d.ts +0 -4
- package/lib/browser-electron/rpc-ops.d.ts +0 -18
- package/lib/browser-electron/runtime-diagnostics.d.ts +0 -12
- package/lib/browser-electron/runtime-route.d.ts +0 -55
- package/lib/browser-electron/runtime-status.d.ts +0 -116
- package/lib/browser-electron/settings-route.d.ts +0 -15
- package/lib/browser-electron/settings-store.d.ts +0 -224
- package/lib/browser-electron/sidebar-only-host.d.ts +0 -36
- package/lib/browser-electron/system-browser.d.ts +0 -367
- package/lib/browser-electron/timing.d.ts +0 -8
- package/lib/browser-electron/virtual-cursor.d.ts +0 -67
- package/lib/browser-electron/windows-system.d.ts +0 -3
- package/lib/index.d.ts +0 -30
- package/lib/tool-browser/element-fields.d.ts +0 -145
- package/lib/tool-browser/index.d.ts +0 -53
- package/lib/tool-browser/page-probe.d.ts +0 -86
- package/scripts/consumer-install-check.mjs +0 -72
- package/scripts/run-tests.mjs +0 -32
- package/scripts/verify-host-compat.mjs +0 -195
- package/scripts/verify-preview-route.mjs +0 -168
- package/scripts/verify-runtime-status.mjs +0 -179
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { EXTENSION_READY_TIMEOUT_MS } from '../../assets/browser-policy.mjs';
|
|
2
|
+
import { ExtensionBrowserViewHost } from './extension-browser.js';
|
|
3
|
+
import { readTabHandle, sessionWireId } from '../browser/handles.js';
|
|
4
|
+
export async function extensionReady(connect, milliseconds = EXTENSION_READY_TIMEOUT_MS) {
|
|
5
|
+
const deadline = Date.now() + milliseconds;
|
|
6
|
+
let timer;
|
|
7
|
+
const attempt = async () => {
|
|
8
|
+
do {
|
|
9
|
+
try {
|
|
10
|
+
await connect();
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch { }
|
|
14
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(100, Math.max(0, deadline - Date.now()))));
|
|
15
|
+
} while (Date.now() < deadline);
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
return await Promise.race([attempt(), new Promise(resolve => { timer = setTimeout(() => resolve(false), milliseconds); })]);
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class PreferredBrowserViewHost {
|
|
26
|
+
browser;
|
|
27
|
+
fallback;
|
|
28
|
+
bound;
|
|
29
|
+
routes = new Map();
|
|
30
|
+
decisions = new Map();
|
|
31
|
+
mapped = new Map();
|
|
32
|
+
released = false;
|
|
33
|
+
selected;
|
|
34
|
+
fallbackReason;
|
|
35
|
+
extension;
|
|
36
|
+
constructor(browser, fallback, client, bound = EXTENSION_READY_TIMEOUT_MS) {
|
|
37
|
+
this.browser = browser;
|
|
38
|
+
this.fallback = fallback;
|
|
39
|
+
this.bound = bound;
|
|
40
|
+
this.extension = new ExtensionBrowserViewHost(browser, client);
|
|
41
|
+
}
|
|
42
|
+
activeCarrier() { return this.selected ?? (this.extension.connectionStatus().connected ? 'extension' : 'cdp'); }
|
|
43
|
+
get disposed() { return this.released; }
|
|
44
|
+
connectionStatus() { return this.extension.connectionStatus(); }
|
|
45
|
+
async refreshStatus() { await this.extension.refreshStatus(); }
|
|
46
|
+
async prepare(scope) { if (scope)
|
|
47
|
+
await (await this.route(scope)).prepare?.(scope); }
|
|
48
|
+
referenceOverlayOwnerFor(scope) {
|
|
49
|
+
const host = this.decisions.get(scope);
|
|
50
|
+
return host ? host.referenceOverlayOwner : 'client';
|
|
51
|
+
}
|
|
52
|
+
connectedView(id) {
|
|
53
|
+
const view = this.mapped.get(id);
|
|
54
|
+
return !!view && !view.dead && (this.decisions.get(sessionWireId(readTabHandle(id))) === this.fallback
|
|
55
|
+
? this.fallback.connectionStatus?.().connected === true : this.extension.connectedView(id));
|
|
56
|
+
}
|
|
57
|
+
route(scope) {
|
|
58
|
+
if (this.released)
|
|
59
|
+
return Promise.reject(new Error('browser carrier was released'));
|
|
60
|
+
let route = this.routes.get(scope);
|
|
61
|
+
if (!route) {
|
|
62
|
+
route = (async () => {
|
|
63
|
+
const ready = await extensionReady(() => this.extension.prepare(), this.bound);
|
|
64
|
+
if (this.released)
|
|
65
|
+
throw new Error('browser carrier was released');
|
|
66
|
+
this.selected = ready ? 'extension' : 'cdp';
|
|
67
|
+
const state = this.extension.connectionStatus().state;
|
|
68
|
+
this.fallbackReason = ready ? undefined : state === 'disabled' ? 'disabled' : state === 'extension-missing' ? 'extension-not-installed' : 'not-connected';
|
|
69
|
+
const host = ready ? this.extension : this.fallback;
|
|
70
|
+
this.decisions.set(scope, host);
|
|
71
|
+
return host;
|
|
72
|
+
})();
|
|
73
|
+
this.routes.set(scope, route);
|
|
74
|
+
}
|
|
75
|
+
return route;
|
|
76
|
+
}
|
|
77
|
+
createView(id) {
|
|
78
|
+
if (this.mapped.has(id))
|
|
79
|
+
throw new Error('browser view already exists');
|
|
80
|
+
const view = {
|
|
81
|
+
dead: false, handle: { id, supportsCdpJpeg: true, sendCommand: async (method, params) => {
|
|
82
|
+
if (view.dead || this.released)
|
|
83
|
+
throw new Error('browser view was released');
|
|
84
|
+
view.real ??= this.route(sessionWireId(readTabHandle(id))).then(host => {
|
|
85
|
+
if (view.dead || this.released)
|
|
86
|
+
throw new Error('browser view was released');
|
|
87
|
+
return { host, handle: host.createView(id) };
|
|
88
|
+
});
|
|
89
|
+
const real = await view.real;
|
|
90
|
+
return real.handle.sendCommand(method, params);
|
|
91
|
+
} },
|
|
92
|
+
};
|
|
93
|
+
this.mapped.set(id, view);
|
|
94
|
+
return view.handle;
|
|
95
|
+
}
|
|
96
|
+
destroyView(handle) {
|
|
97
|
+
const view = this.mapped.get(handle.id);
|
|
98
|
+
if (!view)
|
|
99
|
+
return;
|
|
100
|
+
view.dead = true;
|
|
101
|
+
this.mapped.delete(handle.id);
|
|
102
|
+
void view.real?.then(real => real.host.destroyView(real.handle)).catch(() => { });
|
|
103
|
+
}
|
|
104
|
+
async listPages(scope, options = {}) {
|
|
105
|
+
if (options.requireRunning && !this.routes.has(scope))
|
|
106
|
+
return [];
|
|
107
|
+
return (await this.route(scope)).listPages?.(scope, options) ?? [];
|
|
108
|
+
}
|
|
109
|
+
async adoptPage(id, scope = sessionWireId(readTabHandle(id))) {
|
|
110
|
+
const host = await this.route(scope);
|
|
111
|
+
if (!host.adoptPage)
|
|
112
|
+
throw new Error('carrier does not support page adoption');
|
|
113
|
+
const handle = await host.adoptPage(id, scope);
|
|
114
|
+
this.mapped.set(id, { handle, real: Promise.resolve({ host, handle }), dead: false });
|
|
115
|
+
return handle;
|
|
116
|
+
}
|
|
117
|
+
async closePage(id, scope = sessionWireId(readTabHandle(id))) {
|
|
118
|
+
const host = await this.route(scope);
|
|
119
|
+
if (!host.closePage)
|
|
120
|
+
throw new Error('carrier does not support explicit close');
|
|
121
|
+
await host.closePage(id, scope);
|
|
122
|
+
const view = this.mapped.get(id);
|
|
123
|
+
if (view)
|
|
124
|
+
this.destroyView(view.handle);
|
|
125
|
+
}
|
|
126
|
+
async retireSession(scope) {
|
|
127
|
+
try {
|
|
128
|
+
await (await this.routes.get(scope))?.retireSession?.(scope);
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
this.routes.delete(scope);
|
|
132
|
+
this.decisions.delete(scope);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
dispose() {
|
|
136
|
+
if (this.released)
|
|
137
|
+
return;
|
|
138
|
+
this.released = true;
|
|
139
|
+
for (const view of [...this.mapped.values()])
|
|
140
|
+
this.destroyView(view.handle);
|
|
141
|
+
this.extension.dispose();
|
|
142
|
+
this.fallback.dispose?.();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { PREVIEW_ROUTE, PREVIEW_SESSIONS_ROUTE, BRIDGE_STATUS_ROUTE } from '../../assets/browser-policy.mjs';
|
|
2
|
+
import { URL_PARSE_BASE } from '../../assets/browser-policy.mjs';
|
|
1
3
|
import { replyJson } from './json-reply.js';
|
|
2
4
|
import { sameOrigin } from './settings-route.js';
|
|
3
5
|
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js';
|
|
4
6
|
/** Where the frame endpoint lives. The viewer must use exactly this path, same origin. */
|
|
5
|
-
export
|
|
7
|
+
export { PREVIEW_ROUTE };
|
|
6
8
|
/**
|
|
7
9
|
* Where the viewer asks which sessions it may watch.
|
|
8
10
|
*
|
|
@@ -11,7 +13,7 @@ export const PREVIEW_ROUTE = '/dsh-builtin-browser/browser-view/frame';
|
|
|
11
13
|
* is exactly what the design forbids. So the list is served, and it contains nothing but ids of
|
|
12
14
|
* sessions that currently have a page — no URLs, no titles, no target handles.
|
|
13
15
|
*/
|
|
14
|
-
export
|
|
16
|
+
export { PREVIEW_SESSIONS_ROUTE };
|
|
15
17
|
/**
|
|
16
18
|
* Where the settings panel asks whether the desktop bridge is installed.
|
|
17
19
|
*
|
|
@@ -20,7 +22,7 @@ export const PREVIEW_SESSIONS_ROUTE = '/dsh-builtin-browser/browser-view/session
|
|
|
20
22
|
* The reading is taken from the install tree on disk, so it is true even when the plugin has just
|
|
21
23
|
* started and remembers nothing.
|
|
22
24
|
*/
|
|
23
|
-
export
|
|
25
|
+
export { BRIDGE_STATUS_ROUTE };
|
|
24
26
|
/**
|
|
25
27
|
* List the sessions a viewer may watch.
|
|
26
28
|
*
|
|
@@ -79,7 +81,7 @@ export async function handlePreviewFrame(request, response, settings, browser) {
|
|
|
79
81
|
replyJson(response, 403, { error: 'remote-view-disabled' });
|
|
80
82
|
return;
|
|
81
83
|
}
|
|
82
|
-
const url = new URL(request.url ?? '/',
|
|
84
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE);
|
|
83
85
|
const session = url.searchParams.get('session') ?? '';
|
|
84
86
|
if (session === '') {
|
|
85
87
|
replyJson(response, 400, { error: 'session-required' });
|
|
@@ -139,7 +141,7 @@ export async function handlePreviewFrame(request, response, settings, browser) {
|
|
|
139
141
|
* @param response - the response to write.
|
|
140
142
|
* @param localAppData - the base directory to probe; injected so a test can point at a fixture.
|
|
141
143
|
*/
|
|
142
|
-
export async function handleBridgeStatus(request, response, localAppData) {
|
|
144
|
+
export async function handleBridgeStatus(request, response, localAppData, mainExtension, registry, conventions) {
|
|
143
145
|
if (request.method !== 'GET') {
|
|
144
146
|
replyJson(response, 405, { error: 'method-not-allowed' });
|
|
145
147
|
return;
|
|
@@ -148,14 +150,15 @@ export async function handleBridgeStatus(request, response, localAppData) {
|
|
|
148
150
|
replyJson(response, 403, { error: 'not-same-origin' });
|
|
149
151
|
return;
|
|
150
152
|
}
|
|
151
|
-
const installRoot = findDesktopInstall(localAppData);
|
|
153
|
+
const installRoot = findDesktopInstall(localAppData, registry, conventions);
|
|
152
154
|
const status = installRoot === undefined
|
|
153
155
|
? {
|
|
154
156
|
state: 'missing',
|
|
155
157
|
present: false,
|
|
156
158
|
archivePresent: false,
|
|
157
|
-
detail: 'no DSH Desktop install found
|
|
159
|
+
detail: 'no DSH Desktop install found in the usual roots, at DSH_DESKTOP_PATH or DSH_DESKTOP_APP, '
|
|
160
|
+
+ 'in the uninstall registry, or at this platform\'s conventional locations',
|
|
158
161
|
}
|
|
159
|
-
: readBridgeStatus(installRoot);
|
|
162
|
+
: readBridgeStatus(installRoot, mainExtension);
|
|
160
163
|
replyJson(response, 200, { ok: true, bridge: status });
|
|
161
164
|
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { PERSONAL_PROFILE_DIRS } from '../../assets/browser-products.mjs';
|
|
2
|
+
import { windowsPowerShellPath } from './windows-system.js';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { readFile, readlink } from 'node:fs/promises';
|
|
5
|
+
import { realpathSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
8
|
+
import { promisify } from 'node:util';
|
|
9
|
+
const execute = promisify(execFile);
|
|
10
|
+
const windowsInspection = String.raw `
|
|
11
|
+
$ErrorActionPreference='Stop'
|
|
12
|
+
try {
|
|
13
|
+
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
|
|
14
|
+
$p=Get-CimInstance Win32_Process -Filter ('ProcessId='+$env:DSH_INSPECT_PID)
|
|
15
|
+
if($null -eq $p -or !$p.ExecutablePath -or !$p.CommandLine -or !$p.CreationDate) { throw 'Process identity is unavailable' }
|
|
16
|
+
@{pid=[int]$p.ProcessId;created=$p.CreationDate.ToUniversalTime().Ticks.ToString();
|
|
17
|
+
executable=$p.ExecutablePath;commandLine=$p.CommandLine} | ConvertTo-Json -Compress
|
|
18
|
+
} catch { [Console]::Error.WriteLine($_.Exception.Message); exit 2 }
|
|
19
|
+
`;
|
|
20
|
+
export function parseWindowsCommandLine(command) {
|
|
21
|
+
const argv = [];
|
|
22
|
+
let value = '', quoted = false, started = false;
|
|
23
|
+
for (let i = 0; i < command.length; i++) {
|
|
24
|
+
const char = command[i];
|
|
25
|
+
if ((char === ' ' || char === '\t') && !quoted) {
|
|
26
|
+
if (started)
|
|
27
|
+
argv.push(value);
|
|
28
|
+
value = '';
|
|
29
|
+
started = false;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
started = true;
|
|
33
|
+
if (char === '\\') {
|
|
34
|
+
let count = 1;
|
|
35
|
+
while (command[i + 1] === '\\') {
|
|
36
|
+
count++;
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
if (command[i + 1] !== '"') {
|
|
40
|
+
value += '\\'.repeat(count);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
value += '\\'.repeat(Math.floor(count / 2));
|
|
44
|
+
if (count % 2) {
|
|
45
|
+
value += '"';
|
|
46
|
+
i++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
i++;
|
|
50
|
+
}
|
|
51
|
+
else if (char !== '"') {
|
|
52
|
+
value += char;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (quoted && command[i + 1] === '"') {
|
|
56
|
+
value += '"';
|
|
57
|
+
i++;
|
|
58
|
+
}
|
|
59
|
+
else
|
|
60
|
+
quoted = !quoted;
|
|
61
|
+
}
|
|
62
|
+
if (started)
|
|
63
|
+
argv.push(value);
|
|
64
|
+
return argv;
|
|
65
|
+
}
|
|
66
|
+
export async function inspectProcess(pid, diagnose) {
|
|
67
|
+
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
68
|
+
return undefined;
|
|
69
|
+
try {
|
|
70
|
+
if (process.platform === 'win32') {
|
|
71
|
+
const { stdout } = await execute(windowsPowerShellPath(), ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(windowsInspection, 'utf16le').toString('base64')], { windowsHide: true, timeout: 15000, maxBuffer: 128 * 1024, env: { ...process.env, DSH_INSPECT_PID: String(pid) } });
|
|
72
|
+
const result = JSON.parse(stdout.replace(/^\uFEFF/u, ''));
|
|
73
|
+
if (result.pid !== pid || typeof result.created !== 'string' || !result.created
|
|
74
|
+
|| typeof result.executable !== 'string' || !result.executable
|
|
75
|
+
|| typeof result.commandLine !== 'string' || !result.commandLine)
|
|
76
|
+
throw new Error('Windows returned an incomplete or mismatched process identity');
|
|
77
|
+
return { pid, created: result.created, executable: result.executable, argv: parseWindowsCommandLine(result.commandLine) };
|
|
78
|
+
}
|
|
79
|
+
if (process.platform === 'linux') {
|
|
80
|
+
const root = `/proc/${pid}`;
|
|
81
|
+
const before = await readFile(`${root}/stat`, 'utf8');
|
|
82
|
+
const executable = await readlink(`${root}/exe`);
|
|
83
|
+
const command = await readFile(`${root}/cmdline`, 'utf8');
|
|
84
|
+
const after = await readFile(`${root}/stat`, 'utf8');
|
|
85
|
+
const start = (stat) => stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19];
|
|
86
|
+
const created = start(before);
|
|
87
|
+
if (!created || created !== start(after) || !command.endsWith('\0'))
|
|
88
|
+
return undefined;
|
|
89
|
+
return { pid, created, executable, argv: command.slice(0, -1).split('\0') };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
diagnose?.(String(error).slice(-2000));
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
function canonical(path) {
|
|
98
|
+
const actual = realpathSync(path);
|
|
99
|
+
return process.platform === 'win32' ? actual.toLowerCase() : actual;
|
|
100
|
+
}
|
|
101
|
+
function personalProfile(path) {
|
|
102
|
+
const roots = process.platform === 'win32'
|
|
103
|
+
? PERSONAL_PROFILE_DIRS.win32
|
|
104
|
+
.map(name => join(process.env.LOCALAPPDATA || homedir(), name))
|
|
105
|
+
: process.platform === 'darwin'
|
|
106
|
+
? PERSONAL_PROFILE_DIRS.darwin.map(name => join(homedir(), 'Library', 'Application Support', name))
|
|
107
|
+
: PERSONAL_PROFILE_DIRS.linux.map(name => join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), name));
|
|
108
|
+
return roots.some(root => {
|
|
109
|
+
let actual;
|
|
110
|
+
try {
|
|
111
|
+
actual = canonical(root);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
actual = resolve(root);
|
|
115
|
+
if (process.platform === 'win32')
|
|
116
|
+
actual = actual.toLowerCase();
|
|
117
|
+
}
|
|
118
|
+
const child = relative(actual, path);
|
|
119
|
+
return child === '' || (child !== '..' && !child.startsWith(`..${sep}`) && !isAbsolute(child));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function dataDirectory(argv) {
|
|
123
|
+
const values = [];
|
|
124
|
+
for (let i = 1; i < argv.length; i++) {
|
|
125
|
+
const arg = argv[i];
|
|
126
|
+
if (arg === '--user-data-dir')
|
|
127
|
+
values.push(argv[++i] ?? '');
|
|
128
|
+
else if (arg.startsWith('--user-data-dir='))
|
|
129
|
+
values.push(arg.slice('--user-data-dir='.length));
|
|
130
|
+
}
|
|
131
|
+
return values.length === 1 && values[0] !== '' ? values[0] : undefined;
|
|
132
|
+
}
|
|
133
|
+
export class BrowserProcessOwnership {
|
|
134
|
+
executable;
|
|
135
|
+
directory;
|
|
136
|
+
options;
|
|
137
|
+
records = new WeakMap();
|
|
138
|
+
constructor(executable, directory, options) {
|
|
139
|
+
this.executable = executable;
|
|
140
|
+
this.directory = directory;
|
|
141
|
+
this.options = options;
|
|
142
|
+
}
|
|
143
|
+
live(child) {
|
|
144
|
+
return Number.isSafeInteger(child.pid) && child.exitCode === null && child.signalCode === null;
|
|
145
|
+
}
|
|
146
|
+
async capture(child) {
|
|
147
|
+
if (!this.options.managed || !this.live(child))
|
|
148
|
+
return;
|
|
149
|
+
try {
|
|
150
|
+
const record = await (this.options.inspect ?? inspectProcess)(child.pid);
|
|
151
|
+
if (!record || !this.live(child) || record.pid !== child.pid || !record.created)
|
|
152
|
+
return;
|
|
153
|
+
const directory = canonical(this.directory), argument = dataDirectory(record.argv);
|
|
154
|
+
if (!argument || personalProfile(directory) || canonical(argument) !== directory
|
|
155
|
+
|| canonical(record.executable) !== canonical(this.executable))
|
|
156
|
+
return;
|
|
157
|
+
this.records.set(child, { identity: record, executable: canonical(record.executable), directory });
|
|
158
|
+
}
|
|
159
|
+
catch { /* Fail closed. */ }
|
|
160
|
+
}
|
|
161
|
+
async verify(child) {
|
|
162
|
+
const captured = this.records.get(child);
|
|
163
|
+
if (!captured || !this.live(child))
|
|
164
|
+
return false;
|
|
165
|
+
const record = captured.identity;
|
|
166
|
+
try {
|
|
167
|
+
const current = await (this.options.inspect ?? inspectProcess)(record.pid);
|
|
168
|
+
return this.live(child) && current !== undefined && current.pid === record.pid
|
|
169
|
+
&& current.created === record.created && current.executable === record.executable
|
|
170
|
+
&& JSON.stringify(current.argv) === JSON.stringify(record.argv)
|
|
171
|
+
&& canonical(current.executable) === captured.executable && canonical(this.executable) === captured.executable
|
|
172
|
+
&& canonical(dataDirectory(current.argv) ?? '') === captured.directory && this.ownsDirectory(child);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
ownsDirectory(child, path = this.directory) {
|
|
179
|
+
const captured = this.records.get(child);
|
|
180
|
+
try {
|
|
181
|
+
return captured !== undefined && canonical(path) === captured.directory
|
|
182
|
+
&& canonical(this.directory) === captured.directory && !personalProfile(captured.directory);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async terminate(child) {
|
|
189
|
+
if (!await this.verify(child) || !this.live(child))
|
|
190
|
+
return false;
|
|
191
|
+
try {
|
|
192
|
+
return child.kill();
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { copyFile, lstat, mkdir, readdir, readlink, rename, rm, rmdir, stat, symlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { installedProfileExtension, MAIN_EXTENSION_NAME } from './profile-extension.js';
|
|
4
|
+
import { MaintenanceRecoveryError } from './maintenance-lifecycle.js';
|
|
5
|
+
import { runProfileCommand } from './profile-command.js';
|
|
6
|
+
const pluginPath = join('node_modules', MAIN_EXTENSION_NAME);
|
|
7
|
+
const manifestPaths = ['package.json', 'pnpm-lock.yaml', join('node_modules', '.modules.yaml')];
|
|
8
|
+
async function describe(profile, path, packageEntry = false) {
|
|
9
|
+
const source = join(profile, path);
|
|
10
|
+
let info;
|
|
11
|
+
try {
|
|
12
|
+
info = await lstat(source);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code === 'ENOENT')
|
|
16
|
+
return { path, kind: 'missing' };
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
if (info.isSymbolicLink()) {
|
|
20
|
+
const target = await readlink(source);
|
|
21
|
+
let type = 'directory';
|
|
22
|
+
if (!packageEntry) {
|
|
23
|
+
try {
|
|
24
|
+
type = (await stat(source)).isDirectory() ? 'directory' : 'file';
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (error.code !== 'ENOENT')
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return { path, kind: 'link', target, type };
|
|
32
|
+
}
|
|
33
|
+
return { path, kind: info.isDirectory() ? 'directory' : 'file' };
|
|
34
|
+
}
|
|
35
|
+
async function inventory(profile, io) {
|
|
36
|
+
const entries = [];
|
|
37
|
+
for (const name of (await io.readdir(join(profile, 'node_modules'))).sort()) {
|
|
38
|
+
const path = join('node_modules', name), entry = await describe(profile, path, true);
|
|
39
|
+
entries.push(entry);
|
|
40
|
+
if (name.startsWith('@') && entry.kind === 'directory') {
|
|
41
|
+
for (const child of (await io.readdir(join(profile, path))).sort())
|
|
42
|
+
entries.push(await describe(profile, join(path, child), true));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return entries;
|
|
46
|
+
}
|
|
47
|
+
async function saveProfile(profile, before, io) {
|
|
48
|
+
const entries = [];
|
|
49
|
+
const visit = async (path, packageEntry = false) => {
|
|
50
|
+
const entry = await describe(profile, path, packageEntry);
|
|
51
|
+
entries.push(entry);
|
|
52
|
+
const destination = join(before, 'files', path);
|
|
53
|
+
if (entry.kind === 'directory') {
|
|
54
|
+
await mkdir(destination, { recursive: true });
|
|
55
|
+
for (const name of (await io.readdir(join(profile, path))).sort())
|
|
56
|
+
await visit(join(path, name));
|
|
57
|
+
}
|
|
58
|
+
else if (entry.kind === 'file') {
|
|
59
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
60
|
+
await io.copyFile(join(profile, path), destination);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
if ((await lstat(join(profile, 'node_modules'))).isSymbolicLink())
|
|
64
|
+
throw new Error('REINSTALL_CHECKPOINT_LINKED_MODULES: node_modules must be a real directory');
|
|
65
|
+
const modules = await inventory(profile, io);
|
|
66
|
+
for (const path of manifestPaths)
|
|
67
|
+
await visit(path);
|
|
68
|
+
await visit(pluginPath, true);
|
|
69
|
+
await writeFile(join(before, 'metadata.json'), JSON.stringify({ version: 2, entries, modules }));
|
|
70
|
+
return { entries, modules };
|
|
71
|
+
}
|
|
72
|
+
async function restoreLink(profile, entry, io) {
|
|
73
|
+
const destination = join(profile, entry.path);
|
|
74
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
75
|
+
try {
|
|
76
|
+
await io.symlink(entry.type === 'directory' && process.platform === 'win32' ? resolve(dirname(destination), entry.target) : entry.target, destination, entry.type === 'directory' ? 'junction' : 'file');
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw new Error(`REINSTALL_LINK_RESTORE_FAILED: ${entry.type} link ${entry.path}: ${String(error)}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function restoreFiles(profile, before, entries, io) {
|
|
83
|
+
const errors = [];
|
|
84
|
+
for (const path of [...manifestPaths, pluginPath]) {
|
|
85
|
+
try {
|
|
86
|
+
await rm(join(profile, path), { recursive: true, force: true });
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
errors.push(`${path}: ${String(error)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const entry of entries) {
|
|
93
|
+
try {
|
|
94
|
+
const destination = join(profile, entry.path);
|
|
95
|
+
if (entry.kind === 'directory')
|
|
96
|
+
await mkdir(destination, { recursive: true });
|
|
97
|
+
else if (entry.kind === 'file') {
|
|
98
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
99
|
+
await io.copyFile(join(before, 'files', entry.path), destination);
|
|
100
|
+
}
|
|
101
|
+
else if (entry.kind === 'link')
|
|
102
|
+
await restoreLink(profile, entry, io);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
errors.push(`${entry.path}: ${String(error)}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (errors.length > 0)
|
|
109
|
+
throw new Error(errors.join('; '));
|
|
110
|
+
}
|
|
111
|
+
function unrelated(entries) {
|
|
112
|
+
return JSON.stringify(entries.filter(entry => entry.path !== pluginPath && !manifestPaths.includes(entry.path) && entry.path !== join('node_modules', '.ignored')));
|
|
113
|
+
}
|
|
114
|
+
async function recoverModules(profile, modules, io) {
|
|
115
|
+
const ignored = join('node_modules', '.ignored');
|
|
116
|
+
const recover = async (entry) => {
|
|
117
|
+
if (entry.path === pluginPath || manifestPaths.includes(entry.path) || entry.path === ignored)
|
|
118
|
+
return;
|
|
119
|
+
const current = await describe(profile, entry.path, true);
|
|
120
|
+
if (JSON.stringify(current) === JSON.stringify(entry))
|
|
121
|
+
return;
|
|
122
|
+
if (current.kind !== 'missing')
|
|
123
|
+
throw new Error(`REINSTALL_UNRELATED_PACKAGE_CHANGED: ${entry.path}`);
|
|
124
|
+
if (entry.kind === 'link')
|
|
125
|
+
await restoreLink(profile, entry, io);
|
|
126
|
+
else {
|
|
127
|
+
const relative = entry.path.slice('node_modules'.length + 1), parts = relative.split(/[\\/]/);
|
|
128
|
+
for (let i = 0; i < parts.length; i++) {
|
|
129
|
+
const parent = await describe(profile, join(ignored, ...parts.slice(0, i)), true);
|
|
130
|
+
if (parent.kind !== 'directory')
|
|
131
|
+
throw new Error(`REINSTALL_ORPHAN_RECOVERY_FAILED: unsafe or missing .ignored path for ${entry.path}`);
|
|
132
|
+
}
|
|
133
|
+
await mkdir(dirname(join(profile, entry.path)), { recursive: true });
|
|
134
|
+
await rename(join(profile, ignored, relative), join(profile, entry.path));
|
|
135
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
136
|
+
if (i === 0 && modules.some(entry => entry.path === ignored))
|
|
137
|
+
break;
|
|
138
|
+
try {
|
|
139
|
+
await rmdir(join(profile, ignored, ...parts.slice(0, i)));
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (!['ENOENT', 'ENOTEMPTY'].includes(error.code ?? ''))
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const errors = [];
|
|
149
|
+
for (const entry of modules) {
|
|
150
|
+
try {
|
|
151
|
+
await recover(entry);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
errors.push(String(error));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (errors.length > 0)
|
|
158
|
+
throw new Error(errors.join('; '));
|
|
159
|
+
}
|
|
160
|
+
async function verifyProfile(home) {
|
|
161
|
+
await installedProfileExtension(home);
|
|
162
|
+
const script = `const {createRequire} = await import('node:module'); const {readFile} = await import('node:fs/promises'); await readFile(createRequire(process.argv[1]).resolve(${JSON.stringify(MAIN_EXTENSION_NAME)}));`;
|
|
163
|
+
const isElectron = process.versions.electron !== undefined;
|
|
164
|
+
const argv = [...(isElectron ? ['--expose-internals'] : []), '--input-type=module', '--eval', script, '--', join(home, 'profiles', 'desktop', 'package.json')];
|
|
165
|
+
const env = { ...process.env, ...(isElectron ? { ELECTRON_RUN_AS_NODE: '1', ELECTRON_NO_ASAR: '1' } : {}) };
|
|
166
|
+
const result = await runProfileCommand(process.execPath, argv, env, 10_000);
|
|
167
|
+
if (result.code !== 0)
|
|
168
|
+
throw new Error(`REINSTALL_PROFILE_RESOLUTION_FAILED: ${result.output}`);
|
|
169
|
+
}
|
|
170
|
+
/** The checkpoint lives beside the staged worker, never inside the removable profile. */
|
|
171
|
+
export async function withProfileCheckpoint(home, directory, operation, options) {
|
|
172
|
+
const profile = join(home, 'profiles', 'desktop'), before = join(directory, 'profile-before');
|
|
173
|
+
const io = { copyFile, symlink, readdir, ...options };
|
|
174
|
+
let saved;
|
|
175
|
+
let created = false;
|
|
176
|
+
try {
|
|
177
|
+
await verifyProfile(home);
|
|
178
|
+
await mkdir(before);
|
|
179
|
+
created = true;
|
|
180
|
+
saved = await saveProfile(profile, before, io);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (created)
|
|
184
|
+
await rm(before, { recursive: true, force: true });
|
|
185
|
+
throw new Error(`REINSTALL_CHECKPOINT_FAILED: profile unchanged: ${String(error)}`);
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const result = await operation();
|
|
189
|
+
if (unrelated(await inventory(profile, io)) !== unrelated(saved.modules))
|
|
190
|
+
throw new Error('REINSTALL_UNRELATED_PACKAGES_CHANGED: package manager changed unrelated entries; restoring the profile');
|
|
191
|
+
await verifyProfile(home);
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
catch (failure) {
|
|
195
|
+
const attempt = async (restore) => {
|
|
196
|
+
try {
|
|
197
|
+
await restore();
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
return String(error);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
let filesError = await attempt(() => restoreFiles(profile, before, saved.entries, io));
|
|
205
|
+
let modulesError = await attempt(() => recoverModules(profile, saved.modules, io));
|
|
206
|
+
let resolutionError = await attempt(() => verifyProfile(home));
|
|
207
|
+
let reconcileError;
|
|
208
|
+
if (filesError === undefined && (modulesError !== undefined || resolutionError !== undefined)) {
|
|
209
|
+
reconcileError = await attempt(() => io.reconcile());
|
|
210
|
+
filesError = await attempt(() => restoreFiles(profile, before, saved.entries, io));
|
|
211
|
+
modulesError = await attempt(() => recoverModules(profile, saved.modules, io));
|
|
212
|
+
resolutionError = await attempt(() => verifyProfile(home));
|
|
213
|
+
}
|
|
214
|
+
const restored = filesError === undefined && resolutionError === undefined
|
|
215
|
+
? 'REINSTALL_PROFILE_RESTORED: profile manifests and plugin restored directly and plugin resolution verified'
|
|
216
|
+
: 'Profile restoration could not be fully verified';
|
|
217
|
+
const errors = [filesError, modulesError, resolutionError, reconcileError].filter(error => error !== undefined);
|
|
218
|
+
if (errors.length > 0)
|
|
219
|
+
throw new MaintenanceRecoveryError(`REINSTALL_ROLLBACK_FAILED: ${errors.join('; ')}; ${restored}; original failure: ${String(failure)}; checkpoint: ${before}`);
|
|
220
|
+
throw new Error(`${String(failure)}; ${restored}`);
|
|
221
|
+
}
|
|
222
|
+
}
|