Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 46x 46x 46x 8x 5589x 5589x 5589x 8x 8x 8x 8x 8x 8x 8x 8x 8x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 8x 8x 8x 321x 321x 8x 8x 8x 12x 12x 8x 8x 10x 10x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 2x 2x 8x 8x 1x 1x 8x 8x 8x 8x 8x 8x 8x 5360x 5360x 5360x 3x 3x 3x 20x 20x 3x 3x 5356x 5360x 4x 2x 2x 2x 4x 4x 5360x 5352x 5352x 5352x 5360x 4914x 4911x 4911x 4911x 4911x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 4911x 4909x 4909x 3x 3x 3x 4500x 380x 380x 380x 5360x 5x 5x 5x 5x 53x 53x 5360x 8x 8x 8x 8x 8x 8x 8x 73x 71x 71x 73x 69x 69x 69x 69x 69x 1x 1x 1x 68x 69x 68x 73x 70x 73x 8x 8x 8x 7x 5x 5x 5x 5x 5x 5x 5x 7x 8x 8x 8x 1x 1x 8x 8x 8x 8x 156x 156x 8x 8x 1x 1x 8x 8x 2346x 2346x 2346x 8x 8x 1117x 1117x 1117x 2346x 2346x 2346x 2346x 2346x 1117x 1117x 1117x 1117x 8x 8x 8x 8x 416x 2x 2x 2x 414x 414x 414x 414x 414x 414x 414x 416x 8x 8x 1458x 1458x 1458x 1445x 1445x 1458x 10x 10x 1458x 8x 8x 721x 721x 721x 8x 8x 52x 52x 52x 8x 8x 836x 836x 836x 7x 7x 7x 7x 836x 836x 8x 8x 737x 737x 737x 737x 737x 737x 737x 737x 737x 737x 737x 737x 8x 8x 2133x 2133x 2133x 791x 1045x 2133x 8x 8x 151x 151x 148x 148x 151x 151x 3x 151x 151x 148x 148x 148x 148x 148x 148x 151x 148x 148x 148x 3x 3x 3x 3x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 151x 1x 1x 1x 151x 151x 147x 147x 136x 147x 147x 705x 147x 147x 13x 147x 147x 147x 147x 151x 8x 8x 6310x 6310x 6310x 6310x 6310x 6310x 20x 6310x 6310x 6310x 6310x 8x 8x 353x 353x 8x 8x 5957x 5957x 5957x 5957x 5957x 8x 8x 705x 1x 1x 1x 704x 704x 704x 705x 1x 1x 705x 1x 1x 1x 705x 703x 705x 705x 705x 705x 705x 705x 705x 705x 705x 8x 8x 132x 132x 132x 132x 132x 93x 93x 93x 132x 132x 132x 131x 131x 131x 132x 132x 132x 132x 487x 132x 132x 132x 132x 8x 8x 134x 2x 2x 2x 132x 132x 134x 2x 2x 2x 132x 132x 132x 134x 134x 134x 3049x 3049x 3049x 134x 134x 132x 134x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 3x 3x 3x 3x 40x 132x 132x 132x 40x 40x 132x 92x 92x 92x 132x 134x 134x 8x 8x 74x 74x 74x 74x 74x 74x 74x 74x 8x 8x 135x 135x 127x 127x 127x 135x 135x 8x 8x 4x 4x 4x 4x 4x 4x 4x 8x 8x 254x 254x 254x 254x 16x 16x 16x 254x 254x 254x 8x 8x 577x 577x 577x 577x 577x 2x 2x 2x 2x 577x 577x 8x 8x 495x 495x 495x 8x 8x 74x 74x 74x 74x 74x 4x 4x 4x 74x 74x 8x 8x 8x 8x 2x 2x 2x 2x 6x 8x 4x 8x 8x 8x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 8x 8x 228x 228x 228x 212x 212x 10x 10x 10x 10x 202x 202x 218x 228x 8x 8x 46x 46x 46x 11x 11x 11x 11x 46x 46x 8x 8x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 4x 4x 4x 64x 1x 1x 1x 64x 1x 1x 1x 64x 3x 3x 3x 64x 2x 2x 2x 64x 44x 44x 5x 5x 5x 44x 53x 53x 53x 53x 53x 53x 52x 52x 53x 53x 53x 53x 53x 64x 8x 8x 163x 163x 163x 163x 163x 163x 163x 163x 163x 147x 147x 147x 147x 163x 8x 8x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 14x 29x 29x 29x 29x 29x 29x 8x 8x 99x 99x 99x 99x 99x 99x 99x 99x 99x 8x | // WebSocket evaluation session over wx.connectSocket (DESIGN 2.5): state machine, listener
// guarantees, reconnect with REPLAY, DROP or FAIL audio buffering, application heartbeat and
// result timeout.
//
// Listener guarantees:
// - exactly one of onResult or onError per session, unless cancel() came first, then neither;
// - onClosed is always the last callback and is delivered once;
// - callbacks of one session never nest: a callback that calls back into the session gets the
// resulting callbacks after it returns, in order;
// - getState() equals the state announced by the last onStateChanged.
//
// Heartbeat (mini programs cannot send WebSocket protocol pings):
// - on every connection the SDK sends one probe {"cmd":"ping"} right after "connected", before the
// start or parameter frame, so a server that misreads it cannot lose audio;
// - the periodic heartbeat is armed only after the server answered that probe with {"event":"pong"};
// then a ping goes out every heartbeatIntervalMs while CONNECTED or STARTED, never while ENDING,
// and heartbeatTimeoutMs without any frame counts as a transport failure;
// - a server that answers the probe with {"event":"error","message":"unknown cmd"}, or answers the
// start frame first, has no heartbeat support: heartbeat stays off for the rest of the session and
// the session relies on the connect timeout, close detection and resultTimeoutMs.
//
// Reading progress (CONTRACT 5.5): partial frames reach onPartial while STARTED and ENDING. A new
// server session after a reconnect computes the progress again from zero while the audio is replayed,
// so a progress partial is delivered only when readUnits and matchedUnits are both at least those of
// the last delivered one; the others are dropped with a DEBUG log. Partials without progress always
// pass. Every session starts without a delivered progress. DROP forgets the delivered progress at
// every successful reconnect: the new server session scores only the audio sent after it, so its
// counts start below the delivered ones and would otherwise stay hidden.
import { PcmStats, analyzePcmStats, applyPrecheckMode } from './precheck.js';
import {
asYuguError, fromWsErrorFrame, fromWxFail, isRetryable, localError, ProtocolViolationException,
} from './errors.js';
import { computeBackoffDelay } from './retry.js';
import { parseEvalResult, parsePartialResult } from './result.js';
import { wsAuthQuery } from './signer.js';
import { redactUrl } from './logger.js';
import { describeError } from './http.js';
import { MAX_AUDIO_FRAME_BYTES } from './constants.js';
import { copyBytes, encodeQuery, exactArrayBuffer, microtask, randomHex, toInt, toUint8, truncate } from './util.js';
const TAG = 'YuguSDK:ws';
export const SessionState = Object.freeze({
IDLE: 'IDLE',
CONNECTING: 'CONNECTING',
CONNECTED: 'CONNECTED',
STARTED: 'STARTED',
ENDING: 'ENDING',
RECONNECTING: 'RECONNECTING',
COMPLETED: 'COMPLETED',
FAILED: 'FAILED',
CANCELLED: 'CANCELLED',
CLOSED: 'CLOSED',
});
export const AudioBufferPolicy = Object.freeze({ REPLAY: 'REPLAY', DROP: 'DROP', FAIL: 'FAIL' });
const ALLOWED = {
IDLE: ['CONNECTING', 'FAILED', 'CANCELLED'],
CONNECTING: ['CONNECTED', 'RECONNECTING', 'FAILED', 'CANCELLED'],
CONNECTED: ['STARTED', 'RECONNECTING', 'FAILED', 'CANCELLED'],
STARTED: ['ENDING', 'COMPLETED', 'RECONNECTING', 'FAILED', 'CANCELLED'],
ENDING: ['COMPLETED', 'RECONNECTING', 'FAILED', 'CANCELLED'],
RECONNECTING: ['CONNECTING', 'FAILED', 'CANCELLED'],
COMPLETED: ['CLOSED'],
FAILED: ['CLOSED'],
CANCELLED: ['CLOSED'],
CLOSED: [],
};
// Close codes a reconnect cannot fix: the server refused the session itself (DESIGN 2.5). 1000 before
// the final result is handled the same way.
const FATAL_CLOSE_CODES = [1002, 1003, 1007, 1008, 1009, 1010];
function isFatalCloseCode(code) {
return code === 1000 || FATAL_CLOSE_CODES.indexOf(code) >= 0 || (code >= 4000 && code <= 4999);
}
function isActiveState(s) {
return s !== 'COMPLETED' && s !== 'FAILED' && s !== 'CANCELLED' && s !== 'CLOSED';
}
let sessionSeq = 0;
export class StreamSession {
/**
* Created by YuguClient.streamEvaluate and streamEvaluateCompat; not meant to be constructed
* directly. Connects on the next microtask.
*/
constructor(ctx, cfg, listener) {
this._ctx = ctx;
this._cfg = cfg;
sessionSeq += 1;
this.id = 'ws-' + sessionSeq + '-' + randomHex(3);
this.idempotencyKey = cfg.idempotencyKey || null;
this.mode = cfg.mode;
this._state = 'IDLE';
this._announced = 'IDLE';
this._listener = listener && typeof listener === 'object' ? listener : {};
this._observers = [];
this._queue = [];
this._draining = false;
this._closeQueued = false;
this._cancelled = false;
this._gen = 0;
this._conn = null;
this._timers = { connect: null, result: null, reconnect: null, hbInterval: null, hbWatchdog: null };
this._hbDisabled = !(cfg.heartbeatIntervalMs > 0);
this._hbConfirmed = false;
this._hbStatus = this._hbDisabled ? 'disabled' : 'probing';
this._probeOutstanding = false;
this._everConnected = false;
this._everStarted = false;
this._inReconnect = false;
this._reconnectAttempt = 0;
this._totalReconnects = 0;
// Every reconnect attempt of this session, successful or not. At most 3 x maxAttempts (DESIGN 2.5),
// so a server that keeps accepting and then dropping the session cannot loop forever.
this._totalAttempts = 0;
this._lastClose = null;
this._replay = [];
this._replayBytes = 0;
this._overflow = false;
this._pending = [];
this._droppedBytes = 0;
this._totalDroppedBytes = 0;
this._stats = new PcmStats();
this._bytesAccepted = 0;
this._endRequested = false;
this._endSent = false;
this._localWarnings = [];
this._ignoredAudioLogged = false;
// readUnits and matchedUnits of the last progress partial given to onPartial in this session.
this._lastProgress = null;
microtask(() => this._connect());
}
/** Current state, equal to the state announced by the last onStateChanged. */
getState() {
return this._announced;
}
/** True until the session reaches COMPLETED, FAILED, CANCELLED or CLOSED. */
isActive() {
return isActiveState(this._announced);
}
getStats() {
return {
bytesAccepted: this._bytesAccepted,
reconnects: this._totalReconnects,
reconnectAttempts: this._totalAttempts,
droppedBytes: this._totalDroppedBytes,
replayBufferBytes: this._replayBytes,
heartbeat: this._hbStatus,
};
}
/** Replaces the listener. null removes it; the session keeps running without callbacks. */
setListener(listener) {
this._listener = listener && typeof listener === 'object' ? listener : {};
}
removeListener() {
this.setListener(null);
}
/**
* Queues or sends one chunk of 16 bit little-endian PCM (16000 Hz mono unless the session says
* otherwise). Chunks above 32000 bytes are sent as several frames. Returns false and ignores the
* chunk once end() was called or the session is over.
*/
sendAudio(chunk) {
const u8 = toUint8(chunk);
if (!u8) throw localError(90010, 'sendAudio expects an ArrayBuffer or a typed array');
if (u8.length > MAX_AUDIO_FRAME_BYTES) {
// The platform closes frames above 128 KB with 1009; large chunks go out as several frames.
let accepted = true;
for (let off = 0; off < u8.length && accepted; off += MAX_AUDIO_FRAME_BYTES) {
accepted = this.sendAudio(u8.subarray(off, off + MAX_AUDIO_FRAME_BYTES));
}
return accepted;
}
const s = this._state;
if (!isActiveState(s) || this._endRequested) {
if (!this._ignoredAudioLogged) {
this._ignoredAudioLogged = true;
this._ctx.logger.warn(TAG, this.id + ': sendAudio ignored after ' + (isActiveState(s) ? 'end()' : 'the session ended (' + s + ')'));
}
return false;
}
if (u8.length === 0) return true;
this._stats.add(u8);
this._bytesAccepted += u8.length;
const policy = this._cfg.bufferPolicy;
if (policy === 'REPLAY') {
if (!this._overflow) {
const copy = copyBytes(u8);
this._replay.push(copy);
this._replayBytes += copy.length;
if (this._replayBytes > this._cfg.replayBufferLimitBytes) {
this._overflow = true;
this._replay = [];
this._replayBytes = 0;
this._ctx.logger.warn(TAG, this.id + ': replay buffer passed ' + this._cfg.replayBufferLimitBytes + ' bytes; a later reconnect fails with 90008');
if (s !== 'STARTED') {
this._fail(localError(90008, 'replay buffer overflow while waiting for the server session'));
return false;
}
this._transmit(u8);
return true;
}
if (s === 'STARTED') this._transmit(copy);
return true;
}
if (s === 'STARTED') this._transmit(u8);
return true;
}
if (s === 'STARTED') {
this._transmit(u8);
return true;
}
if (policy === 'DROP' && this._inReconnect) {
this._droppedBytes += u8.length;
this._totalDroppedBytes += u8.length;
return true;
}
this._pending.push(copyBytes(u8));
return true;
}
/**
* Ends the audio. The end frame goes out now when STARTED, else right after the server session
* starts. Runs the precheck on the accumulated audio first. Returns false when already ended or
* when the session is over.
*/
end() {
if (!isActiveState(this._state) || this._endRequested) return false;
this._endRequested = true;
const mode = this._cfg.audioPrecheck;
if (mode !== 'OFF') {
const report = analyzePcmStats(this._stats, this._cfg.sampleRate, 1);
let warnings;
try {
warnings = applyPrecheckMode(report.warnings, mode, { idempotencyKey: this.idempotencyKey, op: this._op() });
} catch (e) {
this._fail(e);
return false;
}
this._localWarnings = warnings;
for (let i = 0; i < warnings.length; i++) this._emit('onWarning', [warnings[i]]);
}
if (this._state === 'STARTED') this._sendEnd();
return true;
}
/** Stops the session without onResult or onError; onClosed still follows. Idempotent. */
cancel() {
if (!isActiveState(this._state)) return;
this._cancelled = true;
this._clearTimer('reconnect');
this._detachConnection('cancelled');
this._batch(() => {
this._setState('CANCELLED');
this._finishClosed(1000, 'cancelled');
});
}
/** Same as cancel() while active, no-op afterwards. Idempotent. */
close() {
this.cancel();
}
// ------------------------------------------------------------------ internals
_op() {
return this.mode === 'compat' ? 'streamEvaluateCompat' : 'streamEvaluate';
}
_addObserver(fn) {
this._observers.push(fn);
}
_enqueue(fn) {
this._queue.push(fn);
if (!this._draining) this._drain();
}
_drain() {
this._draining = true;
try {
while (this._queue.length) {
const next = this._queue.shift();
try {
next();
} catch (e) {
this._ctx.logger.error(TAG, this.id + ': internal callback error', e);
}
}
} finally {
this._draining = false;
}
}
// Queues the callbacks of one logical transition together, so a listener that calls back into the
// session sees its own effects only after the whole transition was announced.
_batch(fn) {
if (this._draining) {
fn();
return;
}
this._draining = true;
try {
fn();
} finally {
this._draining = false;
}
this._drain();
}
_call(name, args) {
const l = this._listener;
const fn = l && l[name];
if (typeof fn !== 'function') return;
try {
fn.apply(l, args);
} catch (e) {
this._ctx.logger.error(TAG, this.id + ': listener.' + name + ' threw', e);
}
}
_emit(name, args) {
if (this._closeQueued) return;
this._enqueue(() => this._call(name, args));
}
_emitEvent(name, args) {
const events = this._ctx.events;
this._enqueue(() => events.emit.apply(events, [name].concat(args)));
}
_notify(event, a, b) {
const list = this._observers.slice();
this._enqueue(() => {
for (let i = 0; i < list.length; i++) {
try {
list[i](event, a, b);
} catch (e) {
this._ctx.logger.error(TAG, this.id + ': observer threw', e);
}
}
});
}
_setState(next) {
const prev = this._state;
if (prev === next) return;
if ((ALLOWED[prev] || []).indexOf(next) < 0) {
this._ctx.logger.error(TAG, this.id + ': unexpected transition ' + prev + ' -> ' + next);
}
this._state = next;
const events = this._ctx.events;
this._enqueue(() => {
this._announced = next;
this._call('onStateChanged', [prev, next]);
events.emit('onSessionStateChanged', this.id, prev, next);
});
this._notify('state', prev, next);
}
_clearTimer(name) {
const t = this._timers[name];
if (t === null) return;
if (name === 'hbInterval') clearInterval(t);
else clearTimeout(t);
this._timers[name] = null;
}
_connect() {
const s = this._state;
if (s !== 'IDLE' && s !== 'RECONNECTING') return;
const gen = ++this._gen;
const query = {};
const extra = this._cfg.query || {};
Object.keys(extra).forEach((k) => {
query[k] = extra[k];
});
if (this.idempotencyKey) query.idempotencyKey = this.idempotencyKey;
const q = wsAuthQuery(this._ctx.auth, query);
const url = this._ctx.wsBaseUrl + this._cfg.path + '?' + encodeQuery(q);
const conn = { gen, task: null, txAudioBytes: 0 };
this._conn = conn;
this._lastClose = null;
this._setState('CONNECTING');
if (gen !== this._gen) return;
this._ctx.logger.debug(TAG, this.id + ': connect ' + redactUrl(url));
const timeoutMs = this._cfg.connectTimeoutMs;
this._timers.connect = setTimeout(() => {
this._timers.connect = null;
if (gen === this._gen && (this._state === 'CONNECTING' || this._state === 'CONNECTED')) {
this._onTransportFailure(localError(90002, 'connect timeout: session not started within ' + timeoutMs + ' ms'));
}
}, timeoutMs);
let task;
try {
task = this._ctx.wx.connectSocket({
url,
header: { 'X-Yugu-SDK': this._ctx.userAgent },
timeout: timeoutMs,
fail: (e) => {
if (gen === this._gen) this._onTransportFailure(fromWxFail(e, 'connectSocket'));
},
});
} catch (e) {
this._onTransportFailure(fromWxFail(e, 'connectSocket'));
return;
}
if (gen !== this._gen) return;
if (!task) {
this._onTransportFailure(localError(90001, 'wx.connectSocket returned no SocketTask'));
return;
}
conn.task = task;
task.onOpen(() => {
if (gen === this._gen) this._ctx.logger.debug(TAG, this.id + ': socket open');
});
task.onMessage((m) => {
if (gen === this._gen) this._onFrame(m ? m.data : undefined);
});
task.onError((e) => {
if (gen === this._gen) this._onTransportFailure(fromWxFail(e, 'socket'));
});
task.onClose((c) => {
if (gen === this._gen) this._onSocketClose(c && typeof c.code === 'number' ? c.code : 1006, (c && c.reason) || '');
});
}
_send(conn, data) {
if (!conn || conn !== this._conn || !conn.task) return;
const gen = conn.gen;
try {
conn.task.send({
data,
fail: (e) => {
if (gen === this._gen) this._onTransportFailure(fromWxFail(e, 'socket send'));
},
});
} catch (e) {
if (gen === this._gen) this._onTransportFailure(fromWxFail(e, 'socket send'));
}
}
_sendJson(conn, obj) {
this._send(conn, JSON.stringify(obj));
}
_transmit(u8) {
const conn = this._conn;
if (!conn) return;
conn.txAudioBytes += u8.length;
this._send(conn, exactArrayBuffer(u8));
}
_onFrame(data) {
if (typeof data !== 'string') {
this._ctx.logger.debug(TAG, this.id + ': binary frame from server ignored');
return;
}
let f = null;
try {
f = JSON.parse(data);
} catch (e) {
f = null;
}
if (!f || typeof f !== 'object' || Array.isArray(f)) {
this._fail(new ProtocolViolationException('server frame is not a JSON object', { code: 90005, rawBody: truncate(data, 4096) }));
return;
}
if (this._timers.hbInterval !== null) this._armWatchdog();
const ev = f.event;
if (ev === 'pong') return this._onPong();
if (ev === 'connected') return this._onConnectedFrame();
if (ev === 'started') return this._onStartedFrame();
if (ev === 'error') return this._onErrorFrame(f);
if (ev === 'result' || (f.eof !== undefined && toInt(f.eof) === 1)) return this._onResultFrame(f);
if (f.eof !== undefined && toInt(f.eof) === 0) return this._onPartialFrame(f);
this._ctx.logger.debug(TAG, this.id + ': frame ignored: ' + (ev || 'no event'));
return undefined;
}
_onConnectedFrame() {
if (this._state !== 'CONNECTING') {
this._ctx.logger.debug(TAG, this.id + ': duplicate connected frame ignored');
return;
}
const conn = this._conn;
this._batch(() => {
this._setState('CONNECTED');
if (!this._everConnected) {
this._everConnected = true;
this._emit('onConnected', []);
}
});
if (this._conn !== conn) return;
if (!this._hbDisabled) {
this._probeOutstanding = true;
this._sendJson(conn, { cmd: 'ping' });
}
if (this._conn !== conn) return;
const frame = {};
const first = this._cfg.firstFrame;
Object.keys(first).forEach((k) => {
frame[k] = first[k];
});
if (this.idempotencyKey) frame.idempotencyKey = this.idempotencyKey;
this._sendJson(conn, frame);
}
_onStartedFrame() {
if (this._state !== 'CONNECTED') {
this._ctx.logger.debug(TAG, this.id + ': duplicate started frame ignored');
return;
}
const conn = this._conn;
this._clearTimer('connect');
if (this._probeOutstanding) {
this._probeOutstanding = false;
this._disableHeartbeat('the server started the session without answering the ping probe');
}
// Audio queued before the start, or kept for replay, goes out before STARTED is announced, so
// a listener calling end() from a callback can never get the end frame ahead of that audio.
const replay = this._cfg.bufferPolicy === 'REPLAY';
const list = replay ? this._replay.slice() : this._pending;
if (!replay) this._pending = [];
for (let i = 0; i < list.length; i++) {
if (this._conn !== conn) return;
this._transmit(list[i]);
}
if (this._conn !== conn) return;
if (this._hbConfirmed) this._startHeartbeat();
let reconnected = null;
if (this._inReconnect) {
reconnected = {
attempt: this._reconnectAttempt,
info: {
droppedBytes: this._droppedBytes,
totalDroppedBytes: this._totalDroppedBytes,
replayedBytes: replay ? this._replayBytes : 0,
},
};
this._inReconnect = false;
this._reconnectAttempt = 0;
this._droppedBytes = 0;
this._totalReconnects += 1;
this._ctx.logger.info(TAG, this.id + ': reconnected on attempt ' + reconnected.attempt);
if (this._cfg.bufferPolicy === 'DROP' && this._lastProgress) {
// The new server session covers only new audio, its progress starts again from zero.
this._lastProgress = null;
this._ctx.logger.debug(TAG, this.id + ': delivered progress forgotten, DROP scores only the audio after the reconnect');
}
}
this._batch(() => {
this._setState('STARTED');
if (reconnected) {
this._emitEvent('onReconnect', [this.id, reconnected.attempt, true]);
this._emit('onReconnected', [reconnected.attempt, reconnected.info]);
} else if (!this._everStarted) {
this._everStarted = true;
this._emit('onStarted', []);
}
});
if (this._endRequested && this._conn === conn && this._state === 'STARTED') this._sendEnd();
}
_sendEnd() {
const conn = this._conn;
this._endSent = true;
this._stopHeartbeat();
this._armResultTimer();
this._sendJson(conn, { cmd: 'end' });
if (this._conn !== conn) return;
this._setState('ENDING');
}
_onPong() {
this._probeOutstanding = false;
if (this._hbDisabled || this._hbConfirmed) return;
this._hbConfirmed = true;
this._hbStatus = 'active';
this._ctx.logger.debug(TAG, this.id + ': heartbeat confirmed by the server');
if (this._state === 'CONNECTED' || this._state === 'STARTED') this._startHeartbeat();
}
_disableHeartbeat(reason) {
if (this._hbDisabled) return;
this._hbDisabled = true;
this._hbConfirmed = false;
this._hbStatus = 'disabled';
this._stopHeartbeat();
this._ctx.logger.info(TAG, this.id + ': heartbeat off for this session: ' + reason);
}
_startHeartbeat() {
this._stopHeartbeat();
const conn = this._conn;
if (this._hbDisabled || !this._hbConfirmed || !conn) return;
this._timers.hbInterval = setInterval(() => {
if (this._conn !== conn) return;
if (this._state !== 'CONNECTED' && this._state !== 'STARTED') return;
this._sendJson(conn, { cmd: 'ping' });
}, this._cfg.heartbeatIntervalMs);
this._armWatchdog();
}
_armWatchdog() {
this._clearTimer('hbWatchdog');
const conn = this._conn;
if (!conn) return;
const ms = this._cfg.heartbeatTimeoutMs;
this._timers.hbWatchdog = setTimeout(() => {
this._timers.hbWatchdog = null;
if (this._conn !== conn) return;
if (this._state !== 'CONNECTED' && this._state !== 'STARTED') return;
this._onTransportFailure(localError(90002, 'heartbeat timeout: no frame from the server for ' + ms + ' ms'));
}, ms);
}
_stopHeartbeat() {
this._clearTimer('hbInterval');
this._clearTimer('hbWatchdog');
}
_armResultTimer() {
this._clearTimer('result');
const conn = this._conn;
if (!conn) return;
const ms = this._cfg.resultTimeoutMs;
this._timers.result = setTimeout(() => {
this._timers.result = null;
if (this._conn !== conn || this._state !== 'ENDING') return;
this._onTransportFailure(localError(90007, 'no final result within ' + ms + ' ms after end()'));
}, ms);
}
_onErrorFrame(f) {
const code = toInt(f.code);
if (!code && this._probeOutstanding && /unknown cmd/i.test(String(f.message || ''))) {
this._probeOutstanding = false;
this._disableHeartbeat('the server does not know {"cmd":"ping"}');
return;
}
const err = fromWsErrorFrame(f, { idempotencyKey: this.idempotencyKey, op: this._op() });
if (isRetryable(err)) this._onTransportFailure(err);
else this._fail(err);
}
_onResultFrame(f) {
if (this._state !== 'STARTED' && this._state !== 'ENDING') {
this._ctx.logger.debug(TAG, this.id + ': result frame ignored in ' + this._state);
return;
}
const result = parseEvalResult(f);
result.idempotencyKey = this.idempotencyKey;
result.replayed = f.replayed === true;
result.localWarnings = this._localWarnings.slice();
result.attempts = this._totalReconnects + 1;
this._detachConnection('completed');
this._batch(() => {
this._setState('COMPLETED');
if (!this._cancelled) this._emit('onResult', [result]);
this._finishClosed(1000, 'completed');
});
}
_onPartialFrame(f) {
if (this._state !== 'STARTED' && this._state !== 'ENDING') return;
const partial = parsePartialResult(f);
if (partial.hasProgress) {
const last = this._lastProgress;
if (last && (partial.readUnits < last.readUnits || partial.matchedUnits < last.matchedUnits)) {
this._ctx.logger.debug(TAG, this.id + ': progress partial dropped: readUnits ' + partial.readUnits + ', matchedUnits ' +
partial.matchedUnits + ' are behind the delivered ' + last.readUnits + ', ' + last.matchedUnits);
return;
}
this._lastProgress = { readUnits: partial.readUnits, matchedUnits: partial.matchedUnits };
}
this._emit('onPartial', [partial]);
}
_onSocketClose(code, reason) {
this._lastClose = { code, reason };
if (!isActiveState(this._state) || this._state === 'RECONNECTING') return;
if (isFatalCloseCode(code)) {
const what = code === 1000 ? 'the server closed the session before the final result' : 'the server closed the session with code ' + code;
this._fail(new ProtocolViolationException(what + (reason ? ' (' + reason + ')' : ''), { code: 90005 }));
return;
}
this._onTransportFailure(localError(90001, 'connection closed with code ' + code + (reason ? ' (' + reason + ')' : '')));
}
_onTransportFailure(error) {
const s = this._state;
if (!isActiveState(s) || s === 'RECONNECTING' || s === 'IDLE') return;
const err = asYuguError(error);
if (!err.idempotencyKey) err.idempotencyKey = this.idempotencyKey;
if (!err.op) err.op = this._op();
const conn = this._conn;
if (this._inReconnect) this._emitEvent('onReconnect', [this.id, this._reconnectAttempt, false]);
this._detachConnection('reconnect');
const policy = this._cfg.bufferPolicy;
const rp = this._cfg.reconnect;
if (policy === 'FAIL' || !rp.enabled) {
this._fail(err);
return;
}
if (policy === 'DROP' && this._endSent) {
this._fail(err);
return;
}
if (policy === 'REPLAY' && this._overflow) {
this._fail(localError(90008, 'cannot reconnect: the replay buffer overflowed (' + err.message + ')', { cause: err }));
return;
}
if (this._reconnectAttempt >= rp.maxAttempts) {
this._fail(localError(90006, 'reconnect attempts exhausted (' + rp.maxAttempts + '): ' + err.message, { cause: err }));
return;
}
if (this._totalAttempts >= 3 * rp.maxAttempts) {
this._fail(localError(90006, 'reconnect limit of this session reached (' + 3 * rp.maxAttempts + ' in total): ' + err.message, { cause: err }));
return;
}
if (!this._inReconnect) {
this._inReconnect = true;
if (policy === 'DROP' && conn) {
this._droppedBytes += conn.txAudioBytes;
this._totalDroppedBytes += conn.txAudioBytes;
}
}
this._reconnectAttempt += 1;
this._totalAttempts += 1;
const attempt = this._reconnectAttempt;
const delay = computeBackoffDelay(attempt, rp, this._ctx.random);
this._ctx.logger.warn(TAG, this.id + ': reconnect ' + attempt + '/' + rp.maxAttempts + ' in ' + delay + ' ms: ' + describeError(err));
this._timers.reconnect = setTimeout(() => {
this._timers.reconnect = null;
if (this._state === 'RECONNECTING') this._connect();
}, delay);
this._batch(() => {
this._setState('RECONNECTING');
this._emit('onReconnecting', [attempt, delay, err]);
});
}
_detachConnection(reason) {
this._gen += 1;
const conn = this._conn;
this._conn = null;
this._clearTimer('connect');
this._clearTimer('result');
this._stopHeartbeat();
this._probeOutstanding = false;
this._hbConfirmed = false;
if (conn && conn.task) {
try {
conn.task.close({ code: 1000, reason: reason || '' });
} catch (e) {
// the socket may already be gone
}
}
}
_fail(error) {
if (!isActiveState(this._state)) return;
const err = asYuguError(error);
if (!err.idempotencyKey) err.idempotencyKey = this.idempotencyKey;
if (!err.op) err.op = this._op();
const lastClose = this._lastClose;
this._clearTimer('reconnect');
this._detachConnection('failed');
this._ctx.logger.info(TAG, this.id + ' failed: ' + describeError(err) + ' ' + err.message);
let code = 1000;
if (lastClose && lastClose.code) code = lastClose.code;
else if (err.category === 'NETWORK' || err.category === 'TIMEOUT') code = 1006;
this._batch(() => {
this._setState('FAILED');
if (!this._cancelled) this._emit('onError', [err]);
this._finishClosed(code, err.message);
});
}
_finishClosed(code, reason) {
this._setState('CLOSED');
this._emit('onClosed', [code, reason]);
this._closeQueued = true;
this._notify('closed', code, reason);
this._replay = [];
this._replayBytes = 0;
this._pending = [];
if (typeof this._ctx.onClosed === 'function') this._ctx.onClosed(this);
}
}
|