diff options
Diffstat (limited to 'packages/meshbay-client/src/main.js')
| -rw-r--r-- | packages/meshbay-client/src/main.js | 85 |
1 files changed, 75 insertions, 10 deletions
diff --git a/packages/meshbay-client/src/main.js b/packages/meshbay-client/src/main.js index f01b8f2..603dedc 100644 --- a/packages/meshbay-client/src/main.js +++ b/packages/meshbay-client/src/main.js @@ -1361,6 +1361,10 @@ function registerBridge() { // dialog over. function winEnsureNodeOnPath() { if (process.platform !== 'win32' || !hasBundledNode()) return; + // The Store package declares meshbay-node.exe as an execution alias, in a + // folder already on PATH and the same for every version; an entry added + // here would name the versioned install folder, deleted by the next update. + if (WIN_STORE) return; const script = path.join(process.resourcesPath, 'ensure-node-path.ps1'); if (!fs.existsSync(script)) return; // dev run, or an older build without it const nodeDir = path.join(process.resourcesPath, 'node-runtime'); @@ -1428,6 +1432,54 @@ function registerBridge() { try { fs.rmSync(WIN_STARTUP_VBS, { force: true }); } catch { /* not there */ } } + // ── Windows, Store package: the startup task stands in for the launcher ── + // The .vbs names the node by its path, and in the Store package that path is + // C:\Program Files\WindowsApps\MeshBay.MeshBay_<version>_..., which every + // update deletes; right after sign-in Windows also refused the launcher the + // file outright ("Permission denied"). Both found on a real install. The + // package declares a startup task instead (build/appx-extensions.xml, off by + // default): Windows runs it, lists it in Settings > Apps > Startup, keeps its + // state across updates and removes it with the package. Only a process with + // the package's identity may switch it, and Windows gives that to the + // executables inside the package but not to one started from elsewhere: a + // powershell.exe run from here got "Element not found". The node's CLI is + // inside, so `autostart install | remove | status` does it, and its first + // line says "startup task <state>" (platform.startup_task). + const WIN_STORE = process.platform === 'win32' && Boolean(process.windowsStore); + + // The Node page asks for the status every couple of seconds, and each answer + // here is a process. The user can also switch the task in Windows Settings, + // so the answer is kept for a while, not for good. + const STARTUP_TASK_TTL_MS = 15000; + let startupTaskKnown = null; // { state, at } + + async function winStartupTask(sub) { + const r = await winNodeCli(['autostart', sub]); + const m = /^startup task (\S+)/m.exec(r.out); + if (m) startupTaskKnown = { state: m[1], at: Date.now() }; + if (!r.ok || !m) { + // The state line is for this file; the rest is the CLI's reason. + const why = r.out.replace(/^startup task \S+\s*/m, '').trim(); + throw new Error(why || r.out || 'the startup task did not answer'); + } + return m[1]; + } + + // Whether the node starts at sign-in: the startup task in the Store package, + // the Startup-folder launcher otherwise. + async function winSigninEnabled() { + if (!WIN_STORE) return winAutostartInstalled(); + if (startupTaskKnown && Date.now() - startupTaskKnown.at < STARTUP_TASK_TTL_MS) { + return startupTaskKnown.state.startsWith('Enabled'); + } + try { + return (await winStartupTask('status')).startsWith('Enabled'); + } catch (err) { + console.error('[node]', err.message); + return false; + } + } + // Windows: starting, stopping and restarting the node is the CLI's, and only // the CLI's (meshbay_node/cli/lifecycle.py) -- one implementation behind // every front door, the Node page, the tray, node:start and a terminal @@ -1459,7 +1511,8 @@ function registerBridge() { return r; } - // Every meshbay-node.exe running, in any session (tasklist lists session 0, + // Every node process running (meshbay-node.exe, and meshbay-nodew.exe, the + // build without a console the Store package's startup task runs), in any session (tasklist lists session 0, // where a service node runs). Whether a node is there is a question for the // process list as well as its control API: the API closes first on the way // down, and a node started some other way than the service task -- from a @@ -1467,7 +1520,7 @@ function registerBridge() { // Both made the Node page say "Stopped" about a node that was running. function winNodePids() { return new Promise((resolve) => { - execFile('tasklist', ['/FI', 'IMAGENAME eq meshbay-node.exe', '/NH', '/FO', 'CSV'], + execFile('tasklist', ['/FI', 'IMAGENAME eq meshbay-node*', '/NH', '/FO', 'CSV'], { windowsHide: true }, (err, stdout) => { if (err) return resolve([]); resolve(String(stdout || '').split(/\r?\n/) @@ -1521,7 +1574,7 @@ function registerBridge() { // boot task, the sign-in launcher, or neither -- "only while MeshBay is open". async function winStartupMode() { if ((await winServiceTaskStatus()).installed) return 'service'; - if (winAutostartInstalled()) return 'signin'; + if (await winSigninEnabled()) return 'signin'; return 'open'; } @@ -1688,7 +1741,7 @@ function registerBridge() { const [bin, svc] = await Promise.all([findNodeBinary(), winServiceTaskStatus()]); return { installed: Boolean(bin), - autostart: winAutostartInstalled(), + autostart: await winSigninEnabled(), service: svc.installed, }; } @@ -1728,7 +1781,7 @@ function registerBridge() { // check below, with an actionable error) -- correct but a dead click the // Node page should not offer in the first place. function winCanElevateServiceMode() { - return app.isPackaged + return app.isPackaged && !WIN_STORE && fs.existsSync(path.join(process.resourcesPath, 'service-mode.ps1')); } @@ -1761,7 +1814,8 @@ function registerBridge() { // gated on `installed`, so with no autostart configured they silently // vanished — the daemon was perfectly manageable, just not launchable // at sign-in. `autostart` carries that state as its own field instead. - const [{ activeState }, bin] = await Promise.all([winNodeActivity(), findNodeBinary()]); + const [{ activeState }, bin, autostart] = await Promise.all( + [winNodeActivity(), findNodeBinary(), winSigninEnabled()]); return { supported: true, // Only claim "startup mode" once a node was actually found (bundled @@ -1771,10 +1825,13 @@ function registerBridge() { // a string, so `null` here hides that whole row. mode: bin ? 'startup' : null, installed: Boolean(bin), - autostart: winAutostartInstalled(), + autostart, activeState, subState: activeState === 'active' ? 'running' : '', canElevate: winCanElevateServiceMode(), + // The Store package starts the node at sign-in at most: at boot is the + // .exe installer's (a boot task names the versioned WindowsApps path). + store: WIN_STORE, }; } if (process.platform !== 'linux') return { supported: false }; @@ -1857,7 +1914,8 @@ Its log: ${nodeLogHint()}`); return { restarted: true }; } - // Install / remove the Windows Startup-folder launcher, and query it. + // Install / remove the Windows Startup-folder launcher (the startup task in + // the Store package), and query it. handle('node:autostart', async (_e, action) => { if (process.platform !== 'win32') return { supported: false }; if (action === 'install') { @@ -1865,16 +1923,23 @@ Its log: ${nodeLogHint()}`); if ((await winServiceTaskStatus()).installed) { throw new Error('the node already runs as a background service'); } + if (WIN_STORE) { + // Refused, with the CLI's reason, when the user switched it off in + // Windows Settings: only they can switch it back on, there. + await winStartupTask('install'); + return { supported: true, installed: true }; + } const bin = await findNodeBinary(); if (!bin) throw new Error('meshbay-node not found on PATH'); winAutostartInstall(bin); return { supported: true, installed: true }; } if (action === 'remove') { - winAutostartRemove(); + if (WIN_STORE) await winStartupTask('remove'); + else winAutostartRemove(); return { supported: true, installed: false }; } - return { supported: true, installed: winAutostartInstalled() }; + return { supported: true, installed: await winSigninEnabled() }; }); // Turn service mode on or off after install — one elevation, task + firewall |