( targetSocketPath: string, message: string | Record<string, unknown>, timeoutMs = 5000, )
| 204 | * used by SendMessageTool for `uds:<path>` addresses. |
| 205 | */ |
| 206 | export async function sendToUdsSocket( |
| 207 | targetSocketPath: string, |
| 208 | message: string | Record<string, unknown>, |
| 209 | timeoutMs = 5000, |
| 210 | ): Promise<void> { |
| 211 | const { parseUdsTarget } = await import('./udsMessaging.js') |
| 212 | const target = parseUdsTarget(targetSocketPath) |
| 213 | const authToken = await findAuthTokenForSocketPath(target.socketPath) |
| 214 | if (!authToken) { |
| 215 | throw new Error(`No auth token found for peer at ${target.socketPath}`) |
| 216 | } |
| 217 | |
| 218 | const data = typeof message === 'string' ? message : jsonStringify(message) |
| 219 | const udsMsg: UdsMessage = { |
| 220 | type: 'text', |
| 221 | data, |
| 222 | ts: new Date().toISOString(), |
| 223 | } |
| 224 | |
| 225 | // Lazily import to avoid circular dep at module-load time |
| 226 | const { getUdsMessagingSocketPath } = await import('./udsMessaging.js') |
| 227 | udsMsg.from = getUdsMessagingSocketPath() |
| 228 | |
| 229 | return new Promise<void>((resolve, reject) => { |
| 230 | let settled = false |
| 231 | let conn: ReturnType<typeof createConnection> |
| 232 | const finish = (error?: Error): void => { |
| 233 | if (settled) return |
| 234 | settled = true |
| 235 | if (error) { |
| 236 | conn.destroy(error) |
| 237 | reject(error) |
| 238 | } else { |
| 239 | conn.end() |
| 240 | resolve() |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | conn = createConnection(target.socketPath, () => { |
| 245 | udsMsg.meta = { ...udsMsg.meta, authToken } |
| 246 | conn.write(jsonStringify(udsMsg) + '\n', err => { |
| 247 | if (err) finish(err) |
| 248 | }) |
| 249 | }) |
| 250 | attachUdsResponseReader(conn, { |
| 251 | maxFrameBytes: MAX_UDS_FRAME_BYTES, |
| 252 | onSettled: finish, |
| 253 | formatSocketError: err => |
| 254 | new UdsPeerConnectionError(target.socketPath, err), |
| 255 | }) |
| 256 | conn.setTimeout(timeoutMs, () => { |
| 257 | finish( |
| 258 | new UdsPeerConnectionError( |
| 259 | target.socketPath, |
| 260 | new Error('Connection timed out'), |
| 261 | ), |
| 262 | ) |
| 263 | }) |
no test coverage detected