private static async GetResponseFromCache()

in src/common.browser/CertChecks.ts [178:242]


    private static async GetResponseFromCache(signature: string, ocspRequest: ocsp.Request, proxyInfo: ProxyInfo): Promise<Buffer> {
        let cachedResponse: Buffer = CertCheckAgent.privMemCache[signature];

        if (!!cachedResponse) {
            this.onEvent(new OCSPMemoryCacheHitEvent(signature));
        }

        // Do we have a result for this certificate on disk in %TMP%?
        if (!cachedResponse) {
            try {
                const diskCacheResponse: any = await CertCheckAgent.privDiskCache.get(signature);
                if (!!diskCacheResponse.isCached) {
                    CertCheckAgent.onEvent(new OCSPDiskCacheHitEvent(signature));
                    CertCheckAgent.StoreMemoryCacheEntry(signature, diskCacheResponse.value);
                    cachedResponse = diskCacheResponse.value;
                }
            } catch (error) {
                cachedResponse = null;
            }
        }

        if (!cachedResponse) {
            return cachedResponse;
        }

        try {
            const cachedOcspResponse: ocsp.Response = ocsp.utils.parseResponse(cachedResponse);
            const tbsData = cachedOcspResponse.value.tbsResponseData;
            if (tbsData.responses.length < 1) {
                this.onEvent(new OCSPCacheFetchErrorEvent(signature, "Not enough data in cached response"));
                return;
            }

            const cachedStartTime: number = tbsData.responses[0].thisUpdate;
            const cachedNextTime: number = tbsData.responses[0].nextUpdate;

            if (cachedNextTime < (Date.now() + this.testTimeOffset - 60000)) {
                // Cached entry has expired.
                this.onEvent(new OCSPCacheEntryExpiredEvent(signature, cachedNextTime));
                cachedResponse = null;
            } else {
                // If we're within one day of the next update, or 50% of the way through the validity period,
                // background an update to the cache.

                const minUpdate: number = Math.min(24 * 60 * 60 * 1000, (cachedNextTime - cachedStartTime) / 2);

                if ((cachedNextTime - (Date.now() + this.testTimeOffset)) < minUpdate) {
                    this.onEvent(new OCSPCacheEntryNeedsRefreshEvent(signature, cachedStartTime, cachedNextTime));
                    this.UpdateCache(ocspRequest, proxyInfo).catch((error: string) => {
                        // Well, not much we can do here.
                        this.onEvent(new OCSPCacheUpdateErrorEvent(signature, error.toString()));
                    });
                } else {
                    this.onEvent(new OCSPCacheHitEvent(signature, cachedStartTime, cachedNextTime));
                }
            }
        } catch (error) {
            this.onEvent(new OCSPCacheFetchErrorEvent(signature, error));
            cachedResponse = null;
        }
        if (!cachedResponse) {
            this.onEvent(new OCSPCacheMissEvent(signature));
        }
        return cachedResponse;
    }