Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add packet diagram #4839

Merged
merged 31 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c41594d
feat: Add packet diagram
sidharthv96 Sep 14, 2023
afd7cf5
fix: Types and nomenclature
sidharthv96 Sep 14, 2023
38d9c6d
test: Add unit tests
sidharthv96 Sep 14, 2023
75f1f92
test: Add rendering tests
sidharthv96 Sep 14, 2023
043729f
feat: Support showBits
sidharthv96 Sep 14, 2023
136f1c5
fix: Diagram DB types
sidharthv96 Sep 14, 2023
0ff3ba3
Add mini-live editing to example.html
sidharthv96 Sep 18, 2023
5661509
Add mini-live editing to example.html
sidharthv96 Sep 18, 2023
3e3519e
docs: Add packet to index
sidharthv96 Sep 19, 2023
ce6bfcb
Merge branch 'next' into feat/948_packetDiagram
sidharthv96 Nov 14, 2023
7c79bbd
refactor: Address review comments
sidharthv96 Nov 14, 2023
b88d1df
fix(packet): Grammar whitespace
sidharthv96 Nov 14, 2023
659db9f
chore: Add types and minimums for packet config
sidharthv96 Nov 14, 2023
9925b9b
feat: Add title support to packetDiagram
sidharthv96 Nov 14, 2023
1a8743e
refactor: Separate default options in styles
sidharthv96 Nov 14, 2023
068a74a
chore: Fix magic number
sidharthv96 Nov 14, 2023
fc400ea
fix: Langium formatting
sidharthv96 Nov 14, 2023
b6983e4
Add title to tests
sidharthv96 Nov 14, 2023
113a400
Add DiagramRenderer type
sidharthv96 Nov 14, 2023
f9df193
chore: Fix imports
sidharthv96 Nov 14, 2023
99313fe
fix editor build
sidharthv96 Nov 15, 2023
f15d24b
Merge branch 'next' into feat/948_packetDiagram
sidharthv96 Nov 15, 2023
0d7644c
refactor: Fix types
sidharthv96 Nov 15, 2023
088fc39
Fix SVG width
sidharthv96 Nov 15, 2023
784a853
feat: Add theming to packet
sidharthv96 Nov 15, 2023
480645d
fix: Add language to codeblock
sidharthv96 Nov 15, 2023
a8105f0
fix: Use configureSvgSize
sidharthv96 Nov 16, 2023
bca645f
test: Add packet and xychart into mermaidAPI test
sidharthv96 Nov 16, 2023
96ae4a5
chore: Remove unnecessary(?) mocks.
sidharthv96 Nov 16, 2023
7ef61d5
test: Add packet test for title, accTitle, accDescr
sidharthv96 Nov 16, 2023
5cc20b5
Merge branch 'next' into feat/948_packetDiagram
sidharthv96 Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .build/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const MERMAID_CONFIG_DIAGRAM_KEYS = [
'gitGraph',
'c4',
'sankey',
'packet',
] as const;

/**
Expand Down
7 changes: 4 additions & 3 deletions .esbuild/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { generateLangium } from '../.build/generateLangium.js';
const parserCtx = await context(
getBuildConfig({ ...defaultOptions, minify: false, core: false, entryName: 'parser' })
);
const mermaidCtx = await context(
getBuildConfig({ ...defaultOptions, minify: false, core: false, entryName: 'mermaid' })
);
const mermaidCtx = await context({
...getBuildConfig({ ...defaultOptions, minify: false, core: false, entryName: 'mermaid' }),
sourcemap: 'linked',
});
const mermaidIIFECtx = await context(
getBuildConfig({
...defaultOptions,
Expand Down
65 changes: 65 additions & 0 deletions cypress/integration/rendering/packet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util';
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved

describe('packet structure', () => {
it('should render a simple packet diagram', () => {
imgSnapshotTest(
`packet-beta
0-10: "hello"
`
);
});

it('should render a complex packet diagram', () => {
imgSnapshotTest(
`packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-223: "data"
`
);
});

it('should render a complex packet diagram with showBits false', () => {
imgSnapshotTest(
`
---
config:
packet:
showBits: false
---
packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-223: "data"
`
);
});
});
73 changes: 73 additions & 0 deletions demos/packet.html
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Mermaid Quick Test Page</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=" />
<style>
div.mermaid {
font-family: 'Courier New', Courier, monospace !important;
}
</style>
</head>

<body>
<h1>Packet diagram demo</h1>
<pre class="mermaid">
packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-223: "data"
</pre>

<pre class="mermaid">
---
config:
packet:
showBits: false
---
packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-223: "data"
</pre>

<script type="module">
import mermaid from '/mermaid.esm.mjs';
mermaid.initialize({
theme: 'forest',
logLevel: 3,
securityLevel: 'loose',
});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[defaultConfig.ts:268](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L268)
[defaultConfig.ts:275](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L275)

---

Expand Down
33 changes: 33 additions & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,43 @@ export interface MermaidConfig {
gitGraph?: GitGraphDiagramConfig;
c4?: C4DiagramConfig;
sankey?: SankeyDiagramConfig;
packet?: PacketDiagramConfig;
dompurifyConfig?: DOMPurifyConfiguration;
wrap?: boolean;
fontSize?: number;
}
/**
* The object containing configurations specific for packet diagrams.
*
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "PacketDiagramConfig".
*/
export interface PacketDiagramConfig extends BaseDiagramConfig {
/**
* The height of each row in the packet diagram.
*/
rowHeight?: number;
/**
* The width of each bit in the packet diagram.
*/
bitWidth?: number;
/**
* The number of bits to display per row.
*/
bitsPerRow?: number;
/**
* Toggle to display or hide bit numbers.
*/
showBits?: boolean;
/**
* The horizontal padding between the blocks in a row.
*/
paddingX?: number;
/**
* The vertical padding between the rows.
*/
paddingY?: number;
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "BaseDiagramConfig".
Expand Down
7 changes: 7 additions & 0 deletions packages/mermaid/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ const config: RequiredDeep<MermaidConfig> = {
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
},
packet: {
...defaultConfigJson.packet,
useWidth: undefined,
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
// this is false, unlike every other diagram (other than gitGraph)
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
},
};

