index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os from 'os';
  2. import {promises as fs} from 'fs';
  3. import bplist from 'bplist-parser';
  4. import untildify from 'untildify';
  5. const macOsVersion = Number(os.release().split('.')[0]);
  6. const filePath = untildify(macOsVersion >= 14 ? '~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist' : '~/Library/Preferences/com.apple.LaunchServices.plist');
  7. export default async function defaultBrowserId() {
  8. if (process.platform !== 'darwin') {
  9. throw new Error('macOS only');
  10. }
  11. let bundleId = 'com.apple.Safari';
  12. let buffer;
  13. try {
  14. buffer = await fs.readFile(filePath);
  15. } catch (error) {
  16. if (error.code === 'ENOENT') {
  17. return bundleId;
  18. }
  19. throw error;
  20. }
  21. const data = bplist.parseBuffer(buffer);
  22. const handlers = data && data[0].LSHandlers;
  23. if (!handlers || handlers.length === 0) {
  24. return bundleId;
  25. }
  26. for (const handler of handlers) {
  27. if (handler.LSHandlerURLScheme === 'http' && handler.LSHandlerRoleAll) {
  28. bundleId = handler.LSHandlerRoleAll;
  29. break;
  30. }
  31. }
  32. return bundleId;
  33. }