UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

1 lines 4.13 kB
{"version":3,"file":"InngestDurableEndpointProxy.cjs","names":["corsHeaders: Record<string, string>"],"sources":["../../src/components/InngestDurableEndpointProxy.ts"],"sourcesContent":["import type { Inngest } from \"./Inngest.ts\";\n\n/**\n * Context extracted from a durable endpoint proxy request.\n */\nexport interface DurableEndpointProxyContext {\n /**\n * The run ID from the query parameters.\n */\n runId: string | null;\n\n /**\n * The token from the query parameters.\n */\n token: string | null;\n\n /**\n * The HTTP method of the request.\n */\n method: string;\n}\n\n/**\n * The result of processing a durable endpoint proxy request.\n */\nexport interface DurableEndpointProxyResult {\n /**\n * HTTP status code.\n */\n status: number;\n\n /**\n * HTTP headers to include in the response.\n */\n headers: Record<string, string>;\n\n /**\n * Response body as a string.\n */\n body: string;\n}\n\n/**\n * Default CORS headers for durable endpoint proxy responses.\n */\nconst corsHeaders: Record<string, string> = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Methods\": \"GET, OPTIONS\",\n \"Access-Control-Allow-Headers\": \"Content-Type\",\n};\n\n/**\n * Helper to create a JSON response with CORS headers.\n */\nconst jsonResponse = (\n status: number,\n body: unknown,\n): DurableEndpointProxyResult => ({\n status,\n headers: { \"Content-Type\": \"application/json\", ...corsHeaders },\n body: typeof body === \"string\" ? body : JSON.stringify(body),\n});\n\n/**\n * Helper to create an error response.\n */\nconst errorResponse = (\n status: number,\n message: string,\n): DurableEndpointProxyResult => jsonResponse(status, { error: message });\n\n/**\n * Core durable endpoint proxy logic - framework-agnostic.\n *\n * This function handles the common logic for durable endpoint proxy handlers:\n * - CORS preflight handling\n * - Parameter validation\n * - Fetching results from Inngest API\n * - Decrypting results via middleware (if configured)\n *\n * Framework adapters wrap this with their specific Request/Response types.\n *\n * @param client - The Inngest client to use for API requests and decryption\n * @param ctx - The request context containing runId, token, and method\n * @returns A DurableEndpointProxyResult with status, headers, and body\n */\nexport async function handleDurableEndpointProxyRequest(\n client: Inngest.Any,\n ctx: DurableEndpointProxyContext,\n): Promise<DurableEndpointProxyResult> {\n // Handle CORS preflight\n if (ctx.method === \"OPTIONS\") {\n return {\n status: 204,\n headers: { ...corsHeaders, \"Access-Control-Max-Age\": \"86400\" },\n body: \"\",\n };\n }\n\n const { runId, token } = ctx;\n\n if (!runId || !token) {\n return errorResponse(400, \"Missing runId or token query parameter\");\n }\n\n try {\n const response = await client[\"inngestApi\"].getRunOutput(runId, token);\n\n if (!response.ok) {\n return jsonResponse(response.status, await response.text());\n }\n\n let body = await response.json();\n body = await client[\"decryptProxyResult\"](body);\n\n return jsonResponse(200, body);\n } catch (error) {\n const message =\n error instanceof Error ? error.message : \"Failed to fetch run output\";\n return errorResponse(500, message);\n }\n}\n"],"mappings":";;;;;AA6CA,MAAMA,cAAsC;CAC1C,+BAA+B;CAC/B,gCAAgC;CAChC,gCAAgC;CACjC;;;;AAKD,MAAM,gBACJ,QACA,UACgC;CAChC;CACA,SAAS;EAAE,gBAAgB;EAAoB,GAAG;EAAa;CAC/D,MAAM,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,KAAK;CAC7D;;;;AAKD,MAAM,iBACJ,QACA,YAC+B,aAAa,QAAQ,EAAE,OAAO,SAAS,CAAC;;;;;;;;;;;;;;;;AAiBzE,eAAsB,kCACpB,QACA,KACqC;AAErC,KAAI,IAAI,WAAW,UACjB,QAAO;EACL,QAAQ;EACR,SAAS;GAAE,GAAG;GAAa,0BAA0B;GAAS;EAC9D,MAAM;EACP;CAGH,MAAM,EAAE,OAAO,UAAU;AAEzB,KAAI,CAAC,SAAS,CAAC,MACb,QAAO,cAAc,KAAK,yCAAyC;AAGrE,KAAI;EACF,MAAM,WAAW,MAAM,OAAO,cAAc,aAAa,OAAO,MAAM;AAEtE,MAAI,CAAC,SAAS,GACZ,QAAO,aAAa,SAAS,QAAQ,MAAM,SAAS,MAAM,CAAC;EAG7D,IAAI,OAAO,MAAM,SAAS,MAAM;AAChC,SAAO,MAAM,OAAO,sBAAsB,KAAK;AAE/C,SAAO,aAAa,KAAK,KAAK;UACvB,OAAO;AAGd,SAAO,cAAc,KADnB,iBAAiB,QAAQ,MAAM,UAAU,6BACT"}