({
store,
handleIncomingPrompt,
}: UsePipeIpcOptions)
| 444 | // --------------------------------------------------------------------------- |
| 445 | |
| 446 | export function usePipeIpc({ |
| 447 | store, |
| 448 | handleIncomingPrompt, |
| 449 | }: UsePipeIpcOptions): void { |
| 450 | if (!feature('UDS_INBOX')) return |
| 451 | |
| 452 | useEffect(() => { |
| 453 | const sessionId = _getSessionId() |
| 454 | if (!sessionId) return |
| 455 | const pipeName = `cli-${sessionId.slice(0, 8)}` |
| 456 | const disposed = { current: false } |
| 457 | let heartbeatTimer: ReturnType<typeof setInterval> | null = null |
| 458 | let heartbeatBusy = false |
| 459 | let pipeServer: PipeServer | null = null |
| 460 | |
| 461 | void (async () => { |
| 462 | try { |
| 463 | // --- Phase 1: Role determination --- |
| 464 | const machId = await pr.getMachineId() |
| 465 | const mac = pr.getMacAddress() |
| 466 | const localIp = pt.getLocalIp() |
| 467 | const host = osm.hostname() |
| 468 | const roleResult = await pr.determineRole(machId) |
| 469 | |
| 470 | const entry = { |
| 471 | id: pipeName, |
| 472 | pid: process.pid, |
| 473 | machineId: machId, |
| 474 | startedAt: Date.now(), |
| 475 | ip: localIp, |
| 476 | mac, |
| 477 | hostname: host, |
| 478 | pipeName, |
| 479 | } |
| 480 | |
| 481 | let initialRole: 'main' | 'sub' = 'main' |
| 482 | let subIndex: number | null = null |
| 483 | let displayRole = 'main' |
| 484 | |
| 485 | if (roleResult.role === 'main' || roleResult.role === 'main-recover') { |
| 486 | await pr.registerAsMain(entry) |
| 487 | } else { |
| 488 | subIndex = roleResult.subIndex |
| 489 | await pr.registerAsSub(entry, subIndex) |
| 490 | initialRole = 'sub' |
| 491 | displayRole = `sub-${subIndex}` |
| 492 | } |
| 493 | |
| 494 | // --- Phase 2: Server creation --- |
| 495 | const server = await pt.createPipeServer( |
| 496 | pipeName, |
| 497 | feature('LAN_PIPES') ? { enableTcp: true, tcpPort: 0 } : undefined, |
| 498 | ) |
| 499 | pipeServer = server |
| 500 | if (disposed.current) { |
| 501 | await server.close() |
| 502 | await pr.unregister(pipeName) |
| 503 | return |
no test coverage detected