|
| 1 | +import * as Context from "effect/Context"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Layer from "effect/Layer"; |
| 4 | +import * as Option from "effect/Option"; |
| 5 | +import * as Ref from "effect/Ref"; |
| 6 | +import * as Scope from "effect/Scope"; |
| 7 | + |
| 8 | +import type * as Electron from "electron"; |
| 9 | + |
| 10 | +import * as ElectronApp from "../electron/ElectronApp.ts"; |
| 11 | +import * as ElectronProtocol from "../electron/ElectronProtocol.ts"; |
| 12 | +import * as DesktopWindow from "../window/DesktopWindow.ts"; |
| 13 | +import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 14 | +import { makeComponentLogger } from "./DesktopObservability.ts"; |
| 15 | + |
| 16 | +/** Host segment of a pairing deep link: `t3code://pair?host=...&label=...#token=...`. */ |
| 17 | +export const PAIRING_LINK_HOST = "pair"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Pick the pairing deep link out of a list of candidate strings (an argv, or a |
| 21 | + * single URL from macOS `open-url`). Only links on this build's own scheme with |
| 22 | + * the `pair` host count; the renderer parses host/label/token from it. |
| 23 | + */ |
| 24 | +export function extractPairingLink( |
| 25 | + candidates: ReadonlyArray<string>, |
| 26 | + scheme: string, |
| 27 | +): string | null { |
| 28 | + const prefix = `${scheme}:`; |
| 29 | + for (const candidate of candidates) { |
| 30 | + const trimmed = candidate.trim(); |
| 31 | + if (!trimmed.toLowerCase().startsWith(prefix)) continue; |
| 32 | + let url: URL; |
| 33 | + try { |
| 34 | + url = new URL(trimmed); |
| 35 | + } catch { |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (url.protocol.toLowerCase() !== prefix || url.host !== PAIRING_LINK_HOST) continue; |
| 39 | + return url.toString(); |
| 40 | + } |
| 41 | + return null; |
| 42 | +} |
| 43 | + |
| 44 | +export class DesktopPairingLink extends Context.Service< |
| 45 | + DesktopPairingLink, |
| 46 | + { |
| 47 | + /** |
| 48 | + * Start listening for pairing deep links. Registers before `ready` so a |
| 49 | + * macOS `open-url` delivered at cold start is not missed. |
| 50 | + */ |
| 51 | + readonly register: Effect.Effect<void, never, Scope.Scope>; |
| 52 | + /** |
| 53 | + * The renderer signals when its coordinator is mounted; a link that |
| 54 | + * arrived earlier (cold start) is delivered then. |
| 55 | + */ |
| 56 | + readonly setRendererReady: (ready: boolean) => Effect.Effect<void>; |
| 57 | + } |
| 58 | +>()("@t3tools/desktop/app/DesktopPairingLink") {} |
| 59 | + |
| 60 | +const { logInfo, logWarning } = makeComponentLogger("desktop-pairing-link"); |
| 61 | + |
| 62 | +/** @public Service construction is part of the canonical Effect module API. */ |
| 63 | +export const make = Effect.gen(function* () { |
| 64 | + const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 65 | + const electronApp = yield* ElectronApp.ElectronApp; |
| 66 | + const desktopWindow = yield* DesktopWindow.DesktopWindow; |
| 67 | + const runPromise = Effect.runPromiseWith(yield* Effect.context<never>()); |
| 68 | + const scheme = ElectronProtocol.getDesktopScheme(environment.isDevelopment); |
| 69 | + |
| 70 | + const pendingRef = yield* Ref.make(Option.none<string>()); |
| 71 | + const rendererReadyRef = yield* Ref.make(false); |
| 72 | + |
| 73 | + const deliver = Effect.fn("desktop.pairingLink.deliver")(function* (link: string) { |
| 74 | + yield* logInfo("delivering pairing link to renderer"); |
| 75 | + yield* desktopWindow.dispatchPairingLink(link); |
| 76 | + }); |
| 77 | + |
| 78 | + // Reveal the window right away so the click feels acknowledged, but hold |
| 79 | + // the link until the renderer says it can act on it. |
| 80 | + const receive = Effect.fn("desktop.pairingLink.receive")(function* (link: string) { |
| 81 | + yield* Ref.set(pendingRef, Option.some(link)); |
| 82 | + if (yield* Ref.get(rendererReadyRef)) { |
| 83 | + yield* Ref.set(pendingRef, Option.none()); |
| 84 | + yield* deliver(link); |
| 85 | + return; |
| 86 | + } |
| 87 | + yield* logInfo("holding pairing link until renderer is ready"); |
| 88 | + yield* desktopWindow.activate; |
| 89 | + }); |
| 90 | + |
| 91 | + const receiveFromCandidates = (candidates: ReadonlyArray<string>) => { |
| 92 | + const link = extractPairingLink(candidates, scheme); |
| 93 | + if (link === null) return; |
| 94 | + void runPromise( |
| 95 | + receive(link).pipe( |
| 96 | + Effect.catchCause((cause) => logWarning("failed to handle pairing link", { cause })), |
| 97 | + ), |
| 98 | + ); |
| 99 | + }; |
| 100 | + |
| 101 | + const register = Effect.gen(function* () { |
| 102 | + // macOS hands URLs to the running (or launching) app via open-url. The |
| 103 | + // Clerk bridge also listens here but only claims its own renderer origin. |
| 104 | + yield* electronApp.on<[Electron.Event, string]>("open-url", (_event, url) => { |
| 105 | + receiveFromCandidates([url]); |
| 106 | + }); |
| 107 | + // Windows and Linux launch a second process with the URL in argv; the |
| 108 | + // primary instance receives that argv here. |
| 109 | + yield* electronApp.on<[Electron.Event, ReadonlyArray<string>, string]>( |
| 110 | + "second-instance", |
| 111 | + (_event, argv) => { |
| 112 | + receiveFromCandidates(argv); |
| 113 | + }, |
| 114 | + ); |
| 115 | + // Cold start on Windows/Linux: the URL is in this process's own argv. |
| 116 | + receiveFromCandidates(process.argv); |
| 117 | + }).pipe(Effect.withSpan("desktop.pairingLink.register")); |
| 118 | + |
| 119 | + const setRendererReady = Effect.fn("desktop.pairingLink.setRendererReady")(function* ( |
| 120 | + ready: boolean, |
| 121 | + ) { |
| 122 | + yield* Ref.set(rendererReadyRef, ready); |
| 123 | + if (!ready) return; |
| 124 | + const pending = yield* Ref.getAndSet(pendingRef, Option.none()); |
| 125 | + if (Option.isSome(pending)) { |
| 126 | + yield* deliver(pending.value).pipe( |
| 127 | + Effect.catchCause((cause) => logWarning("failed to deliver pairing link", { cause })), |
| 128 | + ); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + return DesktopPairingLink.of({ register, setRendererReady }); |
| 133 | +}); |
| 134 | + |
| 135 | +export const layer = Layer.effect(DesktopPairingLink, make); |
0 commit comments