dsh-builtin-browser 0.5.7 → 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 +66 -0
- package/README.en.md +48 -22
- package/README.md +48 -22
- package/client.js +469 -81
- package/desktop-bridge/install-and-start.cmd +4 -5
- package/desktop-bridge/install.mjs +251 -159
- package/desktop-bridge/maintenance-progress.ps1 +166 -0
- package/desktop-bridge/plugin-browser-bridge.js +151 -18
- package/desktop-bridge/request-desktop-quit.ps1 +170 -0
- package/docs/user-guide.md +28 -4
- 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 +1 -19
- package/lib/browser-electron/bridge-install.d.ts +41 -4
- package/lib/browser-electron/bridge-install.js +131 -18
- 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 +76 -45
- package/lib/browser-electron/desktop-bridge-host.d.ts +5 -2
- package/lib/browser-electron/desktop-bridge-host.js +54 -33
- package/lib/browser-electron/desktop-identity.js +21 -9
- package/lib/browser-electron/electron-provision.js +5 -2
- package/lib/browser-electron/entry.js +104 -74
- 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 +26 -37
- 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 +7 -7
- package/lib/browser-electron/provider.js +13 -14
- package/lib/browser-electron/remote-host.js +18 -16
- package/lib/browser-electron/runtime-status.d.ts +27 -13
- package/lib/browser-electron/runtime-status.js +24 -4
- package/lib/browser-electron/settings-store.d.ts +11 -0
- package/lib/browser-electron/settings-store.js +8 -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 +1 -9
- package/lib/browser-electron/system-browser.js +19 -22
- 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 +5 -4
- package/package.json +2 -2
- package/scripts/run-tests.mjs +32 -0
- package/scripts/verify-preview-route.mjs +19 -1
- 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 +1 -18
- package/src/browser-electron/bridge-install.ts +151 -20
- package/src/browser-electron/bridge-maintenance.ts +161 -0
- package/src/browser-electron/bridge-status.ts +78 -48
- package/src/browser-electron/desktop-bridge-host.ts +48 -32
- package/src/browser-electron/desktop-identity.ts +18 -9
- package/src/browser-electron/electron-provision.ts +5 -2
- package/src/browser-electron/entry.ts +91 -69
- 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 +23 -36
- 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 +18 -18
- package/src/browser-electron/remote-host.ts +19 -19
- package/src/browser-electron/runtime-status.ts +52 -17
- package/src/browser-electron/settings-store.ts +20 -3
- package/src/browser-electron/sidebar-only-host.ts +199 -0
- package/src/browser-electron/system-browser.ts +19 -22
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/windows-system.ts +23 -0
- package/src/tool-browser/index.ts +5 -4
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
@echo off
|
|
2
|
-
REM Install (or repair) the
|
|
2
|
+
REM Install (or repair) the bridge after DSH Desktop has exited, then start Desktop.
|
|
3
3
|
REM
|
|
4
|
-
REM
|
|
5
|
-
REM
|
|
6
|
-
REM runs the installer with --restart, which closes the shell itself, installs, and starts it again.
|
|
4
|
+
REM Exit DSH Desktop normally before running this file from an external terminal.
|
|
5
|
+
REM A running target is refused; this launcher does not close or terminate it.
|
|
7
6
|
REM
|
|
8
7
|
REM No arguments needed: the installer probes for the install directory. Pass one to override it.
|
|
9
8
|
REM install-and-start.cmd "C:\path\to\DSH Desktop"
|
|
10
9
|
REM install-and-start.cmd "C:\path\to\DSH Desktop" --yes
|
|
11
10
|
REM
|
|
12
|
-
REM --yes
|
|
11
|
+
REM Legacy --yes is accepted for command-line compatibility; it does not permit closing Desktop.
|
|
13
12
|
|
|
14
13
|
setlocal
|
|
15
14
|
set "HERE=%~dp0"
|
|
@@ -61,16 +61,76 @@
|
|
|
61
61
|
* Default path: probed under %LOCALAPPDATA%\Programs (see findDefaultInstall)
|
|
62
62
|
* Pass --revert to restore the pre-bridge state instead.
|
|
63
63
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* and nothing started from outside survives the exit far enough to install afterwards (issue #36).
|
|
67
|
-
* --restart asks for confirmation on a console; add --yes for an unattended run.
|
|
64
|
+
* Exit DSH Desktop normally before maintenance. --restart is an external-terminal opt-in to
|
|
65
|
+
* start the already-closed app after maintenance; it never closes a running application.
|
|
68
66
|
*/
|
|
69
|
-
import
|
|
67
|
+
import * as nodeFs from 'node:fs'
|
|
70
68
|
import { execFileSync, spawn } from 'node:child_process'
|
|
71
|
-
import {
|
|
69
|
+
import { createRequire } from 'node:module'
|
|
72
70
|
|
|
73
|
-
|
|
71
|
+
/**
|
|
72
|
+
* The filesystem used for every installer operation.
|
|
73
|
+
*
|
|
74
|
+
* Electron patches `fs` so that a `*.asar` path answers from the archive's virtual tree; opening the
|
|
75
|
+
* archive to read its header then fails with ENOENT (measured when the panel's button ran this file
|
|
76
|
+
* from the desktop shell). Even a patched existsSync(app.asar) creates Electron's archive cache
|
|
77
|
+
* and keeps a Windows handle open until process exit. A raw rename cannot release that cache.
|
|
78
|
+
* Use the same untouched module for discovery, validation, extraction and rename, so no earlier
|
|
79
|
+
* probe can lock the file this process later moves. Plain Node uses its ordinary filesystem.
|
|
80
|
+
*/
|
|
81
|
+
const archiveFs = (() => {
|
|
82
|
+
try {
|
|
83
|
+
return createRequire(import.meta.url)('original-fs')
|
|
84
|
+
} catch {
|
|
85
|
+
return nodeFs
|
|
86
|
+
}
|
|
87
|
+
})()
|
|
88
|
+
const { closeSync, copyFileSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, readSync,
|
|
89
|
+
readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } = archiveFs
|
|
90
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
91
|
+
|
|
92
|
+
function isWithin(root, path) {
|
|
93
|
+
const part = relative(root, path)
|
|
94
|
+
return part !== '' && part !== '..' && !part.startsWith('..\\') && !part.startsWith('../') && !isAbsolute(part)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Tests may inspect or modify only an explicitly marked, canonical sandbox. */
|
|
98
|
+
function testBoundary() {
|
|
99
|
+
if (process.env.DSH_BROWSER_TEST !== '1' && process.env.NODE_TEST_CONTEXT === undefined) return undefined
|
|
100
|
+
const configured = process.env.DSH_BRIDGE_TEST_ROOT
|
|
101
|
+
if (!configured || !isAbsolute(configured) || lstatSync(configured).isSymbolicLink()) {
|
|
102
|
+
throw new Error('installer test isolation requires an absolute, non-linked DSH_BRIDGE_TEST_ROOT')
|
|
103
|
+
}
|
|
104
|
+
const root = realpathSync(configured)
|
|
105
|
+
const marker = join(root, '.dsh-bridge-test-root')
|
|
106
|
+
if (!lstatSync(marker).isFile() || lstatSync(marker).isSymbolicLink()
|
|
107
|
+
|| readFileSync(marker, 'utf8') !== 'dsh-browser-installer-test-root-v1\n') {
|
|
108
|
+
throw new Error('installer test isolation marker is missing or invalid')
|
|
109
|
+
}
|
|
110
|
+
return root
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function assertTestTarget(root, path) {
|
|
114
|
+
if (root === undefined) return
|
|
115
|
+
if (!isWithin(root, resolve(path)) || !isWithin(root, realpathSync(path))) {
|
|
116
|
+
throw new Error('installer test target must stay strictly inside DSH_BRIDGE_TEST_ROOT')
|
|
117
|
+
}
|
|
118
|
+
const inspect = directory => {
|
|
119
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
120
|
+
if (entry.isSymbolicLink()) throw new Error('installer test targets cannot contain linked files or directories')
|
|
121
|
+
if (entry.isDirectory()) inspect(join(directory, entry.name))
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
inspect(path)
|
|
125
|
+
for (const key of ['DSH_BRIDGE_ACTION_RESULT_FILE', 'DSH_BRIDGE_ACTION_LOG_FILE']) {
|
|
126
|
+
const output = process.env[key]
|
|
127
|
+
if (output !== undefined && (!isWithin(root, resolve(output))
|
|
128
|
+
|| !isWithin(root, realpathSync(dirname(output)))
|
|
129
|
+
|| (existsSync(output) && !isWithin(root, realpathSync(output))))) {
|
|
130
|
+
throw new Error('installer test result and log files must stay inside DSH_BRIDGE_TEST_ROOT')
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
74
134
|
/**
|
|
75
135
|
* The desktop install to patch when none is given on the command line.
|
|
76
136
|
*
|
|
@@ -80,25 +140,51 @@ const DEFAULT_INSTALL = findDefaultInstall()
|
|
|
80
140
|
* `resources/app.asar` — is stable across both, so that is what is probed.
|
|
81
141
|
* @returns the install directory, or the historical default when nothing matches.
|
|
82
142
|
*/
|
|
83
|
-
function findDefaultInstall() {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const candidate = join(programs, named)
|
|
89
|
-
if (looksInstalled(candidate)) return candidate
|
|
143
|
+
function findDefaultInstall(testRoot) {
|
|
144
|
+
const permitted = candidate => {
|
|
145
|
+
if (testRoot === undefined) return true
|
|
146
|
+
if (!isWithin(testRoot, resolve(candidate))) return false
|
|
147
|
+
try { return isWithin(testRoot, realpathSync(candidate)) } catch { return false }
|
|
90
148
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
149
|
+
const looksInstalled = candidate => permitted(candidate) && (existsSync(join(candidate, 'resources', 'app', 'lib', 'main.js'))
|
|
150
|
+
|| existsSync(join(candidate, 'resources', 'app.asar')))
|
|
151
|
+
const probe = (base) => {
|
|
152
|
+
if (!permitted(base)) return undefined
|
|
153
|
+
for (const named of ['DSH Desktop', 'DeepSeek Harness']) {
|
|
154
|
+
const candidate = join(base, named)
|
|
95
155
|
if (looksInstalled(candidate)) return candidate
|
|
96
156
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
157
|
+
try {
|
|
158
|
+
for (const entry of readdirSync(base, { withFileTypes: true })) {
|
|
159
|
+
if (!entry.isDirectory()) continue
|
|
160
|
+
const candidate = join(base, entry.name)
|
|
161
|
+
if (looksInstalled(candidate)) return candidate
|
|
162
|
+
}
|
|
163
|
+
} catch {
|
|
164
|
+
// A root that does not exist is a state, not an error.
|
|
165
|
+
}
|
|
166
|
+
return undefined
|
|
167
|
+
}
|
|
168
|
+
// Same roots the panel probes: an explicit DSH_DESKTOP_PATH, then the places people actually install
|
|
169
|
+
// to. An install on another drive used to be invisible to both the panel and this default.
|
|
170
|
+
const explicit = process.env.DSH_DESKTOP_PATH
|
|
171
|
+
if (explicit !== undefined && explicit !== '') {
|
|
172
|
+
if (looksInstalled(explicit)) return explicit
|
|
173
|
+
const below = probe(explicit)
|
|
174
|
+
if (below !== undefined) return below
|
|
175
|
+
}
|
|
176
|
+
const localAppData = process.env.LOCALAPPDATA ?? ''
|
|
177
|
+
const roots = [
|
|
178
|
+
...(localAppData === '' ? [] : [join(localAppData, 'Programs')]),
|
|
179
|
+
process.env['ProgramFiles'], process.env['ProgramFiles(x86)'], process.env.APPDATA, process.env.ProgramData,
|
|
180
|
+
].filter(base => typeof base === 'string' && base !== '')
|
|
181
|
+
for (const base of roots) {
|
|
182
|
+
const found = probe(base)
|
|
183
|
+
if (found !== undefined) return found
|
|
100
184
|
}
|
|
101
|
-
return
|
|
185
|
+
return testRoot === undefined
|
|
186
|
+
? join(localAppData === '' ? '.' : join(localAppData, 'Programs'), 'DeepSeek Harness')
|
|
187
|
+
: join(testRoot, 'no-test-desktop-found')
|
|
102
188
|
}
|
|
103
189
|
|
|
104
190
|
/**
|
|
@@ -148,7 +234,9 @@ function selectShellProcesses(rows, root, selfPid) {
|
|
|
148
234
|
const nodeMode = /(?:^|\s)--expose-internals(?:\s|$)|\.[cm]?js(?:"|\s|$)|(?:^|\s)(?:--eval|--print|--input-type)(?:=|\s|$)|(?:^|\s)-(?:e|p)(?:\s|$)/i.test(row.CommandLine)
|
|
149
235
|
if (nodeMode) continue
|
|
150
236
|
if (ancestors.has(row.ProcessId)) {
|
|
151
|
-
|
|
237
|
+
const error = new Error('the installer is a child of DSH Desktop; run this command from a separate terminal with Node.js')
|
|
238
|
+
error.code = 'DSH_CHILD_OF_SHELL'
|
|
239
|
+
throw error
|
|
152
240
|
}
|
|
153
241
|
selected.push(row.ProcessId)
|
|
154
242
|
}
|
|
@@ -164,7 +252,8 @@ function selectShellProcesses(rows, root, selfPid) {
|
|
|
164
252
|
function shellProcesses(root) {
|
|
165
253
|
const text = runCapture('powershell.exe', [
|
|
166
254
|
'-NoProfile', '-NonInteractive', '-Command',
|
|
167
|
-
'
|
|
255
|
+
'[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); '
|
|
256
|
+
+ 'Get-CimInstance Win32_Process -ErrorAction Stop | '
|
|
168
257
|
+ 'Select-Object ProcessId,ParentProcessId,Name,ExecutablePath,CommandLine | ConvertTo-Json -Compress',
|
|
169
258
|
])
|
|
170
259
|
if (text === undefined || text.trim() === '') throw new Error('could not enumerate Desktop processes')
|
|
@@ -185,46 +274,12 @@ function shellProcesses(root) {
|
|
|
185
274
|
*/
|
|
186
275
|
function runCapture(command, argv) {
|
|
187
276
|
try {
|
|
188
|
-
return execFileSync(command, argv, { encoding: 'utf8', stdio: 'pipe', timeout: 30_000 })
|
|
277
|
+
return execFileSync(command, argv, { encoding: 'utf8', stdio: 'pipe', windowsHide: true, timeout: 30_000 })
|
|
189
278
|
} catch {
|
|
190
279
|
return undefined
|
|
191
280
|
}
|
|
192
281
|
}
|
|
193
282
|
|
|
194
|
-
/**
|
|
195
|
-
* Close the desktop shell and wait until nothing holds the archive.
|
|
196
|
-
*
|
|
197
|
-
* Asks first and forces only if the polite request is ignored: a forced kill can lose work in
|
|
198
|
-
* progress, and the user was told the shell would be closed, not that it would be killed. The wait
|
|
199
|
-
* is bounded — a process that refuses to die must not hang the installer forever.
|
|
200
|
-
* @param root - the install directory whose processes to close.
|
|
201
|
-
* @param graceMs - how long to wait after the polite request before forcing.
|
|
202
|
-
* @returns whether nothing is left running.
|
|
203
|
-
*/
|
|
204
|
-
async function stopShell(root, graceMs) {
|
|
205
|
-
const before = shellProcesses(root)
|
|
206
|
-
if (before.length === 0) return true
|
|
207
|
-
console.log(` closing DSH Desktop (${before.length} process(es))…`)
|
|
208
|
-
// No /F: this is a normal window close, so the shell can flush and prompt.
|
|
209
|
-
runCapture('taskkill.exe', before.flatMap(pid => ['/PID', String(pid)]))
|
|
210
|
-
const deadline = Date.now() + graceMs
|
|
211
|
-
while (Date.now() < deadline) {
|
|
212
|
-
if (shellProcesses(root).length === 0) return true
|
|
213
|
-
await new Promise(resolve => setTimeout(resolve, 250))
|
|
214
|
-
}
|
|
215
|
-
const left = shellProcesses(root)
|
|
216
|
-
if (left.length > 0) {
|
|
217
|
-
console.log(` ${left.length} process(es) still running after ${String(graceMs)} ms; forcing`)
|
|
218
|
-
runCapture('taskkill.exe', [...left.flatMap(pid => ['/PID', String(pid)]), '/F'])
|
|
219
|
-
const forcedBy = Date.now() + 15_000
|
|
220
|
-
while (Date.now() < forcedBy) {
|
|
221
|
-
if (shellProcesses(root).length === 0) return true
|
|
222
|
-
await new Promise(resolve => setTimeout(resolve, 250))
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return shellProcesses(root).length === 0
|
|
226
|
-
}
|
|
227
|
-
|
|
228
283
|
/**
|
|
229
284
|
* Start the desktop shell again, detached from this process.
|
|
230
285
|
*
|
|
@@ -233,20 +288,26 @@ async function stopShell(root, graceMs) {
|
|
|
233
288
|
* @param root - the install directory.
|
|
234
289
|
* @returns whether the executable was spawned.
|
|
235
290
|
*/
|
|
291
|
+
let lastStartedPid
|
|
236
292
|
function startShell(root) {
|
|
237
293
|
const exe = join(root, 'DeepSeek Harness.exe')
|
|
238
294
|
if (!existsSync(exe)) {
|
|
239
295
|
console.error(`could not start DSH Desktop: ${exe} does not exist; start it yourself.`)
|
|
240
296
|
return false
|
|
241
297
|
}
|
|
298
|
+
if (process.env.DSH_BROWSER_TEST === '1' || process.env.NODE_TEST_CONTEXT !== undefined) {
|
|
299
|
+
console.error('test mode forbids launching DSH Desktop')
|
|
300
|
+
return false
|
|
301
|
+
}
|
|
242
302
|
try {
|
|
243
303
|
const env = { ...process.env }
|
|
244
304
|
for (const key of Object.keys(env)) {
|
|
245
|
-
if (key.toUpperCase() === 'ELECTRON_RUN_AS_NODE') delete env[key]
|
|
305
|
+
if (key.toUpperCase() === 'ELECTRON_RUN_AS_NODE' || key.toUpperCase().startsWith('DSH_BRIDGE_')) delete env[key]
|
|
246
306
|
}
|
|
247
307
|
const child = spawn(exe, [], { detached: true, stdio: 'ignore', cwd: root, env })
|
|
248
308
|
child.on('error', error => { console.error('could not start DSH Desktop: ' + String(error)) })
|
|
249
309
|
if (child.pid === undefined) return false
|
|
310
|
+
lastStartedPid = child.pid
|
|
250
311
|
child.unref()
|
|
251
312
|
console.log(` started ${exe}`)
|
|
252
313
|
return true
|
|
@@ -257,29 +318,26 @@ function startShell(root) {
|
|
|
257
318
|
}
|
|
258
319
|
|
|
259
320
|
const args = process.argv.slice(2)
|
|
321
|
+
let installRoot
|
|
322
|
+
try {
|
|
323
|
+
const boundary = testBoundary()
|
|
324
|
+
installRoot = args.find(arg => !arg.startsWith('--')) ?? findDefaultInstall(boundary)
|
|
325
|
+
assertTestTarget(boundary, installRoot)
|
|
326
|
+
} catch (error) {
|
|
327
|
+
console.error('bridge installer test isolation refused the operation: ' + String(error))
|
|
328
|
+
process.exit(1)
|
|
329
|
+
}
|
|
260
330
|
// Identity only, no side effects: the panel and a human both need a way to ask "would this work"
|
|
261
331
|
// without closing the shell first.
|
|
262
332
|
if (args.includes('--preflight')) {
|
|
263
|
-
const target =
|
|
333
|
+
const target = installRoot
|
|
264
334
|
const verdict = verifyDesktopIdentity(target)
|
|
265
335
|
console.log(JSON.stringify({ target, ...verdict }))
|
|
266
336
|
process.exit(verdict.ok ? 0 : 1)
|
|
267
337
|
}
|
|
268
338
|
const revert = args.includes('--revert')
|
|
269
|
-
/**
|
|
270
|
-
* Close DSH, install, then start it again — all in THIS process.
|
|
271
|
-
*
|
|
272
|
-
* Reported as issue #36: moving `app.asar` aside fails with EBUSY while the shell is running, and
|
|
273
|
-
* every "wait for the user to close it" arrangement loses the waiter. Two arrangements were measured
|
|
274
|
-
* on Windows and both died at the moment the shell exited: a hidden PowerShell started through WMI
|
|
275
|
-
* vanished with the process tree, and a scheduled task reported `LastTaskResult = 0xC000013A`
|
|
276
|
-
* (STATUS_CONTROL_C_EXIT). The lesson is that nothing may straddle the exit — so this process closes
|
|
277
|
-
* the shell itself, does the work, and starts it again.
|
|
278
|
-
*/
|
|
339
|
+
/** Start after maintenance only when the target was already closed; never close a running shell. */
|
|
279
340
|
const restart = args.includes('--restart')
|
|
280
|
-
/** Skip the interactive confirmation; required for an unattended run. */
|
|
281
|
-
const assumeYes = args.includes('--yes') || args.includes('-y')
|
|
282
|
-
const installRoot = args.find(arg => !arg.startsWith('--')) ?? DEFAULT_INSTALL
|
|
283
341
|
|
|
284
342
|
const resourcesDir = join(installRoot, 'resources')
|
|
285
343
|
const appDir = join(resourcesDir, 'app')
|
|
@@ -306,6 +364,50 @@ const hostCliBackup = `${hostCliPath}.before-bridge`
|
|
|
306
364
|
const CLI_SHIM_MARKER = 'dsh-builtin-browser: resolve the app directory'
|
|
307
365
|
const HOST_CLI_MARKER = 'dsh-builtin-browser: recognize an unpacked `app` directory'
|
|
308
366
|
|
|
367
|
+
let targetWasClosed = false
|
|
368
|
+
const resultFile = process.env.DSH_BRIDGE_ACTION_RESULT_FILE
|
|
369
|
+
const installErrors = []
|
|
370
|
+
if (resultFile !== undefined) {
|
|
371
|
+
const originalError = console.error
|
|
372
|
+
console.error = (...values) => {
|
|
373
|
+
installErrors.push(values.map(String).join(' '))
|
|
374
|
+
if (installErrors.length > 12) installErrors.shift()
|
|
375
|
+
originalError(...values)
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Report maintenance only after its file operations complete. Starting the app is an external
|
|
380
|
+
// command-line opt-in and cannot happen after a running-target or ancestry refusal.
|
|
381
|
+
process.once('exit', code => {
|
|
382
|
+
let restarted = false
|
|
383
|
+
if (restart && targetWasClosed && code === 0) {
|
|
384
|
+
restarted = startShell(installRoot)
|
|
385
|
+
if (restarted) console.log('DSH Desktop was restarted.')
|
|
386
|
+
}
|
|
387
|
+
const finalCode = code === 0 && (!restart || restarted) ? 0 : code || 1
|
|
388
|
+
process.exitCode = finalCode
|
|
389
|
+
if (resultFile !== undefined) {
|
|
390
|
+
const result = {
|
|
391
|
+
ok: finalCode === 0, code: finalCode, action: revert ? 'uninstall' : 'install', restarted,
|
|
392
|
+
...lastStartedPid === undefined ? {} : { pid: lastStartedPid },
|
|
393
|
+
...installErrors.length === 0 ? {} : { error: installErrors.join('\n').slice(-2400) },
|
|
394
|
+
completedAt: new Date().toISOString(),
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const temporary = `${resultFile}.${process.pid}.tmp`
|
|
398
|
+
writeFileSync(temporary, JSON.stringify(result))
|
|
399
|
+
renameSync(temporary, resultFile)
|
|
400
|
+
} catch (error) {
|
|
401
|
+
originalResultError(error)
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
function originalResultError(error) {
|
|
407
|
+
console.error('could not record the bridge installer result: ' + String(error))
|
|
408
|
+
process.exitCode = 1
|
|
409
|
+
}
|
|
410
|
+
|
|
309
411
|
/** The exact block appended to main.js. Keep it recognisable: idempotence and
|
|
310
412
|
* reverting both key off this marker. */
|
|
311
413
|
const MARKER = '// dsh-builtin-browser: hand the plugin a way to drive the sidebar'
|
|
@@ -354,15 +456,22 @@ app.whenReady().then(async () => {
|
|
|
354
456
|
* @returns the parsed header tree plus the offset where file content starts.
|
|
355
457
|
*/
|
|
356
458
|
function readAsarHeader(file) {
|
|
357
|
-
const fd = openSync(file, 'r')
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
459
|
+
const fd = archiveFs.openSync(file, 'r')
|
|
460
|
+
try {
|
|
461
|
+
const sizeBuf = Buffer.alloc(8)
|
|
462
|
+
if (archiveFs.readSync(fd, sizeBuf, 0, 8, 0) !== 8) throw new Error('the archive header is truncated')
|
|
463
|
+
const headerSize = sizeBuf.readUInt32LE(4)
|
|
464
|
+
if (headerSize < 8 || headerSize > 64 * 1024 * 1024) throw new Error('the archive header size is invalid')
|
|
465
|
+
const headerBuf = Buffer.alloc(headerSize)
|
|
466
|
+
if (archiveFs.readSync(fd, headerBuf, 0, headerSize, 8) !== headerSize) throw new Error('the archive header is truncated')
|
|
467
|
+
const jsonLength = headerBuf.readUInt32LE(4)
|
|
468
|
+
if (jsonLength <= 0 || jsonLength > headerSize - 8) throw new Error('the archive JSON length is invalid')
|
|
469
|
+
const header = JSON.parse(headerBuf.toString('utf8', 8, 8 + jsonLength))
|
|
470
|
+
return { fd, header, contentBase: 8 + headerSize }
|
|
471
|
+
} catch (error) {
|
|
472
|
+
archiveFs.closeSync(fd)
|
|
473
|
+
throw error
|
|
474
|
+
}
|
|
366
475
|
}
|
|
367
476
|
|
|
368
477
|
/**
|
|
@@ -395,7 +504,7 @@ function extractAsar(asar, outDir) {
|
|
|
395
504
|
unpackedFiles += 1
|
|
396
505
|
} else {
|
|
397
506
|
const buffer = Buffer.alloc(entry.size)
|
|
398
|
-
readSync(fd, buffer, 0, entry.size, contentBase + Number(entry.offset))
|
|
507
|
+
archiveFs.readSync(fd, buffer, 0, entry.size, contentBase + Number(entry.offset))
|
|
399
508
|
writeFileSync(target, buffer)
|
|
400
509
|
}
|
|
401
510
|
const actual = statSync(target).size
|
|
@@ -404,9 +513,11 @@ function extractAsar(asar, outDir) {
|
|
|
404
513
|
bytes += actual
|
|
405
514
|
}
|
|
406
515
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
516
|
+
try {
|
|
517
|
+
mkdirSync(outDir, { recursive: true })
|
|
518
|
+
walk(header, '')
|
|
519
|
+
return { files, bytes, unpackedFiles }
|
|
520
|
+
} finally { archiveFs.closeSync(fd) }
|
|
410
521
|
}
|
|
411
522
|
|
|
412
523
|
/**
|
|
@@ -545,7 +656,7 @@ function readAsarEntry(archive, entryPath) {
|
|
|
545
656
|
const offset = Number(node.offset)
|
|
546
657
|
if (!Number.isFinite(size) || !Number.isFinite(offset) || size < 0) return undefined
|
|
547
658
|
const buffer = Buffer.alloc(size)
|
|
548
|
-
const read = readSync(fd, buffer, 0, size, parsed.contentBase + offset)
|
|
659
|
+
const read = archiveFs.readSync(fd, buffer, 0, size, parsed.contentBase + offset)
|
|
549
660
|
return read === size ? buffer : undefined
|
|
550
661
|
} catch {
|
|
551
662
|
return undefined
|
|
@@ -572,7 +683,8 @@ function validateArchive(archive) {
|
|
|
572
683
|
}
|
|
573
684
|
const { fd, header, contentBase } = parsed
|
|
574
685
|
try {
|
|
575
|
-
|
|
686
|
+
// Statistics go through the same unpatched module as the raw archive reads.
|
|
687
|
+
const fileSize = archiveFs.statSync(archive).size
|
|
576
688
|
const problems = []
|
|
577
689
|
const walk = (node, prefix) => {
|
|
578
690
|
for (const [name, entry] of Object.entries(node.files ?? {})) {
|
|
@@ -648,33 +760,7 @@ function verifyDesktopIdentity(root) {
|
|
|
648
760
|
try { bytes = readFileSync(join(resources, 'app.asar.unpacked', 'package.json')) } catch { bytes = undefined }
|
|
649
761
|
}
|
|
650
762
|
if (bytes === undefined) {
|
|
651
|
-
|
|
652
|
-
// `.asar` path as a virtual directory, so a raw read of it returns the wrong thing and finds
|
|
653
|
-
// nothing — measured when the panel's button ran this file under the desktop shell. The
|
|
654
|
-
// product's own executable beside the archive is proof enough that this is DSH Desktop.
|
|
655
|
-
const exeNames = ['DeepSeek Harness.exe', 'DSH Desktop.exe']
|
|
656
|
-
const exeFound = exeNames.find(name => existsSync(join(root, name)))
|
|
657
|
-
if (exeFound !== undefined) {
|
|
658
|
-
return { ok: true, layout: 'packed', packageName: String(rule.packageNames?.[0] ?? ''), version: 'unknown' }
|
|
659
|
-
}
|
|
660
|
-
// Last resort, for a plain node run: the archive header could not be parsed. The rule is a name,
|
|
661
|
-
// and a name that appears in the archive is evidence enough to proceed — the alternative is
|
|
662
|
-
// refusing an install that is obviously there.
|
|
663
|
-
const names = Array.isArray(rule.packageNames) ? rule.packageNames.map(String) : []
|
|
664
|
-
let probe = ''
|
|
665
|
-
try {
|
|
666
|
-
const fd = openSync(archive, 'r')
|
|
667
|
-
const size = 8 * 1024 * 1024
|
|
668
|
-
const buf = Buffer.alloc(size)
|
|
669
|
-
const read = readSync(fd, buf, 0, size, 0)
|
|
670
|
-
closeSync(fd)
|
|
671
|
-
probe = buf.subarray(0, read).toString('latin1')
|
|
672
|
-
} catch { probe = '' }
|
|
673
|
-
const found = names.find(name => name !== '' && probe.includes(name))
|
|
674
|
-
if (found === undefined) {
|
|
675
|
-
return { ok: false, reason: `${archive} could not be read and does not name ${names.join(' / ')}` }
|
|
676
|
-
}
|
|
677
|
-
return { ok: true, layout: 'packed', packageName: found, version: 'unknown' }
|
|
763
|
+
return { ok: false, reason: `${archive}: the package manifest could not be read; executable names and archive text are not proof of DSH identity` }
|
|
678
764
|
}
|
|
679
765
|
manifestText = bytes.toString('utf8')
|
|
680
766
|
} else {
|
|
@@ -722,11 +808,18 @@ if (!identity.ok) {
|
|
|
722
808
|
process.exit(1)
|
|
723
809
|
}
|
|
724
810
|
|
|
725
|
-
//
|
|
726
|
-
//
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
811
|
+
// Refuse before any write whenever the target is running. A window close can mean "hide to tray",
|
|
812
|
+
// so maintenance cannot infer consent to terminate the host or its tools.
|
|
813
|
+
try {
|
|
814
|
+
if (shellProcesses(installRoot).length !== 0) {
|
|
815
|
+
console.error('DSH Desktop is running. Exit it normally, then run bridge maintenance from an external terminal.')
|
|
816
|
+
process.exit(1)
|
|
817
|
+
}
|
|
818
|
+
targetWasClosed = true
|
|
819
|
+
} catch (error) {
|
|
820
|
+
console.error('bridge maintenance refused: ' + String(error))
|
|
821
|
+
process.exit(1)
|
|
822
|
+
}
|
|
730
823
|
if (!revert && existsSync(asarPath)) {
|
|
731
824
|
const checked = validateArchive(asarPath)
|
|
732
825
|
if (!checked.ok) {
|
|
@@ -734,42 +827,6 @@ if (!revert && existsSync(asarPath)) {
|
|
|
734
827
|
process.exit(1)
|
|
735
828
|
}
|
|
736
829
|
}
|
|
737
|
-
if (restart) {
|
|
738
|
-
if (!assumeYes) {
|
|
739
|
-
console.log('')
|
|
740
|
-
console.log(`--restart will close DSH Desktop, ${revert ? 'remove' : 'install'} the bridge, and start it again.`)
|
|
741
|
-
console.log('Any unsaved work in a conversation is lost. Re-run with --yes to skip this notice.')
|
|
742
|
-
console.log('')
|
|
743
|
-
try {
|
|
744
|
-
const answer = readFileSync(0, 'utf8').trim().toLowerCase()
|
|
745
|
-
if (answer !== '' && answer !== 'y' && answer !== 'yes') {
|
|
746
|
-
console.error('cancelled; nothing was changed.')
|
|
747
|
-
process.exit(1)
|
|
748
|
-
}
|
|
749
|
-
} catch {
|
|
750
|
-
// No stdin (a service, a detached shell): refuse rather than close a window nobody agreed to.
|
|
751
|
-
console.error('--restart needs confirmation; pass --yes when there is no console to ask.')
|
|
752
|
-
process.exit(1)
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
try {
|
|
756
|
-
shellStopped = await stopShell(installRoot, 20_000)
|
|
757
|
-
} catch (error) {
|
|
758
|
-
console.error('could not safely stop DSH Desktop: ' + String(error))
|
|
759
|
-
process.exit(1)
|
|
760
|
-
}
|
|
761
|
-
if (!shellStopped) {
|
|
762
|
-
console.error('DSH Desktop did not exit, so the archive cannot be replaced.')
|
|
763
|
-
console.error('Close it yourself and run this again without --restart.')
|
|
764
|
-
process.exit(1)
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
process.once('exit', () => {
|
|
768
|
-
if (restart && shellStopped && startShell(installRoot)) {
|
|
769
|
-
console.log('DSH Desktop was restarted.')
|
|
770
|
-
}
|
|
771
|
-
})
|
|
772
|
-
|
|
773
830
|
// The archive wins whenever it exists, so ITS presence — not the absence of the
|
|
774
831
|
// directory — decides the layout. After a desktop update the archive is back
|
|
775
832
|
// (a new build) while an older extraction is still lying around, and keying off
|
|
@@ -785,7 +842,11 @@ if (!existsSync(mainPath) && !existsSync(asarPath)) {
|
|
|
785
842
|
}
|
|
786
843
|
|
|
787
844
|
if (revert) {
|
|
845
|
+
// Tracked separately: restoring a CLI launcher or the runtime resolution is NOT an
|
|
846
|
+
// uninstall. `reverted` only means some component was restored; whether the bridge is
|
|
847
|
+
// actually gone is decided from the entry Electron loads, at the end of this branch.
|
|
788
848
|
let reverted = false
|
|
849
|
+
let mainEntrySkipped = false
|
|
789
850
|
if (existsSync(asarMoved)) {
|
|
790
851
|
if (existsSync(asarPath)) {
|
|
791
852
|
console.error('refusing to replace an app archive that already exists: a desktop upgrade may have installed a newer build')
|
|
@@ -840,6 +901,7 @@ if (revert) {
|
|
|
840
901
|
} else if (!identical) {
|
|
841
902
|
// It carries our marker but does not reduce to the backup: something was patched around
|
|
842
903
|
// our snippet. The backup is left untouched, because guessing here is what destroyed it.
|
|
904
|
+
mainEntrySkipped = true
|
|
843
905
|
console.error(`WARNING: ${mainPath} carries the bridge import, but removing it does not`)
|
|
844
906
|
console.error(` reproduce ${backupPath} — another tool has patched this file too`)
|
|
845
907
|
console.error(' (dsh-purge patches it). Reverting would discard those changes, so nothing')
|
|
@@ -863,6 +925,36 @@ if (revert) {
|
|
|
863
925
|
console.error(`nothing to revert in ${installRoot}`)
|
|
864
926
|
process.exit(1)
|
|
865
927
|
}
|
|
928
|
+
// Verify the OUTCOME, not the actions. `reverted` only says a component was restored, and
|
|
929
|
+
// the CLI launcher used to set it while `main.js` still imported the bridge — so `--revert`
|
|
930
|
+
// exited 0 and reported success even though a fresh check still said "installed".
|
|
931
|
+
// The bridge is gone only when the entry Electron will actually load no longer carries our
|
|
932
|
+
// marker: in the unpacked layout that is resources/app/lib/main.js; when app.asar is back
|
|
933
|
+
// the archive wins and the unpacked tree is inert.
|
|
934
|
+
const archiveBack = existsSync(asarPath)
|
|
935
|
+
let bridgeStillLoaded = false
|
|
936
|
+
if (!archiveBack && existsSync(mainPath)) {
|
|
937
|
+
try {
|
|
938
|
+
bridgeStillLoaded = readFileSync(mainPath, 'utf8').includes(MARKER)
|
|
939
|
+
} catch (error) {
|
|
940
|
+
// An entry we cannot read cannot be certified clean; never claim success on it.
|
|
941
|
+
console.error(`WARNING: cannot read ${mainPath} to verify the uninstall: ${String(error)}`)
|
|
942
|
+
bridgeStillLoaded = true
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (bridgeStillLoaded) {
|
|
946
|
+
console.error('bridge uninstall is INCOMPLETE: the loaded main entry still imports the bridge.')
|
|
947
|
+
console.error(` main entry: ${mainPath} (still patched)`)
|
|
948
|
+
console.error(` backup: ${backupPath}`)
|
|
949
|
+
if (mainEntrySkipped) {
|
|
950
|
+
console.error(' Another tool patched main.js around our snippet. That patch was preserved on')
|
|
951
|
+
console.error(' purpose, so our snippet could not be removed without discarding their work.')
|
|
952
|
+
}
|
|
953
|
+
console.error(' The CLI launcher / runtime resolution may already be restored — that is not')
|
|
954
|
+
console.error(' an uninstall. Restore the main entry by hand, or run --revert again after the')
|
|
955
|
+
console.error(' other patch is reconciled, then re-check the bridge status.')
|
|
956
|
+
process.exit(1)
|
|
957
|
+
}
|
|
866
958
|
// Only where an archive actually exists. In the unpacked layout `resources/app` IS what Electron
|
|
867
959
|
// loads, so telling the user it is inert and safe to delete would invite them to remove the
|
|
868
960
|
// application.
|
|
@@ -896,7 +988,7 @@ if (packedLayout) {
|
|
|
896
988
|
// installation working.
|
|
897
989
|
rmSync(appDir, { recursive: true, force: true })
|
|
898
990
|
}
|
|
899
|
-
console.log(`extracting into ${appDir} —
|
|
991
|
+
console.log(`extracting into ${appDir} — large desktop packages can take several minutes...`)
|
|
900
992
|
const result = extractAsar(asarPath, appDir)
|
|
901
993
|
const megabytes = (result.bytes / 1024 / 1024).toFixed(1)
|
|
902
994
|
const seconds = ((Date.now() - started) / 1000).toFixed(1)
|
|
@@ -911,7 +1003,7 @@ if (packedLayout) {
|
|
|
911
1003
|
console.log(` Electron now loads ${appDir}; the archive is untouched, so the asar integrity fuse cannot trip`)
|
|
912
1004
|
} catch (error) {
|
|
913
1005
|
console.error(`could not move app.asar aside: ${String(error)}`)
|
|
914
|
-
console.error('
|
|
1006
|
+
console.error('the original archive is still active and unchanged; the extracted files were not activated. Check the process holding the archive before retrying.')
|
|
915
1007
|
process.exit(1)
|
|
916
1008
|
}
|
|
917
1009
|
}
|