node-rtc-connection 1.0.19 → 2.0.4

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 (64) hide show
  1. package/README.md +94 -85
  2. package/dist/index.cjs +20 -5606
  3. package/dist/index.mjs +25 -5598
  4. package/dist/types/crypto/der.d.ts +107 -0
  5. package/dist/types/crypto/x509.d.ts +56 -0
  6. package/dist/types/datachannel/RTCDataChannel.d.ts +179 -0
  7. package/dist/types/dtls/RTCCertificate.d.ts +163 -0
  8. package/dist/types/dtls/cipher.d.ts +81 -0
  9. package/dist/types/dtls/connection.d.ts +81 -0
  10. package/dist/types/dtls/prf.d.ts +29 -0
  11. package/dist/types/dtls/protocol.d.ts +127 -0
  12. package/dist/types/foundation/ByteBufferQueue.d.ts +71 -0
  13. package/dist/types/foundation/RTCError.d.ts +152 -0
  14. package/dist/types/ice/RTCIceCandidate.d.ts +161 -0
  15. package/dist/types/ice/ice-agent.d.ts +154 -0
  16. package/dist/types/ice/stun-message.d.ts +92 -0
  17. package/dist/types/index.d.ts +29 -0
  18. package/dist/types/peerconnection/RTCPeerConnection.d.ts +74 -0
  19. package/dist/types/sctp/association.d.ts +77 -0
  20. package/dist/types/sctp/chunks.d.ts +200 -0
  21. package/dist/types/sctp/crc32c.d.ts +24 -0
  22. package/dist/types/sctp/datachannel-manager.d.ts +51 -0
  23. package/dist/types/sctp/dcep.d.ts +56 -0
  24. package/dist/types/sdp/RTCSessionDescription.d.ts +73 -0
  25. package/dist/types/sdp/sdp-utils.d.ts +103 -0
  26. package/dist/types/stun/stun-client.d.ts +119 -0
  27. package/dist/types/transport-stack.d.ts +68 -0
  28. package/package.json +26 -21
  29. package/src/crypto/der.ts +205 -0
  30. package/src/crypto/x509.ts +146 -0
  31. package/src/datachannel/RTCDataChannel.ts +388 -0
  32. package/src/dtls/RTCCertificate.ts +396 -0
  33. package/src/dtls/cipher.ts +198 -0
  34. package/src/dtls/connection.ts +974 -0
  35. package/src/dtls/prf.ts +62 -0
  36. package/src/dtls/protocol.ts +204 -0
  37. package/src/foundation/{ByteBufferQueue.js → ByteBufferQueue.ts} +74 -72
  38. package/src/foundation/{RTCError.js → RTCError.ts} +110 -60
  39. package/src/ice/{RTCIceCandidate.js → RTCIceCandidate.ts} +140 -92
  40. package/src/ice/ice-agent.ts +609 -0
  41. package/src/ice/stun-message.ts +260 -0
  42. package/src/index.ts +72 -0
  43. package/src/peerconnection/RTCPeerConnection.ts +430 -0
  44. package/src/sctp/association.ts +523 -0
  45. package/src/sctp/chunks.ts +350 -0
  46. package/src/sctp/crc32c.ts +57 -0
  47. package/src/sctp/datachannel-manager.ts +187 -0
  48. package/src/sctp/dcep.ts +94 -0
  49. package/src/sdp/{RTCSessionDescription.js → RTCSessionDescription.ts} +42 -29
  50. package/src/sdp/sdp-utils.ts +229 -0
  51. package/src/stun/{stun-client.js → stun-client.ts} +346 -187
  52. package/src/transport-stack.ts +165 -0
  53. package/dist/index.cjs.map +0 -1
  54. package/dist/index.mjs.map +0 -1
  55. package/src/datachannel/RTCDataChannel.js +0 -354
  56. package/src/dtls/RTCCertificate.js +0 -310
  57. package/src/dtls/RTCDtlsTransport.js +0 -247
  58. package/src/ice/RTCIceTransport.js +0 -1018
  59. package/src/index.d.ts +0 -400
  60. package/src/index.js +0 -92
  61. package/src/network/network-transport.js +0 -478
  62. package/src/peerconnection/RTCPeerConnection.js +0 -875
  63. package/src/sctp/RTCSctpTransport.js +0 -253
  64. package/src/sdp/sdp-utils.js +0 -224
