sfn-diagram 0.3.0 → 0.5.1

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,9 +2,12 @@
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
@@ -15,6 +18,7 @@ Generate beautiful, interactive diagrams from AWS Step Functions ASL (Amazon Sta
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,50 @@ Generate beautiful, interactive diagrams from AWS Step Functions ASL (Amazon Sta
22
26
  npm install sfn-diagram
23
27
  ```
24
28
 
29
+ ## Runtime Support
30
+
31
+ The core entry (`sfn-diagram`) builds SVG with a DOM-free string renderer, so
32
+ `generateSvg`, `generateMermaid`, `generateDiagram`, and `generateFromAwsResponse`
33
+ run in **Node, browsers, and edge runtimes** (Cloudflare Workers, Vercel Edge, Deno, Bun)
34
+ with no DOM polyfill.
35
+
36
+ PNG export (`sfn-diagram/png`) and the CLI are **Node-only** — they rely on a headless
37
+ browser (`node-html-to-image`) and Node's filesystem respectively.
38
+
39
+ ```ts
40
+ // Works in Node, browser, and edge:
41
+ import { generateSvg } from 'sfn-diagram';
42
+
43
+ // Node-only:
44
+ import { exportPng } from 'sfn-diagram/png';
45
+ ```
46
+
47
+ ## Command-line Usage
48
+
49
+ The package ships a CLI for use without writing any JavaScript:
50
+
51
+ ```bash
52
+ npx sfn-diagram state.asl.json --format svg -o diagram.svg
53
+ npx sfn-diagram state.asl.json --format mermaid > diagram.mmd
54
+ cat state.asl.json | npx sfn-diagram - --format svg
55
+ ```
56
+
57
+ Flags: `--format <svg|mermaid|png>`, `-o/--output <path>`, `--theme <light|dark>`, `--layout <TB|LR|RL|BT>`, `-h/--help`, `-v/--version`.
58
+
59
+ ## Docker
60
+
61
+ A prebuilt image is published to GitHub Container Registry with Chromium baked in, so PNG export works out of the box:
62
+
63
+ ```bash
64
+ docker run --rm -v "$PWD":/work ghcr.io/yusufaf/sfn-diagram:latest \
65
+ /work/state.asl.json --format svg -o /work/diagram.svg
66
+
67
+ docker run --rm -v "$PWD":/work ghcr.io/yusufaf/sfn-diagram:latest \
68
+ /work/state.asl.json --format png -o /work/diagram.png
69
+ ```
70
+
71
+ Tags: `latest`, `<major>`, `<major>.<minor>`, `<major>.<minor>.<patch>`.
72
+
25
73
  ## Quick Start
26
74
 
27
75
  ### Function-based API
@@ -56,15 +104,15 @@ console.log(`Generated ${width}x${height} diagram`);
56
104
  ```typescript
57
105
  import { SfnDiagramGenerator } from 'sfn-diagram';
58
106
 
59
- const generator = new SfnDiagramGenerator()
60
- .setAsl(asl)
61
- .setTheme('dark')
62
- .setLayout('TB')
63
- .setNodeDimensions({ width: 150, height: 80 });
107
+ const generator = new SfnDiagramGenerator({
108
+ theme: 'dark',
109
+ layout: 'TB',
110
+ nodeWidth: 150,
111
+ nodeHeight: 80,
112
+ });
64
113
 
65
- const { svg } = generator.generateSvg();
66
- const { mermaid } = generator.generateMermaid();
67
- const { png } = await generator.exportPng();
114
+ const { svg } = generator.generateSvg({ aslDefinition: asl });
115
+ const { code } = generator.generateMermaid({ aslDefinition: asl });
68
116
  ```
69
117
 
70
118
  ## API Reference
@@ -91,7 +139,7 @@ const result = generateSvg({
91
139
  customColors: {} // Override colors for specific states
92
140
  });
93
141
 
94
- // Returns: { svg: string, width: number, height: number, nodeCount: number }
142
+ // Returns SvgOutput: { svg: string, width: number, height: number, metadata: { edgeCount: number, nodeCount: number } }
95
143
  ```
96
144
 
97
145
  ### generateMermaid(params)
@@ -106,40 +154,43 @@ const result = generateMermaid({
106
154
  includeComments: true
107
155
  });
108
156
 
109
- // Returns: { mermaid: string, nodeCount: number, edgeCount: number }
157
+ // Returns MermaidOutput: { code: string, metadata: { edgeCount: number, stateCount: number } }
110
158
  ```
111
159
 
112
160
  ### generateDiagram(params)
113
161
 
114
- Generate both SVG and Mermaid output simultaneously.
162
+ Generate a diagram, choosing the output format via the `format` option (defaults to `'svg'`).
115
163
 
116
164
  ```typescript
117
165
  import { generateDiagram } from 'sfn-diagram';
118
166
 
119
- const result = generateDiagram({
167
+ const svgResult = generateDiagram({
120
168
  aslDefinition: asl,
121
169
  theme: 'dark'
122
- });
170
+ }); // Returns SvgOutput
123
171
 
124
- // Returns: { svg: SvgOutput, mermaid: MermaidOutput }
172
+ const mermaidResult = generateDiagram({
173
+ aslDefinition: asl,
174
+ format: 'mermaid'
175
+ }); // Returns MermaidOutput
125
176
  ```
126
177
 
127
178
  ### exportPng(params)
128
179
 
129
- Export diagram as PNG image.
180
+ Export diagram as PNG image. Node-only — imported from the `sfn-diagram/png` subpath.
130
181
 
131
182
  ```typescript
132
- import { exportPng } from 'sfn-diagram';
183
+ import { exportPng } from 'sfn-diagram/png';
133
184
 
134
185
  const result = await exportPng({
135
186
  aslDefinition: asl,
136
187
  theme: 'light',
137
- quality: 1.0,
138
- transparent: false
188
+ pngQuality: 90, // 1–100 (default 90)
189
+ backgroundColor: 'transparent'
139
190
  });
140
191
 
141
- // Returns: { png: Buffer, width: number, height: number }
142
- writeFileSync('diagram.png', result.png);
192
+ // Returns PngOutput: { buffer: Buffer, width: number, height: number, metadata: { format: 'png' } }
193
+ writeFileSync('diagram.png', result.buffer);
143
194
  ```
144
195
 
145
196
  ### generateFromAwsResponse(params)
@@ -158,33 +209,39 @@ const response = await client.send(
158
209
  );
159
210
 
160
211
  const { svg } = generateFromAwsResponse({
161
- awsResponse: response,
212
+ response,
162
213
  theme: 'dark'
163
214
  });
164
215
  ```
165
216
 
166
217
  ### SfnDiagramGenerator Class
167
218
 
168
- Fluent interface for generating diagrams.
219
+ 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.
169
220
 
170
221
  ```typescript
171
222
  import { SfnDiagramGenerator } from 'sfn-diagram';
172
223
 
173
- const generator = new SfnDiagramGenerator()
174
- .setAsl(asl)
175
- .setTheme('dark')
176
- .setLayout('LR')
177
- .setNodeDimensions({ width: 150, height: 80 })
178
- .setSpacing({ rankSeparation: 60, nodeSeparation: 60 })
179
- .setEdgeStyle('curved')
180
- .setPadding(30);
181
-
182
- // Generate outputs
183
- const svgResult = generator.generateSvg();
184
- const mermaidResult = generator.generateMermaid();
185
- const pngResult = await generator.exportPng({ quality: 1.0 });
224
+ const generator = new SfnDiagramGenerator({
225
+ theme: 'dark',
226
+ layout: 'LR',
227
+ nodeWidth: 150,
228
+ nodeHeight: 80,
229
+ rankSeparation: 60,
230
+ nodeSeparation: 60,
231
+ edgeStyle: 'curved',
232
+ padding: 30,
233
+ });
234
+
235
+ // Update options later (chainable)
236
+ generator.setOptions({ theme: 'light' });
237
+
238
+ // Generate outputs (aslDefinition passed per call)
239
+ const svgResult = generator.generateSvg({ aslDefinition: asl });
240
+ const mermaidResult = generator.generateMermaid({ aslDefinition: asl });
186
241
  ```
187
242
 
243
+ > PNG export is a standalone function from `sfn-diagram/png` (`exportPng`), not a method on this class.
244
+
188
245
  ## Configuration Options
189
246
 
190
247
  ### Themes
@@ -408,21 +465,23 @@ const { svg } = generateSvg({ aslDefinition: parallelAsl });
408
465
  ### Export Multiple Formats
409
466
 
410
467
  ```typescript
411
- import { generateDiagram, exportPng } from 'sfn-diagram';
468
+ import { generateSvg, generateMermaid } from 'sfn-diagram';
469
+ import { exportPng } from 'sfn-diagram/png';
412
470
  import { writeFileSync } from 'fs';
413
471
 
414
472
  // Generate SVG and Mermaid
415
- const { svg, mermaid } = generateDiagram({ aslDefinition: asl });
416
- writeFileSync('diagram.svg', svg.svg);
417
- writeFileSync('diagram.mmd', mermaid.mermaid);
473
+ const { svg } = generateSvg({ aslDefinition: asl });
474
+ const { code } = generateMermaid({ aslDefinition: asl });
475
+ writeFileSync('diagram.svg', svg);
476
+ writeFileSync('diagram.mmd', code);
418
477
 
419
- // Generate PNG
420
- const { png } = await exportPng({
478
+ // Generate PNG (Node-only)
479
+ const { buffer } = await exportPng({
421
480
  aslDefinition: asl,
422
- quality: 1.0,
423
- transparent: false
481
+ pngQuality: 90,
482
+ backgroundColor: 'transparent'
424
483
  });
425
- writeFileSync('diagram.png', png);
484
+ writeFileSync('diagram.png', buffer);
426
485
  ```
427
486
 
428
487
  ## TypeScript Support
@@ -435,10 +494,62 @@ import type {
435
494
  DiagramOptions,
436
495
  SvgOutput,
437
496
  MermaidOutput,
438
- PngOutput,
439
497
  CustomTheme,
440
498
  StateType
441
499
  } from 'sfn-diagram';
500
+
501
+ // PNG types live on the Node-only subpath
502
+ import type { PngOutput, ExportPngParams } from 'sfn-diagram/png';
503
+ ```
504
+
505
+ ## Ecosystem
506
+
507
+ ### Playground
508
+
509
+ 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.
510
+
511
+ ```bash
512
+ cd playground
513
+ pnpm install
514
+ pnpm dev
515
+ ```
516
+
517
+ Paste any ASL JSON, switch themes, and see the SVG diagram update instantly — no install required beyond the dev server.
518
+
519
+ ### VS Code Extension
520
+
521
+ Preview Step Functions diagrams directly inside VS Code. Located in [`packages/vscode-sfn-diagram/`](packages/vscode-sfn-diagram/).
522
+
523
+ **Install from source:**
524
+ ```bash
525
+ cd packages/vscode-sfn-diagram
526
+ pnpm install
527
+ pnpm package # produces vscode-sfn-diagram-*.vsix
528
+ code --install-extension vscode-sfn-diagram-*.vsix
529
+ ```
530
+
531
+ **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.
532
+
533
+ ### GitHub Action
534
+
535
+ 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/).
536
+
537
+ ```yaml
538
+ # .github/workflows/sfn-preview.yml
539
+ name: Step Functions Preview
540
+ on:
541
+ pull_request:
542
+ paths: ['**/*.asl.json']
543
+
544
+ jobs:
545
+ preview:
546
+ runs-on: ubuntu-latest
547
+ steps:
548
+ - uses: actions/checkout@v4
549
+ with: { fetch-depth: 0 }
550
+ - uses: ./packages/github-action-sfn-diagram
551
+ with:
552
+ github-token: ${{ secrets.GITHUB_TOKEN }}
442
553
  ```
443
554
 
444
555
  ## Contributing