Files
strata/apps/host/server/cloudflare.mjs
T
Niko1221 0b3299a019 apps/host: the owner's UI and the Node App Host
The UI source from the 14 October handoff (src/, public/, index.html, vite and
tsconfig, tests, audit-fixes, design docs) and the App Host (server/*.mjs): model
import and monitor routes, API-provider catalog and streaming (OpenAI chat, OpenAI Responses,
the Messages API), serving, tunnel, tasks, plus the host routes for Time-skip
(server/timeskip.mjs) and the tuning plan/reports. src/lib/time-skip.ts renders a real
job's own record.

The UI design is unchanged. The rebuild routed #/tuning to a new TunePanel; that is
reverted: src/screens/ModelRun.tsx and src/main.tsx are the handoff's again (#/tuning
goes where the handoff sends it), and src/components/TunePanel.tsx and
src/styles/tuning.css, which only that route used, are not carried over. Left out of
git on purpose: dist/ (build output), node_modules, and docs/evidence + docs/checkpoints
(170 MB of screenshots, kept in StrataSplit\evidence-archive).

From the owner's rebuild branch strata/rebuild-2026-10.
2026-10-15 10:24:09 +02:00

90 lines
5.4 KiB
JavaScript

/** A supervised Cloudflare connector with a private, inference-only origin. */
import { spawn } from 'node:child_process';
import { probe, which } from './util.mjs';
export function tunnelHostname(value) {
const hostname = String(value ?? '').trim().toLowerCase();
if (hostname.length > 253 || !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/.test(hostname)) throw new Error('Enter a hostname such as ai.example.com, without https:// or a path.');
return hostname;
}
export function createCloudflare({ origin, originUrl, hasKey, changed = () => {}, launch = spawn, locate = async () => process.platform === 'win32' ? (await probe('where.exe', ['cloudflared'])).split(/\r?\n/)[0] || null : which('cloudflared'), prepareConnector = null, timeoutMs = 45000 }) {
let binary = null, child = null, timer = null, busy = false, generation = 0;
let value = { status: 'idle', mode: null, url: null, error: null };
const state = () => ({ ...value, installed: !!binary, originUrl });
const update = patch => { value = { ...value, ...patch }; changed(state()); };
async function inspect() { binary = await locate(); changed(state()); return state(); }
async function prepare() {
if (busy) throw new Error('Connector setup is already in progress.');
busy = true;
try {
binary = await locate();
if (!binary && prepareConnector) binary = await prepareConnector();
if (!binary) throw new Error('The connector could not be prepared automatically.');
update({ error: null });
return state();
} finally { busy = false; }
}
async function terminate() {
clearTimeout(timer); timer = null;
const processRef = child; child = null;
if (processRef) {
await new Promise(resolve => {
const fallback = setTimeout(() => { processRef.kill('SIGKILL'); resolve(); }, 1500);
processRef.once('exit', () => { clearTimeout(fallback); resolve(); });
processRef.kill('SIGTERM');
});
}
await origin.stop();
}
async function fail(message, id) {
if (id !== generation) return;
++generation; busy = true; update({ status: 'stopping', error: message, url: null });
try { await terminate(); } finally { busy = false; update({ status: 'error' }); }
}
async function start(body) {
if (busy || ['starting', 'connected', 'stopping'].includes(value.status)) throw new Error('Stop the current tunnel before starting another.');
if (!hasKey()) throw new Error('Create a Strata API key before connecting Cloudflare.');
if (!['quick', 'domain'].includes(body.mode)) throw new Error('Choose a temporary URL or your own domain.');
const hostname = body.mode === 'domain' ? tunnelHostname(body.hostname) : null;
const token = body.mode === 'domain' ? String(body.token ?? '').trim() : '';
if (body.mode === 'domain' && (!/^[A-Za-z0-9+/=_-]{20,8192}$/.test(token))) throw new Error('Paste the tunnel token from Cloudflare, not the full installation command.');
busy = true; const id = ++generation;
update({ status: 'starting', mode: body.mode, url: null, error: null });
try {
binary = await locate();
if (!binary && prepareConnector) binary = await prepareConnector();
if (!binary) throw new Error('The connector could not be prepared automatically. Retry connector setup.');
await origin.start();
// Tokens never appear in command arguments, logs, saved settings, or API responses.
const env = { ...process.env };
for (const key of Object.keys(env)) if (key.startsWith('TUNNEL_')) delete env[key];
if (token) env.TUNNEL_TOKEN = token;
const args = ['tunnel', '--no-autoupdate', '--loglevel', 'info', '--grace-period', '1s', ...(hostname ? ['run'] : ['--url', originUrl])];
child = launch(binary, args, { env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
let tail = '', publicUrl = hostname ? `https://${hostname}` : null, registered = false;
const read = chunk => {
if (generation !== id) return;
tail = (tail + chunk.toString()).slice(-16384);
const match = tail.match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com\b/);
if (!hostname && match) publicUrl = match[0];
if (/Registered tunnel connection/i.test(tail)) registered = true;
if (publicUrl && registered && value.status !== 'connected') { clearTimeout(timer); update({ status: 'connected', url: `${publicUrl}/v1` }); }
};
child.stdout?.on('data', read); child.stderr?.on('data', read);
child.once('error', () => { void fail('Cloudflared could not start. Check the installation and try again.', id); });
child.once('exit', () => { if (generation === id) { child = null; void fail('Cloudflared stopped. Check your network, token and Cloudflare configuration, then reconnect.', id); } });
timer = setTimeout(() => { void fail('Cloudflare did not connect in time. Check your internet connection and tunnel setup, then try again.', id); }, timeoutMs);
timer.unref?.();
return state();
} catch (e) {
++generation; await terminate(); update({ status: 'error', url: null, error: e.message }); throw e;
} finally { busy = false; }
}
async function stop() {
if (busy) throw new Error('The tunnel is updating. Try again in a moment.');
busy = true; ++generation; update({ status: 'stopping', url: null, error: null });
try { await terminate(); update({ status: 'idle', mode: null }); return state(); } finally { busy = false; }
}
return { state, inspect, prepare, start, stop };
}