(data: unknown)
| 183 | } |
| 184 | |
| 185 | private _onMessage(data: unknown): void { |
| 186 | let frame: AnyFrame; |
| 187 | try { |
| 188 | const raw = typeof data === 'string' ? data : String(data); |
| 189 | frame = JSON.parse(raw) as AnyFrame; |
| 190 | } catch (err) { |
| 191 | this.opts.logger('warn', 'ws: dropped non-JSON frame', { err: String(err) }); |
| 192 | recordReportEvent( |
| 193 | { |
| 194 | kind: 'ws', |
| 195 | direction: 'in', |
| 196 | url: this.opts.url, |
| 197 | message: 'dropped non-JSON frame', |
| 198 | error: errorForReport(err), |
| 199 | }, |
| 200 | { reportDir: this.opts.reportDir }, |
| 201 | ); |
| 202 | return; |
| 203 | } |
| 204 | recordReportEvent( |
| 205 | { kind: 'ws', direction: 'in', url: this.opts.url, frame }, |
| 206 | { reportDir: this.opts.reportDir }, |
| 207 | ); |
| 208 | |
| 209 | if (frame.type === 'ping') { |
| 210 | this.send({ type: 'pong', payload: { nonce: pingNonce(frame) } }); |
| 211 | } |
| 212 | |
| 213 | // Dispatch to subscribers first — they observe every frame, regardless of |
| 214 | // whether a `waitForFrame` consumed it. |
| 215 | for (const sub of this._subscribers) { |
| 216 | try { |
| 217 | sub(frame); |
| 218 | } catch (err) { |
| 219 | this.opts.logger('warn', 'ws: subscriber threw', { err: String(err) }); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Find the FIRST waiter whose predicate matches. A waiter is single-shot. |
| 224 | for (let i = 0; i < this._waiters.length; i++) { |
| 225 | const w = this._waiters[i]; |
| 226 | if (w === undefined) continue; |
| 227 | let matches = false; |
| 228 | try { |
| 229 | matches = w.match(frame); |
| 230 | } catch (err) { |
| 231 | this.opts.logger('warn', 'ws: waiter predicate threw', { err: String(err) }); |
| 232 | } |
| 233 | if (matches) { |
| 234 | this._waiters.splice(i, 1); |
| 235 | w.resolve(frame); |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | this._queue.push(frame); |
| 240 | } |
| 241 | |
| 242 | private _onClose(code: number, reason: string): void { |
no test coverage detected