@xi-ix/dsh-multi-sync 0.0.0-stage → 0.1.1
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/README.md +62 -2
- package/client.js +576 -0
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +61 -0
- package/icon.svg +13 -0
- package/index.js +589 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +49 -4
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Multi-machine plugin sync
|
|
2
|
+
|
|
3
|
+
Move the plugin set from one machine to another as one pinned list.
|
|
4
|
+
|
|
5
|
+
## The problem
|
|
6
|
+
|
|
7
|
+
Two machines running DeepSeek Harness drift apart: one has a plugin at `1.2.2`,
|
|
8
|
+
the other at `1.3.0`; one has a locally linked package the other has never heard of.
|
|
9
|
+
Copying `node_modules` across platforms does not work, and copying the whole
|
|
10
|
+
profile drags sessions, credentials and 100 MB of install output along.
|
|
11
|
+
|
|
12
|
+
What actually has to travel is small — which packages are installed, at which
|
|
13
|
+
exact version.
|
|
14
|
+
|
|
15
|
+
## How it works
|
|
16
|
+
|
|
17
|
+
The page has two roles.
|
|
18
|
+
|
|
19
|
+
**Source** scans the running profile. For each direct dependency it keeps the
|
|
20
|
+
package name, the version the installed copy really has (not the range in
|
|
21
|
+
`package.json`), whether the package is a bundle that registers loader rows, and
|
|
22
|
+
the tarball `integrity` from the profile lockfile when one is recorded. Entries a
|
|
23
|
+
registry cannot serve — `file:`, `link:`, `workspace:`, `git+`, `http:` — are moved
|
|
24
|
+
to a separate skipped list with the reason, so the difference between the two
|
|
25
|
+
machines is visible instead of silently dropped.
|
|
26
|
+
|
|
27
|
+
The result is exported as `dsh-plugins.json`, either downloaded or copied from the
|
|
28
|
+
text box, and moved to the other machine by any means: a USB stick, a chat message,
|
|
29
|
+
a synced folder.
|
|
30
|
+
|
|
31
|
+
**Follower** reads that document back — dropped in, chosen from disk, or pasted —
|
|
32
|
+
and compares every entry against what this machine already has. The dialog lists
|
|
33
|
+
one row per plugin with the action that would be taken: new, upgrade, already
|
|
34
|
+
installed, or unavailable. Only the checked entries are installed, in a single
|
|
35
|
+
`dsh plugin --profile <name> add name@version …` call, so the plugin manager does
|
|
36
|
+
one resolution, one lockfile write, and one bundle registration pass.
|
|
37
|
+
|
|
38
|
+
## Limits worth knowing
|
|
39
|
+
|
|
40
|
+
- The harness is never restarted by this plugin, and it cannot be: restarting belongs to
|
|
41
|
+
the desktop shell, which accepts no restart request from a host, and every process
|
|
42
|
+
this plugin starts is reaped together with the app. Restart from the tray icon on
|
|
43
|
+
Windows, or from the menu on macOS.
|
|
44
|
+
- Enable/disable state and per-plugin configuration are not carried. Those belong
|
|
45
|
+
to `cordis.patch.yml` and the plugin market's own state file, which the market
|
|
46
|
+
rewrites; a second writer there is reverted by its self-heal logic.
|
|
47
|
+
- Installations are additive. A plugin installed on this machine but absent from
|
|
48
|
+
the list is never removed.
|
|
49
|
+
- Transitive dependencies are not pinned. A listed plugin's own dependencies
|
|
50
|
+
resolve at install time on each machine.
|
|
51
|
+
|
|
52
|
+
## First run on the new machine
|
|
53
|
+
|
|
54
|
+
This plugin cannot install itself before it exists on that machine, so install it
|
|
55
|
+
once by hand:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
dsh plugin --profile desktop add @xi-ix/dsh-multi-sync@0.1.0
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Then restart, open Settings → Multi-sync, and choose **Follower**.
|
package/icon.svg
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Multi-machine plugin sync">
|
|
2
|
+
<rect x="2" y="2" width="60" height="60" rx="14" fill="#334155"/>
|
|
3
|
+
<g fill="none" stroke="#93c5fd" stroke-width="3.2" stroke-linecap="round" stroke-linejoin="round">
|
|
4
|
+
<path d="M18 26h20a8 8 0 0 1 8 8"/>
|
|
5
|
+
<path d="M46 38H26a8 8 0 0 1-8-8"/>
|
|
6
|
+
</g>
|
|
7
|
+
<g fill="#93c5fd">
|
|
8
|
+
<path d="M21 20.5 28.5 26 21 31.5z"/>
|
|
9
|
+
<path d="M43 32.5 35.5 38 43 43.5z"/>
|
|
10
|
+
</g>
|
|
11
|
+
<rect x="24" y="45" width="16" height="7" rx="2.5" fill="#e2e8f0"/>
|
|
12
|
+
<rect x="29" y="51.5" width="6" height="5" rx="1.5" fill="#e2e8f0"/>
|
|
13
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of @xi-ix/dsh-multi-sync.
|
|
3
|
+
*
|
|
4
|
+
* It owns three things and nothing else:
|
|
5
|
+
* - scan: read the running profile's direct dependencies, drop everything that
|
|
6
|
+
* cannot be fetched from a registry, and report the version each one actually
|
|
7
|
+
* has on disk;
|
|
8
|
+
* - export/parse: turn that scan into one portable JSON document and read one
|
|
9
|
+
* back, so the other machine never has to reach the same registry twice;
|
|
10
|
+
* - apply: hand a set of exact `name@version` specs to `dsh plugin add`, once,
|
|
11
|
+
* and report what happened.
|
|
12
|
+
*
|
|
13
|
+
* It never writes package.json, cordis.patch.yml or the market's state: bundle
|
|
14
|
+
* registration is the plugin manager's job, and enable/disable belongs to the
|
|
15
|
+
* market. That boundary is deliberate — two writers on those files fight.
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
18
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
19
|
+
import { execFile } from 'node:child_process'
|
|
20
|
+
import { homedir, platform } from 'node:os'
|
|
21
|
+
import { basename, join } from 'node:path'
|
|
22
|
+
|
|
23
|
+
export const name = '@xi-ix/dsh-multi-sync'
|
|
24
|
+
export const inject = ['webServer']
|
|
25
|
+
|
|
26
|
+
const PREFIX = '/dsh-multi-sync'
|
|
27
|
+
const STATE_FILE = '.dsh-multi-sync.json'
|
|
28
|
+
const BODY_LIMIT = 1024 * 1024
|
|
29
|
+
const SEMVER = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/
|
|
30
|
+
const PACKAGE_NAME = /^(?:@[a-z0-9][a-z0-9-._~]*\/)?[a-z0-9][a-z0-9-._~]*$/i
|
|
31
|
+
|
|
32
|
+
/** Non-registry dependency specs: local links, git, tarball URLs, workspace protocols. */
|
|
33
|
+
const LOCAL_SPEC = /^(?:file:|link:|workspace:|portal:|\.{0,2}\/|[a-zA-Z]:[\\/])/
|
|
34
|
+
const REMOTE_SPEC = /^(?:git\+|git:|github:|gitlab:|bitbucket:|https?:|npm:|patch:)/
|
|
35
|
+
|
|
36
|
+
export function apply(ctx) {
|
|
37
|
+
const service = createService(ctx)
|
|
38
|
+
ctx.effect(
|
|
39
|
+
() => ctx.webServer.register({ kind: 'prefix', path: PREFIX, handler: service.handle }),
|
|
40
|
+
'dsh-multi-sync: routes',
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------- helpers
|
|
45
|
+
|
|
46
|
+
function reply(res, status, value) {
|
|
47
|
+
const body = JSON.stringify(value)
|
|
48
|
+
res.writeHead(status, {
|
|
49
|
+
'content-type': 'application/json; charset=utf-8',
|
|
50
|
+
'content-length': Buffer.byteLength(body),
|
|
51
|
+
'cache-control': 'no-store',
|
|
52
|
+
})
|
|
53
|
+
res.end(body)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readBody(req) {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const chunks = []
|
|
59
|
+
let size = 0
|
|
60
|
+
req.on('data', (chunk) => {
|
|
61
|
+
size += chunk.length
|
|
62
|
+
if (size > BODY_LIMIT) {
|
|
63
|
+
reject(new Error('request body is larger than 1 MiB'))
|
|
64
|
+
req.destroy()
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
chunks.push(chunk)
|
|
68
|
+
})
|
|
69
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
|
70
|
+
req.on('error', reject)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Reject a cross-origin browser request; same-origin and non-browser callers pass. */
|
|
75
|
+
function trusted(request) {
|
|
76
|
+
const origin = request.headers.origin
|
|
77
|
+
if (!origin) return true
|
|
78
|
+
try {
|
|
79
|
+
return new URL(origin).host === request.headers.host
|
|
80
|
+
} catch {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function loopback(address) {
|
|
86
|
+
if (typeof address !== 'string') return false
|
|
87
|
+
return address === '127.0.0.1' || address === '::1' || /^::ffff:127\./.test(address)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function escapeRegExp(text) {
|
|
91
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function readJson(file) {
|
|
95
|
+
return JSON.parse(await readFile(file, 'utf8'))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function tryReadJson(file) {
|
|
99
|
+
try {
|
|
100
|
+
return await readJson(file)
|
|
101
|
+
} catch {
|
|
102
|
+
return undefined
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function dshHome() {
|
|
107
|
+
return process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Locate the active profile directory.
|
|
112
|
+
*
|
|
113
|
+
* `DSH_PROFILE_DIR` is set for shell calls of a profile-launched harness, which
|
|
114
|
+
* need not reach the host process itself; the rest of the candidates cover the
|
|
115
|
+
* layouts a Desktop or CLI launch leaves behind.
|
|
116
|
+
*/
|
|
117
|
+
function resolveProfile() {
|
|
118
|
+
const explicit = process.env.DSH_PROFILE_DIR
|
|
119
|
+
if (explicit && existsSync(join(explicit, 'package.json'))) {
|
|
120
|
+
return { name: process.env.DSH_PROFILE || basename(explicit), dir: explicit }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const profiles = join(dshHome(), 'profiles')
|
|
124
|
+
const wanted = process.env.DSH_PROFILE || 'desktop'
|
|
125
|
+
const preferred = join(profiles, wanted)
|
|
126
|
+
if (existsSync(join(preferred, 'package.json'))) return { name: wanted, dir: preferred }
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
for (const entry of readdirSync(profiles)) {
|
|
130
|
+
const candidate = join(profiles, entry)
|
|
131
|
+
if (existsSync(join(candidate, 'package.json'))) return { name: entry, dir: candidate }
|
|
132
|
+
}
|
|
133
|
+
} catch {
|
|
134
|
+
// fall through to the error below
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
throw new Error(`cannot find a DSH profile under ${profiles}; set DSH_PROFILE or DSH_PROFILE_DIR`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function runningDshVersion() {
|
|
141
|
+
// The host process need not inherit DSH_INSTALL_DIR, so fall back to the
|
|
142
|
+
// packaged layout relative to the running executable's resources directory.
|
|
143
|
+
const candidates = []
|
|
144
|
+
if (process.env.DSH_INSTALL_DIR) candidates.push(join(process.env.DSH_INSTALL_DIR, 'package.json'))
|
|
145
|
+
if (process.resourcesPath) {
|
|
146
|
+
candidates.push(join(process.resourcesPath, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'))
|
|
147
|
+
candidates.push(join(process.resourcesPath, 'app', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'))
|
|
148
|
+
}
|
|
149
|
+
for (const candidate of candidates) {
|
|
150
|
+
const manifest = await tryReadJson(candidate)
|
|
151
|
+
if (typeof manifest?.version === 'string' && manifest.name === '@deepseek-ai/dsh') return manifest.version
|
|
152
|
+
}
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The version actually installed on disk, which is what both machines must match. */
|
|
157
|
+
async function installedFacts(dir, packageName) {
|
|
158
|
+
const manifest = await tryReadJson(join(dir, 'node_modules', packageName, 'package.json'))
|
|
159
|
+
if (manifest === undefined) return undefined
|
|
160
|
+
return {
|
|
161
|
+
version: typeof manifest.version === 'string' ? manifest.version : null,
|
|
162
|
+
bundle: manifest.dsh?.bundle !== undefined,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Best-effort tarball hash from the profile lockfile; absent when the layout is unfamiliar. */
|
|
167
|
+
async function lockIntegrity(dir, packageName, version) {
|
|
168
|
+
let text
|
|
169
|
+
try {
|
|
170
|
+
text = await readFile(join(dir, 'pnpm-lock.yaml'), 'utf8')
|
|
171
|
+
} catch {
|
|
172
|
+
return undefined
|
|
173
|
+
}
|
|
174
|
+
const key = new RegExp(
|
|
175
|
+
`(?:^|\\n)[ \\t]{2,}['"]?${escapeRegExp(packageName)}@${escapeRegExp(version)}['"]?:[\\s\\S]{0,600}?integrity:[ \\t]*['"]?(sha512-[A-Za-z0-9+/=]+)`,
|
|
176
|
+
)
|
|
177
|
+
return key.exec(text)?.[1]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function specKind(spec) {
|
|
181
|
+
if (LOCAL_SPEC.test(spec)) return 'local'
|
|
182
|
+
if (REMOTE_SPEC.test(spec)) return 'remote'
|
|
183
|
+
return 'registry'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function cleanRange(range) {
|
|
187
|
+
return range.replace(/^[\^~>=<\s]+/, '').trim()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Compare two versions well enough to name an action: release beats prerelease,
|
|
192
|
+
* numeric identifiers compare numerically, the rest lexically. Build metadata is
|
|
193
|
+
* ignored, as SemVer says it should be.
|
|
194
|
+
*/
|
|
195
|
+
function compareVersions(a, b) {
|
|
196
|
+
const split = (value) => {
|
|
197
|
+
const [core, rest = ''] = String(value).split('+')[0].split('-', 2)
|
|
198
|
+
const numbers = core.split('.').map((part) => Number.parseInt(part, 10) || 0)
|
|
199
|
+
while (numbers.length < 3) numbers.push(0)
|
|
200
|
+
return { numbers, prerelease: rest === '' ? [] : rest.split('.') }
|
|
201
|
+
}
|
|
202
|
+
const left = split(a)
|
|
203
|
+
const right = split(b)
|
|
204
|
+
for (let index = 0; index < 3; index += 1) {
|
|
205
|
+
if (left.numbers[index] !== right.numbers[index]) return left.numbers[index] > right.numbers[index] ? 1 : -1
|
|
206
|
+
}
|
|
207
|
+
if (left.prerelease.length === 0 && right.prerelease.length === 0) return 0
|
|
208
|
+
if (left.prerelease.length === 0) return 1
|
|
209
|
+
if (right.prerelease.length === 0) return -1
|
|
210
|
+
const length = Math.max(left.prerelease.length, right.prerelease.length)
|
|
211
|
+
for (let index = 0; index < length; index += 1) {
|
|
212
|
+
const one = left.prerelease[index]
|
|
213
|
+
const two = right.prerelease[index]
|
|
214
|
+
if (one === undefined) return -1
|
|
215
|
+
if (two === undefined) return 1
|
|
216
|
+
if (one === two) continue
|
|
217
|
+
const oneNumber = /^\d+$/.test(one)
|
|
218
|
+
const twoNumber = /^\d+$/.test(two)
|
|
219
|
+
if (oneNumber && twoNumber) return Number(one) > Number(two) ? 1 : -1
|
|
220
|
+
if (oneNumber !== twoNumber) return oneNumber ? -1 : 1
|
|
221
|
+
return one > two ? 1 : -1
|
|
222
|
+
}
|
|
223
|
+
return 0
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ---------------------------------------------------------------- operations
|
|
227
|
+
|
|
228
|
+
async function scan() {
|
|
229
|
+
const profile = resolveProfile()
|
|
230
|
+
const manifest = await readJson(join(profile.dir, 'package.json'))
|
|
231
|
+
const dependencies = manifest.dependencies ?? {}
|
|
232
|
+
const bundles = new Set(manifest.dsh?.profile?.bundles ?? [])
|
|
233
|
+
const plugins = []
|
|
234
|
+
const skipped = []
|
|
235
|
+
|
|
236
|
+
for (const [packageName, spec] of Object.entries(dependencies)) {
|
|
237
|
+
const kind = specKind(String(spec))
|
|
238
|
+
if (kind !== 'registry') {
|
|
239
|
+
skipped.push({ name: packageName, spec: String(spec), reason: kind })
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
if (!PACKAGE_NAME.test(packageName)) {
|
|
243
|
+
skipped.push({ name: packageName, spec: String(spec), reason: 'invalid-name' })
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const facts = await installedFacts(profile.dir, packageName)
|
|
248
|
+
const version = facts?.version ?? (SEMVER.test(cleanRange(String(spec))) ? cleanRange(String(spec)) : null)
|
|
249
|
+
const entry = {
|
|
250
|
+
name: packageName,
|
|
251
|
+
version,
|
|
252
|
+
spec: String(spec),
|
|
253
|
+
bundle: facts?.bundle ?? bundles.has(packageName),
|
|
254
|
+
present: facts !== undefined,
|
|
255
|
+
}
|
|
256
|
+
if (version) {
|
|
257
|
+
const integrity = await lockIntegrity(profile.dir, packageName, version)
|
|
258
|
+
if (integrity) entry.integrity = integrity
|
|
259
|
+
}
|
|
260
|
+
plugins.push(entry)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
plugins.sort((a, b) => a.name.localeCompare(b.name))
|
|
264
|
+
return {
|
|
265
|
+
format: 1,
|
|
266
|
+
generatedAt: new Date().toISOString(),
|
|
267
|
+
dsh: await runningDshVersion(),
|
|
268
|
+
profile: profile.name,
|
|
269
|
+
platform: platform(),
|
|
270
|
+
plugins,
|
|
271
|
+
skipped,
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function readState(dir) {
|
|
276
|
+
const state = await tryReadJson(join(dir, STATE_FILE))
|
|
277
|
+
return { role: state?.role === 'follower' ? 'follower' : 'source' }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function writeState(dir, state) {
|
|
281
|
+
const value = { role: state?.role === 'follower' ? 'follower' : 'source' }
|
|
282
|
+
await writeFile(join(dir, STATE_FILE), JSON.stringify(value, undefined, 2) + '\n', { mode: 0o600 })
|
|
283
|
+
return value
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Accept the export format, a bare array of entries, or one `name[@version]` per line. */
|
|
287
|
+
function parseList(text) {
|
|
288
|
+
const trimmed = String(text ?? '').trim()
|
|
289
|
+
if (trimmed === '') throw new Error('empty list')
|
|
290
|
+
|
|
291
|
+
let document
|
|
292
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
293
|
+
let parsed
|
|
294
|
+
try {
|
|
295
|
+
parsed = JSON.parse(trimmed)
|
|
296
|
+
} catch (error) {
|
|
297
|
+
throw new Error(`invalid JSON: ${error.message}`)
|
|
298
|
+
}
|
|
299
|
+
if (Array.isArray(parsed)) document = { plugins: parsed }
|
|
300
|
+
else if (Array.isArray(parsed.plugins)) document = parsed
|
|
301
|
+
else throw new Error('JSON has no "plugins" array')
|
|
302
|
+
} else {
|
|
303
|
+
const plugins = []
|
|
304
|
+
const skipped = []
|
|
305
|
+
for (const line of trimmed.split(/\r?\n/)) {
|
|
306
|
+
const item = line.replace(/#.*$/, '').trim()
|
|
307
|
+
if (item === '') continue
|
|
308
|
+
const at = item.lastIndexOf('@')
|
|
309
|
+
if (at > 0) plugins.push({ name: item.slice(0, at), version: item.slice(at + 1) })
|
|
310
|
+
else plugins.push({ name: item })
|
|
311
|
+
}
|
|
312
|
+
document = { plugins, skipped }
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const plugins = []
|
|
316
|
+
for (const raw of document.plugins ?? []) {
|
|
317
|
+
if (typeof raw?.name !== 'string' || !PACKAGE_NAME.test(raw.name)) continue
|
|
318
|
+
const version = typeof raw?.version === 'string' && SEMVER.test(raw.version) ? raw.version : null
|
|
319
|
+
if (version === null) continue
|
|
320
|
+
const entry = { name: raw.name, version }
|
|
321
|
+
if (typeof raw.integrity === 'string' && raw.integrity.startsWith('sha512-')) entry.integrity = raw.integrity
|
|
322
|
+
if (typeof raw.bundle === 'boolean') entry.bundle = raw.bundle
|
|
323
|
+
plugins.push(entry)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// One row per package: a hand-edited list may repeat a name, and installing the
|
|
327
|
+
// same package twice in one command is never what the user meant.
|
|
328
|
+
const unique = new Map()
|
|
329
|
+
for (const entry of plugins) unique.set(entry.name, entry)
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
header: {
|
|
333
|
+
format: document.format ?? null,
|
|
334
|
+
generatedAt: document.generatedAt ?? null,
|
|
335
|
+
dsh: document.dsh ?? null,
|
|
336
|
+
profile: document.profile ?? null,
|
|
337
|
+
platform: document.platform ?? null,
|
|
338
|
+
},
|
|
339
|
+
plugins: [...unique.values()],
|
|
340
|
+
skipped: Array.isArray(document.skipped) ? document.skipped : [],
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function runOnce(command, argv, options) {
|
|
345
|
+
return new Promise((resolve) => {
|
|
346
|
+
execFile(
|
|
347
|
+
command,
|
|
348
|
+
argv,
|
|
349
|
+
{ timeout: 15 * 60 * 1000, maxBuffer: 16 * 1024 * 1024, windowsHide: true, ...options },
|
|
350
|
+
(error, stdout, stderr) => {
|
|
351
|
+
resolve({
|
|
352
|
+
ok: error === null || error === undefined,
|
|
353
|
+
code: error?.code ?? 0,
|
|
354
|
+
stdout: String(stdout ?? ''),
|
|
355
|
+
stderr: String(stderr ?? ''),
|
|
356
|
+
message: error?.message,
|
|
357
|
+
})
|
|
358
|
+
},
|
|
359
|
+
)
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Run the DSH CLI, preferring the installation this process belongs to.
|
|
365
|
+
*
|
|
366
|
+
* A profile-launched host does not reliably inherit DSH_INSTALL_DIR, and the
|
|
367
|
+
* packaged CLI lives inside app.asar, which only the Electron runtime can read.
|
|
368
|
+
* The candidates therefore walk from the running executable outwards: the packaged
|
|
369
|
+
* asar CLI through this executable with ELECTRON_RUN_AS_NODE, the runtime shim, then
|
|
370
|
+
* whatever `dsh` the PATH offers. Each candidate is tried until one is not simply
|
|
371
|
+
* missing, so a machine without the CLI reports every location it looked at.
|
|
372
|
+
*/
|
|
373
|
+
function cliAttempts(args) {
|
|
374
|
+
const attempts = []
|
|
375
|
+
const nodeExecutable = process.env.DSH_NODE_EXECUTABLE
|
|
376
|
+
const installDir = process.env.DSH_INSTALL_DIR
|
|
377
|
+
const asNode = { env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' } }
|
|
378
|
+
|
|
379
|
+
if (nodeExecutable && installDir) {
|
|
380
|
+
attempts.push({
|
|
381
|
+
command: nodeExecutable,
|
|
382
|
+
argv: [join(installDir, 'lib', 'bin.js'), ...args],
|
|
383
|
+
options: asNode,
|
|
384
|
+
label: `${nodeExecutable} ${join(installDir, 'lib', 'bin.js')} ${args.join(' ')}`,
|
|
385
|
+
})
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const resources = process.resourcesPath
|
|
389
|
+
if (resources) {
|
|
390
|
+
for (const relative of [
|
|
391
|
+
join('app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js'),
|
|
392
|
+
join('app', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js'),
|
|
393
|
+
]) {
|
|
394
|
+
const entry = join(resources, relative)
|
|
395
|
+
if (!existsSync(entry)) continue
|
|
396
|
+
attempts.push({
|
|
397
|
+
command: process.execPath,
|
|
398
|
+
argv: [entry, ...args],
|
|
399
|
+
options: asNode,
|
|
400
|
+
label: `${process.execPath} ${entry} ${args.join(' ')}`,
|
|
401
|
+
})
|
|
402
|
+
}
|
|
403
|
+
const shim = join(resources, 'runtime', 'cli', 'bin', platform() === 'win32' ? 'dsh.cmd' : 'dsh')
|
|
404
|
+
if (existsSync(shim)) {
|
|
405
|
+
attempts.push({
|
|
406
|
+
command: shim,
|
|
407
|
+
argv: args,
|
|
408
|
+
options: { shell: platform() === 'win32' },
|
|
409
|
+
label: `${shim} ${args.join(' ')}`,
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
attempts.push({
|
|
415
|
+
command: 'dsh',
|
|
416
|
+
argv: args,
|
|
417
|
+
options: { shell: platform() === 'win32' },
|
|
418
|
+
label: `dsh ${args.join(' ')}`,
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
return attempts
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async function runDsh(args) {
|
|
425
|
+
let last
|
|
426
|
+
const tried = []
|
|
427
|
+
for (const attempt of cliAttempts(args)) {
|
|
428
|
+
tried.push(attempt.label)
|
|
429
|
+
last = await runOnce(attempt.command, attempt.argv, attempt.options)
|
|
430
|
+
const missing = last.code === 'ENOENT' || /not found|不是内部或外部命令|cannot find/i.test(last.stderr)
|
|
431
|
+
if (missing) continue
|
|
432
|
+
return { ...last, command: attempt.label, tried }
|
|
433
|
+
}
|
|
434
|
+
return { ...(last ?? { ok: false, stdout: '', stderr: '' }), command: 'none', ok: false, tried }
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async function applyList(items) {
|
|
438
|
+
const profile = resolveProfile()
|
|
439
|
+
const specs = []
|
|
440
|
+
for (const item of items ?? []) {
|
|
441
|
+
if (typeof item?.name !== 'string' || !PACKAGE_NAME.test(item.name)) continue
|
|
442
|
+
if (typeof item.version !== 'string' || !SEMVER.test(item.version)) continue
|
|
443
|
+
specs.push(`${item.name}@${item.version}`)
|
|
444
|
+
}
|
|
445
|
+
if (specs.length === 0) throw new Error('nothing to install: need name@version entries')
|
|
446
|
+
|
|
447
|
+
const run = await runDsh(['plugin', '--profile', profile.name, 'add', ...specs])
|
|
448
|
+
const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
|
|
449
|
+
return {
|
|
450
|
+
ok: run.ok,
|
|
451
|
+
specs,
|
|
452
|
+
command: run.command,
|
|
453
|
+
tried: run.tried ?? [],
|
|
454
|
+
exitCode: typeof run.code === 'number' ? run.code : null,
|
|
455
|
+
stdout: tail(run.stdout),
|
|
456
|
+
stderr: tail(run.stderr),
|
|
457
|
+
message: run.ok ? null : run.message ?? 'dsh plugin add failed',
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// ---------------------------------------------------------------- environment
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Whether this host is the Desktop shell's child process.
|
|
465
|
+
*
|
|
466
|
+
* The shell owns restarting the application: the only messages it accepts from a
|
|
467
|
+
* host are `starting`, `ready`, `platform-session`, `shutdown-complete`, `fatal`
|
|
468
|
+
* and control answers, and none of them asks for a restart. A plugin that stops
|
|
469
|
+
* the host only raises the shell's own failure prompt, so this plugin points at
|
|
470
|
+
* the shell's "Restart App and Host" menu item instead of pretending to restart.
|
|
471
|
+
*/
|
|
472
|
+
function isDesktopHost() {
|
|
473
|
+
return typeof process.send === 'function'
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// ---------------------------------------------------------------- routing
|
|
477
|
+
|
|
478
|
+
function createService(ctx) {
|
|
479
|
+
/**
|
|
480
|
+
* A request from this machine is allowed; anything arriving from the network has
|
|
481
|
+
* to pass the host connection gate when one is mounted, so exposing `dsh web`
|
|
482
|
+
* does not also expose "install arbitrary packages".
|
|
483
|
+
*/
|
|
484
|
+
function admitted(request) {
|
|
485
|
+
if (!trusted(request)) return false
|
|
486
|
+
if (loopback(request.socket?.remoteAddress)) return true
|
|
487
|
+
try {
|
|
488
|
+
const connection = ctx.get('connection')
|
|
489
|
+
if (connection && typeof connection.requestRejection === 'function') {
|
|
490
|
+
return connection.requestRejection(request) === undefined
|
|
491
|
+
}
|
|
492
|
+
} catch {
|
|
493
|
+
// no connection service in this composition
|
|
494
|
+
}
|
|
495
|
+
return false
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
async function route(request, response, path) {
|
|
499
|
+
if (!admitted(request)) {
|
|
500
|
+
reply(response, 403, { ok: false, error: 'request refused' })
|
|
501
|
+
return true
|
|
502
|
+
}
|
|
503
|
+
const method = request.method ?? 'GET'
|
|
504
|
+
|
|
505
|
+
if (path === '/state' && (method === 'GET' || method === 'HEAD')) {
|
|
506
|
+
const profile = resolveProfile()
|
|
507
|
+
const state = await readState(profile.dir)
|
|
508
|
+
reply(response, 200, {
|
|
509
|
+
ok: true,
|
|
510
|
+
role: state.role,
|
|
511
|
+
profile: profile.name,
|
|
512
|
+
profileDir: profile.dir,
|
|
513
|
+
dsh: await runningDshVersion(),
|
|
514
|
+
platform: platform(),
|
|
515
|
+
desktop: isDesktopHost(),
|
|
516
|
+
})
|
|
517
|
+
return true
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (path === '/scan' && method === 'GET') {
|
|
521
|
+
reply(response, 200, { ok: true, list: await scan() })
|
|
522
|
+
return true
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (path === '/role' && method === 'POST') {
|
|
526
|
+
const body = JSON.parse((await readBody(request)) || '{}')
|
|
527
|
+
const profile = resolveProfile()
|
|
528
|
+
const current = await readState(profile.dir)
|
|
529
|
+
const state = await writeState(profile.dir, { ...current, role: body.role })
|
|
530
|
+
reply(response, 200, { ok: true, role: state.role })
|
|
531
|
+
return true
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (path === '/parse' && method === 'POST') {
|
|
535
|
+
const body = JSON.parse((await readBody(request)) || '{}')
|
|
536
|
+
const parsed = parseList(body.text)
|
|
537
|
+
const profile = resolveProfile()
|
|
538
|
+
const manifest = await readJson(join(profile.dir, 'package.json'))
|
|
539
|
+
const dependencies = manifest.dependencies ?? {}
|
|
540
|
+
const rows = []
|
|
541
|
+
for (const entry of parsed.plugins) {
|
|
542
|
+
const facts = await installedFacts(profile.dir, entry.name)
|
|
543
|
+
const spec = typeof dependencies[entry.name] === 'string' ? dependencies[entry.name] : null
|
|
544
|
+
// A local mount is never "already installed" for this purpose: keeping it
|
|
545
|
+
// and replacing it are different outcomes, so it gets its own state.
|
|
546
|
+
const local = spec !== null && specKind(spec) !== 'registry'
|
|
547
|
+
let state = 'new'
|
|
548
|
+
if (facts?.version) {
|
|
549
|
+
const order = compareVersions(facts.version, entry.version)
|
|
550
|
+
state = order === 0 ? 'same' : order > 0 ? 'downgrade' : 'upgrade'
|
|
551
|
+
}
|
|
552
|
+
if (local) state = 'linked'
|
|
553
|
+
rows.push({
|
|
554
|
+
...entry,
|
|
555
|
+
spec,
|
|
556
|
+
installed: facts?.version ?? null,
|
|
557
|
+
state,
|
|
558
|
+
})
|
|
559
|
+
}
|
|
560
|
+
reply(response, 200, { ok: true, header: parsed.header, rows, skipped: parsed.skipped })
|
|
561
|
+
return true
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (path === '/apply' && method === 'POST') {
|
|
565
|
+
const body = JSON.parse((await readBody(request)) || '{}')
|
|
566
|
+
reply(response, 200, { ok: true, result: await applyList(body.items) })
|
|
567
|
+
return true
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return false
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async function handle(request, response) {
|
|
574
|
+
const url = new URL(request.url ?? '/', 'http://localhost')
|
|
575
|
+
const path = url.pathname.startsWith(PREFIX) ? url.pathname.slice(PREFIX.length) || '/' : null
|
|
576
|
+
if (path === null) {
|
|
577
|
+
reply(response, 404, { ok: false, error: 'not found' })
|
|
578
|
+
return
|
|
579
|
+
}
|
|
580
|
+
try {
|
|
581
|
+
const claimed = await route(request, response, path)
|
|
582
|
+
if (!claimed) reply(response, 404, { ok: false, error: `unknown route ${path}` })
|
|
583
|
+
} catch (error) {
|
|
584
|
+
reply(response, 500, { ok: false, error: String(error?.message ?? error) })
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return { handle }
|
|
589
|
+
}
|
package/locale/en.json
ADDED