* Start listening for incoming connections. * @param options - Optional TCP configuration for LAN mode.
(options?: PipeServerOptions)
| 214 | * @param options - Optional TCP configuration for LAN mode. |
| 215 | */ |
| 216 | async start(options?: PipeServerOptions): Promise<void> { |
| 217 | await ensurePipesDir() |
| 218 | |
| 219 | // Clean up stale socket file (Unix only) |
| 220 | if (process.platform !== 'win32') { |
| 221 | try { |
| 222 | await unlink(this.socketPath) |
| 223 | } catch { |
| 224 | // File doesn't exist — fine |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Start UDS/Named Pipe server |
| 229 | await new Promise<void>((resolve, reject) => { |
| 230 | this.server = createServer(socket => this.setupSocket(socket)) |
| 231 | |
| 232 | this.server.on('error', reject) |
| 233 | |
| 234 | this.server.listen(this.socketPath, () => { |
| 235 | // On Windows, Named Pipes don't exist in the filesystem. |
| 236 | // Write a registry file so listPipes() can discover this server. |
| 237 | if (process.platform === 'win32') { |
| 238 | const regFile = join(getPipesDir(), `${this.name}.pipe`) |
| 239 | const { hostname } = require('os') as typeof import('os') |
| 240 | void writeFile( |
| 241 | regFile, |
| 242 | JSON.stringify({ |
| 243 | pid: process.pid, |
| 244 | ts: Date.now(), |
| 245 | ip: getLocalIp(), |
| 246 | hostname: hostname(), |
| 247 | }), |
| 248 | ).catch(() => {}) |
| 249 | } |
| 250 | resolve() |
| 251 | }) |
| 252 | }) |
| 253 | |
| 254 | // Optionally start TCP server for LAN connectivity |
| 255 | if (options?.enableTcp) { |
| 256 | await this.startTcpServer(options.tcpPort ?? 0) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Start TCP listener for LAN peers. |
no test coverage detected