Skip to content

Commit 50a4517

Browse files
committed
refactor: cleanup and add comments for bun plugin
1 parent 0abc51d commit 50a4517

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

packages/bun/src/builders.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { join } from 'node:path';
44
import { BaseBuilder } from '@workflow/cli/dist/lib/builders/base-builder';
55
import type { WorkflowConfig } from '@workflow/cli/dist/lib/config/types';
66

7+
/**
8+
* Workflow builder for Bun.
9+
* Creates the workflow, step and webhook bundles
10+
*/
711
export class LocalBuilder extends BaseBuilder {
812
#outDir: string;
913
constructor(config?: Partial<WorkflowConfig>) {

packages/bun/src/index.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
1-
import type { Serve } from 'bun';
1+
import { join } from 'node:path';
2+
import { serve } from 'bun';
23
import { LocalBuilder } from './builders';
3-
import { workflowPlugin } from './plugin';
4+
import { workflowTransformPlugin } from './plugin';
45

5-
// Build the workflows
6+
// Build the workflow bundles/routes
67
await new LocalBuilder().build();
78

89
// Registers the plugin with Bun runtime
9-
Bun.plugin(workflowPlugin());
10+
Bun.plugin(workflowTransformPlugin());
1011

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+
),
2333
};
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);
3541
};
3642
}

packages/bun/src/plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { transform } from '@swc/core';
22
import type { BunPlugin } from 'bun';
33

4-
// Plugin to build workflows at runtime and do client mode transform
5-
export function workflowPlugin(): BunPlugin {
4+
/**
5+
* Bun plugin to transform workflow files with SWC.
6+
* Automatically works with Bun HMR.
7+
*/
8+
export function workflowTransformPlugin(): BunPlugin {
69
return {
7-
name: 'workflow-plugin',
10+
name: 'workflow-transform-plugin',
811
async setup(build) {
912
// Client transform plugin - only transform TypeScript files
1013
build.onLoad({ filter: /\.(ts|tsx)$/ }, async (args) => {

workbench/bun/server.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import { getHookByToken, getRun, resumeHook, start } from 'workflow/api';
2-
import { createWorkflowRoutes } from 'workflow/bun';
32
import { hydrateWorkflowArguments } from 'workflow/internal/serialization';
4-
import * as step from './.workflows/steps.js';
5-
import * as webhook from './.workflows/webhook.js';
6-
import * as flow from './.workflows/workflows.js';
73
import { allWorkflows } from './_workflows.js';
84

95
const server = Bun.serve({
106
idleTimeout: 30, // Just for local dev e2e tests
117
port: 3000,
128
// `routes` requires Bun v1.2.3+
139
routes: {
14-
// Register Workflow DevKit protocol endpoints
15-
...createWorkflowRoutes({ flow, step, webhook }),
16-
1710
// Custom endpoints for triggering workflows and handling webhooks
1811
'/api/hook': {
1912
POST: async (req) => {

0 commit comments

Comments
 (0)