// 启动前预检:构建产物、Node 版本、凭据、浏览器、调试端口。 // // 目标是把「失败」变成一段人能照着做的说明,而不是一个栈。所有检查失败都抛 // PreflightError(exitCode=2),由 headless.mjs 统一打印并退出。 import fs from 'node:fs'; import { httpJson } from './cdp.mjs'; import { resolveBrowser, listCandidatePaths } from './browser.mjs'; import { isWindows, platformName, portInUseHint, browserInstallHint, killProfileOrphans, } from './platform.mjs'; export class PreflightError extends Error { constructor(lines, exitCode = 2) { super(Array.isArray(lines) ? lines.join('\n') : String(lines)); this.name = 'PreflightError'; this.lines = Array.isArray(lines) ? lines : [String(lines)]; this.exitCode = exitCode; } } /** * 逐项检查,全部通过后返回 { browser, scriptSrc }。 * @param {object} o * @param {boolean} o.checkOnly 自检模式不校验凭据 * @param {string} o.scriptFile 构建产物路径 * @param {string} o.usr * @param {string} o.pwd * @param {number} o.port * @param {string} o.profileDir 端口被占时用来找出「自己的」残留浏览器 * @param {string} [o.chromeBin] 配置文件里指定的浏览器路径(空 = 自动探测) * @param {Function} [o.log] */ export async function runPreflight(o) { const fail = (lines) => { throw new PreflightError(lines); }; // 1) 构建产物:没有它就没东西可注入 if (!fs.existsSync(o.scriptFile)) { fail([ '缺少构建产物 ' + o.scriptFile + ',先运行 npm run build', '(仓库里只提交源码 src/,产物由 esbuild 生成)', ]); } // 2) Node 运行时能力:cdp.mjs 用全局 WebSocket / fetch,Node 22 起才稳定 if (typeof WebSocket !== 'function' || typeof fetch !== 'function') { fail([ '需要 Node 22+(依赖全局 WebSocket / fetch),当前 ' + process.version, isWindows ? 'Windows 上升级:winget install OpenJS.NodeJS.LTS 或到 nodejs.org 下载 LTS 安装包' : 'Linux 上可用 nvm / nodesource 升级:curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -', ]); } // 3) 凭据:自检模式不需要(只读检查不登录) if (!o.checkOnly && (!o.usr || !o.pwd)) { fail([ '缺少环境变量 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD。', '(即使 profile 里有有效 cookie,也建议填上:cookie 失效时才能自动重登)', ]); } // 4) 浏览器:找不到就把所有尝试过的路径列出来,省得用户猜 const browser = o.chromeBin || resolveBrowser(); if (!browser || !fs.existsSync(browser)) { fail(['找不到 Chrome/Chromium(' + platformName + '),可用 YUN_XUE_TANG_CHROME_BIN=/path/to/chrome ' + '或配置文件里的 chrome 字段指定。已尝试:'] .concat(listCandidatePaths().map((p) => ' ' + p)) .concat(browserInstallHint())); } // 5) 调试端口:被占用意味着上一次的浏览器还在跑,Chrome 会直接拒绝启动。 // 无人值守场景下「自己上次没退干净」是最常见的原因,先按 profile 自愈一次: // 只杀命令行里带本 profile 目录的进程,不碰用户自己的浏览器。 const probe = async () => { try { return await httpJson('http://127.0.0.1:' + o.port + '/json/version'); } catch (e) { return null; } }; let live = await probe(); if (live && live.webSocketDebuggerUrl) { const killed = await killProfileOrphans(o.profileDir, { log: o.log }); if (killed) { o.log('[预检] 已清理 ' + killed + ' 个占用本 profile 的残留进程,重新检测端口…'); await new Promise((r) => setTimeout(r, 1200)); live = await probe(); } } if (live && live.webSocketDebuggerUrl) { const hint = portInUseHint(o.port); fail([ 'CDP 端口 ' + o.port + ' 已被占用(' + (live.Browser || '未知浏览器') + ')。', '先结束占用该端口的进程:' + hint[0], hint[1], '或换端口重跑:--port ' + (o.port + 1) + ' / YUN_XUE_TANG_CDP_PORT=' + (o.port + 1), ]); } return { browser, scriptSrc: fs.readFileSync(o.scriptFile, 'utf8') }; }