* Sends a bidi request * @param params * @returns {Promise }
(params)
| 201 | * @returns {Promise<unknown>} |
| 202 | */ |
| 203 | async send(params) { |
| 204 | if (this._closed) { |
| 205 | throw new Error('BiDi connection is closed') |
| 206 | } |
| 207 | if (!this.connected) { |
| 208 | await this.waitForConnection() |
| 209 | } |
| 210 | // Defense in depth: even after waitForConnection() resolves, the socket |
| 211 | // may have transitioned to CLOSING/CLOSED (e.g. caller closed the raw |
| 212 | // socket). Refuse rather than throwing from inside ws.send(). |
| 213 | if (this._ws.readyState !== WebSocket.OPEN) { |
| 214 | throw new Error('BiDi connection is not open') |
| 215 | } |
| 216 | |
| 217 | const id = ++this.id |
| 218 | |
| 219 | this._ws.send(JSON.stringify({ id, ...params })) |
| 220 | |
| 221 | return new Promise((resolve, reject) => { |
| 222 | const timeoutId = setTimeout(() => { |
| 223 | this._pending.delete(id) |
| 224 | reject(new Error(`Request with id ${id} timed out`)) |
| 225 | }, RESPONSE_TIMEOUT) |
| 226 | |
| 227 | this._pending.set(id, { resolve, reject, timeoutId }) |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Subscribe to events |
no test coverage detected