node-datachannel 0.12.0 → 0.20.0-alpha.1

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 (132) hide show
  1. package/.eslintignore +5 -1
  2. package/.eslintrc.json +27 -0
  3. package/.prettierrc +12 -0
  4. package/CMakeLists.txt +13 -13
  5. package/jest.config.ts +14 -0
  6. package/package.json +46 -43
  7. package/rollup.config.mjs +63 -0
  8. package/src/index.ts +10 -0
  9. package/{lib/datachannel-stream.js → src/lib/datachannel-stream.ts} +16 -11
  10. package/src/lib/index.ts +180 -0
  11. package/src/lib/node-datachannel.ts +13 -0
  12. package/src/lib/types.ts +126 -0
  13. package/{lib/websocket-server.js → src/lib/websocket-server.ts} +10 -8
  14. package/src/lib/websocket.ts +26 -0
  15. package/src/polyfill/Events.ts +33 -0
  16. package/src/polyfill/Exception.ts +37 -0
  17. package/src/polyfill/RTCCertificate.ts +17 -0
  18. package/{polyfill/RTCDataChannel.js → src/polyfill/RTCDataChannel.ts} +43 -40
  19. package/src/polyfill/RTCDtlsTransport.ts +47 -0
  20. package/src/polyfill/RTCError.ts +71 -0
  21. package/{polyfill/RTCIceCandidate.js → src/polyfill/RTCIceCandidate.ts} +35 -35
  22. package/{polyfill/RTCIceTransport.js → src/polyfill/RTCIceTransport.ts} +22 -20
  23. package/{polyfill/RTCPeerConnection.js → src/polyfill/RTCPeerConnection.ts} +128 -114
  24. package/{polyfill/RTCSctpTransport.js → src/polyfill/RTCSctpTransport.ts} +16 -14
  25. package/{polyfill/RTCSessionDescription.js → src/polyfill/RTCSessionDescription.ts} +7 -12
  26. package/{polyfill/index.js → src/polyfill/index.ts} +10 -10
  27. package/tsconfig.json +20 -13
  28. package/.eslintrc.cjs +0 -8
  29. package/.github/FUNDING.yml +0 -2
  30. package/.github/workflows/build-linux.yml +0 -109
  31. package/.github/workflows/build-mac-m1.yml +0 -38
  32. package/.github/workflows/build-mac-x64.yml +0 -40
  33. package/.github/workflows/build-win.yml +0 -74
  34. package/.github/workflows/lint.yml +0 -20
  35. package/.github/workflows/npm-publish-manual.yml +0 -23
  36. package/.prettierrc.cjs +0 -8
  37. package/build-containers/Dockerfile.alpine +0 -6
  38. package/build-containers/Dockerfile.debian +0 -7
  39. package/cmake/toolchain/ci.cmake +0 -23
  40. package/examples/client-server/README.md +0 -22
  41. package/examples/client-server/client-benchmark.js +0 -230
  42. package/examples/client-server/client-periodic.js +0 -189
  43. package/examples/client-server/client.js +0 -111
  44. package/examples/client-server/package-lock.json +0 -289
  45. package/examples/client-server/package.json +0 -16
  46. package/examples/client-server/signaling-server.js +0 -39
  47. package/examples/electron-demo/README.md +0 -58
  48. package/examples/electron-demo/forge.config.js +0 -49
  49. package/examples/electron-demo/package-lock.json +0 -15877
  50. package/examples/electron-demo/package.json +0 -44
  51. package/examples/electron-demo/src/index.css +0 -7
  52. package/examples/electron-demo/src/index.html +0 -29
  53. package/examples/electron-demo/src/main.js +0 -53
  54. package/examples/electron-demo/src/node-datachannel.js +0 -123
  55. package/examples/electron-demo/src/preload.js +0 -28
  56. package/examples/electron-demo/src/renderer.js +0 -58
  57. package/examples/electron-demo/webpack.main.config.js +0 -21
  58. package/examples/electron-demo/webpack.renderer.config.js +0 -13
  59. package/examples/electron-demo/webpack.rules.js +0 -35
  60. package/examples/media/README.md +0 -34
  61. package/examples/media/main.html +0 -45
  62. package/examples/media/media.js +0 -54
  63. package/examples/simple-peer-usage/main.js +0 -28
  64. package/examples/simple-peer-usage/package-lock.json +0 -317
  65. package/examples/simple-peer-usage/package.json +0 -16
  66. package/examples/websocket/websocket-client.js +0 -20
  67. package/examples/websocket/websocket-server.js +0 -22
  68. package/jest.config.cjs +0 -7
  69. package/lib/index.cjs +0 -194
  70. package/lib/index.d.cts +0 -300
  71. package/lib/index.d.ts +0 -300
  72. package/lib/index.js +0 -58
  73. package/lib/node-datachannel.js +0 -7
  74. package/polyfill/Events.d.ts +0 -9
  75. package/polyfill/Events.js +0 -29
  76. package/polyfill/Exception.js +0 -23
  77. package/polyfill/RTCCertificate.d.ts +0 -7
  78. package/polyfill/RTCCertificate.js +0 -17
  79. package/polyfill/RTCDataChannel.d.ts +0 -35
  80. package/polyfill/RTCDtlsTransport.d.ts +0 -10
  81. package/polyfill/RTCDtlsTransport.js +0 -47
  82. package/polyfill/RTCError.d.ts +0 -11
  83. package/polyfill/RTCError.js +0 -65
  84. package/polyfill/RTCIceCandidate.d.ts +0 -20
  85. package/polyfill/RTCIceTransport.d.ts +0 -18
  86. package/polyfill/RTCPeerConnection.d.ts +0 -62
  87. package/polyfill/RTCSctpTransport.d.ts +0 -10
  88. package/polyfill/RTCSessionDescription.d.ts +0 -8
  89. package/polyfill/index.cjs +0 -1333
  90. package/polyfill/index.d.cts +0 -37
  91. package/polyfill/index.d.ts +0 -37
  92. package/test/connectivity.js +0 -86
  93. package/test/jest-tests/basic.test.js +0 -37
  94. package/test/jest-tests/multiple-run.test.js +0 -73
  95. package/test/jest-tests/p2p.test.js +0 -99
  96. package/test/jest-tests/polyfill.test.js +0 -10
  97. package/test/jest-tests/streams.test.js +0 -48
  98. package/test/jest-tests/websocket.test.js +0 -69
  99. package/test/polyfill-connectivity.js +0 -89
  100. package/test/websockets.js +0 -50
  101. package/test/wpt-tests/README.md +0 -46
  102. package/test/wpt-tests/chrome-failed-tests.js +0 -42
  103. package/test/wpt-tests/index.js +0 -96
  104. package/test/wpt-tests/last-test-results.md +0 -301
  105. package/test/wpt-tests/package-lock.json +0 -1712
  106. package/test/wpt-tests/package.json +0 -19
  107. package/test/wpt-tests/wpt-test-list.js +0 -139
  108. package/test/wpt-tests/wpt.js +0 -131
  109. /package/src/{data-channel-wrapper.cpp → cpp/data-channel-wrapper.cpp} +0 -0
  110. /package/src/{data-channel-wrapper.h → cpp/data-channel-wrapper.h} +0 -0
  111. /package/src/{main.cpp → cpp/main.cpp} +0 -0
  112. /package/src/{media-audio-wrapper.cpp → cpp/media-audio-wrapper.cpp} +0 -0
  113. /package/src/{media-audio-wrapper.h → cpp/media-audio-wrapper.h} +0 -0
  114. /package/src/{media-direction.cpp → cpp/media-direction.cpp} +0 -0
  115. /package/src/{media-direction.h → cpp/media-direction.h} +0 -0
  116. /package/src/{media-rtcpreceivingsession-wrapper.cpp → cpp/media-rtcpreceivingsession-wrapper.cpp} +0 -0
  117. /package/src/{media-rtcpreceivingsession-wrapper.h → cpp/media-rtcpreceivingsession-wrapper.h} +0 -0
  118. /package/src/{media-track-wrapper.cpp → cpp/media-track-wrapper.cpp} +0 -0
  119. /package/src/{media-track-wrapper.h → cpp/media-track-wrapper.h} +0 -0
  120. /package/src/{media-video-wrapper.cpp → cpp/media-video-wrapper.cpp} +0 -0
  121. /package/src/{media-video-wrapper.h → cpp/media-video-wrapper.h} +0 -0
  122. /package/src/{peer-connection-wrapper.cpp → cpp/peer-connection-wrapper.cpp} +0 -0
  123. /package/src/{peer-connection-wrapper.h → cpp/peer-connection-wrapper.h} +0 -0
  124. /package/src/{rtc-wrapper.cpp → cpp/rtc-wrapper.cpp} +0 -0
  125. /package/src/{rtc-wrapper.h → cpp/rtc-wrapper.h} +0 -0
  126. /package/src/{thread-safe-callback.cpp → cpp/thread-safe-callback.cpp} +0 -0
  127. /package/src/{thread-safe-callback.h → cpp/thread-safe-callback.h} +0 -0
  128. /package/src/{web-socket-server-wrapper.cpp → cpp/web-socket-server-wrapper.cpp} +0 -0
  129. /package/src/{web-socket-server-wrapper.h → cpp/web-socket-server-wrapper.h} +0 -0
  130. /package/src/{web-socket-wrapper.cpp → cpp/web-socket-wrapper.cpp} +0 -0
  131. /package/src/{web-socket-wrapper.h → cpp/web-socket-wrapper.h} +0 -0
  132. /package/{polyfill → src/polyfill}/README.md +0 -0
