fix(desktop): serve packaged H5 shell from Electron builds

Electron's sidecar runs outside app.asar, so H5 static files must be available as normal unpacked files. Point the sidecar at the unpacked renderer dist and keep a server fallback for stale app.asar-style paths.

Constraint: Packaged Bun sidecars cannot read app.asar paths with ordinary fs stat calls.
Rejected: Serve H5 from app.asar directly | the external sidecar is not Electron and does not get asar filesystem support.
Confidence: high
Scope-risk: narrow
Directive: Keep package-smoke checking app.asar.unpacked/dist/index.html before changing asarUnpack or H5 dist paths.
Tested: bun test src/server/__tests__/h5-access-auth.test.ts src/server/__tests__/h5-access-policy.test.ts
Tested: bun test desktop/electron/services/sidecarManager.test.ts scripts/quality-gate/package-smoke/index.test.ts
Tested: bun run check:server
Tested: cd desktop && bun run check:electron
Tested: git diff --check
Tested: SKIP_INSTALL=1 SIGN_BUILD=0 MAC_TARGETS=dmg desktop/scripts/build-macos-arm64.sh
Tested: packaged sidecar curl /?serverUrl=...&h5Token=... returned HTTP 200
Not-tested: Gatekeeper notarization for the local ad-hoc DMG
This commit is contained in:
程序员阿江(Relakkes) 2026-06-01 23:32:56 +08:00
parent 386a41e606
commit 232855fe77
7 changed files with 53 additions and 4 deletions

View File

@ -84,7 +84,7 @@ function getServerRuntime() {
serverRuntime ??= new ElectronServerRuntime({
desktopRoot: unpackedRoot(),
appRoot: appRoot(),
h5DistDir: path.join(appRoot(), 'dist'),
h5DistDir: path.join(unpackedRoot(), 'dist'),
})
return serverRuntime
}

View File

