dsh-builtin-browser 0.5.5 → 0.5.6
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 +61 -1
- package/README.en.md +5 -3
- package/README.md +4 -2
- package/client.js +300 -4
- package/docs/development.md +10 -0
- package/docs/user-guide.md +16 -4
- package/lib/browser-electron/bridge-connection.d.ts +8 -0
- package/lib/browser-electron/bridge-connection.js +8 -0
- package/lib/browser-electron/bridge-endpoint.d.ts +30 -0
- package/lib/browser-electron/bridge-endpoint.js +123 -0
- package/lib/browser-electron/desktop-bridge-host.d.ts +23 -23
- package/lib/browser-electron/desktop-bridge-host.js +35 -35
- package/lib/browser-electron/electron-provision.d.ts +34 -1
- package/lib/browser-electron/electron-provision.js +52 -9
- package/lib/browser-electron/entry.js +62 -1
- package/lib/browser-electron/json-reply.d.ts +21 -0
- package/lib/browser-electron/json-reply.js +17 -0
- package/lib/browser-electron/preview-route.js +1 -12
- package/lib/browser-electron/remote-host.js +6 -5
- package/lib/browser-electron/runtime-route.d.ts +57 -0
- package/lib/browser-electron/runtime-route.js +66 -0
- package/lib/browser-electron/runtime-status.d.ts +102 -0
- package/lib/browser-electron/runtime-status.js +86 -0
- package/package.json +2 -2
- package/scripts/consumer-install-check.mjs +72 -0
- package/scripts/verify-runtime-status.mjs +165 -0
- package/src/browser-electron/bridge-connection.ts +9 -0
- package/src/browser-electron/bridge-endpoint.ts +139 -0
- package/src/browser-electron/desktop-bridge-host.ts +35 -35
- package/src/browser-electron/electron-provision.ts +73 -8
- package/src/browser-electron/entry.ts +67 -1
- package/src/browser-electron/json-reply.ts +32 -0
- package/src/browser-electron/preview-route.ts +1 -12
- package/src/browser-electron/remote-host.ts +6 -5
- package/src/browser-electron/runtime-route.ts +106 -0
- package/src/browser-electron/runtime-status.ts +186 -0
|
@@ -45,6 +45,48 @@ export interface ProvisionResult {
|
|
|
45
45
|
readonly detail: string
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* The state a settings panel shows, which is one state wider than {@link ProvisionResult}.
|
|
50
|
+
*
|
|
51
|
+
* `idle` and `preparing` are the two a log line never needs and a user always does: "nothing has
|
|
52
|
+
* been asked for yet" and "a ~100 MB download is running right now" are the states in which a first
|
|
53
|
+
* browser call is slow, and until this existed neither was visible anywhere in the product — the
|
|
54
|
+
* only trace was a log line the panel cannot read.
|
|
55
|
+
*/
|
|
56
|
+
export interface ProvisionSnapshot {
|
|
57
|
+
/** `idle` and `preparing` are provisional; the rest are terminal for this attempt. */
|
|
58
|
+
readonly state: 'idle' | 'preparing' | 'present' | 'ready' | 'skipped' | 'failed'
|
|
59
|
+
/** One line, already user-facing: never a stack trace. */
|
|
60
|
+
readonly detail: string
|
|
61
|
+
/** When the state last changed, ISO. */
|
|
62
|
+
readonly at: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const IDLE: ProvisionSnapshot = {
|
|
66
|
+
state: 'idle',
|
|
67
|
+
detail: 'the runtime has not been asked for yet',
|
|
68
|
+
at: new Date(0).toISOString(),
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let latest: ProvisionSnapshot = IDLE
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The current provisioning state, for a readiness report.
|
|
75
|
+
*
|
|
76
|
+
* Deliberately a snapshot rather than a promise: a panel polling this must be able to answer in one
|
|
77
|
+
* synchronous read, without awaiting (and thereby echoing) the download it is reporting on.
|
|
78
|
+
* @returns the last state written, or the idle state before anything ran.
|
|
79
|
+
*/
|
|
80
|
+
export function provisionSnapshot(): ProvisionSnapshot {
|
|
81
|
+
return latest
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Replace the reported state. Internal: every terminal result is recorded here. */
|
|
85
|
+
function record(result: ProvisionResult): ProvisionResult {
|
|
86
|
+
latest = { state: result.status, detail: result.detail, at: new Date().toISOString() }
|
|
87
|
+
return result
|
|
88
|
+
}
|
|
89
|
+
|
|
48
90
|
/** Everything this module touches from the outside, so a test can drive it without Electron. */
|
|
49
91
|
export interface ProvisionDeps {
|
|
50
92
|
/** Whether a spawnable binary already exists. */
|
|
@@ -140,11 +182,16 @@ export function defaultDeps(log: (message: string) => void = () => {}): Provisio
|
|
|
140
182
|
* @returns what happened. Never throws.
|
|
141
183
|
*/
|
|
142
184
|
export async function ensureElectronRuntime(deps: ProvisionDeps = defaultDeps()): Promise<ProvisionResult> {
|
|
143
|
-
if (deps.hasBinary()) return { status: 'present', detail: 'an Electron runtime is already available' }
|
|
144
|
-
if (deps.skip()) return { status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' }
|
|
185
|
+
if (deps.hasBinary()) return record({ status: 'present', detail: 'an Electron runtime is already available' })
|
|
186
|
+
if (deps.skip()) return record({ status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' })
|
|
145
187
|
const script = deps.installerScript()
|
|
146
188
|
if (script === undefined) {
|
|
147
|
-
return { status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' }
|
|
189
|
+
return record({ status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' })
|
|
190
|
+
}
|
|
191
|
+
latest = {
|
|
192
|
+
state: 'preparing',
|
|
193
|
+
detail: `downloading Electron ${electronVersion()} (one time, about a minute)`,
|
|
194
|
+
at: new Date().toISOString(),
|
|
148
195
|
}
|
|
149
196
|
deps.log(`preparing Electron ${electronVersion()} in the background (a missing runtime needs a download)`)
|
|
150
197
|
// The bound has to be a REFERENCED timer, and it has to be cleared when the race finishes.
|
|
@@ -165,37 +212,53 @@ export async function ensureElectronRuntime(deps: ProvisionDeps = defaultDeps())
|
|
|
165
212
|
try {
|
|
166
213
|
outcome = await Promise.race([deps.run(script), timeout])
|
|
167
214
|
} catch (error) {
|
|
168
|
-
return { status: 'failed', detail: `the download failed: ${String(error)}` }
|
|
215
|
+
return record({ status: 'failed', detail: `the download failed: ${String(error)}` })
|
|
169
216
|
} finally {
|
|
170
217
|
if (timer !== undefined) clearTimeout(timer)
|
|
171
218
|
}
|
|
172
219
|
if (outcome.code !== 0) {
|
|
173
|
-
return {
|
|
220
|
+
return record({
|
|
174
221
|
status: 'failed',
|
|
175
222
|
detail: `Electron provisioning did not finish (${outcome.detail ?? `exit ${String(outcome.code)}`}); `
|
|
176
|
-
+ 'the browser still works through a sidebar, Chrome or Edge
|
|
177
|
-
|
|
223
|
+
+ 'the browser still works through a sidebar, Chrome or Edge; retry from Settings → Browser → '
|
|
224
|
+
+ 'Readiness ("Prepare the bundled browser"), or run `npx --no dsh-browser-install-electron`',
|
|
225
|
+
})
|
|
178
226
|
}
|
|
179
|
-
return { status: 'ready', detail: 'the Electron runtime is ready' }
|
|
227
|
+
return record({ status: 'ready', detail: 'the Electron runtime is ready' })
|
|
180
228
|
}
|
|
181
229
|
|
|
182
230
|
/** One attempt per process, shared by every caller. */
|
|
183
231
|
let attempt: Promise<ProvisionResult> | undefined
|
|
232
|
+
/** Whether {@link attempt} has settled, which is what makes a retry possible. */
|
|
233
|
+
let settled = false
|
|
184
234
|
|
|
185
235
|
/**
|
|
186
236
|
* Start (or join) the single background attempt.
|
|
187
237
|
*
|
|
188
238
|
* Returns immediately with the shared promise, so a caller that does not care can ignore it and one
|
|
189
239
|
* that does can await it — the returned promise never rejects.
|
|
240
|
+
*
|
|
241
|
+
* `force` is the second half of the settings panel's retry button. Without it the memoised attempt
|
|
242
|
+
* made a failed download permanent for the life of the process: the panel could offer a button, the
|
|
243
|
+
* button could call this, and the answer would be the failure it was trying to replace. An attempt
|
|
244
|
+
* still IN FLIGHT is always joined, never restarted — two downloads into one directory is the one
|
|
245
|
+
* outcome worth refusing.
|
|
190
246
|
* @param log - where the outcome goes.
|
|
191
247
|
* @param deps - the effects; defaulted to this process's, overridable so a test can drive it.
|
|
248
|
+
* @param options - `force` discards a settled attempt so a new one may start.
|
|
192
249
|
* @returns the shared attempt.
|
|
193
250
|
*/
|
|
194
251
|
export function provisionElectronInBackground(
|
|
195
252
|
log: (message: string) => void,
|
|
196
253
|
deps: ProvisionDeps = defaultDeps(log),
|
|
254
|
+
options: { readonly force?: boolean } = {},
|
|
197
255
|
): Promise<ProvisionResult> {
|
|
256
|
+
if (options.force === true && settled) {
|
|
257
|
+
attempt = undefined
|
|
258
|
+
settled = false
|
|
259
|
+
}
|
|
198
260
|
attempt ??= ensureElectronRuntime(deps).then(result => {
|
|
261
|
+
settled = true
|
|
199
262
|
log(`electron runtime: ${result.status} — ${result.detail}`)
|
|
200
263
|
return result
|
|
201
264
|
})
|
|
@@ -205,4 +268,6 @@ export function provisionElectronInBackground(
|
|
|
205
268
|
/** Forget the attempt. Tests only: a real process provisions at most once. */
|
|
206
269
|
export function resetProvisionStateForTest(): void {
|
|
207
270
|
attempt = undefined
|
|
271
|
+
settled = false
|
|
272
|
+
latest = IDLE
|
|
208
273
|
}
|
|
@@ -15,7 +15,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
|
15
15
|
import z from '@deepseek-ai/schemastery'
|
|
16
16
|
import type { BrowserRuntime } from '../browser/runtime.js'
|
|
17
17
|
import { ElectronBrowserProvider } from './provider.js'
|
|
18
|
-
import { provisionElectronInBackground } from './electron-provision.js'
|
|
18
|
+
import { provisionElectronInBackground, provisionSnapshot, hasElectronRuntime } from './electron-provision.js'
|
|
19
19
|
import type { ElectronBrowserViewHost } from './provider.js'
|
|
20
20
|
import type { CarrierLossReason } from '../browser/types.js'
|
|
21
21
|
import { randomUUID } from 'node:crypto'
|
|
@@ -31,6 +31,10 @@ import { MissingSystemBrowserHost } from './missing-system-browser.js'
|
|
|
31
31
|
import { SettingsStore, settingsPath } from './settings-store.js'
|
|
32
32
|
import { sameOrigin } from './settings-route.js'
|
|
33
33
|
import { handleBridgeStatus, handlePreviewFrame, handlePreviewSessions, BRIDGE_STATUS_ROUTE, PREVIEW_ROUTE, PREVIEW_SESSIONS_ROUTE } from './preview-route.js'
|
|
34
|
+
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js'
|
|
35
|
+
import { readBridgeEndpoint } from './bridge-endpoint.js'
|
|
36
|
+
import { readRuntimeStatus } from './runtime-status.js'
|
|
37
|
+
import { handleRuntimeProvision, handleRuntimeStatus, RUNTIME_PROVISION_ROUTE, RUNTIME_STATUS_ROUTE } from './runtime-route.js'
|
|
34
38
|
|
|
35
39
|
export {
|
|
36
40
|
ELECTRON_BROWSER_PROVIDER_ID,
|
|
@@ -509,6 +513,67 @@ export function apply(ctx: Context & { browser: BrowserRuntime }, config: Config
|
|
|
509
513
|
|
|
510
514
|
installSettingsRoute(ctx, settings, () => { chooseCarrier() })
|
|
511
515
|
|
|
516
|
+
/**
|
|
517
|
+
* Expose readiness — "can the browser serve a call right now" — for the settings panel.
|
|
518
|
+
*
|
|
519
|
+
* The panel had switches for every behaviour and no answer to the one question a new install
|
|
520
|
+
* raises: the bundled runtime is downloaded on first need (issue #41 moved it out of `postinstall`
|
|
521
|
+
* for good reasons), so for the first minute of a fresh install the honest state is "preparing",
|
|
522
|
+
* and until this route existed that state was a log line no panel could read. Both readings are
|
|
523
|
+
* cheap and side-effect free: a path probe for the runtime, an install-tree inspection for the
|
|
524
|
+
* bridge — the same two the launch path and the bridge-status route already use.
|
|
525
|
+
*
|
|
526
|
+
* Skipped, like its siblings, on a surface with no web server: a CLI has no panel to show it to.
|
|
527
|
+
* @param ctx - the plugin context.
|
|
528
|
+
* @param settings - the settings document, for the channel being honoured.
|
|
529
|
+
*/
|
|
530
|
+
function installRuntimeStatusRoute(ctx: Context, settings: SettingsStore): void {
|
|
531
|
+
const injectRuntime = (ctx as unknown as {
|
|
532
|
+
inject?: (deps: readonly string[], callback: (host: WebServerHost) => void) => void
|
|
533
|
+
}).inject
|
|
534
|
+
if (typeof injectRuntime !== 'function') return
|
|
535
|
+
const deps = {
|
|
536
|
+
report: () => readRuntimeStatus({
|
|
537
|
+
channel: () => settings.get().browser.channel,
|
|
538
|
+
hasBinary: hasElectronRuntime,
|
|
539
|
+
provision: provisionSnapshot,
|
|
540
|
+
bridge: () => {
|
|
541
|
+
const root = findDesktopInstall()
|
|
542
|
+
return root === undefined ? undefined : readBridgeStatus(root)
|
|
543
|
+
},
|
|
544
|
+
// Installed is not running: the install tree survives a reboot, the endpoint does not. Both
|
|
545
|
+
// readings are cheap and side-effect free — a file read and a signal-0 pid check.
|
|
546
|
+
endpoint: () => readBridgeEndpoint(),
|
|
547
|
+
}),
|
|
548
|
+
// `force` is what makes the panel's retry button mean anything: without it the answer would be
|
|
549
|
+
// the settled failure the user is trying to replace. An attempt still running is joined.
|
|
550
|
+
provision: () => {
|
|
551
|
+
void provisionElectronInBackground(
|
|
552
|
+
message => ctx.logger?.info?.(`dsh-builtin-browser: ${message}`),
|
|
553
|
+
undefined,
|
|
554
|
+
{ force: true },
|
|
555
|
+
)
|
|
556
|
+
return provisionSnapshot()
|
|
557
|
+
},
|
|
558
|
+
}
|
|
559
|
+
try {
|
|
560
|
+
injectRuntime(['webServer'], (host) => {
|
|
561
|
+
host.effect(() => host.webServer.register({
|
|
562
|
+
kind: 'exact',
|
|
563
|
+
path: RUNTIME_STATUS_ROUTE,
|
|
564
|
+
handler: (request, response) => handleRuntimeStatus(request, response, deps),
|
|
565
|
+
}, 'dsh-builtin-browser: runtime status'))
|
|
566
|
+
host.effect(() => host.webServer.register({
|
|
567
|
+
kind: 'exact',
|
|
568
|
+
path: RUNTIME_PROVISION_ROUTE,
|
|
569
|
+
handler: (request, response) => handleRuntimeProvision(request, response, deps),
|
|
570
|
+
}, 'dsh-builtin-browser: runtime provision'))
|
|
571
|
+
})
|
|
572
|
+
} catch {
|
|
573
|
+
// No web server: the panel is unreachable, and the readiness report has no reader.
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
512
577
|
/**
|
|
513
578
|
* Re-run the carrier choice when the settings FILE changes on disk.
|
|
514
579
|
*
|
|
@@ -545,6 +610,7 @@ export function apply(ctx: Context & { browser: BrowserRuntime }, config: Config
|
|
|
545
610
|
watchSettingsFile()
|
|
546
611
|
installPreviewRoute(ctx, settings)
|
|
547
612
|
installBridgeStatusRoute(ctx)
|
|
613
|
+
installRuntimeStatusRoute(ctx, settings)
|
|
548
614
|
}
|
|
549
615
|
|
|
550
616
|
/** Route the settings panel reads and writes. */
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One JSON reply, written the same way by every route this plugin serves.
|
|
3
|
+
*
|
|
4
|
+
* Its own module rather than a helper inside one route file: there are four endpoints now, and the
|
|
5
|
+
* second one that needed this had to import it from the live-view route — which reads as though the
|
|
6
|
+
* readiness endpoints belong to the preview. They do not; only the header rules are shared.
|
|
7
|
+
*
|
|
8
|
+
* The header set is the substance, not boilerplate:
|
|
9
|
+
* - `content-length` computed from the bytes actually written, so a proxy cannot truncate;
|
|
10
|
+
* - `no-store` because every answer here is a reading of the current moment;
|
|
11
|
+
* - `nosniff` + a same-origin resource policy, because these bodies name paths and pids.
|
|
12
|
+
* @module dsh-browser/browser-electron/json-reply
|
|
13
|
+
*/
|
|
14
|
+
import type { ServerResponse } from 'node:http'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Write one JSON body with no caching and no sniffing.
|
|
18
|
+
* @param response - the response to write.
|
|
19
|
+
* @param status - the HTTP status.
|
|
20
|
+
* @param body - anything JSON-serialisable.
|
|
21
|
+
*/
|
|
22
|
+
export function replyJson(response: ServerResponse, status: number, body: unknown): void {
|
|
23
|
+
const text = JSON.stringify(body)
|
|
24
|
+
response.writeHead(status, {
|
|
25
|
+
'content-type': 'application/json; charset=utf-8',
|
|
26
|
+
'content-length': Buffer.byteLength(text),
|
|
27
|
+
'cache-control': 'no-store, private',
|
|
28
|
+
'x-content-type-options': 'nosniff',
|
|
29
|
+
'cross-origin-resource-policy': 'same-origin',
|
|
30
|
+
})
|
|
31
|
+
response.end(text)
|
|
32
|
+
}
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
16
16
|
import type { BrowserError } from '../browser/types.js'
|
|
17
|
+
import { replyJson } from './json-reply.js'
|
|
17
18
|
import { sameOrigin } from './settings-route.js'
|
|
18
19
|
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js'
|
|
19
20
|
|
|
@@ -89,18 +90,6 @@ export async function handlePreviewSessions(
|
|
|
89
90
|
replyJson(response, 200, { enabled: true, sessions: [...sessions] })
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
/** Write one JSON body with no caching and no sniffing. */
|
|
93
|
-
function replyJson(response: ServerResponse, status: number, body: unknown): void {
|
|
94
|
-
const text = JSON.stringify(body)
|
|
95
|
-
response.writeHead(status, {
|
|
96
|
-
'content-type': 'application/json; charset=utf-8',
|
|
97
|
-
'content-length': Buffer.byteLength(text),
|
|
98
|
-
'cache-control': 'no-store, private',
|
|
99
|
-
'x-content-type-options': 'nosniff',
|
|
100
|
-
'cross-origin-resource-policy': 'same-origin',
|
|
101
|
-
})
|
|
102
|
-
response.end(text)
|
|
103
|
-
}
|
|
104
93
|
|
|
105
94
|
/**
|
|
106
95
|
* Serve one frame, or say precisely why not.
|
|
@@ -141,11 +141,12 @@ function resolveElectronPathImpl(inputs: ElectronPathInputs): string {
|
|
|
141
141
|
'dsh-builtin-browser: cannot locate a spawnable Electron binary. ' +
|
|
142
142
|
'The electron package ships with the plugin and its binary is downloaded on first use, not at install time ' +
|
|
143
143
|
'(issue #41: an install-time download made default profiles refuse the plugin). The plugin prepares it ' +
|
|
144
|
-
'automatically when the bundled carrier is selected
|
|
145
|
-
'
|
|
146
|
-
'
|
|
147
|
-
'
|
|
148
|
-
'
|
|
144
|
+
'automatically when the bundled carrier is selected, so right after an install this can mean the ' +
|
|
145
|
+
'download is still running: Settings → Browser → Readiness shows that state and can retry it. To do it ' +
|
|
146
|
+
'by hand instead, run `npx --no dsh-browser-install-electron` in the profile that installed the ' +
|
|
147
|
+
'plugin, then retry. Alternatively set ELECTRON_PATH to an electron executable. Note: the host\'s own ' +
|
|
148
|
+
'executable cannot be reused when it is a packaged Electron app (e.g. DSH Desktop.exe) — only a real ' +
|
|
149
|
+
'electron binary can be spawned.',
|
|
149
150
|
)
|
|
150
151
|
}
|
|
151
152
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The readiness endpoints the settings panel reads.
|
|
3
|
+
*
|
|
4
|
+
* Two of them, because the two questions have different weights: reading the state is a `GET` any
|
|
5
|
+
* same-origin document may make, and starting a download is a `POST` held to the stricter admission
|
|
6
|
+
* rule in `settings-route.ts`. No credential system exists here — that module's own comment says
|
|
7
|
+
* what the rule buys — but a request that STARTS a 100 MB download is on the wrong side of the same
|
|
8
|
+
* line the settings write is on, so it is checked the way a write is.
|
|
9
|
+
*
|
|
10
|
+
* Both answer with the whole report rather than a delta: the panel replaces what it shows with what
|
|
11
|
+
* it is told, so a stale poll can never leave a wrong state on screen.
|
|
12
|
+
* @module dsh-browser/browser-electron/runtime-route
|
|
13
|
+
*/
|
|
14
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
15
|
+
import type { ProvisionSnapshot } from './electron-provision.js'
|
|
16
|
+
import { replyJson } from './json-reply.js'
|
|
17
|
+
import { sameOrigin } from './settings-route.js'
|
|
18
|
+
import type { RuntimeStatusReport } from './runtime-status.js'
|
|
19
|
+
|
|
20
|
+
/** Where the panel reads readiness. */
|
|
21
|
+
export const RUNTIME_STATUS_ROUTE = '/dsh-builtin-browser/runtime-status'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Where the panel asks for the bundled runtime.
|
|
25
|
+
*
|
|
26
|
+
* Non-blocking by design: it starts (or joins) the one background attempt and answers immediately
|
|
27
|
+
* with the state that attempt is in, which is `preparing`. A request that waited for the download
|
|
28
|
+
* would time out in the browser long before a slow network finished, and would hold a connection
|
|
29
|
+
* open for something the panel can poll instead.
|
|
30
|
+
*/
|
|
31
|
+
export const RUNTIME_PROVISION_ROUTE = '/dsh-builtin-browser/runtime-provision'
|
|
32
|
+
|
|
33
|
+
/** What both handlers need. */
|
|
34
|
+
export interface RuntimeRouteDeps {
|
|
35
|
+
/** The current report; a cheap read, never a spawn. */
|
|
36
|
+
readonly report: () => RuntimeStatusReport
|
|
37
|
+
/**
|
|
38
|
+
* Start (or join) a provision and return the state it is in now.
|
|
39
|
+
*
|
|
40
|
+
* Must not throw: a failure belongs in the report as a 200 with a `failed` state, because a user
|
|
41
|
+
* looking at a panel needs to read the reason, not an HTTP status.
|
|
42
|
+
*/
|
|
43
|
+
readonly provision: () => ProvisionSnapshot
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Read readiness: `{ ok, runtime }`.
|
|
48
|
+
* @param request - the incoming request.
|
|
49
|
+
* @param response - the response to write.
|
|
50
|
+
* @param deps - the readers.
|
|
51
|
+
*/
|
|
52
|
+
export async function handleRuntimeStatus(
|
|
53
|
+
request: IncomingMessage,
|
|
54
|
+
response: ServerResponse,
|
|
55
|
+
deps: RuntimeRouteDeps,
|
|
56
|
+
): Promise<void> {
|
|
57
|
+
if (request.method !== 'GET') {
|
|
58
|
+
replyJson(response, 405, { error: 'method-not-allowed' })
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
if (!sameOrigin(request)) {
|
|
62
|
+
replyJson(response, 403, { error: 'not-same-origin' })
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
replyJson(response, 200, { ok: true, runtime: deps.report() })
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Start (or join) a provision, then answer with the state it is in.
|
|
70
|
+
*
|
|
71
|
+
* Always 200 once admitted, whatever happened: the state carries `failed` with its reason, and a
|
|
72
|
+
* panel that had to distinguish 4xx from 5xx to say "the download failed" would show a network
|
|
73
|
+
* error where the user needs the sentence explaining it.
|
|
74
|
+
* @param request - the incoming request.
|
|
75
|
+
* @param response - the response to write.
|
|
76
|
+
* @param deps - the readers.
|
|
77
|
+
*/
|
|
78
|
+
export async function handleRuntimeProvision(
|
|
79
|
+
request: IncomingMessage,
|
|
80
|
+
response: ServerResponse,
|
|
81
|
+
deps: RuntimeRouteDeps,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
if (request.method !== 'POST') {
|
|
84
|
+
replyJson(response, 405, { error: 'method-not-allowed' })
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
// `true`: starting a download is a write, and is admitted the way a settings write is.
|
|
88
|
+
if (!sameOrigin(request, true)) {
|
|
89
|
+
replyJson(response, 403, { error: 'not-same-origin' })
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
let snapshot: ProvisionSnapshot
|
|
93
|
+
try {
|
|
94
|
+
snapshot = deps.provision()
|
|
95
|
+
} catch (error) {
|
|
96
|
+
// `provision` is documented not to throw; if a caller breaks that contract the panel still gets
|
|
97
|
+
// an answer it can render, rather than a dropped connection it cannot tell from a hang.
|
|
98
|
+
replyJson(response, 200, {
|
|
99
|
+
ok: true,
|
|
100
|
+
runtime: deps.report(),
|
|
101
|
+
provisionError: String(error),
|
|
102
|
+
})
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
replyJson(response, 200, { ok: true, runtime: deps.report(), provision: snapshot })
|
|
106
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One answer to "can I use the browser right now", assembled from the three things that decide it.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS
|
|
5
|
+
* Installing this plugin made the browser work with no setup, and that property got silently
|
|
6
|
+
* weaker as the carriers were added: the bundled carrier downloads a ~100 MB Electron runtime on
|
|
7
|
+
* first need (issue #41 — the download had to leave `postinstall`), so on a fresh install the first
|
|
8
|
+
* browser call can arrive while that download is still running, and the only trace anywhere in the
|
|
9
|
+
* product was a log line the settings panel cannot read. A user who saw "cannot locate a spawnable
|
|
10
|
+
* Electron binary" had no way to learn that the plugin was already fetching one, and no button to
|
|
11
|
+
* retry it once it failed.
|
|
12
|
+
*
|
|
13
|
+
* So the readiness of each part is computed ONCE here, in a pure function over injectable readers:
|
|
14
|
+
* the settings panel renders it, and a test drives it without touching Electron, a desktop install
|
|
15
|
+
* or the file system.
|
|
16
|
+
*
|
|
17
|
+
* Everything here is a cheap read by construction. `hasBinary` resolves a path (the same probe a
|
|
18
|
+
* launch uses, so the answer can never disagree with a launch), and the bridge reading is the same
|
|
19
|
+
* install-tree inspection the bridge-status route already serves. Nothing spawns anything.
|
|
20
|
+
* @module dsh-browser/browser-electron/runtime-status
|
|
21
|
+
*/
|
|
22
|
+
import type { BridgeStatus } from './bridge-status.js'
|
|
23
|
+
import type { BridgeEndpointState } from './bridge-endpoint.js'
|
|
24
|
+
import type { ProvisionSnapshot } from './electron-provision.js'
|
|
25
|
+
import type { BrowserChannel } from './system-browser.js'
|
|
26
|
+
|
|
27
|
+
/** The part of the carrier the channel names, in the terms the panel has to explain. */
|
|
28
|
+
export type CarrierKind = 'sidebar' | 'bundled' | 'system' | 'auto'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether the readiness question has an answer at all.
|
|
32
|
+
*
|
|
33
|
+
* `unknown` is not a defect: an explicit Chrome / Edge choice is only falsifiable by launching one,
|
|
34
|
+
* and launching a browser to answer a status question is exactly what this report must not do. The
|
|
35
|
+
* panel says so in words instead of guessing a colour.
|
|
36
|
+
*/
|
|
37
|
+
export type Readiness = 'ready' | 'blocked' | 'unknown'
|
|
38
|
+
|
|
39
|
+
/** The Electron runtime's state, as the panel needs it. */
|
|
40
|
+
export interface ElectronReadiness {
|
|
41
|
+
/** `present` — a launch would find a binary; `preparing` — a download is running now; and so on. */
|
|
42
|
+
readonly state: ProvisionSnapshot['state']
|
|
43
|
+
/** One line, already user-facing: the provisioning module's own words. */
|
|
44
|
+
readonly detail: string
|
|
45
|
+
/** When that state was reached, ISO. */
|
|
46
|
+
readonly at: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The desktop sidebar bridge, or the honest "there is no desktop here" shape. */
|
|
50
|
+
export interface BridgeReadiness {
|
|
51
|
+
/** Whether a DSH Desktop install was found at all. */
|
|
52
|
+
readonly present: boolean
|
|
53
|
+
/** `absent` is reported separately from `missing`: nothing to repair versus something to repair. */
|
|
54
|
+
readonly state: BridgeStatus['state'] | 'absent'
|
|
55
|
+
/** The plugin's own words about the install tree. */
|
|
56
|
+
readonly detail: string
|
|
57
|
+
/**
|
|
58
|
+
* Whether a shell is RUNNING and answering, as opposed to merely installed.
|
|
59
|
+
*
|
|
60
|
+
* Install-tree state survives a reboot and says nothing about a process; the published endpoint is
|
|
61
|
+
* the only thing that does. Reporting "installed" as "ready" is how a panel goes green while every
|
|
62
|
+
* call dials a port nobody owns — the desktop app being closed is the most common broken state
|
|
63
|
+
* there is.
|
|
64
|
+
*/
|
|
65
|
+
readonly running: boolean
|
|
66
|
+
/** The plugin's own words about the running shell (or about why there is none). */
|
|
67
|
+
readonly runningDetail: string
|
|
68
|
+
/** The command that installs or repairs it, for a human to run. Never run by the plugin. */
|
|
69
|
+
readonly command?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The whole report. */
|
|
73
|
+
export interface RuntimeStatusReport {
|
|
74
|
+
/** What the settings document says, verbatim: the panel shows the switch it is honouring. */
|
|
75
|
+
readonly channel: BrowserChannel
|
|
76
|
+
/** What that channel resolves to, in panel terms. */
|
|
77
|
+
readonly carrier: CarrierKind
|
|
78
|
+
/** Whether the browser this channel names can serve a call right now. */
|
|
79
|
+
readonly readiness: Readiness
|
|
80
|
+
readonly electron: ElectronReadiness
|
|
81
|
+
readonly bridge: BridgeReadiness
|
|
82
|
+
/** Whether "Prepare the bundled browser" is worth offering (it is not while one is running). */
|
|
83
|
+
readonly canProvision: boolean
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Everything the report reads, so a test can answer without any of the real machinery. */
|
|
87
|
+
export interface RuntimeStatusDeps {
|
|
88
|
+
/** The stored `browser.channel`. */
|
|
89
|
+
readonly channel: () => BrowserChannel
|
|
90
|
+
/** Whether a launch would find an Electron binary — the launch path's own probe. */
|
|
91
|
+
readonly hasBinary: () => boolean
|
|
92
|
+
/** The provisioning state machine's current state. */
|
|
93
|
+
readonly provision: () => ProvisionSnapshot
|
|
94
|
+
/** The desktop install's bridge state, or undefined when there is no desktop install. */
|
|
95
|
+
readonly bridge: () => BridgeStatus | undefined
|
|
96
|
+
/** The endpoint the shell publishes while it runs; undefined when nothing published one. */
|
|
97
|
+
readonly endpoint: () => BridgeEndpointState | undefined
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Which carrier a channel names. */
|
|
101
|
+
function carrierOf(channel: BrowserChannel): CarrierKind {
|
|
102
|
+
if (channel === 'sidebar') return 'sidebar'
|
|
103
|
+
if (channel === 'bundled') return 'bundled'
|
|
104
|
+
if (channel === 'auto') return 'auto'
|
|
105
|
+
return 'system'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Whether a binary being present settles the Electron question. */
|
|
109
|
+
function electronReady(state: ElectronReadiness['state']): boolean {
|
|
110
|
+
return state === 'present' || state === 'ready'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Assemble the report.
|
|
115
|
+
*
|
|
116
|
+
* The `readiness` field is per carrier on purpose: "the browser is ready" is not a property of this
|
|
117
|
+
* plugin, it is a property of the carrier the user chose. A bundled carrier with a finished download
|
|
118
|
+
* is ready; the sidebar carrier with no bridge is not, however healthy the bundled runtime is.
|
|
119
|
+
* @param deps - the readers; every one of them is a cheap, side-effect-free read.
|
|
120
|
+
* @returns the report the panel renders.
|
|
121
|
+
*/
|
|
122
|
+
export function readRuntimeStatus(deps: RuntimeStatusDeps): RuntimeStatusReport {
|
|
123
|
+
const channel = deps.channel()
|
|
124
|
+
const carrier = carrierOf(channel)
|
|
125
|
+
|
|
126
|
+
// The binary check comes first and wins: a runtime that is on disk is ready whatever the
|
|
127
|
+
// provisioning state machine last said, which is also how a runtime installed by hand (or by an
|
|
128
|
+
// earlier process) is reported correctly without a restart. The DETAIL has to follow the same
|
|
129
|
+
// rule, or the panel shows "present" next to "the runtime has not been asked for yet" — measured
|
|
130
|
+
// on a live install, and the kind of contradiction a reader cannot resolve.
|
|
131
|
+
const snapshot = deps.provision()
|
|
132
|
+
const runnable = deps.hasBinary()
|
|
133
|
+
const state: ElectronReadiness['state'] = runnable ? 'present' : snapshot.state
|
|
134
|
+
const electron: ElectronReadiness = {
|
|
135
|
+
state,
|
|
136
|
+
detail: runnable ? 'an Electron runtime is already available' : snapshot.detail,
|
|
137
|
+
at: runnable ? new Date().toISOString() : snapshot.at,
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const bridgeStatus = deps.bridge()
|
|
141
|
+
const endpoint = deps.endpoint()
|
|
142
|
+
const bridge: BridgeReadiness = bridgeStatus === undefined
|
|
143
|
+
? {
|
|
144
|
+
present: false,
|
|
145
|
+
state: 'absent',
|
|
146
|
+
detail: 'no DSH Desktop install was found on this machine',
|
|
147
|
+
running: endpoint?.alive === true,
|
|
148
|
+
runningDetail: endpoint?.detail ?? 'no desktop shell has published a bridge endpoint',
|
|
149
|
+
}
|
|
150
|
+
: {
|
|
151
|
+
present: bridgeStatus.present,
|
|
152
|
+
state: bridgeStatus.state,
|
|
153
|
+
detail: bridgeStatus.detail,
|
|
154
|
+
running: endpoint?.alive === true,
|
|
155
|
+
runningDetail: endpoint?.detail ?? 'no desktop shell has published a bridge endpoint',
|
|
156
|
+
...bridgeStatus.command === undefined ? {} : { command: bridgeStatus.command },
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The sidebar needs BOTH halves: our patch inside the shell it will be driven through, and that
|
|
160
|
+
// shell actually running. Either one alone is the false green this report exists to prevent.
|
|
161
|
+
const sidebarReady = bridge.state === 'installed' && bridge.running
|
|
162
|
+
|
|
163
|
+
let readiness: Readiness
|
|
164
|
+
if (carrier === 'sidebar') {
|
|
165
|
+
readiness = sidebarReady ? 'ready' : 'blocked'
|
|
166
|
+
} else if (carrier === 'bundled') {
|
|
167
|
+
readiness = electronReady(state) ? 'ready' : 'blocked'
|
|
168
|
+
} else if (carrier === 'auto') {
|
|
169
|
+
// `auto` is satisfied by either carrier, so a working sidebar covers a missing runtime. An
|
|
170
|
+
// explicit Chrome / Edge install would satisfy it too, but proving that means launching one.
|
|
171
|
+
readiness = electronReady(state) || sidebarReady ? 'ready' : 'unknown'
|
|
172
|
+
} else {
|
|
173
|
+
readiness = 'unknown'
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
channel,
|
|
178
|
+
carrier,
|
|
179
|
+
readiness,
|
|
180
|
+
electron,
|
|
181
|
+
bridge,
|
|
182
|
+
// Offering the button while a download runs would be a second download, which is the one thing
|
|
183
|
+
// the shared attempt exists to prevent.
|
|
184
|
+
canProvision: !electronReady(state) && state !== 'preparing',
|
|
185
|
+
}
|
|
186
|
+
}
|