@coboot/headless 0.2.2-rc.3 → 0.2.2-rc.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.
- package/README.md +25 -14
- package/cli.mjs +10 -10
- package/entry.mjs +270 -269
- package/launcher.mjs +4 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# Coboot Headless
|
|
2
2
|
|
|
3
|
-
Node.js
|
|
3
|
+
Node.js 18+ 的无 GUI 后台运行包。本包不携带浏览器二进制,也不会自动下载浏览器。
|
|
4
|
+
Headless 业务运行时要求 Node.js 20+;当入口检测到 Node.js 18/19 时,会通过 `npx` 临时下载并使用
|
|
5
|
+
隔离的 Node.js 22 LTS,不修改系统 Node.js。Node.js 20+ 环境直接使用当前 Node,不产生额外下载。
|
|
4
6
|
首次启动只需一次性设备 token:
|
|
5
7
|
|
|
6
8
|
```bash
|
|
7
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
9
|
+
npx -y @coboot/headless@0.2.2-rc.4 '<device-token>'
|
|
8
10
|
```
|
|
9
11
|
|
|
10
12
|
命令会把运行时安装到 `/data/coboot/.siboot`,在客户端鉴权和运行时初始化完成后启动
|
|
@@ -12,14 +14,23 @@ detached daemon 并退出。不需要设置 `BACKEND_URL`、`CLIENT_UID`。终
|
|
|
12
14
|
目标机安装 Chromium。
|
|
13
15
|
|
|
14
16
|
```bash
|
|
15
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
16
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
17
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
18
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
17
|
+
npx -y @coboot/headless@0.2.2-rc.4 status
|
|
18
|
+
npx -y @coboot/headless@0.2.2-rc.4 logs
|
|
19
|
+
npx -y @coboot/headless@0.2.2-rc.4 restart
|
|
20
|
+
npx -y @coboot/headless@0.2.2-rc.4 stop
|
|
19
21
|
```
|
|
20
22
|
|
|
23
|
+
更新到 npm `latest` 并重启:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npx -y @coboot/headless@latest update
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`update` 会先完成 Node.js 版本、设备凭证和新包完整性预检,再停止旧进程并切换版本;新版本启动失败时
|
|
30
|
+
会自动恢复并重启旧版本。`restart` 只重启已经安装的版本,不再隐式替换运行时。
|
|
31
|
+
|
|
21
32
|
默认数据根目录可通过 `SIBOOT_DATA_ROOT` 修改。该设置不会写入配置,首次安装以及后续的
|
|
22
|
-
`status`、`logs`、`restart`、`stop` 都必须传入同一个值,否则 CLI 会操作 edition 默认目录。
|
|
33
|
+
`status`、`logs`、`restart`、`update`、`stop` 都必须传入同一个值,否则 CLI 会操作 edition 默认目录。
|
|
23
34
|
|
|
24
35
|
|
|
25
36
|
## Coboot 浏览器
|
|
@@ -30,13 +41,13 @@ Coboot daemon 默认禁用 updater 和浏览器预热,第一次调用 `browser
|
|
|
30
41
|
|
|
31
42
|
```bash
|
|
32
43
|
HEADLESS_BROWSER_EXECUTABLE=/absolute/path/to/chrome \
|
|
33
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
44
|
+
npx -y @coboot/headless@0.2.2-rc.4 '<device-token>'
|
|
34
45
|
```
|
|
35
46
|
|
|
36
47
|
需要安装浏览器时,由用户执行:
|
|
37
48
|
|
|
38
49
|
```bash
|
|
39
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
50
|
+
npx -y @coboot/headless@0.2.2-rc.4 install-browser
|
|
40
51
|
```
|
|
41
52
|
|
|
42
53
|
该命令不需要设备 token,也不要求 root。它会先检查现有浏览器并执行启动验证;浏览器已经可用时
|
|
@@ -74,7 +85,7 @@ dnf/yum 先试仓库中的 `chromium`,x86_64 回退到官方 RPM 并关闭 wea
|
|
|
74
85
|
已有设备凭证无效或需要切换设备身份时,显式重新注册:
|
|
75
86
|
|
|
76
87
|
```bash
|
|
77
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
88
|
+
npx -y @coboot/headless@0.2.2-rc.4 --force-enroll '<device-token>'
|
|
78
89
|
```
|
|
79
90
|
|
|
80
91
|
命令会先停止 daemon 并临时备份旧凭证;新凭证和客户端均就绪后才完成替换,失败时恢复旧凭证。
|
|
@@ -111,7 +122,7 @@ HTTP 与 WebSocket 必须同域并与设备 token 匹配:
|
|
|
111
122
|
HEADLESS_ALLOW_ENDPOINT_OVERRIDE=1 \
|
|
112
123
|
BACKEND_URL=https://example.com \
|
|
113
124
|
WS_URL=wss://example.com/wsschat \
|
|
114
|
-
npx -y @coboot/headless@0.2.2-rc.
|
|
125
|
+
npx -y @coboot/headless@0.2.2-rc.4 '<device-token>'
|
|
115
126
|
```
|
|
116
127
|
|
|
117
128
|
WebSocket 覆盖变量名是 `WS_URL`,不是 `WSS_URL`。
|
|
@@ -125,7 +136,7 @@ endpoint 覆盖同样不会写入配置;后续执行 `restart` 时必须再次
|
|
|
125
136
|
CU_EDITION=coboot \
|
|
126
137
|
PKG_NAME=@coboot/headless \
|
|
127
138
|
PKG_BIN_NAME=coboot-headless \
|
|
128
|
-
PKG_VERSION=0.2.2-rc.
|
|
139
|
+
PKG_VERSION=0.2.2-rc.4 \
|
|
129
140
|
pnpm build:headless:npm
|
|
130
141
|
```
|
|
131
142
|
|
|
@@ -133,8 +144,8 @@ pnpm build:headless:npm
|
|
|
133
144
|
|
|
134
145
|
```bash
|
|
135
146
|
npm pack --dry-run --json ./dist-headless
|
|
136
|
-
tar -xOf dist-headless/coboot-headless-0.2.2-rc.
|
|
137
|
-
sha256sum dist-headless/coboot-headless-0.2.2-rc.
|
|
147
|
+
tar -xOf dist-headless/coboot-headless-0.2.2-rc.4.tgz package/package.json
|
|
148
|
+
sha256sum dist-headless/coboot-headless-0.2.2-rc.4.tgz
|
|
138
149
|
```
|
|
139
150
|
|
|
140
151
|
|
package/cli.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{execFileSync as
|
|
2
|
+
import{execFileSync as Me,spawn as q}from"child_process";import m from"fs";import i from"path";import{fileURLToPath as Ue}from"url";var oe=new Set(["help","install-browser","start","stop","restart","update","status","logs"]);function F(e="siboot-headless",r="/data/siboot",o=!1){let n=o?` ${e} install-browser
|
|
3
3
|
`:"";console.log(`Usage:
|
|
4
4
|
${e} <device-token>
|
|
5
5
|
${e} --force-enroll <device-token>
|
|
6
|
-
${
|
|
6
|
+
${n} ${e} start|stop|restart|update|status|logs
|
|
7
7
|
|
|
8
8
|
Environment:
|
|
9
|
-
SIBOOT_DATA_ROOT Persistent data root (default: ${
|
|
10
|
-
${
|
|
11
|
-
`)}`)}}function
|
|
12
|
-
\u8BF7\u786E\u8BA4\u5F53\u524D\u7528\u6237\u5BF9 SIBOOT_DATA_ROOT \u6709\u5199\u6743\u9650\uFF0C\u6216\u7528\u8BE5\u76EE\u5F55\u7684\u5C5E\u4E3B\u8D26\u53F7\u91CD\u65B0\u6267\u884C\u3002`)}return console.log(`Downloading Chromium into ${t}`),
|
|
13
|
-
`))}async function
|
|
14
|
-
`))}async function
|
|
15
|
-
`)),s}import w from"fs";var
|
|
16
|
-
`,256),
|
|
9
|
+
SIBOOT_DATA_ROOT Persistent data root (default: ${r})`)}function I(e){let r=e[0]?.trim()||"";if(!r||r==="--help"||r==="-h"||r==="help")return{command:"help"};if(r==="--force-enroll"){let o=e[1]?.trim()||"";if(!o||e.length!==2)throw new Error("--force-enroll requires one device token");return{command:"install",token:o,forceEnroll:!0}}return oe.has(r)?{command:r}:{command:"install",token:r}}import{execFileSync as W}from"child_process";import{createRequire as te}from"module";import p from"fs";import se from"os";import f from"path";var ie=["/usr/local/bin/google-chrome","/usr/local/bin/chromium","/usr/local/bin/chromium-browser","/usr/bin/google-chrome","/usr/bin/google-chrome-stable","/usr/bin/chromium","/usr/bin/chromium-browser"],ae="https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm",ce="https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb",le=[/error while loading shared libraries:\s*([^\s:]+)/gi,/([A-Za-z0-9_.+-]+\.so(?:\.[0-9A-Za-z.]+)?):\s*cannot open shared object file/gi,/Missing libraries:\s*([^]*?)(?:\n\s*\n|$)/gi],de=[/error while loading shared libraries:/i,/cannot open shared object file/i,/host system is missing dependencies/i,/missing libraries:/i];function B(e,r=process.env){let o=r.PLAYWRIGHT_BROWSERS_PATH?.trim();return o||f.join(e,"browsers")}function H(e){try{return p.accessSync(e,p.constants.X_OK),!0}catch{return!1}}function $(e){for(let r of(process.env.PATH||"").split(f.delimiter)){if(!r)continue;let o=f.join(r,e);if(H(o))return o}return null}function M(){let e=process.env.HEADLESS_BROWSER_EXECUTABLE?.trim();if(e&&H(e))return e;for(let r of ie)if(H(r))return r;return $("google-chrome")||$("google-chrome-stable")||$("chromium")}function ue(e){return de.some(r=>r.test(e))}function me(e){let r=new Set;for(let o of le){o.lastIndex=0;for(let n of e.matchAll(o))for(let t of n[1].split(/[\s,]+/)){let s=t.trim().replace(/[.,:;]+$/,"");/\.so(?:\.[0-9A-Za-z.]+)?$/.test(s)&&r.add(s)}}return[...r]}function fe(e={arch:process.arch},r=$){let o=e.arch==="x64";if(r("apt-get"))return[{command:"apt-get",args:["update"],optional:!0},{attempts:[{command:"apt-get",args:["install","-y","--no-install-recommends","chromium"]},{command:"apt-get",args:["install","-y","--no-install-recommends","chromium-browser"]},...o?[{download:ce,command:"apt-get",args:["install","-y","--no-install-recommends"]}]:[]]}];if(r("apk"))return[{attempts:[{command:"apk",args:["add","--no-cache","chromium"]},{command:"apk",args:["add","--no-cache","--repository=https://dl-cdn.alpinelinux.org/alpine/edge/community","chromium"]}]}];let n=r("dnf")?"dnf":r("yum")?"yum":"";if(n){let t=[{command:n,args:["install","-y","--setopt=install_weak_deps=False","chromium"]}];return o&&t.push({command:n,args:["install","-y","--setopt=install_weak_deps=False",ae]}),[{attempts:t}]}throw new Error("unsupported Linux package manager; install system Chrome/Chromium manually and set HEADLESS_BROWSER_EXECUTABLE")}async function pe(e){let r=p.mkdtempSync(f.join(se.tmpdir(),"coboot-browser-")),o=f.join(r,f.basename(new URL(e).pathname)||"browser-package");console.log(`Downloading: ${e}`);let n=await fetch(e);if(!n.ok)throw new Error(`download failed with HTTP ${n.status}: ${e}`);return p.writeFileSync(o,Buffer.from(await n.arrayBuffer())),{file:o,directory:r}}async function U(e){let r=e.download?await pe(e.download):null,o=r?[...e.args,r.file]:e.args;try{console.log(`Running: ${e.command} ${o.join(" ")}`),W(e.command,o,{stdio:"inherit",env:{...process.env,DEBIAN_FRONTEND:"noninteractive"}})}finally{r&&p.rmSync(r.directory,{recursive:!0,force:!0})}}function _(e){return e instanceof Error?e.message:String(e)}async function we(){for(let e of fe()){if(!e.attempts){try{await U(e)}catch(n){if(!e.optional)throw n;console.warn(`Skipping optional step ${e.command}: ${_(n)}`)}continue}let r=[],o=!1;for(let n of e.attempts)try{await U(n),o=!0;break}catch(t){let s=n.download||n.args[n.args.length-1];r.push(`${s}: ${_(t)}`),console.warn(`Browser package candidate failed (${s}); trying the next one`)}if(!o)throw new Error(`no browser package could be installed:
|
|
10
|
+
${r.join(`
|
|
11
|
+
`)}`)}}function V(e){let r=f.join(e,"browser-runtime","playwright-core"),o=te(f.join(e,"package.json"));return{driverRoot:r,driver:o(r)}}async function he(e){let{driverRoot:r,driver:o}=V(e),n=f.join(r,"cli.js"),t=process.env.PLAYWRIGHT_BROWSERS_PATH;try{p.mkdirSync(t,{recursive:!0,mode:448})}catch(s){throw new Error(`\u65E0\u6CD5\u521B\u5EFA\u6D4F\u89C8\u5668\u76EE\u5F55 ${t}: ${_(s)}
|
|
12
|
+
\u8BF7\u786E\u8BA4\u5F53\u524D\u7528\u6237\u5BF9 SIBOOT_DATA_ROOT \u6709\u5199\u6743\u9650\uFF0C\u6216\u7528\u8BE5\u76EE\u5F55\u7684\u5C5E\u4E3B\u8D26\u53F7\u91CD\u65B0\u6267\u884C\u3002`)}return console.log(`Downloading Chromium into ${t}`),W(process.execPath,[n,"install","chromium","--no-shell"],{stdio:"inherit"}),o.chromium.executablePath()}async function Ee(e,r){let{driver:o}=V(e),n=await o.chromium.launch({executablePath:r,headless:!0,args:["--no-sandbox","--disable-gpu","--disable-dev-shm-usage"]});try{let t=await n.newPage();if(await t.setContent("<title>Headless browser ready</title>"),await t.title()!=="Headless browser ready")throw new Error("browser smoke test returned wrong title");return n.version()}finally{await n.close()}}function Se(e,r){let o=me(r),n=o.length?`\u7F3A\u5C11\u7CFB\u7EDF\u52A8\u6001\u5E93: ${o.join(", ")}`:"\u7F3A\u5C11\u7CFB\u7EDF\u4F9D\u8D56\u5E93";return new Error([`${e} \u65E0\u6CD5\u542F\u52A8\uFF0C${n}\u3002`,"\u8865\u9F50\u7CFB\u7EDF\u4F9D\u8D56\u9700\u8981 root \u6743\u9650\uFF0C\u8BF7\u7528 sudo \u91CD\u65B0\u6267\u884C\u540C\u4E00\u6761 install-browser \u547D\u4EE4\uFF0C\u6216\u8BA9\u7BA1\u7406\u5458\u5B89\u88C5\u4E0A\u8FF0\u5E93\u3002",`\u539F\u59CB\u9519\u8BEF: ${r}`].join(`
|
|
13
|
+
`))}async function N(e,r){console.log(`Browser executable: ${r}`);try{let o=await Ee(e,r);return console.log(`Browser smoke test passed: ${o}`),{executablePath:r,version:o}}catch(o){let n=_(o);throw ue(n)?Se(r,n):o}}function ge(e){try{let r=f.join(e.run,"daemon.pid"),o=Number.parseInt(p.readFileSync(r,"utf8").trim(),10);return!Number.isInteger(o)||o<=0?null:p.statSync(`/proc/${o}`).uid}catch{return null}}function be(e,r){let o=ge(e),n=process.getuid?.();o===null||n===void 0||o===n||console.warn([`\u6CE8\u610F: daemon \u6B63\u4EE5 uid=${o} \u8FD0\u884C\uFF0C\u800C\u6D4F\u89C8\u5668\u88C5\u5728 uid=${n} \u540D\u4E0B\u7684 ${r}\u3002`,"daemon \u53EF\u80FD\u65E0\u6743\u8BFB\u53D6\u8BE5\u76EE\u5F55\u3002\u8BF7\u6539\u7528 daemon \u6240\u5C5E\u8D26\u53F7\u91CD\u65B0\u6267\u884C\u672C\u547D\u4EE4\uFF0C","\u6216\u4EE5 root \u6267\u884C\u8BA9\u5305\u7BA1\u7406\u5668\u628A\u6D4F\u89C8\u5668\u88C5\u5230\u7CFB\u7EDF\u8DEF\u5F84\u3002"].join(`
|
|
14
|
+
`))}async function J(e,r){if(process.platform!=="linux")throw new Error("install-browser currently supports Linux only");let o=B(r.home);process.env.PLAYWRIGHT_BROWSERS_PATH=o;let n=M();if(n)try{return await N(e,n)}catch(a){console.warn(`Existing browser failed the smoke test: ${_(a)}`)}if(process.getuid?.()===0)try{await we();let a=M();if(a)return await N(e,a);console.warn("\u5305\u7BA1\u7406\u5668\u6267\u884C\u5B8C\u6210\u4F46\u6CA1\u6709\u627E\u5230\u6D4F\u89C8\u5668\uFF0C\u56DE\u9000\u5230\u4E0B\u8F7D\u5B89\u88C5")}catch(a){console.warn(`\u7CFB\u7EDF\u5305\u7BA1\u7406\u5668\u5B89\u88C5\u5931\u8D25\uFF0C\u56DE\u9000\u5230\u4E0B\u8F7D\u5B89\u88C5: ${_(a)}`)}let t=await he(e),s=await N(e,t);return console.log(`\u6D4F\u89C8\u5668\u5DF2\u88C5\u5165 ${o}\uFF0Cdaemon \u542F\u52A8\u65F6\u4F1A\u6307\u5411\u540C\u4E00\u76EE\u5F55\uFF0C\u65E0\u9700\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF\u3002`),be(r,o),n&&console.warn([`\u6CE8\u610F: ${n} \u4ECD\u7136\u5B58\u5728\u4E14\u4F1A\u88AB\u4F18\u5148\u4F7F\u7528\uFF0C\u4F46\u5B83\u65E0\u6CD5\u542F\u52A8\u3002`,`\u8BF7\u7528 HEADLESS_BROWSER_EXECUTABLE=${t} \u91CD\u65B0\u542F\u52A8 daemon\uFF0C\u6216\u4EE5 root \u4FEE\u590D\u8BE5\u7CFB\u7EDF\u6D4F\u89C8\u5668\u3002`].join(`
|
|
15
|
+
`)),s}import w from"fs";var _e=e=>new Promise(r=>setTimeout(r,e));function G(e){if(!Number.isInteger(e)||e<=0)return!1;try{return process.kill(e,0),!0}catch{return!1}}function X(e){try{return JSON.parse(w.readFileSync(e,"utf8"))}catch{return null}}function ye(e){let r=X(e);return!!(r?.deviceId?.trim()&&r?.credentialId?.trim()&&r?.ownerUid?.trim()&&r?.deviceName?.trim()&&r?.deviceType>0&&r?.publicKey?.trim()&&r?.privateKeyPem?.trim())}function ve(e){return[e.credential,e.authSession]}function Re(e){for(let r of e.entries){try{w.unlinkSync(r.file)}catch{}r.existed&&w.renameSync(r.backup,r.file)}}function Ae(e){let r=`${process.pid}-${Date.now()}`,o=ve(e).map(t=>({file:t,backup:`${t}.force-enroll-${r}.bak`,existed:w.existsSync(t)})),n=[];try{for(let t of o)t.existed&&(w.renameSync(t.file,t.backup),n.push(t))}catch(t){for(let s of n.reverse())w.renameSync(s.backup,s.file);throw t}return{entries:o}}function $e(e){for(let r of e.entries)r.existed&&w.unlinkSync(r.backup)}function De(e){Re(e)}async function je(e,r){let o=Date.now()+r,n=0;for(;Date.now()<o;){let t=X(`${e.run}/status.json`);if(ye(e.credential)&&t?.client_ready===!0&&G(t.client_pid)){if(n||=Date.now(),Date.now()-n>=1e3)return}else n=0;let s=Number.parseInt(w.readFileSync(`${e.run}/daemon.pid`,"utf8"),10);if(t?.client_exit_code!=null&&!t?.client_pid)throw new Error(`client exited during force enrollment with code ${t.client_exit_code}`);if(s&&!G(s))throw new Error("daemon exited during force enrollment");await _e(100)}throw new Error(`force enrollment did not become ready within ${r}ms`)}async function Y(e,r,o,n={}){let t=Ae(e);try{let s=await r();return await je(e,n.timeoutMs||15e3),$e(t),s}catch(s){await o().catch(()=>{});try{De(t)}catch(a){throw new AggregateError([s,a],"force enrollment and rollback failed")}throw s}}import{createHash as xe}from"crypto";import u from"fs";import d from"path";var Oe=new Set(["siboot","coboot"]);function P(e){for(let r of[e.home,e.releases,e.scripts,e.run,e.workspace,d.join(e.logs,"daemon"),d.join(e.logs,"client"),d.join(e.logs,"update")])u.mkdirSync(r,{recursive:!0,mode:448})}function D(e){let r=JSON.parse(u.readFileSync(d.join(e,"package.json"),"utf8")),o=String(r.name||"").trim();if(!/^(?:@[a-z0-9._-]+\/)?[a-z0-9._-]+$/i.test(o))throw new Error("package name is missing or invalid");let n=String(r.version||"").trim();if(!n||!/^[0-9A-Za-z._-]+$/.test(n))throw new Error("package version is missing or invalid");let t=String(r.headlessEdition||"").trim();if(!Oe.has(t))throw new Error("package headlessEdition must be siboot or coboot");let s=r.headlessLinuxSandbox??null;ke(t,s);let a=r.headlessBrowserRuntime??null;return Le(t,a),{packageName:o,version:n,edition:t,linuxSandbox:s,browserRuntime:a}}function ke(e,r){if(e==="siboot"&&r)throw new Error("Siboot package must not include Linux sandbox metadata");if(e==="coboot"&&(r?.backend!=="bubblewrap"||!Array.isArray(r.architectures)||r.architectures.length===0))throw new Error("Coboot package is missing bundled Linux sandbox metadata")}function Le(e,r){if(e==="siboot"&&r)throw new Error("Siboot package must not include guided browser runtime metadata");if(e==="coboot"&&(r?.driver!=="playwright-core"||!/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(r.driverVersion||"")||r?.browser!=="system-or-guided-install"))throw new Error("Coboot package is missing valid guided browser runtime metadata")}function Te(e,r){let o=d.relative(e.home,r),n=`${e.current}.next-${process.pid}`;u.rmSync(n,{force:!0}),u.symlinkSync(o,n),u.renameSync(n,e.current)}function j(e){try{let r=u.readlinkSync(e.current);return d.resolve(d.dirname(e.current),r)}catch{try{return u.realpathSync(e.current)}catch{return null}}}function g(e,r,o){let n=`${r}.next-${process.pid}`;u.copyFileSync(e,n),u.chmodSync(n,o),u.renameSync(n,r)}function Ie(e,r,o){let n=`${e}.next-${process.pid}`;u.writeFileSync(n,r,{mode:o}),u.renameSync(n,e)}function Ne(e){return xe("sha256").update(u.readFileSync(e)).digest("hex")}function He(e,r){if(!u.existsSync(e)||Ne(e)!==r)throw new Error(`bundled Linux sandbox file failed integrity check: ${d.basename(e)}`)}function Be(e,r,o,n){if(!o)return null;if(n.platform!=="linux")throw new Error(`Coboot Headless bundled sandbox supports Linux only: ${n.platform}`);if(!o.architectures.includes(n.arch))throw new Error(`Coboot Headless does not include Linux sandbox for arch: ${n.arch}`);let t=d.join(e,"native",`linux-${n.arch}`),s=d.join(t,"manifest.json");if(!u.existsSync(s))throw new Error(`package is missing Linux sandbox bundle for arch: ${n.arch}`);let a=JSON.parse(u.readFileSync(s,"utf8"));if(a.backend!==o.backend||a.backendVersion!==o.backendVersion||a.arch!==n.arch)throw new Error("bundled Linux sandbox manifest does not match package metadata");let l={bwrap:d.join(t,"bwrap"),loader:d.join(t,"ld-musl"),libcap:d.join(t,"lib","libcap.so.2")};for(let[E,S]of Object.entries(l))He(S,a.files?.[E]);let c=d.join(r,"native");return u.mkdirSync(d.join(c,"lib"),{recursive:!0,mode:448}),g(l.bwrap,d.join(c,"bwrap"),320),g(l.loader,d.join(c,"ld-musl"),320),g(l.libcap,d.join(c,"lib","libcap.so.2"),256),g(s,d.join(c,"manifest.json"),256),{backend:a.backend,version:a.backendVersion,arch:a.arch,bundled:!0}}function Pe(e,r,o){let n=d.join(e,"browser-runtime","playwright-core"),t=d.join(n,"package.json");if(!u.existsSync(t))throw new Error("package is missing bundled playwright-core");let s=JSON.parse(u.readFileSync(t,"utf8"));if(s.name!==o.driver||s.version!==o.driverVersion)throw new Error("bundled playwright-core does not match package metadata");let a=d.join(r,"node_modules","playwright-core"),l=`${a}.next-${process.pid}`;return u.rmSync(l,{recursive:!0,force:!0}),u.mkdirSync(d.dirname(a),{recursive:!0,mode:448}),u.cpSync(n,l,{recursive:!0}),u.rmSync(a,{recursive:!0,force:!0}),u.renameSync(l,a),o}function x(e,r,o={platform:process.platform,arch:process.arch}){P(r);let{version:n,edition:t,linuxSandbox:s,browserRuntime:a}=D(e);if(t!==r.edition)throw new Error(`package edition ${t} does not match runtime layout ${r.edition}`);let l=d.join(r.releases,n);if(j(r)===l)return{version:n,releaseDir:l,reused:!0};let c=d.join(r.releases,`.staging-${n}-${process.pid}`);u.rmSync(c,{recursive:!0,force:!0}),u.mkdirSync(c,{recursive:!0,mode:448});try{Ce(e,c,{version:n,edition:t,linuxSandbox:s,browserRuntime:a},o),u.rmSync(l,{recursive:!0,force:!0}),u.renameSync(c,l)}catch(E){throw u.rmSync(c,{recursive:!0,force:!0}),E}return{version:n,releaseDir:l,reused:!1}}function Ce(e,r,o,n){let t=d.join(e,"entry.mjs"),s=d.join(e,"daemon.mjs");if(!u.existsSync(t)||!u.existsSync(s))throw new Error("package is missing entry.mjs or daemon.mjs");g(t,d.join(r,"entry.mjs"),320),g(s,d.join(r,"daemon.mjs"),320);let a=Be(e,r,o.linuxSandbox,n),l=o.browserRuntime?Pe(e,r,o.browserRuntime):"disabled";Ie(d.join(r,"manifest.json"),`${JSON.stringify({type:"headless-npm-daemon",version:o.version,edition:o.edition,browserRuntime:l,linuxSandbox:a},null,2)}
|
|
16
|
+
`,256)}function y(e,r){Te(e,r);let o=d.join(e.scripts,"daemon.mjs");u.existsSync(o)||g(d.join(r,"daemon.mjs"),o,320)}function Fe(e,r,o={platform:process.platform,arch:process.arch}){let n=x(e,r,o);return y(r,n.releaseDir),n}var Q=Ue(import.meta.url),ee={siboot:"/data/siboot",coboot:"/data/coboot"},We=15e3,K=7e3,Ve=73,z=20,Je=["BACKEND_URL","WS_URL","SIBOOT_URL","MCP_CREDENTIAL_API_BASE_URL","SKILL_CREDENTIAL_API_BASE_URL"],Ge=["HEADLESS_BWRAP_EXECUTABLE","HEADLESS_BWRAP_LOADER","HEADLESS_BWRAP_LIBRARY_DIR"];function Xe(e,r){let o=e||ee[r],n=i.resolve(o);if(!i.isAbsolute(o)||n===i.parse(n).root)throw new Error(`SIBOOT_DATA_ROOT must be a non-root absolute path: ${e}`);return n}function Ye(e=process.env.SIBOOT_DATA_ROOT,r="siboot"){if(!(r in ee))throw new Error(`unsupported headless edition: ${r}`);let o=Xe(e,r),n=i.join(o,".siboot");return{edition:r,root:o,home:n,releases:i.join(n,"releases"),current:i.join(n,"current"),scripts:i.join(n,"scripts"),run:i.join(n,"run"),logs:i.join(n,"logs"),workspace:i.join(o,"workspace"),credential:i.join(n,"headless-device-credential.json"),authSession:i.join(n,"headless-auth-session.json")}}function Ke(e,r){return`npx -y '${e}@${r}' install-browser`}function R(e){try{let r=Number.parseInt(m.readFileSync(e,"utf8").trim(),10);return Number.isInteger(r)&&r>0?r:null}catch{return null}}function h(e,r=process.platform==="linux"?"/proc":""){if(!e)return!1;try{process.kill(e,0)}catch{return!1}if(!r)return!0;try{let o=m.readFileSync(i.join(r,String(e),"stat"),"utf8"),n=o.lastIndexOf(") ")+2;return n<2||o[n]!=="Z"}catch{return!0}}function re(e,r){let o="";try{o=m.readFileSync(`/proc/${e}/cmdline`,"utf8").replaceAll("\0"," ").trim()}catch{}if(!o)try{o=Me("ps",["-ww","-p",String(e),"-o","command="],{encoding:"utf8"}).trim()}catch{return!1}return r.some(n=>[n,C(n)].some(t=>o.includes(t)))}var O=e=>new Promise(r=>setTimeout(r,e));function L(e=process.versions.node){let r=Number.parseInt(String(e).split(".")[0],10);if(!Number.isInteger(r)||r<z)throw new Error(`Node.js >=${z} is required; current version is ${e}`)}async function Z(e,r,o){let n=R(e);if(!h(n)){try{m.unlinkSync(e)}catch{}return!1}if(!re(n,r))throw new Error(`refusing to stop unrelated process from stale ${i.basename(e)}: ${n}`);process.kill(n,"SIGTERM");let t=Date.now()+K;for(;Date.now()<t&&h(n);)await O(100);if(h(n))throw new Error(`${o} did not stop within ${K/1e3} seconds: ${n}`);return R(e)===n&&m.rmSync(e,{force:!0}),!0}async function b(e){let r=await Z(i.join(e.run,"daemon.pid"),[i.join(e.current,"daemon.mjs"),i.join(e.scripts,"daemon.mjs")],"daemon"),o=await Z(i.join(e.run,"node.pid"),[i.join(e.current,"entry.mjs")],"client");return r||o}function ze(e,r,o=process.env){let n={...o,CU_EDITION:e.edition,NO_ELECTRON:"1",CLIENT_TYPE:"anydev",SIBOOT_BASE_DIR:e.home,SIBOOT_DATA_ROOT:e.root,HEADLESS_DATA_DIR:e.root,SIBOOT_DATA_DIR:e.root,HEADLESS_DEVICE_CREDENTIAL_PATH:e.credential,HEADLESS_AUTH_SESSION_PATH:e.authSession,HEADLESS_DEVICE_REQUIRE_AUTH:"1",HEADLESS_PREWARM_DEFAULT_SESSION:"0",WORKSPACE:e.workspace,ANYDEV_UPDATE_ENABLED:"0"};r?n.HEADLESS_DEVICE_TOKEN=r:delete n.HEADLESS_DEVICE_TOKEN,delete n.CLIENT_UID,delete n.HEADLESS_DEVICE_FORCE_ENROLL;for(let t of Ge)delete n[t];if(e.edition==="coboot"){let t=i.join(e.current,"native");n.HEADLESS_BWRAP_EXECUTABLE=i.join(t,"bwrap"),n.HEADLESS_BWRAP_LOADER=i.join(t,"ld-musl"),n.HEADLESS_BWRAP_LIBRARY_DIR=i.join(t,"lib"),n.HEADLESS_BROWSER_RUNTIME="system-or-guided-install",n.PLAYWRIGHT_BROWSERS_PATH=B(e.home,o)}else delete n.HEADLESS_BROWSER_RUNTIME,delete n.HEADLESS_BROWSER_INSTALL_COMMAND;if(n.HEADLESS_ALLOW_ENDPOINT_OVERRIDE!=="1")for(let t of Je)delete n[t];return n}async function Ze(e,r=We){let o=Date.now()+r,n=!1;for(;Date.now()<o;){let t=R(i.join(e.run,"daemon.pid"));if(!h(t))throw new Error("daemon exited during client startup");let s=null;try{s=JSON.parse(m.readFileSync(i.join(e.run,"status.json"),"utf8"))}catch{}if(s?.client_ready===!0&&h(s.client_pid))return;if(s?.client_exit_code!=null&&!s?.client_pid){if(s.client_exit_code===Ve){n=!0,await O(100);continue}throw new Error(`client exited during startup with code ${s.client_exit_code}`)}await O(100)}throw n?new Error("another Headless Driver still holds the machine lock after startup retries"):new Error(`client did not become ready within ${r}ms`)}function T(e,r=""){let o=e.edition==="coboot"?"coboot-headless":"siboot-headless";if(!m.existsSync(e.credential)&&!r)throw new Error(`device credential is missing; run ${o} <device-token> first`);let n=m.existsSync(i.join(e.current,"daemon.mjs"))?i.join(e.current,"daemon.mjs"):i.join(e.scripts,"daemon.mjs"),t=m.existsSync(i.join(e.current,"entry.mjs")),s=m.existsSync(n);if(!t||!s)throw new Error(`runtime is not installed (entry=${t}, daemon=${s}); run ${o} <device-token> first`);return n}async function A(e,r="",o=process.env){P(e);let n=T(e,r),t=m.existsSync(e.credential)?"":r,s=i.join(e.logs,"daemon",`${new Date().toISOString().slice(0,10)}.log`),a=m.openSync(s,"a",384);q(process.execPath,[n],{detached:!0,env:ze(e,t,o),stdio:["ignore",a,a]}).unref(),m.closeSync(a),await O(500);let c=R(i.join(e.run,"daemon.pid"));if(!h(c))throw new Error(`daemon failed to start; check ${s}`);return await Ze(e),{pid:c,logFile:s}}function k(e){try{let r=JSON.parse(m.readFileSync(i.join(e.current,"manifest.json"),"utf8"));return typeof r.version=="string"?r.version:null}catch{return null}}async function qe(e,r={}){return L(r.nodeVersion),T(e),await b(e),A(e,"",r.baseEnv||process.env)}async function ne(e,r,o={}){L(o.nodeVersion);let n=o.token||"";if(!m.existsSync(r.credential)&&!n){let l=r.edition==="coboot"?"coboot-headless":"siboot-headless";throw new Error(`device credential is missing; run ${l} <device-token> first`)}let t=j(r),s=k(r),a=x(e,r,o.runtime||{platform:process.platform,arch:process.arch});await b(r),y(r,a.releaseDir);try{let l=await A(r,n,o.baseEnv||process.env);return{previousVersion:s,installed:a,started:l}}catch(l){if(await b(r).catch(()=>{}),!t)throw l;y(r,t);try{await A(r,"",o.baseEnv||process.env)}catch(c){throw new AggregateError([l,c],`headless update failed and the previous runtime could not be restarted: ${c instanceof Error?c.message:String(c)}`)}throw new Error(`headless update failed; restored ${s||"the previous version"}`,{cause:l})}}async function Qe(e,r,o,n={}){return ne(e,r,{...n,token:o})}async function er(e,r,o={}){return T(r),ne(e,r,o)}function rr(e){let r=R(i.join(e.run,"daemon.pid")),o=null;try{o=JSON.parse(m.readFileSync(i.join(e.run,"status.json"),"utf8"))}catch{}console.log(JSON.stringify({running:h(r),pid:r,current:m.existsSync(e.current),status:o},null,2))}async function nr(e){let r=i.join(e.logs,"daemon",`${new Date().toISOString().slice(0,10)}.log`);if(!m.existsSync(r))throw new Error(`log file not found: ${r}`);let o=q("tail",["-n","100","-f",r],{stdio:"inherit"});await new Promise((n,t)=>{o.once("error",t),o.once("exit",s=>s===0?n():t(new Error(`tail exited ${s}`)))})}function v(e,r=e.version){let o={...process.env};return e.browserRuntime&&(o.HEADLESS_BROWSER_INSTALL_COMMAND=Ke(e.packageName,r)),o}async function or(e,r,o,n){L();let t=j(o),s=k(o),a=x(e,o);await b(o);try{return await Y(o,async()=>{y(o,a.releaseDir);let l=await A(o,n,v(r));return{installed:a,started:l}},()=>b(o))}catch(l){if(!t)throw l;y(o,t);try{await A(o,"",v(r,s||r.version))}catch(c){throw new AggregateError([l,c],`force enrollment failed and the previous runtime could not be restarted: ${c instanceof Error?c.message:String(c)}`)}throw l}}async function tr(){let e=I(process.argv.slice(2)),r=i.basename(process.argv[1]||"siboot-headless"),o=i.dirname(Q),n=D(o),t=Ye(process.env.SIBOOT_DATA_ROOT,n.edition);if(e.command==="help")return F(r,t.root,!!n.browserRuntime);if(e.command==="install-browser"){if(!n.browserRuntime)throw new Error("install-browser is available in the Coboot headless npm package only");return J(o,t)}let s=n.edition==="coboot"?"Coboot":"Siboot";if(e.command==="status")return rr(t);if(e.command==="logs")return nr(t);if(e.command==="stop"){let c=await b(t);console.log(c?`${s} Headless stopped`:`${s} Headless is not running`);return}if(e.command==="start"){L(),T(t);let c=R(i.join(t.run,"daemon.pid")),E=[i.join(t.current,"daemon.mjs"),i.join(t.scripts,"daemon.mjs")];if(h(c)&&re(c,E)){console.log(`${s} Headless is already running (pid=${c})`);return}await b(t);let S=await A(t,"",v(n,k(t)||n.version));console.log(`${s} Headless started in background (pid=${S.pid})`),console.log(`Logs: ${S.logFile}`);return}if(e.command==="restart"){let c=await qe(t,{baseEnv:v(n,k(t)||n.version)});console.log(`${s} Headless restarted in background (pid=${c.pid})`),console.log(`Logs: ${c.logFile}`);return}if(e.command==="update"){let{previousVersion:c,installed:E,started:S}=await er(o,t,{baseEnv:v(n)});console.log(`${s} Headless updated ${c||"unknown"} -> ${E.version} (pid=${S.pid})`),console.log(`Logs: ${S.logFile}`);return}let{installed:a,started:l}=e.forceEnroll?await or(o,n,t,e.token):await Qe(o,t,e.token||"",{baseEnv:v(n)});console.log(`${s} Headless ${a.version} started in background (pid=${l.pid})`),e.forceEnroll&&console.log("Device credential replaced by force enrollment"),console.log(`Logs: ${l.logFile}`)}function C(e){try{return m.realpathSync(e)}catch{return i.resolve(e)}}process.argv[1]&&C(process.argv[1])===C(Q)&&tr().catch(e=>{let r=i.basename(process.argv[1]||"headless-client");console.error(`${r}: ${e instanceof Error?e.message:String(e)}`),process.exitCode=1});export{L as assertSupportedNodeVersion,ze as createDaemonEnv,Qe as installAndStartHeadlessRuntime,Fe as installHeadlessRuntime,h as isRunning,I as parseHeadlessCliArgs,D as readPackageMetadata,Ye as resolveHeadlessLayout,qe as restartHeadlessRuntime,A as startHeadlessRuntime,b as stopHeadlessRuntime,er as updateHeadlessRuntime,T as validateHeadlessRuntimeStart};
|