@@ -20,9 +20,28 @@ export async function oauthCreateAgent(session: OAuthSession) {
2020 return agent . prepare ( account , gates , moderation )
2121}
2222
23+ const OAUTH_RESTORE_TIMEOUT_MS = 10_000
24+
2325export async function oauthResumeSession ( account : SessionAccount ) {
2426 const client = getWebOAuthClient ( )
25- const session = await client . restore ( account . did )
27+ let session : OAuthSession
28+ try {
29+ session = await Promise . race ( [
30+ client . restore ( account . did ) ,
31+ new Promise < never > ( ( _ , reject ) =>
32+ setTimeout (
33+ ( ) => reject ( new Error ( 'OAuth session restore timed out' ) ) ,
34+ OAUTH_RESTORE_TIMEOUT_MS ,
35+ ) ,
36+ ) ,
37+ ] )
38+ } catch ( e ) {
39+ logger . error ( 'oauthResumeSession: restore failed' , {
40+ did : account . did ,
41+ error : e instanceof Error ? e . message : String ( e ) ,
42+ } )
43+ throw e
44+ }
2645 return await oauthCreateAgent ( session )
2746}
2847
@@ -43,13 +62,36 @@ export async function oauthAgentAndSessionToSessionAccount(
4362) : Promise < SessionAccount | undefined > {
4463 let data : OutputSchema
4564 try {
46- const res = await agent . com . atproto . server . getSession ( )
65+ const res = await Promise . race ( [
66+ agent . com . atproto . server . getSession ( ) ,
67+ new Promise < never > ( ( _ , reject ) =>
68+ setTimeout (
69+ ( ) => reject ( new Error ( 'getSession timed out' ) ) ,
70+ OAUTH_RESTORE_TIMEOUT_MS ,
71+ ) ,
72+ ) ,
73+ ] )
4774 data = res . data
4875 } catch ( e : any ) {
49- logger . error ( e )
76+ logger . error ( 'oauthAgentAndSessionToSessionAccount: getSession failed' , e )
77+ return undefined
78+ }
79+ let aud : string
80+ try {
81+ const tokenInfo = await Promise . race ( [
82+ session . getTokenInfo ( false ) ,
83+ new Promise < never > ( ( _ , reject ) =>
84+ setTimeout (
85+ ( ) => reject ( new Error ( 'getTokenInfo timed out' ) ) ,
86+ OAUTH_RESTORE_TIMEOUT_MS ,
87+ ) ,
88+ ) ,
89+ ] )
90+ aud = tokenInfo . aud
91+ } catch ( e : any ) {
92+ logger . error ( 'oauthAgentAndSessionToSessionAccount: getTokenInfo failed' , e )
5093 return undefined
5194 }
52- const { aud} = await session . getTokenInfo ( false )
5395 return {
5496 service : session . serverMetadata . issuer ,
5597 did : session . did ,
@@ -69,12 +111,8 @@ export class OauthBskyAppAgent extends Agent {
69111 session ?: AtpSessionData
70112 dispatchUrl ?: string
71113
72- #oauthSession: OAuthSession
73- #account?: SessionAccount
74-
75114 constructor ( session : OAuthSession ) {
76115 super ( session )
77- this . #oauthSession = session
78116 }
79117
80118 async prepare (
0 commit comments