mirror of
https://github.com/goauthentik/authentik
synced 2026-04-27 09:57:31 +02:00
* ensure we never throw errors in the browser Signed-off-by: Jens Langhammer <jens@goauthentik.io> * cleaner Signed-off-by: Jens Langhammer <jens@goauthentik.io> * rework Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix misleading variable Signed-off-by: Jens Langhammer <jens@goauthentik.io> * Tidy behavior. --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io> Co-authored-by: Teffen Ellis <teffen@goauthentik.io>
31 lines
930 B
JavaScript
31 lines
930 B
JavaScript
/**
|
|
* @file Version Utilities
|
|
*/
|
|
|
|
/**
|
|
* Default support duration: 2 years in milliseconds.
|
|
*/
|
|
export const DEFAULT_VERSION_SUPPORT_DURATION = 2 * 365 * 24 * 60 * 60 * 1000;
|
|
|
|
/**
|
|
* A validation error indicating the version is invalid.
|
|
*/
|
|
export class VersionValidationError extends Error {}
|
|
|
|
/**
|
|
* @throws {Error} if the version is older than 2 years
|
|
*
|
|
* @param {import("semver").SemVer} parsed Parsed semver version
|
|
* @param {number} [supportDuration] Milliseconds cutoff
|
|
*/
|
|
export function assertVersionSupported(parsed, supportDuration = DEFAULT_VERSION_SUPPORT_DURATION) {
|
|
const versionDate = new Date(parsed.major, parsed.minor - 1);
|
|
const now = new Date();
|
|
|
|
if (now.getTime() - versionDate.getTime() >= supportDuration) {
|
|
const message = `Semver version ${versionDate.getFullYear()}.${versionDate.getMonth()} is older than 2 years.`;
|
|
|
|
throw new VersionValidationError(message);
|
|
}
|
|
}
|