|
1 | | -import type { Serve } from 'bun'; |
| 1 | +import { join } from 'node:path'; |
| 2 | +import { serve } from 'bun'; |
2 | 3 | import { LocalBuilder } from './builders'; |
3 | | -import { workflowPlugin } from './plugin'; |
| 4 | +import { workflowTransformPlugin } from './plugin'; |
4 | 5 |
|
5 | | -// Build the workflows |
| 6 | +// Build the workflow bundles/routes |
6 | 7 | await new LocalBuilder().build(); |
7 | 8 |
|
8 | 9 | // Registers the plugin with Bun runtime |
9 | | -Bun.plugin(workflowPlugin()); |
| 10 | +Bun.plugin(workflowTransformPlugin()); |
10 | 11 |
|
11 | | -export function createWorkflowRoutes(handlers: { |
12 | | - flow: { |
13 | | - POST: ( |
14 | | - req: Request, |
15 | | - server: Serve.BaseServeOptions<any> |
16 | | - ) => Response | Promise<Response>; |
17 | | - }; |
18 | | - step: { |
19 | | - POST: ( |
20 | | - req: Request, |
21 | | - server: Serve.BaseServeOptions<any> |
22 | | - ) => Response | Promise<Response>; |
| 12 | +// Register workflow routes |
| 13 | +registerWorkflowRoutes(); |
| 14 | + |
| 15 | +/** |
| 16 | + * Register the generated workflow routes from builder |
| 17 | + * with a patched Bun.serve |
| 18 | + */ |
| 19 | +async function registerWorkflowRoutes() { |
| 20 | + const cwd = process.cwd(); |
| 21 | + |
| 22 | + // Workflow routes generated by the builder |
| 23 | + const defaultRoutes = { |
| 24 | + '/.well-known/workflow/v1/flow': await import( |
| 25 | + join(cwd, '.workflows/workflows.js') |
| 26 | + ), |
| 27 | + '/.well-known/workflow/v1/step': await import( |
| 28 | + join(cwd, '.workflows/steps.js') |
| 29 | + ), |
| 30 | + '/.well-known/workflow/v1/webhook': await import( |
| 31 | + join(cwd, '.workflows/webhook.js') |
| 32 | + ), |
23 | 33 | }; |
24 | | - webhook: any; // Since webhook module exports various things |
25 | | -}): Serve.Routes< |
26 | | - undefined, |
27 | | - | '/.well-known/workflow/v1/flow' |
28 | | - | '/.well-known/workflow/v1/step' |
29 | | - | '/.well-known/workflow/v1/webhook/:token' |
30 | | -> { |
31 | | - return { |
32 | | - '/.well-known/workflow/v1/flow': handlers.flow, |
33 | | - '/.well-known/workflow/v1/step': handlers.step, |
34 | | - '/.well-known/workflow/v1/webhook/:token': handlers.webhook, |
| 34 | + |
| 35 | + Bun.serve = (config) => { |
| 36 | + (config.routes as any) = { |
| 37 | + ...(config.routes as any), |
| 38 | + ...defaultRoutes, |
| 39 | + }; |
| 40 | + return serve(config); |
35 | 41 | }; |
36 | 42 | } |
0 commit comments