sfn-diagram 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,19 +2,23 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/sfn-diagram.svg)](https://www.npmjs.com/package/sfn-diagram)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![Live Playground](https://img.shields.io/badge/playground-live-brightgreen)](https://yusufaf.github.io/sfn-diagram/)
5
6
 
6
7
  Generate beautiful, interactive diagrams from AWS Step Functions ASL (Amazon States Language) definitions. Supports dual output formats: D3.js-based SVG and Mermaid.js diagram code, plus PNG export.
7
8
 
9
+ **▶ [Try it in the live playground](https://yusufaf.github.io/sfn-diagram/)** — paste any ASL definition and preview the diagram instantly, no install required.
10
+
8
11
  ## Features
9
12
 
10
13
  - **Multiple Output Formats**: SVG (D3.js), Mermaid syntax, and PNG
11
14
  - **Automatic Layout**: Smart graph positioning using Dagre layout engine
12
- - **Full ASL Support**: All state types (Pass, Task, Choice, Wait, Succeed, Fail, Parallel, Map)
15
+ - **Full ASL Support**: All state types (Pass, Task, Choice, Wait, Succeed, Fail, Parallel, Map), both JSONPath and JSONata query languages
13
16
  - **Customizable Themes**: AWS light/dark themes plus custom theme support
14
17
  - **Flexible Layouts**: Top-bottom, left-right, right-left, bottom-top
15
18
  - **Type-Safe**: Full TypeScript support with comprehensive type definitions
16
19
  - **AWS SDK Integration**: Direct integration with AWS Step Functions API
17
20
  - **Dual APIs**: Function-based and class-based interfaces
21
+ - **Runs Anywhere**: SVG and Mermaid generation has zero platform dependencies — works in Node, the browser, and edge runtimes
18
22
 
19
23
  ## Installation
20
24
 
@@ -22,6 +26,32 @@ Generate beautiful, interactive diagrams from AWS Step Functions ASL (Amazon Sta
22
26
  npm install sfn-diagram
23
27
  ```
24
28
 
29
+ The core package pulls in **no browser engine** — SVG and Mermaid generation stay lightweight. PNG export relies on a headless browser, provided by the optional peer dependency `node-html-to-image`. Install it only if you use `sfn-diagram/png`:
30
+
31
+ ```bash
32
+ npm install sfn-diagram node-html-to-image
33
+ ```
34
+
35
+ ## Runtime Support
36
+
37
+ The core entry (`sfn-diagram`) builds SVG with a DOM-free string renderer, so
38
+ `generateSvg`, `generateMermaid`, `generateDiagram`, and `generateFromAwsResponse`
39
+ run in **Node, browsers, and edge runtimes** (Cloudflare Workers, Vercel Edge, Deno, Bun)
40
+ with no DOM polyfill.
41
+
42
+ PNG export (`sfn-diagram/png`) and the CLI are **Node-only** — they rely on a headless
43
+ browser (`node-html-to-image`) and Node's filesystem respectively. Because `node-html-to-image`
44
+ is an **optional peer dependency**, install it alongside `sfn-diagram` when you need PNG output;
45
+ `exportPng` throws an actionable error if it is missing.
46
+
47
+ ```ts
48
+ // Works in Node, browser, and edge:
49
+ import { generateSvg } from 'sfn-diagram';
50
+
51
+ // Node-only:
52
+ import { exportPng } from 'sfn-diagram/png';
53
+ ```
54
+
25
55
  ## Command-line Usage
26
56
 
27
57
  The package ships a CLI for use without writing any JavaScript:
@@ -82,15 +112,15 @@ console.log(`Generated ${width}x${height} diagram`);
82
112
  ```typescript
83
113
  import { SfnDiagramGenerator } from 'sfn-diagram';
84
114
 
85
- const generator = new SfnDiagramGenerator()
86
- .setAsl(asl)
87
- .setTheme('dark')
88
- .setLayout('TB')
89
- .setNodeDimensions({ width: 150, height: 80 });
115
+ const generator = new SfnDiagramGenerator({
116
+ theme: 'dark',
117
+ layout: 'TB',
118
+ nodeWidth: 150,
119
+ nodeHeight: 80,
120
+ });
90
121
 
91
- const { svg } = generator.generateSvg();
92
- const { mermaid } = generator.generateMermaid();
93
- const { png } = await generator.exportPng();
122
+ const { svg } = generator.generateSvg({ aslDefinition: asl });
123
+ const { code } = generator.generateMermaid({ aslDefinition: asl });
94
124
  ```
95
125
 
96
126
  ## API Reference
@@ -117,7 +147,7 @@ const result = generateSvg({
117
147
  customColors: {} // Override colors for specific states
118
148
  });
119
149
 
120
- // Returns: { svg: string, width: number, height: number, nodeCount: number }
150
+ // Returns SvgOutput: { svg: string, width: number, height: number, metadata: { edgeCount: number, nodeCount: number } }
121
151
  ```
122
152
 
123
153
  ### generateMermaid(params)
@@ -132,40 +162,43 @@ const result = generateMermaid({
132
162
  includeComments: true
133
163
  });
134
164
 
135
- // Returns: { mermaid: string, nodeCount: number, edgeCount: number }
165
+ // Returns MermaidOutput: { code: string, metadata: { edgeCount: number, stateCount: number } }
136
166
  ```
137
167
 
138
168
  ### generateDiagram(params)
139
169
 
140
- Generate both SVG and Mermaid output simultaneously.
170
+ Generate a diagram, choosing the output format via the `format` option (defaults to `'svg'`).
141
171
 
142
172
  ```typescript
143
173
  import { generateDiagram } from 'sfn-diagram';
144
174
 
145
- const result = generateDiagram({
175
+ const svgResult = generateDiagram({
146
176
  aslDefinition: asl,
147
177
  theme: 'dark'
148
- });
178
+ }); // Returns SvgOutput
149
179
 
150
- // Returns: { svg: SvgOutput, mermaid: MermaidOutput }
180
+ const mermaidResult = generateDiagram({
181
+ aslDefinition: asl,
182
+ format: 'mermaid'
183
+ }); // Returns MermaidOutput
151
184
  ```
152
185
 
153
186
  ### exportPng(params)
154
187
 
155
- Export diagram as PNG image.
188
+ Export diagram as PNG image. Node-only — imported from the `sfn-diagram/png` subpath.
156
189
 
157
190
  ```typescript
158
- import { exportPng } from 'sfn-diagram';
191
+ import { exportPng } from 'sfn-diagram/png';
159
192
 
160
193
  const result = await exportPng({
161
194
  aslDefinition: asl,
162
195
  theme: 'light',
163
- quality: 1.0,
164
- transparent: false
196
+ pngQuality: 90, // 1–100 (default 90)
197
+ backgroundColor: 'transparent'
165
198
  });
166
199
 
167
- // Returns: { png: Buffer, width: number, height: number }
168
- writeFileSync('diagram.png', result.png);
200
+ // Returns PngOutput: { buffer: Buffer, width: number, height: number, metadata: { format: 'png' } }
201
+ writeFileSync('diagram.png', result.buffer);
169
202
  ```
170
203
 
171
204
  ### generateFromAwsResponse(params)
@@ -184,33 +217,39 @@ const response = await client.send(
184
217
  );
185
218
 
186
219
  const { svg } = generateFromAwsResponse({
187
- awsResponse: response,
220
+ response,
188
221
  theme: 'dark'
189
222
  });
190
223
  ```
191
224
 
192
225
  ### SfnDiagramGenerator Class
193
226
 
194
- Fluent interface for generating diagrams.
227
+ Reusable generator that holds diagram options once and applies them to every call. Configure options via the constructor or the fluent `setOptions()` method; pass the `aslDefinition` per generation.
195
228
 
196
229
  ```typescript
197
230
  import { SfnDiagramGenerator } from 'sfn-diagram';
198
231
 
199
- const generator = new SfnDiagramGenerator()
200
- .setAsl(asl)
201
- .setTheme('dark')
202
- .setLayout('LR')
203
- .setNodeDimensions({ width: 150, height: 80 })
204
- .setSpacing({ rankSeparation: 60, nodeSeparation: 60 })
205
- .setEdgeStyle('curved')
206
- .setPadding(30);
207
-
208
- // Generate outputs
209
- const svgResult = generator.generateSvg();
210
- const mermaidResult = generator.generateMermaid();
211
- const pngResult = await generator.exportPng({ quality: 1.0 });
232
+ const generator = new SfnDiagramGenerator({
233
+ theme: 'dark',
234
+ layout: 'LR',
235
+ nodeWidth: 150,
236
+ nodeHeight: 80,
237
+ rankSeparation: 60,
238
+ nodeSeparation: 60,
239
+ edgeStyle: 'curved',
240
+ padding: 30,
241
+ });
242
+
243
+ // Update options later (chainable)
244
+ generator.setOptions({ theme: 'light' });
245
+
246
+ // Generate outputs (aslDefinition passed per call)
247
+ const svgResult = generator.generateSvg({ aslDefinition: asl });
248
+ const mermaidResult = generator.generateMermaid({ aslDefinition: asl });
212
249
  ```
213
250
 
251
+ > PNG export is a standalone function from `sfn-diagram/png` (`exportPng`), not a method on this class.
252
+
214
253
  ## Configuration Options
215
254
 
216
255
  ### Themes
@@ -332,7 +371,16 @@ All AWS Step Functions state types are fully supported:
332
371
  | **Succeed** | Circle | Terminates successfully |
333
372
  | **Fail** | Circle | Terminates with failure |
334
373
  | **Parallel** | Rectangle | Executes branches in parallel |
335
- | **Map** | Rectangle | Iterates over array items |
374
+ | **Map** | Rectangle | Iterates over array items (legacy `Iterator` and modern `ItemProcessor`/Distributed Map) |
375
+
376
+ ### Error handling & retries
377
+
378
+ - **`Catch`** blocks render as dashed error edges to their handler states.
379
+ - **`Retry`** policies render as a labelled self-loop on the state (e.g. `↻ States.Timeout (4x); States.ALL (2x)`) — a self-transition in Mermaid output.
380
+
381
+ ### Query languages
382
+
383
+ Both JSONPath and JSONata (`QueryLanguage: "JSONata"`) definitions are supported. Choice branch labels are derived from JSONPath comparison operators (`$.score >= 90`, `And`/`Or`/`Not`, `Is*` checks) or from JSONata `Condition` expressions, whichever the state uses.
336
384
 
337
385
  ## Examples
338
386
 
@@ -434,21 +482,23 @@ const { svg } = generateSvg({ aslDefinition: parallelAsl });
434
482
  ### Export Multiple Formats
435
483
 
436
484
  ```typescript
437
- import { generateDiagram, exportPng } from 'sfn-diagram';
485
+ import { generateSvg, generateMermaid } from 'sfn-diagram';
486
+ import { exportPng } from 'sfn-diagram/png';
438
487
  import { writeFileSync } from 'fs';
439
488
 
440
489
  // Generate SVG and Mermaid
441
- const { svg, mermaid } = generateDiagram({ aslDefinition: asl });
442
- writeFileSync('diagram.svg', svg.svg);
443
- writeFileSync('diagram.mmd', mermaid.mermaid);
490
+ const { svg } = generateSvg({ aslDefinition: asl });
491
+ const { code } = generateMermaid({ aslDefinition: asl });
492
+ writeFileSync('diagram.svg', svg);
493
+ writeFileSync('diagram.mmd', code);
444
494
 
445
- // Generate PNG
446
- const { png } = await exportPng({
495
+ // Generate PNG (Node-only)
496
+ const { buffer } = await exportPng({
447
497
  aslDefinition: asl,
448
- quality: 1.0,
449
- transparent: false
498
+ pngQuality: 90,
499
+ backgroundColor: 'transparent'
450
500
  });
451
- writeFileSync('diagram.png', png);
501
+ writeFileSync('diagram.png', buffer);
452
502
  ```
453
503
 
454
504
  ## TypeScript Support
@@ -461,10 +511,62 @@ import type {
461
511
  DiagramOptions,
462
512
  SvgOutput,
463
513
  MermaidOutput,
464
- PngOutput,
465
514
  CustomTheme,
466
515
  StateType
467
516
  } from 'sfn-diagram';
517
+
518
+ // PNG types live on the Node-only subpath
519
+ import type { PngOutput, ExportPngParams } from 'sfn-diagram/png';
520
+ ```
521
+
522
+ ## Ecosystem
523
+
524
+ ### Playground
525
+
526
+ An interactive browser-based editor for exploring ASL definitions and previewing diagrams in real time. **[Open the live playground →](https://yusufaf.github.io/sfn-diagram/)** or run it locally from the [`playground/`](playground/) directory.
527
+
528
+ ```bash
529
+ cd playground
530
+ pnpm install
531
+ pnpm dev
532
+ ```
533
+
534
+ Paste any ASL JSON, switch themes, and see the SVG diagram update instantly — no install required beyond the dev server.
535
+
536
+ ### VS Code Extension
537
+
538
+ Preview Step Functions diagrams directly inside VS Code. Located in [`packages/vscode-sfn-diagram/`](packages/vscode-sfn-diagram/).
539
+
540
+ **Install from source:**
541
+ ```bash
542
+ cd packages/vscode-sfn-diagram
543
+ pnpm install
544
+ pnpm package # produces vscode-sfn-diagram-*.vsix
545
+ code --install-extension vscode-sfn-diagram-*.vsix
546
+ ```
547
+
548
+ **Usage:** Open any `.json` or `.asl` file and run **Step Functions: Preview Step Functions Diagram** from the command palette, or click the diagram icon in the editor title bar.
549
+
550
+ ### GitHub Action
551
+
552
+ Post SVG diff previews and Mermaid diagrams as PR comments whenever ASL files change. Located in [`packages/github-action-sfn-diagram/`](packages/github-action-sfn-diagram/).
553
+
554
+ ```yaml
555
+ # .github/workflows/sfn-preview.yml
556
+ name: Step Functions Preview
557
+ on:
558
+ pull_request:
559
+ paths: ['**/*.asl.json']
560
+
561
+ jobs:
562
+ preview:
563
+ runs-on: ubuntu-latest
564
+ steps:
565
+ - uses: actions/checkout@v4
566
+ with: { fetch-depth: 0 }
567
+ - uses: ./packages/github-action-sfn-diagram
568
+ with:
569
+ github-token: ${{ secrets.GITHUB_TOKEN }}
468
570
  ```
469
571
 
470
572
  ## Contributing