feat: fetch, кеш и lock с SHA-256 фактических байтов (BHL-021)

- bhl fetch/verify: lock.json (sha256 байтов, без LF-нормализации),
  несовпадение суммы прерывает использование артефакта
- missing-file/unexpected-file/missing-cache — явные диагностики
- офлайн validate зафиксированной версии после загрузки (тест)
- validate.ts: Ajv strict-отказ и битый JSON — диагностики вместо crash
- пакет: 36/36 тестов
This commit is contained in:
Suslov-MD
2026-11-29 01:21:02 +03:00
parent 3cb069422f
commit 64f6f3ccb3
5 changed files with 565 additions and 21 deletions
+121
View File
@@ -1,6 +1,7 @@
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
import { fetchSpec, verifyCache } from './fetch.ts';
import { renderSpec } from './render.ts';
import { resolveProfiles } from './resolve.ts';
import { specList, specShow, validateManifest } from './spec.ts';
@@ -15,6 +16,8 @@ const USAGE_MESSAGE =
'profile resolve <spec-root> [--profile <id>] [--preset <id>]; ' +
'report validate <report.json> [--spec-root <path>]; ' +
'manifest validate <manifest.yaml> [--spec-root <path>]; ' +
'fetch <release-dir> --version <semver> --cache <dir>; ' +
'verify --version <semver> --cache <dir>; ' +
'JSON envelope on stdout; exit codes: 0 ok, 1 validation failed, 2 usage, ' +
'3 not implemented, 4 internal.';
@@ -319,6 +322,118 @@ function runSpec(command: string, rest: readonly string[]): number {
return emitSpecShow(parsed);
}
interface FetchFlags {
version?: string;
cache?: string;
positional: string[];
}
const FETCH_FLAGS: Readonly<Record<string, 'version' | 'cache'>> = {
'--version': 'version',
'--cache': 'cache',
};
function takeFetchFlag(
args: readonly string[],
cursor: number,
target: FetchFlags,
arg: string,
): { next: number } | { error: string } {
const flag = FETCH_FLAGS[arg];
const value = args[cursor + 1];
if (flag === undefined || value === undefined) {
return { error: `missing value for ${arg}` };
}
target[flag] = value;
return { next: cursor + 2 };
}
function parseFetchArgs(args: readonly string[]): FetchFlags | { error: string } {
const target: FetchFlags = { positional: [] };
let cursor = 0;
while (cursor < args.length) {
const arg = args[cursor] ?? '';
if (FETCH_FLAGS[arg] !== undefined) {
const taken = takeFetchFlag(args, cursor, target, arg);
if ('error' in taken) {
return taken;
}
cursor = taken.next;
continue;
}
if (arg.startsWith('--')) {
return { error: `unknown flag ${arg}` };
}
target.positional.push(arg);
cursor += 1;
}
return target;
}
function fetchUsage(parsed: FetchFlags): string {
if (parsed.positional.length !== 1) {
return 'fetch requires exactly one <release-dir>';
}
if (parsed.version === undefined) {
return 'fetch requires --version <semver>';
}
if (parsed.cache === undefined) {
return 'fetch requires --cache <dir>';
}
return '';
}
function runFetch(command: string, rest: readonly string[]): number {
const parsed = parseFetchArgs(rest);
if ('error' in parsed) {
emit(failEnvelope(command, 'USAGE', parsed.error));
return ExitCode.Usage;
}
const usageError = fetchUsage(parsed);
if (usageError !== '') {
emit(failEnvelope(command, 'USAGE', usageError));
return ExitCode.Usage;
}
const source = parsed.positional[0] ?? '';
const version = parsed.version ?? '';
const cache = parsed.cache ?? '';
const result = fetchSpec(source, cache, version);
const data = { source, version, cache, files: result.files, errors: result.diagnostics };
if (!result.ok) {
return emitValidationFailure(command, data, result.diagnostics.length);
}
emit(okEnvelope(command, data));
return ExitCode.Ok;
}
function runVerify(command: string, rest: readonly string[]): number {
const parsed = parseFetchArgs(rest);
if ('error' in parsed) {
emit(failEnvelope(command, 'USAGE', parsed.error));
return ExitCode.Usage;
}
if (
parsed.version === undefined ||
parsed.cache === undefined ||
parsed.positional.length !== 0
) {
emit(failEnvelope(command, 'USAGE', 'verify requires --version <semver> --cache <dir>'));
return ExitCode.Usage;
}
const result = verifyCache(parsed.cache, parsed.version);
const data = {
version: parsed.version,
cache: parsed.cache,
files: result.files,
errors: result.diagnostics,
};
if (!result.ok) {
return emitValidationFailure(command, data, result.diagnostics.length);
}
emit(okEnvelope(command, data));
return ExitCode.Ok;
}
function runManifest(command: string, rest: readonly string[]): number {
const [sub, ...args] = rest;
if (sub !== 'validate') {
@@ -432,6 +547,12 @@ export function run(argv: readonly string[]): number {
if (command === 'report') {
return runReport(command, rest);
}
if (command === 'fetch') {
return runFetch(command, rest);
}
if (command === 'verify') {
return runVerify(command, rest);
}
if (command === 'audit') {
emit(failEnvelope(command, 'NOT_IMPLEMENTED', `${command} is not implemented yet`));
return ExitCode.NotImplemented;