|
1 | 1 | // Port Management Service |
2 | 2 |
|
3 | | -import type { Logger } from '@repo/shared'; |
| 3 | +import type { Logger, PortCheckRequest, PortCheckResponse } from '@repo/shared'; |
4 | 4 | import type { |
5 | 5 | InvalidPortContext, |
6 | 6 | PortAlreadyExposedContext, |
@@ -414,6 +414,93 @@ export class PortService { |
414 | 414 | } |
415 | 415 | } |
416 | 416 |
|
| 417 | + /** |
| 418 | + * Check if a port is ready to accept connections |
| 419 | + * Supports both TCP and HTTP modes |
| 420 | + */ |
| 421 | + async checkPortReady(request: PortCheckRequest): Promise<PortCheckResponse> { |
| 422 | + const { |
| 423 | + port, |
| 424 | + mode, |
| 425 | + path = '/', |
| 426 | + statusMin = 200, |
| 427 | + statusMax = 399 |
| 428 | + } = request; |
| 429 | + |
| 430 | + if (mode === 'tcp') { |
| 431 | + return this.checkTcpReady(port); |
| 432 | + } else { |
| 433 | + return this.checkHttpReady(port, path, statusMin, statusMax); |
| 434 | + } |
| 435 | + } |
| 436 | + |
| 437 | + private async checkTcpReady(port: number): Promise<PortCheckResponse> { |
| 438 | + const TCP_TIMEOUT_MS = 5000; // 5 second timeout matching HTTP check |
| 439 | + |
| 440 | + try { |
| 441 | + const timeoutPromise = new Promise<never>((_, reject) => { |
| 442 | + setTimeout( |
| 443 | + () => reject(new Error('TCP connection timeout')), |
| 444 | + TCP_TIMEOUT_MS |
| 445 | + ); |
| 446 | + }); |
| 447 | + |
| 448 | + const connectPromise = Bun.connect({ |
| 449 | + hostname: 'localhost', |
| 450 | + port, |
| 451 | + socket: { |
| 452 | + data() {}, |
| 453 | + open(socket) { |
| 454 | + socket.end(); |
| 455 | + }, |
| 456 | + error() {}, |
| 457 | + close() {} |
| 458 | + } |
| 459 | + }); |
| 460 | + |
| 461 | + const socket = await Promise.race([connectPromise, timeoutPromise]); |
| 462 | + // Connection succeeded |
| 463 | + socket.end(); |
| 464 | + return { ready: true }; |
| 465 | + } catch (error) { |
| 466 | + return { |
| 467 | + ready: false, |
| 468 | + error: error instanceof Error ? error.message : 'TCP connection failed' |
| 469 | + }; |
| 470 | + } |
| 471 | + } |
| 472 | + |
| 473 | + private async checkHttpReady( |
| 474 | + port: number, |
| 475 | + path: string, |
| 476 | + statusMin: number, |
| 477 | + statusMax: number |
| 478 | + ): Promise<PortCheckResponse> { |
| 479 | + try { |
| 480 | + const url = `http://localhost:${port}${path.startsWith('/') ? path : `/${path}`}`; |
| 481 | + const response = await fetch(url, { |
| 482 | + method: 'GET', |
| 483 | + signal: AbortSignal.timeout(5000) // 5 second timeout for individual check |
| 484 | + }); |
| 485 | + |
| 486 | + const statusCode = response.status; |
| 487 | + const ready = statusCode >= statusMin && statusCode <= statusMax; |
| 488 | + |
| 489 | + return { |
| 490 | + ready, |
| 491 | + statusCode, |
| 492 | + error: ready |
| 493 | + ? undefined |
| 494 | + : `HTTP status ${statusCode} not in expected range ${statusMin}-${statusMax}` |
| 495 | + }; |
| 496 | + } catch (error) { |
| 497 | + return { |
| 498 | + ready: false, |
| 499 | + error: error instanceof Error ? error.message : 'HTTP request failed' |
| 500 | + }; |
| 501 | + } |
| 502 | + } |
| 503 | + |
417 | 504 | private startCleanupProcess(): void { |
418 | 505 | this.cleanupInterval = setInterval( |
419 | 506 | async () => { |
|
0 commit comments