@@ -1,40 +1,49 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import 'node-domexception';
2
- import NodeDataChannel from '../lib/index.js';
3
- import RTCSessionDescription from './RTCSessionDescription.js';
4
- import RTCDataChannel from './RTCDataChannel.js';
5
- import RTCIceCandidate from './RTCIceCandidate.js';
6
- import { RTCDataChannelEvent, RTCPeerConnectionIceEvent } from './Events.js';
7
- import RTCSctpTransport from './RTCSctpTransport.js';
8
- import * as exceptions from './Exception.js';
9
-
10
- export default class _RTCPeerConnection extends EventTarget {
11
- static async generateCertificate() {
3
+ import { SelectedCandidateInfo } from '../lib/types';
4
+ import { PeerConnection } from '../lib/index';
5
+ import RTCSessionDescription from './RTCSessionDescription';
6
+ import RTCDataChannel from './RTCDataChannel';
7
+ import RTCIceCandidate from './RTCIceCandidate';
8
+ import { RTCDataChannelEvent, RTCPeerConnectionIceEvent } from './Events';
9
+ import RTCSctpTransport from './RTCSctpTransport';
10
+ import * as exceptions from './Exception';
11
+ import RTCCertificate from './RTCCertificate';
12
+
13
+ // extend RTCConfiguration with peerIdentity
14
+ interface RTCConfiguration extends globalThis.RTCConfiguration {
15
+ peerIdentity?: string;
16
+ }
17
+
18
+ export default class RTCPeerConnection extends EventTarget implements globalThis.RTCPeerConnection {
19
+ static async generateCertificate(): Promise<RTCCertificate> {
12
20
  throw new DOMException('Not implemented');
13
21
  }
14
22
 
15
- #peerConnection;
16
- #localOffer;
17
- #localAnswer;
18
- #dataChannels;
23
+ #peerConnection: PeerConnection;
24
+ #localOffer: any;
25
+ #localAnswer: any;
26
+ #dataChannels: Set<RTCDataChannel>;
19
27
  #dataChannelsClosed = 0;
20
- #config;
21
- #canTrickleIceCandidates;
22
- #sctp;
23
-
24
- #localCandidates = [];
25
- #remoteCandidates = [];
26
-
27
- onconnectionstatechange;
28
- ondatachannel;
29
- onicecandidate;
30
- onicecandidateerror;
31
- oniceconnectionstatechange;
32
- onicegatheringstatechange;
33
- onnegotiationneeded;
34
- onsignalingstatechange;
35
- ontrack;
36
-
37
- _checkConfiguration(config) {
28
+ #config: RTCConfiguration;
29
+ #canTrickleIceCandidates: boolean | null;
30
+ #sctp: RTCSctpTransport;
31
+
32
+ #localCandidates: RTCIceCandidate[] = [];
33
+ #remoteCandidates: RTCIceCandidate[] = [];
34
+
35
+ // events
36
+ onconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
37
+ ondatachannel: ((this: RTCPeerConnection, ev: RTCDataChannelEvent) => any) | null;
38
+ onicecandidate: ((this: RTCPeerConnection, ev: RTCPeerConnectionIceEvent) => any) | null;
39
+ onicecandidateerror: ((this: RTCPeerConnection, ev: Event) => any) | null;
40
+ oniceconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
41
+ onicegatheringstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
42
+ onnegotiationneeded: ((this: RTCPeerConnection, ev: Event) => any) | null;
43
+ onsignalingstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
44
+ ontrack: ((this: RTCPeerConnection, ev: globalThis.RTCTrackEvent) => any) | null;
45
+
46
+ private _checkConfiguration(config: RTCConfiguration): void {
38
47
  if (config && config.iceServers === undefined) config.iceServers = [];
39
48
  if (config && config.iceTransportPolicy === undefined) config.iceTransportPolicy = 'all';
40
49
 
@@ -49,15 +58,15 @@ export default class _RTCPeerConnection extends EventTarget {
49
58
 
50
59
  // If iceServers is string convert to array
51
60
  if (typeof config.iceServers[i].urls === 'string')
52
- config.iceServers[i].urls = [config.iceServers[i].urls];
61
+ config.iceServers[i].urls = [config.iceServers[i].urls as string];
53
62
 
54
63
  // urls can not be empty
55
- if (config.iceServers[i].urls?.some((url) => url == ''))
56
- throw exceptions.SyntaxError('IceServers urls cannot be empty');
64
+ if ((config.iceServers[i].urls as string[])?.some((url) => url == ''))
65
+ throw new exceptions.SyntaxError('IceServers urls cannot be empty');
57
66
 
58
67
  // urls should be valid URLs and match the protocols "stun:|turn:|turns:"
59
68
  if (
60
- config.iceServers[i].urls?.some(
69
+ (config.iceServers[i].urls as string[])?.some(
61
70
  (url) => {
62
71
  try {
63
72
  const parsedURL = new URL(url)
@@ -69,19 +78,19 @@ export default class _RTCPeerConnection extends EventTarget {
69
78
  },
70
79
  )
71
80
  )
72
- throw exceptions.SyntaxError('IceServers urls wrong format');
81
+ throw new exceptions.SyntaxError('IceServers urls wrong format');
73
82
 
74
83
  // If this is a turn server check for username and credential
75
- if (config.iceServers[i].urls?.some((url) => url.startsWith('turn'))) {
84
+ if ((config.iceServers[i].urls as string[])?.some((url) => url.startsWith('turn'))) {
76
85
  if (!config.iceServers[i].username)
77
- throw exceptions.InvalidAccessError('IceServers username cannot be null');
86
+ throw new exceptions.InvalidAccessError('IceServers username cannot be null');
78
87
  if (!config.iceServers[i].credential)
79
- throw exceptions.InvalidAccessError('IceServers username cannot be undefined');
88
+ throw new exceptions.InvalidAccessError('IceServers username cannot be undefined');
80
89
  }
81
90
 
82
91
  // length of urls can not be 0
83
92
  if (config.iceServers[i].urls?.length === 0)
84
- throw exceptions.SyntaxError('IceServers urls cannot be empty');
93
+ throw new exceptions.SyntaxError('IceServers urls cannot be empty');
85
94
  }
86
95
  }
87
96
 
@@ -94,12 +103,14 @@ export default class _RTCPeerConnection extends EventTarget {
94
103
  throw new TypeError('IceTransportPolicy must be either "all" or "relay"');
95
104
  }
96
105
 
97
- setConfiguration(config) {
106
+ setConfiguration(config: RTCConfiguration): void {
98
107
  this._checkConfiguration(config);
99
108
  this.#config = config;
100
109
  }
101
110
 
102
- constructor(config = { iceServers: [], iceTransportPolicy: 'all' }) {
111
+
112
+
113
+ constructor(config: RTCConfiguration = { iceServers: [], iceTransportPolicy: 'all' }) {
103
114
  super();
104
115
 
105
116
  this._checkConfiguration(config);
@@ -110,8 +121,8 @@ export default class _RTCPeerConnection extends EventTarget {
110
121
  this.#canTrickleIceCandidates = null;
111
122
 
112
123
  try {
113
- this.#peerConnection = new NodeDataChannel.PeerConnection(
114
- config?.peerIdentity ?? `peer-${getRandomString(7)}`,
124
+ const peerIdentity = (config as any)?.peerIdentity ?? `peer-${getRandomString(7)}`;
125
+ this.#peerConnection = new PeerConnection(peerIdentity,
115
126
  {
116
127
  ...config,
117
128
  iceServers:
@@ -131,8 +142,8 @@ export default class _RTCPeerConnection extends EventTarget {
131
142
  },
132
143
  );
133
144
  } catch (error) {
134
- if (!error || !error.message) throw exceptions.NotFoundError('Unknown error');
135
- throw exceptions.SyntaxError(error.message);
145
+ if (!error || !error.message) throw new exceptions.NotFoundError('Unknown error');
146
+ throw new exceptions.SyntaxError(error.message);
136
147
  }
137
148
 
138
149
  // forward peerConnection events
@@ -192,43 +203,43 @@ export default class _RTCPeerConnection extends EventTarget {
192
203
  if (this.onicegatheringstatechange) this.onicegatheringstatechange(e);
193
204
  });
194
205
  this.addEventListener('datachannel', (e) => {
195
- if (this.ondatachannel) this.ondatachannel(e);
206
+ if (this.ondatachannel) this.ondatachannel(e as RTCDataChannelEvent);
196
207
  });
197
208
  this.addEventListener('icecandidate', (e) => {
198
- if (this.onicecandidate) this.onicecandidate(e);
209
+ if (this.onicecandidate) this.onicecandidate(e as RTCPeerConnectionIceEvent);
199
210
  });
200
211
 
201
212
  this.#sctp = new RTCSctpTransport({
202
213
  pc: this,
203
214
  extraFunctions: {
204
- maxDataChannelId: () => {
215
+ maxDataChannelId: (): number => {
205
216
  return this.#peerConnection.maxDataChannelId();
206
217
  },
207
- maxMessageSize: () => {
218
+ maxMessageSize: (): number => {
208
219
  return this.#peerConnection.maxMessageSize();
209
220
  },
210
- localCandidates: () => {
221
+ localCandidates: (): RTCIceCandidate[] => {
211
222
  return this.#localCandidates;
212
223
  },
213
- remoteCandidates: () => {
224
+ remoteCandidates: (): RTCIceCandidate[] => {
214
225
  return this.#remoteCandidates;
215
226
  },
216
- selectedCandidatePair: () => {
227
+ selectedCandidatePair: (): { local: SelectedCandidateInfo; remote: SelectedCandidateInfo } | null => {
217
228
  return this.#peerConnection.getSelectedCandidatePair();
218
229
  },
219
230
  },
220
231
  });
221
232
  }
222
233
 
223
- get canTrickleIceCandidates() {
234
+ get canTrickleIceCandidates(): boolean | null {
224
235
  return this.#canTrickleIceCandidates;
225
236
  }
226
237
 
227
- get connectionState() {
238
+ get connectionState(): globalThis.RTCPeerConnectionState {
228
239
  return this.#peerConnection.state();
229
240
  }
230
241
 
231
- get iceConnectionState() {
242
+ get iceConnectionState(): globalThis.RTCIceConnectionState {
232
243
  let state = this.#peerConnection.iceState();
233
244
  // libdatachannel uses 'completed' instead of 'connected'
234
245
  // see /webrtc/getstats.html
@@ -236,43 +247,43 @@ export default class _RTCPeerConnection extends EventTarget {
236
247
  return state;
237
248
  }
238
249
 
239
- get iceGatheringState() {
250
+ get iceGatheringState(): globalThis.RTCIceGatheringState {
240
251
  return this.#peerConnection.gatheringState();
241
252
  }
242
253
 
243
- get currentLocalDescription() {
244
- return new RTCSessionDescription(this.#peerConnection.localDescription());
254
+ get currentLocalDescription(): RTCSessionDescription {
255
+ return new RTCSessionDescription(this.#peerConnection.localDescription() as any);
245
256
  }
246
257
 
247
- get currentRemoteDescription() {
248
- return new RTCSessionDescription(this.#peerConnection.remoteDescription());
258
+ get currentRemoteDescription(): RTCSessionDescription {
259
+ return new RTCSessionDescription(this.#peerConnection.remoteDescription() as any);
249
260
  }
250
261
 
251
- get localDescription() {
252
- return new RTCSessionDescription(this.#peerConnection.localDescription());
262
+ get localDescription(): RTCSessionDescription {
263
+ return new RTCSessionDescription(this.#peerConnection.localDescription() as any);
253
264
  }
254
265
 
255
- get pendingLocalDescription() {
256
- return new RTCSessionDescription(this.#peerConnection.localDescription());
266
+ get pendingLocalDescription(): RTCSessionDescription {
267
+ return new RTCSessionDescription(this.#peerConnection.localDescription() as any);
257
268
  }
258
269
 
259
- get pendingRemoteDescription() {
260
- return new RTCSessionDescription(this.#peerConnection.remoteDescription());
270
+ get pendingRemoteDescription(): RTCSessionDescription {
271
+ return new RTCSessionDescription(this.#peerConnection.remoteDescription() as any);
261
272
  }
262
273
 
263
- get remoteDescription() {
264
- return new RTCSessionDescription(this.#peerConnection.remoteDescription());
274
+ get remoteDescription(): RTCSessionDescription {
275
+ return new RTCSessionDescription(this.#peerConnection.remoteDescription() as any);
265
276
  }
266
277
 
267
- get sctp() {
278
+ get sctp(): RTCSctpTransport {
268
279
  return this.#sctp;
269
280
  }
270
281
 
271
- get signalingState() {
282
+ get signalingState(): globalThis.RTCSignalingState {
272
283
  return this.#peerConnection.signalingState();
273
284
  }
274
285
 
275
- async addIceCandidate(candidate) {
286
+ async addIceCandidate(candidate?: globalThis.RTCIceCandidateInit | RTCIceCandidate): Promise<void> {
276
287
  if (!candidate || !candidate.candidate) {
277
288
  return;
278
289
  }
@@ -289,12 +300,12 @@ export default class _RTCPeerConnection extends EventTarget {
289
300
  // ??
290
301
  if (candidate.sdpMid && candidate.sdpMid.length > 3) {
291
302
  // console.log(candidate.sdpMid);
292
- throw exceptions.OperationError('Invalid sdpMid format');
303
+ throw new exceptions.OperationError('Invalid sdpMid format');
293
304
  }
294
305
 
295
306
  // We don't care about sdpMLineIndex, just for test
296
307
  if (!candidate.sdpMid && candidate.sdpMLineIndex > 1) {
297
- throw exceptions.OperationError('This is only for test case.');
308
+ throw new exceptions.OperationError('This is only for test case.');
298
309
  }
299
310
 
300
311
  try {
@@ -303,26 +314,28 @@ export default class _RTCPeerConnection extends EventTarget {
303
314
  new RTCIceCandidate({ candidate: candidate.candidate, sdpMid: candidate.sdpMid || '0' }),
304
315
  );
305
316
  } catch (error) {
306
- if (!error || !error.message) throw exceptions.NotFoundError('Unknown error');
317
+ if (!error || !error.message) throw new exceptions.NotFoundError('Unknown error');
307
318
 
308
319
  // Check error Message if contains specific message
309
320
  if (error.message.includes('remote candidate without remote description'))
310
- throw exceptions.InvalidStateError(error.message);
311
- if (error.message.includes('Invalid candidate format')) throw exceptions.OperationError(error.message);
321
+ throw new exceptions.InvalidStateError(error.message);
322
+ if (error.message.includes('Invalid candidate format')) throw new exceptions.OperationError(error.message);
312
323
 
313
- throw exceptions.NotFoundError(error.message);
324
+ throw new exceptions.NotFoundError(error.message);
314
325
  }
315
326
  }
316
327
 
317
- addTrack(track, ...streams) {
328
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
329
+ addTrack(_track, ..._streams): globalThis.RTCRtpSender {
318
330
  throw new DOMException('Not implemented');
319
331
  }
320
332
 
321
- addTransceiver(trackOrKind, init) {
333
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
334
+ addTransceiver(_trackOrKind, _init): globalThis.RTCRtpTransceiver {
322
335
  throw new DOMException('Not implemented');
323
336
  }
324
337
 
325
- close() {
338
+ close(): void {
326
339
  // close all channels before shutting down
327
340
  this.#dataChannels.forEach((channel) => {
328
341
  channel.close();
@@ -332,11 +345,12 @@ export default class _RTCPeerConnection extends EventTarget {
332
345
  this.#peerConnection.close();
333
346
  }
334
347
 
335
- createAnswer() {
348
+ createAnswer(): Promise<globalThis.RTCSessionDescriptionInit | any> {
336
349
  return this.#localAnswer;
337
350
  }
338
351
 
339
- createDataChannel(label, opts = {}) {
352
+
353
+ createDataChannel(label, opts = {}): RTCDataChannel {
340
354
  const channel = this.#peerConnection.createDataChannel(label, opts);
341
355
  const dataChannel = new RTCDataChannel(channel, opts);
342
356
 
@@ -350,32 +364,32 @@ export default class _RTCPeerConnection extends EventTarget {
350
364
  return dataChannel;
351
365
  }
352
366
 
353
- createOffer() {
367
+ createOffer(): Promise<globalThis.RTCSessionDescriptionInit | any> {
354
368
  return this.#localOffer;
355
369
  }
356
370
 
357
- getConfiguration() {
371
+ getConfiguration(): globalThis.RTCConfiguration {
358
372
  return this.#config;
359
373
  }
360
374
 
361
- getReceivers() {
375
+ getReceivers(): globalThis.RTCRtpReceiver[] {
362
376
  throw new DOMException('Not implemented');
363
377
  }
364
378
 
365
- getSenders() {
379
+ getSenders(): globalThis.RTCRtpSender[] {
366
380
  throw new DOMException('Not implemented');
367
381
  }
368
382
 
369
- getStats() {
383
+ getStats(): Promise<globalThis.RTCStatsReport> {
370
384
  return new Promise((resolve) => {
371
- let report = new Map();
372
- let cp = this.#peerConnection.getSelectedCandidatePair();
373
- let bytesSent = this.#peerConnection.bytesSent();
374
- let bytesReceived = this.#peerConnection.bytesReceived();
375
- let rtt = this.#peerConnection.rtt();
376
-
377
- let localIdRs = getRandomString(8);
378
- let localId = 'RTCIceCandidate_' + localIdRs;
385
+ 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();
390
+
391
+ const localIdRs = getRandomString(8);
392
+ const localId = 'RTCIceCandidate_' + localIdRs;
379
393
  report.set(localId, {
380
394
  id: localId,
381
395
  type: 'local-candidate',
@@ -385,8 +399,8 @@ export default class _RTCPeerConnection extends EventTarget {
385
399
  port: cp.local.port,
386
400
  });
387
401
 
388
- let remoteIdRs = getRandomString(8);
389
- let remoteId = 'RTCIceCandidate_' + remoteIdRs;
402
+ const remoteIdRs = getRandomString(8);
403
+ const remoteId = 'RTCIceCandidate_' + remoteIdRs;
390
404
  report.set(remoteId, {
391
405
  id: remoteId,
392
406
  type: 'remote-candidate',
@@ -396,7 +410,7 @@ export default class _RTCPeerConnection extends EventTarget {
396
410
  port: cp.remote.port,
397
411
  });
398
412
 
399
- let candidateId = 'RTCIceCandidatePair_' + localIdRs + '_' + remoteIdRs;
413
+ const candidateId = 'RTCIceCandidatePair_' + localIdRs + '_' + remoteIdRs;
400
414
  report.set(candidateId, {
401
415
  id: candidateId,
402
416
  type: 'candidate-pair',
@@ -412,7 +426,7 @@ export default class _RTCPeerConnection extends EventTarget {
412
426
  currentRoundTripTime: rtt,
413
427
  });
414
428
 
415
- let transportId = 'RTCTransport_0_1';
429
+ const transportId = 'RTCTransport_0_1';
416
430
  report.set(transportId, {
417
431
  id: transportId,
418
432
  timestamp: Date.now(),
@@ -437,50 +451,50 @@ export default class _RTCPeerConnection extends EventTarget {
437
451
  });
438
452
  }
439
453
 
440
- getTransceivers() {
454
+ getTransceivers(): globalThis.RTCRtpTransceiver[] {
441
455
  return []; // throw new DOMException('Not implemented');
442
456
  }
443
457
 
444
- removeTrack() {
458
+ removeTrack(): void {
445
459
  throw new DOMException('Not implemented');
446
460
  }
447
461
 
448
- restartIce() {
462
+ restartIce(): Promise<void> {
449
463
  throw new DOMException('Not implemented');
450
464
  }
451
465
 
452
- async setLocalDescription(description) {
466
+ async setLocalDescription(description: globalThis.RTCSessionDescriptionInit): Promise<void> {
453
467
  if (description?.type !== 'offer') {
454
468
  // any other type causes libdatachannel to throw
455
469
  return;
456
470
  }
457
471
 
458
- this.#peerConnection.setLocalDescription(description?.type);
472
+ this.#peerConnection.setLocalDescription(description?.type as any);
459
473
  }
460
474
 
461
- async setRemoteDescription(description) {
475
+ async setRemoteDescription(description: globalThis.RTCSessionDescriptionInit): Promise<void> {
462
476
  if (description.sdp == null) {
463
477
  throw new DOMException('Remote SDP must be set');
464
478
  }
465
479
 
466
- this.#peerConnection.setRemoteDescription(description.sdp, description.type);
480
+ this.#peerConnection.setRemoteDescription(description.sdp, description.type as any);
467
481
  }
468
482
  }
469
483
 
470
- function createDeferredPromise() {
471
- let resolve, reject;
484
+ function createDeferredPromise(): any {
485
+ let resolve: any, reject: any;
472
486
 
473
- let promise = new Promise(function (_resolve, _reject) {
487
+ const promise = new Promise(function (_resolve, _reject) {
474
488
  resolve = _resolve;
475
489
  reject = _reject;
476
490
  });
477
491
 
478
- promise.resolve = resolve;
479
- promise.reject = reject;
492
+ (promise as any).resolve = resolve;
493
+ (promise as any).reject = reject;
480
494
  return promise;
481
495
  }
482
496
 
483
- function getRandomString(length) {
497
+ function getRandomString(length): string {
484
498
  return Math.random()
485
499
  .toString(36)
486
500
  .substring(2, 2 + length);
@@ -1,18 +1,20 @@
1
- import RTCDtlsTransport from './RTCDtlsTransport.js';
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import RTCDtlsTransport from './RTCDtlsTransport';
3
+ import RTCPeerConnection from './RTCPeerConnection';
2
4
 
3
- export default class _RTCSctpTransport extends EventTarget {
4
- #pc = null;
5
+ export default class RTCSctpTransport extends EventTarget implements globalThis.RTCSctpTransport {
6
+ #pc: RTCPeerConnection = null;
5
7
  #extraFunctions = null;
6
- #transport = null;
8
+ #transport: RTCDtlsTransport = null;
7
9
 
8
- onstatechange = null;
10
+ onstatechange: ((this: RTCSctpTransport, ev: Event) => any) | null = null;
9
11
 
10
- constructor({ pc, extraFunctions }) {
12
+ constructor(initial: { pc: RTCPeerConnection, extraFunctions }) {
11
13
  super();
12
- this.#pc = pc;
13
- this.#extraFunctions = extraFunctions;
14
+ this.#pc = initial.pc;
15
+ this.#extraFunctions = initial.extraFunctions;
14
16
 
15
- this.#transport = new RTCDtlsTransport({ pc, extraFunctions });
17
+ this.#transport = new RTCDtlsTransport({ pc: initial.pc, extraFunctions: initial.extraFunctions });
16
18
 
17
19
  // forward peerConnection events
18
20
  this.#pc.addEventListener('connectionstatechange', () => {
@@ -25,29 +27,29 @@ export default class _RTCSctpTransport extends EventTarget {
25
27
  });
26
28
  }
27
29
 
28
- get maxChannels() {
30
+ get maxChannels(): number | null {
29
31
  if (this.state !== 'connected') return null;
30
32
  return this.#pc ? this.#extraFunctions.maxDataChannelId() : 0;
31
33
  }
32
34
 
33
- get maxMessageSize() {
35
+ get maxMessageSize(): number {
34
36
  if (this.state !== 'connected') return null;
35
37
  return this.#pc ? this.#extraFunctions.maxMessageSize() : 0;
36
38
  }
37
39
 
38
- get state() {
40
+ get state(): globalThis.RTCSctpTransportState {
39
41
  // reduce state from new, connecting, connected, disconnected, failed, closed, unknown
40
42
  // to RTCSctpTransport states connecting, connected, closed
41
43
  let state = this.#pc.connectionState;
42
44
  if (state === 'new' || state === 'connecting') {
43
45
  state = 'connecting';
44
- } else if (state === 'disconnected' || state === 'failed' || state === 'closed' || state === 'unknown') {
46
+ } else if (state === 'disconnected' || state === 'failed' || state === 'closed') {
45
47
  state = 'closed';
46
48
  }
47
49
  return state;
48
50
  }
49
51
 
50
- get transport() {
52
+ get transport(): RTCDtlsTransport {
51
53
  return this.#transport;
52
54
  }
53
55
  }
@@ -6,29 +6,24 @@
6
6
  // sdp: 'v=0\r\no=- 1234567890 1234567890 IN IP4 192.168.1.1\r\ns=-\r\nt=0 0\r\na=ice-ufrag:abcd\r\na=ice-pwd:efgh\r\n'
7
7
  // };
8
8
 
9
- export default class _RTCSessionDescription {
10
- #type;
11
- #sdp;
12
-
13
- constructor(init = {}) {
14
- // Allow Empty Constructor
15
- // if (!init || !init.type || !init.sdp) {
16
- // throw new DOMException('Type and sdp properties are required.');
17
- // }
9
+ export default class RTCSessionDescription implements globalThis.RTCSessionDescriptionInit {
10
+ #type: globalThis.RTCSdpType;
11
+ #sdp: string;
18
12
 
13
+ constructor(init: globalThis.RTCSessionDescriptionInit) {
19
14
  this.#type = init ? init.type : null;
20
15
  this.#sdp = init ? init.sdp : null;
21
16
  }
22
17
 
23
- get type() {
18
+ get type(): globalThis.RTCSdpType {
24
19
  return this.#type;
25
20
  }
26
21
 
27
- get sdp() {
22
+ get sdp(): string {
28
23
  return this.#sdp;
29
24
  }
30
25
 
31
- toJSON() {
26
+ toJSON(): globalThis.RTCSessionDescriptionInit {
32
27
  return {
33
28
  sdp: this.#sdp,
34
29
  type: this.#type,
@@ -1,13 +1,13 @@
1
- import RTCCertificate from './RTCCertificate.js';
2
- import RTCDataChannel from './RTCDataChannel.js';
3
- import RTCDtlsTransport from './RTCDtlsTransport.js';
4
- import RTCIceCandidate from './RTCIceCandidate.js';
5
- import RTCIceTransport from './RTCIceTransport.js';
6
- import RTCPeerConnection from './RTCPeerConnection.js';
7
- import RTCSctpTransport from './RTCSctpTransport.js';
8
- import RTCSessionDescription from './RTCSessionDescription.js';
9
- import { RTCDataChannelEvent, RTCPeerConnectionIceEvent } from './Events.js';
10
- import RTCError from './RTCError.js';
1
+ import RTCCertificate from './RTCCertificate';
2
+ import RTCDataChannel from './RTCDataChannel';
3
+ import RTCDtlsTransport from './RTCDtlsTransport';
4
+ import RTCIceCandidate from './RTCIceCandidate';
5
+ import RTCIceTransport from './RTCIceTransport';
6
+ import RTCPeerConnection from './RTCPeerConnection';
7
+ import RTCSctpTransport from './RTCSctpTransport';
8
+ import RTCSessionDescription from './RTCSessionDescription';
9
+ import { RTCDataChannelEvent, RTCPeerConnectionIceEvent } from './Events';
10
+ import RTCError from './RTCError';
11
11
 
12
12
  export {
13
13
  RTCCertificate,
package/tsconfig.json CHANGED
@@ -1,14 +1,21 @@
1
1
  {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "module": "commonjs",
5
- "declaration": true,
6
- "strict": true,
7
- "esModuleInterop": true,
8
- "noEmit": true
9
- },
10
- "exclude": [
11
- "node_modules",
12
- "build"
13
- ]
14
- }
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "moduleResolution": "Node",
5
+ "rootDir": ".",
6
+ "importsNotUsedAsValues": "remove",
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "noImplicitAny": false,
11
+ "noImplicitThis": false,
12
+ "noImplicitReturns": true,
13
+ "strictNullChecks": false,
14
+ "noUnusedLocals": true,
15
+ "alwaysStrict": true,
16
+ "outDir": "./dist",
17
+ "module": "CommonJS"
18
+ },
19
+ "include": ["src/**/*", "test/**/*"],
20
+ "exclude": ["node_modules", "dist", "src/cpp", "test/wpt-tests/wpt"]
21
+ }
package/.eslintrc.cjs DELETED
@@ -1,8 +0,0 @@
1
- module.exports = {
2
- parser: '@typescript-eslint/parser',
3
- plugins: ['@typescript-eslint'],
4
- extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
5
- env: {
6
- node: true,
7
- },
8
- };
@@ -1,2 +0,0 @@
1
- # These are supported funding model platforms
2
- open_collective: node-datachannel