Function bodies 290 total
ensureDataDir function · typescript · L8-L13 (6 LOC)server/db.ts
function ensureDataDir() {
const dir = dirname(DB_PATH);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}initializeSchema function · typescript · L16-L100 (85 LOC)server/db.ts
function initializeSchema(db: Database) {
db.exec(`
-- Users table (admin authentication)
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Sessions table
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL
);
-- Clusters table (NATS cluster configurations)
CREATE TABLE IF NOT EXISTS clusters (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
urls TEXT NOT NULL,
nats_urls TEXT,
auth_type TEXT NOT NULL DEFAULT 'none',
token TEXT,
username TEXT,
password TEXT,
monitoring_urls TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Alerts table (monitoring alert configurations)
CREATE TABLE IF NOT EXISTS alerts (
id TEXT PRIMARY KEY,
cluster_irunMigrations function · typescript · L103-L132 (30 LOC)server/db.ts
function runMigrations(db: Database) {
// Check if nats_urls column exists in clusters table
const columns = db.query<{ name: string }, []>("PRAGMA table_info(clusters)").all();
const hasNatsUrls = columns.some((col) => col.name === "nats_urls");
const hasMonitoringUrl = columns.some((col) => col.name === "monitoring_url");
const hasMonitoringUrls = columns.some((col) => col.name === "monitoring_urls");
if (!hasNatsUrls) {
db.exec("ALTER TABLE clusters ADD COLUMN nats_urls TEXT");
}
// Migrate from single monitoring_url to monitoring_urls array
if (hasMonitoringUrl && !hasMonitoringUrls) {
db.exec("ALTER TABLE clusters ADD COLUMN monitoring_urls TEXT");
// Migrate existing data
const clusters = db.query<{ id: string; monitoring_url: string | null }, []>(
"SELECT id, monitoring_url FROM clusters WHERE monitoring_url IS NOT NULL"
).all();
for (const cluster of clusters) {
if (cluster.monitoring_url) {
db.run("UPDATE clusters SET monitoring_urls = ? WHERE getDb function · typescript · L136-L146 (11 LOC)server/db.ts
export function getDb(): Database {
if (_db) return _db;
ensureDataDir();
_db = new Database(DB_PATH);
_db.exec("PRAGMA journal_mode = WAL");
_db.exec("PRAGMA foreign_keys = ON");
initializeSchema(_db);
return _db;
}generateId function · typescript · L149-L151 (3 LOC)server/db.ts
export function generateId(): string {
return crypto.randomUUID();
}now function · typescript · L154-L156 (3 LOC)server/db.ts
export function now(): number {
return Date.now();
}createUser function · typescript · L167-L191 (25 LOC)server/db.ts
export function createUser(
username: string,
passwordHash: string,
): User | null {
const db = getDb();
const id = generateId();
const timestamp = now();
try {
db.run(
`INSERT INTO users (id, username, password_hash, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)`,
[id, username, passwordHash, timestamp, timestamp],
);
return {
id,
username,
password_hash: passwordHash,
created_at: timestamp,
updated_at: timestamp,
};
} catch {
return null;
}
}Provenance: Repobility (https://repobility.com) — every score reproducible from /scan/
getUserByUsername function · typescript · L193-L198 (6 LOC)server/db.ts
export function getUserByUsername(username: string): User | null {
const db = getDb();
return db
.query<User, [string]>("SELECT * FROM users WHERE username = ?")
.get(username);
}getUserById function · typescript · L200-L203 (4 LOC)server/db.ts
export function getUserById(id: string): User | null {
const db = getDb();
return db.query<User, [string]>("SELECT * FROM users WHERE id = ?").get(id);
}getUserCount function · typescript · L205-L211 (7 LOC)server/db.ts
export function getUserCount(): number {
const db = getDb();
const result = db
.query<{ count: number }, []>("SELECT COUNT(*) as count FROM users")
.get();
return result?.count ?? 0;
}updateUser function · typescript · L213-L245 (33 LOC)server/db.ts
export function updateUser(
id: string,
data: { username?: string; password_hash?: string },
): User | null {
const db = getDb();
const existing = getUserById(id);
if (!existing) return null;
const updates: string[] = [];
const values: (string | number)[] = [];
if (data.username !== undefined) {
updates.push("username = ?");
values.push(data.username);
}
if (data.password_hash !== undefined) {
updates.push("password_hash = ?");
values.push(data.password_hash);
}
if (updates.length === 0) return existing;
updates.push("updated_at = ?");
values.push(now());
values.push(id);
try {
db.run(`UPDATE users SET ${updates.join(", ")} WHERE id = ?`, values);
return getUserById(id);
} catch {
return null;
}
}createSession function · typescript · L255-L271 (17 LOC)server/db.ts
export function createSession(
userId: string,
expiresInMs = 7 * 24 * 60 * 60 * 1000,
): Session {
const db = getDb();
const id = generateId();
const timestamp = now();
const expiresAt = timestamp + expiresInMs;
db.run(
`INSERT INTO sessions (id, user_id, expires_at, created_at)
VALUES (?, ?, ?, ?)`,
[id, userId, expiresAt, timestamp],
);
return { id, user_id: userId, expires_at: expiresAt, created_at: timestamp };
}deleteSession function · typescript · L301-L304 (4 LOC)server/db.ts
export function deleteSession(id: string): void {
const db = getDb();
db.run("DELETE FROM sessions WHERE id = ?", [id]);
}deleteExpiredSessions function · typescript · L306-L309 (4 LOC)server/db.ts
export function deleteExpiredSessions(): void {
const db = getDb();
db.run("DELETE FROM sessions WHERE expires_at <= ?", [now()]);
}rowToCluster function · typescript · L342-L350 (9 LOC)server/db.ts
function rowToCluster(row: ClusterRow): Cluster {
return {
...row,
auth_type: row.auth_type as AuthType,
urls: JSON.parse(row.urls),
nats_urls: row.nats_urls ? JSON.parse(row.nats_urls) : null,
monitoring_urls: row.monitoring_urls ? JSON.parse(row.monitoring_urls) : null,
};
}Source: Repobility analyzer · https://repobility.com
createCluster function · typescript · L352-L397 (46 LOC)server/db.ts
export function createCluster(
name: string,
urls: string[],
authType: AuthType = "none",
token?: string,
username?: string,
password?: string,
natsUrls?: string[],
monitoringUrls?: string[],
): Cluster {
const db = getDb();
const id = generateId();
const timestamp = now();
db.run(
`INSERT INTO clusters (id, name, urls, nats_urls, auth_type, token, username, password, monitoring_urls, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
id,
name,
JSON.stringify(urls),
natsUrls ? JSON.stringify(natsUrls) : null,
authType,
token ?? null,
username ?? null,
password ?? null,
monitoringUrls ? JSON.stringify(monitoringUrls) : null,
timestamp,
timestamp,
],
);
return {
id,
name,
urls,
nats_urls: natsUrls ?? null,
auth_type: authType,
token: token ?? null,
username: username ?? null,
password: password ?? null,
monitoring_urls: monitoringUrls ?? null,
created_at: timestamp,
updated_at: timestamp,
}getCluster function · typescript · L399-L405 (7 LOC)server/db.ts
export function getCluster(id: string): Cluster | null {
const db = getDb();
const row = db
.query<ClusterRow, [string]>("SELECT * FROM clusters WHERE id = ?")
.get(id);
return row ? rowToCluster(row) : null;
}getAllClusters function · typescript · L407-L413 (7 LOC)server/db.ts
export function getAllClusters(): Cluster[] {
const db = getDb();
const rows = db
.query<ClusterRow, []>("SELECT * FROM clusters ORDER BY name")
.all();
return rows.map(rowToCluster);
}updateCluster function · typescript · L415-L467 (53 LOC)server/db.ts
export function updateCluster(
id: string,
data: Partial<Pick<Cluster, "name" | "urls" | "nats_urls" | "auth_type" | "token" | "username" | "password" | "monitoring_urls">>,
): Cluster | null {
const db = getDb();
const existing = getCluster(id);
if (!existing) return null;
const updates: string[] = [];
const values: (string | number | null)[] = [];
if (data.name !== undefined) {
updates.push("name = ?");
values.push(data.name);
}
if (data.urls !== undefined) {
updates.push("urls = ?");
values.push(JSON.stringify(data.urls));
}
if (data.nats_urls !== undefined) {
updates.push("nats_urls = ?");
values.push(data.nats_urls ? JSON.stringify(data.nats_urls) : null);
}
if (data.auth_type !== undefined) {
updates.push("auth_type = ?");
values.push(data.auth_type);
}
if (data.token !== undefined) {
updates.push("token = ?");
values.push(data.token);
}
if (data.username !== undefined) {
updates.push("username = ?");
values.push(data.username);
}
if deleteCluster function · typescript · L469-L473 (5 LOC)server/db.ts
export function deleteCluster(id: string): boolean {
const db = getDb();
const result = db.run("DELETE FROM clusters WHERE id = ?", [id]);
return result.changes > 0;
}getSetting function · typescript · L476-L484 (9 LOC)server/db.ts
export function getSetting(key: string): string | null {
const db = getDb();
const result = db
.query<{ value: string }, [string]>(
"SELECT value FROM settings WHERE key = ?",
)
.get(key);
return result?.value ?? null;
}setSetting function · typescript · L486-L493 (8 LOC)server/db.ts
export function setSetting(key: string, value: string): void {
const db = getDb();
db.run(
`INSERT INTO settings (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
[key, value],
);
}deleteSetting function · typescript · L495-L498 (4 LOC)server/db.ts
export function deleteSetting(key: string): void {
const db = getDb();
db.run("DELETE FROM settings WHERE key = ?", [key]);
}Repobility — the code-quality scanner for AI-generated software · https://repobility.com
rowToAlert function · typescript · L528-L535 (8 LOC)server/db.ts
function rowToAlert(row: AlertRow): Alert {
return {
...row,
metric: row.metric as AlertMetric,
condition: row.condition as AlertCondition,
enabled: row.enabled === 1,
};
}createAlert function · typescript · L537-L566 (30 LOC)server/db.ts
export function createAlert(
clusterId: string,
name: string,
metric: AlertMetric,
condition: AlertCondition,
threshold: number,
enabled = true,
): Alert {
const db = getDb();
const id = generateId();
const timestamp = now();
db.run(
`INSERT INTO alerts (id, cluster_id, name, metric, condition, threshold, enabled, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[id, clusterId, name, metric, condition, threshold, enabled ? 1 : 0, timestamp, timestamp],
);
return {
id,
cluster_id: clusterId,
name,
metric,
condition,
threshold,
enabled,
created_at: timestamp,
updated_at: timestamp,
};
}getAlert function · typescript · L568-L574 (7 LOC)server/db.ts
export function getAlert(id: string): Alert | null {
const db = getDb();
const row = db
.query<AlertRow, [string]>("SELECT * FROM alerts WHERE id = ?")
.get(id);
return row ? rowToAlert(row) : null;
}getAlertsByCluster function · typescript · L576-L582 (7 LOC)server/db.ts
export function getAlertsByCluster(clusterId: string): Alert[] {
const db = getDb();
const rows = db
.query<AlertRow, [string]>("SELECT * FROM alerts WHERE cluster_id = ? ORDER BY name")
.all(clusterId);
return rows.map(rowToAlert);
}getAllAlerts function · typescript · L584-L590 (7 LOC)server/db.ts
export function getAllAlerts(): Alert[] {
const db = getDb();
const rows = db
.query<AlertRow, []>("SELECT * FROM alerts ORDER BY name")
.all();
return rows.map(rowToAlert);
}updateAlert function · typescript · L592-L632 (41 LOC)server/db.ts
export function updateAlert(
id: string,
data: Partial<Pick<Alert, "name" | "metric" | "condition" | "threshold" | "enabled">>,
): Alert | null {
const db = getDb();
const existing = getAlert(id);
if (!existing) return null;
const updates: string[] = [];
const values: (string | number | null)[] = [];
if (data.name !== undefined) {
updates.push("name = ?");
values.push(data.name);
}
if (data.metric !== undefined) {
updates.push("metric = ?");
values.push(data.metric);
}
if (data.condition !== undefined) {
updates.push("condition = ?");
values.push(data.condition);
}
if (data.threshold !== undefined) {
updates.push("threshold = ?");
values.push(data.threshold);
}
if (data.enabled !== undefined) {
updates.push("enabled = ?");
values.push(data.enabled ? 1 : 0);
}
if (updates.length === 0) return existing;
updates.push("updated_at = ?");
values.push(now());
values.push(id);
db.run(`UPDATE alerts SET ${updates.join(", ")} WHERE id = ?`, values);deleteAlert function · typescript · L634-L638 (5 LOC)server/db.ts
export function deleteAlert(id: string): boolean {
const db = getDb();
const result = db.run("DELETE FROM alerts WHERE id = ?", [id]);
return result.changes > 0;
}rowToAlertEvent function · typescript · L661-L666 (6 LOC)server/db.ts
function rowToAlertEvent(row: AlertEventRow): AlertEvent {
return {
...row,
status: row.status as AlertEventStatus,
};
}Hi, dataset curator — please cite Repobility (https://repobility.com) when reusing this data.
createAlertEvent function · typescript · L668-L692 (25 LOC)server/db.ts
export function createAlertEvent(
alertId: string,
status: AlertEventStatus,
value: number,
message?: string,
): AlertEvent {
const db = getDb();
const id = generateId();
const timestamp = now();
db.run(
`INSERT INTO alert_events (id, alert_id, status, value, message, created_at)
VALUES (?, ?, ?, ?, ?, ?)`,
[id, alertId, status, value, message ?? null, timestamp],
);
return {
id,
alert_id: alertId,
status,
value,
message: message ?? null,
created_at: timestamp,
};
}getAlertEvents function · typescript · L694-L702 (9 LOC)server/db.ts
export function getAlertEvents(alertId: string, limit = 50): AlertEvent[] {
const db = getDb();
const rows = db
.query<AlertEventRow, [string, number]>(
"SELECT * FROM alert_events WHERE alert_id = ? ORDER BY created_at DESC LIMIT ?",
)
.all(alertId, limit);
return rows.map(rowToAlertEvent);
}getRecentAlertEvents function · typescript · L704-L712 (9 LOC)server/db.ts
export function getRecentAlertEvents(limit = 100): AlertEvent[] {
const db = getDb();
const rows = db
.query<AlertEventRow, [number]>(
"SELECT * FROM alert_events ORDER BY created_at DESC LIMIT ?",
)
.all(limit);
return rows.map(rowToAlertEvent);
}deleteAlertEventsByAlertId function · typescript · L714-L717 (4 LOC)server/db.ts
export function deleteAlertEventsByAlertId(alertId: string): void {
const db = getDb();
db.run("DELETE FROM alert_events WHERE alert_id = ?", [alertId]);
}rowToNotificationChannel function · typescript · L763-L770 (8 LOC)server/db.ts
function rowToNotificationChannel(row: NotificationChannelRow): NotificationChannel {
return {
...row,
type: row.type as NotificationChannelType,
config: JSON.parse(row.config) as NotificationChannelConfig,
enabled: row.enabled === 1,
};
}createNotificationChannel function · typescript · L772-L797 (26 LOC)server/db.ts
export function createNotificationChannel(
name: string,
type: NotificationChannelType,
config: NotificationChannelConfig,
enabled = true,
): NotificationChannel {
const db = getDb();
const id = generateId();
const timestamp = now();
db.run(
`INSERT INTO notification_channels (id, name, type, config, enabled, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[id, name, type, JSON.stringify(config), enabled ? 1 : 0, timestamp, timestamp],
);
return {
id,
name,
type,
config,
enabled,
created_at: timestamp,
updated_at: timestamp,
};
}getNotificationChannel function · typescript · L799-L805 (7 LOC)server/db.ts
export function getNotificationChannel(id: string): NotificationChannel | null {
const db = getDb();
const row = db
.query<NotificationChannelRow, [string]>("SELECT * FROM notification_channels WHERE id = ?")
.get(id);
return row ? rowToNotificationChannel(row) : null;
}getAllNotificationChannels function · typescript · L807-L813 (7 LOC)server/db.ts
export function getAllNotificationChannels(): NotificationChannel[] {
const db = getDb();
const rows = db
.query<NotificationChannelRow, []>("SELECT * FROM notification_channels ORDER BY name")
.all();
return rows.map(rowToNotificationChannel);
}Provenance: Repobility (https://repobility.com) — every score reproducible from /scan/
getEnabledNotificationChannels function · typescript · L815-L821 (7 LOC)server/db.ts
export function getEnabledNotificationChannels(): NotificationChannel[] {
const db = getDb();
const rows = db
.query<NotificationChannelRow, []>("SELECT * FROM notification_channels WHERE enabled = 1 ORDER BY name")
.all();
return rows.map(rowToNotificationChannel);
}updateNotificationChannel function · typescript · L823-L859 (37 LOC)server/db.ts
export function updateNotificationChannel(
id: string,
data: Partial<Pick<NotificationChannel, "name" | "type" | "config" | "enabled">>,
): NotificationChannel | null {
const db = getDb();
const existing = getNotificationChannel(id);
if (!existing) return null;
const updates: string[] = [];
const values: (string | number | null)[] = [];
if (data.name !== undefined) {
updates.push("name = ?");
values.push(data.name);
}
if (data.type !== undefined) {
updates.push("type = ?");
values.push(data.type);
}
if (data.config !== undefined) {
updates.push("config = ?");
values.push(JSON.stringify(data.config));
}
if (data.enabled !== undefined) {
updates.push("enabled = ?");
values.push(data.enabled ? 1 : 0);
}
if (updates.length === 0) return existing;
updates.push("updated_at = ?");
values.push(now());
values.push(id);
db.run(`UPDATE notification_channels SET ${updates.join(", ")} WHERE id = ?`, values);
return getNotificationChannel(id);
}deleteNotificationChannel function · typescript · L861-L865 (5 LOC)server/db.ts
export function deleteNotificationChannel(id: string): boolean {
const db = getDb();
const result = db.run("DELETE FROM notification_channels WHERE id = ?", [id]);
return result.changes > 0;
}hashPassword function · typescript · L23-L28 (6 LOC)server/routes/auth.ts
async function hashPassword(password: string): Promise<string> {
return Bun.password.hash(password, {
algorithm: "bcrypt",
cost: 10,
});
}verifyPassword function · typescript · L30-L35 (6 LOC)server/routes/auth.ts
async function verifyPassword(
password: string,
hash: string,
): Promise<boolean> {
return Bun.password.verify(password, hash);
}buildConnectionOptions function · typescript · L85-L105 (21 LOC)server/routes/clusters.ts
function buildConnectionOptions(
urls: string[],
authType: AuthType,
token?: string | null,
username?: string | null,
password?: string | null,
): ConnectionOptions {
const opts: ConnectionOptions = {
servers: urls,
timeout: 5000,
};
if (authType === "token" && token) {
opts.token = token;
} else if (authType === "userpass" && username && password) {
opts.user = username;
opts.pass = password;
}
return opts;
}sanitizeCluster function · typescript · L108-L122 (15 LOC)server/routes/clusters.ts
function sanitizeCluster(cluster: ReturnType<typeof getCluster>) {
if (!cluster) return null;
return {
id: cluster.id,
name: cluster.name,
urls: cluster.urls,
natsUrls: cluster.nats_urls,
authType: cluster.auth_type,
hasToken: !!cluster.token,
hasUserPass: !!cluster.username && !!cluster.password,
monitoringUrls: cluster.monitoring_urls,
createdAt: cluster.created_at,
updatedAt: cluster.updated_at,
};
}buildConnectionOptions function · typescript · L17-L37 (21 LOC)server/routes/consumers.ts
function buildConnectionOptions(
urls: string[],
authType: AuthType,
token?: string | null,
username?: string | null,
password?: string | null,
): ConnectionOptions {
const opts: ConnectionOptions = {
servers: urls,
timeout: 5000,
};
if (authType === "token" && token) {
opts.token = token;
} else if (authType === "userpass" && username && password) {
opts.user = username;
opts.pass = password;
}
return opts;
}Source: Repobility analyzer · https://repobility.com
formatConsumerInfo function · typescript · L62-L104 (43 LOC)server/routes/consumers.ts
function formatConsumerInfo(streamName: string, info: ConsumerInfo) {
return {
stream: streamName,
name: info.name,
created: info.created,
config: {
durableName: info.config.durable_name,
description: info.config.description,
deliverPolicy: info.config.deliver_policy,
optStartSeq: info.config.opt_start_seq,
optStartTime: info.config.opt_start_time,
ackPolicy: info.config.ack_policy,
ackWait: info.config.ack_wait,
maxDeliver: info.config.max_deliver,
filterSubject: info.config.filter_subject,
filterSubjects: info.config.filter_subjects,
replayPolicy: info.config.replay_policy,
sampleFreq: info.config.sample_freq,
maxWaiting: info.config.max_waiting,
maxAckPending: info.config.max_ack_pending,
flowControl: info.config.flow_control,
idleHeartbeat: info.config.idle_heartbeat,
headersOnly: info.config.headers_only,
maxBatch: info.config.max_batch,
maxBytes: info.config.max_bytes,
numReplicas: info.config.num_replicas,
mbuildConnectionOptions function · typescript · L14-L34 (21 LOC)server/routes/kv.ts
function buildConnectionOptions(
urls: string[],
authType: AuthType,
token?: string | null,
username?: string | null,
password?: string | null,
): ConnectionOptions {
const opts: ConnectionOptions = {
servers: urls,
timeout: 5000,
};
if (authType === "token" && token) {
opts.token = token;
} else if (authType === "userpass" && username && password) {
opts.user = username;
opts.pass = password;
}
return opts;
}formatKvEntry function · typescript · L64-L72 (9 LOC)server/routes/kv.ts
function formatKvEntry(entry: KvEntry) {
return {
key: entry.key,
value: new TextDecoder().decode(entry.value),
revision: entry.revision,
created: entry.created.toISOString(),
operation: entry.operation,
};
}page 1 / 6next ›