43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { syncAnyTLSNode } from "@/lib/node-agent";
|
|
import { ProvisionError } from "@/lib/certificates";
|
|
export const runtime = "nodejs";
|
|
export async function POST(
|
|
request: Request,
|
|
context: { params: Promise<{ slug: string }> },
|
|
) {
|
|
const headers = { "Cache-Control": "no-store" };
|
|
try {
|
|
const reader = request.body?.getReader();
|
|
if (!reader) throw new ProvisionError("缺少报告。");
|
|
let length = 0;
|
|
const chunks: Uint8Array[] = [];
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
length += value.byteLength;
|
|
if (length > 2 * 1024 * 1024) {
|
|
await reader.cancel();
|
|
return new Response(null, { status: 413, headers });
|
|
}
|
|
chunks.push(value);
|
|
}
|
|
const payload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
return Response.json(
|
|
await syncAnyTLSNode(
|
|
(await context.params).slug,
|
|
request.headers.get("authorization")?.replace(/^Bearer /, "") ?? "",
|
|
payload,
|
|
),
|
|
{ headers },
|
|
);
|
|
} catch (error) {
|
|
return Response.json(
|
|
{
|
|
error:
|
|
error instanceof ProvisionError ? error.message : "节点同步失败。",
|
|
},
|
|
{ status: error instanceof ProvisionError ? 400 : 500, headers },
|
|
);
|
|
}
|
|
}
|