Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Lint
run: bun run lint

- name: Build frontend
run: bun run build:frontend
- name: Generate build assets
run: bun run build:kuma-db && bun run build:frontend

- name: Backend tests
run: bun run test:backend
Expand Down
2 changes: 1 addition & 1 deletion docs/perf/bun-013-src-application-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Date: 2026-06-29

- `src/db/schema/current.sql` is the canonical SQLite DDL for fresh installs.
- `src/db/schema/upgrades/` contains versioned one-time upgrades for upstream Uptime Kuma 2.x databases.
- `src/db/kuma.db` is generated from `current.sql` via `bun run build:kuma-db`.
- `out/kuma.db` is generated from `current.sql` via `bun run build:kuma-db`.

## Validation

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"dev": "bun run --parallel dev:frontend dev:server",
"dev:frontend": "NODE_ENV=development vite --host --config ./config/vite.config.ts",
"dev:frontend:container": "NODE_ENV=development DEVCONTAINER=1 vite --host --config ./config/vite.config.ts",
"dev:server": "NODE_ENV=development bun src/server/server.ts",
"dev:server:watch": "NODE_ENV=development bun --watch src/server/server.ts",
"dev:server": "bun run build:kuma-db && NODE_ENV=development bun src/server/server.ts",
"dev:server:watch": "bun run build:kuma-db && NODE_ENV=development bun --watch src/server/server.ts",
"start": "bun src/server/server.ts",
"build": "bun scripts/build/generate-kuma-db.ts && bun scripts/build/build-binary.ts",
"build": "bun scripts/build/build-binary.ts",
"build:kuma-db": "bun scripts/build/generate-kuma-db.ts",
"build:frontend": "bun --bun vite build --config ./config/vite.config.ts && bun scripts/build/generate-embedded-assets.ts",
"build:frontend": "bun --bun vite build --config ./config/vite.config.ts",
"build:frontend:analyze": "ANALYZE=1 bun run build:frontend",
"build:binary": "bun scripts/build/build-binary.ts",
"preview": "vite preview --host --config ./config/vite.config.ts",
Expand All @@ -34,7 +34,7 @@
"tsc": "tsc --project ./tsconfig-backend.json",
"audit": "bun audit",
"test": "bun run test:backend && bun run test-e2e",
"test-with-build": "bun run build:frontend && bun run test:backend",
"test-with-build": "bun run build:kuma-db && bun run build:frontend && bun run test:backend",
"test:backend": "bun run test:backend:unit && bun run test:auth-security && bun run test:maintenance",
"test:backend:unit": "bun test ./test/backend-test/analytics-scripts.test.ts ./test/backend-test/badge-renderer.test.ts ./test/backend-test/composition-root.test.ts ./test/backend-test/domain.test.ts ./test/backend-test/frontend-entry-assets.test.ts ./test/backend-test/heartbeat-data-plane.test.ts ./test/backend-test/auth-security-metrics-benchmark.test.ts ./test/backend-test/auth-settings-injection.test.ts ./test/backend-test/sqlite-core.test.ts ./test/backend-test/bun-websocket-server.test.ts ./test/backend-test/db-lifecycle-injection.test.ts ./test/backend-test/cert-hostname-match.test.ts ./test/backend-test/check-translations.test.ts ./test/backend-test/gamedig-map.test.ts ./test/backend-test/globalping.test.ts ./test/backend-test/http-client.test.ts ./test/backend-test/import-boundaries.test.ts ./test/backend-test/maintenance.test.ts ./test/backend-test/monitor-conditions/evaluator.test.ts ./test/backend-test/monitor-conditions/expressions.test.ts ./test/backend-test/monitor-lifecycle.test.ts ./test/backend-test/monitor-numeric-validation.test.ts ./test/backend-test/monitor-provider-timeout.test.ts ./test/backend-test/optional-startup-integrations.test.ts ./test/backend-test/prometheus.test.ts ./test/backend-test/real-browser-monitor-lifecycle.test.ts ./test/backend-test/monitor-response.test.ts ./test/backend-test/monitor-runtime-loading.test.ts ./test/backend-test/oauth-client-credentials.test.ts ./test/backend-test/runtime-registry-callsite.test.ts ./test/backend-test/monitor-scheduler.test.ts ./test/backend-test/monitors/gamedig.test.ts ./test/backend-test/monitors/grpc.test.ts ./test/backend-test/monitors/steam.test.ts ./test/backend-test/monitors/tcp.test.ts ./test/backend-test/monitors/websocket.test.ts ./test/backend-test/native-utils.test.ts ./test/backend-test/notification-provider-loading.test.ts ./test/backend-test/notification-provider-metadata.test.ts ./test/backend-test/notification-providers/notification-provider.test.ts ./test/backend-test/ping-chart.test.ts ./test/backend-test/proxy-validation.test.ts ./test/backend-test/radius.test.ts ./test/backend-test/schema.test.ts ./test/backend-test/startup-memory-harness.test.ts ./test/backend-test/status-page.test.ts ./test/backend-test/system-service.test.ts ./test/backend-test/upgrade.test.ts ./test/backend-test/uptime-calculator.test.ts ./test/backend-test/user-resources-injection.test.ts ./test/backend-test/util.test.ts",
"test:backend:all": "bun test ./test/backend-test",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/build-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const target = process.argv.find((arg) => arg.startsWith("--target="))?.slice("-
console.log(target ? `Compiling binary (target=${target})...` : "Compiling binary...");

const result = await Bun.build({
entrypoints: ["src/server/server.ts"],
entrypoints: ["out/embedded-assets-entry.ts"],
compile: {
outfile,
...(target ? { target } : {}),
Expand Down
75 changes: 23 additions & 52 deletions scripts/build/generate-embedded-assets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/usr/bin/env bun
import fs from "fs";
import path from "path";
import { assertFrontendEntryAssets, getFrontendEntryAssets, type ViteManifest } from "./frontend-entry-assets";
import {
assertFrontendEntryAssets,
getFrontendEntryAssets,
type ViteManifest,
} from "@/server/frontend-entry-assets.js";

const projectRoot = path.resolve(import.meta.dirname, "../..");
const distDir = path.join(projectRoot, "dist");
const outputDir = path.join(projectRoot, "src/server/generated");
const outputFile = path.join(outputDir, "embedded-assets.bundle.ts");
const entryAssetsOutputFile = path.join(outputDir, "frontend-entry-assets.ts");
const outputDir = path.join(projectRoot, "out");
const outputFile = path.join(outputDir, "embedded-assets-entry.ts");
const databaseTemplateFile = path.join(outputDir, "kuma.db");
const viteManifestFile = path.join(distDir, ".vite/manifest.json");

function walkFiles(dir: string, root = dir): string[] {
Expand All @@ -32,7 +36,7 @@ function toImportId(index: number) {
}

function toImportPath(relativePath: string) {
return `../../../dist/${relativePath.split("/").join("/")}`;
return `../dist/${relativePath.split("/").join("/")}`;
}

if (!fs.existsSync(distDir)) {
Expand All @@ -51,6 +55,11 @@ if (!fs.existsSync(viteManifestFile)) {
process.exit(1);
}

if (!fs.existsSync(databaseTemplateFile)) {
console.error("out/kuma.db not found. Run `bun run build:kuma-db` first.");
process.exit(1);
}

const manifest = JSON.parse(fs.readFileSync(viteManifestFile, "utf8")) as ViteManifest;
const entryAssets = getFrontendEntryAssets(manifest);
assertFrontendEntryAssets(entryAssets, webAssets);
Expand All @@ -70,60 +79,22 @@ fs.mkdirSync(outputDir, { recursive: true });
const source = `// @ts-nocheck
// AUTO-GENERATED by scripts/build/generate-embedded-assets.ts — do not edit.

import dbTemplate from "./kuma.db" with { type: "file" };
import { registerDatabaseTemplate } from "../src/server/database-template";
import { registerEmbeddedAssets } from "../src/server/embedded-assets";
import { registerFrontendEntryAssets } from "../src/server/frontend-entry-assets";
${importLines.join("\n")}

const embeddedWebAssets = {
${mapEntries.join("\n")}
};

function hasEmbeddedAsset(webPath) {
return Object.prototype.hasOwnProperty.call(embeddedWebAssets, webPath);
}

function getEmbeddedAssetRef(webPath) {
return embeddedWebAssets[webPath];
}

async function readEmbeddedAsset(webPath) {
const ref = embeddedWebAssets[webPath];
if (!ref) {
return null;
}

const file = Bun.file(ref);
if (!(await file.exists())) {
return null;
}

return file;
}

async function readEmbeddedAssetText(webPath) {
const file = await readEmbeddedAsset(webPath);
if (!file) {
return null;
}

return file.text();
}

export {
embeddedWebAssets,
hasEmbeddedAsset,
getEmbeddedAssetRef,
readEmbeddedAsset,
readEmbeddedAssetText,
};
`;

const entryAssetsSource = `// @ts-nocheck
// AUTO-GENERATED by scripts/build/generate-embedded-assets.ts — do not edit.

const frontendEntryAssets = ${JSON.stringify(entryAssets, null, 4)};
registerDatabaseTemplate(dbTemplate);
registerEmbeddedAssets(embeddedWebAssets);
registerFrontendEntryAssets(${JSON.stringify(entryAssets, null, 4)});

export { frontendEntryAssets };
await import("../src/server/server.ts");
`;

fs.writeFileSync(outputFile, source);
fs.writeFileSync(entryAssetsOutputFile, entryAssetsSource);
console.log(`Generated ${outputFile} and ${entryAssetsOutputFile} with ${webAssets.length} embedded web assets.`);
console.log(`Generated ${outputFile} with ${webAssets.length} embedded web assets.`);
5 changes: 3 additions & 2 deletions scripts/build/generate-kuma-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SCHEMA_VERSION_KEY } from "@/server/db-migrations";

const projectRoot = path.resolve(import.meta.dirname, "../..");
const schemaPath = path.join(projectRoot, "src/db/schema/current.sql");
const outputPath = path.join(projectRoot, "src/db/kuma.db");
const outputPath = path.join(projectRoot, "out/kuma.db");

function main() {
const sql = fs.readFileSync(schemaPath, "utf8");
Expand All @@ -16,6 +16,7 @@ function main() {
fs.unlinkSync(outputPath);
}

fs.mkdirSync(path.dirname(outputPath), { recursive: true });
const db = new BunDatabase(outputPath, { create: true, strict: true });
try {
applySqlFile(db, sql);
Expand All @@ -25,7 +26,7 @@ function main() {
.run(SCHEMA_VERSION_KEY, "1");

if (!result || Number(result.changes) !== 1) {
throw new Error(`Failed to seed ${SCHEMA_VERSION_KEY} into generated kuma.db`);
throw new Error(`Failed to seed ${SCHEMA_VERSION_KEY} into generated out/kuma.db`);
}

const seeded = db
Expand Down
2 changes: 1 addition & 1 deletion skills/release-uptime-maku/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Use `.github/workflows/release.yml` as the release implementation. Drive and ver
## 2. Prepare the version

1. Update `package.json` to the exact release version.
2. Run `bun run build` so the embedded frontend records the same version in `src/server/generated/embedded-assets.bundle.ts`.
2. Run `bun run build` so the compiled binary embeds the frontend assets. Build staging is written to the ignored `out/` directory.
3. Inspect the generated diff. Confirm both backend and frontend expose the intended version and that no stale version remains.
4. Do not manually edit generated asset contents.

Expand Down
Binary file removed src/db/kuma.db
Binary file not shown.
4 changes: 2 additions & 2 deletions src/db/schema/current.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Uptime Maku canonical SQLite schema (Knex migration end-state).
-- Regenerate src/db/kuma.db via: bun scripts/build/generate-kuma-db.ts
-- Regenerate out/kuma.db via: bun scripts/build/generate-kuma-db.ts

PRAGMA foreign_keys = OFF;

Expand Down Expand Up @@ -53,4 +53,4 @@ CREATE INDEX `monitor_remote_browser_index` on `monitor` (`remote_browser`);
CREATE TABLE `status_page` (`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `slug` varchar(255) NOT NULL, `title` varchar(255) NOT NULL, `description` text, `icon` varchar(255) NOT NULL, `theme` varchar(30) NOT NULL, `published` boolean NOT NULL DEFAULT '1', `search_engine_index` boolean NOT NULL DEFAULT '1', `show_tags` boolean NOT NULL DEFAULT '0', `password` varchar(255), `created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `footer_text` text, `custom_css` text, `show_powered_by` boolean NOT NULL DEFAULT '1', `analytics_id` varchar(255), `show_certificate_expiry` boolean NOT NULL DEFAULT '0', `auto_refresh_interval` integer DEFAULT '300', `analytics_script_url` varchar(255), `analytics_type` varchar(255) DEFAULT null, `show_only_last_heartbeat` boolean NOT NULL DEFAULT '0', `rss_title` varchar(255));
CREATE UNIQUE INDEX `status_page_slug_unique` on `status_page` (`slug`);

PRAGMA foreign_keys = ON;
PRAGMA foreign_keys = ON;
2 changes: 1 addition & 1 deletion src/server/bun-http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ async function pickFile(filePath, request, precompressed) {
}

async function pickEmbeddedFile(webPath, request, precompressed) {
const { hasEmbeddedAsset, readEmbeddedAsset } = await import("@/server/generated/embedded-assets");
const { hasEmbeddedAsset, readEmbeddedAsset } = await import("@/server/embedded-assets.js");
if (precompressed && acceptsEncoding(request, "br") && hasEmbeddedAsset(`${webPath}.br`)) {
const file = await readEmbeddedAsset(`${webPath}.br`);
if (file) {
Expand Down
15 changes: 15 additions & 0 deletions src/server/database-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let embeddedDatabaseTemplatePath: string | null = null;

function registerDatabaseTemplate(templatePath: string) {
embeddedDatabaseTemplatePath = templatePath;
}

function getEmbeddedDatabaseTemplatePath() {
if (!embeddedDatabaseTemplatePath) {
throw new Error("Compiled database template was not registered.");
}

return embeddedDatabaseTemplatePath;
}

export { getEmbeddedDatabaseTemplatePath, registerDatabaseTemplate };
6 changes: 3 additions & 3 deletions src/server/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { log } from "@/server/logger";
import { isDev } from "@/server/runtime-flags";
import { runCommandSync } from "@/server/process-helper";
import path from "path";
import kumaDbTemplate from "@/db/kuma.db" with { type: "file" };
import { defaultDataDir, isCompiledBinary } from "@/server/app-paths";
import { getEmbeddedDatabaseTemplatePath } from "@/server/database-template.js";

class Database {
/**
* Bootstrap database for SQLite
* @type {string}
*/
static templatePath = "./src/db/kuma.db";
static templatePath = "./out/kuma.db";

/**
* Data Dir (Default: ./data)
Expand Down Expand Up @@ -59,7 +59,7 @@ class Database {
* @returns {void}
*/
static getTemplatePath() {
return isCompiledBinary() ? kumaDbTemplate : Database.templatePath;
return isCompiledBinary() ? getEmbeddedDatabaseTemplatePath() : Database.templatePath;
}

static initDataDir(args) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/db-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface SQLiteStore {
* - DML (GameDig rewrites, LINE Notify deletes, etc.) rolls back with the data transaction.
* - SCHEMA_VERSION_KEY is only bumped after the data phase succeeds.
* - On failure: restart to retry; schema steps are idempotent, data migrations re-run.
* - For severely broken DBs: restore from backup or replace with a fresh src/db/kuma.db.
* - For severely broken DBs: restore from backup or replace with a fresh out/kuma.db.
*/

interface SchemaUpgrade {
Expand Down
64 changes: 64 additions & 0 deletions src/server/embedded-assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import fs from "node:fs";
import path from "node:path";

type EmbeddedAssetMap = Record<string, string>;

const embeddedWebAssets: EmbeddedAssetMap = {};
let hasRegisteredAssets = false;

function sourceAssetPath(webPath: string) {
return path.resolve("dist", webPath);
}

function registerEmbeddedAssets(assets: EmbeddedAssetMap) {
for (const key of Object.keys(embeddedWebAssets)) {
delete embeddedWebAssets[key];
}
Object.assign(embeddedWebAssets, assets);
hasRegisteredAssets = true;
}

function hasEmbeddedAsset(webPath: string) {
if (hasRegisteredAssets) {
return Object.prototype.hasOwnProperty.call(embeddedWebAssets, webPath);
}

return fs.existsSync(sourceAssetPath(webPath));
}

function getEmbeddedAssetRef(webPath: string) {
if (hasRegisteredAssets) {
return embeddedWebAssets[webPath];
}

const filePath = sourceAssetPath(webPath);
return fs.existsSync(filePath) ? filePath : undefined;
}

async function readEmbeddedAsset(webPath: string) {
const ref = getEmbeddedAssetRef(webPath);
if (!ref) {
return null;
}

const file = Bun.file(ref);
if (!(await file.exists())) {
return null;
}

return file;
}

async function readEmbeddedAssetText(webPath: string) {
const file = await readEmbeddedAsset(webPath);
return file ? file.text() : null;
}

export {
embeddedWebAssets,
getEmbeddedAssetRef,
hasEmbeddedAsset,
readEmbeddedAsset,
readEmbeddedAssetText,
registerEmbeddedAssets,
};
Loading
Loading