windows.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {promisify} from 'node:util';
  2. import {execFile} from 'node:child_process';
  3. const execFileAsync = promisify(execFile);
  4. // TODO: Fix the casing of bundle identifiers in the next major version.
  5. // Windows doesn't have browser IDs in the same way macOS/Linux does so we give fake
  6. // ones that look real and match the macOS/Linux versions for cross-platform apps.
  7. const windowsBrowserProgIds = {
  8. MSEdgeHTM: {name: 'Edge', id: 'com.microsoft.edge'}, // The missing `L` is correct.
  9. MSEdgeBHTML: {name: 'Edge Beta', id: 'com.microsoft.edge.beta'},
  10. MSEdgeDHTML: {name: 'Edge Dev', id: 'com.microsoft.edge.dev'},
  11. AppXq0fevzme2pys62n3e0fbqa7peapykr8v: {name: 'Edge', id: 'com.microsoft.edge.old'},
  12. ChromeHTML: {name: 'Chrome', id: 'com.google.chrome'},
  13. ChromeBHTML: {name: 'Chrome Beta', id: 'com.google.chrome.beta'},
  14. ChromeDHTML: {name: 'Chrome Dev', id: 'com.google.chrome.dev'},
  15. ChromiumHTM: {name: 'Chromium', id: 'org.chromium.Chromium'},
  16. BraveHTML: {name: 'Brave', id: 'com.brave.Browser'},
  17. BraveBHTML: {name: 'Brave Beta', id: 'com.brave.Browser.beta'},
  18. BraveDHTML: {name: 'Brave Dev', id: 'com.brave.Browser.dev'},
  19. BraveSSHTM: {name: 'Brave Nightly', id: 'com.brave.Browser.nightly'},
  20. FirefoxURL: {name: 'Firefox', id: 'org.mozilla.firefox'},
  21. OperaStable: {name: 'Opera', id: 'com.operasoftware.Opera'},
  22. VivaldiHTM: {name: 'Vivaldi', id: 'com.vivaldi.Vivaldi'},
  23. 'IE.HTTP': {name: 'Internet Explorer', id: 'com.microsoft.ie'},
  24. };
  25. export const _windowsBrowserProgIdMap = new Map(Object.entries(windowsBrowserProgIds));
  26. export class UnknownBrowserError extends Error {}
  27. export default async function defaultBrowser(_execFileAsync = execFileAsync) {
  28. const {stdout} = await _execFileAsync('reg', [
  29. 'QUERY',
  30. ' HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice',
  31. '/v',
  32. 'ProgId',
  33. ]);
  34. const match = /ProgId\s*REG_SZ\s*(?<id>\S+)/.exec(stdout);
  35. if (!match) {
  36. throw new UnknownBrowserError(`Cannot find Windows browser in stdout: ${JSON.stringify(stdout)}`);
  37. }
  38. const {id} = match.groups;
  39. const browser = windowsBrowserProgIds[id];
  40. if (!browser) {
  41. throw new UnknownBrowserError(`Unknown browser ID: ${id}`);
  42. }
  43. return browser;
  44. }