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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 63x 31x 1x 30x 30x 1x 29x 1x 28x 1x 27x 27x 29x 29x 2x 29x 1x 29x 1x 29x 3x 3x 5x 29x 1x 29x 1x 27x 21x 27x 17x 89x 89x 89x 89x 16x 73x 27x 2x 27x 46x 4x 4x 42x 22x 3x 19x 4x 42x 42x 19x 42x 12x 42x 42x 21x 21x 2x 2x 19x 19x 5x 19x 5x 19x 8x 19x 21x 24x 17x 68x 68x 21x 10x 11x 68x 3x 1x 2x 1x 1x 1x 4x 4x 2x 4x 2x 1x | /*
Copyright (C) 2025 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 {DEFAULT_ALIAS_FOR_COMMON_MANIFEST, DEFAULT_PERMISSION_FOR_COMMON_MANIFEST} from '@back/cluster/constants.mjs';
import {getLoggerWithTag} from '@global/logger/v2/logger.mjs';
const logger = getLoggerWithTag('rootManifestVersionCheck');
export function validateRootManifest(rootJson) {
if (!rootJson) {
throw Error('root manifest object does not exist (undefined or null)');
}
let imports = rootJson.imports;
if (!imports) {
throw Error('root manifest does not contains \'imports\' directive');
}
if(!Array.isArray(imports)) {
throw Error('root manifest \'imports\' directive is not array, but must be');
}
if(typeof imports[0] !== 'object') {
throw Error('root manifest \'imports\' directive is not an object, but must be object with params \'root\' and \'permission\'.');
}
let errors = [];
for(let imp in imports) {
const importForCheck = imports[imp];
if (!importForCheck.root) {
errors.push(`Error in root manifest: imports[${imp}] must have 'root' property, which direct on domain root manifest`);
}
if (!importForCheck.permission) {
errors.push(`Error in root manifest: imports[${imp}] must have 'permission' property, which direct access permission for data`);
}
if (!importForCheck.alias) {
errors.push(`Error in root manifest: imports[${imp}] must have 'alias' property, which direct alias for reload data (used for reload request)`);
}
if (importForCheck.repos) {
const repoErrors = checkRepos(importForCheck.repos);
for (const error of repoErrors) {
errors.push(`Error in root manifest: imports[${imp}] have error in repos array: ${error}`);
}
}
if(imports[imp].permission === DEFAULT_PERMISSION_FOR_COMMON_MANIFEST) {
errors.push(`Error in root manifest: imports[${imp}] has 'permission' property set with reserved word ${DEFAULT_PERMISSION_FOR_COMMON_MANIFEST}`);
}
if(imports[imp].alias === DEFAULT_ALIAS_FOR_COMMON_MANIFEST) {
errors.push(`Error in root manifest: imports[${imp}] has 'alias' property set with reserved word ${DEFAULT_ALIAS_FOR_COMMON_MANIFEST}`);
}
}
if (rootJson.common) {
errors.push(...checkCommon(rootJson.common));
}
if (errors.length > 0) {
throw Error(errors.join('; '));
}
}
/**
* Разбор common - единственное место, где описана его допустимая форма: и checkCommon, и
* normalizeCommon работают с результатом отсюда, своих проверок формы у них нет.
*
* @param common - значение common из root-манифеста в любой из допустимых форм
* @returns {{roots: string[], packages: object[]|undefined, problems: {message: string, fatal: boolean}[]}}
* roots пуст - общего манифеста нет. problems с fatal делают root-манифест невалидным, остальные
* лишь сообщают, что часть объявления не даёт источника
*/
function parseCommon(common) {
const problems = [];
const fail = (message) => problems.push({message, fatal: true});
const note = (message) => problems.push({message, fatal: false});
// Общего манифеста нет вовсе - штатная конфигурация, сообщать не о чем
if (common === undefined || common === null) {
return {roots: [], packages: undefined, problems};
}
if (typeof common === 'string') {
if (!common) {
note('\'common\' is an empty string, treat it as no common manifest');
}
return {roots: common ? [common] : [], packages: undefined, problems};
}
if (typeof common !== 'object' || Array.isArray(common)) {
fail('\'common\' must be a string (URI) or an object with \'root\'/\'packages\' properties');
return {roots: [], packages: undefined, problems};
}
if (common.root !== undefined) {
if (typeof common.root !== 'string') {
fail('\'common.root\' must be a string');
} else if (!common.root) {
note('\'common.root\' is empty, it gives no source and is dropped');
}
}
const packages = parsePackages(common.packages, fail);
// Порядок задаёт приоритет мёржа: root первым, дальше пакеты по порядку объявления
const roots = [
...(common.root ? [common.root] : []),
...(packages || []).map((pkg) => pkg?.root).filter(Boolean)
];
if (roots.length === 0) {
note('\'common\' is declared, but gives no root at all, treat it as no common manifest');
}
return {roots, packages, problems};
}
/**
* Проверяет объявления пакетов.
*
* Версия обязана быть строкой, то есть в YAML записанной в кавычках. Незакавыченную версию из двух
* частей YAML отдаёт числом, а `1.10` как число неотличимо от `1.1` - и это разные требования
* semver: диапазон `1.1` не покрывает `1.10.0`, а `1.10` не покрывает `1.1.0`. Приводить число к
* строке нельзя: к моменту приведения `1.10` уже потеряна
*/
function parsePackages(packages, fail) {
if (packages === undefined) {
return undefined;
}
if (!Array.isArray(packages)) {
fail('\'common.packages\' must be an array');
return undefined;
}
packages.forEach((pkg, i) => {
if (!pkg || typeof pkg.root !== 'string' || !pkg.root) {
fail(`common.packages[${i}] must have a non-empty 'root' property (string)`);
}
if (!pkg || typeof pkg.id !== 'string' || !pkg.id) {
fail(`common.packages[${i}] must have a non-empty 'id' property (string)`);
}
if (!pkg || typeof pkg.version !== 'string' || !pkg.version) {
fail(`common.packages[${i}] must have a non-empty 'version' property (quoted string, e.g. '1.11.0')`);
}
});
return packages;
}
/**
* common - строка (URI) либо объект {root?, packages?}.
* Проверяются только типы: пустой common - легальная запись, означающая
* отсутствие общего манифеста (см. normalizeCommon)
* @returns {string[]} список ошибок, пустой массив если всё корректно
*/
function checkCommon(common) {
return parseCommon(common).problems
.filter((problem) => problem.fatal)
.map((problem) => `Error in root manifest: ${problem.message}`);
}
/**
* Единственное место, где решается, есть ли у root-манифеста общий манифест и из чего он собирается.
* Вырожденные записи (нет поля, пустая строка, {}, {packages: []}, {root: ''}) равнозначны отсутствию common
* @param common - значение common из root-манифеста в любой из допустимых форм
* @returns {{roots: string[], packages: object[]|undefined}} roots пуст - общего манифеста нет
*/
export function normalizeCommon(common) {
const {roots, packages, problems} = parseCommon(common);
for (const problem of problems) {
// fatal сюда доходит только из manifest-reload.controller.mjs: в потоке загрузчика такой
// root-манифест отвергается раньше, на validateRootManifest
if (problem.fatal) {
logger.warn(() => `root manifest: ${problem.message}`);
} else {
logger.info(() => `root manifest: ${problem.message}`);
}
}
return {roots, packages};
}
/**
* проверка, что repos это массив, что его элементы имеют обязательные атрибуты type и url с типом string
* @param repos
* @returns {string[]|*[]}
*/
function checkRepos(repos) {
if (!Array.isArray(repos)) {
return ['repos must be array of object ({type: string, url: string})'];
}
if (repos.length === 0) {
return [];
}
const errors = [];
for (let i = 0; i < repos.length; i++) {
const repo = repos[i];
if (!repo.type || typeof repo.type !== 'string') {
errors.push(`repo [${i}] have incorrect 'type' value (not exist or not string)`);
}
if (!repo.url || typeof repo.url !== 'string') {
errors.push(`repo [${i}] have incorrect 'url' value (not exist or not string)`);
}
}
return errors;
}
|