import assert from 'node:assert/strict'; import { mkdir, mkdtemp, open, rename, rm, writeFile } from 'node:fs/promises'; import path from 'node:path'; import os from 'node:os'; import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import { executable, extractArchive, nativeComponents, obtainAsset, readJSON, relativePath, runFile, withCacheLock, writeJSON, processAlive } from './runtime-assets.mjs'; export function defaultInstallationHome(environment = 'prod') { const base = process.platform === 'win32' ? process.env.LOCALAPPDATA || os.homedir() : path.join(os.homedir(), '.local/share'); return path.join(base, environment === 'dev' ? 'openmotion-dev' : 'openmotion'); } export const nativeCacheDirectory = home => path.join(home, 'cache/native'); export async function nativeConfiguration(root) { const config = await readJSON(path.join(root, '.npm-install.json')); if (!config) return null; return configuration(config); } function configuration(config) { assert.equal(config.schemaVersion, 1, 'Unsupported npm installation'); const found = Object.entries(config.runtime.targets).find(([, value]) => value.platform === process.platform && value.arch === process.arch); assert(found, `No pinned native runtime for ${process.platform}-${process.arch}`); const [target, info] = found; assert(/^[a-z0-9-]+$/.test(target), 'Invalid native target'); const components = nativeComponents(info); for (const component of components) assert(/^[a-f0-9]{64}$/.test(info[component]?.sha256), 'Require a pinned native SHA256'); assert(path.isAbsolute(config.cacheDirectory), 'Native cache must be absolute'); const cache = path.join(config.cacheDirectory, target), suffix = process.platform === 'win32' ? '.exe' : ''; const media = info.media && relativePath(path.join(cache, 'media-' + info.media.sha256), info.media.directory); const mediaTool = name => media ? path.join(media, 'bin', name + suffix) : relativePath(path.join(cache, name + '-' + info[name].sha256), info[name].executable); const browser = relativePath(path.join(cache, 'browser-' + info.browser.sha256), info.browser.executable); return { config, info, cache, components, paths: { HYPERFRAMES_FFMPEG_PATH: mediaTool('ffmpeg'), HYPERFRAMES_FFPROBE_PATH: mediaTool('ffprobe'), HYPERFRAMES_BROWSER_PATH: browser, } }; } export async function configureNativeEnvironment(root) { const native = await nativeConfiguration(root); if (!native) { // Best-effort: an unreadable or unpinned source lock must keep system-tool fallback instead of blocking startup. await configureCachedSourceEnvironment(root).catch(error => console.error('Source native cache discovery was skipped: ' + error.message)); return null; } process.env.HYPERFRAMES_FFMPEG_PATH ||= process.env.FFMPEG_PATH || native.paths.HYPERFRAMES_FFMPEG_PATH; process.env.HYPERFRAMES_FFPROBE_PATH ||= process.env.FFPROBE_PATH || native.paths.HYPERFRAMES_FFPROBE_PATH; process.env.HYPERFRAMES_BROWSER_PATH ||= process.env.MOTION_BROWSER_PATH || native.paths.HYPERFRAMES_BROWSER_PATH; return native; } async function configureCachedSourceEnvironment(root) { const aliases = { HYPERFRAMES_FFMPEG_PATH: 'FFMPEG_PATH', HYPERFRAMES_FFPROBE_PATH: 'FFPROBE_PATH', HYPERFRAMES_BROWSER_PATH: 'MOTION_BROWSER_PATH' }; for (const [key, alias] of Object.entries(aliases)) { if (!process.env[key] && process.env[alias]) process.env[key] = process.env[alias]; } if (Object.keys(aliases).every(key => process.env[key])) return; const runtime = await readJSON(path.join(root, 'scripts/runtime-lock.json')); if (!runtime || !Object.values(runtime.targets).some(info => info.platform === process.platform && info.arch === process.arch)) return; // Reuse completed, pinned assets only. Source checkouts do not own these installations. for (const environment of ['prod', 'dev']) { const home = defaultInstallationHome(environment); for (const cacheDirectory of [nativeCacheDirectory(home), path.join(home, 'cache')]) { const cached = configuration({ schemaVersion: 1, runtime, cacheDirectory }); for (const component of cached.components) { const keys = componentKeys[component].filter(key => !process.env[key]); if (!keys.length || !await ready(cached, component, false).catch(() => false)) continue; for (const key of keys) process.env[key] = cached.paths[key]; } } } } const componentKeys = { browser: ['HYPERFRAMES_BROWSER_PATH'], media: ['HYPERFRAMES_FFMPEG_PATH', 'HYPERFRAMES_FFPROBE_PATH'], ffmpeg: ['HYPERFRAMES_FFMPEG_PATH'], ffprobe: ['HYPERFRAMES_FFPROBE_PATH'] }; const preparing = new Map(); const launching = new Map(); const location = (native, component) => path.join(native.cache, component + '-' + native.info[component].sha256); const overridden = (native, key) => process.env[key] && process.env[key] !== native.paths[key]; async function ready(native, component, useOverrides = true) { const keys = componentKeys[component]; const override = key => useOverrides && overridden(native, key); if (!keys.every(override) && (await readJSON(path.join(location(native, component), 'ready.json')))?.sha256 !== native.info[component].sha256) return false; try { for (const key of keys) { await executable(override(key) ? process.env[key] : native.paths[key]); if (override(key)) await runFile(process.env[key], [key.includes('BROWSER') ? '--version' : '-version'], { timeout: 5000 }); } return true; } catch { return false; } } export async function nativeRuntimeStatus(root) { const native = await configureNativeEnvironment(root); if (!native) return { managed: false, components: [], capabilities: { capture: true, media: true, export: true } }; const components = await Promise.all(native.components.map(async id => { try { if (await ready(native, id)) return { id, stage: 'ready' }; const destination = location(native, id), state = await readJSON(destination + '.status.json'); const owner = await readJSON(destination + '.lock'); const pid = typeof owner === 'number' ? owner : owner?.pid; if (processAlive(pid)) return { id, ...(state?.pid === pid && !['ready', 'failed'].includes(state.stage) ? state : { stage: 'waiting' }) }; if (state?.stage === 'failed') return { id, stage: 'failed', error: state.error, errorCode: state.errorCode }; return { id, stage: 'idle' }; } catch (error) { return { id, stage: 'failed', errorCode: ['EACCES', 'EPERM'].includes(error.code) ? 'permission' : 'checking', error: String(error.message).slice(0, 1000) }; } })); const available = ids => components.filter(component => ids.includes(component.id)).every(component => component.stage === 'ready'); return { managed: true, components, capabilities: { capture: available(native.components), media: available(['media', 'ffmpeg', 'ffprobe']), export: available(native.components) } }; } async function prepareComponent(native, component) { const destination = location(native, component); if (preparing.has(destination)) return preparing.get(destination); if (await ready(native, component)) return; if (preparing.has(destination)) return preparing.get(destination); const task = withCacheLock(destination + '.lock', async () => { if (await ready(native, component)) return; let state = { pid: process.pid, stage: 'waiting' }, lastProgress = 0; const update = async change => { if (change.stage === 'downloading' && state.stage === 'downloading' && change.receivedBytes !== undefined && state.receivedBytes > 0 && Date.now() - lastProgress < 500) return; lastProgress = Date.now(); state = { ...state, ...change, updatedAt: Date.now() }; await writeJSON(destination + '.status.json', state); }; try { await update({ stage: 'waiting' }); const { config, info, paths, cache } = native, keys = componentKeys[component]; for (const key of keys.filter(key => overridden(native, key))) { await executable(process.env[key]); await runFile(process.env[key], [key.includes('BROWSER') ? '--version' : '-version']); } if (await ready(native, component)) return; const asset = info[component]; if (process.platform === 'win32') assert(Number(os.release().split('.')[2]) >= 22000, 'Pinned native runtime requires Windows 11 or later'); console.error(`OpenMotion: preparing pinned ${component} in the background.`); const archive = path.join(config.cacheDirectory, 'downloads', asset.sha256 + (new URL(asset.url).pathname.endsWith('.zip') ? '.zip' : '.tar.gz')); await update({ stage: 'downloading' }); await obtainAsset(asset, archive, config.allowLocalAssets === true, update); await update({ stage: 'extracting' }); await mkdir(cache, { recursive: true }); const temporary = await mkdtemp(path.join(cache, component + '-')); try { await extractArchive(archive, temporary); await update({ stage: 'checking' }); for (const key of keys) { const filename = path.join(temporary, path.relative(destination, paths[key])); if (process.platform === 'win32') assert(filename.length < 260, 'Native tool path is too long; choose a shorter --home directory'); await executable(filename); await runFile(filename, [component === 'browser' ? '--version' : '-version']); } await writeFile(path.join(temporary, 'ready.json'), JSON.stringify({ sha256: asset.sha256 })); await rm(destination, { recursive: true, force: true }); await rename(temporary, destination); } finally { await rm(temporary, { recursive: true, force: true }); } await update({ stage: 'ready' }); } catch (error) { const errorCode = /SHA256/.test(error.message) ? 'integrity' : error.code === 'ENOSPC' ? 'disk_space' : ['EACCES', 'EPERM'].includes(error.code) ? 'permission' : /requires? (?:macOS|Windows)/i.test(error.message) ? 'platform' : error.name === 'TimeoutError' ? 'timeout' : state.stage === 'downloading' ? 'download' : 'checking'; await update({ stage: 'failed', errorCode, error: String(error.message).slice(0, 1000) }); throw error; } }); preparing.set(destination, task); try { return await task; } finally { preparing.delete(destination); } } export async function ensureNativeRuntime(root, components) { const native = await configureNativeEnvironment(root); if (!native) return; const selected = components ?? native.components; assert(selected.every(component => native.components.includes(component)), 'Unknown native component'); const prepared = await Promise.allSettled(selected.map(component => prepareComponent(native, component))); const failed = prepared.find(result => result.status === 'rejected'); if (failed) throw failed.reason; } export async function startNativePreparation(root, retry = false) { if (launching.has(root)) return launching.get(root); const task = (async () => { const status = await nativeRuntimeStatus(root); const components = status.components.filter(component => component.stage === 'idle' || retry && component.stage === 'failed').map(component => component.id); if (!components.length) return status; const native = await nativeConfiguration(root); await mkdir(native.cache, { recursive: true }); const log = await open(path.join(native.cache, 'preparation.log'), 'a', 0o600); try { const child = spawn(process.execPath, [path.join(root, 'scripts/npm-runtime.mjs'), root, ...components], { detached: true, stdio: ['ignore', log.fd, log.fd], env: process.env, windowsHide: true, }); await new Promise((resolve, reject) => { child.once('spawn', resolve); child.once('error', reject); }); child.unref(); } finally { await log.close(); } return status; })(); launching.set(root, task); try { return await task; } finally { launching.delete(root); } } export async function requireNativeRuntime(root, capability) { assert(['capture', 'media', 'export'].includes(capability), 'Unknown native capability'); const status = await nativeRuntimeStatus(root); if (!status.managed || status.capabilities[capability]) return; await startNativePreparation(root); throw Object.assign(new Error('NATIVE_RUNTIME_NOT_READY: Video components are preparing or need retry. Continue editing; check Studio preparation status or run openmotion prepare for this installation, then retry this operation.'), { code: 'NATIVE_RUNTIME_NOT_READY', details: status }); } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { ensureNativeRuntime(process.argv[2], process.argv.length > 3 ? process.argv.slice(3) : undefined).catch(error => { console.error(error.message); process.exitCode = 1; }); }