panrouter 5.4.2 → 5.4.4

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