* Send a request to the daemon and wait for a response.
(method: DaemonMethod, params?: unknown)
| 50 | * Send a request to the daemon and wait for a response. |
| 51 | */ |
| 52 | async request<TResult>(method: DaemonMethod, params?: unknown): Promise<TResult> { |
| 53 | const id = randomUUID(); |
| 54 | const req: DaemonRequest = { |
| 55 | v: DAEMON_PROTOCOL_VERSION, |
| 56 | id, |
| 57 | method, |
| 58 | params, |
| 59 | }; |
| 60 | |
| 61 | return new Promise<TResult>((resolve, reject) => { |
| 62 | const socket = net.createConnection(this.socketPath); |
| 63 | let resolved = false; |
| 64 | |
| 65 | const cleanup = (): void => { |
| 66 | if (!resolved) { |
| 67 | resolved = true; |
| 68 | socket.destroy(); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | const timeoutId = setTimeout(() => { |
| 73 | cleanup(); |
| 74 | reject(new Error(`Daemon request timed out after ${this.timeout}ms`)); |
| 75 | }, this.timeout); |
| 76 | |
| 77 | socket.on('error', (err) => { |
| 78 | clearTimeout(timeoutId); |
| 79 | cleanup(); |
| 80 | if (err.message.includes('ECONNREFUSED') || err.message.includes('ENOENT')) { |
| 81 | reject(new Error('Daemon is not running. Start it with: xcodebuildmcp daemon start')); |
| 82 | } else { |
| 83 | reject(err); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | const onData = createFrameReader( |
| 88 | (msg) => { |
| 89 | const res = msg as DaemonResponse<TResult>; |
| 90 | if (res.id !== id) return; |
| 91 | |
| 92 | clearTimeout(timeoutId); |
| 93 | resolved = true; |
| 94 | socket.end(); |
| 95 | |
| 96 | if (res.error) { |
| 97 | if ( |
| 98 | res.error.code === 'BAD_REQUEST' && |
| 99 | res.error.message.startsWith('Unsupported protocol version') |
| 100 | ) { |
| 101 | reject(new DaemonVersionMismatchError(res.error.message)); |
| 102 | } else { |
| 103 | reject(new Error(`${res.error.code}: ${res.error.message}`)); |
| 104 | } |
| 105 | } else { |
| 106 | resolve(res.result as TResult); |
| 107 | } |
| 108 | }, |
| 109 | (err) => { |
no test coverage detected