node-datachannel 0.26.0 → 0.28.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 (103) hide show
  1. package/.eslintrc.json +25 -25
  2. package/.prettierignore +4 -0
  3. package/.prettierrc +1 -0
  4. package/API.md +4 -4
  5. package/BULDING.md +11 -2
  6. package/CMakeLists.txt +37 -13
  7. package/README.md +22 -18
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs/lib/datachannel-stream.cjs +13 -13
  10. package/dist/cjs/lib/datachannel-stream.cjs.map +1 -1
  11. package/dist/cjs/lib/index.cjs +1 -3
  12. package/dist/cjs/lib/index.cjs.map +1 -1
  13. package/dist/cjs/lib/websocket-server.cjs.map +1 -1
  14. package/dist/cjs/lib/websocket.cjs.map +1 -1
  15. package/dist/cjs/polyfill/Events.cjs +52 -3
  16. package/dist/cjs/polyfill/Events.cjs.map +1 -1
  17. package/dist/cjs/polyfill/Exception.cjs.map +1 -1
  18. package/dist/cjs/polyfill/RTCCertificate.cjs +5 -2
  19. package/dist/cjs/polyfill/RTCCertificate.cjs.map +1 -1
  20. package/dist/cjs/polyfill/RTCDataChannel.cjs +27 -19
  21. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
  22. package/dist/cjs/polyfill/RTCDtlsTransport.cjs +6 -5
  23. package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -1
  24. package/dist/cjs/polyfill/RTCError.cjs +9 -2
  25. package/dist/cjs/polyfill/RTCError.cjs.map +1 -1
  26. package/dist/cjs/polyfill/RTCIceCandidate.cjs +13 -8
  27. package/dist/cjs/polyfill/RTCIceCandidate.cjs.map +1 -1
  28. package/dist/cjs/polyfill/RTCIceTransport.cjs +26 -16
  29. package/dist/cjs/polyfill/RTCIceTransport.cjs.map +1 -1
  30. package/dist/cjs/polyfill/RTCPeerConnection.cjs +80 -70
  31. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
  32. package/dist/cjs/polyfill/RTCSctpTransport.cjs +9 -11
  33. package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -1
  34. package/dist/cjs/polyfill/RTCSessionDescription.cjs +10 -2
  35. package/dist/cjs/polyfill/RTCSessionDescription.cjs.map +1 -1
  36. package/dist/cjs/polyfill/index.cjs.map +1 -1
  37. package/dist/esm/index.mjs.map +1 -1
  38. package/dist/esm/lib/datachannel-stream.mjs.map +1 -1
  39. package/dist/esm/lib/index.mjs +1 -3
  40. package/dist/esm/lib/index.mjs.map +1 -1
  41. package/dist/esm/lib/websocket-server.mjs.map +1 -1
  42. package/dist/esm/lib/websocket.mjs.map +1 -1
  43. package/dist/esm/polyfill/Events.mjs +52 -4
  44. package/dist/esm/polyfill/Events.mjs.map +1 -1
  45. package/dist/esm/polyfill/Exception.mjs.map +1 -1
  46. package/dist/esm/polyfill/RTCCertificate.mjs +5 -2
  47. package/dist/esm/polyfill/RTCCertificate.mjs.map +1 -1
  48. package/dist/esm/polyfill/RTCDataChannel.mjs +27 -19
  49. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
  50. package/dist/esm/polyfill/RTCDtlsTransport.mjs +6 -5
  51. package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -1
  52. package/dist/esm/polyfill/RTCError.mjs +9 -2
  53. package/dist/esm/polyfill/RTCError.mjs.map +1 -1
  54. package/dist/esm/polyfill/RTCIceCandidate.mjs +13 -8
  55. package/dist/esm/polyfill/RTCIceCandidate.mjs.map +1 -1
  56. package/dist/esm/polyfill/RTCIceTransport.mjs +26 -17
  57. package/dist/esm/polyfill/RTCIceTransport.mjs.map +1 -1
  58. package/dist/esm/polyfill/RTCPeerConnection.mjs +80 -70
  59. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
  60. package/dist/esm/polyfill/RTCSctpTransport.mjs +9 -11
  61. package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -1
  62. package/dist/esm/polyfill/RTCSessionDescription.mjs +10 -2
  63. package/dist/esm/polyfill/RTCSessionDescription.mjs.map +1 -1
  64. package/dist/esm/polyfill/index.mjs.map +1 -1
  65. package/dist/types/lib/index.d.ts +5 -2
  66. package/dist/types/lib/types.d.ts +6 -6
  67. package/dist/types/polyfill/Events.d.ts +3 -3
  68. package/dist/types/polyfill/RTCCertificate.d.ts +1 -0
  69. package/dist/types/polyfill/RTCDataChannel.d.ts +6 -6
  70. package/dist/types/polyfill/RTCDtlsTransport.d.ts +4 -6
  71. package/dist/types/polyfill/RTCError.d.ts +1 -0
  72. package/dist/types/polyfill/RTCIceCandidate.d.ts +1 -1
  73. package/dist/types/polyfill/RTCIceTransport.d.ts +16 -10
  74. package/dist/types/polyfill/RTCPeerConnection.d.ts +29 -22
  75. package/dist/types/polyfill/RTCSctpTransport.d.ts +2 -4
  76. package/dist/types/polyfill/RTCSessionDescription.d.ts +1 -0
  77. package/jest.config.ts +8 -8
  78. package/package.json +114 -112
  79. package/rollup.config.mjs +1 -1
  80. package/src/cpp/data-channel-wrapper.cpp +11 -0
  81. package/src/cpp/main.cpp +14 -0
  82. package/src/cpp/peer-connection-wrapper.cpp +9 -1
  83. package/src/cpp/peer-connection-wrapper.h +5 -1
  84. package/src/cpp/rtc-wrapper.cpp +18 -0
  85. package/src/index.ts +0 -1
  86. package/src/lib/datachannel-stream.ts +83 -84
  87. package/src/lib/index.ts +165 -139
  88. package/src/lib/types.ts +97 -72
  89. package/src/lib/websocket-server.ts +25 -25
  90. package/src/lib/websocket.ts +19 -19
  91. package/src/polyfill/Events.ts +65 -19
  92. package/src/polyfill/Exception.ts +24 -24
  93. package/src/polyfill/README.md +8 -8
  94. package/src/polyfill/RTCCertificate.ts +16 -12
  95. package/src/polyfill/RTCDataChannel.ts +198 -192
  96. package/src/polyfill/RTCDtlsTransport.ts +38 -39
  97. package/src/polyfill/RTCError.ts +63 -56
  98. package/src/polyfill/RTCIceCandidate.ts +120 -116
  99. package/src/polyfill/RTCIceTransport.ts +71 -64
  100. package/src/polyfill/RTCPeerConnection.ts +436 -420
  101. package/src/polyfill/RTCSctpTransport.ts +43 -47
  102. package/src/polyfill/RTCSessionDescription.ts +26 -17
  103. package/src/polyfill/index.ts +22 -22
