mermaid
Version:
Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graphs and more.
8 lines (7 loc) • 2.34 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/diagrams/common/sanitizeMetadata.ts"],
"sourcesContent": ["/**\n * Removes prototype-shaped own keys at the parser boundary before metadata is exposed or merged.\n *\n * Rebuilding objects also prevents a downstream consumer from accidentally treating an authored\n * `__proto__` property as merge instructions. Values otherwise remain opaque to diagram adapters.\n */\nexport function stripPrototypeKeys<T>(value: T): T {\n const copies = new WeakMap<object, unknown>();\n\n /**\n * Copies metadata without prototype-shaped keys, reusing copies for aliases and cycles.\n * Registers each container before visiting its children so self-references terminate.\n */\n function sanitize(value: unknown): unknown {\n if (value === null || typeof value !== 'object') {\n return value;\n }\n const existing = copies.get(value);\n if (existing !== undefined) {\n return existing;\n }\n if (Array.isArray(value)) {\n const clean = new Array<unknown>(value.length);\n copies.set(value, clean);\n value.forEach((entry, index) => {\n clean[index] = sanitize(entry);\n });\n return clean;\n }\n const clean: Record<string, unknown> = {};\n copies.set(value, clean);\n for (const key of Object.keys(value)) {\n if (key === '__proto__' || key === 'constructor' || key === 'prototype') {\n continue;\n }\n clean[key] = sanitize((value as Record<string, unknown>)[key]);\n }\n return clean;\n }\n\n return sanitize(value) as T;\n}\n"],
"mappings": "yCAMO,SAASA,EAAsBC,EAAa,CACjD,IAAMC,EAAS,IAAI,QAMnB,SAASC,EAASF,EAAyB,CACzC,GAAIA,IAAU,MAAQ,OAAOA,GAAU,SACrC,OAAOA,EAET,IAAMG,EAAWF,EAAO,IAAID,CAAK,EACjC,GAAIG,IAAa,OACf,OAAOA,EAET,GAAI,MAAM,QAAQH,CAAK,EAAG,CACxB,IAAMI,EAAQ,IAAI,MAAeJ,EAAM,MAAM,EAC7C,OAAAC,EAAO,IAAID,EAAOI,CAAK,EACvBJ,EAAM,QAAQ,CAACK,EAAOC,IAAU,CAC9BF,EAAME,CAAK,EAAIJ,EAASG,CAAK,CAC/B,CAAC,EACMD,CACT,CACA,IAAMA,EAAiC,CAAC,EACxCH,EAAO,IAAID,EAAOI,CAAK,EACvB,QAAWG,KAAO,OAAO,KAAKP,CAAK,EAC7BO,IAAQ,aAAeA,IAAQ,eAAiBA,IAAQ,cAG5DH,EAAMG,CAAG,EAAIL,EAAUF,EAAkCO,CAAG,CAAC,GAE/D,OAAOH,CACT,CAzBS,OAAAI,EAAAN,EAAA,YA2BFA,EAASF,CAAK,CACvB,CAnCgBQ,EAAAT,EAAA",
"names": ["stripPrototypeKeys", "value", "copies", "sanitize", "existing", "clean", "entry", "index", "key", "__name"]
}