|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { |
| 3 | + type CompleteWaitpointTokenResponseBody, |
| 4 | + conditionallyExportPacket, |
| 5 | + stringifyIO, |
| 6 | +} from "@trigger.dev/core/v3"; |
| 7 | +import { WaitpointId } from "@trigger.dev/core/v3/isomorphic"; |
| 8 | +import { z } from "zod"; |
| 9 | +import { $replica } from "~/db.server"; |
| 10 | +import { env } from "~/env.server"; |
| 11 | +import { verifyHttpCallbackHash } from "~/services/httpCallback.server"; |
| 12 | +import { logger } from "~/services/logger.server"; |
| 13 | +import { engine } from "~/v3/runEngine.server"; |
| 14 | + |
| 15 | +const paramsSchema = z.object({ |
| 16 | + waitpointFriendlyId: z.string(), |
| 17 | + hash: z.string(), |
| 18 | +}); |
| 19 | + |
| 20 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 21 | + if (request.method.toUpperCase() !== "POST") { |
| 22 | + return json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } }); |
| 23 | + } |
| 24 | + |
| 25 | + const contentLength = request.headers.get("content-length"); |
| 26 | + if (!contentLength) { |
| 27 | + return json({ error: "Content-Length header is required" }, { status: 411 }); |
| 28 | + } |
| 29 | + |
| 30 | + if (parseInt(contentLength) > env.TASK_PAYLOAD_MAXIMUM_SIZE) { |
| 31 | + return json({ error: "Request body too large" }, { status: 413 }); |
| 32 | + } |
| 33 | + |
| 34 | + const { waitpointFriendlyId, hash } = paramsSchema.parse(params); |
| 35 | + const waitpointId = WaitpointId.toId(waitpointFriendlyId); |
| 36 | + |
| 37 | + try { |
| 38 | + const waitpoint = await $replica.waitpoint.findFirst({ |
| 39 | + where: { |
| 40 | + id: waitpointId, |
| 41 | + }, |
| 42 | + include: { |
| 43 | + environment: { |
| 44 | + select: { |
| 45 | + apiKey: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + if (!waitpoint) { |
| 52 | + return json({ error: "Waitpoint not found" }, { status: 404 }); |
| 53 | + } |
| 54 | + |
| 55 | + if (!verifyHttpCallbackHash(waitpoint.id, hash, waitpoint.environment.apiKey)) { |
| 56 | + return json({ error: "Invalid URL, hash doesn't match" }, { status: 401 }); |
| 57 | + } |
| 58 | + |
| 59 | + if (waitpoint.status === "COMPLETED") { |
| 60 | + return json<CompleteWaitpointTokenResponseBody>({ |
| 61 | + success: true, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + // If the request body is not valid JSON, return an empty object |
| 66 | + const body = await request.json().catch(() => ({})); |
| 67 | + |
| 68 | + const stringifiedData = await stringifyIO(body); |
| 69 | + const finalData = await conditionallyExportPacket( |
| 70 | + stringifiedData, |
| 71 | + `${waitpointId}/waitpoint/http-callback` |
| 72 | + ); |
| 73 | + |
| 74 | + const result = await engine.completeWaitpoint({ |
| 75 | + id: waitpointId, |
| 76 | + output: finalData.data |
| 77 | + ? { type: finalData.dataType, value: finalData.data, isError: false } |
| 78 | + : undefined, |
| 79 | + }); |
| 80 | + |
| 81 | + return json<CompleteWaitpointTokenResponseBody>( |
| 82 | + { |
| 83 | + success: true, |
| 84 | + }, |
| 85 | + { status: 200 } |
| 86 | + ); |
| 87 | + } catch (error) { |
| 88 | + logger.error("Failed to complete HTTP callback", { error }); |
| 89 | + throw json({ error: "Failed to complete HTTP callback" }, { status: 500 }); |
| 90 | + } |
| 91 | +} |
0 commit comments