@hasna/uptime 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1090 @@
1
+ // @bun
2
+ // src/checks.ts
3
+ import net from "net";
4
+ async function runMonitorCheck(monitor, options = {}) {
5
+ if (!monitor.enabled) {
6
+ return { status: "down", latencyMs: null, error: "monitor is disabled" };
7
+ }
8
+ if (monitor.kind === "http")
9
+ return runHttpCheck(monitor, options.fetch ?? fetch);
10
+ return runTcpCheck(monitor);
11
+ }
12
+ async function runHttpCheck(monitor, fetchImpl = fetch) {
13
+ if (!monitor.url)
14
+ return { status: "down", latencyMs: null, error: "missing url" };
15
+ const controller = new AbortController;
16
+ const timeout = setTimeout(() => controller.abort(), monitor.timeoutMs);
17
+ const started = performance.now();
18
+ try {
19
+ const response = await fetchImpl(monitor.url, {
20
+ method: monitor.method || "GET",
21
+ redirect: "manual",
22
+ signal: controller.signal
23
+ });
24
+ const latencyMs = Math.round((performance.now() - started) * 100) / 100;
25
+ const ok = monitor.expectedStatus == null ? response.status >= 200 && response.status < 400 : response.status === monitor.expectedStatus;
26
+ return {
27
+ status: ok ? "up" : "down",
28
+ latencyMs,
29
+ statusCode: response.status,
30
+ error: ok ? null : `unexpected status ${response.status}`
31
+ };
32
+ } catch (error) {
33
+ return {
34
+ status: "down",
35
+ latencyMs: Math.round((performance.now() - started) * 100) / 100,
36
+ statusCode: null,
37
+ error: error instanceof Error ? error.message : String(error)
38
+ };
39
+ } finally {
40
+ clearTimeout(timeout);
41
+ }
42
+ }
43
+ async function runTcpCheck(monitor) {
44
+ if (!monitor.host || !monitor.port)
45
+ return { status: "down", latencyMs: null, error: "missing host or port" };
46
+ const started = performance.now();
47
+ return new Promise((resolve) => {
48
+ const socket = net.createConnection({ host: monitor.host, port: monitor.port, timeout: monitor.timeoutMs });
49
+ let settled = false;
50
+ const finish = (result) => {
51
+ if (settled)
52
+ return;
53
+ settled = true;
54
+ socket.destroy();
55
+ resolve(result);
56
+ };
57
+ socket.once("connect", () => {
58
+ finish({ status: "up", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: null });
59
+ });
60
+ socket.once("timeout", () => {
61
+ finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: "tcp timeout" });
62
+ });
63
+ socket.once("error", (error) => {
64
+ finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: error.message });
65
+ });
66
+ });
67
+ }
68
+
69
+ // src/paths.ts
70
+ import { mkdirSync } from "fs";
71
+ import { homedir } from "os";
72
+ import { join } from "path";
73
+ function uptimeHome() {
74
+ return process.env.HASNA_UPTIME_HOME || join(homedir(), ".hasna", "uptime");
75
+ }
76
+ function uptimeDbPath() {
77
+ return process.env.HASNA_UPTIME_DB || join(uptimeHome(), "uptime.db");
78
+ }
79
+ function ensureUptimeHome() {
80
+ const home = uptimeHome();
81
+ mkdirSync(home, { recursive: true });
82
+ return home;
83
+ }
84
+
85
+ // src/store.ts
86
+ import { mkdirSync as mkdirSync2 } from "fs";
87
+ import { dirname } from "path";
88
+ import { randomUUID } from "crypto";
89
+ import { Database } from "bun:sqlite";
90
+
91
+ // src/limits.ts
92
+ var MIN_INTERVAL_SECONDS = 1;
93
+ var MAX_INTERVAL_SECONDS = 86400;
94
+ var MIN_TIMEOUT_MS = 1;
95
+ var MAX_TIMEOUT_MS = 60000;
96
+ var MIN_RETRY_COUNT = 0;
97
+ var MAX_RETRY_COUNT = 10;
98
+ var MAX_RESULT_LIMIT = 1000;
99
+
100
+ // src/store.ts
101
+ class UptimeStore {
102
+ dbPath;
103
+ db;
104
+ constructor(options = {}) {
105
+ this.dbPath = options.dbPath ?? uptimeDbPath();
106
+ if (this.dbPath !== ":memory:") {
107
+ mkdirSync2(dirname(this.dbPath), { recursive: true });
108
+ }
109
+ this.db = new Database(this.dbPath, { create: true });
110
+ this.db.run("PRAGMA journal_mode = WAL");
111
+ this.db.run("PRAGMA foreign_keys = ON");
112
+ this.migrate();
113
+ }
114
+ close() {
115
+ this.db.close();
116
+ }
117
+ migrate() {
118
+ this.db.run(`
119
+ CREATE TABLE IF NOT EXISTS monitors (
120
+ id TEXT PRIMARY KEY,
121
+ name TEXT NOT NULL UNIQUE,
122
+ kind TEXT NOT NULL CHECK (kind IN ('http', 'tcp')),
123
+ url TEXT,
124
+ host TEXT,
125
+ port INTEGER,
126
+ method TEXT NOT NULL DEFAULT 'GET',
127
+ expected_status INTEGER,
128
+ interval_seconds INTEGER NOT NULL DEFAULT 60,
129
+ timeout_ms INTEGER NOT NULL DEFAULT 5000,
130
+ retry_count INTEGER NOT NULL DEFAULT 0,
131
+ enabled INTEGER NOT NULL DEFAULT 1,
132
+ status TEXT NOT NULL DEFAULT 'unknown',
133
+ last_checked_at TEXT,
134
+ created_at TEXT NOT NULL,
135
+ updated_at TEXT NOT NULL
136
+ )
137
+ `);
138
+ this.db.run(`
139
+ CREATE TABLE IF NOT EXISTS check_results (
140
+ id TEXT PRIMARY KEY,
141
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
142
+ checked_at TEXT NOT NULL,
143
+ status TEXT NOT NULL CHECK (status IN ('up', 'down')),
144
+ latency_ms REAL,
145
+ status_code INTEGER,
146
+ error TEXT,
147
+ attempt_count INTEGER NOT NULL DEFAULT 1
148
+ )
149
+ `);
150
+ this.db.run(`
151
+ CREATE TABLE IF NOT EXISTS incidents (
152
+ id TEXT PRIMARY KEY,
153
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
154
+ status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
155
+ opened_at TEXT NOT NULL,
156
+ closed_at TEXT,
157
+ last_failure_at TEXT NOT NULL,
158
+ failure_count INTEGER NOT NULL DEFAULT 1,
159
+ recovery_check_id TEXT,
160
+ reason TEXT
161
+ )
162
+ `);
163
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_results_monitor_time ON check_results(monitor_id, checked_at DESC)");
164
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_incidents_monitor_status ON incidents(monitor_id, status)");
165
+ }
166
+ createMonitor(input) {
167
+ const normalized = normalizeCreateMonitor(input);
168
+ const now = new Date().toISOString();
169
+ const monitor = {
170
+ id: newId("mon"),
171
+ name: normalized.name,
172
+ kind: normalized.kind,
173
+ url: normalized.url ?? null,
174
+ host: normalized.host ?? null,
175
+ port: normalized.port ?? null,
176
+ method: normalized.method ?? "GET",
177
+ expectedStatus: normalized.expectedStatus ?? null,
178
+ intervalSeconds: normalized.intervalSeconds ?? 60,
179
+ timeoutMs: normalized.timeoutMs ?? 5000,
180
+ retryCount: normalized.retryCount ?? 0,
181
+ enabled: normalized.enabled ?? true,
182
+ status: normalized.enabled === false ? "paused" : "unknown",
183
+ lastCheckedAt: null,
184
+ createdAt: now,
185
+ updatedAt: now
186
+ };
187
+ this.db.query(`INSERT INTO monitors (
188
+ id, name, kind, url, host, port, method, expected_status,
189
+ interval_seconds, timeout_ms, retry_count, enabled, status,
190
+ last_checked_at, created_at, updated_at
191
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(monitor.id, monitor.name, monitor.kind, monitor.url, monitor.host, monitor.port, monitor.method, monitor.expectedStatus, monitor.intervalSeconds, monitor.timeoutMs, monitor.retryCount, monitor.enabled ? 1 : 0, monitor.status, monitor.lastCheckedAt, monitor.createdAt, monitor.updatedAt);
192
+ return monitor;
193
+ }
194
+ listMonitors(options = {}) {
195
+ const rows = options.includeDisabled ? this.db.query("SELECT * FROM monitors ORDER BY name ASC").all() : this.db.query("SELECT * FROM monitors WHERE enabled = 1 ORDER BY name ASC").all();
196
+ return rows.map(monitorFromRow);
197
+ }
198
+ getMonitor(idOrName) {
199
+ const row = this.db.query("SELECT * FROM monitors WHERE id = ? OR name = ?").get(idOrName, idOrName);
200
+ return row ? monitorFromRow(row) : null;
201
+ }
202
+ updateMonitor(idOrName, input) {
203
+ const current = this.getMonitor(idOrName);
204
+ if (!current)
205
+ throw new Error(`Monitor not found: ${idOrName}`);
206
+ const updatedAt = new Date().toISOString();
207
+ const next = normalizeUpdateMonitor(current, input, updatedAt);
208
+ this.db.query(`UPDATE monitors SET
209
+ name = ?, kind = ?, url = ?, host = ?, port = ?, method = ?,
210
+ expected_status = ?, interval_seconds = ?, timeout_ms = ?,
211
+ retry_count = ?, enabled = ?, status = ?, last_checked_at = ?, updated_at = ?
212
+ WHERE id = ?`).run(next.name, next.kind, next.url, next.host, next.port, next.method, next.expectedStatus, next.intervalSeconds, next.timeoutMs, next.retryCount, next.enabled ? 1 : 0, next.status, next.lastCheckedAt, updatedAt, current.id);
213
+ return this.getMonitor(current.id);
214
+ }
215
+ deleteMonitor(idOrName) {
216
+ const current = this.getMonitor(idOrName);
217
+ if (!current)
218
+ return false;
219
+ this.db.query("DELETE FROM monitors WHERE id = ?").run(current.id);
220
+ return true;
221
+ }
222
+ recordCheckResult(input) {
223
+ const monitor = this.getMonitor(input.monitorId);
224
+ if (!monitor)
225
+ throw new Error(`Monitor not found: ${input.monitorId}`);
226
+ const checkedAt = input.checkedAt ?? new Date().toISOString();
227
+ const result = {
228
+ id: newId("chk"),
229
+ monitorId: monitor.id,
230
+ checkedAt,
231
+ status: input.status,
232
+ latencyMs: input.latencyMs,
233
+ statusCode: input.statusCode,
234
+ error: input.error,
235
+ attemptCount: Math.max(1, input.attemptCount)
236
+ };
237
+ const tx = this.db.transaction(() => {
238
+ this.db.query(`INSERT INTO check_results (
239
+ id, monitor_id, checked_at, status, latency_ms, status_code, error, attempt_count
240
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(result.id, result.monitorId, result.checkedAt, result.status, result.latencyMs, result.statusCode, result.error, result.attemptCount);
241
+ this.db.query("UPDATE monitors SET status = ?, last_checked_at = ?, updated_at = ? WHERE id = ?").run(result.status, result.checkedAt, result.checkedAt, result.monitorId);
242
+ this.reconcileIncidentInTransaction(result);
243
+ });
244
+ tx();
245
+ return result;
246
+ }
247
+ listResults(options = {}) {
248
+ const limit = clampLimit(options.limit ?? 50);
249
+ const rows = options.monitorId ? this.db.query("SELECT * FROM check_results WHERE monitor_id = ? ORDER BY checked_at DESC LIMIT ?").all(options.monitorId, limit) : this.db.query("SELECT * FROM check_results ORDER BY checked_at DESC LIMIT ?").all(limit);
250
+ return rows.map(checkResultFromRow);
251
+ }
252
+ listIncidents(options = {}) {
253
+ const clauses = [];
254
+ const args = [];
255
+ if (options.status) {
256
+ clauses.push("status = ?");
257
+ args.push(options.status);
258
+ }
259
+ if (options.monitorId) {
260
+ clauses.push("monitor_id = ?");
261
+ args.push(options.monitorId);
262
+ }
263
+ const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
264
+ args.push(clampLimit(options.limit ?? 50));
265
+ const rows = this.db.query(`SELECT * FROM incidents ${where} ORDER BY opened_at DESC LIMIT ?`).all(...args);
266
+ return rows.map(incidentFromRow);
267
+ }
268
+ getOpenIncident(monitorId) {
269
+ const row = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(monitorId);
270
+ return row ? incidentFromRow(row) : null;
271
+ }
272
+ summary() {
273
+ const monitors = this.listMonitors({ includeDisabled: true });
274
+ const summaries = monitors.map((monitor) => this.monitorSummary(monitor));
275
+ return {
276
+ generatedAt: new Date().toISOString(),
277
+ monitors: summaries,
278
+ totals: {
279
+ monitors: monitors.length,
280
+ enabled: monitors.filter((m) => m.enabled).length,
281
+ up: monitors.filter((m) => m.status === "up").length,
282
+ down: monitors.filter((m) => m.status === "down").length,
283
+ paused: monitors.filter((m) => !m.enabled || m.status === "paused").length,
284
+ unknown: monitors.filter((m) => m.status === "unknown").length,
285
+ openIncidents: this.listIncidents({ status: "open", limit: 1000 }).length
286
+ }
287
+ };
288
+ }
289
+ monitorSummary(monitor) {
290
+ const row = this.db.query(`SELECT
291
+ COUNT(*) as total,
292
+ SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as up_count,
293
+ SUM(CASE WHEN status = 'down' THEN 1 ELSE 0 END) as down_count,
294
+ AVG(CASE WHEN status = 'up' THEN latency_ms ELSE NULL END) as avg_latency
295
+ FROM check_results WHERE monitor_id = ?`).get(monitor.id);
296
+ const total = Number(row.total ?? 0);
297
+ const up = Number(row.up_count ?? 0);
298
+ const down = Number(row.down_count ?? 0);
299
+ return {
300
+ monitor,
301
+ totalChecks: total,
302
+ upChecks: up,
303
+ downChecks: down,
304
+ uptimePercent: total > 0 ? round(up / total * 100, 4) : null,
305
+ averageLatencyMs: row.avg_latency == null ? null : round(row.avg_latency, 2),
306
+ openIncident: this.getOpenIncident(monitor.id)
307
+ };
308
+ }
309
+ reconcileIncidentInTransaction(result) {
310
+ const open = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(result.monitorId);
311
+ if (result.status === "down") {
312
+ if (open) {
313
+ this.db.query("UPDATE incidents SET last_failure_at = ?, failure_count = failure_count + 1, reason = COALESCE(?, reason) WHERE id = ?").run(result.checkedAt, result.error, open.id);
314
+ } else {
315
+ this.db.query(`INSERT INTO incidents (
316
+ id, monitor_id, status, opened_at, closed_at, last_failure_at,
317
+ failure_count, recovery_check_id, reason
318
+ ) VALUES (?, ?, 'open', ?, NULL, ?, 1, NULL, ?)`).run(newId("inc"), result.monitorId, result.checkedAt, result.checkedAt, result.error);
319
+ }
320
+ return;
321
+ }
322
+ if (open) {
323
+ this.db.query("UPDATE incidents SET status = 'closed', closed_at = ?, recovery_check_id = ? WHERE id = ?").run(result.checkedAt, result.id, open.id);
324
+ }
325
+ }
326
+ }
327
+ function normalizeCreateMonitor(input) {
328
+ const name = input.name?.trim();
329
+ if (!name)
330
+ throw new Error("Monitor name is required");
331
+ const method = normalizeMethod(input.method ?? "GET");
332
+ const expectedStatus = normalizeExpectedStatus(input.expectedStatus);
333
+ if (input.kind === "http") {
334
+ const url = normalizeHttpUrl(input.url);
335
+ return {
336
+ name,
337
+ kind: input.kind,
338
+ url,
339
+ method,
340
+ expectedStatus,
341
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
342
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
343
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
344
+ enabled: input.enabled ?? true
345
+ };
346
+ } else if (input.kind === "tcp") {
347
+ const host = input.host?.trim();
348
+ if (!host)
349
+ throw new Error("TCP monitors require host");
350
+ if (!Number.isInteger(input.port) || input.port <= 0 || input.port > 65535) {
351
+ throw new Error("TCP monitors require a port from 1 to 65535");
352
+ }
353
+ return {
354
+ name,
355
+ kind: input.kind,
356
+ host,
357
+ port: input.port,
358
+ method,
359
+ expectedStatus: null,
360
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
361
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
362
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
363
+ enabled: input.enabled ?? true
364
+ };
365
+ } else {
366
+ throw new Error("Monitor kind must be http or tcp");
367
+ }
368
+ }
369
+ function normalizeUpdateMonitor(current, input, updatedAt) {
370
+ const merged = {
371
+ ...current,
372
+ ...input,
373
+ expectedStatus: input.expectedStatus === undefined ? current.expectedStatus : input.expectedStatus,
374
+ updatedAt
375
+ };
376
+ const normalized = normalizeCreateMonitor({
377
+ name: merged.name,
378
+ kind: merged.kind,
379
+ url: merged.url ?? undefined,
380
+ host: merged.host ?? undefined,
381
+ port: merged.port ?? undefined,
382
+ method: merged.method,
383
+ expectedStatus: merged.expectedStatus,
384
+ intervalSeconds: merged.intervalSeconds,
385
+ timeoutMs: merged.timeoutMs,
386
+ retryCount: merged.retryCount,
387
+ enabled: merged.enabled
388
+ });
389
+ const checkDefinitionChanged = normalized.kind !== current.kind || (normalized.url ?? null) !== current.url || (normalized.host ?? null) !== current.host || (normalized.port ?? null) !== current.port || normalized.method !== current.method || normalized.expectedStatus !== current.expectedStatus;
390
+ const status = normalized.enabled ? checkDefinitionChanged || !current.enabled ? "unknown" : current.status : "paused";
391
+ return {
392
+ ...current,
393
+ name: normalized.name,
394
+ kind: normalized.kind,
395
+ url: normalized.url ?? null,
396
+ host: normalized.host ?? null,
397
+ port: normalized.port ?? null,
398
+ method: normalized.method,
399
+ expectedStatus: normalized.expectedStatus,
400
+ intervalSeconds: normalized.intervalSeconds,
401
+ timeoutMs: normalized.timeoutMs,
402
+ retryCount: normalized.retryCount,
403
+ enabled: normalized.enabled,
404
+ status,
405
+ lastCheckedAt: checkDefinitionChanged ? null : current.lastCheckedAt,
406
+ updatedAt
407
+ };
408
+ }
409
+ function normalizeHttpUrl(value) {
410
+ const raw = value?.trim();
411
+ if (!raw)
412
+ throw new Error("HTTP monitors require url");
413
+ const parsed = new URL(raw);
414
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
415
+ throw new Error("HTTP monitor url must use http or https");
416
+ }
417
+ return parsed.toString();
418
+ }
419
+ function normalizeMethod(value) {
420
+ const method = value.trim().toUpperCase();
421
+ if (!/^[A-Z]+$/.test(method))
422
+ throw new Error("HTTP method must contain only letters");
423
+ return method;
424
+ }
425
+ function normalizeExpectedStatus(value) {
426
+ if (value == null)
427
+ return null;
428
+ if (!Number.isInteger(value) || value < 100 || value > 599) {
429
+ throw new Error("expectedStatus must be an HTTP status from 100 to 599");
430
+ }
431
+ return value;
432
+ }
433
+ function monitorFromRow(row) {
434
+ return {
435
+ id: row.id,
436
+ name: row.name,
437
+ kind: row.kind,
438
+ url: row.url,
439
+ host: row.host,
440
+ port: row.port,
441
+ method: row.method,
442
+ expectedStatus: row.expected_status,
443
+ intervalSeconds: row.interval_seconds,
444
+ timeoutMs: row.timeout_ms,
445
+ retryCount: row.retry_count,
446
+ enabled: Boolean(row.enabled),
447
+ status: row.status,
448
+ lastCheckedAt: row.last_checked_at,
449
+ createdAt: row.created_at,
450
+ updatedAt: row.updated_at
451
+ };
452
+ }
453
+ function checkResultFromRow(row) {
454
+ return {
455
+ id: row.id,
456
+ monitorId: row.monitor_id,
457
+ checkedAt: row.checked_at,
458
+ status: row.status,
459
+ latencyMs: row.latency_ms,
460
+ statusCode: row.status_code,
461
+ error: row.error,
462
+ attemptCount: row.attempt_count
463
+ };
464
+ }
465
+ function incidentFromRow(row) {
466
+ return {
467
+ id: row.id,
468
+ monitorId: row.monitor_id,
469
+ status: row.status,
470
+ openedAt: row.opened_at,
471
+ closedAt: row.closed_at,
472
+ lastFailureAt: row.last_failure_at,
473
+ failureCount: row.failure_count,
474
+ recoveryCheckId: row.recovery_check_id,
475
+ reason: row.reason
476
+ };
477
+ }
478
+ function newId(prefix) {
479
+ return `${prefix}_${randomUUID().replace(/-/g, "").slice(0, 18)}`;
480
+ }
481
+ function boundedInteger(value, label, min, max) {
482
+ if (!Number.isInteger(value) || value < min || value > max) {
483
+ throw new Error(`${label} must be an integer from ${min} to ${max}`);
484
+ }
485
+ return value;
486
+ }
487
+ function clampLimit(value) {
488
+ if (!Number.isFinite(value))
489
+ return 50;
490
+ return Math.max(1, Math.min(Math.floor(value), MAX_RESULT_LIMIT));
491
+ }
492
+ function round(value, places) {
493
+ const factor = 10 ** places;
494
+ return Math.round(value * factor) / factor;
495
+ }
496
+
497
+ // src/service.ts
498
+ class UptimeService {
499
+ store;
500
+ checkRunner;
501
+ inFlightChecks = new Set;
502
+ constructor(options = {}) {
503
+ this.store = options.store ?? new UptimeStore(options);
504
+ this.checkRunner = options.checkRunner ?? runMonitorCheck;
505
+ }
506
+ close() {
507
+ this.store.close();
508
+ }
509
+ createMonitor(input) {
510
+ return this.store.createMonitor(input);
511
+ }
512
+ updateMonitor(idOrName, input) {
513
+ return this.store.updateMonitor(idOrName, input);
514
+ }
515
+ deleteMonitor(idOrName) {
516
+ return this.store.deleteMonitor(idOrName);
517
+ }
518
+ listMonitors(options = {}) {
519
+ return this.store.listMonitors(options);
520
+ }
521
+ getMonitor(idOrName) {
522
+ return this.store.getMonitor(idOrName);
523
+ }
524
+ listResults(options = {}) {
525
+ return this.store.listResults(options);
526
+ }
527
+ listIncidents(options = {}) {
528
+ return this.store.listIncidents(options);
529
+ }
530
+ summary() {
531
+ return this.store.summary();
532
+ }
533
+ async checkMonitor(idOrName) {
534
+ const monitor = this.store.getMonitor(idOrName);
535
+ if (!monitor)
536
+ throw new Error(`Monitor not found: ${idOrName}`);
537
+ if (!monitor.enabled)
538
+ throw new Error(`Monitor is disabled: ${monitor.name}`);
539
+ if (this.inFlightChecks.has(monitor.id))
540
+ throw new Error(`Monitor check already in progress: ${monitor.name}`);
541
+ this.inFlightChecks.add(monitor.id);
542
+ try {
543
+ let attemptCount = 0;
544
+ let last = null;
545
+ const maxAttempts = Math.max(1, monitor.retryCount + 1);
546
+ while (attemptCount < maxAttempts) {
547
+ attemptCount += 1;
548
+ last = await this.checkRunner(monitor);
549
+ if (last.status === "up")
550
+ break;
551
+ }
552
+ return this.store.recordCheckResult({
553
+ monitorId: monitor.id,
554
+ status: last.status,
555
+ latencyMs: last.latencyMs,
556
+ statusCode: last.statusCode ?? null,
557
+ error: last.error ?? null,
558
+ attemptCount
559
+ });
560
+ } finally {
561
+ this.inFlightChecks.delete(monitor.id);
562
+ }
563
+ }
564
+ async checkAll() {
565
+ const monitors = this.store.listMonitors();
566
+ const results = [];
567
+ for (const monitor of monitors) {
568
+ results.push(await this.checkMonitor(monitor.id));
569
+ }
570
+ return results;
571
+ }
572
+ startScheduler(options = {}) {
573
+ const tickMs = options.tickMs ?? 1000;
574
+ const timer = setInterval(() => {
575
+ this.runDueChecks().catch((error) => {
576
+ console.error(error instanceof Error ? error.message : String(error));
577
+ });
578
+ }, tickMs);
579
+ return {
580
+ stop: () => clearInterval(timer)
581
+ };
582
+ }
583
+ async runDueChecks(now = new Date) {
584
+ const due = this.store.listMonitors().filter((monitor) => this.isDue(monitor, now));
585
+ const results = [];
586
+ for (const monitor of due) {
587
+ const current = this.store.getMonitor(monitor.id);
588
+ if (!current || !this.isDue(current, now))
589
+ continue;
590
+ results.push(await this.checkMonitor(current.id));
591
+ }
592
+ return results;
593
+ }
594
+ isDue(monitor, now) {
595
+ if (!monitor.enabled)
596
+ return false;
597
+ if (this.inFlightChecks.has(monitor.id))
598
+ return false;
599
+ if (!monitor.lastCheckedAt)
600
+ return true;
601
+ const last = new Date(monitor.lastCheckedAt).getTime();
602
+ return now.getTime() - last >= monitor.intervalSeconds * 1000;
603
+ }
604
+ }
605
+ function createUptimeClient(options = {}) {
606
+ return new UptimeService(options);
607
+ }
608
+
609
+ // src/dashboard.ts
610
+ function dashboardHtml() {
611
+ return `<!doctype html>
612
+ <html lang="en">
613
+ <head>
614
+ <meta charset="utf-8" />
615
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
616
+ <title>Open Uptime</title>
617
+ <style>
618
+ :root {
619
+ color-scheme: light;
620
+ --bg: #f7f8fb;
621
+ --panel: #ffffff;
622
+ --text: #17202a;
623
+ --muted: #5f6b7a;
624
+ --line: #d8dee8;
625
+ --up: #157347;
626
+ --down: #b42318;
627
+ --warn: #9a6700;
628
+ --accent: #2457c5;
629
+ }
630
+ * { box-sizing: border-box; }
631
+ body {
632
+ margin: 0;
633
+ font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
634
+ background: var(--bg);
635
+ color: var(--text);
636
+ }
637
+ header {
638
+ border-bottom: 1px solid var(--line);
639
+ background: var(--panel);
640
+ padding: 18px 24px;
641
+ display: flex;
642
+ justify-content: space-between;
643
+ gap: 16px;
644
+ align-items: center;
645
+ }
646
+ h1 { margin: 0; font-size: 20px; letter-spacing: 0; }
647
+ main { padding: 24px; max-width: 1180px; margin: 0 auto; }
648
+ .grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
649
+ .panel {
650
+ background: var(--panel);
651
+ border: 1px solid var(--line);
652
+ border-radius: 8px;
653
+ padding: 16px;
654
+ }
655
+ .metric { font-size: 28px; font-weight: 700; margin-top: 6px; }
656
+ .muted { color: var(--muted); font-size: 13px; }
657
+ .toolbar { display: flex; gap: 8px; align-items: center; }
658
+ .stack { display: grid; gap: 16px; margin-top: 16px; }
659
+ .form-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }
660
+ label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; }
661
+ input, select {
662
+ width: 100%;
663
+ border: 1px solid var(--line);
664
+ border-radius: 8px;
665
+ padding: 9px 10px;
666
+ background: white;
667
+ color: var(--text);
668
+ font: inherit;
669
+ }
670
+ button {
671
+ border: 1px solid var(--line);
672
+ border-radius: 8px;
673
+ background: var(--panel);
674
+ color: var(--text);
675
+ padding: 8px 12px;
676
+ cursor: pointer;
677
+ font-weight: 600;
678
+ }
679
+ button.primary { background: var(--accent); color: white; border-color: var(--accent); }
680
+ button.danger { color: var(--down); }
681
+ table { width: 100%; border-collapse: collapse; margin-top: 16px; }
682
+ th, td { text-align: left; border-bottom: 1px solid var(--line); padding: 11px 8px; vertical-align: top; }
683
+ th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
684
+ .badge { display: inline-flex; min-width: 72px; justify-content: center; border-radius: 999px; padding: 4px 8px; font-size: 12px; font-weight: 700; }
685
+ .up { background: #dcfce7; color: var(--up); }
686
+ .down { background: #fee2e2; color: var(--down); }
687
+ .paused { background: #fef3c7; color: var(--warn); }
688
+ .unknown { background: #e5e7eb; color: #374151; }
689
+ .row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
690
+ @media (max-width: 760px) {
691
+ header { align-items: flex-start; flex-direction: column; }
692
+ main { padding: 16px; }
693
+ table { display: block; overflow-x: auto; white-space: nowrap; }
694
+ }
695
+ </style>
696
+ </head>
697
+ <body>
698
+ <header>
699
+ <div>
700
+ <h1>Open Uptime</h1>
701
+ <div class="muted">Local uptime and downtime monitoring</div>
702
+ </div>
703
+ <div class="toolbar">
704
+ <button id="check-all" class="primary">Run Checks</button>
705
+ <button id="refresh">Refresh</button>
706
+ </div>
707
+ </header>
708
+ <main>
709
+ <section class="grid" id="metrics"></section>
710
+ <section class="panel" style="margin-top:16px">
711
+ <strong>Add Monitor</strong>
712
+ <form id="monitor-form" class="form-grid">
713
+ <label>Name<input id="form-name" required /></label>
714
+ <label>Kind<select id="form-kind"><option value="http">HTTP</option><option value="tcp">TCP</option></select></label>
715
+ <label>URL<input id="form-url" placeholder="https://example.com/health" /></label>
716
+ <label>Host<input id="form-host" placeholder="127.0.0.1" /></label>
717
+ <label>Port<input id="form-port" type="number" min="1" max="65535" /></label>
718
+ <label>Interval<input id="form-interval" type="number" min="1" value="60" /></label>
719
+ <label>Timeout<input id="form-timeout" type="number" min="1" value="5000" /></label>
720
+ <button id="form-submit" class="primary" type="submit">Add</button>
721
+ <button id="form-cancel" type="button">Cancel</button>
722
+ </form>
723
+ <div class="muted" id="form-status"></div>
724
+ </section>
725
+ <section class="panel" style="margin-top:16px">
726
+ <div style="display:flex;justify-content:space-between;gap:12px;align-items:center">
727
+ <div>
728
+ <strong>Monitors</strong>
729
+ <div class="muted" id="generated"></div>
730
+ </div>
731
+ </div>
732
+ <table>
733
+ <thead>
734
+ <tr><th>Status</th><th>Name</th><th>Target</th><th>Uptime</th><th>Latency</th><th>Last Check</th><th>Incident</th><th></th></tr>
735
+ </thead>
736
+ <tbody id="monitors"></tbody>
737
+ </table>
738
+ </section>
739
+ <section class="stack">
740
+ <section class="panel">
741
+ <strong>Recent Results</strong>
742
+ <table>
743
+ <thead><tr><th>Status</th><th>Monitor</th><th>Checked</th><th>Latency</th><th>Error</th></tr></thead>
744
+ <tbody id="results"></tbody>
745
+ </table>
746
+ </section>
747
+ <section class="panel">
748
+ <strong>Incidents</strong>
749
+ <table>
750
+ <thead><tr><th>Status</th><th>Monitor</th><th>Opened</th><th>Closed</th><th>Failures</th><th>Reason</th></tr></thead>
751
+ <tbody id="incidents"></tbody>
752
+ </table>
753
+ </section>
754
+ </section>
755
+ </main>
756
+ <script>
757
+ let monitorCache = [];
758
+ let editingId = null;
759
+ const fmt = (value) => value == null || value === '' ? '-' : String(value);
760
+ const pct = (value) => value == null ? '-' : Number(value).toFixed(2) + '%';
761
+ const byId = (id) => document.getElementById(id);
762
+ const text = (value) => document.createTextNode(fmt(value));
763
+ function clear(node) { while (node.firstChild) node.removeChild(node.firstChild); }
764
+ function cell(value) {
765
+ const td = document.createElement('td');
766
+ td.appendChild(text(value));
767
+ return td;
768
+ }
769
+ function statusCell(status) {
770
+ const td = document.createElement('td');
771
+ const span = document.createElement('span');
772
+ const safe = ['up', 'down', 'paused', 'unknown'].includes(status) ? status : 'unknown';
773
+ span.className = 'badge ' + safe;
774
+ span.textContent = status || 'unknown';
775
+ td.appendChild(span);
776
+ return td;
777
+ }
778
+ function button(label, handler, className) {
779
+ const btn = document.createElement('button');
780
+ btn.type = 'button';
781
+ btn.textContent = label;
782
+ if (className) btn.className = className;
783
+ btn.addEventListener('click', handler);
784
+ return btn;
785
+ }
786
+ async function load() {
787
+ const [summary, results, incidents] = await Promise.all([
788
+ fetch('/api/summary').then((r) => r.json()),
789
+ fetch('/api/results?limit=20').then((r) => r.json()),
790
+ fetch('/api/incidents?limit=20').then((r) => r.json()),
791
+ ]);
792
+ monitorCache = summary.monitors.map((item) => item.monitor);
793
+ byId('generated').textContent = 'Generated ' + new Date(summary.generatedAt).toLocaleString();
794
+ renderMetrics(summary);
795
+ renderMonitors(summary);
796
+ renderResults(results);
797
+ renderIncidents(incidents);
798
+ }
799
+ function renderMetrics(summary) {
800
+ const root = byId('metrics');
801
+ clear(root);
802
+ for (const [label, value] of [
803
+ ['Monitors', summary.totals.monitors],
804
+ ['Up', summary.totals.up],
805
+ ['Down', summary.totals.down],
806
+ ['Open incidents', summary.totals.openIncidents],
807
+ ]) {
808
+ const panel = document.createElement('div');
809
+ panel.className = 'panel';
810
+ const small = document.createElement('div');
811
+ small.className = 'muted';
812
+ small.textContent = label;
813
+ const metric = document.createElement('div');
814
+ metric.className = 'metric';
815
+ metric.textContent = value;
816
+ panel.append(small, metric);
817
+ root.appendChild(panel);
818
+ }
819
+ }
820
+ function renderMonitors(summary) {
821
+ const root = byId('monitors');
822
+ clear(root);
823
+ for (const item of summary.monitors) {
824
+ const m = item.monitor;
825
+ const target = m.kind === 'http' ? m.url : m.host + ':' + m.port;
826
+ const incident = item.openIncident ? 'open since ' + new Date(item.openIncident.openedAt).toLocaleString() : '-';
827
+ const tr = document.createElement('tr');
828
+ const name = document.createElement('td');
829
+ const strong = document.createElement('strong');
830
+ strong.textContent = m.name;
831
+ const kind = document.createElement('div');
832
+ kind.className = 'muted';
833
+ kind.textContent = m.kind;
834
+ name.append(strong, kind);
835
+ const actions = document.createElement('td');
836
+ actions.className = 'row-actions';
837
+ actions.append(
838
+ button('Check', () => checkOne(m.id)),
839
+ button(m.enabled ? 'Pause' : 'Resume', () => setEnabled(m.id, !m.enabled)),
840
+ button('Edit', () => fillForm(m.id)),
841
+ button('Delete', () => deleteMonitor(m.id), 'danger'),
842
+ );
843
+ tr.append(
844
+ statusCell(m.status),
845
+ name,
846
+ cell(target),
847
+ cell(pct(item.uptimePercent)),
848
+ cell(item.averageLatencyMs == null ? '-' : item.averageLatencyMs + ' ms'),
849
+ cell(m.lastCheckedAt ? new Date(m.lastCheckedAt).toLocaleString() : '-'),
850
+ cell(incident),
851
+ actions,
852
+ );
853
+ root.appendChild(tr);
854
+ }
855
+ }
856
+ function renderResults(results) {
857
+ const root = byId('results');
858
+ clear(root);
859
+ for (const result of results) {
860
+ const tr = document.createElement('tr');
861
+ tr.append(
862
+ statusCell(result.status),
863
+ cell(result.monitorId),
864
+ cell(new Date(result.checkedAt).toLocaleString()),
865
+ cell(result.latencyMs == null ? '-' : result.latencyMs + ' ms'),
866
+ cell(result.error),
867
+ );
868
+ root.appendChild(tr);
869
+ }
870
+ }
871
+ function renderIncidents(incidents) {
872
+ const root = byId('incidents');
873
+ clear(root);
874
+ for (const incident of incidents) {
875
+ const tr = document.createElement('tr');
876
+ tr.append(
877
+ statusCell(incident.status),
878
+ cell(incident.monitorId),
879
+ cell(new Date(incident.openedAt).toLocaleString()),
880
+ cell(incident.closedAt ? new Date(incident.closedAt).toLocaleString() : '-'),
881
+ cell(incident.failureCount),
882
+ cell(incident.reason),
883
+ );
884
+ root.appendChild(tr);
885
+ }
886
+ }
887
+ async function checkOne(id) {
888
+ await fetch('/api/monitors/' + encodeURIComponent(id) + '/check', { method: 'POST' });
889
+ await load();
890
+ }
891
+ async function setEnabled(id, enabled) {
892
+ await fetch('/api/monitors/' + encodeURIComponent(id), {
893
+ method: 'PATCH',
894
+ headers: { 'content-type': 'application/json' },
895
+ body: JSON.stringify({ enabled }),
896
+ });
897
+ await load();
898
+ }
899
+ async function deleteMonitor(id) {
900
+ await fetch('/api/monitors/' + encodeURIComponent(id), { method: 'DELETE' });
901
+ await load();
902
+ }
903
+ function fillForm(id) {
904
+ const m = monitorCache.find((item) => item.id === id);
905
+ if (!m) return;
906
+ editingId = m.id;
907
+ byId('form-name').value = m.name;
908
+ byId('form-kind').value = m.kind;
909
+ byId('form-url').value = m.url || '';
910
+ byId('form-host').value = m.host || '';
911
+ byId('form-port').value = m.port || '';
912
+ byId('form-interval').value = m.intervalSeconds;
913
+ byId('form-timeout').value = m.timeoutMs;
914
+ byId('form-submit').textContent = 'Save';
915
+ byId('form-status').textContent = ['Editing', m.name].join(' ');
916
+ }
917
+ function resetForm() {
918
+ editingId = null;
919
+ byId('monitor-form').reset();
920
+ byId('form-submit').textContent = 'Add';
921
+ byId('form-status').textContent = '';
922
+ }
923
+ byId('monitor-form').addEventListener('submit', async (event) => {
924
+ event.preventDefault();
925
+ const kind = byId('form-kind').value;
926
+ const body = {
927
+ name: byId('form-name').value,
928
+ kind,
929
+ intervalSeconds: Number(byId('form-interval').value || 60),
930
+ timeoutMs: Number(byId('form-timeout').value || 5000),
931
+ };
932
+ if (kind === 'http') body.url = byId('form-url').value;
933
+ else {
934
+ body.host = byId('form-host').value;
935
+ body.port = Number(byId('form-port').value);
936
+ }
937
+ const response = await fetch(editingId ? '/api/monitors/' + encodeURIComponent(editingId) : '/api/monitors', {
938
+ method: editingId ? 'PATCH' : 'POST',
939
+ headers: { 'content-type': 'application/json' },
940
+ body: JSON.stringify(body),
941
+ });
942
+ const payload = await response.json();
943
+ byId('form-status').textContent = response.ok ? [editingId ? 'Saved' : 'Added', payload.name].join(' ') : payload.error;
944
+ if (response.ok) resetForm();
945
+ await load();
946
+ });
947
+ byId('form-cancel').addEventListener('click', resetForm);
948
+ byId('refresh').addEventListener('click', load);
949
+ byId('check-all').addEventListener('click', async () => {
950
+ await fetch('/api/check-all', { method: 'POST' });
951
+ await load();
952
+ });
953
+ load();
954
+ </script>
955
+ </body>
956
+ </html>`;
957
+ }
958
+
959
+ // src/api.ts
960
+ function createApiHandler(service) {
961
+ return async (request) => {
962
+ const url = new URL(request.url);
963
+ try {
964
+ validateLocalMutationRequest(request, url);
965
+ if (request.method === "GET" && url.pathname === "/") {
966
+ return html(dashboardHtml());
967
+ }
968
+ if (request.method === "GET" && url.pathname === "/health") {
969
+ return json({ ok: true, service: "uptime" });
970
+ }
971
+ if (request.method === "GET" && url.pathname === "/api/summary") {
972
+ return json(service.summary());
973
+ }
974
+ if (request.method === "GET" && url.pathname === "/api/monitors") {
975
+ return json(service.listMonitors({ includeDisabled: url.searchParams.get("includeDisabled") === "true" }));
976
+ }
977
+ if (request.method === "POST" && url.pathname === "/api/monitors") {
978
+ return json(service.createMonitor(await jsonBody(request)), 201);
979
+ }
980
+ if (request.method === "GET" && url.pathname === "/api/incidents") {
981
+ const status = url.searchParams.get("status");
982
+ return json(service.listIncidents({
983
+ status: status === "open" || status === "closed" ? status : undefined,
984
+ monitorId: url.searchParams.get("monitorId") ?? undefined,
985
+ limit: numericParam(url, "limit", 50)
986
+ }));
987
+ }
988
+ if (request.method === "GET" && url.pathname === "/api/results") {
989
+ return json(service.listResults({
990
+ monitorId: url.searchParams.get("monitorId") ?? undefined,
991
+ limit: numericParam(url, "limit", 50)
992
+ }));
993
+ }
994
+ if (request.method === "POST" && url.pathname === "/api/check-all") {
995
+ return json(await service.checkAll());
996
+ }
997
+ const monitorMatch = url.pathname.match(/^\/api\/monitors\/([^/]+)(?:\/(check))?$/);
998
+ if (monitorMatch) {
999
+ const id = decodeURIComponent(monitorMatch[1]);
1000
+ if (request.method === "GET" && !monitorMatch[2]) {
1001
+ const monitor = service.getMonitor(id);
1002
+ return monitor ? json(monitor) : json({ error: "not found" }, 404);
1003
+ }
1004
+ if (request.method === "PATCH" && !monitorMatch[2]) {
1005
+ return json(service.updateMonitor(id, await jsonBody(request)));
1006
+ }
1007
+ if (request.method === "DELETE" && !monitorMatch[2]) {
1008
+ return json({ deleted: service.deleteMonitor(id) });
1009
+ }
1010
+ if (request.method === "POST" && monitorMatch[2] === "check") {
1011
+ return json(await service.checkMonitor(id));
1012
+ }
1013
+ }
1014
+ return json({ error: "not found" }, 404);
1015
+ } catch (error) {
1016
+ return json({ error: error instanceof Error ? error.message : String(error) }, error instanceof ApiError ? error.status : 400);
1017
+ }
1018
+ };
1019
+ }
1020
+ function serveUptime(options = {}) {
1021
+ const service = options.service ?? new UptimeService(options);
1022
+ const scheduler = options.check ? service.startScheduler() : undefined;
1023
+ const server = Bun.serve({
1024
+ hostname: options.host ?? "127.0.0.1",
1025
+ port: options.port ?? 3899,
1026
+ fetch: createApiHandler(service)
1027
+ });
1028
+ return { server, service, scheduler };
1029
+ }
1030
+ function json(value, status = 200) {
1031
+ return new Response(JSON.stringify(value, null, 2), {
1032
+ status,
1033
+ headers: {
1034
+ "content-type": "application/json; charset=utf-8",
1035
+ "cache-control": "no-store"
1036
+ }
1037
+ });
1038
+ }
1039
+ function html(value) {
1040
+ return new Response(value, {
1041
+ headers: {
1042
+ "content-type": "text/html; charset=utf-8",
1043
+ "cache-control": "no-store"
1044
+ }
1045
+ });
1046
+ }
1047
+ function numericParam(url, name, fallback) {
1048
+ const raw = url.searchParams.get(name);
1049
+ if (!raw)
1050
+ return fallback;
1051
+ const parsed = Number(raw);
1052
+ return Number.isFinite(parsed) ? parsed : fallback;
1053
+ }
1054
+ function validateLocalMutationRequest(request, url) {
1055
+ if (!["POST", "PATCH", "DELETE"].includes(request.method))
1056
+ return;
1057
+ const origin = request.headers.get("origin");
1058
+ if (origin && origin !== `${url.protocol}//${url.host}`) {
1059
+ throw new ApiError("cross-origin mutation rejected", 403);
1060
+ }
1061
+ }
1062
+ async function jsonBody(request) {
1063
+ const contentType = request.headers.get("content-type") ?? "";
1064
+ const mediaType = contentType.split(";")[0]?.trim().toLowerCase();
1065
+ if (mediaType !== "application/json" && !mediaType.endsWith("+json")) {
1066
+ throw new ApiError("content-type must be application/json", 415);
1067
+ }
1068
+ return request.json();
1069
+ }
1070
+
1071
+ class ApiError extends Error {
1072
+ status;
1073
+ constructor(message, status) {
1074
+ super(message);
1075
+ this.status = status;
1076
+ }
1077
+ }
1078
+ export {
1079
+ uptimeHome,
1080
+ uptimeDbPath,
1081
+ serveUptime,
1082
+ runTcpCheck,
1083
+ runMonitorCheck,
1084
+ runHttpCheck,
1085
+ ensureUptimeHome,
1086
+ createUptimeClient,
1087
+ createApiHandler,
1088
+ UptimeStore,
1089
+ UptimeService
1090
+ };