Skip to content

Commit dd6e716

Browse files
Copilotna-trium-144
andcommitted
Migrate Wandbox API from /api/compile.json to /api/compile.ndjson
Co-authored-by: na-trium-144 <[email protected]>
1 parent 48f45d2 commit dd6e716

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

app/terminal/wandbox/api.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export interface CompileParameter {
6262
is_private?: boolean;
6363
"compiler-info"?: CompilerInfo;
6464
}
65+
export interface CompileNdjsonResult {
66+
type: string;
67+
data: string;
68+
}
69+
6570
export interface CompileResult {
6671
status: string;
6772
signal: string;
@@ -102,8 +107,9 @@ export interface CompileResultWithOutput extends CompileResult {
102107
export async function compileAndRun(
103108
options: CompileProps
104109
): Promise<CompileResultWithOutput> {
105-
const result: CompileResult = await fetch(
106-
new URL("/api/compile.json", WANDBOX),
110+
// Call the ndjson API instead of json API
111+
const response = await fetch(
112+
new URL("/api/compile.ndjson", WANDBOX),
107113
{
108114
method: "post",
109115
headers: {
@@ -121,7 +127,62 @@ export async function compileAndRun(
121127
is_private: true,
122128
} satisfies CompileParameter),
123129
}
124-
).then((res) => res.json());
130+
);
131+
132+
// Read the ndjson response line by line
133+
const text = await response.text();
134+
const lines = text.trim().split("\n");
135+
const ndjsonResults: CompileNdjsonResult[] = lines
136+
.filter((line) => line.trim().length > 0)
137+
.map((line) => JSON.parse(line) as CompileNdjsonResult);
138+
139+
// Merge ndjson results into a CompileResult (same logic as Rust merge_compile_result)
140+
const result: CompileResult = {
141+
status: "",
142+
signal: "",
143+
compiler_output: "",
144+
compiler_error: "",
145+
compiler_message: "",
146+
program_output: "",
147+
program_error: "",
148+
program_message: "",
149+
permlink: "",
150+
url: "",
151+
};
152+
153+
for (const r of ndjsonResults) {
154+
switch (r.type) {
155+
case "Control":
156+
// Ignore control messages
157+
break;
158+
case "CompilerMessageS":
159+
result.compiler_output += r.data;
160+
result.compiler_message += r.data;
161+
break;
162+
case "CompilerMessageE":
163+
result.compiler_error += r.data;
164+
result.compiler_message += r.data;
165+
break;
166+
case "StdOut":
167+
result.program_output += r.data;
168+
result.program_message += r.data;
169+
break;
170+
case "StdErr":
171+
result.program_error += r.data;
172+
result.program_message += r.data;
173+
break;
174+
case "ExitCode":
175+
result.status += r.data;
176+
break;
177+
case "Signal":
178+
result.signal += r.data;
179+
break;
180+
default:
181+
// Ignore unknown types
182+
break;
183+
}
184+
}
185+
125186
return {
126187
...result,
127188
compilerOutput: result.compiler_output.trim()

0 commit comments

Comments
 (0)