panrouter 6.3.8 → 6.3.9

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/package.json +1 -1
  2. package/pool-worker.mjs +29 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "6.3.8",
3
+ "version": "6.3.9",
4
4
  "description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
5
5
  "type": "module",
6
6
  "bin": {
package/pool-worker.mjs CHANGED
@@ -152,6 +152,9 @@ function connectToHub() {
152
152
  ws.send(JSON.stringify({ type: "pong" }));
153
153
  break;
154
154
 
155
+ case "pong":
156
+ break;
157
+
155
158
  case "upgrade":
156
159
  handleUpgrade();
157
160
  break;
@@ -215,7 +218,9 @@ function handleUpgrade() {
215
218
  hasNewer = true;
216
219
  }
217
220
 
218
- if (hasNewer) {
221
+ // ponytail: Windows 跳过内联 install(当前进程锁着 npm 目录必 EBUSY)
222
+ // 后续 start /b 脚本在进程退出后再执行 install
223
+ if (hasNewer && process.platform !== 'win32') {
219
224
  try {
220
225
  execSync("npm install -g panrouter@latest", { stdio: "inherit", timeout: 120000 });
221
226
  } catch (e) {
@@ -223,26 +228,25 @@ function handleUpgrade() {
223
228
  }
224
229
  }
225
230
 
226
- // 从磁盘重新读(跳过 require 缓存)
227
- const newVersion = JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
228
-
229
231
  log("正在重启 9router...");
230
232
  killPort(SERVER_PORT);
231
233
 
234
+ // Windows:无论如何走 start /b 脚本(当前进程锁着 npm 目录无法内联升级)
235
+ if (process.platform === 'win32') {
236
+ const tmpDir = process.env.TEMP || process.env.TMP || "C:\\Windows\\Temp";
237
+ // 如果已经是最新版也跳过 install
238
+ const noInstall = hasNewer ? "" : "& npm cache clean --force 2>nul & npm install -g panrouter@latest";
239
+ // 先 ping 等当前进程退出锁释放 → 杀残留 node → 切 tmp 目录避免被删 → install → 启动
240
+ const upgradeCmd = `start /b cmd /c "@ping 127.0.0.1 -n 3 >nul && taskkill /f /im node.exe 2>nul & ping 127.0.0.1 -n 3 >nul & cd /d \"${tmpDir}\"${noInstall} && panrouter --pool"`;
241
+ try { execSync(upgradeCmd, { stdio: 'pipe', timeout: 5000, shell: true }); } catch {}
242
+ log("升级脚本已提交 (当前进程退出后自动执行),旧进程退出", "OFF");
243
+ process.exit(0);
244
+ }
245
+
246
+ // Unix:检查版本,需要就 install,然后 spawn 新实例
247
+ const newVersion = JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
232
248
  if (oldVersion !== newVersion) {
233
249
  log(`版本变更: v${oldVersion} → v${newVersion},升级并重启`, "OK");
234
-
235
- // Windows 下当前进程锁住了 npm 目录 → 先 exit 释放锁再 npm install
236
- // 用 start /b 提交一个后台脚本,等 2 秒(进程退出锁释放)后执行升级 + 启动
237
- // start /b 的输出会打到当前终端,用户能看到进度
238
- if (process.platform === 'win32') {
239
- const cmd = `start /b cmd /c "@ping 127.0.0.1 -n 3 >nul && npm install -g panrouter@latest && panrouter --pool"`;
240
- try { execSync(cmd, { stdio: 'pipe', timeout: 5000, shell: true }); } catch {}
241
- log("升级脚本已提交 (当前进程退出后自动执行),旧进程退出", "OFF");
242
- process.exit(0);
243
- }
244
-
245
- // Unix 可以直接 spawn(不会锁)
246
250
  spawn(process.execPath, [process.argv[1], "--pool"], {
247
251
  cwd: __dirname,
248
252
  stdio: "inherit",
@@ -588,10 +592,16 @@ export async function start() {
588
592
  const checkReady = setInterval(async () => {
589
593
  if (_serverReady || (await isPortOpen(SERVER_PORT))) {
590
594
  _serverReady = true;
591
- clearInterval(checkReady);
592
595
  startWatchdog();
593
- safeSend({ type: "ready" });
594
- log("9router 已就绪,节点可正常处理请求", "ON");
596
+ // 持续发 ready 直到 WebSocket 连通(注册可能还没完成)
597
+ const tryReady = setInterval(() => {
598
+ if (ws && ws.readyState === WebSocket.OPEN) {
599
+ ws.send(JSON.stringify({ type: "ready" }));
600
+ clearInterval(tryReady);
601
+ clearInterval(checkReady);
602
+ log("9router 已就绪,节点可正常处理请求", "ON");
603
+ }
604
+ }, 1000);
595
605
  }
596
606
  }, 3000);
597
607
  }