@ -40,18 +40,18 @@ describe('Electron sidecar manager', () => {
expect(plan.env.CLAUDE_H5_DIST_DIR).toBe(path.join('/app/desktop', 'dist'))
})
it('can keep sidecar binaries unpacked while pointing app-root at app.asar', () => {
it('can keep sidecar binaries and H5 assets unpacked while pointing app-root at app.asar', () => {
const plan = createServerPlan({
desktopRoot: '/Applications/App.app/Contents/Resources/app.asar.unpacked',
appRoot: '/Applications/App.app/Contents/Resources/app.asar',
h5DistDir: '/Applications/App.app/Contents/Resources/app.asar/dist',
h5DistDir: '/Applications/App.app/Contents/Resources/app.asar.unpacked/dist',
port: 49321,
env: {},
})
expect(plan.command).toContain('/Applications/App.app/Contents/Resources/app.asar.unpacked/src-tauri/binaries/claude-sidecar-')
expect(plan.args).toContain('/Applications/App.app/Contents/Resources/app.asar')
expect(plan.env.CLAUDE_H5_DIST_DIR).toBe('/Applications/App.app/Contents/Resources/app.asar/dist')
expect(plan.env.CLAUDE_H5_DIST_DIR).toBe('/Applications/App.app/Contents/Resources/app.asar.unpacked/dist')
})
it('passes portable config and adapter server URL through the sidecar env', () => {

View File

@ -9,6 +9,7 @@
"productName": "Claude Code Haha",
"asar": true,
"asarUnpack": [
"dist/**",
"node_modules/node-pty/**",
"src-tauri/binaries/**"
],

View File

@ -68,6 +68,7 @@ describe('packaged artifact inspection', () => {
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/MacOS/Claude Code Haha')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app-update.yml')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/dist/index.html')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/src-tauri/binaries/claude-sidecar-aarch64-apple-darwin')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/package.json')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/prebuilds/darwin-arm64/pty.node')
@ -94,6 +95,26 @@ describe('packaged artifact inspection', () => {
expect(report.optionalArtifacts.some((artifact) => artifact.path.endsWith('latest-mac.yml'))).toBe(true)
expect(report.passedChecks.some((check) => check.label.includes('update metadata referenced artifact'))).toBe(true)
expect(report.passedChecks.some((check) => check.label.includes('macOS update artifact blockmap'))).toBe(true)
expect(report.passedChecks.some((check) => check.label === 'macOS unpacked H5 shell')).toBe(true)
})
test('fails macOS inspection when the H5 shell is not unpacked for the sidecar', async () => {
const rootDir = createRepoRoot()
tempDirs.push(rootDir)
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Info.plist')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/MacOS/Claude Code Haha')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app-update.yml')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/src-tauri/binaries/claude-sidecar-aarch64-apple-darwin')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/package.json')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/prebuilds/darwin-arm64/pty.node')
writeFile(rootDir, 'desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/prebuilds/darwin-arm64/spawn-helper')
const report = await inspectPackagedArtifacts(rootDir, { platform: 'macos', packageKind: 'dir' })
expect(report.passed).toBe(false)
expect(report.missingChecks.some((check) => check.label === 'macOS unpacked H5 shell')).toBe(true)
})
test('fails macOS archive checks when latest-mac.yml points at missing assets', async () => {

View File

@ -480,6 +480,7 @@ function inspectMacosArtifacts(rootDir: string, report: PackageSmokeReport, opti
addPresenceCheck(report, rootDir, 'macOS Info.plist', join(contentsDir, 'Info.plist'))
addPresenceCheck(report, rootDir, 'macOS app executable', join(contentsDir, 'MacOS', report.productName))
addPresenceCheck(report, rootDir, 'macOS app.asar', join(resourcesDir, 'app.asar'))
addPresenceCheck(report, rootDir, 'macOS unpacked H5 shell', join(unpackedDir, 'dist', 'index.html'))
addInstalledUpdateMetadataCheck(
report,
rootDir,

View File

@ -256,6 +256,20 @@ describe('remote H5 auth and CORS integration', () => {
await expect(response.text()).resolves.toContain('Mapped H5 Shell')
})
test('finds Electron packaged H5 resources from app.asar.unpacked when the sidecar points at app.asar', async () => {
const asarDistDir = path.join(tmpDir, 'Fake.app', 'Contents', 'Resources', 'app.asar', 'dist')
const unpackedDistDir = path.join(tmpDir, 'Fake.app', 'Contents', 'Resources', 'app.asar.unpacked', 'dist')
process.env.CLAUDE_H5_DIST_DIR = asarDistDir
await fs.mkdir(unpackedDistDir, { recursive: true })
await fs.writeFile(path.join(unpackedDistDir, 'index.html'), 'Electron H5 Shell', 'utf-8')
const response = await fetch(`${baseUrl}/`)
expect(response.status).toBe(200)
await expect(response.text()).resolves.toContain('Electron H5 Shell')
})
test('allows /api/status by default without H5 token or Anthropic key', async () => {
const response = await fetch(`${baseUrl}/api/status`)

View File

@ -53,9 +53,13 @@ export async function handleStaticH5Request(req: Request, url: URL): Promise<Res
async function resolveH5DistDir(): Promise<string | null> {
const candidates = [
process.env.CLAUDE_H5_DIST_DIR,
unpackedAsarDistDir(process.env.CLAUDE_H5_DIST_DIR),
process.env.CLAUDE_APP_ROOT
? path.resolve(process.env.CLAUDE_APP_ROOT, '..', 'Resources', '_up_', 'dist')
: undefined,
process.env.CLAUDE_APP_ROOT
? unpackedAsarDistDir(path.resolve(process.env.CLAUDE_APP_ROOT, 'dist'))
: undefined,
process.env.CLAUDE_APP_ROOT
? path.resolve(process.env.CLAUDE_APP_ROOT, '..', 'Resources', 'dist')
: undefined,
@ -80,6 +84,14 @@ async function resolveH5DistDir(): Promise<string | null> {
return null
}
function unpackedAsarDistDir(value: string | undefined): string | undefined {
if (!value || !value.includes('.asar')) {
return undefined
}
return value.replace(/\.asar(?=$|[/\\])/, '.asar.unpacked')
}
async function resolveStaticFilePath(distDir: string, pathname: string): Promise<string | null> {
const requested = containedPath(distDir, pathname)
if (!requested) {