fix: CI build errors + simplify cache handler and data cache
- Remove `export const dynamic` from 4 routes (incompatible with cacheComponents) - Static import for redis-handler (Turbopack can't resolve relative dynamic imports) - Simplify redis-handler: Buffer.concat, NonNullable, single connect promise - Inline single-use helpers in integration-data-cache - Non-greedy SVG regex to prevent ReDoS - Inline getDevelopmentServiceAliases in next.config - Remove restating comments
This commit is contained in:
12 files changed
+79
-98
No files matched your search
@@ -1,7 +1,3 @@
|
||||
// ponytail: module-level cache is single-instance only.
|
||||
// Multi-instance deployments (REDIS_IS_EXTERNAL=true) bypass this entirely —
|
||||
// each request calls the upstream fetcher directly.
|
||||
// isMultiInstance is read at module load; bypass behavior is verified via code review, not unit tests.
|
||||
const isMultiInstance = process.env.REDIS_IS_EXTERNAL === "true";
|
||||
|
||||
interface CachedEntry<T> {
|
||||
@@ -14,22 +10,6 @@ const cache = new Map<string, CachedEntry<unknown>>();
|
||||
const DEFAULT_TTL_MS = 60_000;
|
||||
const MAX_ENTRIES = 200;
|
||||
|
||||
const cacheKey = (integrationId: string, queryKey: string) => `${integrationId}:${queryKey}`;
|
||||
|
||||
function evictOldestEntry() {
|
||||
if (cache.size <= MAX_ENTRIES) return;
|
||||
|
||||
let oldestKey: string | undefined;
|
||||
let oldestTime = Infinity;
|
||||
for (const [key, entry] of cache) {
|
||||
if (entry.timestamp < oldestTime) {
|
||||
oldestTime = entry.timestamp;
|
||||
oldestKey = key;
|
||||
}
|
||||
}
|
||||
if (oldestKey) cache.delete(oldestKey);
|
||||
}
|
||||
|
||||
export const getCachedIntegrationData = async <T>(
|
||||
integrationId: string,
|
||||
queryKey: string,
|
||||
@@ -40,7 +20,7 @@ export const getCachedIntegrationData = async <T>(
|
||||
return fetcher();
|
||||
}
|
||||
|
||||
const key = cacheKey(integrationId, queryKey);
|
||||
const key = `${integrationId}:${queryKey}`;
|
||||
const existing = cache.get(key) as CachedEntry<T> | undefined;
|
||||
const now = Date.now();
|
||||
|
||||
@@ -58,7 +38,17 @@ export const getCachedIntegrationData = async <T>(
|
||||
.then((data) => {
|
||||
if (cache.get(key) === placeholder) {
|
||||
cache.set(key, { data, timestamp: Date.now() });
|
||||
evictOldestEntry();
|
||||
if (cache.size > MAX_ENTRIES) {
|
||||
let oldestKey: string | undefined;
|
||||
let oldestTime = Infinity;
|
||||
for (const [k, e] of cache) {
|
||||
if (e.timestamp < oldestTime) {
|
||||
oldestTime = e.timestamp;
|
||||
oldestKey = k;
|
||||
}
|
||||
}
|
||||
if (oldestKey) cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
})
|
||||
|
||||
@@ -4,8 +4,7 @@ import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { PiHoleIntegrationV5 } from "../src/pi-hole/v5/pi-hole-integration-v5";
|
||||
import { PiHoleIntegrationV6 } from "../src/pi-hole/v6/pi-hole-integration-v6";
|
||||
import { PiHoleIntegrationV5, PiHoleIntegrationV6 } from "../src";
|
||||
import type { SessionStore } from "../src/base/session-store";
|
||||
import { TestConnectionError } from "../src/base/test-connection/test-connection-error";
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
import { RedisCacheHandler } from "./redis-handler";
|
||||
|
||||
let handlerPromise: Promise<CacheHandler> | undefined;
|
||||
|
||||
const createHandlerAsync = async (): Promise<CacheHandler> => {
|
||||
const useRedis = process.env.REDIS_HOST || process.env.REDIS_IS_EXTERNAL === "true";
|
||||
if (useRedis) {
|
||||
const { RedisCacheHandler } = await import("./redis-handler");
|
||||
return new RedisCacheHandler();
|
||||
}
|
||||
if (useRedis) return new RedisCacheHandler();
|
||||
|
||||
const { createDefaultCacheHandler } = await import("next/dist/server/lib/cache-handlers/default");
|
||||
return createDefaultCacheHandler(32 * 1024 * 1024);
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import Redis from "ioredis";
|
||||
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
type CacheEntry = Awaited<ReturnType<CacheHandler["get"]>> & {};
|
||||
type CacheEntry = NonNullable<Awaited<ReturnType<CacheHandler["get"]>>>;
|
||||
|
||||
type StoredEntry = {
|
||||
tags: string[];
|
||||
@@ -27,14 +27,7 @@ async function readStreamBody(stream: ReadableStream<Uint8Array>) {
|
||||
if (result.value) chunks.push(result.value);
|
||||
}
|
||||
|
||||
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
||||
const body = new Uint8Array(totalLength);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
body.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
return body;
|
||||
return new Uint8Array(Buffer.concat(chunks));
|
||||
}
|
||||
|
||||
function entryFromStored(parsed: StoredEntry): CacheEntry {
|
||||
@@ -58,6 +51,7 @@ export class RedisCacheHandler implements CacheHandler {
|
||||
private redis: Redis;
|
||||
private pendingSets = new Map<string, Promise<CacheEntry>>();
|
||||
private tagCache = new Map<string, number>();
|
||||
private connectPromise: Promise<void> | undefined;
|
||||
private loggedError = false;
|
||||
|
||||
constructor() {
|
||||
@@ -85,8 +79,8 @@ export class RedisCacheHandler implements CacheHandler {
|
||||
return `nextCache:tag:${tag}`;
|
||||
}
|
||||
|
||||
private async ensureConnected() {
|
||||
await this.redis.connect().catch(() => {});
|
||||
private ensureConnected() {
|
||||
return (this.connectPromise ??= this.redis.connect().catch(() => {}));
|
||||
}
|
||||
|
||||
async get(cacheKey: string, softTags: string[]): Promise<CacheEntry | undefined> {
|
||||
@@ -114,7 +108,7 @@ export class RedisCacheHandler implements CacheHandler {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const expiration = await this.getExpiration([...parsed.tags, ...softTags]);
|
||||
const expiration = await this.getExpiration(parsed.tags.concat(softTags));
|
||||
if (expiration !== 0 && expiration > parsed.timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user