Files
shinyobjectz 5d7afd0493 #269's gates on main as it is now: crates and files under their limits by real splits, the copies folded, no "void"
- raea-cli 2139 -> about 1800 lines: raea goal's verbs (the loop's runner, its usage, the render moment, and raea
  make's asset tools regrouped) are raea-cli-goal (engine/cli/goal), and what a refused render changed is
  raea-cli-refused (engine/cli/refused); verbs.rs and usage.rs re-export them, so no call site moved
- engine/native node.rs 515 -> 495: spawnSync's deadline wait is deadline.rs (its pipes drained on threads of their
  own)
- a CommonJS file's names are exported by alias (`export { __cjs_0 as delete }`), so a reserved word is a name too and
  cjs.rs needs no list of them (the list named "void", which tools/no-void.sh can't tell from the product); the node
  test imports one (`delete as del`)
- raea-runtime-export back to main's: #269's own fix for a .png with no --frame (#261) is gone, main fixed it in the
  stills path (8627ba7a), and both had landed as two paths for one case
- tools/skills.sh 504 -> 459: the sections, their co-change warning (SK4) and `stale` are tools/skills-sections.sh,
  sourced as skills-app.sh is
- dupes back to 293/57: cjs.rs's quote and identifier tests one helper each, its resolve without format!, and the new
  lines that matched others by shape written plainly (js.rs's importing folder, check-make's broke lines, apps/make's
  goals, lod-plan's objects)

layers, names, legacy names, size, one-version, no-void, dupes, cargo shear and rask check pass on this tree;
raea-native's (cjs and node, with the reserved-word import and the spawnSync timeout), raea-cli-refused's and
raea-runtime-lod-plan's tests pass.
2026-11-29 17:18:31 -08:00

207 lines
13 KiB
Rust

//! Node's core on JavaScriptCore (js::run_node): a TypeScript script using fs, path, os, crypto, child_process,
//! process and Buffer, an ES module package resolved through package.json's exports, and a CommonJS package (imported,
//! and required from inside another), each result what Node gives.
#![cfg(feature = "modules")]
use std::time::Duration;
#[test]
fn runs_a_tool_as_node_would() {
let d = std::env::temp_dir().join(format!("raea-node-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
let w = |p: &str, s: &str| {
let f = d.join(p);
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
std::fs::write(f, s).unwrap();
};
w("node_modules/esm-pkg/package.json", r#"{ "name": "esm-pkg", "type": "module", "exports": { ".": { "import": "./lib/index.js", "require": "./nope.cjs" }, "./extra": "./lib/extra.js" } }"#);
w("node_modules/esm-pkg/lib/index.js", "export const twice = (x) => x * 2;\nexport default 'esm-default';\n");
w("node_modules/esm-pkg/lib/extra.js", "export const extra = 'extra';\n");
w("node_modules/cjs-pkg/package.json", r#"{ "name": "cjs-pkg", "main": "main.js" }"#);
w("node_modules/cjs-pkg/main.js", "const { join } = require('path');\nconst helper = require('./helper');\nmodule.exports = { joined: join('a', 'b'), helper: helper.name, self: typeof __dirname };\n");
w("node_modules/cjs-pkg/helper.js", "exports.name = 'helper';\n");
w("data.json", r#"{ "n": 3 }"#);
w("tool.ts", r#"
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync, appendFileSync, rmSync } from "fs";
import { join, dirname, basename, relative, extname } from "path";
import { tmpdir } from "os";
import { createHash } from "crypto";
import { spawnSync } from "child_process";
import esm, { twice } from "esm-pkg";
import { extra } from "esm-pkg/extra";
import cjs from "cjs-pkg";
import data from "./data.json";
import { createRequire } from "module";
const out: Record<string, unknown> = {};
const dir = join(process.argv[2], "work");
mkdirSync(join(dir, "deep/er"), { recursive: true });
writeFileSync(join(dir, "a.txt"), "héllo");
appendFileSync(join(dir, "a.txt"), "!");
writeFileSync(join(dir, "b.bin"), new Uint8Array([0, 255, 7]));
out.text = readFileSync(join(dir, "a.txt"), "utf8");
out.bytes = Array.from(readFileSync(join(dir, "b.bin")));
out.isBuffer = Buffer.isBuffer(readFileSync(join(dir, "b.bin")));
out.list = readdirSync(dir).sort();
out.types = readdirSync(dir, { withFileTypes: true }).map((e) => e.name + ":" + (e.isDirectory() ? "d" : "f")).sort();
out.size = statSync(join(dir, "a.txt")).size;
out.mtime = statSync(join(dir, "a.txt")).mtimeMs > 0;
rmSync(join(dir, "deep"), { recursive: true, force: true });
out.gone = existsSync(join(dir, "deep"));
let missing = "";
try { readFileSync(join(dir, "nope")); } catch (e) { missing = (e as any).code; }
out.missing = missing;
out.path = [dirname("/a/b/c.ts"), basename("/a/b/c.ts", ".ts"), extname("x.tar.gz"), relative("/a/b", "/a/c/d"), join("a", "..", "b", "./c")];
out.tmp = tmpdir().length > 0;
out.sha = createHash("sha256").update("abc").digest("hex");
out.sha1 = createHash("sha1").update(Buffer.from("abc")).digest("base64");
const echo = spawnSync("/bin/sh", ["-c", "printf out; printf err >&2; exit 3"]);
out.spawn = [echo.status, echo.stdout.toString(), echo.stderr.toString()];
out.b64 = Buffer.from("héllo").toString("base64") + "|" + Buffer.from("aMOpbGxv", "base64").toString();
out.pkgs = [esm, twice(21), extra, cjs.joined, cjs.helper, cjs.self];
out.json = data.n;
out.argv = process.argv.slice(3);
out.env = typeof process.env.HOME;
out.req = createRequire(import.meta.url)("cjs-pkg").helper;
process.stdout.write(JSON.stringify(out));
process.exit(5);
"#);
let r = raea_native::js::run_node(&d.join("tool.ts"), &[d.display().to_string(), "x".into(), "y".into()], String::new(), Duration::from_secs(20));
let _ = std::fs::remove_dir_all(&d);
assert_eq!(r.exit, 5, "process.exit(5); stderr: {}", r.stderr);
let got: serde_json::Value = serde_json::from_str(&r.stdout).unwrap_or_else(|e| panic!("{e}: {} / {}", r.stdout, r.stderr));
// what Node 22 gives for the same script
let want = serde_json::json!({
"text": "héllo!", "bytes": [0, 255, 7], "isBuffer": true, "list": ["a.txt", "b.bin", "deep"], "types": ["a.txt:f", "b.bin:f", "deep:d"],
"size": 7, "mtime": true, "gone": false, "missing": "ENOENT",
"path": ["/a/b", "c", ".gz", "../c/d", "b/c"], "tmp": true,
"sha": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "sha1": "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=",
"spawn": [3, "out", "err"], "b64": "aMOpbGxv|héllo", "pkgs": ["esm-default", 42, "extra", "a/b", "helper", "string"],
"json": 3, "argv": ["x", "y"], "env": "string", "req": "helper"
});
assert_eq!(got, want);
}
#[test]
fn a_script_that_sets_exit_code_exits_with_it() {
let d = std::env::temp_dir().join(format!("raea-node-exitcode-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
std::fs::write(d.join("set.ts"), "process.stderr.write(\"refused\\n\");\nprocess.exitCode = 3;\n").unwrap();
std::fs::write(d.join("plain.ts"), "process.stdout.write(\"ok\");\n").unwrap();
let set = raea_native::js::run_node(&d.join("set.ts"), &[], String::new(), Duration::from_secs(20));
let plain = raea_native::js::run_node(&d.join("plain.ts"), &[], String::new(), Duration::from_secs(20));
let _ = std::fs::remove_dir_all(&d);
// as Node: the status it set, and what it wrote before ending
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}"));
}
/// spawnSync's `timeout`, as Node's: a program still running past it is killed, the answer's error is ETIMEDOUT and its
/// status null, with what it printed before; one that ends first is answered as without it (`raea goal check` runs a
/// goal's command rules this way, MK38)
#[test]
fn spawn_sync_stops_a_program_at_its_timeout() {
let d = std::env::temp_dir().join(format!("raea-node-timeout-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
std::fs::write(
d.join("t.ts"),
r#"import { spawnSync } from "child_process";
const slow = spawnSync("/bin/sh", ["-c", "printf started; exec sleep 30"], { timeout: 300, encoding: "utf8" });
const quick = spawnSync("/bin/sh", ["-c", "printf done; exit 2"], { timeout: 10000, encoding: "utf8" });
process.stdout.write(JSON.stringify([slow.error && slow.error.code, slow.status, slow.stdout, quick.status, quick.stdout]));
"#,
)
.unwrap();
let t = std::time::Instant::now();
let r = raea_native::js::run_node(&d.join("t.ts"), &[], String::new(), Duration::from_secs(20));
let _ = std::fs::remove_dir_all(&d);
assert!(t.elapsed() < Duration::from_secs(10), "the slow program wasn't stopped: {:?}", t.elapsed());
let got: serde_json::Value = serde_json::from_str(&r.stdout).unwrap_or_else(|e| panic!("{e}: {} / {}", r.stdout, r.stderr));
assert_eq!(got, serde_json::json!(["ETIMEDOUT", null, "started", 2, "done"]));
}
/// What raea goal's runner leans on (docs/goal.feature MK32): a CommonJS package's names imported by name (React's
/// `jsx`, as every .tsx's automatic runtime imports it), a file imported by a URL the tool makes, stdin read as fd 0,
/// and process.exit's code from a promise's callback (`main().then((c) => process.exit(c))`)
#[test]
fn named_cjs_dynamic_files_stdin_and_exit_from_a_promise() {
let d = std::env::temp_dir().join(format!("raea-node-goal-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
let w = |p: &str, s: &str| {
let f = d.join(p);
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
std::fs::write(f, s).unwrap();
};
w("node_modules/rt/package.json", r#"{ "name": "rt", "main": "index.js" }"#);
w("node_modules/rt/index.js", "if (process.env.NODE_ENV === 'production') {\n module.exports = require('./cjs/rt.production.js');\n} else {\n module.exports = require('./cjs/rt.production.js');\n}\n");
w("node_modules/rt/cjs/rt.production.js", "exports.jsx = (t) => 'jsx:' + t;\nexports.Fragment = 'frag';\nexports.delete = 'del';\n");
w("project/brief.ts", "export default { title: 'a brief' };\n");
w("tool.ts", r#"
// (a reserved word is a name too, as Node's interop gives it: node.rs exports each name by alias)
import { jsx, Fragment, delete as del } from "rt";
import { readFileSync } from "node:fs";
import { pathToFileURL } from "node:url";
import { join } from "node:path";
async function main(): Promise<number> {
const brief = (await import(pathToFileURL(join(process.argv[2], "project/brief.ts")).href)).default;
console.log(JSON.stringify({ jsx: jsx("a"), Fragment, del, title: brief.title, stdin: readFileSync(0, "utf8") }));
return 3;
}
main().then((c) => process.exit(c));
setTimeout(() => console.log("a timer after exit runs not"), 5);
"#);
let r = raea_native::js::run_node(&d.join("tool.ts"), &[d.display().to_string()], "edits".into(), Duration::from_secs(20));
assert_eq!(r.exit, 3, "process.exit's code from a promise's callback: {}", r.stderr);
assert_eq!(r.stdout.trim(), r#"{"jsx":"jsx:a","Fragment":"frag","del":"del","title":"a brief","stdin":"edits"}"#, "{}", r.stderr);
}