All files / frontend/plugins parse-plugin-metadata.ts

100% Statements 31/31
100% Branches 40/40
100% Functions 7/7
100% Lines 31/31

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                          101x 393x     101x 878x     101x 145x       101x 150x                     101x     128x                     101x     21x         9x     12x     101x         105x 5x     100x 7x         93x 3x     90x   90x 128x 24x     23x     104x 8x       7x     96x     88x    
/*
  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.
*/
 
import type {
  CatalogPluginMetadata,
  LegacyPluginMetadata
} from './plugin-manager';
 
const isRecord = (value: unknown): value is Record<string, unknown> => {
  return typeof value === 'object' && value !== null && !Array.isArray(value);
};
 
const isNonEmptyString = (value: unknown): value is string => {
  return typeof value === 'string' && value.trim().length > 0;
};
 
const isDocumentType = (value: unknown): value is string => {
  return isNonEmptyString(value)
    && /^[a-z0-9][a-z0-9-]*$/.test(value);
};
 
const isPluginMetadata = (value: unknown): value is LegacyPluginMetadata => {
  return isRecord(value)
    && isNonEmptyString(value.id)
    && Array.isArray(value.documents)
    && value.documents.every(isNonEmptyString)
    && isNonEmptyString(value.entry)
    && Array.isArray(value.scripts)
    && value.scripts.every(isNonEmptyString)
    && Array.isArray(value.styles)
    && value.styles.every(isNonEmptyString);
};
 
const isCatalogPluginMetadata = (
  value: unknown
): value is CatalogPluginMetadata => {
  return isPluginMetadata(value)
    && isRecord(value)
    && value.documents.length > 0
    && value.documents.every(isDocumentType)
    && new Set(value.documents).size === value.documents.length
    && isNonEmptyString(value.version)
    && typeof value.apiVersion === 'number'
    && Number.isInteger(value.apiVersion)
    && value.apiVersion > 0;
};
 
export const parsePluginMetadata = (
  value: unknown
): LegacyPluginMetadata[] => {
  if (
    !isRecord(value)
    || !Array.isArray(value.plugins)
    || !value.plugins.every(isPluginMetadata)
  ) {
    throw new Error('Некорректный формат metadata плагинов');
  }
 
  return value.plugins;
};
 
export const parsePluginCatalog = (
  value: unknown,
  supportedApiVersion: number,
  onWarning: (message: string) => void
): CatalogPluginMetadata[] => {
  if (!isRecord(value)) {
    throw new Error('Некорректный формат каталога плагинов');
  }
 
  if (value.schemaVersion !== 1) {
    throw new Error(
      `Неподдерживаемая версия каталога плагинов: ${String(value.schemaVersion)}`
    );
  }
 
  if (!Array.isArray(value.plugins)) {
    throw new Error('Некорректный список плагинов в каталоге');
  }
 
  const plugins: CatalogPluginMetadata[] = [];
 
  for (const [index, plugin] of value.plugins.entries()) {
    if (!isCatalogPluginMetadata(plugin)) {
      onWarning(
        `Пропущена запись каталога [${index}]: некорректное описание плагина`
      );
      continue;
    }
 
    if (plugin.apiVersion !== supportedApiVersion) {
      onWarning(
        `Плагин [${plugin.id}] пропущен: требуется API ${plugin.apiVersion}, `
        + `портал предоставляет API ${supportedApiVersion}`
      );
      continue;
    }
 
    plugins.push(plugin);
  }
 
  return plugins;
};