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

CursorKeychain.ts

102 lines3.3 KB

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