diff --git a/.changeset/vast-buses-see.md b/.changeset/vast-buses-see.md new file mode 100644 index 000000000..683a3bf3c --- /dev/null +++ b/.changeset/vast-buses-see.md @@ -0,0 +1,5 @@ +--- +'mermaid': minor +--- + +chore: Update packet diagram to use new class-based database structure diff --git a/packages/mermaid/src/diagrams/packet/db.ts b/packages/mermaid/src/diagrams/packet/db.ts index d7b550472..863bd79e1 100644 --- a/packages/mermaid/src/diagrams/packet/db.ts +++ b/packages/mermaid/src/diagrams/packet/db.ts @@ -1,6 +1,7 @@ import { getConfig as commonGetConfig } from '../../config.js'; import type { PacketDiagramConfig } from '../../config.type.js'; import DEFAULT_CONFIG from '../../defaultConfig.js'; +import type { DiagramDB } from '../../diagram-api/types.js'; import { cleanAndMerge } from '../../utils.js'; import { clear as commonClear, @@ -11,49 +12,42 @@ import { setAccTitle, setDiagramTitle, } from '../common/commonDb.js'; -import type { PacketDB, PacketData, PacketWord } from './types.js'; - -const defaultPacketData: PacketData = { - packet: [], -}; - -let data: PacketData = structuredClone(defaultPacketData); - +import type { PacketWord } from './types.js'; const DEFAULT_PACKET_CONFIG: Required = DEFAULT_CONFIG.packet; -const getConfig = (): Required => { - const config = cleanAndMerge({ - ...DEFAULT_PACKET_CONFIG, - ...commonGetConfig().packet, - }); - if (config.showBits) { - config.paddingY += 10; +export class PacketDB implements DiagramDB { + private packet: PacketWord[] = []; + + public getConfig() { + const config = cleanAndMerge({ + ...DEFAULT_PACKET_CONFIG, + ...commonGetConfig().packet, + }); + if (config.showBits) { + config.paddingY += 10; + } + return config; } - return config; -}; -const getPacket = (): PacketWord[] => data.packet; - -const pushWord = (word: PacketWord) => { - if (word.length > 0) { - data.packet.push(word); + public getPacket() { + return this.packet; } -}; -const clear = () => { - commonClear(); - data = structuredClone(defaultPacketData); -}; + public pushWord(word: PacketWord) { + if (word.length > 0) { + this.packet.push(word); + } + } -export const db: PacketDB = { - pushWord, - getPacket, - getConfig, - clear, - setAccTitle, - getAccTitle, - setDiagramTitle, - getDiagramTitle, - getAccDescription, - setAccDescription, -}; + public clear() { + commonClear(); + this.packet = []; + } + + public setAccTitle = setAccTitle; + public getAccTitle = getAccTitle; + public setDiagramTitle = setDiagramTitle; + public getDiagramTitle = getDiagramTitle; + public getAccDescription = getAccDescription; + public setAccDescription = setAccDescription; +} diff --git a/packages/mermaid/src/diagrams/packet/diagram.ts b/packages/mermaid/src/diagrams/packet/diagram.ts index a73a77c05..84a7bca8f 100644 --- a/packages/mermaid/src/diagrams/packet/diagram.ts +++ b/packages/mermaid/src/diagrams/packet/diagram.ts @@ -1,12 +1,14 @@ import type { DiagramDefinition } from '../../diagram-api/types.js'; -import { db } from './db.js'; +import { PacketDB } from './db.js'; import { parser } from './parser.js'; import { renderer } from './renderer.js'; import { styles } from './styles.js'; export const diagram: DiagramDefinition = { parser, - db, + get db() { + return new PacketDB(); + }, renderer, styles, }; diff --git a/packages/mermaid/src/diagrams/packet/packet.spec.ts b/packages/mermaid/src/diagrams/packet/packet.spec.ts index b03ffe4d1..fd7b3211a 100644 --- a/packages/mermaid/src/diagrams/packet/packet.spec.ts +++ b/packages/mermaid/src/diagrams/packet/packet.spec.ts @@ -1,24 +1,26 @@ import { it, describe, expect } from 'vitest'; -import { db } from './db.js'; +import { PacketDB } from './db.js'; import { parser } from './parser.js'; -const { clear, getPacket, getDiagramTitle, getAccTitle, getAccDescription } = db; - describe('packet diagrams', () => { + let db: PacketDB; beforeEach(() => { - clear(); + db = new PacketDB(); + if (parser.parser) { + parser.parser.yy = db; + } }); it('should handle a packet-beta definition', async () => { const str = `packet-beta`; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot('[]'); + expect(db.getPacket()).toMatchInlineSnapshot('[]'); }); it('should handle a packet definition', async () => { const str = `packet`; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot('[]'); + expect(db.getPacket()).toMatchInlineSnapshot('[]'); }); it('should handle diagram with data and title', async () => { @@ -29,10 +31,10 @@ describe('packet diagrams', () => { 0-10: "test" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getDiagramTitle()).toMatchInlineSnapshot('"Packet diagram"'); - expect(getAccTitle()).toMatchInlineSnapshot('"Packet accTitle"'); - expect(getAccDescription()).toMatchInlineSnapshot('"Packet accDescription"'); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getDiagramTitle()).toMatchInlineSnapshot('"Packet diagram"'); + expect(db.getAccTitle()).toMatchInlineSnapshot('"Packet accTitle"'); + expect(db.getAccDescription()).toMatchInlineSnapshot('"Packet accDescription"'); + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { @@ -52,7 +54,7 @@ describe('packet diagrams', () => { 11: "single" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { @@ -78,7 +80,7 @@ describe('packet diagrams', () => { +16: "word" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { @@ -104,7 +106,7 @@ describe('packet diagrams', () => { +16: "word" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { @@ -130,7 +132,7 @@ describe('packet diagrams', () => { 11-90: "multiple" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { @@ -172,7 +174,7 @@ describe('packet diagrams', () => { 17-63: "multiple" `; await expect(parser.parse(str)).resolves.not.toThrow(); - expect(getPacket()).toMatchInlineSnapshot(` + expect(db.getPacket()).toMatchInlineSnapshot(` [ [ { diff --git a/packages/mermaid/src/diagrams/packet/parser.ts b/packages/mermaid/src/diagrams/packet/parser.ts index 1dcccd636..689d86fb3 100644 --- a/packages/mermaid/src/diagrams/packet/parser.ts +++ b/packages/mermaid/src/diagrams/packet/parser.ts @@ -3,12 +3,12 @@ import { parse } from '@mermaid-js/parser'; import type { ParserDefinition } from '../../diagram-api/types.js'; import { log } from '../../logger.js'; import { populateCommonDb } from '../common/populateCommonDb.js'; -import { db } from './db.js'; +import { PacketDB } from './db.js'; import type { PacketBlock, PacketWord } from './types.js'; const maxPacketSize = 10_000; -const populate = (ast: Packet) => { +const populate = (ast: Packet, db: PacketDB) => { populateCommonDb(ast, db); let lastBit = -1; let word: PacketWord = []; @@ -91,9 +91,17 @@ const getNextFittingBlock = ( }; export const parser: ParserDefinition = { + // @ts-expect-error - PacketDB is not assignable to DiagramDB + parser: { yy: undefined }, parse: async (input: string): Promise => { const ast: Packet = await parse('packet', input); + const db = parser.parser?.yy; + if (!(db instanceof PacketDB)) { + throw new Error( + 'parser.parser?.yy was not a PacketDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.' + ); + } log.debug(ast); - populate(ast); + populate(ast, db); }, };