(
method: string,
params?: unknown,
options?: { signal?: AbortSignal; timeoutMs?: number }
)
| 276 | } |
| 277 | |
| 278 | private async sendRequest( |
| 279 | method: string, |
| 280 | params?: unknown, |
| 281 | options?: { signal?: AbortSignal; timeoutMs?: number } |
| 282 | ): Promise<unknown> { |
| 283 | if (!this.connected) { |
| 284 | await this.connect(); |
| 285 | } |
| 286 | |
| 287 | const id = this.nextId++; |
| 288 | const payload: JsonRpcLiteRequest = { |
| 289 | id, |
| 290 | method, |
| 291 | params |
| 292 | }; |
| 293 | |
| 294 | const timeoutMs = options?.timeoutMs ?? CodexAppServerClient.DEFAULT_TIMEOUT_MS; |
| 295 | |
| 296 | return new Promise((resolve, reject) => { |
| 297 | let timeout: ReturnType<typeof setTimeout> | null = null; |
| 298 | let aborted = false; |
| 299 | |
| 300 | const cleanup = () => { |
| 301 | if (timeout) { |
| 302 | clearTimeout(timeout); |
| 303 | } |
| 304 | if (options?.signal) { |
| 305 | options.signal.removeEventListener('abort', onAbort); |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | const onAbort = () => { |
| 310 | if (aborted) return; |
| 311 | aborted = true; |
| 312 | this.pending.delete(id); |
| 313 | cleanup(); |
| 314 | reject(createAbortError()); |
| 315 | }; |
| 316 | |
| 317 | if (options?.signal) { |
| 318 | if (options.signal.aborted) { |
| 319 | onAbort(); |
| 320 | return; |
| 321 | } |
| 322 | options.signal.addEventListener('abort', onAbort, { once: true }); |
| 323 | } |
| 324 | |
| 325 | if (Number.isFinite(timeoutMs)) { |
| 326 | timeout = setTimeout(() => { |
| 327 | if (this.pending.has(id)) { |
| 328 | this.pending.delete(id); |
| 329 | cleanup(); |
| 330 | reject(new Error(`Codex app-server request '${method}' timed out after ${timeoutMs}ms`)); |
| 331 | } |
| 332 | }, timeoutMs); |
| 333 | timeout.unref(); |
| 334 | } |
| 335 |
no test coverage detected