@@ -4,25 +4,47 @@ const CACHE_MS = 5 * 60_000;
44
55const requireForKeyring = NodeModule . createRequire ( import . meta. url ) ;
66
7- /** Share one Keychain request across usage history and limits in this server process. */
7+ /** Rejected when nobody answers the macOS Keychain prompt in time. */
8+ export class CursorKeychainTimeoutError extends Error {
9+ constructor ( ) {
10+ super ( "Timed out waiting for Keychain access." ) ;
11+ }
12+ }
13+
14+ /**
15+ * Share one Keychain request across usage history and limits in this server process.
16+ *
17+ * macOS shows the access prompt on the server's own screen, which a remote
18+ * client cannot answer, so callers give up after `timeoutMs`. The read stays in
19+ * flight: the next call reuses it instead of stacking a second prompt, and picks
20+ * up the token once someone allows access.
21+ */
822export function makeCachedCursorAccessTokenReader (
923 read : ( ) => Promise < string | null > ,
1024 now : ( ) => number = Date . now ,
25+ timeoutMs = 30_000 ,
1126) : ( ) => Promise < string | null > {
1227 let cached : { token : string ; until : number } | null = null ;
1328 let pending : Promise < string | null > | null = null ;
1429 return ( ) => {
1530 if ( cached && cached . until > now ( ) ) return Promise . resolve ( cached . token ) ;
16- if ( pending ) return pending ;
17- pending = read ( )
31+ pending ??= read ( )
1832 . then ( ( token ) => {
1933 cached = token ? { token, until : now ( ) + CACHE_MS } : null ;
2034 return token ;
2135 } )
2236 . finally ( ( ) => {
2337 pending = null ;
2438 } ) ;
25- return pending ;
39+ const deadline = AbortSignal . timeout ( timeoutMs ) ;
40+ return Promise . race ( [
41+ pending ,
42+ new Promise < never > ( ( _ , reject ) => {
43+ deadline . addEventListener ( "abort" , ( ) => reject ( new CursorKeychainTimeoutError ( ) ) , {
44+ once : true ,
45+ } ) ;
46+ } ) ,
47+ ] ) ;
2648 } ;
2749}
2850
0 commit comments