-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
isProtocolHandlerRegistered
cross-platform
- Loading branch information
Showing
4 changed files
with
50 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,13 @@ | ||
import { homedir } from "node:os"; | ||
import { join } from "node:path"; | ||
import { match, P } from "ts-pattern"; | ||
import { z } from "zod"; | ||
import { exec } from "../fs/exec.js"; | ||
|
||
const launchServicesHandlersSchema = z.object({ | ||
LSHandlerURLScheme: z.string().optional(), | ||
}).passthrough().array(); | ||
|
||
export const launchServicesSecureSchema = z.union([ | ||
launchServicesHandlersSchema, | ||
z.object({ | ||
LSHandlers: launchServicesHandlersSchema, | ||
}).passthrough(), | ||
]); | ||
|
||
const parseLaunchServices = ( | ||
launchServices: z.infer<typeof launchServicesSecureSchema>, | ||
) => | ||
match(launchServices) | ||
.returnType<z.infer<typeof launchServicesHandlersSchema>>() | ||
.with(P.array(), (launchServices) => launchServices) | ||
.otherwise((obj) => obj.LSHandlers); | ||
import ProtocolRegistry from "protocol-registry"; | ||
import { match } from "ts-pattern"; | ||
import { isProtocolHandlerRegistered as win } from "./windows/isProtocolHandlerRegistered.js"; | ||
|
||
/** | ||
* Determines whether a handler for a given protocol is registered. | ||
* | ||
* Checks the launch services registered in | ||
* `$HOME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist` | ||
* first, and if not found there falls back checking with `open`, which opens | ||
* the handler for the given protocol in the background if one is registered. | ||
* | ||
* @param protocol The protocol to check for. | ||
*/ | ||
export const isProtocolHandlerRegistered = async (protocol: string) => { | ||
let plutilOutput: string | undefined; | ||
try { | ||
const { stdout } = await exec( | ||
`plutil -convert json -o - "${ | ||
join( | ||
homedir(), | ||
"Library", | ||
"Preferences", | ||
"com.apple.LaunchServices", | ||
"com.apple.launchservices.secure.plist", | ||
) | ||
}"`, | ||
); | ||
plutilOutput = stdout.trim(); | ||
} catch {} | ||
|
||
if (plutilOutput) { | ||
const launchServices = launchServicesHandlersSchema.safeParse( | ||
JSON.parse(plutilOutput), | ||
); | ||
if ( | ||
launchServices.success && | ||
parseLaunchServices(launchServices.data).map((entry) => | ||
entry.LSHandlerURLScheme | ||
).filter( | ||
Boolean, | ||
).includes(protocol) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
try { | ||
await exec(`open --background ${protocol}://`); | ||
return true; | ||
} catch { | ||
try { | ||
await exec(`open ${protocol}://`); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
}; | ||
export const isProtocolHandlerRegistered = match(process.platform) | ||
.returnType<(protocol: string) => Promise<boolean>>() | ||
.with("win32", () => (protocol) => Promise.resolve(win(protocol))) | ||
.otherwise(() => ProtocolRegistry.checkifExists); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { enumerateValues, HKEY, RegistryValueType } from "registry-js"; | ||
|
||
export const isProtocolHandlerRegistered = (protocol: string) => | ||
enumerateValues(HKEY.HKEY_CLASSES_ROOT, protocol) | ||
.concat( | ||
enumerateValues(HKEY.HKEY_CURRENT_USER, `Software\\Classes\\${protocol}`), | ||
) | ||
.some((entry) => | ||
entry.name === "URL Protocol" && entry.type === RegistryValueType.REG_SZ | ||
); |