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