@reactor-team/js-sdk 2.10.1 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -91,7 +91,7 @@ import { z } from "zod";
91
91
  // package.json
92
92
  var package_default = {
93
93
  name: "@reactor-team/js-sdk",
94
- version: "2.10.1",
94
+ version: "2.11.0",
95
95
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
96
96
  license: "Apache-2.0",
97
97
  repository: {
@@ -263,6 +263,16 @@ var WebRTCSdpOfferRequestSchema = z.object({
263
263
  var WebRTCSdpAnswerResponseSchema = z.object({
264
264
  sdp_answer: z.string()
265
265
  });
266
+ var IceCandidateSchema = z.object({
267
+ candidate: z.string(),
268
+ sdp_mid: z.string().optional(),
269
+ sdp_mline_index: z.number().optional()
270
+ });
271
+ var IceCandidatesRequestSchema = z.object({
272
+ candidates: z.array(IceCandidateSchema),
273
+ is_final: z.boolean(),
274
+ client_info: ClientInfoSchema.optional()
275
+ });
266
276
 
267
277
  // src/core/CoordinatorClient.ts
268
278
  var SESSION_POLL_INITIAL_BACKOFF_MS = 200;
@@ -1117,7 +1127,6 @@ function createOffer(pc) {
1117
1127
  yield pc.setLocalDescription(
1118
1128
  new RTCSessionDescription({ type: initial.type, sdp })
1119
1129
  );
1120
- yield waitForIceGathering(pc);
1121
1130
  const localDescription = pc.localDescription;
1122
1131
  if (!localDescription) {
1123
1132
  throw new Error("Failed to create local description");
@@ -1146,28 +1155,6 @@ function transformIceServers(response) {
1146
1155
  return rtcServer;
1147
1156
  });
1148
1157
  }
1149
- function waitForIceGathering(pc, timeoutMs = 5e3) {
1150
- return new Promise((resolve) => {
1151
- if (pc.iceGatheringState === "complete") {
1152
- resolve();
1153
- return;
1154
- }
1155
- const onGatheringStateChange = () => {
1156
- if (pc.iceGatheringState === "complete") {
1157
- pc.removeEventListener(
1158
- "icegatheringstatechange",
1159
- onGatheringStateChange
1160
- );
1161
- resolve();
1162
- }
1163
- };
1164
- pc.addEventListener("icegatheringstatechange", onGatheringStateChange);
1165
- setTimeout(() => {
1166
- pc.removeEventListener("icegatheringstatechange", onGatheringStateChange);
1167
- resolve();
1168
- }, timeoutMs);
1169
- });
1170
- }
1171
1158
  function sendMessage(channel, command, data, scope = "application", maxBytes = DEFAULT_MAX_MESSAGE_BYTES, uploads) {
1172
1159
  if (channel.readyState !== "open") {
1173
1160
  throw new Error(`Data channel not open: ${channel.readyState}`);
@@ -1282,6 +1269,7 @@ var INITIAL_BACKOFF_MS = 200;
1282
1269
  var MAX_BACKOFF_MS = 15e3;
1283
1270
  var BACKOFF_MULTIPLIER = 2;
1284
1271
  var DEFAULT_MAX_POLL_ATTEMPTS = 6;
1272
+ var ICE_CANDIDATE_BATCH_WINDOW_MS = 25;
1285
1273
  var WebRTCTransportClient = class {
1286
1274
  constructor(config) {
1287
1275
  this.eventListeners = /* @__PURE__ */ new Map();
@@ -1290,6 +1278,8 @@ var WebRTCTransportClient = class {
1290
1278
  this.publishedTracks = /* @__PURE__ */ new Map();
1291
1279
  this.peerConnected = false;
1292
1280
  this.dataChannelOpen = false;
1281
+ // Trickle ICE batching
1282
+ this.pendingIceCandidates = [];
1293
1283
  var _a, _b;
1294
1284
  this.baseUrl = config.baseUrl;
1295
1285
  this.sessionId = config.sessionId;
@@ -1424,6 +1414,70 @@ var WebRTCTransportClient = class {
1424
1414
  console.debug("[WebRTCTransport] SDP offer accepted (202)");
1425
1415
  });
1426
1416
  }
1417
+ sendIceCandidates(candidates, is_final) {
1418
+ return __async(this, null, function* () {
1419
+ console.debug(
1420
+ `[WebRTCTransport] Sending ICE candidates (count=${candidates.length}, is_final=${is_final})`
1421
+ );
1422
+ const requestBody = {
1423
+ candidates,
1424
+ is_final,
1425
+ client_info: {
1426
+ sdk_version: REACTOR_SDK_VERSION,
1427
+ sdk_type: REACTOR_SDK_TYPE
1428
+ }
1429
+ };
1430
+ const response = yield fetch(`${this.transportBaseUrl}/ice_candidates`, {
1431
+ method: "POST",
1432
+ headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1433
+ "Content-Type": "application/json"
1434
+ }),
1435
+ body: JSON.stringify(requestBody),
1436
+ signal: this.signal
1437
+ });
1438
+ yield this.checkVersionMismatch(response);
1439
+ if (response.status !== 202) {
1440
+ const errorText = yield response.text();
1441
+ throw new Error(
1442
+ `Failed to send ICE candidates: ${response.status} ${errorText}`
1443
+ );
1444
+ }
1445
+ console.debug("[WebRTCTransport] ICE candidates accepted (202)");
1446
+ return;
1447
+ });
1448
+ }
1449
+ /**
1450
+ * Drain the pending ICE candidates buffer and POST them as a single
1451
+ * batch. Called either by the debounce timer (collected candidates so
1452
+ * far) or directly by the gathering-complete handler (with
1453
+ * ``isFinal=true``).
1454
+ *
1455
+ * Errors are logged at debug level — trickle ICE is fire-and-forget,
1456
+ * and the connection can still succeed even if some candidate batches
1457
+ * are lost (host/srflx candidates from the other side are usually
1458
+ * sufficient).
1459
+ */
1460
+ flushPendingIceCandidates(isFinal) {
1461
+ if (this.iceCandidateFlushTimer !== void 0) {
1462
+ clearTimeout(this.iceCandidateFlushTimer);
1463
+ this.iceCandidateFlushTimer = void 0;
1464
+ }
1465
+ const batch = this.pendingIceCandidates;
1466
+ this.pendingIceCandidates = [];
1467
+ if (batch.length === 0 && !isFinal) {
1468
+ return;
1469
+ }
1470
+ this.sendIceCandidates(batch, isFinal).catch((err) => {
1471
+ console.debug("[WebRTCTransport] ICE candidate flush failed:", err);
1472
+ });
1473
+ }
1474
+ cancelPendingIceCandidates() {
1475
+ if (this.iceCandidateFlushTimer !== void 0) {
1476
+ clearTimeout(this.iceCandidateFlushTimer);
1477
+ this.iceCandidateFlushTimer = void 0;
1478
+ }
1479
+ this.pendingIceCandidates = [];
1480
+ }
1427
1481
  pollSdpAnswer() {
1428
1482
  return __async(this, null, function* () {
1429
1483
  console.debug("[WebRTCTransport] Polling for SDP answer...");
@@ -1493,6 +1547,7 @@ var WebRTCTransportClient = class {
1493
1547
  this.resetTransportTimings();
1494
1548
  this.stopPing();
1495
1549
  this.stopStatsPolling();
1550
+ this.cancelPendingIceCandidates();
1496
1551
  if (this.dataChannel) {
1497
1552
  this.dataChannel.close();
1498
1553
  this.dataChannel = void 0;
@@ -1555,6 +1610,7 @@ var WebRTCTransportClient = class {
1555
1610
  return __async(this, null, function* () {
1556
1611
  this.stopPing();
1557
1612
  this.stopStatsPolling();
1613
+ this.cancelPendingIceCandidates();
1558
1614
  for (const name of Array.from(this.publishedTracks.keys())) {
1559
1615
  yield this.unpublishTrack(name);
1560
1616
  }
@@ -1808,8 +1864,21 @@ var WebRTCTransportClient = class {
1808
1864
  this.emit("trackReceived", trackName, event.track, stream);
1809
1865
  };
1810
1866
  this.peerConnection.onicecandidate = (event) => {
1867
+ var _a, _b;
1811
1868
  if (event.candidate) {
1812
- console.debug("[WebRTCTransport] ICE candidate:", event.candidate);
1869
+ this.pendingIceCandidates.push({
1870
+ candidate: event.candidate.candidate,
1871
+ sdp_mid: (_a = event.candidate.sdpMid) != null ? _a : void 0,
1872
+ sdp_mline_index: (_b = event.candidate.sdpMLineIndex) != null ? _b : void 0
1873
+ });
1874
+ if (this.iceCandidateFlushTimer === void 0) {
1875
+ this.iceCandidateFlushTimer = setTimeout(
1876
+ () => this.flushPendingIceCandidates(false),
1877
+ ICE_CANDIDATE_BATCH_WINDOW_MS
1878
+ );
1879
+ }
1880
+ } else {
1881
+ this.flushPendingIceCandidates(true);
1813
1882
  }
1814
1883
  };
1815
1884
  this.peerConnection.onicecandidateerror = (event) => {