From d2690f37288dd68c32515a2ac34caeae67537283 Mon Sep 17 00:00:00 2001 From: Andrii Andreiev <129078694+AndriiAndreiev@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:26:18 +0300 Subject: [PATCH] fix(node): refactoring according to the new version, test fixes --- packages/node/src/lib/get-project-base-url.ts | 12 +++++---- packages/node/test/index.test.ts | 11 +++++--- .../test/lib/get-project-base-url.test.ts | 25 ++++++++++--------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/node/src/lib/get-project-base-url.ts b/packages/node/src/lib/get-project-base-url.ts index b74eb9185..5434bf74c 100644 --- a/packages/node/src/lib/get-project-base-url.ts +++ b/packages/node/src/lib/get-project-base-url.ts @@ -1,7 +1,7 @@ import crypto from 'crypto'; import findCacheDir from 'find-cache-dir'; -import flatCache from 'flat-cache'; +import { FlatCache } from 'flat-cache'; import timeoutSignal from 'timeout-signal'; import pkg from '../../package.json'; @@ -9,6 +9,8 @@ import config from '../config'; import { logger } from './logger'; +export const cache = new FlatCache(); + export function getCache(readmeApiKey: string) { const encodedApiKey = Buffer.from(`${readmeApiKey}:`).toString('base64'); const cacheDir = findCacheDir({ name: pkg.name, create: true }); @@ -18,16 +20,16 @@ export function getCache(readmeApiKey: string) { // automatically get refreshed when the package is updated/installed. const cacheKey = `${pkg.name}-${pkg.version}-${fsSafeApikey}`; - return flatCache.load(cacheKey, cacheDir); + return cache.load(cacheKey, cacheDir); } export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = config.timeout): Promise { const encodedApiKey = Buffer.from(`${readmeApiKey}:`).toString('base64'); - const cache = getCache(readmeApiKey); + getCache(readmeApiKey); // Does the cache exist? If it doesn't, let's fill it. If it does, let's see if it's stale. Caches should have a TTL // of 1 day. - const lastUpdated = cache.getKey('lastUpdated'); + const lastUpdated = cache.getKey('lastUpdated'); if ( lastUpdated === undefined || @@ -76,7 +78,7 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c return baseUrl; } - const cachedBaseUrl = cache.getKey('baseUrl'); + const cachedBaseUrl = cache.getKey('baseUrl'); logger.verbose({ message: 'Retrieved baseUrl from cache.', args: { baseUrl: cachedBaseUrl } }); return cachedBaseUrl; } diff --git a/packages/node/test/index.test.ts b/packages/node/test/index.test.ts index 2fbfe9d27..e34bd3e60 100644 --- a/packages/node/test/index.test.ts +++ b/packages/node/test/index.test.ts @@ -7,6 +7,7 @@ import * as crypto from 'crypto'; import { createServer } from 'http'; import express from 'express'; +import { FlatCache } from 'flat-cache'; import { delay, http, HttpResponse, passthrough } from 'msw'; import { setupServer } from 'msw/node'; import request from 'supertest'; @@ -21,6 +22,8 @@ import { setBackoff } from '../src/lib/metrics-log'; import getReadMeApiMock from './helpers/getReadMeApiMock'; import { MockLoggerStrategy } from './lib/logger.test'; +const cache = new FlatCache(); + const apiKey = 'mockReadMeApiKey'; const endUserApiKey = '5afa21b97011c63320226ef3'; const incomingGroup = { @@ -60,7 +63,7 @@ function doMetricsHeadersMatch(headers: Headers) { describe('#metrics', function () { beforeEach(function () { server.listen(); - const cache = getCache(apiKey); + getCache(apiKey); cache.setKey('lastUpdated', Date.now()); cache.setKey('baseUrl', 'https://docs.example.com'); @@ -69,7 +72,7 @@ describe('#metrics', function () { afterEach(function () { server.resetHandlers(); - getCache(apiKey).destroy(); + cache.destroy(); }); // Close server after all tests @@ -146,7 +149,7 @@ describe('#metrics', function () { app = express(); app.use((req, res, next) => { const logId = readmeio.log(apiKey, req, res, incomingGroup, { logger: mockLogger }); - res.setHeader('x-log-id', logId); + res.setHeader('x-log-id', logId!); return next(); }); app.get('/test', (req, res) => res.sendStatus(200)); @@ -531,7 +534,7 @@ describe('#metrics', function () { it('should fetch the `baseLogUrl` if not passed', function () { expect.assertions(1); // Invalidating the cache so we do a fetch from the API - const cache = getCache(apiKey); + getCache(apiKey); const lastUpdated = new Date(); lastUpdated.setDate(lastUpdated.getDate() - 2); cache.setKey('lastUpdated', lastUpdated.getTime()); diff --git a/packages/node/test/lib/get-project-base-url.test.ts b/packages/node/test/lib/get-project-base-url.test.ts index 08e7ad6ed..80b49d7cb 100644 --- a/packages/node/test/lib/get-project-base-url.test.ts +++ b/packages/node/test/lib/get-project-base-url.test.ts @@ -4,7 +4,7 @@ import { describe, beforeAll, afterAll, afterEach, expect, it } from 'vitest'; import { getProjectBaseUrl } from '../../src'; import config from '../../src/config'; -import { getCache } from '../../src/lib/get-project-base-url'; +import { getCache, cache } from '../../src/lib/get-project-base-url'; import getReadMeApiMock from '../helpers/getReadMeApiMock'; const apiKey = 'mockReadMeApiKey'; @@ -15,7 +15,7 @@ const restHandlers = [getReadMeApiMock(baseLogUrl)]; const server = setupServer(...restHandlers); function hydrateCache(lastUpdated: number) { - const cache = getCache(apiKey); + getCache(apiKey); cache.setKey('lastUpdated', lastUpdated); cache.setKey('baseUrl', baseLogUrl); @@ -30,7 +30,8 @@ describe('get-project-base-url', function () { afterEach(function () { server.resetHandlers(); - getCache(apiKey).destroy(); + getCache(apiKey); + cache.destroy(); }); // Close server after all tests @@ -40,26 +41,26 @@ describe('get-project-base-url', function () { it('should not call the API for project data if the cache is fresh', async function () { await getProjectBaseUrl(apiKey, 2000); - expect(getCache(apiKey).getKey('baseUrl')).toStrictEqual(baseLogUrl); - const lastUpdated = getCache(apiKey).getKey('lastUpdated'); + expect(cache.getKey('baseUrl')).toStrictEqual(baseLogUrl); + const lastUpdated = cache.getKey('lastUpdated'); await getProjectBaseUrl(apiKey, 2000); - expect(getCache(apiKey).getKey('lastUpdated')).toStrictEqual(lastUpdated); + expect(cache.getKey('lastUpdated')).toStrictEqual(lastUpdated); }); it('should populate the cache if not present', async function () { await getProjectBaseUrl(apiKey, 2000); - expect(getCache(apiKey).getKey('baseUrl')).toStrictEqual(baseLogUrl); + expect(cache.getKey('baseUrl')).toStrictEqual(baseLogUrl); }); it('should refresh the cache if stale', async function () { // Hydrate and postdate the cache to two days ago so it'll be seen as stale. hydrateCache(Math.round(Date.now() / 1000 - 86400 * 2)); - expect(getCache(apiKey).getKey('baseUrl')).toStrictEqual(baseLogUrl); + expect(cache.getKey('baseUrl')).toStrictEqual(baseLogUrl); - const lastUpdated = getCache(apiKey).getKey('lastUpdated'); + const lastUpdated = cache.getKey('lastUpdated'); await getProjectBaseUrl(apiKey, 2000); - expect(getCache(apiKey).getKey('baseUrl')).toStrictEqual(baseLogUrl); - expect(getCache(apiKey).getKey('lastUpdated')).not.toStrictEqual(lastUpdated); + expect(cache.getKey('baseUrl')).toStrictEqual(baseLogUrl); + expect(cache.getKey('lastUpdated')).not.toStrictEqual(lastUpdated); }); it('should temporarily set baseUrl to null if the call to the ReadMe API fails for whatever reason', async function () { @@ -80,6 +81,6 @@ describe('get-project-base-url', function () { ); await getProjectBaseUrl(apiKey, 2000); - expect(getCache(apiKey).getKey('baseUrl')).toBeNull(); + expect(cache.getKey('baseUrl')).toBeNull(); }); });