const keyify = (obj: any, prefix = ''): string[] =>
Expand Down
4 changes: 3 additions & 1 deletion packages/mermaid/src/diagram-api/diagram-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import flowchartElk from '../diagrams/flowchart/elk/detector.js';
import timeline from '../diagrams/timeline/detector.js';
import mindmap from '../diagrams/mindmap/detector.js';
import sankey from '../diagrams/sankey/sankeyDetector.js';
import { packet } from '../diagrams/packet/detector.js';
import { registerLazyLoadedDiagrams } from './detectType.js';
import { registerDiagram } from './diagramAPI.js';

Expand Down Expand Up @@ -84,6 +85,7 @@ export const addDiagrams = () => {
state,
journey,
quadrantChart,
sankey
sankey,
packet
);
};
110 changes: 110 additions & 0 deletions packages/mermaid/src/diagrams/packet/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import type { Block, PacketDB, Row } from './types.js';
import type { PacketDiagramConfig } from '../../config.type.js';

import { log } from '../../logger.js';
import DEFAULT_CONFIG from '../../defaultConfig.js';
import { getConfig as commonGetConfig } from '../../config.js';

interface PacketData {
packet: Row[];
}
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved

const defaultPacketData: PacketData = {
packet: [],
};

let data: PacketData = structuredClone(defaultPacketData);
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;

export const getConfig = (): Required<PacketDiagramConfig> => {
const config = structuredClone({
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
...DEFAULT_PACKET_CONFIG,
...commonGetConfig().packet,
});
if (config.showBits) {
config.paddingY += 10;
}
return config;
};

export const getPacket = (): Row[] => data.packet;

export const getNextFittingBlock = (
block: Block,
row: number,
bitsPerRow: number
): [Required<Block>, Block | undefined] => {
if (block.end === undefined) {
block.end = block.start;
}

if (block.start > block.end) {
throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`);
}

if (block.end + 1 <= row * bitsPerRow) {
return [block as Required<Block>, undefined];
}

return [
{
start: block.start,
end: row * bitsPerRow - 1,
label: block.label,
},
{
start: row * bitsPerRow,
end: block.end,
label: block.label,
},
];
};

export const populate = ({ blocks }: { blocks: Block[] }) => {
let lastByte = -1;
let word: Row = [];
data.packet = [];
let row = 1;
const { bitsPerRow } = getConfig();
for (let { start, end, label } of blocks) {
if (end && end < start) {
throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
}
if (start != lastByte + 1) {
throw new Error(
`Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
lastByte + 1
}.`
);
}
lastByte = end ?? start;
log.debug(`Packet block ${start} - ${lastByte} with label ${label}`);

while (word.length <= bitsPerRow + 1 && data.packet.length < 10_000) {
const [block, nextBlock] = getNextFittingBlock({ start, end, label }, row, bitsPerRow);
word.push(block);
if (block.end + 1 === row * bitsPerRow) {
data.packet.push(word);
word = [];
row++;
}
if (!nextBlock) {
break;
}
({ start, end, label } = nextBlock);
}
}
if (word.length > 0) {
data.packet.push(word);
}
log.debug(data);
};

export const clear = () => {
data = structuredClone(defaultPacketData);
};

export const db: PacketDB = {
getPacket,
getConfig,
};
22 changes: 22 additions & 0 deletions packages/mermaid/src/diagrams/packet/detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {
DiagramDetector,
DiagramLoader,
ExternalDiagramDefinition,
} from '../../diagram-api/types.js';

const id = 'packet';

const detector: DiagramDetector = (txt) => {
return /^\s*packet-beta/.test(txt);
};

const loader: DiagramLoader = async () => {
const { diagram } = await import('./diagram.js');
return { id, diagram };
};

export const packet: ExternalDiagramDefinition = {
id,
detector,
loader,
};
12 changes: 12 additions & 0 deletions packages/mermaid/src/diagrams/packet/diagram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DiagramDefinition } from '../../diagram-api/types.js';
import { parser } from './parser.js';
import { db } from './db.js';
import { renderer } from './renderer.js';
import { styles } from './styles.js';

export const diagram: DiagramDefinition = {
parser,
db,
renderer,
styles,
};
Loading
Loading