2022-04-28 10:57:52 +02:00
|
|
|
import fetch from 'make-fetch-happen';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashStores } from '../types/stores';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import version from '../util/version';
|
|
|
|
import { Logger } from '../logger';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { ISettingStore } from '../types/stores/settings-store';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { hoursToMilliseconds } from 'date-fns';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
|
|
|
export interface IVersionInfo {
|
|
|
|
oss: string;
|
|
|
|
enterprise?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IVersionHolder {
|
|
|
|
current: IVersionInfo;
|
2022-08-31 08:06:25 +02:00
|
|
|
latest: Partial<IVersionInfo>;
|
2021-04-22 10:07:10 +02:00
|
|
|
isLatest: boolean;
|
|
|
|
instanceId: string;
|
|
|
|
}
|
|
|
|
|
2022-01-23 21:26:28 +01:00
|
|
|
export interface IVersionResponse {
|
|
|
|
versions: IVersionInfo;
|
|
|
|
latest: boolean;
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class VersionService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private settingStore: ISettingStore;
|
2021-04-22 10:07:10 +02:00
|
|
|
|
|
|
|
private current: IVersionInfo;
|
|
|
|
|
|
|
|
private latest?: IVersionInfo;
|
|
|
|
|
|
|
|
private enabled: boolean;
|
|
|
|
|
|
|
|
private versionCheckUrl: string;
|
|
|
|
|
|
|
|
private instanceId?: string;
|
|
|
|
|
|
|
|
private isLatest: boolean;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
private timer: NodeJS.Timeout;
|
|
|
|
|
2021-02-19 11:13:25 +01:00
|
|
|
constructor(
|
2021-04-22 10:07:10 +02:00
|
|
|
{ settingStore }: Pick<IUnleashStores, 'settingStore'>,
|
|
|
|
{
|
|
|
|
getLogger,
|
|
|
|
versionCheck,
|
2021-04-23 11:05:25 +02:00
|
|
|
enterpriseVersion,
|
|
|
|
}: Pick<
|
2021-08-12 15:04:37 +02:00
|
|
|
IUnleashConfig,
|
|
|
|
'getLogger' | 'versionCheck' | 'enterpriseVersion'
|
2021-04-23 11:05:25 +02:00
|
|
|
>,
|
2021-02-19 11:13:25 +01:00
|
|
|
) {
|
|
|
|
this.logger = getLogger('lib/services/version-service.js');
|
|
|
|
this.settingStore = settingStore;
|
|
|
|
this.current = {
|
|
|
|
oss: version,
|
2021-04-23 11:05:25 +02:00
|
|
|
enterprise: enterpriseVersion || '',
|
2021-02-19 11:13:25 +01:00
|
|
|
};
|
2021-04-22 10:07:10 +02:00
|
|
|
this.enabled = versionCheck.enable;
|
|
|
|
this.versionCheckUrl = versionCheck.url;
|
|
|
|
this.isLatest = true;
|
2021-03-26 14:32:27 +01:00
|
|
|
process.nextTick(() => this.setup());
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async setup(): Promise<void> {
|
2021-03-26 14:32:27 +01:00
|
|
|
await this.setInstanceId();
|
2021-04-22 10:07:10 +02:00
|
|
|
await this.checkLatestVersion();
|
2021-05-28 11:10:24 +02:00
|
|
|
this.timer = setInterval(
|
|
|
|
async () => this.checkLatestVersion(),
|
2021-11-02 15:13:46 +01:00
|
|
|
hoursToMilliseconds(48),
|
2021-05-28 11:10:24 +02:00
|
|
|
);
|
|
|
|
this.timer.unref();
|
2021-02-19 11:13:25 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async setInstanceId(): Promise<void> {
|
2021-03-15 08:46:03 +01:00
|
|
|
try {
|
2021-02-19 11:13:25 +01:00
|
|
|
const { id } = await this.settingStore.get('instanceInfo');
|
2021-02-22 14:21:23 +01:00
|
|
|
this.instanceId = id;
|
2021-03-15 08:46:03 +01:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.warn('Could not find instanceInfo');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async checkLatestVersion(): Promise<void> {
|
2021-03-15 08:46:03 +01:00
|
|
|
if (this.enabled) {
|
2021-02-19 11:13:25 +01:00
|
|
|
try {
|
2021-03-26 14:32:27 +01:00
|
|
|
const res = await fetch(this.versionCheckUrl, {
|
2021-02-19 11:13:25 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
versions: this.current,
|
2021-03-26 14:32:27 +01:00
|
|
|
instanceId: this.instanceId,
|
2021-02-19 11:13:25 +01:00
|
|
|
}),
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2021-03-26 14:32:27 +01:00
|
|
|
});
|
|
|
|
if (res.ok) {
|
2022-01-23 21:26:28 +01:00
|
|
|
const data = (await res.json()) as IVersionResponse;
|
2021-03-26 14:32:27 +01:00
|
|
|
this.latest = {
|
|
|
|
oss: data.versions.oss,
|
|
|
|
enterprise: data.versions.enterprise,
|
|
|
|
};
|
|
|
|
this.isLatest = data.latest;
|
|
|
|
} else {
|
|
|
|
this.logger.info(
|
|
|
|
`Could not check newest version. Status: ${res.status}`,
|
|
|
|
);
|
|
|
|
}
|
2021-02-19 11:13:25 +01:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.info('Could not check newest version', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
getVersionInfo(): IVersionHolder {
|
2021-02-19 11:13:25 +01:00
|
|
|
return {
|
|
|
|
current: this.current,
|
|
|
|
latest: this.latest || {},
|
2021-02-19 11:16:35 +01:00
|
|
|
isLatest: this.isLatest,
|
2021-02-22 14:21:23 +01:00
|
|
|
instanceId: this.instanceId,
|
2021-02-19 11:13:25 +01:00
|
|
|
};
|
|
|
|
}
|
2021-05-28 11:10:24 +02:00
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
2021-02-19 11:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = VersionService;
|