perf: implement Phase 1 homepage diet via Next.js Cache Components
P1-0: V8 heap cap (HOMARR_MAX_OLD_SPACE_SIZE, default 512MB)
P1-1: Module graph cuts
- Dynamic integration imports (55 static → lazy loaders)
- Conditional DB driver loading (only configured driver loaded)
- Replace jsdom with linkedom for SVG sanitization
- Delete eager widget barrel (55 import * as → 0)
- Extract createRetryableLoader to @homarr/common
P1-2: Enable cacheComponents + restructure root layout
- Suspense boundaries around session-dependent providers
- ColorSchemeScript replaces server-side cookie reads
- Board/manage/auth layouts wrapped in Suspense
P1-3: Cache read paths
- Cache tag registry + invalidation helpers
- Cache invalidation wired into all mutation routers
- Translation deep-merge memoized per locale
P1-4: Prerendered shells + loading.tsx files
P1-5: Shared integration data cache (60s TTL, stale-while-revalidate)
P1-6: Redis-backed Next.js cache handler (@homarr/next-cache-handler)
P1-7: Redis optional (memory channel fallback), TTL on pubSub:last:*,
Redis clients consolidated from 6 to 2
This commit is contained in:
86 files changed
+2219
-1232
No files matched your search
@@ -0,0 +1,62 @@
|
||||
---
|
||||
iteration: 2
|
||||
max_iterations: 15
|
||||
status: active
|
||||
goal: "Implement Phase 1 homepage diet via Next.js Cache Components - P1-0 through P1-7 with all correctness gates"
|
||||
success_criteria:
|
||||
- "[x] P1-0: V8 heap cap in scripts/run.sh with HOMARR_MAX_OLD_SPACE_SIZE env var"
|
||||
- "[x] P1-0: HOMARR_MAX_OLD_SPACE_SIZE documented in docs"
|
||||
- "[x] P1-1.1: Dynamic integration imports replacing 55 static imports in creator.ts"
|
||||
- "[x] P1-1.1: createRetryableLoader extracted to packages/common"
|
||||
- "[x] P1-1.1: creator.spec.ts testing all integration kinds load"
|
||||
- "[x] P1-1.2: DB driver loaded conditionally via DB_DRIVER env"
|
||||
- "[x] P1-1.3: jsdom replaced with linkedom for SVG sanitization"
|
||||
- "[x] P1-1.3: sanitize.spec.ts with XSS test corpus"
|
||||
- "[x] P1-1.4: Eager widget barrel deleted, imports repointed"
|
||||
- "[x] P1-2.1: cacheComponents enabled in next.config.ts with cacheLife profiles"
|
||||
- "[x] P1-2.2: Root layout restructured with Suspense boundaries"
|
||||
- "[x] P1-2.3: Board/manage/auth routes compatible with cacheComponents"
|
||||
- "[x] P1-3.1: Cache tag registry in packages/api/src/cache-tags.ts"
|
||||
- "[x] P1-3.2: Server settings kept with React cache (shared package constraint)"
|
||||
- "[x] P1-3.3: Session providers moved to SessionScopedProviders with Suspense"
|
||||
- "[x] P1-3.4: Per-request translation deep-merge eliminated"
|
||||
- "[x] P1-3.5: Cache invalidation wired into all mutation routers"
|
||||
- "[x] P1-4: Prerendered shells for widget catalog, loading.tsx files"
|
||||
- "[x] P1-5: Shared integration data cache (module-level, 60s TTL, stale-while-revalidate)"
|
||||
- "[x] P1-6: Redis-backed Next cache handler"
|
||||
- "[x] P1-7.1: Redis optional for single-instance (memory channel fallback)"
|
||||
- "[x] P1-7.1: TTL on pubSub:last:* keys (F4)"
|
||||
- "[x] P1-7.1: Redis clients consolidated from 6 to 2 (F3)"
|
||||
- "[x] All 25 new specs pass"
|
||||
- "[x] No diff to trpc.tsx or query-cache.ts"
|
||||
- "[ ] Reviewed with /review-pr at least once"
|
||||
- "[ ] All review issues fixed"
|
||||
- "[ ] Integration type map created for kind-to-class resolution"
|
||||
last_verdict: null
|
||||
last_reason: null
|
||||
last_next: "Run /review-pr, fix issues, iterate until clean"
|
||||
engine: cursor
|
||||
---
|
||||
|
||||
## Progress log
|
||||
|
||||
### Iteration 1 — Implementation complete
|
||||
- P1-0 through P1-7 all implemented via parallel subagents
|
||||
- 66 files changed, 1280 insertions, 1229 deletions
|
||||
- 25 new tests all passing
|
||||
- Type errors from dynamic integration imports fixed with kind-map.ts
|
||||
|
||||
### Iteration 2 — Review cycle begins
|
||||
- Need to commit, push, and run /review-pr
|
||||
- Need to fix any issues found
|
||||
|
||||
## Blockers
|
||||
None currently.
|
||||
|
||||
## Verification
|
||||
- 25/25 tests pass
|
||||
- trpc.tsx and query-cache.ts unchanged (constraint met)
|
||||
- Integration type map resolves subclass methods correctly
|
||||
|
||||
## Task
|
||||
Implement Phase 1 homepage diet via Next.js Cache Components - P1-0 through P1-7 with all correctness gates
|
||||
@@ -79,3 +79,4 @@ apps/docs/.docusaurus
|
||||
apps/docs/build
|
||||
.pnpm-store
|
||||
benchmark-results
|
||||
.bench/
|
||||
@@ -32,6 +32,7 @@ Using the `PUID` and `PGID` will require you to set the correct permissions on t
|
||||
| `LOG_LEVEL` | Log level to use | `debug` / `info` / `warn` / `error` | `info` |
|
||||
| `NO_EXTERNAL_CONNECTION` | Disables Homarr's internet-backed requests, including update checks, weather, and RSS feeds | `true` or `false` | `false` |
|
||||
| `ENABLE_DNS_CACHING` | Enables dns caching. Enabled by default, but if you experience issues with IPv6 or static IPs, see [#4006](https://github.com/homarr-labs/homarr/issues/4006) or disable this flag | `true` or `false` | `true` |
|
||||
| `HOMARR_MAX_OLD_SPACE_SIZE` | V8 old-space heap limit in MB. Prevents RSS drift on large-memory hosts. Raise for very large boards. | Any positive integer (MB) | `512` |
|
||||
|
||||
## Authentication
|
||||
|
||||
|
||||
@@ -27,6 +27,9 @@ const getDevelopmentServiceAliases = () => {
|
||||
};
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
cacheHandlers: {
|
||||
default: path.resolve(import.meta.dirname, "../../packages/next-cache-handler/src/index.ts"),
|
||||
},
|
||||
// Next previews otherwise create agent instruction files in the application
|
||||
// directory during development.
|
||||
agentRules: false,
|
||||
@@ -35,15 +38,21 @@ const nextConfig: NextConfig = {
|
||||
},
|
||||
output: "standalone",
|
||||
reactStrictMode: true,
|
||||
cacheComponents: true,
|
||||
cacheMaxMemorySize: 32 * 1024 * 1024,
|
||||
cacheLife: {
|
||||
config: { stale: 60, revalidate: 60 * 60 * 24, expire: 60 * 60 * 24 * 7 },
|
||||
integration: { stale: 15, revalidate: 60, expire: 60 * 10 },
|
||||
session: { stale: 30, revalidate: 60 * 5, expire: 60 * 30 },
|
||||
},
|
||||
// react compiler breaks mantine-react-table, so disabled for now
|
||||
//reactCompiler: true,
|
||||
/** We already do typechecking as separate tasks in CI */
|
||||
typescript: { ignoreBuildErrors: true },
|
||||
/**
|
||||
* dockerode is required in the external server packages because of https://github.com/homarr-labs/homarr/issues/612
|
||||
* isomorphic-dompurify and jsdom are required, see https://github.com/kkomelin/isomorphic-dompurify/issues/356
|
||||
*/
|
||||
serverExternalPackages: ["dockerode", "isomorphic-dompurify", "jsdom", "better-sqlite3"],
|
||||
serverExternalPackages: ["dockerode", "better-sqlite3", "mysql2", "pg"],
|
||||
experimental: {
|
||||
optimizePackageImports: ["@mantine/core", "@mantine/hooks", "@tabler/icons-react"],
|
||||
turbopackFileSystemCacheForBuild: true,
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"@homarr/integrations": "workspace:^0.1.0",
|
||||
"@homarr/modals": "workspace:^0.1.0",
|
||||
"@homarr/modals-collection": "workspace:^0.1.0",
|
||||
"@homarr/next-cache-handler": "workspace:^0.1.0",
|
||||
"@homarr/notifications": "workspace:^0.1.0",
|
||||
"@homarr/old-import": "workspace:^0.1.0",
|
||||
"@homarr/old-schema": "workspace:^0.1.0",
|
||||
@@ -76,13 +77,14 @@
|
||||
"chroma-js": "catalog:",
|
||||
"clsx": "catalog:",
|
||||
"dayjs": "catalog:",
|
||||
"dompurify": "catalog:",
|
||||
"dotenv": "catalog:",
|
||||
"drizzle-orm": "catalog:",
|
||||
"flag-icons": "catalog:",
|
||||
"glob": "catalog:",
|
||||
"isomorphic-dompurify": "catalog:",
|
||||
"jotai": "catalog:",
|
||||
"jszip": "catalog:",
|
||||
"linkedom": "catalog:",
|
||||
"lucide-react": "catalog:",
|
||||
"mantine-datatable": "catalog:",
|
||||
"mantine-react-table": "catalog:",
|
||||
|
||||
@@ -13,8 +13,8 @@ import { theme } from "@homarr/ui";
|
||||
|
||||
export const CustomMantineProvider = ({
|
||||
children,
|
||||
defaultColorScheme,
|
||||
}: PropsWithChildren<{ defaultColorScheme: MantineColorScheme }>) => {
|
||||
defaultColorScheme = "auto",
|
||||
}: PropsWithChildren<{ defaultColorScheme?: MantineColorScheme }>) => {
|
||||
const manager = useColorSchemeManager();
|
||||
return (
|
||||
<DirectionProvider>
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { PropsWithChildren } from "react";
|
||||
import type { DayOfWeek } from "@mantine/dates";
|
||||
import { NextIntlClientProvider } from "next-intl";
|
||||
|
||||
import { getRscServerSettingsAsync } from "@homarr/api/server-settings-server";
|
||||
import { getRscUserSettingsAsync } from "@homarr/api/user-server";
|
||||
import { env } from "@homarr/auth/env";
|
||||
import { auth } from "@homarr/auth/next";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ModalProvider } from "@homarr/modals";
|
||||
import { Notifications } from "@homarr/notifications";
|
||||
import { SettingsProvider } from "@homarr/settings";
|
||||
import { SpotlightProvider } from "@homarr/spotlight";
|
||||
|
||||
import { Analytics } from "~/components/layout/analytics";
|
||||
import { ServiceWorkerRegistration } from "~/components/layout/service-worker-registration";
|
||||
import { DayJsLoader } from "./_client-providers/dayjs-loader";
|
||||
import { JotaiProvider } from "./_client-providers/jotai";
|
||||
import { CustomMantineProvider } from "./_client-providers/mantine";
|
||||
import { AuthProvider } from "./_client-providers/session";
|
||||
import { TRPCReactProvider } from "./_client-providers/trpc";
|
||||
import { composeWrappers } from "./compose";
|
||||
|
||||
const logger = createLogger({ module: "sessionScopedProviders" });
|
||||
|
||||
export async function SessionScopedProviders({ children }: PropsWithChildren) {
|
||||
const sessionPromise = auth();
|
||||
const userPromise = sessionPromise.then((session) =>
|
||||
session
|
||||
? getRscUserSettingsAsync(session.user.id).catch((error: unknown) => {
|
||||
logger.error(new Error("Failed to load the authenticated user in the root layout", { cause: error }));
|
||||
return null;
|
||||
})
|
||||
: null,
|
||||
);
|
||||
const [session, user, serverSettings] = await Promise.all([
|
||||
sessionPromise,
|
||||
userPromise,
|
||||
getRscServerSettingsAsync(),
|
||||
]);
|
||||
|
||||
const StackedProvider = composeWrappers([
|
||||
(innerProps) => <AuthProvider session={session} logoutUrl={env.AUTH_LOGOUT_REDIRECT_URL} {...innerProps} />,
|
||||
(innerProps) => (
|
||||
<SettingsProvider
|
||||
user={
|
||||
user
|
||||
? {
|
||||
...user,
|
||||
firstDayOfWeek: user.firstDayOfWeek as DayOfWeek,
|
||||
}
|
||||
: null
|
||||
}
|
||||
serverSettings={{
|
||||
board: {
|
||||
homeBoardId: serverSettings.board.homeBoardId,
|
||||
mobileHomeBoardId: serverSettings.board.mobileHomeBoardId,
|
||||
enableStatusByDefault: serverSettings.board.enableStatusByDefault,
|
||||
forceDisableStatus: serverSettings.board.forceDisableStatus,
|
||||
},
|
||||
search: { defaultSearchEngineId: serverSettings.search.defaultSearchEngineId },
|
||||
user: { enableGravatar: serverSettings.user.enableGravatar },
|
||||
}}
|
||||
{...innerProps}
|
||||
/>
|
||||
),
|
||||
(innerProps) => <JotaiProvider {...innerProps} />,
|
||||
(innerProps) => <TRPCReactProvider {...innerProps} />,
|
||||
(innerProps) => <DayJsLoader {...innerProps} />,
|
||||
(innerProps) => <NextIntlClientProvider {...innerProps} />,
|
||||
(innerProps) => <CustomMantineProvider {...innerProps} />,
|
||||
(innerProps) => <ModalProvider {...innerProps} />,
|
||||
(innerProps) => <SpotlightProvider {...innerProps} />,
|
||||
]);
|
||||
|
||||
return (
|
||||
<StackedProvider>
|
||||
<Analytics enabled={serverSettings.analytics.enableGeneral} />
|
||||
<Notifications pauseResetOnHover="notification" />
|
||||
<ServiceWorkerRegistration />
|
||||
{children}
|
||||
</StackedProvider>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Suspense } from "react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Alert, Card, Center, Code, Stack, Text, Title } from "@mantine/core";
|
||||
import { Alert, Card, Center, Code, Loader, Stack, Text, Title } from "@mantine/core";
|
||||
import { IconLogin } from "@tabler/icons-react";
|
||||
|
||||
import { env } from "@homarr/auth/env";
|
||||
@@ -18,8 +19,7 @@ interface LoginProps {
|
||||
}>;
|
||||
}
|
||||
|
||||
export default async function Login(props: LoginProps) {
|
||||
const searchParams = await props.searchParams;
|
||||
async function LoginContent({ searchParams }: { searchParams: { callbackUrl?: string } }) {
|
||||
const session = await auth();
|
||||
|
||||
if (session) {
|
||||
@@ -59,3 +59,19 @@ export default async function Login(props: LoginProps) {
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
|
||||
export default async function Login(props: LoginProps) {
|
||||
const searchParams = await props.searchParams;
|
||||
|
||||
return (
|
||||
<Suspense
|
||||
fallback={
|
||||
<Center h="100vh">
|
||||
<Loader size="lg" />
|
||||
</Center>
|
||||
}
|
||||
>
|
||||
<LoginContent searchParams={searchParams} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Suspense } from "react";
|
||||
import { cache } from "react";
|
||||
import { Center, Loader } from "@mantine/core";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
// Placed here because gridstack styles are used for board content
|
||||
@@ -33,6 +35,52 @@ interface Props<TParams extends Params> {
|
||||
getInitialBoardAsync: (params: TParams) => Promise<Board>;
|
||||
}
|
||||
|
||||
interface BoardContentPageProps<TParams extends Params> {
|
||||
params: TParams;
|
||||
getInitialBoard: (params: TParams) => Promise<Board>;
|
||||
}
|
||||
|
||||
async function BoardContentPageDynamic<TParams extends Params>({
|
||||
params,
|
||||
getInitialBoard,
|
||||
}: BoardContentPageProps<TParams>) {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
const [board, session] = await Promise.all([getInitialBoard(params), auth()]);
|
||||
|
||||
const itemsMap = board.items.reduce((acc, item) => {
|
||||
const existing = acc.get(item.kind);
|
||||
if (existing) {
|
||||
existing.push(item);
|
||||
} else {
|
||||
acc.set(item.kind, [item]);
|
||||
}
|
||||
return acc;
|
||||
}, new Map<WidgetKind, Item[]>());
|
||||
const [integrations] = await Promise.all([
|
||||
getIntegrationsWithPermissionsAsync(session),
|
||||
...Array.from(itemsMap).map(([kind, items]) =>
|
||||
prefetchForKindAsync(kind, queryClient, items).catch((error) => {
|
||||
logger.error(
|
||||
new ErrorWithMetadata(
|
||||
"Failed to prefetch widget",
|
||||
{ widgetKind: kind, itemCount: items.length },
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
]);
|
||||
|
||||
return (
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
<IntegrationProvider integrations={integrations}>
|
||||
<DynamicClientBoard />
|
||||
</IntegrationProvider>
|
||||
</HydrationBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
export const createBoardContentPage = <TParams extends Record<string, unknown>>({
|
||||
getInitialBoardAsync: getInitialBoard,
|
||||
}: Props<TParams>) => {
|
||||
@@ -45,40 +93,17 @@ export const createBoardContentPage = <TParams extends Record<string, unknown>>(
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
page: async ({ params }: { params: Promise<TParams> }) => {
|
||||
const resolvedParams = await params;
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
const [board, session] = await Promise.all([getInitialBoard(resolvedParams), auth()]);
|
||||
|
||||
const itemsMap = board.items.reduce((acc, item) => {
|
||||
const existing = acc.get(item.kind);
|
||||
if (existing) {
|
||||
existing.push(item);
|
||||
} else {
|
||||
acc.set(item.kind, [item]);
|
||||
}
|
||||
return acc;
|
||||
}, new Map<WidgetKind, Item[]>());
|
||||
const [integrations] = await Promise.all([
|
||||
getIntegrationsWithPermissionsAsync(session),
|
||||
...Array.from(itemsMap).map(([kind, items]) =>
|
||||
prefetchForKindAsync(kind, queryClient, items).catch((error) => {
|
||||
logger.error(
|
||||
new ErrorWithMetadata(
|
||||
"Failed to prefetch widget",
|
||||
{ widgetKind: kind, itemCount: items.length },
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
]);
|
||||
|
||||
return (
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
<IntegrationProvider integrations={integrations}>
|
||||
<DynamicClientBoard />
|
||||
</IntegrationProvider>
|
||||
</HydrationBoundary>
|
||||
<Suspense
|
||||
fallback={
|
||||
<Center w="100%" h="100%">
|
||||
<Loader size="xl" />
|
||||
</Center>
|
||||
}
|
||||
>
|
||||
<BoardContentPageDynamic params={resolvedParams} getInitialBoard={getInitialBoard} />
|
||||
</Suspense>
|
||||
);
|
||||
},
|
||||
generateMetadataAsync: async ({ params }: { params: Promise<TParams> }): Promise<Metadata> => {
|
||||
|
||||
@@ -11,8 +11,8 @@ import { useColorSchemeManager } from "../../_client-providers/mantine";
|
||||
|
||||
export const BoardMantineProvider = ({
|
||||
children,
|
||||
defaultColorScheme,
|
||||
}: PropsWithChildren<{ defaultColorScheme: ColorScheme }>) => {
|
||||
defaultColorScheme = "auto",
|
||||
}: PropsWithChildren<{ defaultColorScheme?: ColorScheme }>) => {
|
||||
const board = useRequiredBoard();
|
||||
const colorSchemeManager = useColorSchemeManager();
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Center, Loader } from "@mantine/core";
|
||||
|
||||
export default function BoardLoading() {
|
||||
return (
|
||||
<Center w="100%" h="100vh">
|
||||
<Loader size="xl" />
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { JSX, PropsWithChildren } from "react";
|
||||
import { Suspense } from "react";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { AppShellMain } from "@mantine/core";
|
||||
import { AppShellMain, Skeleton } from "@mantine/core";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { getRscUserSettingsAsync } from "@homarr/api/user-server";
|
||||
@@ -14,7 +15,6 @@ import { BoardLogoWithTitle } from "~/components/layout/logo/board-logo";
|
||||
import { ClientShell } from "~/components/layout/shell";
|
||||
import { BoardTourGate } from "~/components/onboarding/board-tour-gate";
|
||||
import { env } from "~/env";
|
||||
import { getCurrentColorSchemeAsync } from "~/theme/color-scheme";
|
||||
import type { Board } from "./_types";
|
||||
import type { Params } from "./(content)/_creator";
|
||||
import { CustomCss } from "./(content)/_custom-css";
|
||||
@@ -29,6 +29,94 @@ interface CreateBoardLayoutProps<TParams extends Params> {
|
||||
withTour?: boolean;
|
||||
}
|
||||
|
||||
const BoardLayoutFallback = ({ headerActions }: { headerActions: JSX.Element }) => (
|
||||
<ClientShell hasNavigation={false}>
|
||||
<MainHeader logo={<BoardLogoWithTitle size="md" hideTitleOnMobile />} actions={headerActions} hasNavigation={false} />
|
||||
<AppShellMain>
|
||||
<Skeleton height="100%" radius="sm" />
|
||||
</AppShellMain>
|
||||
</ClientShell>
|
||||
);
|
||||
|
||||
interface BoardLayoutDynamicProps<TParams extends Params> {
|
||||
params: TParams;
|
||||
headerActions: JSX.Element;
|
||||
getInitialBoardAsync: (params: TParams) => Promise<Board>;
|
||||
withTour: boolean;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
async function BoardLayoutDynamic<TParams extends Params>({
|
||||
params,
|
||||
headerActions,
|
||||
getInitialBoardAsync: getInitialBoard,
|
||||
withTour,
|
||||
children,
|
||||
}: BoardLayoutDynamicProps<TParams>) {
|
||||
const sessionPromise = auth();
|
||||
const initialBoardPromise = getInitialBoard(params).then(
|
||||
(board) => ({ status: "fulfilled", board }) as const,
|
||||
(error: unknown) => ({ status: "rejected", error }) as const,
|
||||
);
|
||||
const shouldRunBoardTourPromise = sessionPromise.then(async (session) => {
|
||||
if (!withTour || !session || env.DEMO_MODE) return false;
|
||||
|
||||
try {
|
||||
const user = await getRscUserSettingsAsync(session.user.id);
|
||||
return user !== undefined && !user.completedBoardTour;
|
||||
} catch (error) {
|
||||
logger.error(new Error("Failed to load the board tour status", { cause: error }));
|
||||
return false;
|
||||
}
|
||||
});
|
||||
const [session, initialBoardResult, shouldRunBoardTour] = await Promise.all([
|
||||
sessionPromise,
|
||||
initialBoardPromise,
|
||||
shouldRunBoardTourPromise,
|
||||
]);
|
||||
if (initialBoardResult.status === "rejected") {
|
||||
const { error } = initialBoardResult;
|
||||
if (error instanceof TRPCError && error.code === "NOT_FOUND") {
|
||||
if (!session) {
|
||||
logger.debug("No home board found for anonymous user, redirecting to login");
|
||||
redirect("/auth/login");
|
||||
}
|
||||
|
||||
logger.warn(error);
|
||||
notFound();
|
||||
}
|
||||
|
||||
if (error instanceof TRPCError && error.code === "BAD_REQUEST") {
|
||||
notFound();
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
const initialBoard = initialBoardResult.board;
|
||||
|
||||
return (
|
||||
<BoardProvider initialBoard={initialBoard}>
|
||||
<BoardReadyProvider>
|
||||
<EditModeProvider>
|
||||
<BoardMantineProvider>
|
||||
<CustomCss />
|
||||
<BoardTourGate enabled={shouldRunBoardTour}>
|
||||
<ClientShell hasNavigation={false}>
|
||||
<MainHeader
|
||||
logo={<BoardLogoWithTitle size="md" hideTitleOnMobile />}
|
||||
actions={headerActions}
|
||||
hasNavigation={false}
|
||||
/>
|
||||
<AppShellMain>{children}</AppShellMain>
|
||||
</ClientShell>
|
||||
</BoardTourGate>
|
||||
</BoardMantineProvider>
|
||||
</EditModeProvider>
|
||||
</BoardReadyProvider>
|
||||
</BoardProvider>
|
||||
);
|
||||
}
|
||||
|
||||
export const createBoardLayout = <TParams extends Params>({
|
||||
headerActions,
|
||||
getInitialBoardAsync: getInitialBoard,
|
||||
@@ -41,69 +129,18 @@ export const createBoardLayout = <TParams extends Params>({
|
||||
params: Promise<TParams>;
|
||||
}>) => {
|
||||
const resolvedParams = await params;
|
||||
const sessionPromise = auth();
|
||||
const initialBoardPromise = getInitialBoard(resolvedParams).then(
|
||||
(board) => ({ status: "fulfilled", board }) as const,
|
||||
(error: unknown) => ({ status: "rejected", error }) as const,
|
||||
);
|
||||
const colorSchemePromise = getCurrentColorSchemeAsync();
|
||||
const shouldRunBoardTourPromise = sessionPromise.then(async (session) => {
|
||||
if (!withTour || !session || env.DEMO_MODE) return false;
|
||||
|
||||
try {
|
||||
const user = await getRscUserSettingsAsync(session.user.id);
|
||||
return user !== undefined && !user.completedBoardTour;
|
||||
} catch (error) {
|
||||
logger.error(new Error("Failed to load the board tour status", { cause: error }));
|
||||
return false;
|
||||
}
|
||||
});
|
||||
const [session, initialBoardResult, colorScheme, shouldRunBoardTour] = await Promise.all([
|
||||
sessionPromise,
|
||||
initialBoardPromise,
|
||||
colorSchemePromise,
|
||||
shouldRunBoardTourPromise,
|
||||
]);
|
||||
if (initialBoardResult.status === "rejected") {
|
||||
const { error } = initialBoardResult;
|
||||
if (error instanceof TRPCError && error.code === "NOT_FOUND") {
|
||||
if (!session) {
|
||||
logger.debug("No home board found for anonymous user, redirecting to login");
|
||||
redirect("/auth/login");
|
||||
}
|
||||
|
||||
logger.warn(error);
|
||||
notFound();
|
||||
}
|
||||
|
||||
if (error instanceof TRPCError && error.code === "BAD_REQUEST") {
|
||||
notFound();
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
const initialBoard = initialBoardResult.board;
|
||||
|
||||
return (
|
||||
<BoardProvider initialBoard={initialBoard}>
|
||||
<BoardReadyProvider>
|
||||
<EditModeProvider>
|
||||
<BoardMantineProvider defaultColorScheme={colorScheme}>
|
||||
<CustomCss />
|
||||
<BoardTourGate enabled={shouldRunBoardTour}>
|
||||
<ClientShell hasNavigation={false}>
|
||||
<MainHeader
|
||||
logo={<BoardLogoWithTitle size="md" hideTitleOnMobile />}
|
||||
actions={headerActions}
|
||||
hasNavigation={false}
|
||||
/>
|
||||
<AppShellMain>{children}</AppShellMain>
|
||||
</ClientShell>
|
||||
</BoardTourGate>
|
||||
</BoardMantineProvider>
|
||||
</EditModeProvider>
|
||||
</BoardReadyProvider>
|
||||
</BoardProvider>
|
||||
<Suspense fallback={<BoardLayoutFallback headerActions={headerActions} />}>
|
||||
<BoardLayoutDynamic
|
||||
params={resolvedParams}
|
||||
headerActions={headerActions}
|
||||
getInitialBoardAsync={getInitialBoard}
|
||||
withTour={withTour}
|
||||
>
|
||||
{children}
|
||||
</BoardLayoutDynamic>
|
||||
</Suspense>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { Metadata, Viewport } from "next";
|
||||
import { Suspense } from "react";
|
||||
import { Inter } from "next/font/google";
|
||||
import { ColorSchemeScript } from "@mantine/core";
|
||||
|
||||
import "@gfazioli/mantine-onboarding-tour/styles.css";
|
||||
import "@homarr/notifications/styles.css";
|
||||
@@ -10,42 +12,19 @@ import "~/styles/color-scheme.scss";
|
||||
import "~/styles/scroll-area.scss";
|
||||
|
||||
import { notFound } from "next/navigation";
|
||||
import type { DayOfWeek } from "@mantine/dates";
|
||||
import { NextIntlClientProvider } from "next-intl";
|
||||
|
||||
import { getRscServerSettingsAsync } from "@homarr/api/server-settings-server";
|
||||
import { getRscUserSettingsAsync } from "@homarr/api/user-server";
|
||||
import { env } from "@homarr/auth/env";
|
||||
import { auth } from "@homarr/auth/next";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ModalProvider } from "@homarr/modals";
|
||||
import { Notifications } from "@homarr/notifications";
|
||||
import { SettingsProvider } from "@homarr/settings";
|
||||
import { SpotlightProvider } from "@homarr/spotlight";
|
||||
import type { SupportedLanguage } from "@homarr/translation";
|
||||
import { isLocaleRTL, isLocaleSupported } from "@homarr/translation";
|
||||
|
||||
import { Analytics } from "~/components/layout/analytics";
|
||||
import { CrowdinLiveTranslation } from "~/components/layout/crowdin-live-translation";
|
||||
|
||||
import { SearchEngineOptimization } from "~/components/layout/search-engine-optimization";
|
||||
import { ServiceWorkerRegistration } from "~/components/layout/service-worker-registration";
|
||||
import { getCurrentColorSchemeAsync } from "~/theme/color-scheme";
|
||||
import { DayJsLoader } from "./_client-providers/dayjs-loader";
|
||||
import { JotaiProvider } from "./_client-providers/jotai";
|
||||
import { CustomMantineProvider } from "./_client-providers/mantine";
|
||||
import { AuthProvider } from "./_client-providers/session";
|
||||
import { TRPCReactProvider } from "./_client-providers/trpc";
|
||||
import { composeWrappers } from "./compose";
|
||||
import { CrowdinLiveTranslation } from "~/components/layout/crowdin-live-translation";
|
||||
import { SessionScopedProviders } from "./_session-scoped-providers";
|
||||
|
||||
const fontSans = Inter({
|
||||
subsets: ["latin"],
|
||||
variable: "--font-sans",
|
||||
});
|
||||
|
||||
const logger = createLogger({ module: "rootLayout" });
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
export const generateMetadata = async (): Promise<Metadata> => ({
|
||||
title: "Homarr",
|
||||
description:
|
||||
@@ -65,7 +44,7 @@ export const generateMetadata = async (): Promise<Metadata> => ({
|
||||
title: "Homarr",
|
||||
capable: true,
|
||||
startupImage: { url: "/logo/logo.png" },
|
||||
statusBarStyle: (await getCurrentColorSchemeAsync()) === "dark" ? "black-translucent" : "default",
|
||||
statusBarStyle: "default",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -85,82 +64,19 @@ export default async function Layout(props: {
|
||||
notFound();
|
||||
}
|
||||
|
||||
const sessionPromise = auth();
|
||||
const userPromise = sessionPromise.then((session) =>
|
||||
session
|
||||
? getRscUserSettingsAsync(session.user.id).catch((error: unknown) => {
|
||||
logger.error(new Error("Failed to load the authenticated user in the root layout", { cause: error }));
|
||||
return null;
|
||||
})
|
||||
: null,
|
||||
);
|
||||
const [session, user, serverSettings, colorScheme] = await Promise.all([
|
||||
sessionPromise,
|
||||
userPromise,
|
||||
getRscServerSettingsAsync(),
|
||||
getCurrentColorSchemeAsync(),
|
||||
]);
|
||||
const direction = isLocaleRTL(locale) ? "rtl" : "ltr";
|
||||
|
||||
const StackedProvider = composeWrappers([
|
||||
(innerProps) => {
|
||||
return <AuthProvider session={session} logoutUrl={env.AUTH_LOGOUT_REDIRECT_URL} {...innerProps} />;
|
||||
},
|
||||
(innerProps) => (
|
||||
<SettingsProvider
|
||||
user={
|
||||
user
|
||||
? {
|
||||
...user,
|
||||
// Convert type, because output schema is not smart enough to infer $type from drizzle
|
||||
firstDayOfWeek: user.firstDayOfWeek as DayOfWeek,
|
||||
}
|
||||
: null
|
||||
}
|
||||
serverSettings={{
|
||||
board: {
|
||||
homeBoardId: serverSettings.board.homeBoardId,
|
||||
mobileHomeBoardId: serverSettings.board.mobileHomeBoardId,
|
||||
enableStatusByDefault: serverSettings.board.enableStatusByDefault,
|
||||
forceDisableStatus: serverSettings.board.forceDisableStatus,
|
||||
},
|
||||
search: { defaultSearchEngineId: serverSettings.search.defaultSearchEngineId },
|
||||
user: { enableGravatar: serverSettings.user.enableGravatar },
|
||||
}}
|
||||
{...innerProps}
|
||||
/>
|
||||
),
|
||||
(innerProps) => <JotaiProvider {...innerProps} />,
|
||||
(innerProps) => <TRPCReactProvider {...innerProps} />,
|
||||
(innerProps) => <DayJsLoader {...innerProps} />,
|
||||
(innerProps) => <NextIntlClientProvider {...innerProps} />,
|
||||
(innerProps) => <CustomMantineProvider {...innerProps} defaultColorScheme={colorScheme} />,
|
||||
(innerProps) => <ModalProvider {...innerProps} />,
|
||||
(innerProps) => <SpotlightProvider {...innerProps} />,
|
||||
]);
|
||||
|
||||
return (
|
||||
// Instead of ColorSchemScript we use data-mantine-color-scheme to prevent flickering
|
||||
<html
|
||||
lang={locale}
|
||||
dir={direction}
|
||||
data-mantine-color-scheme={colorScheme}
|
||||
style={{
|
||||
backgroundColor: colorScheme === "dark" ? "#242424" : colorScheme === "auto" ? undefined : "#fff",
|
||||
}}
|
||||
suppressHydrationWarning
|
||||
>
|
||||
<html lang={locale} dir={direction} suppressHydrationWarning>
|
||||
<head>
|
||||
<ColorSchemeScript />
|
||||
<SearchEngineOptimization />
|
||||
<CrowdinLiveTranslation locale={locale} />
|
||||
</head>
|
||||
<body className={[fontSans.className, fontSans.variable].join(" ")} suppressHydrationWarning>
|
||||
<Analytics enabled={serverSettings.analytics.enableGeneral} />
|
||||
<StackedProvider>
|
||||
<Notifications pauseResetOnHover="notification" />
|
||||
<ServiceWorkerRegistration />
|
||||
{props.children}
|
||||
</StackedProvider>
|
||||
<Suspense>
|
||||
<SessionScopedProviders>{props.children}</SessionScopedProviders>
|
||||
</Suspense>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { PropsWithChildren } from "react";
|
||||
import { AppShellMain } from "@mantine/core";
|
||||
import { Suspense } from "react";
|
||||
import { AppShellMain, Skeleton } from "@mantine/core";
|
||||
import {
|
||||
IconAffiliateFilled,
|
||||
IconApi,
|
||||
@@ -46,7 +47,17 @@ import { env as nextEnv } from "~/env";
|
||||
|
||||
const logger = createLogger({ module: "manageLayout" });
|
||||
|
||||
export default async function ManageLayout({ children }: PropsWithChildren) {
|
||||
const ManageLayoutFallback = () => (
|
||||
<ClientShell hasNavigation>
|
||||
<MainHeader />
|
||||
<MainNavigation links={[]} />
|
||||
<AppShellMain>
|
||||
<Skeleton height="100%" radius="sm" />
|
||||
</AppShellMain>
|
||||
</ClientShell>
|
||||
);
|
||||
|
||||
async function ManageLayoutContent({ children }: PropsWithChildren) {
|
||||
const sessionPromise = auth();
|
||||
const shouldRunManageTourPromise = sessionPromise.then(async (session) => {
|
||||
if (!session || nextEnv.DEMO_MODE) return false;
|
||||
@@ -237,17 +248,21 @@ export default async function ManageLayout({ children }: PropsWithChildren) {
|
||||
|
||||
const isAdmin = session?.user.permissions.includes("admin") ?? false;
|
||||
|
||||
const shell = (
|
||||
<ClientShell hasNavigation>
|
||||
<MainHeader></MainHeader>
|
||||
<MainNavigation links={navigationLinks}></MainNavigation>
|
||||
<AppShellMain>{children}</AppShellMain>
|
||||
</ClientShell>
|
||||
);
|
||||
|
||||
return (
|
||||
<ManageTourGate enabled={shouldRunManageTour} isAdmin={isAdmin}>
|
||||
{shell}
|
||||
<ClientShell hasNavigation>
|
||||
<MainHeader />
|
||||
<MainNavigation links={navigationLinks} />
|
||||
<AppShellMain>{children}</AppShellMain>
|
||||
</ClientShell>
|
||||
</ManageTourGate>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ManageLayout({ children }: PropsWithChildren) {
|
||||
return (
|
||||
<Suspense fallback={<ManageLayoutFallback />}>
|
||||
<ManageLayoutContent>{children}</ManageLayoutContent>
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Center, Loader } from "@mantine/core";
|
||||
|
||||
export default function ManageLoading() {
|
||||
return (
|
||||
<Center w="100%" h="100vh">
|
||||
<Loader size="lg" />
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
@@ -11,6 +11,10 @@ interface Props {
|
||||
params: Promise<{ kind: string }>;
|
||||
}
|
||||
|
||||
export function generateStaticParams() {
|
||||
return widgetKinds.map((kind) => ({ kind }));
|
||||
}
|
||||
|
||||
export default async function WidgetPreview(props: Props) {
|
||||
const { kind } = await props.params;
|
||||
if (!widgetKinds.includes(kind as WidgetKind)) {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import DOMPurify from "isomorphic-dompurify";
|
||||
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { medias } from "@homarr/db/schema";
|
||||
|
||||
import { sanitizeSvg } from "../svg-purify";
|
||||
|
||||
export async function GET(_req: NextRequest, props: { params: Promise<{ id: string }> }) {
|
||||
const params = await props.params;
|
||||
const image = await db.query.medias.findFirst({
|
||||
@@ -25,9 +26,7 @@ export async function GET(_req: NextRequest, props: { params: Promise<{ id: stri
|
||||
// Sanitize SVG content to prevent XSS attacks
|
||||
if (image.contentType === "image/svg+xml" || image.contentType === "image/svg") {
|
||||
const svgText = new TextDecoder().decode(content);
|
||||
const sanitized = DOMPurify.sanitize(svgText, {
|
||||
USE_PROFILES: { svg: true, svgFilters: true },
|
||||
});
|
||||
const sanitized = sanitizeSvg(svgText);
|
||||
content = new TextEncoder().encode(sanitized);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { purify, sanitizeSvg, svgSanitizeOptions } from "./svg-purify";
|
||||
|
||||
describe("SVG sanitization", () => {
|
||||
it("strips script tags from SVG", () => {
|
||||
const dirty = '<svg><script>alert("xss")</script><circle r="10"/></svg>';
|
||||
const clean = sanitizeSvg(dirty);
|
||||
expect(clean).not.toContain("<script");
|
||||
expect(clean).toContain("circle");
|
||||
});
|
||||
|
||||
it("strips onload event handler", () => {
|
||||
const dirty = '<svg onload="alert(1)"><rect width="100" height="100"/></svg>';
|
||||
const clean = sanitizeSvg(dirty);
|
||||
expect(clean).not.toContain("onload");
|
||||
});
|
||||
|
||||
it("strips data URI in use href", () => {
|
||||
const dirty = '<svg><use href="data:text/html,<script>alert(1)</script>"/></svg>';
|
||||
const clean = sanitizeSvg(dirty);
|
||||
expect(clean).not.toContain("data:");
|
||||
});
|
||||
|
||||
it("strips foreignObject", () => {
|
||||
const dirty =
|
||||
'<svg><foreignObject><body xmlns="http://www.w3.org/1999/xhtml"><script>alert(1)</script></body></foreignObject></svg>';
|
||||
const clean = sanitizeSvg(dirty);
|
||||
expect(clean).not.toContain("foreignObject");
|
||||
});
|
||||
|
||||
it("preserves safe SVG content", () => {
|
||||
const safe = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="red"/></svg>';
|
||||
const clean = sanitizeSvg(safe);
|
||||
expect(clean).toContain("circle");
|
||||
expect(clean).toContain('fill="red"');
|
||||
});
|
||||
|
||||
it("uses the same DOMPurify options as production", () => {
|
||||
const dirty = '<svg><script>alert("xss")</script></svg>';
|
||||
const wrapped = `<html><body>${dirty}</body></html>`;
|
||||
expect(sanitizeSvg(dirty)).toBe(purify.sanitize(wrapped, svgSanitizeOptions));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
import createDOMPurify from "dompurify";
|
||||
import { parseHTML } from "linkedom";
|
||||
|
||||
const svgSanitizeOptions = {
|
||||
USE_PROFILES: { svg: true, svgFilters: true },
|
||||
} as const;
|
||||
|
||||
// ponytail: linkedom stores DOM tree pointers as own value properties; DOMPurify
|
||||
// resolves them via prototype getters (lookupGetter). Without this shim, sanitize
|
||||
// is a no-op and XSS payloads pass through unchanged.
|
||||
const patchLinkedomNodePrototype = (Node: typeof window.Node) => {
|
||||
for (const prop of [
|
||||
"ownerDocument",
|
||||
"parentNode",
|
||||
"nextSibling",
|
||||
"previousSibling",
|
||||
"nodeName",
|
||||
"nodeType",
|
||||
"childNodes",
|
||||
] as const) {
|
||||
if (Object.getOwnPropertyDescriptor(Node.prototype, prop)?.get) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Object.getOwnPropertyDescriptor(Node.prototype, prop)?.value !== undefined) {
|
||||
delete Node.prototype[prop as keyof Node];
|
||||
}
|
||||
|
||||
Object.defineProperty(Node.prototype, prop, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
return Object.getOwnPropertyDescriptor(this, prop)?.value ?? null;
|
||||
},
|
||||
set(value: unknown) {
|
||||
Object.defineProperty(this, prop, {
|
||||
value,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const createDocument = () => parseHTML("<!doctype html><html><head></head><body></body></html>");
|
||||
|
||||
const { window, document, Node } = createDocument();
|
||||
patchLinkedomNodePrototype(Node);
|
||||
|
||||
if (!document.implementation?.createHTMLDocument) {
|
||||
Object.defineProperty(document, "implementation", {
|
||||
value: {
|
||||
createHTMLDocument() {
|
||||
return createDocument().document;
|
||||
},
|
||||
createDocument() {
|
||||
return createDocument().document;
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
window.NodeFilter ??= {
|
||||
FILTER_ACCEPT: 1,
|
||||
FILTER_REJECT: 2,
|
||||
FILTER_SKIP: 3,
|
||||
SHOW_ALL: 0xffffffff,
|
||||
SHOW_ELEMENT: 1,
|
||||
SHOW_ATTRIBUTE: 2,
|
||||
SHOW_TEXT: 4,
|
||||
SHOW_CDATA_SECTION: 8,
|
||||
SHOW_PROCESSING_INSTRUCTION: 64,
|
||||
SHOW_COMMENT: 128,
|
||||
SHOW_DOCUMENT: 0x100,
|
||||
SHOW_DOCUMENT_TYPE: 0x200,
|
||||
SHOW_DOCUMENT_FRAGMENT: 0x400,
|
||||
SHOW_NOTATION: 0x800,
|
||||
} as typeof window.NodeFilter;
|
||||
|
||||
const purify = createDOMPurify(window as unknown as Parameters<typeof createDOMPurify>[0]);
|
||||
|
||||
export const sanitizeSvg = (svgText: string) => {
|
||||
// linkedom's DOMParser omits html/body for fragment-only SVG; DOMPurify needs a body root.
|
||||
const wrapped = `<html><body>${svgText}</body></html>`;
|
||||
return purify.sanitize(wrapped, svgSanitizeOptions);
|
||||
};
|
||||
|
||||
export { svgSanitizeOptions, purify };
|
||||
@@ -10,6 +10,8 @@
|
||||
".": "./src/index.ts",
|
||||
"./client": "./src/client.ts",
|
||||
"./board-server": "./src/board-server.ts",
|
||||
"./cache-invalidation": "./src/cache-invalidation.ts",
|
||||
"./cache-tags": "./src/cache-tags.ts",
|
||||
"./query-cache": "./src/query-cache.ts",
|
||||
"./server-settings-server": "./src/server-settings-server.ts",
|
||||
"./user-server": "./src/user-server.ts",
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { revalidateTag } from "next/cache";
|
||||
|
||||
import { cacheTags } from "./cache-tags";
|
||||
import { invalidateIntegrationDataCache } from "./integration-data-cache";
|
||||
|
||||
export const invalidateBoardCache = (boardId: string, boardName?: string) => {
|
||||
revalidateTag(cacheTags.board(boardId), "max");
|
||||
if (boardName) revalidateTag(cacheTags.boardByName(boardName), "max");
|
||||
revalidateTag(cacheTags.boardList(), "max");
|
||||
};
|
||||
|
||||
export const invalidateIntegrationCache = (integrationId: string) => {
|
||||
invalidateIntegrationDataCache(integrationId);
|
||||
revalidateTag(cacheTags.integration(integrationId), "max");
|
||||
};
|
||||
|
||||
export const invalidateUserCache = (userId: string) => {
|
||||
revalidateTag(cacheTags.user(userId), "max");
|
||||
};
|
||||
|
||||
export const invalidateServerSettingsCache = () => {
|
||||
revalidateTag(cacheTags.serverSettings(), "max");
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { cacheTags } from "./cache-tags";
|
||||
|
||||
describe("cacheTags", () => {
|
||||
it("produces stable tag shapes", () => {
|
||||
expect(cacheTags.serverSettings()).toBe("server-settings");
|
||||
expect(cacheTags.board("abc")).toBe("board:abc");
|
||||
expect(cacheTags.boardByName("My Board")).toBe("board-name:MY BOARD");
|
||||
expect(cacheTags.boardList()).toBe("board-list");
|
||||
expect(cacheTags.user("u1")).toBe("user:u1");
|
||||
expect(cacheTags.integration("i1")).toBe("integration:i1");
|
||||
expect(cacheTags.integrationData("i1", "downloads")).toBe("integration-data:i1:downloads");
|
||||
expect(cacheTags.app("a1")).toBe("app:a1");
|
||||
});
|
||||
|
||||
it("boardByName uppercases consistently", () => {
|
||||
expect(cacheTags.boardByName("home")).toBe(cacheTags.boardByName("HOME"));
|
||||
expect(cacheTags.boardByName("Home")).toBe(cacheTags.boardByName("hOmE"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
export const cacheTags = {
|
||||
serverSettings: () => "server-settings" as const,
|
||||
board: (boardId: string) => `board:${boardId}` as const,
|
||||
boardByName: (name: string) => `board-name:${name.toUpperCase()}` as const,
|
||||
boardList: () => "board-list" as const,
|
||||
user: (userId: string) => `user:${userId}` as const,
|
||||
integration: (integrationId: string) => `integration:${integrationId}` as const,
|
||||
integrationData: (integrationId: string, query: string) => `integration-data:${integrationId}:${query}` as const,
|
||||
app: (appId: string) => `app:${appId}` as const,
|
||||
} as const;
|
||||
@@ -0,0 +1,80 @@
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { getCachedIntegrationData, invalidateIntegrationDataCache } from "./integration-data-cache";
|
||||
|
||||
afterEach(() => {
|
||||
invalidateIntegrationDataCache("integration-a");
|
||||
invalidateIntegrationDataCache("integration-b");
|
||||
});
|
||||
|
||||
describe("getCachedIntegrationData", () => {
|
||||
test("returns cached data within TTL without refetching", async () => {
|
||||
const fetcher = vi.fn(async () => ({ value: 1 }));
|
||||
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:procedure", fetcher, 60_000)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:procedure", fetcher, 60_000)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("serves stale data while revalidating after TTL", async () => {
|
||||
vi.useFakeTimers();
|
||||
const fetcher = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ value: 1 })
|
||||
.mockImplementationOnce(() => new Promise((resolve) => setTimeout(() => resolve({ value: 2 }), 100)));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50);
|
||||
vi.advanceTimersByTime(60);
|
||||
|
||||
const staleResult = await getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50);
|
||||
expect(staleResult).toEqual({ value: 1 });
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
|
||||
vi.advanceTimersByTime(100);
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50)).resolves.toEqual({ value: 2 });
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("returns stale data when refetch fails", async () => {
|
||||
vi.useFakeTimers();
|
||||
const fetcher = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ value: 1 })
|
||||
.mockRejectedValueOnce(new Error("upstream unavailable"));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:error", fetcher, 50);
|
||||
vi.advanceTimersByTime(60);
|
||||
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:error", fetcher, 50)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("invalidates all cache entries for an integration", async () => {
|
||||
const fetcherOne = vi.fn(async () => ({ value: "one" }));
|
||||
const fetcherTwo = vi.fn(async () => ({ value: "two" }));
|
||||
const fetcherOther = vi.fn(async () => ({ value: "other" }));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:one", fetcherOne);
|
||||
await getCachedIntegrationData("integration-a", "widget:two", fetcherTwo);
|
||||
await getCachedIntegrationData("integration-b", "widget:one", fetcherOther);
|
||||
|
||||
invalidateIntegrationDataCache("integration-a");
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:one", fetcherOne);
|
||||
await getCachedIntegrationData("integration-a", "widget:two", fetcherTwo);
|
||||
await getCachedIntegrationData("integration-b", "widget:one", fetcherOther);
|
||||
|
||||
expect(fetcherOne).toHaveBeenCalledTimes(2);
|
||||
expect(fetcherTwo).toHaveBeenCalledTimes(2);
|
||||
expect(fetcherOther).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
// ponytail: module-level cache — one snapshot per integration per query key.
|
||||
// Ceiling: stale data for up to `ttlMs` (60s default). Upgrade: Redis-backed with pub/sub invalidation.
|
||||
|
||||
interface CachedEntry<T> {
|
||||
data: T;
|
||||
timestamp: number;
|
||||
pending?: Promise<T>;
|
||||
}
|
||||
|
||||
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}`;
|
||||
|
||||
export const getCachedIntegrationData = async <T>(
|
||||
integrationId: string,
|
||||
queryKey: string,
|
||||
fetcher: () => Promise<T>,
|
||||
ttlMs = DEFAULT_TTL_MS,
|
||||
): Promise<T> => {
|
||||
const key = cacheKey(integrationId, queryKey);
|
||||
const existing = cache.get(key) as CachedEntry<T> | undefined;
|
||||
const now = Date.now();
|
||||
|
||||
if (existing) {
|
||||
if (now - existing.timestamp < ttlMs) {
|
||||
return existing.data;
|
||||
}
|
||||
if (existing.pending) {
|
||||
return existing.pending;
|
||||
}
|
||||
}
|
||||
|
||||
const pending = fetcher()
|
||||
.then((data) => {
|
||||
cache.set(key, { data, timestamp: Date.now() });
|
||||
|
||||
if (cache.size > MAX_ENTRIES) {
|
||||
let oldest: string | undefined;
|
||||
let oldestTime = Infinity;
|
||||
for (const [k, v] of cache) {
|
||||
if (v.timestamp < oldestTime) {
|
||||
oldestTime = v.timestamp;
|
||||
oldest = k;
|
||||
}
|
||||
}
|
||||
if (oldest) cache.delete(oldest);
|
||||
}
|
||||
|
||||
return data;
|
||||
})
|
||||
.catch((error) => {
|
||||
const entry = cache.get(key) as CachedEntry<T> | undefined;
|
||||
if (entry) {
|
||||
delete entry.pending;
|
||||
if (entry.timestamp > 0) return entry.data;
|
||||
}
|
||||
cache.delete(key);
|
||||
throw error;
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
existing.pending = pending;
|
||||
return existing.data;
|
||||
}
|
||||
|
||||
cache.set(key, { data: undefined as T, timestamp: 0, pending });
|
||||
return pending;
|
||||
};
|
||||
|
||||
export const invalidateIntegrationDataCache = (integrationId: string) => {
|
||||
for (const [key] of cache) {
|
||||
if (key.startsWith(`${integrationId}:`)) {
|
||||
cache.delete(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -5,7 +5,7 @@ import type { Database } from "@homarr/db";
|
||||
import { eq, or } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src";
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src/definition";
|
||||
|
||||
export class AppAccessControl {
|
||||
constructor(
|
||||
|
||||
@@ -59,6 +59,7 @@ import { zodUnionFromArray } from "@homarr/validation/enums";
|
||||
import type { BoardItemAdvancedOptions } from "@homarr/validation/shared";
|
||||
import { sectionSchema, sharedItemSchema } from "@homarr/validation/shared";
|
||||
|
||||
import { invalidateBoardCache, invalidateUserCache } from "../cache-invalidation";
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publicProcedure } from "../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "./board/board-access";
|
||||
import { generateResponsiveGridFor } from "./board/grid-algorithm";
|
||||
@@ -327,6 +328,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await ctx.db.update(users).set({ homeBoardId: boardId }).where(eq(users.id, ctx.session.user.id));
|
||||
}
|
||||
|
||||
invalidateBoardCache(boardId, input.name);
|
||||
|
||||
return { boardId };
|
||||
}),
|
||||
duplicateBoard: permissionRequiredProcedure
|
||||
@@ -555,6 +558,8 @@ export const boardRouter = createTRPCRouter({
|
||||
},
|
||||
});
|
||||
|
||||
invalidateBoardCache(newBoardId, input.name);
|
||||
|
||||
return { boardId: newBoardId };
|
||||
}),
|
||||
renameBoard: protectedProcedure
|
||||
@@ -573,7 +578,17 @@ export const boardRouter = createTRPCRouter({
|
||||
|
||||
await noBoardWithSimilarNameAsync(ctx.db, input.name, [input.id]);
|
||||
|
||||
const existingBoard = await ctx.db.query.boards.findFirst({
|
||||
where: eq(boards.id, input.id),
|
||||
columns: { name: true },
|
||||
});
|
||||
|
||||
await ctx.db.update(boards).set({ name: input.name }).where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id, input.name);
|
||||
if (existingBoard && existingBoard.name !== input.name) {
|
||||
invalidateBoardCache(input.id, existingBoard.name);
|
||||
}
|
||||
}),
|
||||
changeBoardVisibility: protectedProcedure
|
||||
.meta({
|
||||
@@ -604,6 +619,8 @@ export const boardRouter = createTRPCRouter({
|
||||
.update(boards)
|
||||
.set({ isPublic: input.visibility === "public" })
|
||||
.where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id);
|
||||
}),
|
||||
deleteBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -619,7 +636,14 @@ export const boardRouter = createTRPCRouter({
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "full");
|
||||
|
||||
const existingBoard = await ctx.db.query.boards.findFirst({
|
||||
where: eq(boards.id, input.id),
|
||||
columns: { name: true },
|
||||
});
|
||||
|
||||
await ctx.db.delete(boards).where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id, existingBoard?.name);
|
||||
}),
|
||||
setHomeBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -636,6 +660,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "view");
|
||||
|
||||
await ctx.db.update(users).set({ homeBoardId: input.id }).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
setMobileHomeBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -652,6 +678,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "view");
|
||||
|
||||
await ctx.db.update(users).set({ mobileHomeBoardId: input.id }).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
getHomeBoard: publicProcedure.query(async ({ ctx }) => {
|
||||
const userId = ctx.session?.user.id;
|
||||
@@ -806,6 +834,8 @@ export const boardRouter = createTRPCRouter({
|
||||
if (removedLayoutIds.length > 0) {
|
||||
await ctx.db.delete(layouts).where(inArray(layouts.id, removedLayoutIds));
|
||||
}
|
||||
|
||||
invalidateBoardCache(board.id, board.name);
|
||||
}),
|
||||
savePartialBoardSettings: protectedProcedure
|
||||
.meta({
|
||||
@@ -850,6 +880,8 @@ export const boardRouter = createTRPCRouter({
|
||||
disableStatus: input.disableStatus,
|
||||
})
|
||||
.where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id);
|
||||
}),
|
||||
saveBoard: protectedProcedure.input(boardSaveSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "modify");
|
||||
@@ -1266,6 +1298,8 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
invalidateBoardCache(dbBoard.id, dbBoard.name);
|
||||
}),
|
||||
getBoardPermissions: protectedProcedure.input(byIdSchema).query(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "full");
|
||||
@@ -1342,6 +1376,11 @@ export const boardRouter = createTRPCRouter({
|
||||
saveUserBoardPermissions: protectedProcedure.input(boardSavePermissionsSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.entityId), "full");
|
||||
|
||||
const existingUserPermissions = await ctx.db.query.boardUserPermissions.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(boardUserPermissions.boardId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -1377,10 +1416,25 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const userIds = [
|
||||
...new Set([
|
||||
...existingUserPermissions.map((permission) => permission.userId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
for (const userId of userIds) {
|
||||
invalidateUserCache(userId);
|
||||
}
|
||||
}),
|
||||
saveGroupBoardPermissions: protectedProcedure.input(boardSavePermissionsSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.entityId), "full");
|
||||
|
||||
const existingGroupPermissions = await ctx.db.query.boardGroupPermissions.findMany({
|
||||
columns: { groupId: true },
|
||||
where: eq(boardGroupPermissions.boardId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -1418,6 +1472,22 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const groupIds = [
|
||||
...new Set([
|
||||
...existingGroupPermissions.map((permission) => permission.groupId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
if (groupIds.length > 0) {
|
||||
const groupMembersForBoard = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: inArray(groupMembers.groupId, groupIds),
|
||||
});
|
||||
for (const member of groupMembersForBoard) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}
|
||||
}),
|
||||
importOldmarrConfig: permissionRequiredProcedure
|
||||
.requiresPermission("board-create")
|
||||
@@ -1554,6 +1624,8 @@ export const boardRouter = createTRPCRouter({
|
||||
.values(input.integrationIds.map((integrationId) => ({ itemId, integrationId })));
|
||||
}
|
||||
|
||||
invalidateBoardCache(input.boardId, board.name);
|
||||
|
||||
return { itemId };
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
} from "@homarr/validation/group";
|
||||
|
||||
import { createTRPCRouter, onboardingProcedure, permissionRequiredProcedure, protectedProcedure } from "../trpc";
|
||||
import { invalidateUserCache } from "../cache-invalidation";
|
||||
import { nextOnboardingStepAsync } from "./onboard/onboard-queries";
|
||||
|
||||
export const groupRouter = createTRPCRouter({
|
||||
@@ -245,6 +246,14 @@ export const groupRouter = createTRPCRouter({
|
||||
mobileHomeBoardId: input.settings.mobileHomeBoardId,
|
||||
})
|
||||
.where(eq(groups.id, input.id));
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.id),
|
||||
});
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
savePositions: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -275,6 +284,11 @@ export const groupRouter = createTRPCRouter({
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfGroupNotFoundAsync(ctx.db, input.groupId);
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.groupId),
|
||||
});
|
||||
|
||||
await ctx.db.delete(groupPermissions).where(eq(groupPermissions.groupId, input.groupId));
|
||||
|
||||
if (input.permissions.length > 0) {
|
||||
@@ -285,6 +299,10 @@ export const groupRouter = createTRPCRouter({
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
transferOwnership: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -307,7 +325,16 @@ export const groupRouter = createTRPCRouter({
|
||||
await throwIfGroupNotFoundAsync(ctx.db, input.id);
|
||||
await throwIfGroupNameIsReservedAsync(ctx.db, input.id);
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.id),
|
||||
});
|
||||
|
||||
await ctx.db.delete(groups).where(eq(groups.id, input.id));
|
||||
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
addMember: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -339,6 +366,8 @@ export const groupRouter = createTRPCRouter({
|
||||
groupId: input.groupId,
|
||||
userId: input.userId,
|
||||
});
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
removeMember: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -351,6 +380,8 @@ export const groupRouter = createTRPCRouter({
|
||||
await ctx.db
|
||||
.delete(groupMembers)
|
||||
.where(and(eq(groupMembers.groupId, input.groupId), eq(groupMembers.userId, input.userId)));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/validation/widgets/media-request";
|
||||
|
||||
import { createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { invalidateIntegrationCache, invalidateUserCache } from "../../cache-invalidation";
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "./integration-access";
|
||||
import { MissingSecretError, testConnectionAsync } from "./integration-test-connection";
|
||||
@@ -368,6 +369,8 @@ export const integrationRouter = createTRPCRouter({
|
||||
kind: input.kind,
|
||||
url: input.url,
|
||||
});
|
||||
|
||||
invalidateIntegrationCache(integrationId);
|
||||
}),
|
||||
update: protectedProcedure.input(integrationUpdateSchema).mutation(async ({ ctx, input }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.id), "full");
|
||||
@@ -474,6 +477,7 @@ export const integrationRouter = createTRPCRouter({
|
||||
// Invalidate all cached data for this integration so that widgets pick up the
|
||||
// new configuration immediately instead of serving stale (or errored) data.
|
||||
await invalidateIntegrationCacheAsync(input.id);
|
||||
invalidateIntegrationCache(input.id);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
.meta({
|
||||
@@ -498,6 +502,7 @@ export const integrationRouter = createTRPCRouter({
|
||||
|
||||
// Clean up any cached data left behind by the deleted integration.
|
||||
await invalidateIntegrationCacheAsync(input.id);
|
||||
invalidateIntegrationCache(input.id);
|
||||
}),
|
||||
getIntegrationPermissions: protectedProcedure.input(byIdSchema).query(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.id), "full");
|
||||
@@ -576,6 +581,11 @@ export const integrationRouter = createTRPCRouter({
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.entityId), "full");
|
||||
|
||||
const existingUserPermissions = await ctx.db.query.integrationUserPermissions.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(integrationUserPermissions.integrationId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await ctx.db.transaction(async (transaction) => {
|
||||
@@ -616,12 +626,28 @@ export const integrationRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const userIds = [
|
||||
...new Set([
|
||||
...existingUserPermissions.map((permission) => permission.userId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
for (const userId of userIds) {
|
||||
invalidateUserCache(userId);
|
||||
}
|
||||
invalidateIntegrationCache(input.entityId);
|
||||
}),
|
||||
saveGroupIntegrationPermissions: protectedProcedure
|
||||
.input(integrationSavePermissionsSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.entityId), "full");
|
||||
|
||||
const existingGroupPermissions = await ctx.db.query.integrationGroupPermissions.findMany({
|
||||
columns: { groupId: true },
|
||||
where: eq(integrationGroupPermissions.integrationId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -662,6 +688,23 @@ export const integrationRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const groupIds = [
|
||||
...new Set([
|
||||
...existingGroupPermissions.map((permission) => permission.groupId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
if (groupIds.length > 0) {
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: inArray(groupMembers.groupId, groupIds),
|
||||
});
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}
|
||||
invalidateIntegrationCache(input.entityId);
|
||||
}),
|
||||
searchInIntegration: protectedProcedure
|
||||
.meta({
|
||||
|
||||
@@ -4,6 +4,7 @@ import { z } from "zod/v4";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { sectionCollapseStates, sections } from "@homarr/db/schema";
|
||||
|
||||
import { invalidateUserCache } from "../../cache-invalidation";
|
||||
import { createTRPCRouter, protectedProcedure } from "../../trpc";
|
||||
|
||||
export const sectionRouter = createTRPCRouter({
|
||||
@@ -37,6 +38,7 @@ export const sectionRouter = createTRPCRouter({
|
||||
userId: ctx.session.user.id,
|
||||
collapsed: input.collapsed,
|
||||
});
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -48,5 +50,7 @@ export const sectionRouter = createTRPCRouter({
|
||||
.where(
|
||||
and(eq(sectionCollapseStates.sectionId, section.id), eq(sectionCollapseStates.userId, ctx.session.user.id)),
|
||||
);
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
});
|
||||
@@ -14,6 +14,7 @@ import { defaultServerSettingsKeys } from "@homarr/server-settings";
|
||||
import { settingsInitSchema } from "@homarr/validation/settings";
|
||||
|
||||
import { createTRPCRouter, onboardingProcedure, permissionRequiredProcedure, publicProcedure } from "../trpc";
|
||||
import { invalidateServerSettingsCache } from "../cache-invalidation";
|
||||
import { nextOnboardingStepAsync } from "./onboard/onboard-queries";
|
||||
|
||||
const boardServerSettingsSchema = z.object({
|
||||
@@ -91,6 +92,8 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
await insertServerSettingByKeyAsync(ctx.db, "board", next);
|
||||
}
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
|
||||
return next;
|
||||
}),
|
||||
saveSettings: permissionRequiredProcedure
|
||||
@@ -107,6 +110,8 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
...current,
|
||||
...input.value,
|
||||
} as ServerSettings[keyof ServerSettings]);
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
}),
|
||||
initSettings: onboardingProcedure
|
||||
.requiresStep("settings")
|
||||
@@ -116,5 +121,7 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
await updateServerSettingByKeyAsync(ctx.db, "analytics", { ...currentAnalytics, ...input.analytics });
|
||||
await updateServerSettingByKeyAsync(ctx.db, "crawlingAndIndexing", input.crawlingAndIndexing);
|
||||
await nextOnboardingStepAsync(ctx.db, undefined);
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
}),
|
||||
});
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from "@homarr/validation/user";
|
||||
|
||||
import { convertIntersectionToZodObject } from "../schema-merger";
|
||||
import { invalidateUserCache } from "../cache-invalidation";
|
||||
import {
|
||||
createTRPCRouter,
|
||||
isDemoMode,
|
||||
@@ -221,6 +222,8 @@ export const userRouter = createTRPCRouter({
|
||||
image: input.image,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
getAll: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -452,6 +455,8 @@ export const userRouter = createTRPCRouter({
|
||||
emailVerified: emailDirty === true ? null : undefined,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ userId: z.string() }))
|
||||
@@ -598,6 +603,8 @@ export const userRouter = createTRPCRouter({
|
||||
mobileHomeBoardId: input.mobileHomeBoardId,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
changeDefaultSearchEngine: protectedProcedure
|
||||
.input(
|
||||
@@ -653,6 +660,8 @@ export const userRouter = createTRPCRouter({
|
||||
colorScheme: input.colorScheme,
|
||||
})
|
||||
.where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
changeEnableRightClickOnWidgets: protectedProcedure
|
||||
.meta({
|
||||
@@ -679,6 +688,8 @@ export const userRouter = createTRPCRouter({
|
||||
enableRightClickOnWidgets: input.enableRightClickOnWidgets,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changePingIconsEnabled: protectedProcedure
|
||||
.input(userPingIconsEnabledSchema.and(byIdSchema))
|
||||
@@ -697,6 +708,8 @@ export const userRouter = createTRPCRouter({
|
||||
pingIconsEnabled: input.pingIconsEnabled,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changeDdgBangs: protectedProcedure
|
||||
.input(convertIntersectionToZodObject(userDdgBangsSchema.and(byIdSchema)))
|
||||
@@ -724,6 +737,8 @@ export const userRouter = createTRPCRouter({
|
||||
ddgBangs: input.ddgBangs,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changeFirstDayOfWeek: protectedProcedure
|
||||
.input(convertIntersectionToZodObject(userFirstDayOfWeekSchema.and(byIdSchema)))
|
||||
@@ -765,6 +780,8 @@ export const userRouter = createTRPCRouter({
|
||||
firstDayOfWeek: input.firstDayOfWeek,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
completeTour: protectedProcedure
|
||||
.input(z.object({ tour: z.enum(["manage", "board"]) }))
|
||||
@@ -775,6 +792,8 @@ export const userRouter = createTRPCRouter({
|
||||
} as const;
|
||||
|
||||
await ctx.db.update(users).set(columnByTour[input.tour]).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
resetTours: protectedProcedure.mutation(async ({ ctx }) => {
|
||||
await ctx.db
|
||||
@@ -784,6 +803,8 @@ export const userRouter = createTRPCRouter({
|
||||
completedBoardTour: false,
|
||||
})
|
||||
.where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
getTourStatus: protectedProcedure.query(async ({ ctx }) => {
|
||||
if (isDemoMode) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import type { Database } from "@homarr/db";
|
||||
import { users } from "@homarr/db/schema";
|
||||
import { userChangeSearchPreferencesSchema } from "@homarr/validation/user";
|
||||
|
||||
import { invalidateUserCache } from "../../cache-invalidation";
|
||||
|
||||
export const changeSearchPreferencesInputSchema = userChangeSearchPreferencesSchema.and(
|
||||
z.object({ userId: z.string() }),
|
||||
);
|
||||
@@ -51,4 +53,6 @@ export const changeSearchPreferencesAsync = async (
|
||||
ddgBangs: input.ddgBangsEnabled,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
};
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
beszelSystemsRequestHandler,
|
||||
} from "@homarr/request-handler/beszel";
|
||||
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
import { BoundedAsyncQueue } from "./bounded-async-queue";
|
||||
@@ -47,6 +47,7 @@ export const beszelRouter = createTRPCRouter({
|
||||
};
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("beszel", "getSystems"),
|
||||
fallback: (integration, error) => ({
|
||||
integrationId: integration.id,
|
||||
integrationName: integration.name,
|
||||
@@ -119,6 +120,10 @@ export const beszelRouter = createTRPCRouter({
|
||||
};
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("beszel", "getAlerts", {
|
||||
includeHistory: input.includeHistory,
|
||||
maxHistoryItems: input.maxHistoryItems,
|
||||
}),
|
||||
fallback: (integration, error) => ({
|
||||
integrationId: integration.id,
|
||||
integrationName: integration.name,
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { coolifyRequestHandler } from "@homarr/request-handler/coolify";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const coolifyRouter = createTRPCRouter({
|
||||
getInstancesInfo: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "coolify"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = coolifyRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const coolifyRouter = createTRPCRouter({
|
||||
instanceInfo: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("coolify", "getInstancesInfo") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import { dnsHoleRequestHandler } from "@homarr/request-handler/dns-hole";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
export const dnsHoleRouter = createTRPCRouter({
|
||||
@@ -19,13 +19,17 @@ export const dnsHoleRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("dnsHole")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await dnsHoleRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
summary: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("dns-hole", "summary") },
|
||||
);
|
||||
}),
|
||||
|
||||
enable: protectedProcedure
|
||||
|
||||
@@ -6,7 +6,7 @@ import { downloadClientRequestHandler } from "@homarr/request-handler/downloads"
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
const createDownloadClientIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -24,14 +24,18 @@ export const downloadsRouter = createTRPCRouter({
|
||||
.concat(createDownloadClientIntegrationMiddleware("query"))
|
||||
.input(z.object({ limitPerIntegration: z.number().default(50) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = downloadClientRequestHandler.handler(integration, { limit: input.limitPerIntegration });
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("downloads", "getJobsAndStatuses", { limitPerIntegration: input.limitPerIntegration }) },
|
||||
);
|
||||
}),
|
||||
pause: protectedProcedure
|
||||
.meta({
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from "@homarr/request-handler/firewall";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const firewallMiddleware = createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("firewall"));
|
||||
@@ -18,20 +18,34 @@ const queryFirewall = <
|
||||
},
|
||||
>(
|
||||
handler: THandler,
|
||||
queryKey: string,
|
||||
) =>
|
||||
publicProcedure.concat(firewallMiddleware).query(async ({ ctx }) =>
|
||||
settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await handler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
summary: data as Awaited<ReturnType<ReturnType<THandler["handler"]>["getDataAsync"]>>["data"],
|
||||
};
|
||||
}),
|
||||
},
|
||||
{ queryKey },
|
||||
),
|
||||
);
|
||||
|
||||
export const firewallRouter = createTRPCRouter({
|
||||
getFirewallCpuStatus: queryFirewall(firewallCpuRequestHandler),
|
||||
getFirewallInterfacesStatus: queryFirewall(firewallInterfacesRequestHandler),
|
||||
getFirewallVersionStatus: queryFirewall(firewallVersionRequestHandler),
|
||||
getFirewallMemoryStatus: queryFirewall(firewallMemoryRequestHandler),
|
||||
getFirewallCpuStatus: queryFirewall(firewallCpuRequestHandler, integrationQueryKey("firewall", "getFirewallCpuStatus")),
|
||||
getFirewallInterfacesStatus: queryFirewall(
|
||||
firewallInterfacesRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallInterfacesStatus"),
|
||||
),
|
||||
getFirewallVersionStatus: queryFirewall(
|
||||
firewallVersionRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallVersionStatus"),
|
||||
),
|
||||
getFirewallMemoryStatus: queryFirewall(
|
||||
firewallMemoryRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallMemoryStatus"),
|
||||
),
|
||||
});
|
||||
@@ -3,7 +3,7 @@ import { SynologyIntegration } from "@homarr/integrations";
|
||||
import { clusterInfoRequestHandler, systemInfoRequestHandler } from "@homarr/request-handler/health-monitoring";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const healthMonitoringIntegrationKinds = [
|
||||
@@ -27,7 +27,9 @@ export const healthMonitoringRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...healthMonitoringIntegrationKinds))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await systemInfoRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integrationId: integration.id,
|
||||
@@ -35,7 +37,9 @@ export const healthMonitoringRouter = createTRPCRouter({
|
||||
healthInfo: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("health-monitoring", "getSystemHealthStatus") },
|
||||
);
|
||||
}),
|
||||
listStorageVolumes: publicProcedure
|
||||
.meta({
|
||||
|
||||
@@ -6,7 +6,7 @@ import { indexerManagerRequestHandler } from "@homarr/request-handler/indexer-ma
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -16,7 +16,9 @@ export const indexerManagerRouter = createTRPCRouter({
|
||||
getIndexersStatus: publicProcedure
|
||||
.concat(createIndexerManagerIntegrationMiddleware("query"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = indexerManagerRequestHandler.handler(integration, {});
|
||||
const { data: indexers } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -24,7 +26,9 @@ export const indexerManagerRouter = createTRPCRouter({
|
||||
integrationId: integration.id,
|
||||
indexers,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("indexer-manager", "getIndexersStatus") },
|
||||
);
|
||||
}),
|
||||
testAllIndexers: protectedProcedure
|
||||
.concat(createIndexerManagerIntegrationMiddleware("interact"))
|
||||
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { mediaReleaseRequestHandler } from "@homarr/request-handler/media-release";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const mediaReleaseRouter = createTRPCRouter({
|
||||
getMediaReleases: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRelease")))
|
||||
.query(async ({ ctx }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = mediaReleaseRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -22,7 +24,9 @@ export const mediaReleaseRouter = createTRPCRouter({
|
||||
},
|
||||
releases: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-release", "getMediaReleases") },
|
||||
);
|
||||
return results.flatMap((result) =>
|
||||
result.releases.map((release) => ({
|
||||
...release,
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { mediaRequestStatsRequestHandler } from "@homarr/request-handler/media-request-stats";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
export const mediaRequestsRouter = createTRPCRouter({
|
||||
@@ -25,10 +25,14 @@ export const mediaRequestsRouter = createTRPCRouter({
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")))
|
||||
.input(mediaRequestListInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaRequestListRequestHandler.handler(integration, input).getDataAsync();
|
||||
return { integration: { id: integration.id, name: integration.name, kind: integration.kind }, data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-requests", "getLatestRequests", input) },
|
||||
);
|
||||
return results
|
||||
.flatMap(({ data, integration }) =>
|
||||
data.map((request) => ({
|
||||
@@ -57,10 +61,14 @@ export const mediaRequestsRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")))
|
||||
.query(async ({ ctx }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaRequestStatsRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return { integration: { id: integration.id, name: integration.name, kind: integration.kind }, data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-requests", "getStats") },
|
||||
);
|
||||
return {
|
||||
stats: results.flatMap((result) => result.data.stats),
|
||||
users: results
|
||||
|
||||
@@ -5,7 +5,7 @@ import { mediaServerRequestHandler } from "@homarr/request-handler/media-server"
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const createMediaServerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -23,11 +23,15 @@ export const mediaServerRouter = createTRPCRouter({
|
||||
.concat(createMediaServerIntegrationMiddleware("query"))
|
||||
.input(z.object({ showOnlyPlaying: z.boolean() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaServerRequestHandler
|
||||
.handler(integration, { showOnlyPlaying: input.showOnlyPlaying })
|
||||
.getDataAsync();
|
||||
return { integrationId: integration.id, integrationKind: integration.kind, sessions: data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-server", "getCurrentStreams", { showOnlyPlaying: input.showOnlyPlaying }) },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { networkControllerRequestHandler } from "@homarr/request-handler/network-controller";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const networkControllerRouter = createTRPCRouter({
|
||||
summary: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("networkController")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = networkControllerRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -22,6 +24,8 @@ export const networkControllerRouter = createTRPCRouter({
|
||||
summary: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("network-controller", "summary") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -3,7 +3,7 @@ import { notificationsRequestHandler } from "@homarr/request-handler/notificatio
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const createNotificationsIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -13,7 +13,9 @@ export const notificationsRouter = createTRPCRouter({
|
||||
getNotifications: publicProcedure
|
||||
.unstable_concat(createNotificationsIntegrationMiddleware("query"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = notificationsRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -26,6 +28,8 @@ export const notificationsRouter = createTRPCRouter({
|
||||
},
|
||||
data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("notifications", "getNotifications") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import { eq } from "@homarr/db";
|
||||
import { getServerSettingsAsync } from "@homarr/db/queries";
|
||||
import { boards, items } from "@homarr/db/schema";
|
||||
|
||||
import type { WidgetOptionsSettings } from "../../../../widgets/src";
|
||||
import type { WidgetOptionsSettings } from "../../../../widgets/src/definition";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { items, users } from "@homarr/db/schema";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src";
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src/definition";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "rssFeed" });
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { speedtestTrackerRequestHandler } from "@homarr/request-handler/speedtest-tracker";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const speedtestTrackerRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "speedtestTracker"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = speedtestTrackerRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const speedtestTrackerRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("speedtest-tracker", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,12 +1,14 @@
|
||||
import { tracearrRequestHandler } from "@homarr/request-handler/tracearr";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const tracearrRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure.concat(createManyIntegrationMiddleware("query", "tracearr")).query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = tracearrRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -17,6 +19,8 @@ export const tracearrRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("tracearr", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { traefikRequestHandler } from "@homarr/request-handler/traefik";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const traefikRouter = createTRPCRouter({
|
||||
@@ -15,7 +15,9 @@ export const traefikRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", "traefik"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = traefikRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -26,6 +28,8 @@ export const traefikRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("traefik", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from "@homarr/request-handler/umami";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "umami-router" });
|
||||
@@ -39,7 +39,9 @@ export const umamiRouter = createTRPCRouter({
|
||||
)
|
||||
.concat(createManyIntegrationMiddleware("query", "umami"))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = umamiRequestHandler.handler(integration, {
|
||||
websiteId: input.websiteId,
|
||||
timeFrame: input.timeFrame,
|
||||
@@ -54,7 +56,15 @@ export const umamiRouter = createTRPCRouter({
|
||||
visitorStats: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("umami", "getVisitorStats", {
|
||||
websiteId: input.websiteId,
|
||||
timeFrame: input.timeFrame,
|
||||
eventName: input.eventName,
|
||||
}),
|
||||
},
|
||||
);
|
||||
}),
|
||||
|
||||
getEventNames: publicProcedure
|
||||
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { upsSummariesRequestHandler } from "@homarr/request-handler/ups";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const upsRouter = createTRPCRouter({
|
||||
getSummaries: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("ups")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = upsSummariesRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -20,6 +22,8 @@ export const upsRouter = createTRPCRouter({
|
||||
summaries: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("ups", "getSummaries") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,14 +1,16 @@
|
||||
import { uptimeKumaRequestHandler } from "@homarr/request-handler/uptime-kuma";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const uptimeKumaRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "uptimeKuma"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = uptimeKumaRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const uptimeKumaRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("uptime-kuma", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { vpnSummaryHandler } from "@homarr/request-handler/vpn";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const vpnRouter = createTRPCRouter({
|
||||
getSummaries: publicProcedure
|
||||
.unstable_concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("vpn")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await vpnSummaryHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: {
|
||||
@@ -20,6 +22,8 @@ export const vpnRouter = createTRPCRouter({
|
||||
},
|
||||
summary: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("vpn", "getSummaries") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,8 +1,13 @@
|
||||
import "server-only";
|
||||
|
||||
import { cache } from "react";
|
||||
|
||||
import { db } from "@homarr/db";
|
||||
import { getServerSettingsAsync } from "@homarr/db/queries";
|
||||
|
||||
// One settings snapshot per RSC request, shared by the root layout, theme, and
|
||||
// widget prefetching. React invalidates this cache between requests.
|
||||
export const getRscServerSettingsAsync = cache(async () => await getServerSettingsAsync(db));
|
||||
// ponytail: React cache() dedupes within a single request render.
|
||||
// The `use cache` layer (when enabled) dedupes across requests.
|
||||
// Keep both: inner avoids repeated deserialization of cached payload per render.
|
||||
export const getRscServerSettingsAsync = cache(async () => {
|
||||
return await getServerSettingsAsync(db);
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import { getCachedIntegrationData } from "./integration-data-cache";
|
||||
|
||||
const logger = createLogger({ module: "settleIntegrations" });
|
||||
|
||||
interface IntegrationLike {
|
||||
@@ -11,14 +13,29 @@ interface IntegrationLike {
|
||||
|
||||
interface Options<TIntegration extends IntegrationLike, TResult> {
|
||||
fallback?: (integration: TIntegration, error: unknown) => TResult;
|
||||
/** Widget procedure key, e.g. `downloads:getJobsAndStatuses:{"limitPerIntegration":50}` */
|
||||
queryKey?: string;
|
||||
}
|
||||
|
||||
export const integrationQueryKey = (widget: string, procedure: string, params?: unknown) => {
|
||||
if (params === undefined) return `${widget}:${procedure}`;
|
||||
return `${widget}:${procedure}:${JSON.stringify(params)}`;
|
||||
};
|
||||
|
||||
export async function settleIntegrationQueries<TIntegration extends IntegrationLike, TResult>(
|
||||
integrations: TIntegration[],
|
||||
fn: (integration: TIntegration) => Promise<TResult>,
|
||||
options?: Options<TIntegration, TResult>,
|
||||
): Promise<TResult[]> {
|
||||
const settled = await Promise.allSettled(integrations.map(async (integration) => fn(integration)));
|
||||
const queryKey = options?.queryKey;
|
||||
const runQuery = async (integration: TIntegration) => {
|
||||
if (queryKey) {
|
||||
return getCachedIntegrationData(integration.id, queryKey, () => fn(integration));
|
||||
}
|
||||
return fn(integration);
|
||||
};
|
||||
|
||||
const settled = await Promise.allSettled(integrations.map(runQuery));
|
||||
const results: TResult[] = [];
|
||||
const errors: unknown[] = [];
|
||||
|
||||
|
||||
@@ -11,4 +11,5 @@ export * from "./number";
|
||||
export * from "./error";
|
||||
export * from "./theme";
|
||||
export * from "./function";
|
||||
export * from "./retryable-loader";
|
||||
export * from "./id";
|
||||
@@ -0,0 +1,18 @@
|
||||
export const createRetryableLoader = <TKey, TValue>(loaders: ReadonlyMap<TKey, () => Promise<TValue>>) => {
|
||||
const promises = new Map<TKey, Promise<TValue>>();
|
||||
|
||||
return (key: TKey) => {
|
||||
const existing = promises.get(key);
|
||||
if (existing) return existing;
|
||||
|
||||
const loader = loaders.get(key);
|
||||
if (!loader) throw new Error(`No loader is registered for ${String(key)}`);
|
||||
|
||||
const promise = loader();
|
||||
promises.set(key, promise);
|
||||
void promise.catch(() => {
|
||||
if (promises.get(key) === promise) promises.delete(key);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
};
|
||||
@@ -1,12 +1,15 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import { DB_CASING } from "../constants";
|
||||
import { createDbMapping } from "../mapping";
|
||||
import { createMysqlDb } from "./mysql";
|
||||
import { createPostgresDb } from "./postgresql";
|
||||
import { dbEnv } from "../env";
|
||||
import type { SharedDrizzleConfig } from "./shared";
|
||||
import { WinstonDrizzleLogger } from "./shared";
|
||||
import { createSqliteDb } from "./sqlite";
|
||||
|
||||
export type Database<TSchema extends Record<string, unknown>> = ReturnType<typeof createSqliteDb<TSchema>>;
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
export type Database<TSchema extends Record<string, unknown>> = ReturnType<
|
||||
typeof import("./sqlite").createSqliteDb<TSchema>
|
||||
>;
|
||||
|
||||
export const createSharedConfig = <TSchema extends Record<string, unknown>>(
|
||||
schema: TSchema,
|
||||
@@ -18,10 +21,15 @@ export const createSharedConfig = <TSchema extends Record<string, unknown>>(
|
||||
|
||||
export const createDb = <TSchema extends Record<string, unknown>>(schema: TSchema) => {
|
||||
const config = createSharedConfig(schema);
|
||||
|
||||
return createDbMapping({
|
||||
mysql2: () => createMysqlDb(config),
|
||||
"node-postgres": () => createPostgresDb(config),
|
||||
"better-sqlite3": () => createSqliteDb(config),
|
||||
});
|
||||
// ponytail: conditional require — only the configured driver is loaded.
|
||||
// Ceiling: all 3 stay on disk for runtime driver switching; only loaded graph shrinks.
|
||||
const driver = dbEnv.DRIVER ?? "better-sqlite3";
|
||||
switch (driver) {
|
||||
case "mysql2":
|
||||
return (require("./mysql") as typeof import("./mysql")).createMysqlDb(config);
|
||||
case "node-postgres":
|
||||
return (require("./postgresql") as typeof import("./postgresql")).createPostgresDb(config);
|
||||
default:
|
||||
return (require("./sqlite") as typeof import("./sqlite")).createSqliteDb(config);
|
||||
}
|
||||
};
|
||||
@@ -2,7 +2,7 @@ import type { WidgetKind } from "@homarr/definitions";
|
||||
|
||||
import type { Database } from "..";
|
||||
import { inArray } from "..";
|
||||
import type { inferSupportedIntegrations } from "../../widgets/src";
|
||||
import type { inferSupportedIntegrations } from "../../widgets/src/import";
|
||||
import { items } from "../schema";
|
||||
|
||||
export const getItemsWithIntegrationsAsync = async <TKind extends WidgetKind>(
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { integrationKinds } from "@homarr/definitions";
|
||||
|
||||
import { integrationLoaderKinds } from "./creator";
|
||||
|
||||
describe("integration creator", () => {
|
||||
it("should have a loader for every IntegrationKind", () => {
|
||||
expect(new Set(integrationLoaderKinds)).toEqual(new Set(integrationKinds));
|
||||
});
|
||||
});
|
||||
@@ -1,144 +1,101 @@
|
||||
import type { IntegrationKind } from "@homarr/definitions";
|
||||
|
||||
import { AdGuardHomeIntegration } from "../adguard-home/adguard-home-integration";
|
||||
import { BeszelIntegration } from "../beszel/beszel-integration";
|
||||
import { BazarrIntegration } from "../bazarr/bazarr-integration";
|
||||
import { AnchorIntegration } from "../anchor/anchor-integration";
|
||||
import { AudiobookshelfIntegration } from "../audiobookshelf/audiobookshelf-integration";
|
||||
import { CoolifyIntegration } from "../coolify/coolify-integration";
|
||||
import { DashDotIntegration } from "../dashdot/dashdot-integration";
|
||||
import { Aria2Integration } from "../download-client/aria2/aria2-integration";
|
||||
import { DelugeIntegration } from "../download-client/deluge/deluge-integration";
|
||||
import { NzbGetIntegration } from "../download-client/nzbget/nzbget-integration";
|
||||
import { QBitTorrentIntegration } from "../download-client/qbittorrent/qbittorrent-integration";
|
||||
import { SabnzbdIntegration } from "../download-client/sabnzbd/sabnzbd-integration";
|
||||
import { SlskdIntegration } from "../download-client/slskd/slskd-integration";
|
||||
import { TransmissionIntegration } from "../download-client/transmission/transmission-integration";
|
||||
import { EmbyIntegration } from "../emby/emby-integration";
|
||||
import { GlancesIntegration } from "../glances/glances-integration";
|
||||
import { ArchiveTeamWarriorIntegration } from "../archive-team-warrior/archive-team-warrior-integration";
|
||||
import { GluetunIntegration } from "../gluetun/gluetun-integration";
|
||||
import { HomeAssistantIntegration } from "../homeassistant/homeassistant-integration";
|
||||
import { ICalIntegration } from "../ical/ical-integration";
|
||||
import { ImmichIntegration } from "../immich/immich-integration";
|
||||
import { JellyfinIntegration } from "../jellyfin/jellyfin-integration";
|
||||
import { JellyseerrIntegration } from "../jellyseerr/jellyseerr-integration";
|
||||
import { LidarrIntegration } from "../media-organizer/lidarr/lidarr-integration";
|
||||
import { RadarrIntegration } from "../media-organizer/radarr/radarr-integration";
|
||||
import { ReadarrIntegration } from "../media-organizer/readarr/readarr-integration";
|
||||
import { SonarrIntegration } from "../media-organizer/sonarr/sonarr-integration";
|
||||
import { TdarrIntegration } from "../media-transcoding/tdarr-integration";
|
||||
import { MockIntegration } from "../mock/mock-integration";
|
||||
import { NavidromeIntegration } from "../navidrome/navidrome-integration";
|
||||
import { NextcloudIntegration } from "../nextcloud/nextcloud.integration";
|
||||
import { GotifyIntegration } from "../gotify/gotify-integration";
|
||||
import { NTFYIntegration } from "../ntfy/ntfy-integration";
|
||||
import { OpenMediaVaultIntegration } from "../openmediavault/openmediavault-integration";
|
||||
import { OPNsenseIntegration } from "../opnsense/opnsense-integration";
|
||||
import { OverseerrIntegration } from "../overseerr/overseerr-integration";
|
||||
import { PaperlessNgxIntegration } from "../paperless-ngx/paperless-ngx-integration";
|
||||
import { PatchMonIntegration } from "../patchmon/patchmon-integration";
|
||||
import { PeaNutIntegration } from "../peanut/peanut-integration";
|
||||
import { createPiHoleIntegrationAsync } from "../pi-hole/pi-hole-integration-factory";
|
||||
import { createTechnitiumDnsIntegrationAsync } from "../technitium/technitium-integration-factory";
|
||||
import { PlexIntegration } from "../plex/plex-integration";
|
||||
import { ProwlarrIntegration } from "../prowlarr/prowlarr-integration";
|
||||
import { ProxmoxIntegration } from "../proxmox/proxmox-integration";
|
||||
import { SeerrIntegration } from "../seerr/seerr-integration";
|
||||
import { SpeedtestTrackerIntegration } from "../speedtest-tracker/speedtest-tracker-integration";
|
||||
import { TracearrIntegration } from "../tracearr/tracearr-integration";
|
||||
import { TraefikIntegration } from "../traefik/traefik-integration";
|
||||
import { SynologyIntegration } from "../synology/synology-integration";
|
||||
import { TrueNasIntegration } from "../truenas/truenas-integration";
|
||||
import { UmamiIntegration } from "../umami/umami-integration";
|
||||
import { UptimeKumaIntegration } from "../uptime-kuma/uptime-kuma-integration";
|
||||
import { UnifiControllerIntegration } from "../unifi-controller/unifi-controller-integration";
|
||||
import { UnraidIntegration } from "../unraid/unraid-integration";
|
||||
import type { Integration, IntegrationInput } from "./integration";
|
||||
import { createRetryableLoader } from "@homarr/common";
|
||||
|
||||
export const createIntegrationAsync = async <TKind extends keyof typeof integrationCreators>(
|
||||
import type { Integration, IntegrationInput } from "./integration";
|
||||
import type { IntegrationInstanceOfKind } from "./kind-map";
|
||||
|
||||
type IntegrationCtor = new (integration: IntegrationInput) => Integration;
|
||||
type IntegrationFactory = (integration: IntegrationInput) => Promise<Integration>;
|
||||
type IntegrationLoader = () => Promise<IntegrationCtor | [IntegrationFactory]>;
|
||||
|
||||
const integrationLoaders: Record<IntegrationKind, IntegrationLoader> = {
|
||||
anchor: () => import("../anchor/anchor-integration").then((m) => m.AnchorIntegration),
|
||||
adGuardHome: () => import("../adguard-home/adguard-home-integration").then((m) => m.AdGuardHomeIntegration),
|
||||
archiveTeamWarrior: () =>
|
||||
import("../archive-team-warrior/archive-team-warrior-integration").then((m) => m.ArchiveTeamWarriorIntegration),
|
||||
aria2: () => import("../download-client/aria2/aria2-integration").then((m) => m.Aria2Integration),
|
||||
audiobookshelf: () => import("../audiobookshelf/audiobookshelf-integration").then((m) => m.AudiobookshelfIntegration),
|
||||
bazarr: () => import("../bazarr/bazarr-integration").then((m) => m.BazarrIntegration),
|
||||
beszel: () => import("../beszel/beszel-integration").then((m) => m.BeszelIntegration),
|
||||
coolify: () => import("../coolify/coolify-integration").then((m) => m.CoolifyIntegration),
|
||||
dashDot: () => import("../dashdot/dashdot-integration").then((m) => m.DashDotIntegration),
|
||||
deluge: () => import("../download-client/deluge/deluge-integration").then((m) => m.DelugeIntegration),
|
||||
emby: () => import("../emby/emby-integration").then((m) => m.EmbyIntegration),
|
||||
glances: () => import("../glances/glances-integration").then((m) => m.GlancesIntegration),
|
||||
gluetun: () => import("../gluetun/gluetun-integration").then((m) => m.GluetunIntegration),
|
||||
gotify: () => import("../gotify/gotify-integration").then((m) => m.GotifyIntegration),
|
||||
homeAssistant: () => import("../homeassistant/homeassistant-integration").then((m) => m.HomeAssistantIntegration),
|
||||
ical: () => import("../ical/ical-integration").then((m) => m.ICalIntegration),
|
||||
immich: () => import("../immich/immich-integration").then((m) => m.ImmichIntegration),
|
||||
jellyfin: () => import("../jellyfin/jellyfin-integration").then((m) => m.JellyfinIntegration),
|
||||
jellyseerr: () => import("../jellyseerr/jellyseerr-integration").then((m) => m.JellyseerrIntegration),
|
||||
lidarr: () => import("../media-organizer/lidarr/lidarr-integration").then((m) => m.LidarrIntegration),
|
||||
mock: () => import("../mock/mock-integration").then((m) => m.MockIntegration),
|
||||
navidrome: () => import("../navidrome/navidrome-integration").then((m) => m.NavidromeIntegration),
|
||||
nextcloud: () => import("../nextcloud/nextcloud.integration").then((m) => m.NextcloudIntegration),
|
||||
ntfy: () => import("../ntfy/ntfy-integration").then((m) => m.NTFYIntegration),
|
||||
nzbGet: () => import("../download-client/nzbget/nzbget-integration").then((m) => m.NzbGetIntegration),
|
||||
openmediavault: () =>
|
||||
import("../openmediavault/openmediavault-integration").then((m) => m.OpenMediaVaultIntegration),
|
||||
opnsense: () => import("../opnsense/opnsense-integration").then((m) => m.OPNsenseIntegration),
|
||||
overseerr: () => import("../overseerr/overseerr-integration").then((m) => m.OverseerrIntegration),
|
||||
paperlessNgx: () => import("../paperless-ngx/paperless-ngx-integration").then((m) => m.PaperlessNgxIntegration),
|
||||
patchmon: () => import("../patchmon/patchmon-integration").then((m) => m.PatchMonIntegration),
|
||||
peaNut: () => import("../peanut/peanut-integration").then((m) => m.PeaNutIntegration),
|
||||
piHole: () =>
|
||||
import("../pi-hole/pi-hole-integration-factory").then((m) => [m.createPiHoleIntegrationAsync] as [IntegrationFactory]),
|
||||
plex: () => import("../plex/plex-integration").then((m) => m.PlexIntegration),
|
||||
prowlarr: () => import("../prowlarr/prowlarr-integration").then((m) => m.ProwlarrIntegration),
|
||||
proxmox: () => import("../proxmox/proxmox-integration").then((m) => m.ProxmoxIntegration),
|
||||
qBittorrent: () =>
|
||||
import("../download-client/qbittorrent/qbittorrent-integration").then((m) => m.QBitTorrentIntegration),
|
||||
radarr: () => import("../media-organizer/radarr/radarr-integration").then((m) => m.RadarrIntegration),
|
||||
readarr: () => import("../media-organizer/readarr/readarr-integration").then((m) => m.ReadarrIntegration),
|
||||
sabNzbd: () => import("../download-client/sabnzbd/sabnzbd-integration").then((m) => m.SabnzbdIntegration),
|
||||
seerr: () => import("../seerr/seerr-integration").then((m) => m.SeerrIntegration),
|
||||
slskd: () => import("../download-client/slskd/slskd-integration").then((m) => m.SlskdIntegration),
|
||||
sonarr: () => import("../media-organizer/sonarr/sonarr-integration").then((m) => m.SonarrIntegration),
|
||||
speedtestTracker: () =>
|
||||
import("../speedtest-tracker/speedtest-tracker-integration").then((m) => m.SpeedtestTrackerIntegration),
|
||||
synology: () => import("../synology/synology-integration").then((m) => m.SynologyIntegration),
|
||||
tdarr: () => import("../media-transcoding/tdarr-integration").then((m) => m.TdarrIntegration),
|
||||
technitiumDns: () =>
|
||||
import("../technitium/technitium-integration-factory").then(
|
||||
(m) => [m.createTechnitiumDnsIntegrationAsync] as [IntegrationFactory],
|
||||
),
|
||||
tracearr: () => import("../tracearr/tracearr-integration").then((m) => m.TracearrIntegration),
|
||||
traefik: () => import("../traefik/traefik-integration").then((m) => m.TraefikIntegration),
|
||||
transmission: () =>
|
||||
import("../download-client/transmission/transmission-integration").then((m) => m.TransmissionIntegration),
|
||||
truenas: () => import("../truenas/truenas-integration").then((m) => m.TrueNasIntegration),
|
||||
umami: () => import("../umami/umami-integration").then((m) => m.UmamiIntegration),
|
||||
unifiController: () =>
|
||||
import("../unifi-controller/unifi-controller-integration").then((m) => m.UnifiControllerIntegration),
|
||||
unraid: () => import("../unraid/unraid-integration").then((m) => m.UnraidIntegration),
|
||||
uptimeKuma: () => import("../uptime-kuma/uptime-kuma-integration").then((m) => m.UptimeKumaIntegration),
|
||||
};
|
||||
|
||||
export const integrationLoaderKinds = Object.keys(integrationLoaders) as IntegrationKind[];
|
||||
|
||||
const loadIntegrationCreator = createRetryableLoader(
|
||||
new Map(Object.entries(integrationLoaders) as [IntegrationKind, IntegrationLoader][]),
|
||||
);
|
||||
|
||||
export const createIntegrationAsync = async <TKind extends IntegrationKind>(
|
||||
integration: IntegrationInput & { kind: TKind },
|
||||
) => {
|
||||
if (!(integration.kind in integrationCreators)) {
|
||||
): Promise<IntegrationInstanceOfKind<TKind>> => {
|
||||
if (!(integration.kind in integrationLoaders)) {
|
||||
throw new Error(
|
||||
`Unknown integration kind ${integration.kind}. Did you forget to add it to the integration creator?`,
|
||||
);
|
||||
}
|
||||
|
||||
const creator = integrationCreators[integration.kind];
|
||||
const creator = await loadIntegrationCreator(integration.kind);
|
||||
|
||||
// factories are an array, to differentiate in js between class constructors and functions
|
||||
if (Array.isArray(creator)) {
|
||||
return (await creator[0](integration)) as IntegrationInstanceOfKind<TKind>;
|
||||
}
|
||||
|
||||
return new creator(integration) as IntegrationInstanceOfKind<TKind>;
|
||||
};
|
||||
|
||||
type IntegrationInstance = new (integration: IntegrationInput) => Integration;
|
||||
|
||||
// factories are an array, to differentiate in js between class constructors and functions
|
||||
export const integrationCreators = {
|
||||
anchor: AnchorIntegration,
|
||||
piHole: [createPiHoleIntegrationAsync],
|
||||
adGuardHome: AdGuardHomeIntegration,
|
||||
technitiumDns: [createTechnitiumDnsIntegrationAsync],
|
||||
homeAssistant: HomeAssistantIntegration,
|
||||
jellyfin: JellyfinIntegration,
|
||||
plex: PlexIntegration,
|
||||
sonarr: SonarrIntegration,
|
||||
radarr: RadarrIntegration,
|
||||
sabNzbd: SabnzbdIntegration,
|
||||
nzbGet: NzbGetIntegration,
|
||||
qBittorrent: QBitTorrentIntegration,
|
||||
deluge: DelugeIntegration,
|
||||
transmission: TransmissionIntegration,
|
||||
slskd: SlskdIntegration,
|
||||
aria2: Aria2Integration,
|
||||
jellyseerr: JellyseerrIntegration,
|
||||
seerr: SeerrIntegration,
|
||||
overseerr: OverseerrIntegration,
|
||||
prowlarr: ProwlarrIntegration,
|
||||
openmediavault: OpenMediaVaultIntegration,
|
||||
lidarr: LidarrIntegration,
|
||||
readarr: ReadarrIntegration,
|
||||
dashDot: DashDotIntegration,
|
||||
tdarr: TdarrIntegration,
|
||||
proxmox: ProxmoxIntegration,
|
||||
emby: EmbyIntegration,
|
||||
nextcloud: NextcloudIntegration,
|
||||
unifiController: UnifiControllerIntegration,
|
||||
opnsense: OPNsenseIntegration,
|
||||
ical: ICalIntegration,
|
||||
ntfy: NTFYIntegration,
|
||||
gotify: GotifyIntegration,
|
||||
mock: MockIntegration,
|
||||
truenas: TrueNasIntegration,
|
||||
synology: SynologyIntegration,
|
||||
unraid: UnraidIntegration,
|
||||
coolify: CoolifyIntegration,
|
||||
tracearr: TracearrIntegration,
|
||||
glances: GlancesIntegration,
|
||||
immich: ImmichIntegration,
|
||||
paperlessNgx: PaperlessNgxIntegration,
|
||||
patchmon: PatchMonIntegration,
|
||||
speedtestTracker: SpeedtestTrackerIntegration,
|
||||
audiobookshelf: AudiobookshelfIntegration,
|
||||
navidrome: NavidromeIntegration,
|
||||
umami: UmamiIntegration,
|
||||
gluetun: GluetunIntegration,
|
||||
archiveTeamWarrior: ArchiveTeamWarriorIntegration,
|
||||
uptimeKuma: UptimeKumaIntegration,
|
||||
peaNut: PeaNutIntegration,
|
||||
beszel: BeszelIntegration,
|
||||
bazarr: BazarrIntegration,
|
||||
traefik: TraefikIntegration,
|
||||
} satisfies Record<IntegrationKind, IntegrationInstance | [(input: IntegrationInput) => Promise<Integration>]>;
|
||||
|
||||
type IntegrationInstanceOfKind<TKind extends keyof typeof integrationCreators> = {
|
||||
[kind in TKind]: (typeof integrationCreators)[kind] extends [(input: IntegrationInput) => Promise<Integration>]
|
||||
? Awaited<ReturnType<(typeof integrationCreators)[kind][0]>>
|
||||
: (typeof integrationCreators)[kind] extends IntegrationInstance
|
||||
? InstanceType<(typeof integrationCreators)[kind]>
|
||||
: never;
|
||||
}[TKind];
|
||||
@@ -0,0 +1,119 @@
|
||||
import type { IntegrationKind } from "@homarr/definitions";
|
||||
|
||||
// Type-only imports — fully erased at build, zero runtime modules loaded.
|
||||
import type { AdGuardHomeIntegration } from "../adguard-home/adguard-home-integration";
|
||||
import type { AnchorIntegration } from "../anchor/anchor-integration";
|
||||
import type { ArchiveTeamWarriorIntegration } from "../archive-team-warrior/archive-team-warrior-integration";
|
||||
import type { AudiobookshelfIntegration } from "../audiobookshelf/audiobookshelf-integration";
|
||||
import type { BazarrIntegration } from "../bazarr/bazarr-integration";
|
||||
import type { BeszelIntegration } from "../beszel/beszel-integration";
|
||||
import type { CoolifyIntegration } from "../coolify/coolify-integration";
|
||||
import type { DashDotIntegration } from "../dashdot/dashdot-integration";
|
||||
import type { Aria2Integration } from "../download-client/aria2/aria2-integration";
|
||||
import type { DelugeIntegration } from "../download-client/deluge/deluge-integration";
|
||||
import type { NzbGetIntegration } from "../download-client/nzbget/nzbget-integration";
|
||||
import type { QBitTorrentIntegration } from "../download-client/qbittorrent/qbittorrent-integration";
|
||||
import type { SabnzbdIntegration } from "../download-client/sabnzbd/sabnzbd-integration";
|
||||
import type { SlskdIntegration } from "../download-client/slskd/slskd-integration";
|
||||
import type { TransmissionIntegration } from "../download-client/transmission/transmission-integration";
|
||||
import type { EmbyIntegration } from "../emby/emby-integration";
|
||||
import type { GlancesIntegration } from "../glances/glances-integration";
|
||||
import type { GluetunIntegration } from "../gluetun/gluetun-integration";
|
||||
import type { GotifyIntegration } from "../gotify/gotify-integration";
|
||||
import type { HomeAssistantIntegration } from "../homeassistant/homeassistant-integration";
|
||||
import type { ICalIntegration } from "../ical/ical-integration";
|
||||
import type { ImmichIntegration } from "../immich/immich-integration";
|
||||
import type { JellyfinIntegration } from "../jellyfin/jellyfin-integration";
|
||||
import type { JellyseerrIntegration } from "../jellyseerr/jellyseerr-integration";
|
||||
import type { LidarrIntegration } from "../media-organizer/lidarr/lidarr-integration";
|
||||
import type { RadarrIntegration } from "../media-organizer/radarr/radarr-integration";
|
||||
import type { ReadarrIntegration } from "../media-organizer/readarr/readarr-integration";
|
||||
import type { SonarrIntegration } from "../media-organizer/sonarr/sonarr-integration";
|
||||
import type { TdarrIntegration } from "../media-transcoding/tdarr-integration";
|
||||
import type { MockIntegration } from "../mock/mock-integration";
|
||||
import type { NavidromeIntegration } from "../navidrome/navidrome-integration";
|
||||
import type { NextcloudIntegration } from "../nextcloud/nextcloud.integration";
|
||||
import type { NTFYIntegration } from "../ntfy/ntfy-integration";
|
||||
import type { OpenMediaVaultIntegration } from "../openmediavault/openmediavault-integration";
|
||||
import type { OPNsenseIntegration } from "../opnsense/opnsense-integration";
|
||||
import type { OverseerrIntegration } from "../overseerr/overseerr-integration";
|
||||
import type { PaperlessNgxIntegration } from "../paperless-ngx/paperless-ngx-integration";
|
||||
import type { PatchMonIntegration } from "../patchmon/patchmon-integration";
|
||||
import type { PeaNutIntegration } from "../peanut/peanut-integration";
|
||||
import type { PiHoleIntegrationV5 } from "../pi-hole/v5/pi-hole-integration-v5";
|
||||
import type { PiHoleIntegrationV6 } from "../pi-hole/v6/pi-hole-integration-v6";
|
||||
import type { PlexIntegration } from "../plex/plex-integration";
|
||||
import type { ProwlarrIntegration } from "../prowlarr/prowlarr-integration";
|
||||
import type { ProxmoxIntegration } from "../proxmox/proxmox-integration";
|
||||
import type { SeerrIntegration } from "../seerr/seerr-integration";
|
||||
import type { SpeedtestTrackerIntegration } from "../speedtest-tracker/speedtest-tracker-integration";
|
||||
import type { SynologyIntegration } from "../synology/synology-integration";
|
||||
import type { TechnitiumDnsIntegration } from "../technitium/technitium-integration";
|
||||
import type { TracearrIntegration } from "../tracearr/tracearr-integration";
|
||||
import type { TraefikIntegration } from "../traefik/traefik-integration";
|
||||
import type { TrueNasIntegration } from "../truenas/truenas-integration";
|
||||
import type { UmamiIntegration } from "../umami/umami-integration";
|
||||
import type { UnifiControllerIntegration } from "../unifi-controller/unifi-controller-integration";
|
||||
import type { UnraidIntegration } from "../unraid/unraid-integration";
|
||||
import type { UptimeKumaIntegration } from "../uptime-kuma/uptime-kuma-integration";
|
||||
|
||||
export interface IntegrationInstanceMap {
|
||||
anchor: AnchorIntegration;
|
||||
adGuardHome: AdGuardHomeIntegration;
|
||||
archiveTeamWarrior: ArchiveTeamWarriorIntegration;
|
||||
aria2: Aria2Integration;
|
||||
audiobookshelf: AudiobookshelfIntegration;
|
||||
bazarr: BazarrIntegration;
|
||||
beszel: BeszelIntegration;
|
||||
coolify: CoolifyIntegration;
|
||||
dashDot: DashDotIntegration;
|
||||
deluge: DelugeIntegration;
|
||||
emby: EmbyIntegration;
|
||||
glances: GlancesIntegration;
|
||||
gluetun: GluetunIntegration;
|
||||
gotify: GotifyIntegration;
|
||||
homeAssistant: HomeAssistantIntegration;
|
||||
ical: ICalIntegration;
|
||||
immich: ImmichIntegration;
|
||||
jellyfin: JellyfinIntegration;
|
||||
jellyseerr: JellyseerrIntegration;
|
||||
lidarr: LidarrIntegration;
|
||||
mock: MockIntegration;
|
||||
navidrome: NavidromeIntegration;
|
||||
nextcloud: NextcloudIntegration;
|
||||
ntfy: NTFYIntegration;
|
||||
nzbGet: NzbGetIntegration;
|
||||
openmediavault: OpenMediaVaultIntegration;
|
||||
opnsense: OPNsenseIntegration;
|
||||
overseerr: OverseerrIntegration;
|
||||
paperlessNgx: PaperlessNgxIntegration;
|
||||
patchmon: PatchMonIntegration;
|
||||
peaNut: PeaNutIntegration;
|
||||
piHole: PiHoleIntegrationV5 | PiHoleIntegrationV6;
|
||||
plex: PlexIntegration;
|
||||
prowlarr: ProwlarrIntegration;
|
||||
proxmox: ProxmoxIntegration;
|
||||
qBittorrent: QBitTorrentIntegration;
|
||||
radarr: RadarrIntegration;
|
||||
readarr: ReadarrIntegration;
|
||||
sabNzbd: SabnzbdIntegration;
|
||||
seerr: SeerrIntegration;
|
||||
slskd: SlskdIntegration;
|
||||
sonarr: SonarrIntegration;
|
||||
speedtestTracker: SpeedtestTrackerIntegration;
|
||||
synology: SynologyIntegration;
|
||||
tdarr: TdarrIntegration;
|
||||
technitiumDns: TechnitiumDnsIntegration;
|
||||
tracearr: TracearrIntegration;
|
||||
traefik: TraefikIntegration;
|
||||
transmission: TransmissionIntegration;
|
||||
truenas: TrueNasIntegration;
|
||||
umami: UmamiIntegration;
|
||||
unifiController: UnifiControllerIntegration;
|
||||
unraid: UnraidIntegration;
|
||||
uptimeKuma: UptimeKumaIntegration;
|
||||
}
|
||||
|
||||
export type IntegrationInstanceOfKind<TKind extends IntegrationKind> = TKind extends keyof IntegrationInstanceMap
|
||||
? IntegrationInstanceMap[TKind]
|
||||
: never;
|
||||
@@ -99,3 +99,4 @@ export { downloadClientItemSchema } from "./interfaces/downloads/download-client
|
||||
|
||||
// Helpers
|
||||
export { createIntegrationAsync } from "./base/creator";
|
||||
export type { IntegrationInstanceOfKind } from "./base/kind-map";
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@homarr/next-cache-handler",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rm -rf .turbo node_modules",
|
||||
"format": "oxfmt .",
|
||||
"lint": "oxlint",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"ioredis": "catalog:",
|
||||
"next": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/tsconfig": "workspace:*",
|
||||
"typescript": "catalog:",
|
||||
"vitest": "catalog:"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
let handler: CacheHandler;
|
||||
|
||||
const getHandler = async (): Promise<CacheHandler> => {
|
||||
if (handler) return handler;
|
||||
|
||||
const useRedis = process.env.REDIS_HOST || process.env.REDIS_IS_EXTERNAL === "true";
|
||||
if (useRedis) {
|
||||
const { RedisCacheHandler } = await import("./redis-handler");
|
||||
handler = new RedisCacheHandler();
|
||||
} else {
|
||||
const { createDefaultCacheHandler } = await import("next/dist/server/lib/cache-handlers/default");
|
||||
handler = createDefaultCacheHandler(32 * 1024 * 1024);
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
|
||||
export default {
|
||||
get: async (...args: Parameters<CacheHandler["get"]>) => (await getHandler()).get(...args),
|
||||
set: async (...args: Parameters<CacheHandler["set"]>) => (await getHandler()).set(...args),
|
||||
refreshTags: async () => (await getHandler()).refreshTags(),
|
||||
getExpiration: async (...args: Parameters<CacheHandler["getExpiration"]>) =>
|
||||
(await getHandler()).getExpiration(...args),
|
||||
updateTags: async (...args: Parameters<CacheHandler["updateTags"]>) =>
|
||||
(await getHandler()).updateTags(...args),
|
||||
} satisfies CacheHandler;
|
||||
@@ -0,0 +1,184 @@
|
||||
import Redis from "ioredis";
|
||||
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
type CacheEntry = Awaited<ReturnType<CacheHandler["get"]>> & {};
|
||||
|
||||
const BUILD_ID = process.env.__NEXT_BUILD_ID ?? "dev";
|
||||
|
||||
export class RedisCacheHandler implements CacheHandler {
|
||||
private redis: Redis;
|
||||
private pendingSets = new Map<string, Promise<CacheEntry>>();
|
||||
private tagCache = new Map<string, number>();
|
||||
private loggedError = false;
|
||||
|
||||
constructor() {
|
||||
this.redis = new Redis({
|
||||
host: process.env.REDIS_HOST ?? "localhost",
|
||||
port: Number(process.env.REDIS_PORT ?? 6379),
|
||||
password: process.env.REDIS_PASSWORD || undefined,
|
||||
db: Number(process.env.REDIS_CACHE_DB ?? 1),
|
||||
lazyConnect: true,
|
||||
retryStrategy: (times) => Math.min(times * 100, 3000),
|
||||
});
|
||||
this.redis.on("error", () => {
|
||||
if (!this.loggedError) {
|
||||
console.warn("[next-cache-handler] Redis connection error, falling back to memory");
|
||||
this.loggedError = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private entryKey(cacheKey: string) {
|
||||
return `nextCache:v1:${BUILD_ID}:${cacheKey}`;
|
||||
}
|
||||
|
||||
private tagKey(tag: string) {
|
||||
return `nextCache:tag:${tag}`;
|
||||
}
|
||||
|
||||
async get(cacheKey: string, softTags: string[]): Promise<CacheEntry | undefined> {
|
||||
const pending = this.pendingSets.get(cacheKey);
|
||||
if (pending) return pending;
|
||||
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const raw = await this.redis.getBuffer(this.entryKey(cacheKey));
|
||||
if (!raw) return undefined;
|
||||
|
||||
const parsed = JSON.parse(raw.toString()) as {
|
||||
tags: string[];
|
||||
stale: number;
|
||||
timestamp: number;
|
||||
expire: number;
|
||||
revalidate: number;
|
||||
body: number[];
|
||||
};
|
||||
|
||||
const allTags = [...parsed.tags, ...softTags];
|
||||
const expiration = await this.getExpiration(allTags);
|
||||
if (expiration !== 0 && expiration < parsed.timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const body = new Uint8Array(parsed.body);
|
||||
return {
|
||||
value: new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(body);
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
tags: parsed.tags,
|
||||
stale: parsed.stale,
|
||||
timestamp: parsed.timestamp,
|
||||
expire: parsed.expire,
|
||||
revalidate: parsed.revalidate,
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async set(cacheKey: string, pendingEntry: Promise<CacheEntry>): Promise<void> {
|
||||
this.pendingSets.set(cacheKey, pendingEntry);
|
||||
|
||||
try {
|
||||
const entry = await pendingEntry;
|
||||
const reader = entry.value.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
let done = false;
|
||||
while (!done) {
|
||||
const result = await reader.read();
|
||||
done = result.done;
|
||||
if (result.value) chunks.push(result.value);
|
||||
}
|
||||
|
||||
const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
|
||||
const body = new Uint8Array(totalLength);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
body.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
|
||||
const serialized = JSON.stringify({
|
||||
tags: entry.tags,
|
||||
stale: entry.stale,
|
||||
timestamp: entry.timestamp,
|
||||
expire: entry.expire,
|
||||
revalidate: entry.revalidate,
|
||||
body: Array.from(body),
|
||||
});
|
||||
|
||||
const ttlMs = entry.expire * 1000;
|
||||
await this.redis.connect().catch(() => {});
|
||||
if (ttlMs > 0) {
|
||||
await this.redis.set(this.entryKey(cacheKey), serialized, "PX", ttlMs);
|
||||
} else {
|
||||
await this.redis.set(this.entryKey(cacheKey), serialized);
|
||||
}
|
||||
} catch {
|
||||
// Fail open
|
||||
} finally {
|
||||
if (this.pendingSets.get(cacheKey) === pendingEntry) {
|
||||
this.pendingSets.delete(cacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async refreshTags(): Promise<void> {
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const keys = await this.redis.keys("nextCache:tag:*");
|
||||
if (keys.length === 0) {
|
||||
this.tagCache.clear();
|
||||
return;
|
||||
}
|
||||
const values = await this.redis.mget(...keys);
|
||||
this.tagCache.clear();
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const tag = keys[i]!.replace("nextCache:tag:", "");
|
||||
const val = values[i];
|
||||
if (val) this.tagCache.set(tag, Number(val));
|
||||
}
|
||||
} catch {
|
||||
// Fail open — stale tag cache is better than crash
|
||||
}
|
||||
}
|
||||
|
||||
async getExpiration(tags: string[]): Promise<number> {
|
||||
if (tags.length === 0) return 0;
|
||||
|
||||
let maxTimestamp = 0;
|
||||
for (const tag of tags) {
|
||||
const cached = this.tagCache.get(tag);
|
||||
if (cached && cached > maxTimestamp) {
|
||||
maxTimestamp = cached;
|
||||
}
|
||||
}
|
||||
return maxTimestamp;
|
||||
}
|
||||
|
||||
async updateTags(tags: string[], durations?: { expire?: number }): Promise<void> {
|
||||
if (tags.length === 0) return;
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const now = Date.now();
|
||||
const pipeline = this.redis.pipeline();
|
||||
for (const tag of tags) {
|
||||
const key = this.tagKey(tag);
|
||||
pipeline.set(key, String(now));
|
||||
if (durations?.expire) {
|
||||
pipeline.pexpire(key, durations.expire * 1000);
|
||||
} else {
|
||||
pipeline.pexpire(key, 7 * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
this.tagCache.set(tag, now);
|
||||
}
|
||||
await pipeline.exec();
|
||||
} catch {
|
||||
// Fail open
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export type { CacheEntry, CacheHandler, Timestamp } from "next/dist/server/lib/cache-handlers/types";
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@homarr/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { randomUUID } from "crypto";
|
||||
import type { MaybePromise } from "@homarr/common/types";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { createRedisConnection } from "./connection";
|
||||
import { getSubscriberClient, usesMemoryFallback } from "./connection";
|
||||
import { memorySubscribe } from "./memory-channel";
|
||||
|
||||
const logger = createLogger({ module: "channelSubscriptionTracker" });
|
||||
|
||||
@@ -18,7 +19,8 @@ type SubscriptionCallback = (message: string) => MaybePromise<void>;
|
||||
*/
|
||||
export class ChannelSubscriptionTracker {
|
||||
private static subscriptions = new Map<string, Map<string, SubscriptionCallback>>();
|
||||
private static redis = createRedisConnection();
|
||||
private static redis = getSubscriberClient();
|
||||
private static memoryUnsubscribers = new Map<string, () => void>();
|
||||
private static listenerActive = false;
|
||||
|
||||
/**
|
||||
@@ -30,19 +32,27 @@ export class ChannelSubscriptionTracker {
|
||||
public static subscribe(channelName: string, callback: SubscriptionCallback) {
|
||||
logger.debug("Adding redis channel callback", { channel: channelName });
|
||||
|
||||
// We only want to activate the listener once
|
||||
if (!this.listenerActive) {
|
||||
this.activateListener();
|
||||
this.listenerActive = true;
|
||||
}
|
||||
|
||||
const channelSubscriptions = this.subscriptions.get(channelName) ?? new Map<string, SubscriptionCallback>();
|
||||
const id = randomUUID();
|
||||
|
||||
// If there are no subscriptions to the channel, subscribe to it
|
||||
if (channelSubscriptions.size === 0) {
|
||||
logger.debug("Subscribing to redis channel", { channel: channelName });
|
||||
void this.redis.subscribe(channelName);
|
||||
if (usesMemoryFallback()) {
|
||||
const unsub = memorySubscribe(channelName, (message) => {
|
||||
this.dispatchMessage(channelName, message);
|
||||
});
|
||||
this.memoryUnsubscribers.set(channelName, unsub);
|
||||
} else {
|
||||
if (!this.listenerActive) {
|
||||
this.activateListener();
|
||||
this.listenerActive = true;
|
||||
}
|
||||
const redis = this.redis;
|
||||
if (redis) {
|
||||
void redis.subscribe(channelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Adding redis channel callback", { channel: channelName, id });
|
||||
@@ -65,30 +75,44 @@ export class ChannelSubscriptionTracker {
|
||||
}
|
||||
|
||||
logger.debug("Unsubscribing from redis channel", { channel: channelName });
|
||||
void this.redis.unsubscribe(channelName);
|
||||
if (usesMemoryFallback()) {
|
||||
this.memoryUnsubscribers.get(channelName)?.();
|
||||
this.memoryUnsubscribers.delete(channelName);
|
||||
} else {
|
||||
const redis = this.redis;
|
||||
if (redis) {
|
||||
void redis.unsubscribe(channelName);
|
||||
}
|
||||
}
|
||||
this.subscriptions.delete(channelName);
|
||||
};
|
||||
}
|
||||
|
||||
private static dispatchMessage(channel: string, message: string) {
|
||||
const channelSubscriptions = this.subscriptions.get(channel);
|
||||
if (!channelSubscriptions) {
|
||||
logger.warn("Received message on unknown channel", { channel });
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [id, callback] of channelSubscriptions.entries()) {
|
||||
// Don't log messages from the logging channel as it would create an infinite loop
|
||||
if (channel !== "pubSub:logging") {
|
||||
logger.debug("Calling subscription callback", { channel, id });
|
||||
}
|
||||
void callback(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates the listener for the redis client.
|
||||
*/
|
||||
private static activateListener() {
|
||||
logger.debug("Activating listener");
|
||||
this.redis.on("message", (channel, message) => {
|
||||
const channelSubscriptions = this.subscriptions.get(channel);
|
||||
if (!channelSubscriptions) {
|
||||
logger.warn("Received message on unknown channel", { channel });
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [id, callback] of channelSubscriptions.entries()) {
|
||||
// Don't log messages from the logging channel as it would create an infinite loop
|
||||
if (channel !== "pubSub:logging") {
|
||||
logger.debug("Calling subscription callback", { channel, id });
|
||||
}
|
||||
void callback(message);
|
||||
}
|
||||
const redis = this.redis;
|
||||
if (!redis) return;
|
||||
redis.on("message", (channel, message) => {
|
||||
this.dispatchMessage(channel, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,12 @@ import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.mock("./connection", () => ({
|
||||
createRedisConnection: () => null,
|
||||
getDataClient: () => null,
|
||||
getSubscriberClient: () => null,
|
||||
usesMemoryFallback: () => true,
|
||||
}));
|
||||
|
||||
import { createLockChannel } from "./channel";
|
||||
import { createLockChannel, createSubPubChannel } from "./channel";
|
||||
|
||||
describe("createLockChannel", () => {
|
||||
test("degrades gracefully when Redis connections are disabled", async () => {
|
||||
@@ -19,3 +22,31 @@ describe("createLockChannel", () => {
|
||||
await expect(lock.releaseAsync(token)).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("createSubPubChannel with memory fallback", () => {
|
||||
test("publish and subscribe deliver messages in-process", async () => {
|
||||
const channel = createSubPubChannel<{ value: number }>("test-pubsub");
|
||||
|
||||
const received: { value: number }[] = [];
|
||||
channel.subscribe((data) => {
|
||||
received.push(data);
|
||||
});
|
||||
|
||||
await channel.publishAsync({ value: 42 });
|
||||
|
||||
expect(received).toEqual([{ value: 42 }]);
|
||||
await expect(channel.getLastDataAsync()).resolves.toEqual({ value: 42 });
|
||||
});
|
||||
|
||||
test("subscribe replays last persisted data", async () => {
|
||||
const channel = createSubPubChannel<string>("test-replay");
|
||||
await channel.publishAsync("hello");
|
||||
|
||||
const replayed: string[] = [];
|
||||
channel.subscribe((data) => {
|
||||
replayed.push(data);
|
||||
});
|
||||
|
||||
expect(replayed).toEqual(["hello"]);
|
||||
});
|
||||
});
|
||||
@@ -3,10 +3,12 @@ import superjson from "superjson";
|
||||
import { createId } from "@homarr/common";
|
||||
|
||||
import { ChannelSubscriptionTracker } from "./channel-subscription-tracker";
|
||||
import { createRedisConnection } from "./connection";
|
||||
import { getDataClient, usesMemoryFallback } from "./connection";
|
||||
import { memoryGetLast, memoryPublish } from "./memory-channel";
|
||||
|
||||
const publisher = createRedisConnection();
|
||||
const lastDataClient = createRedisConnection();
|
||||
const LAST_DATA_TTL_SECONDS = 86_400;
|
||||
|
||||
const dataClient = getDataClient();
|
||||
|
||||
/**
|
||||
* Creates a new pub/sub channel.
|
||||
@@ -23,11 +25,18 @@ export const createSubPubChannel = <TData>(name: string, { persist }: { persist:
|
||||
*/
|
||||
subscribe: (callback: (data: TData) => void) => {
|
||||
if (persist) {
|
||||
void lastDataClient.get(lastChannelName).then((data) => {
|
||||
if (usesMemoryFallback()) {
|
||||
const data = memoryGetLast(lastChannelName);
|
||||
if (data) {
|
||||
callback(superjson.parse(data));
|
||||
}
|
||||
});
|
||||
} else if (dataClient) {
|
||||
void dataClient.get(lastChannelName).then((data) => {
|
||||
if (data) {
|
||||
callback(superjson.parse(data));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return ChannelSubscriptionTracker.subscribe(channelName, (message) => {
|
||||
callback(superjson.parse(message));
|
||||
@@ -38,20 +47,31 @@ export const createSubPubChannel = <TData>(name: string, { persist }: { persist:
|
||||
* @param data data to be published
|
||||
*/
|
||||
publishAsync: async (data: TData) => {
|
||||
const serialized = superjson.stringify(data);
|
||||
if (persist) {
|
||||
await lastDataClient.set(lastChannelName, superjson.stringify(data));
|
||||
if (usesMemoryFallback()) {
|
||||
memoryPublish(lastChannelName, data);
|
||||
} else if (dataClient) {
|
||||
await dataClient.set(lastChannelName, serialized, "EX", LAST_DATA_TTL_SECONDS);
|
||||
}
|
||||
}
|
||||
if (usesMemoryFallback()) {
|
||||
memoryPublish(channelName, data);
|
||||
} else if (dataClient) {
|
||||
await dataClient.publish(channelName, serialized);
|
||||
}
|
||||
await publisher.publish(channelName, superjson.stringify(data));
|
||||
},
|
||||
getLastDataAsync: async () => {
|
||||
const data = await lastDataClient.get(lastChannelName);
|
||||
if (usesMemoryFallback()) {
|
||||
const data = memoryGetLast(lastChannelName);
|
||||
return data ? superjson.parse<TData>(data) : null;
|
||||
}
|
||||
const data = await dataClient?.get(lastChannelName);
|
||||
return data ? superjson.parse<TData>(data) : null;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const getSetClient = createRedisConnection();
|
||||
|
||||
/**
|
||||
* Creates a new redis channel for a list
|
||||
* @param name name of channel
|
||||
@@ -65,7 +85,8 @@ export const createListChannel = <TItem>(name: string) => {
|
||||
* @returns an array of all items
|
||||
*/
|
||||
getAllAsync: async () => {
|
||||
const items = await getSetClient.lrange(listChannelName, 0, -1);
|
||||
if (!dataClient) return [];
|
||||
const items = await dataClient.lrange(listChannelName, 0, -1);
|
||||
return items.map((item) => superjson.parse<TItem>(item));
|
||||
},
|
||||
/**
|
||||
@@ -73,20 +94,23 @@ export const createListChannel = <TItem>(name: string) => {
|
||||
* @param item item to remove
|
||||
*/
|
||||
removeAsync: async (item: TItem) => {
|
||||
await getSetClient.lrem(listChannelName, 0, superjson.stringify(item));
|
||||
if (!dataClient) return;
|
||||
await dataClient.lrem(listChannelName, 0, superjson.stringify(item));
|
||||
},
|
||||
/**
|
||||
* Clear all items from the channels list
|
||||
*/
|
||||
clearAsync: async () => {
|
||||
await getSetClient.del(listChannelName);
|
||||
if (!dataClient) return;
|
||||
await dataClient.del(listChannelName);
|
||||
},
|
||||
/**
|
||||
* Add an item to the channels list
|
||||
* @param item item to add
|
||||
*/
|
||||
addAsync: async (item: TItem) => {
|
||||
await getSetClient.lpush(listChannelName, superjson.stringify(item));
|
||||
if (!dataClient) return;
|
||||
await dataClient.lpush(listChannelName, superjson.stringify(item));
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -102,7 +126,8 @@ export const createGetSetChannel = <TData>(name: string) => {
|
||||
* @returns data or null if not found
|
||||
*/
|
||||
getAsync: async () => {
|
||||
const data = await getSetClient.get(name);
|
||||
if (!dataClient) return null;
|
||||
const data = await dataClient.get(name);
|
||||
return data ? superjson.parse<TData>(data) : null;
|
||||
},
|
||||
/**
|
||||
@@ -111,17 +136,19 @@ export const createGetSetChannel = <TData>(name: string) => {
|
||||
* @param options optional TTL in seconds
|
||||
*/
|
||||
setAsync: async (data: TData, options?: { ttlSeconds?: number }) => {
|
||||
if (!dataClient) return;
|
||||
if (options?.ttlSeconds) {
|
||||
await getSetClient.set(name, superjson.stringify(data), "EX", options.ttlSeconds);
|
||||
await dataClient.set(name, superjson.stringify(data), "EX", options.ttlSeconds);
|
||||
return;
|
||||
}
|
||||
await getSetClient.set(name, superjson.stringify(data));
|
||||
await dataClient.set(name, superjson.stringify(data));
|
||||
},
|
||||
/**
|
||||
* Remove data from the channel
|
||||
*/
|
||||
removeAsync: async () => {
|
||||
await getSetClient.del(name);
|
||||
if (!dataClient) return;
|
||||
await dataClient.del(name);
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -136,17 +163,15 @@ export const createLockChannel = (name: string) => {
|
||||
return {
|
||||
acquireAsync: async (ttlSeconds: number) => {
|
||||
const token = createId();
|
||||
const client = getSetClient as typeof getSetClient | null;
|
||||
if (!client) return token;
|
||||
if (!dataClient) return token;
|
||||
|
||||
const result = await client.set(name, token, "EX", ttlSeconds, "NX");
|
||||
const result = await dataClient.set(name, token, "EX", ttlSeconds, "NX");
|
||||
return result === "OK" ? token : null;
|
||||
},
|
||||
releaseAsync: async (token: string) => {
|
||||
const client = getSetClient as typeof getSetClient | null;
|
||||
if (!client) return;
|
||||
if (!dataClient) return;
|
||||
|
||||
await client.eval(
|
||||
await dataClient.eval(
|
||||
"if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end",
|
||||
1,
|
||||
name,
|
||||
@@ -157,9 +182,8 @@ export const createLockChannel = (name: string) => {
|
||||
};
|
||||
|
||||
export const invalidateIntegrationCacheAsync = async (integrationId: string): Promise<void> => {
|
||||
const client = getSetClient as typeof getSetClient | null;
|
||||
if (!client) return;
|
||||
await client.del(`session-store:${integrationId}`);
|
||||
if (!dataClient) return;
|
||||
await dataClient.del(`session-store:${integrationId}`);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -167,11 +191,12 @@ export const invalidateIntegrationCacheAsync = async (integrationId: string): Pr
|
||||
*/
|
||||
export const createChannelEventHistoryOld = <TData>(channelName: string, maxElements = 15) => {
|
||||
const popElementsOverMaxAsync = async () => {
|
||||
const length = await getSetClient.llen(channelName);
|
||||
if (!dataClient) return;
|
||||
const length = await dataClient.llen(channelName);
|
||||
if (length <= maxElements) {
|
||||
return;
|
||||
}
|
||||
await getSetClient.ltrim(channelName, 0, maxElements - 1);
|
||||
await dataClient.ltrim(channelName, 0, maxElements - 1);
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -181,33 +206,39 @@ export const createChannelEventHistoryOld = <TData>(channelName: string, maxElem
|
||||
});
|
||||
},
|
||||
publishAndPushAsync: async (data: TData) => {
|
||||
await publisher.publish(channelName, superjson.stringify(data));
|
||||
await getSetClient.lpush(channelName, superjson.stringify({ data, timestamp: new Date() }));
|
||||
if (!dataClient) return;
|
||||
await dataClient.publish(channelName, superjson.stringify(data));
|
||||
await dataClient.lpush(channelName, superjson.stringify({ data, timestamp: new Date() }));
|
||||
await popElementsOverMaxAsync();
|
||||
},
|
||||
pushAsync: async (data: TData) => {
|
||||
await getSetClient.lpush(channelName, superjson.stringify({ data, timestamp: new Date() }));
|
||||
if (!dataClient) return;
|
||||
await dataClient.lpush(channelName, superjson.stringify({ data, timestamp: new Date() }));
|
||||
await popElementsOverMaxAsync();
|
||||
},
|
||||
clearAsync: async () => {
|
||||
await getSetClient.del(channelName);
|
||||
if (!dataClient) return;
|
||||
await dataClient.del(channelName);
|
||||
},
|
||||
getLastAsync: async () => {
|
||||
const length = await getSetClient.llen(channelName);
|
||||
const data = await getSetClient.lrange(channelName, length - 1, length);
|
||||
if (!dataClient) return null;
|
||||
const length = await dataClient.llen(channelName);
|
||||
const data = await dataClient.lrange(channelName, length - 1, length);
|
||||
if (data.length !== 1) return null;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return superjson.parse<{ data: TData; timestamp: Date }>(data[0]!);
|
||||
},
|
||||
getSliceAsync: async (startIndex: number, endIndex: number) => {
|
||||
const range = await getSetClient.lrange(channelName, startIndex, endIndex);
|
||||
if (!dataClient) return [];
|
||||
const range = await dataClient.lrange(channelName, startIndex, endIndex);
|
||||
return range.map((item) => superjson.parse<{ data: TData; timestamp: Date }>(item));
|
||||
},
|
||||
getSliceUntilTimeAsync: async (time: Date) => {
|
||||
const length = await getSetClient.llen(channelName);
|
||||
if (!dataClient) return [];
|
||||
const length = await dataClient.llen(channelName);
|
||||
const items: TData[] = [];
|
||||
const itemsInCollection = await getSetClient.lrange(channelName, 0, length - 1);
|
||||
const itemsInCollection = await dataClient.lrange(channelName, 0, length - 1);
|
||||
|
||||
for (let i = 0; i < length - 1; i++) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
@@ -220,14 +251,13 @@ export const createChannelEventHistoryOld = <TData>(channelName: string, maxElem
|
||||
return items;
|
||||
},
|
||||
getLengthAsync: async () => {
|
||||
return await getSetClient.llen(channelName);
|
||||
if (!dataClient) return 0;
|
||||
return await dataClient.llen(channelName);
|
||||
},
|
||||
name: channelName,
|
||||
};
|
||||
};
|
||||
|
||||
const queueClient = createRedisConnection();
|
||||
|
||||
type WithId<TItem> = TItem & { _id: string };
|
||||
|
||||
/**
|
||||
@@ -238,11 +268,13 @@ type WithId<TItem> = TItem & { _id: string };
|
||||
export const createQueueChannel = <TItem>(name: string) => {
|
||||
const queueChannelName = `queue:${name}`;
|
||||
const getDataAsync = async () => {
|
||||
const data = await queueClient.get(queueChannelName);
|
||||
if (!dataClient) return [];
|
||||
const data = await dataClient.get(queueChannelName);
|
||||
return data ? superjson.parse<WithId<TItem>[]>(data) : [];
|
||||
};
|
||||
const setDataAsync = async (data: WithId<TItem>[]) => {
|
||||
await queueClient.set(queueChannelName, superjson.stringify(data));
|
||||
if (!dataClient) return;
|
||||
await dataClient.set(queueChannelName, superjson.stringify(data));
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -289,5 +321,6 @@ export const createQueueChannel = <TItem>(name: string) => {
|
||||
};
|
||||
|
||||
export const handshakeAsync = async () => {
|
||||
await getSetClient.hello();
|
||||
if (!dataClient) return;
|
||||
await dataClient.hello();
|
||||
};
|
||||
@@ -1,15 +1,32 @@
|
||||
import type { RedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import { createRedisClient } from "@homarr/core/infrastructure/redis";
|
||||
|
||||
/**
|
||||
* Creates a new Redis connection
|
||||
* @returns redis client
|
||||
*/
|
||||
export const createRedisConnection = () => {
|
||||
if (Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS)) {
|
||||
// Return null if we are in CI as we don't want to connect to Redis
|
||||
return null as unknown as RedisClient;
|
||||
}
|
||||
let dataClient: RedisClient | undefined;
|
||||
let subscriberClient: RedisClient | undefined;
|
||||
|
||||
return createRedisClient();
|
||||
const isRedisExplicitlyDisabled = () =>
|
||||
Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS);
|
||||
|
||||
/**
|
||||
* In-process pub/sub fallback for single-instance deployments when Redis is unavailable.
|
||||
* Never used when REDIS_IS_EXTERNAL=true (multi-instance requires real Redis).
|
||||
*/
|
||||
export const usesMemoryFallback = (): boolean =>
|
||||
process.env.REDIS_IS_EXTERNAL !== "true" && isRedisExplicitlyDisabled();
|
||||
|
||||
/** Shared client for SET/GET/PUBLISH and other non-subscribe Redis commands. */
|
||||
export const getDataClient = (): RedisClient | null => {
|
||||
if (usesMemoryFallback()) return null;
|
||||
dataClient ??= createRedisClient();
|
||||
return dataClient;
|
||||
};
|
||||
|
||||
/** Dedicated client for SUBSCRIBE — ioredis requires a separate connection for pub/sub. */
|
||||
export const getSubscriberClient = (): RedisClient | null => {
|
||||
if (usesMemoryFallback()) return null;
|
||||
subscriberClient ??= createRedisClient();
|
||||
return subscriberClient;
|
||||
};
|
||||
|
||||
/** @deprecated Prefer getDataClient() — kept for test mocks. */
|
||||
export const createRedisConnection = () => getDataClient();
|
||||
@@ -0,0 +1,34 @@
|
||||
import EventEmitter from "node:events";
|
||||
|
||||
import superjson from "superjson";
|
||||
|
||||
const emitter = new EventEmitter();
|
||||
emitter.setMaxListeners(100);
|
||||
|
||||
const lastData = new Map<string, { data: string; timestamp: number }>();
|
||||
const MAX_ENTRIES = 500;
|
||||
|
||||
export const memoryPublish = (channel: string, data: unknown) => {
|
||||
const serialized = superjson.stringify(data);
|
||||
lastData.set(channel, { data: serialized, timestamp: Date.now() });
|
||||
|
||||
// Bounded: evict oldest if over limit
|
||||
if (lastData.size > MAX_ENTRIES) {
|
||||
const oldest = lastData.keys().next().value;
|
||||
if (oldest) lastData.delete(oldest);
|
||||
}
|
||||
|
||||
emitter.emit(channel, serialized);
|
||||
};
|
||||
|
||||
export const memorySubscribe = (channel: string, callback: (data: string) => void) => {
|
||||
emitter.on(channel, callback);
|
||||
return () => {
|
||||
emitter.off(channel, callback);
|
||||
};
|
||||
};
|
||||
|
||||
export const memoryGetLast = (channel: string): string | null => {
|
||||
const entry = lastData.get(channel);
|
||||
return entry?.data ?? null;
|
||||
};
|
||||
@@ -6,51 +6,46 @@ import { fallbackLocale, isLocaleSupported } from ".";
|
||||
import type { SupportedLanguage } from "./config";
|
||||
import { createLanguageMapping } from "./mapping";
|
||||
|
||||
// This file is referenced in the `next.config.js` file. See https://next-intl-docs.vercel.app/docs/usage/configuration
|
||||
// ponytail: module-level memo — one merged object per locale used (~250-400 KB each).
|
||||
// Ceiling: ~2 MB for 5 active locales. Upgrade: evict when size > 8.
|
||||
const mergedMessages = new Map<SupportedLanguage, Promise<TranslationObject>>();
|
||||
|
||||
const loadMessagesAsync = (locale: SupportedLanguage): Promise<TranslationObject> => {
|
||||
const existing = mergedMessages.get(locale);
|
||||
if (existing) return existing;
|
||||
|
||||
const promise = (async () => {
|
||||
const languageMap = createLanguageMapping();
|
||||
const current = removeEmptyTranslations((await languageMap[locale]()).default) as TranslationObject;
|
||||
if (locale === fallbackLocale) return current;
|
||||
const fallback = (await languageMap[fallbackLocale]()).default;
|
||||
return deepmerge(fallback, current) as TranslationObject;
|
||||
})();
|
||||
|
||||
mergedMessages.set(locale, promise);
|
||||
void promise.catch(() => {
|
||||
if (mergedMessages.get(locale) === promise) mergedMessages.delete(locale);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
export default getRequestConfig(async ({ requestLocale }) => {
|
||||
let currentLocale = await requestLocale;
|
||||
|
||||
if (!currentLocale || !isLocaleSupported(currentLocale)) {
|
||||
currentLocale = fallbackLocale;
|
||||
}
|
||||
const typedLocale = currentLocale as SupportedLanguage;
|
||||
|
||||
const languageMap = createLanguageMapping();
|
||||
const currentMessages = removeEmptyTranslations((await languageMap[typedLocale]()).default) as TranslationObject;
|
||||
|
||||
// Fallback to default locale if the current locales messages if not all messages are present
|
||||
if (currentLocale !== fallbackLocale) {
|
||||
const fallbackMessages = (await languageMap[fallbackLocale]()).default;
|
||||
return {
|
||||
locale: typedLocale,
|
||||
messages: deepmerge(fallbackMessages, currentMessages),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
locale: currentLocale,
|
||||
messages: currentMessages,
|
||||
};
|
||||
const requested = await requestLocale;
|
||||
const locale: SupportedLanguage =
|
||||
requested && isLocaleSupported(requested) ? (requested as SupportedLanguage) : fallbackLocale;
|
||||
return { locale, messages: await loadMessagesAsync(locale) };
|
||||
});
|
||||
|
||||
const removeEmptyTranslations = (translations: Record<string, unknown>): Record<string, unknown> => {
|
||||
return Object.entries(translations).reduce(
|
||||
(acc, [key, value]) => {
|
||||
if (typeof value !== "string") {
|
||||
return {
|
||||
...acc,
|
||||
[key]: removeEmptyTranslations(value as Record<string, unknown>),
|
||||
};
|
||||
return { ...acc, [key]: removeEmptyTranslations(value as Record<string, unknown>) };
|
||||
}
|
||||
|
||||
if (value.trim() === "") {
|
||||
return acc;
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: value,
|
||||
};
|
||||
return { ...acc, [key]: value };
|
||||
},
|
||||
{} as Record<string, unknown>,
|
||||
);
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./src";
|
||||
@@ -12,12 +12,12 @@
|
||||
}
|
||||
},
|
||||
"exports": {
|
||||
".": "./index.ts",
|
||||
"./_inputs/json-path-tree-picker": "./src/_inputs/json-path-tree-picker.tsx",
|
||||
"./custom-api/component": "./src/custom-api/component.tsx",
|
||||
"./custom-api/extract-display-data": "./src/custom-api/extract-display-data.ts",
|
||||
"./catalog": "./src/catalog.ts",
|
||||
"./definition": "./src/definition.ts",
|
||||
"./import": "./src/import.ts",
|
||||
"./errors": "./src/errors/component.tsx",
|
||||
"./manifest": "./src/manifest.ts",
|
||||
"./options": "./src/options.ts",
|
||||
|
||||
@@ -9,7 +9,7 @@ import type { SettingsContextProps } from "@homarr/settings/creator";
|
||||
import type { stringOrTranslation } from "@homarr/translation";
|
||||
import type { TablerIcon } from "@homarr/ui";
|
||||
|
||||
import type { WidgetImports } from ".";
|
||||
import type { WidgetImports } from "./import";
|
||||
import type { inferOptionsFromCreator, inferOptionsFromDefinition, WidgetOptionsRecord } from "./options";
|
||||
|
||||
export interface WidgetContextMenuAction {
|
||||
|
||||
@@ -1,3 +1,129 @@
|
||||
import type { WidgetKind } from "@homarr/definitions";
|
||||
import type { IntegrationKind, WidgetKind } from "@homarr/definitions";
|
||||
|
||||
import type * as anchorNote from "./anchor-note";
|
||||
import type * as app from "./app";
|
||||
import type * as archiveTeamWarrior from "./archive-team-warrior";
|
||||
import type * as audioStats from "./audio-stats";
|
||||
import type * as bazarr from "./bazarr";
|
||||
import type * as beszelAlerts from "./beszel-alerts";
|
||||
import type * as beszelSystemGrid from "./beszel-system-grid";
|
||||
import type * as beszelSystemStats from "./beszel-system-stats";
|
||||
import type * as beszelSystemTable from "./beszel-system-table";
|
||||
import type * as bookmarks from "./bookmarks";
|
||||
import type * as calendar from "./calendar";
|
||||
import type * as clock from "./clock";
|
||||
import type * as coolify from "./coolify";
|
||||
import type * as customApi from "./custom-api";
|
||||
import type * as dnsHoleControls from "./dns-hole/controls";
|
||||
import type * as dnsHoleSummary from "./dns-hole/summary";
|
||||
import type * as dockerContainers from "./docker";
|
||||
import type * as downloads from "./downloads";
|
||||
import type * as firewall from "./firewall";
|
||||
import type * as healthMonitoring from "./health-monitoring";
|
||||
import type * as iframe from "./iframe";
|
||||
import type * as immichAlbumCarousel from "./immich/album-carousel";
|
||||
import type * as immichServerStats from "./immich/server-stats";
|
||||
import type * as indexerManager from "./indexer-manager";
|
||||
import type * as mediaMissing from "./media-missing";
|
||||
import type * as mediaReleases from "./media-releases";
|
||||
import type * as mediaRequestsList from "./media-requests/list";
|
||||
import type * as mediaRequestsStats from "./media-requests/stats";
|
||||
import type * as mediaServer from "./media-server";
|
||||
import type * as mediaTranscoding from "./media-transcoding";
|
||||
import type * as minecraftServerStatus from "./minecraft/server-status";
|
||||
import type * as networkControllerStatus from "./network-controller/network-status";
|
||||
import type * as networkControllerSummary from "./network-controller/summary";
|
||||
import type * as notebook from "./notebook";
|
||||
import type * as notifications from "./notifications";
|
||||
import type * as paperlessNgx from "./paperless-ngx";
|
||||
import type * as patchmon from "./patchmon";
|
||||
import type * as releases from "./releases";
|
||||
import type * as rssFeed from "./rssFeed";
|
||||
import type * as smartHomeEntityState from "./smart-home/entity-state";
|
||||
import type * as smartHomeExecuteAutomation from "./smart-home/execute-automation";
|
||||
import type * as speedtestTracker from "./speedtest-tracker";
|
||||
import type * as stockPrice from "./stocks";
|
||||
import type * as systemDisks from "./system-disks";
|
||||
import type * as systemResources from "./system-resources";
|
||||
import type * as timetable from "./timetable";
|
||||
import type * as tracearr from "./tracearr";
|
||||
import type * as traefik from "./traefik";
|
||||
import type * as umami from "./umami";
|
||||
import type * as uptimeKuma from "./uptime-kuma";
|
||||
import type * as ups from "./ups";
|
||||
import type * as video from "./video";
|
||||
import type * as vpn from "./vpn";
|
||||
import type * as weather from "./weather";
|
||||
|
||||
export type WidgetImportRecord = Record<WidgetKind, unknown>;
|
||||
|
||||
export type WidgetImports = {
|
||||
clock: typeof clock;
|
||||
weather: typeof weather;
|
||||
app: typeof app;
|
||||
archiveTeamWarrior: typeof archiveTeamWarrior;
|
||||
anchorNote: typeof anchorNote;
|
||||
notebook: typeof notebook;
|
||||
iframe: typeof iframe;
|
||||
video: typeof video;
|
||||
dnsHoleSummary: typeof dnsHoleSummary;
|
||||
dnsHoleControls: typeof dnsHoleControls;
|
||||
"smartHome-entityState": typeof smartHomeEntityState;
|
||||
"smartHome-executeAutomation": typeof smartHomeExecuteAutomation;
|
||||
stockPrice: typeof stockPrice;
|
||||
mediaServer: typeof mediaServer;
|
||||
calendar: typeof calendar;
|
||||
downloads: typeof downloads;
|
||||
"mediaRequests-requestList": typeof mediaRequestsList;
|
||||
"mediaRequests-requestStats": typeof mediaRequestsStats;
|
||||
mediaMissing: typeof mediaMissing;
|
||||
networkControllerSummary: typeof networkControllerSummary;
|
||||
networkControllerStatus: typeof networkControllerStatus;
|
||||
rssFeed: typeof rssFeed;
|
||||
bookmarks: typeof bookmarks;
|
||||
bazarr: typeof bazarr;
|
||||
indexerManager: typeof indexerManager;
|
||||
healthMonitoring: typeof healthMonitoring;
|
||||
mediaTranscoding: typeof mediaTranscoding;
|
||||
minecraftServerStatus: typeof minecraftServerStatus;
|
||||
dockerContainers: typeof dockerContainers;
|
||||
releases: typeof releases;
|
||||
firewall: typeof firewall;
|
||||
notifications: typeof notifications;
|
||||
mediaReleases: typeof mediaReleases;
|
||||
systemResources: typeof systemResources;
|
||||
coolify: typeof coolify;
|
||||
systemDisks: typeof systemDisks;
|
||||
timetable: typeof timetable;
|
||||
"immich-serverStats": typeof immichServerStats;
|
||||
"immich-albumCarousel": typeof immichAlbumCarousel;
|
||||
paperlessNgx: typeof paperlessNgx;
|
||||
patchmon: typeof patchmon;
|
||||
tracearr: typeof tracearr;
|
||||
speedtestTracker: typeof speedtestTracker;
|
||||
uptimeKuma: typeof uptimeKuma;
|
||||
audioStats: typeof audioStats;
|
||||
umami: typeof umami;
|
||||
vpn: typeof vpn;
|
||||
ups: typeof ups;
|
||||
beszelSystemTable: typeof beszelSystemTable;
|
||||
beszelSystemGrid: typeof beszelSystemGrid;
|
||||
beszelAlerts: typeof beszelAlerts;
|
||||
beszelSystemStats: typeof beszelSystemStats;
|
||||
traefik: typeof traefik;
|
||||
customApi: typeof customApi;
|
||||
};
|
||||
|
||||
export type WidgetImportKey = keyof WidgetImports;
|
||||
|
||||
export type inferSupportedIntegrations<TKind extends WidgetKind> = (WidgetImports[TKind]["definition"] extends {
|
||||
supportedIntegrations: string[];
|
||||
}
|
||||
? WidgetImports[TKind]["definition"]["supportedIntegrations"]
|
||||
: string[])[number];
|
||||
|
||||
export type inferSupportedIntegrationsStrict<TKind extends WidgetKind> = (WidgetImports[TKind]["definition"] extends {
|
||||
supportedIntegrations: IntegrationKind[];
|
||||
}
|
||||
? WidgetImports[TKind]["definition"]["supportedIntegrations"]
|
||||
: never[])[number];
|
||||
@@ -1,184 +0,0 @@
|
||||
import type { ComponentType } from "react";
|
||||
import type { Loader } from "next/dynamic";
|
||||
import dynamic from "next/dynamic";
|
||||
import { Center, Loader as UiLoader } from "@mantine/core";
|
||||
|
||||
import { objectEntries } from "@homarr/common";
|
||||
import type { IntegrationKind, WidgetKind } from "@homarr/definitions";
|
||||
import type { SettingsContextProps } from "@homarr/settings/creator";
|
||||
|
||||
import * as anchorNote from "./anchor-note";
|
||||
import * as audioStats from "./audio-stats";
|
||||
import * as app from "./app";
|
||||
import * as archiveTeamWarrior from "./archive-team-warrior";
|
||||
import * as bookmarks from "./bookmarks";
|
||||
import * as bazarr from "./bazarr";
|
||||
import * as calendar from "./calendar";
|
||||
import * as clock from "./clock";
|
||||
import * as coolify from "./coolify";
|
||||
import type { WidgetComponentProps } from "./definition";
|
||||
import * as dnsHoleControls from "./dns-hole/controls";
|
||||
import * as dnsHoleSummary from "./dns-hole/summary";
|
||||
import * as dockerContainers from "./docker";
|
||||
import * as downloads from "./downloads";
|
||||
import * as firewall from "./firewall";
|
||||
import * as healthMonitoring from "./health-monitoring";
|
||||
import * as iframe from "./iframe";
|
||||
import * as immichAlbumCarousel from "./immich/album-carousel";
|
||||
import * as immichServerStats from "./immich/server-stats";
|
||||
import type { WidgetImportRecord } from "./import";
|
||||
import * as indexerManager from "./indexer-manager";
|
||||
import * as mediaReleases from "./media-releases";
|
||||
import * as mediaRequestsList from "./media-requests/list";
|
||||
import * as mediaRequestsStats from "./media-requests/stats";
|
||||
import * as mediaServer from "./media-server";
|
||||
import * as mediaTranscoding from "./media-transcoding";
|
||||
import * as mediaMissing from "./media-missing";
|
||||
import * as minecraftServerStatus from "./minecraft/server-status";
|
||||
import * as networkControllerStatus from "./network-controller/network-status";
|
||||
import * as networkControllerSummary from "./network-controller/summary";
|
||||
import * as notebook from "./notebook";
|
||||
import * as paperlessNgx from "./paperless-ngx";
|
||||
import * as patchmon from "./patchmon";
|
||||
import * as notifications from "./notifications";
|
||||
import type { WidgetOptionDefinition } from "./options";
|
||||
import * as releases from "./releases";
|
||||
import * as rssFeed from "./rssFeed";
|
||||
import * as smartHomeEntityState from "./smart-home/entity-state";
|
||||
import * as smartHomeExecuteAutomation from "./smart-home/execute-automation";
|
||||
import * as speedtestTracker from "./speedtest-tracker";
|
||||
import * as uptimeKuma from "./uptime-kuma";
|
||||
import * as stockPrice from "./stocks";
|
||||
import * as systemDisks from "./system-disks";
|
||||
import * as systemResources from "./system-resources";
|
||||
import * as timetable from "./timetable";
|
||||
import * as traefik from "./traefik";
|
||||
import * as tracearr from "./tracearr";
|
||||
import * as umami from "./umami";
|
||||
import * as ups from "./ups";
|
||||
import * as vpn from "./vpn";
|
||||
import * as beszelSystemTable from "./beszel-system-table";
|
||||
import * as beszelSystemGrid from "./beszel-system-grid";
|
||||
import * as beszelAlerts from "./beszel-alerts";
|
||||
import * as beszelSystemStats from "./beszel-system-stats";
|
||||
import * as video from "./video";
|
||||
import * as weather from "./weather";
|
||||
import * as customApi from "./custom-api";
|
||||
|
||||
export type {
|
||||
WidgetDefinition,
|
||||
WidgetContextMenuAction,
|
||||
WidgetContextActionProps,
|
||||
WidgetOptionsSettings,
|
||||
} from "./definition";
|
||||
export type { WidgetComponentProps };
|
||||
export type { WidgetOptionDefinition, WidgetOptionType } from "./options";
|
||||
|
||||
export const widgetImports = {
|
||||
clock,
|
||||
weather,
|
||||
app,
|
||||
archiveTeamWarrior,
|
||||
anchorNote,
|
||||
notebook,
|
||||
iframe,
|
||||
video,
|
||||
dnsHoleSummary,
|
||||
dnsHoleControls,
|
||||
"smartHome-entityState": smartHomeEntityState,
|
||||
"smartHome-executeAutomation": smartHomeExecuteAutomation,
|
||||
stockPrice,
|
||||
mediaServer,
|
||||
calendar,
|
||||
downloads,
|
||||
"mediaRequests-requestList": mediaRequestsList,
|
||||
"mediaRequests-requestStats": mediaRequestsStats,
|
||||
mediaMissing,
|
||||
networkControllerSummary,
|
||||
networkControllerStatus,
|
||||
rssFeed,
|
||||
bookmarks,
|
||||
bazarr,
|
||||
indexerManager,
|
||||
healthMonitoring,
|
||||
mediaTranscoding,
|
||||
minecraftServerStatus,
|
||||
dockerContainers,
|
||||
releases,
|
||||
firewall,
|
||||
notifications,
|
||||
mediaReleases,
|
||||
systemResources,
|
||||
coolify,
|
||||
systemDisks,
|
||||
timetable,
|
||||
"immich-serverStats": immichServerStats,
|
||||
"immich-albumCarousel": immichAlbumCarousel,
|
||||
paperlessNgx,
|
||||
patchmon,
|
||||
tracearr,
|
||||
speedtestTracker,
|
||||
uptimeKuma,
|
||||
audioStats,
|
||||
umami,
|
||||
vpn,
|
||||
ups,
|
||||
beszelSystemTable,
|
||||
beszelSystemGrid,
|
||||
beszelAlerts,
|
||||
beszelSystemStats,
|
||||
traefik,
|
||||
customApi,
|
||||
} satisfies WidgetImportRecord;
|
||||
|
||||
export type WidgetImports = typeof widgetImports;
|
||||
export type WidgetImportKey = keyof WidgetImports;
|
||||
|
||||
const loadedComponents = new Map<WidgetKind, ComponentType<WidgetComponentProps<WidgetKind>>>();
|
||||
|
||||
export const loadWidgetDynamic = <TKind extends WidgetKind>(kind: TKind) => {
|
||||
const existingComponent = loadedComponents.get(kind);
|
||||
if (existingComponent) return existingComponent;
|
||||
|
||||
const newlyLoadedComponent = dynamic<WidgetComponentProps<TKind>>(
|
||||
widgetImports[kind].componentLoader as Loader<WidgetComponentProps<TKind>>,
|
||||
{
|
||||
loading: () => (
|
||||
<Center w="100%" h="100%">
|
||||
<UiLoader />
|
||||
</Center>
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
loadedComponents.set(kind, newlyLoadedComponent as never);
|
||||
return newlyLoadedComponent;
|
||||
};
|
||||
|
||||
export type inferSupportedIntegrations<TKind extends WidgetKind> = (WidgetImports[TKind]["definition"] extends {
|
||||
supportedIntegrations: string[];
|
||||
}
|
||||
? WidgetImports[TKind]["definition"]["supportedIntegrations"]
|
||||
: string[])[number];
|
||||
|
||||
export type inferSupportedIntegrationsStrict<TKind extends WidgetKind> = (WidgetImports[TKind]["definition"] extends {
|
||||
supportedIntegrations: IntegrationKind[];
|
||||
}
|
||||
? WidgetImports[TKind]["definition"]["supportedIntegrations"]
|
||||
: never[])[number];
|
||||
|
||||
export const reduceWidgetOptionsWithDefaultValues = (
|
||||
kind: WidgetKind,
|
||||
settings: Pick<SettingsContextProps, "enableStatusByDefault" | "forceDisableStatus">,
|
||||
currentValue: Record<string, unknown> = {},
|
||||
) => {
|
||||
const definition = widgetImports[kind].definition;
|
||||
const options = definition.createOptions(settings) as Record<string, WidgetOptionDefinition>;
|
||||
return objectEntries(options).reduce(
|
||||
(prev, [key, value]) => ({
|
||||
...prev,
|
||||
[key]: currentValue[key] ?? value.defaultValue,
|
||||
}),
|
||||
{} as Record<string, unknown>,
|
||||
);
|
||||
};
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { QueryKey } from "@tanstack/react-query";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createRetryableLoader } from "@homarr/common";
|
||||
import { widgetIntegrationSupport, widgetKinds as definedWidgetKinds } from "@homarr/definitions";
|
||||
|
||||
import {
|
||||
createRetryableLoader,
|
||||
loadAllWidgetDefinitions,
|
||||
loadWidgetComponent,
|
||||
loadWidgetDefinition,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ComponentType } from "react";
|
||||
|
||||
import { objectEntries } from "@homarr/common";
|
||||
import { createRetryableLoader, objectEntries } from "@homarr/common";
|
||||
import type { WidgetKind } from "@homarr/definitions";
|
||||
import type { SettingsContextProps } from "@homarr/settings/creator";
|
||||
|
||||
@@ -143,25 +143,6 @@ const definitionPromises = new Map<WidgetKind, Promise<WidgetDefinition>>();
|
||||
|
||||
export const widgetKinds = Object.keys(moduleLoaders) as WidgetKind[];
|
||||
|
||||
export const createRetryableLoader = <TKey, TValue>(loaders: ReadonlyMap<TKey, () => Promise<TValue>>) => {
|
||||
const promises = new Map<TKey, Promise<TValue>>();
|
||||
|
||||
return (key: TKey) => {
|
||||
const existing = promises.get(key);
|
||||
if (existing) return existing;
|
||||
|
||||
const loader = loaders.get(key);
|
||||
if (!loader) throw new Error(`No loader is registered for ${String(key)}`);
|
||||
|
||||
const promise = loader();
|
||||
promises.set(key, promise);
|
||||
void promise.catch(() => {
|
||||
if (promises.get(key) === promise) promises.delete(key);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
};
|
||||
|
||||
export const loadWidgetModule = createRetryableLoader(
|
||||
new Map(Object.entries(moduleLoaders) as [WidgetKind, () => Promise<WidgetModule>][]),
|
||||
);
|
||||
|
||||
Generated
+219
-485
File diff suppressed because it is too large.
Load diff
+2
-1
@@ -131,7 +131,6 @@ catalog:
|
||||
glob: ^13.0.6
|
||||
ical.js: ^2.2.1
|
||||
ioredis: 5.11.1
|
||||
isomorphic-dompurify: ^3.18.0
|
||||
jotai: ^2.20.1
|
||||
jsdom: ^29.1.1
|
||||
json5: ^2.2.3
|
||||
@@ -190,6 +189,8 @@ catalog:
|
||||
zod: ^4.4.3
|
||||
zod-form-data: ^3.0.2
|
||||
zod-validation-error: ^5.0.0
|
||||
linkedom: ^0.18.13
|
||||
dompurify: ^3.4.13
|
||||
|
||||
catalogMode: strict
|
||||
nodeLinker: hoisted
|
||||
|
||||
@@ -142,8 +142,6 @@ export const collectBoardStaticGraph = (repositoryRoot: string) => {
|
||||
const parents = new Map<string, { importer: string; specifier: string }>();
|
||||
const bareWidgetImports: Array<{ importer: string; specifier: string }> = [];
|
||||
const forbiddenModules = new Set([
|
||||
path.join(root, "packages/widgets/index.ts"),
|
||||
path.join(root, "packages/widgets/src/index.tsx"),
|
||||
path.join(root, "packages/spotlight/src/component.ts"),
|
||||
path.join(root, "packages/spotlight/src/components/spotlight.tsx"),
|
||||
path.join(root, "packages/spotlight/src/spotlight-store.ts"),
|
||||
|
||||
@@ -6,6 +6,7 @@ mkdir -p /appdata/redis
|
||||
mkdir -p /appdata/trusted-certificates
|
||||
|
||||
# Run migrations
|
||||
export NODE_OPTIONS="--max-old-space-size=${HOMARR_MAX_OLD_SPACE_SIZE:-512} ${NODE_OPTIONS}"
|
||||
if [ "$DB_MIGRATIONS_DISABLED" = "true" ]; then
|
||||
echo "DB migrations are disabled, skipping"
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user