Ship the browser engine as its own release file
The installer no longer bundles the engine, and the installer script stops when the bundle names it or when the setup file is over 30 MB. pnpm build:browser-engine packages the engine, writes a deterministic ZIP and its record to releases/browser-engine/<version>/, and pins the ZIP in the host. The installer hooks remove the engine folder that 0.1.48 leaves behind, and uninstall removes the downloaded engine.
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, mkdir, readFile, writeFile, copyFile, rm, access } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import zlib from 'node:zlib';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { buildCommands, describePlan, engineFiles, parseArguments, planEngineBuild, platformKey, runEngineBuild, zipFolder } from '../../../scripts/buildBrowserEngine.mjs';
|
||||
|
||||
const repository = path.resolve('.');
|
||||
const exists = (file: string) => access(file).then(() => true, () => false);
|
||||
const releaseUrl = 'https://example.com/releases/download/browser-engine';
|
||||
const windows = { platform: 'win32', arch: 'x64' };
|
||||
|
||||
// A copy of the files that the engine script reads.
|
||||
async function withFixture(body: (root: string, before: string[]) => Promise<void>) {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'runestone-engine-build-'));
|
||||
try {
|
||||
for (const file of [...engineFiles, 'src-tauri/src/browser_engine.rs', 'browser-runtime/src/engine-main.mjs']) {
|
||||
await mkdir(path.dirname(path.join(root, file)), { recursive: true });
|
||||
await copyFile(path.join(repository, file), path.join(root, file));
|
||||
}
|
||||
await body(root, await Promise.all(engineFiles.map(file => readFile(path.join(root, file), 'utf8'))));
|
||||
} finally { await rm(root, { recursive: true, force: true }); }
|
||||
}
|
||||
const contents = (root: string) => Promise.all(engineFiles.map(file => readFile(path.join(root, file), 'utf8')));
|
||||
|
||||
/** The names and contents of a ZIP, read from its central directory. */
|
||||
function unzip(archive: Buffer) {
|
||||
const end = archive.length - 22;
|
||||
assert.equal(archive.readUInt32LE(end), 0x06054b50);
|
||||
const count = archive.readUInt16LE(end + 10);
|
||||
let at = archive.readUInt32LE(end + 16);
|
||||
const entries: { name: string, data: Buffer, mode: number }[] = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
assert.equal(archive.readUInt32LE(at), 0x02014b50);
|
||||
const method = archive.readUInt16LE(at + 10), crc = archive.readUInt32LE(at + 16), size = archive.readUInt32LE(at + 20);
|
||||
const nameLength = archive.readUInt16LE(at + 28), mode = archive.readUInt32LE(at + 38) >>> 16, offset = archive.readUInt32LE(at + 42);
|
||||
const name = archive.subarray(at + 46, at + 46 + nameLength).toString('utf8');
|
||||
const start = offset + 30 + archive.readUInt16LE(offset + 26) + archive.readUInt16LE(offset + 28);
|
||||
const body = archive.subarray(start, start + size);
|
||||
const data = method === 8 ? zlib.inflateRawSync(body) : Buffer.from(body);
|
||||
assert.equal(zlib.crc32(data), crc, name);
|
||||
entries.push({ name, data, mode });
|
||||
at += 46 + nameLength;
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
test('the engine script reads its options and maps platform keys', () => {
|
||||
assert.deepEqual(parseArguments(['--release-url', releaseUrl]), { version: undefined, releaseUrl, dryRun: false });
|
||||
assert.deepEqual(parseArguments(['--dry-run', '--version', '0.1.8', '--release-url', releaseUrl]), { version: '0.1.8', releaseUrl, dryRun: true });
|
||||
for (const args of [[], ['--version', '0.1.8'], ['--release-url'], ['--release-url', releaseUrl, '--release-url', releaseUrl], ['--force']]) assert.throws(() => parseArguments(args));
|
||||
assert.equal(platformKey('win32', 'x64'), 'windows-x86_64');
|
||||
assert.equal(platformKey('linux', 'arm64'), 'linux-aarch64');
|
||||
assert.equal(platformKey('darwin', 'arm64'), 'macos-aarch64');
|
||||
assert.throws(() => platformKey('aix', 'x64'));
|
||||
});
|
||||
|
||||
test('planning checks the URL, the protocol, and existing releases, and changes no file', () => withFixture(async (root, before) => {
|
||||
const plan = await planEngineBuild(root, { version: '0.1.8', releaseUrl }, windows);
|
||||
assert.deepEqual(await contents(root), before);
|
||||
assert.equal(plan.file, 'RuneStone-Browser_0.1.8_win32-x64.zip');
|
||||
assert.equal(plan.url, `${releaseUrl}/RuneStone-Browser_0.1.8_win32-x64.zip`);
|
||||
assert.equal(plan.key, 'windows-x86_64');
|
||||
assert.match(describePlan(plan), /Pin https:\/\/example\.com\/.*0\.1\.8_win32-x64\.zip in src-tauri\/browser-engine\.json/);
|
||||
assert.match(buildCommands(plan)[0].args.join(' '), /browser-runtime[\\/]scripts[\\/]package\.mjs --platform=win32 --arch=x64/);
|
||||
for (const url of ['http://example.com/engine', 'https://user:[email protected]/engine', 'https://example.com/engine?token=1']) {
|
||||
await assert.rejects(planEngineBuild(root, { version: '0.1.8', releaseUrl: url }, windows), /HTTPS/);
|
||||
}
|
||||
await assert.rejects(planEngineBuild(root, { version: '0.1', releaseUrl }, windows), /release version/);
|
||||
await mkdir(path.dirname(plan.artifacts.archive), { recursive: true });
|
||||
await writeFile(plan.artifacts.archive, 'old');
|
||||
await assert.rejects(planEngineBuild(root, { version: '0.1.8', releaseUrl }, windows), /already exists/);
|
||||
const main = path.join(root, 'browser-runtime/src/engine-main.mjs');
|
||||
await writeFile(main, (await readFile(main, 'utf8')).replace("event: 'ready', protocol: 1", "event: 'ready', protocol: 2"));
|
||||
await assert.rejects(planEngineBuild(root, { version: '0.1.9', releaseUrl }, windows), /protocol/);
|
||||
}));
|
||||
|
||||
test('the engine ZIP holds the files at its root, in name order, with the same bytes each time', async () => {
|
||||
const folder = await mkdtemp(path.join(tmpdir(), 'runestone-engine-zip-'));
|
||||
try {
|
||||
await mkdir(path.join(folder, 'locales'), { recursive: true });
|
||||
await mkdir(path.join(folder, 'resources'), { recursive: true });
|
||||
await writeFile(path.join(folder, 'RuneStone-Browser.exe'), Buffer.alloc(4096, 7));
|
||||
await writeFile(path.join(folder, 'locales', 'en-US.pak'), 'strings '.repeat(200));
|
||||
await writeFile(path.join(folder, 'resources', 'app.asar'), createHash('sha256').update('random').digest());
|
||||
const first = await zipFolder(folder);
|
||||
assert.deepEqual(first, await zipFolder(folder));
|
||||
const entries = unzip(first);
|
||||
assert.deepEqual(entries.map(entry => entry.name), ['RuneStone-Browser.exe', 'locales/en-US.pak', 'resources/app.asar']);
|
||||
assert.deepEqual(entries[1].data, Buffer.from('strings '.repeat(200)));
|
||||
assert.equal(entries[0].mode & 0o170000, 0o100000, 'Each entry is a regular file.');
|
||||
} finally { await rm(folder, { recursive: true, force: true }); }
|
||||
});
|
||||
|
||||
test('a completed build writes the ZIP, its record, and the pin', () => withFixture(async (root, before) => {
|
||||
const plan = await planEngineBuild(root, { version: '0.1.8', releaseUrl }, windows);
|
||||
const versions: string[] = [];
|
||||
const result = await runEngineBuild(plan, async () => {
|
||||
versions.push(JSON.parse(await readFile(path.join(root, engineFiles[0]), 'utf8')).version);
|
||||
await mkdir(plan.dist, { recursive: true });
|
||||
await writeFile(path.join(plan.dist, 'RuneStone-Browser.exe'), 'engine');
|
||||
}, path.join(root, 'output/attempt'));
|
||||
assert.deepEqual(versions, ['0.1.8'], 'The package step sees the new version.');
|
||||
const archive = await readFile(plan.artifacts.archive);
|
||||
const sha256 = createHash('sha256').update(archive).digest('hex');
|
||||
assert.equal(result.sha256, sha256);
|
||||
assert.deepEqual(JSON.parse(await readFile(plan.artifacts.metadata, 'utf8')), { version: '0.1.8', protocol: plan.protocol, platform: 'windows-x86_64', file: plan.file, url: plan.url, bytes: archive.length, sha256 });
|
||||
assert.deepEqual(JSON.parse(await readFile(path.join(root, engineFiles[2]), 'utf8')), { version: '0.1.8', protocol: plan.protocol, platforms: { 'windows-x86_64': { url: plan.url, bytes: archive.length, sha256 } } });
|
||||
const lock = JSON.parse(await readFile(path.join(root, engineFiles[1]), 'utf8'));
|
||||
assert.equal(lock.version, '0.1.8');
|
||||
assert.equal(lock.packages[''].version, '0.1.8');
|
||||
assert.notDeepEqual(await contents(root), before);
|
||||
}));
|
||||
|
||||
test('a failed build restores the version fields and the pin', () => withFixture(async (root, before) => {
|
||||
const plan = await planEngineBuild(root, { version: '0.1.8', releaseUrl }, windows);
|
||||
const staging = path.join(root, 'output/attempt');
|
||||
await assert.rejects(runEngineBuild(plan, async () => { throw Error('packager failed'); }, staging), /packager failed/);
|
||||
assert.deepEqual(await contents(root), before);
|
||||
assert.equal(await exists(plan.artifacts.archive), false);
|
||||
// The package step completes, but the program is missing.
|
||||
await assert.rejects(runEngineBuild(plan, async () => { await mkdir(plan.dist, { recursive: true }); }, staging), /no engine program/);
|
||||
assert.deepEqual(await contents(root), before);
|
||||
assert.equal(await exists(plan.artifacts.metadata), false);
|
||||
}));
|
||||
@@ -9,15 +9,13 @@ import { nextHotfix, releaseVersion } from '../../../scripts/releaseVersion.mjs'
|
||||
const repository = path.resolve('.');
|
||||
const exists = (file: string) => access(file).then(() => true, () => false);
|
||||
|
||||
// A copy of the files that the installer script reads, with a placeholder Browser runtime.
|
||||
// A copy of the files that the installer script reads.
|
||||
async function fixture() {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'runestone-installer-build-'));
|
||||
for (const file of [...versionFiles, 'src-tauri/tauri.windows.conf.json', 'src-tauri/windows/hooks.nsh']) {
|
||||
await mkdir(path.dirname(path.join(root, file)), { recursive: true });
|
||||
await copyFile(path.join(repository, file), path.join(root, file));
|
||||
}
|
||||
await mkdir(path.join(root, 'src-tauri/browser-engine'), { recursive: true });
|
||||
await writeFile(path.join(root, 'src-tauri/browser-engine/RuneStone-Browser.exe'), '');
|
||||
const before = await Promise.all(versionFiles.map(file => readFile(path.join(root, file), 'utf8')));
|
||||
const current = releaseVersion(JSON.parse(before[0]).version);
|
||||
return { root, before, current, next: releaseVersion(nextHotfix(current.version)) };
|
||||
@@ -78,14 +76,22 @@ test('a completed build sets the version fields only after the installer exists'
|
||||
}));
|
||||
|
||||
test('a failed build leaves the version fields unchanged', () => withFixture(async ({ root, before, next }) => {
|
||||
for (const failure of ['compile', 'version-resource', 'bundle', 'missing setup file']) {
|
||||
for (const failure of ['compile', 'version-resource', 'bundle', 'missing setup file', 'oversized setup file']) {
|
||||
const plan = await planInstallerBuild(root, next.version);
|
||||
if (failure === 'oversized setup file') plan.maxSetupBytes = 4;
|
||||
const staging = path.join(root, 'output', failure.replaceAll(' ', '-'));
|
||||
const steps = buildCommands(plan, path.join(staging, 'tauri.version.json'));
|
||||
let index = 0;
|
||||
await assert.rejects(runInstallerBuild(plan, async () => {
|
||||
if (steps[index++].step === failure) throw Error('The build command failed with exit code 1');
|
||||
}, staging));
|
||||
const attempt = runInstallerBuild(plan, async () => {
|
||||
const step = steps[index++].step;
|
||||
if (step === failure) throw Error('The build command failed with exit code 1');
|
||||
// A setup file over the size limit, as with a bundled browser engine.
|
||||
if (step === 'bundle' && failure === 'oversized setup file') {
|
||||
await mkdir(path.dirname(plan.artifacts.bundled), { recursive: true });
|
||||
await writeFile(plan.artifacts.bundled, 'setup');
|
||||
}
|
||||
}, staging);
|
||||
await assert.rejects(attempt, failure === 'oversized setup file' ? /more than the limit/ : Error);
|
||||
assert.deepEqual(await contents(root), before, 'A failure during ' + failure + ' must restore the version fields.');
|
||||
assert.equal(await exists(plan.artifacts.release), false);
|
||||
assert.equal(await exists(plan.artifacts.metadata), false);
|
||||
@@ -115,7 +121,11 @@ test('the plan stops before a build that breaks the release rules', () => withFi
|
||||
await writeFile(plan.artifacts.release, 'existing');
|
||||
await assert.rejects(planInstallerBuild(root, next.version), /already exists/);
|
||||
await rm(plan.releaseDir, { recursive: true });
|
||||
await rm(path.join(root, 'src-tauri/browser-engine'), { recursive: true });
|
||||
await assert.rejects(planInstallerBuild(root, next.version), /browser runtime is not staged/);
|
||||
// The browser engine is a separate download. The installer must not bundle it.
|
||||
const config = JSON.parse(before[1]);
|
||||
config.bundle.resources = { 'browser-engine/': 'browser-engine/' };
|
||||
await writeFile(tauriConfig, JSON.stringify(config));
|
||||
await assert.rejects(planInstallerBuild(root, next.version), /must not bundle the browser engine/);
|
||||
await writeFile(tauriConfig, before[1]);
|
||||
assert.deepEqual(await contents(root), before);
|
||||
}));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, readFile, writeFile, copyFile, rm } from 'node:fs/promises';
|
||||
import { mkdtemp, mkdir, readFile, writeFile, copyFile, rm } from 'node:fs/promises';
|
||||
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { tmpdir } from 'node:os';
|
||||
@@ -51,6 +51,10 @@ test('the stock Tauri template still provides what the hooks rely on', { skip: t
|
||||
assert.ok(index >= 0, 'The stock template no longer contains: ' + fragment);
|
||||
return index;
|
||||
};
|
||||
// NSIS_HOOK_POSTUNINSTALL removes the downloaded engine. It needs ${BUNDLEID}, $UpdateMode, and its place after the app data step.
|
||||
const removal = at('RmDir /r "$LOCALAPPDATA\\${BUNDLEID}"');
|
||||
assert.ok(text.indexOf('${AndIf} $UpdateMode <> 1') < removal);
|
||||
assert.ok(removal < at('!insertmacro NSIS_HOOK_POSTUNINSTALL'), 'The uninstall hook must run after the app data step.');
|
||||
const include = at('!include "{{installer_hooks}}"');
|
||||
assert.ok(include < at('!insertmacro MUI_PAGE_WELCOME'), 'Tauri must include the hooks before the first page.');
|
||||
assert.ok(include < at('\nSection EarlyChecks'), 'Tauri must include the hooks before its first section.');
|
||||
@@ -158,7 +162,7 @@ test('the hooks block downgrades in every installer mode', { skip: nsisSkip }, a
|
||||
RequestExecutionLevel user
|
||||
Name "RuneStone hook test"
|
||||
OutFile "${executable}"
|
||||
InstallDir "$TEMP"
|
||||
InstallDir "${path.join(root, 'install')}"
|
||||
AutoCloseWindow true
|
||||
VIProductVersion "${windows}"
|
||||
VIAddVersionKey "ProductName" "RuneStone hook test"
|
||||
@@ -242,3 +246,64 @@ SectionEnd
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
// NSIS_HOOK_POSTINSTALL removes the engine folder that releases up to 0.1.48 put in the installation folder.
|
||||
// NSIS_HOOK_POSTUNINSTALL removes the downloaded engine, keeps the browser profiles, and keeps everything during an update.
|
||||
test('the hooks remove old and downloaded browser engines but keep the profiles', { skip: nsisSkip }, async () => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'runestone-installer-engine-'));
|
||||
const bundle = 'com.runestone.hook-test-' + path.basename(root);
|
||||
const local = path.join(process.env.LOCALAPPDATA!, bundle);
|
||||
const install = path.join(root, 'install');
|
||||
const key = 'Software\\RuneStoneInstallerTest-' + path.basename(root);
|
||||
const program = async (name: string, updateMode: number) => {
|
||||
const executable = path.join(root, name + '.exe'), script = path.join(root, name + '.nsi');
|
||||
const source = `Unicode true
|
||||
SilentInstall silent
|
||||
RequestExecutionLevel user
|
||||
Name "RuneStone engine hook test"
|
||||
OutFile "${executable}"
|
||||
InstallDir "${install}"
|
||||
VIProductVersion "0.1.49.0"
|
||||
VIAddVersionKey "ProductName" "RuneStone engine hook test"
|
||||
VIAddVersionKey "FileVersion" "0.1.49"
|
||||
VIAddVersionKey "ProductVersion" "0.1.49"
|
||||
!define RUNESTONE_UNINSTKEY "${key}"
|
||||
!define BUNDLEID "${bundle}"
|
||||
!include "${hooks}"
|
||||
Var UpdateMode
|
||||
Section Install
|
||||
StrCpy $UpdateMode ${updateMode}
|
||||
!insertmacro NSIS_HOOK_POSTINSTALL
|
||||
!insertmacro NSIS_HOOK_POSTUNINSTALL
|
||||
SectionEnd
|
||||
`;
|
||||
await writeFile(script, source);
|
||||
compile(script, source);
|
||||
return executable;
|
||||
};
|
||||
const seed = async () => {
|
||||
await mkdir(path.join(install, 'browser-engine', 'locales'), { recursive: true });
|
||||
await writeFile(path.join(install, 'browser-engine', 'RuneStone-Browser.exe'), 'old engine');
|
||||
await writeFile(path.join(install, 'runestone.exe'), 'app');
|
||||
await mkdir(path.join(local, 'browser-engine', '0.1.8'), { recursive: true });
|
||||
await writeFile(path.join(local, 'browser-engine', '0.1.8', 'RuneStone-Browser.exe'), 'engine');
|
||||
await mkdir(path.join(local, 'browser'), { recursive: true });
|
||||
await writeFile(path.join(local, 'browser', 'browser.json'), '{}');
|
||||
};
|
||||
try {
|
||||
await seed();
|
||||
assert.equal(spawnSync(await program('remove', 0), ['/S'], { windowsHide: true, timeout: 30000 }).status, 0);
|
||||
assert.equal(existsSync(path.join(install, 'browser-engine')), false, 'Setup removes the old engine folder.');
|
||||
assert.equal(existsSync(path.join(install, 'runestone.exe')), true);
|
||||
assert.equal(existsSync(path.join(local, 'browser-engine')), false, 'Uninstall removes the downloaded engine.');
|
||||
assert.equal(existsSync(path.join(local, 'browser', 'browser.json')), true, 'The profiles stay.');
|
||||
await seed();
|
||||
assert.equal(spawnSync(await program('update', 1), ['/S'], { windowsHide: true, timeout: 30000 }).status, 0);
|
||||
assert.equal(existsSync(path.join(local, 'browser-engine', '0.1.8', 'RuneStone-Browser.exe')), true, 'An update keeps the downloaded engine.');
|
||||
} finally {
|
||||
spawnSync('reg', ['delete', 'HKCU\\' + key, '/f'], { windowsHide: true });
|
||||
assert.equal(local.startsWith(process.env.LOCALAPPDATA!) && path.basename(local).startsWith('com.runestone.hook-test-'), true);
|
||||
await rm(local, { recursive: true, force: true });
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user