@@ -3,45 +3,44 @@ import RTCIceTransport from './RTCIceTransport';
3
3
  import RTCPeerConnection from './RTCPeerConnection';
4
4
 
5
5
  export default class RTCDtlsTransport extends EventTarget implements globalThis.RTCDtlsTransport {
6
- #pc: RTCPeerConnection = null;
7
- #iceTransport = null;
8
-
9
- onstatechange: ((this: RTCDtlsTransport, ev: Event) => any) | null = null;
10
- onerror: ((this: RTCDtlsTransport, ev: Event) => any) | null = null;
11
-
12
- constructor(init: { pc: RTCPeerConnection, extraFunctions }) {
13
- super();
14
- this.#pc = init.pc;
15
-
16
- this.#iceTransport = new RTCIceTransport({ pc: init.pc, extraFunctions: init.extraFunctions });
17
-
18
- // forward peerConnection events
19
- this.#pc.addEventListener('connectionstatechange', () => {
20
- this.dispatchEvent(new Event('statechange'));
21
- });
22
-
23
- // forward events to properties
24
- this.addEventListener('statechange', (e) => {
25
- if (this.onstatechange) this.onstatechange(e);
26
- });
6
+ #pc: RTCPeerConnection = null;
7
+ #iceTransport = null;
8
+
9
+ onstatechange: globalThis.RTCDtlsTransport['onstatechange'] = null;
10
+ onerror: globalThis.RTCDtlsTransport['onstatechange'] = null;
11
+
12
+ constructor(init: { pc: RTCPeerConnection }) {
13
+ super();
14
+ this.#pc = init.pc;
15
+
16
+ this.#iceTransport = new RTCIceTransport({
17
+ pc: init.pc
18
+ });
19
+
20
+ // forward peerConnection events
21
+ this.#pc.addEventListener('connectionstatechange', () => {
22
+ const e = new Event('statechange');
23
+ this.dispatchEvent(e);
24
+ this.onstatechange?.(e);
25
+ });
26
+ }
27
+
28
+ get iceTransport(): globalThis.RTCIceTransport {
29
+ return this.#iceTransport;
30
+ }
31
+
32
+ get state(): globalThis.RTCDtlsTransportState {
33
+ // reduce state from new, connecting, connected, disconnected, failed, closed, unknown
34
+ // to RTCDtlsTRansport states new, connecting, connected, closed, failed
35
+ let state = this.#pc ? this.#pc.connectionState : 'new';
36
+ if (state === 'disconnected') {
37
+ state = 'closed';
27
38
  }
39
+ return state;
40
+ }
28
41
 
