"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VersionsEndpointCache = exports.memCache = void 0;
const zod_1 = require("zod");
const logger_1 = require("../../../logger");
const date_1 = require("../../../util/date");
const http_1 = require("../../../util/http");
const regex_1 = require("../../../util/regex");
const schema_utils_1 = require("../../../util/schema-utils");
const string_1 = require("../../../util/string");
function getContentTail(content) {
    return content.slice(-33);
}
function getContentHead(content) {
    return content.slice(0, 33);
}
function stripContentHead(content) {
    return content.slice(33);
}
function reconcilePackageVersions(packageVersions, versionLines) {
    for (const line of versionLines) {
        const packageName = (0, string_1.copystr)(line.packageName);
        let versions = packageVersions.get(packageName) ?? [];
        const { deletedVersions, addedVersions } = line;
        if (deletedVersions.size > 0) {
            versions = versions.filter((v) => !deletedVersions.has(v));
        }
        if (addedVersions.length > 0) {
            const existingVersions = new Set(versions);
            for (const addedVersion of addedVersions) {
                if (!existingVersions.has(addedVersion)) {
                    const version = (0, string_1.copystr)(addedVersion);
                    versions.push(version);
                }
            }
        }
        packageVersions.set(packageName, versions);
    }
    return packageVersions;
}
function parseFullBody(body) {
    const versionsEndpointSupported = true;
    const packageVersions = reconcilePackageVersions(new Map(), VersionLines.parse(body));
    const syncedAt = new Date();
    const contentLength = body.length;
    const contentTail = getContentTail(body);
    return {
        versionsEndpointSupported,
        packageVersions,
        syncedAt,
        contentLength,
        contentTail,
    };
}
exports.memCache = new Map();
const VersionLines = zod_1.z
    .string()
    .transform((x) => x.split(regex_1.newlineRegex))
    .pipe((0, schema_utils_1.LooseArray)(zod_1.z
    .string()
    .transform((line) => line.trim())
    .refine((line) => line.length > 0)
    .refine((line) => !line.startsWith('created_at:'))
    .refine((line) => line !== '---')
    .transform((line) => line.split(' '))
    .pipe(zod_1.z.tuple([zod_1.z.string(), zod_1.z.string()]).rest(zod_1.z.string()))
    .transform(([packageName, versions]) => {
    const deletedVersions = new Set();
    const addedVersions = [];
    for (const version of versions.split(',')) {
        if (version.startsWith('-')) {
            deletedVersions.add(version.slice(1));
        }
        else {
            addedVersions.push(version);
        }
    }
    return { packageName, deletedVersions, addedVersions };
})));
function isStale(regCache) {
    return (0, date_1.getElapsedMinutes)(regCache.syncedAt) >= 15;
}
class VersionsEndpointCache {
    http;
    constructor(http) {
        this.http = http;
    }
    cacheRequests = new Map();
    /**
     * At any given time, there should only be one request for a given registryUrl.
     */
    async getCache(registryUrl) {
        const cacheKey = `rubygems-versions-cache:${registryUrl}`;
        const oldCache = exports.memCache.get(cacheKey);
        exports.memCache.delete(cacheKey); // If no error is thrown, we'll re-set the cache
        let newCache;
        if (!oldCache) {
            newCache = await this.fullSync(registryUrl);
        }
        else if (oldCache.versionsEndpointSupported === false) {
            newCache = oldCache;
        }
        else if (isStale(oldCache)) {
            newCache = await this.deltaSync(oldCache, registryUrl);
        }
        else {
            newCache = oldCache;
        }
        exports.memCache.set(cacheKey, newCache);
        return newCache;
    }
    async getVersions(registryUrl, packageName) {
        /**
         * Ensure that only one request for a given registryUrl is in flight at a time.
         */
        let cacheRequest = this.cacheRequests.get(registryUrl);
        if (!cacheRequest) {
            cacheRequest = this.getCache(registryUrl);
            this.cacheRequests.set(registryUrl, cacheRequest);
        }
        let cache;
        try {
            cache = await cacheRequest;
        }
        finally {
            this.cacheRequests.delete(registryUrl);
        }
        if (cache.versionsEndpointSupported === false) {
            logger_1.logger.debug({ packageName, registryUrl }, 'Rubygems: endpoint not supported');
            return { type: 'not-supported' };
        }
        const versions = cache.packageVersions.get(packageName);
        if (!versions?.length) {
            logger_1.logger.debug({ packageName, registryUrl }, 'Rubygems: versions not found');
            return { type: 'not-found' };
        }
        return { type: 'success', versions };
    }
    async fullSync(registryUrl) {
        try {
            const url = `${registryUrl}/versions`;
            const opts = { headers: { 'Accept-Encoding': 'gzip' } };
            const { body } = await this.http.get(url, opts);
            return parseFullBody(body);
        }
        catch (err) {
            if (err instanceof http_1.HttpError && err.response?.statusCode === 404) {
                return { versionsEndpointSupported: false };
            }
            throw err;
        }
    }
    async deltaSync(oldCache, registryUrl) {
        try {
            const url = `${registryUrl}/versions`;
            const startByte = oldCache.contentLength - oldCache.contentTail.length;
            const opts = {
                headers: {
                    ['Accept-Encoding']: 'deflate, compress, br',
                    ['Range']: `bytes=${startByte}-`,
                },
            };
            const { statusCode, body } = await this.http.get(url, opts);
            /**
             * Rubygems will return the full body instead of `416 Range Not Satisfiable`.
             * In this case, status code will be 200 instead of 206.
             */
            if (statusCode === 200) {
                return parseFullBody(body);
            }
            /**
             * We request data in range that overlaps previously fetched data.
             * If the head of the response doesn't match the tail of the previous response,
             * it means that the data we have is no longer valid.
             * In this case we start over with a full sync.
             */
            const contentHead = getContentHead(body);
            if (contentHead !== oldCache.contentTail) {
                return this.fullSync(registryUrl);
            }
            /**
             * Update the cache with the new data.
             */
            const versionsEndpointSupported = true;
            const delta = stripContentHead(body);
            const packageVersions = reconcilePackageVersions(oldCache.packageVersions, VersionLines.parse(delta));
            const syncedAt = new Date();
            const contentLength = oldCache.contentLength + delta.length;
            const contentTail = getContentTail(body);
            return {
                versionsEndpointSupported,
                packageVersions,
                syncedAt,
                contentLength,
                contentTail,
            };
        }
        catch (err) {
            if (err instanceof http_1.HttpError) {
                const responseStatus = err.response?.statusCode;
                /**
                 * In case of `416 Range Not Satisfiable` we do a full sync.
                 * This is unlikely to happen in real life, but anyway.
                 */
                if (responseStatus === 416) {
                    return this.fullSync(registryUrl);
                }
                /**
                 * If the endpoint is not supported, we stop trying.
                 * This is unlikely to happen in real life, but still.
                 */
                if (responseStatus === 404) {
                    return { versionsEndpointSupported: false };
                }
            }
            throw err;
        }
    }
}
exports.VersionsEndpointCache = VersionsEndpointCache;
//# sourceMappingURL=versions-endpoint-cache.js.map