leedab 0.2.6 → 0.3.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.
@@ -1,2 +0,0 @@
1
- import type { LeedABConfig } from "../config/schema.js";
2
- export declare function startDashboard(config: LeedABConfig, port?: number): Promise<void>;
@@ -1,85 +0,0 @@
1
- import { createServer } from "node:http";
2
- import { readFile } from "node:fs/promises";
3
- import { resolve, extname } from "node:path";
4
- import { createRoutes } from "./routes.js";
5
- const MIME_TYPES = {
6
- ".html": "text/html",
7
- ".css": "text/css",
8
- ".js": "application/javascript",
9
- ".json": "application/json",
10
- ".svg": "image/svg+xml",
11
- ".png": "image/png",
12
- };
13
- export async function startDashboard(config, port = 3000) {
14
- const routes = createRoutes(config);
15
- // In dev, serve from src/ so HTML/CSS changes are instant (no rebuild needed).
16
- // In production (running from dist/), fall back to the compiled copy.
17
- const devStaticDir = resolve(import.meta.dirname, "../../src/dashboard/static");
18
- const prodStaticDir = resolve(import.meta.dirname, "static");
19
- const { existsSync } = await import("node:fs");
20
- const staticDir = existsSync(devStaticDir) ? devStaticDir : prodStaticDir;
21
- const server = createServer(async (req, res) => {
22
- const url = new URL(req.url ?? "/", `http://localhost:${port}`);
23
- const method = req.method ?? "GET";
24
- // API routes
25
- if (url.pathname.startsWith("/api/")) {
26
- const handler = routes[`${method} ${url.pathname}`];
27
- if (handler) {
28
- try {
29
- await handler(req, res, url);
30
- }
31
- catch (err) {
32
- res.writeHead(500, { "Content-Type": "application/json" });
33
- res.end(JSON.stringify({ error: err.message }));
34
- }
35
- return;
36
- }
37
- res.writeHead(404, { "Content-Type": "application/json" });
38
- res.end(JSON.stringify({ error: "not found" }));
39
- return;
40
- }
41
- // Static files — try .html extension for clean URLs (/console -> /console.html)
42
- let filePath = url.pathname === "/" ? "/index.html" : url.pathname;
43
- if (!extname(filePath))
44
- filePath += ".html";
45
- try {
46
- const fullPath = resolve(staticDir, filePath.slice(1));
47
- const content = await readFile(fullPath);
48
- const ext = extname(fullPath);
49
- res.writeHead(200, { "Content-Type": MIME_TYPES[ext] ?? "text/plain" });
50
- res.end(content);
51
- }
52
- catch {
53
- // SPA fallback
54
- try {
55
- const index = await readFile(resolve(staticDir, "index.html"));
56
- res.writeHead(200, { "Content-Type": "text/html" });
57
- res.end(index);
58
- }
59
- catch {
60
- res.writeHead(404);
61
- res.end("Not found");
62
- }
63
- }
64
- });
65
- return new Promise((resolve, reject) => {
66
- server.on("error", (err) => {
67
- if (err.code === "EADDRINUSE") {
68
- // Try next port
69
- const nextPort = port + 1;
70
- console.log(` Port ${port} in use, trying ${nextPort}...`);
71
- server.listen(nextPort, "127.0.0.1");
72
- server.once("listening", () => {
73
- console.log(` Dashboard: http://localhost:${nextPort}`);
74
- resolve();
75
- });
76
- }
77
- else {
78
- reject(err);
79
- }
80
- });
81
- server.listen(port, "127.0.0.1", () => {
82
- resolve();
83
- });
84
- });
85
- }