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// must reach the OS handler; every other non-web scheme stays blocked.
29const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
30const REMOTE_EDITOR_PROTOCOLS = new Set(
31 REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
32 const scheme = remoteSchemeForEditor(id);
33 return scheme === undefined ? [] : [`${scheme}:`];
34 }),
35);
36
37const isRemoteEditorUrl = (url: URL) =>
38 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
39 url.username.length === 0 &&
40 url.password.length === 0 &&
41 url.host === "vscode-remote" &&
42 url.pathname.startsWith("/ssh-remote+") &&
43 url.pathname.length > "/ssh-remote+".length;
44
45export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
46 if (typeof rawUrl !== "string") {
47 return Option.none();
48 }
49
50 try {
51 const url = new URL(rawUrl);
52 return SAFE_WEB_PROTOCOLS.has(url.protocol) || isRemoteEditorUrl(url)
53 ? Option.some(url.href)
54 : Option.none();
55 } catch {
56 return Option.none();
57 }
58}
59
60export class ElectronShell extends Context.Service<
61 ElectronShell,
62 {
63 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
64 /** Opens a known System Settings pane by identifier, not by URL. */
65 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
66 readonly copyText: (text: string) => Effect.Effect<void>;
67 }
68>()("@t3tools/desktop/electron/ElectronShell") {}
69
70/** @public Service construction is part of the canonical Effect module API. */
71export const make = ElectronShell.of({
72 openExternal: (rawUrl) =>
73 Option.match(parseSafeExternalUrl(rawUrl), {
74 onNone: () => Effect.succeed(false),
75 onSome: (externalUrl) =>
76 Effect.promise(() =>
77 Electron.shell.openExternal(externalUrl).then(
78 () => true,
79 () => false,
80 ),
81 ),
82 }),
83 openSystemSettings: (pane) =>
84 Effect.promise(() =>
85 Electron.shell.openExternal(SYSTEM_SETTINGS_URLS[pane]).then(
86 () => true,
87 () => false,
88 ),
89 ),
90 copyText: (text) =>
91 Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined)),
92});
93
94export const layer = Layer.succeed(ElectronShell, make);