@lakutata/webrtc 3.1.1 → 3.1.5

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.
@@ -21,6 +21,7 @@ const MAX_REJOIN_DELAY = 10000;
21
21
  */
22
22
  function randomSession() {
23
23
  const bytes = new Uint8Array(12);
24
+ //node-compat: crypto (global) (the browsers; on Node.js, SignalingClient gives the session itself)
24
25
  globalThis.crypto.getRandomValues(bytes);
25
26
  return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
26
27
  }
@@ -60,7 +60,7 @@ export interface SocketIOSocketLike {
60
60
  off(event: string, listener?: (...args: any[]) => void): unknown;
61
61
  }
62
62
  /**
63
- * What the signaling needs of a WebSocket (`WebSocket` of the browsers, of Node.js 22 and of Bun) speaking JSON-RPC to
63
+ * What the signaling needs of a WebSocket (`WebSocket` of the browsers, of Node.js 22 and of Bun, of the package `ws`) speaking JSON-RPC to
64
64
  * the server; its client id is the `id` query parameter of its URL.
65
65
  */
66
66
  export interface WebSocketLike {
@@ -2,7 +2,7 @@ import { Exception } from '@lakutata/core';
2
2
  /**
3
3
  * Thrown when `SignalingClient` cannot use the transport of its URL: Socket.IO (an `http:` or `https:` URL) needs the
4
4
  * package `socket.io-client`, JSON-RPC (a `ws:` or `wss:` URL) needs the global `WebSocket` (Node.js 22 and later,
5
- * Bun). Its `errno` is `'E_SIGNALING_CONNECTION'`.
5
+ * Bun) or the package `ws`. Its `errno` is `'E_SIGNALING_CONNECTION'`.
6
6
  */
7
7
  export class SignalingConnectionException extends Exception {
8
8
  constructor() {
@@ -1396,8 +1396,10 @@ export class RTCPeerConnection extends EventTarget {
1396
1396
  return buildStatsReport(stats, this.transceiverManager.getStatsRootIds(selector));
1397
1397
  }
1398
1398
  async close() {
1399
- if (this.isClosed)
1400
- return;
1399
+ this.closing ??= this.closeOnce();
1400
+ return this.closing;
1401
+ }
1402
+ async closeOnce() {
1401
1403
  this.isClosed = true;
1402
1404
  this.pendingRemoteCandidates.length = 0;
1403
1405
  this.setSignalingState('closed');
@@ -7,7 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
- import { availableParallelism } from 'node:os';
10
+ import os from 'node:os';
11
11
  import { Worker } from 'node:worker_threads';
12
12
  import { Component, DTO } from '@lakutata/core';
13
13
  import { Configurable, Singleton } from '@lakutata/core/decorator/di';
@@ -20,6 +20,13 @@ const log = debug('webrtc/peer/threads');
20
20
  * How long a thread asked to stop has to close its connections, in milliseconds
21
21
  */
22
22
  const STOP_TIMEOUT = 5000;
23
+ /**
24
+ * The parallelism the system advises: os.availableParallelism (Node.js 18.14, 19.4), else the cores
25
+ */
26
+ function parallelism() {
27
+ //node-compat: os.availableParallelism
28
+ return typeof os.availableParallelism === 'function' ? os.availableParallelism() : Math.max(1, os.cpus().length);
29
+ }
23
30
  /**
24
31
  * Worker threads serving connections, to use more cores: a {@link PeerConnection} given the name of this component
25
32
  * (its `threads` option) runs its ICE, DTLS, SCTP and SRTP there, while its object, its events and the messages of its
@@ -63,7 +70,7 @@ let PeerThreads = class PeerThreads extends Component {
63
70
  async init() {
64
71
  if (!peerWorkerCode)
65
72
  throw new Error('The code of the worker threads of the connections is not built (scripts/workers.mjs)');
66
- const count = this.threads ?? Math.max(1, availableParallelism() - 1);
73
+ const count = this.threads ?? Math.max(1, parallelism() - 1);
67
74
  this.#slots = Array.from({ length: count }, (_, index) => ({ index: index, connections: new Map(), opening: 0 }));
68
75
  debugHandlerWatchers.add(this.#debugging);
69
76
  }