fix(cli): stream large JavaScript JSON results (#1024)
* fix(javascript): report application analysis progress on the CLI * test(cli): preserve progress in filtered output checks * fix(cli): stream large JavaScript JSON results * test(cli): include streaming output in dispatcher fixture
This commit is contained in:
@@ -11,6 +11,7 @@ describe("executable dispatcher", () => {
|
||||
async ({ processes, workspace }) => {
|
||||
const scripts = await workspace.mkdir("scripts");
|
||||
await workspace.mkdir("dist");
|
||||
await workspace.mkdir("dist/cli");
|
||||
await Promise.all([
|
||||
copyFile("scripts/rea.mjs", join(scripts, "rea.mjs")),
|
||||
workspace.write(
|
||||
@@ -29,6 +30,10 @@ describe("executable dispatcher", () => {
|
||||
"dist/cliOutput.js",
|
||||
"export const sanitizeCliOutput = (output) => output; export const validateCliOutputArguments = () => ({ ok: true }); export const renderCliOutputArgumentError = () => ''; export const renderEmptyFilteredCliOutput = () => undefined;\n",
|
||||
),
|
||||
workspace.write(
|
||||
"dist/cli/streamedJsonOutput.js",
|
||||
"export const createStreamedCliJsonOutput = () => undefined;\n",
|
||||
),
|
||||
workspace.write(
|
||||
"dist/mcpDoctor.js",
|
||||
'export const runProductionMcpDoctorCli = (args) => Promise.resolve({ output: JSON.stringify({ adapter: "mcp-doctor", args }), exitCode: 0 });\n',
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { Filter } from "incur";
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { analyzeJavaScriptApplication } from "../../../src/application/javascript/JavaScriptApplicationService.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const filter = "evidence_id,normalized_result.graph.nodes[0:1].node_id";
|
||||
|
||||
for (const command of ["analyze-javascript-application", "analyze"]) {
|
||||
for (const format of ["json", "jsonl"]) {
|
||||
for (const fullOutput of [false, true]) {
|
||||
for (const filtered of [false, true]) {
|
||||
cliTest(
|
||||
`${command} preserves ${format}, full output ${String(fullOutput)}, filtering ${String(filtered)}`,
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-js-json-output-");
|
||||
await writeFile(join(root, "main.js"), "fetch('');\n");
|
||||
const direct = await analyzeJavaScriptApplication({
|
||||
input_path: root,
|
||||
});
|
||||
if (!direct.ok) throw direct.error;
|
||||
const output = await cli.run({
|
||||
arguments: [
|
||||
command,
|
||||
root,
|
||||
"--format",
|
||||
format,
|
||||
...(fullOutput ? ["--full-output"] : []),
|
||||
...(filtered ? ["--filter-output", filter] : []),
|
||||
],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
let data: unknown = output.json;
|
||||
if (fullOutput) {
|
||||
const envelope = z
|
||||
.object({
|
||||
ok: z.literal(true),
|
||||
data: z.unknown(),
|
||||
meta: z.object({
|
||||
command: z.literal(command),
|
||||
duration: z.string().regex(/^\d+ms$/u),
|
||||
}),
|
||||
})
|
||||
.parse(output.json);
|
||||
data = envelope.data;
|
||||
}
|
||||
expect(data).toEqual(
|
||||
filtered
|
||||
? Filter.apply(direct.value, Filter.parse(filter))
|
||||
: direct.value,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cliTest(
|
||||
"preserves a complete empty filtered JSON document",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-js-json-empty-filter-");
|
||||
await writeFile(join(root, "main.js"), "export const value = 1;\n");
|
||||
const output = await cli.run({
|
||||
arguments: [
|
||||
"analyze-javascript-application",
|
||||
root,
|
||||
"--json",
|
||||
"--filter-output",
|
||||
"absent.field",
|
||||
],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
expect(output.json).toEqual({});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves a typed failed analysis in the JSON result",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-js-json-failure-");
|
||||
const output = await cli.run({
|
||||
arguments: [
|
||||
"analyze-javascript-application",
|
||||
join(root, "absent"),
|
||||
"--json",
|
||||
],
|
||||
});
|
||||
expect(output.json).toMatchObject({
|
||||
code: "artifact_operation_failed",
|
||||
details: { reason: "io" },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves JSON omission for inherited filter methods",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-js-json-inherited-filter-");
|
||||
await writeFile(join(root, "main.js"), "export const value = 1;\n");
|
||||
const output = await cli.run({
|
||||
arguments: [
|
||||
"analyze-javascript-application",
|
||||
root,
|
||||
"--json",
|
||||
"--filter-output",
|
||||
"toString",
|
||||
],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
expect(output.json).toEqual({});
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,170 @@
|
||||
import { Writable } from "node:stream";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { writeJsonOutput } from "../../../src/cli/jsonOutput.js";
|
||||
import { createStreamedCliJsonOutput } from "../../../src/cli/streamedJsonOutput.js";
|
||||
|
||||
const collect = async (
|
||||
value: unknown,
|
||||
format: "json" | "jsonl" = "json",
|
||||
): Promise<string> => {
|
||||
const chunks: Buffer[] = [];
|
||||
const destination = new Writable({
|
||||
write(chunk: unknown, _encoding, callback) {
|
||||
if (!Buffer.isBuffer(chunk)) {
|
||||
callback(new TypeError("Expected a UTF-8 output buffer"));
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
callback();
|
||||
},
|
||||
});
|
||||
await writeJsonOutput(value, destination, format);
|
||||
return Buffer.concat(chunks).toString("utf8");
|
||||
};
|
||||
|
||||
describe("incremental CLI JSON", () => {
|
||||
it.each([
|
||||
null,
|
||||
true,
|
||||
-0,
|
||||
1e-7,
|
||||
"",
|
||||
{ empty: [], object: {}, nested: [{ value: null }] },
|
||||
{ 'quoted"key': '\u0000\b\t\n\f\r\\"', unicode: "雪𝟠\ud800\udfff" },
|
||||
{ text: `${"x".repeat(8191)}𝟠${"y".repeat(8191)}\ud800` },
|
||||
])("matches native JSON encoding for %j", async (value) => {
|
||||
expect(await collect(value)).toBe(`${JSON.stringify(value, null, 2)}\n`);
|
||||
});
|
||||
|
||||
it("preserves shared objects without interpreting them as cycles", async () => {
|
||||
const shared = { value: "shared" };
|
||||
const value = { copies: [shared, shared] };
|
||||
expect(await collect(value)).toBe(`${JSON.stringify(value, null, 2)}\n`);
|
||||
});
|
||||
|
||||
it("writes compact JSONL records and preserves an empty record set", async () => {
|
||||
const value = [{ text: "first\nrecord" }, { text: "雪𝟠" }];
|
||||
expect(await collect(value, "jsonl")).toBe(
|
||||
`${value.map((item) => JSON.stringify(item)).join("\n")}\n`,
|
||||
);
|
||||
expect(await collect([], "jsonl")).toBe("");
|
||||
});
|
||||
|
||||
it("bounds escaped leaf writes and waits for a slow destination", async () => {
|
||||
const leaf = `\u0000雪𝟠`.repeat(32 * 1024);
|
||||
const chunks: Buffer[] = [];
|
||||
let largestChunk = 0;
|
||||
const destination = new Writable({
|
||||
highWaterMark: 1,
|
||||
write(chunk: unknown, _encoding, callback) {
|
||||
if (!Buffer.isBuffer(chunk)) {
|
||||
callback(new TypeError("Expected UTF-8 bytes"));
|
||||
return;
|
||||
}
|
||||
largestChunk = Math.max(largestChunk, chunk.length);
|
||||
setTimeout(() => {
|
||||
chunks.push(chunk);
|
||||
callback();
|
||||
}, 1);
|
||||
},
|
||||
});
|
||||
await writeJsonOutput({ leaf }, destination);
|
||||
expect(largestChunk).toBeLessThan(Buffer.byteLength(leaf));
|
||||
const actual = Buffer.concat(chunks).toString("utf8");
|
||||
const expected = `${JSON.stringify({ leaf }, null, 2)}\n`;
|
||||
expect(actual.length).toBe(expected.length);
|
||||
expect(actual === expected).toBe(true);
|
||||
expect(destination.writableEnded).toBe(false);
|
||||
});
|
||||
|
||||
it("waits for the final write even when it fits the destination buffer", async () => {
|
||||
let completed = false;
|
||||
const destination = new Writable({
|
||||
write(_chunk, _encoding, callback) {
|
||||
setTimeout(() => {
|
||||
completed = true;
|
||||
callback();
|
||||
}, 5);
|
||||
},
|
||||
});
|
||||
await writeJsonOutput({ result: "small" }, destination);
|
||||
expect(completed).toBe(true);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"propagates a failed destination (deferred: %s)",
|
||||
async (deferred) => {
|
||||
const cause = new Error("Fixture destination failed");
|
||||
const destination = new Writable({
|
||||
write(_chunk, _encoding, callback) {
|
||||
if (deferred) setTimeout(() => callback(cause), 5);
|
||||
else callback(cause);
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
writeJsonOutput({ result: "small" }, destination),
|
||||
).rejects.toBe(cause);
|
||||
},
|
||||
);
|
||||
|
||||
it("leaves a reusable destination open without adding error listeners", async () => {
|
||||
const destination = new Writable({
|
||||
write(_chunk, _encoding, callback) {
|
||||
callback();
|
||||
},
|
||||
});
|
||||
const listeners = destination.listenerCount("error");
|
||||
await writeJsonOutput({ result: "first" }, destination);
|
||||
await writeJsonOutput({ result: "second" }, destination);
|
||||
expect(destination.listenerCount("error")).toBe(listeners);
|
||||
expect(destination.writableEnded).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects a cycle at the serialization boundary", async () => {
|
||||
const cycle: Record<string, unknown> = {};
|
||||
cycle.self = cycle;
|
||||
await expect(collect(cycle)).rejects.toThrow("circular reference");
|
||||
});
|
||||
|
||||
it.each([undefined, Number.NaN, Number.POSITIVE_INFINITY, 1n, new Date(0)])(
|
||||
"rejects non-JSON input %s",
|
||||
async (value) => {
|
||||
await expect(collect(value)).rejects.toThrow("ordinary JSON");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("streamed CLI result surface", () => {
|
||||
it.each([
|
||||
[],
|
||||
["--format", "md"],
|
||||
["--json", "--token-count"],
|
||||
["--json", "--token-limit", "5"],
|
||||
["--json", "--format", "invalid"],
|
||||
])("delegates unsupported output controls %j", (...arguments_) => {
|
||||
expect(createStreamedCliJsonOutput(arguments_, new Writable())).toBe(
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps ownership after a failed write so no second document is emitted", async () => {
|
||||
const cause = new Error("Fixture destination failed");
|
||||
const destination = new Writable({
|
||||
write(_chunk, _encoding, callback) {
|
||||
callback(cause);
|
||||
},
|
||||
});
|
||||
const output = createStreamedCliJsonOutput(["--json"], destination);
|
||||
if (output === undefined) throw new Error("Missing JSON output surface");
|
||||
await expect(
|
||||
output.write(
|
||||
{ result: "small" },
|
||||
{ command: "fixture", duration: "1ms", format: "json" },
|
||||
),
|
||||
).rejects.toBe(cause);
|
||||
expect(output.handled).toBe(true);
|
||||
expect(output.failed).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user