cb15c5e0d8
Release / Check for new version (push) Has been cancelled
Release / Build macOS ARM64 (push) Has been cancelled
Release / Build macOS x64 (push) Has been cancelled
Release / Build Linux ARM64 (push) Has been cancelled
Release / Build Linux musl ARM64 (push) Has been cancelled
Release / Build Linux musl x64 (push) Has been cancelled
Release / Build Linux x64 (push) Has been cancelled
Release / Build Windows x64 (push) Has been cancelled
Release / Publish to npm (push) Has been cancelled
Release / Publish sandbox package to npm (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
CI / Rust (windows-latest - x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Native E2E Tests (push) Has been cancelled
CI / Windows Integration Test (push) Has been cancelled
CI / Global Install (macos-latest) (push) Has been cancelled
CI / Global Install (ubuntu-latest) (push) Has been cancelled
CI / Version Sync Check (push) Has been cancelled
CI / Rust (push) Has been cancelled
CI / Dashboard (push) Has been cancelled
CI / Sandbox Package (push) Has been cancelled
CI / Rust (macos-latest - aarch64-apple-darwin) (push) Has been cancelled
CI / Rust (macos-latest - x86_64-apple-darwin) (push) Has been cancelled
CI / Global Install (windows-latest) (push) Has been cancelled
91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import * as sandbox from "@/lib/agent-browser-sandbox";
|
|
import type { StepEvent } from "@/lib/agent-browser-sandbox";
|
|
import { ALLOWED_URLS } from "@/lib/constants";
|
|
import { minuteRateLimit, dailyRateLimit } from "@/lib/rate-limit";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const ip =
|
|
req.headers.get("x-forwarded-for")?.split(",")[0] ?? "anonymous";
|
|
|
|
const minute = await minuteRateLimit.limit(ip);
|
|
if (!minute.success) {
|
|
return NextResponse.json(
|
|
{ error: "Too many requests. Please wait a moment before trying again." },
|
|
{ status: 429 },
|
|
);
|
|
}
|
|
|
|
const daily = await dailyRateLimit.limit(ip);
|
|
if (!daily.success) {
|
|
return NextResponse.json(
|
|
{ error: "Daily limit reached. Please try again tomorrow." },
|
|
{ status: 429 },
|
|
);
|
|
}
|
|
|
|
const body = await req.json();
|
|
const { url, action } = body;
|
|
|
|
if (!url) {
|
|
return NextResponse.json({ error: "Provide a 'url'" }, { status: 400 });
|
|
}
|
|
|
|
if (!(ALLOWED_URLS as readonly string[]).includes(url)) {
|
|
return NextResponse.json({ error: "URL not allowed" }, { status: 400 });
|
|
}
|
|
|
|
if (action !== "screenshot" && action !== "snapshot") {
|
|
return NextResponse.json(
|
|
{ error: "Provide 'action' as 'screenshot' or 'snapshot'" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const stream = new ReadableStream({
|
|
async start(controller) {
|
|
const send = (event: string, data: unknown) => {
|
|
controller.enqueue(
|
|
encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`),
|
|
);
|
|
};
|
|
|
|
const onStep = (step: StepEvent) => {
|
|
send("step", step);
|
|
};
|
|
|
|
try {
|
|
if (action === "screenshot") {
|
|
const result = await sandbox.screenshotUrl(url, {
|
|
fullPage: body.fullPage,
|
|
onStep,
|
|
});
|
|
send("result", { ok: true, ...result });
|
|
} else {
|
|
const result = await sandbox.snapshotUrl(url, {
|
|
interactive: true,
|
|
compact: true,
|
|
onStep,
|
|
});
|
|
send("result", { ok: true, ...result });
|
|
}
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
send("result", { ok: false, error: message });
|
|
}
|
|
|
|
controller.close();
|
|
},
|
|
});
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache",
|
|
Connection: "keep-alive",
|
|
},
|
|
});
|
|
}
|