"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RepoCacheBase = void 0;
const tslib_1 = require("tslib");
const util_1 = require("util");
const zlib_1 = tslib_1.__importDefault(require("zlib"));
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const hasha_1 = tslib_1.__importDefault(require("hasha"));
const global_1 = require("../../../../config/global");
const logger_1 = require("../../../../logger");
const schema = tslib_1.__importStar(require("../../../schema"));
const stringify_1 = require("../../../stringify");
const common_1 = require("../common");
const schemas_1 = require("../schemas");
const compress = (0, util_1.promisify)(zlib_1.default.brotliCompress);
const decompress = (0, util_1.promisify)(zlib_1.default.brotliDecompress);
class RepoCacheBase {
    constructor(repository, fingerprint) {
        this.repository = repository;
        this.fingerprint = fingerprint;
        this.platform = global_1.GlobalConfig.get('platform');
        this.oldHash = null;
        this.data = {};
    }
    async restore(oldCache) {
        if (oldCache.fingerprint !== this.fingerprint) {
            logger_1.logger.debug('Repository cache fingerprint is invalid');
            return;
        }
        const compressed = Buffer.from(oldCache.payload, 'base64');
        const uncompressed = await decompress(compressed);
        const jsonStr = uncompressed.toString('utf8');
        this.data = JSON.parse(jsonStr);
        this.oldHash = oldCache.hash;
    }
    async load() {
        try {
            const rawOldCache = await this.read();
            if (!is_1.default.string(rawOldCache)) {
                logger_1.logger.debug(`RepoCacheBase.load() - expecting data of type 'string' received '${typeof rawOldCache}' instead - skipping`);
                return;
            }
            const oldCache = JSON.parse(rawOldCache);
            if (schema.match(schemas_1.RepoCacheV13, oldCache)) {
                await this.restore(oldCache);
                logger_1.logger.debug('Repository cache is restored from revision 13');
                return;
            }
            logger_1.logger.debug('Repository cache is invalid');
        }
        catch (err) {
            logger_1.logger.debug({ err }, 'Error reading repository cache');
        }
    }
    async save() {
        const jsonStr = (0, stringify_1.safeStringify)(this.data);
        const hash = await hasha_1.default.async(jsonStr);
        if (hash === this.oldHash) {
            return;
        }
        const revision = common_1.CACHE_REVISION;
        const repository = this.repository;
        const fingerprint = this.fingerprint;
        const compressedPayload = await compress(jsonStr);
        const payload = compressedPayload.toString('base64');
        await this.write({
            revision,
            repository,
            fingerprint,
            payload,
            hash,
        });
    }
    getData() {
        return this.data;
    }
    isModified() {
        if (!this.oldHash) {
            return undefined;
        }
        const jsonStr = (0, stringify_1.safeStringify)(this.data);
        return (0, hasha_1.default)(jsonStr) !== this.oldHash;
    }
}
exports.RepoCacheBase = RepoCacheBase;
//# sourceMappingURL=base.js.map