@lakutata/webrtc 3.1.4 → 3.1.6

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.
Files changed (31) hide show
  1. package/README.md +2 -1
  2. package/README.zh-CN.md +1 -1
  3. package/dist/cjs/exceptions/SignalingConnectionException.d.ts +1 -1
  4. package/dist/cjs/exceptions/SignalingConnectionException.js +1 -1
  5. package/dist/cjs/lib/ice/dns/lookup.js +11 -3
  6. package/dist/cjs/lib/peer/peerConnection.d.ts +2 -0
  7. package/dist/cjs/lib/peer/peerConnection.js +4 -2
  8. package/dist/cjs/peer/PeerThreads.js +15 -2
  9. package/dist/cjs/peer/PeerWorkerCode.js +1 -1
  10. package/dist/cjs/peer/SignalingClient.d.ts +2 -1
  11. package/dist/cjs/peer/SignalingClient.js +22 -4
  12. package/dist/cjs/sfu/SFUWorkerCode.js +1 -1
  13. package/dist/cjs/sfu/SFUWorkers.js +3 -1
  14. package/dist/cjs/shared/SignalingSession.js +1 -0
  15. package/dist/cjs/shared/Transports.d.ts +1 -1
  16. package/dist/cjs/turn/TURNWorkers.js +3 -1
  17. package/dist/esm/exceptions/SignalingConnectionException.js +1 -1
  18. package/dist/esm/lib/ice/dns/lookup.js +11 -3
  19. package/dist/esm/lib/peer/peerConnection.js +4 -2
  20. package/dist/esm/peer/PeerThreads.js +12 -2
  21. package/dist/esm/peer/PeerWorkerCode.js +1 -1
  22. package/dist/esm/peer/SignalingClient.js +23 -5
  23. package/dist/esm/sfu/SFUWorkerCode.js +1 -1
  24. package/dist/esm/sfu/SFUWorkers.js +3 -1
  25. package/dist/esm/shared/SignalingSession.js +1 -0
  26. package/dist/esm/turn/TURNWorkers.js +3 -1
  27. package/dist/types/exceptions/SignalingConnectionException.d.ts +1 -1
  28. package/dist/types/lib/peer/peerConnection.d.ts +2 -0
  29. package/dist/types/peer/SignalingClient.d.ts +2 -1
  30. package/dist/types/shared/Transports.d.ts +1 -1
  31. package/package.json +9 -5
@@ -449,7 +449,9 @@ class SFUWorkers {
449
449
  //Running once it says so: a thread failing to start is not started again
450
450
  slot.ran = false;
451
451
  const resourceLimits = this.#host.resourceLimits;
452
- const worker = new node_worker_threads_1.Worker(SFUWorkerCode_js_1.sfuWorkerCode, { eval: true, workerData: this.#data[slot.index], ...resourceLimits ? { resourceLimits: resourceLimits } : {} });
452
+ //Without the options of the command line of the process, which a worker inherits: with --input-type=module, its code
453
+ //(CommonJS) would run as an ES module
454
+ const worker = new node_worker_threads_1.Worker(SFUWorkerCode_js_1.sfuWorkerCode, { eval: true, execArgv: [], workerData: this.#data[slot.index], ...resourceLimits ? { resourceLimits: resourceLimits } : {} });
453
455
  slot.worker = worker;
454
456
  slot.channel = new SFUWorkerChannel_js_1.SFUWorkerChannel(worker, (message) => this.#handle(slot, message));
455
457
  slot.ready = new Promise((resolve, reject) => {
@@ -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 {
@@ -165,7 +165,9 @@ class TURNWorkers {
165
165
  port: this.#port,
166
166
  ...data.options.tls && this.#tlsPort !== undefined ? { tls: { ...data.options.tls, port: this.#tlsPort } } : {}
167
167
  };
168
- const worker = new node_worker_threads_1.Worker(TURNWorkerCode_js_1.turnWorkerCode, { eval: true, workerData: { ...data, options: options } });
168
+ //Without the options of the command line of the process, which a worker inherits: with --input-type=module, its code
169
+ //(CommonJS) would run as an ES module
170
+ const worker = new node_worker_threads_1.Worker(TURNWorkerCode_js_1.turnWorkerCode, { eval: true, execArgv: [], workerData: { ...data, options: options } });
169
171
  slot.worker = worker;
170
172
  if ((0, log_js_1.hasDebugHandler)())
171
173
  worker.postMessage({ type: 'debug', enabled: true });
@@ -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() {
@@ -40,8 +40,13 @@ export class DnsLookup {
40
40
  constructor() {
41
41
  this.cache = new Map();
42
42
  const lookupWorkerFunction = () => {
43
- const worker_thread = global.require('worker_threads');
44
- const { lookup } = global.require('dns');
43
+ //The modules of Node.js: process.getBuiltinModule where there is one (Bun has no global.require), else the
44
+ //require of the script (Node.js before 20.16). No require is named in the function, whose text runs in the
45
+ //worker: a bundler would rewrite it
46
+ //node-compat: process.getBuiltinModule (checked first, global.require where it is missing)
47
+ const load = (id) => typeof process.getBuiltinModule === 'function' ? process.getBuiltinModule(id) : global.require(id);
48
+ const worker_thread = load('worker_threads');
49
+ const { lookup } = load('dns');
45
50
  const dnsLookup = (host) => lookup(host, (err, address, family) => {
46
51
  const res = {
47
52
  err: err?.message,
@@ -59,7 +64,10 @@ export class DnsLookup {
59
64
  };
60
65
  const lookupEval = `(${lookupWorkerFunction})()`;
61
66
  this.thread = new worker_thread.Worker(lookupEval, {
62
- eval: true
67
+ eval: true,
68
+ //Without the options of the command line of the process, which a worker inherits: with --input-type=module, its code
69
+ //(CommonJS) would run as an ES module
70
+ execArgv: []
63
71
  });
64
72
  this.thread.setMaxListeners(100);
65
73
  }
@@ -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
  }
@@ -162,6 +169,9 @@ let PeerThreads = class PeerThreads extends Component {
162
169
  return slot.ready;
163
170
  const worker = new Worker(peerWorkerCode, {
164
171
  eval: true,
172
+ //Without the options of the command line of the process, which a worker inherits: with --input-type=module, its code
173
+ //(CommonJS) would run as an ES module
174
+ execArgv: [],
165
175
  workerData: { thread: slot.index },
166
176
  ...this.resourceLimits ? { resourceLimits: this.resourceLimits } : {}
167
177
  });