UNPKG

@redwoodjs/sdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

35 lines (34 loc) 1.44 kB
import { registerServerReference as baseRegisterServerReference, registerClientReference as baseRegisterClientReference, decodeReply, } from "react-server-dom-webpack/server.edge"; import { getModuleExport } from "../imports/worker"; import { IS_DEV } from "../constants"; export function registerServerReference(action, id, name) { if (typeof action !== "function") { return action; } return baseRegisterServerReference(action, id, name); } export function registerClientReference(id, exportName, target) { const reference = baseRegisterClientReference({}, id, exportName); return Object.defineProperties(target, { ...Object.getOwnPropertyDescriptors(reference), $$async: { value: true }, $$isClientReference: { value: true }, }); } export async function rscActionHandler(req) { const url = new URL(req.url); const contentType = req.headers.get("content-type"); const data = contentType?.startsWith("multipart/form-data") ? await req.formData() : await req.text(); const args = (await decodeReply(data, null)); const actionId = url.searchParams.get("__rsc_action_id"); if (IS_DEV && actionId === "__rsc_hot_update") { return null; } const action = await getModuleExport(actionId); if (typeof action !== "function") { throw new Error(`Action ${actionId} is not a function`); } return action(...args); }