node-datachannel 0.21.0 → 0.23.0

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 (47) hide show
  1. package/.eslintignore +3 -1
  2. package/API.md +1 -0
  3. package/CMakeLists.txt +6 -1
  4. package/README.md +2 -2
  5. package/dist/cjs/lib/datachannel-stream.cjs.map +1 -1
  6. package/dist/cjs/lib/index.cjs.map +1 -1
  7. package/dist/cjs/lib/node-datachannel.cjs +3 -0
  8. package/dist/cjs/lib/node-datachannel.cjs.map +1 -1
  9. package/dist/cjs/lib/websocket-server.cjs +1 -0
  10. package/dist/cjs/lib/websocket-server.cjs.map +1 -1
  11. package/dist/cjs/polyfill/RTCDataChannel.cjs +5 -3
  12. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
  13. package/dist/cjs/polyfill/RTCIceCandidate.cjs +0 -2
  14. package/dist/cjs/polyfill/RTCIceCandidate.cjs.map +1 -1
  15. package/dist/cjs/polyfill/RTCPeerConnection.cjs +7 -5
  16. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
  17. package/dist/esm/lib/datachannel-stream.mjs.map +1 -1
  18. package/dist/esm/lib/index.mjs.map +1 -1
  19. package/dist/esm/lib/node-datachannel.mjs +3 -0
  20. package/dist/esm/lib/node-datachannel.mjs.map +1 -1
  21. package/dist/esm/lib/websocket-server.mjs +1 -0
  22. package/dist/esm/lib/websocket-server.mjs.map +1 -1
  23. package/dist/esm/polyfill/RTCDataChannel.mjs +5 -3
  24. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
  25. package/dist/esm/polyfill/RTCIceCandidate.mjs +0 -2
  26. package/dist/esm/polyfill/RTCIceCandidate.mjs.map +1 -1
  27. package/dist/esm/polyfill/RTCPeerConnection.mjs +7 -5
  28. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
  29. package/dist/types/lib/index.d.ts +5 -5
  30. package/dist/types/lib/types.d.ts +2 -1
  31. package/package.json +7 -8
  32. package/src/cpp/data-channel-wrapper.cpp +7 -3
  33. package/src/cpp/media-audio-wrapper.cpp +7 -3
  34. package/src/cpp/media-rtcpreceivingsession-wrapper.cpp +7 -3
  35. package/src/cpp/media-track-wrapper.cpp +7 -3
  36. package/src/cpp/media-video-wrapper.cpp +7 -3
  37. package/src/cpp/peer-connection-wrapper.cpp +12 -3
  38. package/src/cpp/web-socket-server-wrapper.cpp +7 -3
  39. package/src/cpp/web-socket-wrapper.cpp +7 -3
  40. package/src/lib/datachannel-stream.ts +1 -1
  41. package/src/lib/index.ts +3 -3
  42. package/src/lib/node-datachannel.ts +5 -0
  43. package/src/lib/types.ts +2 -1
  44. package/src/lib/websocket-server.ts +1 -0
  45. package/src/polyfill/RTCDataChannel.ts +5 -3
  46. package/src/polyfill/RTCIceCandidate.ts +0 -1
  47. package/src/polyfill/RTCPeerConnection.ts +8 -5
@@ -9,7 +9,7 @@
9
9
  #include <cctype>
10
10
  #include <sstream>
11
11
 
12
- Napi::FunctionReference PeerConnectionWrapper::constructor;
12
+ Napi::FunctionReference PeerConnectionWrapper::constructor = Napi::FunctionReference();
13
13
  std::unordered_set<PeerConnectionWrapper *> PeerConnectionWrapper::instances;
14
14
 
15
15
  void PeerConnectionWrapper::CloseAll()
@@ -65,8 +65,12 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
65
65
  InstanceMethod("maxMessageSize", &PeerConnectionWrapper::maxMessageSize),
66
66
  });
67
67
 
68
- constructor = Napi::Persistent(func);
69
- constructor.SuppressDestruct();
68
+ // If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
69
+ if(constructor.IsEmpty())
70
+ {
71
+ constructor = Napi::Persistent(func);
72
+ constructor.SuppressDestruct();
73
+ }
70
74
 
71
75
  exports.Set("PeerConnection", func);
72
76
  return exports;
@@ -242,6 +246,11 @@ PeerConnectionWrapper::PeerConnectionWrapper(const Napi::CallbackInfo &info) : N
242
246
  }
243
247
  }
244
248
 
