@observertc/observer-js 1.0.0-beta.10 → 1.0.0-beta.11

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
@@ -2181,7 +2181,8 @@ var ObservedClient = class extends EventEmitter2 {
2181
2181
  // public totalNumberOfIssues = 0;
2182
2182
  mediaDevices = [];
2183
2183
  issues = [];
2184
- _injections = {};
2184
+ _pendingInjections = {};
2185
+ _activeSample;
2185
2186
  closeTimer;
2186
2187
  get numberOfPeerConnections() {
2187
2188
  return this.observedPeerConnections.size;
@@ -2191,11 +2192,8 @@ var ObservedClient = class extends EventEmitter2 {
2191
2192
  }
2192
2193
  close() {
2193
2194
  if (this.closed) return;
2195
+ this._flushPendingInjections();
2194
2196
  this.closed = true;
2195
- this._injections.clientEvents?.forEach((clientEvent) => this._processClientEvent(clientEvent));
2196
- this._injections.clientIssues?.forEach((clientIssue) => this.addIssue(clientIssue));
2197
- this._injections.extensionStats?.forEach((extensionStat) => this.addExtensionStats(extensionStat));
2198
- this._injections.clientMetaItems?.forEach((clientMetaItem) => this.addMetadata(clientMetaItem));
2199
2197
  Array.from(this.observedPeerConnections.values()).forEach((peerConnection) => peerConnection.close());
2200
2198
  if (!this.leftAt) {
2201
2199
  this.leftAt = this.lastSampleTimestamp;
@@ -2218,7 +2216,6 @@ var ObservedClient = class extends EventEmitter2 {
2218
2216
  logger2.warn("Attempted to accept a sample on a closed client %s", this.clientId);
2219
2217
  return;
2220
2218
  }
2221
- this.sink?.write(sample);
2222
2219
  if (this.closeTimer) {
2223
2220
  clearTimeout(this.closeTimer);
2224
2221
  this.closeTimer = void 0;
@@ -2258,7 +2255,8 @@ var ObservedClient = class extends EventEmitter2 {
2258
2255
  this.usingTCP = false;
2259
2256
  this.currentMinRttInMs = void 0;
2260
2257
  this.currentMaxRttInMs = void 0;
2261
- this._mergeInjections(sample);
2258
+ this._mergePendingInjections(sample);
2259
+ this._activeSample = sample;
2262
2260
  const clientEventsPostBuffer = [];
2263
2261
  for (const clientEvent of sample.clientEvents ?? []) {
2264
2262
  this._processClientEvent(clientEvent, clientEventsPostBuffer);
@@ -2346,31 +2344,68 @@ var ObservedClient = class extends EventEmitter2 {
2346
2344
  this.close();
2347
2345
  }, this.settings.closeClientIfIdleForMs);
2348
2346
  }
2347
+ this._activeSample = void 0;
2348
+ try {
2349
+ this.sink?.write(sample);
2350
+ } catch (err) {
2351
+ logger2.warn("ClientSampleSink.write failed for client %s: %o", this.clientId, err);
2352
+ }
2349
2353
  }
2350
2354
  injectMetaData(metaData) {
2351
2355
  if (this.closed) return;
2352
- if (!this._injections.clientMetaItems) this._injections.clientMetaItems = [];
2353
- this._injections.clientMetaItems.push(metaData);
2356
+ if (this._activeSample) {
2357
+ if (!this._activeSample.clientMetaItems) this._activeSample.clientMetaItems = [];
2358
+ this._activeSample.clientMetaItems.push(metaData);
2359
+ this.addMetadata(metaData);
2360
+ return;
2361
+ }
2362
+ if (!this._pendingInjections.clientMetaItems) this._pendingInjections.clientMetaItems = [];
2363
+ this._pendingInjections.clientMetaItems.push(metaData);
2354
2364
  }
2355
2365
  injectEvent(event) {
2356
2366
  if (this.closed) return;
2357
- if (!this._injections.clientEvents) this._injections.clientEvents = [];
2358
- this._injections.clientEvents.push(event);
2367
+ if (this._activeSample) {
2368
+ if (!this._activeSample.clientEvents) this._activeSample.clientEvents = [];
2369
+ this._activeSample.clientEvents.push(event);
2370
+ this._processClientEvent(event);
2371
+ return;
2372
+ }
2373
+ if (!this._pendingInjections.clientEvents) this._pendingInjections.clientEvents = [];
2374
+ this._pendingInjections.clientEvents.push(event);
2359
2375
  }
2360
2376
  injectIssue(issue) {
2361
2377
  if (this.closed) return;
2362
- if (!this._injections.clientIssues) this._injections.clientIssues = [];
2363
- this._injections.clientIssues.push(issue);
2378
+ if (this._activeSample) {
2379
+ if (!this._activeSample.clientIssues) this._activeSample.clientIssues = [];
2380
+ this._activeSample.clientIssues.push(issue);
2381
+ this.addIssue(issue);
2382
+ ++this.deltaNumberOfIssues;
2383
+ return;
2384
+ }
2385
+ if (!this._pendingInjections.clientIssues) this._pendingInjections.clientIssues = [];
2386
+ this._pendingInjections.clientIssues.push(issue);
2364
2387
  }
2365
2388
  injectExtensionStat(stat) {
2366
2389
  if (this.closed) return;
2367
- if (!this._injections.extensionStats) this._injections.extensionStats = [];
2368
- this._injections.extensionStats.push(stat);
2390
+ if (this._activeSample) {
2391
+ if (!this._activeSample.extensionStats) this._activeSample.extensionStats = [];
2392
+ this._activeSample.extensionStats.push(stat);
2393
+ this.addExtensionStats(stat);
2394
+ return;
2395
+ }
2396
+ if (!this._pendingInjections.extensionStats) this._pendingInjections.extensionStats = [];
2397
+ this._pendingInjections.extensionStats.push(stat);
2369
2398
  }
2370
- injectAttachment(key, value) {
2399
+ injectAttachment(attachments) {
2371
2400
  if (this.closed) return;
2372
- if (!this._injections.attachments) this._injections.attachments = {};
2373
- this._injections.attachments[key] = value;
2401
+ if (this._activeSample) {
2402
+ if (!this._activeSample.attachments) this._activeSample.attachments = {};
2403
+ Object.assign(this._activeSample.attachments, attachments);
2404
+ this.attachments = this._activeSample.attachments;
2405
+ return;
2406
+ }
2407
+ if (!this._pendingInjections.attachments) this._pendingInjections.attachments = {};
2408
+ Object.assign(this._pendingInjections.attachments, attachments);
2374
2409
  }
2375
2410
  addMetadata(metadata) {
2376
2411
  if (this.closed) return;
@@ -2632,35 +2667,53 @@ var ObservedClient = class extends EventEmitter2 {
2632
2667
  observedPeerConnection.accept(sample, context);
2633
2668
  return observedPeerConnection;
2634
2669
  }
2635
- _mergeInjections(sample) {
2670
+ _mergePendingInjections(sample) {
2636
2671
  if (this.closed) return sample;
2637
- if (this._injections.clientEvents) {
2672
+ if (this._pendingInjections.clientEvents) {
2638
2673
  if (!sample.clientEvents) sample.clientEvents = [];
2639
- sample.clientEvents.push(...this._injections.clientEvents);
2640
- this._injections.clientEvents = void 0;
2674
+ sample.clientEvents.push(...this._pendingInjections.clientEvents);
2675
+ this._pendingInjections.clientEvents = void 0;
2641
2676
  }
2642
- if (this._injections.clientIssues) {
2677
+ if (this._pendingInjections.clientIssues) {
2643
2678
  if (!sample.clientIssues) sample.clientIssues = [];
2644
- sample.clientIssues.push(...this._injections.clientIssues);
2645
- this._injections.clientIssues = void 0;
2679
+ sample.clientIssues.push(...this._pendingInjections.clientIssues);
2680
+ this._pendingInjections.clientIssues = void 0;
2646
2681
  }
2647
- if (this._injections.extensionStats) {
2682
+ if (this._pendingInjections.extensionStats) {
2648
2683
  if (!sample.extensionStats) sample.extensionStats = [];
2649
- sample.extensionStats.push(...this._injections.extensionStats);
2650
- this._injections.extensionStats = void 0;
2684
+ sample.extensionStats.push(...this._pendingInjections.extensionStats);
2685
+ this._pendingInjections.extensionStats = void 0;
2651
2686
  }
2652
- if (this._injections.attachments) {
2687
+ if (this._pendingInjections.attachments) {
2653
2688
  if (!sample.attachments) sample.attachments = {};
2654
- Object.assign(sample.attachments, this._injections.attachments);
2655
- this._injections.attachments = void 0;
2689
+ Object.assign(sample.attachments, this._pendingInjections.attachments);
2690
+ this._pendingInjections.attachments = void 0;
2656
2691
  }
2657
- if (this._injections.clientMetaItems) {
2692
+ if (this._pendingInjections.clientMetaItems) {
2658
2693
  if (!sample.clientMetaItems) sample.clientMetaItems = [];
2659
- sample.clientMetaItems.push(...this._injections.clientMetaItems);
2660
- this._injections.clientMetaItems = void 0;
2694
+ sample.clientMetaItems.push(...this._pendingInjections.clientMetaItems);
2695
+ this._pendingInjections.clientMetaItems = void 0;
2661
2696
  }
2662
2697
  return sample;
2663
2698
  }
2699
+ _flushPendingInjections() {
2700
+ const hasPending = Boolean(
2701
+ this._pendingInjections.clientEvents || this._pendingInjections.clientIssues || this._pendingInjections.extensionStats || this._pendingInjections.clientMetaItems || this._pendingInjections.attachments
2702
+ );
2703
+ if (!hasPending) return;
2704
+ const finalSample = { timestamp: Date.now() };
2705
+ this._mergePendingInjections(finalSample);
2706
+ for (const clientEvent of finalSample.clientEvents ?? []) this._processClientEvent(clientEvent);
2707
+ for (const metaData of finalSample.clientMetaItems ?? []) this.addMetadata(metaData);
2708
+ for (const issue of finalSample.clientIssues ?? []) this.addIssue(issue);
2709
+ for (const extensionStat of finalSample.extensionStats ?? []) this.addExtensionStats(extensionStat);
2710
+ if (finalSample.attachments) this.attachments = { ...this.attachments, ...finalSample.attachments };
2711
+ try {
2712
+ this.sink?.write(finalSample);
2713
+ } catch (err) {
2714
+ logger2.warn("ClientSampleSink.write failed during injection flush for client %s: %o", this.clientId, err);
2715
+ }
2716
+ }
2664
2717
  /** Emit an Observer-bus event scoped to this client (or a peer connection under it). */
2665
2718
  _notify(type, ...args) {
2666
2719
  this.call.observer.emit(type, ...args);
@@ -3660,17 +3713,19 @@ var Observer = class extends EventEmitter6 {
3660
3713
  observedMediasoupRouter,
3661
3714
  observer: this
3662
3715
  };
3663
- const onPeerConnectionAdded = (observedPeerConnectionScope) => {
3664
- if (!observedMediasoupRouter.webrtcTransportIds.has(observedPeerConnectionScope.observedPeerConnection.peerConnectionId)) return;
3665
- this.emit("mediasoup-router-matched-with-peer-connection", {
3666
- ...observedMediasoupRouterScope,
3667
- ...observedPeerConnectionScope
3668
- });
3669
- };
3670
- const stopMatching = () => this.off("peer-connection-added", onPeerConnectionAdded);
3671
- this.on("peer-connection-added", onPeerConnectionAdded);
3672
- this.once("observer-closed", stopMatching);
3673
- observedMediasoupRouter.once("close", stopMatching);
3716
+ if (settings.matchPeerConnectionByWebRtcTransportId) {
3717
+ const onPeerConnectionAdded = (observedPeerConnectionScope) => {
3718
+ if (!observedMediasoupRouter.webrtcTransportIds.has(observedPeerConnectionScope.observedPeerConnection.peerConnectionId)) return;
3719
+ this.emit("mediasoup-router-matched-with-peer-connection", {
3720
+ ...observedMediasoupRouterScope,
3721
+ ...observedPeerConnectionScope
3722
+ });
3723
+ };
3724
+ const stopMatching = () => this.off("peer-connection-added", onPeerConnectionAdded);
3725
+ this.on("peer-connection-added", onPeerConnectionAdded);
3726
+ this.once("observer-closed", stopMatching);
3727
+ observedMediasoupRouter.once("close", stopMatching);
3728
+ }
3674
3729
  observedMediasoupRouter.once("close", () => {
3675
3730
  this.observedMediasoupRouters.delete(observedMediasoupRouter.id);
3676
3731
  this.emit("mediasoup-router-removed", observedMediasoupRouterScope);