@michengai/dsh-im-connect 0.1.77 → 1.0.0
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 +12 -0
- package/CHANGELOG.zh-CN.md +12 -0
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/client.js +692 -107
- package/lib/client.js +724 -340
- package/lib/engine/file-delivery.js +1 -1
- package/lib/engine/file-delivery.js.map +1 -1
- package/lib/plugin-updater.d.ts +3 -0
- package/lib/plugin-updater.d.ts.map +1 -1
- package/lib/plugin-updater.js +188 -3
- package/lib/plugin-updater.js.map +1 -1
- package/package.json +16 -16
- package/src/engine/file-delivery.ts +1 -1
- package/src/plugin-update-ui.js +32 -13
- package/src/plugin-updater.ts +162 -3
package/src/plugin-updater.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Context } from '@deepseek-ai/cordis'
|
|
2
2
|
import { spawn } from 'node:child_process'
|
|
3
3
|
import { existsSync, readFileSync } from 'node:fs'
|
|
4
|
-
import { readFile } from 'node:fs/promises'
|
|
4
|
+
import { readFile, rename, unlink, writeFile } from 'node:fs/promises'
|
|
5
5
|
import { homedir } from 'node:os'
|
|
6
6
|
import { basename, delimiter, dirname, isAbsolute, resolve } from 'node:path'
|
|
7
7
|
import { fileURLToPath } from 'node:url'
|
|
@@ -267,6 +267,155 @@ async function status(options: PluginUpdaterOptions, target: Runtime): Promise<V
|
|
|
267
267
|
}
|
|
268
268
|
}
|
|
269
269
|
|
|
270
|
+
function releaseAgeSpec(packageSpec: string) {
|
|
271
|
+
const at = packageSpec.lastIndexOf('@')
|
|
272
|
+
if (at <= 0) return undefined
|
|
273
|
+
const name = packageSpec.slice(0, at)
|
|
274
|
+
const version = packageSpec.slice(at + 1)
|
|
275
|
+
if (!isPackageName(name) || !isReleaseVersion(version)) return undefined
|
|
276
|
+
return { name, version }
|
|
277
|
+
}
|
|
278
|
+
function isPackageName(name: string) {
|
|
279
|
+
return /^@[^/\s]+\/[^/\s]+$/.test(name) || /^[^@/\s]+$/.test(name)
|
|
280
|
+
}
|
|
281
|
+
function isReleaseVersion(version: string) {
|
|
282
|
+
return /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version)
|
|
283
|
+
}
|
|
284
|
+
function unreadableReleaseAgeExclude() {
|
|
285
|
+
return new Error('发布年龄白名单无法安全合并,已停止更新,避免这个 profile 之后无法安装插件。')
|
|
286
|
+
}
|
|
287
|
+
function excludeEntry(indent: string, name: string, versions: readonly string[], comment = '') {
|
|
288
|
+
const entry = `${indent}- '${name}@${versions.join(' || ')}'`
|
|
289
|
+
return comment === '' ? entry : `${entry} ${comment}`
|
|
290
|
+
}
|
|
291
|
+
function parseExcludeLine(line: string) {
|
|
292
|
+
const dash = line.indexOf('-')
|
|
293
|
+
if (!/^\s+-/.test(line) || dash < 0) return undefined
|
|
294
|
+
const indent = line.slice(0, dash)
|
|
295
|
+
const raw = line.slice(dash + 1).trim()
|
|
296
|
+
let body = raw
|
|
297
|
+
let comment = ''
|
|
298
|
+
if (raw.startsWith("'") || raw.startsWith('"')) {
|
|
299
|
+
const quote = raw[0] ?? "'"
|
|
300
|
+
const end = raw.lastIndexOf(quote)
|
|
301
|
+
if (end <= 0) return undefined
|
|
302
|
+
const rest = raw.slice(end + 1).trim()
|
|
303
|
+
if (rest !== '' && !rest.startsWith('#')) return undefined
|
|
304
|
+
if (rest.startsWith('#')) comment = rest
|
|
305
|
+
body = raw.slice(1, end)
|
|
306
|
+
} else if (raw.includes('#')) return undefined
|
|
307
|
+
if (body.includes('#')) return undefined
|
|
308
|
+
const versionAt = body.lastIndexOf('@')
|
|
309
|
+
if (versionAt > 0) {
|
|
310
|
+
const name = body.slice(0, versionAt)
|
|
311
|
+
const versions = body.slice(versionAt + 1).split(' || ')
|
|
312
|
+
if (isPackageName(name) && versions.every(isReleaseVersion)) return { indent, name, versions, comment }
|
|
313
|
+
return undefined
|
|
314
|
+
}
|
|
315
|
+
if (isPackageName(body) || /^@[^/\s]+\/\*$/.test(body)) return { indent, name: body, versions: undefined, comment }
|
|
316
|
+
return undefined
|
|
317
|
+
}
|
|
318
|
+
/** 把刚安装的精确版本并进 profile 白名单。pnpm 每个包名只认第一条,所以必须并进已有 union,不能另起一行,也不能收成裸包名。 */
|
|
319
|
+
export function mergeMinimumReleaseAgeExclude(yaml: string, packageSpec: string) {
|
|
320
|
+
const spec = releaseAgeSpec(packageSpec)
|
|
321
|
+
if (spec === undefined) throw new Error('更新版本无法写入发布年龄白名单,已停止更新。')
|
|
322
|
+
const eol = yaml.includes('\r\n') ? '\r\n' : '\n'
|
|
323
|
+
const text = yaml.replaceAll('\r\n', '\n')
|
|
324
|
+
const next = mergeReleaseAgeText(text, spec.name, spec.version)
|
|
325
|
+
return eol === '\r\n' ? next.replaceAll('\n', '\r\n') : next
|
|
326
|
+
}
|
|
327
|
+
function mergeReleaseAgeText(yaml: string, name: string, version: string) {
|
|
328
|
+
if (/minimumReleaseAgeExclude:[^\n\r]/.test(yaml)) throw unreadableReleaseAgeExclude()
|
|
329
|
+
const marker = /(?:^|\n)minimumReleaseAgeExclude:[ \t]*(?:\n|$)/.exec(yaml)
|
|
330
|
+
if (marker === null) {
|
|
331
|
+
const base = yaml.endsWith('\n') || yaml.length === 0 ? yaml : `${yaml}\n`
|
|
332
|
+
const gap = base.length === 0 || base.endsWith('\n\n') ? '' : '\n'
|
|
333
|
+
return `${base}${gap}minimumReleaseAgeExclude:\n - '${name}@${version}'\n`
|
|
334
|
+
}
|
|
335
|
+
const keyAt = (marker.index ?? 0) + (marker[0].startsWith('\n') ? 1 : 0)
|
|
336
|
+
let cursor = keyAt + 'minimumReleaseAgeExclude:'.length
|
|
337
|
+
while (cursor < yaml.length && (yaml[cursor] === ' ' || yaml[cursor] === '\t')) cursor += 1
|
|
338
|
+
if (yaml[cursor] !== '\n') throw unreadableReleaseAgeExclude()
|
|
339
|
+
const bodyStart = cursor + 1
|
|
340
|
+
const lines: string[] = []
|
|
341
|
+
let bodyEnd = bodyStart
|
|
342
|
+
while (bodyEnd <= yaml.length) {
|
|
343
|
+
const nextNl = yaml.indexOf('\n', bodyEnd)
|
|
344
|
+
const lineEnd = nextNl === -1 ? yaml.length : nextNl
|
|
345
|
+
const line = yaml.slice(bodyEnd, lineEnd)
|
|
346
|
+
const ended = line.trim() !== '' && !/^[ \t]*#/.test(line) && !/^[ \t]/.test(line)
|
|
347
|
+
if (ended) break
|
|
348
|
+
if (line.trim() !== '' && !/^[ \t]*#/.test(line) && !/^[ \t]+-/.test(line)) throw unreadableReleaseAgeExclude()
|
|
349
|
+
lines.push(line)
|
|
350
|
+
if (nextNl === -1) {
|
|
351
|
+
bodyEnd = yaml.length
|
|
352
|
+
break
|
|
353
|
+
}
|
|
354
|
+
bodyEnd = nextNl + 1
|
|
355
|
+
if (bodyEnd === yaml.length) break
|
|
356
|
+
}
|
|
357
|
+
let ours: { index: number; versions: string[] | undefined; indent: string; comment: string } | undefined
|
|
358
|
+
const rewritten = lines.map((line, index) => {
|
|
359
|
+
if (line.trim() === '' || /^[ \t]*#/.test(line)) return line
|
|
360
|
+
const parsed = parseExcludeLine(line)
|
|
361
|
+
if (parsed === undefined) throw unreadableReleaseAgeExclude()
|
|
362
|
+
if (parsed.name !== name) return line
|
|
363
|
+
if (ours === undefined) {
|
|
364
|
+
ours = { index, indent: parsed.indent, versions: parsed.versions, comment: parsed.comment }
|
|
365
|
+
return line
|
|
366
|
+
}
|
|
367
|
+
if (ours.versions !== undefined && parsed.versions !== undefined) ours.versions = [...ours.versions, ...parsed.versions.filter((item) => !ours?.versions?.includes(item))]
|
|
368
|
+
return ''
|
|
369
|
+
})
|
|
370
|
+
if (ours?.versions === undefined && ours !== undefined) return yaml
|
|
371
|
+
const indent = ours?.indent ?? lines.find((line) => /^[ \t]+-/.test(line))?.match(/^([ \t]+)/)?.[1] ?? ' '
|
|
372
|
+
const versions = ours?.versions === undefined ? [version] : ours.versions.includes(version) ? ours.versions : [...ours.versions, version]
|
|
373
|
+
const entry = excludeEntry(indent, name, versions, ours?.comment ?? '')
|
|
374
|
+
if (ours === undefined) rewritten.push(entry)
|
|
375
|
+
else rewritten[ours.index] = entry
|
|
376
|
+
const body = rewritten.filter((line) => line !== '').join('\n')
|
|
377
|
+
const prefix = yaml.slice(0, bodyStart)
|
|
378
|
+
const suffix = yaml.slice(bodyEnd)
|
|
379
|
+
const joined = `${prefix}${body}${body.length === 0 || body.endsWith('\n') ? '' : '\n'}${suffix}`
|
|
380
|
+
return joined.endsWith('\n') ? joined : `${joined}\n`
|
|
381
|
+
}
|
|
382
|
+
async function guardReleaseAgeExclude(profileDir: string, packageSpec: string) {
|
|
383
|
+
const file = resolve(profileDir, 'pnpm-workspace.yaml')
|
|
384
|
+
const current = existsSync(file) ? await readFile(file, 'utf8') : 'packages:\n - .\n'
|
|
385
|
+
mergeMinimumReleaseAgeExclude(current, packageSpec)
|
|
386
|
+
}
|
|
387
|
+
async function replaceTextFile(file: string, next: string) {
|
|
388
|
+
const tmp = `${file}.${process.pid}.tmp`
|
|
389
|
+
const backup = `${file}.${process.pid}.bak`
|
|
390
|
+
await writeFile(tmp, next, 'utf8')
|
|
391
|
+
try {
|
|
392
|
+
await rename(tmp, file)
|
|
393
|
+
return
|
|
394
|
+
} catch (error) {
|
|
395
|
+
const code = error && typeof error === 'object' && 'code' in error ? String(error.code) : ''
|
|
396
|
+
if (code !== 'EEXIST' && code !== 'EPERM' && code !== 'EACCES') {
|
|
397
|
+
await unlink(tmp).catch(() => {})
|
|
398
|
+
throw error
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
await unlink(backup).catch(() => {})
|
|
402
|
+
await rename(file, backup)
|
|
403
|
+
try {
|
|
404
|
+
await rename(tmp, file)
|
|
405
|
+
} catch (error) {
|
|
406
|
+
await rename(backup, file).catch(() => {})
|
|
407
|
+
await unlink(tmp).catch(() => {})
|
|
408
|
+
throw error
|
|
409
|
+
}
|
|
410
|
+
await unlink(backup).catch(() => {})
|
|
411
|
+
}
|
|
412
|
+
export async function recordReleaseAgeExclude(profileDir: string, packageSpec: string) {
|
|
413
|
+
const file = resolve(profileDir, 'pnpm-workspace.yaml')
|
|
414
|
+
const current = existsSync(file) ? await readFile(file, 'utf8') : 'packages:\n - .\n'
|
|
415
|
+
const next = mergeMinimumReleaseAgeExclude(current, packageSpec)
|
|
416
|
+
if (next !== current) await replaceTextFile(file, next)
|
|
417
|
+
}
|
|
418
|
+
|
|
270
419
|
async function runCliInstall(target: Runtime, packageSpec: string): Promise<void> {
|
|
271
420
|
if (target.cliEntry === undefined) throw new Error('当前环境不支持自动更新,请使用手工更新命令。')
|
|
272
421
|
await new Promise<void>((resolvePromise, reject) => {
|
|
@@ -288,12 +437,15 @@ async function runCliInstall(target: Runtime, packageSpec: string): Promise<void
|
|
|
288
437
|
|
|
289
438
|
async function install(target: Runtime, packageSpec: string): Promise<void> {
|
|
290
439
|
if (target.desktopPnpm !== undefined) {
|
|
440
|
+
await guardReleaseAgeExclude(target.profileDir, packageSpec)
|
|
291
441
|
const handle = target.desktopPnpm.runPlugin(['add', '--config.minimumReleaseAge=0', packageSpec, '--registry=https://registry.npmjs.org/'], target.profileDir)
|
|
292
442
|
const result = await handle.done
|
|
293
443
|
if (result.exitCode !== 0) throw new Error(`更新进程退出码 ${String(result.exitCode)}。`)
|
|
444
|
+
await recordReleaseAgeExclude(target.profileDir, packageSpec)
|
|
294
445
|
return
|
|
295
446
|
}
|
|
296
447
|
if (target.packageManager !== undefined) {
|
|
448
|
+
await guardReleaseAgeExclude(target.profileDir, packageSpec)
|
|
297
449
|
const manager = target.packageManager
|
|
298
450
|
await new Promise<void>((resolvePromise, reject) => {
|
|
299
451
|
const child = spawn(manager.command, [...manager.args, 'add', '--config.minimumReleaseAge=0', packageSpec, '--registry=https://registry.npmjs.org/'], {
|
|
@@ -306,14 +458,21 @@ async function install(target: Runtime, packageSpec: string): Promise<void> {
|
|
|
306
458
|
child.once('error', error => { clearTimeout(timer); reject(error) })
|
|
307
459
|
child.once('exit', code => { clearTimeout(timer); if (code === 0) resolvePromise(); else reject(new Error(detail.trim() || `更新进程退出码 ${String(code)}`)) })
|
|
308
460
|
})
|
|
461
|
+
await recordReleaseAgeExclude(target.profileDir, packageSpec)
|
|
309
462
|
return
|
|
310
463
|
}
|
|
311
464
|
if (target.pluginManager !== undefined) {
|
|
465
|
+
await guardReleaseAgeExclude(target.profileDir, packageSpec)
|
|
312
466
|
const result = await target.pluginManager.installBundle(packageSpec)
|
|
313
|
-
if (result.application === 'applied' || result.application === 'restart-required' || result.application === 'overridden')
|
|
467
|
+
if (result.application === 'applied' || result.application === 'restart-required' || result.application === 'overridden') {
|
|
468
|
+
await recordReleaseAgeExclude(target.profileDir, packageSpec)
|
|
469
|
+
return
|
|
470
|
+
}
|
|
314
471
|
throw new Error(result.error?.message || result.packageResult?.output || '更新失败,请查看服务端日志。')
|
|
315
472
|
}
|
|
316
|
-
|
|
473
|
+
await guardReleaseAgeExclude(target.profileDir, packageSpec)
|
|
474
|
+
await runCliInstall(target, packageSpec)
|
|
475
|
+
await recordReleaseAgeExclude(target.profileDir, packageSpec)
|
|
317
476
|
}
|
|
318
477
|
|
|
319
478
|
function json(response: HostResponse, statusCode: number, value: unknown): void {
|