@lakutata/webrtc 3.1.4 → 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.
@@ -7,18 +7,36 @@ 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 { randomUUID } from 'node:crypto';
10
+ import { randomBytes, randomUUID } from 'node:crypto';
11
11
  import { DTO, Provider } from '@lakutata/core';
12
12
  import { Autoload, Configurable, Transient } from '@lakutata/core/decorator/di';
13
13
  import { SignalingSession } from '../shared/SignalingSession.js';
14
14
  import { JSONRPCTransport, SocketIOTransport } from '../shared/Transports.js';
15
15
  import { SignalingConnectionException } from '../exceptions/SignalingConnectionException.js';
16
16
  import { PeerConnection } from './PeerConnection.js';
17
+ /**
18
+ * The WebSocket of JSON-RPC: the global one (the browsers, Node.js 22 and later, Bun, Electron), else the one of the
19
+ * package ws (Node.js 18 to 21, which have none)
20
+ */
21
+ async function webSocketClass() {
22
+ const global = globalThis.WebSocket;
23
+ if (global)
24
+ return global;
25
+ try {
26
+ const module = 'ws';
27
+ const loaded = await import(module);
28
+ return (loaded.WebSocket ?? loaded.default);
29
+ }
30
+ catch {
31
+ throw new SignalingConnectionException('JSON-RPC over WebSocket needs the global WebSocket (Node.js 22 and later) or the package ws: npm install ws');
32
+ }
33
+ }
17
34
  /**
18
35
  * A lakutata application as a member of the rooms of a signaling server of this package (`Signaling` and
19
36
  * `SignalingController`, on this application or another one): it joins rooms and connects to their other members,
20
37
  * browsers or applications, with {@link PeerConnection}s. Over Socket.IO (an `http:` or `https:` URL, with the package
21
- * `socket.io-client`) or JSON-RPC over WebSocket (a `ws:` or `wss:` URL, with the `WebSocket` of Node.js 22 and of Bun);
38
+ * `socket.io-client`) or JSON-RPC over WebSocket (a `ws:` or `wss:` URL, with the global `WebSocket` of Node.js 22 and
39
+ * later and of Bun, else the package `ws`);
22
40
  * it reconnects by itself and joins its rooms again, keeping the peer-to-peer connections.
23
41
  *
24
42
  * Get a new one with `getObject(SignalingClient, options)` (it is `@Transient`, and needs no registration); `close()`
@@ -57,6 +75,8 @@ let SignalingClient = class SignalingClient extends Provider {
57
75
  this.#clientId = this.id ?? randomUUID();
58
76
  const transport = await this.transport();
59
77
  this.#session = new SignalingSession(transport, {
78
+ //The random session of node:crypto: the global crypto of the session's default comes in Node.js 19
79
+ session: randomBytes(12).toString('hex'),
60
80
  iceTransportPolicy: this.iceTransportPolicy,
61
81
  createPeerConnection: (configuration) => this.getObject(PeerConnection, {
62
82
  ...configuration,
@@ -178,9 +198,7 @@ let SignalingClient = class SignalingClient extends Provider {
178
198
  async transport() {
179
199
  const url = new URL(this.url);
180
200
  if (url.protocol === 'ws:' || url.protocol === 'wss:') {
181
- const WebSocketClass = globalThis.WebSocket;
182
- if (!WebSocketClass)
183
- throw new SignalingConnectionException('JSON-RPC over WebSocket needs the global WebSocket (Node.js 22 and later)');
201
+ const WebSocketClass = await webSocketClass();
184
202
  url.searchParams.set('id', this.#clientId);
185
203
  const address = url.toString();
186
204
  return new JSONRPCTransport(() => new WebSocketClass(address), this.timeout);