249
+ // Allow skipping fingerprint validation
250
+ if (config.Get("disableFingerprintVerification").IsBoolean()) {
251
+ rtcConfig.disableFingerprintVerification = config.Get("disableFingerprintVerification").As<Napi::Boolean>();
252
+ }
253
+
245
254
  // Create peer-connection
246
255
  try
247
256
  {
@@ -2,7 +2,7 @@
2
2
 
3
3
  #include "plog/Log.h"
4
4
 
5
- Napi::FunctionReference WebSocketServerWrapper::constructor;
5
+ Napi::FunctionReference WebSocketServerWrapper::constructor = Napi::FunctionReference();
6
6
  std::unordered_set<WebSocketServerWrapper *> WebSocketServerWrapper::instances;
7
7
 
8
8
  void WebSocketServerWrapper::StopAll()
@@ -26,8 +26,12 @@ Napi::Object WebSocketServerWrapper::Init(Napi::Env env, Napi::Object exports)
26
26
  InstanceMethod("onClient", &WebSocketServerWrapper::onClient)
27
27
  });
28
28
 
29
- constructor = Napi::Persistent(func);
30
- constructor.SuppressDestruct();
29
+ // If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
30
+ if(constructor.IsEmpty())
31
+ {
32
+ constructor = Napi::Persistent(func);
33
+ constructor.SuppressDestruct();
34
+ }
31
35
 
32
36
  exports.Set("WebSocketServer", func);
33
37
  return exports;
@@ -2,7 +2,7 @@
2
2
 
3
3
  #include "plog/Log.h"
4
4
 
5
- Napi::FunctionReference WebSocketWrapper::constructor;
5
+ Napi::FunctionReference WebSocketWrapper::constructor = Napi::FunctionReference();
6
6
  std::unordered_set<WebSocketWrapper *> WebSocketWrapper::instances;
7
7
 
8
8
  void WebSocketWrapper::CloseAll()
@@ -47,8 +47,12 @@ Napi::Object WebSocketWrapper::Init(Napi::Env env, Napi::Object exports)
47
47
  InstanceMethod("path", &WebSocketWrapper::path),
48
48
  });
49
49
 
50
- constructor = Napi::Persistent(func);
51
- constructor.SuppressDestruct();
50
+ // If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
51
+ if(constructor.IsEmpty())
52
+ {
53
+ constructor = Napi::Persistent(func);
54
+ constructor.SuppressDestruct();
55
+ }
52
56
 
53
57
  exports.Set("WebSocket", func);
54
58
  return exports;
@@ -1,5 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import * as stream from 'stream';
2
- // import { DataChannel } from './node-datachannel.js';
3
3
 
