Something went wrong. Try again.
Local build agent for Nodrix — compiles sketches with your own Arduino toolchain so the browser can flash them. Dials out over WebSocket; n…
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import { arch, platform } from 'node:os';import { artifactUrl, loadConfig, socketUrl, type Config } from './config';import { parseCloudFrame, type AgentFrame, type BuildRequest } from './protocol';import { cliVersion, compile } from './arduino';
const VERSION = '0.1.0';const RECONNECT_MIN_MS = 1_000;const RECONNECT_MAX_MS = 30_000;
let backoff = RECONNECT_MIN_MS;
function send(ws: WebSocket, frame: AgentFrame) { try { ws.send(JSON.stringify(frame)); } catch { /* socket closed under us; the reconnect handles it */ }}
// Over plain HTTP, never the socket — the DO on the other end serves every board.async function upload(cfg: Config, buildId: string, binary: Uint8Array<ArrayBuffer>): Promise<void> { // A Blob so fetch sends a Content-Length; R2 needs the size up front. const res = await fetch(artifactUrl(cfg, buildId), { method: 'PUT', headers: { Authorization: `Bearer ${cfg.token}`, 'Content-Type': 'application/octet-stream' }, body: new Blob([binary]), }); if (!res.ok) throw new Error(`upload failed (${res.status})`);}
async function runBuild(ws: WebSocket, cfg: Config, req: BuildRequest) { console.log(`build ${req.id} (${req.fqbn})`); const result = await compile(req.sketch, req.fqbn, req.id, cfg.sketchbook, (line) => send(ws, { type: 'log', build: req.id, line }) ); if (!result.ok) { send(ws, { type: 'result', build: req.id, ok: false, error: result.error }); return; } try { await upload(cfg, req.id, result.binary); } catch (e) { send(ws, { type: 'result', build: req.id, ok: false, error: (e as Error).message }); return; } send(ws, { type: 'result', build: req.id, ok: true, fqbn: req.fqbn });}
async function connect(cfg: Config) { const cli = await cliVersion(); if (!cli) { console.warn('arduino-cli not found on PATH — builds will fail until it is installed.'); }
// A header, not a query parameter: tokens in URLs end up in logs and history. const ws = new WebSocket(socketUrl(cfg), { headers: { Authorization: `Bearer ${cfg.token}` }, } as unknown as string[]);
ws.addEventListener('open', () => { backoff = RECONNECT_MIN_MS; console.log(`connected to ${cfg.instance}`); send(ws, { type: 'hello', version: VERSION, platform: platform(), arch: arch(), cli }); });
ws.addEventListener('message', (event) => { const frame = parseCloudFrame(typeof event.data === 'string' ? event.data : ''); if (frame?.type === 'build') void runBuild(ws, cfg, frame); });
ws.addEventListener('close', () => { console.log(`disconnected — retrying in ${Math.round(backoff / 1000)}s`); setTimeout(() => void connect(cfg), backoff); backoff = Math.min(backoff * 2, RECONNECT_MAX_MS); });
ws.addEventListener('error', () => { /* close fires next and owns the retry */ });}
try { const cfg = loadConfig(); console.log(`nodrix agent ${VERSION}`); void connect(cfg);} catch (e) { console.error((e as Error).message); process.exit(1);}