|
| 1 | +# WebAssembly Package |
| 2 | + |
| 3 | +The `@goplasmatic/dataflow-wasm` package provides WebAssembly bindings for dataflow-rs, enabling you to run the same workflow engine in the browser that powers your Rust backend. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @goplasmatic/dataflow-wasm |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```typescript |
| 14 | +import init, { WasmEngine } from '@goplasmatic/dataflow-wasm'; |
| 15 | + |
| 16 | +// Initialize the WASM module (required once) |
| 17 | +await init(); |
| 18 | + |
| 19 | +// Define your workflows |
| 20 | +const workflows = [ |
| 21 | + { |
| 22 | + id: 'my-workflow', |
| 23 | + name: 'My Workflow', |
| 24 | + tasks: [ |
| 25 | + { |
| 26 | + id: 'transform', |
| 27 | + name: 'Transform Data', |
| 28 | + function: { |
| 29 | + name: 'map', |
| 30 | + input: { |
| 31 | + mappings: [ |
| 32 | + { path: 'data.output', logic: { var: 'data.input' } } |
| 33 | + ] |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | +]; |
| 40 | + |
| 41 | +// Create the engine |
| 42 | +const engine = new WasmEngine(workflows); |
| 43 | + |
| 44 | +// Process a message |
| 45 | +const message = { |
| 46 | + data: { input: 'hello world' }, |
| 47 | + metadata: {} |
| 48 | +}; |
| 49 | + |
| 50 | +const result = await engine.process(message); |
| 51 | +console.log(result.data.output); // 'hello world' |
| 52 | +``` |
| 53 | + |
| 54 | +## API Reference |
| 55 | + |
| 56 | +### WasmEngine |
| 57 | + |
| 58 | +The main class for executing workflows. |
| 59 | + |
| 60 | +```typescript |
| 61 | +class WasmEngine { |
| 62 | + constructor(workflows: Workflow[]); |
| 63 | + |
| 64 | + // Process a message through all matching workflows |
| 65 | + process(message: Message): Promise<Message>; |
| 66 | + |
| 67 | + // Process with execution trace for debugging |
| 68 | + processWithTrace(message: Message): Promise<ExecutionTrace>; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Types |
| 73 | + |
| 74 | +```typescript |
| 75 | +interface Workflow { |
| 76 | + id: string; |
| 77 | + name: string; |
| 78 | + condition?: JsonLogicValue; // Optional condition to run this workflow |
| 79 | + tasks: Task[]; |
| 80 | +} |
| 81 | + |
| 82 | +interface Task { |
| 83 | + id: string; |
| 84 | + name: string; |
| 85 | + condition?: JsonLogicValue; // Optional condition to run this task |
| 86 | + function: FunctionConfig; |
| 87 | +} |
| 88 | + |
| 89 | +interface FunctionConfig { |
| 90 | + name: string; // 'map' or 'validation' |
| 91 | + input: object; |
| 92 | +} |
| 93 | + |
| 94 | +interface Message { |
| 95 | + data: object; |
| 96 | + metadata: object; |
| 97 | + payload?: object; |
| 98 | + temp_data?: object; |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Execution Tracing |
| 103 | + |
| 104 | +For debugging, use `processWithTrace` to get step-by-step execution details: |
| 105 | + |
| 106 | +```typescript |
| 107 | +const trace = await engine.processWithTrace(message); |
| 108 | + |
| 109 | +console.log('Steps executed:', trace.steps.length); |
| 110 | +console.log('Initial message:', trace.initial_message); |
| 111 | +console.log('Final message:', trace.final_message); |
| 112 | + |
| 113 | +for (const step of trace.steps) { |
| 114 | + console.log(`Task: ${step.task_name}`); |
| 115 | + console.log(`Changes: ${step.changes.length}`); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Building from Source |
| 120 | + |
| 121 | +Requirements: |
| 122 | +- Rust 1.70+ |
| 123 | +- wasm-pack |
| 124 | + |
| 125 | +```bash |
| 126 | +cd wasm |
| 127 | +wasm-pack build --target web --out-dir pkg |
| 128 | +``` |
| 129 | + |
| 130 | +The output will be in `wasm/pkg/`. |
| 131 | + |
| 132 | +## Browser Compatibility |
| 133 | + |
| 134 | +The WASM package works in all modern browsers that support WebAssembly: |
| 135 | +- Chrome 57+ |
| 136 | +- Firefox 52+ |
| 137 | +- Safari 11+ |
| 138 | +- Edge 16+ |
| 139 | + |
| 140 | +## Next Steps |
| 141 | + |
| 142 | +- [UI Package](../ui/overview.md) - React visualization components |
| 143 | +- [Built-in Functions](../built-in-functions/overview.md) - Map and validation functions |
0 commit comments