invoke.ts40 lines · main
| 1 | import { loadDeployment } from './bundle-store.js'; |
| 2 | import { env } from './env.js'; |
| 3 | import { invokeDeno } from './executors/deno.js'; |
| 4 | import { invokeInline } from './executors/inline.js'; |
| 5 | import type { InvokeRequest, InvokeResult } from './types.js'; |
| 6 | |
| 7 | /** |
| 8 | * Route an invoke request to the configured executor. The control plane |
| 9 | * (apps/api) is the only caller — authentication of the end user already |
| 10 | * happened there; the `auth` field on the request is authoritative. |
| 11 | */ |
| 12 | export async function handleInvoke(request: InvokeRequest): Promise<InvokeResult> { |
| 13 | const started = performance.now(); |
| 14 | let bundle; |
| 15 | try { |
| 16 | bundle = await loadDeployment(request.projectId, request.deploymentId); |
| 17 | } catch (err) { |
| 18 | return { |
| 19 | ok: false, |
| 20 | code: 'bundle_fetch_failed', |
| 21 | message: err instanceof Error ? err.message : 'unknown error', |
| 22 | durationMs: Math.round(performance.now() - started), |
| 23 | }; |
| 24 | } |
| 25 | if (!bundle) { |
| 26 | return { |
| 27 | ok: false, |
| 28 | code: 'no_deployment', |
| 29 | message: `deployment ${request.deploymentId} has no bundle`, |
| 30 | durationMs: Math.round(performance.now() - started), |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | switch (env.BRIVEN_RUNTIME_EXECUTOR) { |
| 35 | case 'inline': |
| 36 | return invokeInline(bundle, request); |
| 37 | case 'deno': |
| 38 | return invokeDeno(bundle, request); |
| 39 | } |
| 40 | } |