@signalwire/js 4.0.0-dev-20261125185248 → 4.0.0-dev-20261126145046

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/browser.mjs CHANGED
@@ -9020,6 +9020,15 @@ var Destroyable = class {
9020
9020
  const INVITE_VERSION = 1e3;
9021
9021
  const DEFAULT_ICE_CANDIDATE_TIMEOUT_MS = 600;
9022
9022
  const DEFAULT_ICE_GATHERING_TIMEOUT_MS = 6e3;
9023
+ /**
9024
+ * How long a first gather may find no candidate at all before it is given up.
9025
+ * Longer than the gathering timeout so a TURN allocation whose first Allocates
9026
+ * were lost can still answer (Chromium resends one at ~7.9 s), and shorter than
9027
+ * the dial's create timeout, so a dial still fails with the gathering error.
9028
+ */
9029
+ const DEFAULT_ICE_GATHERING_FAILURE_TIMEOUT_MS = 1e4;
9030
+ /** Prefix of an SDP line that carries an ICE candidate. */
9031
+ const SDP_CANDIDATE_LINE_PREFIX = "a=candidate:";
9023
9032
  const DEFAULT_RECONNECT_CALLS_TIMEOUT_MS = 300 * 1e3;
9024
9033
  const DEFAULT_CONNECTION_TIMEOUT_MS = 1e4;
9025
9034
  /**
@@ -9437,6 +9446,20 @@ var AuxiliaryLegCancelledError = class extends Error {
9437
9446
  this.name = "AuxiliaryLegCancelledError";
9438
9447
  }
9439
9448
  };
9449
+ /**
9450
+ * ICE gathering for a call's first offer or answer ended without a single
9451
+ * candidate, as when the device has no usable network, so there is nothing the
9452
+ * far end could connect to. Fatal for the main leg: the SDP is never sent, and
9453
+ * waiting would stall the call forever.
9454
+ */
9455
+ var IceGatheringError = class extends Error {
9456
+ constructor(sdpType, gatheringState) {
9457
+ super(`ICE gathering ended (${gatheringState}) with no candidates; the ${sdpType} cannot be sent`);
9458
+ this.sdpType = sdpType;
9459
+ this.gatheringState = gatheringState;
9460
+ this.name = "IceGatheringError";
9461
+ }
9462
+ };
9440
9463
  var MediaTrackError = class extends Error {
9441
9464
  constructor(operation, kind, originalError) {
9442
9465
  super(`Media track ${operation} failed for ${kind}`, { cause: originalError instanceof Error ? originalError : void 0 });
@@ -12634,6 +12657,7 @@ var AttachManager = class {
12634
12657
  const options = this.buildCallOptions(attachment);
12635
12658
  let succeeded = false;
12636
12659
  let refusedOnCredentials = false;
12660
+ let gatheredNothing = false;
12637
12661
  for (let attempt = 1; attempt <= 3; attempt++) try {
12638
12662
  await this.session.createOutboundCall(destination, {
12639
12663
  callId,
@@ -12644,6 +12668,7 @@ var AttachManager = class {
12644
12668
  break;
12645
12669
  } catch (error) {
12646
12670
  logger$26.warn(`[AttachManager] Reattach attempt ${attempt}/3 failed for call ${callId}:`, error);
12671
+ gatheredNothing = error instanceof Error && error.cause instanceof IceGatheringError;
12647
12672
  if (isRequesterValidationError(error)) {
12648
12673
  refusedOnCredentials = true;
12649
12674
  logger$26.warn(`[AttachManager] Reattach of ${callId} was refused on credentials; not retrying.`);
@@ -12651,7 +12676,7 @@ var AttachManager = class {
12651
12676
  }
12652
12677
  if (attempt < 3) await new Promise((r) => setTimeout(r, (attempt + 1) * 1e3));
12653
12678
  }
12654
- if (!succeeded) if (this.credentialRecovered() && !refusedOnCredentials) {
12679
+ if (!succeeded) if (this.credentialRecovered() && !refusedOnCredentials && !gatheredNothing) {
12655
12680
  logger$26.warn(`[AttachManager] Reattach of ${callId} was denied after a verified reauthentication, removing reference`);
12656
12681
  await this.detach({
12657
12682
  id: callId,
@@ -15119,6 +15144,17 @@ function extractMediaDirectionsFromSDP(sdp) {
15119
15144
  return result;
15120
15145
  }
15121
15146
  /**
15147
+ * Whether an SDP string carries at least one ICE candidate of any type.
15148
+ *
15149
+ * The end-of-candidates indication (`a=end-of-candidates`) is not a candidate.
15150
+ *
15151
+ * @param sdp - The SDP string to check
15152
+ * @returns true if any line is an `a=candidate:` line
15153
+ */
15154
+ function hasCandidates(sdp) {
15155
+ return sdp.split(/\r?\n/).some((line) => line.startsWith(SDP_CANDIDATE_LINE_PREFIX));
15156
+ }
15157
+ /**
15122
15158
  * Validates that an SDP string has at least one non-host ICE candidate
15123
15159
  * for each media section (m= line).
15124
15160
  *
@@ -15149,7 +15185,7 @@ function isValidLocalDescription(sdp) {
15149
15185
  for (const line of lines) if (line.startsWith("m=")) {
15150
15186
  currentSection += 1;
15151
15187
  mediaSectionsValidCandidates[currentSection] = 0;
15152
- } else if (line.startsWith("a=candidate:")) {
15188
+ } else if (line.startsWith(SDP_CANDIDATE_LINE_PREFIX)) {
15153
15189
  const typeMatch = /\styp\s+(host|srflx|prflx|relay)/.exec(line);
15154
15190
  if (typeMatch && typeMatch[1] !== "host") mediaSectionsValidCandidates[currentSection] += 1;
15155
15191
  }
@@ -15337,19 +15373,31 @@ var ICEGatheringController = class extends Destroyable {
15337
15373
  this.iceCandidateTimeout = options.iceCandidateTimeout ?? DEFAULT_ICE_CANDIDATE_TIMEOUT_MS;
15338
15374
  this.iceGatheringTimeout = options.iceGatheringTimeout ?? DEFAULT_ICE_GATHERING_TIMEOUT_MS;
15339
15375
  this.relayOnly = options.relayOnly ?? false;
15376
+ this.endGatheringWithoutCandidates = options.endGatheringWithoutCandidates ?? (() => false);
15340
15377
  this.setupEventListeners();
15341
15378
  this.subscribeTo(this.peerConnectionControllerNegotiating$.pipe((0, import_cjs$19.filter)((isNegotiating) => isNegotiating)), (isNegotiating) => {
15342
15379
  if (isNegotiating) {
15343
15380
  this.setupEventListeners();
15344
- this.iceGatheringTimer = setTimeout(() => {
15345
- if (this.peerConnection.iceGatheringState !== "complete") {
15346
- logger$21.warn("[ICEGatheringController] ICE gathering timeout, using current SDP");
15347
- this.handleICEGatheringTimeout();
15348
- }
15349
- }, this.iceGatheringTimeout);
15381
+ this.restartGatheringTimer();
15350
15382
  }
15351
15383
  });
15352
15384
  }
15385
+ /**
15386
+ * (Re)starts the gathering timeout and the give-up deadline from now. An
15387
+ * inbound call starts negotiating while it rings, so its answer calls this
15388
+ * to count both from when the answer's gathering actually starts.
15389
+ */
15390
+ restartGatheringTimer() {
15391
+ this.removeTimer("iceGatheringTimer");
15392
+ this.removeTimer("iceGatheringFailureTimer");
15393
+ this.iceGatheringTimer = setTimeout(() => {
15394
+ if (this.peerConnection.iceGatheringState !== "complete") {
15395
+ logger$21.warn("[ICEGatheringController] ICE gathering timeout, using current SDP");
15396
+ this.handleICEGatheringTimeout();
15397
+ }
15398
+ }, this.iceGatheringTimeout);
15399
+ this.iceGatheringFailureTimer = setTimeout(() => this.handleICEGatheringFailureTimeout(), Math.max(this.iceGatheringTimeout, DEFAULT_ICE_GATHERING_FAILURE_TIMEOUT_MS));
15400
+ }
15353
15401
  setupEventListeners() {
15354
15402
  this.peerConnection.removeEventListener("icecandidate", this.onicecandidateHandler);
15355
15403
  this.peerConnection.addEventListener("icecandidate", this.onicecandidateHandler);
@@ -15363,6 +15411,11 @@ var ICEGatheringController = class extends Destroyable {
15363
15411
  const sdp = this.peerConnection.localDescription?.sdp;
15364
15412
  return isValidLocalDescription(sdp ?? "");
15365
15413
  }
15414
+ /** A local description is set and carries no candidate of any type. */
15415
+ get hasNoCandidates() {
15416
+ const sdp = this.peerConnection.localDescription?.sdp;
15417
+ return sdp !== void 0 && !hasCandidates(sdp);
15418
+ }
15366
15419
  get isRelayOnly() {
15367
15420
  return this.relayOnly;
15368
15421
  }
@@ -15396,6 +15449,22 @@ var ICEGatheringController = class extends Destroyable {
15396
15449
  this.stopGathering();
15397
15450
  } else logger$21.debug("### ICE gathering timeout\n", this.peerConnection.localDescription?.sdp);
15398
15451
  }
15452
+ /**
15453
+ * Nothing at all was gathered by the give-up deadline. Without a candidate
15454
+ * the SDP is never sent, so end gathering when allowed rather than wait for
15455
+ * the browser. Any candidate, even a host one, keeps it gathering as before.
15456
+ * A gather that already ended has cleared this timer in stopGathering().
15457
+ */
15458
+ handleICEGatheringFailureTimeout() {
15459
+ this.removeTimer("iceGatheringFailureTimer");
15460
+ if (!this.hasNoCandidates || !this.endGatheringWithoutCandidates()) return;
15461
+ logger$21.warn("[ICEGatheringController] ICE gathering found no candidates, giving up");
15462
+ this._iceCandidatesState.next({
15463
+ state: "timeout",
15464
+ validSDP: false
15465
+ });
15466
+ this.stopGathering();
15467
+ }
15399
15468
  handleICECandidateTimeout() {
15400
15469
  if (this.iceCandidateTimer) this.removeTimer("iceCandidateTimer");
15401
15470
  logger$21.warn("[ICEGatheringController] ICE candidate timeout");
@@ -15428,6 +15497,7 @@ var ICEGatheringController = class extends Destroyable {
15428
15497
  clearAllTimers() {
15429
15498
  logger$21.debug("[ICEGatheringController] Clearing all timers");
15430
15499
  this.removeTimer("iceGatheringTimer");
15500
+ this.removeTimer("iceGatheringFailureTimer");
15431
15501
  this.removeTimer("iceCandidateTimer");
15432
15502
  }
15433
15503
  removeEventListeners() {
@@ -16079,8 +16149,9 @@ var RTCPeerConnectionController = class extends Destroyable {
16079
16149
  this.options = options;
16080
16150
  this.firstSDPExchangeCompleted = false;
16081
16151
  this.negotiationNeeded$ = this.createSubject();
16082
- this.localDescription$ = (0, import_cjs$16.defer)(() => (0, import_cjs$16.from)(this.init())).pipe((0, import_cjs$16.switchMap)(() => this.iceGatheringController.iceCandidatesState$.pipe((0, import_cjs$16.filter)((iceCandidateState) => !["new", "gathering"].includes(iceCandidateState)), (0, import_cjs$16.tap)(() => {
16152
+ this.localDescription$ = (0, import_cjs$16.defer)(() => (0, import_cjs$16.from)(this.init())).pipe((0, import_cjs$16.switchMap)(() => this.iceGatheringController.iceCandidatesState$.pipe((0, import_cjs$16.filter)((iceCandidateState) => iceCandidateState === "complete" || iceCandidateState === "timeout"), (0, import_cjs$16.tap)((iceCandidateState) => {
16083
16153
  this.negotiationEnded();
16154
+ this.failFirstExchangeWithoutCandidates(iceCandidateState);
16084
16155
  }), (0, import_cjs$16.filter)(() => this.shouldEmitLocalDescription), (0, import_cjs$16.map)(() => this.peerConnection?.localDescription), filterNull(), (0, import_cjs$16.tap)((desc) => {
16085
16156
  if (desc.type === "answer") this._type = "offer";
16086
16157
  }))), (0, import_cjs$16.shareReplay)(1), (0, import_cjs$16.takeUntil)(this.destroyed$));
@@ -16190,6 +16261,20 @@ var RTCPeerConnectionController = class extends Destroyable {
16190
16261
  if (!this._iceGatheringController) throw new DependencyError("ICEGatheringController is not initialized");
16191
16262
  return this._iceGatheringController;
16192
16263
  }
16264
+ /**
16265
+ * Gathering for the first offer or answer ended without a single candidate.
16266
+ * That SDP is never sent (see {@link shouldEmitLocalDescription}), and
16267
+ * nothing else would end the call, so report it. A later renegotiation is
16268
+ * left to call recovery.
16269
+ */
16270
+ failFirstExchangeWithoutCandidates(iceCandidateState) {
16271
+ const localDescription = this.peerConnection?.localDescription;
16272
+ if (this.firstSDPExchangeCompleted || !localDescription || hasCandidates(localDescription.sdp)) return;
16273
+ const error = new IceGatheringError(localDescription.type, iceCandidateState);
16274
+ logger$17.error(`[RTCPeerConnectionController] ICE gathering for the ${error.sdpType} ended (${error.gatheringState}) with no candidates`);
16275
+ logger$17.debug("[RTCPeerConnectionController] SDP without candidates:", localDescription.sdp);
16276
+ this._errors$.next(error);
16277
+ }
16193
16278
  get shouldEmitLocalDescription() {
16194
16279
  if (!this.peerConnection) return false;
16195
16280
  const { localDescription, signalingState } = this.peerConnection;
@@ -16434,7 +16519,8 @@ var RTCPeerConnectionController = class extends Destroyable {
16434
16519
  this._iceGatheringController = new ICEGatheringController(this.peerConnection, this.isNegotiating$, {
16435
16520
  iceCandidateTimeout: this.options.iceCandidateTimeout,
16436
16521
  iceGatheringTimeout: this.options.iceGatheringTimeout,
16437
- relayOnly: this.options.relayOnly
16522
+ relayOnly: this.options.relayOnly,
16523
+ endGatheringWithoutCandidates: () => !this.firstSDPExchangeCompleted
16438
16524
  });
16439
16525
  this.transceiverController = new TransceiverController({
16440
16526
  peerConnection: this.peerConnection,
@@ -16549,6 +16635,7 @@ var RTCPeerConnectionController = class extends Destroyable {
16549
16635
  }
16550
16636
  const { answerOptions } = this;
16551
16637
  logger$17.debug("[RTCPeerConnectionController] Creating inbound answer with options:", answerOptions);
16638
+ this.iceGatheringController.restartGatheringTimer();
16552
16639
  await this.createAnswer(answerOptions);
16553
16640
  }
16554
16641
  async handleOfferReceived() {
@@ -17765,7 +17852,8 @@ var WebRTCVertoManager = class extends VertoManager {
17765
17852
  this._rtcPeerConnectionsMap.set(rtcPeerConnController.id, rtcPeerConnController);
17766
17853
  this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values()));
17767
17854
  this.subscribeTo(rtcPeerConnController.errors$, (error) => {
17768
- this.reportLegError(error, rtcPeerConnController);
17855
+ const fatal = error instanceof IceGatheringError && this.isReattaching(rtcPeerConnController) ? false : void 0;
17856
+ this.reportLegError(error, rtcPeerConnController, { fatal });
17769
17857
  });
17770
17858
  if (options.initOffer) this.handleInboundAnswer(rtcPeerConnController);
17771
17859
  }
@@ -17903,6 +17991,10 @@ var WebRTCVertoManager = class extends VertoManager {
17903
17991
  }
17904
17992
  }
17905
17993
  }
17994
+ /** The leg's first exchange is a reattach of a call that already exists on the server. */
17995
+ isReattaching(rtcPeerConnectionController) {
17996
+ return rtcPeerConnectionController.propose === "main" && !rtcPeerConnectionController.firstSDPExchangeCompleted && this.webRtcCallSession.options.reattach === true;
17997
+ }
17906
17998
  dialogParams(rtcPeerConnectionController) {
17907
17999
  const memberId = rtcPeerConnectionController.memberId ?? this._selfId$.value ?? void 0;
17908
18000
  const attach = rtcPeerConnectionController.propose === "main" && !rtcPeerConnectionController.firstSDPExchangeCompleted && this.webRtcCallSession.options.reattach;
@@ -20109,6 +20201,7 @@ function inferCallErrorKind(error) {
20109
20201
  if (error instanceof JSONRPCError) return "signaling";
20110
20202
  if (error instanceof MediaTrackError) return "media";
20111
20203
  if (error instanceof MediaAccessError) return "media";
20204
+ if (error instanceof IceGatheringError) return "media";
20112
20205
  if (error instanceof WebSocketConnectionError || error instanceof TransportConnectionError) return "network";
20113
20206
  return "internal";
20114
20207
  }
@@ -21118,6 +21211,10 @@ var ClientSessionManager = class extends Destroyable {
21118
21211
  [`${callSession.id}`]: callSession,
21119
21212
  ...this._calls$.value
21120
21213
  });
21214
+ this.subscribeTo(callSession.errors$.pipe((0, import_cjs$7.filter)(({ error, leg }) => (leg ?? "main") === "main" && error instanceof IceGatheringError), (0, import_cjs$7.take)(1)), () => {
21215
+ logger$9.warn(`[Session] Reattach of ${callSession.id} gathered no candidates; dropping the local call`);
21216
+ callSession.destroy();
21217
+ });
21121
21218
  }
21122
21219
  async createOutboundCall(destination, options = {}) {
21123
21220
  const destinationURI = destination instanceof Address ? destination.defaultChannel : destination;
@@ -23260,7 +23357,7 @@ var SignalWire = class extends Destroyable {
23260
23357
  logger$1.warn("[SignalWire] Failed to initialize VisibilityController:", error);
23261
23358
  }
23262
23359
  try {
23263
- this._diagnosticsCollector = new DiagnosticsCollector({ sdkVersion: "4.0.0-dev-20261125185248" });
23360
+ this._diagnosticsCollector = new DiagnosticsCollector({ sdkVersion: "4.0.0-dev-20261126145046" });
23264
23361
  } catch (error) {
23265
23362
  logger$1.warn("[SignalWire] Failed to initialize DiagnosticsCollector:", error);
23266
23363
  }
@@ -23815,7 +23912,7 @@ var StaticCredentialProvider = class {
23815
23912
  /**
23816
23913
  * Library version from package.json, injected at build time.
23817
23914
  */
23818
- const version = "4.0.0-dev-20261125185248";
23915
+ const version = "4.0.0-dev-20261126145046";
23819
23916
  /**
23820
23917
  * Flag indicating the library has been loaded and is ready to use.
23821
23918
  * For UMD builds: `window.SignalWire.ready`
@@ -23837,7 +23934,7 @@ const ready = true;
23837
23934
  */
23838
23935
  const emitReadyEvent = () => {
23839
23936
  if (typeof window !== "undefined") {
23840
- const event = new CustomEvent("signalwire:js:ready", { detail: { version: "4.0.0-dev-20261125185248" } });
23937
+ const event = new CustomEvent("signalwire:js:ready", { detail: { version: "4.0.0-dev-20261126145046" } });
23841
23938
  window.dispatchEvent(event);
23842
23939
  }
23843
23940
  };
@@ -23861,5 +23958,5 @@ emitReadyEvent();
23861
23958
  if (typeof process === "undefined") globalThis.process = { env: { NODE_ENV: "production" } };
23862
23959
 
23863
23960
  //#endregion
23864
- export { Address, AuxiliaryLegCancelledError, AuxiliaryLegTimeoutError, CallCreateError, CallNotReadyError, ClientPreferences, CollectionFetchError, DPoPInitError, DeviceTokenError, EmbedTokenCredentialProvider, InvalidCredentialsError, MediaAccessError, MediaTrackError, MessageParseError, OverconstrainedFallbackError, Participant, ParticipantNotReadyError, PreflightError, RecoveryError, ScreenShareAlreadyActiveError, SelfCapabilities, SelfParticipant, SignalWire, StaticCredentialProvider, TokenRefreshError, UnexpectedError, User, VertoPongError, WebRTCCall, embeddableCall, getLogger, isSelfParticipant, ready, setDebugOptions, setLogLevel, setLogger, version };
23961
+ export { Address, AuxiliaryLegCancelledError, AuxiliaryLegTimeoutError, CallCreateError, CallNotReadyError, ClientPreferences, CollectionFetchError, DPoPInitError, DeviceTokenError, EmbedTokenCredentialProvider, IceGatheringError, InvalidCredentialsError, MediaAccessError, MediaTrackError, MessageParseError, OverconstrainedFallbackError, Participant, ParticipantNotReadyError, PreflightError, RecoveryError, ScreenShareAlreadyActiveError, SelfCapabilities, SelfParticipant, SignalWire, StaticCredentialProvider, TokenRefreshError, UnexpectedError, User, VertoPongError, WebRTCCall, embeddableCall, getLogger, isSelfParticipant, ready, setDebugOptions, setLogLevel, setLogger, version };
23865
23962
  //# sourceMappingURL=browser.mjs.map