dsh-builtin-browser 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -1
- 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/desktop-bridge/desktop-identity.json +10 -2
- package/desktop-bridge/install.mjs +28 -2
- package/docs/issue-replies-2026-11-28.md +141 -0
- package/dsh-host-patch/apply.mjs +1 -1
- package/dsh-host-patch/rc3.json +4 -4
- package/dsh-host-patch/rc5.json +4 -4
- package/dsh-host-patch/v023-alpha2.json +2 -2
- package/lib/browser-electron/bridge-status.js +24 -1
- package/lib/browser-electron/desktop-identity.js +47 -8
- package/lib/client.js +2 -1
- package/native-host/install.mjs +5 -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 +412 -0
- package/src/browser-electron/desktop-bridge-host.ts +253 -0
- package/src/browser-electron/desktop-identity.ts +400 -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 +2773 -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,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visible-region snapshots and image-version verification.
|
|
3
|
+
*
|
|
4
|
+
* The design (§8.1/§8.2, B33/B57/B58/B12) asks for three things the plain
|
|
5
|
+
* capture path cannot do:
|
|
6
|
+
* - record viewport / scroll / zoom / DPR before a screenshot;
|
|
7
|
+
* - mask the unselected parts of a multi-column or discontinuous selection
|
|
8
|
+
* instead of saving one big bounding box containing neighbouring content;
|
|
9
|
+
* - prove that bytes downloaded from a URL are the version the page had
|
|
10
|
+
* loaded, so a re-download that returns a newer asset is not advertised
|
|
11
|
+
* as the source image the human selected.
|
|
12
|
+
*
|
|
13
|
+
* Everything that comes back from the page is untrusted input: shapes are
|
|
14
|
+
* validated field by field and every number is clamped. A probe that cannot be
|
|
15
|
+
* collected is reported as `unverifiable`, never as `verified`.
|
|
16
|
+
* @module dsh-browser/browser-reference/visible
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Hard caps: a page can claim neither an unbounded selection nor an unbounded clip. */
|
|
20
|
+
export const MAX_VISIBLE_RECTS = 48
|
|
21
|
+
export const MAX_REGION_EDGE = 8_192
|
|
22
|
+
export const MAX_FACT_STRING = 4_000
|
|
23
|
+
|
|
24
|
+
export const MASK_HOST_ID = '__dsh-ref-visible-mask'
|
|
25
|
+
export const OVERLAY_HOST_ID = '__dsh-ref-overlay'
|
|
26
|
+
export const HIGHLIGHT_HOST_ID = '__dsh-ref-highlight'
|
|
27
|
+
|
|
28
|
+
/** One selected client rectangle, in CSS pixels relative to the viewport. */
|
|
29
|
+
export interface VisibleRect {
|
|
30
|
+
readonly x: number
|
|
31
|
+
readonly y: number
|
|
32
|
+
readonly width: number
|
|
33
|
+
readonly height: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Page facts sampled immediately before a visible-region screenshot. */
|
|
37
|
+
export interface ScreenshotFacts {
|
|
38
|
+
readonly url: string
|
|
39
|
+
readonly title: string
|
|
40
|
+
readonly frameUrl: string
|
|
41
|
+
readonly frameRoute: 'main' | 'frame'
|
|
42
|
+
readonly dpr: number
|
|
43
|
+
readonly scrollX: number
|
|
44
|
+
readonly scrollY: number
|
|
45
|
+
readonly viewportWidth: number
|
|
46
|
+
readonly viewportHeight: number
|
|
47
|
+
readonly visualScale: number
|
|
48
|
+
readonly rects: readonly VisibleRect[]
|
|
49
|
+
readonly multipleRanges: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** CDP `Page.captureScreenshot` clip, in document CSS pixels. */
|
|
53
|
+
export interface VisibleClip {
|
|
54
|
+
readonly x: number
|
|
55
|
+
readonly y: number
|
|
56
|
+
readonly width: number
|
|
57
|
+
readonly height: number
|
|
58
|
+
readonly scale: 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What the host needs to describe the saved region honestly. */
|
|
62
|
+
export interface VisibleCaptureMeta {
|
|
63
|
+
readonly clip: VisibleClip
|
|
64
|
+
readonly rects: number
|
|
65
|
+
readonly partiallyVisible: boolean
|
|
66
|
+
readonly dpr: number
|
|
67
|
+
readonly visualScale: number
|
|
68
|
+
readonly viewport: { readonly width: number; readonly height: number }
|
|
69
|
+
readonly scroll: { readonly x: number; readonly y: number }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Page-side result of hashing the bytes the page session can still reach. */
|
|
73
|
+
export interface VersionProbe {
|
|
74
|
+
readonly ok: boolean
|
|
75
|
+
readonly length?: number
|
|
76
|
+
readonly digest?: string
|
|
77
|
+
readonly reason?: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type VersionStatus = 'verified' | 'changed' | 'unverifiable'
|
|
81
|
+
|
|
82
|
+
/** What a host must return for one visible-region screenshot. */
|
|
83
|
+
export interface VisibleCaptureResult {
|
|
84
|
+
readonly dataUrl: string
|
|
85
|
+
readonly mime: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Wrap a one-shot page expression the same way the capture scripts are wrapped. */
|
|
89
|
+
function pageEval(body: string): string {
|
|
90
|
+
return '(function (spec) { try { ' + body + ' } catch (error) { '
|
|
91
|
+
+ 'return JSON.stringify({ ok: false, reason: "script-error: " + String(error) }) '
|
|
92
|
+
+ '} })(arguments[0] || {})'
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Both halves of the checksum must agree; the implementation is shared verbatim. */
|
|
96
|
+
export function probeDigest(bytes: Uint8Array | Buffer): string {
|
|
97
|
+
let a = 2_166_136_261
|
|
98
|
+
let b = 16_777_619
|
|
99
|
+
for (let i = 0; i < bytes.length; i += 1) {
|
|
100
|
+
a = Math.imul(a ^ bytes[i], 16_777_619) >>> 0
|
|
101
|
+
b = Math.imul(b ^ bytes[i], 2_246_822_519) >>> 0
|
|
102
|
+
}
|
|
103
|
+
return `${a.toString(16)}-${b.toString(16)}-${String(bytes.length)}`
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Sample the facts a screenshot must be judged by. `getClientRects()` is what
|
|
108
|
+
* makes multi-column and discontinuous selections visible: one bounding box
|
|
109
|
+
* would silently include the text between two columns.
|
|
110
|
+
*/
|
|
111
|
+
export function visibleFactsScript(): string {
|
|
112
|
+
const body = [
|
|
113
|
+
'var selection = window.getSelection ? window.getSelection() : null',
|
|
114
|
+
'var rects = []',
|
|
115
|
+
'if (selection && selection.rangeCount > 0 && !selection.isCollapsed) {',
|
|
116
|
+
' for (var i = 0; i < selection.rangeCount && rects.length < 48; i += 1) {',
|
|
117
|
+
' var range = selection.getRangeAt(i)',
|
|
118
|
+
' var list = range.getClientRects ? range.getClientRects() : []',
|
|
119
|
+
' for (var j = 0; j < list.length && rects.length < 48; j += 1) {',
|
|
120
|
+
' var r = list[j]',
|
|
121
|
+
' if (!(r.width >= 1) || !(r.height >= 1)) continue',
|
|
122
|
+
' rects.push({ x: r.left, y: r.top, width: r.width, height: r.height })',
|
|
123
|
+
' }',
|
|
124
|
+
' if (list.length === 0) {',
|
|
125
|
+
' var box = range.getBoundingClientRect()',
|
|
126
|
+
' if (box.width >= 1 && box.height >= 1) rects.push({ x: box.left, y: box.top, width: box.width, height: box.height })',
|
|
127
|
+
' }',
|
|
128
|
+
' }',
|
|
129
|
+
'}',
|
|
130
|
+
'var vv = window.visualViewport',
|
|
131
|
+
'var frameRoute = "main"',
|
|
132
|
+
'try { if (window.top !== window) frameRoute = "frame" } catch (error) { frameRoute = "frame" }',
|
|
133
|
+
'return JSON.stringify({ ok: true, probe: "dsh-visible-facts",',
|
|
134
|
+
' url: String(location.href), title: String(document.title || ""),',
|
|
135
|
+
' frameUrl: String(location.href), frameRoute: frameRoute,',
|
|
136
|
+
' dpr: Number(window.devicePixelRatio || 1),',
|
|
137
|
+
' scrollX: Number(window.scrollX || window.pageXOffset || 0),',
|
|
138
|
+
' scrollY: Number(window.scrollY || window.pageYOffset || 0),',
|
|
139
|
+
' viewportWidth: Number(window.innerWidth || 0), viewportHeight: Number(window.innerHeight || 0),',
|
|
140
|
+
' visualScale: Number(vv && vv.scale ? vv.scale : 1),',
|
|
141
|
+
' rects: rects, multipleRanges: selection ? selection.rangeCount > 1 : false })',
|
|
142
|
+
].join('\n')
|
|
143
|
+
return pageEval(body)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Ask the page session for a digest of the bytes it can still reach at `url`.
|
|
148
|
+
* A plain `fetch` in the page keeps the page's cookies and partitioning; when
|
|
149
|
+
* it is blocked (cross origin, login, signed URL) the result is honest
|
|
150
|
+
* `ok: false` rather than a guess. The digest is not a cryptographic proof —
|
|
151
|
+
* it is a same-session version check, which is exactly what B33/B57 need.
|
|
152
|
+
*/
|
|
153
|
+
export function versionProbeScript(): string {
|
|
154
|
+
const body = [
|
|
155
|
+
'function dshFnv(bytes) {',
|
|
156
|
+
' var a = 2166136261, b = 16777619',
|
|
157
|
+
' for (var i = 0; i < bytes.length; i += 1) {',
|
|
158
|
+
' a = Math.imul(a ^ bytes[i], 16777619) >>> 0',
|
|
159
|
+
' b = Math.imul(b ^ bytes[i], 2246822519) >>> 0',
|
|
160
|
+
' }',
|
|
161
|
+
' return a.toString(16) + "-" + b.toString(16) + "-" + String(bytes.length)',
|
|
162
|
+
'}',
|
|
163
|
+
'var url = String(spec.url || "")',
|
|
164
|
+
'if (!/^https?:\\/\\//i.test(url)) return JSON.stringify({ ok: false, reason: "unsupported-url" })',
|
|
165
|
+
'return fetch(url, { credentials: "include", cache: "no-store" })',
|
|
166
|
+
' .then(function (response) {',
|
|
167
|
+
' if (!response.ok) return { ok: false, reason: "http-" + String(response.status) }',
|
|
168
|
+
' return response.arrayBuffer().then(function (buffer) {',
|
|
169
|
+
' var bytes = new Uint8Array(buffer)',
|
|
170
|
+
' return { ok: true, length: bytes.length, digest: dshFnv(bytes) }',
|
|
171
|
+
' })',
|
|
172
|
+
' })',
|
|
173
|
+
' .then(function (result) { return JSON.stringify(result) })',
|
|
174
|
+
' .catch(function () { return JSON.stringify({ ok: false, reason: "page-fetch-unavailable" }) })',
|
|
175
|
+
].join('\n')
|
|
176
|
+
return pageEval(body)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Cover everything outside the selected rectangles, and hide our own UI so it
|
|
181
|
+
* cannot be baked into the reference image. The mask is a single SVG stacked
|
|
182
|
+
* over the viewport: a white mask rect (visible shade) with black holes
|
|
183
|
+
* (transparent) punched out for every selected client rect. That keeps
|
|
184
|
+
* multi-column selections free of the neighbouring column without any image
|
|
185
|
+
* library, and works for partially visible targets too.
|
|
186
|
+
*/
|
|
187
|
+
export function maskScript(): string {
|
|
188
|
+
const body = [
|
|
189
|
+
'var ns = "http://www.w3.org/2000/svg"',
|
|
190
|
+
'var existing = document.getElementById("' + MASK_HOST_ID + '")',
|
|
191
|
+
'if (existing && existing.parentNode) existing.parentNode.removeChild(existing)',
|
|
192
|
+
'var hidden = []',
|
|
193
|
+
'var ids = ["' + OVERLAY_HOST_ID + '", "' + HIGHLIGHT_HOST_ID + '"]',
|
|
194
|
+
'for (var k = 0; k < ids.length; k += 1) {',
|
|
195
|
+
' var node = document.getElementById(ids[k])',
|
|
196
|
+
' if (node) { hidden.push({ node: node, display: node.style.display }); node.style.display = "none" }',
|
|
197
|
+
'}',
|
|
198
|
+
'window.__dshRefMaskHidden = hidden',
|
|
199
|
+
'var width = Math.max(1, Number(spec.viewportWidth) || Number(window.innerWidth) || 1)',
|
|
200
|
+
'var height = Math.max(1, Number(spec.viewportHeight) || Number(window.innerHeight) || 1)',
|
|
201
|
+
'var host = document.createElement("div")',
|
|
202
|
+
'host.id = "' + MASK_HOST_ID + '"',
|
|
203
|
+
'host.style.cssText = "position:fixed;left:0;top:0;width:100vw;height:100vh;z-index:2147483646;pointer-events:none"',
|
|
204
|
+
'var svg = document.createElementNS(ns, "svg")',
|
|
205
|
+
'svg.setAttribute("width", "100%")',
|
|
206
|
+
'svg.setAttribute("height", "100%")',
|
|
207
|
+
'svg.setAttribute("viewBox", "0 0 " + width + " " + height)',
|
|
208
|
+
'svg.setAttribute("preserveAspectRatio", "none")',
|
|
209
|
+
'var defs = document.createElementNS(ns, "defs")',
|
|
210
|
+
'var mask = document.createElementNS(ns, "mask")',
|
|
211
|
+
'mask.setAttribute("id", "__dsh_ref_mask")',
|
|
212
|
+
'var white = document.createElementNS(ns, "rect")',
|
|
213
|
+
'white.setAttribute("x", "0"); white.setAttribute("y", "0")',
|
|
214
|
+
'white.setAttribute("width", String(width)); white.setAttribute("height", String(height))',
|
|
215
|
+
'white.setAttribute("fill", "white")',
|
|
216
|
+
'mask.appendChild(white)',
|
|
217
|
+
'for (var i = 0; i < spec.rects.length; i += 1) {',
|
|
218
|
+
' var r = spec.rects[i]',
|
|
219
|
+
' var hole = document.createElementNS(ns, "rect")',
|
|
220
|
+
' hole.setAttribute("x", String(r.x)); hole.setAttribute("y", String(r.y))',
|
|
221
|
+
' hole.setAttribute("width", String(r.width)); hole.setAttribute("height", String(r.height))',
|
|
222
|
+
' hole.setAttribute("fill", "black")',
|
|
223
|
+
' mask.appendChild(hole)',
|
|
224
|
+
'}',
|
|
225
|
+
'defs.appendChild(mask); svg.appendChild(defs)',
|
|
226
|
+
'var shade = document.createElementNS(ns, "rect")',
|
|
227
|
+
'shade.setAttribute("x", "0"); shade.setAttribute("y", "0")',
|
|
228
|
+
'shade.setAttribute("width", String(width)); shade.setAttribute("height", String(height))',
|
|
229
|
+
'shade.setAttribute("fill", "#0b0d12")',
|
|
230
|
+
'shade.setAttribute("fill-opacity", "0.94")',
|
|
231
|
+
'shade.setAttribute("mask", "url(#__dsh_ref_mask)")',
|
|
232
|
+
'svg.appendChild(shade); host.appendChild(svg)',
|
|
233
|
+
'document.body.appendChild(host)',
|
|
234
|
+
'return JSON.stringify({ ok: true, rects: spec.rects.length, hidden: hidden.length })',
|
|
235
|
+
].join('\n')
|
|
236
|
+
return pageEval(body)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Remove the mask and restore whatever UI it hid, even after a failed capture. */
|
|
240
|
+
export function unmaskScript(): string {
|
|
241
|
+
const body = [
|
|
242
|
+
'var host = document.getElementById("' + MASK_HOST_ID + '")',
|
|
243
|
+
'if (host && host.parentNode) host.parentNode.removeChild(host)',
|
|
244
|
+
'var hidden = window.__dshRefMaskHidden || []',
|
|
245
|
+
'for (var i = 0; i < hidden.length; i += 1) {',
|
|
246
|
+
' try { hidden[i].node.style.display = hidden[i].display } catch (error) { /* node went away */ }',
|
|
247
|
+
'}',
|
|
248
|
+
'window.__dshRefMaskHidden = []',
|
|
249
|
+
'return JSON.stringify({ ok: true, removed: host ? 1 : 0, restored: hidden.length })',
|
|
250
|
+
].join('\n')
|
|
251
|
+
return pageEval(body)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function finiteOf(value: unknown, fallback: number, min: number, max: number): number {
|
|
255
|
+
const n = typeof value === 'number' ? value : Number(value)
|
|
256
|
+
if (!Number.isFinite(n)) return fallback
|
|
257
|
+
return Math.min(max, Math.max(min, n))
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function rectOf(value: unknown): VisibleRect | undefined {
|
|
261
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
262
|
+
const row = value as Record<string, unknown>
|
|
263
|
+
const x = finiteOf(row.x, 0, -MAX_REGION_EDGE, MAX_REGION_EDGE)
|
|
264
|
+
const y = finiteOf(row.y, 0, -MAX_REGION_EDGE, MAX_REGION_EDGE)
|
|
265
|
+
const width = finiteOf(row.width, 0, 0, MAX_REGION_EDGE)
|
|
266
|
+
const height = finiteOf(row.height, 0, 0, MAX_REGION_EDGE)
|
|
267
|
+
if (width < 1 || height < 1) return undefined
|
|
268
|
+
return { x, y, width, height }
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Validate the untrusted page facts. Anything missing makes the sample unusable. */
|
|
272
|
+
export function validateVisibleFacts(value: unknown): ScreenshotFacts | undefined {
|
|
273
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
274
|
+
const row = value as Record<string, unknown>
|
|
275
|
+
if (row.ok !== true || row.probe !== 'dsh-visible-facts') return undefined
|
|
276
|
+
const url = typeof row.url === 'string' ? row.url.slice(0, MAX_FACT_STRING) : ''
|
|
277
|
+
if (url === '') return undefined
|
|
278
|
+
const viewportWidth = finiteOf(row.viewportWidth, 0, 0, MAX_REGION_EDGE)
|
|
279
|
+
const viewportHeight = finiteOf(row.viewportHeight, 0, 0, MAX_REGION_EDGE)
|
|
280
|
+
if (viewportWidth < 1 || viewportHeight < 1) return undefined
|
|
281
|
+
const rawRects = Array.isArray(row.rects) ? row.rects.slice(0, MAX_VISIBLE_RECTS) : []
|
|
282
|
+
const rects: VisibleRect[] = []
|
|
283
|
+
for (const item of rawRects) {
|
|
284
|
+
const rect = rectOf(item)
|
|
285
|
+
if (rect !== undefined) rects.push(rect)
|
|
286
|
+
}
|
|
287
|
+
if (rects.length === 0) return undefined
|
|
288
|
+
const frameUrl = typeof row.frameUrl === 'string' ? row.frameUrl.slice(0, MAX_FACT_STRING) : url
|
|
289
|
+
return {
|
|
290
|
+
url,
|
|
291
|
+
title: typeof row.title === 'string' ? row.title.slice(0, 500) : '',
|
|
292
|
+
frameUrl,
|
|
293
|
+
frameRoute: row.frameRoute === 'frame' ? 'frame' : 'main',
|
|
294
|
+
dpr: finiteOf(row.dpr, 1, 0.25, 8),
|
|
295
|
+
// A long page can be scrolled far past MAX_REGION_EDGE*8 (65 536px);
|
|
296
|
+
// clamping the scroll offset mis-translates client rects into document
|
|
297
|
+
// coordinates and the clip lands in the wrong place. Keep the value finite
|
|
298
|
+
// but allow a realistically long document (all other caps stay in force).
|
|
299
|
+
scrollX: finiteOf(row.scrollX, 0, -1e7, 1e7),
|
|
300
|
+
scrollY: finiteOf(row.scrollY, 0, -1e7, 1e7),
|
|
301
|
+
viewportWidth,
|
|
302
|
+
viewportHeight,
|
|
303
|
+
visualScale: finiteOf(row.visualScale, 1, 0.25, 8),
|
|
304
|
+
rects,
|
|
305
|
+
multipleRanges: row.multipleRanges === true || rects.length > 1,
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Turn viewport client rects into one CDP clip. The clip is intersected with
|
|
311
|
+
* the viewport (a target only partly on screen keeps only its visible part),
|
|
312
|
+
* then translated into document coordinates with the sampled scroll offset.
|
|
313
|
+
* The mask, not the clip, is what excludes the unselected neighbouring content.
|
|
314
|
+
*/
|
|
315
|
+
export function clipFromFacts(facts: ScreenshotFacts): { clip: VisibleClip; meta: VisibleCaptureMeta } | undefined {
|
|
316
|
+
let left = Number.POSITIVE_INFINITY
|
|
317
|
+
let top = Number.POSITIVE_INFINITY
|
|
318
|
+
let right = Number.NEGATIVE_INFINITY
|
|
319
|
+
let bottom = Number.NEGATIVE_INFINITY
|
|
320
|
+
let partiallyVisible = false
|
|
321
|
+
for (const rect of facts.rects) {
|
|
322
|
+
const x1 = Math.max(0, rect.x)
|
|
323
|
+
const y1 = Math.max(0, rect.y)
|
|
324
|
+
const x2 = Math.min(facts.viewportWidth, rect.x + rect.width)
|
|
325
|
+
const y2 = Math.min(facts.viewportHeight, rect.y + rect.height)
|
|
326
|
+
if (x2 <= x1 || y2 <= y1) continue
|
|
327
|
+
if (x1 > rect.x || y1 > rect.y || x2 < rect.x + rect.width || y2 < rect.y + rect.height) partiallyVisible = true
|
|
328
|
+
left = Math.min(left, x1)
|
|
329
|
+
top = Math.min(top, y1)
|
|
330
|
+
right = Math.max(right, x2)
|
|
331
|
+
bottom = Math.max(bottom, y2)
|
|
332
|
+
}
|
|
333
|
+
if (!Number.isFinite(left) || right <= left || bottom <= top) return undefined
|
|
334
|
+
const width = Math.min(MAX_REGION_EDGE, right - left)
|
|
335
|
+
const height = Math.min(MAX_REGION_EDGE, bottom - top)
|
|
336
|
+
if (width < 1 || height < 1) return undefined
|
|
337
|
+
const clip: VisibleClip = {
|
|
338
|
+
x: Math.round((left + facts.scrollX) * 100) / 100,
|
|
339
|
+
y: Math.round((top + facts.scrollY) * 100) / 100,
|
|
340
|
+
width: Math.round(width * 100) / 100,
|
|
341
|
+
height: Math.round(height * 100) / 100,
|
|
342
|
+
scale: 1,
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
clip,
|
|
346
|
+
meta: {
|
|
347
|
+
clip,
|
|
348
|
+
rects: facts.rects.length,
|
|
349
|
+
partiallyVisible: partiallyVisible || facts.rects.length > 1,
|
|
350
|
+
dpr: facts.dpr,
|
|
351
|
+
visualScale: facts.visualScale,
|
|
352
|
+
viewport: { width: facts.viewportWidth, height: facts.viewportHeight },
|
|
353
|
+
scroll: { x: facts.scrollX, y: facts.scrollY },
|
|
354
|
+
},
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Compare the page session's digest with the bytes the host actually saved. */
|
|
359
|
+
export function compareImageVersion(probe: unknown, bytes: Buffer): VersionStatus {
|
|
360
|
+
if (typeof probe !== 'object' || probe === null) return 'unverifiable'
|
|
361
|
+
const row = probe as Record<string, unknown>
|
|
362
|
+
if (row.ok !== true || typeof row.digest !== 'string' || typeof row.length !== 'number') return 'unverifiable'
|
|
363
|
+
if (!Number.isFinite(row.length) || row.length < 0) return 'unverifiable'
|
|
364
|
+
const digest = probeDigest(bytes)
|
|
365
|
+
if (row.length !== bytes.length || row.digest !== digest) return 'changed'
|
|
366
|
+
return 'verified'
|
|
367
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { symbols } from '@deepseek-ai/cordis'
|
|
3
|
+
import { referenceOwnerKey } from '../reference-owner.ts'
|
|
4
|
+
import { sameReferenceDocument } from '../reference-page.ts'
|
|
5
|
+
import type { BrowserReference } from './types.ts'
|
|
6
|
+
import type { WebRevealStatus } from '../reference-source.ts'
|
|
7
|
+
|
|
8
|
+
export class WebSourceLeaseExpired extends Error {}
|
|
9
|
+
|
|
10
|
+
function validateUrl(url: string): void {
|
|
11
|
+
const parsed = new URL(url)
|
|
12
|
+
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.username || parsed.password) throw new Error('invalid web source URL')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Command { readonly id: string; readonly reference: BrowserReference; readonly partId?: string }
|
|
16
|
+
interface Pending { readonly command: Command; readonly finish: (status: WebRevealStatus) => void }
|
|
17
|
+
interface Source {
|
|
18
|
+
readonly owner: string
|
|
19
|
+
readonly pageId: string
|
|
20
|
+
seen: number
|
|
21
|
+
url: string
|
|
22
|
+
readonly pending: Map<string, Pending>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** App-owned iframe leases; no guest can register or acknowledge a command. */
|
|
26
|
+
export class WebReferenceSources {
|
|
27
|
+
private readonly leases = new Map<string, Source>()
|
|
28
|
+
private readonly timeoutMs: number
|
|
29
|
+
constructor(timeoutMs = 8_000) {
|
|
30
|
+
this.timeoutMs = timeoutMs
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private prune(): void {
|
|
34
|
+
for (const [lease, source] of this.leases) {
|
|
35
|
+
if (Date.now() - source.seen > 15_000) this.remove(lease)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
register(owner: string, pageId: string, url: string): string {
|
|
40
|
+
this.prune()
|
|
41
|
+
owner = referenceOwnerKey(owner)
|
|
42
|
+
if (!owner || !/^iframe:[\w-]{1,100}$/u.test(pageId)) throw new Error('invalid web source')
|
|
43
|
+
validateUrl(url)
|
|
44
|
+
if (this.leases.size >= 1024 || [...this.leases.values()].some(source => source.pageId === pageId)) throw new Error('web source already registered or capacity exceeded')
|
|
45
|
+
const lease = randomUUID()
|
|
46
|
+
this.leases.set(lease, { owner, pageId, url, seen: Date.now(), pending: new Map() })
|
|
47
|
+
return lease
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
owns(owner: string, pageId: string, url?: string): boolean {
|
|
51
|
+
this.prune()
|
|
52
|
+
return [...this.leases.values()].some(source => source.owner === referenceOwnerKey(owner) && source.pageId === pageId && (url === undefined || sameReferenceDocument(source.url, url)))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
poll(lease: string, url: string, acknowledgements: readonly { id: string; status: WebRevealStatus }[]): readonly Command[] {
|
|
56
|
+
this.prune()
|
|
57
|
+
const source = this.leases.get(lease)
|
|
58
|
+
if (source === undefined) throw new WebSourceLeaseExpired('web source lease expired')
|
|
59
|
+
validateUrl(url)
|
|
60
|
+
source.seen = Date.now()
|
|
61
|
+
source.url = url
|
|
62
|
+
for (const ack of acknowledgements) {
|
|
63
|
+
const pending = source.pending.get(ack.id)
|
|
64
|
+
pending?.finish(sameReferenceDocument(url, pending.command.reference.source.pageUrl) ? ack.status : 'page-mismatch')
|
|
65
|
+
}
|
|
66
|
+
return [...source.pending.values()].map(pending => pending.command)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
remove(lease: string): void {
|
|
70
|
+
const source = this.leases.get(lease)
|
|
71
|
+
this.leases.delete(lease)
|
|
72
|
+
for (const pending of source?.pending.values() ?? []) pending.finish('unavailable')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async reveal(reference: BrowserReference, pageId: string, partId?: string, signal?: AbortSignal): Promise<WebRevealStatus> {
|
|
76
|
+
this.prune()
|
|
77
|
+
signal?.throwIfAborted()
|
|
78
|
+
const source = [...this.leases.values()].find(source => source.owner === referenceOwnerKey(reference.owner.sessionId ?? '') && source.pageId === pageId)
|
|
79
|
+
if (source === undefined || source.pending.size >= 64) return 'unavailable'
|
|
80
|
+
if (!sameReferenceDocument(source.url, reference.source.pageUrl)) return 'page-mismatch'
|
|
81
|
+
return new Promise(resolve => {
|
|
82
|
+
const id = randomUUID()
|
|
83
|
+
const finish = (status: WebRevealStatus): void => {
|
|
84
|
+
clearTimeout(timer)
|
|
85
|
+
signal?.removeEventListener('abort', abort)
|
|
86
|
+
source.pending.delete(id)
|
|
87
|
+
resolve(status)
|
|
88
|
+
}
|
|
89
|
+
const abort = (): void => { finish('unavailable') }
|
|
90
|
+
const timer = setTimeout(abort, this.timeoutMs)
|
|
91
|
+
timer.unref?.()
|
|
92
|
+
source.pending.set(id, { command: { id, reference, ...partId === undefined ? {} : { partId } }, finish })
|
|
93
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
94
|
+
if (signal?.aborted) abort()
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const sources = new WeakMap<object, WebReferenceSources>()
|
|
100
|
+
export function webReferenceSources(browser: object): WebReferenceSources {
|
|
101
|
+
const original: unknown = Reflect.get(browser, symbols.original)
|
|
102
|
+
const identity = typeof original === 'object' && original !== null ? original : browser
|
|
103
|
+
let channel = sources.get(identity)
|
|
104
|
+
if (channel === undefined) { channel = new WebReferenceSources(); sources.set(identity, channel) }
|
|
105
|
+
return channel
|
|
106
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply-once guard for the client half (issue #132).
|
|
3
|
+
*
|
|
4
|
+
* The host can hand the same context to `apply()` more than once — two rows resolving the same
|
|
5
|
+
* bundle, or a bundle loaded twice — and the second registration used to throw somewhere in the
|
|
6
|
+
* middle of `apply`, taking every later registration (including the settings section) with it.
|
|
7
|
+
* Refusing the second pass up front turns that into one warning and one intact panel.
|
|
8
|
+
*
|
|
9
|
+
* A WeakSet keyed by the context object is deliberate: it is per module instance, so a genuinely
|
|
10
|
+
* new bundle (the self-update path) gets an empty set and still registers, while the same bundle
|
|
11
|
+
* applied twice to the same context does not.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Decide whether this context still needs the client registrations. */
|
|
15
|
+
export function createApplyGuard(): (ctx: object) => boolean {
|
|
16
|
+
const applied = new WeakSet<object>()
|
|
17
|
+
return ctx => {
|
|
18
|
+
if (applied.has(ctx)) return false
|
|
19
|
+
applied.add(ctx)
|
|
20
|
+
return true
|
|
21
|
+
}
|
|
22
|
+
}
|