All files / global/helpers diagramLink.ts

97.05% Statements 33/34
90% Branches 27/30
100% Functions 6/6
100% Lines 27/27

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 66 67 68 69 70 71 72 73 74 75 76 77 78                                          20x   20x 20x     65x     65x 65x       20x 1269x   2191x           20x 163x 158x                 146x 146x 142x 142x 142x 138x 138x 138x 138x 134x             134x 85x 85x 73x 73x    
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
// Путь файла диаграммы и ссылка на документ-просмотрщик. Правила пути — здесь, одни для ссылки
// (lib, агент) и для разрешения адреса (platform/paths): путь от каталога корневого манифеста, сегменты
// [A-Za-z0-9._-]+ через «/», сегмент не начинается с точки (нет «.», «..», .git, .env), файл — *.diagram.json.
// Всё остальное — ADRD_PATH_INVALID.
 
export const ADRD_PATH_INVALID = 'ADRD_PATH_INVALID';
 
const SEGMENT = /^[A-Za-z0-9_-][A-Za-z0-9._-]*$/;
const DIAGRAM_SUFFIX = '.diagram.json';
 
export class PathError extends Error {
  readonly code = ADRD_PATH_INVALID;
 
  constructor(readonly path: unknown) {
    super(`${ADRD_PATH_INVALID}: ${String(path)}`);
    this.name = 'PathError';
  }
}
 
export const isValidDiagramPath = (path: unknown): path is string =>
  typeof path === 'string'
  && path.endsWith(DIAGRAM_SUFFIX)
  && path.split('/').every((segment) => SEGMENT.test(segment));
 
// Ссылка на документ-просмотрщик: #/entities/docs/blank?dh-doc-id=<viewerDoc>&source=<путь>; недопустимый путь —
// PathError. Не #/docs/<viewerDoc>?source=…: маршрут /docs/:document ядра (Architecture/Document.vue) передаёт
// документу только params { 'dh-doc-id': … } и query source теряет. Маршрут сущности docs с представлением blank
// (Entities/Entity.vue: params || $route.query) передаёт документу все параметры запроса, в том числе source
export const diagramLink = (viewerDoc: string, path: unknown): string => {
  if (!isValidDiagramPath(path)) throw new PathError(path);
  return `#/entities/docs/blank?dh-doc-id=${encodeURIComponent(viewerDoc)}&source=${encodeURIComponent(path)}`;
};
 
export interface DiagramTarget {
  viewerDoc: string;
  source: string;
}
 
export function parseDiagramLink(href: string): DiagramTarget | null {
  const prefix = '#/entities/docs/blank?';
  if (!href.startsWith(prefix)) return null;
  const query = new URLSearchParams(href.slice(prefix.length));
  const keys = Array.from(query.keys());
  if (keys.length !== 2 || query.getAll('dh-doc-id').length !== 1 || query.getAll('source').length !== 1) return null;
  const viewerDoc = query.get('dh-doc-id') || '';
  const source = query.get('source') || '';
  Iif (!/^[A-Za-z0-9._-]+$/.test(viewerDoc)) return null;
  if (!isValidDiagramPath(source)) return null;
  return { viewerDoc, source };
}
 
 
export type SavedDiagram = { path: string; link: string };
 
export function parseSavedDiagram(value: unknown): SavedDiagram | null {
  if (!value || typeof value !== 'object') return null;
  const result = value as Record<string, unknown>;
  if (typeof result.link !== 'string' || !isValidDiagramPath(result.path)) return null;
  const target = parseDiagramLink(result.link);
  return target?.source === result.path ? { path: result.path, link: diagramLink(target.viewerDoc, target.source) } : null;
}