mirror of
https://github.com/openclaw/openclaw.git
synced 2026-09-28 14:12:28 +08:00
* test: prepare bounded TypeScript child probes Prepare native test children through the invocation-owned compiler before their existing deadlines. Preserve module boundaries for import guards and script entry detection, and keep source-loader contracts explicit. Retain native process isolation, worker lifetimes, assertions, and deadlines. Cover MTS declaration selection in the existing compiler fixture. Also declare the generated fallback plugin fixture as ESM and remove one unused internal script export. * test: prepare remaining worker and tooling fixtures Keep installed tooling at its native package location so prepared probes retain native bindings and relative assets. Preserve existing child deadlines, assertions, and source-layout contracts. * test: assert native CLI delegation before option rejection * test: preserve sparse prepared fixture entry contracts * test: mark prepared budget fixtures as ES modules * test: preserve serialized fixture inputs under type-aware lint * test: prepare sparse wrapper and report fixtures * test: prepare remaining CLI wrapper children * test: complete bounded child fixture preparation
60 lines
2.5 KiB
TypeScript
60 lines
2.5 KiB
TypeScript
import { Buffer } from "node:buffer";
|
|
|
|
/** Keep real wrappers and source main guards while borrowing the prepared module graph. */
|
|
export function preparedScriptWrapperPreload(
|
|
entries: ReadonlyArray<readonly [source: URL, prepared: URL]>,
|
|
virtualModules: ReadonlyArray<readonly [source: URL, prepared: URL]> = [],
|
|
): string {
|
|
const modules = Object.fromEntries(
|
|
[...entries, ...virtualModules]
|
|
.filter(([source, prepared]) => source.href !== prepared.href)
|
|
.map(([source, prepared]) => [source.href, prepared.href]),
|
|
);
|
|
if (Object.keys(modules).length === 0) {
|
|
return "";
|
|
}
|
|
const virtualSources = virtualModules
|
|
.filter(([source, prepared]) => source.href !== prepared.href)
|
|
.map(([source]) => source.href);
|
|
// Literal createRequire paths retain source extensions in otherwise compiled modules.
|
|
const resolveVirtual = virtualSources.length
|
|
? ` const requested = specifier.startsWith(".") && context.parentURL
|
|
? new URL(specifier, context.parentURL).href : specifier;
|
|
if (${JSON.stringify(virtualSources)}.includes(requested)) {
|
|
return { url: requested, shortCircuit: true };
|
|
}
|
|
`
|
|
: "";
|
|
return `import { readFileSync } from "node:fs";
|
|
import { registerHooks } from "node:module";
|
|
const modules = ${JSON.stringify(modules)};
|
|
const sources = new Map(Object.entries(modules).map(([source, prepared]) => [prepared, source]));
|
|
registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
${resolveVirtual} const prepared = modules[context.parentURL];
|
|
const resolved = nextResolve(specifier, prepared && specifier.startsWith(".")
|
|
? { ...context, parentURL: prepared } : context);
|
|
const source = sources.get(resolved.url);
|
|
return source ? { ...resolved, url: source } : resolved;
|
|
},
|
|
load(url, context, nextLoad) {
|
|
const prepared = modules[url];
|
|
return prepared ? { format: "module", source: readFileSync(new URL(prepared), "utf8"), shortCircuit: true }
|
|
: nextLoad(url, context);
|
|
},
|
|
});`;
|
|
}
|
|
|
|
export function preparedScriptWrapperEnv(
|
|
entries: ReadonlyArray<readonly [source: URL, prepared: URL]>,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
virtualModules: ReadonlyArray<readonly [source: URL, prepared: URL]> = [],
|
|
): NodeJS.ProcessEnv {
|
|
const preload = preparedScriptWrapperPreload(entries, virtualModules);
|
|
if (!preload) {
|
|
return { ...env };
|
|
}
|
|
const url = `data:text/javascript;base64,${Buffer.from(preload).toString("base64")}`;
|
|
return { ...env, NODE_OPTIONS: [env.NODE_OPTIONS, `--import=${url}`].filter(Boolean).join(" ") };
|
|
}
|