4
4
  /**
5
5
  * Turns a node-datachannel DataChannel into a real Node.js stream, complete with buffering,
package/src/lib/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import nodeDataChannel from './node-datachannel';
2
2
  import _DataChannelStream from './datachannel-stream';
3
3
  import { WebSocketServer } from './websocket-server';
4
- import { Channel, DataChannelInitConfig, DescriptionType, Direction, LogLevel, RtcConfig, RTCIceConnectionState, RTCIceGatheringState, RTCPeerConnectionState, RTCSdpType, RTCSignalingState, SctpSettings, SelectedCandidateInfo } from './types';
4
+ import { Channel, DataChannelInitConfig, DescriptionType, Direction, LogLevel, RtcConfig, RTCIceConnectionState, RTCIceGatheringState, RTCPeerConnectionState, RTCSignalingState, SctpSettings, SelectedCandidateInfo } from './types';
5
5
  import { WebSocket } from './websocket';
6
6
 
7
7
  export function preload(): void { nodeDataChannel.preload(); }
@@ -105,7 +105,7 @@ export interface DataChannel extends Channel {
105
105
  onClosed(cb: () => void): void;
106
106
  onError(cb: (err: string) => void): void;
107
107
  onBufferedAmountLow(cb: () => void): void;
108
- onMessage(cb: (msg: string | Buffer) => void): void;
108
+ onMessage(cb: (msg: string | Buffer | ArrayBuffer) => void): void;
109
109
  }
110
110
  export const DataChannel: {
111
111
  // DataChannel implementation
@@ -115,7 +115,7 @@ export interface PeerConnection {
115
115
  close(): void;
116
116
  setLocalDescription(type?: DescriptionType): void;
117
117
  setRemoteDescription(sdp: string, type: DescriptionType): void;
118
- localDescription(): { type: DescriptionType; sdp: RTCSdpType } | null;
118
+ localDescription(): { type: DescriptionType; sdp: string } | null;
119
119
  remoteDescription(): { type: DescriptionType; sdp: string } | null;
120
120
  addRemoteCandidate(candidate: string, mid: string): void;
121
121
  createDataChannel(label: string, config?: DataChannelInitConfig): DataChannel;
@@ -5,6 +5,11 @@ try {
5
5
  nodeDataChannel = require('../../../build/Release/node_datachannel.node');
6
6
  }
7
7
  catch (e) {
8
+ // If this is not a module not found error, rethrow it
9
+ if (e.code !== 'MODULE_NOT_FOUND') {
10
+ throw e;
11
+ }
12
+
8
13
  // from src
9
14
  nodeDataChannel = require('../../build/Release/node_datachannel.node');
10
15
  }
package/src/lib/types.ts CHANGED
@@ -10,7 +10,7 @@ export interface Channel {
10
10
  onClosed(cb: () => void): void;
11
11
  onError(cb: (err: string) => void): void;
12
12
  onBufferedAmountLow(cb: () => void): void;
13
- onMessage(cb: (msg: string | Buffer) => void): void;
13
+ onMessage(cb: (msg: string | Buffer | ArrayBuffer) => void): void;
14
14
  }
15
15
 
16
16
  export interface WebSocketServerConfiguration {
@@ -76,6 +76,7 @@ export interface RtcConfig {
76
76
  maxMessageSize?: number;
77
77
  mtu?: number;
78
78
  iceTransportPolicy?: TransportPolicy;
79
+ disableFingerprintVerification?: boolean;
79
80
  }
80
81
 
81
82
  // Lowercase to match the description type string from libdatachannel
@@ -4,6 +4,7 @@ import { WebSocketServerConfiguration } from './types';
4
4
  import { WebSocket } from './websocket';
5
5
 
6
6
  export class WebSocketServer extends EventEmitter {
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
8
  #server: any;
8
9
  #clients: WebSocket[] = [];
9
10
 
@@ -1,5 +1,4 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import 'node-domexception';
3
2
  import * as exceptions from './Exception';
4
3
  import { DataChannel } from '../lib/index';
5
4
 
@@ -27,7 +26,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
27
26
  super();
28
27
 
29
28
  this.#dataChannel = dataChannel;
30
- this.#binaryType = 'arraybuffer';
29
+ this.#binaryType = 'blob';
31
30
  this.#readyState = this.#dataChannel.isOpen() ? 'open' : 'connecting';
32
31
  this.#bufferedAmountLowThreshold = 0;
33
32
  this.#maxPacketLifeTime = opts.maxPacketLifeTime || null;
@@ -73,7 +72,10 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
73
72
 
74
73
  this.#dataChannel.onMessage((data) => {
75
74
  if (ArrayBuffer.isView(data)) {
76
- data = Buffer.from(data.buffer);
75
+ if (this.binaryType == 'arraybuffer')
76
+ data = data.buffer;
77
+ else
78
+ data = Buffer.from(data.buffer);
77
79
  }
78
80
 
79
81
  this.dispatchEvent(new MessageEvent('message', { data }));
@@ -2,7 +2,6 @@
2
2
  //
3
3
  // Example: candidate:123456 1 UDP 123456 192.168.1.1 12345 typ host raddr=10.0.0.1 rport=54321 generation 0
4
4
 
5
- import 'node-domexception';
6
5
 
7
6
  export default class RTCIceCandidate implements globalThis.RTCIceCandidate {
8
7
  #address: string | null;
@@ -1,5 +1,4 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import 'node-domexception';
3
2
  import { SelectedCandidateInfo } from '../lib/types';
4
3
  import { PeerConnection } from '../lib/index';
5
4
  import RTCSessionDescription from './RTCSessionDescription';
@@ -383,10 +382,14 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
383
382
  getStats(): Promise<globalThis.RTCStatsReport> {
384
383
  return new Promise((resolve) => {
385
384
  const report = new Map();
386
- const cp = this.#peerConnection.getSelectedCandidatePair();
387
- const bytesSent = this.#peerConnection.bytesSent();
388
- const bytesReceived = this.#peerConnection.bytesReceived();
389
- const rtt = this.#peerConnection.rtt();
385
+ const cp = this.#peerConnection?.getSelectedCandidatePair();
386
+ const bytesSent = this.#peerConnection?.bytesSent();
387
+ const bytesReceived = this.#peerConnection?.bytesReceived();
388
+ const rtt = this.#peerConnection?.rtt();
389
+
390
+ if(!cp) {
391
+ return resolve(report);
392
+ }
390
393
 
391
394
  const localIdRs = getRandomString(8);
392
395
  const localId = 'RTCIceCandidate_' + localIdRs;