perf(backup): load the backup payload schema on first use

backupPayloadSchema is built when validations/backup.ts is evaluated and
holds about 12 MB of heap. The restore job, the stored-backup readers and
the export writer import that module, and they sit on the boot path, so
every server process built the schema at start whether or not it ever
touched a backup.

The schema version, the compatibility check and the per-section summary
move to validations/backup-summary.ts, which never builds the schema. The
call sites that validate a file (upload, restore, the summary and download
routes, and the streamed reader's element check) load backup.ts with a
dynamic import at that moment; everything else takes only types. The
OpenAPI registry keeps its static import, since only the spec generator
loads it.

After warm-up on a production build under --max-old-space-size=524, the
used heap drops by about 29 MB, and the schema no longer appears in a heap
snapshot. A structural test refuses a new static value import of the
module outside the registry.
This commit is contained in:
Marc-André Bombeck committed 2026-10-04 18:23:14 +02:00
1 parent 546e67475f
commit 13ea849188
18 files changed
+355 -242

No files matched your search

@@ -38,7 +38,6 @@ import {
type StreamedBackup,
} from "@/lib/export/streamed-backup";
import { annotate } from "@/lib/logging/context";
import { parseBackupPayload } from "@/lib/validations/backup";
export const dynamic = "force-dynamic";
@@ -108,6 +107,8 @@ export const GET = apiHandler(
let streamed: StreamedBackup;
try {
streamed = await readStreamedBackup(source);
// Loaded on first use, not at import (see `backup-summary.ts`).
const { parseBackupPayload } = await import("@/lib/validations/backup");
parseBackupPayload(streamed.raw);
} catch (err) {
const readFailure = isStoredBackupReadError(err);
@@ -67,14 +67,16 @@ const summarizeMock = vi.fn();
const compatibleMock = vi.fn((version: string) => version !== "99");
vi.mock("@/lib/validations/backup", () => ({
parseBackupPayload: (...a: unknown[]) => parseMock(...a),
summarizeBackup: (...a: unknown[]) => summarizeMock(...a),
isCompatibleSchemaVersion: (version: string) => compatibleMock(version),
// The measurements are read as a stream and counted one by one.
BACKUP_SCHEMA_VERSION: "2",
backupMeasurementSchema: {
safeParse: (value: unknown) => ({ success: true, data: value }),
},
}));
vi.mock("@/lib/validations/backup-summary", () => ({
summarizeBackup: (...a: unknown[]) => summarizeMock(...a),
isCompatibleSchemaVersion: (version: string) => compatibleMock(version),
BACKUP_SCHEMA_VERSION: "2",
}));
import { GET } from "../route";
import { storedCopyIdentity } from "@/lib/export/backup-preview";
@@ -59,9 +59,8 @@ import {
import { annotate } from "@/lib/logging/context";
import {
isCompatibleSchemaVersion,
parseBackupPayload,
summarizeBackup,
} from "@/lib/validations/backup";
} from "@/lib/validations/backup-summary";
export const dynamic = "force-dynamic";
@@ -123,6 +122,8 @@ async function readPreview(
try {
const streamed = await readStreamedBackup(source);
// Loaded on first use, not at import (see `backup-summary.ts`).
const { parseBackupPayload } = await import("@/lib/validations/backup");
const payload = parseBackupPayload(streamed.raw);
const summary = {
...summarizeBackup(payload),
+5 -4
View File
@@ -61,15 +61,13 @@ import { storeBackupBlob } from "@/lib/export/store-backup-blob";
import { StreamedBackupInvalidError } from "@/lib/export/streamed-backup";
import { annotate } from "@/lib/logging/context";
import { checkRateLimit } from "@/lib/rate-limit";
import type { BackupPayload } from "@/lib/validations/backup";
import {
BACKUP_SCHEMA_VERSION,
backupMeasurementSchema,
backupPayloadSchema,
isCompatibleSchemaVersion,
summarizeBackup,
type BackupPayload,
type BackupSummary,
} from "@/lib/validations/backup";
} from "@/lib/validations/backup-summary";
export const dynamic = "force-dynamic";
@@ -252,6 +250,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
yield chunk;
}
}
// Loaded on first use, not at import (see `backup-summary.ts`).
const { backupMeasurementSchema, backupPayloadSchema } =
await import("@/lib/validations/backup");
let firstWithoutId: number | null = null;
let scanned;
try {