29
- get iceTransport(): RTCIceTransport {
30
- return this.#iceTransport;
31
- }
32
-
33
- get state(): RTCDtlsTransportState {
34
- // reduce state from new, connecting, connected, disconnected, failed, closed, unknown
35
- // to RTCDtlsTRansport states new, connecting, connected, closed, failed
36
- let state = this.#pc ? this.#pc.connectionState : 'new';
37
- if (state === 'disconnected') {
38
- state = 'closed';
39
- }
40
- return state;
41
- }
42
-
43
- getRemoteCertificates(): ArrayBuffer[] {
44
- // TODO: implement
45
- return [new ArrayBuffer(0)];
46
- }
42
+ getRemoteCertificates(): ArrayBuffer[] {
43
+ // TODO: implement
44
+ return [new ArrayBuffer(0)];
45
+ }
47
46
  }
@@ -1,71 +1,78 @@
1
1
  export default class RTCError extends DOMException implements globalThis.RTCError {
2
- #errorDetail: RTCErrorDetailType;
3
- #receivedAlert: number | null;
4
- #sctpCauseCode: number | null;
5
- #sdpLineNumber: number | null;
6
- #sentAlert: number | null;
2
+ #errorDetail: globalThis.RTCErrorDetailType;
3
+ #receivedAlert: number | null;
4
+ #sctpCauseCode: number | null;
5
+ #sdpLineNumber: number | null;
6
+ #sentAlert: number | null;
7
+ #httpRequestStatusCode: number | null;
7
8
 
8
- constructor(init: globalThis.RTCErrorInit, message?: string) {
9
- super(message, 'OperationError');
9
+ constructor(init: globalThis.RTCErrorInit, message?: string) {
10
+ super(message, 'OperationError');
10
11
 
11
- if (!init || !init.errorDetail) throw new TypeError('Cannot construct RTCError, errorDetail is required');
12
- if (
13
- [
14
- 'data-channel-failure',
15
- 'dtls-failure',
16
- 'fingerprint-failure',
17
- 'hardware-encoder-error',
18
- 'hardware-encoder-not-available',
19
- 'sctp-failure',
20
- 'sdp-syntax-error',
21
- ].indexOf(init.errorDetail) === -1
22
- )
23
- throw new TypeError('Cannot construct RTCError, errorDetail is invalid');
12
+ if (!init || !init.errorDetail)
13
+ throw new TypeError('Cannot construct RTCError, errorDetail is required');
14
+ if (
15
+ [
16
+ 'data-channel-failure',
17
+ 'dtls-failure',
18
+ 'fingerprint-failure',
19
+ 'hardware-encoder-error',
20
+ 'hardware-encoder-not-available',
21
+ 'sctp-failure',
22
+ 'sdp-syntax-error',
23
+ ].indexOf(init.errorDetail) === -1
24
+ )
25
+ throw new TypeError('Cannot construct RTCError, errorDetail is invalid');
24
26
 
25
- this.#errorDetail = init.errorDetail;
26
- this.#receivedAlert = init.receivedAlert ?? null;
27
- this.#sctpCauseCode = init.sctpCauseCode ?? null;
28
- this.#sdpLineNumber = init.sdpLineNumber ?? null;
29
- this.#sentAlert = init.sentAlert ?? null;
30
- }
27
+ this.#errorDetail = init.errorDetail;
28
+ this.#receivedAlert = init.receivedAlert ?? null;
29
+ this.#sctpCauseCode = init.sctpCauseCode ?? null;
30
+ this.#sdpLineNumber = init.sdpLineNumber ?? null;
31
+ this.#sentAlert = init.sentAlert ?? null;
32
+ this.#httpRequestStatusCode = init.httpRequestStatusCode ?? null;
33
+ }
31
34
 
32
- get errorDetail(): globalThis.RTCErrorDetailType {
33
- return this.#errorDetail;
34
- }
35
+ get errorDetail(): globalThis.RTCErrorDetailType {
36
+ return this.#errorDetail;
37
+ }
35
38
 
36
- set errorDetail(_value) {
37
- throw new TypeError('Cannot set errorDetail, it is read-only');
38
- }
39
+ set errorDetail(_value) {
40
+ throw new TypeError('Cannot set errorDetail, it is read-only');
41
+ }
39
42
 
40
- get receivedAlert(): number | null {
41
- return this.#receivedAlert;
42
- }
43
+ get receivedAlert(): number | null {
44
+ return this.#receivedAlert;
45
+ }
43
46
 
44
- set receivedAlert(_value) {
45
- throw new TypeError('Cannot set receivedAlert, it is read-only');
46
- }
47
+ set receivedAlert(_value) {
48
+ throw new TypeError('Cannot set receivedAlert, it is read-only');
49
+ }
47
50
 
48
- get sctpCauseCode(): number | null {
49
- return this.#sctpCauseCode;
50
- }
51
+ get sctpCauseCode(): number | null {
52
+ return this.#sctpCauseCode;
53
+ }
51
54
 
52
- set sctpCauseCode(_value) {
53
- throw new TypeError('Cannot set sctpCauseCode, it is read-only');
54
- }
55
+ set sctpCauseCode(_value) {
56
+ throw new TypeError('Cannot set sctpCauseCode, it is read-only');
57
+ }
55
58
 
56
- get sdpLineNumber(): number | null {
57
- return this.#sdpLineNumber;
58
- }
59
+ get httpRequestStatusCode(): number | null {
60
+ return this.#httpRequestStatusCode;
61
+ }
59
62
 
60
- set sdpLineNumber(_value) {
61
- throw new TypeError('Cannot set sdpLineNumber, it is read-only');
62
- }
63
+ get sdpLineNumber(): number | null {
64
+ return this.#sdpLineNumber;
65
+ }
63
66
 
64
- get sentAlert(): number | null {
65
- return this.#sentAlert;
66
- }
67
+ set sdpLineNumber(_value) {
68
+ throw new TypeError('Cannot set sdpLineNumber, it is read-only');
69
+ }
67
70
 
68
- set sentAlert(_value) {
69
- throw new TypeError('Cannot set sentAlert, it is read-only');
70
- }
71
+ get sentAlert(): number | null {
72
+ return this.#sentAlert;
73
+ }
74
+
75
+ set sentAlert(_value) {
76
+ throw new TypeError('Cannot set sentAlert, it is read-only');
77
+ }
71
78
  }
