pi-webapp 0.1.34-alpha.3 → 0.1.35

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/bin/pi-webapp.mjs CHANGED
@@ -141,6 +141,9 @@ async function serve(pi, args) {
141
141
  let killTimer;
142
142
  let stopReason;
143
143
  let forceStopRequested = false;
144
+ let stopCommand;
145
+ let stopPreflightTimer;
146
+ let stopPreflightId;
144
147
  const instanceId = randomUUID();
145
148
  const startedAt = performance.now();
146
149
  const lifecycle = (event, fields = {}) => {
@@ -157,11 +160,7 @@ async function serve(pi, args) {
157
160
  let devReloadTimer;
158
161
  let devReloadCount = 0;
159
162
  let devReloadCommand;
160
- const shutdown = (reason) => {
161
- if (stopping) return;
162
- stopping = true;
163
- stopReason = reason;
164
- lifecycle("launcher_stop_requested", { reason });
163
+ const finishShutdown = (reason) => {
165
164
  child.stdin?.end();
166
165
  killTimer = setTimeout(() => {
167
166
  forceStopRequested = true;
@@ -170,6 +169,29 @@ async function serve(pi, args) {
170
169
  }, 5000);
171
170
  killTimer.unref();
172
171
  };
172
+ const refuseShutdown = () => {
173
+ clearTimeout(stopPreflightTimer);
174
+ stopPreflightId = undefined;
175
+ lifecycle("launcher_stop_refused", { reason: stopReason });
176
+ stopping = false;
177
+ restartRequest = null;
178
+ void readFile(stopPath, "utf8").then(value => value.trim() === nonce ? unlink(stopPath) : undefined).catch(() => {});
179
+ };
180
+ const shutdown = (reason) => {
181
+ if (stopping) return;
182
+ stopping = true;
183
+ stopReason = reason;
184
+ lifecycle("launcher_stop_requested", { reason });
185
+ // RPC EOF can exit even when an extension's shutdown handler fails. Drain
186
+ // while the transport is alive and require an explicit successful response.
187
+ if (ready && stopCommand) {
188
+ stopPreflightId = `prepare-stop-${randomUUID()}`;
189
+ // A slow admitted batch may still be draining. A timeout is not proof it
190
+ // stopped: keep its receipt live and never launch a second teardown.
191
+ stopPreflightTimer = setTimeout(() => lifecycle("launcher_stop_pending", { reason }), 35_000);
192
+ child.stdin?.write(JSON.stringify({ id: stopPreflightId, type: "prompt", message: `/${stopCommand} ${stopPreflightId}` }) + "\n");
193
+ } else finishShutdown(reason); // Legacy runtimes have no drain command.
194
+ };
173
195
  const fail = (message) => {
174
196
  if (failed || ready) return;
175
197
  failed = true;
@@ -203,6 +225,20 @@ async function serve(pi, args) {
203
225
  buffer = buffer.slice(index + 1);
204
226
  let record;
205
227
  try { record = JSON.parse(line); } catch { continue; }
228
+ if (record?.type === "extension_ui_request" && record.method === "notify" && typeof record.message === "string" && record.message.startsWith("pi-webapp-stop: ")) {
229
+ let receipt;
230
+ try { receipt = JSON.parse(record.message.slice("pi-webapp-stop: ".length)); } catch { continue; }
231
+ if (!stopping || receipt.id !== stopPreflightId) continue;
232
+ stopPreflightId = undefined;
233
+ clearTimeout(stopPreflightTimer);
234
+ if (receipt.ok === true) finishShutdown(stopReason);
235
+ else refuseShutdown();
236
+ continue;
237
+ }
238
+ if (record?.type === "response" && record.id === stopPreflightId && record.success === false) {
239
+ refuseShutdown();
240
+ continue;
241
+ }
206
242
  if (record?.type === "response" && /^dev-reload-\d+$/.test(record.id ?? "")) {
207
243
  console.error(`pi-webapp: ${record.id} /${devReloadCommand ?? "web-dev-reload"}: ${record.success === true ? "reloaded" : `failed: ${record.error ?? "Unknown RPC error"}`}`);
208
244
  continue;
@@ -220,6 +256,8 @@ async function serve(pi, args) {
220
256
  fail("Pi 没有加载当前项目的 Web 扩展");
221
257
  return;
222
258
  }
259
+ stopCommand = record.data.commands.find((item) => item?.source === "extension"
260
+ && item.sourceInfo?.path === extension && /^web-prepare-stop(?::\d+)?$/.test(item.name))?.name;
223
261
  devReloadCommand = record.data.commands.find((item) => item?.source === "extension"
224
262
  && item.sourceInfo?.path === extension && /^web-dev-reload(?::\d+)?$/.test(item.name))?.name;
225
263
  startupStage("opening-web-command");
@@ -279,6 +317,7 @@ async function serve(pi, args) {
279
317
  });
280
318
  await stateWrite;
281
319
  clearTimeout(startupTimer);
320
+ clearTimeout(stopPreflightTimer);
282
321
  clearTimeout(devReloadTimer);
283
322
  for (const watcher of devWatchers) watcher.close();
284
323
  clearInterval(stopTimer);
@@ -412,10 +451,12 @@ async function stop() {
412
451
  if (!state) { console.log("pi-webapp 未运行"); return; }
413
452
  if (!state.nonce) throw new Error("pi-webapp 正在启动,请稍后再停止");
414
453
  await writeFile(stopPath, state.nonce, { mode: 0o600 });
415
- for (let attempt = 0; attempt < 120; attempt++) {
454
+ for (let attempt = 0; attempt < 400; attempt++) {
416
455
  await new Promise((resolveWait) => setTimeout(resolveWait, 100));
417
456
  const current = await activeState();
418
457
  if (!current || current.nonce !== state.nonce) { console.log("pi-webapp 已停止"); return; }
458
+ try { await stat(stopPath); }
459
+ catch (error) { if (error?.code === "ENOENT") throw new Error("停止已取消:插件保存或释放未完成,服务及数据已保留。请处理插件诊断后重试。"); throw error; }
419
460
  }
420
461
  throw new Error(`停止超时。后台进程 PID:${state.pid};日志:${logPath}`);
421
462
  }