my-durable 0.0.37 → 0.0.38
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/README.md +1 -0
- package/apps/dpi-backend/src/doctor.ts +426 -0
- package/apps/dpi-backend/src/routes.ts +7 -0
- package/apps/dpi-backend/src/store.ts +121 -0
- package/apps/dpi-backend/src/vitals.ts +94 -0
- package/apps/dpi-backend/src/zip.ts +117 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-D4M8tCcs.js +81 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-xOvj8ub1.css +1 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +2 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/HostPanel.vue +224 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Inspector.vue +37 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NetworkSidebar.vue +120 -4
- package/apps/dpi-sdk/src/api.ts +41 -0
- package/apps/dpi-sdk/src/client.ts +17 -0
- package/apps/dpi-sdk/src/protocol/rest-routes.ts +3 -0
- package/bin/dpi.mjs +11 -0
- package/docs/host-project-checklist.md +5 -5
- package/package.json +2 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-BrDwRiiN.css +0 -1
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-DdEAB2rE.js +0 -81
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device vitals: the one screen that answers "is this box healthy?" without
|
|
3
|
+
* opening a terminal. Mirrors the shape herdr prints for a device (`uptime`,
|
|
4
|
+
* `since`) and piggery's `top`, and reuses the statfs disk guard pattern from
|
|
5
|
+
* the hub's audit volume check (`audit.ts` `volumeFreeBytes`).
|
|
6
|
+
*
|
|
7
|
+
* Everything here is cheap and synchronous — no polling on our side, the UIs
|
|
8
|
+
* ask when they are looked at.
|
|
9
|
+
*/
|
|
10
|
+
import { statfsSync } from "node:fs";
|
|
11
|
+
import { cpus, loadavg, uptime as osUptime, freemem, totalmem } from "node:os";
|
|
12
|
+
|
|
13
|
+
export interface DiskVitals {
|
|
14
|
+
path: string;
|
|
15
|
+
totalBytes: number;
|
|
16
|
+
freeBytes: number;
|
|
17
|
+
/** False when the platform/filesystem cannot answer (Windows without statfs). */
|
|
18
|
+
known: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface DeviceVitals {
|
|
22
|
+
/** Seconds the host has been up. */
|
|
23
|
+
uptime: number;
|
|
24
|
+
/** Seconds the device process has been running (the daemon's own age). */
|
|
25
|
+
processUptime: number;
|
|
26
|
+
/** 1/5/15-minute load averages; zeros on Windows, which has no equivalent. */
|
|
27
|
+
loadavg: [number, number, number];
|
|
28
|
+
cpuCount: number;
|
|
29
|
+
memory: { totalBytes: number; freeBytes: number; usedBytes: number; processRssBytes: number; heapUsedBytes: number };
|
|
30
|
+
disk: DiskVitals;
|
|
31
|
+
pid: number;
|
|
32
|
+
at: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Free/total bytes for the volume holding `path`; `known: false` instead of throwing. */
|
|
36
|
+
export function diskVitals(path: string): DiskVitals {
|
|
37
|
+
try {
|
|
38
|
+
const stats = statfsSync(path);
|
|
39
|
+
const total = stats.blocks * stats.bsize;
|
|
40
|
+
const free = stats.bavail * stats.bsize;
|
|
41
|
+
// Some filesystems report 0 blocks (synthetic mounts) — treat as unknown
|
|
42
|
+
// rather than claiming the disk is full.
|
|
43
|
+
if (!Number.isFinite(total) || total <= 0) return { path, totalBytes: 0, freeBytes: 0, known: false };
|
|
44
|
+
return { path, totalBytes: total, freeBytes: free, known: true };
|
|
45
|
+
} catch {
|
|
46
|
+
return { path, totalBytes: 0, freeBytes: 0, known: false };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function deviceVitals(diskPath: string, startedAt: number, now = Date.now()): DeviceVitals {
|
|
51
|
+
const load = loadavg();
|
|
52
|
+
const total = totalmem();
|
|
53
|
+
const free = freemem();
|
|
54
|
+
const memory = process.memoryUsage();
|
|
55
|
+
return {
|
|
56
|
+
uptime: Math.round(osUptime()),
|
|
57
|
+
processUptime: Math.max(0, Math.round((now - startedAt) / 1000)),
|
|
58
|
+
loadavg: [load[0] ?? 0, load[1] ?? 0, load[2] ?? 0],
|
|
59
|
+
cpuCount: cpus().length,
|
|
60
|
+
memory: {
|
|
61
|
+
totalBytes: total,
|
|
62
|
+
freeBytes: free,
|
|
63
|
+
usedBytes: Math.max(0, total - free),
|
|
64
|
+
processRssBytes: memory.rss,
|
|
65
|
+
heapUsedBytes: memory.heapUsed,
|
|
66
|
+
},
|
|
67
|
+
disk: diskVitals(diskPath),
|
|
68
|
+
pid: process.pid,
|
|
69
|
+
at: now,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** "3d 4h", "12m", "45s" — the compact form both the CLI and the web card show. */
|
|
74
|
+
export function humanDuration(seconds: number): string {
|
|
75
|
+
const s = Math.max(0, Math.floor(seconds));
|
|
76
|
+
if (s < 60) return `${s}s`;
|
|
77
|
+
const m = Math.floor(s / 60);
|
|
78
|
+
if (m < 60) return `${m}m`;
|
|
79
|
+
const h = Math.floor(m / 60);
|
|
80
|
+
if (h < 24) return `${h}h ${m % 60}m`;
|
|
81
|
+
return `${Math.floor(h / 24)}d ${h % 24}h`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function humanBytes(bytes: number): string {
|
|
85
|
+
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
|
|
86
|
+
const units = ["B", "KiB", "MiB", "GiB", "TiB"];
|
|
87
|
+
let value = bytes;
|
|
88
|
+
let unit = 0;
|
|
89
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
90
|
+
value /= 1024;
|
|
91
|
+
unit += 1;
|
|
92
|
+
}
|
|
93
|
+
return `${value >= 100 || unit === 0 ? Math.round(value) : value.toFixed(1)} ${units[unit]}`;
|
|
94
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal ZIP writer: no dependencies, no shelling out to `zip`.
|
|
3
|
+
*
|
|
4
|
+
* Export bundles need to be one downloadable file, and the device has no
|
|
5
|
+
* archive library. The format only needs the three records a reader actually
|
|
6
|
+
* walks — local header, central directory, end-of-central-directory — so it is
|
|
7
|
+
* cheaper to write them here than to add a dependency for one button.
|
|
8
|
+
*
|
|
9
|
+
* Entries are deflated (`zlib.deflateRawSync`, method 8) or stored (method 0)
|
|
10
|
+
* when deflating makes them bigger (already-compressed or tiny payloads).
|
|
11
|
+
*/
|
|
12
|
+
import { deflateRawSync } from "node:zlib";
|
|
13
|
+
|
|
14
|
+
export interface ZipEntry {
|
|
15
|
+
/** Forward-slash relative path inside the archive. */
|
|
16
|
+
path: string;
|
|
17
|
+
/** UTF-8 text payload. */
|
|
18
|
+
text: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const LOCAL_SIG = 0x04034b50;
|
|
22
|
+
const CENTRAL_SIG = 0x02014b50;
|
|
23
|
+
const EOCD_SIG = 0x06054b50;
|
|
24
|
+
/** Bit 11: file names are UTF-8 (we write non-ASCII session names). */
|
|
25
|
+
const FLAG_UTF8 = 0x0800;
|
|
26
|
+
const VERSION = 20;
|
|
27
|
+
|
|
28
|
+
const CRC_TABLE = (() => {
|
|
29
|
+
const table = new Uint32Array(256);
|
|
30
|
+
for (let i = 0; i < 256; i += 1) {
|
|
31
|
+
let c = i;
|
|
32
|
+
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
33
|
+
table[i] = c >>> 0;
|
|
34
|
+
}
|
|
35
|
+
return table;
|
|
36
|
+
})();
|
|
37
|
+
|
|
38
|
+
export function crc32(data: Uint8Array): number {
|
|
39
|
+
let crc = 0xffffffff;
|
|
40
|
+
for (let i = 0; i < data.length; i += 1) crc = CRC_TABLE[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
|
|
41
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** MS-DOS time/date: 2-second resolution, 1980 epoch. */
|
|
45
|
+
function dosStamp(date: Date): { time: number; date: number } {
|
|
46
|
+
const year = Math.max(1980, date.getFullYear());
|
|
47
|
+
return {
|
|
48
|
+
time: (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1),
|
|
49
|
+
date: ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Build one ZIP archive from text entries. Deterministic apart from the timestamps. */
|
|
54
|
+
export function zipSync(entries: readonly ZipEntry[], at: Date = new Date()): Buffer {
|
|
55
|
+
const stamp = dosStamp(at);
|
|
56
|
+
const locals: Buffer[] = [];
|
|
57
|
+
const centrals: Buffer[] = [];
|
|
58
|
+
let offset = 0;
|
|
59
|
+
|
|
60
|
+
for (const entry of entries) {
|
|
61
|
+
const name = Buffer.from(entry.path.replace(/^\/+/, ""), "utf8");
|
|
62
|
+
const raw = Buffer.from(entry.text, "utf8");
|
|
63
|
+
const deflated = deflateRawSync(raw);
|
|
64
|
+
const useDeflate = deflated.length < raw.length;
|
|
65
|
+
const body = useDeflate ? deflated : raw;
|
|
66
|
+
const crc = crc32(raw);
|
|
67
|
+
|
|
68
|
+
const local = Buffer.alloc(30);
|
|
69
|
+
local.writeUInt32LE(LOCAL_SIG, 0);
|
|
70
|
+
local.writeUInt16LE(VERSION, 4);
|
|
71
|
+
local.writeUInt16LE(FLAG_UTF8, 6);
|
|
72
|
+
local.writeUInt16LE(useDeflate ? 8 : 0, 8);
|
|
73
|
+
local.writeUInt16LE(stamp.time, 10);
|
|
74
|
+
local.writeUInt16LE(stamp.date, 12);
|
|
75
|
+
local.writeUInt32LE(crc, 14);
|
|
76
|
+
local.writeUInt32LE(body.length, 18);
|
|
77
|
+
local.writeUInt32LE(raw.length, 22);
|
|
78
|
+
local.writeUInt16LE(name.length, 26);
|
|
79
|
+
local.writeUInt16LE(0, 28);
|
|
80
|
+
|
|
81
|
+
const central = Buffer.alloc(46);
|
|
82
|
+
central.writeUInt32LE(CENTRAL_SIG, 0);
|
|
83
|
+
central.writeUInt16LE(VERSION, 4);
|
|
84
|
+
central.writeUInt16LE(VERSION, 6);
|
|
85
|
+
central.writeUInt16LE(FLAG_UTF8, 8);
|
|
86
|
+
central.writeUInt16LE(useDeflate ? 8 : 0, 10);
|
|
87
|
+
central.writeUInt16LE(stamp.time, 12);
|
|
88
|
+
central.writeUInt16LE(stamp.date, 14);
|
|
89
|
+
central.writeUInt32LE(crc, 16);
|
|
90
|
+
central.writeUInt32LE(body.length, 20);
|
|
91
|
+
central.writeUInt32LE(raw.length, 24);
|
|
92
|
+
central.writeUInt16LE(name.length, 28);
|
|
93
|
+
central.writeUInt16LE(0, 30); // extra
|
|
94
|
+
central.writeUInt16LE(0, 32); // comment
|
|
95
|
+
central.writeUInt16LE(0, 34); // disk start
|
|
96
|
+
central.writeUInt16LE(0, 36); // internal attrs
|
|
97
|
+
central.writeUInt32LE(0o644 << 16, 38); // external attrs: rw-r--r--
|
|
98
|
+
central.writeUInt32LE(offset, 42);
|
|
99
|
+
|
|
100
|
+
locals.push(local, name, body);
|
|
101
|
+
centrals.push(central, name);
|
|
102
|
+
offset += local.length + name.length + body.length;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const centralBuf = Buffer.concat(centrals);
|
|
106
|
+
const eocd = Buffer.alloc(22);
|
|
107
|
+
eocd.writeUInt32LE(EOCD_SIG, 0);
|
|
108
|
+
eocd.writeUInt16LE(0, 4); // this disk
|
|
109
|
+
eocd.writeUInt16LE(0, 6); // disk with central directory
|
|
110
|
+
eocd.writeUInt16LE(entries.length, 8);
|
|
111
|
+
eocd.writeUInt16LE(entries.length, 10);
|
|
112
|
+
eocd.writeUInt32LE(centralBuf.length, 12);
|
|
113
|
+
eocd.writeUInt32LE(offset, 16);
|
|
114
|
+
eocd.writeUInt16LE(0, 20); // comment length
|
|
115
|
+
|
|
116
|
+
return Buffer.concat([...locals, centralBuf, eocd]);
|
|
117
|
+
}
|