dsh-plugin-opencode2api 1.3.5 → 1.3.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.
Files changed (2) hide show
  1. package/lib/index.js +27 -7
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -404,9 +404,12 @@ function existingBinaryOk(file) {
404
404
 
405
405
  async function ensureCloudflared() {
406
406
  if (existingBinaryOk(CLOUDFLARED)) return
407
- // 之前下到的可能是镜像的错误网页,留着只会每次启动都失败
408
407
  try {
409
- if (fs.existsSync(CLOUDFLARED)) fs.unlinkSync(CLOUDFLARED)
408
+ if (fs.existsSync(CLOUDFLARED)) {
409
+ const size = fs.statSync(CLOUDFLARED).size
410
+ logTunnel(`existing cloudflared invalid (size=${size}), re-downloading`)
411
+ fs.unlinkSync(CLOUDFLARED)
412
+ }
410
413
  } catch {}
411
414
  const t = cloudflaredTarget()
412
415
  let lastFailure = null
@@ -427,10 +430,19 @@ async function ensureCloudflared() {
427
430
  if (!expectedMagicOk(body)) {
428
431
  throw new Error(`下载到的不是可执行文件(${body.length} 字节,多半是镜像错误页)`)
429
432
  }
430
- await fsp.writeFile(CLOUDFLARED, body)
433
+ // 先写临时文件再改名:正在运行的旧 cloudflared 不会被写坏(Windows/Linux 都安全)
434
+ const temp = `${CLOUDFLARED}.new`
435
+ await fsp.writeFile(temp, body)
436
+ if (!IS_WIN) fs.chmodSync(temp, 0o755)
437
+ try {
438
+ fs.renameSync(temp, CLOUDFLARED)
439
+ } catch (error) {
440
+ try { fs.unlinkSync(temp) } catch {}
441
+ throw error
442
+ }
431
443
  logTunnel(`cloudflared downloaded bytes=${body.length} from=${prefix || 'github'}`)
432
444
  }
433
- if (!IS_WIN) fs.chmodSync(CLOUDFLARED, 0o755)
445
+ if (!IS_WIN && t.archive) fs.chmodSync(CLOUDFLARED, 0o755)
434
446
  return
435
447
  } catch (error) {
436
448
  lastFailure = error
@@ -521,17 +533,25 @@ async function startTunnel() {
521
533
  try { fs.unlinkSync(TUNNEL_PID) } catch {}
522
534
  }
523
535
  if (!fs.existsSync(CLOUDFLARED)) await ensureCloudflared()
524
- const fd = fs.openSync(TUNNEL_LOG, 'a')
536
+ let fd = null
537
+ try {
538
+ fd = fs.openSync(TUNNEL_LOG, 'a')
539
+ } catch {
540
+ // Windows 上日志被杀软临时占用时也要能启动,不能因为打不开日志就整条隧道不跑
541
+ fd = null
542
+ }
525
543
  let child
526
544
  try {
527
545
  child = spawn(CLOUDFLARED, tunnelArgs(pool.tunnelToken), {
528
- stdio: ['ignore', fd, fd],
546
+ stdio: fd === null ? 'ignore' : ['ignore', fd, fd],
529
547
  detached: false,
530
548
  windowsHide: true,
531
549
  })
532
550
  tunnelChild = child
533
551
  } finally {
534
- fs.closeSync(fd)
552
+ if (fd !== null) {
553
+ try { fs.closeSync(fd) } catch {}
554
+ }
535
555
  }
536
556
  child.once('error', (error) => logTunnel(`spawn error: ${error?.message || error}`))
537
557
  child.once('exit', (code, signal) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-opencode2api",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Unofficial one-click installer and auto-runner for opencode2api — a local free OpenAI-compatible API gateway — inside DSH, with a desktop settings UI and a dsh-go native panel (Windows, macOS, Linux, Android).",
5
5
  "repository": {
6
6
  "type": "git",