@@ -2,123 +2,127 @@
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
-
6
5
  export default class RTCIceCandidate implements globalThis.RTCIceCandidate {
7
- #address: string | null;
8
- #candidate: string;
9
- #component: globalThis.RTCIceComponent | null;
10
- #foundation: string | null;
11
- #port: number | null;
12
- #priority: number | null;
13
- #protocol: globalThis.RTCIceProtocol | null;
14
- #relatedAddress: string | null;
15
- #relatedPort: number | null;
16
- #sdpMLineIndex: number | null;
17
- #sdpMid: string | null;
18
- #tcpType: globalThis.RTCIceTcpCandidateType | null;
19
- #type: globalThis.RTCIceCandidateType | null;
20
- #usernameFragment: string | null;
21
-
22
- constructor({ candidate, sdpMLineIndex, sdpMid, usernameFragment }: globalThis.RTCIceCandidateInit) {
23
- if (sdpMLineIndex == null && sdpMid == null)
24
- throw new TypeError('At least one of sdpMLineIndex or sdpMid must be specified');
25
-
26
- this.#candidate = candidate === null ? 'null' : candidate ?? '';
27
- this.#sdpMLineIndex = sdpMLineIndex ?? null;
28
- this.#sdpMid = sdpMid ?? null;
29
- this.#usernameFragment = usernameFragment ?? null;
30
-
31
- if (candidate) {
32
- const fields = candidate.split(' ');
33
- this.#foundation = fields[0].replace('candidate:', ''); // remove text candidate:
34
- this.#component = fields[1] == '1' ? 'rtp' : 'rtcp';
35
- this.#protocol = fields[2] as globalThis.RTCIceProtocol;
36
- this.#priority = parseInt(fields[3], 10);
37
- this.#address = fields[4];
38
- this.#port = parseInt(fields[5], 10);
39
- this.#type = fields[7] as globalThis.RTCIceCandidateType;
40
- this.#tcpType = null;
41
- this.#relatedAddress = null;
42
- this.#relatedPort = null;
43
-
44
- // Parse the candidate string to extract relatedPort and relatedAddress
45
- for (let i = 8; i < fields.length; i++) {
46
- const field = fields[i];
47
- if (field === 'raddr') {
48
- this.#relatedAddress = fields[i + 1];
49
- } else if (field === 'rport') {
50
- this.#relatedPort = parseInt(fields[i + 1], 10);
51
- }
52
-
53
- if (this.#protocol === 'tcp' && field === 'tcptype') {
54
- this.#tcpType = fields[i + 1] as globalThis.RTCIceTcpCandidateType;
55
- }
56
- }
6
+ #address: string | null;
7
+ #candidate: string;
8
+ #component: globalThis.RTCIceComponent | null;
9
+ #foundation: string | null;
10
+ #port: number | null;
11
+ #priority: number | null;
12
+ #protocol: globalThis.RTCIceProtocol | null;
13
+ #relatedAddress: string | null;
14
+ #relatedPort: number | null;
15
+ #sdpMLineIndex: number | null;
16
+ #sdpMid: string | null;
17
+ #tcpType: globalThis.RTCIceTcpCandidateType | null;
18
+ #type: globalThis.RTCIceCandidateType | null;
19
+ #usernameFragment: string | null;
20
+
21
+ constructor({
22
+ candidate,
23
+ sdpMLineIndex,
24
+ sdpMid,
25
+ usernameFragment,
26
+ }: globalThis.RTCIceCandidateInit) {
27
+ if (sdpMLineIndex == null && sdpMid == null)
28
+ throw new TypeError('At least one of sdpMLineIndex or sdpMid must be specified');
29
+
30
+ this.#candidate = candidate === null ? 'null' : (candidate ?? '');
31
+ this.#sdpMLineIndex = sdpMLineIndex ?? null;
32
+ this.#sdpMid = sdpMid ?? null;
33
+ this.#usernameFragment = usernameFragment ?? null;
34
+
35
+ if (candidate) {
36
+ const fields = candidate.split(' ');
37
+ this.#foundation = fields[0]!.replace('candidate:', ''); // remove text candidate:
38
+ this.#component = fields[1] == '1' ? 'rtp' : 'rtcp';
39
+ this.#protocol = fields[2] as globalThis.RTCIceProtocol;
40
+ this.#priority = parseInt(fields[3], 10);
41
+ this.#address = fields[4];
42
+ this.#port = parseInt(fields[5], 10);
43
+ this.#type = fields[7] as globalThis.RTCIceCandidateType;
44
+ this.#tcpType = null;
45
+ this.#relatedAddress = null;
46
+ this.#relatedPort = null;
47
+
48
+ // Parse the candidate string to extract relatedPort and relatedAddress
49
+ for (let i = 8; i < fields.length; i++) {
50
+ const field = fields[i];
51
+ if (field === 'raddr') {
52
+ this.#relatedAddress = fields[i + 1];
53
+ } else if (field === 'rport') {
54
+ this.#relatedPort = parseInt(fields[i + 1], 10);
57
55
  }
58
- }
59
-
60
- get address(): string | null {
61
- return this.#address || null;
62
- }
63
-
64
- get candidate(): string {
65
- return this.#candidate;
66
- }
67
-
68
- get component(): globalThis.RTCIceComponent | null {
69
- return this.#component;
70
- }
71
-
72
- get foundation(): string | null {
73
- return this.#foundation || null;
74
- }
75
-
76
- get port(): number | null {
77
- return this.#port || null;
78
- }
79
-
80
- get priority(): number | null {
81
- return this.#priority || null;
82
- }
83
-
84
- get protocol(): globalThis.RTCIceProtocol | null {
85
- return this.#protocol || null;
86
- }
87
56
 
88
- get relatedAddress(): string | null {
89
- return this.#relatedAddress;
90
- }
91
-
92
- get relatedPort(): number | null {
93
- return this.#relatedPort || null;
94
- }
95
-
96
- get sdpMLineIndex(): number | null {
97
- return this.#sdpMLineIndex;
98
- }
99
-
100
- get sdpMid(): string | null {
101
- return this.#sdpMid;
102
- }
103
-
104
- get tcpType(): globalThis.RTCIceTcpCandidateType | null {
105
- return this.#tcpType;
106
- }
107
-
108
- get type(): globalThis.RTCIceCandidateType | null {
109
- return this.#type || null;
110
- }
111
-
112
- get usernameFragment(): string | null {
113
- return this.#usernameFragment;
114
- }
115
-
116
- toJSON(): globalThis.RTCIceCandidateInit {
117
- return {
118
- candidate: this.#candidate,
119
- sdpMLineIndex: this.#sdpMLineIndex,
120
- sdpMid: this.#sdpMid,
121
- usernameFragment: this.#usernameFragment,
122
- };
123
- }
57
+ if (this.#protocol === 'tcp' && field === 'tcptype') {
58
+ this.#tcpType = fields[i + 1] as globalThis.RTCIceTcpCandidateType;
59
+ }
60
+ }
61
+ }
62
+ }
63
+
64
+ get address(): string | null {
65
+ return this.#address ?? null;
66
+ }
67
+
68
+ get candidate(): string {
69
+ return this.#candidate;
70
+ }
71
+
72
+ get component(): globalThis.RTCIceComponent | null {
73
+ return this.#component;
74
+ }
75
+
76
+ get foundation(): string | null {
77
+ return this.#foundation ?? null;
78
+ }
79
+
80
+ get port(): number | null {
81
+ return this.#port ?? null;
82
+ }
83
+
84
+ get priority(): number | null {
85
+ return this.#priority ?? null;
86
+ }
87
+
88
+ get protocol(): globalThis.RTCIceProtocol | null {
89
+ return this.#protocol ?? null;
90
+ }
91
+
92
+ get relatedAddress(): string | null {
93
+ return this.#relatedAddress;
94
+ }
95
+
96
+ get relatedPort(): number | null {
97
+ return this.#relatedPort ?? null;
98
+ }
99
+
100
+ get sdpMLineIndex(): number | null {
101
+ return this.#sdpMLineIndex;
102
+ }
103
+
104
+ get sdpMid(): string | null {
105
+ return this.#sdpMid;
106
+ }
107
+
108
+ get tcpType(): globalThis.RTCIceTcpCandidateType | null {
109
+ return this.#tcpType;
110
+ }
111
+
112
+ get type(): globalThis.RTCIceCandidateType | null {
113
+ return this.#type ?? null;
114
+ }
115
+
116
+ get usernameFragment(): string | null {
117
+ return this.#usernameFragment;
118
+ }
119
+
120
+ toJSON(): globalThis.RTCIceCandidateInit {
121
+ return {
122
+ candidate: this.#candidate,
123
+ sdpMLineIndex: this.#sdpMLineIndex,
124
+ sdpMid: this.#sdpMid,
125
+ usernameFragment: this.#usernameFragment,
126
+ };
127
+ }
124
128
  }
@@ -3,81 +3,88 @@ import RTCIceCandidate from './RTCIceCandidate';
3
3
  import RTCPeerConnection from './RTCPeerConnection';
4
4
 
5
5
  export default class RTCIceTransport extends EventTarget implements globalThis.RTCIceTransport {
6
- #pc: RTCPeerConnection = null;
7
- #extraFunctions = null;
6
+ #pc: RTCPeerConnection = null;
8
7
 
9
- ongatheringstatechange: ((this: RTCIceTransport, ev: Event) => any) | null = null;
10
- onselectedcandidatepairchange: ((this: RTCIceTransport, ev: Event) => any) | null = null;
11
- onstatechange: ((this: RTCIceTransport, ev: Event) => any) | null = null;
8
+ ongatheringstatechange: globalThis.RTCIceTransport['ongatheringstatechange'] = null;
9
+ onselectedcandidatepairchange: globalThis.RTCIceTransport['onselectedcandidatepairchange'] = null;
10
+ onstatechange: globalThis.RTCIceTransport['onstatechange'] = null;
12
11
 
13
- constructor(init: { pc: RTCPeerConnection, extraFunctions }) {
14
- super();
15
- this.#pc = init.pc;
16
- this.#extraFunctions = init.extraFunctions;
12
+ constructor(init: { pc: RTCPeerConnection }) {
13
+ super();
14
+ this.#pc = init.pc;
17
15
 
18
- // forward peerConnection events
19
- this.#pc.addEventListener('icegatheringstatechange', () => {
20
- this.dispatchEvent(new Event('gatheringstatechange'));
21
- });
22
- this.#pc.addEventListener('iceconnectionstatechange', () => {
23
- this.dispatchEvent(new Event('statechange'));
24
- });
16
+ this.#pc.addEventListener('icegatheringstatechange', () => {
17
+ const e = new Event('gatheringstatechange');
18
+ this.dispatchEvent(e);
19
+ this.ongatheringstatechange?.(e);
20
+ });
21
+ this.#pc.addEventListener('iceconnectionstatechange', () => {
22
+ const e = new Event('statechange');
23
+ this.dispatchEvent(e);
24
+ this.onstatechange?.(e);
25
+ });
26
+ }
25
27
 
