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.
- package/README.md +94 -85
- package/dist/index.cjs +20 -5606
- package/dist/index.mjs +25 -5598
- package/dist/types/crypto/der.d.ts +107 -0
- package/dist/types/crypto/x509.d.ts +56 -0
- package/dist/types/datachannel/RTCDataChannel.d.ts +179 -0
- package/dist/types/dtls/RTCCertificate.d.ts +163 -0
- package/dist/types/dtls/cipher.d.ts +81 -0
- package/dist/types/dtls/connection.d.ts +81 -0
- package/dist/types/dtls/prf.d.ts +29 -0
- package/dist/types/dtls/protocol.d.ts +127 -0
- package/dist/types/foundation/ByteBufferQueue.d.ts +71 -0
- package/dist/types/foundation/RTCError.d.ts +152 -0
- package/dist/types/ice/RTCIceCandidate.d.ts +161 -0
- package/dist/types/ice/ice-agent.d.ts +154 -0
- package/dist/types/ice/stun-message.d.ts +92 -0
- package/dist/types/index.d.ts +29 -0
- package/dist/types/peerconnection/RTCPeerConnection.d.ts +74 -0
- package/dist/types/sctp/association.d.ts +77 -0
- package/dist/types/sctp/chunks.d.ts +200 -0
- package/dist/types/sctp/crc32c.d.ts +24 -0
- package/dist/types/sctp/datachannel-manager.d.ts +51 -0
- package/dist/types/sctp/dcep.d.ts +56 -0
- package/dist/types/sdp/RTCSessionDescription.d.ts +73 -0
- package/dist/types/sdp/sdp-utils.d.ts +103 -0
- package/dist/types/stun/stun-client.d.ts +119 -0
- package/dist/types/transport-stack.d.ts +68 -0
- package/package.json +26 -21
- package/src/crypto/der.ts +205 -0
- package/src/crypto/x509.ts +146 -0
- package/src/datachannel/RTCDataChannel.ts +388 -0
- package/src/dtls/RTCCertificate.ts +396 -0
- package/src/dtls/cipher.ts +198 -0
- package/src/dtls/connection.ts +974 -0
- package/src/dtls/prf.ts +62 -0
- package/src/dtls/protocol.ts +204 -0
- package/src/foundation/{ByteBufferQueue.js → ByteBufferQueue.ts} +74 -72
- package/src/foundation/{RTCError.js → RTCError.ts} +110 -60
- package/src/ice/{RTCIceCandidate.js → RTCIceCandidate.ts} +140 -92
- package/src/ice/ice-agent.ts +609 -0
- package/src/ice/stun-message.ts +260 -0
- package/src/index.ts +72 -0
- package/src/peerconnection/RTCPeerConnection.ts +430 -0
- package/src/sctp/association.ts +523 -0
- package/src/sctp/chunks.ts +350 -0
- package/src/sctp/crc32c.ts +57 -0
- package/src/sctp/datachannel-manager.ts +187 -0
- package/src/sctp/dcep.ts +94 -0
- package/src/sdp/{RTCSessionDescription.js → RTCSessionDescription.ts} +42 -29
- package/src/sdp/sdp-utils.ts +229 -0
- package/src/stun/{stun-client.js → stun-client.ts} +346 -187
- package/src/transport-stack.ts +165 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/datachannel/RTCDataChannel.js +0 -354
- package/src/dtls/RTCCertificate.js +0 -310
- package/src/dtls/RTCDtlsTransport.js +0 -247
- package/src/ice/RTCIceTransport.js +0 -1018
- package/src/index.d.ts +0 -400
- package/src/index.js +0 -92
- package/src/network/network-transport.js +0 -478
- package/src/peerconnection/RTCPeerConnection.js +0 -875
- package/src/sctp/RTCSctpTransport.js +0 -253
- package/src/sdp/sdp-utils.js +0 -224
package/README.md
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# node-rtc-connection
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/node-rtc-connection)
|
|
4
|
+
[](https://www.npmjs.com/package/node-rtc-connection)
|
|
5
|
+
[](https://github.com/nmhung1210/node-rtc-connection/actions/workflows/test.yml)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
[](./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
|
-
- ✅ **
|
|
8
|
-
- ✅ **
|
|
9
|
-
- ✅ **
|
|
10
|
-
- ✅ **TURN
|
|
11
|
-
- ✅ **
|
|
12
|
-
- ✅ **
|
|
13
|
-
- ✅ **
|
|
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
|
-
###
|
|
194
|
+
### ICE server URLs
|
|
174
195
|
|
|
175
|
-
|
|
196
|
+
ICE server URLs are parsed with query-string support:
|
|
176
197
|
|
|
177
198
|
```javascript
|
|
178
199
|
const config = {
|
|
179
200
|
iceServers: [
|
|
180
|
-
|
|
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.
|
|
198
|
-
'turn:turn.
|
|
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: '
|
|
202
|
-
credential: '
|
|
207
|
+
username: 'user',
|
|
208
|
+
credential: 'pass'
|
|
203
209
|
}
|
|
204
210
|
]
|
|
205
211
|
};
|
|
206
212
|
```
|
|
207
213
|
|
|
208
|
-
**
|
|
209
|
-
|
|
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
|
-
**
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
|
382
|
+
The package includes runnable examples in `examples/`:
|
|
379
383
|
|
|
380
|
-
- **`examples/
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
- **`examples/
|
|
384
|
-
|
|
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
|
-
|
|
389
|
-
node
|
|
390
|
-
node examples/turn-demo.js
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
## Configuration File
|
|
394
|
+
# Node ↔ Node
|
|
395
|
+
npm run example:node
|
|
394
396
|
|
|
395
|
-
|
|
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
|
|
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
|
|
469
|
-
- UDP
|
|
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
|
-
|
|
488
|
+
MIT
|
|
486
489
|
|
|
487
490
|
## Contributing
|
|
488
491
|
|
|
489
|
-
Contributions are welcome! Please
|
|
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
|
|
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.
|