Files
orca/src/preload/api/platform-bridge.ts
T
Neil 28cb372559 perf(platform): resolve the immutable platform payload once (#18135)
`window.api.platform.get()` runs ~19x/sec while the app is idle. Every call
recomputed a payload whose fields are all fixed for the process lifetime
(`process.platform`, `process.getSystemVersion()`, `process.arch`, the shell
env vars, and the env-derived Linux display server), allocated a fresh object,
and crossed the context bridge.

Memoize the payload lazily at preload module scope and freeze it, and cache the
resolved platform in `getRendererAppPlatform()` so the 32 renderer call sites
stop crossing the bridge on every render. The user-agent fallback stays uncached
because the web client installs its platform API after boot.
2026-09-02 13:05:30 -07:00

29 lines
1.2 KiB
TypeScript

import { getLinuxDisplayServer } from '../preload-runtime-support'
import type { PreloadApi } from '../api-types'
type PlatformInfo = ReturnType<PreloadApi['platform']['get']>
// Why: the renderer reads this on its render cadence, and every field below is fixed
// for the process lifetime, so resolve once and hand back the same frozen payload.
let platformInfo: PlatformInfo | undefined
function resolvePlatformInfo(): PlatformInfo {
return Object.freeze({
platform: process.platform,
// Why: sandboxed preload cannot require node:os; Electron exposes the OS
// version on process.getSystemVersion when available.
osRelease:
(process as NodeJS.Process & { getSystemVersion?: () => string }).getSystemVersion?.() ?? '',
arch: process.arch,
// Why: these identify the default shell without probing user config files.
// process.env is available in the sandboxed preload; node:os is not.
shell: process.env.SHELL?.trim() || process.env.ComSpec?.trim() || '',
displayServer: getLinuxDisplayServer()
})
}
export const platformApi = {
// Why: resolved lazily so preload startup keeps paying nothing for it.
get: () => (platformInfo ??= resolvePlatformInfo())
} satisfies PreloadApi['platform']