26
- // forward events to properties
27
- this.addEventListener('gatheringstatechange', (e) => {
28
- if (this.ongatheringstatechange) this.ongatheringstatechange(e);
29
- });
30
- this.addEventListener('statechange', (e) => {
31
- if (this.onstatechange) this.onstatechange(e);
32
- });
33
- }
28
+ get component(): globalThis.RTCIceComponent {
29
+ const cp = this.getSelectedCandidatePair();
30
+ if (!cp?.local) return null;
31
+ return cp.local.component;
32
+ }
34
33
 
35
- get component(): globalThis.RTCIceComponent {
36
- const cp = this.getSelectedCandidatePair();
37
- if (!cp) return null;
38
- return cp.local.component;
39
- }
34
+ get gatheringState(): globalThis.RTCIceGatheringState {
35
+ return this.#pc ? this.#pc.iceGatheringState : 'new';
36
+ }
40
37
 
41
- get gatheringState(): globalThis.RTCIceGatheringState {
42
- return this.#pc ? this.#pc.iceGatheringState : 'new';
43
- }
38
+ get role(): globalThis.RTCIceRole {
39
+ return this.#pc.localDescription!.type == 'offer' ? 'controlling' : 'controlled';
40
+ }
44
41
 
45
- get role(): string {
46
- return this.#pc.localDescription.type == 'offer' ? 'controlling' : 'controlled';
47
- }
42
+ get state(): globalThis.RTCIceTransportState {
43
+ return this.#pc ? this.#pc.iceConnectionState : 'new';
44
+ }
48
45
 
