// Verifies that dist/ is a loadable MV3 extension: the manifest parses and // every file it references exists, and every local asset referenced from the // built HTML pages exists. Plain node, no dependencies. // // Usage: node scripts/verify-dist.mjs [distDir] import { existsSync, readFileSync, readdirSync } from 'node:fs' import { dirname, join, resolve } from 'node:path' const dist = resolve(process.argv[2] ?? 'dist') const problems = [] let checked = 0 function requireFile(rel, why) { checked++ if (!existsSync(join(dist, rel))) problems.push(`missing ${rel} (${why})`) } if (!existsSync(dist)) { console.error(`verify-dist: ${dist} does not exist — run \`npm run build\` first`) process.exit(1) } let manifest try { manifest = JSON.parse(readFileSync(join(dist, 'manifest.json'), 'utf8')) } catch (err) { console.error(`verify-dist: cannot read dist/manifest.json: ${err.message}`) process.exit(1) } if (manifest.manifest_version !== 3) { problems.push(`manifest_version is ${manifest.manifest_version}, expected 3`) } // Files the manifest references. for (const icon of Object.values(manifest.icons ?? {})) { requireFile(icon, 'manifest icons') } for (const icon of Object.values(manifest.action?.default_icon ?? {})) { requireFile(icon, 'action.default_icon') } // Chrome uses a service worker; Firefox MV3 uses a module event page. if (manifest.background?.service_worker) { requireFile(manifest.background.service_worker, 'background.service_worker') } for (const script of manifest.background?.scripts ?? []) { requireFile(script, 'background.scripts') } for (const cs of manifest.content_scripts ?? []) { for (const js of cs.js ?? []) requireFile(js, 'content_scripts js') for (const css of cs.css ?? []) requireFile(css, 'content_scripts css') } if (manifest.action?.default_popup) { requireFile(manifest.action.default_popup, 'action.default_popup') } for (const war of manifest.web_accessible_resources ?? []) { for (const res of war.resources ?? []) { if (!res.includes('*')) requireFile(res, 'web_accessible_resources') } } // Pages created at runtime rather than referenced from the manifest. // Firefox has no offscreen API, so its build drops the permission and has no // offscreen page for the worker to create. if (manifest.permissions?.includes('offscreen')) { requireFile('offscreen.html', 'created by the worker via chrome.offscreen') } requireFile('welcome.html', 'opened by the worker on install (src/lib/welcome.ts)') // Firefox's manifest is rewritten for the platform at build time. Check what // that rewrite is responsible for, so a silently skipped transform fails here // rather than at `about:debugging`. const gecko = manifest.browser_specific_settings?.gecko const isFirefox = Boolean(gecko) if (process.env.SUBSTANDARD_TARGET === 'firefox' && !isFirefox) { problems.push('SUBSTANDARD_TARGET=firefox but the manifest has no browser_specific_settings.gecko') } if (isFirefox) { if (manifest.background?.service_worker) { problems.push('Firefox manifest still has background.service_worker') } if (!manifest.background?.scripts?.length) { problems.push('Firefox manifest has no background.scripts') } if (manifest.background?.type !== 'module') { problems.push('Firefox background is not type "module"') } if (manifest.key) problems.push('Firefox manifest still has a key') if (manifest.minimum_chrome_version) { problems.push('Firefox manifest still has minimum_chrome_version') } if (manifest.permissions?.includes('offscreen')) { problems.push('Firefox manifest still asks for the offscreen permission') } if (gecko.id !== 'substandard@permadeath.com') { problems.push(`Firefox gecko id is ${gecko.id}, expected substandard@permadeath.com`) } if (!gecko.strict_min_version) problems.push('Firefox manifest has no gecko.strict_min_version') const dataCollection = gecko.data_collection_permissions?.required if (!Array.isArray(dataCollection) || !dataCollection.includes('none')) { problems.push('Firefox manifest does not declare data_collection_permissions.required = ["none"]') } } // Vendored assets referenced from built JS as extension-root "/vendored/..." // string literals (reader icons in src/lib/readers.ts). for (const name of readdirSync(dist, { recursive: true }).filter((f) => f.endsWith('.js'))) { const js = readFileSync(join(dist, name), 'utf8') for (const m of js.matchAll(/"(\/vendored\/[\w.-]+)"/g)) { requireFile(m[1].slice(1), `referenced by ${name}`) } // A sourceMappingURL pointing at a file that did not ship reads as a // broken build to anyone reviewing it, and gives a 404 in devtools. const map = /\/\/# sourceMappingURL=(\S+)/.exec(js) if (map) requireFile(join(dirname(String(name)), map[1]), `sourcemap of ${name}`) } // The signed-out popup must not statically import the OAuth client chunk: it // never authorizes anything, and keeping that chunk off the initial download // is why src/popup/popup.ts and cards/labels.ts import src/lib/oauth.ts // dynamically (TODO.md). Identify the chunk from its sourcemap rather than its // hashed filename. const popupPath = join(dist, 'popup.js') if (!existsSync(popupPath)) { problems.push('missing popup.js (the popup entry chunk)') } else { const popupJs = readFileSync(popupPath, 'utf8') const staticChunks = [...popupJs.matchAll(/(?:from|import)"\.\/chunks\/([^"]+)"/g)].map( (m) => m[1], ) const dynamicChunks = [...popupJs.matchAll(/import\("\.\/chunks\/([^"]+)"\)/g)].map((m) => m[1]) const chunkSources = (name) => { const mapPath = join(dist, 'chunks', `${name}.map`) if (!existsSync(mapPath)) return [] return JSON.parse(readFileSync(mapPath, 'utf8')).sources } /** The chunk that holds a source file, found by its sourcemap. */ const isChunkFor = (name, source) => chunkSources(name).some((s) => s.endsWith(`/${source}`)) const isOauthChunk = (name) => isChunkFor(name, 'src/lib/oauth.ts') const isLabelsChunk = (name) => isChunkFor(name, 'src/lib/labels.ts') // Both are signed-out-irrelevant: OAuth is for writes, and labels are only // needed once detection has found a publication to label. The popup entry // must reach each through import(), never a static edge. for (const [what, is] of [ ['OAuth', isOauthChunk], ['labels/@atproto.api', isLabelsChunk], ]) { const staticChunk = staticChunks.find(is) if (staticChunk) problems.push(`popup.js statically imports the ${what} chunk ${staticChunk}`) if (!dynamicChunks.some(is)) { problems.push(`popup.js has no dynamic import of the ${what} chunk`) } } } // Local script/stylesheet references inside built HTML pages. for (const name of readdirSync(dist).filter((f) => f.endsWith('.html'))) { const html = readFileSync(join(dist, name), 'utf8') for (const m of html.matchAll(/(?:src|href)="([^"]+)"/g)) { const ref = m[1] if (/^[a-z]+:/i.test(ref) || ref.startsWith('#')) continue // external / anchor requireFile(ref.replace(/^\.?\//, '').split(/[?#]/)[0], `referenced by ${name}`) } } if (problems.length > 0) { console.error(`verify-dist: FAILED (${problems.length} problem(s)):`) for (const p of problems) console.error(` - ${p}`) process.exit(1) } console.log(`verify-dist: OK — ${checked} referenced files present in ${dist}`)