Open History to see the last commit
1import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
2import {
3 isLocalEditorFileUrl,
4 REMOTE_CAPABLE_EDITOR_IDS,
5 remoteSchemeForEditor,
6 type SystemSettingsPane,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12
13import * as Electron from "electron";
14
15// Remote editor deep links and local editor file links must reach the OS handler.
16const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
17const REMOTE_EDITOR_PROTOCOLS = new Set(
18 REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
19 const scheme = remoteSchemeForEditor(id);
20 return scheme === undefined ? [] : [`${scheme}:`];
21 }),
22);
23
24// Zed embeds [user@]host in the path; reject passwords and ports there.
25const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
26
27const isRemoteEditorUrl = (url: URL) =>
28 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
29 url.username.length === 0 &&
30 url.password.length === 0 &&
31 (url.protocol === "zed:"
32 ? url.host === "ssh" && ZED_SSH_PATHNAME.test(url.pathname)
33 : url.protocol === "jetbrains:"
34 ? url.host === "gateway" &&
35 url.pathname === "/ssh/environment" &&
36 (url.searchParams.get("h") ?? "").length > 0
37 : url.host === "vscode-remote" &&
38 url.pathname.startsWith("/ssh-remote+") &&
39 url.pathname.length > "/ssh-remote+".length);
40
41export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
42 if (typeof rawUrl !== "string") {
43 return Option.none();
44 }
45
46 try {
47 const url = new URL(rawUrl);
48 return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
49 isRemoteEditorUrl(url) ||
50 isLocalEditorFileUrl(url)
51 ? Option.some(url.href)
52 : Option.none();
53 } catch {
54 return Option.none();
55 }
56}
57
58export class ElectronShell extends Context.Service<
59 ElectronShell,
60 {
61 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
62 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
63 readonly copyText: (text: string) => Effect.Effect<void>;
64 }
65>()("@t3tools/desktop/electron/ElectronShell") {}
66
67/** @public */
68export const make = ElectronShell.of({
69 openExternal: Effect.fn("ElectronShell.openExternal")(function (rawUrl) {
70 return Option.match(parseSafeExternalUrl(rawUrl), {
71 onNone: () => Effect.succeed(false),
72 onSome: (externalUrl) =>
73 Effect.promise(() =>
74 Electron.shell.openExternal(externalUrl).then(
75 () => true,
76 () => false,
77 ),
78 ),
79 });
80 }),
81 openSystemSettings: Effect.fn("ElectronShell.openSystemSettings")(function (pane) {
82 return Effect.promise(() =>
83 Electron.shell.openExternal(MAC_PERMISSION_SETTINGS_URLS[pane]).then(
84 () => true,
85 () => false,
86 ),
87 );
88 }),
89 copyText: Effect.fn("ElectronShell.copyText")(function (text) {
90 return Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined));
91 }),
92});
93
94export const layer = Layer.succeed(ElectronShell, make);