{"size": 1042, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "sindresorhus/default-browser-id", "max_stars_repo_stars_event_min_datetime": "2015-05-06T17:16:07.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-15T02:29:50.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "sindresorhus/default-browser-id", "max_issues_repo_issues_event_min_datetime": "2016-04-22T12:45:01.000Z", "max_issues_repo_issues_event_max_datetime": "2016-04-22T12:47:05.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "sindresorhus/default-browser-id", "max_forks_repo_forks_event_min_datetime": "2021-01-01T05:51:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-13T20:56:48.000Z"}
{"max_stars_count": 7, "max_issues_count": 1, "max_forks_count": 4, "avg_line_length": 24.2325581395, "max_line_length": 193, "alphanum_fraction": 0.6928982726}
import os from 'os';
import {promises as fs} from 'fs';
import bplist from 'bplist-parser';
import untildify from 'untildify';

const macOsVersion = Number(os.release().split('.')[0]);
const filePath = untildify(macOsVersion >= 14 ? '~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist' : '~/Library/Preferences/com.apple.LaunchServices.plist');

export default async function defaultBrowserId() {
	if (process.platform !== 'darwin') {
		throw new Error('macOS only');
	}

	let bundleId = 'com.apple.Safari';

	let buffer;
	try {
		buffer = await fs.readFile(filePath);
	} catch (error) {
		if (error.code === 'ENOENT') {
			return bundleId;
		}

		throw error;
	}

	const data = bplist.parseBuffer(buffer);
	const handlers = data && data[0].LSHandlers;

	if (!handlers || handlers.length === 0) {
		return bundleId;
	}

	for (const handler of handlers) {
		if (handler.LSHandlerURLScheme === 'http' && handler.LSHandlerRoleAll) {
			bundleId = handler.LSHandlerRoleAll;
			break;
		}
	}

	return bundleId;
}
