Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/api/compile/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { resolveAllImports, resolveAllImportsSources } from "@/lib/import-resolv
import { checkRateLimit, getRateLimitReset } from "@/lib/rate-limiter";

export const runtime = "nodejs";
export const maxDuration = 60;
export const maxDuration = 120;

const MAX_BODY_SIZE = 512000; // 500KB
const WORKER_TIMEOUT_MS = 30_000;
const WORKER_TIMEOUT_EVM_MS = 30_000;
const WORKER_TIMEOUT_PVM_MS = 80_000; // resolc is slower; 20s resolver + 80s compile = 100s < 120s maxDuration

interface CompileRequest {
source?: string;
Expand All @@ -28,17 +29,18 @@ interface CompileResponse {
function compileInWorker(
input: string,
mode: string,
timeoutMs = WORKER_TIMEOUT_MS
timeoutMs?: number
): Promise<any> {
const timeout = timeoutMs ?? (mode === "pvm" ? WORKER_TIMEOUT_PVM_MS : WORKER_TIMEOUT_EVM_MS);
return new Promise((resolve, reject) => {
const worker = new Worker(COMPILE_WORKER_CODE, {
eval: true,
workerData: { mode, input },
});
const timer = setTimeout(() => {
worker.terminate();
reject(new Error("Compilation timed out after 30s"));
}, timeoutMs);
reject(new Error(`Compilation timed out after ${Math.round(timeout / 1000)}s`));
}, timeout);

worker.on("message", (msg) => {
clearTimeout(timer);
Expand Down
37 changes: 35 additions & 2 deletions lib/compile-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@ export async function compileSolidity(
body: JSON.stringify({ source, mode }),
});

const data = await res.json();
let data: any;
try {
data = await res.json();
} catch {
// Server returned non-JSON (e.g., HTML 502/504 gateway timeout)
return {
success: false,
contracts: null,
contractNames: [],
errors: [
{
message: `Server error (HTTP ${res.status}). PVM compilation may need more time — try again or use a simpler contract.`,
severity: "error",
},
],
warnings: [],
};
}

if (!res.ok) {
return {
Expand Down Expand Up @@ -91,7 +108,23 @@ export async function compileSoliditySources(
body,
});

const data = await res.json();
let data: any;
try {
data = await res.json();
} catch {
return {
success: false,
contracts: null,
contractNames: [],
errors: [
{
message: `Server error (HTTP ${res.status}). PVM compilation may need more time — try again or use a simpler contract.`,
severity: "error",
},
],
warnings: [],
};
}

if (!res.ok) {
return {
Expand Down
2 changes: 1 addition & 1 deletion lib/import-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const PER_FETCH_TIMEOUT_MS = 10_000;
const MAX_FILE_SIZE = 500_000; // 500KB per file
const MAX_FILES = 50;
const MAX_ITERATIONS = 10;
const TOTAL_BUDGET_MS = 30_000;
const TOTAL_BUDGET_MS = 20_000; // 20s — leaves headroom within the 120s function limit

interface ResolvedSources {
sources: Record<string, { content: string }>;
Expand Down
Loading