dsh-builtin-browser 0.5.4 → 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 +63 -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
|
@@ -34,6 +34,27 @@ import { dirname, join } from 'node:path';
|
|
|
34
34
|
import { internals } from './remote-host.js';
|
|
35
35
|
/** How long a background provision may run before it is abandoned. */
|
|
36
36
|
export const PROVISION_TIMEOUT_MS = 180_000;
|
|
37
|
+
const IDLE = {
|
|
38
|
+
state: 'idle',
|
|
39
|
+
detail: 'the runtime has not been asked for yet',
|
|
40
|
+
at: new Date(0).toISOString(),
|
|
41
|
+
};
|
|
42
|
+
let latest = IDLE;
|
|
43
|
+
/**
|
|
44
|
+
* The current provisioning state, for a readiness report.
|
|
45
|
+
*
|
|
46
|
+
* Deliberately a snapshot rather than a promise: a panel polling this must be able to answer in one
|
|
47
|
+
* synchronous read, without awaiting (and thereby echoing) the download it is reporting on.
|
|
48
|
+
* @returns the last state written, or the idle state before anything ran.
|
|
49
|
+
*/
|
|
50
|
+
export function provisionSnapshot() {
|
|
51
|
+
return latest;
|
|
52
|
+
}
|
|
53
|
+
/** Replace the reported state. Internal: every terminal result is recorded here. */
|
|
54
|
+
function record(result) {
|
|
55
|
+
latest = { state: result.status, detail: result.detail, at: new Date().toISOString() };
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
37
58
|
/**
|
|
38
59
|
* Whether a spawnable Electron binary is already available.
|
|
39
60
|
*
|
|
@@ -113,13 +134,18 @@ export function defaultDeps(log = () => { }) {
|
|
|
113
134
|
*/
|
|
114
135
|
export async function ensureElectronRuntime(deps = defaultDeps()) {
|
|
115
136
|
if (deps.hasBinary())
|
|
116
|
-
return { status: 'present', detail: 'an Electron runtime is already available' };
|
|
137
|
+
return record({ status: 'present', detail: 'an Electron runtime is already available' });
|
|
117
138
|
if (deps.skip())
|
|
118
|
-
return { status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' };
|
|
139
|
+
return record({ status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' });
|
|
119
140
|
const script = deps.installerScript();
|
|
120
141
|
if (script === undefined) {
|
|
121
|
-
return { status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' };
|
|
142
|
+
return record({ status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' });
|
|
122
143
|
}
|
|
144
|
+
latest = {
|
|
145
|
+
state: 'preparing',
|
|
146
|
+
detail: `downloading Electron ${electronVersion()} (one time, about a minute)`,
|
|
147
|
+
at: new Date().toISOString(),
|
|
148
|
+
};
|
|
123
149
|
deps.log(`preparing Electron ${electronVersion()} in the background (a missing runtime needs a download)`);
|
|
124
150
|
// The bound has to be a REFERENCED timer, and it has to be cleared when the race finishes.
|
|
125
151
|
//
|
|
@@ -137,34 +163,49 @@ export async function ensureElectronRuntime(deps = defaultDeps()) {
|
|
|
137
163
|
outcome = await Promise.race([deps.run(script), timeout]);
|
|
138
164
|
}
|
|
139
165
|
catch (error) {
|
|
140
|
-
return { status: 'failed', detail: `the download failed: ${String(error)}` };
|
|
166
|
+
return record({ status: 'failed', detail: `the download failed: ${String(error)}` });
|
|
141
167
|
}
|
|
142
168
|
finally {
|
|
143
169
|
if (timer !== undefined)
|
|
144
170
|
clearTimeout(timer);
|
|
145
171
|
}
|
|
146
172
|
if (outcome.code !== 0) {
|
|
147
|
-
return {
|
|
173
|
+
return record({
|
|
148
174
|
status: 'failed',
|
|
149
175
|
detail: `Electron provisioning did not finish (${outcome.detail ?? `exit ${String(outcome.code)}`}); `
|
|
150
|
-
+ 'the browser still works through a sidebar, Chrome or Edge
|
|
151
|
-
|
|
176
|
+
+ 'the browser still works through a sidebar, Chrome or Edge; retry from Settings → Browser → '
|
|
177
|
+
+ 'Readiness ("Prepare the bundled browser"), or run `npx --no dsh-browser-install-electron`',
|
|
178
|
+
});
|
|
152
179
|
}
|
|
153
|
-
return { status: 'ready', detail: 'the Electron runtime is ready' };
|
|
180
|
+
return record({ status: 'ready', detail: 'the Electron runtime is ready' });
|
|
154
181
|
}
|
|
155
182
|
/** One attempt per process, shared by every caller. */
|
|
156
183
|
let attempt;
|
|
184
|
+
/** Whether {@link attempt} has settled, which is what makes a retry possible. */
|
|
185
|
+
let settled = false;
|
|
157
186
|
/**
|
|
158
187
|
* Start (or join) the single background attempt.
|
|
159
188
|
*
|
|
160
189
|
* Returns immediately with the shared promise, so a caller that does not care can ignore it and one
|
|
161
190
|
* that does can await it — the returned promise never rejects.
|
|
191
|
+
*
|
|
192
|
+
* `force` is the second half of the settings panel's retry button. Without it the memoised attempt
|
|
193
|
+
* made a failed download permanent for the life of the process: the panel could offer a button, the
|
|
194
|
+
* button could call this, and the answer would be the failure it was trying to replace. An attempt
|
|
195
|
+
* still IN FLIGHT is always joined, never restarted — two downloads into one directory is the one
|
|
196
|
+
* outcome worth refusing.
|
|
162
197
|
* @param log - where the outcome goes.
|
|
163
198
|
* @param deps - the effects; defaulted to this process's, overridable so a test can drive it.
|
|
199
|
+
* @param options - `force` discards a settled attempt so a new one may start.
|
|
164
200
|
* @returns the shared attempt.
|
|
165
201
|
*/
|
|
166
|
-
export function provisionElectronInBackground(log, deps = defaultDeps(log)) {
|
|
202
|
+
export function provisionElectronInBackground(log, deps = defaultDeps(log), options = {}) {
|
|
203
|
+
if (options.force === true && settled) {
|
|
204
|
+
attempt = undefined;
|
|
205
|
+
settled = false;
|
|
206
|
+
}
|
|
167
207
|
attempt ??= ensureElectronRuntime(deps).then(result => {
|
|
208
|
+
settled = true;
|
|
168
209
|
log(`electron runtime: ${result.status} — ${result.detail}`);
|
|
169
210
|
return result;
|
|
170
211
|
});
|
|
@@ -173,4 +214,6 @@ export function provisionElectronInBackground(log, deps = defaultDeps(log)) {
|
|
|
173
214
|
/** Forget the attempt. Tests only: a real process provisions at most once. */
|
|
174
215
|
export function resetProvisionStateForTest() {
|
|
175
216
|
attempt = undefined;
|
|
217
|
+
settled = false;
|
|
218
|
+
latest = IDLE;
|
|
176
219
|
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import z from '@deepseek-ai/schemastery';
|
|
13
13
|
import { ElectronBrowserProvider } from './provider.js';
|
|
14
|
-
import { provisionElectronInBackground } from './electron-provision.js';
|
|
14
|
+
import { provisionElectronInBackground, provisionSnapshot, hasElectronRuntime } from './electron-provision.js';
|
|
15
15
|
import { randomUUID } from 'node:crypto';
|
|
16
16
|
import { existsSync, mkdirSync, renameSync, statSync } from 'node:fs';
|
|
17
17
|
import { homedir } from 'node:os';
|
|
@@ -25,6 +25,10 @@ import { MissingSystemBrowserHost } from './missing-system-browser.js';
|
|
|
25
25
|
import { SettingsStore, settingsPath } from './settings-store.js';
|
|
26
26
|
import { sameOrigin } from './settings-route.js';
|
|
27
27
|
import { handleBridgeStatus, handlePreviewFrame, handlePreviewSessions, BRIDGE_STATUS_ROUTE, PREVIEW_ROUTE, PREVIEW_SESSIONS_ROUTE } from './preview-route.js';
|
|
28
|
+
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js';
|
|
29
|
+
import { readBridgeEndpoint } from './bridge-endpoint.js';
|
|
30
|
+
import { readRuntimeStatus } from './runtime-status.js';
|
|
31
|
+
import { handleRuntimeProvision, handleRuntimeStatus, RUNTIME_PROVISION_ROUTE, RUNTIME_STATUS_ROUTE } from './runtime-route.js';
|
|
28
32
|
export { ELECTRON_BROWSER_PROVIDER_ID, ElectronBrowserProvider, } from './provider.js';
|
|
29
33
|
export { RemoteElectronViewHost, defaultHostMainPath } from './remote-host.js';
|
|
30
34
|
/** Cordis plugin name used by loader diagnostics. */
|
|
@@ -487,6 +491,62 @@ export function apply(ctx, config) {
|
|
|
487
491
|
}
|
|
488
492
|
}
|
|
489
493
|
installSettingsRoute(ctx, settings, () => { chooseCarrier(); });
|
|
494
|
+
/**
|
|
495
|
+
* Expose readiness — "can the browser serve a call right now" — for the settings panel.
|
|
496
|
+
*
|
|
497
|
+
* The panel had switches for every behaviour and no answer to the one question a new install
|
|
498
|
+
* raises: the bundled runtime is downloaded on first need (issue #41 moved it out of `postinstall`
|
|
499
|
+
* for good reasons), so for the first minute of a fresh install the honest state is "preparing",
|
|
500
|
+
* and until this route existed that state was a log line no panel could read. Both readings are
|
|
501
|
+
* cheap and side-effect free: a path probe for the runtime, an install-tree inspection for the
|
|
502
|
+
* bridge — the same two the launch path and the bridge-status route already use.
|
|
503
|
+
*
|
|
504
|
+
* Skipped, like its siblings, on a surface with no web server: a CLI has no panel to show it to.
|
|
505
|
+
* @param ctx - the plugin context.
|
|
506
|
+
* @param settings - the settings document, for the channel being honoured.
|
|
507
|
+
*/
|
|
508
|
+
function installRuntimeStatusRoute(ctx, settings) {
|
|
509
|
+
const injectRuntime = ctx.inject;
|
|
510
|
+
if (typeof injectRuntime !== 'function')
|
|
511
|
+
return;
|
|
512
|
+
const deps = {
|
|
513
|
+
report: () => readRuntimeStatus({
|
|
514
|
+
channel: () => settings.get().browser.channel,
|
|
515
|
+
hasBinary: hasElectronRuntime,
|
|
516
|
+
provision: provisionSnapshot,
|
|
517
|
+
bridge: () => {
|
|
518
|
+
const root = findDesktopInstall();
|
|
519
|
+
return root === undefined ? undefined : readBridgeStatus(root);
|
|
520
|
+
},
|
|
521
|
+
// Installed is not running: the install tree survives a reboot, the endpoint does not. Both
|
|
522
|
+
// readings are cheap and side-effect free — a file read and a signal-0 pid check.
|
|
523
|
+
endpoint: () => readBridgeEndpoint(),
|
|
524
|
+
}),
|
|
525
|
+
// `force` is what makes the panel's retry button mean anything: without it the answer would be
|
|
526
|
+
// the settled failure the user is trying to replace. An attempt still running is joined.
|
|
527
|
+
provision: () => {
|
|
528
|
+
void provisionElectronInBackground(message => ctx.logger?.info?.(`dsh-builtin-browser: ${message}`), undefined, { force: true });
|
|
529
|
+
return provisionSnapshot();
|
|
530
|
+
},
|
|
531
|
+
};
|
|
532
|
+
try {
|
|
533
|
+
injectRuntime(['webServer'], (host) => {
|
|
534
|
+
host.effect(() => host.webServer.register({
|
|
535
|
+
kind: 'exact',
|
|
536
|
+
path: RUNTIME_STATUS_ROUTE,
|
|
537
|
+
handler: (request, response) => handleRuntimeStatus(request, response, deps),
|
|
538
|
+
}, 'dsh-builtin-browser: runtime status'));
|
|
539
|
+
host.effect(() => host.webServer.register({
|
|
540
|
+
kind: 'exact',
|
|
541
|
+
path: RUNTIME_PROVISION_ROUTE,
|
|
542
|
+
handler: (request, response) => handleRuntimeProvision(request, response, deps),
|
|
543
|
+
}, 'dsh-builtin-browser: runtime provision'));
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
catch {
|
|
547
|
+
// No web server: the panel is unreachable, and the readiness report has no reader.
|
|
548
|
+
}
|
|
549
|
+
}
|
|
490
550
|
/**
|
|
491
551
|
* Re-run the carrier choice when the settings FILE changes on disk.
|
|
492
552
|
*
|
|
@@ -536,6 +596,7 @@ export function apply(ctx, config) {
|
|
|
536
596
|
watchSettingsFile();
|
|
537
597
|
installPreviewRoute(ctx, settings);
|
|
538
598
|
installBridgeStatusRoute(ctx);
|
|
599
|
+
installRuntimeStatusRoute(ctx, settings);
|
|
539
600
|
}
|
|
540
601
|
/** Route the settings panel reads and writes. */
|
|
541
602
|
const SETTINGS_ROUTE = '/dsh-builtin-browser/settings';
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
* Write one JSON body with no caching and no sniffing.
|
|
17
|
+
* @param response - the response to write.
|
|
18
|
+
* @param status - the HTTP status.
|
|
19
|
+
* @param body - anything JSON-serialisable.
|
|
20
|
+
*/
|
|
21
|
+
export declare function replyJson(response: ServerResponse, status: number, body: unknown): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write one JSON body with no caching and no sniffing.
|
|
3
|
+
* @param response - the response to write.
|
|
4
|
+
* @param status - the HTTP status.
|
|
5
|
+
* @param body - anything JSON-serialisable.
|
|
6
|
+
*/
|
|
7
|
+
export function replyJson(response, status, body) {
|
|
8
|
+
const text = JSON.stringify(body);
|
|
9
|
+
response.writeHead(status, {
|
|
10
|
+
'content-type': 'application/json; charset=utf-8',
|
|
11
|
+
'content-length': Buffer.byteLength(text),
|
|
12
|
+
'cache-control': 'no-store, private',
|
|
13
|
+
'x-content-type-options': 'nosniff',
|
|
14
|
+
'cross-origin-resource-policy': 'same-origin',
|
|
15
|
+
});
|
|
16
|
+
response.end(text);
|
|
17
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { replyJson } from './json-reply.js';
|
|
1
2
|
import { sameOrigin } from './settings-route.js';
|
|
2
3
|
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js';
|
|
3
4
|
/** Where the frame endpoint lives. The viewer must use exactly this path, same origin. */
|
|
@@ -49,18 +50,6 @@ export async function handlePreviewSessions(request, response, settings, browser
|
|
|
49
50
|
const sessions = browser.watchableSessions?.() ?? [];
|
|
50
51
|
replyJson(response, 200, { enabled: true, sessions: [...sessions] });
|
|
51
52
|
}
|
|
52
|
-
/** Write one JSON body with no caching and no sniffing. */
|
|
53
|
-
function replyJson(response, status, body) {
|
|
54
|
-
const text = JSON.stringify(body);
|
|
55
|
-
response.writeHead(status, {
|
|
56
|
-
'content-type': 'application/json; charset=utf-8',
|
|
57
|
-
'content-length': Buffer.byteLength(text),
|
|
58
|
-
'cache-control': 'no-store, private',
|
|
59
|
-
'x-content-type-options': 'nosniff',
|
|
60
|
-
'cross-origin-resource-policy': 'same-origin',
|
|
61
|
-
});
|
|
62
|
-
response.end(text);
|
|
63
|
-
}
|
|
64
53
|
/**
|
|
65
54
|
* Serve one frame, or say precisely why not.
|
|
66
55
|
*
|
|
@@ -97,11 +97,12 @@ function resolveElectronPathImpl(inputs) {
|
|
|
97
97
|
throw new Error('dsh-builtin-browser: cannot locate a spawnable Electron binary. ' +
|
|
98
98
|
'The electron package ships with the plugin and its binary is downloaded on first use, not at install time ' +
|
|
99
99
|
'(issue #41: an install-time download made default profiles refuse the plugin). The plugin prepares it ' +
|
|
100
|
-
'automatically when the bundled carrier is selected
|
|
101
|
-
'
|
|
102
|
-
'
|
|
103
|
-
'
|
|
104
|
-
'
|
|
100
|
+
'automatically when the bundled carrier is selected, so right after an install this can mean the ' +
|
|
101
|
+
'download is still running: Settings → Browser → Readiness shows that state and can retry it. To do it ' +
|
|
102
|
+
'by hand instead, run `npx --no dsh-browser-install-electron` in the profile that installed the ' +
|
|
103
|
+
'plugin, then retry. Alternatively set ELECTRON_PATH to an electron executable. Note: the host\'s own ' +
|
|
104
|
+
'executable cannot be reused when it is a packaged Electron app (e.g. DSH Desktop.exe) — only a real ' +
|
|
105
|
+
'electron binary can be spawned.');
|
|
105
106
|
}
|
|
106
107
|
/** DSH install anchors: electron installed separately (profile / global prefix), newest wins. */
|
|
107
108
|
function anchoredElectronBinary() {
|
|
@@ -0,0 +1,57 @@
|
|
|
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 type { RuntimeStatusReport } from './runtime-status.js';
|
|
17
|
+
/** Where the panel reads readiness. */
|
|
18
|
+
export declare const RUNTIME_STATUS_ROUTE = "/dsh-builtin-browser/runtime-status";
|
|
19
|
+
/**
|
|
20
|
+
* Where the panel asks for the bundled runtime.
|
|
21
|
+
*
|
|
22
|
+
* Non-blocking by design: it starts (or joins) the one background attempt and answers immediately
|
|
23
|
+
* with the state that attempt is in, which is `preparing`. A request that waited for the download
|
|
24
|
+
* would time out in the browser long before a slow network finished, and would hold a connection
|
|
25
|
+
* open for something the panel can poll instead.
|
|
26
|
+
*/
|
|
27
|
+
export declare const RUNTIME_PROVISION_ROUTE = "/dsh-builtin-browser/runtime-provision";
|
|
28
|
+
/** What both handlers need. */
|
|
29
|
+
export interface RuntimeRouteDeps {
|
|
30
|
+
/** The current report; a cheap read, never a spawn. */
|
|
31
|
+
readonly report: () => RuntimeStatusReport;
|
|
32
|
+
/**
|
|
33
|
+
* Start (or join) a provision and return the state it is in now.
|
|
34
|
+
*
|
|
35
|
+
* Must not throw: a failure belongs in the report as a 200 with a `failed` state, because a user
|
|
36
|
+
* looking at a panel needs to read the reason, not an HTTP status.
|
|
37
|
+
*/
|
|
38
|
+
readonly provision: () => ProvisionSnapshot;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Read readiness: `{ ok, runtime }`.
|
|
42
|
+
* @param request - the incoming request.
|
|
43
|
+
* @param response - the response to write.
|
|
44
|
+
* @param deps - the readers.
|
|
45
|
+
*/
|
|
46
|
+
export declare function handleRuntimeStatus(request: IncomingMessage, response: ServerResponse, deps: RuntimeRouteDeps): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Start (or join) a provision, then answer with the state it is in.
|
|
49
|
+
*
|
|
50
|
+
* Always 200 once admitted, whatever happened: the state carries `failed` with its reason, and a
|
|
51
|
+
* panel that had to distinguish 4xx from 5xx to say "the download failed" would show a network
|
|
52
|
+
* error where the user needs the sentence explaining it.
|
|
53
|
+
* @param request - the incoming request.
|
|
54
|
+
* @param response - the response to write.
|
|
55
|
+
* @param deps - the readers.
|
|
56
|
+
*/
|
|
57
|
+
export declare function handleRuntimeProvision(request: IncomingMessage, response: ServerResponse, deps: RuntimeRouteDeps): Promise<void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { replyJson } from './json-reply.js';
|
|
2
|
+
import { sameOrigin } from './settings-route.js';
|
|
3
|
+
/** Where the panel reads readiness. */
|
|
4
|
+
export const RUNTIME_STATUS_ROUTE = '/dsh-builtin-browser/runtime-status';
|
|
5
|
+
/**
|
|
6
|
+
* Where the panel asks for the bundled runtime.
|
|
7
|
+
*
|
|
8
|
+
* Non-blocking by design: it starts (or joins) the one background attempt and answers immediately
|
|
9
|
+
* with the state that attempt is in, which is `preparing`. A request that waited for the download
|
|
10
|
+
* would time out in the browser long before a slow network finished, and would hold a connection
|
|
11
|
+
* open for something the panel can poll instead.
|
|
12
|
+
*/
|
|
13
|
+
export const RUNTIME_PROVISION_ROUTE = '/dsh-builtin-browser/runtime-provision';
|
|
14
|
+
/**
|
|
15
|
+
* Read readiness: `{ ok, runtime }`.
|
|
16
|
+
* @param request - the incoming request.
|
|
17
|
+
* @param response - the response to write.
|
|
18
|
+
* @param deps - the readers.
|
|
19
|
+
*/
|
|
20
|
+
export async function handleRuntimeStatus(request, response, deps) {
|
|
21
|
+
if (request.method !== 'GET') {
|
|
22
|
+
replyJson(response, 405, { error: 'method-not-allowed' });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!sameOrigin(request)) {
|
|
26
|
+
replyJson(response, 403, { error: 'not-same-origin' });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
replyJson(response, 200, { ok: true, runtime: deps.report() });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Start (or join) a provision, then answer with the state it is in.
|
|
33
|
+
*
|
|
34
|
+
* Always 200 once admitted, whatever happened: the state carries `failed` with its reason, and a
|
|
35
|
+
* panel that had to distinguish 4xx from 5xx to say "the download failed" would show a network
|
|
36
|
+
* error where the user needs the sentence explaining it.
|
|
37
|
+
* @param request - the incoming request.
|
|
38
|
+
* @param response - the response to write.
|
|
39
|
+
* @param deps - the readers.
|
|
40
|
+
*/
|
|
41
|
+
export async function handleRuntimeProvision(request, response, deps) {
|
|
42
|
+
if (request.method !== 'POST') {
|
|
43
|
+
replyJson(response, 405, { error: 'method-not-allowed' });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// `true`: starting a download is a write, and is admitted the way a settings write is.
|
|
47
|
+
if (!sameOrigin(request, true)) {
|
|
48
|
+
replyJson(response, 403, { error: 'not-same-origin' });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
let snapshot;
|
|
52
|
+
try {
|
|
53
|
+
snapshot = deps.provision();
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// `provision` is documented not to throw; if a caller breaks that contract the panel still gets
|
|
57
|
+
// an answer it can render, rather than a dropped connection it cannot tell from a hang.
|
|
58
|
+
replyJson(response, 200, {
|
|
59
|
+
ok: true,
|
|
60
|
+
runtime: deps.report(),
|
|
61
|
+
provisionError: String(error),
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
replyJson(response, 200, { ok: true, runtime: deps.report(), provision: snapshot });
|
|
66
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
/** The part of the carrier the channel names, in the terms the panel has to explain. */
|
|
27
|
+
export type CarrierKind = 'sidebar' | 'bundled' | 'system' | 'auto';
|
|
28
|
+
/**
|
|
29
|
+
* Whether the readiness question has an answer at all.
|
|
30
|
+
*
|
|
31
|
+
* `unknown` is not a defect: an explicit Chrome / Edge choice is only falsifiable by launching one,
|
|
32
|
+
* and launching a browser to answer a status question is exactly what this report must not do. The
|
|
33
|
+
* panel says so in words instead of guessing a colour.
|
|
34
|
+
*/
|
|
35
|
+
export type Readiness = 'ready' | 'blocked' | 'unknown';
|
|
36
|
+
/** The Electron runtime's state, as the panel needs it. */
|
|
37
|
+
export interface ElectronReadiness {
|
|
38
|
+
/** `present` — a launch would find a binary; `preparing` — a download is running now; and so on. */
|
|
39
|
+
readonly state: ProvisionSnapshot['state'];
|
|
40
|
+
/** One line, already user-facing: the provisioning module's own words. */
|
|
41
|
+
readonly detail: string;
|
|
42
|
+
/** When that state was reached, ISO. */
|
|
43
|
+
readonly at: string;
|
|
44
|
+
}
|
|
45
|
+
/** The desktop sidebar bridge, or the honest "there is no desktop here" shape. */
|
|
46
|
+
export interface BridgeReadiness {
|
|
47
|
+
/** Whether a DSH Desktop install was found at all. */
|
|
48
|
+
readonly present: boolean;
|
|
49
|
+
/** `absent` is reported separately from `missing`: nothing to repair versus something to repair. */
|
|
50
|
+
readonly state: BridgeStatus['state'] | 'absent';
|
|
51
|
+
/** The plugin's own words about the install tree. */
|
|
52
|
+
readonly detail: string;
|
|
53
|
+
/**
|
|
54
|
+
* Whether a shell is RUNNING and answering, as opposed to merely installed.
|
|
55
|
+
*
|
|
56
|
+
* Install-tree state survives a reboot and says nothing about a process; the published endpoint is
|
|
57
|
+
* the only thing that does. Reporting "installed" as "ready" is how a panel goes green while every
|
|
58
|
+
* call dials a port nobody owns — the desktop app being closed is the most common broken state
|
|
59
|
+
* there is.
|
|
60
|
+
*/
|
|
61
|
+
readonly running: boolean;
|
|
62
|
+
/** The plugin's own words about the running shell (or about why there is none). */
|
|
63
|
+
readonly runningDetail: string;
|
|
64
|
+
/** The command that installs or repairs it, for a human to run. Never run by the plugin. */
|
|
65
|
+
readonly command?: string;
|
|
66
|
+
}
|
|
67
|
+
/** The whole report. */
|
|
68
|
+
export interface RuntimeStatusReport {
|
|
69
|
+
/** What the settings document says, verbatim: the panel shows the switch it is honouring. */
|
|
70
|
+
readonly channel: BrowserChannel;
|
|
71
|
+
/** What that channel resolves to, in panel terms. */
|
|
72
|
+
readonly carrier: CarrierKind;
|
|
73
|
+
/** Whether the browser this channel names can serve a call right now. */
|
|
74
|
+
readonly readiness: Readiness;
|
|
75
|
+
readonly electron: ElectronReadiness;
|
|
76
|
+
readonly bridge: BridgeReadiness;
|
|
77
|
+
/** Whether "Prepare the bundled browser" is worth offering (it is not while one is running). */
|
|
78
|
+
readonly canProvision: boolean;
|
|
79
|
+
}
|
|
80
|
+
/** Everything the report reads, so a test can answer without any of the real machinery. */
|
|
81
|
+
export interface RuntimeStatusDeps {
|
|
82
|
+
/** The stored `browser.channel`. */
|
|
83
|
+
readonly channel: () => BrowserChannel;
|
|
84
|
+
/** Whether a launch would find an Electron binary — the launch path's own probe. */
|
|
85
|
+
readonly hasBinary: () => boolean;
|
|
86
|
+
/** The provisioning state machine's current state. */
|
|
87
|
+
readonly provision: () => ProvisionSnapshot;
|
|
88
|
+
/** The desktop install's bridge state, or undefined when there is no desktop install. */
|
|
89
|
+
readonly bridge: () => BridgeStatus | undefined;
|
|
90
|
+
/** The endpoint the shell publishes while it runs; undefined when nothing published one. */
|
|
91
|
+
readonly endpoint: () => BridgeEndpointState | undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Assemble the report.
|
|
95
|
+
*
|
|
96
|
+
* The `readiness` field is per carrier on purpose: "the browser is ready" is not a property of this
|
|
97
|
+
* plugin, it is a property of the carrier the user chose. A bundled carrier with a finished download
|
|
98
|
+
* is ready; the sidebar carrier with no bridge is not, however healthy the bundled runtime is.
|
|
99
|
+
* @param deps - the readers; every one of them is a cheap, side-effect-free read.
|
|
100
|
+
* @returns the report the panel renders.
|
|
101
|
+
*/
|
|
102
|
+
export declare function readRuntimeStatus(deps: RuntimeStatusDeps): RuntimeStatusReport;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** Which carrier a channel names. */
|
|
2
|
+
function carrierOf(channel) {
|
|
3
|
+
if (channel === 'sidebar')
|
|
4
|
+
return 'sidebar';
|
|
5
|
+
if (channel === 'bundled')
|
|
6
|
+
return 'bundled';
|
|
7
|
+
if (channel === 'auto')
|
|
8
|
+
return 'auto';
|
|
9
|
+
return 'system';
|
|
10
|
+
}
|
|
11
|
+
/** Whether a binary being present settles the Electron question. */
|
|
12
|
+
function electronReady(state) {
|
|
13
|
+
return state === 'present' || state === 'ready';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Assemble the report.
|
|
17
|
+
*
|
|
18
|
+
* The `readiness` field is per carrier on purpose: "the browser is ready" is not a property of this
|
|
19
|
+
* plugin, it is a property of the carrier the user chose. A bundled carrier with a finished download
|
|
20
|
+
* is ready; the sidebar carrier with no bridge is not, however healthy the bundled runtime is.
|
|
21
|
+
* @param deps - the readers; every one of them is a cheap, side-effect-free read.
|
|
22
|
+
* @returns the report the panel renders.
|
|
23
|
+
*/
|
|
24
|
+
export function readRuntimeStatus(deps) {
|
|
25
|
+
const channel = deps.channel();
|
|
26
|
+
const carrier = carrierOf(channel);
|
|
27
|
+
// The binary check comes first and wins: a runtime that is on disk is ready whatever the
|
|
28
|
+
// provisioning state machine last said, which is also how a runtime installed by hand (or by an
|
|
29
|
+
// earlier process) is reported correctly without a restart. The DETAIL has to follow the same
|
|
30
|
+
// rule, or the panel shows "present" next to "the runtime has not been asked for yet" — measured
|
|
31
|
+
// on a live install, and the kind of contradiction a reader cannot resolve.
|
|
32
|
+
const snapshot = deps.provision();
|
|
33
|
+
const runnable = deps.hasBinary();
|
|
34
|
+
const state = runnable ? 'present' : snapshot.state;
|
|
35
|
+
const electron = {
|
|
36
|
+
state,
|
|
37
|
+
detail: runnable ? 'an Electron runtime is already available' : snapshot.detail,
|
|
38
|
+
at: runnable ? new Date().toISOString() : snapshot.at,
|
|
39
|
+
};
|
|
40
|
+
const bridgeStatus = deps.bridge();
|
|
41
|
+
const endpoint = deps.endpoint();
|
|
42
|
+
const bridge = bridgeStatus === undefined
|
|
43
|
+
? {
|
|
44
|
+
present: false,
|
|
45
|
+
state: 'absent',
|
|
46
|
+
detail: 'no DSH Desktop install was found on this machine',
|
|
47
|
+
running: endpoint?.alive === true,
|
|
48
|
+
runningDetail: endpoint?.detail ?? 'no desktop shell has published a bridge endpoint',
|
|
49
|
+
}
|
|
50
|
+
: {
|
|
51
|
+
present: bridgeStatus.present,
|
|
52
|
+
state: bridgeStatus.state,
|
|
53
|
+
detail: bridgeStatus.detail,
|
|
54
|
+
running: endpoint?.alive === true,
|
|
55
|
+
runningDetail: endpoint?.detail ?? 'no desktop shell has published a bridge endpoint',
|
|
56
|
+
...bridgeStatus.command === undefined ? {} : { command: bridgeStatus.command },
|
|
57
|
+
};
|
|
58
|
+
// The sidebar needs BOTH halves: our patch inside the shell it will be driven through, and that
|
|
59
|
+
// shell actually running. Either one alone is the false green this report exists to prevent.
|
|
60
|
+
const sidebarReady = bridge.state === 'installed' && bridge.running;
|
|
61
|
+
let readiness;
|
|
62
|
+
if (carrier === 'sidebar') {
|
|
63
|
+
readiness = sidebarReady ? 'ready' : 'blocked';
|
|
64
|
+
}
|
|
65
|
+
else if (carrier === 'bundled') {
|
|
66
|
+
readiness = electronReady(state) ? 'ready' : 'blocked';
|
|
67
|
+
}
|
|
68
|
+
else if (carrier === 'auto') {
|
|
69
|
+
// `auto` is satisfied by either carrier, so a working sidebar covers a missing runtime. An
|
|
70
|
+
// explicit Chrome / Edge install would satisfy it too, but proving that means launching one.
|
|
71
|
+
readiness = electronReady(state) || sidebarReady ? 'ready' : 'unknown';
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
readiness = 'unknown';
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
channel,
|
|
78
|
+
carrier,
|
|
79
|
+
readiness,
|
|
80
|
+
electron,
|
|
81
|
+
bridge,
|
|
82
|
+
// Offering the button while a download runs would be a second download, which is the one thing
|
|
83
|
+
// the shared attempt exists to prevent.
|
|
84
|
+
canProvision: !electronReady(state) && state !== 'preparing',
|
|
85
|
+
};
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-builtin-browser",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "Shared real browser plugin for DeepSeek Harness: install-and-use
|
|
3
|
+
"version": "0.5.6",
|
|
4
|
+
"description": "Shared real browser plugin for DeepSeek Harness: install-and-use — a visible native browser the human can take over, driven by the agent over CDP. DOM-level interaction (React/Vue safe), per-task session isolation, cookie persistence (dsh_browser_auth), CAPTCHA detection, batch form fill (dsh_browser_fill), authenticated download, history/replay, action restriction",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|