(h3Event: H3Event)
| 24 | import { getExpectedRedirectStatus } from "../server/util.ts"; |
| 25 | |
| 26 | export async function handleServerFunction(h3Event: H3Event) { |
| 27 | const event = getFetchEvent(h3Event); |
| 28 | const request = event.request; |
| 29 | |
| 30 | const serverReference = request.headers.get("X-Server-Id"); |
| 31 | const instance = request.headers.get("X-Server-Instance"); |
| 32 | const singleFlight = request.headers.has("X-Single-Flight"); |
| 33 | const url = new URL(request.url); |
| 34 | let functionId: string | undefined | null; |
| 35 | if (serverReference) { |
| 36 | // invariant(typeof serverReference === "string", "Invalid server function"); |
| 37 | [functionId] = serverReference.split("#"); |
| 38 | } else { |
| 39 | functionId = url.searchParams.get("id"); |
| 40 | |
| 41 | if (!functionId) { |
| 42 | return process.env.NODE_ENV === "development" |
| 43 | ? new Response("Server function not found", { status: 404 }) |
| 44 | : new Response(null, { status: 404 }); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const serverFunction = getServerFunction(functionId!); |
| 49 | |
| 50 | let parsed: any[] = []; |
| 51 | |
| 52 | // grab bound arguments from url when no JS |
| 53 | if (!instance || request.method === "GET") { |
| 54 | const args = url.searchParams.get("args"); |
| 55 | if (args) { |
| 56 | const result = (await deserializeFromJSONString(args)) as any[]; |
| 57 | for (const arg of result) { |
| 58 | parsed.push(arg); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | if (request.method === "POST" && request.body !== null) { |
| 63 | const bodyFormat = request.headers.get(BODY_FORMAT_KEY); |
| 64 | const decoded = await extractBody("", false, request.clone()); |
| 65 | if (bodyFormat === BodyFormat.Seroval) { |
| 66 | parsed = decoded as any[]; |
| 67 | } else { |
| 68 | parsed.push(decoded); |
| 69 | } |
| 70 | } |
| 71 | try { |
| 72 | let result = await provideRequestEvent(event, async () => { |
| 73 | /* @ts-expect-error */ |
| 74 | sharedConfig.context = { event }; |
| 75 | event.locals.serverFunctionMeta = { |
| 76 | id: functionId, |
| 77 | }; |
| 78 | return serverFunction(...parsed); |
| 79 | }); |
| 80 | |
| 81 | if (singleFlight && instance) { |
| 82 | result = await handleSingleFlight(event, result); |
| 83 | } |
no test coverage detected