pi-webapp 0.1.57-alpha.1 → 0.1.57-alpha.2
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 +49 -10
- package/dist/extension.js +94 -94
- package/dist/hosted-recovery.js +2 -2
- package/dist/hosted-worker.js +2 -2
- package/package.json +2 -2
- package/web/dist/assets/app.js +3 -3
package/bin/pi-webapp.mjs
CHANGED
|
@@ -116,7 +116,30 @@ function notifyParent(message) {
|
|
|
116
116
|
if (process.connected) process.send?.(message);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
function parseStartArgs(args) {
|
|
120
|
+
const piArgs = [];
|
|
121
|
+
let host, port;
|
|
122
|
+
for (let i = 0; i < args.length; i++) {
|
|
123
|
+
const arg = args[i];
|
|
124
|
+
if (arg === "--") { piArgs.push(...args.slice(i)); break; }
|
|
125
|
+
const match = /^(--host|--port)(?:=(.*))?$/.exec(arg);
|
|
126
|
+
if (!match) { piArgs.push(arg); continue; }
|
|
127
|
+
const value = match[2] ?? args[++i];
|
|
128
|
+
if (match[1] === "--host") {
|
|
129
|
+
if (host !== undefined) throw new Error("--host 只能指定一次");
|
|
130
|
+
if (value !== "127.0.0.1" && value !== "0.0.0.0") throw new Error("--host 必须是 127.0.0.1 或 0.0.0.0");
|
|
131
|
+
host = value;
|
|
132
|
+
} else {
|
|
133
|
+
if (port !== undefined) throw new Error("--port 只能指定一次");
|
|
134
|
+
if (value === undefined || !/^\d+$/.test(value) || Number(value) < 1 || Number(value) > 65535) throw new Error("--port 必须是 1–65535 之间的整数");
|
|
135
|
+
port = Number(value);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return { host, port, piArgs };
|
|
139
|
+
}
|
|
140
|
+
|
|
119
141
|
async function serve(pi, args) {
|
|
142
|
+
const { host = "127.0.0.1", port, piArgs } = parseStartArgs(args);
|
|
120
143
|
const nonce = randomUUID();
|
|
121
144
|
await mkdir(dirname(statePath), { recursive: true });
|
|
122
145
|
let claimed = false;
|
|
@@ -133,8 +156,8 @@ async function serve(pi, args) {
|
|
|
133
156
|
|
|
134
157
|
const pluginRoots = (process.env.PI_WEBAPP_PLUGIN_DEV_ROOTS ?? "").split(delimiter).filter(Boolean).map((root) => resolve(root));
|
|
135
158
|
const piPrefix = process.env.PI_WEBAPP_PI_CLI ? [resolve(process.env.PI_WEBAPP_PI_CLI)] : [];
|
|
136
|
-
const child = spawn(pi, [...piPrefix, "--mode", "rpc", "-e", extension, ...pluginRoots.flatMap((root) => ["-e", root]), ...
|
|
137
|
-
cwd: process.cwd(), env: { ...process.env, NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ""} --import=${JSON.stringify(new URL("./model-network-bootstrap.mjs", import.meta.url).href)}`.trim(), PI_WEBAPP_AUTO_OPEN: "0", PI_WEBAPP_LAUNCHER_NONCE: nonce, PI_WEBAPP_LAUNCHER_HANDOFF_PROTOCOL: "1", PI_WEBAPP_LAUNCHER_VERSION: packageVersion }, stdio: ["pipe", "pipe", "pipe"],
|
|
159
|
+
const child = spawn(pi, [...piPrefix, "--mode", "rpc", "-e", extension, ...pluginRoots.flatMap((root) => ["-e", root]), ...piArgs], {
|
|
160
|
+
cwd: process.cwd(), env: { ...process.env, NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ""} --import=${JSON.stringify(new URL("./model-network-bootstrap.mjs", import.meta.url).href)}`.trim(), PI_WEBAPP_AUTO_OPEN: "0", PI_WEBAPP_LISTEN_HOST: host, PI_WEBAPP_LISTEN_PORT: port === undefined ? "" : String(port), PI_WEBAPP_LAUNCHER_NONCE: nonce, PI_WEBAPP_LAUNCHER_HANDOFF_PROTOCOL: "1", PI_WEBAPP_LAUNCHER_VERSION: packageVersion }, stdio: ["pipe", "pipe", "pipe"],
|
|
138
161
|
});
|
|
139
162
|
child.stderr?.pipe(process.stderr, { end: false });
|
|
140
163
|
let ready = false;
|
|
@@ -322,20 +345,29 @@ async function serve(pi, args) {
|
|
|
322
345
|
fail(`Pi 拒绝打开网页:${record.error ?? "未知错误"}`);
|
|
323
346
|
return;
|
|
324
347
|
}
|
|
325
|
-
if (record?.type !== "extension_ui_request" || record.method !== "notify" || typeof record.message !== "string"
|
|
348
|
+
if (record?.type !== "extension_ui_request" || record.method !== "notify" || typeof record.message !== "string") continue;
|
|
349
|
+
if (record.message.startsWith("pi-webapp-start-error: ")) {
|
|
350
|
+
fail(record.message.slice("pi-webapp-start-error: ".length));
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (!record.message.startsWith("pi-webapp: ")) continue;
|
|
326
354
|
startupStage("web-ready-notification");
|
|
327
355
|
let url;
|
|
328
356
|
try { url = new URL(record.message.slice("pi-webapp: ".length)); }
|
|
329
357
|
catch { continue; }
|
|
330
358
|
if (url.hostname !== "127.0.0.1" || !url.hash) continue;
|
|
359
|
+
if (port !== undefined && Number(url.port || "80") !== port) {
|
|
360
|
+
fail(`Pi Web 未监听指定端口 ${port}。日志:${logPath}`);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
331
363
|
runningUrl = url.href;
|
|
332
364
|
opening = true;
|
|
333
|
-
stateWrite = saveState({ pid: process.pid, piPid: child.pid, nonce, cwd: process.cwd(), url: url.href, ...runtimeInfo })
|
|
365
|
+
stateWrite = saveState({ pid: process.pid, piPid: child.pid, nonce, cwd: process.cwd(), url: url.href, listenHost: host, ...runtimeInfo })
|
|
334
366
|
.then(() => {
|
|
335
367
|
if (closed || failed) return;
|
|
336
368
|
ready = true;
|
|
337
369
|
clearTimeout(startupTimer);
|
|
338
|
-
lifecycle("launcher_ready", { port: Number(url.port) });
|
|
370
|
+
lifecycle("launcher_ready", { port: Number(url.port || "80") });
|
|
339
371
|
notifyParent({ type: "ready", pid: process.pid, url: url.href });
|
|
340
372
|
for (const root of pluginRoots) {
|
|
341
373
|
try {
|
|
@@ -364,7 +396,7 @@ async function serve(pi, args) {
|
|
|
364
396
|
closed = true;
|
|
365
397
|
lifecycle("pi_process_exit", { ready, exitCode: code, signal,
|
|
366
398
|
reason: stopReason ?? (ready ? "unexpected_exit" : "startup_failure"), forceStopRequested,
|
|
367
|
-
port: runningUrl ? Number(new URL(runningUrl).port) : null });
|
|
399
|
+
port: runningUrl ? Number(new URL(runningUrl).port || "80") : null });
|
|
368
400
|
if (!ready) fail(`Pi 提前退出(代码 ${code ?? "未知"})。日志:${logPath}`);
|
|
369
401
|
resolveExit();
|
|
370
402
|
});
|
|
@@ -406,7 +438,7 @@ async function serve(pi, args) {
|
|
|
406
438
|
await unlink(restartPath).catch(() => {});
|
|
407
439
|
const oldUrl = new URL(runningUrl);
|
|
408
440
|
const fd = openSync(logPath, "a", 0o600);
|
|
409
|
-
const restartEnv = { ...process.env, PI_WEBAPP_AUTO_OPEN: "0", PI_WEBAPP_RESTART_PORT: oldUrl.port, PI_WEBAPP_RESTART_TOKEN: oldUrl.hash.slice(1) };
|
|
441
|
+
const restartEnv = { ...process.env, PI_WEBAPP_AUTO_OPEN: "0", PI_WEBAPP_RESTART_PORT: oldUrl.port || "80", PI_WEBAPP_RESTART_TOKEN: oldUrl.hash.slice(1) };
|
|
410
442
|
try {
|
|
411
443
|
const runLauncher = async (path) => {
|
|
412
444
|
const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion, desktopFallbackVersion) : null;
|
|
@@ -490,8 +522,14 @@ async function managedLauncher() {
|
|
|
490
522
|
}
|
|
491
523
|
|
|
492
524
|
async function start(args) {
|
|
525
|
+
const { host, port, piArgs } = parseStartArgs(args);
|
|
493
526
|
const existing = await activeState();
|
|
494
527
|
if (existing) {
|
|
528
|
+
if ((host !== undefined || port !== undefined) && (!existing.url
|
|
529
|
+
|| (host !== undefined && host !== (existing.listenHost ?? "127.0.0.1"))
|
|
530
|
+
|| (port !== undefined && port !== Number(new URL(existing.url).port || "80")))) {
|
|
531
|
+
throw new Error("pi-webapp 已在启动或使用不同的监听配置。请先运行 pi-webapp stop,再使用指定参数启动。");
|
|
532
|
+
}
|
|
495
533
|
console.log(existing.url ? `pi-webapp 已在后台运行:${existing.url}` : "pi-webapp 正在启动");
|
|
496
534
|
if (existing.url) await openPageIfLocal(existing.url);
|
|
497
535
|
return;
|
|
@@ -512,7 +550,7 @@ async function start(args) {
|
|
|
512
550
|
const nodeVersion = versionParts(process.versions.node);
|
|
513
551
|
if (!nodeVersion || !compatible(nodeVersion, [22, 19, 0])) throw new Error("需要 Node.js 22.19 或更新版本。");
|
|
514
552
|
if (!existsSync(extension)) throw new Error("找不到构建后的扩展。请先运行 npm run build。");
|
|
515
|
-
if (
|
|
553
|
+
if (piArgs.some((arg) => ["--mode", "--print", "-p"].includes(arg))) throw new Error("后台启动不支持覆盖 Pi 的运行模式");
|
|
516
554
|
const piCli = await packagePiCli(dirname(dirname(launcher))) ?? process.env.PI_WEBAPP_PI_CLI;
|
|
517
555
|
const pi = piCli ? process.execPath : probe("pi") ?? installPi();
|
|
518
556
|
await mkdir(dirname(statePath), { recursive: true });
|
|
@@ -600,8 +638,9 @@ async function main() {
|
|
|
600
638
|
}
|
|
601
639
|
return;
|
|
602
640
|
}
|
|
603
|
-
|
|
604
|
-
if (argument
|
|
641
|
+
const usage = "用法:pi-webapp [start] [--port <1–65535>] [--host <127.0.0.1|0.0.0.0>] [Pi 参数]\n pi-webapp status | stop [--force]";
|
|
642
|
+
if (argument === "--help" || argument === "-h" || (argument === "start" && rest.length === 1 && ["--help", "-h"].includes(rest[0]))) { console.log(usage); return; }
|
|
643
|
+
if (argument && argument !== "start" && !argument.startsWith("-")) throw new Error(usage);
|
|
605
644
|
await start(argument === "start" || !argument ? rest : [argument, ...rest]);
|
|
606
645
|
}
|
|
607
646
|
|