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([consolidate-cert-path-constants]): extract cert path constants to lib/certPaths.js

[._.]/ Adam Eivy2de7ee5fcd32
diff --git a/lib/certPaths.js b/lib/certPaths.js
new file mode 100644
index 0000000000..cde77256f2
--- /dev/null
+++ b/lib/certPaths.js
@@ -0,0 +1,27 @@
+/**
+ * certPaths — resolve the canonical cert / key / meta file paths under a
+ * given data directory.
+ *
+ * Sibling of `lib/tailscale-https.js` (both accept a `certDir` and share the
+ * `data/certs/{cert,key,meta}` layout). Zero-dep and side-effect-free — safe
+ * to import from `scripts/`, `server/`, `server/lib/`, and managed-app code.
+ *
+ * Usage:
+ * import { certPaths } from '../lib/certPaths.js';
+ * const { dir, cert, key, meta } = certPaths(PATHS.data);
+ *
+ * @param {string} dataDir absolute path to the `data/` root (e.g. `PATHS.data`
+ * on the server, or `join(ROOT, 'data')` in scripts).
+ * @returns {{ dir: string, cert: string, key: string, meta: string }}
+ */
+import { join } from 'path';
+
+export function certPaths(dataDir) {
+ const dir = join(dataDir, 'certs');
+ return {
+ dir,
+ cert: join(dir, 'cert.pem'),
+ key: join(dir, 'key.pem'),
+ meta: join(dir, 'meta.json'),
+ };
+}
diff --git a/lib/certPaths.test.js b/lib/certPaths.test.js
new file mode 100644
index 0000000000..79341fa79d
--- /dev/null
+++ b/lib/certPaths.test.js
@@ -0,0 +1,21 @@
+import { describe, it, expect } from 'vitest';
+import { join } from 'path';
+import { certPaths } from './certPaths.js';
+
+describe('certPaths', () => {
+ it('returns dir + cert/key/meta paths anchored at <dataDir>/certs', () => {
+ const dataDir = '/tmp/portos-data';
+ expect(certPaths(dataDir)).toEqual({
+ dir: join(dataDir, 'certs'),
+ cert: join(dataDir, 'certs', 'cert.pem'),
+ key: join(dataDir, 'certs', 'key.pem'),
+ meta: join(dataDir, 'certs', 'meta.json'),
+ });
+ });
+
+ it('joins relative dataDir without normalizing away the prefix', () => {
+ const { dir, cert } = certPaths('data');
+ expect(dir).toBe(join('data', 'certs'));
+ expect(cert).toBe(join('data', 'certs', 'cert.pem'));
+ });
+});
diff --git a/scripts/open-ui-in-browser.js b/scripts/open-ui-in-browser.js
index 37ad3eec82..54f70544a3 100644
--- a/scripts/open-ui-in-browser.js
+++ b/scripts/open-ui-in-browser.js
@@ -8,9 +8,10 @@
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { hasTailscaleCert } from '../lib/tailscale-https.js';
+import { certPaths } from '../lib/certPaths.js';
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
-const CERT_DIR = join(ROOT, 'data', 'certs');
+const { dir: CERT_DIR } = certPaths(join(ROOT, 'data'));
const API_PORT = Number(process.env.PORT) || 5555;
const HTTP_LOOPBACK_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
// Share the same cert predicate the server's HTTPS gate uses (file presence
diff --git a/scripts/print-access-url.js b/scripts/print-access-url.js
index ad5ee754d3..1488857e02 100644
--- a/scripts/print-access-url.js
+++ b/scripts/print-access-url.js
@@ -8,13 +8,13 @@
* boot HTTP-only while setup.sh's file-presence check still claimed HTTPS).
*/
import { hasTailscaleCert } from '../lib/tailscale-https.js';
+import { certPaths } from '../lib/certPaths.js';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
-const CERT_DIR = join(ROOT, 'data', 'certs');
-const META_PATH = join(CERT_DIR, 'meta.json');
+const { dir: CERT_DIR, meta: META_PATH } = certPaths(join(ROOT, 'data'));
const API_PORT = Number(process.env.PORT) || 5555;
const MIRROR_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
diff --git a/scripts/setup-cert.js b/scripts/setup-cert.js
index 51f0c2be21..d9a70e37a0 100644
--- a/scripts/setup-cert.js
+++ b/scripts/setup-cert.js
@@ -33,13 +33,11 @@ import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { findTailscale } from '../server/lib/tailscale.js';
import { hasTailscaleCert } from '../lib/tailscale-https.js';
+import { certPaths } from '../lib/certPaths.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = join(__dirname, '..');
-const CERT_DIR = join(ROOT, 'data', 'certs');
-const KEY_PATH = join(CERT_DIR, 'key.pem');
-const CERT_PATH = join(CERT_DIR, 'cert.pem');
-const META_PATH = join(CERT_DIR, 'meta.json');
+const { dir: CERT_DIR, cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(join(ROOT, 'data'));
const FORCE = process.argv.includes('--force');
const SELF_SIGNED_ONLY = process.argv.includes('--self-signed');
diff --git a/server/index.js b/server/index.js
index 6db61b72e5..6bbec15253 100644
--- a/server/index.js
+++ b/server/index.js
@@ -7,6 +7,7 @@ import { PORTS } from './lib/ports.js';
import { existsSync } from 'fs';
import { readFile, unlink } from 'fs/promises';
import { createTailscaleServers } from '../lib/tailscale-https.js';
+import { certPaths } from '../lib/certPaths.js';
import { getSelfHost } from './lib/peerSelfHost.js';
import { getBuildId, getStampedIndexHtml } from './lib/buildId.js';
@@ -150,7 +151,7 @@ const PORT = process.env.PORT || 5555;
const HOST = process.env.HOST || '0.0.0.0';
// Delegates HTTPS / HTTP-mirror wiring to lib/tailscale-https.js — see there.
-const CERT_DIR = join(PATHS.data, 'certs');
+const { dir: CERT_DIR } = certPaths(PATHS.data);
const { server: httpServer, mirror: localHttpServer, httpsEnabled } =
createTailscaleServers(app, { certDir: CERT_DIR });
const scheme = httpsEnabled ? 'https' : 'http';
diff --git a/server/lib/networkExposure.js b/server/lib/networkExposure.js
index 87e647719a..df0efda32f 100644
--- a/server/lib/networkExposure.js
+++ b/server/lib/networkExposure.js
@@ -11,13 +11,13 @@
* should show.
*/
import { readFileSync, statSync } from 'node:fs';
-import { join } from 'node:path';
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';
-const META_PATH = join(PATHS.data, 'certs', 'meta.json');
+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 →".
diff --git a/server/lib/peerSelfHost.js b/server/lib/peerSelfHost.js
index 508cae58a5..4f81509db6 100644
--- a/server/lib/peerSelfHost.js
+++ b/server/lib/peerSelfHost.js
@@ -2,10 +2,10 @@
// 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 { join } from 'node:path';
import { PATHS, safeJSONParse } from './fileUtils.js';
+import { certPaths } from '../../lib/certPaths.js';
-const META_PATH = join(PATHS.data, 'certs', 'meta.json');
+const { meta: META_PATH } = certPaths(PATHS.data);
export function getSelfHost() {
if (process.env.PORTOS_HOST) return process.env.PORTOS_HOST;
diff --git a/server/routes/apps.js b/server/routes/apps.js
index aceadb9769..e99d1b0050 100644
--- a/server/routes/apps.js
+++ b/server/routes/apps.js
@@ -19,6 +19,7 @@ import { parseEcosystemFromPath, usesPm2 } from '../services/streamingDetect.js'
import { detectAppIcon, getIconContentType, isUsableSvg } from '../services/appIconDetect.js';
import { hasDeployScript } from '../services/appDeployer.js';
import { checkScripts, installScripts, XCODE_SCRIPT_NAMES } from '../services/xcodeScripts.js';
+import { certPaths } from '../../lib/certPaths.js';
const router = Router();
@@ -268,7 +269,7 @@ router.post('/:id/upgrade-tls', loadApp, asyncHandler(async (req, res) => {
overwrote: alreadyExists,
tlsPort,
snippet,
- certDirHint: join(PATHS.data, 'certs'),
+ certDirHint: certPaths(PATHS.data).dir,
note: 'Point your app at the PortOS cert dir (or symlink it) so apps share the single Tailscale cert.'
});
}));
diff --git a/server/services/apps.js b/server/services/apps.js
index 31f34d9872..e53aabdb36 100644
--- a/server/services/apps.js
+++ b/server/services/apps.js
@@ -8,6 +8,7 @@ import { SELF_IMPROVEMENT_TASK_TYPES } from './taskSchedule.js';
import { sanitizeTaskMetadata } from '../lib/validation.js';
import { PORTS } from '../lib/ports.js';
import { hasTailscaleCert } from '../../lib/tailscale-https.js';
+import { certPaths } from '../../lib/certPaths.js';
const DATA_DIR = PATHS.data;
const APPS_FILE = join(DATA_DIR, 'apps.json');
@@ -21,7 +22,7 @@ export const PORTOS_APP_ID = 'portos-default';
function buildPortosApp() {
// tlsPort reflects whether the Tailscale cert is actually on disk; if not,
// don't advertise HTTPS so the Launch button doesn't target a broken scheme.
- const certPresent = hasTailscaleCert(join(PATHS.data, 'certs'));
+ const certPresent = hasTailscaleCert(certPaths(PATHS.data).dir);
return {
name: 'PortOS',
description: 'Local App OS portal for dev machines',
diff --git a/server/services/certProvisioner.js b/server/services/certProvisioner.js
index 668505b8de..ec1a14a7be 100644
--- a/server/services/certProvisioner.js
+++ b/server/services/certProvisioner.js
@@ -19,19 +19,16 @@
*/
import { execFile } from 'child_process';
import { existsSync, mkdirSync, statSync } from 'fs';
-import { join } from 'path';
import { promisify } from 'util';
import { PATHS, atomicWrite } from '../lib/fileUtils.js';
import { findTailscale } from '../lib/tailscale.js';
import { getHttpsEnabledAtBoot } from '../lib/httpsState.js';
import { PORTS } from '../lib/ports.js';
+import { certPaths } from '../../lib/certPaths.js';
const execFileAsync = promisify(execFile);
-const CERT_DIR = join(PATHS.data, 'certs');
-const KEY_PATH = join(CERT_DIR, 'key.pem');
-const CERT_PATH = join(CERT_DIR, 'cert.pem');
-const META_PATH = join(CERT_DIR, 'meta.json');
+const { dir: CERT_DIR, cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(PATHS.data);
async function tailscaleStatus(bin) {
const { stdout } = await execFileAsync(bin, ['status', '--json'], { timeout: 5000 });
diff --git a/server/services/certRenewer.js b/server/services/certRenewer.js
index 514bc82ddb..6ae59e2469 100644
--- a/server/services/certRenewer.js
+++ b/server/services/certRenewer.js
@@ -13,17 +13,14 @@
*/
import { execFile } from 'child_process';
import { existsSync, readFileSync, statSync } from 'fs';
-import { join } from 'path';
import { promisify } from 'util';
import { PATHS } from '../lib/fileUtils.js';
import { findTailscale } from '../lib/tailscale.js';
+import { certPaths } from '../../lib/certPaths.js';
const execFileAsync = promisify(execFile);
-const CERT_DIR = join(PATHS.data, 'certs');
-const CERT_PATH = join(CERT_DIR, 'cert.pem');
-const KEY_PATH = join(CERT_DIR, 'key.pem');
-const META_PATH = join(CERT_DIR, 'meta.json');
+const { cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(PATHS.data);
const RENEW_INTERVAL_MS = 24 * 60 * 60 * 1000;
function readMeta() {
diff --git a/server/vitest.config.js b/server/vitest.config.js
index fe567cf37d..9a5057782c 100644
--- a/server/vitest.config.js
+++ b/server/vitest.config.js
@@ -14,6 +14,7 @@ export default defineConfig({
'**/*.test.js',
'../client/src/**/*.test.js',
'../scripts/**/*.test.js',
+ '../lib/**/*.test.js',
],
coverage: {
provider: 'v8',