@@ -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
}
@@ -136,16 +135,16 @@ export function createLazyAuthCoordinator(serverUrlHash: string, callbackPort: n
136
135
initializeAuth : async ( ) => {
137
136
// If auth has already been initialized, return the existing state
138
137
if ( authState ) {
139
- if ( DEBUG ) debugLog ( 'Auth already initialized, reusing existing state' )
138
+ debugLog ( 'Auth already initialized, reusing existing state' )
140
139
return authState
141
140
}
142
141
143
142
log ( 'Initializing auth coordination on-demand' )
144
- if ( DEBUG ) debugLog ( 'Initializing auth coordination on-demand' , { serverUrlHash, callbackPort } )
143
+ debugLog ( 'Initializing auth coordination on-demand' , { serverUrlHash, callbackPort } )
145
144
146
145
// Initialize auth using the existing coordinateAuth logic
147
146
authState = await coordinateAuth ( serverUrlHash , callbackPort , events )
148
- if ( DEBUG ) debugLog ( 'Auth coordination completed' , { skipBrowserAuth : authState . skipBrowserAuth } )
147
+ debugLog ( 'Auth coordination completed' , { skipBrowserAuth : authState . skipBrowserAuth } )
149
148
return authState
150
149
} ,
151
150
}
@@ -163,17 +162,15 @@ export async function coordinateAuth(
163
162
callbackPort : number ,
164
163
events : EventEmitter ,
165
164
) : Promise < { server : Server ; waitForAuthCode : ( ) => Promise < string > ; skipBrowserAuth : boolean } > {
166
- if ( DEBUG ) debugLog ( 'Coordinating authentication' , { serverUrlHash, callbackPort } )
165
+ debugLog ( 'Coordinating authentication' , { serverUrlHash, callbackPort } )
167
166
168
167
// Check for a lockfile (disabled on Windows for the time being)
169
168
const lockData = process . platform === 'win32' ? null : await checkLockfile ( serverUrlHash )
170
169
171
- if ( DEBUG ) {
172
- if ( process . platform === 'win32' ) {
173
- debugLog ( 'Skipping lockfile check on Windows' )
174
- } else {
175
- debugLog ( 'Lockfile check result' , { found : ! ! lockData , lockData } )
176
- }
170
+ if ( process . platform === 'win32' ) {
171
+ debugLog ( 'Skipping lockfile check on Windows' )
172
+ } else {
173
+ debugLog ( 'Lockfile check result' , { found : ! ! lockData , lockData } )
177
174
}
178
175
179
176
// If there's a valid lockfile, try to use the existing auth process
@@ -182,7 +179,7 @@ export async function coordinateAuth(
182
179
183
180
try {
184
181
// Try to wait for the authentication to complete
185
- if ( DEBUG ) debugLog ( 'Waiting for authentication from other instance' )
182
+ debugLog ( 'Waiting for authentication from other instance' )
186
183
const authCompleted = await waitForAuthentication ( lockData . port )
187
184
188
185
if ( authCompleted ) {
@@ -191,7 +188,7 @@ export async function coordinateAuth(
191
188
// Setup a dummy server - the client will use tokens directly from disk
192
189
const dummyServer = express ( ) . listen ( 0 ) // Listen on any available port
193
190
const dummyPort = ( dummyServer . address ( ) as AddressInfo ) . port
194
- if ( DEBUG ) debugLog ( 'Started dummy server' , { port : dummyPort } )
191
+ debugLog ( 'Started dummy server' , { port : dummyPort } )
195
192
196
193
// This shouldn't actually be called in normal operation, but provide it for API compatibility
197
194
const dummyWaitForAuthCode = ( ) => {
@@ -210,11 +207,11 @@ export async function coordinateAuth(
210
207
}
211
208
} catch ( error ) {
212
209
log ( `Error waiting for authentication: ${ error } ` )
213
- if ( DEBUG ) debugLog ( 'Error waiting for authentication' , error )
210
+ debugLog ( 'Error waiting for authentication' , error )
214
211
}
215
212
216
213
// If we get here, the other process didn't complete auth successfully
217
- if ( DEBUG ) debugLog ( 'Other instance did not complete auth successfully, deleting lockfile' )
214
+ debugLog ( 'Other instance did not complete auth successfully, deleting lockfile' )
218
215
await deleteLockfile ( serverUrlHash )
219
216
} else if ( lockData ) {
220
217
// Invalid lockfile, delete it
@@ -223,7 +220,7 @@ export async function coordinateAuth(
223
220
}
224
221
225
222
// Create our own lockfile
226
- if ( DEBUG ) debugLog ( 'Setting up OAuth callback server' , { port : callbackPort } )
223
+ debugLog ( 'Setting up OAuth callback server' , { port : callbackPort } )
227
224
const { server, waitForAuthCode, authCompletedPromise } = setupOAuthCallbackServerWithLongPoll ( {
228
225
port : callbackPort ,
229
226
path : '/oauth/callback' ,
@@ -233,7 +230,7 @@ export async function coordinateAuth(
233
230
// Get the actual port the server is running on
234
231
const address = server . address ( ) as AddressInfo
235
232
const actualPort = address . port
236
- if ( DEBUG ) debugLog ( 'OAuth callback server running' , { port : actualPort } )
233
+ debugLog ( 'OAuth callback server running' , { port : actualPort } )
237
234
238
235
log ( `Creating lockfile for server ${ serverUrlHash } with process ${ process . pid } on port ${ actualPort } ` )
239
236
await createLockfile ( serverUrlHash , process . pid , actualPort )
@@ -245,7 +242,7 @@ export async function coordinateAuth(
245
242
await deleteLockfile ( serverUrlHash )
246
243
} catch ( error ) {
247
244
log ( `Error cleaning up lockfile: ${ error } ` )
248
- if ( DEBUG ) debugLog ( 'Error cleaning up lockfile' , error )
245
+ debugLog ( 'Error cleaning up lockfile' , error )
249
246
}
250
247
}
251
248
@@ -254,19 +251,19 @@ export async function coordinateAuth(
254
251
// Synchronous version for 'exit' event since we can't use async here
255
252
const configPath = getConfigFilePath ( serverUrlHash , 'lock.json' )
256
253
unlinkSync ( configPath )
257
- if ( DEBUG ) console . error ( `[DEBUG] Removed lockfile on exit: ${ configPath } `)
254
+ debugLog ( ` Removed lockfile on exit: ${ configPath } `)
258
255
} catch ( error ) {
259
- if ( DEBUG ) console . error ( `[DEBUG] Error removing lockfile on exit:`, error )
256
+ debugLog ( ` Error removing lockfile on exit:`, error )
260
257
}
261
258
} )
262
259
263
260
// Also handle SIGINT separately
264
261
process . once ( 'SIGINT' , async ( ) => {
265
- if ( DEBUG ) debugLog ( 'Received SIGINT signal, cleaning up' )
262
+ debugLog ( 'Received SIGINT signal, cleaning up' )
266
263
await cleanupHandler ( )
267
264
} )
268
265
269
- if ( DEBUG ) debugLog ( 'Auth coordination complete, returning primary instance handlers' )
266
+ debugLog ( 'Auth coordination complete, returning primary instance handlers' )
270
267
return {
271
268
server,
272
269
waitForAuthCode,
0 commit comments