@usemosio/cli 1.0.0-next.1 → 1.0.0-next.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/lib/launcher.mjs CHANGED
@@ -10,7 +10,15 @@ import { ensure, loadLock, quickMissing, status, aptHint, missingChromeLibs } fr
10
10
 
11
11
  const require = createRequire(import.meta.url);
12
12
 
13
- const PLATFORMS = { "linux-x64": "@usemosio/cli-linux-x64" };
13
+ const PLATFORMS = {
14
+ "linux-x64": "@usemosio/cli-linux-x64",
15
+ "win32-x64": "@usemosio/cli-win32-x64",
16
+ "darwin-arm64": "@usemosio/cli-darwin-arm64",
17
+ "darwin-x64": "@usemosio/cli-darwin-x64",
18
+ };
19
+
20
+ /** A binary of the platform package (`mosio.exe` on Windows). */
21
+ export const binPath = (root, name, platform = process.platform) => join(root, "bin", platform === "win32" ? `${name}.exe` : name);
14
22
 
15
23
  /** Commands that never start the browser or FFmpeg, so they do not trigger the first-run download. */
16
24
  const NO_TOOLCHAIN = new Set(["init", "doctor", "setup", "check", "ids", "log", "status", "diff", "show", "rollback", "commit", "migrate", "registry", "help"]);
@@ -26,8 +34,8 @@ export function findRoot() {
26
34
  if (process.env.MOSIO_PLATFORM_ROOT) return process.env.MOSIO_PLATFORM_ROOT;
27
35
  const { key, name } = platformPackage();
28
36
  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.`);
37
+ err(`mosio: ${key} is not supported yet. Supported: ${Object.keys(PLATFORMS).join(", ")}.\n` +
38
+ ` Other systems can run Mosio in a container: see docs/INSTALL.md.`);
31
39
  process.exit(1);
32
40
  }
33
41
  try {
@@ -40,10 +48,15 @@ export function findRoot() {
40
48
  }
41
49
  }
42
50
 
43
- export function defaultHome(env = process.env) {
51
+ /** Per-user data directory: %LOCALAPPDATA% on Windows, $XDG_DATA_HOME or ~/.local/share elsewhere. */
52
+ export function dataDir(env = process.env, platform = process.platform) {
53
+ if (platform === "win32") return env.LOCALAPPDATA || join(homedir(), "AppData", "Local");
54
+ return env.XDG_DATA_HOME || join(homedir(), ".local", "share");
55
+ }
56
+
57
+ export function defaultHome(env = process.env, platform = process.platform) {
44
58
  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");
59
+ return join(dataDir(env, platform), "mosio", "home");
47
60
  }
48
61
 
49
62
  /** The subcommand, skipping the global flags (`--lock FILE`, `--trust-blender`) that may come before it. */
@@ -64,9 +77,8 @@ export function isEphemeral(root, env = process.env) {
64
77
  return /[\\/]_npx[\\/]/.test(root);
65
78
  }
66
79
 
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);
80
+ export function stableDir(version, env = process.env, platform = process.platform) {
81
+ return join(dataDir(env, platform), "mosio", "npm", version);
70
82
  }
71
83
 
72
84
  /** Copy the platform package to a directory that outlives the npx cache. Returns the new root. */
@@ -88,19 +100,31 @@ function childEnv(root, home, env = process.env) {
88
100
  return { ...env, MOSIO_HOME: home, MOSIO_KIT: env.MOSIO_KIT || root, MOSIO_NODE: env.MOSIO_NODE || process.execPath };
89
101
  }
90
102
 
103
+ /**
104
+ * Pass signals on to the child. On Windows the child shares this console and gets Ctrl-C itself (and cancels
105
+ * cleanly); `child.kill` there would be TerminateProcess, so the launcher only stays alive until the child exits.
106
+ */
107
+ function forwardSignals(child) {
108
+ for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) {
109
+ process.on(sig, () => { if (process.platform !== "win32") child.kill(sig); });
110
+ }
111
+ }
112
+
91
113
  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); });
114
+ const bin = binPath(root, "mosio");
115
+ const child = spawn(bin, args, { stdio: "inherit", env });
116
+ forwardSignals(child);
117
+ child.on("error", (e) => { err(`mosio: cannot start ${bin}: ${e.message}`); process.exit(127); });
95
118
  child.on("exit", (code, signal) => {
96
119
  if (signal) { process.removeAllListeners(signal); process.kill(process.pid, signal); } else process.exit(code ?? 1);
97
120
  });
98
121
  }
99
122
 
100
123
  function parseSetup(args) {
101
- const o = { chromium: true, ffmpeg: true, fonts: true, check: false, force: false, help: false };
124
+ const o = { chromium: true, ffmpeg: true, fonts: true, libraries: true, check: false, force: false, help: false };
102
125
  for (const a of args) {
103
126
  if (a === "--no-ffmpeg") o.ffmpeg = false;
127
+ else if (a === "--no-libraries") o.libraries = false;
104
128
  else if (a === "--no-fonts") o.fonts = false;
105
129
  else if (a === "--no-browser") o.chromium = false;
106
130
  else if (a === "--check") o.check = true;
@@ -111,14 +135,16 @@ function parseSetup(args) {
111
135
  return o;
112
136
  }
113
137
 
114
- const SETUP_HELP = `mosio setup [--check] [--force] [--no-ffmpeg] [--no-fonts] [--no-browser]
138
+ const SETUP_HELP = `mosio setup [--check] [--force] [--no-ffmpeg] [--no-fonts] [--no-browser] [--no-libraries]
115
139
 
116
140
  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.
141
+ SHA-256 in toolchain.lock.json: Chromium (chrome-headless-shell), a static FFmpeg and ffprobe, the Inter font and the
142
+ scene libraries registry scenes use (GSAP, three.js, lottie-web, d3; from registry.npmjs.org).
118
143
  It needs no root, curl, unzip or xz. Prints one JSON document on stdout; progress goes to stderr.
119
144
  --check only report what is installed (nothing is downloaded)
120
145
  --force download again even when present
121
146
  --no-ffmpeg keep the FFmpeg you have (the pinned Debian package, or another build: mosio doctor says if it matches)
147
+ --no-libraries skip the scene libraries (a project that declares one then fails to render)
122
148
  MOSIO_HOME where it goes (default ~/.local/share/mosio/home)
123
149
  MOSIO_NO_FETCH=1 never download on first use (mosio then stops with a pointer to this command)
124
150
  `;
@@ -129,7 +155,7 @@ async function setup(root, home, args) {
129
155
  const lock = await loadLock(root);
130
156
  if (o.check) {
131
157
  const st = await status(home, lock);
132
- const ok = st.chromium.installed && st.ffmpeg.installed && st.fonts.installed;
158
+ const ok = st.chromium.installed && st.ffmpeg.installed && st.fonts.installed && st.libraries.installed;
133
159
  process.stdout.write(JSON.stringify({ ok, mosio_home: home, ...st }, null, 2) + "\n");
134
160
  return ok ? 0 : 1;
135
161
  }
@@ -140,14 +166,14 @@ async function setup(root, home, args) {
140
166
  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
167
  return ok ? 0 : 1;
142
168
  } 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");
169
+ process.stdout.write(JSON.stringify({ ok: false, code: "E_SETUP", error: `E_SETUP: ${e.message}`, fix: "check the network (HTTPS to storage.googleapis.com, github.com and registry.npmjs.org) and run mosio setup again; nothing partial is kept" }, null, 2) + "\n");
144
170
  return 1;
145
171
  }
146
172
  }
