Files
orca/config/scripts/desktop-distribution/runtime-verification/verify-packaged-serve-payload.cjs
Neil 9bd6272fdf refactor(tooling): organize build, CI, and test owners (#38130)
* refactor(tooling): organize test runtime and maintenance owners

* test(tooling): preserve relocated runtime fixture dependency edges

* refactor(tooling): preserve xterm routing owner checks

* ci(tooling): include routing owner in sparse checkout

* test(tooling): preserve copied workflow dependency closures

* fix(tooling): normalize hashed JSON assets in release parity

* fix(tooling): distinguish asset aliases from literal references

* fix(tooling): frame normalized asset reference payloads

* fix(tooling): normalize decoded JSON asset references

* refactor(tooling): organize build, CI and release owners

* fix(tooling): align moved generator and workflow path contracts

* fix(tooling): update generated plugin bundle owner banners

* test(tooling): create relocated mobile verifier fixture directories

* test(tooling): copy headless detector dependency closure

* test(tooling): align native batch policy fixtures with owner path

* fix(tooling): link moved reliability command to historical evidence

* fix(tooling): preserve nested sandbox owners and canonical guide source

* fix(tooling): target release version owners in checkout fixture
2026-11-04 21:08:19 -08:00

125 lines
4.7 KiB
JavaScript

const { existsSync, readFileSync, statSync } = require('node:fs')
const { dirname, join, posix } = require('node:path')
const WEB_CLIENT_INDEX = 'web-index.html'
const REMEDY = 'Run pnpm build:web-from-renderer (build:desktop and build:release already do).'
function failure(message) {
return new Error(`[verify-packaged-serve-payload] ${message}`)
}
/** The relative files web-index.html loads; the page is useless if any is missing. */
function referencedAssets(html) {
const assets = new Set()
for (const match of html.matchAll(/\b(?:src|href)=["']\.\/([^"'?#]+)/g)) {
assets.add(posix.normalize(match[1]))
}
return [...assets]
}
// Plain Node orcad cannot read app.asar; unpack browser client and dependencies beside CLI app root or serve launches without web client (#30100).
function assertPackagedWebClient(resourcesDir) {
const outDir = join(resourcesDir, 'app.asar.unpacked', 'out')
if (!existsSync(join(outDir, 'cli'))) {
throw failure(`no unpacked CLI at ${join(outDir, 'cli')}; the web client root resolves from it`)
}
const webRoot = join(outDir, 'renderer')
const manifestPath = join(webRoot, '.orca-web-client-files.json')
if (!existsSync(manifestPath)) {
throw failure(`missing unpacked web file manifest at ${manifestPath}. ${REMEDY}`)
}
const files = JSON.parse(readFileSync(manifestPath, 'utf8'))
if (
!Array.isArray(files) ||
!files.includes(WEB_CLIENT_INDEX) ||
!files.every(
(file) =>
typeof file === 'string' &&
(file === WEB_CLIENT_INDEX || /^(?:assets|cmaps|standard_fonts|wasm)\//.test(file)) &&
!file.includes('\\') &&
!file.includes('\0') &&
file.split('/').every((part) => part !== '' && part !== '.' && part !== '..') &&
!file.endsWith('.map')
)
) {
throw failure(`invalid web file manifest at ${manifestPath}`)
}
const absent = files.filter(
(file) => !statSync(join(webRoot, file), { throwIfNoEntry: false })?.isFile()
)
if (absent.length > 0) {
throw failure(`missing unpacked web files: ${absent.join(', ')}`)
}
const indexPath = join(webRoot, WEB_CLIENT_INDEX)
if (!existsSync(indexPath)) {
throw failure(`missing unpacked ${indexPath}. ${REMEDY}`)
}
const assets = referencedAssets(readFileSync(indexPath, 'utf8'))
if (assets.length === 0) {
throw failure(`${indexPath} references no ./ assets. ${REMEDY}`)
}
const missing = assets.filter(
(asset) =>
asset.startsWith('..') ||
!files.includes(asset) ||
!existsSync(join(webRoot, ...asset.split('/')))
)
if (missing.length > 0) {
throw failure(`${indexPath} loads files missing from ${webRoot}: ${missing.join(', ')}`)
}
console.log(`[verify-packaged-serve-payload] OK — ${String(assets.length)} asset(s) unpacked`)
}
// Relative to out/; mirrors ORCAD_LOCAL_SERVE_SELECTION_ENTRY, which a test pins.
const SERVE_SELECTION_ENTRY = 'main/orcad/orcad-local-serve-selection-entry.js'
/** The file Node loads for a relative require, or null when none is unpacked. */
function resolveRequiredFile(path) {
const candidates = [path, `${path}.js`, `${path}.json`, join(path, 'index.js')]
return (
candidates.find((candidate) => statSync(candidate, { throwIfNoEntry: false })?.isFile()) ?? null
)
}
/** Relative requires reachable from `entry`, so a packed chunk fails here and not at serve time. */
function missingRelativeRequires(entry) {
const missing = new Set()
const visited = new Set([entry])
const queue = [entry]
while (queue.length > 0) {
const file = queue.shift()
for (const match of readFileSync(file, 'utf8').matchAll(
/\brequire\(["'](\.{1,2}\/[^"']+)["']\)/g
)) {
const required = join(dirname(file), match[1])
const resolved = resolveRequiredFile(required)
if (!resolved) {
missing.add(required)
} else if (!visited.has(resolved)) {
visited.add(resolved)
queue.push(resolved)
}
}
}
return [...missing]
}
// Run host-selection entry as plain Node from app executable; packed entry forces every serve into fallback (#27404).
function assertPackagedServeSelectionEntry(resourcesDir) {
const entry = join(resourcesDir, 'app.asar.unpacked', 'out', ...SERVE_SELECTION_ENTRY.split('/'))
if (!existsSync(entry)) {
throw failure(`missing unpacked ${entry}; orca serve runs it under ELECTRON_RUN_AS_NODE`)
}
const missing = missingRelativeRequires(entry)
if (missing.length > 0) {
throw failure(`${entry} requires files missing from the unpacked app: ${missing.join(', ')}`)
}
console.log('[verify-packaged-serve-payload] OK — serve-selection entry unpacked')
}
module.exports = {
SERVE_SELECTION_ENTRY,
assertPackagedServeSelectionEntry,
assertPackagedWebClient
}