@usemosio/cli 0.0.0-stage → 1.0.0-next.1
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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +29 -2
- package/bin/mosio-agent.js +5 -0
- package/bin/mosio.js +7 -0
- package/lib/download.mjs +66 -0
- package/lib/launcher.mjs +229 -0
- package/lib/toolchain.mjs +243 -0
- package/lib/zip.mjs +85 -0
- package/package.json +38 -4
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Mosio
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mosio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @usemosio/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `mosio` command: agent-first programmatic video on a pinned Chromium.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i -g @usemosio/cli # or: npx @usemosio/cli init ./my-video
|
|
7
|
+
mosio doctor # what is installed, what is missing and how to fix it
|
|
8
|
+
mosio init ./my-video
|
|
9
|
+
mosio check ./my-video
|
|
10
|
+
mosio render ./my-video -o my-video.mp4 # first run downloads the toolchain (see below)
|
|
11
|
+
mosio studio ./my-video
|
|
12
|
+
mosio mcp install # wire Claude Code, Codex and Cursor to Mosio
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Platform: **Linux x86-64** (Debian 13 or similar, glibc). macOS and Windows are not supported yet; use a container.
|
|
16
|
+
Node 22.12 or newer. `mosio` refuses to run as root (Chromium keeps its sandbox on).
|
|
17
|
+
|
|
18
|
+
## What is in the package
|
|
19
|
+
|
|
20
|
+
- `@usemosio/cli` (this package) is a small launcher. It depends optionally on a platform package,
|
|
21
|
+
`@usemosio/cli-linux-x64`, which carries `bin/mosio`, `bin/mosio-agent`, `toolchain.lock.json`, the kit
|
|
22
|
+
(`scripts/`, `primitives/`, `skills/`, `fixtures/`) and the built Studio.
|
|
23
|
+
- The toolchain is **not** in the package. On first use `mosio` (or `mosio setup`) downloads into `$MOSIO_HOME`
|
|
24
|
+
(default `~/.local/share/mosio/home`), checked against the SHA-256 in `toolchain.lock.json`:
|
|
25
|
+
Chromium (chrome-headless-shell, about 120 MB), a static FFmpeg and ffprobe (GPL-3.0, about 58 MB, downloaded
|
|
26
|
+
for you rather than shipped), and the Inter font (0.9 MB).
|
|
27
|
+
- Chromium needs a few system libraries. `mosio doctor` and `mosio setup` list exactly which are missing and the
|
|
28
|
+
`apt-get` command for them.
|
|
29
|
+
|
|
30
|
+
`MOSIO_NO_FETCH=1` turns the automatic download off. Full guide: docs/INSTALL.md in the repository.
|
package/bin/mosio.js
ADDED
package/lib/download.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// HTTPS download with a SHA-256 check, retries and a progress callback. Node built-ins only.
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { createWriteStream } from "node:fs";
|
|
4
|
+
import { rename, rm, mkdir } from "node:fs/promises";
|
|
5
|
+
import { dirname } from "node:path";
|
|
6
|
+
import https from "node:https";
|
|
7
|
+
|
|
8
|
+
const MAX_REDIRECTS = 5;
|
|
9
|
+
|
|
10
|
+
function get(url, redirects = 0) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
let u;
|
|
13
|
+
try { u = new URL(url); } catch { return reject(new Error(`bad URL ${url}`)); }
|
|
14
|
+
if (u.protocol !== "https:") return reject(new Error(`refusing non-https URL ${url}`));
|
|
15
|
+
const req = https.get(u, { headers: { "user-agent": "mosio-cli-setup" } }, (res) => {
|
|
16
|
+
const code = res.statusCode ?? 0;
|
|
17
|
+
if (code >= 300 && code < 400 && res.headers.location) {
|
|
18
|
+
res.resume();
|
|
19
|
+
if (redirects >= MAX_REDIRECTS) return reject(new Error(`too many redirects for ${url}`));
|
|
20
|
+
return resolve(get(new URL(res.headers.location, u).href, redirects + 1));
|
|
21
|
+
}
|
|
22
|
+
if (code !== 200) { res.resume(); return reject(new Error(`HTTP ${code} for ${url}`)); }
|
|
23
|
+
resolve(res);
|
|
24
|
+
});
|
|
25
|
+
req.setTimeout(60_000, () => req.destroy(new Error(`timeout connecting to ${url}`)));
|
|
26
|
+
req.on("error", reject);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Download `url` to `dest` (through `dest.partial`), checking `sha256` (hex) of the bytes as they arrive.
|
|
32
|
+
* Resolves `{ bytes, sha256 }`. Nothing is left at `dest` unless the hash matched.
|
|
33
|
+
*/
|
|
34
|
+
export async function download(url, dest, { sha256, onProgress, retries = 3, label = url } = {}) {
|
|
35
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
36
|
+
const partial = `${dest}.partial-${process.pid}`;
|
|
37
|
+
let lastErr;
|
|
38
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
39
|
+
try {
|
|
40
|
+
const res = await get(url);
|
|
41
|
+
const total = Number(res.headers["content-length"] ?? 0);
|
|
42
|
+
const hash = createHash("sha256");
|
|
43
|
+
let bytes = 0;
|
|
44
|
+
await new Promise((resolve, reject) => {
|
|
45
|
+
const out = createWriteStream(partial, { mode: 0o600 });
|
|
46
|
+
res.on("data", (c) => { hash.update(c); bytes += c.length; onProgress?.(bytes, total); });
|
|
47
|
+
res.on("error", reject);
|
|
48
|
+
out.on("error", reject);
|
|
49
|
+
out.on("finish", resolve);
|
|
50
|
+
res.pipe(out);
|
|
51
|
+
});
|
|
52
|
+
const got = hash.digest("hex");
|
|
53
|
+
if (sha256 && got !== sha256) {
|
|
54
|
+
throw Object.assign(new Error(`SHA-256 mismatch for ${label}: got ${got}, the lock pins ${sha256}`), { fatal: true });
|
|
55
|
+
}
|
|
56
|
+
await rename(partial, dest);
|
|
57
|
+
return { bytes, sha256: got };
|
|
58
|
+
} catch (e) {
|
|
59
|
+
lastErr = e;
|
|
60
|
+
await rm(partial, { force: true });
|
|
61
|
+
if (e.fatal || attempt === retries) break;
|
|
62
|
+
await new Promise((r) => setTimeout(r, 1000 * attempt));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw lastErr;
|
|
66
|
+
}
|
package/lib/launcher.mjs
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// The `mosio` command of the npm package: finds the platform package that carries the real binaries, makes sure
|
|
2
|
+
// the pinned toolchain is in $MOSIO_HOME, and runs the binary. Everything else (every command, every flag) is the
|
|
3
|
+
// Rust binary's own; this file adds `mosio setup`, a first-run fetch, doctor hints and the stable MCP path.
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { ensure, loadLock, quickMissing, status, aptHint, missingChromeLibs } from "./toolchain.mjs";
|
|
10
|
+
|
|
11
|
+
const require = createRequire(import.meta.url);
|
|
12
|
+
|
|
13
|
+
const PLATFORMS = { "linux-x64": "@usemosio/cli-linux-x64" };
|
|
14
|
+
|
|
15
|
+
/** Commands that never start the browser or FFmpeg, so they do not trigger the first-run download. */
|
|
16
|
+
const NO_TOOLCHAIN = new Set(["init", "doctor", "setup", "check", "ids", "log", "status", "diff", "show", "rollback", "commit", "migrate", "registry", "help"]);
|
|
17
|
+
|
|
18
|
+
const err = (m) => process.stderr.write(m.endsWith("\n") ? m : m + "\n");
|
|
19
|
+
|
|
20
|
+
export function platformPackage(platform = process.platform, arch = process.arch) {
|
|
21
|
+
return { key: `${platform}-${arch}`, name: PLATFORMS[`${platform}-${arch}`] };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The directory of the installed platform package (its bin/ holds mosio and mosio-agent). */
|
|
25
|
+
export function findRoot() {
|
|
26
|
+
if (process.env.MOSIO_PLATFORM_ROOT) return process.env.MOSIO_PLATFORM_ROOT;
|
|
27
|
+
const { key, name } = platformPackage();
|
|
28
|
+
if (!name) {
|
|
29
|
+
err(`mosio: ${key} is not supported yet. Linux x86-64 (Debian 13 or similar) is the only platform with a pinned toolchain.\n` +
|
|
30
|
+
` macOS and Windows users can run Mosio in a container: see docs/INSTALL.md.`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return dirname(require.resolve(`${name}/package.json`));
|
|
35
|
+
} catch {
|
|
36
|
+
err(`mosio: the platform package ${name} is not installed.\n` +
|
|
37
|
+
` It is an optional dependency of @usemosio/cli: reinstall without --omit=optional / --no-optional,\n` +
|
|
38
|
+
` or install it next to the CLI: npm i -g ${name}`);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function defaultHome(env = process.env) {
|
|
44
|
+
if (env.MOSIO_HOME) return env.MOSIO_HOME;
|
|
45
|
+
const data = env.XDG_DATA_HOME || join(homedir(), ".local", "share");
|
|
46
|
+
return join(data, "mosio", "home");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The subcommand, skipping the global flags (`--lock FILE`, `--trust-blender`) that may come before it. */
|
|
50
|
+
export function subcommand(args) {
|
|
51
|
+
for (let i = 0; i < args.length; i++) {
|
|
52
|
+
const a = args[i];
|
|
53
|
+
if (a === "--lock") { i++; continue; }
|
|
54
|
+
if (a.startsWith("--lock=") || a === "--trust-blender") continue;
|
|
55
|
+
return a;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Running from the `npx` cache: that directory can be pruned, so MCP configs must not point into it. */
|
|
61
|
+
export function isEphemeral(root, env = process.env) {
|
|
62
|
+
if (env.MOSIO_STABLE === "1") return true;
|
|
63
|
+
if (env.MOSIO_STABLE === "0") return false;
|
|
64
|
+
return /[\\/]_npx[\\/]/.test(root);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function stableDir(version, env = process.env) {
|
|
68
|
+
const data = env.XDG_DATA_HOME || join(homedir(), ".local", "share");
|
|
69
|
+
return join(data, "mosio", "npm", version);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Copy the platform package to a directory that outlives the npx cache. Returns the new root. */
|
|
73
|
+
export function makeStable(root, env = process.env) {
|
|
74
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
75
|
+
const dest = stableDir(pkg.version, env);
|
|
76
|
+
if (existsSync(join(dest, ".complete"))) return dest;
|
|
77
|
+
const tmp = `${dest}.tmp-${process.pid}`;
|
|
78
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
79
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
80
|
+
cpSync(root, tmp, { recursive: true, dereference: true });
|
|
81
|
+
writeFileSync(join(tmp, ".complete"), `${pkg.version}\n`);
|
|
82
|
+
rmSync(dest, { recursive: true, force: true });
|
|
83
|
+
renameSync(tmp, dest);
|
|
84
|
+
return dest;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function childEnv(root, home, env = process.env) {
|
|
88
|
+
return { ...env, MOSIO_HOME: home, MOSIO_KIT: env.MOSIO_KIT || root, MOSIO_NODE: env.MOSIO_NODE || process.execPath };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function exec(root, args, env) {
|
|
92
|
+
const child = spawn(join(root, "bin", "mosio"), args, { stdio: "inherit", env });
|
|
93
|
+
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(sig, () => child.kill(sig));
|
|
94
|
+
child.on("error", (e) => { err(`mosio: cannot start ${join(root, "bin", "mosio")}: ${e.message}`); process.exit(127); });
|
|
95
|
+
child.on("exit", (code, signal) => {
|
|
96
|
+
if (signal) { process.removeAllListeners(signal); process.kill(process.pid, signal); } else process.exit(code ?? 1);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function parseSetup(args) {
|
|
101
|
+
const o = { chromium: true, ffmpeg: true, fonts: true, check: false, force: false, help: false };
|
|
102
|
+
for (const a of args) {
|
|
103
|
+
if (a === "--no-ffmpeg") o.ffmpeg = false;
|
|
104
|
+
else if (a === "--no-fonts") o.fonts = false;
|
|
105
|
+
else if (a === "--no-browser") o.chromium = false;
|
|
106
|
+
else if (a === "--check") o.check = true;
|
|
107
|
+
else if (a === "--force") o.force = true;
|
|
108
|
+
else if (a === "-h" || a === "--help") o.help = true;
|
|
109
|
+
else { err(`mosio setup: unknown option ${a} (see mosio setup --help)`); process.exit(2); }
|
|
110
|
+
}
|
|
111
|
+
return o;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const SETUP_HELP = `mosio setup [--check] [--force] [--no-ffmpeg] [--no-fonts] [--no-browser]
|
|
115
|
+
|
|
116
|
+
Download the pinned toolchain that is not in the npm package into $MOSIO_HOME and check every file against the
|
|
117
|
+
SHA-256 in toolchain.lock.json: Chromium (chrome-headless-shell), a static FFmpeg and ffprobe, and the Inter font.
|
|
118
|
+
It needs no root, curl, unzip or xz. Prints one JSON document on stdout; progress goes to stderr.
|
|
119
|
+
--check only report what is installed (nothing is downloaded)
|
|
120
|
+
--force download again even when present
|
|
121
|
+
--no-ffmpeg keep the FFmpeg you have (the pinned Debian package, or another build: mosio doctor says if it matches)
|
|
122
|
+
MOSIO_HOME where it goes (default ~/.local/share/mosio/home)
|
|
123
|
+
MOSIO_NO_FETCH=1 never download on first use (mosio then stops with a pointer to this command)
|
|
124
|
+
`;
|
|
125
|
+
|
|
126
|
+
async function setup(root, home, args) {
|
|
127
|
+
const o = parseSetup(args);
|
|
128
|
+
if (o.help) { process.stdout.write(SETUP_HELP); return 0; }
|
|
129
|
+
const lock = await loadLock(root);
|
|
130
|
+
if (o.check) {
|
|
131
|
+
const st = await status(home, lock);
|
|
132
|
+
const ok = st.chromium.installed && st.ffmpeg.installed && st.fonts.installed;
|
|
133
|
+
process.stdout.write(JSON.stringify({ ok, mosio_home: home, ...st }, null, 2) + "\n");
|
|
134
|
+
return ok ? 0 : 1;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const r = await ensure(home, lock, o, { log: err, force: o.force });
|
|
138
|
+
const ok = r.missing_system_libraries.length === 0;
|
|
139
|
+
if (!ok) err(aptHint(r.missing_system_libraries));
|
|
140
|
+
process.stdout.write(JSON.stringify({ ok, mosio_home: home, ...r, next: ok ? "mosio doctor" : "install the system libraries above, then run mosio doctor" }, null, 2) + "\n");
|
|
141
|
+
return ok ? 0 : 1;
|
|
142
|
+
} catch (e) {
|
|
143
|
+
process.stdout.write(JSON.stringify({ ok: false, code: "E_SETUP", error: `E_SETUP: ${e.message}`, fix: "check the network (HTTPS to storage.googleapis.com and github.com) and run mosio setup again; nothing partial is kept" }, null, 2) + "\n");
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** `mosio doctor` plus what to do about every failure, on stderr (stdout stays the binary's one JSON document). */
|
|
149
|
+
function doctor(root, args, env) {
|
|
150
|
+
const r = spawnSync(join(root, "bin", "mosio"), args, { env, encoding: "utf8", maxBuffer: 64 << 20 });
|
|
151
|
+
process.stdout.write(r.stdout ?? "");
|
|
152
|
+
process.stderr.write(r.stderr ?? "");
|
|
153
|
+
let report;
|
|
154
|
+
try { report = JSON.parse(r.stdout); } catch { return r.status ?? 1; }
|
|
155
|
+
const bad = (report.checks ?? []).filter((c) => c.status === "fail" || c.status === "blocked");
|
|
156
|
+
if (bad.length) {
|
|
157
|
+
err("\nmosio doctor: what to do");
|
|
158
|
+
const missing = bad.filter((c) => c.fix);
|
|
159
|
+
const seen = new Set();
|
|
160
|
+
for (const c of missing) if (!seen.has(c.fix)) { seen.add(c.fix); err(` - ${c.name}: ${c.fix}`); }
|
|
161
|
+
if (bad.some((c) => /chrome\./.test(c.name))) {
|
|
162
|
+
const libs = existsSync(report.chrome_path ?? "") ? missingChromeLibs(report.chrome_path) : [];
|
|
163
|
+
if (libs.length) err(" - " + aptHint(libs).replace(/\n/g, "\n "));
|
|
164
|
+
else if (!existsSync(report.chrome_path ?? "")) err(" - chromium: not downloaded yet: run `mosio setup`");
|
|
165
|
+
}
|
|
166
|
+
if (bad.some((c) => c.name.startsWith("ffmpeg")) && !seen.has("ffmpeg")) err(" - ffmpeg: `mosio setup` downloads the pinned static build; `mosio setup --no-ffmpeg` keeps your own");
|
|
167
|
+
}
|
|
168
|
+
const curl = spawnSync("curl", ["--version"], { stdio: "ignore" });
|
|
169
|
+
if (curl.error) err(" - note: `curl` is not installed; `mosio registry fetch` and `mosio media sfx fetch` need it (apt-get install curl)");
|
|
170
|
+
return r.status ?? 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
174
|
+
const root0 = findRoot();
|
|
175
|
+
const home = defaultHome();
|
|
176
|
+
const env = childEnv(root0, home);
|
|
177
|
+
const cmd = subcommand(argv);
|
|
178
|
+
const isMcpInstall = cmd === "mcp" && (argv.includes("install") || argv.includes("uninstall"));
|
|
179
|
+
|
|
180
|
+
if (cmd === "setup") process.exit(await setup(root0, home, argv.slice(argv.indexOf("setup") + 1)));
|
|
181
|
+
if (cmd === "doctor") process.exit(doctor(root0, argv, env));
|
|
182
|
+
|
|
183
|
+
let root = root0;
|
|
184
|
+
// `mosio mcp install` records the absolute path of the running binary in the agent's config: never an npx cache path
|
|
185
|
+
if (isMcpInstall && isEphemeral(root0)) {
|
|
186
|
+
if (argv.includes("--dry-run")) {
|
|
187
|
+
err(`mosio: running from a temporary npx directory; a real install first copies itself to ${stableDir(JSON.parse(readFileSync(join(root0, "package.json"), "utf8")).version)} (this dry run shows the temporary paths)`);
|
|
188
|
+
} else {
|
|
189
|
+
root = makeStable(root0);
|
|
190
|
+
err(`mosio: copied the install to ${root} so the agent configs do not point into the npx cache`);
|
|
191
|
+
}
|
|
192
|
+
env.MOSIO_KIT = root;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// first run: download what the command needs (the browser and FFmpeg are not in the package)
|
|
196
|
+
const asksHelp = argv.includes("--help") || argv.includes("-h");
|
|
197
|
+
const needs = !asksHelp && cmd !== undefined && cmd !== "" && !cmd.startsWith("-") && (!NO_TOOLCHAIN.has(cmd) || (isMcpInstall && !argv.includes("--dry-run")));
|
|
198
|
+
if (needs) {
|
|
199
|
+
const lock = await loadLock(root);
|
|
200
|
+
const missing = await quickMissing(home, lock);
|
|
201
|
+
if (missing.length) {
|
|
202
|
+
if (process.env.MOSIO_NO_FETCH === "1") {
|
|
203
|
+
err(`mosio: ${missing.join(", ")} not installed in ${home} and MOSIO_NO_FETCH=1: run \`mosio setup\` while online.`);
|
|
204
|
+
} else {
|
|
205
|
+
err(`mosio: first run: downloading the pinned toolchain (${missing.join(", ")}) into ${home}; this happens once (mosio setup --check shows the state)`);
|
|
206
|
+
try {
|
|
207
|
+
const r = await ensure(home, lock, { chromium: missing.includes("chromium"), ffmpeg: missing.includes("ffmpeg"), fonts: missing.includes("fonts") }, { log: err });
|
|
208
|
+
if (r.missing_system_libraries.length) err(aptHint(r.missing_system_libraries));
|
|
209
|
+
err(`mosio: toolchain ready (${(r.downloaded_bytes / 1048576).toFixed(0)} MB in ${r.seconds}s)`);
|
|
210
|
+
} catch (e) {
|
|
211
|
+
err(`mosio: E_SETUP: ${e.message}\nmosio: run \`mosio setup\` again when the network works.`);
|
|
212
|
+
process.exit(1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
exec(root, argv, env);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
/** `mosio-agent ...`: run the platform package's agent host. */
|
|
222
|
+
export function runAgent(argv) {
|
|
223
|
+
const root = findRoot();
|
|
224
|
+
const env = childEnv(root, defaultHome());
|
|
225
|
+
const child = spawn(join(root, "bin", "mosio-agent"), argv, { stdio: "inherit", env });
|
|
226
|
+
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(sig, () => child.kill(sig));
|
|
227
|
+
child.on("error", (e) => { err(`mosio-agent: ${e.message}`); process.exit(127); });
|
|
228
|
+
child.on("exit", (code, signal) => { if (signal) { process.removeAllListeners(signal); process.kill(process.pid, signal); } else process.exit(code ?? 1); });
|
|
229
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// The first-run toolchain: everything `toolchain.lock.json` pins that is not in the npm package. Chromium, the
|
|
2
|
+
// static FFmpeg and the Inter font are downloaded into $MOSIO_HOME and checked against the SHA-256 in the lock,
|
|
3
|
+
// the way scripts/bootstrap.sh does for a checkout. Nothing here needs root, curl, unzip or xz.
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { createReadStream, existsSync } from "node:fs";
|
|
6
|
+
import { chmod, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { createGunzip } from "node:zlib";
|
|
9
|
+
import { createWriteStream } from "node:fs";
|
|
10
|
+
import { pipeline } from "node:stream/promises";
|
|
11
|
+
import { join, dirname } from "node:path";
|
|
12
|
+
import { download } from "./download.mjs";
|
|
13
|
+
import { extractZip } from "./zip.mjs";
|
|
14
|
+
|
|
15
|
+
/** Chromium's runtime libraries, as scripts/bootstrap.sh installs them (Debian 13). */
|
|
16
|
+
export const CHROMIUM_APT = [
|
|
17
|
+
"libnss3", "libatk1.0-0t64", "libatk-bridge2.0-0t64", "libcups2t64", "libdrm2", "libxkbcommon0", "libxcomposite1",
|
|
18
|
+
"libxdamage1", "libxfixes3", "libxrandr2", "libgbm1", "libpango-1.0-0", "libcairo2", "libasound2t64", "libxshmfence1",
|
|
19
|
+
"libdbus-1-3", "fontconfig",
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export async function sha256File(file) {
|
|
23
|
+
const h = createHash("sha256");
|
|
24
|
+
await pipeline(createReadStream(file), h);
|
|
25
|
+
return h.digest("hex");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function loadLock(kitRoot) {
|
|
29
|
+
return JSON.parse(await readFile(join(kitRoot, "toolchain.lock.json"), "utf8"));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function layout(home, lock) {
|
|
33
|
+
const c = lock.chrome_headless_shell;
|
|
34
|
+
const chromeDir = join(home, "browsers", `chrome-headless-shell-${c.version}`);
|
|
35
|
+
const ff = join(home, "ffmpeg", "bin");
|
|
36
|
+
const font = lock.fonts.download;
|
|
37
|
+
return {
|
|
38
|
+
chromeDir,
|
|
39
|
+
chrome: join(chromeDir, c.binary_relpath),
|
|
40
|
+
ffmpegBin: ff,
|
|
41
|
+
ffmpeg: join(ff, "ffmpeg"),
|
|
42
|
+
ffprobe: join(ff, "ffprobe"),
|
|
43
|
+
fontFile: join(home, "fonts", font.file),
|
|
44
|
+
fontConf: join(home, "fontconfig", "fonts.conf"),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const mb = (n) => `${(n / 1048576).toFixed(1)} MB`;
|
|
49
|
+
|
|
50
|
+
function run(cmd, args) {
|
|
51
|
+
const r = spawnSync(cmd, args, { encoding: "utf8", timeout: 20_000 });
|
|
52
|
+
return r.error ? null : r;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** True when the machine already has the pinned Debian FFmpeg (and ffprobe): then no download is needed. */
|
|
56
|
+
export function systemFfmpegIsPinned(lock) {
|
|
57
|
+
const want = lock.ffmpeg.version_line;
|
|
58
|
+
const r = run("ffmpeg", ["-hide_banner", "-version"]);
|
|
59
|
+
if (!r || r.status !== 0 || !String(r.stdout).split("\n")[0].startsWith(want)) return false;
|
|
60
|
+
const p = run("ffprobe", ["-hide_banner", "-version"]);
|
|
61
|
+
return !!p && p.status === 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function systemHasInter() {
|
|
65
|
+
const r = run("fc-list", [":family=Inter"]);
|
|
66
|
+
return !!r && r.status === 0 && String(r.stdout).trim().length > 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function fileHasSha(file, sha) {
|
|
70
|
+
try { return (await sha256File(file)) === sha; } catch { return false; }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What is installed, without downloading anything. Hashes every file (a few hundred MB: about a second). */
|
|
74
|
+
export async function status(home, lock) {
|
|
75
|
+
const L = layout(home, lock);
|
|
76
|
+
const c = lock.chrome_headless_shell;
|
|
77
|
+
const s = lock.ffmpeg.static;
|
|
78
|
+
const f = lock.fonts.download;
|
|
79
|
+
const out = {};
|
|
80
|
+
out.chromium = { installed: await fileHasSha(L.chrome, c.binary_sha256), path: L.chrome, version: c.version };
|
|
81
|
+
const ffHome = (await fileHasSha(L.ffmpeg, s.ffmpeg.binary_sha256)) && (await fileHasSha(L.ffprobe, s.ffprobe.binary_sha256));
|
|
82
|
+
const ffSys = !ffHome && systemFfmpegIsPinned(lock);
|
|
83
|
+
out.ffmpeg = { installed: ffHome || ffSys, source: ffHome ? "download" : ffSys ? "system (pinned Debian package)" : null, path: ffHome ? L.ffmpegBin : null, version: s.version };
|
|
84
|
+
const fontHome = await fileHasSha(L.fontFile, f.file_sha256);
|
|
85
|
+
const fontSys = !fontHome && systemHasInter();
|
|
86
|
+
out.fonts = { installed: fontHome || fontSys, source: fontHome ? "download" : fontSys ? "system (fonts-inter)" : null, path: fontHome ? L.fontFile : null };
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function progress(label, log) {
|
|
91
|
+
let last = 0;
|
|
92
|
+
return (bytes, total) => {
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
if (now - last < 1500 && bytes !== total) return;
|
|
95
|
+
last = now;
|
|
96
|
+
log(` ${label}: ${mb(bytes)}${total ? ` of ${mb(total)}` : ""}`);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function installChromium(home, lock, log) {
|
|
101
|
+
const c = lock.chrome_headless_shell;
|
|
102
|
+
const L = layout(home, lock);
|
|
103
|
+
const dl = join(home, "downloads");
|
|
104
|
+
const zip = join(dl, `chrome-headless-shell-${c.version}-linux64.zip`);
|
|
105
|
+
log(`Chromium ${c.version} (chrome-headless-shell, ${c.url})`);
|
|
106
|
+
const t0 = Date.now();
|
|
107
|
+
const { bytes } = await download(c.url, zip, { sha256: c.zip_sha256, onProgress: progress("downloading", log), label: "chrome-headless-shell zip" });
|
|
108
|
+
const tmp = `${L.chromeDir}.tmp-${process.pid}`;
|
|
109
|
+
await rm(tmp, { recursive: true, force: true });
|
|
110
|
+
await mkdir(tmp, { recursive: true });
|
|
111
|
+
try {
|
|
112
|
+
await extractZip(zip, tmp);
|
|
113
|
+
if (!(await fileHasSha(join(tmp, c.binary_relpath), c.binary_sha256))) throw new Error("chrome-headless-shell binary SHA-256 mismatch after unzip");
|
|
114
|
+
await rm(L.chromeDir, { recursive: true, force: true });
|
|
115
|
+
await rename(tmp, L.chromeDir);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
await rm(tmp, { recursive: true, force: true });
|
|
118
|
+
throw e;
|
|
119
|
+
}
|
|
120
|
+
await rm(zip, { force: true });
|
|
121
|
+
log(` installed at ${L.chromeDir}`);
|
|
122
|
+
return { bytes, seconds: (Date.now() - t0) / 1000 };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function gunzipTo(src, dest, sha, mode) {
|
|
126
|
+
const tmp = `${dest}.tmp-${process.pid}`;
|
|
127
|
+
await pipeline(createReadStream(src), createGunzip(), createWriteStream(tmp, { mode }));
|
|
128
|
+
if ((await sha256File(tmp)) !== sha) { await rm(tmp, { force: true }); throw new Error(`${dest}: SHA-256 mismatch after gunzip`); }
|
|
129
|
+
await chmod(tmp, mode);
|
|
130
|
+
await rename(tmp, dest);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function installFfmpeg(home, lock, log) {
|
|
134
|
+
const s = lock.ffmpeg.static;
|
|
135
|
+
const L = layout(home, lock);
|
|
136
|
+
const dl = join(home, "downloads");
|
|
137
|
+
log(`FFmpeg ${s.version} static build (GPL-3.0-or-later; downloaded for you, not shipped in the npm package)`);
|
|
138
|
+
const t0 = Date.now();
|
|
139
|
+
let bytes = 0;
|
|
140
|
+
await mkdir(L.ffmpegBin, { recursive: true });
|
|
141
|
+
for (const [name, spec, dest] of [["ffmpeg", s.ffmpeg, L.ffmpeg], ["ffprobe", s.ffprobe, L.ffprobe]]) {
|
|
142
|
+
const gz = join(dl, `${name}-${s.version}-linux-x64.gz`);
|
|
143
|
+
const r = await download(spec.url, gz, { sha256: spec.gz_sha256, onProgress: progress(name, log), label: `${name} gz` });
|
|
144
|
+
bytes += r.bytes;
|
|
145
|
+
await gunzipTo(gz, dest, spec.binary_sha256, 0o755);
|
|
146
|
+
await rm(gz, { force: true });
|
|
147
|
+
}
|
|
148
|
+
const lic = join(dirname(L.ffmpegBin), "LICENSE.txt");
|
|
149
|
+
const r = await download(s.license_file.url, lic, { sha256: s.license_file.sha256, label: "FFmpeg licence text" });
|
|
150
|
+
bytes += r.bytes;
|
|
151
|
+
log(` installed at ${L.ffmpegBin}`);
|
|
152
|
+
return { bytes, seconds: (Date.now() - t0) / 1000 };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function installFonts(home, lock, log) {
|
|
156
|
+
const f = lock.fonts.download;
|
|
157
|
+
const L = layout(home, lock);
|
|
158
|
+
log(`Font ${f.family} ${f.version} (${f.license})`);
|
|
159
|
+
const t0 = Date.now();
|
|
160
|
+
const r = await download(f.url, L.fontFile, { sha256: f.file_sha256, label: "Inter font" });
|
|
161
|
+
const l = await download(f.license_file.url, join(dirname(L.fontFile), "Inter-LICENSE.txt"), { sha256: f.license_file.sha256, label: "Inter licence" });
|
|
162
|
+
// the browser reads fontconfig: the system configuration plus the downloaded directory
|
|
163
|
+
const esc = (x) => x.replace(/&/g, "&").replace(/</g, "<");
|
|
164
|
+
await mkdir(dirname(L.fontConf), { recursive: true });
|
|
165
|
+
await writeFile(
|
|
166
|
+
L.fontConf,
|
|
167
|
+
`<?xml version="1.0"?>\n<!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">\n<fontconfig>\n <include ignore_missing="yes">/etc/fonts/fonts.conf</include>\n <dir>${esc(dirname(L.fontFile))}</dir>\n <cachedir>${esc(join(dirname(L.fontConf), "cache"))}</cachedir>\n</fontconfig>\n`,
|
|
168
|
+
);
|
|
169
|
+
log(` installed at ${L.fontFile}`);
|
|
170
|
+
return { bytes: r.bytes + l.bytes, seconds: (Date.now() - t0) / 1000 };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const stampFile = (home) => join(home, ".mosio-setup.json");
|
|
174
|
+
|
|
175
|
+
async function readStamp(home) {
|
|
176
|
+
try { return JSON.parse(await readFile(stampFile(home), "utf8")); } catch { return {}; }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Cheap "is it there" for every command (no hashing: the stamp records the lock hashes `mosio setup` verified, and
|
|
181
|
+
* `mosio` itself re-checks the browser's SHA-256 on every run). Returns the components that are missing.
|
|
182
|
+
*/
|
|
183
|
+
export async function quickMissing(home, lock) {
|
|
184
|
+
const L = layout(home, lock);
|
|
185
|
+
const stamp = await readStamp(home);
|
|
186
|
+
const missing = [];
|
|
187
|
+
if (!(stamp.chromium === lock.chrome_headless_shell.binary_sha256 && existsSync(L.chrome))) missing.push("chromium");
|
|
188
|
+
const ffOk = stamp.ffmpeg === lock.ffmpeg.static.ffmpeg.binary_sha256 && existsSync(L.ffmpeg) && existsSync(L.ffprobe);
|
|
189
|
+
if (!ffOk && !systemFfmpegIsPinned(lock)) missing.push("ffmpeg");
|
|
190
|
+
const fontOk = stamp.fonts === lock.fonts.download.file_sha256 && existsSync(L.fontFile);
|
|
191
|
+
if (!fontOk && !systemHasInter()) missing.push("fonts");
|
|
192
|
+
return missing;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The shared libraries `ldd` cannot resolve for the browser (empty when it can start, or ldd is missing). */
|
|
196
|
+
export function missingChromeLibs(chrome) {
|
|
197
|
+
const r = run("ldd", [chrome]);
|
|
198
|
+
if (!r) return [];
|
|
199
|
+
return String(r.stdout).split("\n").filter((l) => l.includes("not found")).map((l) => l.trim().split(/\s+/)[0]);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function aptHint(libs) {
|
|
203
|
+
return `Chromium needs system libraries that are missing (${libs.join(", ")}). On Debian/Ubuntu, as root:\n apt-get install -y --no-install-recommends ${CHROMIUM_APT.join(" ")}`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Install what is missing. `want` = { chromium, ffmpeg, fonts } (booleans). Returns a report; throws on a failed
|
|
208
|
+
* download or a hash mismatch (nothing is installed in that case).
|
|
209
|
+
*/
|
|
210
|
+
export async function ensure(home, lock, want, { log = () => {}, force = false } = {}) {
|
|
211
|
+
await mkdir(home, { recursive: true });
|
|
212
|
+
const st = force ? {} : await status(home, lock);
|
|
213
|
+
const report = [];
|
|
214
|
+
const t0 = Date.now();
|
|
215
|
+
const steps = [
|
|
216
|
+
["chromium", want.chromium, st.chromium?.installed, installChromium],
|
|
217
|
+
["ffmpeg", want.ffmpeg, st.ffmpeg?.installed, installFfmpeg],
|
|
218
|
+
["fonts", want.fonts, st.fonts?.installed, installFonts],
|
|
219
|
+
];
|
|
220
|
+
for (const [name, wanted, have, fn] of steps) {
|
|
221
|
+
if (!wanted) { report.push({ name, state: "skipped" }); continue; }
|
|
222
|
+
if (have) { report.push({ name, state: "present", source: st[name].source ?? "download" }); continue; }
|
|
223
|
+
const r = await fn(home, lock, log);
|
|
224
|
+
report.push({ name, state: "installed", bytes: r.bytes, seconds: Number(r.seconds.toFixed(1)) });
|
|
225
|
+
}
|
|
226
|
+
// record what is now verified in $MOSIO_HOME, so later commands can check cheaply
|
|
227
|
+
const after = await status(home, lock);
|
|
228
|
+
const stamp = await readStamp(home);
|
|
229
|
+
if (after.chromium.installed) stamp.chromium = lock.chrome_headless_shell.binary_sha256;
|
|
230
|
+
if (after.ffmpeg.source === "download") stamp.ffmpeg = lock.ffmpeg.static.ffmpeg.binary_sha256;
|
|
231
|
+
if (after.fonts.source === "download") stamp.fonts = lock.fonts.download.file_sha256;
|
|
232
|
+
await writeFile(stampFile(home), JSON.stringify(stamp, null, 2) + "\n");
|
|
233
|
+
const L = layout(home, lock);
|
|
234
|
+
let libs = [];
|
|
235
|
+
if (existsSync(L.chrome)) libs = missingChromeLibs(L.chrome);
|
|
236
|
+
return {
|
|
237
|
+
components: report,
|
|
238
|
+
downloaded_bytes: report.reduce((a, r) => a + (r.bytes ?? 0), 0),
|
|
239
|
+
seconds: Number(((Date.now() - t0) / 1000).toFixed(1)),
|
|
240
|
+
missing_system_libraries: libs,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
package/lib/zip.mjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Minimal ZIP extractor (stored and deflated entries, no ZIP64, no encryption) so the first-run download needs
|
|
2
|
+
// neither `unzip` nor any npm dependency. The archive's SHA-256 is checked before this runs; this still refuses
|
|
3
|
+
// names that would escape the destination and symbolic links.
|
|
4
|
+
import { open } from "node:fs/promises";
|
|
5
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
6
|
+
import { mkdir, chmod } from "node:fs/promises";
|
|
7
|
+
import { createInflateRaw } from "node:zlib";
|
|
8
|
+
import { pipeline } from "node:stream/promises";
|
|
9
|
+
import { dirname, resolve, sep } from "node:path";
|
|
10
|
+
|
|
11
|
+
const EOCD_SIG = 0x06054b50;
|
|
12
|
+
const CEN_SIG = 0x02014b50;
|
|
13
|
+
const LOC_SIG = 0x04034b50;
|
|
14
|
+
|
|
15
|
+
/** The entries of a zip file: [{ name, method, csize, usize, offset, mode }] */
|
|
16
|
+
export async function listZip(file) {
|
|
17
|
+
const fh = await open(file, "r");
|
|
18
|
+
try {
|
|
19
|
+
const { size } = await fh.stat();
|
|
20
|
+
const tailLen = Math.min(size, 65_557);
|
|
21
|
+
const tail = Buffer.alloc(tailLen);
|
|
22
|
+
await fh.read(tail, 0, tailLen, size - tailLen);
|
|
23
|
+
let e = -1;
|
|
24
|
+
for (let i = tailLen - 22; i >= 0; i--) if (tail.readUInt32LE(i) === EOCD_SIG) { e = i; break; }
|
|
25
|
+
if (e < 0) throw new Error("not a zip file (no end-of-central-directory record)");
|
|
26
|
+
const count = tail.readUInt16LE(e + 10);
|
|
27
|
+
const cdSize = tail.readUInt32LE(e + 12);
|
|
28
|
+
const cdOff = tail.readUInt32LE(e + 16);
|
|
29
|
+
if (count === 0xffff || cdSize === 0xffffffff || cdOff === 0xffffffff) throw new Error("ZIP64 archives are not supported");
|
|
30
|
+
const cd = Buffer.alloc(cdSize);
|
|
31
|
+
await fh.read(cd, 0, cdSize, cdOff);
|
|
32
|
+
const out = [];
|
|
33
|
+
let p = 0;
|
|
34
|
+
for (let i = 0; i < count; i++) {
|
|
35
|
+
if (cd.readUInt32LE(p) !== CEN_SIG) throw new Error("corrupt zip central directory");
|
|
36
|
+
const flags = cd.readUInt16LE(p + 8);
|
|
37
|
+
const method = cd.readUInt16LE(p + 10);
|
|
38
|
+
const csize = cd.readUInt32LE(p + 20);
|
|
39
|
+
const usize = cd.readUInt32LE(p + 24);
|
|
40
|
+
const nlen = cd.readUInt16LE(p + 28), xlen = cd.readUInt16LE(p + 30), clen = cd.readUInt16LE(p + 32);
|
|
41
|
+
const ext = cd.readUInt32LE(p + 38);
|
|
42
|
+
const offset = cd.readUInt32LE(p + 42);
|
|
43
|
+
const name = cd.toString("utf8", p + 46, p + 46 + nlen);
|
|
44
|
+
if (flags & 1) throw new Error(`encrypted zip entry ${name}`);
|
|
45
|
+
out.push({ name, method, csize, usize, offset, mode: (ext >>> 16) & 0xffff });
|
|
46
|
+
p += 46 + nlen + xlen + clen;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
} finally {
|
|
50
|
+
await fh.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Extract every entry below `destDir`. Returns the number of files written. */
|
|
55
|
+
export async function extractZip(file, destDir, { onlyPrefix } = {}) {
|
|
56
|
+
const entries = await listZip(file);
|
|
57
|
+
const root = resolve(destDir);
|
|
58
|
+
const fh = await open(file, "r");
|
|
59
|
+
let files = 0;
|
|
60
|
+
try {
|
|
61
|
+
for (const en of entries) {
|
|
62
|
+
if (onlyPrefix && !en.name.startsWith(onlyPrefix)) continue;
|
|
63
|
+
const target = resolve(root, en.name);
|
|
64
|
+
if (target !== root && !target.startsWith(root + sep)) throw new Error(`zip entry escapes the destination: ${en.name}`);
|
|
65
|
+
if (en.name.endsWith("/")) { await mkdir(target, { recursive: true }); continue; }
|
|
66
|
+
if ((en.mode & 0o170000) === 0o120000) throw new Error(`zip entry ${en.name} is a symbolic link; refusing`);
|
|
67
|
+
const lh = Buffer.alloc(30);
|
|
68
|
+
await fh.read(lh, 0, 30, en.offset);
|
|
69
|
+
if (lh.readUInt32LE(0) !== LOC_SIG) throw new Error(`corrupt zip local header for ${en.name}`);
|
|
70
|
+
const start = en.offset + 30 + lh.readUInt16LE(26) + lh.readUInt16LE(28);
|
|
71
|
+
await mkdir(dirname(target), { recursive: true });
|
|
72
|
+
const src = createReadStream(file, { start, end: start + en.csize - 1 });
|
|
73
|
+
const dst = createWriteStream(target);
|
|
74
|
+
if (en.csize === 0) { dst.end(); await new Promise((r) => dst.on("close", r)); }
|
|
75
|
+
else if (en.method === 0) await pipeline(src, dst);
|
|
76
|
+
else if (en.method === 8) await pipeline(src, createInflateRaw(), dst);
|
|
77
|
+
else throw new Error(`unsupported zip method ${en.method} for ${en.name}`);
|
|
78
|
+
await chmod(target, en.mode & 0o111 ? 0o755 : 0o644);
|
|
79
|
+
files++;
|
|
80
|
+
}
|
|
81
|
+
} finally {
|
|
82
|
+
await fh.close();
|
|
83
|
+
}
|
|
84
|
+
return files;
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usemosio/cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-next.1",
|
|
4
|
+
"description": "Mosio: agent-first programmatic video on a pinned Chromium. The `mosio` command (init, check, render, studio, mcp).",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"mosio": "bin/mosio.js",
|
|
9
|
+
"mosio-agent": "bin/mosio-agent.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"bin",
|
|
13
|
+
"lib",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE-MIT",
|
|
16
|
+
"LICENSE-APACHE"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22.12"
|
|
20
|
+
},
|
|
21
|
+
"optionalDependencies": {
|
|
22
|
+
"@usemosio/cli-linux-x64": "1.0.0-next.1"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/usemosio/mosio.git",
|
|
30
|
+
"directory": "packaging/npm/cli"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/usemosio/mosio#readme",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"video",
|
|
35
|
+
"motion-graphics",
|
|
36
|
+
"mcp",
|
|
37
|
+
"agent",
|
|
38
|
+
"render"
|
|
39
|
+
]
|
|
40
|
+
}
|