Update src/app/api/browser/route.ts
Browse files- src/app/api/browser/route.ts +66 -8
src/app/api/browser/route.ts
CHANGED
|
@@ -1,12 +1,29 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
export async function POST(req: NextRequest) {
|
| 5 |
try {
|
| 6 |
-
const
|
|
|
|
| 7 |
|
| 8 |
if (action === "init") {
|
| 9 |
await initBrowser();
|
|
|
|
| 10 |
return NextResponse.json({
|
| 11 |
success: true,
|
| 12 |
message: "Browser launched on about:blank",
|
|
@@ -14,17 +31,58 @@ export async function POST(req: NextRequest) {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
if (action === "navigate") {
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return NextResponse.json({
|
| 19 |
success: true,
|
| 20 |
-
|
| 21 |
});
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
} catch (e: unknown) {
|
| 26 |
-
const
|
| 27 |
-
|
| 28 |
-
return NextResponse.json({ error: msg }, { status: 500 });
|
| 29 |
}
|
| 30 |
}
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import {
|
| 3 |
+
closeBrowser,
|
| 4 |
+
initBrowser,
|
| 5 |
+
isBrowserAlive,
|
| 6 |
+
navigateTo,
|
| 7 |
+
} from "@/lib/browser";
|
| 8 |
+
|
| 9 |
+
export const runtime = "nodejs";
|
| 10 |
+
export const dynamic = "force-dynamic";
|
| 11 |
+
|
| 12 |
+
function normalizeUrl(input: string): string {
|
| 13 |
+
const value = (input || "").trim();
|
| 14 |
+
if (!value) throw new Error("URL is required");
|
| 15 |
+
if (/^https?:\/\//i.test(value)) return value;
|
| 16 |
+
return `https://${value}`;
|
| 17 |
+
}
|
| 18 |
|
| 19 |
export async function POST(req: NextRequest) {
|
| 20 |
try {
|
| 21 |
+
const body = await req.json().catch(() => ({}));
|
| 22 |
+
const { action, url } = body as { action?: string; url?: string };
|
| 23 |
|
| 24 |
if (action === "init") {
|
| 25 |
await initBrowser();
|
| 26 |
+
|
| 27 |
return NextResponse.json({
|
| 28 |
success: true,
|
| 29 |
message: "Browser launched on about:blank",
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
if (action === "navigate") {
|
| 34 |
+
const targetUrl = normalizeUrl(url || "");
|
| 35 |
+
await navigateTo(targetUrl);
|
| 36 |
+
|
| 37 |
+
return NextResponse.json({
|
| 38 |
+
success: true,
|
| 39 |
+
message: `Navigated to ${targetUrl}`,
|
| 40 |
+
url: targetUrl,
|
| 41 |
+
});
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
if (action === "status") {
|
| 45 |
return NextResponse.json({
|
| 46 |
success: true,
|
| 47 |
+
alive: isBrowserAlive(),
|
| 48 |
});
|
| 49 |
}
|
| 50 |
|
| 51 |
+
if (action === "close") {
|
| 52 |
+
await closeBrowser();
|
| 53 |
+
return NextResponse.json({
|
| 54 |
+
success: true,
|
| 55 |
+
message: "Browser closed",
|
| 56 |
+
});
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
return NextResponse.json(
|
| 60 |
+
{
|
| 61 |
+
error: "Unknown action. Use one of: init, navigate, status, close",
|
| 62 |
+
},
|
| 63 |
+
{ status: 400 }
|
| 64 |
+
);
|
| 65 |
+
} catch (e: unknown) {
|
| 66 |
+
const message = e instanceof Error ? e.message : "Unknown error";
|
| 67 |
+
console.error("[/api/browser]", message);
|
| 68 |
+
|
| 69 |
+
return NextResponse.json(
|
| 70 |
+
{
|
| 71 |
+
error: message,
|
| 72 |
+
},
|
| 73 |
+
{ status: 500 }
|
| 74 |
+
);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
export async function GET() {
|
| 79 |
+
try {
|
| 80 |
+
return NextResponse.json({
|
| 81 |
+
success: true,
|
| 82 |
+
alive: isBrowserAlive(),
|
| 83 |
+
});
|
| 84 |
} catch (e: unknown) {
|
| 85 |
+
const message = e instanceof Error ? e.message : "Unknown error";
|
| 86 |
+
return NextResponse.json({ error: message }, { status: 500 });
|
|
|
|
| 87 |
}
|
| 88 |
}
|