├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── test.js ├── readme.md ├── index.js ├── package.json └── license /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import defaultBrowserId from './index.js'; 3 | 4 | test('main', async t => { 5 | const result = await defaultBrowserId(); 6 | console.log('Default browser:', result); 7 | t.regex(result, /^com\./); 8 | }); 9 | 10 | /* 11 | Example content: 12 | 13 | ``` 14 | ( 15 | { 16 | LSHandlerPreferredVersions = { 17 | LSHandlerRoleAll = "-"; 18 | }; 19 | LSHandlerRoleAll = "com.apple.safari"; 20 | LSHandlerURLScheme = http; 21 | }, 22 | … 23 | ) 24 | ``` 25 | */ 26 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: macos-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # default-browser-id 2 | 3 | > Get the [bundle identifier](https://developer.apple.com/library/Mac/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/plist/info/CFBundleIdentifier) of the default browser *(macOS)*\ 4 | > Example: `com.apple.Safari` 5 | 6 | ## Install 7 | 8 | ```sh 9 | npm install default-browser-id 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | import defaultBrowserId from 'default-browser-id'; 16 | 17 | console.log(await defaultBrowserId()); 18 | //=> 'com.apple.Safari' 19 | ``` 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {promisify} from 'node:util'; 2 | import process from 'node:process'; 3 | import {execFile} from 'node:child_process'; 4 | 5 | const execFileAsync = promisify(execFile); 6 | 7 | export default async function defaultBrowserId() { 8 | if (process.platform !== 'darwin') { 9 | throw new Error('macOS only'); 10 | } 11 | 12 | const {stdout} = await execFileAsync('defaults', ['read', 'com.apple.LaunchServices/com.apple.launchservices.secure', 'LSHandlers']); 13 | 14 | // `(?!-)` is to prevent matching `LSHandlerRoleAll = "-";`. 15 | const match = /LSHandlerRoleAll = "(?!-)(?[^"]+?)";\s+?LSHandlerURLScheme = (?:http|https);/.exec(stdout); 16 | 17 | const browserId = match?.groups.id ?? 'com.apple.Safari'; 18 | 19 | // Correct the case for Safari's bundle identifier 20 | if (browserId === 'com.apple.safari') { 21 | return 'com.apple.Safari'; 22 | } 23 | 24 | return browserId; 25 | } 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "default-browser-id", 3 | "version": "5.0.1", 4 | "description": "Get the bundle identifier of the default browser (macOS). Example: com.apple.Safari", 5 | "license": "MIT", 6 | "repository": "sindresorhus/default-browser-id", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "sideEffects": false, 16 | "engines": { 17 | "node": ">=18" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava" 21 | }, 22 | "files": [ 23 | "index.js" 24 | ], 25 | "keywords": [ 26 | "macos", 27 | "browser", 28 | "default", 29 | "plist", 30 | "web", 31 | "bundle", 32 | "bundleid", 33 | "id", 34 | "identifier", 35 | "uti" 36 | ], 37 | "devDependencies": { 38 | "ava": "^6.0.1", 39 | "xo": "^0.56.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | --------------------------------------------------------------------------------