Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): bump flat-cache from 3.1.0 to 6.1.1 #1102

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 96 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"content-type": "^1.0.5",
"find-cache-dir": "^3.3.2",
"flat-cache": "^3.0.4",
"flat-cache": "^6.1.1",
"lodash": "^4.17.15",
"ssri": "^12.0.0",
"timeout-signal": "^1.1.0",
Expand Down
12 changes: 7 additions & 5 deletions packages/node/src/lib/get-project-base-url.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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';
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 });
Expand All @@ -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<string> {
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<number>('lastUpdated');

if (
lastUpdated === undefined ||
Expand Down Expand Up @@ -76,7 +78,7 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c

return baseUrl;
}
const cachedBaseUrl = cache.getKey('baseUrl');
const cachedBaseUrl = cache.getKey<string>('baseUrl');
logger.verbose({ message: 'Retrieved baseUrl from cache.', args: { baseUrl: cachedBaseUrl } });
return cachedBaseUrl;
}
11 changes: 7 additions & 4 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = {
Expand Down Expand Up @@ -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');
Expand All @@ -69,7 +72,7 @@ describe('#metrics', function () {

afterEach(function () {
server.resetHandlers();
getCache(apiKey).destroy();
cache.destroy();
});

// Close server after all tests
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
Loading