@@ -83,15 +83,14 @@ export async function isLockValid(lockData: LockfileData): Promise<boolean> {
83
83
*/
84
84
export async function waitForAuthentication ( port : number ) : Promise < boolean > {
85
85
log ( `Waiting for authentication from the server on port ${ port } ...` )
86
- if ( DEBUG ) debugLog ( `Waiting for authentication from server on port ${ port } ` )
87
86
88
87
try {
89
88
let attempts = 0
90
89
while ( true ) {
91
90
attempts ++
92
91
const url = `http://127.0.0.1:${ port } /wait-for-auth`
93
92
log ( `Querying: ${ url } ` )
94
- if ( DEBUG ) debugLog ( `Poll attempt ${ attempts } : ${ url } ` )
93
+ if ( DEBUG ) debugLog ( `Poll attempt ${ attempts } ` )
95
94
96
95
try {
97
96
const response = await fetch ( url )
@@ -100,16 +99,14 @@ export async function waitForAuthentication(port: number): Promise<boolean> {
100
99
if ( response . status === 200 ) {
101
100
// Auth completed, but we don't return the code anymore
102
101
log ( `Authentication completed by other instance` )
103
- if ( DEBUG ) debugLog ( `Authentication completed by other instance` )
104
102
return true
105
103
} else if ( response . status === 202 ) {
106
104
// Continue polling
107
105
log ( `Authentication still in progress` )
108
- if ( DEBUG ) debugLog ( `Authentication still in progress, will retry in 1s` )
106
+ if ( DEBUG ) debugLog ( `Will retry in 1s` )
109
107
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
110
108
} else {
111
109
log ( `Unexpected response status: ${ response . status } ` )
112
- if ( DEBUG ) debugLog ( `Unexpected response status` , { status : response . status } )
113
110
return false
114
111
}
115
112
} catch ( fetchError ) {
@@ -181,17 +178,15 @@ export async function coordinateAuth(
181
178
182
179
// If there's a valid lockfile, try to use the existing auth process
183
180
if ( lockData && ( await isLockValid ( lockData ) ) ) {
184
- log ( `Another instance is handling authentication on port ${ lockData . port } ` )
185
- if ( DEBUG ) debugLog ( 'Another instance is handling authentication' , { port : lockData . port , pid : lockData . pid } )
181
+ log ( `Another instance is handling authentication on port ${ lockData . port } (pid: ${ lockData . pid } )` )
186
182
187
183
try {
188
184
// Try to wait for the authentication to complete
189
185
if ( DEBUG ) debugLog ( 'Waiting for authentication from other instance' )
190
186
const authCompleted = await waitForAuthentication ( lockData . port )
191
187
192
188
if ( authCompleted ) {
193
- log ( 'Authentication completed by another instance' )
194
- if ( DEBUG ) debugLog ( 'Authentication completed by another instance, will use tokens from disk' )
189
+ log ( 'Authentication completed by another instance. Using tokens from disk' )
195
190
196
191
// Setup a dummy server - the client will use tokens directly from disk
197
192
const dummyServer = express ( ) . listen ( 0 ) // Listen on any available port
@@ -201,7 +196,6 @@ export async function coordinateAuth(
201
196
// This shouldn't actually be called in normal operation, but provide it for API compatibility
202
197
const dummyWaitForAuthCode = ( ) => {
203
198
log ( 'WARNING: waitForAuthCode called in secondary instance - this is unexpected' )
204
- if ( DEBUG ) debugLog ( 'WARNING: waitForAuthCode called in secondary instance - this is unexpected' )
205
199
// Return a promise that never resolves - the client should use the tokens from disk instead
206
200
return new Promise < string > ( ( ) => { } )
207
201
}
@@ -213,7 +207,6 @@ export async function coordinateAuth(
213
207
}
214
208
} else {
215
209
log ( 'Taking over authentication process...' )
216
- if ( DEBUG ) debugLog ( 'Taking over authentication process' )
217
210
}
218
211
} catch ( error ) {
219
212
log ( `Error waiting for authentication: ${ error } ` )
@@ -226,7 +219,6 @@ export async function coordinateAuth(
226
219
} else if ( lockData ) {
227
220
// Invalid lockfile, delete it
228
221
log ( 'Found invalid lockfile, deleting it' )
229
- if ( DEBUG ) debugLog ( 'Found invalid lockfile, deleting it' )
230
222
await deleteLockfile ( serverUrlHash )
231
223
}
232
224
@@ -244,14 +236,12 @@ export async function coordinateAuth(
244
236
if ( DEBUG ) debugLog ( 'OAuth callback server running' , { port : actualPort } )
245
237
246
238
log ( `Creating lockfile for server ${ serverUrlHash } with process ${ process . pid } on port ${ actualPort } ` )
247
- if ( DEBUG ) debugLog ( 'Creating lockfile' , { serverUrlHash, pid : process . pid , port : actualPort } )
248
239
await createLockfile ( serverUrlHash , process . pid , actualPort )
249
240
250
241
// Make sure lockfile is deleted on process exit
251
242
const cleanupHandler = async ( ) => {
252
243
try {
253
244
log ( `Cleaning up lockfile for server ${ serverUrlHash } ` )
254
- if ( DEBUG ) debugLog ( 'Cleaning up lockfile' )
255
245
await deleteLockfile ( serverUrlHash )
256
246
} catch ( error ) {
257
247
log ( `Error cleaning up lockfile: ${ error } ` )
0 commit comments