@@ -1,247 +0,0 @@
1
- /**
2
- * @file RTCDtlsTransport.js
3
- * @description DTLS transport implementation for WebRTC security layer.
4
- * @module dtls/RTCDtlsTransport
5
- *
6
- * Ported from Chromium's RTCDtlsTransport implementation:
7
- * - cc/rtc_dtls_transport.h
8
- * - cc/rtc_dtls_transport.cc
9
- * - cc/rtc_dtls_transport.idl
10
- */
11
-
12
- const EventEmitter = require('events');
13
-
14
- /**
15
- * RTCDtlsTransportState - Current state of the DTLS transport
16
- * @readonly
17
- * @enum {string}
18
- */
19
- const RTCDtlsTransportState = Object.freeze({
20
- NEW: 'new',
21
- CONNECTING: 'connecting',
22
- CONNECTED: 'connected',
23
- CLOSED: 'closed',
24
- FAILED: 'failed'
25
- });
26
-
27
- /**
28
- * @class RTCDtlsTransport
29
- * @extends EventEmitter
30
- * @description Represents the DTLS transport layer providing encryption for WebRTC.
31
- * DTLS (Datagram Transport Layer Security) provides security for data transport
32
- * over ICE. This class manages the DTLS handshake and connection state.
33
- *
34
- * Events:
35
- * - 'statechange': Fired when the transport state changes
36
- * - 'error': Fired when an error occurs
37
- *
38
- * @example
39
- * const dtlsTransport = new RTCDtlsTransport(iceTransport);
40
- * dtlsTransport.on('statechange', () => {
41
- * console.log('DTLS state:', dtlsTransport.state);
42
- * });
43
- * dtlsTransport.on('error', (error) => {
44
- * console.error('DTLS error:', error);
45
- * });
46
- */
47
- class RTCDtlsTransport extends EventEmitter {
48
- /**
49
- * Create an RTCDtlsTransport instance.
50
- * @param {RTCIceTransport} iceTransport - The underlying ICE transport
51
- * @throws {TypeError} If iceTransport is not provided or invalid
52
- */
53
- constructor(iceTransport) {
54
- super();
55
-
56
- if (!iceTransport || typeof iceTransport !== 'object') {
57
- throw new TypeError('iceTransport is required');
58
- }
59
-
60
- // Store the ICE transport
61
- this._iceTransport = iceTransport;
62
-
63
- // Internal state
64
- this._state = RTCDtlsTransportState.NEW;
65
- this._remoteCertificates = [];
66
-
67
- // Closed flag
68
- this._closed = false;
69
- this._closedFromOwner = false;
70
-
71
- // Listen to ICE transport state changes
72
- this._iceTransport.on('statechange', () => {
73
- this._onIceStateChange();
74
- });
75
- }
76
-
77
- /**
78
- * Get the underlying ICE transport.
79
- * @returns {RTCIceTransport} The ICE transport
80
- */
81
- get iceTransport() {
82
- return this._iceTransport;
83
- }
84
-
85
- /**
86
- * Get the current DTLS transport state.
87
- * @returns {string} The transport state
88
- */
89
- get state() {
90
- if (this._closedFromOwner) {
91
- return RTCDtlsTransportState.CLOSED;
92
- }
93
- return this._state;
94
- }
95
-
96
- /**
97
- * Get the remote peer's certificate chain.
98
- * Returns an array of certificates in DER format as ArrayBuffers.
99
- *
100
- * @returns {Array<ArrayBuffer>} Array of remote certificates
101
- */
102
- getRemoteCertificates() {
103
- return this._remoteCertificates.map(cert => {
104
- // Return copies to prevent modification
105
- return cert.slice(0);
106
- });
107
- }
108
-
109
- /**
110
- * Start the DTLS handshake.
111
- * This is called internally when the ICE transport is connected.
112
- * @private
113
- */
114
- _start() {
115
- if (this._state !== RTCDtlsTransportState.NEW) {
116
- return;
117
- }
118
-
119
- this._setState(RTCDtlsTransportState.CONNECTING);
120
-
121
- // With real network transport, DTLS is handled by the network layer
122
- // Transition to connected immediately since we're using raw TCP/UDP
123
- setImmediate(() => {
124
- if (!this._closed && this._state === RTCDtlsTransportState.CONNECTING) {
125
- this._setState(RTCDtlsTransportState.CONNECTED);
126
- }
127
- });
128
- }
129
-
130
- /**
131
- * Close the DTLS transport.
132
- * Transitions to closed state and stops the underlying ICE transport.
133
- */
134
- close() {
135
- if (this._closed) {
136
- return;
137
- }
138
-
139
- this._closedFromOwner = true;
140
-
141
- // Emit state change if not already closed
142
- if (this._state !== RTCDtlsTransportState.CLOSED) {
143
- this.emit('statechange');
144
- }
145
-
146
- // Stop the ICE transport
147
- if (this._iceTransport && !this._iceTransport.isClosed()) {
148
- this._iceTransport.stop();
149
- }
150
-
151
- this._closed = true;
152
- }
153
-
154
- /**
155
- * Internal close method (called when transport fails or times out).
156
- * @param {string} reason - Reason for closing
157
- * @private
158
- */
159
- _close(reason) {
160
- if (this._closed) {
161
- return;
162
- }
163
-
164
- this._closed = true;
165
-
166
- if (reason === 'failed') {
167
- this._setState(RTCDtlsTransportState.FAILED);
168
- } else {
169
- this._setState(RTCDtlsTransportState.CLOSED);
170
- }
171
- }
172
-
173
- /**
174
- * Set the transport state and emit event if changed.
175
- * @param {string} newState - The new state
176
- * @private
177
- */
178
- _setState(newState) {
179
- if (this._state !== newState) {
180
- this._state = newState;
181
- this.emit('statechange');
182
-
183
- // If failed, emit error event
184
- if (newState === RTCDtlsTransportState.FAILED) {
185
- this.emit('error', new Error('DTLS transport failed'));
186
- }
187
- }
188
- }
189
-
190
- /**
191
- * Handle ICE transport state changes.
192
- * @private
193
- */
194
- _onIceStateChange() {
195
- const iceState = this._iceTransport.state;
196
-
197
- // Start DTLS when ICE is connected
198
- if (iceState === 'connected' || iceState === 'completed') {
199
- if (this._state === RTCDtlsTransportState.NEW) {
200
- this._start();
201
- }
202
- }
203
-
204
- // Handle ICE failures
205
- if (iceState === 'failed') {
206
- this._close('failed');
207
- }
208
-
209
- // Handle ICE closure
210
- if (iceState === 'closed') {
211
- this._close('closed');
212
- }
213
- }
214
-
215
- /**
216
- * Set remote certificates (called internally after handshake).
217
- * @param {Array<ArrayBuffer>} certificates - DER-encoded certificates
218
- * @private
219
- */
220
- _setRemoteCertificates(certificates) {
221
- if (!Array.isArray(certificates)) {
222
- return;
223
- }
224
-
225
- this._remoteCertificates = certificates.map(cert => {
226
- // Store copies
227
- if (cert instanceof ArrayBuffer) {
228
- return cert.slice(0);
229
- }
230
- return cert;
231
- });
232
- }
233
-
234
- /**
235
- * Check if the transport is closed.
236
- * @returns {boolean} True if closed, false otherwise
237
- */
238
- isClosed() {
239
- return this._state === RTCDtlsTransportState.CLOSED ||
240
- this._state === RTCDtlsTransportState.FAILED;
241
- }
242
- }
243
-
244
- module.exports = {
245
- RTCDtlsTransport,
246
- RTCDtlsTransportState
247
- };