dsh-builtin-browser 0.5.6 → 0.5.8
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 +148 -0
- package/README.en.md +75 -81
- package/README.md +75 -82
- package/client.js +831 -384
- package/desktop-bridge/install-and-start.cmd +4 -5
- package/desktop-bridge/install.mjs +322 -141
- package/desktop-bridge/maintenance-progress.ps1 +166 -0
- package/desktop-bridge/plugin-browser-bridge.js +157 -19
- package/desktop-bridge/request-desktop-quit.ps1 +170 -0
- package/docs/tool-reference.md +20 -1
- package/docs/user-guide.md +28 -4
- package/lib/browser/runtime.d.ts +3 -1
- package/lib/browser/runtime.js +9 -0
- package/lib/browser/types.d.ts +42 -0
- package/lib/browser-electron/authenticated-route.d.ts +27 -0
- package/lib/browser-electron/authenticated-route.js +39 -0
- package/lib/browser-electron/automatic-maintenance.d.ts +41 -0
- package/lib/browser-electron/automatic-maintenance.js +426 -0
- package/lib/browser-electron/bridge-auto.d.ts +93 -0
- package/lib/browser-electron/bridge-auto.js +113 -0
- package/lib/browser-electron/bridge-connection.d.ts +0 -2
- package/lib/browser-electron/bridge-connection.js +2 -4
- package/lib/browser-electron/bridge-endpoint.js +8 -19
- package/lib/browser-electron/bridge-install.d.ts +85 -0
- package/lib/browser-electron/bridge-install.js +211 -0
- package/lib/browser-electron/bridge-maintenance.d.ts +24 -0
- package/lib/browser-electron/bridge-maintenance.js +125 -0
- package/lib/browser-electron/bridge-status.d.ts +10 -3
- package/lib/browser-electron/bridge-status.js +74 -24
- package/lib/browser-electron/desktop-bridge-host.d.ts +11 -5
- package/lib/browser-electron/desktop-bridge-host.js +67 -25
- package/lib/browser-electron/desktop-identity.js +21 -9
- package/lib/browser-electron/electron-provision.d.ts +1 -1
- package/lib/browser-electron/electron-provision.js +36 -13
- package/lib/browser-electron/entry.js +173 -67
- package/lib/browser-electron/ephemeral-profile.js +67 -26
- package/lib/browser-electron/history-store.js +2 -2
- package/lib/browser-electron/host-main.js +48 -38
- package/lib/browser-electron/http-body.d.ts +24 -0
- package/lib/browser-electron/http-body.js +33 -0
- package/lib/browser-electron/json-reply.d.ts +13 -1
- package/lib/browser-electron/json-reply.js +16 -2
- package/lib/browser-electron/maintenance-broker.d.ts +35 -0
- package/lib/browser-electron/maintenance-broker.js +114 -0
- package/lib/browser-electron/maintenance-lifecycle.d.ts +32 -0
- package/lib/browser-electron/maintenance-lifecycle.js +67 -0
- package/lib/browser-electron/maintenance-route.d.ts +18 -0
- package/lib/browser-electron/maintenance-route.js +75 -0
- package/lib/browser-electron/paths.d.ts +4 -0
- package/lib/browser-electron/paths.js +19 -0
- package/lib/browser-electron/process.d.ts +14 -0
- package/lib/browser-electron/process.js +24 -0
- package/lib/browser-electron/provider.d.ts +16 -9
- package/lib/browser-electron/provider.js +87 -18
- package/lib/browser-electron/remote-host.js +22 -20
- package/lib/browser-electron/runtime-route.d.ts +3 -5
- package/lib/browser-electron/runtime-status.d.ts +27 -13
- package/lib/browser-electron/runtime-status.js +28 -6
- package/lib/browser-electron/settings-route.d.ts +5 -18
- package/lib/browser-electron/settings-route.js +4 -14
- package/lib/browser-electron/settings-store.d.ts +28 -0
- package/lib/browser-electron/settings-store.js +32 -2
- package/lib/browser-electron/sidebar-only-host.d.ts +36 -0
- package/lib/browser-electron/sidebar-only-host.js +192 -0
- package/lib/browser-electron/system-browser.d.ts +6 -11
- package/lib/browser-electron/system-browser.js +29 -24
- package/lib/browser-electron/timing.d.ts +8 -0
- package/lib/browser-electron/timing.js +13 -0
- package/lib/browser-electron/windows-system.d.ts +3 -0
- package/lib/browser-electron/windows-system.js +19 -0
- package/lib/tool-browser/index.js +1045 -7
- package/lib/tool-browser/page-probe.d.ts +86 -0
- package/lib/tool-browser/page-probe.js +460 -0
- package/package.json +5 -3
- package/scripts/run-tests.mjs +32 -0
- package/scripts/verify-preview-route.mjs +19 -1
- package/scripts/verify-runtime-status.mjs +14 -0
- package/skills/dsh-builtin-browser/SKILL.md +64 -0
- package/skills/dsh-builtin-browser/references/batch.md +23 -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/src/browser/runtime.ts +11 -0
- package/src/browser/types.ts +19 -0
- package/src/browser-electron/authenticated-route.ts +56 -0
- package/src/browser-electron/automatic-maintenance.ts +414 -0
- package/src/browser-electron/bridge-auto.ts +158 -0
- package/src/browser-electron/bridge-connection.ts +2 -4
- package/src/browser-electron/bridge-endpoint.ts +8 -18
- package/src/browser-electron/bridge-install.ts +253 -0
- package/src/browser-electron/bridge-maintenance.ts +161 -0
- package/src/browser-electron/bridge-status.ts +76 -26
- package/src/browser-electron/desktop-bridge-host.ts +60 -26
- package/src/browser-electron/desktop-identity.ts +18 -9
- package/src/browser-electron/electron-provision.ts +35 -14
- package/src/browser-electron/entry.ts +152 -62
- package/src/browser-electron/ephemeral-profile.ts +65 -28
- package/src/browser-electron/history-store.ts +2 -2
- package/src/browser-electron/host-main.ts +40 -34
- package/src/browser-electron/http-body.ts +42 -0
- package/src/browser-electron/json-reply.ts +18 -3
- package/src/browser-electron/maintenance-broker.ts +142 -0
- package/src/browser-electron/maintenance-lifecycle.ts +86 -0
- package/src/browser-electron/maintenance-route.ts +64 -0
- package/src/browser-electron/paths.ts +21 -0
- package/src/browser-electron/process.ts +23 -0
- package/src/browser-electron/provider.ts +106 -22
- package/src/browser-electron/remote-host.ts +23 -23
- package/src/browser-electron/runtime-route.ts +3 -5
- package/src/browser-electron/runtime-status.ts +56 -19
- package/src/browser-electron/settings-route.ts +9 -32
- package/src/browser-electron/settings-store.ts +59 -3
- package/src/browser-electron/sidebar-only-host.ts +199 -0
- package/src/browser-electron/system-browser.ts +26 -23
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/windows-system.ts +23 -0
- package/src/tool-browser/index.ts +995 -10
- package/src/tool-browser/page-probe.ts +474 -0
|
@@ -25,8 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
import { BridgeConnection, type BridgeEndpoint } from './bridge-connection.js'
|
|
27
27
|
import { readFileSync } from 'node:fs'
|
|
28
|
-
import {
|
|
29
|
-
import { join } from 'node:path'
|
|
28
|
+
import { bridgeEndpointPath } from './paths.js'
|
|
30
29
|
import { randomUUID } from 'node:crypto'
|
|
31
30
|
import type { CommandOptions, ElectronBrowserViewHost, ElectronViewHandle } from './provider.js'
|
|
32
31
|
import type { BrowserUserAction } from './provider.js'
|
|
@@ -34,10 +33,7 @@ import type { CarrierPage, CarrierLossReason } from '../browser/types.js'
|
|
|
34
33
|
|
|
35
34
|
|
|
36
35
|
/** Absolute path of the endpoint file the shell writes. */
|
|
37
|
-
export
|
|
38
|
-
const home = process.env.DSH_HOME ?? join(homedir(), '.dsh')
|
|
39
|
-
return join(home, 'dsh-builtin-browser-bridge.json')
|
|
40
|
-
}
|
|
36
|
+
export { bridgeEndpointPath }
|
|
41
37
|
|
|
42
38
|
/**
|
|
43
39
|
* The error code the bridge uses when the conversation's panel exists but the shell is not showing
|
|
@@ -49,6 +45,7 @@ export function bridgeEndpointPath(): string {
|
|
|
49
45
|
* blank — losing the page the user had open.
|
|
50
46
|
*/
|
|
51
47
|
const PANEL_NOT_DISPLAYED = 'panel-not-displayed'
|
|
48
|
+
const PANEL_INITIALIZING = 'panel-initializing'
|
|
52
49
|
|
|
53
50
|
/**
|
|
54
51
|
* How many times a display-state refusal is retried, and how long each retry waits.
|
|
@@ -71,12 +68,12 @@ const PANEL_DISPLAY_RETRY_MS = 400
|
|
|
71
68
|
* this host retries while it renders — so reaching here means the shell did not render it at all.
|
|
72
69
|
*/
|
|
73
70
|
export class BrowserPanelNotDisplayedError extends Error {
|
|
74
|
-
readonly code
|
|
71
|
+
readonly code: string
|
|
75
72
|
|
|
76
73
|
/**
|
|
77
74
|
* @param detail - the bridge's own explanation, appended so the message says why.
|
|
78
75
|
*/
|
|
79
|
-
constructor(detail: string) {
|
|
76
|
+
constructor(detail: string, code = PANEL_NOT_DISPLAYED) {
|
|
80
77
|
super(
|
|
81
78
|
'dsh-builtin-browser: this conversation\'s sidebar page did not attach — the shell was asked to '
|
|
82
79
|
+ 'show this conversation and its panel still did not render. Nothing was lost, and the page '
|
|
@@ -84,6 +81,7 @@ export class BrowserPanelNotDisplayedError extends Error {
|
|
|
84
81
|
+ `(${detail})`,
|
|
85
82
|
)
|
|
86
83
|
this.name = 'BrowserPanelNotDisplayedError'
|
|
84
|
+
this.code = code
|
|
87
85
|
}
|
|
88
86
|
}
|
|
89
87
|
|
|
@@ -177,7 +175,8 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
177
175
|
/**
|
|
178
176
|
* @param endpoint - the shell's published bridge endpoint.
|
|
179
177
|
*/
|
|
180
|
-
private constructor(endpoint: BridgeEndpoint, sessionId?: string)
|
|
178
|
+
private constructor(endpoint: BridgeEndpoint, sessionId?: string, private readonly _isQuiet: () => boolean = () => false,
|
|
179
|
+
private readonly protocol = 2) {
|
|
181
180
|
this.owner = sessionId !== undefined && sessionId !== String.fromCharCode(39,39) ? sessionId : randomUUID()
|
|
182
181
|
// The transport reaches this only by concluding the shell stopped refreshing its endpoint, which
|
|
183
182
|
// is `shell-exited`.
|
|
@@ -212,7 +211,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
212
211
|
* caller falls back to self-hosting, which always works.
|
|
213
212
|
* @returns the host, or undefined when no bridge is available.
|
|
214
213
|
*/
|
|
215
|
-
static async discover(sessionId?: string): Promise<DesktopBridgeViewHost | undefined> {
|
|
214
|
+
static async discover(sessionId?: string, isQuiet?: () => boolean): Promise<DesktopBridgeViewHost | undefined> {
|
|
216
215
|
let endpoint: BridgeEndpoint
|
|
217
216
|
try {
|
|
218
217
|
const raw = JSON.parse(readFileSync(bridgeEndpointPath(), 'utf8')) as Partial<BridgeEndpoint>
|
|
@@ -246,7 +245,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
246
245
|
DesktopBridgeViewHost.refuseIframe = true
|
|
247
246
|
return undefined
|
|
248
247
|
}
|
|
249
|
-
return new DesktopBridgeViewHost(endpoint, sessionId)
|
|
248
|
+
return new DesktopBridgeViewHost(endpoint, sessionId, isQuiet, Number(answer.protocol ?? 2))
|
|
250
249
|
} finally {
|
|
251
250
|
probe.close()
|
|
252
251
|
}
|
|
@@ -352,10 +351,19 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
352
351
|
|
|
353
352
|
private async guestFor(viewId: string, url?: string, options?: CommandOptions): Promise<number> {
|
|
354
353
|
const entry = this.views.get(viewId)
|
|
354
|
+
if (entry === undefined) throw new Error('dsh-builtin-browser: sidebar view was released')
|
|
355
|
+
const stillOwned = (): void => {
|
|
356
|
+
if (this.views.get(viewId) !== entry) throw new Error('dsh-builtin-browser: sidebar view was released')
|
|
357
|
+
}
|
|
355
358
|
// No liveness check here: verifying the cached guest cost a round-trip on every
|
|
356
359
|
// command. A guest that has gone away is detected by the command itself failing,
|
|
357
360
|
// and `createView`'s sendCommand then discards the cache and calls back in here.
|
|
358
|
-
|
|
361
|
+
// Explicit navigation also reopens a folded panel. Cached guests are sufficient for reads
|
|
362
|
+
// and input, but not for the user's request to open an address in this conversation's sidebar.
|
|
363
|
+
if (entry?.guest !== undefined && url === undefined) return entry.guest
|
|
364
|
+
if (options?.initialize === false) {
|
|
365
|
+
throw new BrowserPanelNotDisplayedError('this view has no attached guest yet', PANEL_INITIALIZING)
|
|
366
|
+
}
|
|
359
367
|
|
|
360
368
|
// The conversation THIS VIEW serves — read from the view, never from the process.
|
|
361
369
|
//
|
|
@@ -369,11 +377,19 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
369
377
|
// conversation's own panel by id through the renderer, rather than by pressing the shell's
|
|
370
378
|
// shortcut or clicking its card, both of which the shell delivers to whatever it displays.
|
|
371
379
|
const owner = entry?.owner ?? this.owner
|
|
380
|
+
if (url !== undefined && this.protocol < 3
|
|
381
|
+
&& [...this.views.entries()].some(([id, view]) => id !== viewId && view.owner === owner && view.guest !== undefined)) {
|
|
382
|
+
throw new Error('dsh-builtin-browser: this desktop bridge needs an update for separate sidebar tabs; use Repair and restart in Browser settings')
|
|
383
|
+
}
|
|
372
384
|
// A command that must not present anything (issue #46: the live preview) takes a read-only path.
|
|
373
385
|
// It asks the bridge what this conversation ALREADY has and never enters the retry loop below — // that loop is what asks the shell to display the conversation, which is right for a tool call
|
|
374
386
|
// and wrong for "let me look at it". A page that is not on screen therefore reports as a display
|
|
375
387
|
// state, and the preview route turns that into the panel's paused state.
|
|
376
388
|
if (options?.present === false) {
|
|
389
|
+
// A logical view without a binding cannot borrow a neighbouring tab for a preview.
|
|
390
|
+
if (this.protocol >= 3) {
|
|
391
|
+
throw new BrowserPanelNotDisplayedError('this view has no attached guest yet', PANEL_INITIALIZING)
|
|
392
|
+
}
|
|
377
393
|
// `list` and NOT `ensureSidebar`. This is deliberate and it is the whole fix: every version of
|
|
378
394
|
// `ensureSidebar` can drive the shell — the current one asks the renderer to display the
|
|
379
395
|
// conversation, and the pre-#37 one literally pressed the sidebar shortcut and typed into the
|
|
@@ -385,22 +401,24 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
385
401
|
? listed.sidebar.filter(page => page !== null && typeof page === 'object' && page.type === 'webview')
|
|
386
402
|
: []
|
|
387
403
|
const wanted = url !== undefined && url !== '' ? url : undefined
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
: pages.find(page => typeof page.url === 'string' && (page.url === wanted || page.url.startsWith(wanted))) ?? pages[0]
|
|
404
|
+
const samePage = (url: string, wanted: string): boolean => url === wanted || url.startsWith(`${wanted}?`) || url.startsWith(`${wanted}#`)
|
|
405
|
+
const match = pages.find(page => wanted !== undefined && typeof page.url === 'string' && samePage(page.url, wanted)) ?? pages[0]
|
|
391
406
|
const id = match !== undefined ? Number(match.id) : Number.NaN
|
|
392
407
|
if (!Number.isFinite(id) || id <= 0) {
|
|
393
408
|
throw new BrowserPanelNotDisplayedError(
|
|
394
409
|
'the preview asked for this conversation\'s page without presenting it, and no page is attached '
|
|
395
|
-
|
|
410
|
+
+ `(the shell holds ${pages.length})`,
|
|
396
411
|
)
|
|
397
412
|
}
|
|
398
|
-
|
|
413
|
+
stillOwned()
|
|
414
|
+
entry.guest = id
|
|
399
415
|
return id
|
|
400
416
|
}
|
|
401
417
|
const ask = (): Promise<Record<string, unknown>> => this.connection.call({
|
|
402
418
|
op: 'ensureSidebar',
|
|
403
419
|
owner,
|
|
420
|
+
viewId,
|
|
421
|
+
...entry?.guest === undefined ? {} : { guestId: entry.guest },
|
|
404
422
|
...url !== undefined && url !== '' ? { url } : {},
|
|
405
423
|
}, 30_000)
|
|
406
424
|
let answer = await ask()
|
|
@@ -425,26 +443,31 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
425
443
|
|| String(value.error ?? '').includes('can only attach'))
|
|
426
444
|
for (let attempt = 0; attempt < PANEL_DISPLAY_RETRIES && displayRefusal(answer); attempt += 1) {
|
|
427
445
|
await new Promise(resolve => setTimeout(resolve, PANEL_DISPLAY_RETRY_MS))
|
|
446
|
+
stillOwned()
|
|
428
447
|
answer = await ask()
|
|
429
448
|
}
|
|
449
|
+
stillOwned()
|
|
430
450
|
if (answer.ok !== true) {
|
|
431
451
|
// Reachable only after the retries above have run out, and the code still decides — not the
|
|
432
452
|
// wording:
|
|
433
453
|
// panel-not-displayed -> the shell was asked to show the conversation (client half's
|
|
434
454
|
// `openSession`) and did not render it in ~1.6 s: no loss, no swap.
|
|
435
|
-
//
|
|
436
|
-
//
|
|
455
|
+
// A missing guest within a time budget is still initialization, including on old bridges
|
|
456
|
+
// that only returned text. It is not evidence that this shell uses an undrivable iframe.
|
|
437
457
|
const code = String(answer.code ?? '')
|
|
438
|
-
const detail = String(answer.error)
|
|
458
|
+
const detail = String(answer.error ?? answer.code ?? 'unknown sidebar failure')
|
|
439
459
|
if (code === PANEL_NOT_DISPLAYED) throw new BrowserPanelNotDisplayedError(detail)
|
|
440
|
-
if (detail.includes('produced no page'))
|
|
460
|
+
if (code === PANEL_INITIALIZING || (code === '' && detail.includes('produced no page'))) {
|
|
461
|
+
throw new BrowserPanelNotDisplayedError(detail, PANEL_INITIALIZING)
|
|
462
|
+
}
|
|
463
|
+
if (code === 'panel-unusable') this.reportLost('panel-unusable')
|
|
441
464
|
throw new Error(`dsh-builtin-browser: sidebar unavailable (${detail})`)
|
|
442
465
|
}
|
|
443
466
|
const id = Number(answer.id)
|
|
444
467
|
if (!Number.isFinite(id) || id <= 0) {
|
|
445
468
|
throw new Error('dsh-builtin-browser: the sidebar reported no browser page for this conversation')
|
|
446
469
|
}
|
|
447
|
-
|
|
470
|
+
entry.guest = id
|
|
448
471
|
return id
|
|
449
472
|
}
|
|
450
473
|
|
|
@@ -458,13 +481,21 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
458
481
|
const navigateUrl = (method: string, params?: Record<string, unknown>): string | undefined =>
|
|
459
482
|
method === 'Page.navigate' ? String((params as { url?: unknown })?.url ?? '') : undefined
|
|
460
483
|
const run = async (guest: number, method: string, params?: Record<string, unknown>): Promise<Record<string, unknown>> => {
|
|
461
|
-
const
|
|
484
|
+
const owner = this.views.get(viewId)?.owner ?? this.owner
|
|
485
|
+
const answer = await this.connection.call({ op: 'cdp', id: guest, method, params, owner })
|
|
462
486
|
if (answer.ok !== true) throw new Error(`dsh-builtin-browser: sidebar command failed: ${String(answer.error)}`)
|
|
463
487
|
// The bridge forwards whatever CDP answered; the seam expects an object.
|
|
464
488
|
return (answer.result ?? {}) as Record<string, unknown>
|
|
465
489
|
}
|
|
466
490
|
return {
|
|
467
491
|
id: viewId,
|
|
492
|
+
download: async (url: string, savePath: string) => {
|
|
493
|
+
if (this.protocol < 3) throw new Error('dsh-builtin-browser: repair and restart the desktop bridge for sidebar downloads')
|
|
494
|
+
const guest = await this.guestFor(viewId)
|
|
495
|
+
const owner = this.views.get(viewId)?.owner ?? this.owner
|
|
496
|
+
const answer = await this.connection.call({ op: 'download', id: guest, owner, url, savePath }, 60_000)
|
|
497
|
+
if (answer.ok !== true) throw new Error(`dsh-builtin-browser: sidebar download failed: ${String(answer.error)}`)
|
|
498
|
+
},
|
|
468
499
|
sendCommand: async (method: string, params?: Record<string, unknown>, options?: CommandOptions) => {
|
|
469
500
|
// No liveness probe up front. Checking first cost a whole round-trip on
|
|
470
501
|
// every command; instead the command runs and its failure is what triggers
|
|
@@ -514,7 +545,10 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
514
545
|
* it must not fail the navigation that has already happened.
|
|
515
546
|
* @param handle - the view to bring forward.
|
|
516
547
|
*/
|
|
517
|
-
showView(handle: ElectronViewHandle): void {
|
|
548
|
+
showView(handle: ElectronViewHandle, _label?: string, options?: { readonly quiet?: boolean; readonly select?: boolean }): void {
|
|
549
|
+
// Selecting a sidebar tab only changes the task's pane. The bridge never activates the
|
|
550
|
+
// desktop window, so explicit tab selection works in quiet mode as well.
|
|
551
|
+
if (options?.quiet === true && options.select !== true) return
|
|
518
552
|
// `owner` is required: the bridge refuses to bring forward a guest held by another session.
|
|
519
553
|
// It comes from the view, not the process — with several conversations live, a process-wide
|
|
520
554
|
// owner would raise the wrong conversation's tab.
|
|
@@ -523,7 +557,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
523
557
|
// showActive also runs before a view has navigated; do not materialize a blank page just to
|
|
524
558
|
// present it. Once materialized, only the shell's numeric guest id can name its tab.
|
|
525
559
|
if (guest === undefined) return
|
|
526
|
-
void this.connection.call({ op: 'showTab', viewId: guest, owner }, 5_000).catch(() => undefined)
|
|
560
|
+
void this.connection.call({ op: 'showTab', viewId: guest, logicalViewId: handle.id, owner }, 5_000).catch(() => undefined)
|
|
527
561
|
}
|
|
528
562
|
|
|
529
563
|
|
|
@@ -601,7 +635,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
|
|
|
601
635
|
*/
|
|
602
636
|
async collapse(): Promise<void> {
|
|
603
637
|
try {
|
|
604
|
-
await this.connection.call({ op: 'collapseSidebar' }, 10_000)
|
|
638
|
+
await this.connection.call({ op: 'collapseSidebar', owner: this.owner }, 10_000)
|
|
605
639
|
} catch {
|
|
606
640
|
// Presentation only; failing to fold is never worth an error.
|
|
607
641
|
}
|
|
@@ -23,8 +23,16 @@
|
|
|
23
23
|
* @module dsh-browser/browser-electron/desktop-identity
|
|
24
24
|
*/
|
|
25
25
|
import { closeSync, existsSync, openSync, readFileSync, readSync } from 'node:fs'
|
|
26
|
+
import { createRequire } from 'node:module'
|
|
26
27
|
import { join } from 'node:path'
|
|
27
28
|
|
|
29
|
+
// Electron's patched fs opens an ASAR as a virtual directory. Identity needs the raw archive
|
|
30
|
+
// header and file offsets, including when this reader runs in the Desktop's Node-mode worker.
|
|
31
|
+
const archiveFs: Pick<typeof import('node:fs'), 'openSync' | 'readSync' | 'closeSync' | 'existsSync'> = (() => {
|
|
32
|
+
try { return createRequire(import.meta.url)('original-fs') as typeof import('node:fs') }
|
|
33
|
+
catch { return { openSync, readSync, closeSync, existsSync } }
|
|
34
|
+
})()
|
|
35
|
+
|
|
28
36
|
/** The identity rule, as data. */
|
|
29
37
|
interface IdentityRule {
|
|
30
38
|
readonly packageNames: readonly string[]
|
|
@@ -116,13 +124,13 @@ interface AsarEntry {
|
|
|
116
124
|
function readAsarHeader(archive: string): { header: { files?: Record<string, AsarEntry> }; contentBase: number } | { error: string } {
|
|
117
125
|
let fd: number | undefined
|
|
118
126
|
try {
|
|
119
|
-
fd = openSync(archive, 'r')
|
|
127
|
+
fd = archiveFs.openSync(archive, 'r')
|
|
120
128
|
const outer = Buffer.alloc(8)
|
|
121
|
-
if (readSync(fd, outer, 0, 8, 0) !== 8) return { error: 'the archive header is truncated' }
|
|
129
|
+
if (archiveFs.readSync(fd, outer, 0, 8, 0) !== 8) return { error: 'the archive header is truncated' }
|
|
122
130
|
const headerSize = outer.readUInt32LE(4)
|
|
123
131
|
if (headerSize < 8 || headerSize > 64 * 1024 * 1024) return { error: `implausible archive header size (${headerSize})` }
|
|
124
132
|
const header = Buffer.alloc(headerSize)
|
|
125
|
-
if (readSync(fd, header, 0, headerSize, 8) !== headerSize) return { error: 'the archive header is truncated' }
|
|
133
|
+
if (archiveFs.readSync(fd, header, 0, headerSize, 8) !== headerSize) return { error: 'the archive header is truncated' }
|
|
126
134
|
const jsonLength = header.readUInt32LE(4)
|
|
127
135
|
if (jsonLength <= 0 || jsonLength > headerSize - 8) return { error: 'the archive header has an implausible JSON length' }
|
|
128
136
|
return { header: JSON.parse(header.toString('utf8', 8, 8 + jsonLength)), contentBase: 8 + headerSize }
|
|
@@ -130,7 +138,7 @@ function readAsarHeader(archive: string): { header: { files?: Record<string, Asa
|
|
|
130
138
|
return { error: `the archive header could not be read: ${String(error)}` }
|
|
131
139
|
} finally {
|
|
132
140
|
if (fd !== undefined) {
|
|
133
|
-
try { closeSync(fd) } catch { /* already closed */ }
|
|
141
|
+
try { archiveFs.closeSync(fd) } catch { /* already closed */ }
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
144
|
}
|
|
@@ -167,15 +175,15 @@ export function readAsarFile(archive: string, path: string): Buffer | undefined
|
|
|
167
175
|
if (typeof entry.offset !== 'string' || typeof entry.size !== 'number' || entry.size < 0) return undefined
|
|
168
176
|
let fd: number | undefined
|
|
169
177
|
try {
|
|
170
|
-
fd = openSync(archive, 'r')
|
|
178
|
+
fd = archiveFs.openSync(archive, 'r')
|
|
171
179
|
const buffer = Buffer.alloc(entry.size)
|
|
172
|
-
const read = readSync(fd, buffer, 0, entry.size, parsed.contentBase + Number(entry.offset))
|
|
180
|
+
const read = archiveFs.readSync(fd, buffer, 0, entry.size, parsed.contentBase + Number(entry.offset))
|
|
173
181
|
return read === entry.size ? buffer : undefined
|
|
174
182
|
} catch {
|
|
175
183
|
return undefined
|
|
176
184
|
} finally {
|
|
177
185
|
if (fd !== undefined) {
|
|
178
|
-
try { closeSync(fd) } catch { /* already closed */ }
|
|
186
|
+
try { archiveFs.closeSync(fd) } catch { /* already closed */ }
|
|
179
187
|
}
|
|
180
188
|
}
|
|
181
189
|
}
|
|
@@ -197,7 +205,7 @@ export function readDesktopIdentity(installRoot: string): DesktopIdentity | Iden
|
|
|
197
205
|
|
|
198
206
|
let archivePresent: boolean
|
|
199
207
|
try {
|
|
200
|
-
archivePresent = existsSync(archive)
|
|
208
|
+
archivePresent = archiveFs.existsSync(archive)
|
|
201
209
|
} catch {
|
|
202
210
|
return { ok: false, reason: `cannot inspect ${archive}` }
|
|
203
211
|
}
|
|
@@ -223,7 +231,8 @@ export function readDesktopIdentity(installRoot: string): DesktopIdentity | Iden
|
|
|
223
231
|
const manifestPath = join(appDir, 'package.json')
|
|
224
232
|
try {
|
|
225
233
|
manifestText = readFileSync(manifestPath, 'utf8')
|
|
226
|
-
} catch {
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error(`[dsh-browser] cannot read the package manifest at ${manifestPath}: ${String(error)}`)
|
|
227
236
|
return { ok: false, reason: `${manifestPath} is missing; a directory without a package manifest is not a DSH Desktop install` }
|
|
228
237
|
}
|
|
229
238
|
mainPresent = existsSync(join(appDir, ...rule.mainEntry.split('/')))
|
|
@@ -94,7 +94,7 @@ export interface ProvisionDeps {
|
|
|
94
94
|
/** The installed electron package's own installer script, or undefined when it is not there. */
|
|
95
95
|
readonly installerScript: () => string | undefined
|
|
96
96
|
/** Runs the installer; resolves to an exit code, or rejects/returns null on failure. */
|
|
97
|
-
readonly run: (script: string) => Promise<{ code: number | null; detail?: string }>
|
|
97
|
+
readonly run: (script: string, signal?: AbortSignal) => Promise<{ code: number | null; detail?: string }>
|
|
98
98
|
/** Where the outcome goes. */
|
|
99
99
|
readonly log: (message: string) => void
|
|
100
100
|
/** Opt-out honoured exactly like the install script honours it. */
|
|
@@ -150,21 +150,35 @@ function electronVersion(): string {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
let runningInstaller: Promise<{ code: number | null; detail?: string }> | undefined
|
|
154
|
+
|
|
153
155
|
/** The real dependencies: this process's spawn, fs and logger. */
|
|
154
156
|
export function defaultDeps(log: (message: string) => void = () => {}): ProvisionDeps {
|
|
155
157
|
return {
|
|
156
158
|
hasBinary: hasElectronRuntime,
|
|
157
159
|
installerScript: electronInstallerScript,
|
|
158
|
-
run:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
160
|
+
run: (script, signal) => {
|
|
161
|
+
if (runningInstaller !== undefined) return runningInstaller
|
|
162
|
+
const run = new Promise<{ code: number | null; detail?: string }>(resolve => {
|
|
163
|
+
const child = spawn(process.execPath, [script], {
|
|
164
|
+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
|
|
165
|
+
stdio: ['ignore', 'ignore', 'ignore'],
|
|
166
|
+
windowsHide: true,
|
|
167
|
+
})
|
|
168
|
+
let failure: string | undefined
|
|
169
|
+
const abort = (): void => { child.kill('SIGKILL') }
|
|
170
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
171
|
+
if (signal?.aborted) abort()
|
|
172
|
+
child.once('error', error => { failure = String(error) })
|
|
173
|
+
child.once('close', code => {
|
|
174
|
+
signal?.removeEventListener('abort', abort)
|
|
175
|
+
resolve({ code, detail: failure })
|
|
176
|
+
})
|
|
167
177
|
})
|
|
178
|
+
runningInstaller = run
|
|
179
|
+
const clear = (): void => { if (runningInstaller === run) runningInstaller = undefined }
|
|
180
|
+
void run.then(clear, clear)
|
|
181
|
+
return run
|
|
168
182
|
},
|
|
169
183
|
log,
|
|
170
184
|
skip: () => typeof process.env.ELECTRON_SKIP_BINARY_DOWNLOAD === 'string'
|
|
@@ -188,12 +202,15 @@ export async function ensureElectronRuntime(deps: ProvisionDeps = defaultDeps())
|
|
|
188
202
|
if (script === undefined) {
|
|
189
203
|
return record({ status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' })
|
|
190
204
|
}
|
|
205
|
+
// Resolve the installed Electron version once: each call re-runs createRequire,
|
|
206
|
+
// require.resolve and a synchronous package.json read + parse.
|
|
207
|
+
const version = electronVersion()
|
|
191
208
|
latest = {
|
|
192
209
|
state: 'preparing',
|
|
193
|
-
detail: `downloading Electron ${
|
|
210
|
+
detail: `downloading Electron ${version} (one time, about a minute)`,
|
|
194
211
|
at: new Date().toISOString(),
|
|
195
212
|
}
|
|
196
|
-
deps.log(`preparing Electron ${
|
|
213
|
+
deps.log(`preparing Electron ${version} in the background (a missing runtime needs a download)`)
|
|
197
214
|
// The bound has to be a REFERENCED timer, and it has to be cleared when the race finishes.
|
|
198
215
|
//
|
|
199
216
|
// A first version unref'd it — "do not keep the process alive for a download" — which is exactly
|
|
@@ -202,15 +219,19 @@ export async function ensureElectronRuntime(deps: ProvisionDeps = defaultDeps())
|
|
|
202
219
|
// this path failed with "Promise resolution is still pending but the event loop has already
|
|
203
220
|
// resolved". Keep it referenced, and clear it so a success does not leave a 180 s handle behind.
|
|
204
221
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
222
|
+
const controller = new AbortController()
|
|
205
223
|
const timeout = new Promise<{ code: number | null; detail: string }>(resolve => {
|
|
206
224
|
timer = setTimeout(
|
|
207
|
-
() =>
|
|
225
|
+
() => {
|
|
226
|
+
controller.abort()
|
|
227
|
+
resolve({ code: null, detail: `gave up after ${Math.round(deps.timeoutMs / 1000)}s` })
|
|
228
|
+
},
|
|
208
229
|
deps.timeoutMs,
|
|
209
230
|
)
|
|
210
231
|
})
|
|
211
232
|
let outcome: { code: number | null; detail?: string }
|
|
212
233
|
try {
|
|
213
|
-
outcome = await Promise.race([deps.run(script), timeout])
|
|
234
|
+
outcome = await Promise.race([deps.run(script, controller.signal), timeout])
|
|
214
235
|
} catch (error) {
|
|
215
236
|
return record({ status: 'failed', detail: `the download failed: ${String(error)}` })
|
|
216
237
|
} finally {
|