Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CursorKeychain.ts

106 lines3.5 KB

1/**
2 * macOS shows the access prompt on the server's own screen, which a remote client cannot answer, so
3 * a read gives up after a timeout. The Keychain read stays in flight: the next read joins it
4 * instead of stacking a second prompt, and picks up the token once someone allows access.
5 */
6import * as NodeModule from "node:module";
7​
8import * as Clock from "effect/Clock";
9import * as Context from "effect/Context";
10import * as Deferred from "effect/Deferred";
11import * as Duration from "effect/Duration";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as Schema from "effect/Schema";
16​
17const CACHE_MS = 5 * 60_000;
18const DEFAULT_TIMEOUT = Duration.seconds(30);
19​
20const requireForKeyring = NodeModule.createRequire(import.meta.url);
21​
22export class CursorKeychainTimeoutError extends Schema.TaggedError<CursorKeychainTimeoutError>()(
23 "CursorKeychainTimeoutError",
24 {},
25) {
26 override get message(): string {
27 return "Timed out waiting for Keychain access.";
28 }
30​
31export const isCursorKeychainTimeoutError = Schema.is(CursorKeychainTimeoutError);
32​
33export class CursorKeychainReadError extends Schema.TaggedError<CursorKeychainReadError>()(
34 "CursorKeychainReadError",
35 { cause: Schema.Defect() },
36) {
37 override get message(): string {
38 return "Cursor Keychain credentials could not be read.";
39 }
41​
42export class CursorKeychain extends Context.Service<
43 CursorKeychain,
44 {
45 readonly accessToken: Effect.Effect<
46 string | null,
47 CursorKeychainTimeoutError | CursorKeychainReadError
48 >;
49 }
50>()("@t3tools/provider-cursor/server/CursorKeychain") {}
51​
52const readKeychainEntry = Effect.tryPromise({
53 try: async () => {
54 const { AsyncEntry } = requireForKeyring(
55 "@napi-rs/keyring",
56 ) as typeof import("@napi-rs/keyring");
57 return (await new AsyncEntry("cursor-access-token", "cursor-user").getPassword()) ?? null;
58 },
59 catch: (cause) => new CursorKeychainReadError({ cause }),
60});
61​
62export const make = (
63 read: Effect.Effect<string | null, CursorKeychainReadError> = readKeychainEntry,
64 timeout: Duration.Input = DEFAULT_TIMEOUT,
65): CursorKeychain["Service"] => {
66 let cached: { readonly token: string; readonly untilMs: number } | null = null;
67 let pending: Deferred.Deferred<string | null, CursorKeychainReadError> | null = null;
68​
69 const startRead = Effect.uninterruptible(
70 Effect.gen(function* () {
71 if (pending !== null) return pending;
72 const done = Deferred.makeUnsafe<string | null, CursorKeychainReadError>();
73 pending = done;
74 yield* read.pipe(
75 Effect.tap((token) =>
76 Clock.currentTimeMillis.pipe(
77 Effect.map((nowMs) => {
78 cached = token ? { token, untilMs: nowMs + CACHE_MS } : null;
79 }),
80 ),
81 ),
82 Effect.onExit((exit) =>
83 Effect.suspend(() => {
84 pending = null;
85 return Deferred.done(done, exit);
86 }),
87 ),
88 Effect.forkDetach,
89 );
90 return done;
91 }),
92 );
93​
94 const accessToken = Effect.gen(function* () {
95 const nowMs = yield* Clock.currentTimeMillis;
96 if (cached !== null && cached.untilMs > nowMs) return cached.token;
97 const done = yield* startRead;
98 const token = yield* Deferred.await(done).pipe(Effect.timeoutOption(timeout));
99 if (Option.isNone(token)) return yield* new CursorKeychainTimeoutError();
100 return token.value;
101 });
102​
103 return CursorKeychain.of({ accessToken });
104};
105​
106export const layer = Layer.sync(CursorKeychain, () => make());