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

cursorCredentialStore.ts

33 lines1.2 KB

1import * as NodeModule from "node:module";
2​
3const CACHE_MS = 5 * 60_000;
4​
5const requireForKeyring = NodeModule.createRequire(import.meta.url);
6​
7/** Share one Keychain request across usage history and limits in this server process. */
8export function makeCachedCursorAccessTokenReader(
9 read: () => Promise<string | null>,
10 now: () => number = Date.now,
11): () => Promise<string | null> {
12 let cached: { token: string; until: number } | null = null;
13 let pending: Promise<string | null> | null = null;
14 return () => {
15 if (cached && cached.until > now()) return Promise.resolve(cached.token);
16 if (pending) return pending;
17 pending = read()
18 .then((token) => {
19 cached = token ? { token, until: now() + CACHE_MS } : null;
20 return token;
21 })
22 .finally(() => {
23 pending = null;
24 });
25 return pending;
26 };
28​
29/** Read the Cursor CLI's default macOS credential without invoking the shared security binary. */
30export const readMacCursorAccessToken = makeCachedCursorAccessTokenReader(async () => {
31 const { AsyncEntry } = requireForKeyring("@napi-rs/keyring") as typeof import("@napi-rs/keyring");
32 return (await new AsyncEntry("cursor-access-token", "cursor-user").getPassword()) ?? null;
33});