49
- get state(): globalThis.RTCIceTransportState {
50
- return this.#pc ? this.#pc.iceConnectionState : 'new';
51
- }
46
+ getLocalCandidates(): globalThis.RTCIceCandidate[] {
47
+ return this.#pc?.ext_localCandidates ?? [];
48
+ }
52
49
 
53
- getLocalCandidates(): RTCIceCandidate[] {
54
- return this.#pc ? this.#extraFunctions.localCandidates() : [];
55
- }
50
+ getLocalParameters(): RTCIceParameters | null {
51
+ return new RTCIceParameters(
52
+ new RTCIceCandidate({
53
+ candidate: this.#pc.selectedCandidatePair()!.local.candidate,
54
+ sdpMLineIndex: 0,
55
+ }),
56
+ );
57
+ }
56
58
 
57
- getLocalParameters(): any {
58
- /** */
59
- }
59
+ getRemoteCandidates(): globalThis.RTCIceCandidate[] {
60
+ return this.#pc?.ext_remoteCandidates ?? [];
61
+ }
60
62
 
61
- getRemoteCandidates(): RTCIceCandidate[] {
62
- return this.#pc ? this.#extraFunctions.remoteCandidates() : [];
63
- }
63
+ getRemoteParameters(): any {
64
+ /** */
65
+ }
64
66
 
