Open History to see the last commit
1import {
2 REMOTE_CAPABLE_EDITOR_IDS,
3 remoteSchemeForEditor,
4 type SystemSettingsPane,
5} from "@t3tools/contracts";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10
11import * as Electron from "electron";
12
13/**
14 * Deep links to individual System Settings panes. These are app-fixed, not
15 * renderer-supplied, so they skip `parseSafeExternalUrl` — which exists to keep
16 * arbitrary link schemes from reaching the OS handler — and open through their
17 * own path below. The pane rather than the URL crosses the IPC boundary, so a
18 * renderer can only ask for one of these known destinations.
19 *
20 * Full Disk Access uses the post-Ventura `PrivacySecurity.extension` anchor.
21 */
22const SYSTEM_SETTINGS_URLS: Record<SystemSettingsPane, string> = {
23 "full-disk-access":
24 "x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_AllFiles",
25};
26
27// Remote open-in-editor deep links (`vscode://vscode-remote/ssh-remote+…`,
28// `zed://ssh/<host>/<path>`) must reach the OS handler; every other non-web
29// scheme stays blocked.
30const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
31const REMOTE_EDITOR_PROTOCOLS = new Set(
32 REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
33 const scheme = remoteSchemeForEditor(id);
34 return scheme === undefined ? [] : [`${scheme}:`];
35 }),
36);
37
38// Zed's host sits in the first path segment, so it needs its own userinfo ban.
39const ZED_SSH_PATHNAME = /^\/[^/@:]+\/.*$/;
40
41const isRemoteEditorUrl = (url: URL) =>
42 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
43 url.username.length === 0 &&
44 url.password.length === 0 &&
45 (url.protocol === "zed:"
46 ? url.host === "ssh" && ZED_SSH_PATHNAME.test(url.pathname)
47 : url.host === "vscode-remote" &&
48 url.pathname.startsWith("/ssh-remote+") &&
49 url.pathname.length > "/ssh-remote+".length);
50
51export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
52 if (typeof rawUrl !== "string") {
53 return Option.none();
54 }
55
56 try {
57 const url = new URL(rawUrl);
58 return SAFE_WEB_PROTOCOLS.has(url.protocol) || isRemoteEditorUrl(url)
59 ? Option.some(url.href)
60 : Option.none();
61 } catch {
62 return Option.none();
63 }
64}
65
66export class ElectronShell extends Context.Service<
67 ElectronShell,
68 {
69 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
70 /** Opens a known System Settings pane by identifier, not by URL. */
71 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
72 readonly copyText: (text: string) => Effect.Effect<void>;
73 }
74>()("@t3tools/desktop/electron/ElectronShell") {}
75
76/** @public Service construction is part of the canonical Effect module API. */
77export const make = ElectronShell.of({
78 openExternal: (rawUrl) =>
79 Option.match(parseSafeExternalUrl(rawUrl), {
80 onNone: () => Effect.succeed(false),
81 onSome: (externalUrl) =>
82 Effect.promise(() =>
83 Electron.shell.openExternal(externalUrl).then(
84 () => true,
85 () => false,
86 ),
87 ),
88 }),
89 openSystemSettings: (pane) =>
90 Effect.promise(() =>
91 Electron.shell.openExternal(SYSTEM_SETTINGS_URLS[pane]).then(
92 () => true,
93 () => false,
94 ),
95 ),
96 copyText: (text) =>
97 Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined)),
98});
99
100export const layer = Layer.succeed(ElectronShell, make);