All files / src/lib/services version-service.ts

94.12% Statements 32/34
85.71% Branches 6/7
87.5% Functions 7/8
93.94% Lines 31/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 13261x     61x     61x                                     61x                                                           150x 150x 150x       150x 150x 150x 150x       149x 149x 149x       149x       149x 149x 57x   92x         152x 4x 4x               2x 2x 2x       2x             2x           7x                 124x 124x       61x  
import fetch from 'make-fetch-happen';
import { IUnleashStores } from '../types/stores';
import { IUnleashConfig } from '../types/option';
import version from '../util/version';
import { Logger } from '../logger';
import { ISettingStore } from '../types/stores/settings-store';
import { hoursToMilliseconds } from 'date-fns';
 
export interface IVersionInfo {
    oss: string;
    enterprise?: string;
}
 
export interface IVersionHolder {
    current: IVersionInfo;
    latest: IVersionInfo | {};
    isLatest: boolean;
    instanceId: string;
}
 
export interface IVersionResponse {
    versions: IVersionInfo;
    latest: boolean;
}
 
export default class VersionService {
    private logger: Logger;
 
    private settingStore: ISettingStore;
 
    private current: IVersionInfo;
 
    private latest?: IVersionInfo;
 
    private enabled: boolean;
 
    private versionCheckUrl: string;
 
    private instanceId?: string;
 
    private isLatest: boolean;
 
    private timer: NodeJS.Timeout;
 
    constructor(
        { settingStore }: Pick<IUnleashStores, 'settingStore'>,
        {
            getLogger,
            versionCheck,
            enterpriseVersion,
        }: Pick<
            IUnleashConfig,
            'getLogger' | 'versionCheck' | 'enterpriseVersion'
        >,
    ) {
        this.logger = getLogger('lib/services/version-service.js');
        this.settingStore = settingStore;
        this.current = {
            oss: version,
            enterprise: enterpriseVersion || '',
        };
        this.enabled = versionCheck.enable;
        this.versionCheckUrl = versionCheck.url;
        this.isLatest = true;
        process.nextTick(() => this.setup());
    }
 
    async setup(): Promise<void> {
        await this.setInstanceId();
        await this.checkLatestVersion();
        this.timer = setInterval(
            async () => this.checkLatestVersion(),
            hoursToMilliseconds(48),
        );
        this.timer.unref();
    }
 
    async setInstanceId(): Promise<void> {
        try {
            const { id } = await this.settingStore.get('instanceInfo');
            this.instanceId = id;
        } catch (err) {
            this.logger.warn('Could not find instanceInfo');
        }
    }
 
    async checkLatestVersion(): Promise<void> {
        if (this.enabled) {
            try {
                const res = await fetch(this.versionCheckUrl, {
                    method: 'POST',
                    body: JSON.stringify({
                        versions: this.current,
                        instanceId: this.instanceId,
                    }),
                    headers: { 'Content-Type': 'application/json' },
                });
                if (res.ok) {
                    const data = (await res.json()) as IVersionResponse;
                    this.latest = {
                        oss: data.versions.oss,
                        enterprise: data.versions.enterprise,
                    };
                    this.isLatest = data.latest;
                } else E{
                    this.logger.info(
                        `Could not check newest version. Status: ${res.status}`,
                    );
                }
            } catch (err) {
                this.logger.info('Could not check newest version', err);
            }
        }
    }
 
    getVersionInfo(): IVersionHolder {
        return {
            current: this.current,
            latest: this.latest || {},
            isLatest: this.isLatest,
            instanceId: this.instanceId,
        };
    }
 
    destroy(): void {
        clearInterval(this.timer);
        this.timer = null;
    }
}
 
module.exports = VersionService;