( info: DaemonInfo, req: DaemonRequest, statePaths: DaemonPaths, timeoutMs: number | undefined, options: SendRequestOptions, )
| 360 | } |
| 361 | |
| 362 | async function sendHttpRequest( |
| 363 | info: DaemonInfo, |
| 364 | req: DaemonRequest, |
| 365 | statePaths: DaemonPaths, |
| 366 | timeoutMs: number | undefined, |
| 367 | options: SendRequestOptions, |
| 368 | ): Promise<DaemonResponse> { |
| 369 | const rpcUrl = info.baseUrl |
| 370 | ? new URL(buildDaemonHttpUrl(info.baseUrl, 'rpc')) |
| 371 | : info.httpPort |
| 372 | ? new URL(`http://127.0.0.1:${info.httpPort}/rpc`) |
| 373 | : null; |
| 374 | if (!rpcUrl) throw new AppError('COMMAND_FAILED', DAEMON_HTTP_ENDPOINT_UNAVAILABLE_MESSAGE); |
| 375 | const rpcPayload = JSON.stringify(buildHttpRpcPayload(req, { includeTokenParam: !info.baseUrl })); |
| 376 | const headers: Record<string, string | number> = { |
| 377 | 'content-type': 'application/json', |
| 378 | 'content-length': Buffer.byteLength(rpcPayload), |
| 379 | }; |
| 380 | if (info.baseUrl) { |
| 381 | Object.assign(headers, buildDaemonHttpAuthHeaders(info.token)); |
| 382 | } |
| 383 | |
| 384 | return await new Promise((resolve, reject) => { |
| 385 | const transport = rpcUrl.protocol === 'https:' ? https : http; |
| 386 | const request = transport.request( |
| 387 | { |
| 388 | protocol: rpcUrl.protocol, |
| 389 | host: rpcUrl.hostname, |
| 390 | port: rpcUrl.port, |
| 391 | method: 'POST', |
| 392 | path: rpcUrl.pathname + rpcUrl.search, |
| 393 | headers, |
| 394 | }, |
| 395 | (res) => { |
| 396 | if (shouldReadDaemonProgressStream(req, res.headers?.['content-type'])) { |
| 397 | readDaemonHttpProgressResponse(res, { |
| 398 | req, |
| 399 | onProgress: options.onProgress, |
| 400 | reject, |
| 401 | clearTimeout: () => { |
| 402 | if (timeoutHandle) clearTimeout(timeoutHandle); |
| 403 | }, |
| 404 | handleResponseBody: (body) => |
| 405 | handleDaemonHttpResponseBody(body, { info, req, resolve, reject }), |
| 406 | }); |
| 407 | return; |
| 408 | } |
| 409 | void readNodeHttpResponseBody(res) |
| 410 | .then((body) => { |
| 411 | if (timeoutHandle) clearTimeout(timeoutHandle); |
| 412 | handleDaemonHttpResponseBody(body, { info, req, resolve, reject }); |
| 413 | }) |
| 414 | .catch((err: unknown) => { |
| 415 | if (timeoutHandle) clearTimeout(timeoutHandle); |
| 416 | reject( |
| 417 | new AppError( |
| 418 | 'COMMAND_FAILED', |
| 419 | 'Failed to read daemon response', |
no test coverage detected