65
- getRemoteParameters(): any {
66
- /** */
67
- }
67
+ getSelectedCandidatePair(): globalThis.RTCIceCandidatePair | null {
68
+ const cp = this.#pc?.selectedCandidatePair();
69
+ if (!cp) return null;
70
+ return {
71
+ local: new RTCIceCandidate({
72
+ candidate: cp.local.candidate,
73
+ sdpMid: cp.local.mid,
74
+ }),
75
+ remote: new RTCIceCandidate({
76
+ candidate: cp.remote.candidate,
77
+ sdpMid: cp.remote.mid,
78
+ }),
79
+ };
80
+ }
81
+ }
68
82
 
69
- getSelectedCandidatePair(): globalThis.RTCIceCandidatePair | null {
70
- const cp = this.#extraFunctions.selectedCandidatePair();
71
- if (!cp) return null;
72
- return {
73
- local: new RTCIceCandidate({
74
- candidate: cp.local.candidate,
75
- sdpMid: cp.local.mid,
76
- }),
77
- remote: new RTCIceCandidate({
78
- candidate: cp.remote.candidate,
79
- sdpMid: cp.remote.mid,
80
- }),
81
- };
82
- }
83
+ export class RTCIceParameters implements globalThis.RTCIceParameters {
84
+ usernameFragment = '';
85
+ password = '';
86
+ constructor({ usernameFragment, password = '' }) {
87
+ this.usernameFragment = usernameFragment;
88
+ this.password = password;
89
+ }
83
90
  }