import { NextResponse } from "next/server"; import { authorizeWorkerRequest } from "yt-dlp-transcript-common/lib/workerToken"; import { readWorkerResult } from "yt-dlp-transcript-common/controller/workerServer"; export const dynamic = "force-dynamic"; const ID_RE = /^[A-Za-z0-9_-]+$/; // Stream the produced transcript.json once the job is done. 404 if not ready. export async function GET( request: Request, { params }: { params: Promise<{ id: string }> }, ) { const auth = authorizeWorkerRequest(request.headers.get("authorization")); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const { id } = await params; if (!ID_RE.test(id)) { return NextResponse.json({ error: "invalid id" }, { status: 400 }); } const bytes = await readWorkerResult(id); if (!bytes) { return NextResponse.json({ error: "no result yet" }, { status: 404 }); } return new NextResponse(new Uint8Array(bytes), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "no-store", }, }); }