diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 39a44c0eb..ee4528d96 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -119,3 +119,15 @@ jobs: env: CARGO_PROFILE_TEST_DEBUG: "0" run: cargo test --locked -p raea-runtime-plugins -p raea-io-media -p raea-core-fonts -p raea-native -p raea-core-brief -p raea-core-k -p raea-bundle -p raea-app-check -p raea-app-check-flash -p raea-io-ffmpeg -p raea-app-workspace -p raea-app-tools -p raea-app-targets + - name: Windows' and Linux's own (#169) + # what a Windows or Linux user meets that no other step ran: QuickJS's Intl (raea-runtime-jsengine's + # quickjs_formats_as_javascriptcore_does, on Linux), openh264 at --quality master (raea-io-soft), git found or + # fetched (raea-io-assets), and raea's Node core under the agent shell (raea-native's tests/node.rs: Node's names + # for the system, Windows' paths); tests/node.rs needs raea-native's modules feature and tests/quality.rs + # raea-io-soft's h264 + env: + CARGO_PROFILE_TEST_DEBUG: "0" + run: | + cargo test --locked -p raea-native --features modules --test node + cargo test --locked -p raea-io-soft --features h264 + cargo test --locked -p raea-runtime-jsengine -p raea-io-assets diff --git a/Cargo.lock b/Cargo.lock index bb598681a..a475148c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14051,6 +14051,7 @@ dependencies = [ "raea-app-registry", "raea-core-brief", "raea-core-facts", + "raea-io-assets", "raea-io-home", "serde_json", ] @@ -15349,6 +15350,8 @@ dependencies = [ name = "raea-runtime-jsengine" version = "0.1.0" dependencies = [ + "iana-time-zone", + "miniz_oxide 0.8.9", "objc2 0.6.5", "raea-io-home", "rquickjs", diff --git a/NOTICE b/NOTICE index 2f9f860eb..c678e6f9e 100644 --- a/NOTICE +++ b/NOTICE @@ -30,6 +30,10 @@ paths for Windows in runtime/bundle). Its bin\ also carries, each with its licen uv (uv.exe; Astral, MIT OR Apache-2.0). The Windows app adds Git for Windows with Git LFS (dugite-native; GPLv2 and its parts' licences, in git\), which raea runs as a separate program; its complete corresponding source is published as the release tools-git--sources. +raea.exe, on a PC with no Git, fetches the same build once into its tools cache (io/assets git()). +The Windows and Linux builds' JavaScript engine, QuickJS (rquickjs, MIT), carries FormatJS's Intl polyfills (MIT, +Copyright FormatJS) and the Unicode CLDR locale data they're made with (Unicode-3.0), packed in +runtime/jsengine/js/intl.pack; both licences are in runtime/jsengine/js/intl/LICENSE-FormatJS.md and in the pack. The native target (engine/rune, docs/targets.feature) builds on Rune (https://github.com/anomalyco/rune, Copyright 2026 Anomaly, MIT; LICENSES/rune-0.0.1/repository-LICENSE): rune, rune-kit and their crates, by a pinned rev. diff --git a/apps/workspace/Cargo.toml b/apps/workspace/Cargo.toml index bcb951141..e61be5e60 100644 --- a/apps/workspace/Cargo.toml +++ b/apps/workspace/Cargo.toml @@ -11,6 +11,8 @@ dunce = "1" # the controller schema (core/brief controller.rs): a controller file is checked and normalized against it raea-core-brief = { path = "../../core/brief" } raea-io-home = { path = "../../io/home" } +# git, which a workspace is: the PATH's, else on Windows the app's or raea's own (raea_io_assets::git) +raea-io-assets = { path = "../../io/assets" } serde_json = "1" # a project's compositions, components and controllers read from their source (docs/projects.feature PJ8) raea-app-registry = { path = "registry" } diff --git a/apps/workspace/src/controllers.rs b/apps/workspace/src/controllers.rs index f57f299e9..6e7f6abf4 100644 --- a/apps/workspace/src/controllers.rs +++ b/apps/workspace/src/controllers.rs @@ -62,10 +62,10 @@ pub(crate) fn overrides(from: &Path) -> Vec<(String, String)> { } fn commit(ws: &Path, paths: &[&Path], what: &str, by: &str) -> R<()> { - let mut add = Command::new("git"); + let mut add = Command::new(workspace::program()); add.arg("-C").arg(ws).args(["add", "-A", "--"]).args(paths); - add.status().map_err(|e| format!("git: {e}"))?; - let ok = workspace::git_cmd().arg("-C").arg(ws).args(["-c", &format!("user.name={by}"), "commit", "-q", "-m", &format!("{by}: {what}"), "--"]).args(paths).status().map_err(|e| format!("git: {e}"))?; + add.status().map_err(workspace::missing)?; + let ok = workspace::git_cmd().arg("-C").arg(ws).args(["-c", &format!("user.name={by}"), "commit", "-q", "-m", &format!("{by}: {what}"), "--"]).args(paths).status().map_err(workspace::missing)?; if ok.success() { Ok(()) } else { Err(format!("couldn't commit {what}")) } } diff --git a/apps/workspace/src/workspace.rs b/apps/workspace/src/workspace.rs index 80d1b76d6..8d36c5cb9 100644 --- a/apps/workspace/src/workspace.rs +++ b/apps/workspace/src/workspace.rs @@ -13,7 +13,7 @@ const USAGE: &str = "usage: raea workspace [--json] | raea workspace init [NAME] /// full name (a CI runner's, a container's) has no name either ("empty ident name ... not allowed"), and git has no /// fallback below user.name to give it, so when no user.name is set the author and committer are named for the account. pub(crate) fn git_cmd() -> Command { - let mut c = Command::new("git"); + let mut c = Command::new(program()); let user = std::env::var("USER").unwrap_or_else(|_| "raea".into()); if std::env::var_os("EMAIL").is_none() { c.env("EMAIL", format!("{user}@raea.local")); @@ -24,14 +24,33 @@ pub(crate) fn git_cmd() -> Command { c } +/// The git raea runs (raea_io_assets::git: the PATH's, else on Windows the app's or raea's own, fetched once), found +/// once a process; "git" when there's none, its spawn then failing as `missing` says. +fn found() -> &'static Result { + static GIT: std::sync::OnceLock> = std::sync::OnceLock::new(); + GIT.get_or_init(raea_io_assets::git) +} + +pub(crate) fn program() -> std::path::PathBuf { + found().clone().unwrap_or_else(|_| "git".into()) +} + +/// A git that didn't start, said as why: there's no git here (and how to get one), or what the system said +pub(crate) fn missing(e: std::io::Error) -> String { + match found() { + Err(why) if e.kind() == std::io::ErrorKind::NotFound => why.clone(), + _ => format!("git: {e}"), + } +} + /// Whether git has a user.name to sign commits with (asked once a process). fn has_name() -> bool { static HAS: std::sync::OnceLock = std::sync::OnceLock::new(); - *HAS.get_or_init(|| Command::new("git").args(["config", "--get", "user.name"]).output().is_ok_and(|o| o.status.success() && !o.stdout.trim_ascii().is_empty())) + *HAS.get_or_init(|| Command::new(program()).args(["config", "--get", "user.name"]).output().is_ok_and(|o| o.status.success() && !o.stdout.trim_ascii().is_empty())) } pub(crate) fn git(dir: &Path, args: &[&str]) -> R { - let out = git_cmd().arg("-C").arg(dir).args(args).output().map_err(|e| format!("git: {e}"))?; + let out = git_cmd().arg("-C").arg(dir).args(args).output().map_err(missing)?; if !out.status.success() { return Err(format!("git {}: {}", args.join(" "), String::from_utf8_lossy(&out.stderr).trim())); } @@ -40,12 +59,12 @@ pub(crate) fn git(dir: &Path, args: &[&str]) -> R { /// `git config` on a file (or the global config when `file` is None), its output trimmed; Err when git says no. pub(crate) fn config(file: Option<&Path>, args: &[&str]) -> R { - let mut c = Command::new("git"); + let mut c = Command::new(program()); c.arg("config"); if let Some(f) = file { c.arg("-f").arg(f); } - let out = c.args(args).output().map_err(|e| format!("git: {e}"))?; + let out = c.args(args).output().map_err(missing)?; if !out.status.success() { return Err(format!("git config {}: {}", args.join(" "), String::from_utf8_lossy(&out.stderr).trim())); } @@ -111,7 +130,8 @@ pub fn default_root(home: &Path) -> PathBuf { if home.join("Library/Mobile Documents/com~apple~CloudDocs/Documents").is_dir() { home.join("raea") } else { - home.join("Documents/raea") + // a folder at a time: on Windows "Documents/raea" printed as C:\Users\you\Documents/raea\... + home.join("Documents").join("raea") } } diff --git a/engine/cli/conformance/app.test.ts b/engine/cli/conformance/app.test.ts new file mode 100644 index 000000000..60e173c7f --- /dev/null +++ b/engine/cli/conformance/app.test.ts @@ -0,0 +1,190 @@ +// `raea show` and its app on a Mac, Windows and Linux (src/app.ts), each system faked (a Here): where the running +// app's host is, which app is installed, how files open without it, how it's started, and the harness found up the +// processes. #169: on Windows `raea show` asked `kill` whether the app ran, looked only in /Applications, and said +// it had opened the files "in the Mac's own viewer". +import { afterAll, describe, expect, test } from "bun:test"; +import { appHost, computer, harness, installedApp, openFiles, show, type Here } from "../src/app"; +import { pixelsOf } from "../src/brand"; +import { absolute, fromCwd } from "../src/show"; + +const WIN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\raea"; + +/** A system: `platform`, its files (path → text), and its programs' answers; every run is kept in `ran`. */ +function here(platform: string, files: Record = {}, answer: (cmd: string, args: string[]) => { status?: number; stdout?: string } = () => ({})) { + const ran: string[][] = []; + const h: Here & { ran: string[][] } = { + platform, + ran, + run: (cmd, args) => { + ran.push([cmd, ...args]); + const a = answer(cmd, args); + return { status: a.status ?? 0, stdout: a.stdout ?? "" }; + }, + exists: (p) => p in files, + read: (p) => { + if (!(p in files)) throw new Error(`ENOENT ${p}`); + return files[p]; + }, + env: { RAEA_NO_OPEN: undefined }, + home: platform === "win32" ? "C:/Users/me" : "/home/you", + }; + return h; +} + +// the app's host, as the window's (apps/app host/show.ts): an item's link for a show, and /host/shown to say it finds it. +// Its own process: show() posts with a synchronous curl, which a server in this one couldn't answer +const host = Bun.spawn(["bun", "-e", `const s = Bun.serve({ port: 0, fetch: (r) => { const p = new URL(r.url).pathname; return p === "/host/show" ? Response.json({ url: "raea://item/7" }) : p === "/host/shown" ? Response.json({}) : new Response("not found", { status: 404 }); } }); console.log(s.port);`], { stdout: "pipe" }); +afterAll(() => host.kill()); +/** its first line: the port it listens on */ +async function portOf(out: ReadableStream) { + const r = out.getReader(); + let text = ""; + while (!text.includes("\n")) { + const { value, done } = await r.read(); + if (done) throw new Error(`the test's host stopped before it listened: ${text}`); + text += new TextDecoder().decode(value); + } + r.releaseLock(); + return Number(text.trim()); +} +const HOST = `http://127.0.0.1:${await portOf(host.stdout)}`; + +describe("the running app's host", () => { + test("on Windows, its pid is asked of tasklist (Windows has no kill)", () => { + const file = { "C:/Users/me/.raea/app-host.json": JSON.stringify({ url: HOST, pid: 4321 }) }; + const up = here("win32", file, (cmd, args) => (cmd === "tasklist" && args.includes("PID eq 4321") ? { stdout: '"raea.exe","4321","Console","1","80,000 K"\r\n' } : {})); + expect(appHost(up)).toBe(HOST); + expect(up.ran.some(([c]) => c === "kill")).toBe(false); + // a file a stopped app left: tasklist finds no such process ("INFO: No tasks are running...") + const gone = here("win32", file, () => ({ stdout: "INFO: No tasks are running which match the specified criteria.\r\n" })); + expect(appHost(gone)).toBeNull(); + }); + + test("on a Mac or Linux, kill -0", () => { + const file = { "/home/you/.raea/app-host.json": JSON.stringify({ url: HOST, pid: 99 }) }; + expect(appHost(here("linux", file, (cmd) => ({ status: cmd === "kill" ? 0 : 1 })))).toBe(HOST); + expect(appHost(here("linux", file, (cmd) => ({ status: cmd === "kill" ? 1 : 0 })))).toBeNull(); + }); +}); + +describe("the installed app", () => { + test("on Windows, raea.exe in the folder its installer was given (Settings > Apps keeps it)", () => { + const reg = `\r\nHKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\raea\r\n InstallLocation REG_SZ D:\\Apps\\raea\r\n\r\n`; + const up = here("win32", { "D:\\Apps\\raea\\raea.exe": "" }, (cmd, args) => (cmd === "reg" && args[1] === WIN_KEY ? { stdout: reg } : {})); + expect(installedApp(up)).toBe("D:\\Apps\\raea\\raea.exe"); + }); + + test("on Windows with no InstallLocation, the default folder", () => { + const up = here("win32", { "C:\\Users\\me\\AppData\\Local\\Programs\\raea\\raea.exe": "" }, () => ({ status: 1 })); + up.env.LOCALAPPDATA = "C:\\Users\\me\\AppData\\Local"; + expect(installedApp(up)).toBe("C:\\Users\\me\\AppData\\Local\\Programs\\raea\\raea.exe"); + }); + + test("on a Mac, raea.app in Applications; on Linux, the launcher raea.desktop runs", () => { + expect(installedApp(here("darwin", { "/Applications/raea.app": "" }))).toBe("/Applications/raea.app"); + const desktop = "[Desktop Entry]\nName=raea\nExec=\"/home/you/.local/share/raea-app/launch.sh\"\n"; + expect(installedApp(here("linux", { "/home/you/.local/share/applications/raea.desktop": desktop, "/home/you/.local/share/raea-app/launch.sh": "" }))).toBe("/home/you/.local/share/raea-app/launch.sh"); + expect(installedApp(here("linux"))).toBeUndefined(); + }); +}); + +describe("raea show without the app", () => { + test("on Windows, each file opens in Windows' own viewer through Explorer, its path in Windows' slashes", () => { + const up = here("win32", {}, () => ({ status: 1 })); + const r = show({ files: ["C:/Users/me/cut & keep/a.mp4", "C:/Users/me/b.png"] }, up) as { opened: string[]; viewer: string }; + expect(r.viewer).toBe("Windows' own viewer"); + expect(up.ran.filter(([c]) => c === "explorer.exe")).toEqual([["explorer.exe", "C:\\Users\\me\\cut & keep\\a.mp4"], ["explorer.exe", "C:\\Users\\me\\b.png"]]); + expect(up.ran.some(([c]) => c === "open")).toBe(false); + }); + + test("a question needs the app, said for the system it runs on", () => { + expect(() => show({ files: [], question: "Which?" }, here("win32", {}, () => ({ status: 1 })))).toThrow("isn't on this PC"); + expect(() => show({ files: [], question: "Which?" }, here("darwin"))).toThrow("isn't on this Mac"); + expect(computer(here("linux")).name).toBe("this computer"); + }); + + test("on Linux, xdg-open", () => { + const up = here("linux"); + openFiles(["/home/you/a.mp4"], up); + expect(up.ran).toEqual([["xdg-open", "/home/you/a.mp4"]]); + }); +}); + +describe("raea show starting the app", () => { + test("on Windows: raea.exe started through Explorer, its host waited for, the item posted, no raea:// link", () => { + const files: Record = { "C:\\Users\\me\\AppData\\Local\\Programs\\raea\\raea.exe": "" }; + const up = here("win32", files, (cmd, args) => { + // the app, once started, writes its host's address + if (cmd === "explorer.exe" && args[0].endsWith("raea.exe")) files["C:/Users/me/.raea/app-host.json"] = JSON.stringify({ url: HOST, pid: 77 }); + if (cmd === "tasklist") return { stdout: '"raea.exe","77","Console","1","1 K"' }; + return { status: cmd === "reg" ? 1 : 0 }; + }); + up.env.LOCALAPPDATA = "C:\\Users\\me\\AppData\\Local"; + const r = show({ files: ["C:/Users/me/a.mp4"] }, up) as { url: string; brought: boolean }; + expect(r).toEqual({ url: "raea://item/7", brought: false }); + expect(up.ran.some(([c]) => c === "open" || c === "pgrep")).toBe(false); + }); + + test("on a Mac: open -a, and the window brought up on the item it was started for", () => { + const files: Record = { "/Applications/raea.app": "" }; + const up = here("darwin", files, (cmd, args) => { + if (cmd === "open" && args[0] === "-a") files["/home/you/.raea/app-host.json"] = JSON.stringify({ url: HOST, pid: 77 }); + return { status: cmd === "pgrep" ? 1 : 0 }; + }); + expect(show({ files: ["/tmp/a.mp4"] }, up)).toEqual({ url: "raea://item/7", brought: true }); + expect(up.ran.at(-1)).toEqual(["open", "raea://item/7"]); + }); +}); + +describe("the harness that ran raea show", () => { + test("on Windows, found up every process's record from one PowerShell call, claude.exe as claude", () => { + const list = [`${process.pid}\t500\t"C:\\Users\\me\\raea\\bin\\raea.exe" show a.mp4`, `500\t400\t"C:\\Users\\me\\.local\\bin\\claude.exe" --resume`, `400\t1\tC:\\Windows\\explorer.exe`].join("\r\n"); + const up = here("win32", {}, (cmd) => (cmd === "powershell" ? { stdout: list } : { status: 1 })); + expect(harness(up)).toEqual({ name: "Claude Code", pid: 500 }); + expect(up.ran.filter(([c]) => c === "powershell")).toHaveLength(1); + }); + + test("a runtime's script by its name, the quoted path's spaces kept", () => { + const list = [`${process.pid}\t600\traea.exe show`, `600\t1\t"C:\\Program Files\\nodejs\\node.exe" C:\\Users\\me\\AppData\\Roaming\\npm\\gemini.cmd`].join("\n"); + expect(harness(here("win32", {}, () => ({ stdout: list })))).toEqual({ name: "Gemini", pid: 600 }); + }); + + test("on a Mac or Linux, ps up from the parent", () => { + const up = here("linux", {}, (cmd, args) => (cmd === "ps" && args.at(-1) === String(process.ppid) ? { stdout: " 1 /usr/local/bin/codex exec" } : { stdout: "" })); + expect(harness(up)).toEqual({ name: "Codex", pid: process.ppid }); + }); +}); + +describe("a path from the cwd", () => { + test("a Windows one is absolute, with either slash, a share's too", () => { + for (const p of ["C:\\Users\\me\\a.mp4", "c:/x", "\\\\nas\\clips\\a.mp4", "\\x", "/x"]) expect(absolute(p)).toBe(true); + for (const p of ["a.mp4", "cuts\\a.mp4", "C:a.mp4", "./a"]) expect(absolute(p)).toBe(false); + expect(fromCwd("C:/Users/me/proj", "D:\\clips\\a.mp4")).toBe("D:\\clips\\a.mp4"); + expect(fromCwd("C:/Users/me/proj/", "out\\a.png")).toBe("C:/Users/me/proj/out\\a.png"); + expect(fromCwd("/home/you", "a.png")).toBe("/home/you/a.png"); + }); +}); + +describe("a logo's pixels, on any system", () => { + // until 0.1.8 asked of sips, a Mac's: on Windows and Linux every raster logo in brand.json had no size. Each is the + // head of a 37x23 image from a real encoder (ffmpeg's PNG, GIF and ICO; cwebp's lossy and lossless; Pillow's WebP with + // alpha, VP8X; sips's JPEG, an Exif and a Photoshop block before its frame; ImageMagick's progressive JPEG) + const HEADS: Record = { + png: "iVBORw0KGgoAAAANSUhEUgAAACUAAAAX", + gif: "R0lGODlhJQAXAA==", + "webp lossy": "UklGRiACAABXRUJQVlA4IBQCAAAwDwCdASolABcA", + "webp lossless": "UklGRrIAAABXRUJQVlA4TKUAAAAvJIAF", + "webp with alpha": "UklGRkwCAABXRUJQVlA4IEACAABQDwCdASolABcA", + ico: "AAABAAEAJRc=", + jpeg: "/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAJaADAAQAAAABAAAAFwAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAFwAlAw==", + "jpeg progressive": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wgARCAAXACUD", + }; + for (const [kind, head] of Object.entries(HEADS)) { + test(kind, () => expect(pixelsOf(Buffer.from(head, "base64"))).toEqual({ width: 37, height: 23 })); + } + test("anything else, or a header cut short, has none", () => { + expect(pixelsOf(new TextEncoder().encode("II*\0 a TIFF"))).toBeUndefined(); + expect(pixelsOf(Buffer.from(HEADS.png, "base64").subarray(0, 18))).toBeUndefined(); + expect(pixelsOf(new Uint8Array())).toBeUndefined(); + }); +}); diff --git a/engine/cli/src/app.ts b/engine/cli/src/app.ts index 51f799746..2c39525a6 100644 --- a/engine/cli/src/app.ts +++ b/engine/cli/src/app.ts @@ -1,25 +1,104 @@ // The raea app's host, as the native `raea` reaches it (native.ts): where the running app listens, and the verbs that -// post to it: `raea show` (and its --on tasks), `raea library`, and the agent harness a show names as its poster. +// post to it: `raea show` (and its --on tasks), `raea library`, and the agent harness a show names as its poster. On a +// Mac, Windows or Linux (#169: on Windows `raea show` never found the running app, since it asked `kill`, and said it +// had opened the files "in the Mac's own viewer", which it hadn't). import { spawnSync } from "node:child_process"; import { existsSync, readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename } from "node:path"; import { HARNESSES } from "./show"; +/** What these verbs ask of the computer they run on (a test gives its own: conformance/app.test.ts). */ +export interface Here { + platform: string; + run: (cmd: string, args: string[]) => { status: number | null; stdout: string }; + exists: (path: string) => boolean; + read: (path: string) => string; + env: Record; + home: string; +} + +export const HERE: Here = { + platform: process.platform, + run: (cmd, args) => { + const r = spawnSync(cmd, args, { encoding: "utf8" }); + return { status: r.status, stdout: r.stdout ?? "" }; + }, + exists: existsSync, + read: (p) => readFileSync(p, "utf8"), + env: process.env, + home: homedir(), +}; + +/** This computer as raea names it, and the viewer files open in without the app. */ +export function computer(h: Here = HERE): { name: string; viewer: string } { + if (h.platform === "darwin") return { name: "this Mac", viewer: "the Mac's own viewer" }; + if (h.platform === "win32") return { name: "this PC", viewer: "Windows' own viewer" }; + return { name: "this computer", viewer: "the desktop's own viewer" }; +} + +/** Whether process `pid` is running: `kill -0`, or on Windows (which has no kill) tasklist. */ +function alive(pid: number, h: Here): boolean { + if (h.platform !== "win32") return h.run("kill", ["-0", String(pid)]).status === 0; + return h.run("tasklist", ["/FI", `PID eq ${pid}`, "/NH", "/FO", "CSV"]).stdout.includes(`"${pid}"`); +} + /** The running raea app's host: RAEA_FEED_HOST for an agent the app started, else the address the installed app leaves * in ~/.raea/app-host.json while it runs (apps/app host/host-file.ts), if its process is still there. */ -function appHost(): string | null { - if (process.env.RAEA_FEED_HOST) return process.env.RAEA_FEED_HOST; - const file = process.env.RAEA_HOST_FILE || `${homedir()}/.raea/app-host.json`; +export function appHost(h: Here = HERE): string | null { + if (h.env.RAEA_FEED_HOST) return h.env.RAEA_FEED_HOST; + const file = h.env.RAEA_HOST_FILE || `${h.home}/.raea/app-host.json`; try { - const h = JSON.parse(readFileSync(file, "utf8")) as { url?: string; pid?: number }; - if (h.url && h.pid && spawnSync("kill", ["-0", String(h.pid)]).status === 0) return h.url; + const x = JSON.parse(h.read(file)) as { url?: string; pid?: number }; + if (x.url && x.pid && alive(x.pid, h)) return x.url; } catch { // no app running, or a file left by one that stopped } return null; } +/** The installed raea app, RAEA_APP first: on a Mac raea.app in Applications; on Windows raea.exe in the folder its + * installer put it in (the person picks it, #178; Settings > Apps keeps it as InstallLocation), else the default; on + * Linux the launch.sh that linux/install.sh's raea.desktop runs. */ +export function installedApp(h: Here = HERE): string | undefined { + const at: (string | undefined)[] = [h.env.RAEA_APP]; + if (h.platform === "darwin") at.push("/Applications/raea.app", `${h.home}/Applications/raea.app`); + else if (h.platform === "win32") { + const q = h.run("reg", ["query", "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\raea", "/v", "InstallLocation"]).stdout; + const dir = /InstallLocation\s+REG_\w+\s+(.+?)\s*$/m.exec(q)?.[1]; + if (dir) at.push(`${dir.replace(/[\\/]$/, "")}\\raea.exe`); + if (h.env.LOCALAPPDATA) at.push(`${h.env.LOCALAPPDATA}\\Programs\\raea\\raea.exe`); + } else { + try { + const entry = h.read(`${h.env.XDG_DATA_HOME || `${h.home}/.local/share`}/applications/raea.desktop`); + at.push(/^Exec="?([^"\n]+?)"?\s*$/m.exec(entry)?.[1]); + } catch { + // not in the desktop's app list + } + } + return at.find((p) => p && h.exists(p)); +} + +/** Files opened as the system opens them (a double-click's app): open, Windows' Explorer, xdg-open. */ +export function openFiles(files: string[], h: Here = HERE) { + if (h.platform === "darwin") h.run("open", files); + // Explorer, not cmd's start: start reads its line as cmd does, so a folder named with & or ^ breaks it + else if (h.platform === "win32") for (const f of files) h.run("explorer.exe", [f.replace(/\//g, "\\")]); + else for (const f of files) h.run("xdg-open", [f]); +} + +/** The installed app started: open -a on a Mac, Explorer on Windows, its launch.sh in the background on Linux. */ +export function startApp(app: string, h: Here = HERE) { + if (h.platform === "darwin") h.run("open", ["-a", app]); + else if (h.platform === "win32") h.run("explorer.exe", [app]); + else h.run("sh", ["-c", 'nohup "$0" >/dev/null 2>&1 &', app]); +} + +/** A quarter of a second between looks for the app's host (Windows has no sleep command: PowerShell's). */ +function pause(h: Here) { + if (h.platform === "win32") h.run("powershell", ["-NoProfile", "-Command", "Start-Sleep -Milliseconds 250"]); + else h.run("/bin/sleep", ["0.25"]); +} + /** POST a host route of the raea app at `at`, answering its JSON; its refusal is thrown as it says it. */ function postHost(at: string, route: string, a: Record): any { const r = spawnSync("curl", ["-sS", "--max-time", "120", "-w", "\n%{http_code}", "-H", "content-type: application/json", "--data-binary", "@-", `${at.replace(/\/$/, "")}${route}`], { input: JSON.stringify(a), encoding: "utf8", maxBuffer: 64 << 20 }); @@ -33,24 +112,26 @@ function postHost(at: string, route: string, a: Record): any { /** `raea show` (show.ts): the item made by the raea app's host (apps/app host/show.ts), the app started first when it's * installed and not running. The window is brought up on it by its raea:// link only with --open or when the app was - * just started; otherwise the page finds it and says so. Without the app, the files open in the Mac's own viewer. */ -export function show(a: Record): unknown { - let at = appHost(); + * just started (on a Mac or Linux: Windows' app has no raea:// link, so there the post lands as its notice); otherwise + * the page finds it and says so. Without the app, the files open in the system's own viewer. */ +export function show(a: Record, h: Here = HERE): unknown { + let at = appHost(h); let launched = false; if (!at) { - const app = [process.env.RAEA_APP, "/Applications/raea.app", `${homedir()}/Applications/raea.app`].find((p) => p && existsSync(p)); + const app = installedApp(h); const files = (a.files as string[]) ?? []; if (!app) { - if (a.question || a.questions) throw new Error("a question needs the raea app, and it isn't on this Mac (raea.io)"); - spawnSync("open", files); - return { opened: files }; + if (a.question || a.questions) throw new Error(`a question needs the raea app, and it isn't on ${computer(h).name} (raea.io)`); + openFiles(files, h); + return { opened: files, ...computer(h) }; } - if (spawnSync("pgrep", ["-f", `${app}/Contents/MacOS/raea-window`]).status === 0) + if (h.platform === "darwin" && h.run("pgrep", ["-f", `${app}/Contents/MacOS/raea-window`]).status === 0) throw new Error("the raea app running here is older than raea show (it leaves no ~/.raea/app-host.json); quit it and open it again to update, then show it again"); - spawnSync("open", ["-a", app]); + startApp(app, h); launched = true; - // the window's host writes its address once it listens: a minute at most, in quarter seconds - for (let i = 0; i < 240 && !(at = appHost()); i++) spawnSync("/bin/sleep", ["0.25"]); + // the window's host writes its address once it listens: a minute at most + const until = Date.now() + 60_000; + while (!(at = appHost(h)) && Date.now() < until) pause(h); if (!at) throw new Error(`the raea app didn't start (no ~/.raea/app-host.json after a minute); open ${app} and show it again`); } let shown: { url: string }; @@ -72,23 +153,48 @@ export function show(a: Record): unknown { return false; } })(); - const brought = !process.env.RAEA_NO_OPEN && (a.open === true || launched || !finds); - if (brought) spawnSync("open", [shown.url]); + const brought = !h.env.RAEA_NO_OPEN && h.platform !== "win32" && (a.open === true || launched || !finds); + if (brought) h.run(h.platform === "darwin" ? "open" : "xdg-open", [shown.url]); return { ...shown, brought }; } +/** A process's parent and command line: ps, or on Windows every process's at once from PowerShell (one call, not one + * per step up: each takes a good part of a second there). */ +function processes(h: Here): { start: number; of: (pid: number) => { ppid: number; args: string } | undefined } { + if (h.platform !== "win32") { + return { + start: process.ppid, + of: (pid) => { + const m = /^\s*(\d+)\s+(.*)$/.exec(h.run("ps", ["-o", "ppid=,args=", "-p", String(pid)]).stdout); + return m ? { ppid: Number(m[1]), args: m[2] } : undefined; + }, + }; + } + const all = new Map(); + const list = h.run("powershell", ["-NoProfile", "-NonInteractive", "-Command", "Get-CimInstance Win32_Process | ForEach-Object { '{0}`t{1}`t{2}' -f $_.ProcessId, $_.ParentProcessId, $_.CommandLine }"]).stdout; + for (const line of list.split(/\r?\n/)) { + const [pid, ppid, ...args] = line.split("\t"); + if (Number(pid) > 0) all.set(Number(pid), { ppid: Number(ppid), args: args.join("\t") }); + } + return { start: all.get(process.pid)?.ppid ?? 0, of: (pid) => all.get(pid) }; +} + /** The agent harness that started this command (show.ts HARNESSES), found up its parent processes: a program's own - * name, or the script a runtime runs (`node .../gemini`). Nothing when no harness is up there. */ -export function harness(): { name: string; pid: number } | undefined { - let pid = process.ppid; + * name (claude, or on Windows claude.exe), or the script a runtime runs (`node .../gemini`). Nothing when no harness + * is up there. (Until 0.1.8 raea's engine had no process.ppid, so this found none anywhere.) */ +export function harness(h: Here = HERE): { name: string; pid: number } | undefined { + const up = processes(h); + let pid = up.start; for (let i = 0; i < 16 && pid > 1; i++) { - const m = /^\s*(\d+)\s+(.*)$/.exec(spawnSync("ps", ["-o", "ppid=,args=", "-p", String(pid)], { encoding: "utf8" }).stdout ?? ""); - if (!m) return undefined; - for (const w of m[2].split(/\s+/).slice(0, 2).map((w) => basename(w).toLowerCase())) { + const p = up.of(pid); + if (!p) return undefined; + // a Windows command line quotes a path with spaces in it: "C:\Program Files\nodejs\node.exe" ... + const words = (p.args.match(/"[^"]*"|\S+/g) ?? []).slice(0, 2).map((w) => w.replace(/^"|"$/g, "")); + for (const w of words.map((w) => w.split(/[\\/]/).pop()!.toLowerCase().replace(/\.(exe|cmd|bat|ps1)$/, ""))) { const hit = HARNESSES.find(([re]) => re.test(w)); if (hit) return { name: hit[1], pid }; } - pid = Number(m[1]); + pid = p.ppid; } return undefined; } @@ -109,7 +215,7 @@ export function task(a: Record): unknown { /** An op of the feed host's libraries (apps/app host/library/service.ts): POST /host/library. */ export function library(a: Record): unknown { const at = appHost(); - if (!at) throw new Error("libraries live in the raea app: open it on this Mac (or set RAEA_FEED_HOST to its address)"); + if (!at) throw new Error(`libraries live in the raea app: open it on ${computer().name} (or set RAEA_FEED_HOST to its address)`); return postHost(at, "/host/library", a); } diff --git a/engine/cli/src/brand.ts b/engine/cli/src/brand.ts index 490850684..cc2f96ba6 100644 --- a/engine/cli/src/brand.ts +++ b/engine/cli/src/brand.ts @@ -251,3 +251,32 @@ export function brandKit(input: BrandInput, date = new Date().toISOString().slic ]; return { files, said: said.join("\n") }; } + +/** A raster's pixels from its header: PNG, GIF, JPEG, WebP or ICO (a logo's formats), on any system */ +export function pixelsOf(b: Uint8Array): { width: number; height: number } | undefined { + const v = new DataView(b.buffer, b.byteOffset, b.byteLength); + const tag = (at: number, s: string) => String.fromCharCode(...b.subarray(at, at + s.length)) === s; + const wh = (width: number, height: number) => (width > 0 && height > 0 ? { width, height } : undefined); + try { + if (tag(1, "PNG")) return wh(v.getUint32(16), v.getUint32(20)); + if (tag(0, "GIF8")) return wh(v.getUint16(6, true), v.getUint16(8, true)); + if (tag(0, "RIFF") && tag(8, "WEBP")) { + if (tag(12, "VP8 ")) return wh(v.getUint16(26, true) & 0x3fff, v.getUint16(28, true) & 0x3fff); + if (tag(12, "VP8L")) return wh(1 + (((b[22] & 0x3f) << 8) | b[21]), 1 + (((b[24] & 0xf) << 10) | (b[23] << 2) | ((b[22] & 0xc0) >> 6))); + if (tag(12, "VP8X")) return wh(1 + (b[24] | (b[25] << 8) | (b[26] << 16)), 1 + (b[27] | (b[28] << 8) | (b[29] << 16))); + } + if (b[0] === 0 && b[1] === 0 && b[2] === 1 && b[3] === 0) return wh(b[6] || 256, b[7] || 256); + if (b[0] === 0xff && b[1] === 0xd8) { + // JPEG: its segments up to the first frame header (SOF0-15, not DHT, JPG or DAC) + for (let i = 2; i + 9 < b.length; ) { + if (b[i] !== 0xff) return undefined; + const m = b[i + 1]; + if (m >= 0xc0 && m <= 0xcf && m !== 0xc4 && m !== 0xc8 && m !== 0xcc) return wh(v.getUint16(i + 7), v.getUint16(i + 5)); + i += 2 + v.getUint16(i + 2); + } + } + } catch { + // a header cut short + } + return undefined; +} diff --git a/engine/cli/src/index.ts b/engine/cli/src/index.ts index 2b83fcc17..93539a038 100644 --- a/engine/cli/src/index.ts +++ b/engine/cli/src/index.ts @@ -24,7 +24,7 @@ const out = (stdout: string, exitCode = 0, stderr = "") => ({ stdout, stderr, ex // `raea library` (library.ts), kept apart from the imports above import { libraryVerb } from "./library"; // `raea show` (show.ts): what a session shows the person, in the raea app -import { labelsIn, showVerb } from "./show"; +import { absolute, fromCwd, labelsIn, showVerb } from "./show"; // `raea clothes` (clothes.ts): garments for SOMA's body from words import { clothesVerb } from "./clothes"; // `raea bug` (bug.ts): an agent's report of a bug in raea, to raea's server @@ -113,8 +113,12 @@ Not there: React hooks come from "react" (useMemo, useState), not from raea. /** The workspace a folder is in: the nearest folder at or above it holding .raea/workspace, through the shell's fs. */ async function workspaceAbove(c: Ctx, from: string): Promise { - for (let d = from.replace(/\/$/, ""); d; d = d.slice(0, d.lastIndexOf("/"))) { + // up to the root, / or on Windows C: + for (let d = from.replace(/[\\/]+$/, ""); d; ) { if (await c.fs.exists(`${d}/.raea/workspace`)) return d; + const up = Math.max(d.lastIndexOf("/"), d.lastIndexOf("\\")); + if (up < 0) break; + d = d.slice(0, up); } return undefined; } @@ -123,7 +127,7 @@ async function workspaceAbove(c: Ctx, from: string): Promise export function raeaHandler(host?: Host, engine?: Engine) { return async (args: string[], ctx: unknown) => { const c = ctx as unknown as Ctx; - const p = (rel: string) => (rel.startsWith("/") ? rel : `${c.cwd.replace(/\/$/, "")}/${rel}`); + const p = (rel: string) => fromCwd(c.cwd, rel); const ask = (op: string, a: Record = {}) => { if (!host) throw new Error(`raea ${op} needs a host (the native raea's src/native.ts); this shell has none`); return host(op, a); @@ -163,7 +167,7 @@ export function raeaHandler(host?: Host, engine?: Engine) { if (args.includes("--layout") && args[args.indexOf("--layout") + 1] !== "feature") return out("", 2, "raea init [--layout feature]\n"); if (args.includes("--layout")) { for (const d of FEATURE_DIRS) await c.fs.mkdir(p(d), { recursive: true }); - const name = c.cwd.replace(/\/$/, "").split("/").pop() || "project"; + const name = c.cwd.replace(/[\\/]$/, "").split(/[\\/]/).pop() || "project"; for (const [f, body] of Object.entries(featureLayout(name, shape))) await put(f, body); return out(made.length ? `made ${made.join(", ")}; render it with raea render src/main.tsx\n` : "nothing to make: the project's files are here\n"); } @@ -205,7 +209,7 @@ export function raeaHandler(host?: Host, engine?: Engine) { } await c.fs.mkdir(p(home), { recursive: true }); const done: string[] = []; - const shown = (rel: string) => (home.startsWith("/") ? `shared/components/${rel.slice(home.length + 1)}` : rel); + const shown = (rel: string) => (absolute(home) ? `shared/components/${rel.slice(home.length + 1)}` : rel); for (const n of names) { const set = REGISTRY[n]; if (set) { diff --git a/engine/cli/src/native.ts b/engine/cli/src/native.ts index 15f0f20bf..c44b6cbfa 100644 --- a/engine/cli/src/native.ts +++ b/engine/cli/src/native.ts @@ -7,16 +7,17 @@ import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { basename, delimiter, dirname, extname } from "node:path"; -import { brandKit } from "./brand"; +import { brandKit, pixelsOf } from "./brand"; import { readSite, stylesOf } from "./scrape"; import { wordsOf } from "./heard"; import { harness, library, show, task } from "./app"; +import { fromCwd } from "./show"; import { clothesHost } from "./wardrobe"; import { raeaHandler, type Engine, type Host } from "./index"; const [raea, ...args] = process.argv.slice(2); const cwd = process.cwd(); -const at = (p: string) => (p.startsWith("/") ? p : `${cwd}/${p}`); +const at = (p: string) => fromCwd(cwd, p); /** The project's shape: its cut's (cuts.json's edit), else a 1080x1920 vertical at 30 fps */ function shape() { @@ -98,6 +99,9 @@ function ensure(name: string): string { } function hearTake(take: string, out: string) { + // parakeet-mlx is Apple silicon's (MLX); elsewhere raea has no transcriber yet, so the two ways that work there + if (process.platform !== "darwin") + throw new Error(`cutting ${take} by its words needs its transcript, and raea transcribes only on a Mac so far: give it one as ${basename(out)} ([{ "text", "start_ms", "end_ms" }], beside it), or cut it by time: --at A-B`); const dir = mkdtempSync(`${tmpdir()}/raea-hear-`); try { const wav = `${dir}/take.wav`; @@ -339,13 +343,15 @@ function curl(url: string, o: { out?: string; secs?: number } = {}): { body: str return { body, url: final || url }; } -/** A logo's size: an SVG's viewBox or width/height, a raster's pixels by sips */ +/** A logo's size: an SVG's viewBox or width/height, a raster's pixels from its header (else, on a Mac, by sips) */ function sizeOf(file: string): { width?: number; height?: number } { if (file.endsWith(".svg")) { const head = readFileSync(file, "utf8").slice(0, 2000); const vb = /viewBox\s*=\s*["']\s*[-\d.]+[\s,]+[-\d.]+[\s,]+([\d.]+)[\s,]+([\d.]+)/i.exec(head); return vb ? { width: Math.round(+vb[1]), height: Math.round(+vb[2]) } : {}; } + const own = pixelsOf(readFileSync(file)); + if (own || process.platform !== "darwin") return own ?? {}; const r = spawnSync("sips", ["-g", "pixelWidth", "-g", "pixelHeight", file], { encoding: "utf8" }); const n = (k: string) => Number(new RegExp(`${k}: (\\d+)`).exec(r.stdout ?? "")?.[1]) || undefined; return { width: n("pixelWidth"), height: n("pixelHeight") }; diff --git a/engine/cli/src/show.ts b/engine/cli/src/show.ts index 35e02aac8..7ee09a471 100644 --- a/engine/cli/src/show.ts +++ b/engine/cli/src/show.ts @@ -50,6 +50,14 @@ const FLAGS = new Set(["--open"]); /** The harness an agent runs in, named as the feed shows it, from the program that started this command (native.ts * walks up its parents): any harness, not only the ones that mark their shells. */ +/** Whether a path is absolute: POSIX's /..., and Windows' C:\... (or C:/...), \\server\share\... and \... Until 0.1.8 + * only a POSIX one was, so on Windows `raea show C:\...\cut.mp4` named a file under the cwd (#169). Here, not node:path: + * index.ts runs in the app's own shell too, with no Node. */ +export const absolute = (f: string) => /^(?:[\\/]|[A-Za-z]:[\\/])/.test(f); + +/** `f` from `cwd`: itself when it's absolute */ +export const fromCwd = (cwd: string, f: string) => (absolute(f) ? f : `${cwd.replace(/[\\/]$/, "")}/${f}`); + export const HARNESSES: [RegExp, string][] = [ [/^claude$/, "Claude Code"], [/^codex/, "Codex"], [/^gemini$/, "Gemini"], [/^kimi$/, "Kimi"], [/^opencode$/, "OpenCode"], [/^goose$/, "Goose"], [/^cursor-agent$/, "Cursor"], [/^amp$/, "Amp"], [/^qwen$/, "Qwen Code"], [/^crush$/, "Crush"], [/^aider$/, "Aider"], @@ -80,7 +88,7 @@ export function parseShow(args: string[], cwd: string) { const choices = many("--choice"); const unknown = args.find((a, i) => a.startsWith("--") && !WITH_VALUE.has(a) && !FLAGS.has(a) && !WITH_VALUE.has(args[i - 1] ?? "")); if (unknown) throw new Error(`no ${unknown} here\n${SHOW_USAGE}`); - const abs = (f: string) => (f.startsWith("/") ? f : `${cwd.replace(/\/$/, "")}/${f}`); + const abs = (f: string) => fromCwd(cwd, f); const files = args.filter((a, i) => !a.startsWith("--") && !WITH_VALUE.has(args[i - 1] ?? "")).map(abs); // the questions in order: each --question starts one and its --choice's follow; a --preview after a --choice is that // choice's, after the --question (before any choice) the question's own @@ -151,7 +159,7 @@ export function showVerb(ask: Ask, args: string[], cwd: string, env: Record ask("task", { item, comments, state, by, sub: a.sub, note: state === "done" ? a.note : undefined, asks: state === "working" && a.asks.length ? a.asks : undefined, version, ...(state === "working" ? { status: a.status, states: a.states, sign } : {}) }) as { item: string; comments: string[] }; const said = (m: { item: string; comments: string[] }, state: string) => `Marked ${m.comments.join(", ")} ${state} on ${m.item}: the person sees ${a.sub ? `${by.name} · ${a.sub}` : by.name} ${state === "working" ? "on them" : "finished them"}${state === "working" && a.status ? `; it reads "${a.status}"${a.states?.length ? ` (its asks: ${a.states.join(", ")})` : ""}` : ""}.`; diff --git a/engine/native/src/js.rs b/engine/native/src/js.rs index e1a1eb0bb..572ca3669 100644 --- a/engine/native/src/js.rs +++ b/engine/native/src/js.rs @@ -186,7 +186,9 @@ fn run_here(root: &Path, cwd: &Path, entry: Entry, argv: Vec, env: serde let globals = [ ("__raea_argv", json!(argv)), ("__raea_env", Value::Object(env)), - ("__raea_platform", json!(if cfg!(target_os = "ios") { "ios" } else { std::env::consts::OS })), + // process.platform in Node's words (darwin, win32, linux): until 0.1.8 Rust's (macos, windows), so the agent + // shell's every test for a Mac or a Windows PC was false (#169) + ("__raea_platform", json!(match std::env::consts::OS { "macos" => "darwin", "windows" => "win32", os => os })), ("__raea_stdin", json!(stdin)), ("__raea_cwd", json!(cwd.display().to_string())), ]; diff --git a/engine/native/src/node.js b/engine/native/src/node.js index 840ffe71f..4fce2e4ec 100644 --- a/engine/native/src/node.js +++ b/engine/native/src/node.js @@ -97,7 +97,12 @@ return encoding ? b.toString(encoding) : b; }; - // ---- path (POSIX) ------------------------------------------------------------------------------------------------- + // ---- path --------------------------------------------------------------------------------------------------------- + // POSIX's, and on Windows Windows' parsing: a drive's root (C:\), a share's (\\server\share\) and the cwd's drive's + // (\) start an absolute path, and \ separates as / does. A Windows path comes out with / (which Windows' calls all + // take), so the tools' own "a/b" joins, splits and prefix tests hold there too. Until 0.1.8 every Windows path was a + // relative one here: resolve(".") named "/C:\Users\...", no folder at all, and isAbsolute("C:\x") was false (#169). + const win = g.process.platform === "win32"; const normalizeParts = (parts, abs) => { const out = []; for (const p of parts) { @@ -107,48 +112,79 @@ } return out; }; - const path = { - sep: "/", delimiter: ":", - normalize(p) { - if (p === "") return "."; - const abs = p.startsWith("/"), trail = p.endsWith("/"); - let s = normalizeParts(p.split("/"), abs).join("/"); - if (!s && !abs) s = "."; - if (s && trail) s += "/"; - return (abs ? "/" : "") + s; - }, - join(...a) { const s = a.filter((x) => x !== "").join("/"); return s ? path.normalize(s) : "."; }, - resolve(...a) { - let r = ""; - for (let i = a.length - 1; i >= 0 && !r.startsWith("/"); i--) if (a[i]) r = a[i] + (r ? "/" + r : ""); - if (!r.startsWith("/")) r = proc.cwd() + (r ? "/" + r : ""); - const s = "/" + normalizeParts(r.split("/"), true).join("/"); - return s; - }, - isAbsolute: (p) => p.startsWith("/"), - dirname(p) { - if (!p) return "."; - const s = p.replace(/\/+$/, ""); - if (!s) return "/"; - const i = s.lastIndexOf("/"); - return i < 0 ? "." : i === 0 ? "/" : s.slice(0, i); - }, - basename(p, ext) { - let b = p.replace(/\/+$/, ""); - b = b.slice(b.lastIndexOf("/") + 1); - return ext && b.endsWith(ext) && b !== ext ? b.slice(0, -ext.length) : b; - }, - extname(p) { const b = path.basename(p); const i = b.lastIndexOf("."); return i <= 0 ? "" : b.slice(i); }, - relative(from, to) { - const f = path.resolve(from).split("/").filter(Boolean), t = path.resolve(to).split("/").filter(Boolean); - let i = 0; - while (i < f.length && i < t.length && f[i] === t[i]) i++; - return [...Array(f.length - i).fill(".."), ...t.slice(i)].join("/"); - }, - parse(p) { const base = path.basename(p), ext = path.extname(p); return { root: p.startsWith("/") ? "/" : "", dir: path.dirname(p), base, ext, name: ext ? base.slice(0, -ext.length) : base }; }, - format(o) { return (o.dir ? o.dir + "/" : o.root || "") + (o.base || (o.name || "") + (o.ext || "")); }, + /** path for POSIX or (`windows`) Windows, its paths written with / */ + const pathFor = (windows) => { + const slash = windows ? (x) => String(x).replace(/\\/g, "/") : (x) => String(x); + // the root an absolute path starts with, "" for a relative one: "/", and on Windows "C:/" or "//server/share/" + const rootOf = (x) => { + if (!windows) return x.startsWith("/") ? "/" : ""; + const share = /^\/\/[^/]+\/[^/]+(?:\/|$)/.exec(x); + if (share) return share[0].endsWith("/") ? share[0] : share[0] + "/"; + const drive = /^[A-Za-z]:\//.exec(x); + return drive ? drive[0] : x.startsWith("/") ? "/" : ""; + }; + // Windows' names don't care about case + const same = windows ? (a, b) => a.toLowerCase() === b.toLowerCase() : (a, b) => a === b; + const P = { + sep: "/", delimiter: windows ? ";" : ":", + normalize(x) { + x = slash(x); + if (x === "") return "."; + const root = rootOf(x), trail = x.endsWith("/") && x.length > root.length; + let s = normalizeParts(x.slice(root.length).split("/"), !!root).join("/"); + if (!s && !root) s = "."; + if (s && trail) s += "/"; + return root + s; + }, + join(...a) { const s = a.filter((x) => x !== "").join("/"); return s ? P.normalize(s) : "."; }, + resolve(...a) { + let r = ""; + for (let i = a.length - 1; i >= 0 && !rootOf(r); i--) if (a[i]) r = slash(a[i]) + (r ? "/" + r : ""); + const here = slash(proc.cwd()); + if (!rootOf(r)) r = here + (r ? "/" + r : ""); + // a bare \ on Windows is the cwd's drive's root + if (windows && rootOf(r) === "/") r = (/^[A-Za-z]:/.exec(here) || [""])[0] + r; + const root = rootOf(r); + return root + normalizeParts(r.slice(root.length).split("/"), true).join("/"); + }, + isAbsolute: (x) => rootOf(slash(x)) !== "", + dirname(x) { + if (!x) return "."; + x = slash(x); + const root = rootOf(x), s = x.replace(/\/+$/, ""); + if (s.length < root.length) return root; + const i = s.lastIndexOf("/"); + return i < 0 ? "." : i < root.length ? root : s.slice(0, i); + }, + basename(x, ext) { + x = slash(x); + let b = x.replace(/\/+$/, ""); + b = b.length < rootOf(x).length ? "" : b.slice(b.lastIndexOf("/") + 1); + return ext && b.endsWith(ext) && b !== ext ? b.slice(0, -ext.length) : b; + }, + extname(x) { const b = P.basename(x); const i = b.lastIndexOf("."); return i <= 0 ? "" : b.slice(i); }, + relative(from, to) { + const a = P.resolve(from), b = P.resolve(to); + // another drive (or share) on Windows: no way there but the whole path + if (!same(rootOf(a), rootOf(b))) return b; + const f = a.slice(rootOf(a).length).split("/").filter(Boolean), t = b.slice(rootOf(b).length).split("/").filter(Boolean); + let i = 0; + while (i < f.length && i < t.length && same(f[i], t[i])) i++; + return [...Array(f.length - i).fill(".."), ...t.slice(i)].join("/"); + }, + parse(x) { const base = P.basename(x), ext = P.extname(x); return { root: rootOf(slash(x)), dir: P.dirname(x), base, ext, name: ext ? base.slice(0, -ext.length) : base }; }, + format(o) { return (o.dir ? o.dir + "/" : o.root || "") + (o.base || (o.name || "") + (o.ext || "")); }, + }; + return P; }; - path.posix = path; + const path = pathFor(win); + path.posix = win ? pathFor(false) : path; + // Windows' parsing on any system (Node's path.win32 writes \; this one /, as raea's Windows does) + path.win32 = win ? path : pathFor(true); + path.posix.posix = path.win32.posix = path.posix; + path.posix.win32 = path.win32.win32 = path.win32; + /** a path a native gave, as path writes it: / on Windows */ + const fwd = win ? (x) => String(x).replace(/\\/g, "/") : (x) => x; // ---- fs --------------------------------------------------------------------------------------------------------------- const stats = (s) => ({ @@ -171,8 +207,8 @@ rmdirSync(p, o) { call("rm", pathOf(p), !!(o && o.recursive), false); }, renameSync: (a, b) => call("rename", pathOf(a), pathOf(b)), copyFileSync: (a, b) => call("copyFile", pathOf(a), pathOf(b)), - realpathSync: (p) => call("realpath", pathOf(p)), - mkdtempSync: (prefix) => call("mkdtemp", String(prefix)), + realpathSync: (p) => fwd(call("realpath", pathOf(p))), + mkdtempSync: (prefix) => fwd(call("mkdtemp", String(prefix))), constants: { F_OK: 0, R_OK: 4, W_OK: 2, X_OK: 1 }, createWriteStream(p) { fs.writeFileSync(p, ""); return { write(d) { fs.appendFileSync(p, d); return true; }, end(d) { if (d) fs.appendFileSync(p, d); }, on() { return this; } }; }, }; @@ -186,9 +222,11 @@ fs.promises = promises; // ---- os, crypto, child_process -------------------------------------------------------------------------------------- + // the system's own words, as Node gives them (until 0.1.8 every system here was an arm64 Mac's) const os = { - tmpdir: () => call("tmpdir"), homedir: () => call("homedir"), platform: () => "darwin", arch: () => "arm64", EOL: "\n", - cpus: () => Array.from({ length: 8 }, () => ({ model: "", speed: 0 })), type: () => "Darwin", release: () => "", hostname: () => "raea", endianness: () => "LE", + tmpdir: () => fwd(call("tmpdir")), homedir: () => fwd(call("homedir")), platform: () => proc.platform, arch: () => proc.arch, EOL: win ? "\r\n" : "\n", + cpus: () => Array.from({ length: 8 }, () => ({ model: "", speed: 0 })), type: () => ({ darwin: "Darwin", win32: "Windows_NT", linux: "Linux" })[proc.platform] || proc.platform, + release: () => "", hostname: () => "raea", endianness: () => "LE", }; const crypto = { createHash(alg) { @@ -226,18 +264,23 @@ if (r.status !== 0) { const e = new Error(`Command failed: ${cmd} ${(args || []).join(" ")}\n${r.stderr}`); e.status = r.status; e.stdout = r.stdout; e.stderr = r.stderr; throw e; } return r.stdout; }, - execSync(command, o) { return childProcess.execFileSync("/bin/sh", ["-c", String(command)], o); }, + // a command line through the system's shell, as Node's: sh, or on Windows cmd + execSync(command, o) { return win ? childProcess.execFileSync(g.process.env.ComSpec || "cmd.exe", ["/d", "/s", "/c", String(command)], o) : childProcess.execFileSync("/bin/sh", ["-c", String(command)], o); }, }; // ---- process, url, util, events, module --------------------------------------------------------------------------- const proc = g.process; - let cwd = call("cwd"); + let cwd = fwd(call("cwd")); proc.cwd = () => cwd; proc.chdir = (d) => { cwd = path.resolve(d); }; proc.version = "v22.0.0"; proc.versions = { node: "22.0.0", raea: "1" }; - proc.arch = "arm64"; - proc.pid = 1; + // the process's own (until 0.1.8 a pid of 1 and no ppid, so `raea show` never found the harness that ran it) + const own = call("process"); + proc.arch = own.arch; + proc.pid = own.pid; + let parent; + Object.defineProperty(proc, "ppid", { get: () => (parent === undefined ? (parent = call("ppid")) : parent), configurable: true }); proc.execPath = "raea"; proc.exitCode = undefined; proc.nextTick = (fn, ...a) => queueMicrotask(() => fn(...a)); @@ -249,9 +292,19 @@ proc.memoryUsage = () => ({ rss: 0, heapTotal: 0, heapUsed: 0, external: 0, arrayBuffers: 0 }); proc.on = proc.once = proc.off = () => proc; proc.emitWarning = () => {}; + // file:///C:/x on Windows is C:/x, and file://server/share/x is //server/share/x const url = { - fileURLToPath: (u) => decodeURIComponent(new URL(String(u)).pathname), - pathToFileURL: (p) => new URL("file://" + encodeURI(path.resolve(String(p)))), + fileURLToPath(u) { + const x = new URL(String(u)); + const p = decodeURIComponent(x.pathname); + if (!win) return p; + return x.hostname ? "//" + x.hostname + p : /^\/[A-Za-z]:(\/|$)/.test(p) ? p.slice(1) : p; + }, + pathToFileURL(p) { + const r = path.resolve(String(p)); + const share = win && r.startsWith("//"); + return new URL("file:" + (share ? "" : win ? "///" : "//") + encodeURI(r).replace(/[?#]/g, encodeURIComponent)); + }, URL: g.URL, URLSearchParams: g.URLSearchParams, }; const inspect = (x) => { try { return typeof x === "string" ? x : JSON.stringify(x); } catch { return String(x); } }; @@ -304,7 +357,7 @@ req.cache = cache; return req; }; - builtins.module = { createRequire: (from) => requireFrom(path.dirname(pathOf(from).replace(/^file:\/\//, ""))), builtinModules: Object.keys(builtins) }; + builtins.module = { createRequire: (from) => { const f = String(from instanceof URL ? from.href : from); return requireFrom(path.dirname(f.startsWith("file:") ? url.fileURLToPath(f) : f)); }, builtinModules: Object.keys(builtins) }; g.__raea_node_builtins = builtins; g.__raea_node_require = requireFrom; g.Buffer = Buffer; diff --git a/engine/native/src/node.rs b/engine/native/src/node.rs index 56333d3cf..91e7e477b 100644 --- a/engine/native/src/node.rs +++ b/engine/native/src/node.rs @@ -10,7 +10,7 @@ use base64::Engine as _; use serde_json::{json, Value}; mod os; -use os::{mode_ctime, signal}; +use os::{mode_ctime, node_arch, parent_pid, signal}; /// The builtins node.js defines, by the names an import or require gives them (with or without `node:`) @@ -181,7 +181,8 @@ fn js_string(s: &str) -> String { /// looked for packages from "." pub fn load(path: &Path, entry: bool) -> Result { let abs = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf()); - let url = js_string(&format!("file://{}", abs.display())); + // file:///C:/... on Windows (until 0.1.8 file://C:\..., a URL with no path a fileURLToPath could read) + let url = js_string(&url::Url::from_file_path(&abs).map(String::from).unwrap_or_else(|_| format!("file://{}", abs.display()))); load_module(path).map(|s| s.replace("import.meta.main", if entry { "true" } else { "false" }).replace("import.meta.url", &url)) } @@ -358,7 +359,11 @@ pub fn call(op: &str, a: &[Value]) -> Value { json!(b64(&v)) } "spawn" => spawn(&s(0), a.get(1), a.get(2).unwrap_or(&Value::Null)), - "tmpdir" => json!(std::env::temp_dir().display().to_string().trim_end_matches('/')), + // without its last separator, as Node's (Windows' TEMP ends in \\) + "tmpdir" => json!(std::env::temp_dir().display().to_string().trim_end_matches(['/', '\\'])), + // process.arch and process.pid, in Node's words (x64, arm64) + "process" => json!({"arch": node_arch(), "pid": std::process::id()}), + "ppid" => parent_pid().map(Value::from).unwrap_or(Value::Null), "homedir" => json!(raea_io_home::user_home().unwrap_or_default().display().to_string()), "cwd" => json!(std::env::current_dir().map(|p| p.display().to_string()).unwrap_or_default()), "resolve" => resolve_file(&s(0), Path::new(&s(1)), false).map(|p| json!(p.display().to_string())).unwrap_or_else(|e| err("MODULE_NOT_FOUND", e)), diff --git a/engine/native/src/node/os.rs b/engine/native/src/node/os.rs index 137cd6dc9..917882212 100644 --- a/engine/native/src/node/os.rs +++ b/engine/native/src/node/os.rs @@ -38,3 +38,33 @@ fn signal_name(n: i32) -> String { n => format!("SIG{n}"), } } + +/// process.arch: Node's word for the CPU this was built for +pub(super) fn node_arch() -> &'static str { + match std::env::consts::ARCH { + "x86_64" => "x64", + "aarch64" => "arm64", + "x86" => "ia32", + other => other, + } +} + +/// process.ppid: the process that started this one. Windows keeps it in a process's own record, read here through +/// PowerShell (once, when asked: the agent shell asks only to find the harness that ran `raea show`). +pub(super) fn parent_pid() -> Option { + #[cfg(unix)] + { + // SAFETY: getppid takes nothing and can't fail + Some(unsafe { libc::getppid() } as u32) + } + #[cfg(windows)] + { + let q = format!("(Get-CimInstance Win32_Process -Filter 'ProcessId={}').ParentProcessId", std::process::id()); + let o = std::process::Command::new("powershell").args(["-NoProfile", "-NonInteractive", "-Command", &q]).stdin(std::process::Stdio::null()).output().ok()?; + String::from_utf8_lossy(&o.stdout).trim().parse().ok() + } + #[cfg(not(any(unix, windows)))] + { + None + } +} diff --git a/engine/native/tests/node.rs b/engine/native/tests/node.rs index 4f535ef1a..3f023ad9a 100644 --- a/engine/native/tests/node.rs +++ b/engine/native/tests/node.rs @@ -93,3 +93,55 @@ fn a_script_that_sets_exit_code_exits_with_it() { assert_eq!((set.exit, set.stderr.as_str()), (3, "refused\n")); assert_eq!((plain.exit, plain.stdout.as_str()), (0, "ok")); } + +/// The system as Node names it, and Windows' paths parsed as Windows does (#169: on a Windows PC raea's Node core said +/// it was an arm64 Mac, process.platform was Rust's "windows", resolve(".") named "/C:\Users\...", no folder at all, and +/// there was no process.ppid on any system, so `raea show` never found the harness that ran it). path.win32 parses +/// Windows' paths on any system, as the Windows path does there, writing them with /. Each answer is Node 22's, its \ +/// written as /. +#[test] +fn names_the_system_as_node_does_and_parses_windows_paths() { + let d = std::env::temp_dir().join(format!("raea-node-system-{}", std::process::id())); + std::fs::create_dir_all(&d).unwrap(); + std::fs::write(d.join("system.ts"), r#" +import { arch, platform, type, tmpdir, homedir } from "os"; +import path from "path"; +import { fileURLToPath, pathToFileURL } from "url"; +const w = path.win32; +process.stdout.write(JSON.stringify({ + system: [process.platform === platform(), process.arch === arch(), process.pid > 1, process.ppid > 1 && process.ppid !== process.pid, type()], + here: [path.isAbsolute(process.cwd()), path.resolve(".") === process.cwd(), path.isAbsolute(tmpdir()), !/[\\/]$/.test(tmpdir()), path.isAbsolute(homedir())], + resolve: [w.resolve("C:\\a", "b\\..\\c"), w.resolve("C:\\a", "D:\\x", "y"), w.resolve("\\\\nas\\clips\\", "a.mp4"), w.resolve("C:/a/", "..", "..")], + absolute: ["C:\\x", "c:/x", "\\\\nas\\clips", "\\x", "x", "C:x"].map((p) => w.isAbsolute(p)), + parts: [w.dirname("C:\\a\\b.txt"), w.dirname("C:\\a"), w.dirname("C:\\"), w.basename("C:\\a\\b.txt", ".txt"), w.extname("C:\\a.b\\c.mp4"), w.basename("C:\\")], + join: [w.join("C:\\Users\\me", ".raea", "app-host.json"), w.normalize("C:\\a\\.\\b\\..\\c\\"), w.parse("C:\\a\\b.txt").root], + relative: [w.relative("C:\\a\\b", "C:\\a\\c\\d"), w.relative("C:\\A\\b", "c:\\a\\B\\x"), w.relative("C:\\a", "D:\\b")], + delimiter: [w.delimiter, path.posix.delimiter, path.posix.resolve("/a", "b"), path.posix.isAbsolute("C:\\x")], + url: [fileURLToPath(pathToFileURL(path.resolve("a b#1.txt"))) === path.resolve("a b#1.txt"), pathToFileURL("/x y").href.startsWith("file:///")], +})); +"#).unwrap(); + let r = raea_native::js::run_node(&d.join("system.ts"), &[], String::new(), Duration::from_secs(20)); + let _ = std::fs::remove_dir_all(&d); + let got: serde_json::Value = serde_json::from_str(&r.stdout).unwrap_or_else(|e| panic!("{e}: {} / {}", r.stdout, r.stderr)); + let kind = match std::env::consts::OS { "macos" => "Darwin", "windows" => "Windows_NT", "linux" => "Linux", os => os }; + let want = serde_json::json!({ + "system": [true, true, true, true, kind], + "here": [true, true, true, true, true], + "resolve": ["C:/a/c", "D:/x/y", "//nas/clips/a.mp4", "C:/"], + "absolute": [true, true, true, true, false, false], + "parts": ["C:/a", "C:/", "C:/", "b", ".mp4", ""], + "join": ["C:/Users/me/.raea/app-host.json", "C:/a/c/", "C:/"], + "relative": ["../c/d", "x", "D:/b"], + "delimiter": [";", ":", "/a/b", false], + "url": [true, true], + }); + assert_eq!(got, want); + // and Node's own words for this system + let platform = match std::env::consts::OS { "macos" => "darwin", "windows" => "win32", os => os }; + let probe = d.with_extension("platform.ts"); + std::fs::write(&probe, "process.stdout.write(process.platform + ' ' + process.arch);\n").unwrap(); + let r = raea_native::js::run_node(&probe, &[], String::new(), Duration::from_secs(20)); + let _ = std::fs::remove_file(&probe); + let arch = match std::env::consts::ARCH { "x86_64" => "x64", "aarch64" => "arm64", a => a }; + assert_eq!(r.stdout, format!("{platform} {arch}")); +} diff --git a/io/assets/src/lib.rs b/io/assets/src/lib.rs index 800226256..7dd3f151e 100644 --- a/io/assets/src/lib.rs +++ b/io/assets/src/lib.rs @@ -25,7 +25,7 @@ pub use splat::{ensure_splat, splat_dir, splat_params}; mod local; use local::*; mod setup; -pub use setup::{ffmpeg_tool, run_setup, setup_how, uv, venv_python}; +pub use setup::{ffmpeg_tool, git, run_setup, setup_how, uv, venv_python}; #[cfg(test)] mod tests; diff --git a/io/assets/src/setup.rs b/io/assets/src/setup.rs index d2ae37560..5a13fe21d 100644 --- a/io/assets/src/setup.rs +++ b/io/assets/src/setup.rs @@ -124,6 +124,62 @@ pub fn uv() -> Result { settle(&part, &dir, &bin, fetched, "uv") } +/// The Git for Windows raea fetches where there's none (the app's host, apps/app host/tools.ts WIN_GIT, fetches the same +/// into the same folder, so either finds the other's): dugite-native's portable build with git-lfs, GitHub Desktop's +const WIN_GIT_URL: &str = "https://github.com/desktop/dugite-native/releases/download/v2.56.0-2/dugite-native-v2.56.0-a44aeca-windows-x64.tar.gz"; +const WIN_GIT_DIR: &str = "git-v2.56.0-2-windows-x64"; +const WIN_GIT_SUM: &str = "f1e17876337d7be143e5d51976da36643d65528125cfbc14d57f171de0fd5f14"; + +/// git, which a workspace is (raea workspace, project, controller): $RAEA_GIT, else the one on the PATH. On Windows, +/// where Git is rarely installed, then the raea app's (its installer puts Git in its folder, which Settings > Apps +/// keeps as InstallLocation), a Git for Windows that isn't on the PATH, and raea's own in $RAEA_TOOLS_CACHE (else +/// ~/.cache/raea-tools), fetched there the first time (47 MB). Until 0.1.8 raea.exe took git from the PATH only, and on +/// a PC with only the app's every workspace command failed with "git: program not found" (#169). +pub fn git() -> Result { + if let Some(p) = std::env::var_os("RAEA_GIT").map(PathBuf::from).filter(|p| p.is_file()) { + return Ok(p); + } + if let Some(p) = on_path("git") { + return Ok(p); + } + if !cfg!(windows) { + let how = if cfg!(target_os = "macos") { "xcode-select --install" } else { "apt install git, or dnf install git" }; + return Err(format!("raea's workspaces are git repos, and git isn't installed: {how}")); + } + let cache = raea_io_home::env_path("RAEA_TOOLS_CACHE", ".cache/raea-tools").join(WIN_GIT_DIR); + let bin = cache.join("cmd").join("git.exe"); + if let Some(p) = windows_gits().into_iter().chain([bin.clone()]).find(|p| p.is_file()) { + return Ok(p); + } + static FETCHING: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let _one = FETCHING.lock().unwrap_or_else(|e| e.into_inner()); + if bin.is_file() { + return Ok(bin); + } + raea_io_home::offline("fetching Git for Windows: raea's workspaces are git repos")?; + eprintln!("raea: a workspace is a git repo, and this PC has no Git; fetching raea's own (Git for Windows, once, 47 MB) into {}", cache.display()); + let part = cache.with_file_name(format!("{WIN_GIT_DIR}.part{}", std::process::id())); + let fetched = fetch_archive(&part, WIN_GIT_URL, "git.tar.gz", WIN_GIT_SUM, false, &[]); + // it runs before it's taken, as the app's host checks it + let runs = |p: &Path| std::process::Command::new(p).arg("--version").output().is_ok_and(|o| o.status.success()); + let fetched = fetched.and_then(|()| if runs(&part.join("cmd").join("git.exe")) { Ok(()) } else { Err("the Git raea fetched doesn't run".into()) }); + settle(&part, &cache, &bin, fetched, "Git for Windows") +} + +/// The gits a Windows PC may have off its PATH: the raea app's (its install folder's git\cmd), else in the folder its +/// installer uses by default, and Git for Windows' own for everyone and for the one account. +fn windows_gits() -> Vec { + let key = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\raea"; + let reg = std::process::Command::new("reg").args(["query", key, "/v", "InstallLocation"]).output().ok(); + let app = reg.and_then(|o| String::from_utf8_lossy(&o.stdout).lines().find_map(|l| l.trim().strip_prefix("InstallLocation").map(|v| v.trim().trim_start_matches("REG_EXPAND_SZ").trim_start_matches("REG_SZ").trim().to_string()))); + let env = |k: &str| std::env::var_os(k).map(PathBuf::from); + let mut at: Vec = app.filter(|d| !d.is_empty()).map(|d| PathBuf::from(d).join("git")).into_iter().collect(); + at.extend(env("LOCALAPPDATA").map(|d| d.join("Programs").join("raea").join("git"))); + at.extend(env("ProgramFiles").map(|d| d.join("Git"))); + at.extend(env("LOCALAPPDATA").map(|d| d.join("Programs").join("Git"))); + at.into_iter().map(|d| d.join("cmd").join("git.exe")).collect() +} + /// A fetch's end: its `part` folder moved in as `dir` when it's whole and nothing got there first, the part removed, /// and, once the tool is in place, the parts stopped fetches left beside it swept (`NAME.part`, from a Ctrl+C or /// a killed process: FFmpeg's held a 114 MB zip, and each run made a new one). A part another process is still diff --git a/io/soft/src/lib.rs b/io/soft/src/lib.rs index 3802771c5..0c5006687 100644 --- a/io/soft/src/lib.rs +++ b/io/soft/src/lib.rs @@ -282,6 +282,12 @@ pub fn h264_encoder(out: &Path, o: &EncodeOpts, intra_qp: Option) -> R Vec { + let (cx, cy, r) = (w as f32 * (0.3 + k as f32 * 0.004), h as f32 * 0.45, w as f32 * 0.8); + (0..w * h).flat_map(|i| { + let (dx, dy) = ((i % w) as f32 - cx, (i / w) as f32 - cy); + let v = (2.0 + 36.0 * (1.0 - (dx * dx + dy * dy).sqrt() / r).max(0.0).powi(2)).round() as u8; + [v, v, v, 255] + }).collect() +} + +/// The share of 8x8 blocks whose mean red is more than a level off between two RGBA pictures +fn blocks_off(a: &[u8], b: &[u8], w: u32, h: u32) -> f64 { + let (bw, bh) = (w as usize / 8, h as usize / 8); + let mean = |p: &[u8], bx: usize, by: usize| (0..64).map(|k| p[((by * 8 + k / 8) * w as usize + bx * 8 + k % 8) * 4] as f32).sum::() / 64.0; + let off = (0..bw * bh).filter(|i| (mean(a, i % bw, i / bw) - mean(b, i % bw, i / bw)).abs() > 1.0).count(); + off as f64 / (bw * bh) as f64 +} + +#[test] +fn a_dark_glow_drifting_keeps_every_block_at_master() { + use raea_io_codec::Backend; + let (w, h, n) = (640, 360, 45); + let o = EncodeOpts { width: w, height: h, fps: 30.0, kbps: 10_000, quality: Some(0.95), nv12: false, realtime: false }; + let out = std::env::temp_dir().join(format!("raea-soft-darks-{}.mp4", std::process::id())); + let mut enc = h264_encoder(&out, &o, None).unwrap(); + for k in 0..n { + enc.push(&glow(w, h, k)).unwrap(); + } + enc.finish(None).unwrap(); + let mut dec = raea_io_soft::Soft.open_video(&out).unwrap(); + let last = dec.frame_at((n - 1) as f64 / 30.0).unwrap(); + let _ = std::fs::remove_file(&out); + let off = blocks_off(&last.rgba, &glow(w, h, n - 1), w, h); + eprintln!("blocks off by more than a level: {:.2}%", off * 100.0); + // with background detection on, the ground lagged the glow: blocks stood where earlier frames had left them, 5.3% of + // them more than a level off by frame 44; without it 1.6% (the rest is the quantiser's) + assert!(off < 0.03, "{:.1}% of 8x8 blocks more than a level off the picture at frame {}", off * 100.0, n - 1); +} diff --git a/runtime/js/js/prelude.js b/runtime/js/js/prelude.js index 22a9f56f5..ca5683c23 100644 --- a/runtime/js/js/prelude.js +++ b/runtime/js/js/prelude.js @@ -79,8 +79,10 @@ g.__ui_frame = 0; g.__ui_setFrame = (n) => void (g.__ui_frame = n); // Number's toLocaleString through one Intl.NumberFormat per locale and options, kept (JavaScriptCore made a - // formatter at every call: a counter formatted each frame was most of its frame). Only where there's an Intl - if (typeof Intl !== "undefined" && Intl.NumberFormat) { + // formatter at every call: a counter formatted each frame was most of its frame). Only where there's an Intl, asked + // with `in`: QuickJS's (raea-runtime-jsengine js/intl) is unpacked the first time it's read, and a page that never + // formats shouldn't pay for it + if ("Intl" in globalThis) { const formats = new Map(); const toLocale = Number.prototype.toLocaleString; Object.defineProperty(Number.prototype, "toLocaleString", { diff --git a/runtime/jsengine/Cargo.toml b/runtime/jsengine/Cargo.toml index 645328691..90e5d6149 100644 --- a/runtime/jsengine/Cargo.toml +++ b/runtime/jsengine/Cargo.toml @@ -11,11 +11,14 @@ name = "raea_runtime_jsengine" [features] # QuickJS on Apple too, beside JavaScriptCore: how the portable path is tested on a Mac -portable = ["dep:rquickjs"] +portable = ["dep:rquickjs", "dep:miniz_oxide", "dep:iana-time-zone"] [dependencies] raea-io-home = { path = "../../io/home" } rquickjs = { version = "0.14", optional = true, features = ["loader"] } +# QuickJS's Intl (js/intl): its pack's chunks are raw deflate, and the date polyfill's default zone is the system's +miniz_oxide = { version = "0.8", optional = true } +iana-time-zone = { version = "0.1", optional = true } [target.'cfg(target_vendor = "apple")'.dependencies] # JavaScriptCore's Objective-C module loader (JSScript, JSModuleLoaderDelegate): ES modules on the system engine @@ -23,3 +26,5 @@ objc2 = "0.6" [target.'cfg(not(target_vendor = "apple"))'.dependencies] rquickjs = { version = "0.14", features = ["loader"] } +miniz_oxide = "0.8" +iana-time-zone = "0.1" diff --git a/runtime/jsengine/js/intl.pack b/runtime/jsengine/js/intl.pack new file mode 100644 index 000000000..aa407d52a Binary files /dev/null and b/runtime/jsengine/js/intl.pack differ diff --git a/runtime/jsengine/js/intl/LICENSE-FormatJS.md b/runtime/jsengine/js/intl/LICENSE-FormatJS.md new file mode 100755 index 000000000..127a3ec0a --- /dev/null +++ b/runtime/jsengine/js/intl/LICENSE-FormatJS.md @@ -0,0 +1,56 @@ +MIT License + +Copyright (c) 2022, 2023, 2026 FormatJS (intl-supportedvaluesof 2022, the polyfills 2023, bigdecimal 2026) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +--- + +The locale data (each locale's entry in intl.pack, and the English in its core) is FormatJS's, made from the Unicode +Common Locale Data Repository (CLDR), under the Unicode License: + +UNICODE LICENSE V3 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © Unicode, Inc. + +NOTICE TO USER: Carefully read the following legal agreement. BY +DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR +SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE +TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT +DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. + +SPDX-License-Identifier: Unicode-3.0 diff --git a/runtime/jsengine/js/intl/build.ts b/runtime/jsengine/js/intl/build.ts new file mode 100644 index 000000000..3608b8fdf --- /dev/null +++ b/runtime/jsengine/js/intl/build.ts @@ -0,0 +1,38 @@ +// QuickJS's Intl, packed (docs/ports.feature PT0b): `bun install --frozen-lockfile && bun build.ts` here writes +// ../intl.pack, which raea-runtime-jsengine embeds (src/quickjs.rs) and loader.js reads. QuickJS has no Intl, so a +// composition that formatted a number threw "Intl is not defined" on Windows and Linux (#169) while JavaScriptCore, +// the Mac's, formatted it. The pack is entries of [name length u16][name][length u32][raw deflate], little-endian: +// "core" (core.js bundled: FormatJS's polyfills, English, the common time zones), "index" (the JSON list of the other +// locales) and one entry per locale, its data for each polyfill that has some. Uncompressed the 37 locales are 20 MB. +import { deflateRawSync } from "node:zlib"; +import { existsSync, readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; + +const here = import.meta.dir; +/** the locales besides English: the languages most of raea's people write in, and the regions that format apart */ +export const LOCALES = ["en-GB", "en-AU", "en-CA", "en-IN", "de", "fr", "fr-CA", "es", "es-419", "es-MX", "it", "pt", "pt-PT", "nl", "sv", "da", "nb", "fi", "pl", "cs", "sk", "hu", "ro", "el", "tr", "ru", "uk", "he", "ar", "hi", "bn", "th", "id", "ms", "vi", "ja", "ko", "zh", "zh-Hant", "fil"]; +const DATA = ["intl-pluralrules", "intl-numberformat", "intl-datetimeformat", "intl-relativetimeformat", "intl-listformat"]; + +const built = await Bun.build({ entrypoints: [join(here, "core.js")], minify: true, format: "iife", target: "browser" }); +if (!built.success) throw new Error(built.logs.join("\n")); +const licence = readFileSync(join(here, "LICENSE-FormatJS.md"), "utf8").trim().split("\n").map((l) => ` * ${l}`.trimEnd()).join("\n"); +const core = `/*\n * FormatJS's Intl polyfills, packed for raea's QuickJS (runtime/jsengine/js/intl):\n${licence}\n */\n${await built.outputs[0].text()}`; + +const entries: [string, string][] = [["core", core], ["index", JSON.stringify(LOCALES)]]; +for (const l of LOCALES) { + const parts = DATA.map((p) => join(here, "node_modules/@formatjs", p, "locale-data", `${l}.js`)).filter(existsSync); + if (parts.length < 4) throw new Error(`${l}: data for ${parts.length} of the ${DATA.length} polyfills`); + entries.push([l, parts.map((f) => readFileSync(f, "utf8")).join(";\n")]); +} +const chunks: Buffer[] = []; +for (const [name, src] of entries) { + const n = Buffer.from(name), z = deflateRawSync(Buffer.from(src), { level: 9 }); + const head = Buffer.alloc(6 + n.length); + head.writeUInt16LE(n.length, 0); + n.copy(head, 2); + head.writeUInt32LE(z.length, 2 + n.length); + chunks.push(head, z); +} +const pack = Buffer.concat(chunks); +writeFileSync(join(here, "..", "intl.pack"), pack); +console.log(`intl.pack: ${entries.length} entries, ${(pack.length / 1024).toFixed(0)} KB (core ${(core.length / 1024).toFixed(0)} KB before deflate)`); diff --git a/runtime/jsengine/js/intl/bun.lock b/runtime/jsengine/js/intl/bun.lock new file mode 100644 index 000000000..21201d500 --- /dev/null +++ b/runtime/jsengine/js/intl/bun.lock @@ -0,0 +1,41 @@ +{ + "lockfileVersion": 2, + "configVersion": 1, + "workspaces": { + "": { + "name": "raea-intl", + "dependencies": { + "@formatjs/intl-datetimeformat": "8.0.2", + "@formatjs/intl-getcanonicallocales": "3.3.0", + "@formatjs/intl-listformat": "9.0.1", + "@formatjs/intl-locale": "5.4.2", + "@formatjs/intl-numberformat": "10.0.2", + "@formatjs/intl-pluralrules": "7.0.2", + "@formatjs/intl-relativetimeformat": "13.0.1", + }, + }, + }, + "packages": { + "@formatjs/bigdecimal": ["@formatjs/bigdecimal@0.2.10", "", {}, "sha512-npIs6UARNOg5amJY5UOVsd6HUQQfzYjet0sa0SWIqzsnVhxLSpaIx2DjoIVrr/vqmjURjvroeW6dItjfy3Uf8A=="], + + "@formatjs/fast-memoize": ["@formatjs/fast-memoize@3.1.7", "", {}, "sha512-zXfhLpvA6T7+efdt9JLbBwZ00tT7NsBMDVnDu8rpHeNNv8KfRZAMo2gkG0k9lK/Nzc//3kJ9pImsfuJxk3KhUA=="], + + "@formatjs/intl-datetimeformat": ["@formatjs/intl-datetimeformat@8.0.2", "", { "dependencies": { "@formatjs/bigdecimal": "0.2.10", "@formatjs/intl-localematcher": "0.10.1" } }, "sha512-isKDo8jeQ8VoYvrweku92ajF/Z9fJ76DDnw2eCmKBTJTwZa5GeGl4u40RJiFYPbR6ROluvN0CsbNtGtnd2NyIQ=="], + + "@formatjs/intl-getcanonicallocales": ["@formatjs/intl-getcanonicallocales@3.3.0", "", {}, "sha512-ndj9jw/LHAftSe34D/A6+lj3Y7GHVfPEZ6FZP2wjd8/beYeh3d1sjlSpZSBlzegSKS3U3mUEIzLCpI0+jKNT6w=="], + + "@formatjs/intl-listformat": ["@formatjs/intl-listformat@9.0.1", "", { "dependencies": { "@formatjs/intl-localematcher": "0.10.1" } }, "sha512-V7kMddXDOLeyXAVCBEp8eBnllmPMBqu39U1GO/82xzlaUyK/Gkc3Qgal+9HJx6n1xfCvUn7XHK5t5dSlF7Y2lA=="], + + "@formatjs/intl-locale": ["@formatjs/intl-locale@5.4.2", "", { "dependencies": { "@formatjs/intl-getcanonicallocales": "3.3.0", "@formatjs/intl-supportedvaluesof": "3.0.0" } }, "sha512-2upVZtpzktEkLMffAk14T2HB62B7ytqrlppZuVX4StnbOHNc4qx5ovimMMMn8mkqvAwFlPX+UZygetny+quqIQ=="], + + "@formatjs/intl-localematcher": ["@formatjs/intl-localematcher@0.10.1", "", { "dependencies": { "@formatjs/fast-memoize": "3.1.7" } }, "sha512-gBqglQK9rXuHsLmM8wzTMBy4Xo4d0jamirIVKuEL8F1rwbskWdpMA1kZarSkny+uvr5q/K6GSzE0Rcq1wZLl3g=="], + + "@formatjs/intl-numberformat": ["@formatjs/intl-numberformat@10.0.2", "", { "dependencies": { "@formatjs/bigdecimal": "0.2.10", "@formatjs/intl-localematcher": "0.10.1" } }, "sha512-iLS3ly1lGZ/a/IVpcSUgC37RjQUZ2wN6zR9+Zbr2uXsxATfMLD+fBqggiXuFudLmZXpbPueCs4Mz3vDsaOVE9A=="], + + "@formatjs/intl-pluralrules": ["@formatjs/intl-pluralrules@7.0.2", "", { "dependencies": { "@formatjs/bigdecimal": "0.2.10", "@formatjs/intl-localematcher": "0.10.1" } }, "sha512-PJ4+ldlqXlo3csmP02u2HqyXw09TDB/fatYpDCyWryfEQeVWl2EbMzij9SBksPaW2U//+q69lXR3IMnCRQAmrw=="], + + "@formatjs/intl-relativetimeformat": ["@formatjs/intl-relativetimeformat@13.0.1", "", { "dependencies": { "@formatjs/intl-localematcher": "0.10.1" } }, "sha512-Gl5dCHb7elEC19tTzxIyllWLHaHnN+HpQYEaA9xxAt9rKgRLwC3vGty1w8bVGG7p2YcGSPIg5c4qn4KdkGcTqg=="], + + "@formatjs/intl-supportedvaluesof": ["@formatjs/intl-supportedvaluesof@3.0.0", "", { "dependencies": { "@formatjs/fast-memoize": "3.1.7" } }, "sha512-9rG37qcIBFyX+HSyPelJKpAatFaO+ElvnWYO/FFMERVZG9e9Wj51CWAvF7+ozYd7B1uKBL2ub6aLzrkWA0UBxA=="], + } +} diff --git a/runtime/jsengine/js/intl/core.js b/runtime/jsengine/js/intl/core.js new file mode 100644 index 000000000..2185cc237 --- /dev/null +++ b/runtime/jsengine/js/intl/core.js @@ -0,0 +1,16 @@ +// Generated into ../intl.pack by build.ts: the Intl QuickJS lacks, FormatJS's polyfills (MIT, LICENSE-FormatJS.md) +// in the order each needs the one before, with English's data and the common time zones. Every other locale's data is +// its own chunk in the pack, run the first time a script asks for that locale (loader.js). +import "@formatjs/intl-getcanonicallocales/polyfill.js"; +import "@formatjs/intl-locale/polyfill.js"; +import "@formatjs/intl-pluralrules/polyfill.js"; +import "@formatjs/intl-pluralrules/locale-data/en.js"; +import "@formatjs/intl-numberformat/polyfill.js"; +import "@formatjs/intl-numberformat/locale-data/en.js"; +import "@formatjs/intl-datetimeformat/polyfill.js"; +import "@formatjs/intl-datetimeformat/locale-data/en.js"; +import "@formatjs/intl-datetimeformat/add-golden-tz.js"; +import "@formatjs/intl-relativetimeformat/polyfill.js"; +import "@formatjs/intl-relativetimeformat/locale-data/en.js"; +import "@formatjs/intl-listformat/polyfill.js"; +import "@formatjs/intl-listformat/locale-data/en.js"; diff --git a/runtime/jsengine/js/intl/loader.js b/runtime/jsengine/js/intl/loader.js new file mode 100644 index 000000000..613b97cc1 --- /dev/null +++ b/runtime/jsengine/js/intl/loader.js @@ -0,0 +1,148 @@ +// QuickJS's Intl (src/quickjs.rs runs this first in every context): JavaScriptCore, the Mac's engine, has Intl, and +// QuickJS, Windows' and Linux's, has none, so a composition that formatted a number threw there (#169). FormatJS's +// polyfills are the core of intl.pack (build.ts), unpacked and run the first time anything reads Intl or calls a +// toLocale* method, so a script that never formats pays nothing; a locale's data runs the first time something asks +// for that locale. FormatJS has no Collator: the small one here sorts by letter, then accent, then case (lower first), +// as ICU's root order does for words, and needs none of the core. +(() => { + const g = globalThis; + if (typeof g.Intl !== "undefined") return; + const chunk = g.__raea_intl_chunk; + const zone = g.__raea_intl_zone; + delete g.__raea_intl_chunk; + delete g.__raea_intl_zone; + const run = (src) => (0, eval)(src); + let intl; + let packed = []; + const loaded = new Set(["en"]); + // the pack's locale for a tag: itself, else its language (zh-TW, zh-HK and zh-MO are written as zh-Hant) + const packedFor = (tag) => { + const t = String(tag); + const traditional = /^zh-(Hant|TW|HK|MO)\b/i.test(t) ? "zh-Hant" : null; + for (const c of [t, traditional, t.split("-")[0]]) { + const hit = c && packed.find((p) => p.toLowerCase() === c.toLowerCase()); + if (hit) return hit; + } + return null; + }; + const need = (locales) => { + boot(); + const tags = locales === undefined || locales === null ? [] : typeof locales === "string" || typeof locales[Symbol.iterator] !== "function" ? [locales] : [...locales]; + for (const tag of tags) { + const p = packedFor(tag); + if (p && !loaded.has(p)) { + loaded.add(p); + run(chunk(p)); + } + } + }; + // a constructor that runs its locales' data first: the same prototype (instanceof holds), and its statics + const wrap = (Native) => { + function Intl(locales, options) { + need(locales); + return new.target ? Reflect.construct(Native, [locales, options], new.target === Intl ? Native : new.target) : Native(locales, options); + } + Object.setPrototypeOf(Intl, Native); + Object.defineProperty(Intl, "name", { value: Native.name }); + Intl.prototype = Native.prototype; + if (Native.supportedLocalesOf) Intl.supportedLocalesOf = (locales, options) => (need(locales), Native.supportedLocalesOf(locales, options)); + return Intl; + }; + + // the toLocale* methods: until the core runs, each is one of these, which runs it and then FormatJS's own + // (impl); whatever wraps them later (raea-runtime-js's prelude keeps one NumberFormat per locale) is kept + const METHODS = [[Number.prototype, "toLocaleString"], [Date.prototype, "toLocaleString"], [Date.prototype, "toLocaleDateString"], [Date.prototype, "toLocaleTimeString"]]; + if (typeof BigInt !== "undefined") METHODS.push([BigInt.prototype, "toLocaleString"]); + const impl = METHODS.map(([proto, name]) => proto[name]); + METHODS.forEach(([proto, name], i) => { + const mine = { [name](locales, options) { + need(locales); + return impl[i].call(this, locales, options); + } }[name]; + Object.defineProperty(proto, name, { value: mine, writable: true, configurable: true }); + }); + + let booting = false; + const boot = () => { + if (intl) return intl; + // the core asking for Intl while it runs gets what it has made so far, not a second core + if (booting) return g.Intl; + booting = true; + const before = METHODS.map(([proto, name]) => proto[name]); + delete g.Intl; + run(chunk("core")); + intl = g.Intl; + packed = JSON.parse(chunk("index")); + METHODS.forEach(([proto, name], i) => { + if (proto[name] !== before[i]) impl[i] = proto[name]; + Object.defineProperty(proto, name, { value: before[i], writable: true, configurable: true }); + }); + if (zone) { + try { + intl.DateTimeFormat.__setDefaultTimeZone(zone); + } catch { + // a zone outside the common ones in the pack: UTC + } + } + const put = (k, v) => Object.defineProperty(intl, k, { value: v, writable: true, configurable: true }); + for (const k of ["DateTimeFormat", "NumberFormat", "PluralRules", "RelativeTimeFormat", "ListFormat"]) if (intl[k]) put(k, wrap(intl[k])); + put("Collator", Collator); + return intl; + }; + + const plain = (s) => s.normalize("NFD").replace(/[̀-ͯ]/g, ""); + const cmp = (x, y) => (x < y ? -1 : x > y ? 1 : 0); + // numbers in words by their value: "item 2" before "item 10" + const nat = (x, y) => { + const a = x.match(/\d+|\D+/g) ?? []; + const b = y.match(/\d+|\D+/g) ?? []; + for (let i = 0; i < Math.min(a.length, b.length); i++) { + const d = /^\d/.test(a[i]) && /^\d/.test(b[i]) ? Number(a[i]) - Number(b[i]) : cmp(a[i], b[i]); + if (d) return Math.sign(d); + } + return Math.sign(a.length - b.length); + }; + class Collator { + constructor(locales, o = {}) { + const tag = (typeof locales === "string" ? locales : locales?.[0]) ?? "en"; + this.o = { locale: String(tag), usage: o.usage ?? "sort", sensitivity: o.sensitivity ?? "variant", ignorePunctuation: !!o.ignorePunctuation, collation: "default", numeric: !!o.numeric, caseFirst: o.caseFirst ?? "false" }; + const { sensitivity: s, caseFirst } = this.o; + const by = this.o.numeric ? nat : cmp; + const drop = this.o.ignorePunctuation ? (x) => x.replace(/[\p{P}\s]/gu, "") : (x) => x; + this.compare = (a, b) => { + const x = drop(String(a)); + const y = drop(String(b)); + const letters = by(plain(x).toLowerCase(), plain(y).toLowerCase()); + if (letters || s === "base") return letters; + if (s !== "case") { + const accents = by(x.toLowerCase(), y.toLowerCase()); + if (accents || s === "accent") return accents; + } + // "case": a and A differ, a and á don't + const cased = s === "case" ? by(plain(x), plain(y)) : by(x, y); + return caseFirst === "upper" ? cased : -cased; + }; + } + resolvedOptions() { + return { ...this.o }; + } + static supportedLocalesOf(locales) { + return boot().getCanonicalLocales(locales ?? []); + } + } + const collators = new Map(); + Object.defineProperty(String.prototype, "localeCompare", { + value: function localeCompare(that, locales, options) { + const key = options === undefined && (locales === undefined || typeof locales === "string") ? (locales ?? "") : null; + let c = key === null ? undefined : collators.get(key); + if (!c) { + c = new Collator(locales, options); + if (key !== null && collators.size < 32) collators.set(key, c); + } + return c.compare(String(this), String(that)); + }, + writable: true, + configurable: true, + }); + Object.defineProperty(g, "Intl", { get: boot, set: (v) => Object.defineProperty(g, "Intl", { value: v, writable: true, configurable: true }), configurable: true }); +})(); diff --git a/runtime/jsengine/js/intl/package.json b/runtime/jsengine/js/intl/package.json new file mode 100644 index 000000000..09d000dc4 --- /dev/null +++ b/runtime/jsengine/js/intl/package.json @@ -0,0 +1,18 @@ +{ + "name": "raea-intl", + "private": true, + "description": "QuickJS's Intl (raea-runtime-jsengine): FormatJS's polyfills and their CLDR data, packed by build.ts into ../intl.pack", + "license": "MIT", + "scripts": { + "build": "bun build.ts" + }, + "dependencies": { + "@formatjs/intl-getcanonicallocales": "3.3.0", + "@formatjs/intl-locale": "5.4.2", + "@formatjs/intl-pluralrules": "7.0.2", + "@formatjs/intl-numberformat": "10.0.2", + "@formatjs/intl-datetimeformat": "8.0.2", + "@formatjs/intl-relativetimeformat": "13.0.1", + "@formatjs/intl-listformat": "9.0.1" + } +} diff --git a/runtime/jsengine/src/jsc/modules.rs b/runtime/jsengine/src/jsc/modules.rs index ebdf99aa8..72240aabd 100644 --- a/runtime/jsengine/src/jsc/modules.rs +++ b/runtime/jsengine/src/jsc/modules.rs @@ -49,8 +49,33 @@ fn rs(o: &AnyObject) -> String { } } +/// `source` with its import.meta.url set when it reads it: macOS 15's JavaScriptCore leaves it undefined for a module +/// from this loader (macOS 27's sets the module's URL), and `new URL("./x.png", import.meta.url)` then threw. `??=` +/// leaves a URL the engine gave alone. On line 1 before the module's own first line (imports are hoisted), so every +/// other line keeps its number; a #! line has to stay first, so it's left as it is. +fn with_meta_url(url: &str, source: &str) -> String { + if !source.contains("import.meta") || source.starts_with("#!") { + return source.to_string(); + } + let mut lit = String::with_capacity(url.len() + 2); + lit.push('"'); + for c in url.chars() { + match c { + '"' | '\\' => { + lit.push('\\'); + lit.push(c); + } + c if (c as u32) < 0x20 || c == '\u{2028}' || c == '\u{2029}' => lit.push_str(&format!("\\u{:04x}", c as u32)), + c => lit.push(c), + } + } + lit.push('"'); + format!("import.meta.url ??= {lit};{source}") +} + /// A module script for `url`, or JavaScriptCore's reason it isn't one (a syntax error). fn script(vm: &AnyObject, url: &str, source: &str) -> Result, String> { + let source = &with_meta_url(url, source); unsafe { let u: Option> = msg_send![objc2::class!(NSURL), URLWithString: &*ns(url)]; let Some(u) = u else { return Err(format!("{url}: not a URL")) }; @@ -146,3 +171,16 @@ impl Modules { unsafe extern "C" { fn JSValueProtect(ctx: *const c_void, v: *const c_void); } + +#[cfg(test)] +mod tests { + use super::with_meta_url; + + #[test] + fn a_module_that_reads_import_meta_gets_its_url_on_line_one() { + assert_eq!(with_meta_url("file:///m/a.mjs", "export const x = 1;\n"), "export const x = 1;\n"); + assert_eq!(with_meta_url("file:///m/a.mjs", "f(import.meta.url);\ng();"), "import.meta.url ??= \"file:///m/a.mjs\";f(import.meta.url);\ng();"); + assert_eq!(with_meta_url("file:///C:/a \"b\"\\c.mjs", "import.meta"), "import.meta.url ??= \"file:///C:/a \\\"b\\\"\\\\c.mjs\";import.meta"); + assert_eq!(with_meta_url("file:///m/a.mjs", "#!/usr/bin/env raea\nimport.meta.url"), "#!/usr/bin/env raea\nimport.meta.url"); + } +} diff --git a/runtime/jsengine/src/lib.rs b/runtime/jsengine/src/lib.rs index 42149aae3..faa49b06a 100644 --- a/runtime/jsengine/src/lib.rs +++ b/runtime/jsengine/src/lib.rs @@ -230,6 +230,52 @@ mod tests { assert!(stopped(&e), "{}: {e}", js.name()); } + /// What a composition formats, as JavaScriptCore formats it (#169: QuickJS had no Intl, so these threw on Windows + /// and Linux): numbers, money, percents and compact counts, dates in a zone, plural rules, relative times, lists, + /// and words sorted. Each case is [expression, JavaScriptCore's answer]. + const INTL: [(&str, &str); 14] = [ + ("(1234567.891).toLocaleString('en-US')", "1,234,567.891"), + ("(1234567.891).toLocaleString('de-DE')", "1.234.567,891"), + ("new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(12.5)", "$12.50"), + ("new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' }).format(5000)", "\u{20ac}5,000.00"), + ("new Intl.NumberFormat('en', { notation: 'compact' }).format(1234567)", "1.2M"), + ("(0.256).toLocaleString('en-US', { style: 'percent' })", "26%"), + ("new Intl.NumberFormat('zh-TW').format(1234.5)", "1,234.5"), + ("new Intl.DateTimeFormat('en-GB', { dateStyle: 'long', timeZone: 'UTC' }).format(new Date(Date.UTC(2026, 10, 19)))", "19 November 2026"), + ("new Intl.DateTimeFormat('fr-FR', { weekday: 'long', day: 'numeric', month: 'long', timeZone: 'UTC' }).format(new Date(Date.UTC(2026, 10, 19)))", "jeudi 19 novembre"), + ("new Date(Date.UTC(2026, 10, 19, 15, 4)).toLocaleDateString('en-US', { timeZone: 'UTC' })", "11/19/2026"), + ("[new Intl.PluralRules('en-US').select(2), new Intl.PluralRules('ru').select(3)].join(' ')", "other few"), + ("new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(-1, 'day')", "yesterday"), + ("new Intl.ListFormat('en', { type: 'conjunction' }).format(['a', 'b', 'c'])", "a, b, and c"), + ("['b', 'a', 'B', '\\u00e1'].sort((x, y) => x.localeCompare(y)).join(',')", "a,\u{e1},b,B"), + ]; + + /// Each INTL case's answer on `engine`, or what it threw + fn intl(engine: Engine) -> Vec { + let mut js = engine.start().unwrap(); + let seen = Rc::new(RefCell::new(Vec::new())); + let s = seen.clone(); + js.native("note", Box::new(move |a| { + s.borrow_mut().push(a.join("|")); + Ok(Ret::Undefined) + })) + .unwrap(); + for (expr, _) in INTL { + js.eval(&format!("try {{ note({expr}) }} catch (e) {{ note('threw ' + e) }}"), "intl.js").unwrap(); + } + seen.take() + } + + #[cfg(any(feature = "portable", not(target_vendor = "apple")))] + #[test] + fn quickjs_formats_as_javascriptcore_does() { + let got = intl(Engine::QuickJs); + assert_eq!(got, INTL.map(|(_, want)| want.to_string())); + // and on a Mac, as the engine there does + #[cfg(target_vendor = "apple")] + assert_eq!(got, intl(Engine::Jsc)); + } + #[cfg(target_vendor = "apple")] #[test] fn jsc() { diff --git a/runtime/jsengine/src/quickjs.rs b/runtime/jsengine/src/quickjs.rs index 645753439..ef0dea3f5 100644 --- a/runtime/jsengine/src/quickjs.rs +++ b/runtime/jsengine/src/quickjs.rs @@ -24,7 +24,9 @@ impl Quick { // React's commit recurses through the tree; give it room (the host runs on the main thread's 8 MB stack) rt.set_max_stack_size(4 << 20); let ctx = Context::full(&rt).map_err(|e| e.to_string())?; - Ok(Quick { rt, ctx }) + let mut q = Quick { rt, ctx }; + intl(&mut q)?; + Ok(q) } /// The promise jobs queued, run until none is left (one that throws is logged, as an unhandled rejection is). @@ -42,6 +44,35 @@ impl Quick { } } +/// FormatJS's Intl polyfills and their CLDR data (js/intl/build.ts): "core", "index" and one entry per locale, each +/// raw deflate. QuickJS has no Intl; JavaScriptCore has, so this is QuickJS's alone. +const INTL_PACK: &[u8] = include_bytes!("../js/intl.pack"); + +/// An entry of the Intl pack, inflated: its source (or the index's JSON), None when there's no such entry. +pub(crate) fn intl_chunk(name: &str) -> Option { + let mut at = 0; + while at + 2 <= INTL_PACK.len() { + let n = u16::from_le_bytes([INTL_PACK[at], INTL_PACK[at + 1]]) as usize; + let key = INTL_PACK.get(at + 2..at + 2 + n)?; + let len = u32::from_le_bytes(INTL_PACK.get(at + 2 + n..at + 6 + n)?.try_into().ok()?) as usize; + let body = INTL_PACK.get(at + 6 + n..at + 6 + n + len)?; + if key == name.as_bytes() { + return String::from_utf8(miniz_oxide::inflate::decompress_to_vec(body).ok()?).ok(); + } + at += 6 + n + len; + } + None +} + +/// Intl in the context, the first time it's used (js/intl/loader.js): the pack's chunks through a native, and the +/// system's time zone as the date polyfill's default (JavaScriptCore formats dates in local time; TZ first, as there). +fn intl(q: &mut Quick) -> Result<(), String> { + q.native("__raea_intl_chunk", Box::new(|a| intl_chunk(crate::arg(a, 0)).map(Ret::Str).ok_or_else(|| format!("no Intl data {:?}", crate::arg(a, 0)))))?; + let zone = std::env::var("TZ").ok().filter(|z| z.contains('/')).or_else(|| iana_time_zone::get_timezone().ok()).unwrap_or_default(); + q.ctx.with(|ctx| ctx.globals().set("__raea_intl_zone", zone)).map_err(|e| e.to_string())?; + q.eval(include_str!("../js/intl/loader.js"), "intl-loader.js") +} + /// Imports name their modules by URL already (the host rewrote them); one relative to its importer is joined to its folder. struct Urls;