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.
- package/.eslintrc.json +25 -25
- package/.prettierignore +4 -0
- package/.prettierrc +1 -0
- package/API.md +4 -4
- package/BULDING.md +11 -2
- package/CMakeLists.txt +37 -13
- package/README.md +22 -18
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/lib/datachannel-stream.cjs +13 -13
- package/dist/cjs/lib/datachannel-stream.cjs.map +1 -1
- package/dist/cjs/lib/index.cjs +1 -3
- package/dist/cjs/lib/index.cjs.map +1 -1
- package/dist/cjs/lib/websocket-server.cjs.map +1 -1
- package/dist/cjs/lib/websocket.cjs.map +1 -1
- package/dist/cjs/polyfill/Events.cjs +52 -3
- package/dist/cjs/polyfill/Events.cjs.map +1 -1
- package/dist/cjs/polyfill/Exception.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCCertificate.cjs +5 -2
- package/dist/cjs/polyfill/RTCCertificate.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCDataChannel.cjs +27 -19
- package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCDtlsTransport.cjs +6 -5
- package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCError.cjs +9 -2
- package/dist/cjs/polyfill/RTCError.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCIceCandidate.cjs +13 -8
- package/dist/cjs/polyfill/RTCIceCandidate.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCIceTransport.cjs +26 -16
- package/dist/cjs/polyfill/RTCIceTransport.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCPeerConnection.cjs +80 -70
- package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCSctpTransport.cjs +9 -11
- package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -1
- package/dist/cjs/polyfill/RTCSessionDescription.cjs +10 -2
- package/dist/cjs/polyfill/RTCSessionDescription.cjs.map +1 -1
- package/dist/cjs/polyfill/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/datachannel-stream.mjs.map +1 -1
- package/dist/esm/lib/index.mjs +1 -3
- package/dist/esm/lib/index.mjs.map +1 -1
- package/dist/esm/lib/websocket-server.mjs.map +1 -1
- package/dist/esm/lib/websocket.mjs.map +1 -1
- package/dist/esm/polyfill/Events.mjs +52 -4
- package/dist/esm/polyfill/Events.mjs.map +1 -1
- package/dist/esm/polyfill/Exception.mjs.map +1 -1
- package/dist/esm/polyfill/RTCCertificate.mjs +5 -2
- package/dist/esm/polyfill/RTCCertificate.mjs.map +1 -1
- package/dist/esm/polyfill/RTCDataChannel.mjs +27 -19
- package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
- package/dist/esm/polyfill/RTCDtlsTransport.mjs +6 -5
- package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -1
- package/dist/esm/polyfill/RTCError.mjs +9 -2
- package/dist/esm/polyfill/RTCError.mjs.map +1 -1
- package/dist/esm/polyfill/RTCIceCandidate.mjs +13 -8
- package/dist/esm/polyfill/RTCIceCandidate.mjs.map +1 -1
- package/dist/esm/polyfill/RTCIceTransport.mjs +26 -17
- package/dist/esm/polyfill/RTCIceTransport.mjs.map +1 -1
- package/dist/esm/polyfill/RTCPeerConnection.mjs +80 -70
- package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
- package/dist/esm/polyfill/RTCSctpTransport.mjs +9 -11
- package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -1
- package/dist/esm/polyfill/RTCSessionDescription.mjs +10 -2
- package/dist/esm/polyfill/RTCSessionDescription.mjs.map +1 -1
- package/dist/esm/polyfill/index.mjs.map +1 -1
- package/dist/types/lib/index.d.ts +5 -2
- package/dist/types/lib/types.d.ts +6 -6
- package/dist/types/polyfill/Events.d.ts +3 -3
- package/dist/types/polyfill/RTCCertificate.d.ts +1 -0
- package/dist/types/polyfill/RTCDataChannel.d.ts +6 -6
- package/dist/types/polyfill/RTCDtlsTransport.d.ts +4 -6
- package/dist/types/polyfill/RTCError.d.ts +1 -0
- package/dist/types/polyfill/RTCIceCandidate.d.ts +1 -1
- package/dist/types/polyfill/RTCIceTransport.d.ts +16 -10
- package/dist/types/polyfill/RTCPeerConnection.d.ts +29 -22
- package/dist/types/polyfill/RTCSctpTransport.d.ts +2 -4
- package/dist/types/polyfill/RTCSessionDescription.d.ts +1 -0
- package/jest.config.ts +8 -8
- package/package.json +114 -112
- package/rollup.config.mjs +1 -1
- package/src/cpp/data-channel-wrapper.cpp +11 -0
- package/src/cpp/main.cpp +14 -0
- package/src/cpp/peer-connection-wrapper.cpp +9 -1
- package/src/cpp/peer-connection-wrapper.h +5 -1
- package/src/cpp/rtc-wrapper.cpp +18 -0
- package/src/index.ts +0 -1
- package/src/lib/datachannel-stream.ts +83 -84
- package/src/lib/index.ts +165 -139
- package/src/lib/types.ts +97 -72
- package/src/lib/websocket-server.ts +25 -25
- package/src/lib/websocket.ts +19 -19
- package/src/polyfill/Events.ts +65 -19
- package/src/polyfill/Exception.ts +24 -24
- package/src/polyfill/README.md +8 -8
- package/src/polyfill/RTCCertificate.ts +16 -12
- package/src/polyfill/RTCDataChannel.ts +198 -192
- package/src/polyfill/RTCDtlsTransport.ts +38 -39
- package/src/polyfill/RTCError.ts +63 -56
- package/src/polyfill/RTCIceCandidate.ts +120 -116
- package/src/polyfill/RTCIceTransport.ts +71 -64
- package/src/polyfill/RTCPeerConnection.ts +436 -420
- package/src/polyfill/RTCSctpTransport.ts +43 -47
- package/src/polyfill/RTCSessionDescription.ts +26 -17
- package/src/polyfill/index.ts +22 -22
package/src/polyfill/Events.ts
CHANGED
|
@@ -1,33 +1,79 @@
|
|
|
1
1
|
import RTCDataChannel from './RTCDataChannel';
|
|
2
|
-
import
|
|
3
|
-
|
|
2
|
+
import RTCError from './RTCError';
|
|
4
3
|
|
|
5
4
|
export class RTCPeerConnectionIceEvent extends Event implements globalThis.RTCPeerConnectionIceEvent {
|
|
6
|
-
|
|
5
|
+
#candidate: globalThis.RTCIceCandidate;
|
|
6
|
+
|
|
7
|
+
constructor(candidate: globalThis.RTCIceCandidate) {
|
|
8
|
+
super('icecandidate');
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
this.#candidate = candidate;
|
|
11
|
+
}
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
get candidate(): globalThis.RTCIceCandidate {
|
|
14
|
+
return this.#candidate;
|
|
15
|
+
}
|
|
13
16
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
get url(): string {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export class RTCDataChannelEvent extends Event implements globalThis.RTCDataChannelEvent {
|
|
20
|
-
|
|
23
|
+
#channel: RTCDataChannel;
|
|
24
|
+
|
|
25
|
+
constructor(type: string = 'datachannel', eventInitDict: globalThis.RTCDataChannelEventInit) {
|
|
26
|
+
super(type);
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
if (arguments.length === 0)
|
|
29
|
+
throw new TypeError(
|
|
30
|
+
`Failed to construct 'RTCDataChannelEvent': 2 arguments required, but only ${arguments.length} present.`,
|
|
31
|
+
);
|
|
32
|
+
if (typeof eventInitDict !== 'object')
|
|
33
|
+
throw new TypeError(
|
|
34
|
+
"Failed to construct 'RTCDataChannelEvent': The provided value is not of type 'RTCDataChannelEventInit'.",
|
|
35
|
+
);
|
|
36
|
+
if (!eventInitDict.channel)
|
|
37
|
+
throw new TypeError(
|
|
38
|
+
"Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Required member is undefined.",
|
|
39
|
+
);
|
|
40
|
+
if (eventInitDict.channel.constructor !== RTCDataChannel)
|
|
41
|
+
throw new TypeError(
|
|
42
|
+
"Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Failed to convert value to 'RTCDataChannel'.",
|
|
43
|
+
);
|
|
24
44
|
|
|
25
|
-
|
|
45
|
+
this.#channel = eventInitDict?.channel;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get channel(): RTCDataChannel {
|
|
49
|
+
return this.#channel;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
26
52
|
|
|
27
|
-
|
|
28
|
-
|
|
53
|
+
export class RTCErrorEvent extends Event implements globalThis.RTCErrorEvent {
|
|
54
|
+
#error: RTCError;
|
|
55
|
+
constructor(type: string, init: globalThis.RTCErrorEventInit) {
|
|
56
|
+
if (arguments.length < 2)
|
|
57
|
+
throw new TypeError(
|
|
58
|
+
`Failed to construct 'RTCErrorEvent': 2 arguments required, but only ${arguments.length} present.`,
|
|
59
|
+
);
|
|
60
|
+
if (typeof init !== 'object')
|
|
61
|
+
throw new TypeError(
|
|
62
|
+
"Failed to construct 'RTCErrorEvent': The provided value is not of type 'RTCErrorEventInit'.",
|
|
63
|
+
);
|
|
64
|
+
if (!init.error)
|
|
65
|
+
throw new TypeError(
|
|
66
|
+
"Failed to construct 'RTCErrorEvent': Failed to read the 'error' property from 'RTCErrorEventInit': Required member is undefined.",
|
|
67
|
+
);
|
|
68
|
+
if (init.error.constructor !== RTCError)
|
|
69
|
+
throw new TypeError(
|
|
70
|
+
"Failed to construct 'RTCErrorEvent': Failed to read the 'error' property from 'RTCErrorEventInit': Failed to convert value to 'RTCError'.",
|
|
71
|
+
);
|
|
72
|
+
super(type || 'error');
|
|
73
|
+
this.#error = init.error;
|
|
74
|
+
}
|
|
29
75
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
76
|
+
get error(): RTCError {
|
|
77
|
+
return this.#error;
|
|
78
|
+
}
|
|
33
79
|
}
|
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
2
|
|
|
3
3
|
export class InvalidStateError extends DOMException {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
4
|
+
constructor(msg: string) {
|
|
5
|
+
super(msg, 'InvalidStateError');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
8
|
|
|
9
9
|
export class TypeError extends DOMException {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
10
|
+
constructor(msg: string) {
|
|
11
|
+
super(msg, 'TypeError');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
14
|
|
|
15
15
|
export class OperationError extends DOMException {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
16
|
+
constructor(msg: string) {
|
|
17
|
+
super(msg, 'OperationError');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
20
|
|
|
21
21
|
export class NotFoundError extends DOMException {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
22
|
+
constructor(msg: string) {
|
|
23
|
+
super(msg, 'NotFoundError');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
26
|
|
|
27
27
|
export class InvalidAccessError extends DOMException {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
28
|
+
constructor(msg: string) {
|
|
29
|
+
super(msg, 'InvalidAccessError');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
32
|
|
|
33
33
|
export class SyntaxError extends DOMException {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
34
|
+
constructor(msg: string) {
|
|
35
|
+
super(msg, 'SyntaxError');
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/polyfill/README.md
CHANGED
|
@@ -20,22 +20,22 @@ var peer1 = new Peer({ initiator: true, wrtc: nodeDatachannelPolyfill });
|
|
|
20
20
|
var peer2 = new Peer({ wrtc: nodeDatachannelPolyfill });
|
|
21
21
|
|
|
22
22
|
peer1.on('signal', (data) => {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
// when peer1 has signaling data, give it to peer2 somehow
|
|
24
|
+
peer2.signal(data);
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
peer2.on('signal', (data) => {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
// when peer2 has signaling data, give it to peer1 somehow
|
|
29
|
+
peer1.signal(data);
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
peer1.on('connect', () => {
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
// wait for 'connect' event before using the data channel
|
|
34
|
+
peer1.send('hey peer2, how is it going?');
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
peer2.on('data', (data) => {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// got a data channel message
|
|
39
|
+
console.log('got a message from peer1: ' + data);
|
|
40
40
|
});
|
|
41
41
|
```
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
export default class RTCCertificate implements globalThis.RTCCertificate {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
#expires: number = 0;
|
|
3
|
+
#fingerprints: globalThis.RTCDtlsFingerprint[] = [];
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
constructor() {
|
|
6
|
+
this.#expires = null;
|
|
7
|
+
this.#fingerprints = [];
|
|
8
|
+
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
get expires(): number {
|
|
11
|
+
return this.#expires;
|
|
12
|
+
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
getFingerprints(): globalThis.RTCDtlsFingerprint[] {
|
|
15
|
+
return this.#fingerprints;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getAlgorithm(): string {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
17
21
|
}
|
|
@@ -1,200 +1,206 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import * as exceptions from './Exception';
|
|
3
3
|
import { DataChannel } from '../lib/index';
|
|
4
|
+
import { RTCErrorEvent } from './Events';
|
|
4
5
|
|
|
5
6
|
export default class RTCDataChannel extends EventTarget implements globalThis.RTCDataChannel {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
this.#dataChannel.sendMessageBinary(data);
|
|
7
|
+
#dataChannel: DataChannel;
|
|
8
|
+
#readyState: globalThis.RTCDataChannelState;
|
|
9
|
+
#bufferedAmountLowThreshold: number;
|
|
10
|
+
#binaryType: BinaryType;
|
|
11
|
+
#maxPacketLifeTime: number | null;
|
|
12
|
+
#maxRetransmits: number | null;
|
|
13
|
+
#negotiated: boolean;
|
|
14
|
+
#ordered: boolean;
|
|
15
|
+
#id: number;
|
|
16
|
+
#label: string;
|
|
17
|
+
#protocol: string;
|
|
18
|
+
|
|
19
|
+
#closeRequested = false;
|
|
20
|
+
|
|
21
|
+
// events
|
|
22
|
+
onbufferedamountlow: globalThis.RTCDataChannel['onbufferedamountlow'] = null;
|
|
23
|
+
onclose: globalThis.RTCDataChannel['onclose'] = null;
|
|
24
|
+
onclosing: globalThis.RTCDataChannel['onclosing'] = null;
|
|
25
|
+
onerror: globalThis.RTCDataChannel['onerror'] = null;
|
|
26
|
+
onmessage: globalThis.RTCDataChannel['onmessage'] = null;
|
|
27
|
+
onopen: globalThis.RTCDataChannel['onopen'] = null;
|
|
28
|
+
|
|
29
|
+
constructor(dataChannel: DataChannel, opts: globalThis.RTCDataChannelInit = {}) {
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
this.#dataChannel = dataChannel;
|
|
33
|
+
this.#binaryType = 'blob';
|
|
34
|
+
this.#readyState = this.#dataChannel.isOpen() ? 'open' : 'connecting';
|
|
35
|
+
this.#bufferedAmountLowThreshold = 0;
|
|
36
|
+
this.#maxPacketLifeTime = opts.maxPacketLifeTime ?? null;
|
|
37
|
+
this.#maxRetransmits = opts.maxRetransmits ?? null;
|
|
38
|
+
this.#negotiated = opts.negotiated ?? false;
|
|
39
|
+
this.#ordered = opts.ordered ?? true;
|
|
40
|
+
this.#id = this.#dataChannel.getId();
|
|
41
|
+
this.#label = this.#dataChannel.getLabel();
|
|
42
|
+
this.#protocol = this.#dataChannel.getProtocol();
|
|
43
|
+
|
|
44
|
+
// forward dataChannel events
|
|
45
|
+
this.#dataChannel.onOpen(() => {
|
|
46
|
+
this.#readyState = 'open';
|
|
47
|
+
this.dispatchEvent(new Event('open', {}));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
this.#dataChannel.onClosed(() => {
|
|
51
|
+
// Simulate closing event
|
|
52
|
+
if (!this.#closeRequested) {
|
|
53
|
+
this.#readyState = 'closing';
|
|
54
|
+
this.dispatchEvent(new Event('closing'));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setImmediate(() => {
|
|
58
|
+
this.#readyState = 'closed';
|
|
59
|
+
this.dispatchEvent(new Event('close'));
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this.#dataChannel.onError((msg) => {
|
|
64
|
+
this.dispatchEvent(
|
|
65
|
+
new RTCErrorEvent('error', {
|
|
66
|
+
error: new RTCError(
|
|
67
|
+
{
|
|
68
|
+
errorDetail: 'data-channel-failure',
|
|
69
|
+
},
|
|
70
|
+
msg,
|
|
71
|
+
),
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this.#dataChannel.onBufferedAmountLow(() => {
|
|
77
|
+
this.dispatchEvent(new Event('bufferedamountlow'));
|
|
78
|
+
});
|
|
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);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.dispatchEvent(new MessageEvent('message', { data }));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// forward events to properties
|
|
90
|
+
this.addEventListener('message', (e) => {
|
|
91
|
+
if (this.onmessage) this.onmessage(e as MessageEvent);
|
|
92
|
+
});
|
|
93
|
+
this.addEventListener('bufferedamountlow', (e) => {
|
|
94
|
+
if (this.onbufferedamountlow) this.onbufferedamountlow(e);
|
|
95
|
+
});
|
|
96
|
+
this.addEventListener('error', (e) => {
|
|
97
|
+
if (this.onerror) this.onerror(e as RTCErrorEvent);
|
|
98
|
+
});
|
|
99
|
+
this.addEventListener('close', (e) => {
|
|
100
|
+
if (this.onclose) this.onclose(e);
|
|
101
|
+
});
|
|
102
|
+
this.addEventListener('closing', (e) => {
|
|
103
|
+
if (this.onclosing) this.onclosing(e);
|
|
104
|
+
});
|
|
105
|
+
this.addEventListener('open', (e) => {
|
|
106
|
+
if (this.onopen) this.onopen(e);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
set binaryType(type) {
|
|
111
|
+
if (type !== 'blob' && type !== 'arraybuffer') {
|
|
112
|
+
throw new DOMException(
|
|
113
|
+
"Failed to set the 'binaryType' property on 'RTCDataChannel': Unknown binary type : " +
|
|
114
|
+
type,
|
|
115
|
+
'TypeMismatchError',
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
this.#binaryType = type;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get binaryType(): BinaryType {
|
|
122
|
+
return this.#binaryType;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
get bufferedAmount(): number {
|
|
126
|
+
return this.#dataChannel.bufferedAmount();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get bufferedAmountLowThreshold(): number {
|
|
130
|
+
return this.#bufferedAmountLowThreshold;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
set bufferedAmountLowThreshold(value) {
|
|
134
|
+
const number = Number(value) || 0;
|
|
135
|
+
this.#bufferedAmountLowThreshold = number;
|
|
136
|
+
this.#dataChannel.setBufferedAmountLowThreshold(number);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
get id(): number | null {
|
|
140
|
+
return this.#id;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
get label(): string {
|
|
144
|
+
return this.#label;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get maxPacketLifeTime(): number | null {
|
|
148
|
+
return this.#maxPacketLifeTime;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
get maxRetransmits(): number | null {
|
|
152
|
+
return this.#maxRetransmits;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get negotiated(): boolean {
|
|
156
|
+
return this.#negotiated;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
get ordered(): boolean {
|
|
160
|
+
return this.#ordered;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get protocol(): string {
|
|
164
|
+
return this.#protocol;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get readyState(): globalThis.RTCDataChannelState {
|
|
168
|
+
return this.#readyState;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
send(data): void {
|
|
172
|
+
if (this.#readyState !== 'open') {
|
|
173
|
+
throw new exceptions.InvalidStateError(
|
|
174
|
+
"Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'",
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Needs network error, type error implemented
|
|
179
|
+
if (typeof data === 'string') {
|
|
180
|
+
this.#dataChannel.sendMessage(data);
|
|
181
|
+
} else if (data instanceof Blob) {
|
|
182
|
+
data.arrayBuffer().then((ab) => {
|
|
183
|
+
if (process?.versions?.bun) {
|
|
184
|
+
this.#dataChannel.sendMessageBinary(Buffer.from(ab));
|
|
185
185
|
} else {
|
|
186
|
-
|
|
187
|
-
this.#dataChannel.sendMessageBinary(Buffer.from(data));
|
|
188
|
-
} else {
|
|
189
|
-
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
|
|
190
|
-
}
|
|
186
|
+
this.#dataChannel.sendMessageBinary(new Uint8Array(ab));
|
|
191
187
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
188
|
+
});
|
|
189
|
+
} else if (data instanceof Uint8Array) {
|
|
190
|
+
this.#dataChannel.sendMessageBinary(data);
|
|
191
|
+
} else {
|
|
192
|
+
if (process?.versions?.bun) {
|
|
193
|
+
this.#dataChannel.sendMessageBinary(Buffer.from(data));
|
|
194
|
+
} else {
|
|
195
|
+
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
close(): void {
|
|
201
|
+
this.#closeRequested = true;
|
|
202
|
+
setImmediate(() => {
|
|
203
|
+
this.#dataChannel.close();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
200
206
|
}
|