node-datachannel 0.28.0 → 0.30.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 (59) hide show
  1. package/.clang-format +17 -0
  2. package/.prettierignore +3 -0
  3. package/API.md +30 -0
  4. package/BULDING.md +2 -1
  5. package/CMakeLists.txt +7 -2
  6. package/dist/cjs/lib/index.cjs +4 -1
  7. package/dist/cjs/lib/index.cjs.map +1 -1
  8. package/dist/cjs/polyfill/Events.cjs.map +1 -1
  9. package/dist/cjs/polyfill/RTCDataChannel.cjs +18 -5
  10. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
  11. package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -1
  12. package/dist/cjs/polyfill/RTCPeerConnection.cjs +6 -1
  13. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
  14. package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -1
  15. package/dist/esm/lib/index.mjs +4 -2
  16. package/dist/esm/lib/index.mjs.map +1 -1
  17. package/dist/esm/polyfill/Events.mjs.map +1 -1
  18. package/dist/esm/polyfill/RTCDataChannel.mjs +18 -5
  19. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
  20. package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -1
  21. package/dist/esm/polyfill/RTCPeerConnection.mjs +6 -1
  22. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
  23. package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -1
  24. package/dist/types/lib/index.d.ts +14 -3
  25. package/dist/types/lib/types.d.ts +24 -1
  26. package/dist/types/polyfill/RTCPeerConnection.d.ts +1 -2
  27. package/package.json +4 -2
  28. package/src/cpp/data-channel-wrapper.cpp +432 -411
  29. package/src/cpp/data-channel-wrapper.h +0 -2
  30. package/src/cpp/ice-udp-mux-listener-wrapper.cpp +157 -0
  31. package/src/cpp/ice-udp-mux-listener-wrapper.h +43 -0
  32. package/src/cpp/main.cpp +12 -10
  33. package/src/cpp/media-audio-wrapper.cpp +291 -294
  34. package/src/cpp/media-audio-wrapper.h +1 -3
  35. package/src/cpp/media-direction.cpp +32 -32
  36. package/src/cpp/media-direction.h +1 -1
  37. package/src/cpp/media-rtcpreceivingsession-wrapper.cpp +24 -28
  38. package/src/cpp/media-rtcpreceivingsession-wrapper.h +0 -5
  39. package/src/cpp/media-track-wrapper.cpp +350 -339
  40. package/src/cpp/media-track-wrapper.h +0 -3
  41. package/src/cpp/media-video-wrapper.cpp +312 -313
  42. package/src/cpp/media-video-wrapper.h +1 -3
  43. package/src/cpp/peer-connection-wrapper.cpp +1097 -984
  44. package/src/cpp/peer-connection-wrapper.h +1 -2
  45. package/src/cpp/rtc-wrapper.cpp +142 -138
  46. package/src/cpp/rtc-wrapper.h +8 -10
  47. package/src/cpp/thread-safe-callback.cpp +39 -50
  48. package/src/cpp/thread-safe-callback.h +26 -28
  49. package/src/cpp/web-socket-server-wrapper.cpp +205 -199
  50. package/src/cpp/web-socket-server-wrapper.h +0 -4
  51. package/src/cpp/web-socket-wrapper.cpp +625 -598
  52. package/src/cpp/web-socket-wrapper.h +0 -3
  53. package/src/lib/index.ts +14 -1
  54. package/src/lib/types.ts +26 -0
  55. package/src/polyfill/Events.ts +4 -1
  56. package/src/polyfill/RTCDataChannel.ts +24 -5
  57. package/src/polyfill/RTCDtlsTransport.ts +1 -1
  58. package/src/polyfill/RTCPeerConnection.ts +9 -3
  59. package/src/polyfill/RTCSctpTransport.ts +2 -2
@@ -1,9 +1,6 @@
1
1
  #ifndef WEB_SOCKET_WRAPPER_H
2
2
  #define WEB_SOCKET_WRAPPER_H
3
3
 
4
- #include <iostream>
5
- #include <string>
6
- #include <variant>
7
4
  #include <memory>
8
5
  #include <unordered_set>
9
6
 
package/src/lib/index.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  SelectedCandidateInfo,
17
17
  } from './types';
18
18
  import { WebSocket } from './websocket';
19
+ import type { CertificateFingerprint, IceUdpMuxRequest, LocalDescriptionInit } from './types';
19
20
 
