/** * The adaptation pack settings in packages/patcher/tuning/patch-pack.jsonc. */ import { readFileSync } from 'node:fs'; import { parseJsonc } from '@wto/jsonc'; import { PatchPackError } from './patch-pack-error.ts'; export interface PatchPackTuning { /** `owner/repo` on GitHub. */ readonly repository: string; readonly releasesPerCheck: number; readonly maxPackBytes: number; readonly maxUnpackedBytes: number; readonly requestTimeoutMs: number; } const TUNING_FILE = new URL('../../tuning/patch-pack.jsonc', import.meta.url); let cached: PatchPackTuning | null = null; export function patchPackTuning(): PatchPackTuning { if (cached) return cached; const value = parseJsonc(readFileSync(TUNING_FILE, 'utf8')); if (typeof value !== 'object' || value === null || Array.isArray(value)) throw tuningError('the file is not an object'); const fields = value as Record; const repository = fields['repository']; if (typeof repository !== 'string' || !/^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/.test(repository)) { throw tuningError('"repository" must be "owner/repo"'); } cached = { repository, releasesPerCheck: positiveInteger(fields, 'releases_per_check'), maxPackBytes: positiveInteger(fields, 'max_pack_bytes'), maxUnpackedBytes: positiveInteger(fields, 'max_unpacked_bytes'), requestTimeoutMs: positiveInteger(fields, 'request_timeout_ms'), }; return cached; } function positiveInteger(fields: Record, key: string): number { const value = fields[key]; if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) throw tuningError(`"${key}" must be a positive integer`); return value; } function tuningError(detail: string): PatchPackError { return new PatchPackError('tuning', `tuning/patch-pack.jsonc: ${detail}`); }