Lines 855-895javascript
855 // ── Version check ────────────────────────
856 else if (req.method === 'GET' && pathname === '/api/version') {
857 const pkg = require('../package.json');
858 const current = pkg.version;
859 // Fetch latest from npm registry
860 fetchLatestVersion(pkg.name).then(latest => {
861 json(res, { current, latest, updateAvailable: latest && latest !== current && isNewer(latest, current) });
863 json(res, { current, latest: null, updateAvailable: false });
867 // ── Self-update ─────────────────────────
868 else if (req.method === 'POST' && pathname === '/api/update') {
869 const pkg = require('../package.json');
870 log('UPDATE', `Starting self-update from v${pkg.version}...`);
871 json(res, { ok: true, message: 'Updating... Page will reload.' });
872 // Run update in background after response is sent
874 const { execSync, spawn } = require('child_process');
876 execSync('npm i -g codbash-app@latest', { stdio: 'inherit', timeout: 120000 });
877 log('UPDATE', 'Update installed. Restarting server...');
HighRuntime Package Install
Package source invokes a package manager install command at runtime.
src/server.jsView on unpkg · L875 878 // Spawn a FULLY-detached restarter BEFORE exiting. Spawning inside a
879 // `process.on('exit')` handler is unreliable — the event loop is already
880 // draining, so the child frequently never survives and the server ends
881 // up dead (nothing reclaims the port). Instead: detach + unref + ignore
882 // stdio so the child outlives us, become its own process-group leader
883 // (so the restarter's `lsof … | kill -9` on the port can't take it down),
884 // then exit cleanly. The restarter loads the freshly-installed code from
885 // the same argv[1] path that npm just overwrote.
888 [process.argv[1], 'restart', `--port=${port}`, `--host=${host}`, '--no-browser'],
889 { detached: true, stdio: 'ignore' }
892 // Give the OS a tick to fully spawn the detached child before we exit
893 // and release the port for the restarter to grab.
894 setTimeout(() => process.exit(0), 250);