20
21
  export function preload(): void {
21
22
  nodeDataChannel.preload();
@@ -136,10 +137,11 @@ export const DataChannel: {
136
137
 
137
138
  export interface PeerConnection {
138
139
  close(): void;
139
- setLocalDescription(type?: DescriptionType): void;
140
+ setLocalDescription(type?: DescriptionType, init?: LocalDescriptionInit): void;
140
141
  setRemoteDescription(sdp: string, type: DescriptionType): void;
141
142
  localDescription(): { type: DescriptionType; sdp: string } | null;
142
143
  remoteDescription(): { type: DescriptionType; sdp: string } | null;
144
+ remoteFingerprint(): CertificateFingerprint;
143
145
  addRemoteCandidate(candidate: string, mid: string): void;
144
146
  createDataChannel(label: string, config?: DataChannelInitConfig): DataChannel;
145
147
  addTrack(media: Video | Audio): Track;
@@ -170,6 +172,16 @@ export const PeerConnection: {
170
172
  new (peerName: string, config: RtcConfig): PeerConnection;
171
173
  } = nodeDataChannel.PeerConnection;
172
174
 
175
+ export interface IceUdpMuxListener {
176
+ address?: string;
177
+ port: number;
178
+ stop(): void;
179
+ onUnhandledStunRequest(cb: (req: IceUdpMuxRequest) => void): void;
180
+ }
181
+ export const IceUdpMuxListener: {
182
+ new (port: number, address?: string): IceUdpMuxListener;
183
+ } = nodeDataChannel.IceUdpMuxListener;
184
+
173
185
  export interface RtcpReceivingSession {}
174
186
 
175
187
  export const RtcpReceivingSession: {
@@ -196,6 +208,7 @@ export default {
196
208
  WebSocket,
197
209
  WebSocketServer,
198
210
  DataChannelStream,
211
+ IceUdpMuxListener,
199
212
  };
200
213
 
201
214
  // Types
package/src/lib/types.ts CHANGED
@@ -73,6 +73,10 @@ export interface RtcConfig {
73
73
  mtu?: number;
74
74
  iceTransportPolicy?: TransportPolicy;
75
75
  disableFingerprintVerification?: boolean;
76
+ disableAutoGathering?: boolean;
77
+ certificatePemFile?: string;
78
+ keyPemFile?: string;
79
+ keyPemPass?: string;
76
80
  }
77
81
 
78
82
  // Lowercase to match the description type string from libdatachannel
@@ -113,6 +117,11 @@ export type RTCSignalingState =
113
117
  | 'have-remote-pranswer'
114
118
  | 'stable';
115
119
 
120
+ export interface LocalDescriptionInit {
121
+ iceUfrag?: string;
122
+ icePwd?: string;
123
+ }
124
+
116
125
  export interface DataChannelInitConfig {
117
126
  protocol?: string;
118
127
  negotiated?: boolean;
@@ -122,6 +131,17 @@ export interface DataChannelInitConfig {
122
131
  maxRetransmits?: number; // Reliability
123
132
  }
124
133
 
134
+ export interface CertificateFingerprint {
135
+ /**
136
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/RTCCertificate/getFingerprints#value
137
+ */
138
+ value: string;
139
+ /**
140
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/RTCCertificate/getFingerprints#algorithm
141
+ */
142
+ algorithm: 'sha-1' | 'sha-224' | 'sha-256' | 'sha-384' | 'sha-512' | 'md5' | 'md2';
143
+ }
144
+
125
145
  export interface SelectedCandidateInfo {
126
146
  address: string;
127
147
  port: number;
@@ -134,3 +154,9 @@ export interface SelectedCandidateInfo {
134
154
 
135
155
  // Must be same as rtc enum class Direction
136
156
  export type Direction = 'SendOnly' | 'RecvOnly' | 'SendRecv' | 'Inactive' | 'Unknown';
157
+
158
+ export interface IceUdpMuxRequest {
159
+ ufrag: string;
160
+ host: string;
161
+ port: number;
162
+ }
@@ -1,7 +1,10 @@
1
1
  import RTCDataChannel from './RTCDataChannel';
2
2
  import RTCError from './RTCError';
3
3
 
4
- export class RTCPeerConnectionIceEvent extends Event implements globalThis.RTCPeerConnectionIceEvent {
4
+ export class RTCPeerConnectionIceEvent
5
+ extends Event
6
+ implements globalThis.RTCPeerConnectionIceEvent
7
+ {
5
8
  #candidate: globalThis.RTCIceCandidate;
6
9
 
7
10
  constructor(candidate: globalThis.RTCIceCandidate) {
@@ -30,7 +30,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
30
30
  super();
31
31
 
32
32
  this.#dataChannel = dataChannel;
33
- this.#binaryType = 'blob';
33
+ this.#binaryType = 'arraybuffer';
34
34
  this.#readyState = this.#dataChannel.isOpen() ? 'open' : 'connecting';
35
35
  this.#bufferedAmountLowThreshold = 0;
36
36
  this.#maxPacketLifeTime = opts.maxPacketLifeTime ?? null;
@@ -77,10 +77,29 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
77
77
  this.dispatchEvent(new Event('bufferedamountlow'));
78
78
  });
79
79
 
80
- this.#dataChannel.onMessage((data) => {
81
- if (ArrayBuffer.isView(data)) {
82
- if (this.binaryType == 'arraybuffer') data = data.buffer;
83
- else data = Buffer.from(data.buffer);
80
+ this.#dataChannel.onMessage((message) => {
81
+ if (typeof message === 'string') {
82
+ this.dispatchEvent(new MessageEvent('message', { data: message }));
83
+ return;
84
+ }
85
+
86
+ let data: Blob | ArrayBuffer;
87
+
88
+ if (message instanceof ArrayBuffer) {
89
+ data = message;
90
+ } else {
91
+ data = message.buffer;
92
+
93
+ if (message.byteOffset !== 0 || message.byteLength !== message.buffer.byteLength) {
94
+ // message is view on underlying buffer, must create new
95
+ // ArrayBuffer that only contains message data
96
+ data = new ArrayBuffer(message.byteLength);
97
+ new Uint8Array(data, 0, message.byteLength).set(message);
98
+ }
99
+ }
100
+
101
+ if (this.#binaryType === 'blob') {
102
+ data = new Blob([data]);
84
103
  }
85
104
 
86
105
  this.dispatchEvent(new MessageEvent('message', { data }));
@@ -14,7 +14,7 @@ export default class RTCDtlsTransport extends EventTarget implements globalThis.
14
14
  this.#pc = init.pc;
15
15
 
16
16
  this.#iceTransport = new RTCIceTransport({
17
- pc: init.pc
17
+ pc: init.pc,
18
18
  });
19
19
 
20
20
  // forward peerConnection events
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { SelectedCandidateInfo } from '../lib/types';
2
+ import { DataChannelInitConfig, SelectedCandidateInfo } from '../lib/types';
3
3
  import { PeerConnection } from '../lib/index';
4
4
  import RTCSessionDescription from './RTCSessionDescription';
5
5
  import RTCDataChannel from './RTCDataChannel';
@@ -36,7 +36,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
36
36
  // events
37
37
  onconnectionstatechange: globalThis.RTCPeerConnection['onconnectionstatechange'] = null;
38
38
  // For ondatachannel we need to define type manually
39
- ondatachannel: ((this: globalThis.RTCPeerConnection, ev: RTCDataChannelEvent) => any) | null;
39
+ ondatachannel: ((this: globalThis.RTCPeerConnection, ev: globalThis.RTCDataChannelEvent) => any) | null;
40
40
  onicecandidate: globalThis.RTCPeerConnection['onicecandidate'] = null;
41
41
  onicecandidateerror: globalThis.RTCPeerConnection['onicecandidateerror'] = null;
42
42
  oniceconnectionstatechange: globalThis.RTCPeerConnection['oniceconnectionstatechange'] = null;
@@ -362,7 +362,13 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
362
362
  }
363
363
 
364
364
  createDataChannel(label: string, opts: globalThis.RTCDataChannelInit = {}): RTCDataChannel {
365
- const channel = this.#peerConnection.createDataChannel(label, opts);
365
+ const nativeOpts: DataChannelInitConfig = {
366
+ ...opts,
367
+ // libdatachannel uses "unordered", opposite of RTCDataChannelInit.ordered
368
+ unordered: opts.ordered === false ? true : false,
369
+ };
370
+
371
+ const channel = this.#peerConnection.createDataChannel(label, nativeOpts);
366
372
  const dataChannel = new RTCDataChannel(channel, opts);
367
373
 
368
374
  // ensure we can close all channels when shutting down
@@ -13,7 +13,7 @@ export default class RTCSctpTransport extends EventTarget implements globalThis.
13
13
  this.#pc = initial.pc;
14
14
 
15
15
  this.#transport = new RTCDtlsTransport({
16
- pc: initial.pc
16
+ pc: initial.pc,
17
17
  });
18
18
 
19
19
  this.#pc.addEventListener('connectionstatechange', () => {
@@ -30,7 +30,7 @@ export default class RTCSctpTransport extends EventTarget implements globalThis.
30
30
 
31
31
  get maxMessageSize(): number {
32
32
  if (this.state !== 'connected') return null;
33
- return this.#pc?.ext_maxMessageSize ?? 65536;;
33
+ return this.#pc?.ext_maxMessageSize ?? 65536;
34
34
  }
35
35
 
36
36
  get state(): globalThis.RTCSctpTransportState {