| 411 | } |
| 412 | |
| 413 | private async connectUds(timeoutMs: number): Promise<void> { |
| 414 | const { access } = await import('fs/promises') |
| 415 | const deadline = Date.now() + timeoutMs |
| 416 | const retryDelayMs = 300 |
| 417 | |
| 418 | // Wait for socket file to exist (Unix only) |
| 419 | if (process.platform !== 'win32') { |
| 420 | while (Date.now() < deadline) { |
| 421 | try { |
| 422 | await access(this.socketPath) |
| 423 | break |
| 424 | } catch { |
| 425 | if (Date.now() + retryDelayMs >= deadline) { |
| 426 | throw new Error( |
| 427 | `Pipe "${this.targetName}" not found at ${this.socketPath}. Is the server running?`, |
| 428 | ) |
| 429 | } |
| 430 | await new Promise(r => setTimeout(r, retryDelayMs)) |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return new Promise((resolve, reject) => { |
| 436 | const timer = setTimeout( |
| 437 | () => { |
| 438 | reject( |
| 439 | new Error( |
| 440 | `Connection to pipe "${this.targetName}" timed out after ${timeoutMs}ms`, |
| 441 | ), |
| 442 | ) |
| 443 | }, |
| 444 | Math.max(deadline - Date.now(), 1000), |
| 445 | ) |
| 446 | |
| 447 | const socket = createConnection({ path: this.socketPath }, () => { |
| 448 | clearTimeout(timer) |
| 449 | this.socket = socket |
| 450 | this.setupSocketListeners(socket) |
| 451 | this.emit('connected') |
| 452 | resolve() |
| 453 | }) |
| 454 | |
| 455 | socket.on('error', err => { |
| 456 | clearTimeout(timer) |
| 457 | socket.destroy() |
| 458 | reject(err) |
| 459 | }) |
| 460 | }) |
| 461 | } |
| 462 | |
| 463 | private setupSocketListeners(socket: Socket): void { |
| 464 | attachNdjsonFramer<PipeMessage>(socket, msg => { |