@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.js CHANGED
@@ -153,7 +153,7 @@ var import_zod = require("zod");
153
153
  // package.json
154
154
  var package_default = {
155
155
  name: "@reactor-team/js-sdk",
156
- version: "2.10.1",
156
+ version: "2.11.0",
157
157
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
158
158
  license: "Apache-2.0",
159
159
  repository: {
@@ -325,6 +325,16 @@ var WebRTCSdpOfferRequestSchema = import_zod.z.object({
325
325
  var WebRTCSdpAnswerResponseSchema = import_zod.z.object({
326
326
  sdp_answer: import_zod.z.string()
327
327
  });
328
+ var IceCandidateSchema = import_zod.z.object({
329
+ candidate: import_zod.z.string(),
330
+ sdp_mid: import_zod.z.string().optional(),
331
+ sdp_mline_index: import_zod.z.number().optional()
332
+ });
333
+ var IceCandidatesRequestSchema = import_zod.z.object({
334
+ candidates: import_zod.z.array(IceCandidateSchema),
335
+ is_final: import_zod.z.boolean(),
336
+ client_info: ClientInfoSchema.optional()
337
+ });
328
338
 
329
339
  // src/core/CoordinatorClient.ts
330
340
  var SESSION_POLL_INITIAL_BACKOFF_MS = 200;
@@ -1179,7 +1189,6 @@ function createOffer(pc) {
1179
1189
  yield pc.setLocalDescription(
1180
1190
  new RTCSessionDescription({ type: initial.type, sdp })
1181
1191
  );
1182
- yield waitForIceGathering(pc);
1183
1192
  const localDescription = pc.localDescription;
1184
1193
  if (!localDescription) {
1185
1194
  throw new Error("Failed to create local description");
@@ -1208,28 +1217,6 @@ function transformIceServers(response) {
1208
1217
  return rtcServer;
1209
1218
  });
1210
1219
  }
1211
- function waitForIceGathering(pc, timeoutMs = 5e3) {
1212
- return new Promise((resolve) => {
1213
- if (pc.iceGatheringState === "complete") {
1214
- resolve();
1215
- return;
1216
- }
1217
- const onGatheringStateChange = () => {
1218
- if (pc.iceGatheringState === "complete") {
1219
- pc.removeEventListener(
1220
- "icegatheringstatechange",
1221
- onGatheringStateChange
1222
- );
1223
- resolve();
1224
- }
1225
- };
1226
- pc.addEventListener("icegatheringstatechange", onGatheringStateChange);
1227
- setTimeout(() => {
1228
- pc.removeEventListener("icegatheringstatechange", onGatheringStateChange);
1229
- resolve();
1230
- }, timeoutMs);
1231
- });
1232
- }
1233
1220
  function sendMessage(channel, command, data, scope = "application", maxBytes = DEFAULT_MAX_MESSAGE_BYTES, uploads) {
1234
1221
  if (channel.readyState !== "open") {
1235
1222
  throw new Error(`Data channel not open: ${channel.readyState}`);
@@ -1344,6 +1331,7 @@ var INITIAL_BACKOFF_MS = 200;
1344
1331
  var MAX_BACKOFF_MS = 15e3;
1345
1332
  var BACKOFF_MULTIPLIER = 2;
1346
1333
  var DEFAULT_MAX_POLL_ATTEMPTS = 6;
1334
+ var ICE_CANDIDATE_BATCH_WINDOW_MS = 25;
1347
1335
  var WebRTCTransportClient = class {
1348
1336
  constructor(config) {
1349
1337
  this.eventListeners = /* @__PURE__ */ new Map();
@@ -1352,6 +1340,8 @@ var WebRTCTransportClient = class {
1352
1340
  this.publishedTracks = /* @__PURE__ */ new Map();
1353
1341
  this.peerConnected = false;
1354
1342
  this.dataChannelOpen = false;
1343
+ // Trickle ICE batching
1344
+ this.pendingIceCandidates = [];
1355
1345
  var _a, _b;
1356
1346
  this.baseUrl = config.baseUrl;
1357
1347
  this.sessionId = config.sessionId;
@@ -1486,6 +1476,70 @@ var WebRTCTransportClient = class {
1486
1476
  console.debug("[WebRTCTransport] SDP offer accepted (202)");
1487
1477
  });
1488
1478
  }
1479
+ sendIceCandidates(candidates, is_final) {
1480
+ return __async(this, null, function* () {
1481
+ console.debug(
1482
+ `[WebRTCTransport] Sending ICE candidates (count=${candidates.length}, is_final=${is_final})`
1483
+ );
1484
+ const requestBody = {
1485
+ candidates,
1486
+ is_final,
1487
+ client_info: {
1488
+ sdk_version: REACTOR_SDK_VERSION,
1489
+ sdk_type: REACTOR_SDK_TYPE
1490
+ }
1491
+ };
1492
+ const response = yield fetch(`${this.transportBaseUrl}/ice_candidates`, {
1493
+ method: "POST",
1494
+ headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1495
+ "Content-Type": "application/json"
1496
+ }),
1497
+ body: JSON.stringify(requestBody),
1498
+ signal: this.signal
1499
+ });
1500
+ yield this.checkVersionMismatch(response);
1501
+ if (response.status !== 202) {
1502
+ const errorText = yield response.text();
1503
+ throw new Error(
1504
+ `Failed to send ICE candidates: ${response.status} ${errorText}`
1505
+ );
1506
+ }
1507
+ console.debug("[WebRTCTransport] ICE candidates accepted (202)");
1508
+ return;
1509
+ });
1510
+ }
1511
+ /**
1512
+ * Drain the pending ICE candidates buffer and POST them as a single
1513
+ * batch. Called either by the debounce timer (collected candidates so
1514
+ * far) or directly by the gathering-complete handler (with
1515
+ * ``isFinal=true``).
1516
+ *
1517
+ * Errors are logged at debug level — trickle ICE is fire-and-forget,
1518
+ * and the connection can still succeed even if some candidate batches
1519
+ * are lost (host/srflx candidates from the other side are usually
1520
+ * sufficient).
1521
+ */
1522
+ flushPendingIceCandidates(isFinal) {
1523
+ if (this.iceCandidateFlushTimer !== void 0) {
1524
+ clearTimeout(this.iceCandidateFlushTimer);
1525
+ this.iceCandidateFlushTimer = void 0;
1526
+ }
1527
+ const batch = this.pendingIceCandidates;
1528
+ this.pendingIceCandidates = [];
1529
+ if (batch.length === 0 && !isFinal) {
1530
+ return;
1531
+ }
1532
+ this.sendIceCandidates(batch, isFinal).catch((err) => {
1533
+ console.debug("[WebRTCTransport] ICE candidate flush failed:", err);
1534
+ });
1535
+ }
1536
+ cancelPendingIceCandidates() {
1537
+ if (this.iceCandidateFlushTimer !== void 0) {
1538
+ clearTimeout(this.iceCandidateFlushTimer);
1539
+ this.iceCandidateFlushTimer = void 0;
1540
+ }
1541
+ this.pendingIceCandidates = [];
1542
+ }
1489
1543
  pollSdpAnswer() {
1490
1544
  return __async(this, null, function* () {
1491
1545
  console.debug("[WebRTCTransport] Polling for SDP answer...");
@@ -1555,6 +1609,7 @@ var WebRTCTransportClient = class {
1555
1609
  this.resetTransportTimings();
1556
1610
  this.stopPing();
1557
1611
  this.stopStatsPolling();
1612
+ this.cancelPendingIceCandidates();
1558
1613
  if (this.dataChannel) {
1559
1614
  this.dataChannel.close();
1560
1615
  this.dataChannel = void 0;
@@ -1617,6 +1672,7 @@ var WebRTCTransportClient = class {
1617
1672
  return __async(this, null, function* () {
1618
1673
  this.stopPing();
1619
1674
  this.stopStatsPolling();
1675
+ this.cancelPendingIceCandidates();
1620
1676
  for (const name of Array.from(this.publishedTracks.keys())) {
1621
1677
  yield this.unpublishTrack(name);
1622
1678
  }
@@ -1870,8 +1926,21 @@ var WebRTCTransportClient = class {
1870
1926
  this.emit("trackReceived", trackName, event.track, stream);
1871
1927
  };
1872
1928
  this.peerConnection.onicecandidate = (event) => {
1929
+ var _a, _b;
1873
1930
  if (event.candidate) {
1874
- console.debug("[WebRTCTransport] ICE candidate:", event.candidate);
1931
+ this.pendingIceCandidates.push({
1932
+ candidate: event.candidate.candidate,
1933
+ sdp_mid: (_a = event.candidate.sdpMid) != null ? _a : void 0,
1934
+ sdp_mline_index: (_b = event.candidate.sdpMLineIndex) != null ? _b : void 0
1935
+ });
1936
+ if (this.iceCandidateFlushTimer === void 0) {
1937
+ this.iceCandidateFlushTimer = setTimeout(
1938
+ () => this.flushPendingIceCandidates(false),
1939
+ ICE_CANDIDATE_BATCH_WINDOW_MS
1940
+ );
1941
+ }
1942
+ } else {
1943
+ this.flushPendingIceCandidates(true);
1875
1944
  }
1876
1945
  };
1877
1946
  this.peerConnection.onicecandidateerror = (event) => {