(eventType, data, options = {})
| 467 | } |
| 468 | |
| 469 | async request(eventType, data, options = {}) { |
| 470 | const correlationId = |
| 471 | normalizeCorrelationId(options?.correlationId) || |
| 472 | createCorrelationId("request"); |
| 473 | const payload = this.buildPayload(data); |
| 474 | payload.correlationId = correlationId; |
| 475 | |
| 476 | const timeoutMs = Number(options.timeoutMs ?? DEFAULT_TIMEOUT_MS); |
| 477 | this.debugLog("request", { eventType, correlationId, timeoutMs }); |
| 478 | if (!Number.isFinite(timeoutMs) || timeoutMs < 0) { |
| 479 | throw new Error("timeoutMs must be a non-negative number"); |
| 480 | } |
| 481 | this.ensurePayloadSize(payload); |
| 482 | await this.connect(); |
| 483 | if (!this.isConnected()) { |
| 484 | throw new Error("Not connected"); |
| 485 | } |
| 486 | |
| 487 | return new Promise((resolve, reject) => { |
| 488 | if (timeoutMs > 0) { |
| 489 | this.socket |
| 490 | .timeout(timeoutMs) |
| 491 | .emit(eventType, payload, (err, response) => { |
| 492 | if (err) { |
| 493 | reject(new Error("Request timeout")); |
| 494 | return; |
| 495 | } |
| 496 | resolve(this.normalizeRequestResponse(response)); |
| 497 | }); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | this.socket.emit(eventType, payload, (response) => { |
| 502 | resolve(this.normalizeRequestResponse(response)); |
| 503 | }); |
| 504 | }); |
| 505 | } |
| 506 | |
| 507 | normalizeRequestResponse(response) { |
| 508 | if (!response || typeof response !== "object") { |
no test coverage detected