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
package/README.md CHANGED
@@ -1,16 +1,27 @@
1
- # NodeRTC
1
+ # node-rtc-connection
2
2
 
3
- A Node.js WebRTC implementation with full ICE/STUN/TURN support for real peer-to-peer networking.
3
+ [![npm version](https://img.shields.io/npm/v/node-rtc-connection.svg)](https://www.npmjs.com/package/node-rtc-connection)
4
+ [![npm downloads](https://img.shields.io/npm/dm/node-rtc-connection.svg)](https://www.npmjs.com/package/node-rtc-connection)
5
+ [![CI](https://github.com/nmhung1210/node-rtc-connection/actions/workflows/test.yml/badge.svg)](https://github.com/nmhung1210/node-rtc-connection/actions/workflows/test.yml)
6
+ [![Node.js](https://img.shields.io/node/v/node-rtc-connection.svg)](https://nodejs.org)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-3178c6.svg)](https://www.typescriptlang.org/)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
9
+
10
+ A from-scratch, pure-Node.js WebRTC data-channel implementation that
11
+ **interoperates with browsers**. No native dependencies — the entire ICE / DTLS
12
+ / SCTP stack is built on Node's `crypto` and `dgram`. Written in TypeScript;
13
+ ships type declarations.
4
14
 
5
15
  ## Features
6
16
 
7
- - ✅ **Real Network Transport**: Uses actual UDP/TCP sockets for true peer-to-peer connections
8
- - ✅ **ICE Support**: Full Interactive Connectivity Establishment with candidate gathering
9
- - ✅ **STUN Support**: NAT traversal with server reflexive candidates
10
- - ✅ **TURN Support**: Relay candidates for restrictive network environments
11
- - ✅ **Data Channels**: Reliable and ordered data channels for P2P communication
12
- - ✅ **DTLS/SCTP**: Secure transport with DTLS encryption and SCTP for data channels
13
- - ✅ **Standards Compliant**: Follows WebRTC and ICE specifications
17
+ - ✅ **Browser interoperable**: Verified end-to-end against Chromium (Playwright) and OpenSSL
18
+ - ✅ **Real protocols, not stubs**: Genuine DTLS 1.2 handshake + SCTP association over UDP
19
+ - ✅ **ICE** (RFC 8445): connectivity checks with MESSAGE-INTEGRITY, host/srflx/relay candidates
20
+ - ✅ **STUN/TURN** (RFC 5389/5766): NAT traversal and relay for restrictive networks
21
+ - ✅ **DTLS 1.2** (RFC 6347): `ECDHE_ECDSA_AES128_GCM`, mutual auth, self-signed ECDSA certs
22
+ - ✅ **SCTP + DCEP** (RFC 8831/8832): ordered/unordered data channels, string + binary
23
+ - ✅ **W3C API**: familiar `RTCPeerConnection` / `RTCDataChannel` surface
24
+ - ✅ **Pure Node.js, no native deps**; CommonJS + ESM bundles with TypeScript types
14
25
 
15
26
  ## Installation
16
27
 
@@ -18,6 +29,16 @@ A Node.js WebRTC implementation with full ICE/STUN/TURN support for real peer-to
18
29
  npm install node-rtc-connection
19
30
  ```
20
31
 
32
+ Works from both CommonJS and ES modules, and bundles TypeScript declarations:
33
+
34
+ ```javascript
35
+ // CommonJS
36
+ const { RTCPeerConnection } = require('node-rtc-connection');
37
+
38
+ // ES modules / TypeScript
39
+ import { RTCPeerConnection } from 'node-rtc-connection';
40
+ ```
41
+
21
42
  ## Quick Start
22
43
 
23
44
  ### Basic Local Connection (No STUN/TURN)
@@ -170,51 +191,34 @@ const config = {
170
191
  const pc = new RTCPeerConnection(config);
171
192
  ```
172
193
 
173
- ### Query String Parameters in Server URLs
194
+ ### ICE server URLs
174
195
 
175
- The library supports query string parameters in ICE server URLs for advanced configuration:
196
+ ICE server URLs are parsed with query-string support:
176
197
 
177
198
  ```javascript
178
199
  const config = {
179
200
  iceServers: [
180
- // Transport selection
181
- {
182
- urls: 'turn:turn.example.com:3478?transport=udp',
183
- username: 'user',
184
- credential: 'pass'
185
- },
186
-
187
- // Multiple parameters
188
- {
189
- urls: 'turn:turn.example.com:3478?transport=tcp&ttl=86400',
190
- username: 'user',
191
- credential: 'pass'
192
- },
193
-
194
- // Multiple URLs with different transports
201
+ { urls: 'stun:stun.l.google.com:19302' },
195
202
  {
196
203
  urls: [
197
- 'turn:turn.cloudflare.com:3478?transport=udp',
198
- 'turn:turn.cloudflare.com:3478?transport=tcp',
199
- 'turns:turn.cloudflare.com:5349?transport=tcp'
204
+ 'turn:turn.example.com:3478?transport=udp',
205
+ 'turn:turn.example.com:53?transport=udp'
200
206
  ],
201
- username: 'cloudflare_user',
202
- credential: 'cloudflare_pass'
207
+ username: 'user',
208
+ credential: 'pass'
203
209
  }
204
210
  ]
205
211
  };
206
212
  ```
207
213
 
208
- **Supported Query Parameters:**
209
- - `transport=udp|tcp` - Select transport protocol (UDP or TCP)
210
- - `ttl=<seconds>` - Set allocation lifetime for TURN (default: 600)
211
- - Custom parameters - Can be added for vendor-specific features
214
+ **URL format:** `stun:host[:port]` and `turn:host[:port][?transport=udp&...]`.
215
+ The default port is `3478` (`5349` for the `turns:` scheme).
212
216
 
213
- **URL Format Examples:**
214
- - `stun:host:port` - Basic STUN server
215
- - `turn:host:port?transport=udp` - TURN with UDP transport
216
- - `turn:host:port?transport=tcp&custom=value` - Multiple parameters
217
- - `turns:host:port?transport=tcp` - Secure TURN over TLS
217
+ > **Transport support:** connectivity currently uses **UDP only**. STUN
218
+ > reflexive and TURN relay candidates are gathered over UDP; `transport=tcp`
219
+ > and the `turns:` (TLS) scheme are parsed but not yet used for the data path,
220
+ > so list a UDP TURN URL for the relay to work. Unknown query parameters are
221
+ > preserved and ignored.
218
222
 
219
223
  ## Data Channel API
220
224
 
@@ -375,51 +379,29 @@ createPeerConnection().catch(console.error);
375
379
 
376
380
  ## Example Files
377
381
 
378
- The package includes several example files demonstrating different features:
382
+ The package includes runnable examples in `examples/`:
379
383
 
380
- - **`examples/real-networking-demo.js`** - Basic peer-to-peer connection without STUN/TURN
381
- - **`examples/stun-demo.js`** - STUN server usage for NAT traversal
382
- - **`examples/turn-demo.js`** - TURN relay with full peer-to-peer communication
383
- - **`examples/peer-connection-demo.js`** - Simple peer connection setup
384
- - **`examples/simple-datachannel.js`** - Basic data channel usage
384
+ - **`examples/node-to-node.ts`** Two node-rtc-connection peers in one process
385
+ establish a real data channel and exchange string + binary messages. The
386
+ quickest way to see the full ICE/DTLS/SCTP stack work.
387
+ - **`examples/browser-server.ts`** + **`examples/browser-client.html`** A
388
+ Node.js HTTP server that runs a node-rtc-connection peer (the offerer) and serves a chat
389
+ page. A browser opens the page, runs its native `RTCPeerConnection` as the
390
+ answerer, and the two establish a genuine WebRTC data channel over UDP.
385
391
 
386
- Run examples:
392
+ Run them (the examples are TypeScript, run via `tsx`):
387
393
  ```bash
388
- node examples/real-networking-demo.js
389
- node examples/stun-demo.js
390
- node examples/turn-demo.js
391
- ```
392
-
393
- ## Configuration File
394
+ # Node ↔ Node
395
+ npm run example:node
394
396
 
395
- The examples use a `peer.config.json` file for centralized configuration:
396
-
397
- ```json
398
- {
399
- "iceServers": [
400
- { "urls": "stun:stun.l.google.com:19302" }
401
- ],
402
- "localDemo": {
403
- "iceServers": []
404
- },
405
- "stunOnly": {
406
- "iceServers": [
407
- { "urls": "stun:stun.l.google.com:19302" }
408
- ]
409
- },
410
- "turnConfig": {
411
- "iceServers": [
412
- { "urls": "stun:stun.example.com:3478" },
413
- {
414
- "urls": "turn:turn.example.com:3478",
415
- "username": "user",
416
- "credential": "pass"
417
- }
418
- ]
419
- }
420
- }
397
+ # Node Browser then open http://localhost:3000
398
+ npm run example:browser
421
399
  ```
422
400
 
401
+ > The browser example uses plain HTTP for signaling and folds ICE candidates
402
+ > into the SDP (non-trickle) to keep it simple. A production app would typically
403
+ > use WebSockets with trickle ICE.
404
+
423
405
  ## API Reference
424
406
 
425
407
  ### RTCPeerConnection
@@ -449,7 +431,7 @@ new RTCPeerConnection(configuration?)
449
431
  ### RTCDataChannel
450
432
 
451
433
  #### Methods
452
- - `send(data)` - Send data (string or Buffer)
434
+ - `send(data)` - Send `string`, `ArrayBuffer`, a typed array / `ArrayBufferView`, or a Node `Buffer`
453
435
  - `close()` - Close the channel
454
436
 
455
437
  #### Properties
@@ -462,11 +444,12 @@ new RTCPeerConnection(configuration?)
462
444
  - `id` - Channel ID
463
445
  - `readyState` - Current state ('connecting', 'open', 'closing', 'closed')
464
446
  - `bufferedAmount` - Bytes queued to send
447
+ - `binaryType` - `'arraybuffer'` (default) or `'blob'`; controls how received binary frames are delivered
465
448
 
466
449
  ## Requirements
467
450
 
468
- - Node.js 14 or higher
469
- - UDP/TCP network access for ICE connectivity
451
+ - Node.js 18 or higher
452
+ - UDP network access for ICE connectivity (and to a TURN server, if used)
470
453
 
471
454
  ## Setting Up Your Own TURN Server
472
455
 
@@ -480,14 +463,40 @@ apt-get install coturn
480
463
  turnserver -v -L 0.0.0.0 -a -u user:password -r realm
481
464
  ```
482
465
 
466
+ ## Development
467
+
468
+ The project is written in strict TypeScript. Sources live in `src/`; tests in
469
+ `test/` run directly through [`tsx`](https://github.com/privatenumber/tsx) (no
470
+ precompile step).
471
+
472
+ ```bash
473
+ npm run build # rollup → minified dist/ bundles + dist/types/ declarations
474
+ npm run typecheck # strict tsc --noEmit over src + tests
475
+ npm test # full suite (auto-starts a coturn container for the TURN test)
476
+ npm run test:unit # SKIP_INTEGRATION=1 — no Docker / browser / external servers
477
+ npm run test:coverage # full suite under c8
478
+ ```
479
+
480
+ The full test suite proves interoperability against external references:
481
+ DTLS handshakes against `openssl`, an end-to-end data channel against real
482
+ Chromium (via Playwright), and a relay path against a real `coturn` server.
483
+ Integration tests skip gracefully when their dependency (Docker, openssl,
484
+ Chromium) is unavailable or when `SKIP_INTEGRATION=1`.
485
+
483
486
  ## License
484
487
 
485
- BSD-3-Clause
488
+ MIT
486
489
 
487
490
  ## Contributing
488
491
 
489
- Contributions are welcome! Please feel free to submit issues or pull requests.
492
+ Contributions are welcome! Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for
493
+ the development workflow and conventions, and our
494
+ [Code of Conduct](./CODE_OF_CONDUCT.md). Security issues should be reported
495
+ privately — see [SECURITY.md](./SECURITY.md). Release notes live in
496
+ [CHANGELOG.md](./CHANGELOG.md).
490
497
 
491
498
  ## Acknowledgments
492
499
 
493
- This implementation is inspired by and follows the WebRTC standards and specifications, with particular reference to Chromium's WebRTC implementation.
500
+ This is a from-scratch, pure-Node.js implementation that follows the relevant
501
+ IETF RFCs (8445 ICE, 6347 DTLS 1.2, 8831 SCTP-over-DTLS, 8832 DCEP) and the W3C
502
+ WebRTC specification.