panrouter 5.4.1 → 5.4.3

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 +36 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "5.4.1",
3
+ "version": "5.4.3",
4
4
  "description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
5
5
  "type": "module",
6
6
  "bin": {
package/pool-worker.mjs CHANGED
@@ -178,42 +178,59 @@ function scheduleReconnect() {
178
178
 
179
179
  function handleUpgrade() {
180
180
  log("收到主控升级指令,开始升级...", "WARN");
181
- // 断开连接
182
181
  if (ws) {
183
182
  try { ws.close(1000, "Upgrading"); } catch {}
184
183
  ws = null;
185
184
  }
186
185
  stopHeartbeat();
187
186
 
188
- log("正在执行 npm install -g panrouter@latest ...");
187
+ const pkgPath = path.join(__dirname, "package.json");
188
+ const oldVersion = JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
189
+ log(`当前版本: v${oldVersion},正在执行 npm install -g panrouter@latest ...`);
189
190
  try {
190
191
  execSync("npm install -g panrouter@latest", { stdio: "inherit", timeout: 120000 });
191
- log("升级完成,正在重启...", "OK");
192
192
  } catch (e) {
193
193
  log(`升级失败: ${e.message},仍尝试重启`, "ERR");
194
194
  }
195
195
 
196
- // PID 文件让新进程可追踪(旧 PID 文件被 cleanupStaleSelf 覆盖)
197
- try {
198
- if (!fs.existsSync(RUNTIME_DIR)) fs.mkdirSync(RUNTIME_DIR, { recursive: true });
199
- fs.writeFileSync(PID_FILE, String(process.pid), "utf-8");
200
- } catch {}
196
+ // 从磁盘重新读(跳过 require 缓存)
197
+ const newVersion = JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
201
198
 
202
- // 清理旧的 server.mjs,确保新实例用最新代码
199
+ // 清理旧 server.mjs
203
200
  log("正在重启代理服务...");
204
201
  killPort(SERVER_PORT);
205
202
 
206
- // 启动新后台实例,完全脱离当前终端
207
- const child = spawn(process.execPath, [process.argv[1], "--pool"], {
208
- cwd: __dirname,
209
- stdio: "ignore",
210
- detached: true,
211
- windowsHide: true,
212
- });
213
- child.unref();
203
+ if (oldVersion !== newVersion) {
204
+ // 版本变了 spawn 新进程加载新代码
205
+ log(`版本变更: v${oldVersion} → v${newVersion},启动新进程`, "OK");
206
+ try {
207
+ if (!fs.existsSync(RUNTIME_DIR)) fs.mkdirSync(RUNTIME_DIR, { recursive: true });
208
+ fs.writeFileSync(PID_FILE, String(process.pid), "utf-8");
209
+ } catch {}
214
210
 
215
- log("新实例已在后台启动", "OFF");
216
- process.exit(0);
211
+ spawn(process.execPath, [process.argv[1], "--pool"], {
212
+ cwd: __dirname,
213
+ stdio: "inherit",
214
+ });
215
+ log("新实例已启动,旧进程退出", "OFF");
216
+ process.exit(0);
217
+ } else {
218
+ // 版本没变 → 在当前进程内直接重启连接
219
+ log(`版本未变更 (v${oldVersion}),在当前进程内重新连接`, "OK");
220
+ doRestart();
221
+ }
222
+ }
223
+
224
+ async function doRestart() {
225
+ const serverOk = await ensureServer();
226
+ if (!serverOk) {
227
+ log("代理服务重启失败", "ERR");
228
+ process.exit(1);
229
+ }
230
+ reconnectDelay = WS_RECONNECT_BASE;
231
+ writePid();
232
+ connectToHub();
233
+ log("节点看门狗已启动,等待公网入口...", "ON");
217
234
  }
218
235
 
219
236
  // ─── 处理来自主控的请求 ─────────────────────────────────────────────────────