Skip to content

Commit

Permalink
fix: support .tar.xz archives for Firefox 135+ (#13391)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Dec 10, 2024
1 parent 75f76cc commit d247a7f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/guides/system-requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
- Firefox browser system requirements:

- https://www.mozilla.org/en-US/firefox/system-requirements/
- The `xz` utility is required to unpack Firefox versions 135+ for Linux.
9 changes: 7 additions & 2 deletions packages/browsers/src/browser-data/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import {getJSON} from '../httpUtil.js';

import {BrowserPlatform, type ProfileOptions} from './types.js';

function getFormat(buildId: string): string {
const majorVersion = Number(buildId.split('.').shift()!);
return majorVersion >= 135 ? 'xz' : 'bz2';
}

function archiveNightly(platform: BrowserPlatform, buildId: string): string {
switch (platform) {
case BrowserPlatform.LINUX:
return `firefox-${buildId}.en-US.${platform}-x86_64.tar.bz2`;
return `firefox-${buildId}.en-US.${platform}-x86_64.tar.${getFormat(buildId)}`;
case BrowserPlatform.MAC_ARM:
case BrowserPlatform.MAC:
return `firefox-${buildId}.en-US.mac.dmg`;
Expand All @@ -27,7 +32,7 @@ function archiveNightly(platform: BrowserPlatform, buildId: string): string {
function archive(platform: BrowserPlatform, buildId: string): string {
switch (platform) {
case BrowserPlatform.LINUX:
return `firefox-${buildId}.tar.bz2`;
return `firefox-${buildId}.tar.${getFormat(buildId)}`;
case BrowserPlatform.MAC_ARM:
case BrowserPlatform.MAC:
return `Firefox ${buildId}.dmg`;
Expand Down
62 changes: 61 additions & 1 deletion packages/browsers/src/fileUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {spawnSync} from 'child_process';
import {spawnSync, spawn} from 'child_process';
import {createReadStream} from 'fs';
import {mkdir, readdir} from 'fs/promises';
import * as path from 'path';
import {Stream} from 'stream';

import extractZip from 'extract-zip';
import tar from 'tar-fs';
Expand Down Expand Up @@ -39,6 +40,8 @@ export async function unpackArchive(
`Failed to extract ${archivePath} to ${folderPath}: ${result.output}`,
);
}
} else if (archivePath.endsWith('.tar.xz')) {
await extractTarXz(archivePath, folderPath);
} else {
throw new Error(`Unsupported archive format: ${archivePath}`);
}
Expand All @@ -57,6 +60,63 @@ function extractTar(tarPath: string, folderPath: string): Promise<void> {
});
}

/**
* @internal
*/
function createXzStream() {
const child = spawn('xz', ['-d']);
const stream = new Stream.Transform({
transform(chunk, encoding, callback) {
if (!child.stdin.write(chunk, encoding)) {
child.stdin.once('drain', callback);
} else {
callback();
}
},

flush(callback) {
if (child.stdout.destroyed) {
callback();
} else {
child.stdin.end();
child.stdout.on('close', callback);
}
},
});

child.stdin.on('error', e => {
if ('code' in e && e.code === 'EPIPE') {
// finished before reading the file finished (i.e. head)
stream.emit('end');
} else {
stream.destroy(e);
}
});

child.stdout
.on('data', data => {
return stream.push(data);
})
.on('error', e => {
return stream.destroy(e);
});

return stream;
}

/**
* @internal
*/
function extractTarXz(tarPath: string, folderPath: string): Promise<void> {
return new Promise((fulfill, reject) => {
const tarStream = tar.extract(folderPath);
tarStream.on('error', reject);
tarStream.on('finish', fulfill);
const readStream = createReadStream(tarPath);
readStream.pipe(createXzStream()).pipe(tarStream);
});
}

/**
* @internal
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/browsers/test/src/firefox/firefox-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ describe('Firefox', () => {
resolveDownloadUrl(BrowserPlatform.LINUX, '111.0a1'),
'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/firefox-111.0a1.en-US.linux-x86_64.tar.bz2',
);
assert.strictEqual(
resolveDownloadUrl(BrowserPlatform.LINUX, '135.0a1'),
'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/firefox-135.0a1.en-US.linux-x86_64.tar.xz',
);
assert.strictEqual(
resolveDownloadUrl(BrowserPlatform.LINUX, '136.0a1'),
'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/firefox-136.0a1.en-US.linux-x86_64.tar.xz',
);
assert.strictEqual(
resolveDownloadUrl(BrowserPlatform.MAC, '111.0a1'),
'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/firefox-111.0a1.en-US.mac.dmg',
Expand Down

0 comments on commit d247a7f

Please sign in to comment.