@@ -4,7 +4,7 @@ import { Server } from 'http'
4
4
import express from 'express'
5
5
import { AddressInfo } from 'net'
6
6
import { unlinkSync } from 'fs'
7
- import { log , debugLog , DEBUG , setupOAuthCallbackServerWithLongPoll } from './utils'
7
+ import { log , debugLog , setupOAuthCallbackServerWithLongPoll } from './utils'
8
8
9
9
export type AuthCoordinator = {
10
10
initializeAuth : ( ) => Promise < { server : Server ; waitForAuthCode : ( ) => Promise < string > ; skipBrowserAuth : boolean } >
@@ -18,10 +18,10 @@ export type AuthCoordinator = {
18
18
export async function isPidRunning ( pid : number ) : Promise < boolean > {
19
19
try {
20
20
process . kill ( pid , 0 ) // Doesn't kill the process, just checks if it exists
21
- if ( DEBUG ) debugLog ( `Process ${ pid } is running` )
21
+ debugLog ( `Process ${ pid } is running` )
22
22
return true
23
23
} catch ( err ) {
24
- if ( DEBUG ) debugLog ( `Process ${ pid } is not running` , err )
24
+ debugLog ( `Process ${ pid } is not running` , err )
25
25
return false
26
26
}
27
27
}
@@ -32,30 +32,29 @@ export async function isPidRunning(pid: number): Promise<boolean> {
32
32
* @returns True if the lockfile is valid, false otherwise
33
33
*/
34
34
export async function isLockValid ( lockData : LockfileData ) : Promise < boolean > {
35
- if ( DEBUG ) debugLog ( 'Checking if lockfile is valid' , lockData )
35
+ debugLog ( 'Checking if lockfile is valid' , lockData )
36
36
37
37
// Check if the lockfile is too old (over 30 minutes)
38
38
const MAX_LOCK_AGE = 30 * 60 * 1000 // 30 minutes
39
39
if ( Date . now ( ) - lockData . timestamp > MAX_LOCK_AGE ) {
40
40
log ( 'Lockfile is too old' )
41
- if ( DEBUG )
42
- debugLog ( 'Lockfile is too old' , {
43
- age : Date . now ( ) - lockData . timestamp ,
44
- maxAge : MAX_LOCK_AGE ,
45
- } )
41
+ debugLog ( 'Lockfile is too old' , {
42
+ age : Date . now ( ) - lockData . timestamp ,
43
+ maxAge : MAX_LOCK_AGE ,
44
+ } )
46
45
return false
47
46
}
48
47
49
48
// Check if the process is still running
50
49
if ( ! ( await isPidRunning ( lockData . pid ) ) ) {
51
50
log ( 'Process from lockfile is not running' )
52
- if ( DEBUG ) debugLog ( 'Process from lockfile is not running' , { pid : lockData . pid } )
51
+ debugLog ( 'Process from lockfile is not running' , { pid : lockData . pid } )
53
52
return false
54
53
}
55
54
56
55
// Check if the endpoint is accessible
57
56
try {
58
- if ( DEBUG ) debugLog ( 'Checking if endpoint is accessible' , { port : lockData . port } )
57
+ debugLog ( 'Checking if endpoint is accessible' , { port : lockData . port } )
59
58
60
59
const controller = new AbortController ( )
61
60
const timeout = setTimeout ( ( ) => controller . abort ( ) , 1000 )
@@ -67,11 +66,11 @@ export async function isLockValid(lockData: LockfileData): Promise<boolean> {
67
66
clearTimeout ( timeout )
68
67
69
68
const isValid = response . status === 200 || response . status === 202
70
- if ( DEBUG ) debugLog ( `Endpoint check result: ${ isValid ? 'valid' : 'invalid' } ` , { status : response . status } )
69
+ debugLog ( `Endpoint check result: ${ isValid ? 'valid' : 'invalid' } ` , { status : response . status } )
71
70
return isValid
72
71
} catch ( error ) {
73
72
log ( `Error connecting to auth server: ${ ( error as Error ) . message } ` )
74
- if ( DEBUG ) debugLog ( 'Error connecting to auth server' , error )
73
+ debugLog ( 'Error connecting to auth server' , error )
75
74
return false
76
75
}
77
76
}
@@ -90,11 +89,11 @@ export async function waitForAuthentication(port: number): Promise<boolean> {
90
89
attempts ++
91
90
const url = `http://127.0.0.1:${ port } /wait-for-auth`
92
91
log ( `Querying: ${ url } ` )
93
- if ( DEBUG ) debugLog ( `Poll attempt ${ attempts } ` )
92
+ debugLog ( `Poll attempt ${ attempts } ` )
94
93
95
94
try {
96
95
const response = await fetch ( url )
97
- if ( DEBUG ) debugLog ( `Poll response status: ${ response . status } ` )
96
+ debugLog ( `Poll response status: ${ response . status } ` )
98
97
99
98
if ( response . status === 200 ) {
100
99
// Auth completed, but we don't return the code anymore
@@ -103,21 +102,21 @@ export async function waitForAuthentication(port: number): Promise<boolean> {
103
102
} else if ( response . status === 202 ) {
104
103
// Continue polling
105
104
log ( `Authentication still in progress` )
106
- if ( DEBUG ) debugLog ( `Will retry in 1s` )
105
+ debugLog ( `Will retry in 1s` )
107
106
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
108
107
} else {
109
108
log ( `Unexpected response status: ${ response . status } ` )
110
109
return false
111
110
}
112
111
} catch ( fetchError ) {
113
- if ( DEBUG ) debugLog ( `Fetch error during poll` , fetchError )
112
+ debugLog ( `Fetch error during poll` , fetchError )
114
113
// If we can't connect, we'll try again after a delay
115
114
await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
116
115
}
117
116
}
118
117
} catch ( error ) {
119
118
log ( `Error waiting for authentication: ${ ( error as Error ) . message } ` )
120
- if ( DEBUG ) debugLog ( `Error waiting for authentication` , error )
119
+ debugLog ( `Error waiting for authentication` , error )
121
120
return false
122
121
}
123
122
}
@@ -141,16 +140,16 @@ export function createLazyAuthCoordinator(
141
140
initializeAuth : async ( ) => {
142
141
// If auth has already been initialized, return the existing state
143
142
if ( authState ) {
144
- if ( DEBUG ) debugLog ( 'Auth already initialized, reusing existing state' )
143
+ debugLog ( 'Auth already initialized, reusing existing state' )
145
144
return authState
146
145
}
147
146
148
147
log ( 'Initializing auth coordination on-demand' )
149
- if ( DEBUG ) debugLog ( 'Initializing auth coordination on-demand' , { serverUrlHash, callbackPort } )
148
+ debugLog ( 'Initializing auth coordination on-demand' , { serverUrlHash, callbackPort } )
150
149
151
150
// Initialize auth using the existing coordinateAuth logic
152
151
authState = await coordinateAuth ( serverUrlHash , callbackPort , events , authTimeoutMs )
153
- if ( DEBUG ) debugLog ( 'Auth coordination completed' , { skipBrowserAuth : authState . skipBrowserAuth } )
152
+ debugLog ( 'Auth coordination completed' , { skipBrowserAuth : authState . skipBrowserAuth } )
154
153
return authState
155
154
} ,
156
155
}
@@ -169,17 +168,15 @@ export async function coordinateAuth(
169
168
events : EventEmitter ,
170
169
authTimeoutMs : number ,
171
170
) : Promise < { server : Server ; waitForAuthCode : ( ) => Promise < string > ; skipBrowserAuth : boolean } > {
172
- if ( DEBUG ) debugLog ( 'Coordinating authentication' , { serverUrlHash, callbackPort } )
171
+ debugLog ( 'Coordinating authentication' , { serverUrlHash, callbackPort } )
173
172
174
173
// Check for a lockfile (disabled on Windows for the time being)
175
174
const lockData = process . platform === 'win32' ? null : await checkLockfile ( serverUrlHash )
176
175
177
- if ( DEBUG ) {
178
- if ( process . platform === 'win32' ) {
179
- debugLog ( 'Skipping lockfile check on Windows' )
180
- } else {
181
- debugLog ( 'Lockfile check result' , { found : ! ! lockData , lockData } )
182
- }
176
+ if ( process . platform === 'win32' ) {
177
+ debugLog ( 'Skipping lockfile check on Windows' )
178
+ } else {
179
+ debugLog ( 'Lockfile check result' , { found : ! ! lockData , lockData } )
183
180
}
184
181
185
182
// If there's a valid lockfile, try to use the existing auth process
@@ -188,7 +185,7 @@ export async function coordinateAuth(
188
185
189
186
try {
190
187
// Try to wait for the authentication to complete
191
- if ( DEBUG ) debugLog ( 'Waiting for authentication from other instance' )
188
+ debugLog ( 'Waiting for authentication from other instance' )
192
189
const authCompleted = await waitForAuthentication ( lockData . port )
193
190
194
191
if ( authCompleted ) {
@@ -197,7 +194,7 @@ export async function coordinateAuth(
197
194
// Setup a dummy server - the client will use tokens directly from disk
198
195
const dummyServer = express ( ) . listen ( 0 ) // Listen on any available port
199
196
const dummyPort = ( dummyServer . address ( ) as AddressInfo ) . port
200
- if ( DEBUG ) debugLog ( 'Started dummy server' , { port : dummyPort } )
197
+ debugLog ( 'Started dummy server' , { port : dummyPort } )
201
198
202
199
// This shouldn't actually be called in normal operation, but provide it for API compatibility
203
200
const dummyWaitForAuthCode = ( ) => {
@@ -216,11 +213,11 @@ export async function coordinateAuth(
216
213
}
217
214
} catch ( error ) {
218
215
log ( `Error waiting for authentication: ${ error } ` )
219
- if ( DEBUG ) debugLog ( 'Error waiting for authentication' , error )
216
+ debugLog ( 'Error waiting for authentication' , error )
220
217
}
221
218
222
219
// If we get here, the other process didn't complete auth successfully
223
- if ( DEBUG ) debugLog ( 'Other instance did not complete auth successfully, deleting lockfile' )
220
+ debugLog ( 'Other instance did not complete auth successfully, deleting lockfile' )
224
221
await deleteLockfile ( serverUrlHash )
225
222
} else if ( lockData ) {
226
223
// Invalid lockfile, delete it
@@ -229,7 +226,7 @@ export async function coordinateAuth(
229
226
}
230
227
231
228
// Create our own lockfile
232
- if ( DEBUG ) debugLog ( 'Setting up OAuth callback server' , { port : callbackPort } )
229
+ debugLog ( 'Setting up OAuth callback server' , { port : callbackPort } )
233
230
const { server, waitForAuthCode, authCompletedPromise } = setupOAuthCallbackServerWithLongPoll ( {
234
231
port : callbackPort ,
235
232
path : '/oauth/callback' ,
@@ -240,7 +237,7 @@ export async function coordinateAuth(
240
237
// Get the actual port the server is running on
241
238
const address = server . address ( ) as AddressInfo
242
239
const actualPort = address . port
243
- if ( DEBUG ) debugLog ( 'OAuth callback server running' , { port : actualPort } )
240
+ debugLog ( 'OAuth callback server running' , { port : actualPort } )
244
241
245
242
log ( `Creating lockfile for server ${ serverUrlHash } with process ${ process . pid } on port ${ actualPort } ` )
246
243
await createLockfile ( serverUrlHash , process . pid , actualPort )
@@ -252,7 +249,7 @@ export async function coordinateAuth(
252
249
await deleteLockfile ( serverUrlHash )
253
250
} catch ( error ) {
254
251
log ( `Error cleaning up lockfile: ${ error } ` )
255
- if ( DEBUG ) debugLog ( 'Error cleaning up lockfile' , error )
252
+ debugLog ( 'Error cleaning up lockfile' , error )
256
253
}
257
254
}
258
255
@@ -261,19 +258,19 @@ export async function coordinateAuth(
261
258
// Synchronous version for 'exit' event since we can't use async here
262
259
const configPath = getConfigFilePath ( serverUrlHash , 'lock.json' )
263
260
unlinkSync ( configPath )
264
- if ( DEBUG ) console . error ( `[DEBUG] Removed lockfile on exit: ${ configPath } `)
261
+ debugLog ( ` Removed lockfile on exit: ${ configPath } `)
265
262
} catch ( error ) {
266
- if ( DEBUG ) console . error ( `[DEBUG] Error removing lockfile on exit:`, error )
263
+ debugLog ( ` Error removing lockfile on exit:`, error )
267
264
}
268
265
} )
269
266
270
267
// Also handle SIGINT separately
271
268
process . once ( 'SIGINT' , async ( ) => {
272
- if ( DEBUG ) debugLog ( 'Received SIGINT signal, cleaning up' )
269
+ debugLog ( 'Received SIGINT signal, cleaning up' )
273
270
await cleanupHandler ( )
274
271
} )
275
272
276
- if ( DEBUG ) debugLog ( 'Auth coordination complete, returning primary instance handlers' )
273
+ debugLog ( 'Auth coordination complete, returning primary instance handlers' )
277
274
return {
278
275
server,
279
276
waitForAuthCode,
0 commit comments