147
173
 
148
174
  /** `mosio doctor` plus what to do about every failure, on stderr (stdout stays the binary's one JSON document). */
149
175
  function doctor(root, args, env) {
150
- const r = spawnSync(join(root, "bin", "mosio"), args, { env, encoding: "utf8", maxBuffer: 64 << 20 });
176
+ const r = spawnSync(binPath(root, "mosio"), args, { env, encoding: "utf8", maxBuffer: 64 << 20 });
151
177
  process.stdout.write(r.stdout ?? "");
152
178
  process.stderr.write(r.stderr ?? "");
153
179
  let report;
@@ -166,7 +192,7 @@ function doctor(root, args, env) {
166
192
  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
193
  }
168
194
  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)");
195
+ if (curl.error) err(` - note: \`curl\` is not installed; \`mosio registry fetch\` and \`mosio media sfx fetch\` need it${process.platform === "linux" ? " (apt-get install curl)" : ""}`);
170
196
  return r.status ?? 1;
171
197
  }
172
198
 
@@ -204,7 +230,7 @@ export async function main(argv = process.argv.slice(2)) {
204
230
  } else {
205
231
  err(`mosio: first run: downloading the pinned toolchain (${missing.join(", ")}) into ${home}; this happens once (mosio setup --check shows the state)`);
206
232
  try {
207
- const r = await ensure(home, lock, { chromium: missing.includes("chromium"), ffmpeg: missing.includes("ffmpeg"), fonts: missing.includes("fonts") }, { log: err });
233
+ const r = await ensure(home, lock, { chromium: missing.includes("chromium"), ffmpeg: missing.includes("ffmpeg"), fonts: missing.includes("fonts"), libraries: missing.includes("libraries") }, { log: err });
208
234
  if (r.missing_system_libraries.length) err(aptHint(r.missing_system_libraries));
209
235
  err(`mosio: toolchain ready (${(r.downloaded_bytes / 1048576).toFixed(0)} MB in ${r.seconds}s)`);
210
236
  } catch (e) {
@@ -222,8 +248,8 @@ export async function main(argv = process.argv.slice(2)) {
222
248
  export function runAgent(argv) {
223
249
  const root = findRoot();
224
250
  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));
251
+ const child = spawn(binPath(root, "mosio-agent"), argv, { stdio: "inherit", env });
252
+ forwardSignals(child);
227
253
  child.on("error", (e) => { err(`mosio-agent: ${e.message}`); process.exit(127); });
228
254
  child.on("exit", (code, signal) => { if (signal) { process.removeAllListeners(signal); process.kill(process.pid, signal); } else process.exit(code ?? 1); });
229
255
  }
package/lib/tar.mjs ADDED
@@ -0,0 +1,55 @@
1
+ // Reading a .tgz (an npm package tarball) without a `tar` binary: gunzip, then the ustar headers, with the long names
2
+ // of GNU (`L`) and pax (`x`) entries. Regular files only; links and devices are skipped. Node built-ins only.
3
+ import { gunzipSync } from "node:zlib";
4
+
5
+ function str(buf, start, len) {
6
+ const b = buf.subarray(start, start + len);
7
+ const nul = b.indexOf(0);
8
+ return b.subarray(0, nul < 0 ? b.length : nul).toString("utf8");
9
+ }
10
+
11
+ function octal(buf, start, len) {
12
+ const s = str(buf, start, len).trim();
13
+ return s ? parseInt(s, 8) : 0;
14
+ }
15
+
16
+ /** The `path` record of a pax extended header, if any. */
17
+ function paxPath(data) {
18
+ const text = data.toString("utf8");
19
+ let i = 0;
20
+ while (i < text.length) {
21
+ const sp = text.indexOf(" ", i);
22
+ if (sp < 0) break;
23
+ const len = parseInt(text.slice(i, sp), 10);
24
+ if (!(len > 0)) break;
25
+ const rec = text.slice(sp + 1, i + len - 1);
26
+ const eq = rec.indexOf("=");
27
+ if (eq > 0 && rec.slice(0, eq) === "path") return rec.slice(eq + 1);
28
+ i += len;
29
+ }
30
+ return null;
31
+ }
32
+
33
+ /** Every regular file of a gzipped tarball, as `[{ path, data }]` in archive order. */
34
+ export function readTgz(tgz) {
35
+ const buf = gunzipSync(tgz);
36
+ const out = [];
37
+ let off = 0;
38
+ let longName = null;
39
+ while (off + 512 <= buf.length) {
40
+ const h = buf.subarray(off, off + 512);
41
+ if (h.every((b) => b === 0)) break;
42
+ const size = octal(h, 124, 12);
43
+ const type = String.fromCharCode(h[156] || 48);
44
+ const data = buf.subarray(off + 512, off + 512 + size);
45
+ off += 512 + Math.ceil(size / 512) * 512;
46
+ if (type === "L") { longName = str(data, 0, data.length); continue; }
47
+ if (type === "x") { longName = paxPath(data) ?? longName; continue; }
48
+ if (type === "g") continue;
49
+ const prefix = str(h, 345, 155);
50
+ const name = longName ?? (prefix ? `${prefix}/${str(h, 0, 100)}` : str(h, 0, 100));
51
+ longName = null;
52
+ if (type === "0" || type === "\0" || type === "7") out.push({ path: name.replace(/^\.\//, ""), data: Buffer.from(data) });
53
+ }
54
+ return out;
55
+ }
package/lib/toolchain.mjs CHANGED
@@ -1,6 +1,7 @@
1
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.
2
+ // static FFmpeg, the Inter font, the Noto Color Emoji font and the scene libraries (GSAP, three.js, lottie-web, d3) are
3
+ // downloaded into $MOSIO_HOME and checked against the SHA-256 in the lock, the way scripts/bootstrap.sh does for a
4
+ // checkout. Nothing here needs root, curl, tar, unzip or xz.
4
5
  import { createHash } from "node:crypto";
5
6
  import { createReadStream, existsSync } from "node:fs";
6
7
  import { chmod, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
@@ -8,9 +9,10 @@ import { spawnSync } from "node:child_process";
8
9
  import { createGunzip } from "node:zlib";
9
10
  import { createWriteStream } from "node:fs";
10
11
  import { pipeline } from "node:stream/promises";
11
- import { join, dirname } from "node:path";
12
+ import { join, dirname, basename, normalize, isAbsolute } from "node:path";
12
13
  import { download } from "./download.mjs";
13
14
  import { extractZip } from "./zip.mjs";
15
+ import { readTgz } from "./tar.mjs";
14
16
 
15
17
  /** Chromium's runtime libraries, as scripts/bootstrap.sh installs them (Debian 13). */
16
18
  export const CHROMIUM_APT = [
@@ -25,12 +27,35 @@ export async function sha256File(file) {
25
27
  return h.digest("hex");
26
28
  }
27
29
 
30
+ /** Chrome for Testing's platform name for a Node platform/arch (`null`: the lock's top-level values, linux64). */
31
+ const CFT = { "darwin-arm64": "mac-arm64", "darwin-x64": "mac-x64", "win32-x64": "win64" };
32
+
33
+ /**
34
+ * The lock as it applies to one platform: `chrome_headless_shell` and `ffmpeg.static` get their `platforms` entry for
35
+ * this platform laid over the top-level (Linux x86-64) values. Versions and licences are shared.
36
+ */
37
+ export function forPlatform(lock, platform = process.platform, arch = process.arch) {
38
+ const key = `${platform}-${arch}`;
39
+ const over = (v, k) => (k && v?.platforms?.[k] ? { ...v, ...v.platforms[k] } : v);
40
+ return {
41
+ ...lock,
42
+ chrome_headless_shell: over(lock.chrome_headless_shell, CFT[key]),
43
+ ffmpeg: { ...lock.ffmpeg, static: over(lock.ffmpeg.static, key === "linux-x64" ? null : key) },
44
+ };
45
+ }
46
+
28
47
  export async function loadLock(kitRoot) {
29
- return JSON.parse(await readFile(join(kitRoot, "toolchain.lock.json"), "utf8"));
48
+ return forPlatform(JSON.parse(await readFile(join(kitRoot, "toolchain.lock.json"), "utf8")));
49
+ }
50
+
51
+ /** macOS: drop a quarantine attribute (a tarball downloaded with a browser) so Gatekeeper does not block the tools. */
52
+ export function unquarantine(path) {
53
+ if (process.platform === "darwin") run("xattr", ["-dr", "com.apple.quarantine", path]);
30
54
  }
31
55
 
32
- export function layout(home, lock) {
56
+ export function layout(home, lock, platform = process.platform) {
33
57
  const c = lock.chrome_headless_shell;
58
+ const exe = platform === "win32" ? ".exe" : "";
34
59
  const chromeDir = join(home, "browsers", `chrome-headless-shell-${c.version}`);
35
60
  const ff = join(home, "ffmpeg", "bin");
36
61
  const font = lock.fonts.download;
@@ -38,9 +63,10 @@ export function layout(home, lock) {
38
63
  chromeDir,
39
64
  chrome: join(chromeDir, c.binary_relpath),
40
65
  ffmpegBin: ff,
41
- ffmpeg: join(ff, "ffmpeg"),
42
- ffprobe: join(ff, "ffprobe"),
66
+ ffmpeg: join(ff, `ffmpeg${exe}`),
67
+ ffprobe: join(ff, `ffprobe${exe}`),
43
68
  fontFile: join(home, "fonts", font.file),
69
+ emojiFile: emojiFor(lock, platform) ? join(home, "fonts", emojiFor(lock, platform).file) : null,
44
70
  fontConf: join(home, "fontconfig", "fonts.conf"),
45
71
  };
46
72
  }
@@ -61,11 +87,28 @@ export function systemFfmpegIsPinned(lock) {
61
87
  return !!p && p.status === 0;
62
88
  }
63
89
 
90
+ /**
91
+ * The pinned emoji font, or null where it does not apply: `fonts.emoji.platforms` lists the Node platforms that need
92
+ * it (Linux; Windows and macOS have a system emoji font and Chrome there does not read fontconfig).
93
+ */
94
+ export function emojiFor(lock, platform = process.platform) {
95
+ const e = lock.fonts?.emoji;
96
+ return e && (!e.platforms || e.platforms.includes(platform)) ? e : null;
97
+ }
98
+
64
99
  export function systemHasInter() {
65
100
  const r = run("fc-list", [":family=Inter"]);
66
101
  return !!r && r.status === 0 && String(r.stdout).trim().length > 0;
67
102
  }
68
103
 
104
+ /** True when fontconfig already knows an emoji font the lock accepts (the Debian package fonts-noto-color-emoji). */
105
+ export function systemHasEmoji(lock) {
106
+ const e = emojiFor(lock);
107
+ if (!e) return true;
108
+ const r = run("fc-list", [`:family=${e.family}`]);
109
+ return !!r && r.status === 0 && String(r.stdout).trim().length > 0;
110
+ }
111
+
69
112
  async function fileHasSha(file, sha) {
70
113
  try { return (await sha256File(file)) === sha; } catch { return false; }
71
114
  }
@@ -83,17 +126,32 @@ export async function status(home, lock) {
83
126
  out.ffmpeg = { installed: ffHome || ffSys, source: ffHome ? "download" : ffSys ? "system (pinned Debian package)" : null, path: ffHome ? L.ffmpegBin : null, version: s.version };
84
127
  const fontHome = await fileHasSha(L.fontFile, f.file_sha256);
85
128
  const fontSys = !fontHome && systemHasInter();
86
- out.fonts = { installed: fontHome || fontSys, source: fontHome ? "download" : fontSys ? "system (fonts-inter)" : null, path: fontHome ? L.fontFile : null };
129
+ const e = emojiFor(lock);
130
+ const emojiHome = !!e && (await fileHasSha(L.emojiFile, e.file_sha256));
131
+ const emojiSys = !emojiHome && systemHasEmoji(lock);
132
+ out.fonts = {
133
+ installed: (fontHome || fontSys) && (emojiHome || emojiSys),
134
+ source: fontHome ? "download" : fontSys ? "system (fonts-inter)" : null,
135
+ path: fontHome ? L.fontFile : null,
136
+ emoji: { installed: emojiHome || emojiSys, source: emojiHome ? "download" : emojiSys ? "system" : null, path: emojiHome ? L.emojiFile : null },
137
+ };
138
+ const libs = libraries(home, lock);
139
+ out.libraries = { installed: await librariesInstalled(home, lock), source: "download", items: libs.map((l) => `${l.name}@${l.version}`), path: join(home, "lib") };
87
140
  return out;
88
141
  }
89
142
 
143
+ // every 1.5 s: MB, percent and speed, so a slow connection never looks like a hang
90
144
  function progress(label, log) {
91
145
  let last = 0;
146
+ const start = Date.now();
92
147
  return (bytes, total) => {
93
148
  const now = Date.now();
94
149
  if (now - last < 1500 && bytes !== total) return;
95
150
  last = now;
96
- log(` ${label}: ${mb(bytes)}${total ? ` of ${mb(total)}` : ""}`);
151
+ const pct = total ? ` (${Math.floor((bytes / total) * 100)}%)` : "";
152
+ const secs = (now - start) / 1000;
153
+ const rate = secs >= 1 ? `, ${(bytes / 1048576 / secs).toFixed(1)} MB/s` : "";
154
+ log(` ${label}: ${mb(bytes)}${total ? ` of ${mb(total)}` : ""}${pct}${rate}`);
97
155
  };
98
156
  }
99
157
 
@@ -101,7 +159,7 @@ async function installChromium(home, lock, log) {
101
159
  const c = lock.chrome_headless_shell;
102
160
  const L = layout(home, lock);
103
161
  const dl = join(home, "downloads");
104
- const zip = join(dl, `chrome-headless-shell-${c.version}-linux64.zip`);
162
+ const zip = join(dl, `chrome-headless-shell-${c.version}-${c.url.split("/").at(-2)}.zip`);
105
163
  log(`Chromium ${c.version} (chrome-headless-shell, ${c.url})`);
106
164
  const t0 = Date.now();
107
165
  const { bytes } = await download(c.url, zip, { sha256: c.zip_sha256, onProgress: progress("downloading", log), label: "chrome-headless-shell zip" });
@@ -113,6 +171,7 @@ async function installChromium(home, lock, log) {
113
171
  if (!(await fileHasSha(join(tmp, c.binary_relpath), c.binary_sha256))) throw new Error("chrome-headless-shell binary SHA-256 mismatch after unzip");
114
172
  await rm(L.chromeDir, { recursive: true, force: true });
115
173
  await rename(tmp, L.chromeDir);
174
+ unquarantine(L.chromeDir);
116
175
  } catch (e) {
117
176
  await rm(tmp, { recursive: true, force: true });
118
177
  throw e;
@@ -130,6 +189,21 @@ async function gunzipTo(src, dest, sha, mode) {
130
189
  await rename(tmp, dest);
131
190
  }
132
191
 
192
+ async function unzipOne(zip, member, dest, sha) {
193
+ const tmp = `${dest}.unzip-${process.pid}`;
194
+ await rm(tmp, { recursive: true, force: true });
195
+ await mkdir(tmp, { recursive: true });
196
+ try {
197
+ await extractZip(zip, tmp);
198
+ const got = join(tmp, member);
199
+ if ((await sha256File(got)) !== sha) throw new Error(`${dest}: SHA-256 mismatch after unzip`);
200
+ await chmod(got, 0o755);
201
+ await rename(got, dest);
202
+ } finally {
203
+ await rm(tmp, { recursive: true, force: true });
204
+ }
205
+ }
206
+
133
207
  async function installFfmpeg(home, lock, log) {
134
208
  const s = lock.ffmpeg.static;
135
209
  const L = layout(home, lock);
@@ -138,13 +212,29 @@ async function installFfmpeg(home, lock, log) {
138
212
  const t0 = Date.now();
139
213
  let bytes = 0;
140
214
  await mkdir(L.ffmpegBin, { recursive: true });
215
+ // zips by SHA-256: ffmpeg and ffprobe may come from the same archive (Windows), downloaded once
216
+ const zips = new Map();
141
217
  for (const [name, spec, dest] of [["ffmpeg", s.ffmpeg, L.ffmpeg], ["ffprobe", s.ffprobe, L.ffprobe]]) {
218
+ if (s.archive === "zip") {
219
+ // macOS builds: one zip per binary holding just `ffmpeg` / `ffprobe`; Windows: one zip, `member` names the file
220
+ let zip = zips.get(spec.zip_sha256);
221
+ if (!zip) {
222
+ zip = join(dl, `ffmpeg-${s.version}-${process.platform}-${process.arch}-${spec.zip_sha256.slice(0, 12)}.zip`);
223
+ const r = await download(spec.url, zip, { sha256: spec.zip_sha256, onProgress: progress(name, log), label: `${name} zip` });
224
+ bytes += r.bytes;
225
+ zips.set(spec.zip_sha256, zip);
226
+ }
227
+ await unzipOne(zip, spec.member ?? name, dest, spec.binary_sha256);
228
+ continue;
229
+ }
142
230
  const gz = join(dl, `${name}-${s.version}-linux-x64.gz`);
143
231
  const r = await download(spec.url, gz, { sha256: spec.gz_sha256, onProgress: progress(name, log), label: `${name} gz` });
144
232
  bytes += r.bytes;
145
233
  await gunzipTo(gz, dest, spec.binary_sha256, 0o755);
146
234
  await rm(gz, { force: true });
147
235
  }
236
+ for (const zip of zips.values()) await rm(zip, { force: true });
237
+ unquarantine(L.ffmpegBin);
148
238
  const lic = join(dirname(L.ffmpegBin), "LICENSE.txt");
149
239
  const r = await download(s.license_file.url, lic, { sha256: s.license_file.sha256, label: "FFmpeg licence text" });
150
240
  bytes += r.bytes;
@@ -155,19 +245,87 @@ async function installFfmpeg(home, lock, log) {
155
245
  async function installFonts(home, lock, log) {
156
246
  const f = lock.fonts.download;
157
247
  const L = layout(home, lock);
158
- log(`Font ${f.family} ${f.version} (${f.license})`);
159
248
  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
249
+ let bytes = 0;
250
+ if (!(await fileHasSha(L.fontFile, f.file_sha256)) && !systemHasInter()) {
251
+ log(`Font ${f.family} ${f.version} (${f.license})`);
252
+ bytes += (await download(f.url, L.fontFile, { sha256: f.file_sha256, label: "Inter font" })).bytes;
253
+ bytes += (await download(f.license_file.url, join(dirname(L.fontFile), "Inter-LICENSE.txt"), { sha256: f.license_file.sha256, label: "Inter licence" })).bytes;
254
+ }
255
+ // emoji: no project ships an emoji font, so without this every emoji renders as an empty box
256
+ const e = emojiFor(lock);
257
+ if (e && !(await fileHasSha(L.emojiFile, e.file_sha256)) && !systemHasEmoji(lock)) {
258
+ log(`Font ${e.family} ${e.version} (${e.license})`);
259
+ bytes += (await download(e.url, L.emojiFile, { sha256: e.file_sha256, label: "emoji font" })).bytes;
260
+ bytes += (await download(e.license_file.url, join(dirname(L.emojiFile), "NotoColorEmoji-LICENSE.txt"), { sha256: e.license_file.sha256, label: "emoji font licence" })).bytes;
261
+ }
262
+ // the browser reads fontconfig: the system configuration plus the downloaded directory, with the pinned emoji font
263
+ // preferred for the generic "emoji" family (Chromium's fallback for emoji characters asks fontconfig for it)
163
264
  const esc = (x) => x.replace(/&/g, "&amp;").replace(/</g, "&lt;");
265
+ const alias = e ? ` <alias binding="same">\n <family>emoji</family>\n <prefer><family>${esc(e.family)}</family></prefer>\n </alias>\n` : "";
164
266
  await mkdir(dirname(L.fontConf), { recursive: true });
165
267
  await writeFile(
166
268
  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`,
269
+ `<?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${alias}</fontconfig>\n`,
168
270
  );
169
- log(` installed at ${L.fontFile}`);
170
- return { bytes: r.bytes + l.bytes, seconds: (Date.now() - t0) / 1000 };
271
+ log(` installed in ${dirname(L.fontFile)}`);
272
+ return { bytes, seconds: (Date.now() - t0) / 1000 };
273
+ }
274
+
275
+ /**
276
+ * The scene libraries of the lock (`libraries`: GSAP, three.js, lottie-web, d3), each where `mosio` looks for it:
277
+ * `$MOSIO_HOME/lib/<name>@<version>/<file>`, `file` being `file_at`, else the base name of `file_in_tarball`.
278
+ * Registry scenes and recipes declare them (`"libraries": ["gsap@3.15.1"]`), so a render needs them on disk.
279
+ */
280
+ export function libraries(home, lock) {
281
+ return Object.entries(lock.libraries ?? {}).map(([name, l]) => {
282
+ const dir = join(home, "lib", `${name}@${l.version}`);
283
+ return { name, version: l.version, spec: l, dir, file: join(dir, l.file_at ?? basename(l.file_in_tarball)) };
284
+ });
285
+ }
286
+
287
+ async function installLibraries(home, lock, log) {
288
+ const t0 = Date.now();
289
+ let bytes = 0;
290
+ for (const lib of libraries(home, lock)) {
291
+ const l = lib.spec;
292
+ if (await fileHasSha(lib.file, l.file_sha256)) continue;
293
+ log(`Library ${lib.name}@${lib.version} (${l.tarball})`);
294
+ const tgz = join(home, "downloads", `${lib.name}-${lib.version}.tgz`);
295
+ bytes += (await download(l.tarball, tgz, { sha256: l.tarball_sha256, label: `${lib.name} tarball` })).bytes;
296
+ const entries = readTgz(await readFile(tgz));
297
+ // extract_dirs keeps the layout below the package root (three: build/, examples/jsm/); otherwise the one file
298
+ const pick = l.extract_dirs
299
+ ? entries.filter((e) => l.extract_dirs.some((d) => e.path === d || e.path.startsWith(`${d}/`))).map((e) => [e, e.path.split("/").slice(1).join("/")])
300
+ : entries.filter((e) => e.path === l.file_in_tarball).map((e) => [e, basename(e.path)]);
301
+ const tmp = `${lib.dir}.tmp-${process.pid}`;
302
+ await rm(tmp, { recursive: true, force: true });
303
+ try {
304
+ for (const [e, rel] of pick) {
305
+ const n = normalize(rel);
306
+ if (!n || isAbsolute(n) || n.startsWith("..")) throw new Error(`${lib.name}: unsafe path ${e.path} in the tarball`);
307
+ await mkdir(dirname(join(tmp, n)), { recursive: true });
308
+ await writeFile(join(tmp, n), e.data);
309
+ }
310
+ const rel = l.file_at ?? basename(l.file_in_tarball);
311
+ if (!(await fileHasSha(join(tmp, rel), l.file_sha256))) throw new Error(`${lib.name}: SHA-256 mismatch of ${rel} after extraction`);
312
+ await rm(lib.dir, { recursive: true, force: true });
313
+ await mkdir(dirname(lib.dir), { recursive: true });
314
+ await rename(tmp, lib.dir);
315
+ } catch (e) {
316
+ await rm(tmp, { recursive: true, force: true });
317
+ throw e;
318
+ } finally {
319
+ await rm(tgz, { force: true });
320
+ }
321
+ log(` installed at ${lib.dir}`);
322
+ }
323
+ return { bytes, seconds: (Date.now() - t0) / 1000 };
324
+ }
325
+
326
+ async function librariesInstalled(home, lock) {
327
+ for (const lib of libraries(home, lock)) if (!(await fileHasSha(lib.file, lib.spec.file_sha256))) return false;
328
+ return true;
171
329
  }
172
330
 
173
331
  const stampFile = (home) => join(home, ".mosio-setup.json");
@@ -188,10 +346,17 @@ export async function quickMissing(home, lock) {
188
346
  const ffOk = stamp.ffmpeg === lock.ffmpeg.static.ffmpeg.binary_sha256 && existsSync(L.ffmpeg) && existsSync(L.ffprobe);
189
347
  if (!ffOk && !systemFfmpegIsPinned(lock)) missing.push("ffmpeg");
190
348
  const fontOk = stamp.fonts === lock.fonts.download.file_sha256 && existsSync(L.fontFile);
191
- if (!fontOk && !systemHasInter()) missing.push("fonts");
349
+ const e = emojiFor(lock);
350
+ const emojiOk = !e || (stamp.emoji === e.file_sha256 && existsSync(L.emojiFile));
351
+ if ((!fontOk && !systemHasInter()) || (!emojiOk && !systemHasEmoji(lock))) missing.push("fonts");
352
+ const libs = libraries(home, lock);
353
+ if (libs.length && !(stamp.libraries === libsKey(lock) && libs.every((l) => existsSync(l.file)))) missing.push("libraries");
192
354
  return missing;
193
355
  }
194
356
 
357
+ /** What the stamp records for the libraries: their pinned file hashes, so a new lock fetches again. */
358
+ const libsKey = (lock) => Object.values(lock.libraries ?? {}).map((l) => l.file_sha256).join(",");
359
+
195
360
  /** The shared libraries `ldd` cannot resolve for the browser (empty when it can start, or ldd is missing). */
196
361
  export function missingChromeLibs(chrome) {
197
362
  const r = run("ldd", [chrome]);
@@ -204,7 +369,7 @@ export function aptHint(libs) {
204
369
  }
205
370
 
206
371
  /**
207
- * Install what is missing. `want` = { chromium, ffmpeg, fonts } (booleans). Returns a report; throws on a failed
372
+ * Install what is missing. `want` = { chromium, ffmpeg, fonts, libraries } (booleans; `libraries` left out means yes). Returns a report; throws on a failed
208
373
  * download or a hash mismatch (nothing is installed in that case).
209
374
  */
210
375
  export async function ensure(home, lock, want, { log = () => {}, force = false } = {}) {
@@ -216,6 +381,7 @@ export async function ensure(home, lock, want, { log = () => {}, force = false }
216
381
  ["chromium", want.chromium, st.chromium?.installed, installChromium],
217
382
  ["ffmpeg", want.ffmpeg, st.ffmpeg?.installed, installFfmpeg],
218
383
  ["fonts", want.fonts, st.fonts?.installed, installFonts],
384
+ ["libraries", want.libraries !== false, st.libraries?.installed, installLibraries],
219
385
  ];
220
386
  for (const [name, wanted, have, fn] of steps) {
221
387
  if (!wanted) { report.push({ name, state: "skipped" }); continue; }
@@ -229,6 +395,8 @@ export async function ensure(home, lock, want, { log = () => {}, force = false }
229
395
  if (after.chromium.installed) stamp.chromium = lock.chrome_headless_shell.binary_sha256;
230
396
  if (after.ffmpeg.source === "download") stamp.ffmpeg = lock.ffmpeg.static.ffmpeg.binary_sha256;
231
397
  if (after.fonts.source === "download") stamp.fonts = lock.fonts.download.file_sha256;
398
+ if (after.fonts.emoji.source === "download") stamp.emoji = emojiFor(lock).file_sha256;
399
+ if (after.libraries.installed) stamp.libraries = libsKey(lock);
232
400
  await writeFile(stampFile(home), JSON.stringify(stamp, null, 2) + "\n");
233
401
  const L = layout(home, lock);
234
402
  let libs = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usemosio/cli",
3
- "version": "1.0.0-next.1",
3
+ "version": "1.0.0-next.4",
4
4
  "description": "Mosio: agent-first programmatic video on a pinned Chromium. The `mosio` command (init, check, render, studio, mcp).",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "type": "module",
@@ -19,7 +19,10 @@
19
19
  "node": ">=22.12"
20
20
  },
21
21
  "optionalDependencies": {
22
- "@usemosio/cli-linux-x64": "1.0.0-next.1"
22
+ "@usemosio/cli-linux-x64": "1.0.0-next.4",
23
+ "@usemosio/cli-darwin-arm64": "1.0.0-next.4",
24
+ "@usemosio/cli-darwin-x64": "1.0.0-next.4",
25
+ "@usemosio/cli-win32-x64": "1.0.0-next.4"
23
26
  },
24
27
  "publishConfig": {
25
28
  "access": "public"