import assert from "node:assert/strict"; import fs from "node:fs/promises"; import path from "node:path"; import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { HOST_NAME } from "../extension/shared.js"; export async function installedHelperRoot() { if (process.env.YTDL_INSTALLED_ROOT) return process.env.YTDL_INSTALLED_ROOT; // A packaged parent process can redirect AppData. Use the registration that // Edge actually opens, rather than a potentially stale LOCALAPPDATA directory. assert.match(HOST_NAME, /^[a-z.]+$/); const command = `$ErrorActionPreference='Stop'; [Console]::OutputEncoding=[Text.UTF8Encoding]::new($false); (Get-Item -LiteralPath 'Registry::HKEY_CURRENT_USER\\Software\\Microsoft\\Edge\\NativeMessagingHosts\\${HOST_NAME}').GetValue('')`; const registration = (await promisify(execFile)("powershell.exe", ["-NoProfile", "-Command", command])).stdout.trim(); const native = JSON.parse((await fs.readFile(registration, "utf8")).replace(/^\uFEFF/, "")); assert.equal(native.name, HOST_NAME); const release = path.dirname(path.dirname(native.path)); const bundle = JSON.parse(await fs.readFile(path.join(release, "bundle.json"), "utf8")); assert.equal(bundle.product, "YouTubeDL.Helper"); assert.equal(bundle.mode, "autonomous"); assert.equal(path.basename(path.dirname(release)), "releases"); return path.dirname(path.dirname(release)); }