Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor([extract-readcertmeta-helper]): consolidate data/certs/meta.json readers
[._.]/ Adam Eivy5f8a24f6e13f
diff --git a/server/lib/README.md b/server/lib/README.mdindex 75ab9b5ba8..9cd2fcd92d 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -99,6 +99,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| Module | Purpose ||---|---|+| `certMeta.js` | `readCertMeta()` — read `data/certs/meta.json` (mode/hostname/ips) with safe-JSON + missing-file guards. || `httpClient.js` | Fetch-based HTTP client factory (axios.create replacement). || `fetchWithTimeout.js` | `fetch` wrapper with AbortController timeout. || `peerHttpClient.js` | Federation HTTP/Socket.IO client (TLS validation off — Tailnet is the trust boundary). |diff --git a/server/lib/certMeta.js b/server/lib/certMeta.jsnew file mode 100644index 0000000000..e5c37be2a7--- /dev/null+++ b/server/lib/certMeta.js@@ -0,0 +1,14 @@+// Shared reader for the `data/certs/meta.json` marker `npm run setup:cert`+// writes — returns null on missing-or-partial so callers treat "no meta" the+// same as "no cert" without each one re-implementing the guard.+import { readFileSync, statSync } from 'node:fs';+import { PATHS, safeJSONParse } from './fileUtils.js';+import { certPaths } from '../../lib/certPaths.js';++const { meta: META_PATH } = certPaths(PATHS.data);++export function readCertMeta() {+ const stat = statSync(META_PATH, { throwIfNoEntry: false });+ if (!stat) return null;+ return safeJSONParse(readFileSync(META_PATH, 'utf-8'), null);+}diff --git a/server/lib/certMeta.test.js b/server/lib/certMeta.test.jsnew file mode 100644index 0000000000..678d8fb5d8--- /dev/null+++ b/server/lib/certMeta.test.js@@ -0,0 +1,58 @@+import { describe, it, expect, beforeEach, vi } from 'vitest';++vi.mock('./fileUtils.js', () => ({+ PATHS: { data: '/mock/data' },+ safeJSONParse: (s, fallback) => { try { return JSON.parse(s); } catch { return fallback; } },+}));++vi.mock('node:fs', () => ({+ readFileSync: vi.fn(),+ statSync: vi.fn(),+}));++import { readFileSync, statSync } from 'node:fs';+import { readCertMeta } from './certMeta.js';++describe('certMeta.readCertMeta', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it('returns null when meta.json does not exist', () => {+ statSync.mockReturnValue(undefined);+ expect(readCertMeta()).toBeNull();+ expect(readFileSync).not.toHaveBeenCalled();+ });++ it('returns null when meta.json contains malformed JSON (mid-write)', () => {+ statSync.mockReturnValue({ mtimeMs: 1 });+ readFileSync.mockReturnValue('{ "mode": "tailscale", "hostname":');+ expect(readCertMeta()).toBeNull();+ });++ it('returns parsed tailscale meta with mode + hostname + ips', () => {+ statSync.mockReturnValue({ mtimeMs: 1 });+ readFileSync.mockReturnValue(JSON.stringify({+ mode: 'tailscale',+ hostname: 'void.taile8179.ts.net',+ ips: ['100.111.11.146'],+ }));+ expect(readCertMeta()).toEqual({+ mode: 'tailscale',+ hostname: 'void.taile8179.ts.net',+ ips: ['100.111.11.146'],+ });+ });++ it('returns parsed self-signed meta', () => {+ statSync.mockReturnValue({ mtimeMs: 1 });+ readFileSync.mockReturnValue(JSON.stringify({+ mode: 'self-signed',+ ips: ['127.0.0.1', '100.111.11.146'],+ }));+ expect(readCertMeta()).toEqual({+ mode: 'self-signed',+ ips: ['127.0.0.1', '100.111.11.146'],+ });+ });+});diff --git a/server/lib/index.js b/server/lib/index.jsindex 3507b16cc4..af988d24ce 100644--- a/server/lib/index.js+++ b/server/lib/index.js@@ -81,6 +81,7 @@ export * from './ffmpeg.js';export * from './pythonSetup.js';// === Networking ===+export * from './certMeta.js';export * from './fetchWithTimeout.js';export * from './httpClient.js';export * from './httpsState.js';diff --git a/server/lib/networkExposure.js b/server/lib/networkExposure.jsindex df0efda32f..54c2dd5e82 100644--- a/server/lib/networkExposure.js+++ b/server/lib/networkExposure.js@@ -10,25 +10,16 @@* the running process is actually serving, which is exactly what the widget* should show.*/-import { readFileSync, statSync } from 'node:fs';-import { PATHS, safeJSONParse } from './fileUtils.js';import { PORTS } from './ports.js';import { getHttpsEnabledAtBoot } from './httpsState.js';import { getSelfHost } from './peerSelfHost.js';-import { certPaths } from '../../lib/certPaths.js';+import { readCertMeta } from './certMeta.js';-const { meta: META_PATH } = certPaths(PATHS.data);// docs/PORTS.md is checked into the repo but the server doesn't serve the// docs/ directory, so link out to GitHub for the canonical guide rather than// to a 404. The widget shows this as "Learn more →".const PORTS_DOCS_URL = 'https://github.com/atomantic/PortOS/blob/main/docs/PORTS.md';-function readCertMeta() {- const stat = statSync(META_PATH, { throwIfNoEntry: false });- if (!stat) return null;- return safeJSONParse(readFileSync(META_PATH, 'utf-8'), null);-}-// Loopback-only bind hosts — for these, the browser treats the page as// "potentially trustworthy" (Secure Contexts spec), so getUserMedia and other// powerful APIs work over plain HTTP. Any other host (Tailscale IP, LAN IP,diff --git a/server/lib/networkExposure.test.js b/server/lib/networkExposure.test.jsindex 55010a8a01..713c01f48a 100644--- a/server/lib/networkExposure.test.js+++ b/server/lib/networkExposure.test.js@@ -3,7 +3,6 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';vi.mock('./fileUtils.js', () => ({PATHS: { data: '/mock/data' },safeJSONParse: (s, fallback) => { try { return JSON.parse(s); } catch { return fallback; } },- tryReadFile: vi.fn().mockResolvedValue(null),}));vi.mock('./httpsState.js', () => ({diff --git a/server/lib/peerSelfHost.js b/server/lib/peerSelfHost.jsindex 4f81509db6..b99b2de0cf 100644--- a/server/lib/peerSelfHost.js+++ b/server/lib/peerSelfHost.js@@ -1,21 +1,12 @@// Returns the Tailscale-issued hostname this PortOS sends in federation// announces. Self-signed mode binds to localhost + IPs, which can't be// announced as a host, so falls through to null.-import { readFileSync, statSync } from 'node:fs';-import { PATHS, safeJSONParse } from './fileUtils.js';-import { certPaths } from '../../lib/certPaths.js';--const { meta: META_PATH } = certPaths(PATHS.data);+import { readCertMeta } from './certMeta.js';export function getSelfHost() {if (process.env.PORTOS_HOST) return process.env.PORTOS_HOST;- const stat = statSync(META_PATH, { throwIfNoEntry: false });- if (!stat) return null;-- // safeJSONParse so a partially-written meta.json (mid setup:cert) doesn't- // throw at every announce/probe — getSelfHost is called in hot paths.- const meta = safeJSONParse(readFileSync(META_PATH, 'utf-8'), null);+ const meta = readCertMeta();if (!meta) return null;return meta.mode === 'tailscale' && typeof meta.hostname === 'string' && meta.hostname? meta.hostnamediff --git a/server/lib/peerSelfHost.test.js b/server/lib/peerSelfHost.test.jsindex 622b1e3eed..8086c2d8b6 100644--- a/server/lib/peerSelfHost.test.js+++ b/server/lib/peerSelfHost.test.js@@ -1,7 +1,6 @@import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';vi.mock('./fileUtils.js', () => ({-tryReadFile: vi.fn().mockResolvedValue(null),PATHS: { data: '/mock/data' },safeJSONParse: (s, fallback) => { try { return JSON.parse(s); } catch { return fallback; } }}));diff --git a/server/services/certRenewer.js b/server/services/certRenewer.jsindex 6ae59e2469..bc41e80754 100644--- a/server/services/certRenewer.js+++ b/server/services/certRenewer.js@@ -16,20 +16,16 @@ import { existsSync, readFileSync, statSync } from 'fs';import { promisify } from 'util';import { PATHS } from '../lib/fileUtils.js';import { findTailscale } from '../lib/tailscale.js';+import { readCertMeta } from '../lib/certMeta.js';import { certPaths } from '../../lib/certPaths.js';const execFileAsync = promisify(execFile);-const { cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(PATHS.data);+const { cert: CERT_PATH, key: KEY_PATH } = certPaths(PATHS.data);const RENEW_INTERVAL_MS = 24 * 60 * 60 * 1000;-function readMeta() {- if (!existsSync(META_PATH)) return null;- return JSON.parse(readFileSync(META_PATH, 'utf-8'));-}-async function renewOnce(httpsServer) {- const meta = readMeta();+ const meta = readCertMeta();if (!meta || meta.mode !== 'tailscale' || !meta.hostname) return;const bin = findTailscale();@@ -63,7 +59,7 @@ async function renewOnce(httpsServer) {export function initCertRenewer(httpsServer) {if (!httpsServer || typeof httpsServer.setSecureContext !== 'function') return;- const meta = readMeta();+ const meta = readCertMeta();if (!meta || meta.mode !== 'tailscale') return;renewOnce(httpsServer).catch(err => console.error(`❌ cert renewer initial run: ${err.message}`));