Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
#!/usr/bin/env node
/**
* Build extension submission zips from the current HEAD commit.
*
* node scripts/build-zip.mjs
*
* Or via npm: npm run build:zip
*
* Uses `git archive --format=zip` so the output is POSIX-style with
* forward-slash paths, which AMO's automated validator requires (it
* silently rejects zips made by PowerShell's Compress-Archive because
* those carry Windows backslash separators inside the central directory).
*
* Source-of-truth is the HEAD commit, NOT the working tree — uncommitted
* local artifacts (.test-profile, .claude, dist/, etc.) won't leak into
* the submission zip. If you need to ship something, commit it first.
*
* Output:
* dist/webbrain-chrome-<version>.zip
* dist/webbrain-edge-<version>.zip
* dist/webbrain-firefox-<version>.zip
* dist/webbrain-<source-name>-corresponding-source.zip
*
* <version> is read from package.json at HEAD, and every archived manifest
* must match it. An uncommitted version bump is rejected instead of creating
* a new-looking filename around an old manifest.
*/
import { readFileSync, mkdirSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { fileURLToPath, pathToFileURL } from 'node:url';
import path from 'node:path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '..');
const targets = [
{ packageName: 'chrome', sourceDir: 'chrome' },
// Microsoft Edge uses the same Chromium-compatible MV3 source tree.
{ packageName: 'edge', sourceDir: 'chrome' },
{ packageName: 'firefox', sourceDir: 'firefox' },
];
const CORRESPONDING_SOURCE_ROOT = 'dist/corresponding-source';
export function correspondingSourceArchivePath(sourceName) {
return `dist/webbrain-${sourceName}-corresponding-source.zip`;
}
export function assertCorrespondingSourceArchiveEntries(entries, sourceName, trackedFiles, label) {
const prefix = `${sourceName}/`;
for (const relativePath of trackedFiles) {
if (!entries.includes(`${prefix}${relativePath}`)) {
throw new Error(`${label} is missing tracked corresponding source ${relativePath}.`);
}
}
}
export function assertMatchingArchiveVersion(expected, actual, label) {
if (actual !== expected) {
throw new Error(`${label} is ${actual}, but the release package version is ${expected}.`);
}
}
export function assertPackageRootGplLicense(source, label) {
const text = String(source || '');
if (!/combined work under GPL-3\.0-or-later/.test(text)
|| !/GNU GENERAL PUBLIC LICENSE\s+Version 3, 29 June 2007/.test(text)) {
throw new Error(`${label} must identify the combined package as GPL-3.0-or-later and include GPLv3.`);
}
}
const FLAG_LICENSE_PATH = 'icons/flags/LICENSE.flag-icons.txt';
const REJECTED_FLAG_LICENSE_PATH = 'icons/flags/LICENSE.flag-icons';
export function listZipEntryNames(filePath) {
const archive = readFileSync(filePath);
const eocdSignature = 0x06054b50;
const centralHeaderSignature = 0x02014b50;
const earliestEocd = Math.max(0, archive.length - 0xffff - 22);
let eocdOffset = -1;
for (let offset = archive.length - 22; offset >= earliestEocd; offset -= 1) {
if (
archive.readUInt32LE(offset) === eocdSignature
&& offset + 22 + archive.readUInt16LE(offset + 20) === archive.length
) {
eocdOffset = offset;
break;
}
}
if (eocdOffset < 0) throw new Error(`${filePath} has no ZIP end-of-central-directory record.`);
const entryCount = archive.readUInt16LE(eocdOffset + 10);
let offset = archive.readUInt32LE(eocdOffset + 16);
const entries = [];
for (let index = 0; index < entryCount; index += 1) {
if (offset + 46 > archive.length || archive.readUInt32LE(offset) !== centralHeaderSignature) {
throw new Error(`${filePath} has an invalid ZIP central-directory entry at index ${index}.`);
}
const nameLength = archive.readUInt16LE(offset + 28);
const extraLength = archive.readUInt16LE(offset + 30);
const commentLength = archive.readUInt16LE(offset + 32);
const nameStart = offset + 46;
const nameEnd = nameStart + nameLength;
if (nameEnd > archive.length) {
throw new Error(`${filePath} has a truncated ZIP filename at index ${index}.`);
}
entries.push(archive.toString('utf8', nameStart, nameEnd));
offset = nameEnd + extraLength + commentLength;
}
return entries;
}
export function assertSingleRootExtensionManifest(entries, label) {
const manifests = entries.filter(entry => (
String(entry || '').split('/').pop()?.toLowerCase() === 'manifest.json'
));
if (manifests.length !== 1 || manifests[0] !== 'manifest.json') {
const found = manifests.length ? manifests.join(', ') : 'none';
throw new Error(
`${label} must contain exactly one manifest.json at the package root; found: ${found}.`
);
}
}
export function assertStoreSafeFlagLicenseEntries(entries, label) {
if (!entries.includes(FLAG_LICENSE_PATH)) {
throw new Error(`${label} is missing ${FLAG_LICENSE_PATH}.`);
}
if (entries.includes(REJECTED_FLAG_LICENSE_PATH)) {
throw new Error(`${label} still contains Opera-rejected ${REJECTED_FLAG_LICENSE_PATH}.`);
}
}
const STORE_REVIEWED_JAVASCRIPT_PATHS = [
'vendor/pdfjs/compat.mjs',
'vendor/pdfjs/index.mjs',
'vendor/pdfjs/pdf.mjs',
'vendor/pdfjs/pdf.worker.mjs',
'vendor/pdfjs/worker.mjs',
'src/providers/manager.js',
];
const STORE_REJECTION_PATTERNS = [
{
// core-js's Object.create fallback for old IE (shipped in the PDF.js legacy
// build); Chrome Web Store rejected it as obfuscated code.
pattern: /\bActiveXObject\b|\bNullProtoObjectVia(?:ActiveX|IFrame)\b/,
reason: 'core-js Internet Explorer ActiveX/iframe fallback',
},
{
pattern: /\bLT\s*\+\s*SCRIPT\s*\+\s*GT\s*\+\s*content\s*\+\s*LT\s*\+\s*['"]\/['"]\s*\+\s*SCRIPT\s*\+\s*GT\b/,
reason: 'split PDF.js <script> construction',
},
{
pattern: /['"]java['"]\s*\+\s*SCRIPT\s*\+\s*['"]:['"]/,
reason: 'split javascript: scheme',
},
{
pattern: /data:(?:image|audio)\/[a-z0-9.+-]+;base64,[a-z0-9+/=]{128,}/i,
reason: 'long inline base64 media payload',
},
];
export function assertStoreReviewableJavaScript(source, label) {
for (const { pattern, reason } of STORE_REJECTION_PATTERNS) {
if (pattern.test(source)) {
throw new Error(`${label} contains ${reason}; use transparent source or a packaged asset.`);
}
}
}
// Archiving a subtree (HEAD:src/chrome) hides the root .gitattributes, so a
// machine with core.autocrlf=true would write CRLF text files into the zip.
// Keep the committed LF bytes, so vendored files stay identical to upstream.
const GIT_ARCHIVE_AS_COMMITTED = ['-c', 'core.autocrlf=false', '-c', 'core.eol=lf'];
function readTextAtHead(relativePath) {
return execFileSync('git', ['show', `HEAD:${relativePath}`], {
cwd: root,
encoding: 'utf8',
maxBuffer: 8 * 1024 * 1024,
stdio: ['ignore', 'pipe', 'pipe'],
});
}
function readJsonAtHead(relativePath) {
return JSON.parse(readTextAtHead(relativePath));
}
function listTreeEntryNamesAtHead(relativePath) {
return execFileSync(
'git',
['ls-tree', '-r', '--name-only', `HEAD:${relativePath}`],
{
cwd: root,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}
).split(/\r?\n/).filter(Boolean);
}
function listTreeDirectoryNamesAtHead(relativePath) {
return execFileSync(
'git',
['ls-tree', '-d', '--name-only', `HEAD:${relativePath}`],
{
cwd: root,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}
).split(/\r?\n/).filter(Boolean);
}
function runCli() {
const workingPackage = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8'));
const headPackage = readJsonAtHead('package.json');
assertMatchingArchiveVersion(
headPackage.version,
workingPackage.version,
'Working-tree package.json version'
);
const version = headPackage.version;
for (const { sourceDir } of targets) {
const manifest = readJsonAtHead(`src/${sourceDir}/manifest.json`);
assertMatchingArchiveVersion(version, manifest.version, `HEAD src/${sourceDir}/manifest.json version`);
assertPackageRootGplLicense(
readTextAtHead(`src/${sourceDir}/LICENSE`),
`HEAD src/${sourceDir}/LICENSE`
);
const sourceEntries = listTreeEntryNamesAtHead(`src/${sourceDir}`);
assertSingleRootExtensionManifest(sourceEntries, `HEAD src/${sourceDir}`);
assertStoreSafeFlagLicenseEntries(sourceEntries, `HEAD src/${sourceDir}`);
for (const relativePath of STORE_REVIEWED_JAVASCRIPT_PATHS) {
const archivePath = `src/${sourceDir}/${relativePath}`;
assertStoreReviewableJavaScript(readTextAtHead(archivePath), `HEAD ${archivePath}`);
}
}
const distDir = path.join(root, 'dist');
mkdirSync(distDir, { recursive: true });
console.log(`Building extension zips for v${version} from HEAD …`);
for (const { packageName, sourceDir } of targets) {
const out = path.join(distDir, `webbrain-${packageName}-${version}.zip`);
// -o writes directly to the file; avoids needing shell redirection,
// so this runs identically on bash, zsh, cmd, and PowerShell.
execFileSync(
'git',
[...GIT_ARCHIVE_AS_COMMITTED, 'archive', '--format=zip', '-o', out, `HEAD:src/${sourceDir}`],
{ stdio: 'inherit', cwd: root }
);
const packageEntries = listZipEntryNames(out);
const packageLabel = `dist/webbrain-${packageName}-${version}.zip`;
assertSingleRootExtensionManifest(packageEntries, packageLabel);
assertStoreSafeFlagLicenseEntries(packageEntries, packageLabel);
console.log(` ✓ dist/webbrain-${packageName}-${version}.zip`);
}
// These sources are a release artifact, not scratch output. Build their ZIPs
// from HEAD just like the browser packages so the tagged tree and uploaded
// assets describe exactly the same GPL-covered runtime source.
for (const sourceName of listTreeDirectoryNamesAtHead(CORRESPONDING_SOURCE_ROOT)) {
const sourcePath = `${CORRESPONDING_SOURCE_ROOT}/${sourceName}`;
const relativeOut = correspondingSourceArchivePath(sourceName);
const out = path.join(root, relativeOut);
execFileSync(
'git',
[
...GIT_ARCHIVE_AS_COMMITTED,
'archive',
'--format=zip',
'-o',
out,
`--prefix=${sourceName}/`,
`HEAD:${sourcePath}`,
],
{ stdio: 'inherit', cwd: root }
);
assertCorrespondingSourceArchiveEntries(
listZipEntryNames(out),
sourceName,
listTreeEntryNamesAtHead(sourcePath),
relativeOut
);
console.log(` ✓ ${relativeOut}`);
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
try {
runCli();
} catch (error) {
console.error(`build-zip: ${error.message}`);
process.exit(1);
}
}