All files workflowSerialization.js

100% Statements 65/65
64.7% Branches 22/34
100% Functions 4/4
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661x 1x 1x 4x 10x 10x 10x 4x 4x 5x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 5x 4x 4x 5x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x  
import { sanitizeRuntimeValuesForPersistence } from './runtimeValuePersistence.js';
 
export function serializeWorkflowState(nodes, edges) {
  const compactObject = (value) => {
    if (!value || typeof value !== 'object') return null;
    const entries = Object.entries(value);
    return entries.length > 0 ? Object.fromEntries(entries) : null;
  };
  const getExtraData = (data) => compactObject(Object.fromEntries(
    Object.entries(data || {}).filter(([key]) => ![
      'label',
      'className',
      'widgetValues',
      'runtimeValues',
      'definition',
      'previewImage',
      'tableRows',
      'meshData',
      'overlay',
      'scalarValue',
      'processingTimeMs',
      'warning',
    ].includes(key))
  ));
  const getRuntimeValues = (node) => compactObject(
    sanitizeRuntimeValuesForPersistence(node.data?.className, node.data?.runtimeValues),
  );
 
  return {
    version: 1,
    nodes: nodes.map((node) => {
      const runtimeValues = getRuntimeValues(node);
      return {
        id: node.id,
        type: node.type || 'custom',
        position: node.position,
        ...(node.className ? { className: node.className } : {}),
        ...(node.parentId ? { parentId: node.parentId } : {}),
        ...(node.extent ? { extent: node.extent } : {}),
        ...(node.hidden ? { hidden: true } : {}),
        ...(node.style ? { style: node.style } : {}),
        dragHandle: node.dragHandle || '.drag-handle',
        data: {
          label: node.data?.label || node.data?.className || 'Node',
          className: node.data?.className || '',
          widgetValues: node.data?.widgetValues || {},
          ...(runtimeValues ? { runtimeValues } : {}),
          ...(getExtraData(node.data) ? { extraData: getExtraData(node.data) } : {}),
          output: node.data?.definition?.output || [],
          output_name: node.data?.definition?.output_name || [],
        },
      };
    }),
    edges: edges.map((edge) => ({
      id: edge.id,
      source: edge.source,
      sourceHandle: edge.sourceHandle,
      target: edge.target,
      targetHandle: edge.targetHandle,
      ...(edge.style ? { style: edge.style } : {}),
      ...(edge.hidden ? { hidden: true } : {}),
      ...(edge.data ? { data: edge.data } : {}),
    })),
  };
}