Each host build pins one engine version in src-tauri/browser-engine.json. The new engine manager downloads that ZIP over HTTPS, makes sure that its size and SHA-256 match the pin, extracts it safely to the local app data folder, and marks the installation last. It can also install a local ZIP, cancel, report progress, and remove the engine. The host refuses an engine that sends another protocol with its ready event.
36 lines
2.3 KiB
JavaScript
36 lines
2.3 KiB
JavaScript
import net from 'node:net';
|
|
import { spawn } from 'node:child_process';
|
|
import { randomBytes } from 'node:crypto';
|
|
import { mkdtemp } from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
const root = await mkdtemp(path.join(os.tmpdir(), 'runestone-protocol-'));
|
|
const token = randomBytes(32).toString('hex');
|
|
let socket;
|
|
const server = net.createServer(client => {
|
|
socket = client;
|
|
let buffer = '';
|
|
client.on('data', bytes => {
|
|
buffer += bytes;
|
|
let end;
|
|
while ((end = buffer.indexOf('\n')) >= 0) {
|
|
const line = buffer.slice(0, end); buffer = buffer.slice(end + 1);
|
|
const message = JSON.parse(line);
|
|
if (message.token) { assert.equal(message.token, token); console.log('PASS runtime authentication'); }
|
|
if (message.event === 'ready') { assert.equal(message.protocol, 1); assert.match(message.version, /^\d+\.\d+\.\d+/); console.log('PASS runtime protocol'); client.write(JSON.stringify({ id: 1, command: 'state', args: {} }) + '\n'); }
|
|
if (message.id === 1) { assert.deepEqual(message.result.profiles, [{ id: 'user', name: 'User' }]); client.write(JSON.stringify({ id: 2, command: 'newTab', args: {} }) + '\n'); }
|
|
if (message.id === 2) { assert.ok(message.result.tabId); console.log('PASS packaged runtime commands'); client.write(JSON.stringify({ id: 3, command: 'shutdown', args: {} }) + '\n'); }
|
|
if (message.id === 3) console.log('PASS clean shutdown response');
|
|
}
|
|
});
|
|
});
|
|
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
const executable = path.resolve(`dist/RuneStone-Browser-${process.platform}-${process.arch}/${process.platform === 'win32' ? 'RuneStone-Browser.exe' : 'RuneStone-Browser'}`);
|
|
const child = spawn(executable, [`--rs-connect=${server.address().port}`, `--rs-browser-root=${root}`], { windowsHide: true, stdio: ['ignore', 'ignore', 'pipe'], env: { ...process.env, RUNESTONE_BROWSER_TOKEN: token } });
|
|
child.stderr.on('data', chunk => process.stderr.write(chunk));
|
|
const timer = setTimeout(() => { child.kill(); process.exitCode = 1; }, 15_000);
|
|
const code = await new Promise(resolve => child.once('exit', resolve)); clearTimeout(timer);
|
|
socket?.destroy(); await new Promise(resolve => server.close(resolve));
|
|
assert.equal(code, 0);
|