signal-desktop/ts/util/hasExpired.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2021-08-23 22:45:11 +00:00
import { Environment, getEnvironment } from '../environment';
import { isInPast } from './timestamp';
import * as log from '../logging/log';
import * as Errors from '../types/errors';
2021-08-23 22:45:11 +00:00
const ONE_DAY_MS = 86400 * 1000;
const NINETY_ONE_DAYS = 91 * ONE_DAY_MS;
const THIRTY_ONE_DAYS = 31 * ONE_DAY_MS;
export function hasExpired(): boolean {
let buildExpiration = 0;
try {
buildExpiration = window.getExpiration();
if (buildExpiration) {
log.info('Build expires: ', new Date(buildExpiration).toISOString());
}
} catch (e) {
log.error('Error retrieving build expiration date', Errors.toLogFormat(e));
return true;
}
2021-08-23 22:45:11 +00:00
if (getEnvironment() === Environment.Production) {
const safeExpirationMs = window.Events.getAutoDownloadUpdate()
? NINETY_ONE_DAYS
: THIRTY_ONE_DAYS;
2021-08-23 22:45:11 +00:00
const buildExpirationDuration = buildExpiration - Date.now();
const tooFarIntoFuture = buildExpirationDuration > safeExpirationMs;
if (tooFarIntoFuture) {
log.error(
'Build expiration is set too far into the future',
buildExpiration
);
}
2021-08-23 22:45:11 +00:00
return tooFarIntoFuture || isInPast(buildExpiration);
}
2021-08-23 22:45:11 +00:00
return buildExpiration !== 0 && isInPast(buildExpiration);
}