import { DateTime } from 'luxon'; import { logger } from '../../../logger/index.ts'; import { regEx } from '../../../util/regex.ts'; import type { DistroInfo } from '../distro.ts'; import type { DistroInfoRecordWithVersion } from '../types.ts'; const refreshInterval = { days: 1 }; const datedRegex = regEx( /^(?\w+)-(?\d{8})(?\.\d{1,2})?$/, ); export function isDatedCodeName( input: string, distroInfo: DistroInfo, ): boolean { const match = datedRegex.exec(input); if (!match?.groups) { return false; } const codename = match.groups.codename; return distroInfo.isCodename(codename); } export function getDatedContainerImageCodename(version: string): null | string { const match = datedRegex.exec(version); if (!match?.groups) { return null; } return match.groups.codename; } export function getDatedContainerImageVersion(version: string): null | number { const match = datedRegex.exec(version); if (!match?.groups) { return null; } return parseInt(match.groups.date, 10); } export function getDatedContainerImageSuffix(version: string): null | string { const match = datedRegex.exec(version); if (!match?.groups?.suffix) { return null; } return match.groups.suffix; } export class RollingReleasesData { private ltsToVer = new Map(); private verToLts = new Map(); private timestamp = DateTime.fromMillis(0).toUTC(); // start of epoch private distroInfo: DistroInfo; constructor(distroInfo: DistroInfo) { this.distroInfo = distroInfo; } getVersionByLts(input: string): string { this.build(); const schedule = this.ltsToVer.get(input); if (schedule) { return schedule.version; } return input; } getLtsByVersion(input: string): string { this.build(); const di = this.verToLts.get(input); if (di) { return di.series; } return input; } has(version: string): boolean { this.build(); return this.ltsToVer.has(version); } schedule(version: string): DistroInfoRecordWithVersion | undefined { this.build(); let schedule: DistroInfoRecordWithVersion | undefined = undefined; if (this.verToLts.has(version)) { schedule = this.verToLts.get(version); } if (this.ltsToVer.has(version)) { schedule = this.ltsToVer.get(version); } return schedule; } private build(): void { const now = DateTime.now().toUTC(); if (now < this.timestamp.plus(refreshInterval)) { return; } logger.debug('RollingReleasesData - data written'); this.timestamp = now; for (let i = 0; i < 3; i++) { const di = this.distroInfo.getNLatest(i); // should never happen, just for type checking if (!di) { return; } let prefix = ''; for (let j = 0; j < i; j++) { prefix += 'old'; } di.series = `${prefix}stable`; this.ltsToVer.set(di.series, di); this.verToLts.set(di.version, di); } } }