Skip to content

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

refactor([hoist-certmeta-to-top-level-lib]): move readCertMeta to top-level lib/ + take metaPath arg

[._.]/ Adam Eivy207a9b8c4b59
diff --git a/lib/certMeta.js b/lib/certMeta.js
new file mode 100644
index 0000000000..9a6f1d2177
--- /dev/null
+++ b/lib/certMeta.js
@@ -0,0 +1,32 @@
+/**
+ * certMeta — best-effort reader for the `data/certs/meta.json` marker that
+ * `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.
+ *
+ * Sibling of `lib/certPaths.js` and `lib/tailscale-https.js`. Zero-dep and
+ * side-effect-free, safe to import from `scripts/`, `server/`, `server/lib/`,
+ * and managed-app code — which is why this inlines `JSON.parse` + try/catch
+ * rather than reusing `server/lib/fileUtils.js#safeJSONParse` (that would
+ * pull the server-lib chain into `scripts/` and managed apps).
+ *
+ * Usage:
+ * import { certPaths } from './lib/certPaths.js';
+ * import { readCertMeta } from './lib/certMeta.js';
+ * const { meta: META_PATH } = certPaths(PATHS.data);
+ * const meta = readCertMeta(META_PATH); // null when absent or mid-write
+ *
+ * @param {string} metaPath absolute path to `meta.json`.
+ * @returns {object | null}
+ */
+import { readFileSync, statSync } from 'node:fs';
+
+export function readCertMeta(metaPath) {
+ const stat = statSync(metaPath, { throwIfNoEntry: false });
+ if (!stat) return null;
+ try {
+ return JSON.parse(readFileSync(metaPath, 'utf-8'));
+ } catch {
+ return null;
+ }
+}
diff --git a/server/lib/certMeta.test.js b/lib/certMeta.test.js
similarity index 67%
rename from server/lib/certMeta.test.js
rename to lib/certMeta.test.js
index 678d8fb5d8..bb199f5555 100644
--- a/server/lib/certMeta.test.js
+++ b/lib/certMeta.test.js
@@ -1,10 +1,5 @@
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(),
@@ -13,6 +8,8 @@ vi.mock('node:fs', () => ({
import { readFileSync, statSync } from 'node:fs';
import { readCertMeta } from './certMeta.js';
+const META_PATH = '/mock/data/certs/meta.json';
+
describe('certMeta.readCertMeta', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -20,14 +17,14 @@ describe('certMeta.readCertMeta', () => {
it('returns null when meta.json does not exist', () => {
statSync.mockReturnValue(undefined);
- expect(readCertMeta()).toBeNull();
+ expect(readCertMeta(META_PATH)).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();
+ expect(readCertMeta(META_PATH)).toBeNull();
});
it('returns parsed tailscale meta with mode + hostname + ips', () => {
@@ -37,7 +34,7 @@ describe('certMeta.readCertMeta', () => {
hostname: 'void.taile8179.ts.net',
ips: ['100.111.11.146'],
}));
- expect(readCertMeta()).toEqual({
+ expect(readCertMeta(META_PATH)).toEqual({
mode: 'tailscale',
hostname: 'void.taile8179.ts.net',
ips: ['100.111.11.146'],
@@ -50,9 +47,17 @@ describe('certMeta.readCertMeta', () => {
mode: 'self-signed',
ips: ['127.0.0.1', '100.111.11.146'],
}));
- expect(readCertMeta()).toEqual({
+ expect(readCertMeta(META_PATH)).toEqual({
mode: 'self-signed',
ips: ['127.0.0.1', '100.111.11.146'],
});
});
+
+ it('honors the metaPath argument (callers can swap paths per data dir)', () => {
+ statSync.mockReturnValue({ mtimeMs: 1 });
+ readFileSync.mockReturnValue(JSON.stringify({ mode: 'self-signed' }));
+ readCertMeta('/alt/data/certs/meta.json');
+ expect(statSync).toHaveBeenCalledWith('/alt/data/certs/meta.json', { throwIfNoEntry: false });
+ expect(readFileSync).toHaveBeenCalledWith('/alt/data/certs/meta.json', 'utf-8');
+ });
});
diff --git a/scripts/print-access-url.js b/scripts/print-access-url.js
index 1488857e02..b15f12a38f 100644
--- a/scripts/print-access-url.js
+++ b/scripts/print-access-url.js
@@ -9,7 +9,7 @@
*/
import { hasTailscaleCert } from '../lib/tailscale-https.js';
import { certPaths } from '../lib/certPaths.js';
-import { readFileSync } from 'fs';
+import { readCertMeta } from '../lib/certMeta.js';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
@@ -23,10 +23,7 @@ if (!hasTailscaleCert(CERT_DIR)) {
process.exit(0);
}
-let mode = '';
-try {
- mode = JSON.parse(readFileSync(META_PATH, 'utf-8')).mode || '';
-} catch { /* meta unreadable — fall through to generic warning */ }
+const mode = readCertMeta(META_PATH)?.mode || '';
console.log(`Access at: http://localhost:${MIRROR_PORT} (loopback HTTP mirror — no cert warning)`);
if (mode === 'tailscale') {
diff --git a/server/lib/README.md b/server/lib/README.md
index 9cd2fcd92d..75ab9b5ba8 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -99,7 +99,6 @@ 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.js
deleted file mode 100644
index e5c37be2a7..0000000000
--- a/server/lib/certMeta.js
+++ /dev/null
@@ -1,14 +0,0 @@
-// 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/index.js b/server/lib/index.js
index af988d24ce..3507b16cc4 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -81,7 +81,6 @@ 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.js
index 54c2dd5e82..1837622270 100644
--- a/server/lib/networkExposure.js
+++ b/server/lib/networkExposure.js
@@ -11,9 +11,13 @@
* should show.
*/
import { PORTS } from './ports.js';
+import { PATHS } from './fileUtils.js';
import { getHttpsEnabledAtBoot } from './httpsState.js';
import { getSelfHost } from './peerSelfHost.js';
-import { readCertMeta } from './certMeta.js';
+import { certPaths } from '../../lib/certPaths.js';
+import { readCertMeta } from '../../lib/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
@@ -37,7 +41,7 @@ export function getNetworkExposureStatus() {
const bindPort = Number(process.env.PORT) || PORTS.API;
const loopbackPort = Number(process.env.PORTOS_HTTP_PORT) || PORTS.API_LOCAL;
- const meta = readCertMeta();
+ const meta = readCertMeta(META_PATH);
const certMode = httpsEnabled ? (meta?.mode || 'unknown') : null;
const tailscaleHost = getSelfHost();
const tailscaleIps = Array.isArray(meta?.ips) ? meta.ips : [];
diff --git a/server/lib/peerSelfHost.js b/server/lib/peerSelfHost.js
index b99b2de0cf..b9f077313d 100644
--- a/server/lib/peerSelfHost.js
+++ b/server/lib/peerSelfHost.js
@@ -1,12 +1,16 @@
// 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 { readCertMeta } from './certMeta.js';
+import { PATHS } from './fileUtils.js';
+import { certPaths } from '../../lib/certPaths.js';
+import { readCertMeta } from '../../lib/certMeta.js';
+
+const { meta: META_PATH } = certPaths(PATHS.data);
export function getSelfHost() {
if (process.env.PORTOS_HOST) return process.env.PORTOS_HOST;
- const meta = readCertMeta();
+ const meta = readCertMeta(META_PATH);
if (!meta) return null;
return meta.mode === 'tailscale' && typeof meta.hostname === 'string' && meta.hostname
? meta.hostname
diff --git a/server/services/certRenewer.js b/server/services/certRenewer.js
index bc41e80754..3f48f97c29 100644
--- a/server/services/certRenewer.js
+++ b/server/services/certRenewer.js
@@ -16,16 +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';
+import { readCertMeta } from '../../lib/certMeta.js';
const execFileAsync = promisify(execFile);
-const { cert: CERT_PATH, key: KEY_PATH } = certPaths(PATHS.data);
+const { cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(PATHS.data);
const RENEW_INTERVAL_MS = 24 * 60 * 60 * 1000;
async function renewOnce(httpsServer) {
- const meta = readCertMeta();
+ const meta = readCertMeta(META_PATH);
if (!meta || meta.mode !== 'tailscale' || !meta.hostname) return;
const bin = findTailscale();
@@ -59,7 +59,7 @@ async function renewOnce(httpsServer) {
export function initCertRenewer(httpsServer) {
if (!httpsServer || typeof httpsServer.setSecureContext !== 'function') return;
- const meta = readCertMeta();
+ const meta = readCertMeta(META_PATH);
if (!meta || meta.mode !== 'tailscale') return;
renewOnce(httpsServer).catch(err => console.error(`❌ cert renewer initial run: ${err.message}`));